blob: ef0fd4830803f00c1142ee7feebb55bf91840b26 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Artur Paszkiewicz3418d032017-03-09 09:59:59 +0100481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 0;
483}
484
NeilBrownd1688a62011-10-11 16:49:52 +1100485static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100486 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
NeilBrownb5663ba2009-03-31 14:39:38 +1100488static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
NeilBrownd1688a62011-10-11 16:49:52 +1100490 struct r5conf *conf = sh->raid_conf;
Shaohua Li566c09c2013-11-14 15:16:17 +1100491 int i, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200493 BUG_ON(atomic_read(&sh->count) != 0);
494 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000495 BUG_ON(stripe_operations_active(sh));
shli@kernel.org59fc6302014-12-15 12:57:03 +1100496 BUG_ON(sh->batch_head);
Dan Williamsd84e0f12007-01-02 13:52:30 -0700497
Dan Williams45b42332007-07-09 11:56:43 -0700498 pr_debug("init_stripe called, stripe %llu\n",
Markus Stockhausenb8e6a152014-08-23 20:19:27 +1000499 (unsigned long long)sector);
Shaohua Li566c09c2013-11-14 15:16:17 +1100500retry:
501 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100502 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100503 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100505 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 sh->state = 0;
507
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800508 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 struct r5dev *dev = &sh->dev[i];
510
Dan Williamsd84e0f12007-01-02 13:52:30 -0700511 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 test_bit(R5_LOCKED, &dev->flags)) {
NeilBrowncc6167b2016-11-02 14:16:50 +1100513 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700515 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000517 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 dev->flags = 0;
Guoqing Jiang27a4ff82017-08-10 16:12:17 +0800520 dev->sector = raid5_compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100522 if (read_seqcount_retry(&conf->gen_lock, seq))
523 goto retry;
shli@kernel.org7a87f432014-12-15 12:57:03 +1100524 sh->overwrite_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800526 sh->cpu = smp_processor_id();
shli@kernel.orgda41ba62014-12-15 12:57:03 +1100527 set_bit(STRIPE_BATCH_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
NeilBrownd1688a62011-10-11 16:49:52 +1100530static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100531 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 struct stripe_head *sh;
534
Dan Williams45b42332007-07-09 11:56:43 -0700535 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800536 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100537 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700539 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return NULL;
541}
542
NeilBrown674806d2010-06-16 17:17:53 +1000543/*
544 * Need to check if array has failed when deciding whether to:
545 * - start an array
546 * - remove non-faulty devices
547 * - add a spare
548 * - allow a reshape
549 * This determination is simple when no reshape is happening.
550 * However if there is a reshape, we need to carefully check
551 * both the before and after sections.
552 * This is because some failed devices may only affect one
553 * of the two sections, and some non-in_sync devices may
554 * be insync in the section most affected by failed devices.
555 */
Song Liu2e38a372017-01-24 10:45:30 -0800556int raid5_calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000557{
NeilBrown908f4fb2011-12-23 10:17:50 +1100558 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000559 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000560
561 rcu_read_lock();
562 degraded = 0;
563 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100564 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000565 if (rdev && test_bit(Faulty, &rdev->flags))
566 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000567 if (!rdev || test_bit(Faulty, &rdev->flags))
568 degraded++;
569 else if (test_bit(In_sync, &rdev->flags))
570 ;
571 else
572 /* not in-sync or faulty.
573 * If the reshape increases the number of devices,
574 * this is being recovered by the reshape, so
575 * this 'previous' section is not in_sync.
576 * If the number of devices is being reduced however,
577 * the device can only be part of the array if
578 * we are reverting a reshape, so this section will
579 * be in-sync.
580 */
581 if (conf->raid_disks >= conf->previous_raid_disks)
582 degraded++;
583 }
584 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100585 if (conf->raid_disks == conf->previous_raid_disks)
586 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000587 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100588 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000589 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100590 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000591 if (rdev && test_bit(Faulty, &rdev->flags))
592 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000593 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100594 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000595 else if (test_bit(In_sync, &rdev->flags))
596 ;
597 else
598 /* not in-sync or faulty.
599 * If reshape increases the number of devices, this
600 * section has already been recovered, else it
601 * almost certainly hasn't.
602 */
603 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100604 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000605 }
606 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100607 if (degraded2 > degraded)
608 return degraded2;
609 return degraded;
610}
611
612static int has_failed(struct r5conf *conf)
613{
614 int degraded;
615
616 if (conf->mddev->reshape_position == MaxSector)
617 return conf->mddev->degraded > conf->max_degraded;
618
Song Liu2e38a372017-01-24 10:45:30 -0800619 degraded = raid5_calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000620 if (degraded > conf->max_degraded)
621 return 1;
622 return 0;
623}
624
Shaohua Li6d036f72015-08-13 14:31:57 -0700625struct stripe_head *
626raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
627 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 struct stripe_head *sh;
Yufen Yuc911c462020-07-18 05:29:07 -0400630 int hash = stripe_hash_locks_hash(conf, sector);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800631 int inc_empty_inactive_list_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Dan Williams45b42332007-07-09 11:56:43 -0700633 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Shaohua Li566c09c2013-11-14 15:16:17 +1100635 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 do {
Yuanhan Liub1b46482015-05-08 18:19:06 +1000638 wait_event_lock_irq(conf->wait_for_quiescent,
NeilBrowna8c906c2009-06-09 14:39:59 +1000639 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100640 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100641 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (!sh) {
NeilBrownedbe83a2015-02-26 12:47:56 +1100643 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100644 sh = get_free_stripe(conf, hash);
Shaohua Li713bc5c2015-05-28 17:33:47 -0700645 if (!sh && !test_bit(R5_DID_ALLOC,
646 &conf->cache_state))
NeilBrownedbe83a2015-02-26 12:47:56 +1100647 set_bit(R5_ALLOC_MORE,
648 &conf->cache_state);
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (noblock && sh == NULL)
651 break;
Song Liua39f7af2016-11-17 15:24:40 -0800652
653 r5c_check_stripe_cache_usage(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (!sh) {
NeilBrown54233992015-02-26 12:21:04 +1100655 set_bit(R5_INACTIVE_BLOCKED,
656 &conf->cache_state);
Song Liua39f7af2016-11-17 15:24:40 -0800657 r5l_wake_reclaim(conf->log, 0);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800658 wait_event_lock_irq(
659 conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +1100660 !list_empty(conf->inactive_list + hash) &&
661 (atomic_read(&conf->active_stripes)
662 < (conf->max_nr_stripes * 3 / 4)
NeilBrown54233992015-02-26 12:21:04 +1100663 || !test_bit(R5_INACTIVE_BLOCKED,
664 &conf->cache_state)),
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800665 *(conf->hash_locks + hash));
NeilBrown54233992015-02-26 12:21:04 +1100666 clear_bit(R5_INACTIVE_BLOCKED,
667 &conf->cache_state);
NeilBrown7da9d452014-01-22 11:45:03 +1100668 } else {
NeilBrownb5663ba2009-03-31 14:39:38 +1100669 init_stripe(sh, sector, previous);
NeilBrown7da9d452014-01-22 11:45:03 +1100670 atomic_inc(&sh->count);
671 }
Shaohua Lie240c182014-04-09 11:27:42 +0800672 } else if (!atomic_inc_not_zero(&sh->count)) {
NeilBrown6d183de2013-11-28 10:55:27 +1100673 spin_lock(&conf->device_lock);
Shaohua Lie240c182014-04-09 11:27:42 +0800674 if (!atomic_read(&sh->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (!test_bit(STRIPE_HANDLE, &sh->state))
676 atomic_inc(&conf->active_stripes);
NeilBrown5af9bef2014-01-14 15:16:10 +1100677 BUG_ON(list_empty(&sh->lru) &&
678 !test_bit(STRIPE_EXPANDING, &sh->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800679 inc_empty_inactive_list_flag = 0;
680 if (!list_empty(conf->inactive_list + hash))
681 inc_empty_inactive_list_flag = 1;
NeilBrown16a53ec2006-06-26 00:27:38 -0700682 list_del_init(&sh->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800683 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
684 atomic_inc(&conf->empty_inactive_list_nr);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800685 if (sh->group) {
686 sh->group->stripes_cnt--;
687 sh->group = NULL;
688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
NeilBrown7da9d452014-01-22 11:45:03 +1100690 atomic_inc(&sh->count);
NeilBrown6d183de2013-11-28 10:55:27 +1100691 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693 } while (sh == NULL);
694
Shaohua Li566c09c2013-11-14 15:16:17 +1100695 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return sh;
697}
698
shli@kernel.org7a87f432014-12-15 12:57:03 +1100699static bool is_full_stripe_write(struct stripe_head *sh)
700{
701 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
702 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
703}
704
shli@kernel.org59fc6302014-12-15 12:57:03 +1100705static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
Christoph Hellwig368ecad2019-04-04 18:56:15 +0200706 __acquires(&sh1->stripe_lock)
707 __acquires(&sh2->stripe_lock)
shli@kernel.org59fc6302014-12-15 12:57:03 +1100708{
shli@kernel.org59fc6302014-12-15 12:57:03 +1100709 if (sh1 > sh2) {
Julia Cartwright3d05f3a2017-04-28 12:41:02 -0500710 spin_lock_irq(&sh2->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100711 spin_lock_nested(&sh1->stripe_lock, 1);
712 } else {
Julia Cartwright3d05f3a2017-04-28 12:41:02 -0500713 spin_lock_irq(&sh1->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100714 spin_lock_nested(&sh2->stripe_lock, 1);
715 }
716}
717
718static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
Christoph Hellwig368ecad2019-04-04 18:56:15 +0200719 __releases(&sh1->stripe_lock)
720 __releases(&sh2->stripe_lock)
shli@kernel.org59fc6302014-12-15 12:57:03 +1100721{
722 spin_unlock(&sh1->stripe_lock);
Julia Cartwright3d05f3a2017-04-28 12:41:02 -0500723 spin_unlock_irq(&sh2->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100724}
725
726/* Only freshly new full stripe normal write stripe can be added to a batch list */
727static bool stripe_can_batch(struct stripe_head *sh)
728{
Shaohua Li9c3e3332015-08-13 14:32:02 -0700729 struct r5conf *conf = sh->raid_conf;
730
Shaohua Lie254de62018-08-29 11:05:42 -0700731 if (raid5_has_log(conf) || raid5_has_ppl(conf))
Shaohua Li9c3e3332015-08-13 14:32:02 -0700732 return false;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100733 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
NeilBrownd0852df52015-05-27 08:43:45 +1000734 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
shli@kernel.org59fc6302014-12-15 12:57:03 +1100735 is_full_stripe_write(sh);
736}
737
738/* we only do back search */
739static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
740{
741 struct stripe_head *head;
742 sector_t head_sector, tmp_sec;
743 int hash;
744 int dd_idx;
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800745 int inc_empty_inactive_list_flag;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100746
shli@kernel.org59fc6302014-12-15 12:57:03 +1100747 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
748 tmp_sec = sh->sector;
749 if (!sector_div(tmp_sec, conf->chunk_sectors))
750 return;
Yufen Yuc911c462020-07-18 05:29:07 -0400751 head_sector = sh->sector - RAID5_STRIPE_SECTORS(conf);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100752
Yufen Yuc911c462020-07-18 05:29:07 -0400753 hash = stripe_hash_locks_hash(conf, head_sector);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100754 spin_lock_irq(conf->hash_locks + hash);
755 head = __find_stripe(conf, head_sector, conf->generation);
756 if (head && !atomic_inc_not_zero(&head->count)) {
757 spin_lock(&conf->device_lock);
758 if (!atomic_read(&head->count)) {
759 if (!test_bit(STRIPE_HANDLE, &head->state))
760 atomic_inc(&conf->active_stripes);
761 BUG_ON(list_empty(&head->lru) &&
762 !test_bit(STRIPE_EXPANDING, &head->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800763 inc_empty_inactive_list_flag = 0;
764 if (!list_empty(conf->inactive_list + hash))
765 inc_empty_inactive_list_flag = 1;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100766 list_del_init(&head->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800767 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
768 atomic_inc(&conf->empty_inactive_list_nr);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100769 if (head->group) {
770 head->group->stripes_cnt--;
771 head->group = NULL;
772 }
773 }
774 atomic_inc(&head->count);
775 spin_unlock(&conf->device_lock);
776 }
777 spin_unlock_irq(conf->hash_locks + hash);
778
779 if (!head)
780 return;
781 if (!stripe_can_batch(head))
782 goto out;
783
784 lock_two_stripes(head, sh);
785 /* clear_batch_ready clear the flag */
786 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
787 goto unlock_out;
788
789 if (sh->batch_head)
790 goto unlock_out;
791
792 dd_idx = 0;
793 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
794 dd_idx++;
Jens Axboe1eff9d32016-08-05 15:35:16 -0600795 if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
Mike Christie796a5cf2016-06-05 14:32:07 -0500796 bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
shli@kernel.org59fc6302014-12-15 12:57:03 +1100797 goto unlock_out;
798
799 if (head->batch_head) {
800 spin_lock(&head->batch_head->batch_lock);
801 /* This batch list is already running */
802 if (!stripe_can_batch(head)) {
803 spin_unlock(&head->batch_head->batch_lock);
804 goto unlock_out;
805 }
Shaohua Li36648472017-08-25 10:40:02 -0700806 /*
807 * We must assign batch_head of this stripe within the
808 * batch_lock, otherwise clear_batch_ready of batch head
809 * stripe could clear BATCH_READY bit of this stripe and
810 * this stripe->batch_head doesn't get assigned, which
811 * could confuse clear_batch_ready for this stripe
812 */
813 sh->batch_head = head->batch_head;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100814
815 /*
816 * at this point, head's BATCH_READY could be cleared, but we
817 * can still add the stripe to batch list
818 */
819 list_add(&sh->batch_list, &head->batch_list);
820 spin_unlock(&head->batch_head->batch_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100821 } else {
822 head->batch_head = head;
823 sh->batch_head = head->batch_head;
824 spin_lock(&head->batch_lock);
825 list_add_tail(&sh->batch_list, &head->batch_list);
826 spin_unlock(&head->batch_lock);
827 }
828
829 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
830 if (atomic_dec_return(&conf->preread_active_stripes)
831 < IO_THRESHOLD)
832 md_wakeup_thread(conf->mddev->thread);
833
NeilBrown2b6b2452015-05-21 15:10:01 +1000834 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
835 int seq = sh->bm_seq;
836 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
837 sh->batch_head->bm_seq > seq)
838 seq = sh->batch_head->bm_seq;
839 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
840 sh->batch_head->bm_seq = seq;
841 }
842
shli@kernel.org59fc6302014-12-15 12:57:03 +1100843 atomic_inc(&sh->count);
844unlock_out:
845 unlock_two_stripes(head, sh);
846out:
Shaohua Li6d036f72015-08-13 14:31:57 -0700847 raid5_release_stripe(head);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100848}
849
NeilBrown05616be2012-05-21 09:27:00 +1000850/* Determine if 'data_offset' or 'new_data_offset' should be used
851 * in this stripe_head.
852 */
853static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
854{
855 sector_t progress = conf->reshape_progress;
856 /* Need a memory barrier to make sure we see the value
857 * of conf->generation, or ->data_offset that was set before
858 * reshape_progress was updated.
859 */
860 smp_rmb();
861 if (progress == MaxSector)
862 return 0;
863 if (sh->generation == conf->generation - 1)
864 return 0;
865 /* We are in a reshape, and this is a new-generation stripe,
866 * so use new_data_offset.
867 */
868 return 1;
869}
870
Shaohua Liaaf9f122017-03-03 22:06:12 -0800871static void dispatch_bio_list(struct bio_list *tmp)
Shaohua Li765d7042017-01-04 09:33:23 -0800872{
Shaohua Li765d7042017-01-04 09:33:23 -0800873 struct bio *bio;
874
Shaohua Liaaf9f122017-03-03 22:06:12 -0800875 while ((bio = bio_list_pop(tmp)))
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200876 submit_bio_noacct(bio);
Shaohua Li765d7042017-01-04 09:33:23 -0800877}
878
Shaohua Liaaf9f122017-03-03 22:06:12 -0800879static int cmp_stripe(void *priv, struct list_head *a, struct list_head *b)
Shaohua Li765d7042017-01-04 09:33:23 -0800880{
Shaohua Liaaf9f122017-03-03 22:06:12 -0800881 const struct r5pending_data *da = list_entry(a,
882 struct r5pending_data, sibling);
883 const struct r5pending_data *db = list_entry(b,
884 struct r5pending_data, sibling);
885 if (da->sector > db->sector)
886 return 1;
887 if (da->sector < db->sector)
888 return -1;
889 return 0;
890}
891
892static void dispatch_defer_bios(struct r5conf *conf, int target,
893 struct bio_list *list)
894{
895 struct r5pending_data *data;
896 struct list_head *first, *next = NULL;
897 int cnt = 0;
898
899 if (conf->pending_data_cnt == 0)
Shaohua Li765d7042017-01-04 09:33:23 -0800900 return;
Shaohua Liaaf9f122017-03-03 22:06:12 -0800901
902 list_sort(NULL, &conf->pending_list, cmp_stripe);
903
904 first = conf->pending_list.next;
905
906 /* temporarily move the head */
907 if (conf->next_pending_data)
908 list_move_tail(&conf->pending_list,
909 &conf->next_pending_data->sibling);
910
911 while (!list_empty(&conf->pending_list)) {
912 data = list_first_entry(&conf->pending_list,
913 struct r5pending_data, sibling);
914 if (&data->sibling == first)
915 first = data->sibling.next;
916 next = data->sibling.next;
917
918 bio_list_merge(list, &data->bios);
919 list_move(&data->sibling, &conf->free_list);
920 cnt++;
921 if (cnt >= target)
922 break;
Shaohua Li765d7042017-01-04 09:33:23 -0800923 }
Shaohua Liaaf9f122017-03-03 22:06:12 -0800924 conf->pending_data_cnt -= cnt;
925 BUG_ON(conf->pending_data_cnt < 0 || cnt < target);
926
927 if (next != &conf->pending_list)
928 conf->next_pending_data = list_entry(next,
929 struct r5pending_data, sibling);
930 else
931 conf->next_pending_data = NULL;
932 /* list isn't empty */
933 if (first != &conf->pending_list)
934 list_move_tail(&conf->pending_list, first);
935}
936
937static void flush_deferred_bios(struct r5conf *conf)
938{
939 struct bio_list tmp = BIO_EMPTY_LIST;
940
941 if (conf->pending_data_cnt == 0)
942 return;
943
Shaohua Li765d7042017-01-04 09:33:23 -0800944 spin_lock(&conf->pending_bios_lock);
Shaohua Liaaf9f122017-03-03 22:06:12 -0800945 dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp);
946 BUG_ON(conf->pending_data_cnt != 0);
Shaohua Li765d7042017-01-04 09:33:23 -0800947 spin_unlock(&conf->pending_bios_lock);
Shaohua Liaaf9f122017-03-03 22:06:12 -0800948
949 dispatch_bio_list(&tmp);
950}
951
952static void defer_issue_bios(struct r5conf *conf, sector_t sector,
953 struct bio_list *bios)
954{
955 struct bio_list tmp = BIO_EMPTY_LIST;
956 struct r5pending_data *ent;
957
958 spin_lock(&conf->pending_bios_lock);
959 ent = list_first_entry(&conf->free_list, struct r5pending_data,
960 sibling);
961 list_move_tail(&ent->sibling, &conf->pending_list);
962 ent->sector = sector;
963 bio_list_init(&ent->bios);
964 bio_list_merge(&ent->bios, bios);
965 conf->pending_data_cnt++;
966 if (conf->pending_data_cnt >= PENDING_IO_MAX)
967 dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp);
968
969 spin_unlock(&conf->pending_bios_lock);
970
971 dispatch_bio_list(&tmp);
Shaohua Li765d7042017-01-04 09:33:23 -0800972}
973
NeilBrown6712ecf2007-09-27 12:47:43 +0200974static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200975raid5_end_read_request(struct bio *bi);
NeilBrown6712ecf2007-09-27 12:47:43 +0200976static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200977raid5_end_write_request(struct bio *bi);
Dan Williams91c00922007-01-02 13:52:30 -0700978
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000979static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700980{
NeilBrownd1688a62011-10-11 16:49:52 +1100981 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700982 int i, disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100983 struct stripe_head *head_sh = sh;
Shaohua Liaaf9f122017-03-03 22:06:12 -0800984 struct bio_list pending_bios = BIO_EMPTY_LIST;
985 bool should_defer;
Dan Williams91c00922007-01-02 13:52:30 -0700986
987 might_sleep();
988
Artur Paszkiewiczff875732017-03-09 09:59:58 +0100989 if (log_stripe(sh, s) == 0)
990 return;
Song Liu1e6d6902016-11-17 15:24:39 -0800991
Shaohua Liaaf9f122017-03-03 22:06:12 -0800992 should_defer = conf->batch_bio_dispatch && conf->group_cnt;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700993
Dan Williams91c00922007-01-02 13:52:30 -0700994 for (i = disks; i--; ) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500995 int op, op_flags = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +1100996 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100997 struct bio *bi, *rbi;
998 struct md_rdev *rdev, *rrdev = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100999
1000 sh = head_sh;
Tejun Heoe9c74692010-09-03 11:56:18 +02001001 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001002 op = REQ_OP_WRITE;
Tejun Heoe9c74692010-09-03 11:56:18 +02001003 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001004 op_flags = REQ_FUA;
Shaohua Li9e4447682012-10-11 13:49:49 +11001005 if (test_bit(R5_Discard, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001006 op = REQ_OP_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +02001007 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001008 op = REQ_OP_READ;
NeilBrown9a3e1102011-12-23 10:17:53 +11001009 else if (test_and_clear_bit(R5_WantReplace,
1010 &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001011 op = REQ_OP_WRITE;
NeilBrown9a3e1102011-12-23 10:17:53 +11001012 replace_only = 1;
1013 } else
Dan Williams91c00922007-01-02 13:52:30 -07001014 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +10001015 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001016 op_flags |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -07001017
shli@kernel.org59fc6302014-12-15 12:57:03 +11001018again:
Dan Williams91c00922007-01-02 13:52:30 -07001019 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +11001020 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -07001021
Dan Williams91c00922007-01-02 13:52:30 -07001022 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +11001023 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +11001024 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
1025 rdev = rcu_dereference(conf->disks[i].rdev);
1026 if (!rdev) {
1027 rdev = rrdev;
1028 rrdev = NULL;
1029 }
Mike Christie796a5cf2016-06-05 14:32:07 -05001030 if (op_is_write(op)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001031 if (replace_only)
1032 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +11001033 if (rdev == rrdev)
1034 /* We raced and saw duplicates */
1035 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +11001036 } else {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001037 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +11001038 rdev = rrdev;
1039 rrdev = NULL;
1040 }
NeilBrown977df362011-12-23 10:17:53 +11001041
Dan Williams91c00922007-01-02 13:52:30 -07001042 if (rdev && test_bit(Faulty, &rdev->flags))
1043 rdev = NULL;
1044 if (rdev)
1045 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +11001046 if (rrdev && test_bit(Faulty, &rrdev->flags))
1047 rrdev = NULL;
1048 if (rrdev)
1049 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -07001050 rcu_read_unlock();
1051
NeilBrown73e92e52011-07-28 11:39:22 +10001052 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +11001053 * need to check for writes. We never accept write errors
1054 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +10001055 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001056 while (op_is_write(op) && rdev &&
NeilBrown73e92e52011-07-28 11:39:22 +10001057 test_bit(WriteErrorSeen, &rdev->flags)) {
1058 sector_t first_bad;
1059 int bad_sectors;
Yufen Yuc911c462020-07-18 05:29:07 -04001060 int bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
NeilBrown73e92e52011-07-28 11:39:22 +10001061 &first_bad, &bad_sectors);
1062 if (!bad)
1063 break;
1064
1065 if (bad < 0) {
1066 set_bit(BlockedBadBlocks, &rdev->flags);
1067 if (!conf->mddev->external &&
Shaohua Li29530792016-12-08 15:48:19 -08001068 conf->mddev->sb_flags) {
NeilBrown73e92e52011-07-28 11:39:22 +10001069 /* It is very unlikely, but we might
1070 * still need to write out the
1071 * bad block log - better give it
1072 * a chance*/
1073 md_check_recovery(conf->mddev);
1074 }
majianpeng18507532012-07-03 12:11:54 +10001075 /*
1076 * Because md_wait_for_blocked_rdev
1077 * will dec nr_pending, we must
1078 * increment it first.
1079 */
1080 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +10001081 md_wait_for_blocked_rdev(rdev, conf->mddev);
1082 } else {
1083 /* Acknowledged bad block - skip the write */
1084 rdev_dec_pending(rdev, conf->mddev);
1085 rdev = NULL;
1086 }
1087 }
1088
Dan Williams91c00922007-01-02 13:52:30 -07001089 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001090 if (s->syncing || s->expanding || s->expanded
1091 || s->replacing)
Yufen Yuc911c462020-07-18 05:29:07 -04001092 md_sync_acct(rdev->bdev, RAID5_STRIPE_SECTORS(conf));
Dan Williams91c00922007-01-02 13:52:30 -07001093
Dan Williams2b7497f2008-06-28 08:31:52 +10001094 set_bit(STRIPE_IO_STARTED, &sh->state);
1095
Christoph Hellwig74d46992017-08-23 19:10:32 +02001096 bio_set_dev(bi, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -05001097 bio_set_op_attrs(bi, op, op_flags);
1098 bi->bi_end_io = op_is_write(op)
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001099 ? raid5_end_write_request
1100 : raid5_end_read_request;
1101 bi->bi_private = sh;
1102
Mike Christie6296b962016-06-05 14:32:21 -05001103 pr_debug("%s: for %llu schedule op %d on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001104 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001105 bi->bi_opf, i);
Dan Williams91c00922007-01-02 13:52:30 -07001106 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001107 if (sh != head_sh)
1108 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001109 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001110 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001111 + rdev->new_data_offset);
1112 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001113 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001114 + rdev->data_offset);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001115 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
Jens Axboe1eff9d32016-08-05 15:35:16 -06001116 bi->bi_opf |= REQ_NOMERGE;
majianpeng3f9e7c12012-07-31 10:04:21 +10001117
Shaohua Lid592a992014-05-21 17:57:44 +08001118 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1119 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
Song Liu86aa1392017-01-12 17:22:41 -08001120
1121 if (!op_is_write(op) &&
1122 test_bit(R5_InJournal, &sh->dev[i].flags))
1123 /*
1124 * issuing read for a page in journal, this
1125 * must be preparing for prexor in rmw; read
1126 * the data into orig_page
1127 */
1128 sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
1129 else
1130 sh->dev[i].vec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001131 bi->bi_vcnt = 1;
Yufen Yuc911c462020-07-18 05:29:07 -04001132 bi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf);
Dan Williams91c00922007-01-02 13:52:30 -07001133 bi->bi_io_vec[0].bv_offset = 0;
Yufen Yuc911c462020-07-18 05:29:07 -04001134 bi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf);
Mariusz Dabrowski2cd259a2018-04-19 19:28:10 +02001135 bi->bi_write_hint = sh->dev[i].write_hint;
1136 if (!rrdev)
Eugene Syromiatnikovf1934892019-09-20 17:58:28 +02001137 sh->dev[i].write_hint = RWH_WRITE_LIFE_NOT_SET;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001138 /*
1139 * If this is discard request, set bi_vcnt 0. We don't
1140 * want to confuse SCSI because SCSI will replace payload
1141 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001142 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001143 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +11001144 if (rrdev)
1145 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001146
1147 if (conf->mddev->gendisk)
Christoph Hellwig74d46992017-08-23 19:10:32 +02001148 trace_block_bio_remap(bi->bi_disk->queue,
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001149 bi, disk_devt(conf->mddev->gendisk),
1150 sh->dev[i].sector);
Shaohua Liaaf9f122017-03-03 22:06:12 -08001151 if (should_defer && op_is_write(op))
1152 bio_list_add(&pending_bios, bi);
1153 else
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001154 submit_bio_noacct(bi);
NeilBrown977df362011-12-23 10:17:53 +11001155 }
1156 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001157 if (s->syncing || s->expanding || s->expanded
1158 || s->replacing)
Yufen Yuc911c462020-07-18 05:29:07 -04001159 md_sync_acct(rrdev->bdev, RAID5_STRIPE_SECTORS(conf));
NeilBrown977df362011-12-23 10:17:53 +11001160
1161 set_bit(STRIPE_IO_STARTED, &sh->state);
1162
Christoph Hellwig74d46992017-08-23 19:10:32 +02001163 bio_set_dev(rbi, rrdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -05001164 bio_set_op_attrs(rbi, op, op_flags);
1165 BUG_ON(!op_is_write(op));
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001166 rbi->bi_end_io = raid5_end_write_request;
1167 rbi->bi_private = sh;
1168
Mike Christie6296b962016-06-05 14:32:21 -05001169 pr_debug("%s: for %llu schedule op %d on "
NeilBrown977df362011-12-23 10:17:53 +11001170 "replacement disc %d\n",
1171 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001172 rbi->bi_opf, i);
NeilBrown977df362011-12-23 10:17:53 +11001173 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001174 if (sh != head_sh)
1175 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001176 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001177 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001178 + rrdev->new_data_offset);
1179 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001180 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001181 + rrdev->data_offset);
Shaohua Lid592a992014-05-21 17:57:44 +08001182 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1183 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1184 sh->dev[i].rvec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001185 rbi->bi_vcnt = 1;
Yufen Yuc911c462020-07-18 05:29:07 -04001186 rbi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf);
NeilBrown977df362011-12-23 10:17:53 +11001187 rbi->bi_io_vec[0].bv_offset = 0;
Yufen Yuc911c462020-07-18 05:29:07 -04001188 rbi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf);
Mariusz Dabrowski2cd259a2018-04-19 19:28:10 +02001189 rbi->bi_write_hint = sh->dev[i].write_hint;
Eugene Syromiatnikovf1934892019-09-20 17:58:28 +02001190 sh->dev[i].write_hint = RWH_WRITE_LIFE_NOT_SET;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001191 /*
1192 * If this is discard request, set bi_vcnt 0. We don't
1193 * want to confuse SCSI because SCSI will replace payload
1194 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001195 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001196 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001197 if (conf->mddev->gendisk)
Christoph Hellwig74d46992017-08-23 19:10:32 +02001198 trace_block_bio_remap(rbi->bi_disk->queue,
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001199 rbi, disk_devt(conf->mddev->gendisk),
1200 sh->dev[i].sector);
Shaohua Liaaf9f122017-03-03 22:06:12 -08001201 if (should_defer && op_is_write(op))
1202 bio_list_add(&pending_bios, rbi);
1203 else
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001204 submit_bio_noacct(rbi);
NeilBrown977df362011-12-23 10:17:53 +11001205 }
1206 if (!rdev && !rrdev) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001207 if (op_is_write(op))
Dan Williams91c00922007-01-02 13:52:30 -07001208 set_bit(STRIPE_DEGRADED, &sh->state);
Mike Christie6296b962016-06-05 14:32:21 -05001209 pr_debug("skip op %d on disc %d for sector %llu\n",
Jens Axboe1eff9d32016-08-05 15:35:16 -06001210 bi->bi_opf, i, (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001211 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1212 set_bit(STRIPE_HANDLE, &sh->state);
1213 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001214
1215 if (!head_sh->batch_head)
1216 continue;
1217 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1218 batch_list);
1219 if (sh != head_sh)
1220 goto again;
Dan Williams91c00922007-01-02 13:52:30 -07001221 }
Shaohua Liaaf9f122017-03-03 22:06:12 -08001222
1223 if (should_defer && !bio_list_empty(&pending_bios))
1224 defer_issue_bios(conf, head_sh->sector, &pending_bios);
Dan Williams91c00922007-01-02 13:52:30 -07001225}
1226
1227static struct dma_async_tx_descriptor *
Shaohua Lid592a992014-05-21 17:57:44 +08001228async_copy_data(int frombio, struct bio *bio, struct page **page,
1229 sector_t sector, struct dma_async_tx_descriptor *tx,
Song Liu1e6d6902016-11-17 15:24:39 -08001230 struct stripe_head *sh, int no_skipcopy)
Dan Williams91c00922007-01-02 13:52:30 -07001231{
Kent Overstreet79886132013-11-23 17:19:00 -08001232 struct bio_vec bvl;
1233 struct bvec_iter iter;
Dan Williams91c00922007-01-02 13:52:30 -07001234 struct page *bio_page;
Dan Williams91c00922007-01-02 13:52:30 -07001235 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -07001236 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -07001237 enum async_tx_flags flags = 0;
Yufen Yuc911c462020-07-18 05:29:07 -04001238 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001239
Kent Overstreet4f024f32013-10-11 15:44:27 -07001240 if (bio->bi_iter.bi_sector >= sector)
1241 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
Dan Williams91c00922007-01-02 13:52:30 -07001242 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001243 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -07001244
Dan Williams0403e382009-09-08 17:42:50 -07001245 if (frombio)
1246 flags |= ASYNC_TX_FENCE;
1247 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1248
Kent Overstreet79886132013-11-23 17:19:00 -08001249 bio_for_each_segment(bvl, bio, iter) {
1250 int len = bvl.bv_len;
Dan Williams91c00922007-01-02 13:52:30 -07001251 int clen;
1252 int b_offset = 0;
1253
1254 if (page_offset < 0) {
1255 b_offset = -page_offset;
1256 page_offset += b_offset;
1257 len -= b_offset;
1258 }
1259
Yufen Yuc911c462020-07-18 05:29:07 -04001260 if (len > 0 && page_offset + len > RAID5_STRIPE_SIZE(conf))
1261 clen = RAID5_STRIPE_SIZE(conf) - page_offset;
Dan Williams91c00922007-01-02 13:52:30 -07001262 else
1263 clen = len;
1264
1265 if (clen > 0) {
Kent Overstreet79886132013-11-23 17:19:00 -08001266 b_offset += bvl.bv_offset;
1267 bio_page = bvl.bv_page;
Shaohua Lid592a992014-05-21 17:57:44 +08001268 if (frombio) {
Yufen Yuc911c462020-07-18 05:29:07 -04001269 if (conf->skip_copy &&
Shaohua Lid592a992014-05-21 17:57:44 +08001270 b_offset == 0 && page_offset == 0 &&
Yufen Yuc911c462020-07-18 05:29:07 -04001271 clen == RAID5_STRIPE_SIZE(conf) &&
Song Liu1e6d6902016-11-17 15:24:39 -08001272 !no_skipcopy)
Shaohua Lid592a992014-05-21 17:57:44 +08001273 *page = bio_page;
1274 else
1275 tx = async_memcpy(*page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001276 b_offset, clen, &submit);
Shaohua Lid592a992014-05-21 17:57:44 +08001277 } else
1278 tx = async_memcpy(bio_page, *page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001279 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001280 }
Dan Williamsa08abd82009-06-03 11:43:59 -07001281 /* chain the operations */
1282 submit.depend_tx = tx;
1283
Dan Williams91c00922007-01-02 13:52:30 -07001284 if (clen < len) /* hit end of page */
1285 break;
1286 page_offset += len;
1287 }
1288
1289 return tx;
1290}
1291
1292static void ops_complete_biofill(void *stripe_head_ref)
1293{
1294 struct stripe_head *sh = stripe_head_ref;
Dan Williamse4d84902007-09-24 10:06:13 -07001295 int i;
Yufen Yuc911c462020-07-18 05:29:07 -04001296 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001297
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001298 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001299 (unsigned long long)sh->sector);
1300
1301 /* clear completed biofills */
1302 for (i = sh->disks; i--; ) {
1303 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001304
1305 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001306 /* and check if we need to reply to a read request,
1307 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001308 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001309 */
1310 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001311 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001312
Dan Williams91c00922007-01-02 13:52:30 -07001313 BUG_ON(!dev->read);
1314 rbi = dev->read;
1315 dev->read = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001316 while (rbi && rbi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04001317 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
1318 rbi2 = r5_next_bio(conf, rbi, dev->sector);
NeilBrown016c76a2017-03-15 14:05:13 +11001319 bio_endio(rbi);
Dan Williams91c00922007-01-02 13:52:30 -07001320 rbi = rbi2;
1321 }
1322 }
1323 }
Dan Williams83de75c2008-06-28 08:31:58 +10001324 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001325
Dan Williamse4d84902007-09-24 10:06:13 -07001326 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001327 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001328}
1329
1330static void ops_run_biofill(struct stripe_head *sh)
1331{
1332 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001333 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001334 int i;
Yufen Yuc911c462020-07-18 05:29:07 -04001335 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001336
shli@kernel.org59fc6302014-12-15 12:57:03 +11001337 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001338 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001339 (unsigned long long)sh->sector);
1340
1341 for (i = sh->disks; i--; ) {
1342 struct r5dev *dev = &sh->dev[i];
1343 if (test_bit(R5_Wantfill, &dev->flags)) {
1344 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001345 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001346 dev->read = rbi = dev->toread;
1347 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001348 spin_unlock_irq(&sh->stripe_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001349 while (rbi && rbi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04001350 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
Shaohua Lid592a992014-05-21 17:57:44 +08001351 tx = async_copy_data(0, rbi, &dev->page,
Song Liu1e6d6902016-11-17 15:24:39 -08001352 dev->sector, tx, sh, 0);
Yufen Yuc911c462020-07-18 05:29:07 -04001353 rbi = r5_next_bio(conf, rbi, dev->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001354 }
1355 }
1356 }
1357
1358 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001359 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1360 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001361}
1362
Dan Williams4e7d2c02009-08-29 19:13:11 -07001363static void mark_target_uptodate(struct stripe_head *sh, int target)
1364{
1365 struct r5dev *tgt;
1366
1367 if (target < 0)
1368 return;
1369
1370 tgt = &sh->dev[target];
1371 set_bit(R5_UPTODATE, &tgt->flags);
1372 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1373 clear_bit(R5_Wantcompute, &tgt->flags);
1374}
1375
Dan Williamsac6b53b2009-07-14 13:40:19 -07001376static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001377{
1378 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001379
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001380 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001381 (unsigned long long)sh->sector);
1382
Dan Williamsac6b53b2009-07-14 13:40:19 -07001383 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001384 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001385 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001386
Dan Williamsecc65c92008-06-28 08:31:57 +10001387 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1388 if (sh->check_state == check_state_compute_run)
1389 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001390 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001391 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001392}
1393
Dan Williamsd6f38f32009-07-14 11:50:52 -07001394/* return a pointer to the address conversion region of the scribble buffer */
Kent Overstreetb330e6a2019-03-11 23:31:06 -07001395static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
Dan Williams91c00922007-01-02 13:52:30 -07001396{
Kent Overstreetb330e6a2019-03-11 23:31:06 -07001397 return percpu->scribble + i * percpu->scribble_obj_size;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001398}
1399
1400/* return a pointer to the address conversion region of the scribble buffer */
Kent Overstreetb330e6a2019-03-11 23:31:06 -07001401static addr_conv_t *to_addr_conv(struct stripe_head *sh,
1402 struct raid5_percpu *percpu, int i)
shli@kernel.org46d5b782014-12-15 12:57:02 +11001403{
Kent Overstreetb330e6a2019-03-11 23:31:06 -07001404 return (void *) (to_addr_page(percpu, i) + sh->disks + 2);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001405}
1406
1407static struct dma_async_tx_descriptor *
1408ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1409{
Dan Williams91c00922007-01-02 13:52:30 -07001410 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001411 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001412 int target = sh->ops.target;
1413 struct r5dev *tgt = &sh->dev[target];
1414 struct page *xor_dest = tgt->page;
1415 int count = 0;
1416 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001417 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001418 int i;
1419
shli@kernel.org59fc6302014-12-15 12:57:03 +11001420 BUG_ON(sh->batch_head);
1421
Dan Williams91c00922007-01-02 13:52:30 -07001422 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001423 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001424 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1425
1426 for (i = disks; i--; )
1427 if (i != target)
1428 xor_srcs[count++] = sh->dev[i].page;
1429
1430 atomic_inc(&sh->count);
1431
Dan Williams0403e382009-09-08 17:42:50 -07001432 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001433 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
Dan Williams91c00922007-01-02 13:52:30 -07001434 if (unlikely(count == 1))
Yufen Yuc911c462020-07-18 05:29:07 -04001435 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0,
1436 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001437 else
Yufen Yuc911c462020-07-18 05:29:07 -04001438 tx = async_xor(xor_dest, xor_srcs, 0, count,
1439 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001440
Dan Williams91c00922007-01-02 13:52:30 -07001441 return tx;
1442}
1443
Dan Williamsac6b53b2009-07-14 13:40:19 -07001444/* set_syndrome_sources - populate source buffers for gen_syndrome
1445 * @srcs - (struct page *) array of size sh->disks
1446 * @sh - stripe_head to parse
1447 *
1448 * Populates srcs in proper layout order for the stripe and returns the
1449 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1450 * destination buffer is recorded in srcs[count] and the Q destination
1451 * is recorded in srcs[count+1]].
1452 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001453static int set_syndrome_sources(struct page **srcs,
1454 struct stripe_head *sh,
1455 int srctype)
Dan Williamsac6b53b2009-07-14 13:40:19 -07001456{
1457 int disks = sh->disks;
1458 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1459 int d0_idx = raid6_d0(sh);
1460 int count;
1461 int i;
1462
1463 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001464 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001465
1466 count = 0;
1467 i = d0_idx;
1468 do {
1469 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001470 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001471
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001472 if (i == sh->qd_idx || i == sh->pd_idx ||
1473 (srctype == SYNDROME_SRC_ALL) ||
1474 (srctype == SYNDROME_SRC_WANT_DRAIN &&
Song Liu1e6d6902016-11-17 15:24:39 -08001475 (test_bit(R5_Wantdrain, &dev->flags) ||
1476 test_bit(R5_InJournal, &dev->flags))) ||
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001477 (srctype == SYNDROME_SRC_WRITTEN &&
Song Liu09777622017-03-13 13:44:35 -07001478 (dev->written ||
1479 test_bit(R5_InJournal, &dev->flags)))) {
Song Liu1e6d6902016-11-17 15:24:39 -08001480 if (test_bit(R5_InJournal, &dev->flags))
1481 srcs[slot] = sh->dev[i].orig_page;
1482 else
1483 srcs[slot] = sh->dev[i].page;
1484 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001485 i = raid6_next_disk(i, disks);
1486 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001487
NeilBrowne4424fe2009-10-16 16:27:34 +11001488 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001489}
1490
1491static struct dma_async_tx_descriptor *
1492ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1493{
1494 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001495 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001496 int target;
1497 int qd_idx = sh->qd_idx;
1498 struct dma_async_tx_descriptor *tx;
1499 struct async_submit_ctl submit;
1500 struct r5dev *tgt;
1501 struct page *dest;
1502 int i;
1503 int count;
1504
shli@kernel.org59fc6302014-12-15 12:57:03 +11001505 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001506 if (sh->ops.target < 0)
1507 target = sh->ops.target2;
1508 else if (sh->ops.target2 < 0)
1509 target = sh->ops.target;
1510 else
1511 /* we should only have one valid target */
1512 BUG();
1513 BUG_ON(target < 0);
1514 pr_debug("%s: stripe %llu block: %d\n",
1515 __func__, (unsigned long long)sh->sector, target);
1516
1517 tgt = &sh->dev[target];
1518 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1519 dest = tgt->page;
1520
1521 atomic_inc(&sh->count);
1522
1523 if (target == qd_idx) {
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001524 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001525 blocks[count] = NULL; /* regenerating p is not necessary */
1526 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001527 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1528 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001529 to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04001530 tx = async_gen_syndrome(blocks, 0, count+2,
1531 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001532 } else {
1533 /* Compute any data- or p-drive using XOR */
1534 count = 0;
1535 for (i = disks; i-- ; ) {
1536 if (i == target || i == qd_idx)
1537 continue;
1538 blocks[count++] = sh->dev[i].page;
1539 }
1540
Dan Williams0403e382009-09-08 17:42:50 -07001541 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1542 NULL, ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001543 to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04001544 tx = async_xor(dest, blocks, 0, count,
1545 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001546 }
1547
1548 return tx;
1549}
1550
1551static struct dma_async_tx_descriptor *
1552ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1553{
1554 int i, count, disks = sh->disks;
1555 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1556 int d0_idx = raid6_d0(sh);
1557 int faila = -1, failb = -1;
1558 int target = sh->ops.target;
1559 int target2 = sh->ops.target2;
1560 struct r5dev *tgt = &sh->dev[target];
1561 struct r5dev *tgt2 = &sh->dev[target2];
1562 struct dma_async_tx_descriptor *tx;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001563 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001564 struct async_submit_ctl submit;
1565
shli@kernel.org59fc6302014-12-15 12:57:03 +11001566 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001567 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1568 __func__, (unsigned long long)sh->sector, target, target2);
1569 BUG_ON(target < 0 || target2 < 0);
1570 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1571 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1572
Dan Williams6c910a72009-09-16 12:24:54 -07001573 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001574 * slot number conversion for 'faila' and 'failb'
1575 */
1576 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001577 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001578 count = 0;
1579 i = d0_idx;
1580 do {
1581 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1582
1583 blocks[slot] = sh->dev[i].page;
1584
1585 if (i == target)
1586 faila = slot;
1587 if (i == target2)
1588 failb = slot;
1589 i = raid6_next_disk(i, disks);
1590 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001591
1592 BUG_ON(faila == failb);
1593 if (failb < faila)
1594 swap(faila, failb);
1595 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1596 __func__, (unsigned long long)sh->sector, faila, failb);
1597
1598 atomic_inc(&sh->count);
1599
1600 if (failb == syndrome_disks+1) {
1601 /* Q disk is one of the missing disks */
1602 if (faila == syndrome_disks) {
1603 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001604 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1605 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001606 to_addr_conv(sh, percpu, 0));
NeilBrowne4424fe2009-10-16 16:27:34 +11001607 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001608 RAID5_STRIPE_SIZE(sh->raid_conf),
1609 &submit);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001610 } else {
1611 struct page *dest;
1612 int data_target;
1613 int qd_idx = sh->qd_idx;
1614
1615 /* Missing D+Q: recompute D from P, then recompute Q */
1616 if (target == qd_idx)
1617 data_target = target2;
1618 else
1619 data_target = target;
1620
1621 count = 0;
1622 for (i = disks; i-- ; ) {
1623 if (i == data_target || i == qd_idx)
1624 continue;
1625 blocks[count++] = sh->dev[i].page;
1626 }
1627 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001628 init_async_submit(&submit,
1629 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1630 NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001631 to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04001632 tx = async_xor(dest, blocks, 0, count,
1633 RAID5_STRIPE_SIZE(sh->raid_conf),
Dan Williamsac6b53b2009-07-14 13:40:19 -07001634 &submit);
1635
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001636 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williams0403e382009-09-08 17:42:50 -07001637 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1638 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001639 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001640 return async_gen_syndrome(blocks, 0, count+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001641 RAID5_STRIPE_SIZE(sh->raid_conf),
1642 &submit);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001643 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001644 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001645 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1646 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001647 to_addr_conv(sh, percpu, 0));
Dan Williams6c910a72009-09-16 12:24:54 -07001648 if (failb == syndrome_disks) {
1649 /* We're missing D+P. */
1650 return async_raid6_datap_recov(syndrome_disks+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001651 RAID5_STRIPE_SIZE(sh->raid_conf),
1652 faila,
1653 blocks, &submit);
Dan Williams6c910a72009-09-16 12:24:54 -07001654 } else {
1655 /* We're missing D+D. */
1656 return async_raid6_2data_recov(syndrome_disks+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001657 RAID5_STRIPE_SIZE(sh->raid_conf),
1658 faila, failb,
1659 blocks, &submit);
Dan Williams6c910a72009-09-16 12:24:54 -07001660 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001661 }
1662}
1663
Dan Williams91c00922007-01-02 13:52:30 -07001664static void ops_complete_prexor(void *stripe_head_ref)
1665{
1666 struct stripe_head *sh = stripe_head_ref;
1667
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001668 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001669 (unsigned long long)sh->sector);
Song Liu1e6d6902016-11-17 15:24:39 -08001670
1671 if (r5c_is_writeback(sh->raid_conf->log))
1672 /*
1673 * raid5-cache write back uses orig_page during prexor.
1674 * After prexor, it is time to free orig_page
1675 */
1676 r5c_release_extra_page(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001677}
1678
1679static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001680ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1681 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001682{
Dan Williams91c00922007-01-02 13:52:30 -07001683 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001684 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001685 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001686 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001687
1688 /* existing parity data subtracted */
1689 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1690
shli@kernel.org59fc6302014-12-15 12:57:03 +11001691 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001692 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001693 (unsigned long long)sh->sector);
1694
1695 for (i = disks; i--; ) {
1696 struct r5dev *dev = &sh->dev[i];
1697 /* Only process blocks that are known to be uptodate */
Song Liu1e6d6902016-11-17 15:24:39 -08001698 if (test_bit(R5_InJournal, &dev->flags))
1699 xor_srcs[count++] = dev->orig_page;
1700 else if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001701 xor_srcs[count++] = dev->page;
1702 }
1703
Dan Williams0403e382009-09-08 17:42:50 -07001704 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001705 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04001706 tx = async_xor(xor_dest, xor_srcs, 0, count,
1707 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001708
1709 return tx;
1710}
1711
1712static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001713ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1714 struct dma_async_tx_descriptor *tx)
1715{
1716 struct page **blocks = to_addr_page(percpu, 0);
1717 int count;
1718 struct async_submit_ctl submit;
1719
1720 pr_debug("%s: stripe %llu\n", __func__,
1721 (unsigned long long)sh->sector);
1722
1723 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN);
1724
1725 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1726 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04001727 tx = async_gen_syndrome(blocks, 0, count+2,
1728 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001729
1730 return tx;
1731}
1732
1733static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001734ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001735{
Song Liu1e6d6902016-11-17 15:24:39 -08001736 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001737 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001738 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001739 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -07001740
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001741 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001742 (unsigned long long)sh->sector);
1743
1744 for (i = disks; i--; ) {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001745 struct r5dev *dev;
Dan Williams91c00922007-01-02 13:52:30 -07001746 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001747
shli@kernel.org59fc6302014-12-15 12:57:03 +11001748 sh = head_sh;
1749 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001750 struct bio *wbi;
1751
shli@kernel.org59fc6302014-12-15 12:57:03 +11001752again:
1753 dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001754 /*
1755 * clear R5_InJournal, so when rewriting a page in
1756 * journal, it is not skipped by r5l_log_stripe()
1757 */
1758 clear_bit(R5_InJournal, &dev->flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10001759 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001760 chosen = dev->towrite;
1761 dev->towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11001762 sh->overwrite_disks = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001763 BUG_ON(dev->written);
1764 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001765 spin_unlock_irq(&sh->stripe_lock);
Shaohua Lid592a992014-05-21 17:57:44 +08001766 WARN_ON(dev->page != dev->orig_page);
Dan Williams91c00922007-01-02 13:52:30 -07001767
Kent Overstreet4f024f32013-10-11 15:44:27 -07001768 while (wbi && wbi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04001769 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001770 if (wbi->bi_opf & REQ_FUA)
Tejun Heoe9c74692010-09-03 11:56:18 +02001771 set_bit(R5_WantFUA, &dev->flags);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001772 if (wbi->bi_opf & REQ_SYNC)
Shaohua Libc0934f2012-05-22 13:55:05 +10001773 set_bit(R5_SyncIO, &dev->flags);
Mike Christie796a5cf2016-06-05 14:32:07 -05001774 if (bio_op(wbi) == REQ_OP_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001775 set_bit(R5_Discard, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08001776 else {
1777 tx = async_copy_data(1, wbi, &dev->page,
Song Liu1e6d6902016-11-17 15:24:39 -08001778 dev->sector, tx, sh,
1779 r5c_is_writeback(conf->log));
1780 if (dev->page != dev->orig_page &&
1781 !r5c_is_writeback(conf->log)) {
Shaohua Lid592a992014-05-21 17:57:44 +08001782 set_bit(R5_SkipCopy, &dev->flags);
1783 clear_bit(R5_UPTODATE, &dev->flags);
1784 clear_bit(R5_OVERWRITE, &dev->flags);
1785 }
1786 }
Yufen Yuc911c462020-07-18 05:29:07 -04001787 wbi = r5_next_bio(conf, wbi, dev->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001788 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001789
1790 if (head_sh->batch_head) {
1791 sh = list_first_entry(&sh->batch_list,
1792 struct stripe_head,
1793 batch_list);
1794 if (sh == head_sh)
1795 continue;
1796 goto again;
1797 }
Dan Williams91c00922007-01-02 13:52:30 -07001798 }
1799 }
1800
1801 return tx;
1802}
1803
Dan Williamsac6b53b2009-07-14 13:40:19 -07001804static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001805{
1806 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001807 int disks = sh->disks;
1808 int pd_idx = sh->pd_idx;
1809 int qd_idx = sh->qd_idx;
1810 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001811 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001812
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001813 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001814 (unsigned long long)sh->sector);
1815
Shaohua Libc0934f2012-05-22 13:55:05 +10001816 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001817 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001818 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001819 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001820 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001821
Dan Williams91c00922007-01-02 13:52:30 -07001822 for (i = disks; i--; ) {
1823 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001824
Tejun Heoe9c74692010-09-03 11:56:18 +02001825 if (dev->written || i == pd_idx || i == qd_idx) {
NeilBrown235b6002017-10-17 16:18:36 +11001826 if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001827 set_bit(R5_UPTODATE, &dev->flags);
NeilBrown235b6002017-10-17 16:18:36 +11001828 if (test_bit(STRIPE_EXPAND_READY, &sh->state))
1829 set_bit(R5_Expanded, &dev->flags);
1830 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001831 if (fua)
1832 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001833 if (sync)
1834 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001835 }
Dan Williams91c00922007-01-02 13:52:30 -07001836 }
1837
Dan Williamsd8ee0722008-06-28 08:32:06 +10001838 if (sh->reconstruct_state == reconstruct_state_drain_run)
1839 sh->reconstruct_state = reconstruct_state_drain_result;
1840 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1841 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1842 else {
1843 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1844 sh->reconstruct_state = reconstruct_state_result;
1845 }
Dan Williams91c00922007-01-02 13:52:30 -07001846
1847 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001848 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001849}
1850
1851static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001852ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1853 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001854{
Dan Williams91c00922007-01-02 13:52:30 -07001855 int disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001856 struct page **xor_srcs;
Dan Williamsa08abd82009-06-03 11:43:59 -07001857 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001858 int count, pd_idx = sh->pd_idx, i;
Dan Williams91c00922007-01-02 13:52:30 -07001859 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001860 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001861 unsigned long flags;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001862 int j = 0;
1863 struct stripe_head *head_sh = sh;
1864 int last_stripe;
Dan Williams91c00922007-01-02 13:52:30 -07001865
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001866 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001867 (unsigned long long)sh->sector);
1868
Shaohua Li620125f2012-10-11 13:49:05 +11001869 for (i = 0; i < sh->disks; i++) {
1870 if (pd_idx == i)
1871 continue;
1872 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1873 break;
1874 }
1875 if (i >= sh->disks) {
1876 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001877 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1878 ops_complete_reconstruct(sh);
1879 return;
1880 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001881again:
1882 count = 0;
1883 xor_srcs = to_addr_page(percpu, j);
Dan Williams91c00922007-01-02 13:52:30 -07001884 /* check if prexor is active which means only process blocks
1885 * that are part of a read-modify-write (written)
1886 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001887 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001888 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001889 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1890 for (i = disks; i--; ) {
1891 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001892 if (head_sh->dev[i].written ||
1893 test_bit(R5_InJournal, &head_sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -07001894 xor_srcs[count++] = dev->page;
1895 }
1896 } else {
1897 xor_dest = sh->dev[pd_idx].page;
1898 for (i = disks; i--; ) {
1899 struct r5dev *dev = &sh->dev[i];
1900 if (i != pd_idx)
1901 xor_srcs[count++] = dev->page;
1902 }
1903 }
1904
Dan Williams91c00922007-01-02 13:52:30 -07001905 /* 1/ if we prexor'd then the dest is reused as a source
1906 * 2/ if we did not prexor then we are redoing the parity
1907 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1908 * for the synchronous xor case
1909 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001910 last_stripe = !head_sh->batch_head ||
1911 list_first_entry(&sh->batch_list,
1912 struct stripe_head, batch_list) == head_sh;
1913 if (last_stripe) {
1914 flags = ASYNC_TX_ACK |
1915 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
Dan Williams91c00922007-01-02 13:52:30 -07001916
shli@kernel.org59fc6302014-12-15 12:57:03 +11001917 atomic_inc(&head_sh->count);
1918 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
1919 to_addr_conv(sh, percpu, j));
1920 } else {
1921 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
1922 init_async_submit(&submit, flags, tx, NULL, NULL,
1923 to_addr_conv(sh, percpu, j));
1924 }
Dan Williams91c00922007-01-02 13:52:30 -07001925
Dan Williamsa08abd82009-06-03 11:43:59 -07001926 if (unlikely(count == 1))
Yufen Yuc911c462020-07-18 05:29:07 -04001927 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0,
1928 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williamsa08abd82009-06-03 11:43:59 -07001929 else
Yufen Yuc911c462020-07-18 05:29:07 -04001930 tx = async_xor(xor_dest, xor_srcs, 0, count,
1931 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001932 if (!last_stripe) {
1933 j++;
1934 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1935 batch_list);
1936 goto again;
1937 }
Dan Williams91c00922007-01-02 13:52:30 -07001938}
1939
Dan Williamsac6b53b2009-07-14 13:40:19 -07001940static void
1941ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1942 struct dma_async_tx_descriptor *tx)
1943{
1944 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001945 struct page **blocks;
1946 int count, i, j = 0;
1947 struct stripe_head *head_sh = sh;
1948 int last_stripe;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001949 int synflags;
1950 unsigned long txflags;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001951
1952 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1953
Shaohua Li620125f2012-10-11 13:49:05 +11001954 for (i = 0; i < sh->disks; i++) {
1955 if (sh->pd_idx == i || sh->qd_idx == i)
1956 continue;
1957 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1958 break;
1959 }
1960 if (i >= sh->disks) {
1961 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001962 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1963 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1964 ops_complete_reconstruct(sh);
1965 return;
1966 }
1967
shli@kernel.org59fc6302014-12-15 12:57:03 +11001968again:
1969 blocks = to_addr_page(percpu, j);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001970
1971 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1972 synflags = SYNDROME_SRC_WRITTEN;
1973 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
1974 } else {
1975 synflags = SYNDROME_SRC_ALL;
1976 txflags = ASYNC_TX_ACK;
1977 }
1978
1979 count = set_syndrome_sources(blocks, sh, synflags);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001980 last_stripe = !head_sh->batch_head ||
1981 list_first_entry(&sh->batch_list,
1982 struct stripe_head, batch_list) == head_sh;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001983
shli@kernel.org59fc6302014-12-15 12:57:03 +11001984 if (last_stripe) {
1985 atomic_inc(&head_sh->count);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001986 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
shli@kernel.org59fc6302014-12-15 12:57:03 +11001987 head_sh, to_addr_conv(sh, percpu, j));
1988 } else
1989 init_async_submit(&submit, 0, tx, NULL, NULL,
1990 to_addr_conv(sh, percpu, j));
Yufen Yuc911c462020-07-18 05:29:07 -04001991 tx = async_gen_syndrome(blocks, 0, count+2,
1992 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001993 if (!last_stripe) {
1994 j++;
1995 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1996 batch_list);
1997 goto again;
1998 }
Dan Williams91c00922007-01-02 13:52:30 -07001999}
2000
2001static void ops_complete_check(void *stripe_head_ref)
2002{
2003 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07002004
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002005 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07002006 (unsigned long long)sh->sector);
2007
Dan Williamsecc65c92008-06-28 08:31:57 +10002008 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07002009 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002010 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07002011}
2012
Dan Williamsac6b53b2009-07-14 13:40:19 -07002013static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07002014{
Dan Williams91c00922007-01-02 13:52:30 -07002015 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002016 int pd_idx = sh->pd_idx;
2017 int qd_idx = sh->qd_idx;
2018 struct page *xor_dest;
shli@kernel.org46d5b782014-12-15 12:57:02 +11002019 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07002020 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07002021 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002022 int count;
2023 int i;
Dan Williams91c00922007-01-02 13:52:30 -07002024
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002025 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07002026 (unsigned long long)sh->sector);
2027
shli@kernel.org59fc6302014-12-15 12:57:03 +11002028 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002029 count = 0;
2030 xor_dest = sh->dev[pd_idx].page;
2031 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07002032 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07002033 if (i == pd_idx || i == qd_idx)
2034 continue;
2035 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07002036 }
2037
Dan Williamsd6f38f32009-07-14 11:50:52 -07002038 init_async_submit(&submit, 0, NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11002039 to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04002040 tx = async_xor_val(xor_dest, xor_srcs, 0, count,
2041 RAID5_STRIPE_SIZE(sh->raid_conf),
Dan Williamsa08abd82009-06-03 11:43:59 -07002042 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07002043
Dan Williams91c00922007-01-02 13:52:30 -07002044 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07002045 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
2046 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07002047}
2048
Dan Williamsac6b53b2009-07-14 13:40:19 -07002049static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
2050{
shli@kernel.org46d5b782014-12-15 12:57:02 +11002051 struct page **srcs = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002052 struct async_submit_ctl submit;
2053 int count;
2054
2055 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
2056 (unsigned long long)sh->sector, checkp);
2057
shli@kernel.org59fc6302014-12-15 12:57:03 +11002058 BUG_ON(sh->batch_head);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002059 count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002060 if (!checkp)
2061 srcs[count] = NULL;
2062
2063 atomic_inc(&sh->count);
2064 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
shli@kernel.org46d5b782014-12-15 12:57:02 +11002065 sh, to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04002066 async_syndrome_val(srcs, 0, count+2,
2067 RAID5_STRIPE_SIZE(sh->raid_conf),
Dan Williamsac6b53b2009-07-14 13:40:19 -07002068 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
2069}
2070
NeilBrown51acbce2013-02-28 09:08:34 +11002071static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07002072{
2073 int overlap_clear = 0, i, disks = sh->disks;
2074 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11002075 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002076 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002077 struct raid5_percpu *percpu;
2078 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07002079
Dan Williamsd6f38f32009-07-14 11:50:52 -07002080 cpu = get_cpu();
2081 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10002082 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07002083 ops_run_biofill(sh);
2084 overlap_clear++;
2085 }
2086
Dan Williams7b3a8712008-06-28 08:32:09 +10002087 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07002088 if (level < 6)
2089 tx = ops_run_compute5(sh, percpu);
2090 else {
2091 if (sh->ops.target2 < 0 || sh->ops.target < 0)
2092 tx = ops_run_compute6_1(sh, percpu);
2093 else
2094 tx = ops_run_compute6_2(sh, percpu);
2095 }
2096 /* terminate the chain if reconstruct is not set to be run */
2097 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10002098 async_tx_ack(tx);
2099 }
Dan Williams91c00922007-01-02 13:52:30 -07002100
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002101 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
2102 if (level < 6)
2103 tx = ops_run_prexor5(sh, percpu, tx);
2104 else
2105 tx = ops_run_prexor6(sh, percpu, tx);
2106 }
Dan Williams91c00922007-01-02 13:52:30 -07002107
Artur Paszkiewiczae1713e2017-04-04 13:13:58 +02002108 if (test_bit(STRIPE_OP_PARTIAL_PARITY, &ops_request))
2109 tx = ops_run_partial_parity(sh, percpu, tx);
2110
Dan Williams600aa102008-06-28 08:32:05 +10002111 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10002112 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07002113 overlap_clear++;
2114 }
2115
Dan Williamsac6b53b2009-07-14 13:40:19 -07002116 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
2117 if (level < 6)
2118 ops_run_reconstruct5(sh, percpu, tx);
2119 else
2120 ops_run_reconstruct6(sh, percpu, tx);
2121 }
Dan Williams91c00922007-01-02 13:52:30 -07002122
Dan Williamsac6b53b2009-07-14 13:40:19 -07002123 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
2124 if (sh->check_state == check_state_run)
2125 ops_run_check_p(sh, percpu);
2126 else if (sh->check_state == check_state_run_q)
2127 ops_run_check_pq(sh, percpu, 0);
2128 else if (sh->check_state == check_state_run_pq)
2129 ops_run_check_pq(sh, percpu, 1);
2130 else
2131 BUG();
2132 }
Dan Williams91c00922007-01-02 13:52:30 -07002133
shli@kernel.org59fc6302014-12-15 12:57:03 +11002134 if (overlap_clear && !sh->batch_head)
Dan Williams91c00922007-01-02 13:52:30 -07002135 for (i = disks; i--; ) {
2136 struct r5dev *dev = &sh->dev[i];
2137 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2138 wake_up(&sh->raid_conf->wait_for_overlap);
2139 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07002140 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07002141}
2142
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002143static void free_stripe(struct kmem_cache *sc, struct stripe_head *sh)
2144{
2145 if (sh->ppl_page)
2146 __free_page(sh->ppl_page);
2147 kmem_cache_free(sc, sh);
2148}
2149
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002150static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002151 int disks, struct r5conf *conf)
NeilBrownf18c1a32015-05-08 18:19:04 +10002152{
2153 struct stripe_head *sh;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002154 int i;
NeilBrownf18c1a32015-05-08 18:19:04 +10002155
2156 sh = kmem_cache_zalloc(sc, gfp);
2157 if (sh) {
2158 spin_lock_init(&sh->stripe_lock);
2159 spin_lock_init(&sh->batch_lock);
2160 INIT_LIST_HEAD(&sh->batch_list);
2161 INIT_LIST_HEAD(&sh->lru);
Song Liua39f7af2016-11-17 15:24:40 -08002162 INIT_LIST_HEAD(&sh->r5c);
Song Liud7bd3982016-11-23 22:50:39 -08002163 INIT_LIST_HEAD(&sh->log_list);
NeilBrownf18c1a32015-05-08 18:19:04 +10002164 atomic_set(&sh->count, 1);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002165 sh->raid_conf = conf;
Song Liua39f7af2016-11-17 15:24:40 -08002166 sh->log_start = MaxSector;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002167 for (i = 0; i < disks; i++) {
2168 struct r5dev *dev = &sh->dev[i];
2169
Ming Lei3a83f462016-11-22 08:57:21 -07002170 bio_init(&dev->req, &dev->vec, 1);
2171 bio_init(&dev->rreq, &dev->rvec, 1);
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002172 }
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002173
2174 if (raid5_has_ppl(conf)) {
2175 sh->ppl_page = alloc_page(gfp);
2176 if (!sh->ppl_page) {
2177 free_stripe(sc, sh);
2178 sh = NULL;
2179 }
2180 }
NeilBrownf18c1a32015-05-08 18:19:04 +10002181 }
2182 return sh;
2183}
NeilBrown486f0642015-02-25 12:10:35 +11002184static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 struct stripe_head *sh;
NeilBrownf18c1a32015-05-08 18:19:04 +10002187
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002188 sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size, conf);
NeilBrown3f294f42005-11-08 21:39:25 -08002189 if (!sh)
2190 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10002191
NeilBrowna9683a72015-02-25 12:02:51 +11002192 if (grow_buffers(sh, gfp)) {
NeilBrowne4e11e32010-06-16 16:45:16 +10002193 shrink_buffers(sh);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002194 free_stripe(conf->slab_cache, sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002195 return 0;
2196 }
NeilBrown486f0642015-02-25 12:10:35 +11002197 sh->hash_lock_index =
2198 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrown3f294f42005-11-08 21:39:25 -08002199 /* we just created an active stripe so... */
NeilBrown3f294f42005-11-08 21:39:25 -08002200 atomic_inc(&conf->active_stripes);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002201
Shaohua Li6d036f72015-08-13 14:31:57 -07002202 raid5_release_stripe(sh);
NeilBrown486f0642015-02-25 12:10:35 +11002203 conf->max_nr_stripes++;
NeilBrown3f294f42005-11-08 21:39:25 -08002204 return 1;
2205}
2206
NeilBrownd1688a62011-10-11 16:49:52 +11002207static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08002208{
Christoph Lametere18b8902006-12-06 20:33:20 -08002209 struct kmem_cache *sc;
Arnd Bergmann53b8d892018-02-20 14:09:11 +01002210 size_t namelen = sizeof(conf->cache_name[0]);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002211 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
NeilBrownf4be6b42010-06-01 19:37:25 +10002213 if (conf->mddev->gendisk)
Arnd Bergmann53b8d892018-02-20 14:09:11 +01002214 snprintf(conf->cache_name[0], namelen,
NeilBrownf4be6b42010-06-01 19:37:25 +10002215 "raid%d-%s", conf->level, mdname(conf->mddev));
2216 else
Arnd Bergmann53b8d892018-02-20 14:09:11 +01002217 snprintf(conf->cache_name[0], namelen,
NeilBrownf4be6b42010-06-01 19:37:25 +10002218 "raid%d-%p", conf->level, conf->mddev);
Arnd Bergmann53b8d892018-02-20 14:09:11 +01002219 snprintf(conf->cache_name[1], namelen, "%.27s-alt", conf->cache_name[0]);
NeilBrownf4be6b42010-06-01 19:37:25 +10002220
NeilBrownad01c9e2006-03-27 01:18:07 -08002221 conf->active_name = 0;
2222 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002224 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (!sc)
2226 return 1;
2227 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002228 conf->pool_size = devs;
NeilBrown486f0642015-02-25 12:10:35 +11002229 while (num--)
2230 if (!grow_one_stripe(conf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 return 1;
NeilBrown486f0642015-02-25 12:10:35 +11002232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 return 0;
2234}
NeilBrown29269552006-03-27 01:18:10 -08002235
Dan Williamsd6f38f32009-07-14 11:50:52 -07002236/**
Coly Li7f8a30e2020-04-09 22:17:22 +08002237 * scribble_alloc - allocate percpu scribble buffer for required size
2238 * of the scribble region
Damien Le Moal2aada5b2020-07-16 13:54:42 +09002239 * @percpu: from for_each_present_cpu() of the caller
2240 * @num: total number of disks in the array
2241 * @cnt: scribble objs count for required size of the scribble region
Dan Williamsd6f38f32009-07-14 11:50:52 -07002242 *
Coly Li7f8a30e2020-04-09 22:17:22 +08002243 * The scribble buffer size must be enough to contain:
Dan Williamsd6f38f32009-07-14 11:50:52 -07002244 * 1/ a struct page pointer for each device in the array +2
2245 * 2/ room to convert each entry in (1) to its corresponding dma
2246 * (dma_map_page()) or page (page_address()) address.
2247 *
2248 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2249 * calculate over all devices (not just the data blocks), using zeros in place
2250 * of the P and Q blocks.
2251 */
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002252static int scribble_alloc(struct raid5_percpu *percpu,
Coly Liba54d4d2020-04-09 22:17:21 +08002253 int num, int cnt)
Dan Williamsd6f38f32009-07-14 11:50:52 -07002254{
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002255 size_t obj_size =
2256 sizeof(struct page *) * (num+2) +
2257 sizeof(addr_conv_t) * (num+2);
2258 void *scribble;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002259
Coly Liba54d4d2020-04-09 22:17:21 +08002260 /*
2261 * If here is in raid array suspend context, it is in memalloc noio
2262 * context as well, there is no potential recursive memory reclaim
2263 * I/Os with the GFP_KERNEL flag.
2264 */
2265 scribble = kvmalloc_array(cnt, obj_size, GFP_KERNEL);
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002266 if (!scribble)
2267 return -ENOMEM;
2268
2269 kvfree(percpu->scribble);
2270
2271 percpu->scribble = scribble;
2272 percpu->scribble_obj_size = obj_size;
2273 return 0;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002274}
2275
NeilBrown738a2732015-05-08 18:19:39 +10002276static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2277{
2278 unsigned long cpu;
2279 int err = 0;
2280
Shaohua Li27a353c2016-02-24 17:38:28 -08002281 /*
2282 * Never shrink. And mddev_suspend() could deadlock if this is called
2283 * from raid5d. In that case, scribble_disks and scribble_sectors
2284 * should equal to new_disks and new_sectors
2285 */
2286 if (conf->scribble_disks >= new_disks &&
2287 conf->scribble_sectors >= new_sectors)
2288 return 0;
NeilBrown738a2732015-05-08 18:19:39 +10002289 mddev_suspend(conf->mddev);
2290 get_online_cpus();
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002291
NeilBrown738a2732015-05-08 18:19:39 +10002292 for_each_present_cpu(cpu) {
2293 struct raid5_percpu *percpu;
NeilBrown738a2732015-05-08 18:19:39 +10002294
2295 percpu = per_cpu_ptr(conf->percpu, cpu);
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002296 err = scribble_alloc(percpu, new_disks,
Yufen Yuc911c462020-07-18 05:29:07 -04002297 new_sectors / RAID5_STRIPE_SECTORS(conf));
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002298 if (err)
NeilBrown738a2732015-05-08 18:19:39 +10002299 break;
NeilBrown738a2732015-05-08 18:19:39 +10002300 }
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002301
NeilBrown738a2732015-05-08 18:19:39 +10002302 put_online_cpus();
2303 mddev_resume(conf->mddev);
Shaohua Li27a353c2016-02-24 17:38:28 -08002304 if (!err) {
2305 conf->scribble_disks = new_disks;
2306 conf->scribble_sectors = new_sectors;
2307 }
NeilBrown738a2732015-05-08 18:19:39 +10002308 return err;
2309}
2310
NeilBrownd1688a62011-10-11 16:49:52 +11002311static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08002312{
2313 /* Make all the stripes able to hold 'newsize' devices.
2314 * New slots in each stripe get 'page' set to a new page.
2315 *
2316 * This happens in stages:
2317 * 1/ create a new kmem_cache and allocate the required number of
2318 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09002319 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08002320 * to the new stripe_heads. This will have the side effect of
2321 * freezing the array as once all stripe_heads have been collected,
2322 * no IO will be possible. Old stripe heads are freed once their
2323 * pages have been transferred over, and the old kmem_cache is
2324 * freed when all stripes are done.
2325 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
Zhilong Liu3560741e2017-03-15 16:14:53 +08002326 * we simple return a failure status - no need to clean anything up.
NeilBrownad01c9e2006-03-27 01:18:07 -08002327 * 4/ allocate new pages for the new slots in the new stripe_heads.
2328 * If this fails, we don't bother trying the shrink the
2329 * stripe_heads down again, we just leave them as they are.
2330 * As each stripe_head is processed the new one is released into
2331 * active service.
2332 *
2333 * Once step2 is started, we cannot afford to wait for a write,
2334 * so we use GFP_NOIO allocations.
2335 */
2336 struct stripe_head *osh, *nsh;
2337 LIST_HEAD(newstripes);
2338 struct disk_info *ndisks;
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02002339 int err = 0;
Christoph Lametere18b8902006-12-06 20:33:20 -08002340 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002341 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11002342 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08002343
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02002344 md_allow_write(conf->mddev);
NeilBrown2a2275d2007-01-26 00:57:11 -08002345
NeilBrownad01c9e2006-03-27 01:18:07 -08002346 /* Step 1 */
2347 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2348 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002349 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08002350 if (!sc)
2351 return -ENOMEM;
2352
NeilBrown2d5b5692015-07-06 12:49:23 +10002353 /* Need to ensure auto-resizing doesn't interfere */
2354 mutex_lock(&conf->cache_size_mutex);
2355
NeilBrownad01c9e2006-03-27 01:18:07 -08002356 for (i = conf->max_nr_stripes; i; i--) {
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002357 nsh = alloc_stripe(sc, GFP_KERNEL, newsize, conf);
NeilBrownad01c9e2006-03-27 01:18:07 -08002358 if (!nsh)
2359 break;
2360
NeilBrownad01c9e2006-03-27 01:18:07 -08002361 list_add(&nsh->lru, &newstripes);
2362 }
2363 if (i) {
2364 /* didn't get enough, give up */
2365 while (!list_empty(&newstripes)) {
2366 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2367 list_del(&nsh->lru);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002368 free_stripe(sc, nsh);
NeilBrownad01c9e2006-03-27 01:18:07 -08002369 }
2370 kmem_cache_destroy(sc);
NeilBrown2d5b5692015-07-06 12:49:23 +10002371 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002372 return -ENOMEM;
2373 }
2374 /* Step 2 - Must use GFP_NOIO now.
2375 * OK, we have enough stripes, start collecting inactive
2376 * stripes and copying them over
2377 */
Shaohua Li566c09c2013-11-14 15:16:17 +11002378 hash = 0;
2379 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08002380 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11002381 lock_device_hash_lock(conf, hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08002382 wait_event_cmd(conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +11002383 !list_empty(conf->inactive_list + hash),
2384 unlock_device_hash_lock(conf, hash),
2385 lock_device_hash_lock(conf, hash));
2386 osh = get_free_stripe(conf, hash);
2387 unlock_device_hash_lock(conf, hash);
NeilBrownf18c1a32015-05-08 18:19:04 +10002388
Shaohua Lid592a992014-05-21 17:57:44 +08002389 for(i=0; i<conf->pool_size; i++) {
NeilBrownad01c9e2006-03-27 01:18:07 -08002390 nsh->dev[i].page = osh->dev[i].page;
Shaohua Lid592a992014-05-21 17:57:44 +08002391 nsh->dev[i].orig_page = osh->dev[i].page;
2392 }
Shaohua Li566c09c2013-11-14 15:16:17 +11002393 nsh->hash_lock_index = hash;
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002394 free_stripe(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11002395 cnt++;
2396 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2397 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2398 hash++;
2399 cnt = 0;
2400 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002401 }
2402 kmem_cache_destroy(conf->slab_cache);
2403
2404 /* Step 3.
2405 * At this point, we are holding all the stripes so the array
2406 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07002407 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08002408 */
Kees Cook6396bb22018-06-12 14:03:40 -07002409 ndisks = kcalloc(newsize, sizeof(struct disk_info), GFP_NOIO);
NeilBrownad01c9e2006-03-27 01:18:07 -08002410 if (ndisks) {
Song Liud7bd3982016-11-23 22:50:39 -08002411 for (i = 0; i < conf->pool_size; i++)
NeilBrownad01c9e2006-03-27 01:18:07 -08002412 ndisks[i] = conf->disks[i];
Song Liud7bd3982016-11-23 22:50:39 -08002413
2414 for (i = conf->pool_size; i < newsize; i++) {
2415 ndisks[i].extra_page = alloc_page(GFP_NOIO);
2416 if (!ndisks[i].extra_page)
2417 err = -ENOMEM;
2418 }
2419
2420 if (err) {
2421 for (i = conf->pool_size; i < newsize; i++)
2422 if (ndisks[i].extra_page)
2423 put_page(ndisks[i].extra_page);
2424 kfree(ndisks);
2425 } else {
2426 kfree(conf->disks);
2427 conf->disks = ndisks;
2428 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002429 } else
2430 err = -ENOMEM;
2431
NeilBrown2d5b5692015-07-06 12:49:23 +10002432 mutex_unlock(&conf->cache_size_mutex);
Dennis Yang583da482017-03-29 15:46:13 +08002433
2434 conf->slab_cache = sc;
2435 conf->active_name = 1-conf->active_name;
2436
NeilBrownad01c9e2006-03-27 01:18:07 -08002437 /* Step 4, return new stripes to service */
2438 while(!list_empty(&newstripes)) {
2439 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2440 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07002441
NeilBrownad01c9e2006-03-27 01:18:07 -08002442 for (i=conf->raid_disks; i < newsize; i++)
2443 if (nsh->dev[i].page == NULL) {
2444 struct page *p = alloc_page(GFP_NOIO);
2445 nsh->dev[i].page = p;
Shaohua Lid592a992014-05-21 17:57:44 +08002446 nsh->dev[i].orig_page = p;
NeilBrownad01c9e2006-03-27 01:18:07 -08002447 if (!p)
2448 err = -ENOMEM;
2449 }
Shaohua Li6d036f72015-08-13 14:31:57 -07002450 raid5_release_stripe(nsh);
NeilBrownad01c9e2006-03-27 01:18:07 -08002451 }
2452 /* critical section pass, GFP_NOIO no longer needed */
2453
NeilBrown6e9eac22015-05-08 18:19:34 +10002454 if (!err)
2455 conf->pool_size = newsize;
NeilBrownad01c9e2006-03-27 01:18:07 -08002456 return err;
2457}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
NeilBrown486f0642015-02-25 12:10:35 +11002459static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
2461 struct stripe_head *sh;
NeilBrown49895bc2015-08-03 17:09:57 +10002462 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Shaohua Li566c09c2013-11-14 15:16:17 +11002464 spin_lock_irq(conf->hash_locks + hash);
2465 sh = get_free_stripe(conf, hash);
2466 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08002467 if (!sh)
2468 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002469 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10002470 shrink_buffers(sh);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002471 free_stripe(conf->slab_cache, sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002472 atomic_dec(&conf->active_stripes);
NeilBrown486f0642015-02-25 12:10:35 +11002473 conf->max_nr_stripes--;
NeilBrown3f294f42005-11-08 21:39:25 -08002474 return 1;
2475}
2476
NeilBrownd1688a62011-10-11 16:49:52 +11002477static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08002478{
NeilBrown486f0642015-02-25 12:10:35 +11002479 while (conf->max_nr_stripes &&
2480 drop_one_stripe(conf))
2481 ;
NeilBrown3f294f42005-11-08 21:39:25 -08002482
Julia Lawall644df1a2015-09-13 14:15:10 +02002483 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 conf->slab_cache = NULL;
2485}
2486
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002487static void raid5_end_read_request(struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
NeilBrown99c0fb52009-03-31 14:39:38 +11002489 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002490 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002491 int disks = sh->disks, i;
NeilBrownd6950432006-07-10 04:44:20 -07002492 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11002493 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10002494 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
2496 for (i=0 ; i<disks; i++)
2497 if (bi == &sh->dev[i].req)
2498 break;
2499
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002500 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
Dan Williams45b42332007-07-09 11:56:43 -07002501 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002502 bi->bi_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002504 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002506 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 }
NeilBrown14a75d32011-12-23 10:17:52 +11002508 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11002509 /* If replacement finished while this request was outstanding,
2510 * 'replacement' might be NULL already.
2511 * In that case it moved down to 'rdev'.
2512 * rdev is not removed until all requests are finished.
2513 */
NeilBrown14a75d32011-12-23 10:17:52 +11002514 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002515 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11002516 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
NeilBrown05616be2012-05-21 09:27:00 +10002518 if (use_new_offset(conf, sh))
2519 s = sh->sector + rdev->new_data_offset;
2520 else
2521 s = sh->sector + rdev->data_offset;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002522 if (!bi->bi_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08002524 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11002525 /* Note that this cannot happen on a
2526 * replacement device. We just fail those on
2527 * any error
2528 */
NeilBrowncc6167b2016-11-02 14:16:50 +11002529 pr_info_ratelimited(
2530 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
Yufen Yuc911c462020-07-18 05:29:07 -04002531 mdname(conf->mddev), RAID5_STRIPE_SECTORS(conf),
NeilBrown05616be2012-05-21 09:27:00 +10002532 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002533 bdevname(rdev->bdev, b));
Yufen Yuc911c462020-07-18 05:29:07 -04002534 atomic_add(RAID5_STRIPE_SECTORS(conf), &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08002535 clear_bit(R5_ReadError, &sh->dev[i].flags);
2536 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002537 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2538 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2539
Song Liu86aa1392017-01-12 17:22:41 -08002540 if (test_bit(R5_InJournal, &sh->dev[i].flags))
2541 /*
2542 * end read for a page in journal, this
2543 * must be preparing for prexor in rmw
2544 */
2545 set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
2546
NeilBrown14a75d32011-12-23 10:17:52 +11002547 if (atomic_read(&rdev->read_errors))
2548 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002550 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002551 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002552 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
Nigel Croxonb76b4712019-09-06 09:21:33 -04002555 if (!(bi->bi_status == BLK_STS_PROTECTION))
2556 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002557 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowncc6167b2016-11-02 14:16:50 +11002558 pr_warn_ratelimited(
2559 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
NeilBrown14a75d32011-12-23 10:17:52 +11002560 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002561 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002562 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002563 else if (conf->mddev->degraded >= conf->max_degraded) {
2564 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002565 pr_warn_ratelimited(
2566 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002567 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002568 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002569 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002570 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002571 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002572 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002573 pr_warn_ratelimited(
2574 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002575 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002576 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002577 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002578 } else if (atomic_read(&rdev->read_errors)
Nigel Croxon0009fad2019-08-21 09:27:08 -04002579 > conf->max_nr_stripes) {
2580 if (!test_bit(Faulty, &rdev->flags)) {
2581 pr_warn("md/raid:%s: %d read_errors > %d stripes\n",
2582 mdname(conf->mddev),
2583 atomic_read(&rdev->read_errors),
2584 conf->max_nr_stripes);
2585 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
2586 mdname(conf->mddev), bdn);
2587 }
2588 } else
NeilBrownba22dcb2005-11-08 21:39:31 -08002589 retry = 1;
Bian Yuedfa1f62013-11-14 15:16:17 +11002590 if (set_bad && test_bit(In_sync, &rdev->flags)
2591 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2592 retry = 1;
NeilBrownba22dcb2005-11-08 21:39:31 -08002593 if (retry)
Xiao Ni143f6e72019-07-08 10:14:32 +08002594 if (sh->qd_idx >= 0 && sh->pd_idx == i)
2595 set_bit(R5_ReadError, &sh->dev[i].flags);
2596 else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
majianpeng3f9e7c12012-07-31 10:04:21 +10002597 set_bit(R5_ReadError, &sh->dev[i].flags);
2598 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2599 } else
2600 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002601 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002602 clear_bit(R5_ReadError, &sh->dev[i].flags);
2603 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002604 if (!(set_bad
2605 && test_bit(In_sync, &rdev->flags)
2606 && rdev_set_badblocks(
Yufen Yuc911c462020-07-18 05:29:07 -04002607 rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), 0)))
majianpeng2e8ac3032012-07-03 15:57:02 +10002608 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 }
NeilBrown14a75d32011-12-23 10:17:52 +11002611 rdev_dec_pending(rdev, conf->mddev);
Shaohua Lic9445552016-09-08 10:43:58 -07002612 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2614 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002615 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616}
2617
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002618static void raid5_end_write_request(struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
NeilBrown99c0fb52009-03-31 14:39:38 +11002620 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002621 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002622 int disks = sh->disks, i;
Kees Cook3f649ab2020-06-03 13:09:38 -07002623 struct md_rdev *rdev;
NeilBrownb84db562011-07-28 11:39:23 +10002624 sector_t first_bad;
2625 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002626 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
NeilBrown977df362011-12-23 10:17:53 +11002628 for (i = 0 ; i < disks; i++) {
2629 if (bi == &sh->dev[i].req) {
2630 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 break;
NeilBrown977df362011-12-23 10:17:53 +11002632 }
2633 if (bi == &sh->dev[i].rreq) {
2634 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002635 if (rdev)
2636 replacement = 1;
2637 else
2638 /* rdev was removed and 'replacement'
2639 * replaced it. rdev is not removed
2640 * until all requests are finished.
2641 */
2642 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002643 break;
2644 }
2645 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002646 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002648 bi->bi_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002650 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002652 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
2654
NeilBrown977df362011-12-23 10:17:53 +11002655 if (replacement) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002656 if (bi->bi_status)
NeilBrown977df362011-12-23 10:17:53 +11002657 md_error(conf->mddev, rdev);
2658 else if (is_badblock(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04002659 RAID5_STRIPE_SECTORS(conf),
NeilBrown977df362011-12-23 10:17:53 +11002660 &first_bad, &bad_sectors))
2661 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2662 } else {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002663 if (bi->bi_status) {
NeilBrown9f97e4b2014-01-16 09:35:38 +11002664 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11002665 set_bit(WriteErrorSeen, &rdev->flags);
2666 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002667 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2668 set_bit(MD_RECOVERY_NEEDED,
2669 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002670 } else if (is_badblock(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04002671 RAID5_STRIPE_SECTORS(conf),
NeilBrownc0b32972013-04-24 11:42:42 +10002672 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002673 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002674 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2675 /* That was a successful write so make
2676 * sure it looks like we already did
2677 * a re-write.
2678 */
2679 set_bit(R5_ReWrite, &sh->dev[i].flags);
2680 }
NeilBrown977df362011-12-23 10:17:53 +11002681 }
2682 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002684 if (sh->batch_head && bi->bi_status && !replacement)
shli@kernel.org72ac7332014-12-15 12:57:03 +11002685 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2686
Shaohua Lic9445552016-09-08 10:43:58 -07002687 bio_reset(bi);
NeilBrown977df362011-12-23 10:17:53 +11002688 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2689 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002691 raid5_release_stripe(sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002692
2693 if (sh->batch_head && sh != sh->batch_head)
Shaohua Li6d036f72015-08-13 14:31:57 -07002694 raid5_release_stripe(sh->batch_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
2696
Shaohua Li849674e2016-01-20 13:52:20 -08002697static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
2699 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002700 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002701 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002702 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
NeilBrown908f4fb2011-12-23 10:17:50 +11002704 spin_lock_irqsave(&conf->device_lock, flags);
Mariusz Tkaczykfb73b352018-09-04 15:08:30 +02002705
2706 if (test_bit(In_sync, &rdev->flags) &&
2707 mddev->degraded == conf->max_degraded) {
2708 /*
2709 * Don't allow to achieve failed state
2710 * Don't try to recover this device
2711 */
2712 conf->recovery_disabled = mddev->recovery_disabled;
2713 spin_unlock_irqrestore(&conf->device_lock, flags);
2714 return;
2715 }
2716
bingjingcaff69d82017-11-17 10:57:44 +08002717 set_bit(Faulty, &rdev->flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11002718 clear_bit(In_sync, &rdev->flags);
Song Liu2e38a372017-01-24 10:45:30 -08002719 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11002720 spin_unlock_irqrestore(&conf->device_lock, flags);
2721 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2722
NeilBrownde393cd2011-07-28 11:31:48 +10002723 set_bit(Blocked, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08002724 set_mask_bits(&mddev->sb_flags, 0,
2725 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrowncc6167b2016-11-02 14:16:50 +11002726 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
2727 "md/raid:%s: Operation continuing on %d devices.\n",
2728 mdname(mddev),
2729 bdevname(rdev->bdev, b),
2730 mdname(mddev),
2731 conf->raid_disks - mddev->degraded);
Song Liu70d466f2017-05-11 15:28:28 -07002732 r5c_update_on_rdev_error(mddev, rdev);
NeilBrown16a53ec2006-06-26 00:27:38 -07002733}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
2735/*
2736 * Input: a 'big' sector number,
2737 * Output: index of the data and parity disk, and the sector # in them.
2738 */
Shaohua Li6d036f72015-08-13 14:31:57 -07002739sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2740 int previous, int *dd_idx,
2741 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002743 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002744 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002746 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002747 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002749 int algorithm = previous ? conf->prev_algo
2750 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002751 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2752 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002753 int raid_disks = previous ? conf->previous_raid_disks
2754 : conf->raid_disks;
2755 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 /* First compute the information on this sector */
2758
2759 /*
2760 * Compute the chunk number and the sector offset inside the chunk
2761 */
2762 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2763 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 /*
2766 * Compute the stripe number
2767 */
NeilBrown35f2a592010-04-20 14:13:34 +10002768 stripe = chunk_number;
2769 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002770 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 /*
2772 * Select the parity disk based on the user selected algorithm.
2773 */
NeilBrown84789552011-07-27 11:00:36 +10002774 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002775 switch(conf->level) {
2776 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002777 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002778 break;
2779 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002780 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002782 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002783 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 (*dd_idx)++;
2785 break;
2786 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002787 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002788 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 (*dd_idx)++;
2790 break;
2791 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002792 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002793 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 break;
2795 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002796 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002797 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002799 case ALGORITHM_PARITY_0:
2800 pd_idx = 0;
2801 (*dd_idx)++;
2802 break;
2803 case ALGORITHM_PARITY_N:
2804 pd_idx = data_disks;
2805 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002807 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002808 }
2809 break;
2810 case 6:
2811
NeilBrowne183eae2009-03-31 15:20:22 +11002812 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002813 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002814 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002815 qd_idx = pd_idx + 1;
2816 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002817 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002818 qd_idx = 0;
2819 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002820 (*dd_idx) += 2; /* D D P Q D */
2821 break;
2822 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002823 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002824 qd_idx = pd_idx + 1;
2825 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002826 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002827 qd_idx = 0;
2828 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002829 (*dd_idx) += 2; /* D D P Q D */
2830 break;
2831 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002832 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002833 qd_idx = (pd_idx + 1) % raid_disks;
2834 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002835 break;
2836 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002837 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002838 qd_idx = (pd_idx + 1) % raid_disks;
2839 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002840 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002841
2842 case ALGORITHM_PARITY_0:
2843 pd_idx = 0;
2844 qd_idx = 1;
2845 (*dd_idx) += 2;
2846 break;
2847 case ALGORITHM_PARITY_N:
2848 pd_idx = data_disks;
2849 qd_idx = data_disks + 1;
2850 break;
2851
2852 case ALGORITHM_ROTATING_ZERO_RESTART:
2853 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2854 * of blocks for computing Q is different.
2855 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002856 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002857 qd_idx = pd_idx + 1;
2858 if (pd_idx == raid_disks-1) {
2859 (*dd_idx)++; /* Q D D D P */
2860 qd_idx = 0;
2861 } else if (*dd_idx >= pd_idx)
2862 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002863 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002864 break;
2865
2866 case ALGORITHM_ROTATING_N_RESTART:
2867 /* Same a left_asymmetric, by first stripe is
2868 * D D D P Q rather than
2869 * Q D D D P
2870 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002871 stripe2 += 1;
2872 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002873 qd_idx = pd_idx + 1;
2874 if (pd_idx == raid_disks-1) {
2875 (*dd_idx)++; /* Q D D D P */
2876 qd_idx = 0;
2877 } else if (*dd_idx >= pd_idx)
2878 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002879 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002880 break;
2881
2882 case ALGORITHM_ROTATING_N_CONTINUE:
2883 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002884 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002885 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2886 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002887 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002888 break;
2889
2890 case ALGORITHM_LEFT_ASYMMETRIC_6:
2891 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002892 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002893 if (*dd_idx >= pd_idx)
2894 (*dd_idx)++;
2895 qd_idx = raid_disks - 1;
2896 break;
2897
2898 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002899 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002900 if (*dd_idx >= pd_idx)
2901 (*dd_idx)++;
2902 qd_idx = raid_disks - 1;
2903 break;
2904
2905 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002906 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002907 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2908 qd_idx = raid_disks - 1;
2909 break;
2910
2911 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002912 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002913 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2914 qd_idx = raid_disks - 1;
2915 break;
2916
2917 case ALGORITHM_PARITY_0_6:
2918 pd_idx = 0;
2919 (*dd_idx)++;
2920 qd_idx = raid_disks - 1;
2921 break;
2922
NeilBrown16a53ec2006-06-26 00:27:38 -07002923 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002924 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002925 }
2926 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 }
2928
NeilBrown911d4ee2009-03-31 14:39:38 +11002929 if (sh) {
2930 sh->pd_idx = pd_idx;
2931 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002932 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 /*
2935 * Finally, compute the new sector number
2936 */
2937 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2938 return new_sector;
2939}
2940
Shaohua Li6d036f72015-08-13 14:31:57 -07002941sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
NeilBrownd1688a62011-10-11 16:49:52 +11002943 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002944 int raid_disks = sh->disks;
2945 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002947 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2948 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002949 int algorithm = previous ? conf->prev_algo
2950 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 sector_t stripe;
2952 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002953 sector_t chunk_number;
2954 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002956 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
2958 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2959 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
NeilBrown16a53ec2006-06-26 00:27:38 -07002961 if (i == sh->pd_idx)
2962 return 0;
2963 switch(conf->level) {
2964 case 4: break;
2965 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002966 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 case ALGORITHM_LEFT_ASYMMETRIC:
2968 case ALGORITHM_RIGHT_ASYMMETRIC:
2969 if (i > sh->pd_idx)
2970 i--;
2971 break;
2972 case ALGORITHM_LEFT_SYMMETRIC:
2973 case ALGORITHM_RIGHT_SYMMETRIC:
2974 if (i < sh->pd_idx)
2975 i += raid_disks;
2976 i -= (sh->pd_idx + 1);
2977 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002978 case ALGORITHM_PARITY_0:
2979 i -= 1;
2980 break;
2981 case ALGORITHM_PARITY_N:
2982 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002984 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002985 }
2986 break;
2987 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002988 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002989 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002990 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002991 case ALGORITHM_LEFT_ASYMMETRIC:
2992 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002993 case ALGORITHM_ROTATING_ZERO_RESTART:
2994 case ALGORITHM_ROTATING_N_RESTART:
2995 if (sh->pd_idx == raid_disks-1)
2996 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002997 else if (i > sh->pd_idx)
2998 i -= 2; /* D D P Q D */
2999 break;
3000 case ALGORITHM_LEFT_SYMMETRIC:
3001 case ALGORITHM_RIGHT_SYMMETRIC:
3002 if (sh->pd_idx == raid_disks-1)
3003 i--; /* Q D D D P */
3004 else {
3005 /* D D P Q D */
3006 if (i < sh->pd_idx)
3007 i += raid_disks;
3008 i -= (sh->pd_idx + 2);
3009 }
3010 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11003011 case ALGORITHM_PARITY_0:
3012 i -= 2;
3013 break;
3014 case ALGORITHM_PARITY_N:
3015 break;
3016 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11003017 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11003018 if (sh->pd_idx == 0)
3019 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11003020 else {
3021 /* D D Q P D */
3022 if (i < sh->pd_idx)
3023 i += raid_disks;
3024 i -= (sh->pd_idx + 1);
3025 }
NeilBrown99c0fb52009-03-31 14:39:38 +11003026 break;
3027 case ALGORITHM_LEFT_ASYMMETRIC_6:
3028 case ALGORITHM_RIGHT_ASYMMETRIC_6:
3029 if (i > sh->pd_idx)
3030 i--;
3031 break;
3032 case ALGORITHM_LEFT_SYMMETRIC_6:
3033 case ALGORITHM_RIGHT_SYMMETRIC_6:
3034 if (i < sh->pd_idx)
3035 i += data_disks + 1;
3036 i -= (sh->pd_idx + 1);
3037 break;
3038 case ALGORITHM_PARITY_0_6:
3039 i -= 1;
3040 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07003041 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11003042 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07003043 }
3044 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 }
3046
3047 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10003048 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
NeilBrown112bf892009-03-31 14:39:38 +11003050 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11003051 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11003052 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
3053 || sh2.qd_idx != sh->qd_idx) {
NeilBrowncc6167b2016-11-02 14:16:50 +11003054 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
3055 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return 0;
3057 }
3058 return r_sector;
3059}
3060
Song Liu07e83362017-01-23 17:12:58 -08003061/*
3062 * There are cases where we want handle_stripe_dirtying() and
3063 * schedule_reconstruction() to delay towrite to some dev of a stripe.
3064 *
3065 * This function checks whether we want to delay the towrite. Specifically,
3066 * we delay the towrite when:
3067 *
3068 * 1. degraded stripe has a non-overwrite to the missing dev, AND this
3069 * stripe has data in journal (for other devices).
3070 *
3071 * In this case, when reading data for the non-overwrite dev, it is
3072 * necessary to handle complex rmw of write back cache (prexor with
3073 * orig_page, and xor with page). To keep read path simple, we would
3074 * like to flush data in journal to RAID disks first, so complex rmw
3075 * is handled in the write patch (handle_stripe_dirtying).
3076 *
Song Liu39b99582017-01-24 14:08:23 -08003077 * 2. when journal space is critical (R5C_LOG_CRITICAL=1)
3078 *
3079 * It is important to be able to flush all stripes in raid5-cache.
3080 * Therefore, we need reserve some space on the journal device for
3081 * these flushes. If flush operation includes pending writes to the
3082 * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe
3083 * for the flush out. If we exclude these pending writes from flush
3084 * operation, we only need (conf->max_degraded + 1) pages per stripe.
3085 * Therefore, excluding pending writes in these cases enables more
3086 * efficient use of the journal device.
3087 *
3088 * Note: To make sure the stripe makes progress, we only delay
3089 * towrite for stripes with data already in journal (injournal > 0).
3090 * When LOG_CRITICAL, stripes with injournal == 0 will be sent to
3091 * no_space_stripes list.
3092 *
Song Liu70d466f2017-05-11 15:28:28 -07003093 * 3. during journal failure
3094 * In journal failure, we try to flush all cached data to raid disks
3095 * based on data in stripe cache. The array is read-only to upper
3096 * layers, so we would skip all pending writes.
3097 *
Song Liu07e83362017-01-23 17:12:58 -08003098 */
Song Liu39b99582017-01-24 14:08:23 -08003099static inline bool delay_towrite(struct r5conf *conf,
3100 struct r5dev *dev,
3101 struct stripe_head_state *s)
Song Liu07e83362017-01-23 17:12:58 -08003102{
Song Liu39b99582017-01-24 14:08:23 -08003103 /* case 1 above */
3104 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3105 !test_bit(R5_Insync, &dev->flags) && s->injournal)
3106 return true;
3107 /* case 2 above */
3108 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
3109 s->injournal > 0)
3110 return true;
Song Liu70d466f2017-05-11 15:28:28 -07003111 /* case 3 above */
3112 if (s->log_failed && s->injournal)
3113 return true;
Song Liu39b99582017-01-24 14:08:23 -08003114 return false;
Song Liu07e83362017-01-23 17:12:58 -08003115}
3116
Dan Williams600aa102008-06-28 08:32:05 +10003117static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003118schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10003119 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07003120{
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003121 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11003122 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003123 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07003124
Dan Williamse33129d2007-01-02 13:52:30 -07003125 if (rcw) {
Song Liu1e6d6902016-11-17 15:24:39 -08003126 /*
3127 * In some cases, handle_stripe_dirtying initially decided to
3128 * run rmw and allocates extra page for prexor. However, rcw is
3129 * cheaper later on. We need to free the extra page now,
3130 * because we won't be able to do that in ops_complete_prexor().
3131 */
3132 r5c_release_extra_page(sh);
Dan Williamse33129d2007-01-02 13:52:30 -07003133
3134 for (i = disks; i--; ) {
3135 struct r5dev *dev = &sh->dev[i];
3136
Song Liu39b99582017-01-24 14:08:23 -08003137 if (dev->towrite && !delay_towrite(conf, dev, s)) {
Dan Williamse33129d2007-01-02 13:52:30 -07003138 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10003139 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003140 if (!expand)
3141 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003142 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003143 } else if (test_bit(R5_InJournal, &dev->flags)) {
3144 set_bit(R5_LOCKED, &dev->flags);
3145 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003146 }
3147 }
NeilBrownce7d3632013-03-04 12:37:14 +11003148 /* if we are not expanding this is a proper write request, and
3149 * there will be bios with new data to be drained into the
3150 * stripe cache
3151 */
3152 if (!expand) {
3153 if (!s->locked)
3154 /* False alarm, nothing to do */
3155 return;
3156 sh->reconstruct_state = reconstruct_state_drain_run;
3157 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3158 } else
3159 sh->reconstruct_state = reconstruct_state_run;
3160
3161 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3162
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003163 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003164 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003165 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07003166 } else {
3167 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
3168 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003169 BUG_ON(level == 6 &&
3170 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
3171 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
Dan Williamse33129d2007-01-02 13:52:30 -07003172
Dan Williamse33129d2007-01-02 13:52:30 -07003173 for (i = disks; i--; ) {
3174 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003175 if (i == pd_idx || i == qd_idx)
Dan Williamse33129d2007-01-02 13:52:30 -07003176 continue;
3177
Dan Williamse33129d2007-01-02 13:52:30 -07003178 if (dev->towrite &&
3179 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10003180 test_bit(R5_Wantcompute, &dev->flags))) {
3181 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003182 set_bit(R5_LOCKED, &dev->flags);
3183 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003184 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003185 } else if (test_bit(R5_InJournal, &dev->flags)) {
3186 set_bit(R5_LOCKED, &dev->flags);
3187 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003188 }
3189 }
NeilBrownce7d3632013-03-04 12:37:14 +11003190 if (!s->locked)
3191 /* False alarm - nothing to do */
3192 return;
3193 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
3194 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
3195 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3196 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003197 }
3198
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003199 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07003200 * are in flight
3201 */
3202 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
3203 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10003204 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003205
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003206 if (level == 6) {
3207 int qd_idx = sh->qd_idx;
3208 struct r5dev *dev = &sh->dev[qd_idx];
3209
3210 set_bit(R5_LOCKED, &dev->flags);
3211 clear_bit(R5_UPTODATE, &dev->flags);
3212 s->locked++;
3213 }
3214
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02003215 if (raid5_has_ppl(sh->raid_conf) && sh->ppl_page &&
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01003216 test_bit(STRIPE_OP_BIODRAIN, &s->ops_request) &&
3217 !test_bit(STRIPE_FULL_WRITE, &sh->state) &&
3218 test_bit(R5_Insync, &sh->dev[pd_idx].flags))
3219 set_bit(STRIPE_OP_PARTIAL_PARITY, &s->ops_request);
3220
Dan Williams600aa102008-06-28 08:32:05 +10003221 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07003222 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10003223 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003224}
NeilBrown16a53ec2006-06-26 00:27:38 -07003225
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226/*
3227 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07003228 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 * The bi_next chain must be in order.
3230 */
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003231static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
3232 int forwrite, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233{
3234 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11003235 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07003236 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
NeilBrowncbe47ec2011-07-26 11:20:35 +10003238 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003239 (unsigned long long)bi->bi_iter.bi_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 (unsigned long long)sh->sector);
3241
Shaohua Lib17459c2012-07-19 16:01:31 +10003242 spin_lock_irq(&sh->stripe_lock);
Mariusz Dabrowski2cd259a2018-04-19 19:28:10 +02003243 sh->dev[dd_idx].write_hint = bi->bi_write_hint;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003244 /* Don't allow new IO added to stripes in batch list */
3245 if (sh->batch_head)
3246 goto overlap;
NeilBrown72626682005-09-09 16:23:54 -07003247 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003249 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07003250 firstwrite = 1;
3251 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 bip = &sh->dev[dd_idx].toread;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003253 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
3254 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 goto overlap;
3256 bip = & (*bip)->bi_next;
3257 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07003258 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 goto overlap;
3260
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01003261 if (forwrite && raid5_has_ppl(conf)) {
3262 /*
3263 * With PPL only writes to consecutive data chunks within a
3264 * stripe are allowed because for a single stripe_head we can
3265 * only have one PPL entry at a time, which describes one data
3266 * range. Not really an overlap, but wait_for_overlap can be
3267 * used to handle this.
3268 */
3269 sector_t sector;
3270 sector_t first = 0;
3271 sector_t last = 0;
3272 int count = 0;
3273 int i;
3274
3275 for (i = 0; i < sh->disks; i++) {
3276 if (i != sh->pd_idx &&
3277 (i == dd_idx || sh->dev[i].towrite)) {
3278 sector = sh->dev[i].sector;
3279 if (count == 0 || sector < first)
3280 first = sector;
3281 if (sector > last)
3282 last = sector;
3283 count++;
3284 }
3285 }
3286
3287 if (first + conf->chunk_sectors * (count - 1) != last)
3288 goto overlap;
3289 }
3290
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003291 if (!forwrite || previous)
3292 clear_bit(STRIPE_BATCH_READY, &sh->state);
3293
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02003294 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 if (*bip)
3296 bi->bi_next = *bip;
3297 *bip = bi;
NeilBrown016c76a2017-03-15 14:05:13 +11003298 bio_inc_remaining(bi);
NeilBrown49728052017-03-15 14:05:12 +11003299 md_write_inc(conf->mddev, bi);
NeilBrown72626682005-09-09 16:23:54 -07003300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 if (forwrite) {
3302 /* check if page is covered */
3303 sector_t sector = sh->dev[dd_idx].sector;
3304 for (bi=sh->dev[dd_idx].towrite;
Yufen Yuc911c462020-07-18 05:29:07 -04003305 sector < sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07003306 bi && bi->bi_iter.bi_sector <= sector;
Yufen Yuc911c462020-07-18 05:29:07 -04003307 bi = r5_next_bio(conf, bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07003308 if (bio_end_sector(bi) >= sector)
3309 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 }
Yufen Yuc911c462020-07-18 05:29:07 -04003311 if (sector >= sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf))
shli@kernel.org7a87f432014-12-15 12:57:03 +11003312 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
3313 sh->overwrite_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003315
3316 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003317 (unsigned long long)(*bip)->bi_iter.bi_sector,
NeilBrowncbe47ec2011-07-26 11:20:35 +10003318 (unsigned long long)sh->sector, dd_idx);
3319
3320 if (conf->mddev->bitmap && firstwrite) {
NeilBrownd0852df52015-05-27 08:43:45 +10003321 /* Cannot hold spinlock over bitmap_startwrite,
3322 * but must ensure this isn't added to a batch until
3323 * we have added to the bitmap and set bm_seq.
3324 * So set STRIPE_BITMAP_PENDING to prevent
3325 * batching.
3326 * If multiple add_stripe_bio() calls race here they
3327 * much all set STRIPE_BITMAP_PENDING. So only the first one
3328 * to complete "bitmap_startwrite" gets to set
3329 * STRIPE_BIT_DELAY. This is important as once a stripe
3330 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3331 * any more.
3332 */
3333 set_bit(STRIPE_BITMAP_PENDING, &sh->state);
3334 spin_unlock_irq(&sh->stripe_lock);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003335 md_bitmap_startwrite(conf->mddev->bitmap, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003336 RAID5_STRIPE_SECTORS(conf), 0);
NeilBrownd0852df52015-05-27 08:43:45 +10003337 spin_lock_irq(&sh->stripe_lock);
3338 clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
3339 if (!sh->batch_head) {
3340 sh->bm_seq = conf->seq_flush+1;
3341 set_bit(STRIPE_BIT_DELAY, &sh->state);
3342 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003343 }
NeilBrownd0852df52015-05-27 08:43:45 +10003344 spin_unlock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003345
3346 if (stripe_can_batch(sh))
3347 stripe_add_to_batch_list(conf, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 return 1;
3349
3350 overlap:
3351 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10003352 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 return 0;
3354}
3355
NeilBrownd1688a62011-10-11 16:49:52 +11003356static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08003357
NeilBrownd1688a62011-10-11 16:49:52 +11003358static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003359 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08003360{
NeilBrown784052e2009-03-31 15:19:07 +11003361 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10003362 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11003363 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003364 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11003365 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003366
NeilBrown112bf892009-03-31 14:39:38 +11003367 raid5_compute_sector(conf,
3368 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08003369 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11003370 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003371 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003372}
3373
Dan Williamsa4456852007-07-09 11:56:43 -07003374static void
NeilBrownd1688a62011-10-11 16:49:52 +11003375handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
NeilBrownbd83d0a2017-03-15 14:05:12 +11003376 struct stripe_head_state *s, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003377{
3378 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003379 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003380 for (i = disks; i--; ) {
3381 struct bio *bi;
3382 int bitmap_end = 0;
3383
3384 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003385 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07003386 rcu_read_lock();
3387 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownf5b67ae2016-06-02 16:19:53 +10003388 if (rdev && test_bit(In_sync, &rdev->flags) &&
3389 !test_bit(Faulty, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10003390 atomic_inc(&rdev->nr_pending);
3391 else
3392 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003393 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10003394 if (rdev) {
3395 if (!rdev_set_badblocks(
3396 rdev,
3397 sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003398 RAID5_STRIPE_SECTORS(conf), 0))
NeilBrown7f0da592011-07-28 11:39:22 +10003399 md_error(conf->mddev, rdev);
3400 rdev_dec_pending(rdev, conf->mddev);
3401 }
Dan Williamsa4456852007-07-09 11:56:43 -07003402 }
Shaohua Lib17459c2012-07-19 16:01:31 +10003403 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003404 /* fail all writes first */
3405 bi = sh->dev[i].towrite;
3406 sh->dev[i].towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11003407 sh->overwrite_disks = 0;
Shaohua Lib17459c2012-07-19 16:01:31 +10003408 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11003409 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003410 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07003411
Artur Paszkiewiczff875732017-03-09 09:59:58 +01003412 log_stripe_write_finished(sh);
Shaohua Li0576b1c2015-08-13 14:32:00 -07003413
Dan Williamsa4456852007-07-09 11:56:43 -07003414 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3415 wake_up(&conf->wait_for_overlap);
3416
Kent Overstreet4f024f32013-10-11 15:44:27 -07003417 while (bi && bi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04003418 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3419 struct bio *nextbi = r5_next_bio(conf, bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003420
NeilBrown49728052017-03-15 14:05:12 +11003421 md_write_end(conf->mddev);
Guoqing Jiang6308d8e2017-07-21 16:33:44 +08003422 bio_io_error(bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003423 bi = nextbi;
3424 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003425 if (bitmap_end)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003426 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003427 RAID5_STRIPE_SECTORS(conf), 0, 0);
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003428 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003429 /* and fail all 'written' */
3430 bi = sh->dev[i].written;
3431 sh->dev[i].written = NULL;
Shaohua Lid592a992014-05-21 17:57:44 +08003432 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3433 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3434 sh->dev[i].page = sh->dev[i].orig_page;
3435 }
3436
Dan Williamsa4456852007-07-09 11:56:43 -07003437 if (bi) bitmap_end = 1;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003438 while (bi && bi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04003439 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3440 struct bio *bi2 = r5_next_bio(conf, bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003441
NeilBrown49728052017-03-15 14:05:12 +11003442 md_write_end(conf->mddev);
Guoqing Jiang6308d8e2017-07-21 16:33:44 +08003443 bio_io_error(bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003444 bi = bi2;
3445 }
3446
Dan Williamsb5e98d62007-01-02 13:52:31 -07003447 /* fail any reads if this device is non-operational and
3448 * the data has not reached the cache yet.
3449 */
3450 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
Shaohua Li6e74a9c2015-10-08 21:54:08 -07003451 s->failed > conf->max_degraded &&
Dan Williamsb5e98d62007-01-02 13:52:31 -07003452 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3453 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11003454 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003455 bi = sh->dev[i].toread;
3456 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11003457 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003458 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3459 wake_up(&conf->wait_for_overlap);
Shaohua Liebda7802015-09-18 10:20:13 -07003460 if (bi)
3461 s->to_read--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003462 while (bi && bi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04003463 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003464 struct bio *nextbi =
Yufen Yuc911c462020-07-18 05:29:07 -04003465 r5_next_bio(conf, bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003466
Guoqing Jiang6308d8e2017-07-21 16:33:44 +08003467 bio_io_error(bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003468 bi = nextbi;
3469 }
3470 }
Dan Williamsa4456852007-07-09 11:56:43 -07003471 if (bitmap_end)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003472 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003473 RAID5_STRIPE_SECTORS(conf), 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10003474 /* If we were in the middle of a write the parity block might
3475 * still be locked - so just clear all R5_LOCKED flags
3476 */
3477 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003478 }
Shaohua Liebda7802015-09-18 10:20:13 -07003479 s->to_write = 0;
3480 s->written = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003481
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003482 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3483 if (atomic_dec_and_test(&conf->pending_full_writes))
3484 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003485}
3486
NeilBrown7f0da592011-07-28 11:39:22 +10003487static void
NeilBrownd1688a62011-10-11 16:49:52 +11003488handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10003489 struct stripe_head_state *s)
3490{
3491 int abort = 0;
3492 int i;
3493
shli@kernel.org59fc6302014-12-15 12:57:03 +11003494 BUG_ON(sh->batch_head);
NeilBrown7f0da592011-07-28 11:39:22 +10003495 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003496 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3497 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10003498 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11003499 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10003500 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10003501 * Don't even need to abort as that is handled elsewhere
3502 * if needed, and not always wanted e.g. if there is a known
3503 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11003504 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10003505 * non-sync devices, or abort the recovery
3506 */
NeilBrown18b98372012-04-01 23:48:38 +10003507 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3508 /* During recovery devices cannot be removed, so
3509 * locking and refcounting of rdevs is not needed
3510 */
NeilBrowne50d3992016-06-02 16:19:52 +10003511 rcu_read_lock();
NeilBrown18b98372012-04-01 23:48:38 +10003512 for (i = 0; i < conf->raid_disks; i++) {
NeilBrowne50d3992016-06-02 16:19:52 +10003513 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown18b98372012-04-01 23:48:38 +10003514 if (rdev
3515 && !test_bit(Faulty, &rdev->flags)
3516 && !test_bit(In_sync, &rdev->flags)
3517 && !rdev_set_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003518 RAID5_STRIPE_SECTORS(conf), 0))
NeilBrown18b98372012-04-01 23:48:38 +10003519 abort = 1;
NeilBrowne50d3992016-06-02 16:19:52 +10003520 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown18b98372012-04-01 23:48:38 +10003521 if (rdev
3522 && !test_bit(Faulty, &rdev->flags)
3523 && !test_bit(In_sync, &rdev->flags)
3524 && !rdev_set_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003525 RAID5_STRIPE_SECTORS(conf), 0))
NeilBrown18b98372012-04-01 23:48:38 +10003526 abort = 1;
3527 }
NeilBrowne50d3992016-06-02 16:19:52 +10003528 rcu_read_unlock();
NeilBrown18b98372012-04-01 23:48:38 +10003529 if (abort)
3530 conf->recovery_disabled =
3531 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10003532 }
Yufen Yuc911c462020-07-18 05:29:07 -04003533 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10003534}
3535
NeilBrown9a3e1102011-12-23 10:17:53 +11003536static int want_replace(struct stripe_head *sh, int disk_idx)
3537{
3538 struct md_rdev *rdev;
3539 int rv = 0;
NeilBrown3f232d62016-06-02 16:19:52 +10003540
3541 rcu_read_lock();
3542 rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
NeilBrown9a3e1102011-12-23 10:17:53 +11003543 if (rdev
3544 && !test_bit(Faulty, &rdev->flags)
3545 && !test_bit(In_sync, &rdev->flags)
3546 && (rdev->recovery_offset <= sh->sector
3547 || rdev->mddev->recovery_cp <= sh->sector))
3548 rv = 1;
NeilBrown3f232d62016-06-02 16:19:52 +10003549 rcu_read_unlock();
NeilBrown9a3e1102011-12-23 10:17:53 +11003550 return rv;
3551}
3552
NeilBrown2c58f062015-02-02 11:32:23 +11003553static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3554 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07003555{
3556 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10003557 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3558 &sh->dev[s->failed_num[1]] };
NeilBrownea664c82015-02-02 14:03:28 +11003559 int i;
ChangSyun Peng45a4d8f2020-07-31 17:50:31 +08003560 bool force_rcw = (sh->raid_conf->rmw_level == PARITY_DISABLE_RMW);
Dan Williamsf38e1212007-01-02 13:52:30 -07003561
NeilBrowna79cfe12015-02-02 11:37:59 +11003562
3563 if (test_bit(R5_LOCKED, &dev->flags) ||
3564 test_bit(R5_UPTODATE, &dev->flags))
3565 /* No point reading this as we already have it or have
3566 * decided to get it.
3567 */
3568 return 0;
3569
3570 if (dev->toread ||
3571 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3572 /* We need this block to directly satisfy a request */
3573 return 1;
3574
3575 if (s->syncing || s->expanding ||
3576 (s->replacing && want_replace(sh, disk_idx)))
3577 /* When syncing, or expanding we read everything.
3578 * When replacing, we need the replaced block.
3579 */
3580 return 1;
3581
3582 if ((s->failed >= 1 && fdev[0]->toread) ||
3583 (s->failed >= 2 && fdev[1]->toread))
3584 /* If we want to read from a failed device, then
3585 * we need to actually read every other device.
3586 */
3587 return 1;
3588
NeilBrowna9d56952015-02-02 11:49:10 +11003589 /* Sometimes neither read-modify-write nor reconstruct-write
3590 * cycles can work. In those cases we read every block we
3591 * can. Then the parity-update is certain to have enough to
3592 * work with.
3593 * This can only be a problem when we need to write something,
3594 * and some device has failed. If either of those tests
3595 * fail we need look no further.
3596 */
3597 if (!s->failed || !s->to_write)
3598 return 0;
3599
3600 if (test_bit(R5_Insync, &dev->flags) &&
3601 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3602 /* Pre-reads at not permitted until after short delay
3603 * to gather multiple requests. However if this
Zhilong Liu3560741e2017-03-15 16:14:53 +08003604 * device is no Insync, the block could only be computed
NeilBrowna9d56952015-02-02 11:49:10 +11003605 * and there is no need to delay that.
3606 */
3607 return 0;
NeilBrownea664c82015-02-02 14:03:28 +11003608
NeilBrown36707bb2015-09-24 15:25:36 +10003609 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrownea664c82015-02-02 14:03:28 +11003610 if (fdev[i]->towrite &&
3611 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3612 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3613 /* If we have a partial write to a failed
3614 * device, then we will need to reconstruct
3615 * the content of that device, so all other
3616 * devices must be read.
3617 */
3618 return 1;
ChangSyun Peng45a4d8f2020-07-31 17:50:31 +08003619
3620 if (s->failed >= 2 &&
3621 (fdev[i]->towrite ||
3622 s->failed_num[i] == sh->pd_idx ||
3623 s->failed_num[i] == sh->qd_idx) &&
3624 !test_bit(R5_UPTODATE, &fdev[i]->flags))
3625 /* In max degraded raid6, If the failed disk is P, Q,
3626 * or we want to read the failed disk, we need to do
3627 * reconstruct-write.
3628 */
3629 force_rcw = true;
NeilBrownea664c82015-02-02 14:03:28 +11003630 }
3631
ChangSyun Peng45a4d8f2020-07-31 17:50:31 +08003632 /* If we are forced to do a reconstruct-write, because parity
3633 * cannot be trusted and we are currently recovering it, there
3634 * is extra need to be careful.
NeilBrownea664c82015-02-02 14:03:28 +11003635 * If one of the devices that we would need to read, because
3636 * it is not being overwritten (and maybe not written at all)
3637 * is missing/faulty, then we need to read everything we can.
3638 */
ChangSyun Peng45a4d8f2020-07-31 17:50:31 +08003639 if (!force_rcw &&
NeilBrownea664c82015-02-02 14:03:28 +11003640 sh->sector < sh->raid_conf->mddev->recovery_cp)
3641 /* reconstruct-write isn't being forced */
3642 return 0;
NeilBrown36707bb2015-09-24 15:25:36 +10003643 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrown10d82c52015-05-08 18:19:33 +10003644 if (s->failed_num[i] != sh->pd_idx &&
3645 s->failed_num[i] != sh->qd_idx &&
3646 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
NeilBrownea664c82015-02-02 14:03:28 +11003647 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3648 return 1;
3649 }
3650
NeilBrown2c58f062015-02-02 11:32:23 +11003651 return 0;
3652}
3653
Song Liuba026842017-01-12 17:22:42 -08003654/* fetch_block - checks the given member device to see if its data needs
3655 * to be read or computed to satisfy a request.
3656 *
3657 * Returns 1 when no more member devices need to be checked, otherwise returns
3658 * 0 to tell the loop in handle_stripe_fill to continue
3659 */
NeilBrown2c58f062015-02-02 11:32:23 +11003660static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3661 int disk_idx, int disks)
3662{
3663 struct r5dev *dev = &sh->dev[disk_idx];
3664
3665 /* is the data in this block needed, and can we get it? */
3666 if (need_this_block(sh, s, disk_idx, disks)) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003667 /* we would like to get this block, possibly by computing it,
3668 * otherwise read it if the backing disk is insync
3669 */
3670 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3671 BUG_ON(test_bit(R5_Wantread, &dev->flags));
NeilBrownb0c783b2015-05-08 18:19:32 +10003672 BUG_ON(sh->batch_head);
NeilBrown7471fb72017-04-03 12:11:32 +10003673
3674 /*
3675 * In the raid6 case if the only non-uptodate disk is P
3676 * then we already trusted P to compute the other failed
3677 * drives. It is safe to compute rather than re-read P.
3678 * In other cases we only compute blocks from failed
3679 * devices, otherwise check/repair might fail to detect
3680 * a real inconsistency.
3681 */
3682
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003683 if ((s->uptodate == disks - 1) &&
NeilBrown7471fb72017-04-03 12:11:32 +10003684 ((sh->qd_idx >= 0 && sh->pd_idx == disk_idx) ||
NeilBrownf2b3b442011-07-26 11:35:19 +10003685 (s->failed && (disk_idx == s->failed_num[0] ||
NeilBrown7471fb72017-04-03 12:11:32 +10003686 disk_idx == s->failed_num[1])))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003687 /* have disk failed, and we're requested to fetch it;
3688 * do compute it
3689 */
3690 pr_debug("Computing stripe %llu block %d\n",
3691 (unsigned long long)sh->sector, disk_idx);
3692 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3693 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3694 set_bit(R5_Wantcompute, &dev->flags);
3695 sh->ops.target = disk_idx;
3696 sh->ops.target2 = -1; /* no 2nd target */
3697 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10003698 /* Careful: from this point on 'uptodate' is in the eye
3699 * of raid_run_ops which services 'compute' operations
3700 * before writes. R5_Wantcompute flags a block that will
3701 * be R5_UPTODATE by the time it is needed for a
3702 * subsequent operation.
3703 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003704 s->uptodate++;
3705 return 1;
3706 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3707 /* Computing 2-failure is *very* expensive; only
3708 * do it if failed >= 2
3709 */
3710 int other;
3711 for (other = disks; other--; ) {
3712 if (other == disk_idx)
3713 continue;
3714 if (!test_bit(R5_UPTODATE,
3715 &sh->dev[other].flags))
3716 break;
3717 }
3718 BUG_ON(other < 0);
3719 pr_debug("Computing stripe %llu blocks %d,%d\n",
3720 (unsigned long long)sh->sector,
3721 disk_idx, other);
3722 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3723 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3724 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3725 set_bit(R5_Wantcompute, &sh->dev[other].flags);
3726 sh->ops.target = disk_idx;
3727 sh->ops.target2 = other;
3728 s->uptodate += 2;
3729 s->req_compute = 1;
3730 return 1;
3731 } else if (test_bit(R5_Insync, &dev->flags)) {
3732 set_bit(R5_LOCKED, &dev->flags);
3733 set_bit(R5_Wantread, &dev->flags);
3734 s->locked++;
3735 pr_debug("Reading block %d (sync=%d)\n",
3736 disk_idx, s->syncing);
3737 }
3738 }
3739
3740 return 0;
3741}
3742
Damien Le Moal2aada5b2020-07-16 13:54:42 +09003743/*
NeilBrown93b3dbc2011-07-27 11:00:36 +10003744 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003745 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10003746static void handle_stripe_fill(struct stripe_head *sh,
3747 struct stripe_head_state *s,
3748 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003749{
3750 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003751
3752 /* look for blocks to read/compute, skip this if a compute
3753 * is already in flight, or if the stripe contents are in the
3754 * midst of changing due to a write
3755 */
3756 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Song Liu07e83362017-01-23 17:12:58 -08003757 !sh->reconstruct_state) {
3758
3759 /*
3760 * For degraded stripe with data in journal, do not handle
3761 * read requests yet, instead, flush the stripe to raid
3762 * disks first, this avoids handling complex rmw of write
3763 * back cache (prexor with orig_page, and then xor with
3764 * page) in the read path
3765 */
3766 if (s->injournal && s->failed) {
3767 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
3768 r5c_make_stripe_write_out(sh);
3769 goto out;
3770 }
3771
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003772 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10003773 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003774 break;
Song Liu07e83362017-01-23 17:12:58 -08003775 }
3776out:
Dan Williamsa4456852007-07-09 11:56:43 -07003777 set_bit(STRIPE_HANDLE, &sh->state);
3778}
3779
NeilBrown787b76f2015-05-21 12:56:41 +10003780static void break_stripe_batch_list(struct stripe_head *head_sh,
3781 unsigned long handle_flags);
Dan Williams1fe797e2008-06-28 09:16:30 +10003782/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07003783 * any written block on an uptodate or failed drive can be returned.
3784 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3785 * never LOCKED, so we don't need to test 'failed' directly.
3786 */
NeilBrownd1688a62011-10-11 16:49:52 +11003787static void handle_stripe_clean_event(struct r5conf *conf,
NeilBrownbd83d0a2017-03-15 14:05:12 +11003788 struct stripe_head *sh, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003789{
3790 int i;
3791 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003792 int discard_pending = 0;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003793 struct stripe_head *head_sh = sh;
3794 bool do_endio = false;
Dan Williamsa4456852007-07-09 11:56:43 -07003795
3796 for (i = disks; i--; )
3797 if (sh->dev[i].written) {
3798 dev = &sh->dev[i];
3799 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003800 (test_bit(R5_UPTODATE, &dev->flags) ||
Shaohua Lid592a992014-05-21 17:57:44 +08003801 test_bit(R5_Discard, &dev->flags) ||
3802 test_bit(R5_SkipCopy, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003803 /* We can return any write requests */
3804 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003805 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003806 if (test_and_clear_bit(R5_Discard, &dev->flags))
3807 clear_bit(R5_UPTODATE, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08003808 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3809 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
Shaohua Lid592a992014-05-21 17:57:44 +08003810 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003811 do_endio = true;
3812
3813returnbi:
3814 dev->page = dev->orig_page;
Dan Williamsa4456852007-07-09 11:56:43 -07003815 wbi = dev->written;
3816 dev->written = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003817 while (wbi && wbi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04003818 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
3819 wbi2 = r5_next_bio(conf, wbi, dev->sector);
NeilBrown49728052017-03-15 14:05:12 +11003820 md_write_end(conf->mddev);
NeilBrown016c76a2017-03-15 14:05:13 +11003821 bio_endio(wbi);
Dan Williamsa4456852007-07-09 11:56:43 -07003822 wbi = wbi2;
3823 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003824 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003825 RAID5_STRIPE_SECTORS(conf),
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003826 !test_bit(STRIPE_DEGRADED, &sh->state),
3827 0);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003828 if (head_sh->batch_head) {
3829 sh = list_first_entry(&sh->batch_list,
3830 struct stripe_head,
3831 batch_list);
3832 if (sh != head_sh) {
3833 dev = &sh->dev[i];
3834 goto returnbi;
3835 }
3836 }
3837 sh = head_sh;
3838 dev = &sh->dev[i];
NeilBrownf8dfcff2013-03-12 12:18:06 +11003839 } else if (test_bit(R5_Discard, &dev->flags))
3840 discard_pending = 1;
3841 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -07003842
Artur Paszkiewiczff875732017-03-09 09:59:58 +01003843 log_stripe_write_finished(sh);
Shaohua Li0576b1c2015-08-13 14:32:00 -07003844
NeilBrownf8dfcff2013-03-12 12:18:06 +11003845 if (!discard_pending &&
3846 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003847 int hash;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003848 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3849 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3850 if (sh->qd_idx >= 0) {
3851 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3852 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3853 }
3854 /* now that discard is done we can proceed with any sync */
3855 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08003856 /*
3857 * SCSI discard will change some bio fields and the stripe has
3858 * no updated data, so remove it from hash list and the stripe
3859 * will be reinitialized
3860 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11003861unhash:
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003862 hash = sh->hash_lock_index;
3863 spin_lock_irq(conf->hash_locks + hash);
Shaohua Lid47648f2013-10-19 14:51:42 +08003864 remove_hash(sh);
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003865 spin_unlock_irq(conf->hash_locks + hash);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003866 if (head_sh->batch_head) {
3867 sh = list_first_entry(&sh->batch_list,
3868 struct stripe_head, batch_list);
3869 if (sh != head_sh)
3870 goto unhash;
3871 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003872 sh = head_sh;
3873
NeilBrownf8dfcff2013-03-12 12:18:06 +11003874 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3875 set_bit(STRIPE_HANDLE, &sh->state);
3876
3877 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003878
3879 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3880 if (atomic_dec_and_test(&conf->pending_full_writes))
3881 md_wakeup_thread(conf->mddev->thread);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003882
NeilBrown787b76f2015-05-21 12:56:41 +10003883 if (head_sh->batch_head && do_endio)
3884 break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
Dan Williamsa4456852007-07-09 11:56:43 -07003885}
3886
Song Liu86aa1392017-01-12 17:22:41 -08003887/*
3888 * For RMW in write back cache, we need extra page in prexor to store the
3889 * old data. This page is stored in dev->orig_page.
3890 *
3891 * This function checks whether we have data for prexor. The exact logic
3892 * is:
3893 * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
3894 */
3895static inline bool uptodate_for_rmw(struct r5dev *dev)
3896{
3897 return (test_bit(R5_UPTODATE, &dev->flags)) &&
3898 (!test_bit(R5_InJournal, &dev->flags) ||
3899 test_bit(R5_OrigPageUPTDODATE, &dev->flags));
3900}
3901
Song Liud7bd3982016-11-23 22:50:39 -08003902static int handle_stripe_dirtying(struct r5conf *conf,
3903 struct stripe_head *sh,
3904 struct stripe_head_state *s,
3905 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003906{
3907 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003908 sector_t recovery_cp = conf->mddev->recovery_cp;
3909
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003910 /* Check whether resync is now happening or should start.
Alexander Lyakasa7854482012-10-11 13:50:12 +11003911 * If yes, then the array is dirty (after unclean shutdown or
3912 * initial creation), so parity in some stripes might be inconsistent.
3913 * In this case, we need to always do reconstruct-write, to ensure
3914 * that in case of drive failure or read-error correction, we
3915 * generate correct data from the parity.
3916 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003917 if (conf->rmw_level == PARITY_DISABLE_RMW ||
NeilBrown26ac1072015-02-18 11:35:14 +11003918 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
3919 s->failed == 0)) {
Alexander Lyakasa7854482012-10-11 13:50:12 +11003920 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10003921 * look like rcw is cheaper
3922 */
3923 rcw = 1; rmw = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003924 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
3925 conf->rmw_level, (unsigned long long)recovery_cp,
Alexander Lyakasa7854482012-10-11 13:50:12 +11003926 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10003927 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003928 /* would I have to read this buffer for read_modify_write */
3929 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08003930 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu07e83362017-01-23 17:12:58 -08003931 i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08003932 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003933 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08003934 !(uptodate_for_rmw(dev) ||
Dan Williamsf38e1212007-01-02 13:52:30 -07003935 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003936 if (test_bit(R5_Insync, &dev->flags))
3937 rmw++;
3938 else
3939 rmw += 2*disks; /* cannot read it */
3940 }
3941 /* Would I have to read this buffer for reconstruct_write */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003942 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3943 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003944 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003945 !(test_bit(R5_UPTODATE, &dev->flags) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003946 test_bit(R5_Wantcompute, &dev->flags))) {
NeilBrown67f45542014-05-28 13:39:22 +10003947 if (test_bit(R5_Insync, &dev->flags))
3948 rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07003949 else
3950 rcw += 2*disks;
3951 }
3952 }
Song Liu1e6d6902016-11-17 15:24:39 -08003953
Song Liu39b99582017-01-24 14:08:23 -08003954 pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n",
3955 (unsigned long long)sh->sector, sh->state, rmw, rcw);
Dan Williamsa4456852007-07-09 11:56:43 -07003956 set_bit(STRIPE_HANDLE, &sh->state);
Song Liu41257582016-05-23 17:25:06 -07003957 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003958 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003959 if (conf->mddev->queue)
3960 blk_add_trace_msg(conf->mddev->queue,
3961 "raid5 rmw %llu %d",
3962 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07003963 for (i = disks; i--; ) {
3964 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08003965 if (test_bit(R5_InJournal, &dev->flags) &&
3966 dev->page == dev->orig_page &&
3967 !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) {
3968 /* alloc page for prexor */
Song Liud7bd3982016-11-23 22:50:39 -08003969 struct page *p = alloc_page(GFP_NOIO);
Song Liu1e6d6902016-11-17 15:24:39 -08003970
Song Liud7bd3982016-11-23 22:50:39 -08003971 if (p) {
3972 dev->orig_page = p;
3973 continue;
3974 }
3975
3976 /*
3977 * alloc_page() failed, try use
3978 * disk_info->extra_page
3979 */
3980 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE,
3981 &conf->cache_state)) {
3982 r5c_use_extra_page(sh);
3983 break;
3984 }
3985
3986 /* extra_page in use, add to delayed_list */
3987 set_bit(STRIPE_DELAYED, &sh->state);
3988 s->waiting_extra_page = 1;
3989 return -EAGAIN;
Song Liu1e6d6902016-11-17 15:24:39 -08003990 }
Song Liud7bd3982016-11-23 22:50:39 -08003991 }
Song Liu1e6d6902016-11-17 15:24:39 -08003992
Song Liud7bd3982016-11-23 22:50:39 -08003993 for (i = disks; i--; ) {
3994 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08003995 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003996 i == sh->pd_idx || i == sh->qd_idx ||
3997 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003998 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08003999 !(uptodate_for_rmw(dev) ||
Song Liu1e6d6902016-11-17 15:24:39 -08004000 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07004001 test_bit(R5_Insync, &dev->flags)) {
NeilBrown67f45542014-05-28 13:39:22 +10004002 if (test_bit(STRIPE_PREREAD_ACTIVE,
4003 &sh->state)) {
4004 pr_debug("Read_old block %d for r-m-w\n",
4005 i);
Dan Williamsa4456852007-07-09 11:56:43 -07004006 set_bit(R5_LOCKED, &dev->flags);
4007 set_bit(R5_Wantread, &dev->flags);
4008 s->locked++;
Guoqing Jiange3914d52020-07-28 12:01:40 +02004009 } else
Dan Williamsa4456852007-07-09 11:56:43 -07004010 set_bit(STRIPE_DELAYED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07004011 }
4012 }
NeilBrowna9add5d2012-10-31 11:59:09 +11004013 }
Song Liu41257582016-05-23 17:25:06 -07004014 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07004015 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11004016 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10004017 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07004018 for (i = disks; i--; ) {
4019 struct r5dev *dev = &sh->dev[i];
4020 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10004021 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07004022 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07004023 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10004024 test_bit(R5_Wantcompute, &dev->flags))) {
4025 rcw++;
NeilBrown67f45542014-05-28 13:39:22 +10004026 if (test_bit(R5_Insync, &dev->flags) &&
4027 test_bit(STRIPE_PREREAD_ACTIVE,
4028 &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07004029 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07004030 "%d for Reconstruct\n", i);
4031 set_bit(R5_LOCKED, &dev->flags);
4032 set_bit(R5_Wantread, &dev->flags);
4033 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11004034 qread++;
Guoqing Jiange3914d52020-07-28 12:01:40 +02004035 } else
Dan Williamsa4456852007-07-09 11:56:43 -07004036 set_bit(STRIPE_DELAYED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07004037 }
4038 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004039 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11004040 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
4041 (unsigned long long)sh->sector,
4042 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10004043 }
NeilBrownb1b02fe2015-02-02 10:44:29 +11004044
4045 if (rcw > disks && rmw > disks &&
4046 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4047 set_bit(STRIPE_DELAYED, &sh->state);
4048
Dan Williamsa4456852007-07-09 11:56:43 -07004049 /* now if nothing is locked, and if we have enough data,
4050 * we can start a write request
4051 */
Dan Williamsf38e1212007-01-02 13:52:30 -07004052 /* since handle_stripe can be called at any time we need to handle the
4053 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07004054 * subsequent call wants to start a write request. raid_run_ops only
4055 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07004056 * simultaneously. If this is not the case then new writes need to be
4057 * held off until the compute completes.
4058 */
Dan Williams976ea8d2008-06-28 08:32:03 +10004059 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
4060 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
Song Liu1e6d6902016-11-17 15:24:39 -08004061 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07004062 schedule_reconstruction(sh, s, rcw == 0, 0);
Song Liud7bd3982016-11-23 22:50:39 -08004063 return 0;
Dan Williamsa4456852007-07-09 11:56:43 -07004064}
4065
NeilBrownd1688a62011-10-11 16:49:52 +11004066static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07004067 struct stripe_head_state *s, int disks)
4068{
Dan Williamsecc65c92008-06-28 08:31:57 +10004069 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07004070
shli@kernel.org59fc6302014-12-15 12:57:03 +11004071 BUG_ON(sh->batch_head);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004072 set_bit(STRIPE_HANDLE, &sh->state);
4073
Dan Williamsecc65c92008-06-28 08:31:57 +10004074 switch (sh->check_state) {
4075 case check_state_idle:
4076 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07004077 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07004078 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10004079 sh->check_state = check_state_run;
4080 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004081 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004082 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10004083 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07004084 }
NeilBrownf2b3b442011-07-26 11:35:19 +10004085 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10004086 /* fall through */
4087 case check_state_compute_result:
4088 sh->check_state = check_state_idle;
4089 if (!dev)
4090 dev = &sh->dev[sh->pd_idx];
4091
4092 /* check that a write has not made the stripe insync */
4093 if (test_bit(STRIPE_INSYNC, &sh->state))
4094 break;
4095
4096 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07004097 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
4098 BUG_ON(s->uptodate != disks);
4099
4100 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10004101 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07004102 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07004103
Dan Williamsa4456852007-07-09 11:56:43 -07004104 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07004105 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10004106 break;
4107 case check_state_run:
4108 break; /* we will be called again upon completion */
4109 case check_state_check_result:
4110 sh->check_state = check_state_idle;
4111
4112 /* if a failure occurred during the check operation, leave
4113 * STRIPE_INSYNC not set and let the stripe be handled again
4114 */
4115 if (s->failed)
4116 break;
4117
4118 /* handle a successful check operation, if parity is correct
4119 * we are done. Otherwise update the mismatch count and repair
4120 * parity if !MD_RECOVERY_CHECK
4121 */
Dan Williamsad283ea2009-08-29 19:09:26 -07004122 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10004123 /* parity is correct (on disc,
4124 * not in buffer any more)
4125 */
4126 set_bit(STRIPE_INSYNC, &sh->state);
4127 else {
Yufen Yuc911c462020-07-18 05:29:07 -04004128 atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches);
Nixe1539032017-05-16 10:13:31 +01004129 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
Dan Williamsecc65c92008-06-28 08:31:57 +10004130 /* don't try to repair!! */
4131 set_bit(STRIPE_INSYNC, &sh->state);
Nixe1539032017-05-16 10:13:31 +01004132 pr_warn_ratelimited("%s: mismatch sector in range "
4133 "%llu-%llu\n", mdname(conf->mddev),
4134 (unsigned long long) sh->sector,
4135 (unsigned long long) sh->sector +
Yufen Yuc911c462020-07-18 05:29:07 -04004136 RAID5_STRIPE_SECTORS(conf));
Nixe1539032017-05-16 10:13:31 +01004137 } else {
Dan Williamsecc65c92008-06-28 08:31:57 +10004138 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10004139 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10004140 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4141 set_bit(R5_Wantcompute,
4142 &sh->dev[sh->pd_idx].flags);
4143 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07004144 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10004145 s->uptodate++;
4146 }
4147 }
4148 break;
4149 case check_state_compute_run:
4150 break;
4151 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11004152 pr_err("%s: unknown check_state: %d sector: %llu\n",
Dan Williamsecc65c92008-06-28 08:31:57 +10004153 __func__, sh->check_state,
4154 (unsigned long long) sh->sector);
4155 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07004156 }
4157}
4158
NeilBrownd1688a62011-10-11 16:49:52 +11004159static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07004160 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10004161 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07004162{
Dan Williamsa4456852007-07-09 11:56:43 -07004163 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11004164 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07004165 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07004166
shli@kernel.org59fc6302014-12-15 12:57:03 +11004167 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07004168 set_bit(STRIPE_HANDLE, &sh->state);
4169
4170 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004171
Dan Williamsa4456852007-07-09 11:56:43 -07004172 /* Want to check and possibly repair P and Q.
4173 * However there could be one 'failed' device, in which
4174 * case we can only check one of them, possibly using the
4175 * other to generate missing data
4176 */
4177
Dan Williamsd82dfee2009-07-14 13:40:57 -07004178 switch (sh->check_state) {
4179 case check_state_idle:
4180 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10004181 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004182 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07004183 * makes sense to check P (If anything else were failed,
4184 * we would have used P to recreate it).
4185 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004186 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07004187 }
NeilBrownf2b3b442011-07-26 11:35:19 +10004188 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004189 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07004190 * anything, so it makes sense to check it
4191 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004192 if (sh->check_state == check_state_run)
4193 sh->check_state = check_state_run_pq;
4194 else
4195 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07004196 }
Dan Williams36d1c642009-07-14 11:48:22 -07004197
Dan Williamsd82dfee2009-07-14 13:40:57 -07004198 /* discard potentially stale zero_sum_result */
4199 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07004200
Dan Williamsd82dfee2009-07-14 13:40:57 -07004201 if (sh->check_state == check_state_run) {
4202 /* async_xor_zero_sum destroys the contents of P */
4203 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
4204 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07004205 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004206 if (sh->check_state >= check_state_run &&
4207 sh->check_state <= check_state_run_pq) {
4208 /* async_syndrome_zero_sum preserves P and Q, so
4209 * no need to mark them !uptodate here
4210 */
4211 set_bit(STRIPE_OP_CHECK, &s->ops_request);
4212 break;
4213 }
Dan Williams36d1c642009-07-14 11:48:22 -07004214
Dan Williamsd82dfee2009-07-14 13:40:57 -07004215 /* we have 2-disk failure */
4216 BUG_ON(s->failed != 2);
4217 /* fall through */
4218 case check_state_compute_result:
4219 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07004220
Dan Williamsd82dfee2009-07-14 13:40:57 -07004221 /* check that a write has not made the stripe insync */
4222 if (test_bit(STRIPE_INSYNC, &sh->state))
4223 break;
Dan Williamsa4456852007-07-09 11:56:43 -07004224
4225 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07004226 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07004227 */
Nigel Croxonb2176a12019-04-16 09:50:09 -07004228 dev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07004229 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004230 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07004231 s->locked++;
4232 set_bit(R5_LOCKED, &dev->flags);
4233 set_bit(R5_Wantwrite, &dev->flags);
4234 }
4235 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004236 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07004237 s->locked++;
4238 set_bit(R5_LOCKED, &dev->flags);
4239 set_bit(R5_Wantwrite, &dev->flags);
4240 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004241 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004242 dev = &sh->dev[pd_idx];
4243 s->locked++;
4244 set_bit(R5_LOCKED, &dev->flags);
4245 set_bit(R5_Wantwrite, &dev->flags);
4246 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004247 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004248 dev = &sh->dev[qd_idx];
4249 s->locked++;
4250 set_bit(R5_LOCKED, &dev->flags);
4251 set_bit(R5_Wantwrite, &dev->flags);
4252 }
Nigel Croxonb2176a12019-04-16 09:50:09 -07004253 if (WARN_ONCE(dev && !test_bit(R5_UPTODATE, &dev->flags),
4254 "%s: disk%td not up to date\n",
4255 mdname(conf->mddev),
4256 dev - (struct r5dev *) &sh->dev)) {
4257 clear_bit(R5_LOCKED, &dev->flags);
4258 clear_bit(R5_Wantwrite, &dev->flags);
4259 s->locked--;
4260 }
Dan Williamsa4456852007-07-09 11:56:43 -07004261 clear_bit(STRIPE_DEGRADED, &sh->state);
4262
4263 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004264 break;
4265 case check_state_run:
4266 case check_state_run_q:
4267 case check_state_run_pq:
4268 break; /* we will be called again upon completion */
4269 case check_state_check_result:
4270 sh->check_state = check_state_idle;
4271
4272 /* handle a successful check operation, if parity is correct
4273 * we are done. Otherwise update the mismatch count and repair
4274 * parity if !MD_RECOVERY_CHECK
4275 */
4276 if (sh->ops.zero_sum_result == 0) {
Song Liua25d8c32019-04-16 09:34:21 -07004277 /* both parities are correct */
4278 if (!s->failed)
4279 set_bit(STRIPE_INSYNC, &sh->state);
4280 else {
4281 /* in contrast to the raid5 case we can validate
4282 * parity, but still have a failure to write
4283 * back
4284 */
4285 sh->check_state = check_state_compute_result;
4286 /* Returning at this point means that we may go
4287 * off and bring p and/or q uptodate again so
4288 * we make sure to check zero_sum_result again
4289 * to verify if p or q need writeback
4290 */
4291 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004292 } else {
Yufen Yuc911c462020-07-18 05:29:07 -04004293 atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches);
Nixe1539032017-05-16 10:13:31 +01004294 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004295 /* don't try to repair!! */
4296 set_bit(STRIPE_INSYNC, &sh->state);
Nixe1539032017-05-16 10:13:31 +01004297 pr_warn_ratelimited("%s: mismatch sector in range "
4298 "%llu-%llu\n", mdname(conf->mddev),
4299 (unsigned long long) sh->sector,
4300 (unsigned long long) sh->sector +
Yufen Yuc911c462020-07-18 05:29:07 -04004301 RAID5_STRIPE_SECTORS(conf));
Nixe1539032017-05-16 10:13:31 +01004302 } else {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004303 int *target = &sh->ops.target;
4304
4305 sh->ops.target = -1;
4306 sh->ops.target2 = -1;
4307 sh->check_state = check_state_compute_run;
4308 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4309 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4310 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4311 set_bit(R5_Wantcompute,
4312 &sh->dev[pd_idx].flags);
4313 *target = pd_idx;
4314 target = &sh->ops.target2;
4315 s->uptodate++;
4316 }
4317 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4318 set_bit(R5_Wantcompute,
4319 &sh->dev[qd_idx].flags);
4320 *target = qd_idx;
4321 s->uptodate++;
4322 }
4323 }
4324 }
4325 break;
4326 case check_state_compute_run:
4327 break;
4328 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11004329 pr_warn("%s: unknown check_state: %d sector: %llu\n",
4330 __func__, sh->check_state,
4331 (unsigned long long) sh->sector);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004332 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07004333 }
4334}
4335
NeilBrownd1688a62011-10-11 16:49:52 +11004336static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07004337{
4338 int i;
4339
4340 /* We have read all the blocks in this stripe and now we need to
4341 * copy some of them into a target stripe for expand.
4342 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07004343 struct dma_async_tx_descriptor *tx = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +11004344 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07004345 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4346 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11004347 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11004348 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07004349 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07004350 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07004351
Shaohua Li6d036f72015-08-13 14:31:57 -07004352 sector_t bn = raid5_compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11004353 sector_t s = raid5_compute_sector(conf, bn, 0,
4354 &dd_idx, NULL);
Shaohua Li6d036f72015-08-13 14:31:57 -07004355 sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07004356 if (sh2 == NULL)
4357 /* so far only the early blocks of this stripe
4358 * have been requested. When later blocks
4359 * get requested, we will try again
4360 */
4361 continue;
4362 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
4363 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
4364 /* must have already done this block */
Shaohua Li6d036f72015-08-13 14:31:57 -07004365 raid5_release_stripe(sh2);
Dan Williamsa4456852007-07-09 11:56:43 -07004366 continue;
4367 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07004368
4369 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07004370 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004371 tx = async_memcpy(sh2->dev[dd_idx].page,
Yufen Yuc911c462020-07-18 05:29:07 -04004372 sh->dev[i].page, 0, 0, RAID5_STRIPE_SIZE(conf),
Dan Williamsa08abd82009-06-03 11:43:59 -07004373 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004374
Dan Williamsa4456852007-07-09 11:56:43 -07004375 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
4376 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
4377 for (j = 0; j < conf->raid_disks; j++)
4378 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10004379 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07004380 !test_bit(R5_Expanded, &sh2->dev[j].flags))
4381 break;
4382 if (j == conf->raid_disks) {
4383 set_bit(STRIPE_EXPAND_READY, &sh2->state);
4384 set_bit(STRIPE_HANDLE, &sh2->state);
4385 }
Shaohua Li6d036f72015-08-13 14:31:57 -07004386 raid5_release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004387
Dan Williamsa4456852007-07-09 11:56:43 -07004388 }
NeilBrowna2e08552007-09-11 15:23:36 -07004389 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11004390 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07004391}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
4393/*
4394 * handle_stripe - do things to a stripe.
4395 *
NeilBrown9a3e1102011-12-23 10:17:53 +11004396 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4397 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11004399 * return some read requests which now have data
4400 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 * schedule a read on some buffers
4402 * schedule a write of some buffers
4403 * return confirmation of parity correctness
4404 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 */
Dan Williamsa4456852007-07-09 11:56:43 -07004406
NeilBrownacfe7262011-07-27 11:00:36 +10004407static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07004408{
NeilBrownd1688a62011-10-11 16:49:52 +11004409 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08004410 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004411 struct r5dev *dev;
4412 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11004413 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07004414
NeilBrownacfe7262011-07-27 11:00:36 +10004415 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07004416
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004417 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
4418 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
NeilBrownacfe7262011-07-27 11:00:36 +10004419 s->failed_num[0] = -1;
4420 s->failed_num[1] = -1;
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004421 s->log_failed = r5l_log_disk_error(conf);
NeilBrownacfe7262011-07-27 11:00:36 +10004422
4423 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07004424 rcu_read_lock();
4425 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004426 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10004427 sector_t first_bad;
4428 int bad_sectors;
4429 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10004430
NeilBrown16a53ec2006-06-26 00:27:38 -07004431 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07004432
Dan Williams45b42332007-07-09 11:56:43 -07004433 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11004434 i, dev->flags,
4435 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004436 /* maybe we can reply to a read
4437 *
4438 * new wantfill requests are only permitted while
4439 * ops_complete_biofill is guaranteed to be inactive
4440 */
4441 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4442 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4443 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07004444
4445 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10004446 if (test_bit(R5_LOCKED, &dev->flags))
4447 s->locked++;
4448 if (test_bit(R5_UPTODATE, &dev->flags))
4449 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004450 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004451 s->compute++;
4452 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004453 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004454
NeilBrownacfe7262011-07-27 11:00:36 +10004455 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004456 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10004457 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10004458 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004459 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10004460 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004461 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004462 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004463 }
Dan Williamsa4456852007-07-09 11:56:43 -07004464 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10004465 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11004466 /* Prefer to use the replacement for reads, but only
4467 * if it is recovered enough and has no bad blocks.
4468 */
4469 rdev = rcu_dereference(conf->disks[i].replacement);
4470 if (rdev && !test_bit(Faulty, &rdev->flags) &&
Yufen Yuc911c462020-07-18 05:29:07 -04004471 rdev->recovery_offset >= sh->sector + RAID5_STRIPE_SECTORS(conf) &&
4472 !is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
NeilBrown14a75d32011-12-23 10:17:52 +11004473 &first_bad, &bad_sectors))
4474 set_bit(R5_ReadRepl, &dev->flags);
4475 else {
NeilBrowne6030cb2015-07-17 13:26:23 +10004476 if (rdev && !test_bit(Faulty, &rdev->flags))
NeilBrown9a3e1102011-12-23 10:17:53 +11004477 set_bit(R5_NeedReplace, &dev->flags);
NeilBrowne6030cb2015-07-17 13:26:23 +10004478 else
4479 clear_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11004480 rdev = rcu_dereference(conf->disks[i].rdev);
4481 clear_bit(R5_ReadRepl, &dev->flags);
4482 }
NeilBrown9283d8c2011-12-08 16:27:57 +11004483 if (rdev && test_bit(Faulty, &rdev->flags))
4484 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10004485 if (rdev) {
Yufen Yuc911c462020-07-18 05:29:07 -04004486 is_bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
NeilBrown31c176e2011-07-28 11:39:22 +10004487 &first_bad, &bad_sectors);
4488 if (s->blocked_rdev == NULL
4489 && (test_bit(Blocked, &rdev->flags)
4490 || is_bad < 0)) {
4491 if (is_bad < 0)
4492 set_bit(BlockedBadBlocks,
4493 &rdev->flags);
4494 s->blocked_rdev = rdev;
4495 atomic_inc(&rdev->nr_pending);
4496 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004497 }
NeilBrown415e72d2010-06-17 17:25:21 +10004498 clear_bit(R5_Insync, &dev->flags);
4499 if (!rdev)
4500 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10004501 else if (is_bad) {
4502 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10004503 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4504 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10004505 /* treat as in-sync, but with a read error
4506 * which we can now try to correct
4507 */
4508 set_bit(R5_Insync, &dev->flags);
4509 set_bit(R5_ReadError, &dev->flags);
4510 }
4511 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10004512 set_bit(R5_Insync, &dev->flags);
Yufen Yuc911c462020-07-18 05:29:07 -04004513 else if (sh->sector + RAID5_STRIPE_SECTORS(conf) <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10004514 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11004515 set_bit(R5_Insync, &dev->flags);
4516 else if (test_bit(R5_UPTODATE, &dev->flags) &&
4517 test_bit(R5_Expanded, &dev->flags))
4518 /* If we've reshaped into here, we assume it is Insync.
4519 * We will shortly update recovery_offset to make
4520 * it official.
4521 */
4522 set_bit(R5_Insync, &dev->flags);
4523
NeilBrown1cc03eb2014-01-06 13:19:42 +11004524 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004525 /* This flag does not apply to '.replacement'
4526 * only to .rdev, so make sure to check that*/
4527 struct md_rdev *rdev2 = rcu_dereference(
4528 conf->disks[i].rdev);
4529 if (rdev2 == rdev)
4530 clear_bit(R5_Insync, &dev->flags);
4531 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004532 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004533 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10004534 } else
4535 clear_bit(R5_WriteError, &dev->flags);
4536 }
NeilBrown1cc03eb2014-01-06 13:19:42 +11004537 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004538 /* This flag does not apply to '.replacement'
4539 * only to .rdev, so make sure to check that*/
4540 struct md_rdev *rdev2 = rcu_dereference(
4541 conf->disks[i].rdev);
4542 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10004543 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004544 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10004545 } else
4546 clear_bit(R5_MadeGood, &dev->flags);
4547 }
NeilBrown977df362011-12-23 10:17:53 +11004548 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4549 struct md_rdev *rdev2 = rcu_dereference(
4550 conf->disks[i].replacement);
4551 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4552 s->handle_bad_blocks = 1;
4553 atomic_inc(&rdev2->nr_pending);
4554 } else
4555 clear_bit(R5_MadeGoodRepl, &dev->flags);
4556 }
NeilBrown415e72d2010-06-17 17:25:21 +10004557 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004558 /* The ReadError flag will just be confusing now */
4559 clear_bit(R5_ReadError, &dev->flags);
4560 clear_bit(R5_ReWrite, &dev->flags);
4561 }
NeilBrown415e72d2010-06-17 17:25:21 +10004562 if (test_bit(R5_ReadError, &dev->flags))
4563 clear_bit(R5_Insync, &dev->flags);
4564 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004565 if (s->failed < 2)
4566 s->failed_num[s->failed] = i;
4567 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11004568 if (rdev && !test_bit(Faulty, &rdev->flags))
4569 do_recovery = 1;
BingJing Changd63e2fc2018-08-01 17:08:36 +08004570 else if (!rdev) {
4571 rdev = rcu_dereference(
4572 conf->disks[i].replacement);
4573 if (rdev && !test_bit(Faulty, &rdev->flags))
4574 do_recovery = 1;
4575 }
NeilBrown415e72d2010-06-17 17:25:21 +10004576 }
Song Liu2ded3702016-11-17 15:24:38 -08004577
4578 if (test_bit(R5_InJournal, &dev->flags))
4579 s->injournal++;
Song Liu1e6d6902016-11-17 15:24:39 -08004580 if (test_bit(R5_InJournal, &dev->flags) && dev->written)
4581 s->just_cached++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004582 }
NeilBrown9a3e1102011-12-23 10:17:53 +11004583 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4584 /* If there is a failed device being replaced,
4585 * we must be recovering.
4586 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10004587 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11004588 * else we can only be replacing
4589 * sync and recovery both need to read all devices, and so
4590 * use the same flag.
4591 */
4592 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10004593 sh->sector >= conf->mddev->recovery_cp ||
4594 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11004595 s->syncing = 1;
4596 else
4597 s->replacing = 1;
4598 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004599 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10004600}
NeilBrownf4168852007-02-28 20:11:53 -08004601
Guoqing Jiangcb9902d2020-06-16 11:25:51 +02004602/*
4603 * Return '1' if this is a member of batch, or '0' if it is a lone stripe or
4604 * a head which can now be handled.
4605 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11004606static int clear_batch_ready(struct stripe_head *sh)
4607{
4608 struct stripe_head *tmp;
4609 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
NeilBrownb15a9db2015-05-22 15:20:04 +10004610 return (sh->batch_head && sh->batch_head != sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11004611 spin_lock(&sh->stripe_lock);
4612 if (!sh->batch_head) {
4613 spin_unlock(&sh->stripe_lock);
4614 return 0;
4615 }
4616
4617 /*
4618 * this stripe could be added to a batch list before we check
4619 * BATCH_READY, skips it
4620 */
4621 if (sh->batch_head != sh) {
4622 spin_unlock(&sh->stripe_lock);
4623 return 1;
4624 }
4625 spin_lock(&sh->batch_lock);
4626 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4627 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4628 spin_unlock(&sh->batch_lock);
4629 spin_unlock(&sh->stripe_lock);
4630
4631 /*
4632 * BATCH_READY is cleared, no new stripes can be added.
4633 * batch_list can be accessed without lock
4634 */
4635 return 0;
4636}
4637
NeilBrown3960ce72015-05-21 12:20:36 +10004638static void break_stripe_batch_list(struct stripe_head *head_sh,
4639 unsigned long handle_flags)
shli@kernel.org72ac7332014-12-15 12:57:03 +11004640{
NeilBrown4e3d62f2015-05-21 11:50:16 +10004641 struct stripe_head *sh, *next;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004642 int i;
NeilBrownfb642b92015-05-21 12:00:47 +10004643 int do_wakeup = 0;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004644
NeilBrownbb270512015-05-08 18:19:40 +10004645 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4646
shli@kernel.org72ac7332014-12-15 12:57:03 +11004647 list_del_init(&sh->batch_list);
4648
Shaohua Lifb3229d2016-03-09 10:08:38 -08004649 WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
NeilBrown1b956f72015-05-21 12:40:26 +10004650 (1 << STRIPE_SYNCING) |
4651 (1 << STRIPE_REPLACED) |
NeilBrown1b956f72015-05-21 12:40:26 +10004652 (1 << STRIPE_DELAYED) |
4653 (1 << STRIPE_BIT_DELAY) |
4654 (1 << STRIPE_FULL_WRITE) |
4655 (1 << STRIPE_BIOFILL_RUN) |
4656 (1 << STRIPE_COMPUTE_RUN) |
NeilBrown1b956f72015-05-21 12:40:26 +10004657 (1 << STRIPE_DISCARD) |
4658 (1 << STRIPE_BATCH_READY) |
4659 (1 << STRIPE_BATCH_ERR) |
Shaohua Lifb3229d2016-03-09 10:08:38 -08004660 (1 << STRIPE_BITMAP_PENDING)),
4661 "stripe state: %lx\n", sh->state);
4662 WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4663 (1 << STRIPE_REPLACED)),
4664 "head stripe state: %lx\n", head_sh->state);
NeilBrown1b956f72015-05-21 12:40:26 +10004665
4666 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
NeilBrown550da242016-03-09 12:58:25 +11004667 (1 << STRIPE_PREREAD_ACTIVE) |
Dennis Yang184a09e2017-09-06 11:02:35 +08004668 (1 << STRIPE_DEGRADED) |
4669 (1 << STRIPE_ON_UNPLUG_LIST)),
NeilBrown1b956f72015-05-21 12:40:26 +10004670 head_sh->state & (1 << STRIPE_INSYNC));
4671
shli@kernel.org72ac7332014-12-15 12:57:03 +11004672 sh->check_state = head_sh->check_state;
4673 sh->reconstruct_state = head_sh->reconstruct_state;
Amy Chiang448ec632018-05-16 18:59:35 +08004674 spin_lock_irq(&sh->stripe_lock);
4675 sh->batch_head = NULL;
4676 spin_unlock_irq(&sh->stripe_lock);
NeilBrownfb642b92015-05-21 12:00:47 +10004677 for (i = 0; i < sh->disks; i++) {
4678 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4679 do_wakeup = 1;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004680 sh->dev[i].flags = head_sh->dev[i].flags &
4681 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
NeilBrownfb642b92015-05-21 12:00:47 +10004682 }
NeilBrown3960ce72015-05-21 12:20:36 +10004683 if (handle_flags == 0 ||
4684 sh->state & handle_flags)
4685 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07004686 raid5_release_stripe(sh);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004687 }
NeilBrownfb642b92015-05-21 12:00:47 +10004688 spin_lock_irq(&head_sh->stripe_lock);
4689 head_sh->batch_head = NULL;
4690 spin_unlock_irq(&head_sh->stripe_lock);
4691 for (i = 0; i < head_sh->disks; i++)
4692 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4693 do_wakeup = 1;
NeilBrown3960ce72015-05-21 12:20:36 +10004694 if (head_sh->state & handle_flags)
4695 set_bit(STRIPE_HANDLE, &head_sh->state);
NeilBrownfb642b92015-05-21 12:00:47 +10004696
4697 if (do_wakeup)
4698 wake_up(&head_sh->raid_conf->wait_for_overlap);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004699}
4700
NeilBrowncc940152011-07-26 11:35:35 +10004701static void handle_stripe(struct stripe_head *sh)
4702{
4703 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11004704 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10004705 int i;
NeilBrown84789552011-07-27 11:00:36 +10004706 int prexor;
4707 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004708 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10004709
4710 clear_bit(STRIPE_HANDLE, &sh->state);
Guoqing Jianga377a472020-06-16 11:25:50 +02004711
4712 /*
4713 * handle_stripe should not continue handle the batched stripe, only
4714 * the head of batch list or lone stripe can continue. Otherwise we
4715 * could see break_stripe_batch_list warns about the STRIPE_ACTIVE
4716 * is set for the batched stripe.
4717 */
4718 if (clear_batch_ready(sh))
4719 return;
4720
Dan Williams257a4b42011-11-08 16:22:06 +11004721 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10004722 /* already being handled, ensure it gets handled
4723 * again when current action finishes */
4724 set_bit(STRIPE_HANDLE, &sh->state);
4725 return;
4726 }
4727
NeilBrown4e3d62f2015-05-21 11:50:16 +10004728 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
NeilBrown3960ce72015-05-21 12:20:36 +10004729 break_stripe_batch_list(sh, 0);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004730
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004731 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
NeilBrownf8dfcff2013-03-12 12:18:06 +11004732 spin_lock(&sh->stripe_lock);
Song Liu5ddf0442017-05-11 17:03:44 -07004733 /*
4734 * Cannot process 'sync' concurrently with 'discard'.
4735 * Flush data in r5cache before 'sync'.
4736 */
4737 if (!test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) &&
4738 !test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) &&
4739 !test_bit(STRIPE_DISCARD, &sh->state) &&
NeilBrownf8dfcff2013-03-12 12:18:06 +11004740 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4741 set_bit(STRIPE_SYNCING, &sh->state);
4742 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10004743 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004744 }
4745 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10004746 }
4747 clear_bit(STRIPE_DELAYED, &sh->state);
4748
4749 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4750 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4751 (unsigned long long)sh->sector, sh->state,
4752 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4753 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10004754
NeilBrownacfe7262011-07-27 11:00:36 +10004755 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10004756
Shaohua Lib70abcb2015-08-13 14:31:58 -07004757 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
4758 goto finish;
4759
NeilBrown16d997b2017-03-15 14:05:12 +11004760 if (s.handle_bad_blocks ||
4761 test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004762 set_bit(STRIPE_HANDLE, &sh->state);
4763 goto finish;
4764 }
4765
NeilBrown474af965fe2011-07-27 11:00:36 +10004766 if (unlikely(s.blocked_rdev)) {
4767 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11004768 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10004769 set_bit(STRIPE_HANDLE, &sh->state);
4770 goto finish;
4771 }
4772 /* There is nothing for the blocked_rdev to block */
4773 rdev_dec_pending(s.blocked_rdev, conf->mddev);
4774 s.blocked_rdev = NULL;
4775 }
4776
4777 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4778 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4779 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4780 }
4781
4782 pr_debug("locked=%d uptodate=%d to_read=%d"
4783 " to_write=%d failed=%d failed_num=%d,%d\n",
4784 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4785 s.failed_num[0], s.failed_num[1]);
Song Liu70d466f2017-05-11 15:28:28 -07004786 /*
4787 * check if the array has lost more than max_degraded devices and,
NeilBrown474af965fe2011-07-27 11:00:36 +10004788 * if so, some requests might need to be failed.
Song Liu70d466f2017-05-11 15:28:28 -07004789 *
4790 * When journal device failed (log_failed), we will only process
4791 * the stripe if there is data need write to raid disks
NeilBrown474af965fe2011-07-27 11:00:36 +10004792 */
Song Liu70d466f2017-05-11 15:28:28 -07004793 if (s.failed > conf->max_degraded ||
4794 (s.log_failed && s.injournal == 0)) {
NeilBrown9a3f5302011-11-08 16:22:01 +11004795 sh->check_state = 0;
4796 sh->reconstruct_state = 0;
NeilBrown626f2092015-05-22 14:03:10 +10004797 break_stripe_batch_list(sh, 0);
NeilBrown9a3f5302011-11-08 16:22:01 +11004798 if (s.to_read+s.to_write+s.written)
NeilBrownbd83d0a2017-03-15 14:05:12 +11004799 handle_failed_stripe(conf, sh, &s, disks);
NeilBrown9a3e1102011-12-23 10:17:53 +11004800 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11004801 handle_failed_sync(conf, sh, &s);
4802 }
NeilBrown474af965fe2011-07-27 11:00:36 +10004803
NeilBrown84789552011-07-27 11:00:36 +10004804 /* Now we check to see if any write operations have recently
4805 * completed
4806 */
4807 prexor = 0;
4808 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4809 prexor = 1;
4810 if (sh->reconstruct_state == reconstruct_state_drain_result ||
4811 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
4812 sh->reconstruct_state = reconstruct_state_idle;
4813
4814 /* All the 'written' buffers and the parity block are ready to
4815 * be written back to disk
4816 */
Shaohua Li9e4447682012-10-11 13:49:49 +11004817 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
4818 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004819 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11004820 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
4821 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004822 for (i = disks; i--; ) {
4823 struct r5dev *dev = &sh->dev[i];
4824 if (test_bit(R5_LOCKED, &dev->flags) &&
4825 (i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08004826 dev->written || test_bit(R5_InJournal,
4827 &dev->flags))) {
NeilBrown84789552011-07-27 11:00:36 +10004828 pr_debug("Writing block %d\n", i);
4829 set_bit(R5_Wantwrite, &dev->flags);
4830 if (prexor)
4831 continue;
NeilBrown9c4bdf62014-08-13 09:57:07 +10004832 if (s.failed > 1)
4833 continue;
NeilBrown84789552011-07-27 11:00:36 +10004834 if (!test_bit(R5_Insync, &dev->flags) ||
4835 ((i == sh->pd_idx || i == sh->qd_idx) &&
4836 s.failed == 0))
4837 set_bit(STRIPE_INSYNC, &sh->state);
4838 }
4839 }
4840 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4841 s.dec_preread_active = 1;
4842 }
4843
NeilBrownef5b7c62012-11-22 09:13:36 +11004844 /*
4845 * might be able to return some write requests if the parity blocks
4846 * are safe, or on a failed drive
4847 */
4848 pdev = &sh->dev[sh->pd_idx];
4849 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
4850 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
4851 qdev = &sh->dev[sh->qd_idx];
4852 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
4853 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
4854 || conf->level < 6;
4855
4856 if (s.written &&
4857 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
4858 && !test_bit(R5_LOCKED, &pdev->flags)
4859 && (test_bit(R5_UPTODATE, &pdev->flags) ||
4860 test_bit(R5_Discard, &pdev->flags))))) &&
4861 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
4862 && !test_bit(R5_LOCKED, &qdev->flags)
4863 && (test_bit(R5_UPTODATE, &qdev->flags) ||
4864 test_bit(R5_Discard, &qdev->flags))))))
NeilBrownbd83d0a2017-03-15 14:05:12 +11004865 handle_stripe_clean_event(conf, sh, disks);
NeilBrownef5b7c62012-11-22 09:13:36 +11004866
Song Liu1e6d6902016-11-17 15:24:39 -08004867 if (s.just_cached)
NeilBrownbd83d0a2017-03-15 14:05:12 +11004868 r5c_handle_cached_data_endio(conf, sh, disks);
Artur Paszkiewiczff875732017-03-09 09:59:58 +01004869 log_stripe_write_finished(sh);
Song Liu1e6d6902016-11-17 15:24:39 -08004870
NeilBrownef5b7c62012-11-22 09:13:36 +11004871 /* Now we might consider reading some blocks, either to check/generate
4872 * parity, or to satisfy requests
4873 * or to load a block that is being partially written.
4874 */
4875 if (s.to_read || s.non_overwrite
ChangSyun Penga1c6ae32020-07-31 17:50:17 +08004876 || (s.to_write && s.failed)
NeilBrownef5b7c62012-11-22 09:13:36 +11004877 || (s.syncing && (s.uptodate + s.compute < disks))
4878 || s.replacing
4879 || s.expanding)
4880 handle_stripe_fill(sh, &s, disks);
4881
Song Liu2ded3702016-11-17 15:24:38 -08004882 /*
4883 * When the stripe finishes full journal write cycle (write to journal
4884 * and raid disk), this is the clean up procedure so it is ready for
4885 * next operation.
4886 */
4887 r5c_finish_stripe_write_out(conf, sh, &s);
4888
4889 /*
4890 * Now to consider new write requests, cache write back and what else,
4891 * if anything should be read. We do not handle new writes when:
NeilBrown84789552011-07-27 11:00:36 +10004892 * 1/ A 'write' operation (copy+xor) is already in flight.
4893 * 2/ A 'check' operation is in flight, as it may clobber the parity
4894 * block.
Song Liu2ded3702016-11-17 15:24:38 -08004895 * 3/ A r5c cache log write is in flight.
NeilBrown84789552011-07-27 11:00:36 +10004896 */
Song Liu2ded3702016-11-17 15:24:38 -08004897
4898 if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
4899 if (!r5c_is_writeback(conf->log)) {
4900 if (s.to_write)
4901 handle_stripe_dirtying(conf, sh, &s, disks);
4902 } else { /* write back cache */
4903 int ret = 0;
4904
4905 /* First, try handle writes in caching phase */
4906 if (s.to_write)
4907 ret = r5c_try_caching_write(conf, sh, &s,
4908 disks);
4909 /*
4910 * If caching phase failed: ret == -EAGAIN
4911 * OR
4912 * stripe under reclaim: !caching && injournal
4913 *
4914 * fall back to handle_stripe_dirtying()
4915 */
4916 if (ret == -EAGAIN ||
4917 /* stripe under reclaim: !caching && injournal */
4918 (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
Song Liud7bd3982016-11-23 22:50:39 -08004919 s.injournal > 0)) {
4920 ret = handle_stripe_dirtying(conf, sh, &s,
4921 disks);
4922 if (ret == -EAGAIN)
4923 goto finish;
4924 }
Song Liu2ded3702016-11-17 15:24:38 -08004925 }
4926 }
NeilBrown84789552011-07-27 11:00:36 +10004927
4928 /* maybe we need to check and possibly fix the parity for this stripe
4929 * Any reads will already have been scheduled, so we just see if enough
4930 * data is available. The parity check is held off while parity
4931 * dependent operations are in flight.
4932 */
4933 if (sh->check_state ||
4934 (s.syncing && s.locked == 0 &&
4935 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
4936 !test_bit(STRIPE_INSYNC, &sh->state))) {
4937 if (conf->level == 6)
4938 handle_parity_checks6(conf, sh, &s, disks);
4939 else
4940 handle_parity_checks5(conf, sh, &s, disks);
4941 }
NeilBrownc5a31002011-07-27 11:00:36 +10004942
NeilBrownf94c0b62013-07-22 12:57:21 +10004943 if ((s.replacing || s.syncing) && s.locked == 0
4944 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
4945 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11004946 /* Write out to replacement devices where possible */
4947 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10004948 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
4949 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11004950 set_bit(R5_WantReplace, &sh->dev[i].flags);
4951 set_bit(R5_LOCKED, &sh->dev[i].flags);
4952 s.locked++;
4953 }
NeilBrownf94c0b62013-07-22 12:57:21 +10004954 if (s.replacing)
4955 set_bit(STRIPE_INSYNC, &sh->state);
4956 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11004957 }
4958 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10004959 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11004960 test_bit(STRIPE_INSYNC, &sh->state)) {
Yufen Yuc911c462020-07-18 05:29:07 -04004961 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1);
NeilBrownc5a31002011-07-27 11:00:36 +10004962 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004963 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
4964 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10004965 }
4966
4967 /* If the failed drives are just a ReadError, then we might need
4968 * to progress the repair/check process
4969 */
4970 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
4971 for (i = 0; i < s.failed; i++) {
4972 struct r5dev *dev = &sh->dev[s.failed_num[i]];
4973 if (test_bit(R5_ReadError, &dev->flags)
4974 && !test_bit(R5_LOCKED, &dev->flags)
4975 && test_bit(R5_UPTODATE, &dev->flags)
4976 ) {
4977 if (!test_bit(R5_ReWrite, &dev->flags)) {
4978 set_bit(R5_Wantwrite, &dev->flags);
4979 set_bit(R5_ReWrite, &dev->flags);
Guoqing Jiang3a31cf32020-07-28 12:01:43 +02004980 } else
NeilBrownc5a31002011-07-27 11:00:36 +10004981 /* let's read it back */
4982 set_bit(R5_Wantread, &dev->flags);
Guoqing Jiang3a31cf32020-07-28 12:01:43 +02004983 set_bit(R5_LOCKED, &dev->flags);
4984 s.locked++;
NeilBrownc5a31002011-07-27 11:00:36 +10004985 }
4986 }
4987
NeilBrown3687c062011-07-27 11:00:36 +10004988 /* Finish reconstruct operations initiated by the expansion process */
4989 if (sh->reconstruct_state == reconstruct_state_result) {
4990 struct stripe_head *sh_src
Shaohua Li6d036f72015-08-13 14:31:57 -07004991 = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrown3687c062011-07-27 11:00:36 +10004992 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
4993 /* sh cannot be written until sh_src has been read.
4994 * so arrange for sh to be delayed a little
4995 */
4996 set_bit(STRIPE_DELAYED, &sh->state);
4997 set_bit(STRIPE_HANDLE, &sh->state);
4998 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
4999 &sh_src->state))
5000 atomic_inc(&conf->preread_active_stripes);
Shaohua Li6d036f72015-08-13 14:31:57 -07005001 raid5_release_stripe(sh_src);
NeilBrown3687c062011-07-27 11:00:36 +10005002 goto finish;
5003 }
5004 if (sh_src)
Shaohua Li6d036f72015-08-13 14:31:57 -07005005 raid5_release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07005006
NeilBrown3687c062011-07-27 11:00:36 +10005007 sh->reconstruct_state = reconstruct_state_idle;
5008 clear_bit(STRIPE_EXPANDING, &sh->state);
5009 for (i = conf->raid_disks; i--; ) {
5010 set_bit(R5_Wantwrite, &sh->dev[i].flags);
5011 set_bit(R5_LOCKED, &sh->dev[i].flags);
5012 s.locked++;
5013 }
5014 }
5015
5016 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
5017 !sh->reconstruct_state) {
5018 /* Need to write out all blocks after computing parity */
5019 sh->disks = conf->raid_disks;
5020 stripe_set_idx(sh->sector, conf, 0, sh);
5021 schedule_reconstruction(sh, &s, 1, 1);
5022 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
5023 clear_bit(STRIPE_EXPAND_READY, &sh->state);
5024 atomic_dec(&conf->reshape_stripes);
5025 wake_up(&conf->wait_for_overlap);
Yufen Yuc911c462020-07-18 05:29:07 -04005026 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1);
NeilBrown3687c062011-07-27 11:00:36 +10005027 }
5028
5029 if (s.expanding && s.locked == 0 &&
5030 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
5031 handle_stripe_expansion(conf, sh);
5032
5033finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07005034 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10005035 if (unlikely(s.blocked_rdev)) {
5036 if (conf->mddev->external)
5037 md_wait_for_blocked_rdev(s.blocked_rdev,
5038 conf->mddev);
5039 else
5040 /* Internal metadata will immediately
5041 * be written by raid5d, so we don't
5042 * need to wait here.
5043 */
5044 rdev_dec_pending(s.blocked_rdev,
5045 conf->mddev);
5046 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07005047
NeilBrownbc2607f2011-07-28 11:39:22 +10005048 if (s.handle_bad_blocks)
5049 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11005050 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10005051 struct r5dev *dev = &sh->dev[i];
5052 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
5053 /* We own a safe reference to the rdev */
5054 rdev = conf->disks[i].rdev;
5055 if (!rdev_set_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04005056 RAID5_STRIPE_SECTORS(conf), 0))
NeilBrownbc2607f2011-07-28 11:39:22 +10005057 md_error(conf->mddev, rdev);
5058 rdev_dec_pending(rdev, conf->mddev);
5059 }
NeilBrownb84db562011-07-28 11:39:23 +10005060 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
5061 rdev = conf->disks[i].rdev;
5062 rdev_clear_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04005063 RAID5_STRIPE_SECTORS(conf), 0);
NeilBrownb84db562011-07-28 11:39:23 +10005064 rdev_dec_pending(rdev, conf->mddev);
5065 }
NeilBrown977df362011-12-23 10:17:53 +11005066 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
5067 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11005068 if (!rdev)
5069 /* rdev have been moved down */
5070 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11005071 rdev_clear_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04005072 RAID5_STRIPE_SECTORS(conf), 0);
NeilBrown977df362011-12-23 10:17:53 +11005073 rdev_dec_pending(rdev, conf->mddev);
5074 }
NeilBrownbc2607f2011-07-28 11:39:22 +10005075 }
5076
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07005077 if (s.ops_request)
5078 raid_run_ops(sh, s.ops_request);
5079
Dan Williamsf0e43bc2008-06-28 08:31:55 +10005080 ops_run_io(sh, &s);
5081
NeilBrownc5709ef2011-07-26 11:35:20 +10005082 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11005083 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02005084 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11005085 * have actually been submitted.
5086 */
5087 atomic_dec(&conf->preread_active_stripes);
5088 if (atomic_read(&conf->preread_active_stripes) <
5089 IO_THRESHOLD)
5090 md_wakeup_thread(conf->mddev->thread);
5091 }
5092
Dan Williams257a4b42011-11-08 16:22:06 +11005093 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07005094}
5095
NeilBrownd1688a62011-10-11 16:49:52 +11005096static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097{
5098 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
5099 while (!list_empty(&conf->delayed_list)) {
5100 struct list_head *l = conf->delayed_list.next;
5101 struct stripe_head *sh;
5102 sh = list_entry(l, struct stripe_head, lru);
5103 list_del_init(l);
5104 clear_bit(STRIPE_DELAYED, &sh->state);
5105 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5106 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005107 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08005108 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 }
NeilBrown482c0832011-04-18 18:25:42 +10005110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111}
5112
Shaohua Li566c09c2013-11-14 15:16:17 +11005113static void activate_bit_delay(struct r5conf *conf,
5114 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07005115{
5116 /* device_lock is held */
5117 struct list_head head;
5118 list_add(&head, &conf->bitmap_list);
5119 list_del_init(&conf->bitmap_list);
5120 while (!list_empty(&head)) {
5121 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11005122 int hash;
NeilBrown72626682005-09-09 16:23:54 -07005123 list_del_init(&sh->lru);
5124 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11005125 hash = sh->hash_lock_index;
5126 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07005127 }
5128}
5129
NeilBrownfd01b882011-10-11 16:47:53 +11005130static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005131{
NeilBrown3cb5edf2015-07-15 17:24:17 +10005132 struct r5conf *conf = mddev->private;
Christoph Hellwig10433d02017-08-23 19:10:28 +02005133 sector_t sector = bio->bi_iter.bi_sector;
NeilBrown3cb5edf2015-07-15 17:24:17 +10005134 unsigned int chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005135 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005136
Christoph Hellwig10433d02017-08-23 19:10:28 +02005137 WARN_ON_ONCE(bio->bi_partno);
5138
NeilBrown3cb5edf2015-07-15 17:24:17 +10005139 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005140 return chunk_sectors >=
5141 ((sector & (chunk_sectors - 1)) + bio_sectors);
5142}
5143
5144/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005145 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
5146 * later sampled by raid5d.
5147 */
NeilBrownd1688a62011-10-11 16:49:52 +11005148static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005149{
5150 unsigned long flags;
5151
5152 spin_lock_irqsave(&conf->device_lock, flags);
5153
5154 bi->bi_next = conf->retry_read_aligned_list;
5155 conf->retry_read_aligned_list = bi;
5156
5157 spin_unlock_irqrestore(&conf->device_lock, flags);
5158 md_wakeup_thread(conf->mddev->thread);
5159}
5160
NeilBrown0472a422017-03-15 14:05:13 +11005161static struct bio *remove_bio_from_retry(struct r5conf *conf,
5162 unsigned int *offset)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005163{
5164 struct bio *bi;
5165
5166 bi = conf->retry_read_aligned;
5167 if (bi) {
NeilBrown0472a422017-03-15 14:05:13 +11005168 *offset = conf->retry_read_offset;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005169 conf->retry_read_aligned = NULL;
5170 return bi;
5171 }
5172 bi = conf->retry_read_aligned_list;
5173 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08005174 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005175 bi->bi_next = NULL;
NeilBrown0472a422017-03-15 14:05:13 +11005176 *offset = 0;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005177 }
5178
5179 return bi;
5180}
5181
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005182/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005183 * The "raid5_align_endio" should check if the read succeeded and if it
5184 * did, call bio_endio on the original bio (having bio_put the new bio
5185 * first).
5186 * If the read failed..
5187 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005188static void raid5_align_endio(struct bio *bi)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005189{
5190 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11005191 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005192 struct r5conf *conf;
NeilBrown3cb03002011-10-11 16:45:26 +11005193 struct md_rdev *rdev;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02005194 blk_status_t error = bi->bi_status;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005195
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005196 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005197
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005198 rdev = (void*)raid_bi->bi_next;
5199 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11005200 mddev = rdev->mddev;
5201 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005202
5203 rdev_dec_pending(rdev, conf->mddev);
5204
Sasha Levin9b81c842015-08-10 19:05:18 -04005205 if (!error) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005206 bio_endio(raid_bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005207 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10005208 wake_up(&conf->wait_for_quiescent);
NeilBrown6712ecf2007-09-27 12:47:43 +02005209 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005210 }
5211
Dan Williams45b42332007-07-09 11:56:43 -07005212 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005213
5214 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005215}
5216
Ming Lin7ef6b122015-05-06 22:51:24 -07005217static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005218{
NeilBrownd1688a62011-10-11 16:49:52 +11005219 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11005220 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005221 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11005222 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11005223 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005224
5225 if (!in_chunk_boundary(mddev, raid_bio)) {
Ming Lin7ef6b122015-05-06 22:51:24 -07005226 pr_debug("%s: non aligned\n", __func__);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005227 return 0;
5228 }
5229 /*
Ming Leid7a10302017-02-14 23:29:03 +08005230 * use bio_clone_fast to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005231 */
Kent Overstreetafeee512018-05-20 18:25:52 -04005232 align_bi = bio_clone_fast(raid_bio, GFP_NOIO, &mddev->bio_set);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005233 if (!align_bi)
5234 return 0;
5235 /*
5236 * set bi_end_io to a new function, and set bi_private to the
5237 * original bio.
5238 */
5239 align_bi->bi_end_io = raid5_align_endio;
5240 align_bi->bi_private = raid_bio;
5241 /*
5242 * compute position
5243 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005244 align_bi->bi_iter.bi_sector =
5245 raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
5246 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005247
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005248 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005249 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11005250 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
5251 if (!rdev || test_bit(Faulty, &rdev->flags) ||
5252 rdev->recovery_offset < end_sector) {
5253 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
5254 if (rdev &&
5255 (test_bit(Faulty, &rdev->flags) ||
5256 !(test_bit(In_sync, &rdev->flags) ||
5257 rdev->recovery_offset >= end_sector)))
5258 rdev = NULL;
5259 }
Song Liu03b047f2017-01-11 13:39:14 -08005260
5261 if (r5c_big_stripe_cached(conf, align_bi->bi_iter.bi_sector)) {
5262 rcu_read_unlock();
5263 bio_put(align_bi);
5264 return 0;
5265 }
5266
NeilBrown671488c2011-12-23 10:17:52 +11005267 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10005268 sector_t first_bad;
5269 int bad_sectors;
5270
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005271 atomic_inc(&rdev->nr_pending);
5272 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005273 raid_bio->bi_next = (void*)rdev;
Christoph Hellwig74d46992017-08-23 19:10:32 +02005274 bio_set_dev(align_bi, rdev->bdev);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005275
Kent Overstreet7140aaf2013-09-25 13:37:01 -07005276 if (is_badblock(rdev, align_bi->bi_iter.bi_sector,
Kent Overstreet4f024f32013-10-11 15:44:27 -07005277 bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10005278 &first_bad, &bad_sectors)) {
Neil Brown387bb172007-02-08 14:20:29 -08005279 bio_put(align_bi);
5280 rdev_dec_pending(rdev, mddev);
5281 return 0;
5282 }
5283
majianpeng6c0544e2012-06-12 08:31:10 +08005284 /* No reshape active, so we can trust rdev->data_offset */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005285 align_bi->bi_iter.bi_sector += rdev->data_offset;
majianpeng6c0544e2012-06-12 08:31:10 +08005286
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005287 spin_lock_irq(&conf->device_lock);
Yuanhan Liub1b46482015-05-08 18:19:06 +10005288 wait_event_lock_irq(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005289 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01005290 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005291 atomic_inc(&conf->active_aligned_reads);
5292 spin_unlock_irq(&conf->device_lock);
5293
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005294 if (mddev->gendisk)
Christoph Hellwig74d46992017-08-23 19:10:32 +02005295 trace_block_bio_remap(align_bi->bi_disk->queue,
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005296 align_bi, disk_devt(mddev->gendisk),
Kent Overstreet4f024f32013-10-11 15:44:27 -07005297 raid_bio->bi_iter.bi_sector);
Christoph Hellwiged00aab2020-07-01 10:59:44 +02005298 submit_bio_noacct(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005299 return 1;
5300 } else {
5301 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005302 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005303 return 0;
5304 }
5305}
5306
Ming Lin7ef6b122015-05-06 22:51:24 -07005307static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
5308{
5309 struct bio *split;
NeilBrowndd7a8f52017-04-05 14:05:51 +10005310 sector_t sector = raid_bio->bi_iter.bi_sector;
5311 unsigned chunk_sects = mddev->chunk_sectors;
5312 unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
Ming Lin7ef6b122015-05-06 22:51:24 -07005313
NeilBrowndd7a8f52017-04-05 14:05:51 +10005314 if (sectors < bio_sectors(raid_bio)) {
5315 struct r5conf *conf = mddev->private;
Kent Overstreetafeee512018-05-20 18:25:52 -04005316 split = bio_split(raid_bio, sectors, GFP_NOIO, &conf->bio_split);
NeilBrowndd7a8f52017-04-05 14:05:51 +10005317 bio_chain(split, raid_bio);
Christoph Hellwiged00aab2020-07-01 10:59:44 +02005318 submit_bio_noacct(raid_bio);
NeilBrowndd7a8f52017-04-05 14:05:51 +10005319 raid_bio = split;
5320 }
Ming Lin7ef6b122015-05-06 22:51:24 -07005321
NeilBrowndd7a8f52017-04-05 14:05:51 +10005322 if (!raid5_read_one_chunk(mddev, raid_bio))
5323 return raid_bio;
Ming Lin7ef6b122015-05-06 22:51:24 -07005324
5325 return NULL;
5326}
5327
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005328/* __get_priority_stripe - get the next stripe to process
5329 *
5330 * Full stripe writes are allowed to pass preread active stripes up until
5331 * the bypass_threshold is exceeded. In general the bypass_count
5332 * increments when the handle_list is handled before the hold_list; however, it
5333 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
5334 * stripe with in flight i/o. The bypass_count will be reset when the
5335 * head of the hold_list has changed, i.e. the head was promoted to the
5336 * handle_list.
5337 */
Shaohua Li851c30c2013-08-28 14:30:16 +08005338static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005339{
Shaohua Li535ae4e2017-02-15 19:37:32 -08005340 struct stripe_head *sh, *tmp;
Shaohua Li851c30c2013-08-28 14:30:16 +08005341 struct list_head *handle_list = NULL;
Shaohua Li535ae4e2017-02-15 19:37:32 -08005342 struct r5worker_group *wg;
Song Liu70d466f2017-05-11 15:28:28 -07005343 bool second_try = !r5c_is_writeback(conf->log) &&
5344 !r5l_log_disk_error(conf);
5345 bool try_loprio = test_bit(R5C_LOG_TIGHT, &conf->cache_state) ||
5346 r5l_log_disk_error(conf);
Shaohua Li851c30c2013-08-28 14:30:16 +08005347
Shaohua Li535ae4e2017-02-15 19:37:32 -08005348again:
5349 wg = NULL;
5350 sh = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005351 if (conf->worker_cnt_per_group == 0) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005352 handle_list = try_loprio ? &conf->loprio_list :
5353 &conf->handle_list;
Shaohua Li851c30c2013-08-28 14:30:16 +08005354 } else if (group != ANY_GROUP) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005355 handle_list = try_loprio ? &conf->worker_groups[group].loprio_list :
5356 &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005357 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08005358 } else {
5359 int i;
5360 for (i = 0; i < conf->group_cnt; i++) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005361 handle_list = try_loprio ? &conf->worker_groups[i].loprio_list :
5362 &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005363 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08005364 if (!list_empty(handle_list))
5365 break;
5366 }
5367 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005368
5369 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5370 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08005371 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005372 list_empty(&conf->hold_list) ? "empty" : "busy",
5373 atomic_read(&conf->pending_full_writes), conf->bypass_count);
5374
Shaohua Li851c30c2013-08-28 14:30:16 +08005375 if (!list_empty(handle_list)) {
5376 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005377
5378 if (list_empty(&conf->hold_list))
5379 conf->bypass_count = 0;
5380 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
5381 if (conf->hold_list.next == conf->last_hold)
5382 conf->bypass_count++;
5383 else {
5384 conf->last_hold = conf->hold_list.next;
5385 conf->bypass_count -= conf->bypass_threshold;
5386 if (conf->bypass_count < 0)
5387 conf->bypass_count = 0;
5388 }
5389 }
5390 } else if (!list_empty(&conf->hold_list) &&
5391 ((conf->bypass_threshold &&
5392 conf->bypass_count > conf->bypass_threshold) ||
5393 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08005394
5395 list_for_each_entry(tmp, &conf->hold_list, lru) {
5396 if (conf->worker_cnt_per_group == 0 ||
5397 group == ANY_GROUP ||
5398 !cpu_online(tmp->cpu) ||
5399 cpu_to_group(tmp->cpu) == group) {
5400 sh = tmp;
5401 break;
5402 }
5403 }
5404
5405 if (sh) {
5406 conf->bypass_count -= conf->bypass_threshold;
5407 if (conf->bypass_count < 0)
5408 conf->bypass_count = 0;
5409 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08005410 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005411 }
5412
Shaohua Li535ae4e2017-02-15 19:37:32 -08005413 if (!sh) {
5414 if (second_try)
5415 return NULL;
5416 second_try = true;
5417 try_loprio = !try_loprio;
5418 goto again;
5419 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005420
Shaohua Libfc90cb2013-08-29 15:40:32 +08005421 if (wg) {
5422 wg->stripes_cnt--;
5423 sh->group = NULL;
5424 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005425 list_del_init(&sh->lru);
Shaohua Lic7a6d352014-04-15 09:12:54 +08005426 BUG_ON(atomic_inc_return(&sh->count) != 1);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005427 return sh;
5428}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005429
Shaohua Li8811b592012-08-02 08:33:00 +10005430struct raid5_plug_cb {
5431 struct blk_plug_cb cb;
5432 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11005433 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10005434};
5435
5436static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
5437{
5438 struct raid5_plug_cb *cb = container_of(
5439 blk_cb, struct raid5_plug_cb, cb);
5440 struct stripe_head *sh;
5441 struct mddev *mddev = cb->cb.data;
5442 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11005443 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11005444 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10005445
5446 if (cb->list.next && !list_empty(&cb->list)) {
5447 spin_lock_irq(&conf->device_lock);
5448 while (!list_empty(&cb->list)) {
5449 sh = list_first_entry(&cb->list, struct stripe_head, lru);
5450 list_del_init(&sh->lru);
5451 /*
5452 * avoid race release_stripe_plug() sees
5453 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5454 * is still in our list
5455 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005456 smp_mb__before_atomic();
Shaohua Li8811b592012-08-02 08:33:00 +10005457 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08005458 /*
5459 * STRIPE_ON_RELEASE_LIST could be set here. In that
5460 * case, the count is always > 1 here
5461 */
Shaohua Li566c09c2013-11-14 15:16:17 +11005462 hash = sh->hash_lock_index;
5463 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11005464 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10005465 }
5466 spin_unlock_irq(&conf->device_lock);
5467 }
Shaohua Li566c09c2013-11-14 15:16:17 +11005468 release_inactive_stripe_list(conf, cb->temp_inactive_list,
5469 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005470 if (mddev->queue)
5471 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10005472 kfree(cb);
5473}
5474
5475static void release_stripe_plug(struct mddev *mddev,
5476 struct stripe_head *sh)
5477{
5478 struct blk_plug_cb *blk_cb = blk_check_plugged(
5479 raid5_unplug, mddev,
5480 sizeof(struct raid5_plug_cb));
5481 struct raid5_plug_cb *cb;
5482
5483 if (!blk_cb) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005484 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005485 return;
5486 }
5487
5488 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
5489
Shaohua Li566c09c2013-11-14 15:16:17 +11005490 if (cb->list.next == NULL) {
5491 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10005492 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11005493 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5494 INIT_LIST_HEAD(cb->temp_inactive_list + i);
5495 }
Shaohua Li8811b592012-08-02 08:33:00 +10005496
5497 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
5498 list_add_tail(&sh->lru, &cb->list);
5499 else
Shaohua Li6d036f72015-08-13 14:31:57 -07005500 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005501}
5502
Shaohua Li620125f2012-10-11 13:49:05 +11005503static void make_discard_request(struct mddev *mddev, struct bio *bi)
5504{
5505 struct r5conf *conf = mddev->private;
5506 sector_t logical_sector, last_sector;
5507 struct stripe_head *sh;
Shaohua Li620125f2012-10-11 13:49:05 +11005508 int stripe_sectors;
5509
5510 if (mddev->reshape_position != MaxSector)
5511 /* Skip discard while reshape is happening */
5512 return;
5513
Yufen Yuc911c462020-07-18 05:29:07 -04005514 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
Guoqing Jiangb0f01ec2019-09-03 11:41:03 +02005515 last_sector = bio_end_sector(bi);
Shaohua Li620125f2012-10-11 13:49:05 +11005516
5517 bi->bi_next = NULL;
Shaohua Li620125f2012-10-11 13:49:05 +11005518
5519 stripe_sectors = conf->chunk_sectors *
5520 (conf->raid_disks - conf->max_degraded);
5521 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5522 stripe_sectors);
5523 sector_div(last_sector, stripe_sectors);
5524
5525 logical_sector *= conf->chunk_sectors;
5526 last_sector *= conf->chunk_sectors;
5527
5528 for (; logical_sector < last_sector;
Yufen Yuc911c462020-07-18 05:29:07 -04005529 logical_sector += RAID5_STRIPE_SECTORS(conf)) {
Shaohua Li620125f2012-10-11 13:49:05 +11005530 DEFINE_WAIT(w);
5531 int d;
5532 again:
Shaohua Li6d036f72015-08-13 14:31:57 -07005533 sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
Shaohua Li620125f2012-10-11 13:49:05 +11005534 prepare_to_wait(&conf->wait_for_overlap, &w,
5535 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005536 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5537 if (test_bit(STRIPE_SYNCING, &sh->state)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005538 raid5_release_stripe(sh);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005539 schedule();
5540 goto again;
5541 }
5542 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11005543 spin_lock_irq(&sh->stripe_lock);
5544 for (d = 0; d < conf->raid_disks; d++) {
5545 if (d == sh->pd_idx || d == sh->qd_idx)
5546 continue;
5547 if (sh->dev[d].towrite || sh->dev[d].toread) {
5548 set_bit(R5_Overlap, &sh->dev[d].flags);
5549 spin_unlock_irq(&sh->stripe_lock);
Shaohua Li6d036f72015-08-13 14:31:57 -07005550 raid5_release_stripe(sh);
Shaohua Li620125f2012-10-11 13:49:05 +11005551 schedule();
5552 goto again;
5553 }
5554 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11005555 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11005556 finish_wait(&conf->wait_for_overlap, &w);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005557 sh->overwrite_disks = 0;
Shaohua Li620125f2012-10-11 13:49:05 +11005558 for (d = 0; d < conf->raid_disks; d++) {
5559 if (d == sh->pd_idx || d == sh->qd_idx)
5560 continue;
5561 sh->dev[d].towrite = bi;
5562 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
NeilBrown016c76a2017-03-15 14:05:13 +11005563 bio_inc_remaining(bi);
NeilBrown49728052017-03-15 14:05:12 +11005564 md_write_inc(mddev, bi);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005565 sh->overwrite_disks++;
Shaohua Li620125f2012-10-11 13:49:05 +11005566 }
5567 spin_unlock_irq(&sh->stripe_lock);
5568 if (conf->mddev->bitmap) {
5569 for (d = 0;
5570 d < conf->raid_disks - conf->max_degraded;
5571 d++)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005572 md_bitmap_startwrite(mddev->bitmap,
5573 sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04005574 RAID5_STRIPE_SECTORS(conf),
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005575 0);
Shaohua Li620125f2012-10-11 13:49:05 +11005576 sh->bm_seq = conf->seq_flush + 1;
5577 set_bit(STRIPE_BIT_DELAY, &sh->state);
5578 }
5579
5580 set_bit(STRIPE_HANDLE, &sh->state);
5581 clear_bit(STRIPE_DELAYED, &sh->state);
5582 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5583 atomic_inc(&conf->preread_active_stripes);
5584 release_stripe_plug(mddev, sh);
5585 }
5586
NeilBrown016c76a2017-03-15 14:05:13 +11005587 bio_endio(bi);
Shaohua Li620125f2012-10-11 13:49:05 +11005588}
5589
NeilBrowncc27b0c2017-06-05 16:49:39 +10005590static bool raid5_make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591{
NeilBrownd1688a62011-10-11 16:49:52 +11005592 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11005593 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 sector_t new_sector;
5595 sector_t logical_sector, last_sector;
5596 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01005597 const int rw = bio_data_dir(bi);
Shaohua Li27c0f682014-04-09 11:25:47 +08005598 DEFINE_WAIT(w);
5599 bool do_prepare;
Song Liu3bddb7f2016-11-18 16:46:50 -08005600 bool do_flush = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
Jens Axboe1eff9d32016-08-05 15:35:16 -06005602 if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01005603 int ret = log_handle_flush_request(conf, bi);
Shaohua Li828cbe92015-09-02 13:49:49 -07005604
5605 if (ret == 0)
NeilBrowncc27b0c2017-06-05 16:49:39 +10005606 return true;
Shaohua Li828cbe92015-09-02 13:49:49 -07005607 if (ret == -ENODEV) {
David Jeffery775d7832019-09-16 13:15:14 -04005608 if (md_flush_request(mddev, bi))
5609 return true;
Shaohua Li828cbe92015-09-02 13:49:49 -07005610 }
5611 /* ret == -EAGAIN, fallback */
Song Liu3bddb7f2016-11-18 16:46:50 -08005612 /*
5613 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
5614 * we need to flush journal device
5615 */
5616 do_flush = bi->bi_opf & REQ_PREFLUSH;
NeilBrowne5dcdd82005-09-09 16:23:41 -07005617 }
5618
NeilBrowncc27b0c2017-06-05 16:49:39 +10005619 if (!md_write_start(mddev, bi))
5620 return false;
Eric Mei9ffc8f72015-03-18 23:39:11 -06005621 /*
5622 * If array is degraded, better not do chunk aligned read because
5623 * later we might have to read it again in order to reconstruct
5624 * data on failed drives.
5625 */
5626 if (rw == READ && mddev->degraded == 0 &&
Ming Lin7ef6b122015-05-06 22:51:24 -07005627 mddev->reshape_position == MaxSector) {
5628 bi = chunk_aligned_read(mddev, bi);
5629 if (!bi)
NeilBrowncc27b0c2017-06-05 16:49:39 +10005630 return true;
Ming Lin7ef6b122015-05-06 22:51:24 -07005631 }
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08005632
Mike Christie796a5cf2016-06-05 14:32:07 -05005633 if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
Shaohua Li620125f2012-10-11 13:49:05 +11005634 make_discard_request(mddev, bi);
NeilBrowncc27b0c2017-06-05 16:49:39 +10005635 md_write_end(mddev);
5636 return true;
Shaohua Li620125f2012-10-11 13:49:05 +11005637 }
5638
Yufen Yuc911c462020-07-18 05:29:07 -04005639 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005640 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 bi->bi_next = NULL;
NeilBrown06d91a52005-06-21 17:17:12 -07005642
Shaohua Li27c0f682014-04-09 11:25:47 +08005643 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
Yufen Yuc911c462020-07-18 05:29:07 -04005644 for (; logical_sector < last_sector; logical_sector += RAID5_STRIPE_SECTORS(conf)) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005645 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10005646 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08005647
Shaohua Li27c0f682014-04-09 11:25:47 +08005648 do_prepare = false;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005649 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10005650 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11005651 previous = 0;
Shaohua Li27c0f682014-04-09 11:25:47 +08005652 if (do_prepare)
5653 prepare_to_wait(&conf->wait_for_overlap, &w,
5654 TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005655 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11005656 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08005657 * 64bit on a 32bit platform, and so it might be
5658 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02005659 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08005660 * the lock is dropped, so once we get a reference
5661 * to the stripe that we think it is, we will have
5662 * to check again.
5663 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005664 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005665 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005666 ? logical_sector < conf->reshape_progress
5667 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005668 previous = 1;
5669 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10005670 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005671 ? logical_sector < conf->reshape_safe
5672 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08005673 spin_unlock_irq(&conf->device_lock);
5674 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005675 do_prepare = true;
NeilBrownb578d552006-03-27 01:18:12 -08005676 goto retry;
5677 }
5678 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005679 spin_unlock_irq(&conf->device_lock);
5680 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005681
NeilBrown112bf892009-03-31 14:39:38 +11005682 new_sector = raid5_compute_sector(conf, logical_sector,
5683 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11005684 &dd_idx, NULL);
Shaohua Li849674e2016-01-20 13:52:20 -08005685 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10005686 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 (unsigned long long)logical_sector);
5688
Shaohua Li6d036f72015-08-13 14:31:57 -07005689 sh = raid5_get_active_stripe(conf, new_sector, previous,
Jens Axboe1eff9d32016-08-05 15:35:16 -06005690 (bi->bi_opf & REQ_RAHEAD), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11005692 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005693 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08005694 * stripe, so we must do the range check again.
5695 * Expansion could still move past after this
5696 * test, but as we are holding a reference to
5697 * 'sh', we know that if that happens,
5698 * STRIPE_EXPANDING will get set and the expansion
5699 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005700 */
5701 int must_retry = 0;
5702 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005703 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11005704 ? logical_sector >= conf->reshape_progress
5705 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005706 /* mismatch, need to try again */
5707 must_retry = 1;
5708 spin_unlock_irq(&conf->device_lock);
5709 if (must_retry) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005710 raid5_release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07005711 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005712 do_prepare = true;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005713 goto retry;
5714 }
5715 }
NeilBrownc46501b2013-08-27 15:52:13 +10005716 if (read_seqcount_retry(&conf->gen_lock, seq)) {
5717 /* Might have got the wrong stripe_head
5718 * by accident
5719 */
Shaohua Li6d036f72015-08-13 14:31:57 -07005720 raid5_release_stripe(sh);
NeilBrownc46501b2013-08-27 15:52:13 +10005721 goto retry;
5722 }
NeilBrowne62e58a2009-07-01 13:15:35 +10005723
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005724 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
shli@kernel.orgda41ba62014-12-15 12:57:03 +11005725 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005726 /* Stripe is busy expanding or
5727 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 * and wait a while
5729 */
NeilBrown482c0832011-04-18 18:25:42 +10005730 md_wakeup_thread(mddev->thread);
Shaohua Li6d036f72015-08-13 14:31:57 -07005731 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005733 do_prepare = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 goto retry;
5735 }
Song Liu3bddb7f2016-11-18 16:46:50 -08005736 if (do_flush) {
5737 set_bit(STRIPE_R5C_PREFLUSH, &sh->state);
5738 /* we only need flush for one stripe */
5739 do_flush = false;
5740 }
5741
Guoqing Jiang1684e972020-06-16 11:25:52 +02005742 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrown6ed30032008-02-06 01:40:00 -08005743 clear_bit(STRIPE_DELAYED, &sh->state);
shli@kernel.org59fc6302014-12-15 12:57:03 +11005744 if ((!sh->batch_head || sh == sh->batch_head) &&
Jens Axboe1eff9d32016-08-05 15:35:16 -06005745 (bi->bi_opf & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11005746 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5747 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10005748 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 } else {
5750 /* cannot get stripe for read-ahead, just give-up */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02005751 bi->bi_status = BLK_STS_IOERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 break;
5753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 }
Shaohua Li27c0f682014-04-09 11:25:47 +08005755 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown7c13edc2011-04-18 18:25:43 +10005756
NeilBrown49728052017-03-15 14:05:12 +11005757 if (rw == WRITE)
5758 md_write_end(mddev);
NeilBrown016c76a2017-03-15 14:05:13 +11005759 bio_endio(bi);
NeilBrowncc27b0c2017-06-05 16:49:39 +10005760 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761}
5762
NeilBrownfd01b882011-10-11 16:47:53 +11005763static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11005764
NeilBrownfd01b882011-10-11 16:47:53 +11005765static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766{
NeilBrown52c03292006-06-26 00:27:43 -07005767 /* reshaping is quite different to recovery/resync so it is
5768 * handled quite separately ... here.
5769 *
5770 * On each call to sync_request, we gather one chunk worth of
5771 * destination stripes and flag them as expanding.
5772 * Then we find all the source stripes and request reads.
5773 * As the reads complete, handle_stripe will copy the data
5774 * into the destination stripe and release that stripe.
5775 */
NeilBrownd1688a62011-10-11 16:49:52 +11005776 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 struct stripe_head *sh;
NeilBrowndb0505d2017-10-17 16:18:36 +11005778 struct md_rdev *rdev;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005779 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08005780 int raid_disks = conf->previous_raid_disks;
5781 int data_disks = raid_disks - conf->max_degraded;
5782 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07005783 int i;
5784 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11005785 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11005786 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11005787 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11005788 struct list_head stripes;
NeilBrown92140482015-07-06 12:28:45 +10005789 sector_t retn;
NeilBrown52c03292006-06-26 00:27:43 -07005790
NeilBrownfef9c612009-03-31 15:16:46 +11005791 if (sector_nr == 0) {
5792 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10005793 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005794 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
5795 sector_nr = raid5_size(mddev, 0, 0)
5796 - conf->reshape_progress;
NeilBrown6cbd8142015-07-24 13:30:32 +10005797 } else if (mddev->reshape_backwards &&
5798 conf->reshape_progress == MaxSector) {
5799 /* shouldn't happen, but just in case, finish up.*/
5800 sector_nr = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +10005801 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005802 conf->reshape_progress > 0)
5803 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005804 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005805 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11005806 mddev->curr_resync_completed = sector_nr;
Junxiao Bie1a86db2020-07-14 16:10:26 -07005807 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrownfef9c612009-03-31 15:16:46 +11005808 *skipped = 1;
NeilBrown92140482015-07-06 12:28:45 +10005809 retn = sector_nr;
5810 goto finish;
NeilBrownfef9c612009-03-31 15:16:46 +11005811 }
NeilBrown52c03292006-06-26 00:27:43 -07005812 }
5813
NeilBrown7a661382009-03-31 15:21:40 +11005814 /* We need to process a full chunk at a time.
5815 * If old and new chunk sizes differ, we need to process the
5816 * largest of these
5817 */
NeilBrown3cb5edf2015-07-15 17:24:17 +10005818
5819 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
NeilBrown7a661382009-03-31 15:21:40 +11005820
NeilBrownb5254dd2012-05-21 09:27:01 +10005821 /* We update the metadata at least every 10 seconds, or when
5822 * the data about to be copied would over-write the source of
5823 * the data at the front of the range. i.e. one new_stripe
5824 * along from reshape_progress new_maps to after where
5825 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07005826 */
NeilBrownfef9c612009-03-31 15:16:46 +11005827 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005828 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11005829 readpos = conf->reshape_progress;
5830 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005831 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08005832 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10005833 if (mddev->reshape_backwards) {
NeilBrownc74c0d72015-07-15 17:54:15 +10005834 BUG_ON(writepos < reshape_sectors);
5835 writepos -= reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11005836 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11005837 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11005838 } else {
NeilBrown7a661382009-03-31 15:21:40 +11005839 writepos += reshape_sectors;
NeilBrownc74c0d72015-07-15 17:54:15 +10005840 /* readpos and safepos are worst-case calculations.
5841 * A negative number is overly pessimistic, and causes
5842 * obvious problems for unsigned storage. So clip to 0.
5843 */
NeilBrowned37d832009-05-27 21:39:05 +10005844 readpos -= min_t(sector_t, reshape_sectors, readpos);
5845 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11005846 }
NeilBrown52c03292006-06-26 00:27:43 -07005847
NeilBrownb5254dd2012-05-21 09:27:01 +10005848 /* Having calculated the 'writepos' possibly use it
5849 * to set 'stripe_addr' which is where we will write to.
5850 */
5851 if (mddev->reshape_backwards) {
5852 BUG_ON(conf->reshape_progress == 0);
5853 stripe_addr = writepos;
5854 BUG_ON((mddev->dev_sectors &
5855 ~((sector_t)reshape_sectors - 1))
5856 - reshape_sectors - stripe_addr
5857 != sector_nr);
5858 } else {
5859 BUG_ON(writepos != sector_nr + reshape_sectors);
5860 stripe_addr = sector_nr;
5861 }
5862
NeilBrownc8f517c2009-03-31 15:28:40 +11005863 /* 'writepos' is the most advanced device address we might write.
5864 * 'readpos' is the least advanced device address we might read.
5865 * 'safepos' is the least address recorded in the metadata as having
5866 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10005867 * If there is a min_offset_diff, these are adjusted either by
5868 * increasing the safepos/readpos if diff is negative, or
5869 * increasing writepos if diff is positive.
5870 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11005871 * ensure safety in the face of a crash - that must be done by userspace
5872 * making a backup of the data. So in that case there is no particular
5873 * rush to update metadata.
5874 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5875 * update the metadata to advance 'safepos' to match 'readpos' so that
5876 * we can be safe in the event of a crash.
5877 * So we insist on updating metadata if safepos is behind writepos and
5878 * readpos is beyond writepos.
5879 * In any case, update the metadata every 10 seconds.
5880 * Maybe that number should be configurable, but I'm not sure it is
5881 * worth it.... maybe it could be a multiple of safemode_delay???
5882 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005883 if (conf->min_offset_diff < 0) {
5884 safepos += -conf->min_offset_diff;
5885 readpos += -conf->min_offset_diff;
5886 } else
5887 writepos += conf->min_offset_diff;
5888
NeilBrown2c810cd2012-05-21 09:27:00 +10005889 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11005890 ? (safepos > writepos && readpos < writepos)
5891 : (safepos < writepos && readpos > writepos)) ||
5892 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07005893 /* Cannot proceed until we've updated the superblock... */
5894 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005895 atomic_read(&conf->reshape_stripes)==0
5896 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5897 if (atomic_read(&conf->reshape_stripes) != 0)
5898 return 0;
NeilBrownfef9c612009-03-31 15:16:46 +11005899 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005900 mddev->curr_resync_completed = sector_nr;
NeilBrowndb0505d2017-10-17 16:18:36 +11005901 if (!mddev->reshape_backwards)
5902 /* Can update recovery_offset */
5903 rdev_for_each(rdev, mddev)
5904 if (rdev->raid_disk >= 0 &&
5905 !test_bit(Journal, &rdev->flags) &&
5906 !test_bit(In_sync, &rdev->flags) &&
5907 rdev->recovery_offset < sector_nr)
5908 rdev->recovery_offset = sector_nr;
5909
NeilBrownc8f517c2009-03-31 15:28:40 +11005910 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08005911 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown52c03292006-06-26 00:27:43 -07005912 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08005913 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11005914 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5915 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5916 return 0;
NeilBrown52c03292006-06-26 00:27:43 -07005917 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005918 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07005919 spin_unlock_irq(&conf->device_lock);
5920 wake_up(&conf->wait_for_overlap);
Junxiao Bie1a86db2020-07-14 16:10:26 -07005921 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown52c03292006-06-26 00:27:43 -07005922 }
5923
NeilBrownab69ae12009-03-31 15:26:47 +11005924 INIT_LIST_HEAD(&stripes);
Yufen Yuc911c462020-07-18 05:29:07 -04005925 for (i = 0; i < reshape_sectors; i += RAID5_STRIPE_SECTORS(conf)) {
NeilBrown52c03292006-06-26 00:27:43 -07005926 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10005927 int skipped_disk = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07005928 sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005929 set_bit(STRIPE_EXPANDING, &sh->state);
5930 atomic_inc(&conf->reshape_stripes);
5931 /* If any of this stripe is beyond the end of the old
5932 * array, then we need to zero those blocks
5933 */
5934 for (j=sh->disks; j--;) {
5935 sector_t s;
5936 if (j == sh->pd_idx)
5937 continue;
NeilBrownf4168852007-02-28 20:11:53 -08005938 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11005939 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08005940 continue;
Shaohua Li6d036f72015-08-13 14:31:57 -07005941 s = raid5_compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11005942 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10005943 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07005944 continue;
5945 }
Yufen Yuc911c462020-07-18 05:29:07 -04005946 memset(page_address(sh->dev[j].page), 0, RAID5_STRIPE_SIZE(conf));
NeilBrown52c03292006-06-26 00:27:43 -07005947 set_bit(R5_Expanded, &sh->dev[j].flags);
5948 set_bit(R5_UPTODATE, &sh->dev[j].flags);
5949 }
NeilBrowna9f326e2009-09-23 18:06:41 +10005950 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07005951 set_bit(STRIPE_EXPAND_READY, &sh->state);
5952 set_bit(STRIPE_HANDLE, &sh->state);
5953 }
NeilBrownab69ae12009-03-31 15:26:47 +11005954 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07005955 }
5956 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005957 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11005958 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005959 else
NeilBrown7a661382009-03-31 15:21:40 +11005960 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07005961 spin_unlock_irq(&conf->device_lock);
5962 /* Ok, those stripe are ready. We can start scheduling
5963 * reads on the source stripes.
5964 * The source stripes are determined by mapping the first and last
5965 * block on the destination stripes.
5966 */
NeilBrown52c03292006-06-26 00:27:43 -07005967 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11005968 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11005969 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07005970 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10005971 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10005972 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11005973 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11005974 if (last_sector >= mddev->dev_sectors)
5975 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07005976 while (first_sector <= last_sector) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005977 sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005978 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
5979 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07005980 raid5_release_stripe(sh);
Yufen Yuc911c462020-07-18 05:29:07 -04005981 first_sector += RAID5_STRIPE_SECTORS(conf);
NeilBrown52c03292006-06-26 00:27:43 -07005982 }
NeilBrownab69ae12009-03-31 15:26:47 +11005983 /* Now that the sources are clearly marked, we can release
5984 * the destination stripes
5985 */
5986 while (!list_empty(&stripes)) {
5987 sh = list_entry(stripes.next, struct stripe_head, lru);
5988 list_del_init(&sh->lru);
Shaohua Li6d036f72015-08-13 14:31:57 -07005989 raid5_release_stripe(sh);
NeilBrownab69ae12009-03-31 15:26:47 +11005990 }
NeilBrownc6207272008-02-06 01:39:52 -08005991 /* If this takes us to the resync_max point where we have to pause,
5992 * then we need to write out the superblock.
5993 */
NeilBrown7a661382009-03-31 15:21:40 +11005994 sector_nr += reshape_sectors;
NeilBrown92140482015-07-06 12:28:45 +10005995 retn = reshape_sectors;
5996finish:
NeilBrownc5e19d92015-07-17 12:06:02 +10005997 if (mddev->curr_resync_completed > mddev->resync_max ||
5998 (sector_nr - mddev->curr_resync_completed) * 2
NeilBrownc03f6a12009-04-17 11:06:30 +10005999 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08006000 /* Cannot proceed until we've updated the superblock... */
6001 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11006002 atomic_read(&conf->reshape_stripes) == 0
6003 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6004 if (atomic_read(&conf->reshape_stripes) != 0)
6005 goto ret;
NeilBrownfef9c612009-03-31 15:16:46 +11006006 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11006007 mddev->curr_resync_completed = sector_nr;
NeilBrowndb0505d2017-10-17 16:18:36 +11006008 if (!mddev->reshape_backwards)
6009 /* Can update recovery_offset */
6010 rdev_for_each(rdev, mddev)
6011 if (rdev->raid_disk >= 0 &&
6012 !test_bit(Journal, &rdev->flags) &&
6013 !test_bit(In_sync, &rdev->flags) &&
6014 rdev->recovery_offset < sector_nr)
6015 rdev->recovery_offset = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11006016 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08006017 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownc6207272008-02-06 01:39:52 -08006018 md_wakeup_thread(mddev->thread);
6019 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08006020 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)
NeilBrownc91abf52013-11-19 12:02:01 +11006021 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6022 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6023 goto ret;
NeilBrownc6207272008-02-06 01:39:52 -08006024 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11006025 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08006026 spin_unlock_irq(&conf->device_lock);
6027 wake_up(&conf->wait_for_overlap);
Junxiao Bie1a86db2020-07-14 16:10:26 -07006028 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrownc6207272008-02-06 01:39:52 -08006029 }
NeilBrownc91abf52013-11-19 12:02:01 +11006030ret:
NeilBrown92140482015-07-06 12:28:45 +10006031 return retn;
NeilBrown52c03292006-06-26 00:27:43 -07006032}
6033
Shaohua Li849674e2016-01-20 13:52:20 -08006034static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
6035 int *skipped)
NeilBrown52c03292006-06-26 00:27:43 -07006036{
NeilBrownd1688a62011-10-11 16:49:52 +11006037 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07006038 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11006039 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11006040 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07006041 int still_degraded = 0;
6042 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043
NeilBrown72626682005-09-09 16:23:54 -07006044 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11006046
NeilBrown29269552006-03-27 01:18:10 -08006047 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
6048 end_reshape(conf);
6049 return 0;
6050 }
NeilBrown72626682005-09-09 16:23:54 -07006051
6052 if (mddev->curr_resync < max_sector) /* aborted */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006053 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
6054 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07006055 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07006056 conf->fullsync = 0;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006057 md_bitmap_close_sync(mddev->bitmap);
NeilBrown72626682005-09-09 16:23:54 -07006058
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 return 0;
6060 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08006061
NeilBrown64bd6602009-08-03 10:59:58 +10006062 /* Allow raid5_quiesce to complete */
6063 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
6064
NeilBrown52c03292006-06-26 00:27:43 -07006065 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6066 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08006067
NeilBrownc6207272008-02-06 01:39:52 -08006068 /* No need to check resync_max as we never do more than one
6069 * stripe, and as resync_max will always be on a chunk boundary,
6070 * if the check in md_do_sync didn't fire, there is no chance
6071 * of overstepping resync_max here
6072 */
6073
NeilBrown16a53ec2006-06-26 00:27:38 -07006074 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 * to resync, then assert that we are finished, because there is
6076 * nothing we can do.
6077 */
NeilBrown3285edf2006-06-26 00:27:55 -07006078 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07006079 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11006080 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07006081 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 return rv;
6083 }
majianpeng6f608042013-04-24 11:42:41 +10006084 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
6085 !conf->fullsync &&
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006086 !md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
Yufen Yuc911c462020-07-18 05:29:07 -04006087 sync_blocks >= RAID5_STRIPE_SECTORS(conf)) {
NeilBrown72626682005-09-09 16:23:54 -07006088 /* we can skip this block, and probably more */
Yufen Yu83c3e5e2020-07-22 23:29:05 -04006089 do_div(sync_blocks, RAID5_STRIPE_SECTORS(conf));
NeilBrown72626682005-09-09 16:23:54 -07006090 *skipped = 1;
Yufen Yuc911c462020-07-18 05:29:07 -04006091 /* keep things rounded to whole stripes */
6092 return sync_blocks * RAID5_STRIPE_SECTORS(conf);
NeilBrown72626682005-09-09 16:23:54 -07006093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006095 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
NeilBrownb47490c2008-02-06 01:39:50 -08006096
Shaohua Li6d036f72015-08-13 14:31:57 -07006097 sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 if (sh == NULL) {
Shaohua Li6d036f72015-08-13 14:31:57 -07006099 sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07006101 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08006103 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 }
NeilBrown16a53ec2006-06-26 00:27:38 -07006105 /* Need to check if array will still be degraded after recovery/resync
Eric Mei16d9cfa2015-01-06 09:35:02 -08006106 * Note in case of > 1 drive failures it's possible we're rebuilding
6107 * one drive while leaving another faulty drive in array.
NeilBrown16a53ec2006-06-26 00:27:38 -07006108 */
Eric Mei16d9cfa2015-01-06 09:35:02 -08006109 rcu_read_lock();
6110 for (i = 0; i < conf->raid_disks; i++) {
Mark Rutland6aa7de02017-10-23 14:07:29 -07006111 struct md_rdev *rdev = READ_ONCE(conf->disks[i].rdev);
Eric Mei16d9cfa2015-01-06 09:35:02 -08006112
6113 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
NeilBrown16a53ec2006-06-26 00:27:38 -07006114 still_degraded = 1;
Eric Mei16d9cfa2015-01-06 09:35:02 -08006115 }
6116 rcu_read_unlock();
NeilBrown16a53ec2006-06-26 00:27:38 -07006117
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006118 md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07006119
NeilBrown83206d62011-07-26 11:19:49 +10006120 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Eivind Sarto053f5b62014-06-09 17:06:19 -07006121 set_bit(STRIPE_HANDLE, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122
Shaohua Li6d036f72015-08-13 14:31:57 -07006123 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124
Yufen Yuc911c462020-07-18 05:29:07 -04006125 return RAID5_STRIPE_SECTORS(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126}
6127
NeilBrown0472a422017-03-15 14:05:13 +11006128static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio,
6129 unsigned int offset)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006130{
6131 /* We may not be able to submit a whole bio at once as there
6132 * may not be enough stripe_heads available.
6133 * We cannot pre-allocate enough stripe_heads as we may need
6134 * more than exist in the cache (if we allow ever large chunks).
6135 * So we do one stripe head at a time and record in
6136 * ->bi_hw_segments how many have been done.
6137 *
6138 * We *know* that this entire raid_bio is in one chunk, so
6139 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
6140 */
6141 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11006142 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006143 sector_t sector, logical_sector, last_sector;
6144 int scnt = 0;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006145 int handled = 0;
6146
Kent Overstreet4f024f32013-10-11 15:44:27 -07006147 logical_sector = raid_bio->bi_iter.bi_sector &
Yufen Yuc911c462020-07-18 05:29:07 -04006148 ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
NeilBrown112bf892009-03-31 14:39:38 +11006149 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11006150 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07006151 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006152
6153 for (; logical_sector < last_sector;
Yufen Yuc911c462020-07-18 05:29:07 -04006154 logical_sector += RAID5_STRIPE_SECTORS(conf),
6155 sector += RAID5_STRIPE_SECTORS(conf),
Neil Brown387bb172007-02-08 14:20:29 -08006156 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006157
NeilBrown0472a422017-03-15 14:05:13 +11006158 if (scnt < offset)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006159 /* already done this stripe */
6160 continue;
6161
Shaohua Li6d036f72015-08-13 14:31:57 -07006162 sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006163
6164 if (!sh) {
6165 /* failed to get a stripe - must wait */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006166 conf->retry_read_aligned = raid_bio;
NeilBrown0472a422017-03-15 14:05:13 +11006167 conf->retry_read_offset = scnt;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006168 return handled;
6169 }
6170
shli@kernel.orgda41ba62014-12-15 12:57:03 +11006171 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07006172 raid5_release_stripe(sh);
Neil Brown387bb172007-02-08 14:20:29 -08006173 conf->retry_read_aligned = raid_bio;
NeilBrown0472a422017-03-15 14:05:13 +11006174 conf->retry_read_offset = scnt;
Neil Brown387bb172007-02-08 14:20:29 -08006175 return handled;
6176 }
6177
majianpeng3f9e7c12012-07-31 10:04:21 +10006178 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07006179 handle_stripe(sh);
Shaohua Li6d036f72015-08-13 14:31:57 -07006180 raid5_release_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006181 handled++;
6182 }
NeilBrown016c76a2017-03-15 14:05:13 +11006183
6184 bio_endio(raid_bio);
6185
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006186 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10006187 wake_up(&conf->wait_for_quiescent);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006188 return handled;
6189}
6190
Shaohua Libfc90cb2013-08-29 15:40:32 +08006191static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11006192 struct r5worker *worker,
6193 struct list_head *temp_inactive_list)
Christoph Hellwigefcd4872019-04-04 18:56:16 +02006194 __releases(&conf->device_lock)
6195 __acquires(&conf->device_lock)
Shaohua Li46a06402012-08-02 08:33:15 +10006196{
6197 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11006198 int i, batch_size = 0, hash;
6199 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10006200
6201 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08006202 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10006203 batch[batch_size++] = sh;
6204
Shaohua Li566c09c2013-11-14 15:16:17 +11006205 if (batch_size == 0) {
6206 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6207 if (!list_empty(temp_inactive_list + i))
6208 break;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006209 if (i == NR_STRIPE_HASH_LOCKS) {
6210 spin_unlock_irq(&conf->device_lock);
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01006211 log_flush_stripe_to_raid(conf);
Shaohua Lia8c34f92015-09-02 13:49:46 -07006212 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006213 return batch_size;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006214 }
Shaohua Li566c09c2013-11-14 15:16:17 +11006215 release_inactive = true;
6216 }
Shaohua Li46a06402012-08-02 08:33:15 +10006217 spin_unlock_irq(&conf->device_lock);
6218
Shaohua Li566c09c2013-11-14 15:16:17 +11006219 release_inactive_stripe_list(conf, temp_inactive_list,
6220 NR_STRIPE_HASH_LOCKS);
6221
Shaohua Lia8c34f92015-09-02 13:49:46 -07006222 r5l_flush_stripe_to_raid(conf->log);
Shaohua Li566c09c2013-11-14 15:16:17 +11006223 if (release_inactive) {
6224 spin_lock_irq(&conf->device_lock);
6225 return 0;
6226 }
6227
Shaohua Li46a06402012-08-02 08:33:15 +10006228 for (i = 0; i < batch_size; i++)
6229 handle_stripe(batch[i]);
Artur Paszkiewiczff875732017-03-09 09:59:58 +01006230 log_write_stripe_run(conf);
Shaohua Li46a06402012-08-02 08:33:15 +10006231
6232 cond_resched();
6233
6234 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006235 for (i = 0; i < batch_size; i++) {
6236 hash = batch[i]->hash_lock_index;
6237 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
6238 }
Shaohua Li46a06402012-08-02 08:33:15 +10006239 return batch_size;
6240}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006241
Shaohua Li851c30c2013-08-28 14:30:16 +08006242static void raid5_do_work(struct work_struct *work)
6243{
6244 struct r5worker *worker = container_of(work, struct r5worker, work);
6245 struct r5worker_group *group = worker->group;
6246 struct r5conf *conf = group->conf;
NeilBrown16d997b2017-03-15 14:05:12 +11006247 struct mddev *mddev = conf->mddev;
Shaohua Li851c30c2013-08-28 14:30:16 +08006248 int group_id = group - conf->worker_groups;
6249 int handled;
6250 struct blk_plug plug;
6251
6252 pr_debug("+++ raid5worker active\n");
6253
6254 blk_start_plug(&plug);
6255 handled = 0;
6256 spin_lock_irq(&conf->device_lock);
6257 while (1) {
6258 int batch_size, released;
6259
Shaohua Li566c09c2013-11-14 15:16:17 +11006260 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006261
Shaohua Li566c09c2013-11-14 15:16:17 +11006262 batch_size = handle_active_stripes(conf, group_id, worker,
6263 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08006264 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08006265 if (!batch_size && !released)
6266 break;
6267 handled += batch_size;
NeilBrown16d997b2017-03-15 14:05:12 +11006268 wait_event_lock_irq(mddev->sb_wait,
6269 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags),
6270 conf->device_lock);
Shaohua Li851c30c2013-08-28 14:30:16 +08006271 }
6272 pr_debug("%d stripes handled\n", handled);
6273
6274 spin_unlock_irq(&conf->device_lock);
Ofer Heifetz7e96d552017-07-24 09:17:40 +03006275
Song Liu9c72a18e42017-08-24 09:53:59 -07006276 flush_deferred_bios(conf);
6277
6278 r5l_flush_stripe_to_raid(conf->log);
6279
Ofer Heifetz7e96d552017-07-24 09:17:40 +03006280 async_tx_issue_pending_all();
Shaohua Li851c30c2013-08-28 14:30:16 +08006281 blk_finish_plug(&plug);
6282
6283 pr_debug("--- raid5worker inactive\n");
6284}
6285
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286/*
6287 * This is our raid5 kernel thread.
6288 *
6289 * We scan the hash table for stripes which can be handled now.
6290 * During the scan, completed stripes are saved for us by the interrupt
6291 * handler, so that they will not have to wait for our next wakeup.
6292 */
Shaohua Li4ed87312012-10-11 13:34:00 +11006293static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294{
Shaohua Li4ed87312012-10-11 13:34:00 +11006295 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11006296 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006298 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299
Dan Williams45b42332007-07-09 11:56:43 -07006300 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301
6302 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006304 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 handled = 0;
6306 spin_lock_irq(&conf->device_lock);
6307 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006308 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08006309 int batch_size, released;
NeilBrown0472a422017-03-15 14:05:13 +11006310 unsigned int offset;
Shaohua Li773ca822013-08-27 17:50:39 +08006311
Shaohua Li566c09c2013-11-14 15:16:17 +11006312 released = release_stripe_list(conf, conf->temp_inactive_list);
NeilBrownedbe83a2015-02-26 12:47:56 +11006313 if (released)
6314 clear_bit(R5_DID_ALLOC, &conf->cache_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315
NeilBrown0021b7b2012-07-31 09:08:14 +02006316 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10006317 !list_empty(&conf->bitmap_list)) {
6318 /* Now is a good time to flush some bitmap updates */
6319 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08006320 spin_unlock_irq(&conf->device_lock);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006321 md_bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08006322 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10006323 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11006324 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07006325 }
NeilBrown0021b7b2012-07-31 09:08:14 +02006326 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07006327
NeilBrown0472a422017-03-15 14:05:13 +11006328 while ((bio = remove_bio_from_retry(conf, &offset))) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006329 int ok;
6330 spin_unlock_irq(&conf->device_lock);
NeilBrown0472a422017-03-15 14:05:13 +11006331 ok = retry_aligned_read(conf, bio, offset);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006332 spin_lock_irq(&conf->device_lock);
6333 if (!ok)
6334 break;
6335 handled++;
6336 }
6337
Shaohua Li566c09c2013-11-14 15:16:17 +11006338 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
6339 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08006340 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 break;
Shaohua Li46a06402012-08-02 08:33:15 +10006342 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343
Shaohua Li29530792016-12-08 15:48:19 -08006344 if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) {
Shaohua Li46a06402012-08-02 08:33:15 +10006345 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10006346 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10006347 spin_lock_irq(&conf->device_lock);
6348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 }
Dan Williams45b42332007-07-09 11:56:43 -07006350 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351
6352 spin_unlock_irq(&conf->device_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006353 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
6354 mutex_trylock(&conf->cache_size_mutex)) {
NeilBrownedbe83a2015-02-26 12:47:56 +11006355 grow_one_stripe(conf, __GFP_NOWARN);
6356 /* Set flag even if allocation failed. This helps
6357 * slow down allocation requests when mem is short
6358 */
6359 set_bit(R5_DID_ALLOC, &conf->cache_state);
NeilBrown2d5b5692015-07-06 12:49:23 +10006360 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362
Shaohua Li765d7042017-01-04 09:33:23 -08006363 flush_deferred_bios(conf);
6364
Shaohua Li0576b1c2015-08-13 14:32:00 -07006365 r5l_flush_stripe_to_raid(conf->log);
6366
Dan Williamsc9f21aa2008-07-23 12:05:51 -07006367 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006368 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369
Dan Williams45b42332007-07-09 11:56:43 -07006370 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371}
6372
NeilBrown3f294f42005-11-08 21:39:25 -08006373static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006374raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006375{
NeilBrown7b1485b2014-12-15 12:56:59 +11006376 struct r5conf *conf;
6377 int ret = 0;
6378 spin_lock(&mddev->lock);
6379 conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006380 if (conf)
NeilBrownedbe83a2015-02-26 12:47:56 +11006381 ret = sprintf(page, "%d\n", conf->min_nr_stripes);
NeilBrown7b1485b2014-12-15 12:56:59 +11006382 spin_unlock(&mddev->lock);
6383 return ret;
NeilBrown3f294f42005-11-08 21:39:25 -08006384}
6385
NeilBrownc41d4ac2010-06-01 19:37:24 +10006386int
NeilBrownfd01b882011-10-11 16:47:53 +11006387raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10006388{
Alexei Naberezhnov483cbbe2018-03-27 16:54:16 -07006389 int result = 0;
NeilBrownd1688a62011-10-11 16:49:52 +11006390 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10006391
6392 if (size <= 16 || size > 32768)
6393 return -EINVAL;
NeilBrown486f0642015-02-25 12:10:35 +11006394
NeilBrownedbe83a2015-02-26 12:47:56 +11006395 conf->min_nr_stripes = size;
NeilBrown2d5b5692015-07-06 12:49:23 +10006396 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006397 while (size < conf->max_nr_stripes &&
6398 drop_one_stripe(conf))
6399 ;
NeilBrown2d5b5692015-07-06 12:49:23 +10006400 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006401
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02006402 md_allow_write(mddev);
NeilBrown486f0642015-02-25 12:10:35 +11006403
NeilBrown2d5b5692015-07-06 12:49:23 +10006404 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006405 while (size > conf->max_nr_stripes)
Alexei Naberezhnov483cbbe2018-03-27 16:54:16 -07006406 if (!grow_one_stripe(conf, GFP_KERNEL)) {
6407 conf->min_nr_stripes = conf->max_nr_stripes;
6408 result = -ENOMEM;
NeilBrown486f0642015-02-25 12:10:35 +11006409 break;
Alexei Naberezhnov483cbbe2018-03-27 16:54:16 -07006410 }
NeilBrown2d5b5692015-07-06 12:49:23 +10006411 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006412
Alexei Naberezhnov483cbbe2018-03-27 16:54:16 -07006413 return result;
NeilBrownc41d4ac2010-06-01 19:37:24 +10006414}
6415EXPORT_SYMBOL(raid5_set_cache_size);
6416
NeilBrown3f294f42005-11-08 21:39:25 -08006417static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006418raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08006419{
NeilBrown67918752014-12-15 12:57:01 +11006420 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006421 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07006422 int err;
6423
NeilBrown3f294f42005-11-08 21:39:25 -08006424 if (len >= PAGE_SIZE)
6425 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006426 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08006427 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006428 err = mddev_lock(mddev);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006429 if (err)
6430 return err;
NeilBrown67918752014-12-15 12:57:01 +11006431 conf = mddev->private;
6432 if (!conf)
6433 err = -ENODEV;
6434 else
6435 err = raid5_set_cache_size(mddev, new);
6436 mddev_unlock(mddev);
6437
6438 return err ?: len;
NeilBrown3f294f42005-11-08 21:39:25 -08006439}
NeilBrown007583c2005-11-08 21:39:30 -08006440
NeilBrown96de1e62005-11-08 21:39:39 -08006441static struct md_sysfs_entry
6442raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
6443 raid5_show_stripe_cache_size,
6444 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08006445
6446static ssize_t
Markus Stockhausend06f1912014-12-15 12:57:05 +11006447raid5_show_rmw_level(struct mddev *mddev, char *page)
6448{
6449 struct r5conf *conf = mddev->private;
6450 if (conf)
6451 return sprintf(page, "%d\n", conf->rmw_level);
6452 else
6453 return 0;
6454}
6455
6456static ssize_t
6457raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
6458{
6459 struct r5conf *conf = mddev->private;
6460 unsigned long new;
6461
6462 if (!conf)
6463 return -ENODEV;
6464
6465 if (len >= PAGE_SIZE)
6466 return -EINVAL;
6467
6468 if (kstrtoul(page, 10, &new))
6469 return -EINVAL;
6470
6471 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
6472 return -EINVAL;
6473
6474 if (new != PARITY_DISABLE_RMW &&
6475 new != PARITY_ENABLE_RMW &&
6476 new != PARITY_PREFER_RMW)
6477 return -EINVAL;
6478
6479 conf->rmw_level = new;
6480 return len;
6481}
6482
6483static struct md_sysfs_entry
6484raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
6485 raid5_show_rmw_level,
6486 raid5_store_rmw_level);
6487
Yufen Yu3b5408b2020-07-18 05:29:09 -04006488static ssize_t
6489raid5_show_stripe_size(struct mddev *mddev, char *page)
6490{
6491 struct r5conf *conf;
6492 int ret = 0;
6493
6494 spin_lock(&mddev->lock);
6495 conf = mddev->private;
6496 if (conf)
6497 ret = sprintf(page, "%lu\n", RAID5_STRIPE_SIZE(conf));
6498 spin_unlock(&mddev->lock);
6499 return ret;
6500}
6501
6502#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
6503static ssize_t
6504raid5_store_stripe_size(struct mddev *mddev, const char *page, size_t len)
6505{
6506 struct r5conf *conf;
6507 unsigned long new;
6508 int err;
6509
6510 if (len >= PAGE_SIZE)
6511 return -EINVAL;
6512 if (kstrtoul(page, 10, &new))
6513 return -EINVAL;
6514
6515 /*
6516 * The value should not be bigger than PAGE_SIZE. It requires to
6517 * be multiple of DEFAULT_STRIPE_SIZE.
6518 */
6519 if (new % DEFAULT_STRIPE_SIZE != 0 || new > PAGE_SIZE || new == 0)
6520 return -EINVAL;
6521
6522 err = mddev_lock(mddev);
6523 if (err)
6524 return err;
6525
6526 conf = mddev->private;
6527 if (!conf) {
6528 err = -ENODEV;
6529 goto out_unlock;
6530 }
6531
6532 if (new == conf->stripe_size)
6533 goto out_unlock;
6534
6535 pr_debug("md/raid: change stripe_size from %lu to %lu\n",
6536 conf->stripe_size, new);
6537
6538 mddev_suspend(mddev);
6539 conf->stripe_size = new;
6540 conf->stripe_shift = ilog2(new) - 9;
6541 conf->stripe_sectors = new >> 9;
6542 mddev_resume(mddev);
6543
6544out_unlock:
6545 mddev_unlock(mddev);
6546 return err ?: len;
6547}
6548
6549static struct md_sysfs_entry
6550raid5_stripe_size = __ATTR(stripe_size, 0644,
6551 raid5_show_stripe_size,
6552 raid5_store_stripe_size);
6553#else
6554static struct md_sysfs_entry
6555raid5_stripe_size = __ATTR(stripe_size, 0444,
6556 raid5_show_stripe_size,
6557 NULL);
6558#endif
Markus Stockhausend06f1912014-12-15 12:57:05 +11006559
6560static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006561raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006562{
NeilBrown7b1485b2014-12-15 12:56:59 +11006563 struct r5conf *conf;
6564 int ret = 0;
6565 spin_lock(&mddev->lock);
6566 conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006567 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006568 ret = sprintf(page, "%d\n", conf->bypass_threshold);
6569 spin_unlock(&mddev->lock);
6570 return ret;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006571}
6572
6573static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006574raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006575{
NeilBrown67918752014-12-15 12:57:01 +11006576 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006577 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006578 int err;
6579
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006580 if (len >= PAGE_SIZE)
6581 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006582 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006583 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006584
6585 err = mddev_lock(mddev);
6586 if (err)
6587 return err;
6588 conf = mddev->private;
6589 if (!conf)
6590 err = -ENODEV;
NeilBrownedbe83a2015-02-26 12:47:56 +11006591 else if (new > conf->min_nr_stripes)
NeilBrown67918752014-12-15 12:57:01 +11006592 err = -EINVAL;
6593 else
6594 conf->bypass_threshold = new;
6595 mddev_unlock(mddev);
6596 return err ?: len;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006597}
6598
6599static struct md_sysfs_entry
6600raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
6601 S_IRUGO | S_IWUSR,
6602 raid5_show_preread_threshold,
6603 raid5_store_preread_threshold);
6604
6605static ssize_t
Shaohua Lid592a992014-05-21 17:57:44 +08006606raid5_show_skip_copy(struct mddev *mddev, char *page)
6607{
NeilBrown7b1485b2014-12-15 12:56:59 +11006608 struct r5conf *conf;
6609 int ret = 0;
6610 spin_lock(&mddev->lock);
6611 conf = mddev->private;
Shaohua Lid592a992014-05-21 17:57:44 +08006612 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006613 ret = sprintf(page, "%d\n", conf->skip_copy);
6614 spin_unlock(&mddev->lock);
6615 return ret;
Shaohua Lid592a992014-05-21 17:57:44 +08006616}
6617
6618static ssize_t
6619raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
6620{
NeilBrown67918752014-12-15 12:57:01 +11006621 struct r5conf *conf;
Shaohua Lid592a992014-05-21 17:57:44 +08006622 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006623 int err;
6624
Shaohua Lid592a992014-05-21 17:57:44 +08006625 if (len >= PAGE_SIZE)
6626 return -EINVAL;
Shaohua Lid592a992014-05-21 17:57:44 +08006627 if (kstrtoul(page, 10, &new))
6628 return -EINVAL;
6629 new = !!new;
Shaohua Lid592a992014-05-21 17:57:44 +08006630
NeilBrown67918752014-12-15 12:57:01 +11006631 err = mddev_lock(mddev);
6632 if (err)
6633 return err;
6634 conf = mddev->private;
6635 if (!conf)
6636 err = -ENODEV;
6637 else if (new != conf->skip_copy) {
6638 mddev_suspend(mddev);
6639 conf->skip_copy = new;
6640 if (new)
Jan Karadc3b17c2017-02-02 15:56:50 +01006641 mddev->queue->backing_dev_info->capabilities |=
NeilBrown67918752014-12-15 12:57:01 +11006642 BDI_CAP_STABLE_WRITES;
6643 else
Jan Karadc3b17c2017-02-02 15:56:50 +01006644 mddev->queue->backing_dev_info->capabilities &=
NeilBrown67918752014-12-15 12:57:01 +11006645 ~BDI_CAP_STABLE_WRITES;
6646 mddev_resume(mddev);
6647 }
6648 mddev_unlock(mddev);
6649 return err ?: len;
Shaohua Lid592a992014-05-21 17:57:44 +08006650}
6651
6652static struct md_sysfs_entry
6653raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
6654 raid5_show_skip_copy,
6655 raid5_store_skip_copy);
6656
Shaohua Lid592a992014-05-21 17:57:44 +08006657static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006658stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006659{
NeilBrownd1688a62011-10-11 16:49:52 +11006660 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006661 if (conf)
6662 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
6663 else
6664 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08006665}
6666
NeilBrown96de1e62005-11-08 21:39:39 -08006667static struct md_sysfs_entry
6668raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08006669
Shaohua Lib7214202013-08-27 17:50:42 +08006670static ssize_t
6671raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
6672{
NeilBrown7b1485b2014-12-15 12:56:59 +11006673 struct r5conf *conf;
6674 int ret = 0;
6675 spin_lock(&mddev->lock);
6676 conf = mddev->private;
Shaohua Lib7214202013-08-27 17:50:42 +08006677 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006678 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
6679 spin_unlock(&mddev->lock);
6680 return ret;
Shaohua Lib7214202013-08-27 17:50:42 +08006681}
6682
majianpeng60aaf932013-11-14 15:16:20 +11006683static int alloc_thread_groups(struct r5conf *conf, int cnt,
6684 int *group_cnt,
majianpeng60aaf932013-11-14 15:16:20 +11006685 struct r5worker_group **worker_groups);
Shaohua Lib7214202013-08-27 17:50:42 +08006686static ssize_t
6687raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
6688{
NeilBrown67918752014-12-15 12:57:01 +11006689 struct r5conf *conf;
Shaohua Li7d5d7b52017-09-21 11:03:52 -07006690 unsigned int new;
Shaohua Lib7214202013-08-27 17:50:42 +08006691 int err;
majianpeng60aaf932013-11-14 15:16:20 +11006692 struct r5worker_group *new_groups, *old_groups;
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006693 int group_cnt;
Shaohua Lib7214202013-08-27 17:50:42 +08006694
6695 if (len >= PAGE_SIZE)
6696 return -EINVAL;
Shaohua Li7d5d7b52017-09-21 11:03:52 -07006697 if (kstrtouint(page, 10, &new))
6698 return -EINVAL;
6699 /* 8192 should be big enough */
6700 if (new > 8192)
Shaohua Lib7214202013-08-27 17:50:42 +08006701 return -EINVAL;
6702
NeilBrown67918752014-12-15 12:57:01 +11006703 err = mddev_lock(mddev);
Shaohua Lib7214202013-08-27 17:50:42 +08006704 if (err)
6705 return err;
NeilBrown67918752014-12-15 12:57:01 +11006706 conf = mddev->private;
6707 if (!conf)
6708 err = -ENODEV;
6709 else if (new != conf->worker_cnt_per_group) {
6710 mddev_suspend(mddev);
6711
6712 old_groups = conf->worker_groups;
6713 if (old_groups)
6714 flush_workqueue(raid5_wq);
6715
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006716 err = alloc_thread_groups(conf, new, &group_cnt, &new_groups);
NeilBrown67918752014-12-15 12:57:01 +11006717 if (!err) {
6718 spin_lock_irq(&conf->device_lock);
6719 conf->group_cnt = group_cnt;
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006720 conf->worker_cnt_per_group = new;
NeilBrown67918752014-12-15 12:57:01 +11006721 conf->worker_groups = new_groups;
6722 spin_unlock_irq(&conf->device_lock);
6723
6724 if (old_groups)
6725 kfree(old_groups[0].workers);
6726 kfree(old_groups);
6727 }
6728 mddev_resume(mddev);
6729 }
6730 mddev_unlock(mddev);
6731
6732 return err ?: len;
Shaohua Lib7214202013-08-27 17:50:42 +08006733}
6734
6735static struct md_sysfs_entry
6736raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
6737 raid5_show_group_thread_cnt,
6738 raid5_store_group_thread_cnt);
6739
NeilBrown007583c2005-11-08 21:39:30 -08006740static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08006741 &raid5_stripecache_size.attr,
6742 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006743 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08006744 &raid5_group_thread_cnt.attr,
Shaohua Lid592a992014-05-21 17:57:44 +08006745 &raid5_skip_copy.attr,
Markus Stockhausend06f1912014-12-15 12:57:05 +11006746 &raid5_rmw_level.attr,
Yufen Yu3b5408b2020-07-18 05:29:09 -04006747 &raid5_stripe_size.attr,
Song Liu2c7da142016-11-17 15:24:41 -08006748 &r5c_journal_mode.attr,
Mariusz Dabrowskia596d082019-02-18 15:04:09 +01006749 &ppl_write_hint.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08006750 NULL,
6751};
NeilBrown007583c2005-11-08 21:39:30 -08006752static struct attribute_group raid5_attrs_group = {
6753 .name = NULL,
6754 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08006755};
6756
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006757static int alloc_thread_groups(struct r5conf *conf, int cnt, int *group_cnt,
majianpeng60aaf932013-11-14 15:16:20 +11006758 struct r5worker_group **worker_groups)
Shaohua Li851c30c2013-08-28 14:30:16 +08006759{
Shaohua Li566c09c2013-11-14 15:16:17 +11006760 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08006761 ssize_t size;
6762 struct r5worker *workers;
6763
Shaohua Li851c30c2013-08-28 14:30:16 +08006764 if (cnt == 0) {
majianpeng60aaf932013-11-14 15:16:20 +11006765 *group_cnt = 0;
6766 *worker_groups = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08006767 return 0;
6768 }
majianpeng60aaf932013-11-14 15:16:20 +11006769 *group_cnt = num_possible_nodes();
Shaohua Li851c30c2013-08-28 14:30:16 +08006770 size = sizeof(struct r5worker) * cnt;
Kees Cook6396bb22018-06-12 14:03:40 -07006771 workers = kcalloc(size, *group_cnt, GFP_NOIO);
6772 *worker_groups = kcalloc(*group_cnt, sizeof(struct r5worker_group),
6773 GFP_NOIO);
majianpeng60aaf932013-11-14 15:16:20 +11006774 if (!*worker_groups || !workers) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006775 kfree(workers);
majianpeng60aaf932013-11-14 15:16:20 +11006776 kfree(*worker_groups);
Shaohua Li851c30c2013-08-28 14:30:16 +08006777 return -ENOMEM;
6778 }
6779
majianpeng60aaf932013-11-14 15:16:20 +11006780 for (i = 0; i < *group_cnt; i++) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006781 struct r5worker_group *group;
6782
NeilBrown0c775d52013-11-25 11:12:43 +11006783 group = &(*worker_groups)[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08006784 INIT_LIST_HEAD(&group->handle_list);
Shaohua Li535ae4e2017-02-15 19:37:32 -08006785 INIT_LIST_HEAD(&group->loprio_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006786 group->conf = conf;
6787 group->workers = workers + i * cnt;
6788
6789 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11006790 struct r5worker *worker = group->workers + j;
6791 worker->group = group;
6792 INIT_WORK(&worker->work, raid5_do_work);
6793
6794 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
6795 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08006796 }
6797 }
6798
6799 return 0;
6800}
6801
6802static void free_thread_groups(struct r5conf *conf)
6803{
6804 if (conf->worker_groups)
6805 kfree(conf->worker_groups[0].workers);
6806 kfree(conf->worker_groups);
6807 conf->worker_groups = NULL;
6808}
6809
Dan Williams80c3a6c2009-03-17 18:10:40 -07006810static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11006811raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07006812{
NeilBrownd1688a62011-10-11 16:49:52 +11006813 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006814
6815 if (!sectors)
6816 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006817 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11006818 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11006819 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006820
NeilBrown3cb5edf2015-07-15 17:24:17 +10006821 sectors &= ~((sector_t)conf->chunk_sectors - 1);
6822 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006823 return sectors * (raid_disks - conf->max_degraded);
6824}
6825
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306826static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6827{
6828 safe_put_page(percpu->spare_page);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306829 percpu->spare_page = NULL;
Kent Overstreetb330e6a2019-03-11 23:31:06 -07006830 kvfree(percpu->scribble);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306831 percpu->scribble = NULL;
6832}
6833
6834static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6835{
Kent Overstreetb330e6a2019-03-11 23:31:06 -07006836 if (conf->level == 6 && !percpu->spare_page) {
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306837 percpu->spare_page = alloc_page(GFP_KERNEL);
Kent Overstreetb330e6a2019-03-11 23:31:06 -07006838 if (!percpu->spare_page)
6839 return -ENOMEM;
6840 }
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306841
Kent Overstreetb330e6a2019-03-11 23:31:06 -07006842 if (scribble_alloc(percpu,
6843 max(conf->raid_disks,
6844 conf->previous_raid_disks),
6845 max(conf->chunk_sectors,
6846 conf->prev_chunk_sectors)
Yufen Yuc911c462020-07-18 05:29:07 -04006847 / RAID5_STRIPE_SECTORS(conf))) {
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306848 free_scratch_buffer(conf, percpu);
6849 return -ENOMEM;
6850 }
6851
6852 return 0;
6853}
6854
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006855static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
6856{
6857 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
6858
6859 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
6860 return 0;
6861}
6862
NeilBrownd1688a62011-10-11 16:49:52 +11006863static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006864{
Dan Williams36d1c642009-07-14 11:48:22 -07006865 if (!conf->percpu)
6866 return;
6867
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006868 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Dan Williams36d1c642009-07-14 11:48:22 -07006869 free_percpu(conf->percpu);
6870}
6871
NeilBrownd1688a62011-10-11 16:49:52 +11006872static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10006873{
Song Liud7bd3982016-11-23 22:50:39 -08006874 int i;
6875
Artur Paszkiewiczff875732017-03-09 09:59:58 +01006876 log_exit(conf);
6877
Aliaksei Karaliou565e0452017-12-23 21:20:31 +03006878 unregister_shrinker(&conf->shrinker);
Shaohua Li851c30c2013-08-28 14:30:16 +08006879 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10006880 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07006881 raid5_free_percpu(conf);
Song Liud7bd3982016-11-23 22:50:39 -08006882 for (i = 0; i < conf->pool_size; i++)
6883 if (conf->disks[i].extra_page)
6884 put_page(conf->disks[i].extra_page);
Dan Williams95fc17a2009-07-31 12:39:15 +10006885 kfree(conf->disks);
Kent Overstreetafeee512018-05-20 18:25:52 -04006886 bioset_exit(&conf->bio_split);
Dan Williams95fc17a2009-07-31 12:39:15 +10006887 kfree(conf->stripe_hashtbl);
Shaohua Liaaf9f122017-03-03 22:06:12 -08006888 kfree(conf->pending_data);
Dan Williams95fc17a2009-07-31 12:39:15 +10006889 kfree(conf);
6890}
6891
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006892static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
Dan Williams36d1c642009-07-14 11:48:22 -07006893{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006894 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
Dan Williams36d1c642009-07-14 11:48:22 -07006895 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
6896
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006897 if (alloc_scratch_buffer(conf, percpu)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006898 pr_warn("%s: failed memory allocation for cpu%u\n",
6899 __func__, cpu);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006900 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006901 }
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006902 return 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006903}
Dan Williams36d1c642009-07-14 11:48:22 -07006904
NeilBrownd1688a62011-10-11 16:49:52 +11006905static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006906{
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306907 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006908
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306909 conf->percpu = alloc_percpu(struct raid5_percpu);
6910 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07006911 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006912
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006913 err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Shaohua Li27a353c2016-02-24 17:38:28 -08006914 if (!err) {
6915 conf->scribble_disks = max(conf->raid_disks,
6916 conf->previous_raid_disks);
6917 conf->scribble_sectors = max(conf->chunk_sectors,
6918 conf->prev_chunk_sectors);
6919 }
Dan Williams36d1c642009-07-14 11:48:22 -07006920 return err;
6921}
6922
NeilBrownedbe83a2015-02-26 12:47:56 +11006923static unsigned long raid5_cache_scan(struct shrinker *shrink,
6924 struct shrink_control *sc)
6925{
6926 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
NeilBrown2d5b5692015-07-06 12:49:23 +10006927 unsigned long ret = SHRINK_STOP;
6928
6929 if (mutex_trylock(&conf->cache_size_mutex)) {
6930 ret= 0;
NeilBrown49895bc2015-08-03 17:09:57 +10006931 while (ret < sc->nr_to_scan &&
6932 conf->max_nr_stripes > conf->min_nr_stripes) {
NeilBrown2d5b5692015-07-06 12:49:23 +10006933 if (drop_one_stripe(conf) == 0) {
6934 ret = SHRINK_STOP;
6935 break;
6936 }
6937 ret++;
6938 }
6939 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006940 }
6941 return ret;
6942}
6943
6944static unsigned long raid5_cache_count(struct shrinker *shrink,
6945 struct shrink_control *sc)
6946{
6947 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
6948
6949 if (conf->max_nr_stripes < conf->min_nr_stripes)
6950 /* unlikely, but not impossible */
6951 return 0;
6952 return conf->max_nr_stripes - conf->min_nr_stripes;
6953}
6954
NeilBrownd1688a62011-10-11 16:49:52 +11006955static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956{
NeilBrownd1688a62011-10-11 16:49:52 +11006957 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006958 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11006959 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10006961 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11006962 int i;
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006963 int group_cnt;
majianpeng60aaf932013-11-14 15:16:20 +11006964 struct r5worker_group *new_group;
Kent Overstreetafeee512018-05-20 18:25:52 -04006965 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966
NeilBrown91adb562009-03-31 14:39:39 +11006967 if (mddev->new_level != 5
6968 && mddev->new_level != 4
6969 && mddev->new_level != 6) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006970 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
6971 mdname(mddev), mddev->new_level);
NeilBrown91adb562009-03-31 14:39:39 +11006972 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 }
NeilBrown91adb562009-03-31 14:39:39 +11006974 if ((mddev->new_level == 5
6975 && !algorithm_valid_raid5(mddev->new_layout)) ||
6976 (mddev->new_level == 6
6977 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006978 pr_warn("md/raid:%s: layout %d not supported\n",
6979 mdname(mddev), mddev->new_layout);
NeilBrown91adb562009-03-31 14:39:39 +11006980 return ERR_PTR(-EIO);
6981 }
6982 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006983 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
6984 mdname(mddev), mddev->raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006985 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11006986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987
Andre Noll664e7c42009-06-18 08:45:27 +10006988 if (!mddev->new_chunk_sectors ||
6989 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
6990 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006991 pr_warn("md/raid:%s: invalid chunk size %d\n",
6992 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11006993 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11006994 }
6995
NeilBrownd1688a62011-10-11 16:49:52 +11006996 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11006997 if (conf == NULL)
6998 goto abort;
Yufen Yuc911c462020-07-18 05:29:07 -04006999
Yufen Yue2368582020-07-18 05:29:08 -04007000#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
7001 conf->stripe_size = DEFAULT_STRIPE_SIZE;
7002 conf->stripe_shift = ilog2(DEFAULT_STRIPE_SIZE) - 9;
7003 conf->stripe_sectors = DEFAULT_STRIPE_SIZE >> 9;
7004#endif
Shaohua Liaaf9f122017-03-03 22:06:12 -08007005 INIT_LIST_HEAD(&conf->free_list);
7006 INIT_LIST_HEAD(&conf->pending_list);
Kees Cook6396bb22018-06-12 14:03:40 -07007007 conf->pending_data = kcalloc(PENDING_IO_MAX,
7008 sizeof(struct r5pending_data),
7009 GFP_KERNEL);
Shaohua Liaaf9f122017-03-03 22:06:12 -08007010 if (!conf->pending_data)
7011 goto abort;
7012 for (i = 0; i < PENDING_IO_MAX; i++)
7013 list_add(&conf->pending_data[i].sibling, &conf->free_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08007014 /* Don't enable multi-threading by default*/
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01007015 if (!alloc_thread_groups(conf, 0, &group_cnt, &new_group)) {
majianpeng60aaf932013-11-14 15:16:20 +11007016 conf->group_cnt = group_cnt;
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01007017 conf->worker_cnt_per_group = 0;
majianpeng60aaf932013-11-14 15:16:20 +11007018 conf->worker_groups = new_group;
7019 } else
Shaohua Li851c30c2013-08-28 14:30:16 +08007020 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11007021 spin_lock_init(&conf->device_lock);
Ahmed S. Darwish0a87b252020-07-20 17:55:25 +02007022 seqcount_spinlock_init(&conf->gen_lock, &conf->device_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10007023 mutex_init(&conf->cache_size_mutex);
Yuanhan Liub1b46482015-05-08 18:19:06 +10007024 init_waitqueue_head(&conf->wait_for_quiescent);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08007025 init_waitqueue_head(&conf->wait_for_stripe);
Dan Williamsf5efd452009-10-16 15:55:38 +11007026 init_waitqueue_head(&conf->wait_for_overlap);
7027 INIT_LIST_HEAD(&conf->handle_list);
Shaohua Li535ae4e2017-02-15 19:37:32 -08007028 INIT_LIST_HEAD(&conf->loprio_list);
Dan Williamsf5efd452009-10-16 15:55:38 +11007029 INIT_LIST_HEAD(&conf->hold_list);
7030 INIT_LIST_HEAD(&conf->delayed_list);
7031 INIT_LIST_HEAD(&conf->bitmap_list);
Shaohua Li773ca822013-08-27 17:50:39 +08007032 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11007033 atomic_set(&conf->active_stripes, 0);
7034 atomic_set(&conf->preread_active_stripes, 0);
7035 atomic_set(&conf->active_aligned_reads, 0);
Shaohua Li765d7042017-01-04 09:33:23 -08007036 spin_lock_init(&conf->pending_bios_lock);
7037 conf->batch_bio_dispatch = true;
7038 rdev_for_each(rdev, mddev) {
7039 if (test_bit(Journal, &rdev->flags))
7040 continue;
7041 if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
7042 conf->batch_bio_dispatch = false;
7043 break;
7044 }
7045 }
7046
Dan Williamsf5efd452009-10-16 15:55:38 +11007047 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11007048 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11007049
7050 conf->raid_disks = mddev->raid_disks;
7051 if (mddev->reshape_position == MaxSector)
7052 conf->previous_raid_disks = mddev->raid_disks;
7053 else
7054 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11007055 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11007056
Kees Cook6396bb22018-06-12 14:03:40 -07007057 conf->disks = kcalloc(max_disks, sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11007058 GFP_KERNEL);
Song Liud7bd3982016-11-23 22:50:39 -08007059
NeilBrown91adb562009-03-31 14:39:39 +11007060 if (!conf->disks)
7061 goto abort;
7062
Song Liud7bd3982016-11-23 22:50:39 -08007063 for (i = 0; i < max_disks; i++) {
7064 conf->disks[i].extra_page = alloc_page(GFP_KERNEL);
7065 if (!conf->disks[i].extra_page)
7066 goto abort;
7067 }
7068
Kent Overstreetafeee512018-05-20 18:25:52 -04007069 ret = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
7070 if (ret)
NeilBrowndd7a8f52017-04-05 14:05:51 +10007071 goto abort;
NeilBrown91adb562009-03-31 14:39:39 +11007072 conf->mddev = mddev;
7073
7074 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
7075 goto abort;
7076
Shaohua Li566c09c2013-11-14 15:16:17 +11007077 /* We init hash_locks[0] separately to that it can be used
7078 * as the reference lock in the spin_lock_nest_lock() call
7079 * in lock_all_device_hash_locks_irq in order to convince
7080 * lockdep that we know what we are doing.
7081 */
7082 spin_lock_init(conf->hash_locks);
7083 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
7084 spin_lock_init(conf->hash_locks + i);
7085
7086 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
7087 INIT_LIST_HEAD(conf->inactive_list + i);
7088
7089 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
7090 INIT_LIST_HEAD(conf->temp_inactive_list + i);
7091
Song Liu1e6d6902016-11-17 15:24:39 -08007092 atomic_set(&conf->r5c_cached_full_stripes, 0);
7093 INIT_LIST_HEAD(&conf->r5c_full_stripe_list);
7094 atomic_set(&conf->r5c_cached_partial_stripes, 0);
7095 INIT_LIST_HEAD(&conf->r5c_partial_stripe_list);
Shaohua Lie33fbb92017-02-10 16:18:09 -08007096 atomic_set(&conf->r5c_flushing_full_stripes, 0);
7097 atomic_set(&conf->r5c_flushing_partial_stripes, 0);
Song Liu1e6d6902016-11-17 15:24:39 -08007098
Dan Williams36d1c642009-07-14 11:48:22 -07007099 conf->level = mddev->new_level;
shli@kernel.org46d5b782014-12-15 12:57:02 +11007100 conf->chunk_sectors = mddev->new_chunk_sectors;
Dan Williams36d1c642009-07-14 11:48:22 -07007101 if (raid5_alloc_percpu(conf) != 0)
7102 goto abort;
7103
NeilBrown0c55e022010-05-03 14:09:02 +10007104 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11007105
NeilBrowndafb20f2012-03-19 12:46:39 +11007106 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11007107 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11007108 if (raid_disk >= max_disks
Shaohua Lif2076e72015-10-08 21:54:12 -07007109 || raid_disk < 0 || test_bit(Journal, &rdev->flags))
NeilBrown91adb562009-03-31 14:39:39 +11007110 continue;
7111 disk = conf->disks + raid_disk;
7112
NeilBrown17045f52011-12-23 10:17:53 +11007113 if (test_bit(Replacement, &rdev->flags)) {
7114 if (disk->replacement)
7115 goto abort;
7116 disk->replacement = rdev;
7117 } else {
7118 if (disk->rdev)
7119 goto abort;
7120 disk->rdev = rdev;
7121 }
NeilBrown91adb562009-03-31 14:39:39 +11007122
7123 if (test_bit(In_sync, &rdev->flags)) {
7124 char b[BDEVNAME_SIZE];
NeilBrowncc6167b2016-11-02 14:16:50 +11007125 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
7126 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05007127 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11007128 /* Cannot rely on bitmap to complete recovery */
7129 conf->fullsync = 1;
7130 }
7131
NeilBrown91adb562009-03-31 14:39:39 +11007132 conf->level = mddev->new_level;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11007133 if (conf->level == 6) {
NeilBrown91adb562009-03-31 14:39:39 +11007134 conf->max_degraded = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11007135 if (raid6_call.xor_syndrome)
7136 conf->rmw_level = PARITY_ENABLE_RMW;
7137 else
7138 conf->rmw_level = PARITY_DISABLE_RMW;
7139 } else {
NeilBrown91adb562009-03-31 14:39:39 +11007140 conf->max_degraded = 1;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11007141 conf->rmw_level = PARITY_ENABLE_RMW;
7142 }
NeilBrown91adb562009-03-31 14:39:39 +11007143 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11007144 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11007145 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10007146 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11007147 conf->prev_algo = mddev->layout;
NeilBrown5cac6bc2015-07-17 12:17:50 +10007148 } else {
7149 conf->prev_chunk_sectors = conf->chunk_sectors;
7150 conf->prev_algo = conf->algorithm;
NeilBrowne183eae2009-03-31 15:20:22 +11007151 }
NeilBrown91adb562009-03-31 14:39:39 +11007152
NeilBrownedbe83a2015-02-26 12:47:56 +11007153 conf->min_nr_stripes = NR_STRIPES;
Shaohua Liad5b0f72016-08-30 10:29:33 -07007154 if (mddev->reshape_position != MaxSector) {
7155 int stripes = max_t(int,
Yufen Yuc911c462020-07-18 05:29:07 -04007156 ((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4,
7157 ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4);
Shaohua Liad5b0f72016-08-30 10:29:33 -07007158 conf->min_nr_stripes = max(NR_STRIPES, stripes);
7159 if (conf->min_nr_stripes != NR_STRIPES)
NeilBrowncc6167b2016-11-02 14:16:50 +11007160 pr_info("md/raid:%s: force stripe size %d for reshape\n",
Shaohua Liad5b0f72016-08-30 10:29:33 -07007161 mdname(mddev), conf->min_nr_stripes);
7162 }
NeilBrownedbe83a2015-02-26 12:47:56 +11007163 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11007164 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li4bda5562013-11-14 15:16:17 +11007165 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
NeilBrownedbe83a2015-02-26 12:47:56 +11007166 if (grow_stripes(conf, conf->min_nr_stripes)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007167 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
7168 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11007169 goto abort;
7170 } else
NeilBrowncc6167b2016-11-02 14:16:50 +11007171 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory);
NeilBrownedbe83a2015-02-26 12:47:56 +11007172 /*
7173 * Losing a stripe head costs more than the time to refill it,
7174 * it reduces the queue depth and so can hurt throughput.
7175 * So set it rather large, scaled by number of devices.
7176 */
7177 conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
7178 conf->shrinker.scan_objects = raid5_cache_scan;
7179 conf->shrinker.count_objects = raid5_cache_count;
7180 conf->shrinker.batch = 128;
7181 conf->shrinker.flags = 0;
Chao Yu6a0f53f2016-09-20 10:33:57 +08007182 if (register_shrinker(&conf->shrinker)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007183 pr_warn("md/raid:%s: couldn't register shrinker.\n",
7184 mdname(mddev));
Chao Yu6a0f53f2016-09-20 10:33:57 +08007185 goto abort;
7186 }
NeilBrown91adb562009-03-31 14:39:39 +11007187
NeilBrown02326052012-07-03 15:56:52 +10007188 sprintf(pers_name, "raid%d", mddev->new_level);
7189 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11007190 if (!conf->thread) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007191 pr_warn("md/raid:%s: couldn't allocate thread.\n",
7192 mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11007193 goto abort;
7194 }
7195
7196 return conf;
7197
7198 abort:
7199 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10007200 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11007201 return ERR_PTR(-EIO);
7202 } else
7203 return ERR_PTR(-ENOMEM);
7204}
7205
NeilBrownc148ffd2009-11-13 17:47:00 +11007206static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
7207{
7208 switch (algo) {
7209 case ALGORITHM_PARITY_0:
7210 if (raid_disk < max_degraded)
7211 return 1;
7212 break;
7213 case ALGORITHM_PARITY_N:
7214 if (raid_disk >= raid_disks - max_degraded)
7215 return 1;
7216 break;
7217 case ALGORITHM_PARITY_0_6:
NeilBrownf72ffdd2014-09-30 14:23:59 +10007218 if (raid_disk == 0 ||
NeilBrownc148ffd2009-11-13 17:47:00 +11007219 raid_disk == raid_disks - 1)
7220 return 1;
7221 break;
7222 case ALGORITHM_LEFT_ASYMMETRIC_6:
7223 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7224 case ALGORITHM_LEFT_SYMMETRIC_6:
7225 case ALGORITHM_RIGHT_SYMMETRIC_6:
7226 if (raid_disk == raid_disks - 1)
7227 return 1;
7228 }
7229 return 0;
7230}
7231
Shaohua Li849674e2016-01-20 13:52:20 -08007232static int raid5_run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11007233{
NeilBrownd1688a62011-10-11 16:49:52 +11007234 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10007235 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11007236 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007237 struct md_rdev *rdev;
Shaohua Li713cf5a2015-08-13 14:32:03 -07007238 struct md_rdev *journal_dev = NULL;
NeilBrownc148ffd2009-11-13 17:47:00 +11007239 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11007240 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10007241 long long min_offset_diff = 0;
7242 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11007243
NeilBrowna415c0f2017-06-05 16:05:13 +10007244 if (mddev_init_writes_pending(mddev) < 0)
7245 return -ENOMEM;
7246
Andre Noll8c6ac862009-06-18 08:48:06 +10007247 if (mddev->recovery_cp != MaxSector)
NeilBrowncc6167b2016-11-02 14:16:50 +11007248 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
7249 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10007250
7251 rdev_for_each(rdev, mddev) {
7252 long long diff;
Shaohua Li713cf5a2015-08-13 14:32:03 -07007253
Shaohua Lif2076e72015-10-08 21:54:12 -07007254 if (test_bit(Journal, &rdev->flags)) {
Shaohua Li713cf5a2015-08-13 14:32:03 -07007255 journal_dev = rdev;
Shaohua Lif2076e72015-10-08 21:54:12 -07007256 continue;
7257 }
NeilBrownb5254dd2012-05-21 09:27:01 +10007258 if (rdev->raid_disk < 0)
7259 continue;
7260 diff = (rdev->new_data_offset - rdev->data_offset);
7261 if (first) {
7262 min_offset_diff = diff;
7263 first = 0;
7264 } else if (mddev->reshape_backwards &&
7265 diff < min_offset_diff)
7266 min_offset_diff = diff;
7267 else if (!mddev->reshape_backwards &&
7268 diff > min_offset_diff)
7269 min_offset_diff = diff;
7270 }
7271
NeilBrown230b55f2017-10-17 14:24:09 +11007272 if ((test_bit(MD_HAS_JOURNAL, &mddev->flags) || journal_dev) &&
7273 (mddev->bitmap_info.offset || mddev->bitmap_info.file)) {
7274 pr_notice("md/raid:%s: array cannot have both journal and bitmap\n",
7275 mdname(mddev));
7276 return -EINVAL;
7277 }
7278
NeilBrownf6705572006-03-27 01:18:11 -08007279 if (mddev->reshape_position != MaxSector) {
7280 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10007281 * Difficulties arise if the stripe we would write to
7282 * next is at or after the stripe we would read from next.
7283 * For a reshape that changes the number of devices, this
7284 * is only possible for a very short time, and mdadm makes
7285 * sure that time appears to have past before assembling
7286 * the array. So we fail if that time hasn't passed.
7287 * For a reshape that keeps the number of devices the same
7288 * mdadm must be monitoring the reshape can keeping the
7289 * critical areas read-only and backed up. It will start
7290 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08007291 */
7292 sector_t here_new, here_old;
7293 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11007294 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrown05256d92015-07-15 17:36:21 +10007295 int chunk_sectors;
7296 int new_data_disks;
NeilBrownf6705572006-03-27 01:18:11 -08007297
Shaohua Li713cf5a2015-08-13 14:32:03 -07007298 if (journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007299 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
7300 mdname(mddev));
Shaohua Li713cf5a2015-08-13 14:32:03 -07007301 return -EINVAL;
7302 }
7303
NeilBrown88ce4932009-03-31 15:24:23 +11007304 if (mddev->new_level != mddev->level) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007305 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
7306 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007307 return -EINVAL;
7308 }
NeilBrownf6705572006-03-27 01:18:11 -08007309 old_disks = mddev->raid_disks - mddev->delta_disks;
7310 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08007311 * further up in new geometry must map after here in old
7312 * geometry.
NeilBrown05256d92015-07-15 17:36:21 +10007313 * If the chunk sizes are different, then as we perform reshape
7314 * in units of the largest of the two, reshape_position needs
7315 * be a multiple of the largest chunk size times new data disks.
NeilBrownf6705572006-03-27 01:18:11 -08007316 */
7317 here_new = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007318 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
7319 new_data_disks = mddev->raid_disks - max_degraded;
7320 if (sector_div(here_new, chunk_sectors * new_data_disks)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007321 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
7322 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007323 return -EINVAL;
7324 }
NeilBrown05256d92015-07-15 17:36:21 +10007325 reshape_offset = here_new * chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08007326 /* here_new is the stripe we will write to */
7327 here_old = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007328 sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
NeilBrownf4168852007-02-28 20:11:53 -08007329 /* here_old is the first stripe that we might need to read
7330 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10007331 if (mddev->delta_disks == 0) {
7332 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10007333 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10007334 * and taking constant backups.
7335 * mdadm always starts a situation like this in
7336 * readonly mode so it can take control before
7337 * allowing any writes. So just check for that.
7338 */
NeilBrownb5254dd2012-05-21 09:27:01 +10007339 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
7340 abs(min_offset_diff) >= mddev->new_chunk_sectors)
7341 /* not really in-place - so OK */;
7342 else if (mddev->ro == 0) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007343 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
7344 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10007345 return -EINVAL;
7346 }
NeilBrown2c810cd2012-05-21 09:27:00 +10007347 } else if (mddev->reshape_backwards
NeilBrown05256d92015-07-15 17:36:21 +10007348 ? (here_new * chunk_sectors + min_offset_diff <=
7349 here_old * chunk_sectors)
7350 : (here_new * chunk_sectors >=
7351 here_old * chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08007352 /* Reading from the same stripe as writing to - bad */
NeilBrowncc6167b2016-11-02 14:16:50 +11007353 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
7354 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007355 return -EINVAL;
7356 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007357 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007358 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08007359 } else {
NeilBrown91adb562009-03-31 14:39:39 +11007360 BUG_ON(mddev->level != mddev->new_level);
7361 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10007362 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11007363 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08007364 }
7365
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01007366 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) &&
7367 test_bit(MD_HAS_PPL, &mddev->flags)) {
7368 pr_warn("md/raid:%s: using journal device and PPL not allowed - disabling PPL\n",
7369 mdname(mddev));
7370 clear_bit(MD_HAS_PPL, &mddev->flags);
Pawel Baldysiakddc08822017-08-16 17:13:45 +02007371 clear_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags);
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01007372 }
7373
NeilBrown245f46c2009-03-31 14:39:39 +11007374 if (mddev->private == NULL)
7375 conf = setup_conf(mddev);
7376 else
7377 conf = mddev->private;
7378
NeilBrown91adb562009-03-31 14:39:39 +11007379 if (IS_ERR(conf))
7380 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08007381
Song Liu486b0f72016-08-19 15:34:01 -07007382 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
7383 if (!journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007384 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
7385 mdname(mddev));
Song Liu486b0f72016-08-19 15:34:01 -07007386 mddev->ro = 1;
7387 set_disk_ro(mddev->gendisk, 1);
7388 } else if (mddev->recovery_cp == MaxSector)
7389 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
Shaohua Li7dde2ad2015-10-08 21:54:10 -07007390 }
7391
NeilBrownb5254dd2012-05-21 09:27:01 +10007392 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11007393 mddev->thread = conf->thread;
7394 conf->thread = NULL;
7395 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396
NeilBrown17045f52011-12-23 10:17:53 +11007397 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
7398 i++) {
7399 rdev = conf->disks[i].rdev;
7400 if (!rdev && conf->disks[i].replacement) {
7401 /* The replacement is all we have yet */
7402 rdev = conf->disks[i].replacement;
7403 conf->disks[i].replacement = NULL;
7404 clear_bit(Replacement, &rdev->flags);
7405 conf->disks[i].rdev = rdev;
7406 }
7407 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11007408 continue;
NeilBrown17045f52011-12-23 10:17:53 +11007409 if (conf->disks[i].replacement &&
7410 conf->reshape_progress != MaxSector) {
7411 /* replacements and reshape simply do not mix. */
NeilBrowncc6167b2016-11-02 14:16:50 +11007412 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
NeilBrown17045f52011-12-23 10:17:53 +11007413 goto abort;
7414 }
NeilBrown2f115882010-06-17 17:41:03 +10007415 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11007416 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10007417 continue;
7418 }
NeilBrownc148ffd2009-11-13 17:47:00 +11007419 /* This disc is not fully in-sync. However if it
7420 * just stored parity (beyond the recovery_offset),
7421 * when we don't need to be concerned about the
7422 * array being dirty.
7423 * When reshape goes 'backwards', we never have
7424 * partially completed devices, so we only need
7425 * to worry about reshape going forwards.
7426 */
7427 /* Hack because v0.91 doesn't store recovery_offset properly. */
7428 if (mddev->major_version == 0 &&
7429 mddev->minor_version > 90)
7430 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007431
NeilBrownc148ffd2009-11-13 17:47:00 +11007432 if (rdev->recovery_offset < reshape_offset) {
7433 /* We need to check old and new layout */
7434 if (!only_parity(rdev->raid_disk,
7435 conf->algorithm,
7436 conf->raid_disks,
7437 conf->max_degraded))
7438 continue;
7439 }
7440 if (!only_parity(rdev->raid_disk,
7441 conf->prev_algo,
7442 conf->previous_raid_disks,
7443 conf->max_degraded))
7444 continue;
7445 dirty_parity_disks++;
7446 }
NeilBrown91adb562009-03-31 14:39:39 +11007447
NeilBrown17045f52011-12-23 10:17:53 +11007448 /*
7449 * 0 for a fully functional array, 1 or 2 for a degraded array.
7450 */
Song Liu2e38a372017-01-24 10:45:30 -08007451 mddev->degraded = raid5_calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452
NeilBrown674806d2010-06-16 17:17:53 +10007453 if (has_failed(conf)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007454 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07007455 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007456 goto abort;
7457 }
7458
NeilBrown91adb562009-03-31 14:39:39 +11007459 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10007460 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11007461 mddev->resync_max_sectors = mddev->dev_sectors;
7462
NeilBrownc148ffd2009-11-13 17:47:00 +11007463 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464 mddev->recovery_cp != MaxSector) {
Artur Paszkiewicz4536bf9b2017-03-09 10:00:01 +01007465 if (test_bit(MD_HAS_PPL, &mddev->flags))
7466 pr_crit("md/raid:%s: starting dirty degraded array with PPL.\n",
7467 mdname(mddev));
7468 else if (mddev->ok_start_degraded)
NeilBrowncc6167b2016-11-02 14:16:50 +11007469 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
7470 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007471 else {
NeilBrowncc6167b2016-11-02 14:16:50 +11007472 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
7473 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007474 goto abort;
7475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 }
7477
NeilBrowncc6167b2016-11-02 14:16:50 +11007478 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
7479 mdname(mddev), conf->level,
7480 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
7481 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007482
7483 print_raid5_conf(conf);
7484
NeilBrownfef9c612009-03-31 15:16:46 +11007485 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11007486 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08007487 atomic_set(&conf->reshape_stripes, 0);
7488 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7489 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7490 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7491 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7492 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007493 "reshape");
Aditya Pakkie406f122019-03-04 16:48:54 -06007494 if (!mddev->sync_thread)
7495 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08007496 }
7497
Linus Torvalds1da177e2005-04-16 15:20:36 -07007498 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10007499 if (mddev->to_remove == &raid5_attrs_group)
7500 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007501 else if (mddev->kobj.sd &&
7502 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrowncc6167b2016-11-02 14:16:50 +11007503 pr_warn("raid5: failed to create sysfs attributes for %s\n",
7504 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10007505 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
7506
7507 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007508 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10007509 /* read-ahead size must cover two whole stripes, which
7510 * is 2 * (datadisks) * chunksize where 'n' is the
7511 * number of raid devices
7512 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 int data_disks = conf->previous_raid_disks - conf->max_degraded;
7514 int stripe = data_disks *
7515 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Jan Karadc3b17c2017-02-02 15:56:50 +01007516 if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
7517 mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10007518
NeilBrown9f7c2222010-07-26 12:04:13 +10007519 chunk_size = mddev->chunk_sectors << 9;
7520 blk_queue_io_min(mddev->queue, chunk_size);
7521 blk_queue_io_opt(mddev->queue, chunk_size *
7522 (conf->raid_disks - conf->max_degraded));
Kent Overstreetc78afc62013-07-11 22:39:53 -07007523 mddev->queue->limits.raid_partial_stripes_expensive = 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007524 /*
7525 * We can only discard a whole stripe. It doesn't make sense to
7526 * discard data disk but write parity disk
7527 */
7528 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11007529 /* Round up to power of 2, as discard handling
7530 * currently assumes that */
7531 while ((stripe-1) & stripe)
7532 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007533 mddev->queue->limits.discard_alignment = stripe;
7534 mddev->queue->limits.discard_granularity = stripe;
Konstantin Khlebnikove8d7c332016-11-27 19:32:32 +03007535
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007536 blk_queue_max_write_same_sectors(mddev->queue, 0);
Christoph Hellwig3deff1a2017-04-05 19:21:03 +02007537 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007538
NeilBrown05616be2012-05-21 09:27:00 +10007539 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007540 disk_stack_limits(mddev->gendisk, rdev->bdev,
7541 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10007542 disk_stack_limits(mddev->gendisk, rdev->bdev,
7543 rdev->new_data_offset << 9);
7544 }
Shaohua Li620125f2012-10-11 13:49:05 +11007545
Christoph Hellwig48920ff2017-04-05 19:21:23 +02007546 /*
7547 * zeroing is required, otherwise data
7548 * could be lost. Consider a scenario: discard a stripe
7549 * (the stripe could be inconsistent if
7550 * discard_zeroes_data is 0); write one disk of the
7551 * stripe (the stripe could be inconsistent again
7552 * depending on which disks are used to calculate
7553 * parity); the disk is broken; The stripe data of this
7554 * disk is lost.
7555 *
7556 * We only allow DISCARD if the sysadmin has confirmed that
7557 * only safe devices are in use by setting a module parameter.
7558 * A better idea might be to turn DISCARD into WRITE_ZEROES
7559 * requests, as that is required to be safe.
7560 */
7561 if (devices_handle_discard_safely &&
Jes Sorensene7597e62016-02-16 16:44:24 -05007562 mddev->queue->limits.max_discard_sectors >= (stripe >> 9) &&
7563 mddev->queue->limits.discard_granularity >= stripe)
Bart Van Assche8b904b52018-03-07 17:10:10 -08007564 blk_queue_flag_set(QUEUE_FLAG_DISCARD,
Shaohua Li620125f2012-10-11 13:49:05 +11007565 mddev->queue);
7566 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08007567 blk_queue_flag_clear(QUEUE_FLAG_DISCARD,
Shaohua Li620125f2012-10-11 13:49:05 +11007568 mddev->queue);
Shaohua Li1dffddd2016-09-08 10:49:06 -07007569
7570 blk_queue_max_hw_sectors(mddev->queue, UINT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571 }
7572
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02007573 if (log_init(conf, journal_dev, raid5_has_ppl(conf)))
Artur Paszkiewiczff875732017-03-09 09:59:58 +01007574 goto abort;
Shaohua Li5c7e81c2015-08-13 14:32:04 -07007575
Linus Torvalds1da177e2005-04-16 15:20:36 -07007576 return 0;
7577abort:
NeilBrown01f96c02011-09-21 15:30:20 +10007578 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11007579 print_raid5_conf(conf);
7580 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007581 mddev->private = NULL;
NeilBrowncc6167b2016-11-02 14:16:50 +11007582 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 return -EIO;
7584}
7585
NeilBrownafa0f552014-12-15 12:56:58 +11007586static void raid5_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587{
NeilBrownafa0f552014-12-15 12:56:58 +11007588 struct r5conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007589
Dan Williams95fc17a2009-07-31 12:39:15 +10007590 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10007591 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592}
7593
Shaohua Li849674e2016-01-20 13:52:20 -08007594static void raid5_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595{
NeilBrownd1688a62011-10-11 16:49:52 +11007596 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007597 int i;
7598
Andre Noll9d8f0362009-06-18 08:45:01 +10007599 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
NeilBrown3cb5edf2015-07-15 17:24:17 +10007600 conf->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07007601 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
NeilBrown5fd13352016-06-02 16:19:52 +10007602 rcu_read_lock();
7603 for (i = 0; i < conf->raid_disks; i++) {
7604 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
7605 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
7606 }
7607 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007608 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007609}
7610
NeilBrownd1688a62011-10-11 16:49:52 +11007611static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612{
7613 int i;
7614 struct disk_info *tmp;
7615
NeilBrowncc6167b2016-11-02 14:16:50 +11007616 pr_debug("RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617 if (!conf) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007618 pr_debug("(conf==NULL)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 return;
7620 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007621 pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level,
NeilBrown0c55e022010-05-03 14:09:02 +10007622 conf->raid_disks,
7623 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007624
7625 for (i = 0; i < conf->raid_disks; i++) {
7626 char b[BDEVNAME_SIZE];
7627 tmp = conf->disks + i;
7628 if (tmp->rdev)
NeilBrowncc6167b2016-11-02 14:16:50 +11007629 pr_debug(" disk %d, o:%d, dev:%s\n",
NeilBrown0c55e022010-05-03 14:09:02 +10007630 i, !test_bit(Faulty, &tmp->rdev->flags),
7631 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007632 }
7633}
7634
NeilBrownfd01b882011-10-11 16:47:53 +11007635static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007636{
7637 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11007638 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007639 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10007640 int count = 0;
7641 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007642
7643 for (i = 0; i < conf->raid_disks; i++) {
7644 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11007645 if (tmp->replacement
7646 && tmp->replacement->recovery_offset == MaxSector
7647 && !test_bit(Faulty, &tmp->replacement->flags)
7648 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
7649 /* Replacement has just become active. */
7650 if (!tmp->rdev
7651 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
7652 count++;
7653 if (tmp->rdev) {
7654 /* Replaced device not technically faulty,
7655 * but we need to be sure it gets removed
7656 * and never re-added.
7657 */
7658 set_bit(Faulty, &tmp->rdev->flags);
7659 sysfs_notify_dirent_safe(
7660 tmp->rdev->sysfs_state);
7661 }
7662 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
7663 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10007664 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08007665 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07007666 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10007667 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11007668 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007669 }
7670 }
NeilBrown6b965622010-08-18 11:56:59 +10007671 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08007672 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007673 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007674 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007675 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676}
7677
NeilBrownb8321b62011-12-23 10:17:51 +11007678static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007679{
NeilBrownd1688a62011-10-11 16:49:52 +11007680 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007681 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11007682 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11007683 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007684 struct disk_info *p = conf->disks + number;
7685
7686 print_raid5_conf(conf);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007687 if (test_bit(Journal, &rdev->flags) && conf->log) {
Shaohua Lic2bb6242015-10-08 21:54:07 -07007688 /*
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007689 * we can't wait pending write here, as this is called in
7690 * raid5d, wait will deadlock.
NeilBrown84dd97a2017-03-15 14:05:14 +11007691 * neilb: there is no locking about new writes here,
7692 * so this cannot be safe.
Shaohua Lic2bb6242015-10-08 21:54:07 -07007693 */
Song Liu70d466f2017-05-11 15:28:28 -07007694 if (atomic_read(&conf->active_stripes) ||
7695 atomic_read(&conf->r5c_cached_full_stripes) ||
7696 atomic_read(&conf->r5c_cached_partial_stripes)) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007697 return -EBUSY;
NeilBrown84dd97a2017-03-15 14:05:14 +11007698 }
Artur Paszkiewiczff875732017-03-09 09:59:58 +01007699 log_exit(conf);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007700 return 0;
Shaohua Lic2bb6242015-10-08 21:54:07 -07007701 }
NeilBrown657e3e42011-12-23 10:17:52 +11007702 if (rdev == p->rdev)
7703 rdevp = &p->rdev;
7704 else if (rdev == p->replacement)
7705 rdevp = &p->replacement;
7706 else
7707 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11007708
NeilBrown657e3e42011-12-23 10:17:52 +11007709 if (number >= conf->raid_disks &&
7710 conf->reshape_progress == MaxSector)
7711 clear_bit(In_sync, &rdev->flags);
7712
7713 if (test_bit(In_sync, &rdev->flags) ||
7714 atomic_read(&rdev->nr_pending)) {
7715 err = -EBUSY;
7716 goto abort;
7717 }
7718 /* Only remove non-faulty devices if recovery
7719 * isn't possible.
7720 */
7721 if (!test_bit(Faulty, &rdev->flags) &&
7722 mddev->recovery_disabled != conf->recovery_disabled &&
7723 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11007724 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11007725 number < conf->raid_disks) {
7726 err = -EBUSY;
7727 goto abort;
7728 }
7729 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10007730 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
7731 synchronize_rcu();
7732 if (atomic_read(&rdev->nr_pending)) {
7733 /* lost the race, try later */
7734 err = -EBUSY;
7735 *rdevp = rdev;
7736 }
7737 }
Artur Paszkiewicz6358c232017-03-09 10:00:02 +01007738 if (!err) {
7739 err = log_modify(conf, rdev, false);
7740 if (err)
7741 goto abort;
7742 }
NeilBrownd787be42016-06-02 16:19:53 +10007743 if (p->replacement) {
NeilBrowndd054fc2011-12-23 10:17:53 +11007744 /* We must have just cleared 'rdev' */
7745 p->rdev = p->replacement;
7746 clear_bit(Replacement, &p->replacement->flags);
7747 smp_mb(); /* Make sure other CPUs may see both as identical
7748 * but will never see neither - if they are careful
7749 */
7750 p->replacement = NULL;
Artur Paszkiewicz6358c232017-03-09 10:00:02 +01007751
7752 if (!err)
7753 err = log_modify(conf, p->rdev, true);
Guoqing Jiange5bc9c32017-04-24 15:58:04 +08007754 }
7755
7756 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007757abort:
7758
7759 print_raid5_conf(conf);
7760 return err;
7761}
7762
NeilBrownfd01b882011-10-11 16:47:53 +11007763static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764{
NeilBrownd1688a62011-10-11 16:49:52 +11007765 struct r5conf *conf = mddev->private;
Xiao Nid9771f52019-06-14 15:41:05 -07007766 int ret, err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007767 int disk;
7768 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10007769 int first = 0;
7770 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007771
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007772 if (test_bit(Journal, &rdev->flags)) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007773 if (conf->log)
7774 return -EBUSY;
7775
7776 rdev->raid_disk = 0;
7777 /*
7778 * The array is in readonly mode if journal is missing, so no
7779 * write requests running. We should be safe
7780 */
Xiao Nid9771f52019-06-14 15:41:05 -07007781 ret = log_init(conf, rdev, false);
7782 if (ret)
7783 return ret;
7784
7785 ret = r5l_start(conf->log);
7786 if (ret)
7787 return ret;
7788
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007789 return 0;
7790 }
NeilBrown7f0da592011-07-28 11:39:22 +10007791 if (mddev->recovery_disabled == conf->recovery_disabled)
7792 return -EBUSY;
7793
NeilBrowndc10c642012-03-19 12:46:37 +11007794 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007795 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10007796 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007797
Neil Brown6c2fce22008-06-28 08:31:31 +10007798 if (rdev->raid_disk >= 0)
7799 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800
7801 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07007802 * find the disk ... but prefer rdev->saved_raid_disk
7803 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007804 */
NeilBrown16a53ec2006-06-26 00:27:38 -07007805 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10007806 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07007807 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10007808 first = rdev->saved_raid_disk;
7809
7810 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007811 p = conf->disks + disk;
7812 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08007813 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007814 rdev->raid_disk = disk;
NeilBrown72626682005-09-09 16:23:54 -07007815 if (rdev->saved_raid_disk != disk)
7816 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08007817 rcu_assign_pointer(p->rdev, rdev);
Artur Paszkiewicz6358c232017-03-09 10:00:02 +01007818
7819 err = log_modify(conf, rdev, true);
7820
NeilBrown5cfb22a2012-07-03 11:46:53 +10007821 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007822 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007823 }
7824 for (disk = first; disk <= last; disk++) {
7825 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007826 if (test_bit(WantReplacement, &p->rdev->flags) &&
7827 p->replacement == NULL) {
7828 clear_bit(In_sync, &rdev->flags);
7829 set_bit(Replacement, &rdev->flags);
7830 rdev->raid_disk = disk;
7831 err = 0;
7832 conf->fullsync = 1;
7833 rcu_assign_pointer(p->replacement, rdev);
7834 break;
7835 }
7836 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007837out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007838 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10007839 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840}
7841
NeilBrownfd01b882011-10-11 16:47:53 +11007842static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007843{
7844 /* no resync is happening, and there is enough space
7845 * on all devices, so we can resize.
7846 * We need to make sure resync covers any new space.
7847 * If the array is shrinking we should possibly wait until
7848 * any io in the removed space completes, but it hardly seems
7849 * worth it.
7850 */
NeilBrowna4a61252012-05-22 13:55:27 +10007851 sector_t newsize;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007852 struct r5conf *conf = mddev->private;
7853
Shaohua Lie254de62018-08-29 11:05:42 -07007854 if (raid5_has_log(conf) || raid5_has_ppl(conf))
Shaohua Li713cf5a2015-08-13 14:32:03 -07007855 return -EINVAL;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007856 sectors &= ~((sector_t)conf->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10007857 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
7858 if (mddev->external_size &&
7859 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11007860 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10007861 if (mddev->bitmap) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007862 int ret = md_bitmap_resize(mddev->bitmap, sectors, 0, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10007863 if (ret)
7864 return ret;
7865 }
7866 md_set_array_sectors(mddev, newsize);
NeilBrownb0986362011-05-11 15:52:21 +10007867 if (sectors > mddev->dev_sectors &&
7868 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11007869 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007870 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7871 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007872 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07007873 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874 return 0;
7875}
7876
NeilBrownfd01b882011-10-11 16:47:53 +11007877static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10007878{
7879 /* Can only proceed if there are plenty of stripe_heads.
7880 * We need a minimum of one full stripe,, and for sensible progress
7881 * it is best to have about 4 times that.
7882 * If we require 4 times, then the default 256 4K stripe_heads will
7883 * allow for chunk sizes up to 256K, which is probably OK.
7884 * If the chunk size is greater, user-space should request more
7885 * stripe_heads first.
7886 */
NeilBrownd1688a62011-10-11 16:49:52 +11007887 struct r5conf *conf = mddev->private;
Yufen Yuc911c462020-07-18 05:29:07 -04007888 if (((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007889 > conf->min_nr_stripes ||
Yufen Yuc911c462020-07-18 05:29:07 -04007890 ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007891 > conf->min_nr_stripes) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007892 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7893 mdname(mddev),
7894 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
Yufen Yuc911c462020-07-18 05:29:07 -04007895 / RAID5_STRIPE_SIZE(conf))*4);
NeilBrown01ee22b2009-06-18 08:47:20 +10007896 return 0;
7897 }
7898 return 1;
7899}
7900
NeilBrownfd01b882011-10-11 16:47:53 +11007901static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08007902{
NeilBrownd1688a62011-10-11 16:49:52 +11007903 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08007904
Shaohua Lie254de62018-08-29 11:05:42 -07007905 if (raid5_has_log(conf) || raid5_has_ppl(conf))
Shaohua Li713cf5a2015-08-13 14:32:03 -07007906 return -EINVAL;
NeilBrown88ce4932009-03-31 15:24:23 +11007907 if (mddev->delta_disks == 0 &&
7908 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10007909 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10007910 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10007911 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11007912 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10007913 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11007914 /* We might be able to shrink, but the devices must
7915 * be made bigger first.
7916 * For raid6, 4 is the minimum size.
7917 * Otherwise 2 is the minimum
7918 */
7919 int min = 2;
7920 if (mddev->level == 6)
7921 min = 4;
7922 if (mddev->raid_disks + mddev->delta_disks < min)
7923 return -EINVAL;
7924 }
NeilBrown29269552006-03-27 01:18:10 -08007925
NeilBrown01ee22b2009-06-18 08:47:20 +10007926 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08007927 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08007928
NeilBrown738a2732015-05-08 18:19:39 +10007929 if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
7930 mddev->delta_disks > 0)
7931 if (resize_chunks(conf,
7932 conf->previous_raid_disks
7933 + max(0, mddev->delta_disks),
7934 max(mddev->new_chunk_sectors,
7935 mddev->chunk_sectors)
7936 ) < 0)
7937 return -ENOMEM;
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02007938
7939 if (conf->previous_raid_disks + mddev->delta_disks <= conf->pool_size)
7940 return 0; /* never bother to shrink */
NeilBrowne56108d62012-10-11 14:24:13 +11007941 return resize_stripes(conf, (conf->previous_raid_disks
7942 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08007943}
7944
NeilBrownfd01b882011-10-11 16:47:53 +11007945static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08007946{
NeilBrownd1688a62011-10-11 16:49:52 +11007947 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11007948 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08007949 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07007950 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08007951
NeilBrownf4168852007-02-28 20:11:53 -08007952 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08007953 return -EBUSY;
7954
NeilBrown01ee22b2009-06-18 08:47:20 +10007955 if (!check_stripe_cache(mddev))
7956 return -ENOSPC;
7957
NeilBrown30b67642012-05-22 13:55:28 +10007958 if (has_failed(conf))
7959 return -EINVAL;
7960
NeilBrownc6563a82012-05-21 09:27:00 +10007961 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11007962 if (!test_bit(In_sync, &rdev->flags)
7963 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08007964 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10007965 }
NeilBrown63c70c42006-03-27 01:18:13 -08007966
NeilBrownf4168852007-02-28 20:11:53 -08007967 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08007968 /* Not enough devices even to make a degraded array
7969 * of that size
7970 */
7971 return -EINVAL;
7972
NeilBrownec32a2b2009-03-31 15:17:38 +11007973 /* Refuse to reduce size of the array. Any reductions in
7974 * array size must be through explicit setting of array_size
7975 * attribute.
7976 */
7977 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
7978 < mddev->array_sectors) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007979 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
7980 mdname(mddev));
NeilBrownec32a2b2009-03-31 15:17:38 +11007981 return -EINVAL;
7982 }
7983
NeilBrownf6705572006-03-27 01:18:11 -08007984 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08007985 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10007986 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007987 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08007988 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007989 conf->prev_chunk_sectors = conf->chunk_sectors;
7990 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11007991 conf->prev_algo = conf->algorithm;
7992 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10007993 conf->generation++;
7994 /* Code that selects data_offset needs to see the generation update
7995 * if reshape_progress has been set - so a memory barrier needed.
7996 */
7997 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10007998 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11007999 conf->reshape_progress = raid5_size(mddev, 0, 0);
8000 else
8001 conf->reshape_progress = 0;
8002 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10008003 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08008004 spin_unlock_irq(&conf->device_lock);
8005
NeilBrown4d77e3b2013-08-27 15:57:47 +10008006 /* Now make sure any requests that proceeded on the assumption
8007 * the reshape wasn't running - like Discard or Read - have
8008 * completed.
8009 */
8010 mddev_suspend(mddev);
8011 mddev_resume(mddev);
8012
NeilBrown29269552006-03-27 01:18:10 -08008013 /* Add some new drives, as many as will fit.
8014 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10008015 * Don't add devices if we are reducing the number of
8016 * devices in the array. This is because it is not possible
8017 * to correctly record the "partially reconstructed" state of
8018 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08008019 */
NeilBrown87a8dec2011-01-31 11:57:43 +11008020 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11008021 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11008022 if (rdev->raid_disk < 0 &&
8023 !test_bit(Faulty, &rdev->flags)) {
8024 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11008025 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11008026 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11008027 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11008028 else
NeilBrown87a8dec2011-01-31 11:57:43 +11008029 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10008030
Damien Le Moal2aada5b2020-07-16 13:54:42 +09008031 /* Failure here is OK */
8032 sysfs_link_rdev(mddev, rdev);
NeilBrown50da0842011-01-31 11:57:43 +11008033 }
NeilBrown87a8dec2011-01-31 11:57:43 +11008034 } else if (rdev->raid_disk >= conf->previous_raid_disks
8035 && !test_bit(Faulty, &rdev->flags)) {
8036 /* This is a spare that was manually added */
8037 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11008038 }
NeilBrown29269552006-03-27 01:18:10 -08008039
NeilBrown87a8dec2011-01-31 11:57:43 +11008040 /* When a reshape changes the number of devices,
8041 * ->degraded is measured against the larger of the
8042 * pre and post number of devices.
8043 */
NeilBrownec32a2b2009-03-31 15:17:38 +11008044 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08008045 mddev->degraded = raid5_calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11008046 spin_unlock_irqrestore(&conf->device_lock, flags);
8047 }
NeilBrown63c70c42006-03-27 01:18:13 -08008048 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10008049 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08008050 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownf6705572006-03-27 01:18:11 -08008051
NeilBrown29269552006-03-27 01:18:10 -08008052 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8053 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008054 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown29269552006-03-27 01:18:10 -08008055 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8056 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8057 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10008058 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08008059 if (!mddev->sync_thread) {
8060 mddev->recovery = 0;
8061 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11008062 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08008063 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11008064 mddev->new_chunk_sectors =
8065 conf->chunk_sectors = conf->prev_chunk_sectors;
8066 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10008067 rdev_for_each(rdev, mddev)
8068 rdev->new_data_offset = rdev->data_offset;
8069 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11008070 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11008071 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11008072 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11008073 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08008074 spin_unlock_irq(&conf->device_lock);
8075 return -EAGAIN;
8076 }
NeilBrownc8f517c2009-03-31 15:28:40 +11008077 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08008078 md_wakeup_thread(mddev->sync_thread);
8079 md_new_event(mddev);
8080 return 0;
8081}
NeilBrown29269552006-03-27 01:18:10 -08008082
NeilBrownec32a2b2009-03-31 15:17:38 +11008083/* This is called from the reshape thread and should make any
8084 * changes needed in 'conf'
8085 */
NeilBrownd1688a62011-10-11 16:49:52 +11008086static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08008087{
NeilBrown29269552006-03-27 01:18:10 -08008088
NeilBrownf6705572006-03-27 01:18:11 -08008089 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrowndb0505d2017-10-17 16:18:36 +11008090 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07008091
NeilBrownf6705572006-03-27 01:18:11 -08008092 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11008093 conf->previous_raid_disks = conf->raid_disks;
Xiao Nib5d27712017-07-05 17:34:04 +08008094 md_finish_reshape(conf->mddev);
NeilBrown05616be2012-05-21 09:27:00 +10008095 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11008096 conf->reshape_progress = MaxSector;
NeilBrown6cbd8142015-07-24 13:30:32 +10008097 conf->mddev->reshape_position = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11008098 rdev_for_each(rdev, conf->mddev)
8099 if (rdev->raid_disk >= 0 &&
8100 !test_bit(Journal, &rdev->flags) &&
8101 !test_bit(In_sync, &rdev->flags))
8102 rdev->recovery_offset = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08008103 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11008104 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07008105
8106 /* read-ahead size must cover two whole stripes, which is
8107 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
8108 */
NeilBrown4a5add42010-06-01 19:37:28 +10008109 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11008110 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10008111 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11008112 / PAGE_SIZE);
Jan Karadc3b17c2017-02-02 15:56:50 +01008113 if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
8114 conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown16a53ec2006-06-26 00:27:38 -07008115 }
NeilBrown29269552006-03-27 01:18:10 -08008116 }
NeilBrown29269552006-03-27 01:18:10 -08008117}
8118
NeilBrownec32a2b2009-03-31 15:17:38 +11008119/* This is called from the raid5d thread with mddev_lock held.
8120 * It makes config changes to the device.
8121 */
NeilBrownfd01b882011-10-11 16:47:53 +11008122static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11008123{
NeilBrownd1688a62011-10-11 16:49:52 +11008124 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11008125
8126 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8127
BingJing Chang88763912018-02-22 13:34:46 +08008128 if (mddev->delta_disks <= 0) {
NeilBrownec32a2b2009-03-31 15:17:38 +11008129 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11008130 spin_lock_irq(&conf->device_lock);
Song Liu2e38a372017-01-24 10:45:30 -08008131 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11008132 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11008133 for (d = conf->raid_disks ;
8134 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10008135 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11008136 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10008137 if (rdev)
8138 clear_bit(In_sync, &rdev->flags);
8139 rdev = conf->disks[d].replacement;
8140 if (rdev)
8141 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10008142 }
NeilBrowncea9c222009-03-31 15:15:05 +11008143 }
NeilBrown88ce4932009-03-31 15:24:23 +11008144 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10008145 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11008146 mddev->reshape_position = MaxSector;
8147 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10008148 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11008149 }
8150}
8151
NeilBrownb03e0cc2017-10-19 12:49:15 +11008152static void raid5_quiesce(struct mddev *mddev, int quiesce)
NeilBrown72626682005-09-09 16:23:54 -07008153{
NeilBrownd1688a62011-10-11 16:49:52 +11008154 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07008155
NeilBrownb03e0cc2017-10-19 12:49:15 +11008156 if (quiesce) {
8157 /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11008158 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10008159 /* '2' tells resync/reshape to pause so that all
8160 * active stripes can drain
8161 */
Song Liua39f7af2016-11-17 15:24:40 -08008162 r5c_flush_cache(conf, INT_MAX);
NeilBrown64bd6602009-08-03 10:59:58 +10008163 conf->quiesce = 2;
Yuanhan Liub1b46482015-05-08 18:19:06 +10008164 wait_event_cmd(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08008165 atomic_read(&conf->active_stripes) == 0 &&
8166 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11008167 unlock_all_device_hash_locks_irq(conf),
8168 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10008169 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11008170 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10008171 /* allow reshape to continue */
8172 wake_up(&conf->wait_for_overlap);
NeilBrownb03e0cc2017-10-19 12:49:15 +11008173 } else {
8174 /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11008175 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07008176 conf->quiesce = 0;
Yuanhan Liub1b46482015-05-08 18:19:06 +10008177 wake_up(&conf->wait_for_quiescent);
NeilBrowne464eaf2006-03-27 01:18:14 -08008178 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11008179 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07008180 }
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01008181 log_quiesce(conf, quiesce);
NeilBrown72626682005-09-09 16:23:54 -07008182}
NeilBrownb15c2e52006-01-06 00:20:16 -08008183
NeilBrownfd01b882011-10-11 16:47:53 +11008184static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11008185{
NeilBrowne373ab12011-10-11 16:48:59 +11008186 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07008187 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11008188
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008189 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11008190 if (raid0_conf->nr_strip_zones > 1) {
NeilBrowncc6167b2016-11-02 14:16:50 +11008191 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
8192 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008193 return ERR_PTR(-EINVAL);
8194 }
8195
NeilBrowne373ab12011-10-11 16:48:59 +11008196 sectors = raid0_conf->strip_zone[0].zone_end;
8197 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10008198 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008199 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11008200 mddev->new_layout = ALGORITHM_PARITY_N;
8201 mddev->new_chunk_sectors = mddev->chunk_sectors;
8202 mddev->raid_disks += 1;
8203 mddev->delta_disks = 1;
8204 /* make sure it will be not marked as dirty */
8205 mddev->recovery_cp = MaxSector;
8206
8207 return setup_conf(mddev);
8208}
8209
NeilBrownfd01b882011-10-11 16:47:53 +11008210static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11008211{
8212 int chunksect;
Shaohua Li6995f0b2016-12-08 15:48:17 -08008213 void *ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11008214
8215 if (mddev->raid_disks != 2 ||
8216 mddev->degraded > 1)
8217 return ERR_PTR(-EINVAL);
8218
8219 /* Should check if there are write-behind devices? */
8220
8221 chunksect = 64*2; /* 64K by default */
8222
8223 /* The array must be an exact multiple of chunksize */
8224 while (chunksect && (mddev->array_sectors & (chunksect-1)))
8225 chunksect >>= 1;
8226
Yufen Yuc911c462020-07-18 05:29:07 -04008227 if ((chunksect<<9) < RAID5_STRIPE_SIZE((struct r5conf *)mddev->private))
NeilBrownd562b0c2009-03-31 14:39:39 +11008228 /* array size does not allow a suitable chunk size */
8229 return ERR_PTR(-EINVAL);
8230
8231 mddev->new_level = 5;
8232 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10008233 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11008234
Shaohua Li6995f0b2016-12-08 15:48:17 -08008235 ret = setup_conf(mddev);
Jes Sorensen32cd7cb2017-01-06 19:31:35 -05008236 if (!IS_ERR(ret))
Shaohua Li394ed8e2017-01-04 16:10:19 -08008237 mddev_clear_unsupported_flags(mddev,
8238 UNSUPPORTED_MDDEV_FLAGS);
Shaohua Li6995f0b2016-12-08 15:48:17 -08008239 return ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11008240}
8241
NeilBrownfd01b882011-10-11 16:47:53 +11008242static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11008243{
8244 int new_layout;
8245
8246 switch (mddev->layout) {
8247 case ALGORITHM_LEFT_ASYMMETRIC_6:
8248 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
8249 break;
8250 case ALGORITHM_RIGHT_ASYMMETRIC_6:
8251 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
8252 break;
8253 case ALGORITHM_LEFT_SYMMETRIC_6:
8254 new_layout = ALGORITHM_LEFT_SYMMETRIC;
8255 break;
8256 case ALGORITHM_RIGHT_SYMMETRIC_6:
8257 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
8258 break;
8259 case ALGORITHM_PARITY_0_6:
8260 new_layout = ALGORITHM_PARITY_0;
8261 break;
8262 case ALGORITHM_PARITY_N:
8263 new_layout = ALGORITHM_PARITY_N;
8264 break;
8265 default:
8266 return ERR_PTR(-EINVAL);
8267 }
8268 mddev->new_level = 5;
8269 mddev->new_layout = new_layout;
8270 mddev->delta_disks = -1;
8271 mddev->raid_disks -= 1;
8272 return setup_conf(mddev);
8273}
8274
NeilBrownfd01b882011-10-11 16:47:53 +11008275static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11008276{
NeilBrown88ce4932009-03-31 15:24:23 +11008277 /* For a 2-drive array, the layout and chunk size can be changed
8278 * immediately as not restriping is needed.
8279 * For larger arrays we record the new value - after validation
8280 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11008281 */
NeilBrownd1688a62011-10-11 16:49:52 +11008282 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10008283 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11008284
NeilBrown597a7112009-06-18 08:47:42 +10008285 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11008286 return -EINVAL;
8287 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008288 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11008289 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008290 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11008291 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008292 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11008293 /* not factor of array size */
8294 return -EINVAL;
8295 }
8296
8297 /* They look valid */
8298
NeilBrown88ce4932009-03-31 15:24:23 +11008299 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10008300 /* can make the change immediately */
8301 if (mddev->new_layout >= 0) {
8302 conf->algorithm = mddev->new_layout;
8303 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11008304 }
8305 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10008306 conf->chunk_sectors = new_chunk ;
8307 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11008308 }
Shaohua Li29530792016-12-08 15:48:19 -08008309 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown88ce4932009-03-31 15:24:23 +11008310 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11008311 }
NeilBrown50ac1682009-06-18 08:47:55 +10008312 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11008313}
8314
NeilBrownfd01b882011-10-11 16:47:53 +11008315static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11008316{
NeilBrown597a7112009-06-18 08:47:42 +10008317 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10008318
NeilBrown597a7112009-06-18 08:47:42 +10008319 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11008320 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008321 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008322 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11008323 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008324 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11008325 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008326 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11008327 /* not factor of array size */
8328 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008329 }
NeilBrown88ce4932009-03-31 15:24:23 +11008330
8331 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10008332 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11008333}
8334
NeilBrownfd01b882011-10-11 16:47:53 +11008335static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11008336{
8337 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008338 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008339 * raid1 - if there are two drives. We need to know the chunk size
8340 * raid4 - trivial - just use a raid4 layout.
8341 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008342 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008343 if (mddev->level == 0)
8344 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11008345 if (mddev->level == 1)
8346 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11008347 if (mddev->level == 4) {
8348 mddev->new_layout = ALGORITHM_PARITY_N;
8349 mddev->new_level = 5;
8350 return setup_conf(mddev);
8351 }
NeilBrownfc9739c2009-03-31 14:57:20 +11008352 if (mddev->level == 6)
8353 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11008354
8355 return ERR_PTR(-EINVAL);
8356}
8357
NeilBrownfd01b882011-10-11 16:47:53 +11008358static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11008359{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008360 /* raid4 can take over:
8361 * raid0 - if there is only one strip zone
8362 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11008363 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008364 if (mddev->level == 0)
8365 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11008366 if (mddev->level == 5 &&
8367 mddev->layout == ALGORITHM_PARITY_N) {
8368 mddev->new_layout = 0;
8369 mddev->new_level = 4;
8370 return setup_conf(mddev);
8371 }
8372 return ERR_PTR(-EINVAL);
8373}
NeilBrownd562b0c2009-03-31 14:39:39 +11008374
NeilBrown84fc4b52011-10-11 16:49:58 +11008375static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11008376
NeilBrownfd01b882011-10-11 16:47:53 +11008377static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11008378{
8379 /* Currently can only take over a raid5. We map the
8380 * personality to an equivalent raid6 personality
8381 * with the Q block at the end.
8382 */
8383 int new_layout;
8384
8385 if (mddev->pers != &raid5_personality)
8386 return ERR_PTR(-EINVAL);
8387 if (mddev->degraded > 1)
8388 return ERR_PTR(-EINVAL);
8389 if (mddev->raid_disks > 253)
8390 return ERR_PTR(-EINVAL);
8391 if (mddev->raid_disks < 3)
8392 return ERR_PTR(-EINVAL);
8393
8394 switch (mddev->layout) {
8395 case ALGORITHM_LEFT_ASYMMETRIC:
8396 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
8397 break;
8398 case ALGORITHM_RIGHT_ASYMMETRIC:
8399 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
8400 break;
8401 case ALGORITHM_LEFT_SYMMETRIC:
8402 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
8403 break;
8404 case ALGORITHM_RIGHT_SYMMETRIC:
8405 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
8406 break;
8407 case ALGORITHM_PARITY_0:
8408 new_layout = ALGORITHM_PARITY_0_6;
8409 break;
8410 case ALGORITHM_PARITY_N:
8411 new_layout = ALGORITHM_PARITY_N;
8412 break;
8413 default:
8414 return ERR_PTR(-EINVAL);
8415 }
8416 mddev->new_level = 6;
8417 mddev->new_layout = new_layout;
8418 mddev->delta_disks = 1;
8419 mddev->raid_disks += 1;
8420 return setup_conf(mddev);
8421}
8422
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01008423static int raid5_change_consistency_policy(struct mddev *mddev, const char *buf)
8424{
8425 struct r5conf *conf;
8426 int err;
8427
8428 err = mddev_lock(mddev);
8429 if (err)
8430 return err;
8431 conf = mddev->private;
8432 if (!conf) {
8433 mddev_unlock(mddev);
8434 return -ENODEV;
8435 }
8436
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02008437 if (strncmp(buf, "ppl", 3) == 0) {
Song Liu0bb0c102017-03-27 10:51:33 -07008438 /* ppl only works with RAID 5 */
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02008439 if (!raid5_has_ppl(conf) && conf->level == 5) {
8440 err = log_init(conf, NULL, true);
8441 if (!err) {
8442 err = resize_stripes(conf, conf->pool_size);
8443 if (err)
8444 log_exit(conf);
8445 }
Song Liu0bb0c102017-03-27 10:51:33 -07008446 } else
8447 err = -EINVAL;
8448 } else if (strncmp(buf, "resync", 6) == 0) {
8449 if (raid5_has_ppl(conf)) {
8450 mddev_suspend(mddev);
8451 log_exit(conf);
Song Liu0bb0c102017-03-27 10:51:33 -07008452 mddev_resume(mddev);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02008453 err = resize_stripes(conf, conf->pool_size);
Song Liu0bb0c102017-03-27 10:51:33 -07008454 } else if (test_bit(MD_HAS_JOURNAL, &conf->mddev->flags) &&
8455 r5l_log_disk_error(conf)) {
8456 bool journal_dev_exists = false;
8457 struct md_rdev *rdev;
8458
8459 rdev_for_each(rdev, mddev)
8460 if (test_bit(Journal, &rdev->flags)) {
8461 journal_dev_exists = true;
8462 break;
8463 }
8464
8465 if (!journal_dev_exists) {
8466 mddev_suspend(mddev);
8467 clear_bit(MD_HAS_JOURNAL, &mddev->flags);
8468 mddev_resume(mddev);
8469 } else /* need remove journal device first */
8470 err = -EBUSY;
8471 } else
8472 err = -EINVAL;
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01008473 } else {
8474 err = -EINVAL;
8475 }
8476
8477 if (!err)
8478 md_update_sb(mddev, 1);
8479
8480 mddev_unlock(mddev);
8481
8482 return err;
8483}
8484
Song Liud5d885f2017-11-19 22:17:01 -08008485static int raid5_start(struct mddev *mddev)
8486{
8487 struct r5conf *conf = mddev->private;
8488
8489 return r5l_start(conf->log);
8490}
8491
NeilBrown84fc4b52011-10-11 16:49:58 +11008492static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07008493{
8494 .name = "raid6",
8495 .level = 6,
8496 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008497 .make_request = raid5_make_request,
8498 .run = raid5_run,
Song Liud5d885f2017-11-19 22:17:01 -08008499 .start = raid5_start,
NeilBrownafa0f552014-12-15 12:56:58 +11008500 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008501 .status = raid5_status,
8502 .error_handler = raid5_error,
NeilBrown16a53ec2006-06-26 00:27:38 -07008503 .hot_add_disk = raid5_add_disk,
8504 .hot_remove_disk= raid5_remove_disk,
8505 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008506 .sync_request = raid5_sync_request,
NeilBrown16a53ec2006-06-26 00:27:38 -07008507 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008508 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10008509 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08008510 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008511 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07008512 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11008513 .takeover = raid6_takeover,
Song Liu0bb0c102017-03-27 10:51:33 -07008514 .change_consistency_policy = raid5_change_consistency_policy,
NeilBrown16a53ec2006-06-26 00:27:38 -07008515};
NeilBrown84fc4b52011-10-11 16:49:58 +11008516static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008517{
8518 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08008519 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008520 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008521 .make_request = raid5_make_request,
8522 .run = raid5_run,
Song Liud5d885f2017-11-19 22:17:01 -08008523 .start = raid5_start,
NeilBrownafa0f552014-12-15 12:56:58 +11008524 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008525 .status = raid5_status,
8526 .error_handler = raid5_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008527 .hot_add_disk = raid5_add_disk,
8528 .hot_remove_disk= raid5_remove_disk,
8529 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008530 .sync_request = raid5_sync_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008532 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08008533 .check_reshape = raid5_check_reshape,
8534 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008535 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07008536 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11008537 .takeover = raid5_takeover,
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01008538 .change_consistency_policy = raid5_change_consistency_policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008539};
8540
NeilBrown84fc4b52011-10-11 16:49:58 +11008541static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008542{
NeilBrown2604b702006-01-06 00:20:36 -08008543 .name = "raid4",
8544 .level = 4,
8545 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008546 .make_request = raid5_make_request,
8547 .run = raid5_run,
Song Liud5d885f2017-11-19 22:17:01 -08008548 .start = raid5_start,
NeilBrownafa0f552014-12-15 12:56:58 +11008549 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008550 .status = raid5_status,
8551 .error_handler = raid5_error,
NeilBrown2604b702006-01-06 00:20:36 -08008552 .hot_add_disk = raid5_add_disk,
8553 .hot_remove_disk= raid5_remove_disk,
8554 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008555 .sync_request = raid5_sync_request,
NeilBrown2604b702006-01-06 00:20:36 -08008556 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008557 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08008558 .check_reshape = raid5_check_reshape,
8559 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008560 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08008561 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11008562 .takeover = raid4_takeover,
Song Liu0bb0c102017-03-27 10:51:33 -07008563 .change_consistency_policy = raid5_change_consistency_policy,
NeilBrown2604b702006-01-06 00:20:36 -08008564};
8565
8566static int __init raid5_init(void)
8567{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008568 int ret;
8569
Shaohua Li851c30c2013-08-28 14:30:16 +08008570 raid5_wq = alloc_workqueue("raid5wq",
8571 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
8572 if (!raid5_wq)
8573 return -ENOMEM;
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008574
8575 ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
8576 "md/raid5:prepare",
8577 raid456_cpu_up_prepare,
8578 raid456_cpu_dead);
8579 if (ret) {
8580 destroy_workqueue(raid5_wq);
8581 return ret;
8582 }
NeilBrown16a53ec2006-06-26 00:27:38 -07008583 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008584 register_md_personality(&raid5_personality);
8585 register_md_personality(&raid4_personality);
8586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008587}
8588
NeilBrown2604b702006-01-06 00:20:36 -08008589static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008590{
NeilBrown16a53ec2006-06-26 00:27:38 -07008591 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008592 unregister_md_personality(&raid5_personality);
8593 unregister_md_personality(&raid4_personality);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008594 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
Shaohua Li851c30c2013-08-28 14:30:16 +08008595 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008596}
8597
8598module_init(raid5_init);
8599module_exit(raid5_exit);
8600MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008601MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008602MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08008603MODULE_ALIAS("md-raid5");
8604MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08008605MODULE_ALIAS("md-level-5");
8606MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07008607MODULE_ALIAS("md-personality-8"); /* RAID6 */
8608MODULE_ALIAS("md-raid6");
8609MODULE_ALIAS("md-level-6");
8610
8611/* This used to be two separate modules, they were: */
8612MODULE_ALIAS("raid5");
8613MODULE_ALIAS("raid6");