blob: 5a05277f4be728eee40e7d431434a9ff26023c9e [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
Yufen Yu046169f2020-08-20 09:22:12 -0400451#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
452static void free_stripe_pages(struct stripe_head *sh)
453{
454 int i;
455 struct page *p;
456
457 /* Have not allocate page pool */
458 if (!sh->pages)
459 return;
460
461 for (i = 0; i < sh->nr_pages; i++) {
462 p = sh->pages[i];
463 if (p)
464 put_page(p);
465 sh->pages[i] = NULL;
466 }
467}
468
469static int alloc_stripe_pages(struct stripe_head *sh, gfp_t gfp)
470{
471 int i;
472 struct page *p;
473
474 for (i = 0; i < sh->nr_pages; i++) {
475 /* The page have allocated. */
476 if (sh->pages[i])
477 continue;
478
479 p = alloc_page(gfp);
480 if (!p) {
481 free_stripe_pages(sh);
482 return -ENOMEM;
483 }
484 sh->pages[i] = p;
485 }
486 return 0;
487}
488
489static int
490init_stripe_shared_pages(struct stripe_head *sh, struct r5conf *conf, int disks)
491{
492 int nr_pages, cnt;
493
494 if (sh->pages)
495 return 0;
496
497 /* Each of the sh->dev[i] need one conf->stripe_size */
498 cnt = PAGE_SIZE / conf->stripe_size;
499 nr_pages = (disks + cnt - 1) / cnt;
500
501 sh->pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
502 if (!sh->pages)
503 return -ENOMEM;
504 sh->nr_pages = nr_pages;
505 sh->stripes_per_page = cnt;
506 return 0;
507}
508#endif
509
NeilBrowne4e11e32010-06-16 16:45:16 +1000510static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000513 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Yufen Yu046169f2020-08-20 09:22:12 -0400515#if PAGE_SIZE == DEFAULT_STRIPE_SIZE
NeilBrowne4e11e32010-06-16 16:45:16 +1000516 for (i = 0; i < num ; i++) {
Yufen Yu046169f2020-08-20 09:22:12 -0400517 struct page *p;
518
Shaohua Lid592a992014-05-21 17:57:44 +0800519 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 p = sh->dev[i].page;
521 if (!p)
522 continue;
523 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800524 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
Yufen Yu046169f2020-08-20 09:22:12 -0400526#else
527 for (i = 0; i < num; i++)
528 sh->dev[i].page = NULL;
529 free_stripe_pages(sh); /* Free pages */
530#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
NeilBrowna9683a72015-02-25 12:02:51 +1100533static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000536 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Yufen Yu046169f2020-08-20 09:22:12 -0400538#if PAGE_SIZE == DEFAULT_STRIPE_SIZE
NeilBrowne4e11e32010-06-16 16:45:16 +1000539 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct page *page;
541
NeilBrowna9683a72015-02-25 12:02:51 +1100542 if (!(page = alloc_page(gfp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return 1;
544 }
545 sh->dev[i].page = page;
Shaohua Lid592a992014-05-21 17:57:44 +0800546 sh->dev[i].orig_page = page;
Yufen Yu7aba13b2020-08-20 09:22:06 -0400547 sh->dev[i].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
Yufen Yu046169f2020-08-20 09:22:12 -0400549#else
550 if (alloc_stripe_pages(sh, gfp))
551 return -ENOMEM;
Artur Paszkiewicz3418d032017-03-09 09:59:59 +0100552
Yufen Yu046169f2020-08-20 09:22:12 -0400553 for (i = 0; i < num; i++) {
554 sh->dev[i].page = raid5_get_dev_page(sh, i);
555 sh->dev[i].orig_page = sh->dev[i].page;
556 sh->dev[i].offset = raid5_get_page_offset(sh, i);
557 }
558#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return 0;
560}
561
NeilBrownd1688a62011-10-11 16:49:52 +1100562static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100563 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
NeilBrownb5663ba2009-03-31 14:39:38 +1100565static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
NeilBrownd1688a62011-10-11 16:49:52 +1100567 struct r5conf *conf = sh->raid_conf;
Shaohua Li566c09c2013-11-14 15:16:17 +1100568 int i, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200570 BUG_ON(atomic_read(&sh->count) != 0);
571 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000572 BUG_ON(stripe_operations_active(sh));
shli@kernel.org59fc6302014-12-15 12:57:03 +1100573 BUG_ON(sh->batch_head);
Dan Williamsd84e0f12007-01-02 13:52:30 -0700574
Dan Williams45b42332007-07-09 11:56:43 -0700575 pr_debug("init_stripe called, stripe %llu\n",
Markus Stockhausenb8e6a152014-08-23 20:19:27 +1000576 (unsigned long long)sector);
Shaohua Li566c09c2013-11-14 15:16:17 +1100577retry:
578 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100579 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100580 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100582 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 sh->state = 0;
584
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800585 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 struct r5dev *dev = &sh->dev[i];
587
Dan Williamsd84e0f12007-01-02 13:52:30 -0700588 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 test_bit(R5_LOCKED, &dev->flags)) {
NeilBrowncc6167b2016-11-02 14:16:50 +1100590 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700592 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000594 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596 dev->flags = 0;
Guoqing Jiang27a4ff82017-08-10 16:12:17 +0800597 dev->sector = raid5_compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100599 if (read_seqcount_retry(&conf->gen_lock, seq))
600 goto retry;
shli@kernel.org7a87f432014-12-15 12:57:03 +1100601 sh->overwrite_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800603 sh->cpu = smp_processor_id();
shli@kernel.orgda41ba62014-12-15 12:57:03 +1100604 set_bit(STRIPE_BATCH_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
NeilBrownd1688a62011-10-11 16:49:52 +1100607static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100608 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 struct stripe_head *sh;
611
Dan Williams45b42332007-07-09 11:56:43 -0700612 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800613 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100614 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700616 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return NULL;
618}
619
NeilBrown674806d2010-06-16 17:17:53 +1000620/*
621 * Need to check if array has failed when deciding whether to:
622 * - start an array
623 * - remove non-faulty devices
624 * - add a spare
625 * - allow a reshape
626 * This determination is simple when no reshape is happening.
627 * However if there is a reshape, we need to carefully check
628 * both the before and after sections.
629 * This is because some failed devices may only affect one
630 * of the two sections, and some non-in_sync devices may
631 * be insync in the section most affected by failed devices.
632 */
Song Liu2e38a372017-01-24 10:45:30 -0800633int raid5_calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000634{
NeilBrown908f4fb2011-12-23 10:17:50 +1100635 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000636 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000637
638 rcu_read_lock();
639 degraded = 0;
640 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100641 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000642 if (rdev && test_bit(Faulty, &rdev->flags))
643 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000644 if (!rdev || test_bit(Faulty, &rdev->flags))
645 degraded++;
646 else if (test_bit(In_sync, &rdev->flags))
647 ;
648 else
649 /* not in-sync or faulty.
650 * If the reshape increases the number of devices,
651 * this is being recovered by the reshape, so
652 * this 'previous' section is not in_sync.
653 * If the number of devices is being reduced however,
654 * the device can only be part of the array if
655 * we are reverting a reshape, so this section will
656 * be in-sync.
657 */
658 if (conf->raid_disks >= conf->previous_raid_disks)
659 degraded++;
660 }
661 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100662 if (conf->raid_disks == conf->previous_raid_disks)
663 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000664 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100665 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000666 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100667 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000668 if (rdev && test_bit(Faulty, &rdev->flags))
669 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000670 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100671 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000672 else if (test_bit(In_sync, &rdev->flags))
673 ;
674 else
675 /* not in-sync or faulty.
676 * If reshape increases the number of devices, this
677 * section has already been recovered, else it
678 * almost certainly hasn't.
679 */
680 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100681 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000682 }
683 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100684 if (degraded2 > degraded)
685 return degraded2;
686 return degraded;
687}
688
689static int has_failed(struct r5conf *conf)
690{
691 int degraded;
692
693 if (conf->mddev->reshape_position == MaxSector)
694 return conf->mddev->degraded > conf->max_degraded;
695
Song Liu2e38a372017-01-24 10:45:30 -0800696 degraded = raid5_calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000697 if (degraded > conf->max_degraded)
698 return 1;
699 return 0;
700}
701
Shaohua Li6d036f72015-08-13 14:31:57 -0700702struct stripe_head *
703raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
704 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 struct stripe_head *sh;
Yufen Yuc911c462020-07-18 05:29:07 -0400707 int hash = stripe_hash_locks_hash(conf, sector);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800708 int inc_empty_inactive_list_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Dan Williams45b42332007-07-09 11:56:43 -0700710 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Shaohua Li566c09c2013-11-14 15:16:17 +1100712 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 do {
Yuanhan Liub1b46482015-05-08 18:19:06 +1000715 wait_event_lock_irq(conf->wait_for_quiescent,
NeilBrowna8c906c2009-06-09 14:39:59 +1000716 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100717 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100718 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (!sh) {
NeilBrownedbe83a2015-02-26 12:47:56 +1100720 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100721 sh = get_free_stripe(conf, hash);
Shaohua Li713bc5c2015-05-28 17:33:47 -0700722 if (!sh && !test_bit(R5_DID_ALLOC,
723 &conf->cache_state))
NeilBrownedbe83a2015-02-26 12:47:56 +1100724 set_bit(R5_ALLOC_MORE,
725 &conf->cache_state);
726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (noblock && sh == NULL)
728 break;
Song Liua39f7af2016-11-17 15:24:40 -0800729
730 r5c_check_stripe_cache_usage(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (!sh) {
NeilBrown54233992015-02-26 12:21:04 +1100732 set_bit(R5_INACTIVE_BLOCKED,
733 &conf->cache_state);
Song Liua39f7af2016-11-17 15:24:40 -0800734 r5l_wake_reclaim(conf->log, 0);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800735 wait_event_lock_irq(
736 conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +1100737 !list_empty(conf->inactive_list + hash) &&
738 (atomic_read(&conf->active_stripes)
739 < (conf->max_nr_stripes * 3 / 4)
NeilBrown54233992015-02-26 12:21:04 +1100740 || !test_bit(R5_INACTIVE_BLOCKED,
741 &conf->cache_state)),
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800742 *(conf->hash_locks + hash));
NeilBrown54233992015-02-26 12:21:04 +1100743 clear_bit(R5_INACTIVE_BLOCKED,
744 &conf->cache_state);
NeilBrown7da9d452014-01-22 11:45:03 +1100745 } else {
NeilBrownb5663ba2009-03-31 14:39:38 +1100746 init_stripe(sh, sector, previous);
NeilBrown7da9d452014-01-22 11:45:03 +1100747 atomic_inc(&sh->count);
748 }
Shaohua Lie240c182014-04-09 11:27:42 +0800749 } else if (!atomic_inc_not_zero(&sh->count)) {
NeilBrown6d183de2013-11-28 10:55:27 +1100750 spin_lock(&conf->device_lock);
Shaohua Lie240c182014-04-09 11:27:42 +0800751 if (!atomic_read(&sh->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (!test_bit(STRIPE_HANDLE, &sh->state))
753 atomic_inc(&conf->active_stripes);
NeilBrown5af9bef2014-01-14 15:16:10 +1100754 BUG_ON(list_empty(&sh->lru) &&
755 !test_bit(STRIPE_EXPANDING, &sh->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800756 inc_empty_inactive_list_flag = 0;
757 if (!list_empty(conf->inactive_list + hash))
758 inc_empty_inactive_list_flag = 1;
NeilBrown16a53ec2006-06-26 00:27:38 -0700759 list_del_init(&sh->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800760 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
761 atomic_inc(&conf->empty_inactive_list_nr);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800762 if (sh->group) {
763 sh->group->stripes_cnt--;
764 sh->group = NULL;
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
NeilBrown7da9d452014-01-22 11:45:03 +1100767 atomic_inc(&sh->count);
NeilBrown6d183de2013-11-28 10:55:27 +1100768 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770 } while (sh == NULL);
771
Shaohua Li566c09c2013-11-14 15:16:17 +1100772 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 return sh;
774}
775
shli@kernel.org7a87f432014-12-15 12:57:03 +1100776static bool is_full_stripe_write(struct stripe_head *sh)
777{
778 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
779 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
780}
781
shli@kernel.org59fc6302014-12-15 12:57:03 +1100782static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
Christoph Hellwig368ecad2019-04-04 18:56:15 +0200783 __acquires(&sh1->stripe_lock)
784 __acquires(&sh2->stripe_lock)
shli@kernel.org59fc6302014-12-15 12:57:03 +1100785{
shli@kernel.org59fc6302014-12-15 12:57:03 +1100786 if (sh1 > sh2) {
Julia Cartwright3d05f3a2017-04-28 12:41:02 -0500787 spin_lock_irq(&sh2->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100788 spin_lock_nested(&sh1->stripe_lock, 1);
789 } else {
Julia Cartwright3d05f3a2017-04-28 12:41:02 -0500790 spin_lock_irq(&sh1->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100791 spin_lock_nested(&sh2->stripe_lock, 1);
792 }
793}
794
795static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
Christoph Hellwig368ecad2019-04-04 18:56:15 +0200796 __releases(&sh1->stripe_lock)
797 __releases(&sh2->stripe_lock)
shli@kernel.org59fc6302014-12-15 12:57:03 +1100798{
799 spin_unlock(&sh1->stripe_lock);
Julia Cartwright3d05f3a2017-04-28 12:41:02 -0500800 spin_unlock_irq(&sh2->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100801}
802
803/* Only freshly new full stripe normal write stripe can be added to a batch list */
804static bool stripe_can_batch(struct stripe_head *sh)
805{
Shaohua Li9c3e3332015-08-13 14:32:02 -0700806 struct r5conf *conf = sh->raid_conf;
807
Shaohua Lie254de62018-08-29 11:05:42 -0700808 if (raid5_has_log(conf) || raid5_has_ppl(conf))
Shaohua Li9c3e3332015-08-13 14:32:02 -0700809 return false;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100810 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
NeilBrownd0852df52015-05-27 08:43:45 +1000811 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
shli@kernel.org59fc6302014-12-15 12:57:03 +1100812 is_full_stripe_write(sh);
813}
814
815/* we only do back search */
816static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
817{
818 struct stripe_head *head;
819 sector_t head_sector, tmp_sec;
820 int hash;
821 int dd_idx;
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800822 int inc_empty_inactive_list_flag;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100823
shli@kernel.org59fc6302014-12-15 12:57:03 +1100824 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
825 tmp_sec = sh->sector;
826 if (!sector_div(tmp_sec, conf->chunk_sectors))
827 return;
Yufen Yuc911c462020-07-18 05:29:07 -0400828 head_sector = sh->sector - RAID5_STRIPE_SECTORS(conf);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100829
Yufen Yuc911c462020-07-18 05:29:07 -0400830 hash = stripe_hash_locks_hash(conf, head_sector);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100831 spin_lock_irq(conf->hash_locks + hash);
832 head = __find_stripe(conf, head_sector, conf->generation);
833 if (head && !atomic_inc_not_zero(&head->count)) {
834 spin_lock(&conf->device_lock);
835 if (!atomic_read(&head->count)) {
836 if (!test_bit(STRIPE_HANDLE, &head->state))
837 atomic_inc(&conf->active_stripes);
838 BUG_ON(list_empty(&head->lru) &&
839 !test_bit(STRIPE_EXPANDING, &head->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800840 inc_empty_inactive_list_flag = 0;
841 if (!list_empty(conf->inactive_list + hash))
842 inc_empty_inactive_list_flag = 1;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100843 list_del_init(&head->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800844 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
845 atomic_inc(&conf->empty_inactive_list_nr);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100846 if (head->group) {
847 head->group->stripes_cnt--;
848 head->group = NULL;
849 }
850 }
851 atomic_inc(&head->count);
852 spin_unlock(&conf->device_lock);
853 }
854 spin_unlock_irq(conf->hash_locks + hash);
855
856 if (!head)
857 return;
858 if (!stripe_can_batch(head))
859 goto out;
860
861 lock_two_stripes(head, sh);
862 /* clear_batch_ready clear the flag */
863 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
864 goto unlock_out;
865
866 if (sh->batch_head)
867 goto unlock_out;
868
869 dd_idx = 0;
870 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
871 dd_idx++;
Jens Axboe1eff9d32016-08-05 15:35:16 -0600872 if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
Mike Christie796a5cf2016-06-05 14:32:07 -0500873 bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
shli@kernel.org59fc6302014-12-15 12:57:03 +1100874 goto unlock_out;
875
876 if (head->batch_head) {
877 spin_lock(&head->batch_head->batch_lock);
878 /* This batch list is already running */
879 if (!stripe_can_batch(head)) {
880 spin_unlock(&head->batch_head->batch_lock);
881 goto unlock_out;
882 }
Shaohua Li36648472017-08-25 10:40:02 -0700883 /*
884 * We must assign batch_head of this stripe within the
885 * batch_lock, otherwise clear_batch_ready of batch head
886 * stripe could clear BATCH_READY bit of this stripe and
887 * this stripe->batch_head doesn't get assigned, which
888 * could confuse clear_batch_ready for this stripe
889 */
890 sh->batch_head = head->batch_head;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100891
892 /*
893 * at this point, head's BATCH_READY could be cleared, but we
894 * can still add the stripe to batch list
895 */
896 list_add(&sh->batch_list, &head->batch_list);
897 spin_unlock(&head->batch_head->batch_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100898 } else {
899 head->batch_head = head;
900 sh->batch_head = head->batch_head;
901 spin_lock(&head->batch_lock);
902 list_add_tail(&sh->batch_list, &head->batch_list);
903 spin_unlock(&head->batch_lock);
904 }
905
906 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
907 if (atomic_dec_return(&conf->preread_active_stripes)
908 < IO_THRESHOLD)
909 md_wakeup_thread(conf->mddev->thread);
910
NeilBrown2b6b2452015-05-21 15:10:01 +1000911 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
912 int seq = sh->bm_seq;
913 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
914 sh->batch_head->bm_seq > seq)
915 seq = sh->batch_head->bm_seq;
916 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
917 sh->batch_head->bm_seq = seq;
918 }
919
shli@kernel.org59fc6302014-12-15 12:57:03 +1100920 atomic_inc(&sh->count);
921unlock_out:
922 unlock_two_stripes(head, sh);
923out:
Shaohua Li6d036f72015-08-13 14:31:57 -0700924 raid5_release_stripe(head);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100925}
926
NeilBrown05616be2012-05-21 09:27:00 +1000927/* Determine if 'data_offset' or 'new_data_offset' should be used
928 * in this stripe_head.
929 */
930static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
931{
932 sector_t progress = conf->reshape_progress;
933 /* Need a memory barrier to make sure we see the value
934 * of conf->generation, or ->data_offset that was set before
935 * reshape_progress was updated.
936 */
937 smp_rmb();
938 if (progress == MaxSector)
939 return 0;
940 if (sh->generation == conf->generation - 1)
941 return 0;
942 /* We are in a reshape, and this is a new-generation stripe,
943 * so use new_data_offset.
944 */
945 return 1;
946}
947
Shaohua Liaaf9f122017-03-03 22:06:12 -0800948static void dispatch_bio_list(struct bio_list *tmp)
Shaohua Li765d7042017-01-04 09:33:23 -0800949{
Shaohua Li765d7042017-01-04 09:33:23 -0800950 struct bio *bio;
951
Shaohua Liaaf9f122017-03-03 22:06:12 -0800952 while ((bio = bio_list_pop(tmp)))
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200953 submit_bio_noacct(bio);
Shaohua Li765d7042017-01-04 09:33:23 -0800954}
955
Sami Tolvanen4f0f5862021-04-08 11:28:34 -0700956static int cmp_stripe(void *priv, const struct list_head *a,
957 const struct list_head *b)
Shaohua Li765d7042017-01-04 09:33:23 -0800958{
Shaohua Liaaf9f122017-03-03 22:06:12 -0800959 const struct r5pending_data *da = list_entry(a,
960 struct r5pending_data, sibling);
961 const struct r5pending_data *db = list_entry(b,
962 struct r5pending_data, sibling);
963 if (da->sector > db->sector)
964 return 1;
965 if (da->sector < db->sector)
966 return -1;
967 return 0;
968}
969
970static void dispatch_defer_bios(struct r5conf *conf, int target,
971 struct bio_list *list)
972{
973 struct r5pending_data *data;
974 struct list_head *first, *next = NULL;
975 int cnt = 0;
976
977 if (conf->pending_data_cnt == 0)
Shaohua Li765d7042017-01-04 09:33:23 -0800978 return;
Shaohua Liaaf9f122017-03-03 22:06:12 -0800979
980 list_sort(NULL, &conf->pending_list, cmp_stripe);
981
982 first = conf->pending_list.next;
983
984 /* temporarily move the head */
985 if (conf->next_pending_data)
986 list_move_tail(&conf->pending_list,
987 &conf->next_pending_data->sibling);
988
989 while (!list_empty(&conf->pending_list)) {
990 data = list_first_entry(&conf->pending_list,
991 struct r5pending_data, sibling);
992 if (&data->sibling == first)
993 first = data->sibling.next;
994 next = data->sibling.next;
995
996 bio_list_merge(list, &data->bios);
997 list_move(&data->sibling, &conf->free_list);
998 cnt++;
999 if (cnt >= target)
1000 break;
Shaohua Li765d7042017-01-04 09:33:23 -08001001 }
Shaohua Liaaf9f122017-03-03 22:06:12 -08001002 conf->pending_data_cnt -= cnt;
1003 BUG_ON(conf->pending_data_cnt < 0 || cnt < target);
1004
1005 if (next != &conf->pending_list)
1006 conf->next_pending_data = list_entry(next,
1007 struct r5pending_data, sibling);
1008 else
1009 conf->next_pending_data = NULL;
1010 /* list isn't empty */
1011 if (first != &conf->pending_list)
1012 list_move_tail(&conf->pending_list, first);
1013}
1014
1015static void flush_deferred_bios(struct r5conf *conf)
1016{
1017 struct bio_list tmp = BIO_EMPTY_LIST;
1018
1019 if (conf->pending_data_cnt == 0)
1020 return;
1021
Shaohua Li765d7042017-01-04 09:33:23 -08001022 spin_lock(&conf->pending_bios_lock);
Shaohua Liaaf9f122017-03-03 22:06:12 -08001023 dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp);
1024 BUG_ON(conf->pending_data_cnt != 0);
Shaohua Li765d7042017-01-04 09:33:23 -08001025 spin_unlock(&conf->pending_bios_lock);
Shaohua Liaaf9f122017-03-03 22:06:12 -08001026
1027 dispatch_bio_list(&tmp);
1028}
1029
1030static void defer_issue_bios(struct r5conf *conf, sector_t sector,
1031 struct bio_list *bios)
1032{
1033 struct bio_list tmp = BIO_EMPTY_LIST;
1034 struct r5pending_data *ent;
1035
1036 spin_lock(&conf->pending_bios_lock);
1037 ent = list_first_entry(&conf->free_list, struct r5pending_data,
1038 sibling);
1039 list_move_tail(&ent->sibling, &conf->pending_list);
1040 ent->sector = sector;
1041 bio_list_init(&ent->bios);
1042 bio_list_merge(&ent->bios, bios);
1043 conf->pending_data_cnt++;
1044 if (conf->pending_data_cnt >= PENDING_IO_MAX)
1045 dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp);
1046
1047 spin_unlock(&conf->pending_bios_lock);
1048
1049 dispatch_bio_list(&tmp);
Shaohua Li765d7042017-01-04 09:33:23 -08001050}
1051
NeilBrown6712ecf2007-09-27 12:47:43 +02001052static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001053raid5_end_read_request(struct bio *bi);
NeilBrown6712ecf2007-09-27 12:47:43 +02001054static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001055raid5_end_write_request(struct bio *bi);
Dan Williams91c00922007-01-02 13:52:30 -07001056
Dan Williamsc4e5ac02008-06-28 08:31:53 +10001057static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -07001058{
NeilBrownd1688a62011-10-11 16:49:52 +11001059 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001060 int i, disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001061 struct stripe_head *head_sh = sh;
Shaohua Liaaf9f122017-03-03 22:06:12 -08001062 struct bio_list pending_bios = BIO_EMPTY_LIST;
1063 bool should_defer;
Dan Williams91c00922007-01-02 13:52:30 -07001064
1065 might_sleep();
1066
Artur Paszkiewiczff875732017-03-09 09:59:58 +01001067 if (log_stripe(sh, s) == 0)
1068 return;
Song Liu1e6d6902016-11-17 15:24:39 -08001069
Shaohua Liaaf9f122017-03-03 22:06:12 -08001070 should_defer = conf->batch_bio_dispatch && conf->group_cnt;
Shaohua Lif6bed0e2015-08-13 14:31:59 -07001071
Dan Williams91c00922007-01-02 13:52:30 -07001072 for (i = disks; i--; ) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001073 int op, op_flags = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11001074 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +11001075 struct bio *bi, *rbi;
1076 struct md_rdev *rdev, *rrdev = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001077
1078 sh = head_sh;
Tejun Heoe9c74692010-09-03 11:56:18 +02001079 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001080 op = REQ_OP_WRITE;
Tejun Heoe9c74692010-09-03 11:56:18 +02001081 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001082 op_flags = REQ_FUA;
Shaohua Li9e4447682012-10-11 13:49:49 +11001083 if (test_bit(R5_Discard, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001084 op = REQ_OP_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +02001085 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001086 op = REQ_OP_READ;
NeilBrown9a3e1102011-12-23 10:17:53 +11001087 else if (test_and_clear_bit(R5_WantReplace,
1088 &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001089 op = REQ_OP_WRITE;
NeilBrown9a3e1102011-12-23 10:17:53 +11001090 replace_only = 1;
1091 } else
Dan Williams91c00922007-01-02 13:52:30 -07001092 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +10001093 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001094 op_flags |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -07001095
shli@kernel.org59fc6302014-12-15 12:57:03 +11001096again:
Dan Williams91c00922007-01-02 13:52:30 -07001097 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +11001098 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -07001099
Dan Williams91c00922007-01-02 13:52:30 -07001100 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +11001101 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +11001102 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
1103 rdev = rcu_dereference(conf->disks[i].rdev);
1104 if (!rdev) {
1105 rdev = rrdev;
1106 rrdev = NULL;
1107 }
Mike Christie796a5cf2016-06-05 14:32:07 -05001108 if (op_is_write(op)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001109 if (replace_only)
1110 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +11001111 if (rdev == rrdev)
1112 /* We raced and saw duplicates */
1113 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +11001114 } else {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001115 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +11001116 rdev = rrdev;
1117 rrdev = NULL;
1118 }
NeilBrown977df362011-12-23 10:17:53 +11001119
Dan Williams91c00922007-01-02 13:52:30 -07001120 if (rdev && test_bit(Faulty, &rdev->flags))
1121 rdev = NULL;
1122 if (rdev)
1123 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +11001124 if (rrdev && test_bit(Faulty, &rrdev->flags))
1125 rrdev = NULL;
1126 if (rrdev)
1127 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -07001128 rcu_read_unlock();
1129
NeilBrown73e92e52011-07-28 11:39:22 +10001130 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +11001131 * need to check for writes. We never accept write errors
1132 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +10001133 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001134 while (op_is_write(op) && rdev &&
NeilBrown73e92e52011-07-28 11:39:22 +10001135 test_bit(WriteErrorSeen, &rdev->flags)) {
1136 sector_t first_bad;
1137 int bad_sectors;
Yufen Yuc911c462020-07-18 05:29:07 -04001138 int bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
NeilBrown73e92e52011-07-28 11:39:22 +10001139 &first_bad, &bad_sectors);
1140 if (!bad)
1141 break;
1142
1143 if (bad < 0) {
1144 set_bit(BlockedBadBlocks, &rdev->flags);
1145 if (!conf->mddev->external &&
Shaohua Li29530792016-12-08 15:48:19 -08001146 conf->mddev->sb_flags) {
NeilBrown73e92e52011-07-28 11:39:22 +10001147 /* It is very unlikely, but we might
1148 * still need to write out the
1149 * bad block log - better give it
1150 * a chance*/
1151 md_check_recovery(conf->mddev);
1152 }
majianpeng18507532012-07-03 12:11:54 +10001153 /*
1154 * Because md_wait_for_blocked_rdev
1155 * will dec nr_pending, we must
1156 * increment it first.
1157 */
1158 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +10001159 md_wait_for_blocked_rdev(rdev, conf->mddev);
1160 } else {
1161 /* Acknowledged bad block - skip the write */
1162 rdev_dec_pending(rdev, conf->mddev);
1163 rdev = NULL;
1164 }
1165 }
1166
Dan Williams91c00922007-01-02 13:52:30 -07001167 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001168 if (s->syncing || s->expanding || s->expanded
1169 || s->replacing)
Yufen Yuc911c462020-07-18 05:29:07 -04001170 md_sync_acct(rdev->bdev, RAID5_STRIPE_SECTORS(conf));
Dan Williams91c00922007-01-02 13:52:30 -07001171
Dan Williams2b7497f2008-06-28 08:31:52 +10001172 set_bit(STRIPE_IO_STARTED, &sh->state);
1173
Christoph Hellwig74d46992017-08-23 19:10:32 +02001174 bio_set_dev(bi, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -05001175 bio_set_op_attrs(bi, op, op_flags);
1176 bi->bi_end_io = op_is_write(op)
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001177 ? raid5_end_write_request
1178 : raid5_end_read_request;
1179 bi->bi_private = sh;
1180
Mike Christie6296b962016-06-05 14:32:21 -05001181 pr_debug("%s: for %llu schedule op %d on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001182 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001183 bi->bi_opf, i);
Dan Williams91c00922007-01-02 13:52:30 -07001184 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001185 if (sh != head_sh)
1186 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001187 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001188 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001189 + rdev->new_data_offset);
1190 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001191 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001192 + rdev->data_offset);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001193 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
Jens Axboe1eff9d32016-08-05 15:35:16 -06001194 bi->bi_opf |= REQ_NOMERGE;
majianpeng3f9e7c12012-07-31 10:04:21 +10001195
Shaohua Lid592a992014-05-21 17:57:44 +08001196 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1197 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
Song Liu86aa1392017-01-12 17:22:41 -08001198
1199 if (!op_is_write(op) &&
1200 test_bit(R5_InJournal, &sh->dev[i].flags))
1201 /*
1202 * issuing read for a page in journal, this
1203 * must be preparing for prexor in rmw; read
1204 * the data into orig_page
1205 */
1206 sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
1207 else
1208 sh->dev[i].vec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001209 bi->bi_vcnt = 1;
Yufen Yuc911c462020-07-18 05:29:07 -04001210 bi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf);
Yufen Yu7aba13b2020-08-20 09:22:06 -04001211 bi->bi_io_vec[0].bv_offset = sh->dev[i].offset;
Yufen Yuc911c462020-07-18 05:29:07 -04001212 bi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf);
Mariusz Dabrowski2cd259a2018-04-19 19:28:10 +02001213 bi->bi_write_hint = sh->dev[i].write_hint;
1214 if (!rrdev)
Eugene Syromiatnikovf1934892019-09-20 17:58:28 +02001215 sh->dev[i].write_hint = RWH_WRITE_LIFE_NOT_SET;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001216 /*
1217 * If this is discard request, set bi_vcnt 0. We don't
1218 * want to confuse SCSI because SCSI will replace payload
1219 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001220 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001221 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +11001222 if (rrdev)
1223 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001224
1225 if (conf->mddev->gendisk)
Christoph Hellwig1c02fca2020-12-03 17:21:38 +01001226 trace_block_bio_remap(bi,
1227 disk_devt(conf->mddev->gendisk),
1228 sh->dev[i].sector);
Shaohua Liaaf9f122017-03-03 22:06:12 -08001229 if (should_defer && op_is_write(op))
1230 bio_list_add(&pending_bios, bi);
1231 else
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001232 submit_bio_noacct(bi);
NeilBrown977df362011-12-23 10:17:53 +11001233 }
1234 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001235 if (s->syncing || s->expanding || s->expanded
1236 || s->replacing)
Yufen Yuc911c462020-07-18 05:29:07 -04001237 md_sync_acct(rrdev->bdev, RAID5_STRIPE_SECTORS(conf));
NeilBrown977df362011-12-23 10:17:53 +11001238
1239 set_bit(STRIPE_IO_STARTED, &sh->state);
1240
Christoph Hellwig74d46992017-08-23 19:10:32 +02001241 bio_set_dev(rbi, rrdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -05001242 bio_set_op_attrs(rbi, op, op_flags);
1243 BUG_ON(!op_is_write(op));
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001244 rbi->bi_end_io = raid5_end_write_request;
1245 rbi->bi_private = sh;
1246
Mike Christie6296b962016-06-05 14:32:21 -05001247 pr_debug("%s: for %llu schedule op %d on "
NeilBrown977df362011-12-23 10:17:53 +11001248 "replacement disc %d\n",
1249 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001250 rbi->bi_opf, i);
NeilBrown977df362011-12-23 10:17:53 +11001251 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001252 if (sh != head_sh)
1253 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001254 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001255 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001256 + rrdev->new_data_offset);
1257 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001258 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001259 + rrdev->data_offset);
Shaohua Lid592a992014-05-21 17:57:44 +08001260 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1261 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1262 sh->dev[i].rvec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001263 rbi->bi_vcnt = 1;
Yufen Yuc911c462020-07-18 05:29:07 -04001264 rbi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf);
Yufen Yu7aba13b2020-08-20 09:22:06 -04001265 rbi->bi_io_vec[0].bv_offset = sh->dev[i].offset;
Yufen Yuc911c462020-07-18 05:29:07 -04001266 rbi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf);
Mariusz Dabrowski2cd259a2018-04-19 19:28:10 +02001267 rbi->bi_write_hint = sh->dev[i].write_hint;
Eugene Syromiatnikovf1934892019-09-20 17:58:28 +02001268 sh->dev[i].write_hint = RWH_WRITE_LIFE_NOT_SET;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001269 /*
1270 * If this is discard request, set bi_vcnt 0. We don't
1271 * want to confuse SCSI because SCSI will replace payload
1272 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001273 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001274 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001275 if (conf->mddev->gendisk)
Christoph Hellwig1c02fca2020-12-03 17:21:38 +01001276 trace_block_bio_remap(rbi,
1277 disk_devt(conf->mddev->gendisk),
1278 sh->dev[i].sector);
Shaohua Liaaf9f122017-03-03 22:06:12 -08001279 if (should_defer && op_is_write(op))
1280 bio_list_add(&pending_bios, rbi);
1281 else
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001282 submit_bio_noacct(rbi);
NeilBrown977df362011-12-23 10:17:53 +11001283 }
1284 if (!rdev && !rrdev) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001285 if (op_is_write(op))
Dan Williams91c00922007-01-02 13:52:30 -07001286 set_bit(STRIPE_DEGRADED, &sh->state);
Mike Christie6296b962016-06-05 14:32:21 -05001287 pr_debug("skip op %d on disc %d for sector %llu\n",
Jens Axboe1eff9d32016-08-05 15:35:16 -06001288 bi->bi_opf, i, (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001289 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1290 set_bit(STRIPE_HANDLE, &sh->state);
1291 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001292
1293 if (!head_sh->batch_head)
1294 continue;
1295 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1296 batch_list);
1297 if (sh != head_sh)
1298 goto again;
Dan Williams91c00922007-01-02 13:52:30 -07001299 }
Shaohua Liaaf9f122017-03-03 22:06:12 -08001300
1301 if (should_defer && !bio_list_empty(&pending_bios))
1302 defer_issue_bios(conf, head_sh->sector, &pending_bios);
Dan Williams91c00922007-01-02 13:52:30 -07001303}
1304
1305static struct dma_async_tx_descriptor *
Shaohua Lid592a992014-05-21 17:57:44 +08001306async_copy_data(int frombio, struct bio *bio, struct page **page,
Yufen Yu248728d2020-08-20 09:22:07 -04001307 unsigned int poff, sector_t sector, struct dma_async_tx_descriptor *tx,
Song Liu1e6d6902016-11-17 15:24:39 -08001308 struct stripe_head *sh, int no_skipcopy)
Dan Williams91c00922007-01-02 13:52:30 -07001309{
Kent Overstreet79886132013-11-23 17:19:00 -08001310 struct bio_vec bvl;
1311 struct bvec_iter iter;
Dan Williams91c00922007-01-02 13:52:30 -07001312 struct page *bio_page;
Dan Williams91c00922007-01-02 13:52:30 -07001313 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -07001314 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -07001315 enum async_tx_flags flags = 0;
Yufen Yuc911c462020-07-18 05:29:07 -04001316 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001317
Kent Overstreet4f024f32013-10-11 15:44:27 -07001318 if (bio->bi_iter.bi_sector >= sector)
1319 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
Dan Williams91c00922007-01-02 13:52:30 -07001320 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001321 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -07001322
Dan Williams0403e382009-09-08 17:42:50 -07001323 if (frombio)
1324 flags |= ASYNC_TX_FENCE;
1325 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1326
Kent Overstreet79886132013-11-23 17:19:00 -08001327 bio_for_each_segment(bvl, bio, iter) {
1328 int len = bvl.bv_len;
Dan Williams91c00922007-01-02 13:52:30 -07001329 int clen;
1330 int b_offset = 0;
1331
1332 if (page_offset < 0) {
1333 b_offset = -page_offset;
1334 page_offset += b_offset;
1335 len -= b_offset;
1336 }
1337
Yufen Yuc911c462020-07-18 05:29:07 -04001338 if (len > 0 && page_offset + len > RAID5_STRIPE_SIZE(conf))
1339 clen = RAID5_STRIPE_SIZE(conf) - page_offset;
Dan Williams91c00922007-01-02 13:52:30 -07001340 else
1341 clen = len;
1342
1343 if (clen > 0) {
Kent Overstreet79886132013-11-23 17:19:00 -08001344 b_offset += bvl.bv_offset;
1345 bio_page = bvl.bv_page;
Shaohua Lid592a992014-05-21 17:57:44 +08001346 if (frombio) {
Yufen Yuc911c462020-07-18 05:29:07 -04001347 if (conf->skip_copy &&
Shaohua Lid592a992014-05-21 17:57:44 +08001348 b_offset == 0 && page_offset == 0 &&
Yufen Yuc911c462020-07-18 05:29:07 -04001349 clen == RAID5_STRIPE_SIZE(conf) &&
Song Liu1e6d6902016-11-17 15:24:39 -08001350 !no_skipcopy)
Shaohua Lid592a992014-05-21 17:57:44 +08001351 *page = bio_page;
1352 else
Yufen Yu248728d2020-08-20 09:22:07 -04001353 tx = async_memcpy(*page, bio_page, page_offset + poff,
Dan Williamsa08abd82009-06-03 11:43:59 -07001354 b_offset, clen, &submit);
Shaohua Lid592a992014-05-21 17:57:44 +08001355 } else
1356 tx = async_memcpy(bio_page, *page, b_offset,
Yufen Yu248728d2020-08-20 09:22:07 -04001357 page_offset + poff, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001358 }
Dan Williamsa08abd82009-06-03 11:43:59 -07001359 /* chain the operations */
1360 submit.depend_tx = tx;
1361
Dan Williams91c00922007-01-02 13:52:30 -07001362 if (clen < len) /* hit end of page */
1363 break;
1364 page_offset += len;
1365 }
1366
1367 return tx;
1368}
1369
1370static void ops_complete_biofill(void *stripe_head_ref)
1371{
1372 struct stripe_head *sh = stripe_head_ref;
Dan Williamse4d84902007-09-24 10:06:13 -07001373 int i;
Yufen Yuc911c462020-07-18 05:29:07 -04001374 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001375
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001376 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001377 (unsigned long long)sh->sector);
1378
1379 /* clear completed biofills */
1380 for (i = sh->disks; i--; ) {
1381 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001382
1383 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001384 /* and check if we need to reply to a read request,
1385 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001386 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001387 */
1388 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001389 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001390
Dan Williams91c00922007-01-02 13:52:30 -07001391 BUG_ON(!dev->read);
1392 rbi = dev->read;
1393 dev->read = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001394 while (rbi && rbi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04001395 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
1396 rbi2 = r5_next_bio(conf, rbi, dev->sector);
NeilBrown016c76a2017-03-15 14:05:13 +11001397 bio_endio(rbi);
Dan Williams91c00922007-01-02 13:52:30 -07001398 rbi = rbi2;
1399 }
1400 }
1401 }
Dan Williams83de75c2008-06-28 08:31:58 +10001402 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001403
Dan Williamse4d84902007-09-24 10:06:13 -07001404 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001405 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001406}
1407
1408static void ops_run_biofill(struct stripe_head *sh)
1409{
1410 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001411 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001412 int i;
Yufen Yuc911c462020-07-18 05:29:07 -04001413 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001414
shli@kernel.org59fc6302014-12-15 12:57:03 +11001415 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001416 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001417 (unsigned long long)sh->sector);
1418
1419 for (i = sh->disks; i--; ) {
1420 struct r5dev *dev = &sh->dev[i];
1421 if (test_bit(R5_Wantfill, &dev->flags)) {
1422 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001423 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001424 dev->read = rbi = dev->toread;
1425 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001426 spin_unlock_irq(&sh->stripe_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001427 while (rbi && rbi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04001428 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
Shaohua Lid592a992014-05-21 17:57:44 +08001429 tx = async_copy_data(0, rbi, &dev->page,
Yufen Yu248728d2020-08-20 09:22:07 -04001430 dev->offset,
Song Liu1e6d6902016-11-17 15:24:39 -08001431 dev->sector, tx, sh, 0);
Yufen Yuc911c462020-07-18 05:29:07 -04001432 rbi = r5_next_bio(conf, rbi, dev->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001433 }
1434 }
1435 }
1436
1437 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001438 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1439 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001440}
1441
Dan Williams4e7d2c02009-08-29 19:13:11 -07001442static void mark_target_uptodate(struct stripe_head *sh, int target)
1443{
1444 struct r5dev *tgt;
1445
1446 if (target < 0)
1447 return;
1448
1449 tgt = &sh->dev[target];
1450 set_bit(R5_UPTODATE, &tgt->flags);
1451 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1452 clear_bit(R5_Wantcompute, &tgt->flags);
1453}
1454
Dan Williamsac6b53b2009-07-14 13:40:19 -07001455static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001456{
1457 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001458
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001459 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001460 (unsigned long long)sh->sector);
1461
Dan Williamsac6b53b2009-07-14 13:40:19 -07001462 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001463 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001464 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001465
Dan Williamsecc65c92008-06-28 08:31:57 +10001466 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1467 if (sh->check_state == check_state_compute_run)
1468 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001469 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001470 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001471}
1472
Dan Williamsd6f38f32009-07-14 11:50:52 -07001473/* return a pointer to the address conversion region of the scribble buffer */
Kent Overstreetb330e6a2019-03-11 23:31:06 -07001474static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
Dan Williams91c00922007-01-02 13:52:30 -07001475{
Kent Overstreetb330e6a2019-03-11 23:31:06 -07001476 return percpu->scribble + i * percpu->scribble_obj_size;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001477}
1478
1479/* return a pointer to the address conversion region of the scribble buffer */
Kent Overstreetb330e6a2019-03-11 23:31:06 -07001480static addr_conv_t *to_addr_conv(struct stripe_head *sh,
1481 struct raid5_percpu *percpu, int i)
shli@kernel.org46d5b782014-12-15 12:57:02 +11001482{
Kent Overstreetb330e6a2019-03-11 23:31:06 -07001483 return (void *) (to_addr_page(percpu, i) + sh->disks + 2);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001484}
1485
Yufen Yu7aba13b2020-08-20 09:22:06 -04001486/*
1487 * Return a pointer to record offset address.
1488 */
1489static unsigned int *
1490to_addr_offs(struct stripe_head *sh, struct raid5_percpu *percpu)
1491{
1492 return (unsigned int *) (to_addr_conv(sh, percpu, 0) + sh->disks + 2);
1493}
1494
Dan Williamsd6f38f32009-07-14 11:50:52 -07001495static struct dma_async_tx_descriptor *
1496ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1497{
Dan Williams91c00922007-01-02 13:52:30 -07001498 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001499 struct page **xor_srcs = to_addr_page(percpu, 0);
Yufen Yu7aba13b2020-08-20 09:22:06 -04001500 unsigned int *off_srcs = to_addr_offs(sh, percpu);
Dan Williams91c00922007-01-02 13:52:30 -07001501 int target = sh->ops.target;
1502 struct r5dev *tgt = &sh->dev[target];
1503 struct page *xor_dest = tgt->page;
Yufen Yu7aba13b2020-08-20 09:22:06 -04001504 unsigned int off_dest = tgt->offset;
Dan Williams91c00922007-01-02 13:52:30 -07001505 int count = 0;
1506 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001507 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001508 int i;
1509
shli@kernel.org59fc6302014-12-15 12:57:03 +11001510 BUG_ON(sh->batch_head);
1511
Dan Williams91c00922007-01-02 13:52:30 -07001512 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001513 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001514 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1515
Yufen Yu7aba13b2020-08-20 09:22:06 -04001516 for (i = disks; i--; ) {
1517 if (i != target) {
1518 off_srcs[count] = sh->dev[i].offset;
Dan Williams91c00922007-01-02 13:52:30 -07001519 xor_srcs[count++] = sh->dev[i].page;
Yufen Yu7aba13b2020-08-20 09:22:06 -04001520 }
1521 }
Dan Williams91c00922007-01-02 13:52:30 -07001522
1523 atomic_inc(&sh->count);
1524
Dan Williams0403e382009-09-08 17:42:50 -07001525 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001526 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
Dan Williams91c00922007-01-02 13:52:30 -07001527 if (unlikely(count == 1))
Yufen Yu7aba13b2020-08-20 09:22:06 -04001528 tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0],
Yufen Yuc911c462020-07-18 05:29:07 -04001529 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001530 else
Yufen Yua7c224a2020-08-20 09:22:09 -04001531 tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
Yufen Yuc911c462020-07-18 05:29:07 -04001532 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001533
Dan Williams91c00922007-01-02 13:52:30 -07001534 return tx;
1535}
1536
Dan Williamsac6b53b2009-07-14 13:40:19 -07001537/* set_syndrome_sources - populate source buffers for gen_syndrome
1538 * @srcs - (struct page *) array of size sh->disks
Yufen Yud69454b2020-08-20 09:22:10 -04001539 * @offs - (unsigned int) array of offset for each page
Dan Williamsac6b53b2009-07-14 13:40:19 -07001540 * @sh - stripe_head to parse
1541 *
1542 * Populates srcs in proper layout order for the stripe and returns the
1543 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1544 * destination buffer is recorded in srcs[count] and the Q destination
1545 * is recorded in srcs[count+1]].
1546 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001547static int set_syndrome_sources(struct page **srcs,
Yufen Yud69454b2020-08-20 09:22:10 -04001548 unsigned int *offs,
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001549 struct stripe_head *sh,
1550 int srctype)
Dan Williamsac6b53b2009-07-14 13:40:19 -07001551{
1552 int disks = sh->disks;
1553 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1554 int d0_idx = raid6_d0(sh);
1555 int count;
1556 int i;
1557
1558 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001559 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001560
1561 count = 0;
1562 i = d0_idx;
1563 do {
1564 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001565 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001566
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001567 if (i == sh->qd_idx || i == sh->pd_idx ||
1568 (srctype == SYNDROME_SRC_ALL) ||
1569 (srctype == SYNDROME_SRC_WANT_DRAIN &&
Song Liu1e6d6902016-11-17 15:24:39 -08001570 (test_bit(R5_Wantdrain, &dev->flags) ||
1571 test_bit(R5_InJournal, &dev->flags))) ||
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001572 (srctype == SYNDROME_SRC_WRITTEN &&
Song Liu09777622017-03-13 13:44:35 -07001573 (dev->written ||
1574 test_bit(R5_InJournal, &dev->flags)))) {
Song Liu1e6d6902016-11-17 15:24:39 -08001575 if (test_bit(R5_InJournal, &dev->flags))
1576 srcs[slot] = sh->dev[i].orig_page;
1577 else
1578 srcs[slot] = sh->dev[i].page;
Yufen Yud69454b2020-08-20 09:22:10 -04001579 /*
1580 * For R5_InJournal, PAGE_SIZE must be 4KB and will
1581 * not shared page. In that case, dev[i].offset
1582 * is 0.
1583 */
1584 offs[slot] = sh->dev[i].offset;
Song Liu1e6d6902016-11-17 15:24:39 -08001585 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001586 i = raid6_next_disk(i, disks);
1587 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001588
NeilBrowne4424fe2009-10-16 16:27:34 +11001589 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001590}
1591
1592static struct dma_async_tx_descriptor *
1593ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1594{
1595 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001596 struct page **blocks = to_addr_page(percpu, 0);
Yufen Yua7c224a2020-08-20 09:22:09 -04001597 unsigned int *offs = to_addr_offs(sh, percpu);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001598 int target;
1599 int qd_idx = sh->qd_idx;
1600 struct dma_async_tx_descriptor *tx;
1601 struct async_submit_ctl submit;
1602 struct r5dev *tgt;
1603 struct page *dest;
Yufen Yua7c224a2020-08-20 09:22:09 -04001604 unsigned int dest_off;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001605 int i;
1606 int count;
1607
shli@kernel.org59fc6302014-12-15 12:57:03 +11001608 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001609 if (sh->ops.target < 0)
1610 target = sh->ops.target2;
1611 else if (sh->ops.target2 < 0)
1612 target = sh->ops.target;
1613 else
1614 /* we should only have one valid target */
1615 BUG();
1616 BUG_ON(target < 0);
1617 pr_debug("%s: stripe %llu block: %d\n",
1618 __func__, (unsigned long long)sh->sector, target);
1619
1620 tgt = &sh->dev[target];
1621 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1622 dest = tgt->page;
Yufen Yua7c224a2020-08-20 09:22:09 -04001623 dest_off = tgt->offset;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001624
1625 atomic_inc(&sh->count);
1626
1627 if (target == qd_idx) {
Yufen Yud69454b2020-08-20 09:22:10 -04001628 count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001629 blocks[count] = NULL; /* regenerating p is not necessary */
1630 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001631 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1632 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001633 to_addr_conv(sh, percpu, 0));
Yufen Yud69454b2020-08-20 09:22:10 -04001634 tx = async_gen_syndrome(blocks, offs, count+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001635 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001636 } else {
1637 /* Compute any data- or p-drive using XOR */
1638 count = 0;
1639 for (i = disks; i-- ; ) {
1640 if (i == target || i == qd_idx)
1641 continue;
Yufen Yua7c224a2020-08-20 09:22:09 -04001642 offs[count] = sh->dev[i].offset;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001643 blocks[count++] = sh->dev[i].page;
1644 }
1645
Dan Williams0403e382009-09-08 17:42:50 -07001646 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1647 NULL, ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001648 to_addr_conv(sh, percpu, 0));
Yufen Yua7c224a2020-08-20 09:22:09 -04001649 tx = async_xor_offs(dest, dest_off, blocks, offs, count,
Yufen Yuc911c462020-07-18 05:29:07 -04001650 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001651 }
1652
1653 return tx;
1654}
1655
1656static struct dma_async_tx_descriptor *
1657ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1658{
1659 int i, count, disks = sh->disks;
1660 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1661 int d0_idx = raid6_d0(sh);
1662 int faila = -1, failb = -1;
1663 int target = sh->ops.target;
1664 int target2 = sh->ops.target2;
1665 struct r5dev *tgt = &sh->dev[target];
1666 struct r5dev *tgt2 = &sh->dev[target2];
1667 struct dma_async_tx_descriptor *tx;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001668 struct page **blocks = to_addr_page(percpu, 0);
Yufen Yua7c224a2020-08-20 09:22:09 -04001669 unsigned int *offs = to_addr_offs(sh, percpu);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001670 struct async_submit_ctl submit;
1671
shli@kernel.org59fc6302014-12-15 12:57:03 +11001672 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001673 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1674 __func__, (unsigned long long)sh->sector, target, target2);
1675 BUG_ON(target < 0 || target2 < 0);
1676 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1677 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1678
Dan Williams6c910a72009-09-16 12:24:54 -07001679 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001680 * slot number conversion for 'faila' and 'failb'
1681 */
Yufen Yua7c224a2020-08-20 09:22:09 -04001682 for (i = 0; i < disks ; i++) {
1683 offs[i] = 0;
NeilBrown5dd33c92009-10-16 16:40:25 +11001684 blocks[i] = NULL;
Yufen Yua7c224a2020-08-20 09:22:09 -04001685 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001686 count = 0;
1687 i = d0_idx;
1688 do {
1689 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1690
Yufen Yua7c224a2020-08-20 09:22:09 -04001691 offs[slot] = sh->dev[i].offset;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001692 blocks[slot] = sh->dev[i].page;
1693
1694 if (i == target)
1695 faila = slot;
1696 if (i == target2)
1697 failb = slot;
1698 i = raid6_next_disk(i, disks);
1699 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001700
1701 BUG_ON(faila == failb);
1702 if (failb < faila)
1703 swap(faila, failb);
1704 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1705 __func__, (unsigned long long)sh->sector, faila, failb);
1706
1707 atomic_inc(&sh->count);
1708
1709 if (failb == syndrome_disks+1) {
1710 /* Q disk is one of the missing disks */
1711 if (faila == syndrome_disks) {
1712 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001713 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1714 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001715 to_addr_conv(sh, percpu, 0));
Yufen Yud69454b2020-08-20 09:22:10 -04001716 return async_gen_syndrome(blocks, offs, syndrome_disks+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001717 RAID5_STRIPE_SIZE(sh->raid_conf),
1718 &submit);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001719 } else {
1720 struct page *dest;
Yufen Yua7c224a2020-08-20 09:22:09 -04001721 unsigned int dest_off;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001722 int data_target;
1723 int qd_idx = sh->qd_idx;
1724
1725 /* Missing D+Q: recompute D from P, then recompute Q */
1726 if (target == qd_idx)
1727 data_target = target2;
1728 else
1729 data_target = target;
1730
1731 count = 0;
1732 for (i = disks; i-- ; ) {
1733 if (i == data_target || i == qd_idx)
1734 continue;
Yufen Yua7c224a2020-08-20 09:22:09 -04001735 offs[count] = sh->dev[i].offset;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001736 blocks[count++] = sh->dev[i].page;
1737 }
1738 dest = sh->dev[data_target].page;
Yufen Yua7c224a2020-08-20 09:22:09 -04001739 dest_off = sh->dev[data_target].offset;
Dan Williams0403e382009-09-08 17:42:50 -07001740 init_async_submit(&submit,
1741 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1742 NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001743 to_addr_conv(sh, percpu, 0));
Yufen Yua7c224a2020-08-20 09:22:09 -04001744 tx = async_xor_offs(dest, dest_off, blocks, offs, count,
Yufen Yuc911c462020-07-18 05:29:07 -04001745 RAID5_STRIPE_SIZE(sh->raid_conf),
Dan Williamsac6b53b2009-07-14 13:40:19 -07001746 &submit);
1747
Yufen Yud69454b2020-08-20 09:22:10 -04001748 count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL);
Dan Williams0403e382009-09-08 17:42:50 -07001749 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1750 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001751 to_addr_conv(sh, percpu, 0));
Yufen Yud69454b2020-08-20 09:22:10 -04001752 return async_gen_syndrome(blocks, offs, count+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001753 RAID5_STRIPE_SIZE(sh->raid_conf),
1754 &submit);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001755 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001756 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001757 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1758 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001759 to_addr_conv(sh, percpu, 0));
Dan Williams6c910a72009-09-16 12:24:54 -07001760 if (failb == syndrome_disks) {
1761 /* We're missing D+P. */
1762 return async_raid6_datap_recov(syndrome_disks+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001763 RAID5_STRIPE_SIZE(sh->raid_conf),
1764 faila,
Yufen Yu4f86ff52020-08-20 09:22:11 -04001765 blocks, offs, &submit);
Dan Williams6c910a72009-09-16 12:24:54 -07001766 } else {
1767 /* We're missing D+D. */
1768 return async_raid6_2data_recov(syndrome_disks+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001769 RAID5_STRIPE_SIZE(sh->raid_conf),
1770 faila, failb,
Yufen Yu4f86ff52020-08-20 09:22:11 -04001771 blocks, offs, &submit);
Dan Williams6c910a72009-09-16 12:24:54 -07001772 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001773 }
1774}
1775
Dan Williams91c00922007-01-02 13:52:30 -07001776static void ops_complete_prexor(void *stripe_head_ref)
1777{
1778 struct stripe_head *sh = stripe_head_ref;
1779
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001780 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001781 (unsigned long long)sh->sector);
Song Liu1e6d6902016-11-17 15:24:39 -08001782
1783 if (r5c_is_writeback(sh->raid_conf->log))
1784 /*
1785 * raid5-cache write back uses orig_page during prexor.
1786 * After prexor, it is time to free orig_page
1787 */
1788 r5c_release_extra_page(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001789}
1790
1791static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001792ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1793 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001794{
Dan Williams91c00922007-01-02 13:52:30 -07001795 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001796 struct page **xor_srcs = to_addr_page(percpu, 0);
Yufen Yua7c224a2020-08-20 09:22:09 -04001797 unsigned int *off_srcs = to_addr_offs(sh, percpu);
Dan Williams91c00922007-01-02 13:52:30 -07001798 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001799 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001800
1801 /* existing parity data subtracted */
Yufen Yua7c224a2020-08-20 09:22:09 -04001802 unsigned int off_dest = off_srcs[count] = sh->dev[pd_idx].offset;
Dan Williams91c00922007-01-02 13:52:30 -07001803 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1804
shli@kernel.org59fc6302014-12-15 12:57:03 +11001805 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001806 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001807 (unsigned long long)sh->sector);
1808
1809 for (i = disks; i--; ) {
1810 struct r5dev *dev = &sh->dev[i];
1811 /* Only process blocks that are known to be uptodate */
Yufen Yua7c224a2020-08-20 09:22:09 -04001812 if (test_bit(R5_InJournal, &dev->flags)) {
1813 /*
1814 * For this case, PAGE_SIZE must be equal to 4KB and
1815 * page offset is zero.
1816 */
1817 off_srcs[count] = dev->offset;
Song Liu1e6d6902016-11-17 15:24:39 -08001818 xor_srcs[count++] = dev->orig_page;
Yufen Yua7c224a2020-08-20 09:22:09 -04001819 } else if (test_bit(R5_Wantdrain, &dev->flags)) {
1820 off_srcs[count] = dev->offset;
Dan Williams91c00922007-01-02 13:52:30 -07001821 xor_srcs[count++] = dev->page;
Yufen Yua7c224a2020-08-20 09:22:09 -04001822 }
Dan Williams91c00922007-01-02 13:52:30 -07001823 }
1824
Dan Williams0403e382009-09-08 17:42:50 -07001825 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001826 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Yufen Yua7c224a2020-08-20 09:22:09 -04001827 tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
Yufen Yuc911c462020-07-18 05:29:07 -04001828 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001829
1830 return tx;
1831}
1832
1833static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001834ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1835 struct dma_async_tx_descriptor *tx)
1836{
1837 struct page **blocks = to_addr_page(percpu, 0);
Yufen Yud69454b2020-08-20 09:22:10 -04001838 unsigned int *offs = to_addr_offs(sh, percpu);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001839 int count;
1840 struct async_submit_ctl submit;
1841
1842 pr_debug("%s: stripe %llu\n", __func__,
1843 (unsigned long long)sh->sector);
1844
Yufen Yud69454b2020-08-20 09:22:10 -04001845 count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_WANT_DRAIN);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001846
1847 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1848 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Yufen Yud69454b2020-08-20 09:22:10 -04001849 tx = async_gen_syndrome(blocks, offs, count+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001850 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001851
1852 return tx;
1853}
1854
1855static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001856ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001857{
Song Liu1e6d6902016-11-17 15:24:39 -08001858 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001859 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001860 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001861 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -07001862
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001863 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001864 (unsigned long long)sh->sector);
1865
1866 for (i = disks; i--; ) {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001867 struct r5dev *dev;
Dan Williams91c00922007-01-02 13:52:30 -07001868 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001869
shli@kernel.org59fc6302014-12-15 12:57:03 +11001870 sh = head_sh;
1871 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001872 struct bio *wbi;
1873
shli@kernel.org59fc6302014-12-15 12:57:03 +11001874again:
1875 dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001876 /*
1877 * clear R5_InJournal, so when rewriting a page in
1878 * journal, it is not skipped by r5l_log_stripe()
1879 */
1880 clear_bit(R5_InJournal, &dev->flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10001881 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001882 chosen = dev->towrite;
1883 dev->towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11001884 sh->overwrite_disks = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001885 BUG_ON(dev->written);
1886 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001887 spin_unlock_irq(&sh->stripe_lock);
Shaohua Lid592a992014-05-21 17:57:44 +08001888 WARN_ON(dev->page != dev->orig_page);
Dan Williams91c00922007-01-02 13:52:30 -07001889
Kent Overstreet4f024f32013-10-11 15:44:27 -07001890 while (wbi && wbi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04001891 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001892 if (wbi->bi_opf & REQ_FUA)
Tejun Heoe9c74692010-09-03 11:56:18 +02001893 set_bit(R5_WantFUA, &dev->flags);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001894 if (wbi->bi_opf & REQ_SYNC)
Shaohua Libc0934f2012-05-22 13:55:05 +10001895 set_bit(R5_SyncIO, &dev->flags);
Mike Christie796a5cf2016-06-05 14:32:07 -05001896 if (bio_op(wbi) == REQ_OP_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001897 set_bit(R5_Discard, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08001898 else {
1899 tx = async_copy_data(1, wbi, &dev->page,
Yufen Yu248728d2020-08-20 09:22:07 -04001900 dev->offset,
Song Liu1e6d6902016-11-17 15:24:39 -08001901 dev->sector, tx, sh,
1902 r5c_is_writeback(conf->log));
1903 if (dev->page != dev->orig_page &&
1904 !r5c_is_writeback(conf->log)) {
Shaohua Lid592a992014-05-21 17:57:44 +08001905 set_bit(R5_SkipCopy, &dev->flags);
1906 clear_bit(R5_UPTODATE, &dev->flags);
1907 clear_bit(R5_OVERWRITE, &dev->flags);
1908 }
1909 }
Yufen Yuc911c462020-07-18 05:29:07 -04001910 wbi = r5_next_bio(conf, wbi, dev->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001911 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001912
1913 if (head_sh->batch_head) {
1914 sh = list_first_entry(&sh->batch_list,
1915 struct stripe_head,
1916 batch_list);
1917 if (sh == head_sh)
1918 continue;
1919 goto again;
1920 }
Dan Williams91c00922007-01-02 13:52:30 -07001921 }
1922 }
1923
1924 return tx;
1925}
1926
Dan Williamsac6b53b2009-07-14 13:40:19 -07001927static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001928{
1929 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001930 int disks = sh->disks;
1931 int pd_idx = sh->pd_idx;
1932 int qd_idx = sh->qd_idx;
1933 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001934 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001935
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001936 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001937 (unsigned long long)sh->sector);
1938
Shaohua Libc0934f2012-05-22 13:55:05 +10001939 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001940 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001941 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001942 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001943 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001944
Dan Williams91c00922007-01-02 13:52:30 -07001945 for (i = disks; i--; ) {
1946 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001947
Tejun Heoe9c74692010-09-03 11:56:18 +02001948 if (dev->written || i == pd_idx || i == qd_idx) {
NeilBrown235b6002017-10-17 16:18:36 +11001949 if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001950 set_bit(R5_UPTODATE, &dev->flags);
NeilBrown235b6002017-10-17 16:18:36 +11001951 if (test_bit(STRIPE_EXPAND_READY, &sh->state))
1952 set_bit(R5_Expanded, &dev->flags);
1953 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001954 if (fua)
1955 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001956 if (sync)
1957 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001958 }
Dan Williams91c00922007-01-02 13:52:30 -07001959 }
1960
Dan Williamsd8ee0722008-06-28 08:32:06 +10001961 if (sh->reconstruct_state == reconstruct_state_drain_run)
1962 sh->reconstruct_state = reconstruct_state_drain_result;
1963 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1964 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1965 else {
1966 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1967 sh->reconstruct_state = reconstruct_state_result;
1968 }
Dan Williams91c00922007-01-02 13:52:30 -07001969
1970 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001971 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001972}
1973
1974static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001975ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1976 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001977{
Dan Williams91c00922007-01-02 13:52:30 -07001978 int disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001979 struct page **xor_srcs;
Yufen Yu7aba13b2020-08-20 09:22:06 -04001980 unsigned int *off_srcs;
Dan Williamsa08abd82009-06-03 11:43:59 -07001981 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001982 int count, pd_idx = sh->pd_idx, i;
Dan Williams91c00922007-01-02 13:52:30 -07001983 struct page *xor_dest;
Yufen Yu7aba13b2020-08-20 09:22:06 -04001984 unsigned int off_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001985 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001986 unsigned long flags;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001987 int j = 0;
1988 struct stripe_head *head_sh = sh;
1989 int last_stripe;
Dan Williams91c00922007-01-02 13:52:30 -07001990
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001991 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001992 (unsigned long long)sh->sector);
1993
Shaohua Li620125f2012-10-11 13:49:05 +11001994 for (i = 0; i < sh->disks; i++) {
1995 if (pd_idx == i)
1996 continue;
1997 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1998 break;
1999 }
2000 if (i >= sh->disks) {
2001 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11002002 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
2003 ops_complete_reconstruct(sh);
2004 return;
2005 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11002006again:
2007 count = 0;
2008 xor_srcs = to_addr_page(percpu, j);
Yufen Yu7aba13b2020-08-20 09:22:06 -04002009 off_srcs = to_addr_offs(sh, percpu);
Dan Williams91c00922007-01-02 13:52:30 -07002010 /* check if prexor is active which means only process blocks
2011 * that are part of a read-modify-write (written)
2012 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11002013 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10002014 prexor = 1;
Yufen Yu7aba13b2020-08-20 09:22:06 -04002015 off_dest = off_srcs[count] = sh->dev[pd_idx].offset;
Dan Williams91c00922007-01-02 13:52:30 -07002016 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
2017 for (i = disks; i--; ) {
2018 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08002019 if (head_sh->dev[i].written ||
Yufen Yu7aba13b2020-08-20 09:22:06 -04002020 test_bit(R5_InJournal, &head_sh->dev[i].flags)) {
2021 off_srcs[count] = dev->offset;
Dan Williams91c00922007-01-02 13:52:30 -07002022 xor_srcs[count++] = dev->page;
Yufen Yu7aba13b2020-08-20 09:22:06 -04002023 }
Dan Williams91c00922007-01-02 13:52:30 -07002024 }
2025 } else {
2026 xor_dest = sh->dev[pd_idx].page;
Yufen Yu7aba13b2020-08-20 09:22:06 -04002027 off_dest = sh->dev[pd_idx].offset;
Dan Williams91c00922007-01-02 13:52:30 -07002028 for (i = disks; i--; ) {
2029 struct r5dev *dev = &sh->dev[i];
Yufen Yu7aba13b2020-08-20 09:22:06 -04002030 if (i != pd_idx) {
2031 off_srcs[count] = dev->offset;
Dan Williams91c00922007-01-02 13:52:30 -07002032 xor_srcs[count++] = dev->page;
Yufen Yu7aba13b2020-08-20 09:22:06 -04002033 }
Dan Williams91c00922007-01-02 13:52:30 -07002034 }
2035 }
2036
Dan Williams91c00922007-01-02 13:52:30 -07002037 /* 1/ if we prexor'd then the dest is reused as a source
2038 * 2/ if we did not prexor then we are redoing the parity
2039 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
2040 * for the synchronous xor case
2041 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11002042 last_stripe = !head_sh->batch_head ||
2043 list_first_entry(&sh->batch_list,
2044 struct stripe_head, batch_list) == head_sh;
2045 if (last_stripe) {
2046 flags = ASYNC_TX_ACK |
2047 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
Dan Williams91c00922007-01-02 13:52:30 -07002048
shli@kernel.org59fc6302014-12-15 12:57:03 +11002049 atomic_inc(&head_sh->count);
2050 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
2051 to_addr_conv(sh, percpu, j));
2052 } else {
2053 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
2054 init_async_submit(&submit, flags, tx, NULL, NULL,
2055 to_addr_conv(sh, percpu, j));
2056 }
Dan Williams91c00922007-01-02 13:52:30 -07002057
Dan Williamsa08abd82009-06-03 11:43:59 -07002058 if (unlikely(count == 1))
Yufen Yu7aba13b2020-08-20 09:22:06 -04002059 tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0],
Yufen Yuc911c462020-07-18 05:29:07 -04002060 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williamsa08abd82009-06-03 11:43:59 -07002061 else
Yufen Yua7c224a2020-08-20 09:22:09 -04002062 tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
Yufen Yuc911c462020-07-18 05:29:07 -04002063 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002064 if (!last_stripe) {
2065 j++;
2066 sh = list_first_entry(&sh->batch_list, struct stripe_head,
2067 batch_list);
2068 goto again;
2069 }
Dan Williams91c00922007-01-02 13:52:30 -07002070}
2071
Dan Williamsac6b53b2009-07-14 13:40:19 -07002072static void
2073ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
2074 struct dma_async_tx_descriptor *tx)
2075{
2076 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11002077 struct page **blocks;
Yufen Yud69454b2020-08-20 09:22:10 -04002078 unsigned int *offs;
shli@kernel.org59fc6302014-12-15 12:57:03 +11002079 int count, i, j = 0;
2080 struct stripe_head *head_sh = sh;
2081 int last_stripe;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002082 int synflags;
2083 unsigned long txflags;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002084
2085 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
2086
Shaohua Li620125f2012-10-11 13:49:05 +11002087 for (i = 0; i < sh->disks; i++) {
2088 if (sh->pd_idx == i || sh->qd_idx == i)
2089 continue;
2090 if (!test_bit(R5_Discard, &sh->dev[i].flags))
2091 break;
2092 }
2093 if (i >= sh->disks) {
2094 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11002095 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
2096 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
2097 ops_complete_reconstruct(sh);
2098 return;
2099 }
2100
shli@kernel.org59fc6302014-12-15 12:57:03 +11002101again:
2102 blocks = to_addr_page(percpu, j);
Yufen Yud69454b2020-08-20 09:22:10 -04002103 offs = to_addr_offs(sh, percpu);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002104
2105 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
2106 synflags = SYNDROME_SRC_WRITTEN;
2107 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
2108 } else {
2109 synflags = SYNDROME_SRC_ALL;
2110 txflags = ASYNC_TX_ACK;
2111 }
2112
Yufen Yud69454b2020-08-20 09:22:10 -04002113 count = set_syndrome_sources(blocks, offs, sh, synflags);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002114 last_stripe = !head_sh->batch_head ||
2115 list_first_entry(&sh->batch_list,
2116 struct stripe_head, batch_list) == head_sh;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002117
shli@kernel.org59fc6302014-12-15 12:57:03 +11002118 if (last_stripe) {
2119 atomic_inc(&head_sh->count);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002120 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
shli@kernel.org59fc6302014-12-15 12:57:03 +11002121 head_sh, to_addr_conv(sh, percpu, j));
2122 } else
2123 init_async_submit(&submit, 0, tx, NULL, NULL,
2124 to_addr_conv(sh, percpu, j));
Yufen Yud69454b2020-08-20 09:22:10 -04002125 tx = async_gen_syndrome(blocks, offs, count+2,
Yufen Yuc911c462020-07-18 05:29:07 -04002126 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002127 if (!last_stripe) {
2128 j++;
2129 sh = list_first_entry(&sh->batch_list, struct stripe_head,
2130 batch_list);
2131 goto again;
2132 }
Dan Williams91c00922007-01-02 13:52:30 -07002133}
2134
2135static void ops_complete_check(void *stripe_head_ref)
2136{
2137 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07002138
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002139 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07002140 (unsigned long long)sh->sector);
2141
Dan Williamsecc65c92008-06-28 08:31:57 +10002142 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07002143 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002144 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07002145}
2146
Dan Williamsac6b53b2009-07-14 13:40:19 -07002147static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07002148{
Dan Williams91c00922007-01-02 13:52:30 -07002149 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002150 int pd_idx = sh->pd_idx;
2151 int qd_idx = sh->qd_idx;
2152 struct page *xor_dest;
Yufen Yua7c224a2020-08-20 09:22:09 -04002153 unsigned int off_dest;
shli@kernel.org46d5b782014-12-15 12:57:02 +11002154 struct page **xor_srcs = to_addr_page(percpu, 0);
Yufen Yua7c224a2020-08-20 09:22:09 -04002155 unsigned int *off_srcs = to_addr_offs(sh, percpu);
Dan Williams91c00922007-01-02 13:52:30 -07002156 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07002157 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002158 int count;
2159 int i;
Dan Williams91c00922007-01-02 13:52:30 -07002160
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002161 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07002162 (unsigned long long)sh->sector);
2163
shli@kernel.org59fc6302014-12-15 12:57:03 +11002164 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002165 count = 0;
2166 xor_dest = sh->dev[pd_idx].page;
Yufen Yua7c224a2020-08-20 09:22:09 -04002167 off_dest = sh->dev[pd_idx].offset;
2168 off_srcs[count] = off_dest;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002169 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07002170 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07002171 if (i == pd_idx || i == qd_idx)
2172 continue;
Yufen Yua7c224a2020-08-20 09:22:09 -04002173 off_srcs[count] = sh->dev[i].offset;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002174 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07002175 }
2176
Dan Williamsd6f38f32009-07-14 11:50:52 -07002177 init_async_submit(&submit, 0, NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11002178 to_addr_conv(sh, percpu, 0));
Yufen Yua7c224a2020-08-20 09:22:09 -04002179 tx = async_xor_val_offs(xor_dest, off_dest, xor_srcs, off_srcs, count,
Yufen Yuc911c462020-07-18 05:29:07 -04002180 RAID5_STRIPE_SIZE(sh->raid_conf),
Dan Williamsa08abd82009-06-03 11:43:59 -07002181 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07002182
Dan Williams91c00922007-01-02 13:52:30 -07002183 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07002184 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
2185 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07002186}
2187
Dan Williamsac6b53b2009-07-14 13:40:19 -07002188static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
2189{
shli@kernel.org46d5b782014-12-15 12:57:02 +11002190 struct page **srcs = to_addr_page(percpu, 0);
Yufen Yud69454b2020-08-20 09:22:10 -04002191 unsigned int *offs = to_addr_offs(sh, percpu);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002192 struct async_submit_ctl submit;
2193 int count;
2194
2195 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
2196 (unsigned long long)sh->sector, checkp);
2197
shli@kernel.org59fc6302014-12-15 12:57:03 +11002198 BUG_ON(sh->batch_head);
Yufen Yud69454b2020-08-20 09:22:10 -04002199 count = set_syndrome_sources(srcs, offs, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002200 if (!checkp)
2201 srcs[count] = NULL;
2202
2203 atomic_inc(&sh->count);
2204 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
shli@kernel.org46d5b782014-12-15 12:57:02 +11002205 sh, to_addr_conv(sh, percpu, 0));
Yufen Yud69454b2020-08-20 09:22:10 -04002206 async_syndrome_val(srcs, offs, count+2,
Yufen Yuc911c462020-07-18 05:29:07 -04002207 RAID5_STRIPE_SIZE(sh->raid_conf),
Yufen Yud69454b2020-08-20 09:22:10 -04002208 &sh->ops.zero_sum_result, percpu->spare_page, 0, &submit);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002209}
2210
NeilBrown51acbce2013-02-28 09:08:34 +11002211static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07002212{
2213 int overlap_clear = 0, i, disks = sh->disks;
2214 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11002215 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002216 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002217 struct raid5_percpu *percpu;
2218 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07002219
Dan Williamsd6f38f32009-07-14 11:50:52 -07002220 cpu = get_cpu();
2221 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10002222 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07002223 ops_run_biofill(sh);
2224 overlap_clear++;
2225 }
2226
Dan Williams7b3a8712008-06-28 08:32:09 +10002227 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07002228 if (level < 6)
2229 tx = ops_run_compute5(sh, percpu);
2230 else {
2231 if (sh->ops.target2 < 0 || sh->ops.target < 0)
2232 tx = ops_run_compute6_1(sh, percpu);
2233 else
2234 tx = ops_run_compute6_2(sh, percpu);
2235 }
2236 /* terminate the chain if reconstruct is not set to be run */
2237 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10002238 async_tx_ack(tx);
2239 }
Dan Williams91c00922007-01-02 13:52:30 -07002240
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002241 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
2242 if (level < 6)
2243 tx = ops_run_prexor5(sh, percpu, tx);
2244 else
2245 tx = ops_run_prexor6(sh, percpu, tx);
2246 }
Dan Williams91c00922007-01-02 13:52:30 -07002247
Artur Paszkiewiczae1713e2017-04-04 13:13:58 +02002248 if (test_bit(STRIPE_OP_PARTIAL_PARITY, &ops_request))
2249 tx = ops_run_partial_parity(sh, percpu, tx);
2250
Dan Williams600aa102008-06-28 08:32:05 +10002251 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10002252 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07002253 overlap_clear++;
2254 }
2255
Dan Williamsac6b53b2009-07-14 13:40:19 -07002256 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
2257 if (level < 6)
2258 ops_run_reconstruct5(sh, percpu, tx);
2259 else
2260 ops_run_reconstruct6(sh, percpu, tx);
2261 }
Dan Williams91c00922007-01-02 13:52:30 -07002262
Dan Williamsac6b53b2009-07-14 13:40:19 -07002263 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
2264 if (sh->check_state == check_state_run)
2265 ops_run_check_p(sh, percpu);
2266 else if (sh->check_state == check_state_run_q)
2267 ops_run_check_pq(sh, percpu, 0);
2268 else if (sh->check_state == check_state_run_pq)
2269 ops_run_check_pq(sh, percpu, 1);
2270 else
2271 BUG();
2272 }
Dan Williams91c00922007-01-02 13:52:30 -07002273
shli@kernel.org59fc6302014-12-15 12:57:03 +11002274 if (overlap_clear && !sh->batch_head)
Dan Williams91c00922007-01-02 13:52:30 -07002275 for (i = disks; i--; ) {
2276 struct r5dev *dev = &sh->dev[i];
2277 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2278 wake_up(&sh->raid_conf->wait_for_overlap);
2279 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07002280 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07002281}
2282
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002283static void free_stripe(struct kmem_cache *sc, struct stripe_head *sh)
2284{
Yufen Yu046169f2020-08-20 09:22:12 -04002285#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2286 kfree(sh->pages);
2287#endif
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002288 if (sh->ppl_page)
2289 __free_page(sh->ppl_page);
2290 kmem_cache_free(sc, sh);
2291}
2292
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002293static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002294 int disks, struct r5conf *conf)
NeilBrownf18c1a32015-05-08 18:19:04 +10002295{
2296 struct stripe_head *sh;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002297 int i;
NeilBrownf18c1a32015-05-08 18:19:04 +10002298
2299 sh = kmem_cache_zalloc(sc, gfp);
2300 if (sh) {
2301 spin_lock_init(&sh->stripe_lock);
2302 spin_lock_init(&sh->batch_lock);
2303 INIT_LIST_HEAD(&sh->batch_list);
2304 INIT_LIST_HEAD(&sh->lru);
Song Liua39f7af2016-11-17 15:24:40 -08002305 INIT_LIST_HEAD(&sh->r5c);
Song Liud7bd3982016-11-23 22:50:39 -08002306 INIT_LIST_HEAD(&sh->log_list);
NeilBrownf18c1a32015-05-08 18:19:04 +10002307 atomic_set(&sh->count, 1);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002308 sh->raid_conf = conf;
Song Liua39f7af2016-11-17 15:24:40 -08002309 sh->log_start = MaxSector;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002310 for (i = 0; i < disks; i++) {
2311 struct r5dev *dev = &sh->dev[i];
2312
Ming Lei3a83f462016-11-22 08:57:21 -07002313 bio_init(&dev->req, &dev->vec, 1);
2314 bio_init(&dev->rreq, &dev->rvec, 1);
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002315 }
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002316
2317 if (raid5_has_ppl(conf)) {
2318 sh->ppl_page = alloc_page(gfp);
2319 if (!sh->ppl_page) {
2320 free_stripe(sc, sh);
Yufen Yu046169f2020-08-20 09:22:12 -04002321 return NULL;
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002322 }
2323 }
Yufen Yu046169f2020-08-20 09:22:12 -04002324#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2325 if (init_stripe_shared_pages(sh, conf, disks)) {
2326 free_stripe(sc, sh);
2327 return NULL;
2328 }
2329#endif
NeilBrownf18c1a32015-05-08 18:19:04 +10002330 }
2331 return sh;
2332}
NeilBrown486f0642015-02-25 12:10:35 +11002333static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334{
2335 struct stripe_head *sh;
NeilBrownf18c1a32015-05-08 18:19:04 +10002336
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002337 sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size, conf);
NeilBrown3f294f42005-11-08 21:39:25 -08002338 if (!sh)
2339 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10002340
NeilBrowna9683a72015-02-25 12:02:51 +11002341 if (grow_buffers(sh, gfp)) {
NeilBrowne4e11e32010-06-16 16:45:16 +10002342 shrink_buffers(sh);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002343 free_stripe(conf->slab_cache, sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002344 return 0;
2345 }
NeilBrown486f0642015-02-25 12:10:35 +11002346 sh->hash_lock_index =
2347 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrown3f294f42005-11-08 21:39:25 -08002348 /* we just created an active stripe so... */
NeilBrown3f294f42005-11-08 21:39:25 -08002349 atomic_inc(&conf->active_stripes);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002350
Shaohua Li6d036f72015-08-13 14:31:57 -07002351 raid5_release_stripe(sh);
NeilBrown486f0642015-02-25 12:10:35 +11002352 conf->max_nr_stripes++;
NeilBrown3f294f42005-11-08 21:39:25 -08002353 return 1;
2354}
2355
NeilBrownd1688a62011-10-11 16:49:52 +11002356static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08002357{
Christoph Lametere18b8902006-12-06 20:33:20 -08002358 struct kmem_cache *sc;
Arnd Bergmann53b8d892018-02-20 14:09:11 +01002359 size_t namelen = sizeof(conf->cache_name[0]);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002360 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
NeilBrownf4be6b42010-06-01 19:37:25 +10002362 if (conf->mddev->gendisk)
Arnd Bergmann53b8d892018-02-20 14:09:11 +01002363 snprintf(conf->cache_name[0], namelen,
NeilBrownf4be6b42010-06-01 19:37:25 +10002364 "raid%d-%s", conf->level, mdname(conf->mddev));
2365 else
Arnd Bergmann53b8d892018-02-20 14:09:11 +01002366 snprintf(conf->cache_name[0], namelen,
NeilBrownf4be6b42010-06-01 19:37:25 +10002367 "raid%d-%p", conf->level, conf->mddev);
Arnd Bergmann53b8d892018-02-20 14:09:11 +01002368 snprintf(conf->cache_name[1], namelen, "%.27s-alt", conf->cache_name[0]);
NeilBrownf4be6b42010-06-01 19:37:25 +10002369
NeilBrownad01c9e2006-03-27 01:18:07 -08002370 conf->active_name = 0;
2371 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002373 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (!sc)
2375 return 1;
2376 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002377 conf->pool_size = devs;
NeilBrown486f0642015-02-25 12:10:35 +11002378 while (num--)
2379 if (!grow_one_stripe(conf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return 1;
NeilBrown486f0642015-02-25 12:10:35 +11002381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return 0;
2383}
NeilBrown29269552006-03-27 01:18:10 -08002384
Dan Williamsd6f38f32009-07-14 11:50:52 -07002385/**
Coly Li7f8a30e2020-04-09 22:17:22 +08002386 * scribble_alloc - allocate percpu scribble buffer for required size
2387 * of the scribble region
Damien Le Moal2aada5b2020-07-16 13:54:42 +09002388 * @percpu: from for_each_present_cpu() of the caller
2389 * @num: total number of disks in the array
2390 * @cnt: scribble objs count for required size of the scribble region
Dan Williamsd6f38f32009-07-14 11:50:52 -07002391 *
Coly Li7f8a30e2020-04-09 22:17:22 +08002392 * The scribble buffer size must be enough to contain:
Dan Williamsd6f38f32009-07-14 11:50:52 -07002393 * 1/ a struct page pointer for each device in the array +2
2394 * 2/ room to convert each entry in (1) to its corresponding dma
2395 * (dma_map_page()) or page (page_address()) address.
2396 *
2397 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2398 * calculate over all devices (not just the data blocks), using zeros in place
2399 * of the P and Q blocks.
2400 */
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002401static int scribble_alloc(struct raid5_percpu *percpu,
Coly Liba54d4d2020-04-09 22:17:21 +08002402 int num, int cnt)
Dan Williamsd6f38f32009-07-14 11:50:52 -07002403{
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002404 size_t obj_size =
Yufen Yu7aba13b2020-08-20 09:22:06 -04002405 sizeof(struct page *) * (num + 2) +
2406 sizeof(addr_conv_t) * (num + 2) +
2407 sizeof(unsigned int) * (num + 2);
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002408 void *scribble;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002409
Coly Liba54d4d2020-04-09 22:17:21 +08002410 /*
2411 * If here is in raid array suspend context, it is in memalloc noio
2412 * context as well, there is no potential recursive memory reclaim
2413 * I/Os with the GFP_KERNEL flag.
2414 */
2415 scribble = kvmalloc_array(cnt, obj_size, GFP_KERNEL);
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002416 if (!scribble)
2417 return -ENOMEM;
2418
2419 kvfree(percpu->scribble);
2420
2421 percpu->scribble = scribble;
2422 percpu->scribble_obj_size = obj_size;
2423 return 0;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002424}
2425
NeilBrown738a2732015-05-08 18:19:39 +10002426static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2427{
2428 unsigned long cpu;
2429 int err = 0;
2430
Shaohua Li27a353c2016-02-24 17:38:28 -08002431 /*
2432 * Never shrink. And mddev_suspend() could deadlock if this is called
2433 * from raid5d. In that case, scribble_disks and scribble_sectors
2434 * should equal to new_disks and new_sectors
2435 */
2436 if (conf->scribble_disks >= new_disks &&
2437 conf->scribble_sectors >= new_sectors)
2438 return 0;
NeilBrown738a2732015-05-08 18:19:39 +10002439 mddev_suspend(conf->mddev);
2440 get_online_cpus();
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002441
NeilBrown738a2732015-05-08 18:19:39 +10002442 for_each_present_cpu(cpu) {
2443 struct raid5_percpu *percpu;
NeilBrown738a2732015-05-08 18:19:39 +10002444
2445 percpu = per_cpu_ptr(conf->percpu, cpu);
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002446 err = scribble_alloc(percpu, new_disks,
Yufen Yuc911c462020-07-18 05:29:07 -04002447 new_sectors / RAID5_STRIPE_SECTORS(conf));
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002448 if (err)
NeilBrown738a2732015-05-08 18:19:39 +10002449 break;
NeilBrown738a2732015-05-08 18:19:39 +10002450 }
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002451
NeilBrown738a2732015-05-08 18:19:39 +10002452 put_online_cpus();
2453 mddev_resume(conf->mddev);
Shaohua Li27a353c2016-02-24 17:38:28 -08002454 if (!err) {
2455 conf->scribble_disks = new_disks;
2456 conf->scribble_sectors = new_sectors;
2457 }
NeilBrown738a2732015-05-08 18:19:39 +10002458 return err;
2459}
2460
NeilBrownd1688a62011-10-11 16:49:52 +11002461static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08002462{
2463 /* Make all the stripes able to hold 'newsize' devices.
2464 * New slots in each stripe get 'page' set to a new page.
2465 *
2466 * This happens in stages:
2467 * 1/ create a new kmem_cache and allocate the required number of
2468 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09002469 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08002470 * to the new stripe_heads. This will have the side effect of
2471 * freezing the array as once all stripe_heads have been collected,
2472 * no IO will be possible. Old stripe heads are freed once their
2473 * pages have been transferred over, and the old kmem_cache is
2474 * freed when all stripes are done.
2475 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
Zhilong Liu3560741e2017-03-15 16:14:53 +08002476 * we simple return a failure status - no need to clean anything up.
NeilBrownad01c9e2006-03-27 01:18:07 -08002477 * 4/ allocate new pages for the new slots in the new stripe_heads.
2478 * If this fails, we don't bother trying the shrink the
2479 * stripe_heads down again, we just leave them as they are.
2480 * As each stripe_head is processed the new one is released into
2481 * active service.
2482 *
2483 * Once step2 is started, we cannot afford to wait for a write,
2484 * so we use GFP_NOIO allocations.
2485 */
2486 struct stripe_head *osh, *nsh;
2487 LIST_HEAD(newstripes);
2488 struct disk_info *ndisks;
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02002489 int err = 0;
Christoph Lametere18b8902006-12-06 20:33:20 -08002490 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002491 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11002492 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08002493
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02002494 md_allow_write(conf->mddev);
NeilBrown2a2275d2007-01-26 00:57:11 -08002495
NeilBrownad01c9e2006-03-27 01:18:07 -08002496 /* Step 1 */
2497 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2498 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002499 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08002500 if (!sc)
2501 return -ENOMEM;
2502
NeilBrown2d5b5692015-07-06 12:49:23 +10002503 /* Need to ensure auto-resizing doesn't interfere */
2504 mutex_lock(&conf->cache_size_mutex);
2505
NeilBrownad01c9e2006-03-27 01:18:07 -08002506 for (i = conf->max_nr_stripes; i; i--) {
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002507 nsh = alloc_stripe(sc, GFP_KERNEL, newsize, conf);
NeilBrownad01c9e2006-03-27 01:18:07 -08002508 if (!nsh)
2509 break;
2510
NeilBrownad01c9e2006-03-27 01:18:07 -08002511 list_add(&nsh->lru, &newstripes);
2512 }
2513 if (i) {
2514 /* didn't get enough, give up */
2515 while (!list_empty(&newstripes)) {
2516 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2517 list_del(&nsh->lru);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002518 free_stripe(sc, nsh);
NeilBrownad01c9e2006-03-27 01:18:07 -08002519 }
2520 kmem_cache_destroy(sc);
NeilBrown2d5b5692015-07-06 12:49:23 +10002521 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002522 return -ENOMEM;
2523 }
2524 /* Step 2 - Must use GFP_NOIO now.
2525 * OK, we have enough stripes, start collecting inactive
2526 * stripes and copying them over
2527 */
Shaohua Li566c09c2013-11-14 15:16:17 +11002528 hash = 0;
2529 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08002530 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11002531 lock_device_hash_lock(conf, hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08002532 wait_event_cmd(conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +11002533 !list_empty(conf->inactive_list + hash),
2534 unlock_device_hash_lock(conf, hash),
2535 lock_device_hash_lock(conf, hash));
2536 osh = get_free_stripe(conf, hash);
2537 unlock_device_hash_lock(conf, hash);
NeilBrownf18c1a32015-05-08 18:19:04 +10002538
Yufen Yuf16acaf2020-08-20 09:22:13 -04002539#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2540 for (i = 0; i < osh->nr_pages; i++) {
2541 nsh->pages[i] = osh->pages[i];
2542 osh->pages[i] = NULL;
2543 }
2544#endif
Shaohua Lid592a992014-05-21 17:57:44 +08002545 for(i=0; i<conf->pool_size; i++) {
NeilBrownad01c9e2006-03-27 01:18:07 -08002546 nsh->dev[i].page = osh->dev[i].page;
Shaohua Lid592a992014-05-21 17:57:44 +08002547 nsh->dev[i].orig_page = osh->dev[i].page;
Yufen Yu7aba13b2020-08-20 09:22:06 -04002548 nsh->dev[i].offset = osh->dev[i].offset;
Shaohua Lid592a992014-05-21 17:57:44 +08002549 }
Shaohua Li566c09c2013-11-14 15:16:17 +11002550 nsh->hash_lock_index = hash;
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002551 free_stripe(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11002552 cnt++;
2553 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2554 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2555 hash++;
2556 cnt = 0;
2557 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002558 }
2559 kmem_cache_destroy(conf->slab_cache);
2560
2561 /* Step 3.
2562 * At this point, we are holding all the stripes so the array
2563 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07002564 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08002565 */
Kees Cook6396bb22018-06-12 14:03:40 -07002566 ndisks = kcalloc(newsize, sizeof(struct disk_info), GFP_NOIO);
NeilBrownad01c9e2006-03-27 01:18:07 -08002567 if (ndisks) {
Song Liud7bd3982016-11-23 22:50:39 -08002568 for (i = 0; i < conf->pool_size; i++)
NeilBrownad01c9e2006-03-27 01:18:07 -08002569 ndisks[i] = conf->disks[i];
Song Liud7bd3982016-11-23 22:50:39 -08002570
2571 for (i = conf->pool_size; i < newsize; i++) {
2572 ndisks[i].extra_page = alloc_page(GFP_NOIO);
2573 if (!ndisks[i].extra_page)
2574 err = -ENOMEM;
2575 }
2576
2577 if (err) {
2578 for (i = conf->pool_size; i < newsize; i++)
2579 if (ndisks[i].extra_page)
2580 put_page(ndisks[i].extra_page);
2581 kfree(ndisks);
2582 } else {
2583 kfree(conf->disks);
2584 conf->disks = ndisks;
2585 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002586 } else
2587 err = -ENOMEM;
2588
Dennis Yang583da482017-03-29 15:46:13 +08002589 conf->slab_cache = sc;
2590 conf->active_name = 1-conf->active_name;
2591
NeilBrownad01c9e2006-03-27 01:18:07 -08002592 /* Step 4, return new stripes to service */
2593 while(!list_empty(&newstripes)) {
2594 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2595 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07002596
Yufen Yuf16acaf2020-08-20 09:22:13 -04002597#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
2598 for (i = 0; i < nsh->nr_pages; i++) {
2599 if (nsh->pages[i])
2600 continue;
2601 nsh->pages[i] = alloc_page(GFP_NOIO);
2602 if (!nsh->pages[i])
2603 err = -ENOMEM;
2604 }
2605
2606 for (i = conf->raid_disks; i < newsize; i++) {
2607 if (nsh->dev[i].page)
2608 continue;
2609 nsh->dev[i].page = raid5_get_dev_page(nsh, i);
2610 nsh->dev[i].orig_page = nsh->dev[i].page;
2611 nsh->dev[i].offset = raid5_get_page_offset(nsh, i);
2612 }
2613#else
NeilBrownad01c9e2006-03-27 01:18:07 -08002614 for (i=conf->raid_disks; i < newsize; i++)
2615 if (nsh->dev[i].page == NULL) {
2616 struct page *p = alloc_page(GFP_NOIO);
2617 nsh->dev[i].page = p;
Shaohua Lid592a992014-05-21 17:57:44 +08002618 nsh->dev[i].orig_page = p;
Yufen Yu7aba13b2020-08-20 09:22:06 -04002619 nsh->dev[i].offset = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08002620 if (!p)
2621 err = -ENOMEM;
2622 }
Yufen Yuf16acaf2020-08-20 09:22:13 -04002623#endif
Shaohua Li6d036f72015-08-13 14:31:57 -07002624 raid5_release_stripe(nsh);
NeilBrownad01c9e2006-03-27 01:18:07 -08002625 }
2626 /* critical section pass, GFP_NOIO no longer needed */
2627
NeilBrown6e9eac22015-05-08 18:19:34 +10002628 if (!err)
2629 conf->pool_size = newsize;
Song Liub44c0182020-10-05 09:35:21 -07002630 mutex_unlock(&conf->cache_size_mutex);
2631
NeilBrownad01c9e2006-03-27 01:18:07 -08002632 return err;
2633}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
NeilBrown486f0642015-02-25 12:10:35 +11002635static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
2637 struct stripe_head *sh;
NeilBrown49895bc2015-08-03 17:09:57 +10002638 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Shaohua Li566c09c2013-11-14 15:16:17 +11002640 spin_lock_irq(conf->hash_locks + hash);
2641 sh = get_free_stripe(conf, hash);
2642 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08002643 if (!sh)
2644 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002645 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10002646 shrink_buffers(sh);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002647 free_stripe(conf->slab_cache, sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002648 atomic_dec(&conf->active_stripes);
NeilBrown486f0642015-02-25 12:10:35 +11002649 conf->max_nr_stripes--;
NeilBrown3f294f42005-11-08 21:39:25 -08002650 return 1;
2651}
2652
NeilBrownd1688a62011-10-11 16:49:52 +11002653static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08002654{
NeilBrown486f0642015-02-25 12:10:35 +11002655 while (conf->max_nr_stripes &&
2656 drop_one_stripe(conf))
2657 ;
NeilBrown3f294f42005-11-08 21:39:25 -08002658
Julia Lawall644df1a2015-09-13 14:15:10 +02002659 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 conf->slab_cache = NULL;
2661}
2662
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002663static void raid5_end_read_request(struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
NeilBrown99c0fb52009-03-31 14:39:38 +11002665 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002666 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002667 int disks = sh->disks, i;
NeilBrownd6950432006-07-10 04:44:20 -07002668 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11002669 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10002670 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
2672 for (i=0 ; i<disks; i++)
2673 if (bi == &sh->dev[i].req)
2674 break;
2675
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002676 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
Dan Williams45b42332007-07-09 11:56:43 -07002677 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002678 bi->bi_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002680 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002682 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 }
NeilBrown14a75d32011-12-23 10:17:52 +11002684 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11002685 /* If replacement finished while this request was outstanding,
2686 * 'replacement' might be NULL already.
2687 * In that case it moved down to 'rdev'.
2688 * rdev is not removed until all requests are finished.
2689 */
NeilBrown14a75d32011-12-23 10:17:52 +11002690 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002691 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11002692 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
NeilBrown05616be2012-05-21 09:27:00 +10002694 if (use_new_offset(conf, sh))
2695 s = sh->sector + rdev->new_data_offset;
2696 else
2697 s = sh->sector + rdev->data_offset;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002698 if (!bi->bi_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08002700 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11002701 /* Note that this cannot happen on a
2702 * replacement device. We just fail those on
2703 * any error
2704 */
NeilBrowncc6167b2016-11-02 14:16:50 +11002705 pr_info_ratelimited(
2706 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
Yufen Yuc911c462020-07-18 05:29:07 -04002707 mdname(conf->mddev), RAID5_STRIPE_SECTORS(conf),
NeilBrown05616be2012-05-21 09:27:00 +10002708 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002709 bdevname(rdev->bdev, b));
Yufen Yuc911c462020-07-18 05:29:07 -04002710 atomic_add(RAID5_STRIPE_SECTORS(conf), &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08002711 clear_bit(R5_ReadError, &sh->dev[i].flags);
2712 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002713 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2714 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2715
Song Liu86aa1392017-01-12 17:22:41 -08002716 if (test_bit(R5_InJournal, &sh->dev[i].flags))
2717 /*
2718 * end read for a page in journal, this
2719 * must be preparing for prexor in rmw
2720 */
2721 set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
2722
NeilBrown14a75d32011-12-23 10:17:52 +11002723 if (atomic_read(&rdev->read_errors))
2724 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002726 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002727 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002728 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
Nigel Croxonb76b4712019-09-06 09:21:33 -04002731 if (!(bi->bi_status == BLK_STS_PROTECTION))
2732 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002733 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowncc6167b2016-11-02 14:16:50 +11002734 pr_warn_ratelimited(
2735 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
NeilBrown14a75d32011-12-23 10:17:52 +11002736 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002737 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002738 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002739 else if (conf->mddev->degraded >= conf->max_degraded) {
2740 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002741 pr_warn_ratelimited(
2742 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002743 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002744 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002745 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002746 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002747 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002748 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002749 pr_warn_ratelimited(
2750 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002751 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002752 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002753 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002754 } else if (atomic_read(&rdev->read_errors)
Nigel Croxon0009fad2019-08-21 09:27:08 -04002755 > conf->max_nr_stripes) {
2756 if (!test_bit(Faulty, &rdev->flags)) {
2757 pr_warn("md/raid:%s: %d read_errors > %d stripes\n",
2758 mdname(conf->mddev),
2759 atomic_read(&rdev->read_errors),
2760 conf->max_nr_stripes);
2761 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
2762 mdname(conf->mddev), bdn);
2763 }
2764 } else
NeilBrownba22dcb2005-11-08 21:39:31 -08002765 retry = 1;
Bian Yuedfa1f62013-11-14 15:16:17 +11002766 if (set_bad && test_bit(In_sync, &rdev->flags)
2767 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2768 retry = 1;
NeilBrownba22dcb2005-11-08 21:39:31 -08002769 if (retry)
Xiao Ni143f6e72019-07-08 10:14:32 +08002770 if (sh->qd_idx >= 0 && sh->pd_idx == i)
2771 set_bit(R5_ReadError, &sh->dev[i].flags);
2772 else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
majianpeng3f9e7c12012-07-31 10:04:21 +10002773 set_bit(R5_ReadError, &sh->dev[i].flags);
2774 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2775 } else
2776 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002777 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002778 clear_bit(R5_ReadError, &sh->dev[i].flags);
2779 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002780 if (!(set_bad
2781 && test_bit(In_sync, &rdev->flags)
2782 && rdev_set_badblocks(
Yufen Yuc911c462020-07-18 05:29:07 -04002783 rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), 0)))
majianpeng2e8ac3032012-07-03 15:57:02 +10002784 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 }
NeilBrown14a75d32011-12-23 10:17:52 +11002787 rdev_dec_pending(rdev, conf->mddev);
Shaohua Lic9445552016-09-08 10:43:58 -07002788 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2790 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002791 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}
2793
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002794static void raid5_end_write_request(struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
NeilBrown99c0fb52009-03-31 14:39:38 +11002796 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002797 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002798 int disks = sh->disks, i;
Kees Cook3f649ab2020-06-03 13:09:38 -07002799 struct md_rdev *rdev;
NeilBrownb84db562011-07-28 11:39:23 +10002800 sector_t first_bad;
2801 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002802 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
NeilBrown977df362011-12-23 10:17:53 +11002804 for (i = 0 ; i < disks; i++) {
2805 if (bi == &sh->dev[i].req) {
2806 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 break;
NeilBrown977df362011-12-23 10:17:53 +11002808 }
2809 if (bi == &sh->dev[i].rreq) {
2810 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002811 if (rdev)
2812 replacement = 1;
2813 else
2814 /* rdev was removed and 'replacement'
2815 * replaced it. rdev is not removed
2816 * until all requests are finished.
2817 */
2818 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002819 break;
2820 }
2821 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002822 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002824 bi->bi_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002826 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002828 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
2830
NeilBrown977df362011-12-23 10:17:53 +11002831 if (replacement) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002832 if (bi->bi_status)
NeilBrown977df362011-12-23 10:17:53 +11002833 md_error(conf->mddev, rdev);
2834 else if (is_badblock(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04002835 RAID5_STRIPE_SECTORS(conf),
NeilBrown977df362011-12-23 10:17:53 +11002836 &first_bad, &bad_sectors))
2837 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2838 } else {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002839 if (bi->bi_status) {
NeilBrown9f97e4b2014-01-16 09:35:38 +11002840 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11002841 set_bit(WriteErrorSeen, &rdev->flags);
2842 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002843 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2844 set_bit(MD_RECOVERY_NEEDED,
2845 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002846 } else if (is_badblock(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04002847 RAID5_STRIPE_SECTORS(conf),
NeilBrownc0b32972013-04-24 11:42:42 +10002848 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002849 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002850 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2851 /* That was a successful write so make
2852 * sure it looks like we already did
2853 * a re-write.
2854 */
2855 set_bit(R5_ReWrite, &sh->dev[i].flags);
2856 }
NeilBrown977df362011-12-23 10:17:53 +11002857 }
2858 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002860 if (sh->batch_head && bi->bi_status && !replacement)
shli@kernel.org72ac7332014-12-15 12:57:03 +11002861 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2862
Shaohua Lic9445552016-09-08 10:43:58 -07002863 bio_reset(bi);
NeilBrown977df362011-12-23 10:17:53 +11002864 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2865 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002867 raid5_release_stripe(sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002868
2869 if (sh->batch_head && sh != sh->batch_head)
Shaohua Li6d036f72015-08-13 14:31:57 -07002870 raid5_release_stripe(sh->batch_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871}
2872
Shaohua Li849674e2016-01-20 13:52:20 -08002873static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
2875 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002876 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002877 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002878 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
NeilBrown908f4fb2011-12-23 10:17:50 +11002880 spin_lock_irqsave(&conf->device_lock, flags);
Mariusz Tkaczykfb73b352018-09-04 15:08:30 +02002881
2882 if (test_bit(In_sync, &rdev->flags) &&
2883 mddev->degraded == conf->max_degraded) {
2884 /*
2885 * Don't allow to achieve failed state
2886 * Don't try to recover this device
2887 */
2888 conf->recovery_disabled = mddev->recovery_disabled;
2889 spin_unlock_irqrestore(&conf->device_lock, flags);
2890 return;
2891 }
2892
bingjingcaff69d82017-11-17 10:57:44 +08002893 set_bit(Faulty, &rdev->flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11002894 clear_bit(In_sync, &rdev->flags);
Song Liu2e38a372017-01-24 10:45:30 -08002895 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11002896 spin_unlock_irqrestore(&conf->device_lock, flags);
2897 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2898
NeilBrownde393cd2011-07-28 11:31:48 +10002899 set_bit(Blocked, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08002900 set_mask_bits(&mddev->sb_flags, 0,
2901 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrowncc6167b2016-11-02 14:16:50 +11002902 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
2903 "md/raid:%s: Operation continuing on %d devices.\n",
2904 mdname(mddev),
2905 bdevname(rdev->bdev, b),
2906 mdname(mddev),
2907 conf->raid_disks - mddev->degraded);
Song Liu70d466f2017-05-11 15:28:28 -07002908 r5c_update_on_rdev_error(mddev, rdev);
NeilBrown16a53ec2006-06-26 00:27:38 -07002909}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911/*
2912 * Input: a 'big' sector number,
2913 * Output: index of the data and parity disk, and the sector # in them.
2914 */
Shaohua Li6d036f72015-08-13 14:31:57 -07002915sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2916 int previous, int *dd_idx,
2917 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002919 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002920 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002922 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002923 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002925 int algorithm = previous ? conf->prev_algo
2926 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002927 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2928 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002929 int raid_disks = previous ? conf->previous_raid_disks
2930 : conf->raid_disks;
2931 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933 /* First compute the information on this sector */
2934
2935 /*
2936 * Compute the chunk number and the sector offset inside the chunk
2937 */
2938 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2939 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
2941 /*
2942 * Compute the stripe number
2943 */
NeilBrown35f2a592010-04-20 14:13:34 +10002944 stripe = chunk_number;
2945 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002946 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 /*
2948 * Select the parity disk based on the user selected algorithm.
2949 */
NeilBrown84789552011-07-27 11:00:36 +10002950 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002951 switch(conf->level) {
2952 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002953 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002954 break;
2955 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002956 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002958 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002959 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 (*dd_idx)++;
2961 break;
2962 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002963 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002964 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 (*dd_idx)++;
2966 break;
2967 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002968 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002969 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 break;
2971 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002972 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002973 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002975 case ALGORITHM_PARITY_0:
2976 pd_idx = 0;
2977 (*dd_idx)++;
2978 break;
2979 case ALGORITHM_PARITY_N:
2980 pd_idx = data_disks;
2981 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002983 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002984 }
2985 break;
2986 case 6:
2987
NeilBrowne183eae2009-03-31 15:20:22 +11002988 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002989 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002990 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002991 qd_idx = pd_idx + 1;
2992 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002993 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002994 qd_idx = 0;
2995 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002996 (*dd_idx) += 2; /* D D P Q D */
2997 break;
2998 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002999 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11003000 qd_idx = pd_idx + 1;
3001 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11003002 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11003003 qd_idx = 0;
3004 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07003005 (*dd_idx) += 2; /* D D P Q D */
3006 break;
3007 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10003008 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11003009 qd_idx = (pd_idx + 1) % raid_disks;
3010 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07003011 break;
3012 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10003013 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11003014 qd_idx = (pd_idx + 1) % raid_disks;
3015 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07003016 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11003017
3018 case ALGORITHM_PARITY_0:
3019 pd_idx = 0;
3020 qd_idx = 1;
3021 (*dd_idx) += 2;
3022 break;
3023 case ALGORITHM_PARITY_N:
3024 pd_idx = data_disks;
3025 qd_idx = data_disks + 1;
3026 break;
3027
3028 case ALGORITHM_ROTATING_ZERO_RESTART:
3029 /* Exactly the same as RIGHT_ASYMMETRIC, but or
3030 * of blocks for computing Q is different.
3031 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10003032 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11003033 qd_idx = pd_idx + 1;
3034 if (pd_idx == raid_disks-1) {
3035 (*dd_idx)++; /* Q D D D P */
3036 qd_idx = 0;
3037 } else if (*dd_idx >= pd_idx)
3038 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11003039 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11003040 break;
3041
3042 case ALGORITHM_ROTATING_N_RESTART:
3043 /* Same a left_asymmetric, by first stripe is
3044 * D D D P Q rather than
3045 * Q D D D P
3046 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10003047 stripe2 += 1;
3048 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11003049 qd_idx = pd_idx + 1;
3050 if (pd_idx == raid_disks-1) {
3051 (*dd_idx)++; /* Q D D D P */
3052 qd_idx = 0;
3053 } else if (*dd_idx >= pd_idx)
3054 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11003055 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11003056 break;
3057
3058 case ALGORITHM_ROTATING_N_CONTINUE:
3059 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10003060 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11003061 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
3062 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11003063 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11003064 break;
3065
3066 case ALGORITHM_LEFT_ASYMMETRIC_6:
3067 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10003068 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11003069 if (*dd_idx >= pd_idx)
3070 (*dd_idx)++;
3071 qd_idx = raid_disks - 1;
3072 break;
3073
3074 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10003075 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11003076 if (*dd_idx >= pd_idx)
3077 (*dd_idx)++;
3078 qd_idx = raid_disks - 1;
3079 break;
3080
3081 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10003082 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11003083 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
3084 qd_idx = raid_disks - 1;
3085 break;
3086
3087 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10003088 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11003089 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
3090 qd_idx = raid_disks - 1;
3091 break;
3092
3093 case ALGORITHM_PARITY_0_6:
3094 pd_idx = 0;
3095 (*dd_idx)++;
3096 qd_idx = raid_disks - 1;
3097 break;
3098
NeilBrown16a53ec2006-06-26 00:27:38 -07003099 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11003100 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07003101 }
3102 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 }
3104
NeilBrown911d4ee2009-03-31 14:39:38 +11003105 if (sh) {
3106 sh->pd_idx = pd_idx;
3107 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11003108 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11003109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 /*
3111 * Finally, compute the new sector number
3112 */
3113 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
3114 return new_sector;
3115}
3116
Shaohua Li6d036f72015-08-13 14:31:57 -07003117sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
NeilBrownd1688a62011-10-11 16:49:52 +11003119 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08003120 int raid_disks = sh->disks;
3121 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10003123 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
3124 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11003125 int algorithm = previous ? conf->prev_algo
3126 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 sector_t stripe;
3128 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10003129 sector_t chunk_number;
3130 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11003132 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
3134 chunk_offset = sector_div(new_sector, sectors_per_chunk);
3135 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
NeilBrown16a53ec2006-06-26 00:27:38 -07003137 if (i == sh->pd_idx)
3138 return 0;
3139 switch(conf->level) {
3140 case 4: break;
3141 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11003142 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 case ALGORITHM_LEFT_ASYMMETRIC:
3144 case ALGORITHM_RIGHT_ASYMMETRIC:
3145 if (i > sh->pd_idx)
3146 i--;
3147 break;
3148 case ALGORITHM_LEFT_SYMMETRIC:
3149 case ALGORITHM_RIGHT_SYMMETRIC:
3150 if (i < sh->pd_idx)
3151 i += raid_disks;
3152 i -= (sh->pd_idx + 1);
3153 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11003154 case ALGORITHM_PARITY_0:
3155 i -= 1;
3156 break;
3157 case ALGORITHM_PARITY_N:
3158 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11003160 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07003161 }
3162 break;
3163 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11003164 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07003165 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11003166 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003167 case ALGORITHM_LEFT_ASYMMETRIC:
3168 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11003169 case ALGORITHM_ROTATING_ZERO_RESTART:
3170 case ALGORITHM_ROTATING_N_RESTART:
3171 if (sh->pd_idx == raid_disks-1)
3172 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07003173 else if (i > sh->pd_idx)
3174 i -= 2; /* D D P Q D */
3175 break;
3176 case ALGORITHM_LEFT_SYMMETRIC:
3177 case ALGORITHM_RIGHT_SYMMETRIC:
3178 if (sh->pd_idx == raid_disks-1)
3179 i--; /* Q D D D P */
3180 else {
3181 /* D D P Q D */
3182 if (i < sh->pd_idx)
3183 i += raid_disks;
3184 i -= (sh->pd_idx + 2);
3185 }
3186 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11003187 case ALGORITHM_PARITY_0:
3188 i -= 2;
3189 break;
3190 case ALGORITHM_PARITY_N:
3191 break;
3192 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11003193 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11003194 if (sh->pd_idx == 0)
3195 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11003196 else {
3197 /* D D Q P D */
3198 if (i < sh->pd_idx)
3199 i += raid_disks;
3200 i -= (sh->pd_idx + 1);
3201 }
NeilBrown99c0fb52009-03-31 14:39:38 +11003202 break;
3203 case ALGORITHM_LEFT_ASYMMETRIC_6:
3204 case ALGORITHM_RIGHT_ASYMMETRIC_6:
3205 if (i > sh->pd_idx)
3206 i--;
3207 break;
3208 case ALGORITHM_LEFT_SYMMETRIC_6:
3209 case ALGORITHM_RIGHT_SYMMETRIC_6:
3210 if (i < sh->pd_idx)
3211 i += data_disks + 1;
3212 i -= (sh->pd_idx + 1);
3213 break;
3214 case ALGORITHM_PARITY_0_6:
3215 i -= 1;
3216 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07003217 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11003218 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07003219 }
3220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 }
3222
3223 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10003224 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
NeilBrown112bf892009-03-31 14:39:38 +11003226 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11003227 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11003228 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
3229 || sh2.qd_idx != sh->qd_idx) {
NeilBrowncc6167b2016-11-02 14:16:50 +11003230 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
3231 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 return 0;
3233 }
3234 return r_sector;
3235}
3236
Song Liu07e83362017-01-23 17:12:58 -08003237/*
3238 * There are cases where we want handle_stripe_dirtying() and
3239 * schedule_reconstruction() to delay towrite to some dev of a stripe.
3240 *
3241 * This function checks whether we want to delay the towrite. Specifically,
3242 * we delay the towrite when:
3243 *
3244 * 1. degraded stripe has a non-overwrite to the missing dev, AND this
3245 * stripe has data in journal (for other devices).
3246 *
3247 * In this case, when reading data for the non-overwrite dev, it is
3248 * necessary to handle complex rmw of write back cache (prexor with
3249 * orig_page, and xor with page). To keep read path simple, we would
3250 * like to flush data in journal to RAID disks first, so complex rmw
3251 * is handled in the write patch (handle_stripe_dirtying).
3252 *
Song Liu39b99582017-01-24 14:08:23 -08003253 * 2. when journal space is critical (R5C_LOG_CRITICAL=1)
3254 *
3255 * It is important to be able to flush all stripes in raid5-cache.
3256 * Therefore, we need reserve some space on the journal device for
3257 * these flushes. If flush operation includes pending writes to the
3258 * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe
3259 * for the flush out. If we exclude these pending writes from flush
3260 * operation, we only need (conf->max_degraded + 1) pages per stripe.
3261 * Therefore, excluding pending writes in these cases enables more
3262 * efficient use of the journal device.
3263 *
3264 * Note: To make sure the stripe makes progress, we only delay
3265 * towrite for stripes with data already in journal (injournal > 0).
3266 * When LOG_CRITICAL, stripes with injournal == 0 will be sent to
3267 * no_space_stripes list.
3268 *
Song Liu70d466f2017-05-11 15:28:28 -07003269 * 3. during journal failure
3270 * In journal failure, we try to flush all cached data to raid disks
3271 * based on data in stripe cache. The array is read-only to upper
3272 * layers, so we would skip all pending writes.
3273 *
Song Liu07e83362017-01-23 17:12:58 -08003274 */
Song Liu39b99582017-01-24 14:08:23 -08003275static inline bool delay_towrite(struct r5conf *conf,
3276 struct r5dev *dev,
3277 struct stripe_head_state *s)
Song Liu07e83362017-01-23 17:12:58 -08003278{
Song Liu39b99582017-01-24 14:08:23 -08003279 /* case 1 above */
3280 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3281 !test_bit(R5_Insync, &dev->flags) && s->injournal)
3282 return true;
3283 /* case 2 above */
3284 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
3285 s->injournal > 0)
3286 return true;
Song Liu70d466f2017-05-11 15:28:28 -07003287 /* case 3 above */
3288 if (s->log_failed && s->injournal)
3289 return true;
Song Liu39b99582017-01-24 14:08:23 -08003290 return false;
Song Liu07e83362017-01-23 17:12:58 -08003291}
3292
Dan Williams600aa102008-06-28 08:32:05 +10003293static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003294schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10003295 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07003296{
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003297 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11003298 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003299 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07003300
Dan Williamse33129d2007-01-02 13:52:30 -07003301 if (rcw) {
Song Liu1e6d6902016-11-17 15:24:39 -08003302 /*
3303 * In some cases, handle_stripe_dirtying initially decided to
3304 * run rmw and allocates extra page for prexor. However, rcw is
3305 * cheaper later on. We need to free the extra page now,
3306 * because we won't be able to do that in ops_complete_prexor().
3307 */
3308 r5c_release_extra_page(sh);
Dan Williamse33129d2007-01-02 13:52:30 -07003309
3310 for (i = disks; i--; ) {
3311 struct r5dev *dev = &sh->dev[i];
3312
Song Liu39b99582017-01-24 14:08:23 -08003313 if (dev->towrite && !delay_towrite(conf, dev, s)) {
Dan Williamse33129d2007-01-02 13:52:30 -07003314 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10003315 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003316 if (!expand)
3317 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003318 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003319 } else if (test_bit(R5_InJournal, &dev->flags)) {
3320 set_bit(R5_LOCKED, &dev->flags);
3321 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003322 }
3323 }
NeilBrownce7d3632013-03-04 12:37:14 +11003324 /* if we are not expanding this is a proper write request, and
3325 * there will be bios with new data to be drained into the
3326 * stripe cache
3327 */
3328 if (!expand) {
3329 if (!s->locked)
3330 /* False alarm, nothing to do */
3331 return;
3332 sh->reconstruct_state = reconstruct_state_drain_run;
3333 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3334 } else
3335 sh->reconstruct_state = reconstruct_state_run;
3336
3337 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3338
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003339 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003340 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003341 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07003342 } else {
3343 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
3344 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003345 BUG_ON(level == 6 &&
3346 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
3347 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
Dan Williamse33129d2007-01-02 13:52:30 -07003348
Dan Williamse33129d2007-01-02 13:52:30 -07003349 for (i = disks; i--; ) {
3350 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003351 if (i == pd_idx || i == qd_idx)
Dan Williamse33129d2007-01-02 13:52:30 -07003352 continue;
3353
Dan Williamse33129d2007-01-02 13:52:30 -07003354 if (dev->towrite &&
3355 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10003356 test_bit(R5_Wantcompute, &dev->flags))) {
3357 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003358 set_bit(R5_LOCKED, &dev->flags);
3359 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003360 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003361 } else if (test_bit(R5_InJournal, &dev->flags)) {
3362 set_bit(R5_LOCKED, &dev->flags);
3363 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003364 }
3365 }
NeilBrownce7d3632013-03-04 12:37:14 +11003366 if (!s->locked)
3367 /* False alarm - nothing to do */
3368 return;
3369 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
3370 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
3371 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3372 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003373 }
3374
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003375 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07003376 * are in flight
3377 */
3378 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
3379 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10003380 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003381
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003382 if (level == 6) {
3383 int qd_idx = sh->qd_idx;
3384 struct r5dev *dev = &sh->dev[qd_idx];
3385
3386 set_bit(R5_LOCKED, &dev->flags);
3387 clear_bit(R5_UPTODATE, &dev->flags);
3388 s->locked++;
3389 }
3390
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02003391 if (raid5_has_ppl(sh->raid_conf) && sh->ppl_page &&
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01003392 test_bit(STRIPE_OP_BIODRAIN, &s->ops_request) &&
3393 !test_bit(STRIPE_FULL_WRITE, &sh->state) &&
3394 test_bit(R5_Insync, &sh->dev[pd_idx].flags))
3395 set_bit(STRIPE_OP_PARTIAL_PARITY, &s->ops_request);
3396
Dan Williams600aa102008-06-28 08:32:05 +10003397 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07003398 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10003399 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003400}
NeilBrown16a53ec2006-06-26 00:27:38 -07003401
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402/*
3403 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07003404 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 * The bi_next chain must be in order.
3406 */
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003407static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
3408 int forwrite, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
3410 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11003411 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07003412 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
NeilBrowncbe47ec2011-07-26 11:20:35 +10003414 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003415 (unsigned long long)bi->bi_iter.bi_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 (unsigned long long)sh->sector);
3417
Shaohua Lib17459c2012-07-19 16:01:31 +10003418 spin_lock_irq(&sh->stripe_lock);
Mariusz Dabrowski2cd259a2018-04-19 19:28:10 +02003419 sh->dev[dd_idx].write_hint = bi->bi_write_hint;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003420 /* Don't allow new IO added to stripes in batch list */
3421 if (sh->batch_head)
3422 goto overlap;
NeilBrown72626682005-09-09 16:23:54 -07003423 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003425 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07003426 firstwrite = 1;
3427 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 bip = &sh->dev[dd_idx].toread;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003429 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
3430 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 goto overlap;
3432 bip = & (*bip)->bi_next;
3433 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07003434 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 goto overlap;
3436
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01003437 if (forwrite && raid5_has_ppl(conf)) {
3438 /*
3439 * With PPL only writes to consecutive data chunks within a
3440 * stripe are allowed because for a single stripe_head we can
3441 * only have one PPL entry at a time, which describes one data
3442 * range. Not really an overlap, but wait_for_overlap can be
3443 * used to handle this.
3444 */
3445 sector_t sector;
3446 sector_t first = 0;
3447 sector_t last = 0;
3448 int count = 0;
3449 int i;
3450
3451 for (i = 0; i < sh->disks; i++) {
3452 if (i != sh->pd_idx &&
3453 (i == dd_idx || sh->dev[i].towrite)) {
3454 sector = sh->dev[i].sector;
3455 if (count == 0 || sector < first)
3456 first = sector;
3457 if (sector > last)
3458 last = sector;
3459 count++;
3460 }
3461 }
3462
3463 if (first + conf->chunk_sectors * (count - 1) != last)
3464 goto overlap;
3465 }
3466
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003467 if (!forwrite || previous)
3468 clear_bit(STRIPE_BATCH_READY, &sh->state);
3469
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02003470 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 if (*bip)
3472 bi->bi_next = *bip;
3473 *bip = bi;
NeilBrown016c76a2017-03-15 14:05:13 +11003474 bio_inc_remaining(bi);
NeilBrown49728052017-03-15 14:05:12 +11003475 md_write_inc(conf->mddev, bi);
NeilBrown72626682005-09-09 16:23:54 -07003476
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 if (forwrite) {
3478 /* check if page is covered */
3479 sector_t sector = sh->dev[dd_idx].sector;
3480 for (bi=sh->dev[dd_idx].towrite;
Yufen Yuc911c462020-07-18 05:29:07 -04003481 sector < sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07003482 bi && bi->bi_iter.bi_sector <= sector;
Yufen Yuc911c462020-07-18 05:29:07 -04003483 bi = r5_next_bio(conf, bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07003484 if (bio_end_sector(bi) >= sector)
3485 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 }
Yufen Yuc911c462020-07-18 05:29:07 -04003487 if (sector >= sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf))
shli@kernel.org7a87f432014-12-15 12:57:03 +11003488 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
3489 sh->overwrite_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003491
3492 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003493 (unsigned long long)(*bip)->bi_iter.bi_sector,
NeilBrowncbe47ec2011-07-26 11:20:35 +10003494 (unsigned long long)sh->sector, dd_idx);
3495
3496 if (conf->mddev->bitmap && firstwrite) {
NeilBrownd0852df52015-05-27 08:43:45 +10003497 /* Cannot hold spinlock over bitmap_startwrite,
3498 * but must ensure this isn't added to a batch until
3499 * we have added to the bitmap and set bm_seq.
3500 * So set STRIPE_BITMAP_PENDING to prevent
3501 * batching.
3502 * If multiple add_stripe_bio() calls race here they
3503 * much all set STRIPE_BITMAP_PENDING. So only the first one
3504 * to complete "bitmap_startwrite" gets to set
3505 * STRIPE_BIT_DELAY. This is important as once a stripe
3506 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3507 * any more.
3508 */
3509 set_bit(STRIPE_BITMAP_PENDING, &sh->state);
3510 spin_unlock_irq(&sh->stripe_lock);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003511 md_bitmap_startwrite(conf->mddev->bitmap, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003512 RAID5_STRIPE_SECTORS(conf), 0);
NeilBrownd0852df52015-05-27 08:43:45 +10003513 spin_lock_irq(&sh->stripe_lock);
3514 clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
3515 if (!sh->batch_head) {
3516 sh->bm_seq = conf->seq_flush+1;
3517 set_bit(STRIPE_BIT_DELAY, &sh->state);
3518 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003519 }
NeilBrownd0852df52015-05-27 08:43:45 +10003520 spin_unlock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003521
3522 if (stripe_can_batch(sh))
3523 stripe_add_to_batch_list(conf, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 return 1;
3525
3526 overlap:
3527 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10003528 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 return 0;
3530}
3531
NeilBrownd1688a62011-10-11 16:49:52 +11003532static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08003533
NeilBrownd1688a62011-10-11 16:49:52 +11003534static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003535 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08003536{
NeilBrown784052e2009-03-31 15:19:07 +11003537 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10003538 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11003539 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003540 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11003541 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003542
NeilBrown112bf892009-03-31 14:39:38 +11003543 raid5_compute_sector(conf,
3544 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08003545 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11003546 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003547 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003548}
3549
Dan Williamsa4456852007-07-09 11:56:43 -07003550static void
NeilBrownd1688a62011-10-11 16:49:52 +11003551handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
NeilBrownbd83d0a2017-03-15 14:05:12 +11003552 struct stripe_head_state *s, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003553{
3554 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003555 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003556 for (i = disks; i--; ) {
3557 struct bio *bi;
3558 int bitmap_end = 0;
3559
3560 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003561 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07003562 rcu_read_lock();
3563 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownf5b67ae2016-06-02 16:19:53 +10003564 if (rdev && test_bit(In_sync, &rdev->flags) &&
3565 !test_bit(Faulty, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10003566 atomic_inc(&rdev->nr_pending);
3567 else
3568 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003569 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10003570 if (rdev) {
3571 if (!rdev_set_badblocks(
3572 rdev,
3573 sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003574 RAID5_STRIPE_SECTORS(conf), 0))
NeilBrown7f0da592011-07-28 11:39:22 +10003575 md_error(conf->mddev, rdev);
3576 rdev_dec_pending(rdev, conf->mddev);
3577 }
Dan Williamsa4456852007-07-09 11:56:43 -07003578 }
Shaohua Lib17459c2012-07-19 16:01:31 +10003579 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003580 /* fail all writes first */
3581 bi = sh->dev[i].towrite;
3582 sh->dev[i].towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11003583 sh->overwrite_disks = 0;
Shaohua Lib17459c2012-07-19 16:01:31 +10003584 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11003585 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003586 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07003587
Artur Paszkiewiczff875732017-03-09 09:59:58 +01003588 log_stripe_write_finished(sh);
Shaohua Li0576b1c2015-08-13 14:32:00 -07003589
Dan Williamsa4456852007-07-09 11:56:43 -07003590 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3591 wake_up(&conf->wait_for_overlap);
3592
Kent Overstreet4f024f32013-10-11 15:44:27 -07003593 while (bi && bi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04003594 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3595 struct bio *nextbi = r5_next_bio(conf, bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003596
NeilBrown49728052017-03-15 14:05:12 +11003597 md_write_end(conf->mddev);
Guoqing Jiang6308d8e2017-07-21 16:33:44 +08003598 bio_io_error(bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003599 bi = nextbi;
3600 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003601 if (bitmap_end)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003602 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003603 RAID5_STRIPE_SECTORS(conf), 0, 0);
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003604 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003605 /* and fail all 'written' */
3606 bi = sh->dev[i].written;
3607 sh->dev[i].written = NULL;
Shaohua Lid592a992014-05-21 17:57:44 +08003608 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3609 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3610 sh->dev[i].page = sh->dev[i].orig_page;
3611 }
3612
Dan Williamsa4456852007-07-09 11:56:43 -07003613 if (bi) bitmap_end = 1;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003614 while (bi && bi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04003615 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3616 struct bio *bi2 = r5_next_bio(conf, bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003617
NeilBrown49728052017-03-15 14:05:12 +11003618 md_write_end(conf->mddev);
Guoqing Jiang6308d8e2017-07-21 16:33:44 +08003619 bio_io_error(bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003620 bi = bi2;
3621 }
3622
Dan Williamsb5e98d62007-01-02 13:52:31 -07003623 /* fail any reads if this device is non-operational and
3624 * the data has not reached the cache yet.
3625 */
3626 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
Shaohua Li6e74a9c2015-10-08 21:54:08 -07003627 s->failed > conf->max_degraded &&
Dan Williamsb5e98d62007-01-02 13:52:31 -07003628 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3629 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11003630 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003631 bi = sh->dev[i].toread;
3632 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11003633 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003634 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3635 wake_up(&conf->wait_for_overlap);
Shaohua Liebda7802015-09-18 10:20:13 -07003636 if (bi)
3637 s->to_read--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003638 while (bi && bi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04003639 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003640 struct bio *nextbi =
Yufen Yuc911c462020-07-18 05:29:07 -04003641 r5_next_bio(conf, bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003642
Guoqing Jiang6308d8e2017-07-21 16:33:44 +08003643 bio_io_error(bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003644 bi = nextbi;
3645 }
3646 }
Dan Williamsa4456852007-07-09 11:56:43 -07003647 if (bitmap_end)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003648 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003649 RAID5_STRIPE_SECTORS(conf), 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10003650 /* If we were in the middle of a write the parity block might
3651 * still be locked - so just clear all R5_LOCKED flags
3652 */
3653 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003654 }
Shaohua Liebda7802015-09-18 10:20:13 -07003655 s->to_write = 0;
3656 s->written = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003657
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003658 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3659 if (atomic_dec_and_test(&conf->pending_full_writes))
3660 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003661}
3662
NeilBrown7f0da592011-07-28 11:39:22 +10003663static void
NeilBrownd1688a62011-10-11 16:49:52 +11003664handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10003665 struct stripe_head_state *s)
3666{
3667 int abort = 0;
3668 int i;
3669
shli@kernel.org59fc6302014-12-15 12:57:03 +11003670 BUG_ON(sh->batch_head);
NeilBrown7f0da592011-07-28 11:39:22 +10003671 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003672 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3673 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10003674 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11003675 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10003676 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10003677 * Don't even need to abort as that is handled elsewhere
3678 * if needed, and not always wanted e.g. if there is a known
3679 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11003680 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10003681 * non-sync devices, or abort the recovery
3682 */
NeilBrown18b98372012-04-01 23:48:38 +10003683 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3684 /* During recovery devices cannot be removed, so
3685 * locking and refcounting of rdevs is not needed
3686 */
NeilBrowne50d3992016-06-02 16:19:52 +10003687 rcu_read_lock();
NeilBrown18b98372012-04-01 23:48:38 +10003688 for (i = 0; i < conf->raid_disks; i++) {
NeilBrowne50d3992016-06-02 16:19:52 +10003689 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown18b98372012-04-01 23:48:38 +10003690 if (rdev
3691 && !test_bit(Faulty, &rdev->flags)
3692 && !test_bit(In_sync, &rdev->flags)
3693 && !rdev_set_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003694 RAID5_STRIPE_SECTORS(conf), 0))
NeilBrown18b98372012-04-01 23:48:38 +10003695 abort = 1;
NeilBrowne50d3992016-06-02 16:19:52 +10003696 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown18b98372012-04-01 23:48:38 +10003697 if (rdev
3698 && !test_bit(Faulty, &rdev->flags)
3699 && !test_bit(In_sync, &rdev->flags)
3700 && !rdev_set_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003701 RAID5_STRIPE_SECTORS(conf), 0))
NeilBrown18b98372012-04-01 23:48:38 +10003702 abort = 1;
3703 }
NeilBrowne50d3992016-06-02 16:19:52 +10003704 rcu_read_unlock();
NeilBrown18b98372012-04-01 23:48:38 +10003705 if (abort)
3706 conf->recovery_disabled =
3707 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10003708 }
Yufen Yuc911c462020-07-18 05:29:07 -04003709 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10003710}
3711
NeilBrown9a3e1102011-12-23 10:17:53 +11003712static int want_replace(struct stripe_head *sh, int disk_idx)
3713{
3714 struct md_rdev *rdev;
3715 int rv = 0;
NeilBrown3f232d62016-06-02 16:19:52 +10003716
3717 rcu_read_lock();
3718 rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
NeilBrown9a3e1102011-12-23 10:17:53 +11003719 if (rdev
3720 && !test_bit(Faulty, &rdev->flags)
3721 && !test_bit(In_sync, &rdev->flags)
3722 && (rdev->recovery_offset <= sh->sector
3723 || rdev->mddev->recovery_cp <= sh->sector))
3724 rv = 1;
NeilBrown3f232d62016-06-02 16:19:52 +10003725 rcu_read_unlock();
NeilBrown9a3e1102011-12-23 10:17:53 +11003726 return rv;
3727}
3728
NeilBrown2c58f062015-02-02 11:32:23 +11003729static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3730 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07003731{
3732 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10003733 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3734 &sh->dev[s->failed_num[1]] };
NeilBrownea664c82015-02-02 14:03:28 +11003735 int i;
ChangSyun Peng45a4d8f2020-07-31 17:50:31 +08003736 bool force_rcw = (sh->raid_conf->rmw_level == PARITY_DISABLE_RMW);
Dan Williamsf38e1212007-01-02 13:52:30 -07003737
NeilBrowna79cfe12015-02-02 11:37:59 +11003738
3739 if (test_bit(R5_LOCKED, &dev->flags) ||
3740 test_bit(R5_UPTODATE, &dev->flags))
3741 /* No point reading this as we already have it or have
3742 * decided to get it.
3743 */
3744 return 0;
3745
3746 if (dev->toread ||
3747 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3748 /* We need this block to directly satisfy a request */
3749 return 1;
3750
3751 if (s->syncing || s->expanding ||
3752 (s->replacing && want_replace(sh, disk_idx)))
3753 /* When syncing, or expanding we read everything.
3754 * When replacing, we need the replaced block.
3755 */
3756 return 1;
3757
3758 if ((s->failed >= 1 && fdev[0]->toread) ||
3759 (s->failed >= 2 && fdev[1]->toread))
3760 /* If we want to read from a failed device, then
3761 * we need to actually read every other device.
3762 */
3763 return 1;
3764
NeilBrowna9d56952015-02-02 11:49:10 +11003765 /* Sometimes neither read-modify-write nor reconstruct-write
3766 * cycles can work. In those cases we read every block we
3767 * can. Then the parity-update is certain to have enough to
3768 * work with.
3769 * This can only be a problem when we need to write something,
3770 * and some device has failed. If either of those tests
3771 * fail we need look no further.
3772 */
3773 if (!s->failed || !s->to_write)
3774 return 0;
3775
3776 if (test_bit(R5_Insync, &dev->flags) &&
3777 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3778 /* Pre-reads at not permitted until after short delay
3779 * to gather multiple requests. However if this
Zhilong Liu3560741e2017-03-15 16:14:53 +08003780 * device is no Insync, the block could only be computed
NeilBrowna9d56952015-02-02 11:49:10 +11003781 * and there is no need to delay that.
3782 */
3783 return 0;
NeilBrownea664c82015-02-02 14:03:28 +11003784
NeilBrown36707bb2015-09-24 15:25:36 +10003785 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrownea664c82015-02-02 14:03:28 +11003786 if (fdev[i]->towrite &&
3787 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3788 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3789 /* If we have a partial write to a failed
3790 * device, then we will need to reconstruct
3791 * the content of that device, so all other
3792 * devices must be read.
3793 */
3794 return 1;
ChangSyun Peng45a4d8f2020-07-31 17:50:31 +08003795
3796 if (s->failed >= 2 &&
3797 (fdev[i]->towrite ||
3798 s->failed_num[i] == sh->pd_idx ||
3799 s->failed_num[i] == sh->qd_idx) &&
3800 !test_bit(R5_UPTODATE, &fdev[i]->flags))
3801 /* In max degraded raid6, If the failed disk is P, Q,
3802 * or we want to read the failed disk, we need to do
3803 * reconstruct-write.
3804 */
3805 force_rcw = true;
NeilBrownea664c82015-02-02 14:03:28 +11003806 }
3807
ChangSyun Peng45a4d8f2020-07-31 17:50:31 +08003808 /* If we are forced to do a reconstruct-write, because parity
3809 * cannot be trusted and we are currently recovering it, there
3810 * is extra need to be careful.
NeilBrownea664c82015-02-02 14:03:28 +11003811 * If one of the devices that we would need to read, because
3812 * it is not being overwritten (and maybe not written at all)
3813 * is missing/faulty, then we need to read everything we can.
3814 */
ChangSyun Peng45a4d8f2020-07-31 17:50:31 +08003815 if (!force_rcw &&
NeilBrownea664c82015-02-02 14:03:28 +11003816 sh->sector < sh->raid_conf->mddev->recovery_cp)
3817 /* reconstruct-write isn't being forced */
3818 return 0;
NeilBrown36707bb2015-09-24 15:25:36 +10003819 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrown10d82c52015-05-08 18:19:33 +10003820 if (s->failed_num[i] != sh->pd_idx &&
3821 s->failed_num[i] != sh->qd_idx &&
3822 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
NeilBrownea664c82015-02-02 14:03:28 +11003823 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3824 return 1;
3825 }
3826
NeilBrown2c58f062015-02-02 11:32:23 +11003827 return 0;
3828}
3829
Song Liuba026842017-01-12 17:22:42 -08003830/* fetch_block - checks the given member device to see if its data needs
3831 * to be read or computed to satisfy a request.
3832 *
3833 * Returns 1 when no more member devices need to be checked, otherwise returns
3834 * 0 to tell the loop in handle_stripe_fill to continue
3835 */
NeilBrown2c58f062015-02-02 11:32:23 +11003836static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3837 int disk_idx, int disks)
3838{
3839 struct r5dev *dev = &sh->dev[disk_idx];
3840
3841 /* is the data in this block needed, and can we get it? */
3842 if (need_this_block(sh, s, disk_idx, disks)) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003843 /* we would like to get this block, possibly by computing it,
3844 * otherwise read it if the backing disk is insync
3845 */
3846 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3847 BUG_ON(test_bit(R5_Wantread, &dev->flags));
NeilBrownb0c783b2015-05-08 18:19:32 +10003848 BUG_ON(sh->batch_head);
NeilBrown7471fb72017-04-03 12:11:32 +10003849
3850 /*
3851 * In the raid6 case if the only non-uptodate disk is P
3852 * then we already trusted P to compute the other failed
3853 * drives. It is safe to compute rather than re-read P.
3854 * In other cases we only compute blocks from failed
3855 * devices, otherwise check/repair might fail to detect
3856 * a real inconsistency.
3857 */
3858
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003859 if ((s->uptodate == disks - 1) &&
NeilBrown7471fb72017-04-03 12:11:32 +10003860 ((sh->qd_idx >= 0 && sh->pd_idx == disk_idx) ||
NeilBrownf2b3b442011-07-26 11:35:19 +10003861 (s->failed && (disk_idx == s->failed_num[0] ||
NeilBrown7471fb72017-04-03 12:11:32 +10003862 disk_idx == s->failed_num[1])))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003863 /* have disk failed, and we're requested to fetch it;
3864 * do compute it
3865 */
3866 pr_debug("Computing stripe %llu block %d\n",
3867 (unsigned long long)sh->sector, disk_idx);
3868 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3869 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3870 set_bit(R5_Wantcompute, &dev->flags);
3871 sh->ops.target = disk_idx;
3872 sh->ops.target2 = -1; /* no 2nd target */
3873 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10003874 /* Careful: from this point on 'uptodate' is in the eye
3875 * of raid_run_ops which services 'compute' operations
3876 * before writes. R5_Wantcompute flags a block that will
3877 * be R5_UPTODATE by the time it is needed for a
3878 * subsequent operation.
3879 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003880 s->uptodate++;
3881 return 1;
3882 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3883 /* Computing 2-failure is *very* expensive; only
3884 * do it if failed >= 2
3885 */
3886 int other;
3887 for (other = disks; other--; ) {
3888 if (other == disk_idx)
3889 continue;
3890 if (!test_bit(R5_UPTODATE,
3891 &sh->dev[other].flags))
3892 break;
3893 }
3894 BUG_ON(other < 0);
3895 pr_debug("Computing stripe %llu blocks %d,%d\n",
3896 (unsigned long long)sh->sector,
3897 disk_idx, other);
3898 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3899 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3900 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3901 set_bit(R5_Wantcompute, &sh->dev[other].flags);
3902 sh->ops.target = disk_idx;
3903 sh->ops.target2 = other;
3904 s->uptodate += 2;
3905 s->req_compute = 1;
3906 return 1;
3907 } else if (test_bit(R5_Insync, &dev->flags)) {
3908 set_bit(R5_LOCKED, &dev->flags);
3909 set_bit(R5_Wantread, &dev->flags);
3910 s->locked++;
3911 pr_debug("Reading block %d (sync=%d)\n",
3912 disk_idx, s->syncing);
3913 }
3914 }
3915
3916 return 0;
3917}
3918
Damien Le Moal2aada5b2020-07-16 13:54:42 +09003919/*
NeilBrown93b3dbc2011-07-27 11:00:36 +10003920 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003921 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10003922static void handle_stripe_fill(struct stripe_head *sh,
3923 struct stripe_head_state *s,
3924 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003925{
3926 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003927
3928 /* look for blocks to read/compute, skip this if a compute
3929 * is already in flight, or if the stripe contents are in the
3930 * midst of changing due to a write
3931 */
3932 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Song Liu07e83362017-01-23 17:12:58 -08003933 !sh->reconstruct_state) {
3934
3935 /*
3936 * For degraded stripe with data in journal, do not handle
3937 * read requests yet, instead, flush the stripe to raid
3938 * disks first, this avoids handling complex rmw of write
3939 * back cache (prexor with orig_page, and then xor with
3940 * page) in the read path
3941 */
3942 if (s->injournal && s->failed) {
3943 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
3944 r5c_make_stripe_write_out(sh);
3945 goto out;
3946 }
3947
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003948 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10003949 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003950 break;
Song Liu07e83362017-01-23 17:12:58 -08003951 }
3952out:
Dan Williamsa4456852007-07-09 11:56:43 -07003953 set_bit(STRIPE_HANDLE, &sh->state);
3954}
3955
NeilBrown787b76f2015-05-21 12:56:41 +10003956static void break_stripe_batch_list(struct stripe_head *head_sh,
3957 unsigned long handle_flags);
Dan Williams1fe797e2008-06-28 09:16:30 +10003958/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07003959 * any written block on an uptodate or failed drive can be returned.
3960 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3961 * never LOCKED, so we don't need to test 'failed' directly.
3962 */
NeilBrownd1688a62011-10-11 16:49:52 +11003963static void handle_stripe_clean_event(struct r5conf *conf,
NeilBrownbd83d0a2017-03-15 14:05:12 +11003964 struct stripe_head *sh, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003965{
3966 int i;
3967 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003968 int discard_pending = 0;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003969 struct stripe_head *head_sh = sh;
3970 bool do_endio = false;
Dan Williamsa4456852007-07-09 11:56:43 -07003971
3972 for (i = disks; i--; )
3973 if (sh->dev[i].written) {
3974 dev = &sh->dev[i];
3975 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003976 (test_bit(R5_UPTODATE, &dev->flags) ||
Shaohua Lid592a992014-05-21 17:57:44 +08003977 test_bit(R5_Discard, &dev->flags) ||
3978 test_bit(R5_SkipCopy, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003979 /* We can return any write requests */
3980 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003981 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003982 if (test_and_clear_bit(R5_Discard, &dev->flags))
3983 clear_bit(R5_UPTODATE, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08003984 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3985 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
Shaohua Lid592a992014-05-21 17:57:44 +08003986 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003987 do_endio = true;
3988
3989returnbi:
3990 dev->page = dev->orig_page;
Dan Williamsa4456852007-07-09 11:56:43 -07003991 wbi = dev->written;
3992 dev->written = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003993 while (wbi && wbi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04003994 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
3995 wbi2 = r5_next_bio(conf, wbi, dev->sector);
NeilBrown49728052017-03-15 14:05:12 +11003996 md_write_end(conf->mddev);
NeilBrown016c76a2017-03-15 14:05:13 +11003997 bio_endio(wbi);
Dan Williamsa4456852007-07-09 11:56:43 -07003998 wbi = wbi2;
3999 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004000 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04004001 RAID5_STRIPE_SECTORS(conf),
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07004002 !test_bit(STRIPE_DEGRADED, &sh->state),
4003 0);
shli@kernel.org59fc6302014-12-15 12:57:03 +11004004 if (head_sh->batch_head) {
4005 sh = list_first_entry(&sh->batch_list,
4006 struct stripe_head,
4007 batch_list);
4008 if (sh != head_sh) {
4009 dev = &sh->dev[i];
4010 goto returnbi;
4011 }
4012 }
4013 sh = head_sh;
4014 dev = &sh->dev[i];
NeilBrownf8dfcff2013-03-12 12:18:06 +11004015 } else if (test_bit(R5_Discard, &dev->flags))
4016 discard_pending = 1;
4017 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -07004018
Artur Paszkiewiczff875732017-03-09 09:59:58 +01004019 log_stripe_write_finished(sh);
Shaohua Li0576b1c2015-08-13 14:32:00 -07004020
NeilBrownf8dfcff2013-03-12 12:18:06 +11004021 if (!discard_pending &&
4022 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
Roman Gushchinb8a9d662015-10-31 10:53:50 +11004023 int hash;
NeilBrownf8dfcff2013-03-12 12:18:06 +11004024 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
4025 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
4026 if (sh->qd_idx >= 0) {
4027 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
4028 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
4029 }
4030 /* now that discard is done we can proceed with any sync */
4031 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08004032 /*
4033 * SCSI discard will change some bio fields and the stripe has
4034 * no updated data, so remove it from hash list and the stripe
4035 * will be reinitialized
4036 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11004037unhash:
Roman Gushchinb8a9d662015-10-31 10:53:50 +11004038 hash = sh->hash_lock_index;
4039 spin_lock_irq(conf->hash_locks + hash);
Shaohua Lid47648f2013-10-19 14:51:42 +08004040 remove_hash(sh);
Roman Gushchinb8a9d662015-10-31 10:53:50 +11004041 spin_unlock_irq(conf->hash_locks + hash);
shli@kernel.org59fc6302014-12-15 12:57:03 +11004042 if (head_sh->batch_head) {
4043 sh = list_first_entry(&sh->batch_list,
4044 struct stripe_head, batch_list);
4045 if (sh != head_sh)
4046 goto unhash;
4047 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11004048 sh = head_sh;
4049
NeilBrownf8dfcff2013-03-12 12:18:06 +11004050 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
4051 set_bit(STRIPE_HANDLE, &sh->state);
4052
4053 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004054
4055 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
4056 if (atomic_dec_and_test(&conf->pending_full_writes))
4057 md_wakeup_thread(conf->mddev->thread);
shli@kernel.org59fc6302014-12-15 12:57:03 +11004058
NeilBrown787b76f2015-05-21 12:56:41 +10004059 if (head_sh->batch_head && do_endio)
4060 break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
Dan Williamsa4456852007-07-09 11:56:43 -07004061}
4062
Song Liu86aa1392017-01-12 17:22:41 -08004063/*
4064 * For RMW in write back cache, we need extra page in prexor to store the
4065 * old data. This page is stored in dev->orig_page.
4066 *
4067 * This function checks whether we have data for prexor. The exact logic
4068 * is:
4069 * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
4070 */
4071static inline bool uptodate_for_rmw(struct r5dev *dev)
4072{
4073 return (test_bit(R5_UPTODATE, &dev->flags)) &&
4074 (!test_bit(R5_InJournal, &dev->flags) ||
4075 test_bit(R5_OrigPageUPTDODATE, &dev->flags));
4076}
4077
Song Liud7bd3982016-11-23 22:50:39 -08004078static int handle_stripe_dirtying(struct r5conf *conf,
4079 struct stripe_head *sh,
4080 struct stripe_head_state *s,
4081 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07004082{
4083 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11004084 sector_t recovery_cp = conf->mddev->recovery_cp;
4085
Markus Stockhausen584acdd2014-12-15 12:57:05 +11004086 /* Check whether resync is now happening or should start.
Alexander Lyakasa7854482012-10-11 13:50:12 +11004087 * If yes, then the array is dirty (after unclean shutdown or
4088 * initial creation), so parity in some stripes might be inconsistent.
4089 * In this case, we need to always do reconstruct-write, to ensure
4090 * that in case of drive failure or read-error correction, we
4091 * generate correct data from the parity.
4092 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11004093 if (conf->rmw_level == PARITY_DISABLE_RMW ||
NeilBrown26ac1072015-02-18 11:35:14 +11004094 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
4095 s->failed == 0)) {
Alexander Lyakasa7854482012-10-11 13:50:12 +11004096 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10004097 * look like rcw is cheaper
4098 */
4099 rcw = 1; rmw = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11004100 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
4101 conf->rmw_level, (unsigned long long)recovery_cp,
Alexander Lyakasa7854482012-10-11 13:50:12 +11004102 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10004103 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07004104 /* would I have to read this buffer for read_modify_write */
4105 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08004106 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu07e83362017-01-23 17:12:58 -08004107 i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08004108 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07004109 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08004110 !(uptodate_for_rmw(dev) ||
Dan Williamsf38e1212007-01-02 13:52:30 -07004111 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07004112 if (test_bit(R5_Insync, &dev->flags))
4113 rmw++;
4114 else
4115 rmw += 2*disks; /* cannot read it */
4116 }
4117 /* Would I have to read this buffer for reconstruct_write */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11004118 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
4119 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07004120 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07004121 !(test_bit(R5_UPTODATE, &dev->flags) ||
Song Liu1e6d6902016-11-17 15:24:39 -08004122 test_bit(R5_Wantcompute, &dev->flags))) {
NeilBrown67f45542014-05-28 13:39:22 +10004123 if (test_bit(R5_Insync, &dev->flags))
4124 rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07004125 else
4126 rcw += 2*disks;
4127 }
4128 }
Song Liu1e6d6902016-11-17 15:24:39 -08004129
Song Liu39b99582017-01-24 14:08:23 -08004130 pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n",
4131 (unsigned long long)sh->sector, sh->state, rmw, rcw);
Dan Williamsa4456852007-07-09 11:56:43 -07004132 set_bit(STRIPE_HANDLE, &sh->state);
Song Liu41257582016-05-23 17:25:06 -07004133 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07004134 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004135 if (conf->mddev->queue)
4136 blk_add_trace_msg(conf->mddev->queue,
4137 "raid5 rmw %llu %d",
4138 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07004139 for (i = disks; i--; ) {
4140 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08004141 if (test_bit(R5_InJournal, &dev->flags) &&
4142 dev->page == dev->orig_page &&
4143 !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) {
4144 /* alloc page for prexor */
Song Liud7bd3982016-11-23 22:50:39 -08004145 struct page *p = alloc_page(GFP_NOIO);
Song Liu1e6d6902016-11-17 15:24:39 -08004146
Song Liud7bd3982016-11-23 22:50:39 -08004147 if (p) {
4148 dev->orig_page = p;
4149 continue;
4150 }
4151
4152 /*
4153 * alloc_page() failed, try use
4154 * disk_info->extra_page
4155 */
4156 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE,
4157 &conf->cache_state)) {
4158 r5c_use_extra_page(sh);
4159 break;
4160 }
4161
4162 /* extra_page in use, add to delayed_list */
4163 set_bit(STRIPE_DELAYED, &sh->state);
4164 s->waiting_extra_page = 1;
4165 return -EAGAIN;
Song Liu1e6d6902016-11-17 15:24:39 -08004166 }
Song Liud7bd3982016-11-23 22:50:39 -08004167 }
Song Liu1e6d6902016-11-17 15:24:39 -08004168
Song Liud7bd3982016-11-23 22:50:39 -08004169 for (i = disks; i--; ) {
4170 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08004171 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu1e6d6902016-11-17 15:24:39 -08004172 i == sh->pd_idx || i == sh->qd_idx ||
4173 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07004174 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08004175 !(uptodate_for_rmw(dev) ||
Song Liu1e6d6902016-11-17 15:24:39 -08004176 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07004177 test_bit(R5_Insync, &dev->flags)) {
NeilBrown67f45542014-05-28 13:39:22 +10004178 if (test_bit(STRIPE_PREREAD_ACTIVE,
4179 &sh->state)) {
4180 pr_debug("Read_old block %d for r-m-w\n",
4181 i);
Dan Williamsa4456852007-07-09 11:56:43 -07004182 set_bit(R5_LOCKED, &dev->flags);
4183 set_bit(R5_Wantread, &dev->flags);
4184 s->locked++;
Guoqing Jiange3914d52020-07-28 12:01:40 +02004185 } else
Dan Williamsa4456852007-07-09 11:56:43 -07004186 set_bit(STRIPE_DELAYED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07004187 }
4188 }
NeilBrowna9add5d2012-10-31 11:59:09 +11004189 }
Song Liu41257582016-05-23 17:25:06 -07004190 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07004191 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11004192 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10004193 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07004194 for (i = disks; i--; ) {
4195 struct r5dev *dev = &sh->dev[i];
4196 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10004197 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07004198 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07004199 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10004200 test_bit(R5_Wantcompute, &dev->flags))) {
4201 rcw++;
NeilBrown67f45542014-05-28 13:39:22 +10004202 if (test_bit(R5_Insync, &dev->flags) &&
4203 test_bit(STRIPE_PREREAD_ACTIVE,
4204 &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07004205 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07004206 "%d for Reconstruct\n", i);
4207 set_bit(R5_LOCKED, &dev->flags);
4208 set_bit(R5_Wantread, &dev->flags);
4209 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11004210 qread++;
Guoqing Jiange3914d52020-07-28 12:01:40 +02004211 } else
Dan Williamsa4456852007-07-09 11:56:43 -07004212 set_bit(STRIPE_DELAYED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07004213 }
4214 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004215 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11004216 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
4217 (unsigned long long)sh->sector,
4218 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10004219 }
NeilBrownb1b02fe2015-02-02 10:44:29 +11004220
4221 if (rcw > disks && rmw > disks &&
4222 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4223 set_bit(STRIPE_DELAYED, &sh->state);
4224
Dan Williamsa4456852007-07-09 11:56:43 -07004225 /* now if nothing is locked, and if we have enough data,
4226 * we can start a write request
4227 */
Dan Williamsf38e1212007-01-02 13:52:30 -07004228 /* since handle_stripe can be called at any time we need to handle the
4229 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07004230 * subsequent call wants to start a write request. raid_run_ops only
4231 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07004232 * simultaneously. If this is not the case then new writes need to be
4233 * held off until the compute completes.
4234 */
Dan Williams976ea8d2008-06-28 08:32:03 +10004235 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
4236 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
Song Liu1e6d6902016-11-17 15:24:39 -08004237 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07004238 schedule_reconstruction(sh, s, rcw == 0, 0);
Song Liud7bd3982016-11-23 22:50:39 -08004239 return 0;
Dan Williamsa4456852007-07-09 11:56:43 -07004240}
4241
NeilBrownd1688a62011-10-11 16:49:52 +11004242static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07004243 struct stripe_head_state *s, int disks)
4244{
Dan Williamsecc65c92008-06-28 08:31:57 +10004245 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07004246
shli@kernel.org59fc6302014-12-15 12:57:03 +11004247 BUG_ON(sh->batch_head);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004248 set_bit(STRIPE_HANDLE, &sh->state);
4249
Dan Williamsecc65c92008-06-28 08:31:57 +10004250 switch (sh->check_state) {
4251 case check_state_idle:
4252 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07004253 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07004254 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10004255 sh->check_state = check_state_run;
4256 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004257 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004258 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10004259 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07004260 }
NeilBrownf2b3b442011-07-26 11:35:19 +10004261 dev = &sh->dev[s->failed_num[0]];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004262 fallthrough;
Dan Williamsecc65c92008-06-28 08:31:57 +10004263 case check_state_compute_result:
4264 sh->check_state = check_state_idle;
4265 if (!dev)
4266 dev = &sh->dev[sh->pd_idx];
4267
4268 /* check that a write has not made the stripe insync */
4269 if (test_bit(STRIPE_INSYNC, &sh->state))
4270 break;
4271
4272 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07004273 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
4274 BUG_ON(s->uptodate != disks);
4275
4276 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10004277 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07004278 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07004279
Dan Williamsa4456852007-07-09 11:56:43 -07004280 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07004281 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10004282 break;
4283 case check_state_run:
4284 break; /* we will be called again upon completion */
4285 case check_state_check_result:
4286 sh->check_state = check_state_idle;
4287
4288 /* if a failure occurred during the check operation, leave
4289 * STRIPE_INSYNC not set and let the stripe be handled again
4290 */
4291 if (s->failed)
4292 break;
4293
4294 /* handle a successful check operation, if parity is correct
4295 * we are done. Otherwise update the mismatch count and repair
4296 * parity if !MD_RECOVERY_CHECK
4297 */
Dan Williamsad283ea2009-08-29 19:09:26 -07004298 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10004299 /* parity is correct (on disc,
4300 * not in buffer any more)
4301 */
4302 set_bit(STRIPE_INSYNC, &sh->state);
4303 else {
Yufen Yuc911c462020-07-18 05:29:07 -04004304 atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches);
Nixe1539032017-05-16 10:13:31 +01004305 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
Dan Williamsecc65c92008-06-28 08:31:57 +10004306 /* don't try to repair!! */
4307 set_bit(STRIPE_INSYNC, &sh->state);
Nixe1539032017-05-16 10:13:31 +01004308 pr_warn_ratelimited("%s: mismatch sector in range "
4309 "%llu-%llu\n", mdname(conf->mddev),
4310 (unsigned long long) sh->sector,
4311 (unsigned long long) sh->sector +
Yufen Yuc911c462020-07-18 05:29:07 -04004312 RAID5_STRIPE_SECTORS(conf));
Nixe1539032017-05-16 10:13:31 +01004313 } else {
Dan Williamsecc65c92008-06-28 08:31:57 +10004314 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10004315 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10004316 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4317 set_bit(R5_Wantcompute,
4318 &sh->dev[sh->pd_idx].flags);
4319 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07004320 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10004321 s->uptodate++;
4322 }
4323 }
4324 break;
4325 case check_state_compute_run:
4326 break;
4327 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11004328 pr_err("%s: unknown check_state: %d sector: %llu\n",
Dan Williamsecc65c92008-06-28 08:31:57 +10004329 __func__, sh->check_state,
4330 (unsigned long long) sh->sector);
4331 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07004332 }
4333}
4334
NeilBrownd1688a62011-10-11 16:49:52 +11004335static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07004336 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10004337 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07004338{
Dan Williamsa4456852007-07-09 11:56:43 -07004339 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11004340 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07004341 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07004342
shli@kernel.org59fc6302014-12-15 12:57:03 +11004343 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07004344 set_bit(STRIPE_HANDLE, &sh->state);
4345
4346 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004347
Dan Williamsa4456852007-07-09 11:56:43 -07004348 /* Want to check and possibly repair P and Q.
4349 * However there could be one 'failed' device, in which
4350 * case we can only check one of them, possibly using the
4351 * other to generate missing data
4352 */
4353
Dan Williamsd82dfee2009-07-14 13:40:57 -07004354 switch (sh->check_state) {
4355 case check_state_idle:
4356 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10004357 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004358 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07004359 * makes sense to check P (If anything else were failed,
4360 * we would have used P to recreate it).
4361 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004362 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07004363 }
NeilBrownf2b3b442011-07-26 11:35:19 +10004364 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004365 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07004366 * anything, so it makes sense to check it
4367 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004368 if (sh->check_state == check_state_run)
4369 sh->check_state = check_state_run_pq;
4370 else
4371 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07004372 }
Dan Williams36d1c642009-07-14 11:48:22 -07004373
Dan Williamsd82dfee2009-07-14 13:40:57 -07004374 /* discard potentially stale zero_sum_result */
4375 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07004376
Dan Williamsd82dfee2009-07-14 13:40:57 -07004377 if (sh->check_state == check_state_run) {
4378 /* async_xor_zero_sum destroys the contents of P */
4379 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
4380 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07004381 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004382 if (sh->check_state >= check_state_run &&
4383 sh->check_state <= check_state_run_pq) {
4384 /* async_syndrome_zero_sum preserves P and Q, so
4385 * no need to mark them !uptodate here
4386 */
4387 set_bit(STRIPE_OP_CHECK, &s->ops_request);
4388 break;
4389 }
Dan Williams36d1c642009-07-14 11:48:22 -07004390
Dan Williamsd82dfee2009-07-14 13:40:57 -07004391 /* we have 2-disk failure */
4392 BUG_ON(s->failed != 2);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004393 fallthrough;
Dan Williamsd82dfee2009-07-14 13:40:57 -07004394 case check_state_compute_result:
4395 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07004396
Dan Williamsd82dfee2009-07-14 13:40:57 -07004397 /* check that a write has not made the stripe insync */
4398 if (test_bit(STRIPE_INSYNC, &sh->state))
4399 break;
Dan Williamsa4456852007-07-09 11:56:43 -07004400
4401 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07004402 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07004403 */
Nigel Croxonb2176a12019-04-16 09:50:09 -07004404 dev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07004405 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004406 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07004407 s->locked++;
4408 set_bit(R5_LOCKED, &dev->flags);
4409 set_bit(R5_Wantwrite, &dev->flags);
4410 }
4411 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004412 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07004413 s->locked++;
4414 set_bit(R5_LOCKED, &dev->flags);
4415 set_bit(R5_Wantwrite, &dev->flags);
4416 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004417 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004418 dev = &sh->dev[pd_idx];
4419 s->locked++;
4420 set_bit(R5_LOCKED, &dev->flags);
4421 set_bit(R5_Wantwrite, &dev->flags);
4422 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004423 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004424 dev = &sh->dev[qd_idx];
4425 s->locked++;
4426 set_bit(R5_LOCKED, &dev->flags);
4427 set_bit(R5_Wantwrite, &dev->flags);
4428 }
Nigel Croxonb2176a12019-04-16 09:50:09 -07004429 if (WARN_ONCE(dev && !test_bit(R5_UPTODATE, &dev->flags),
4430 "%s: disk%td not up to date\n",
4431 mdname(conf->mddev),
4432 dev - (struct r5dev *) &sh->dev)) {
4433 clear_bit(R5_LOCKED, &dev->flags);
4434 clear_bit(R5_Wantwrite, &dev->flags);
4435 s->locked--;
4436 }
Dan Williamsa4456852007-07-09 11:56:43 -07004437 clear_bit(STRIPE_DEGRADED, &sh->state);
4438
4439 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004440 break;
4441 case check_state_run:
4442 case check_state_run_q:
4443 case check_state_run_pq:
4444 break; /* we will be called again upon completion */
4445 case check_state_check_result:
4446 sh->check_state = check_state_idle;
4447
4448 /* handle a successful check operation, if parity is correct
4449 * we are done. Otherwise update the mismatch count and repair
4450 * parity if !MD_RECOVERY_CHECK
4451 */
4452 if (sh->ops.zero_sum_result == 0) {
Song Liua25d8c32019-04-16 09:34:21 -07004453 /* both parities are correct */
4454 if (!s->failed)
4455 set_bit(STRIPE_INSYNC, &sh->state);
4456 else {
4457 /* in contrast to the raid5 case we can validate
4458 * parity, but still have a failure to write
4459 * back
4460 */
4461 sh->check_state = check_state_compute_result;
4462 /* Returning at this point means that we may go
4463 * off and bring p and/or q uptodate again so
4464 * we make sure to check zero_sum_result again
4465 * to verify if p or q need writeback
4466 */
4467 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004468 } else {
Yufen Yuc911c462020-07-18 05:29:07 -04004469 atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches);
Nixe1539032017-05-16 10:13:31 +01004470 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004471 /* don't try to repair!! */
4472 set_bit(STRIPE_INSYNC, &sh->state);
Nixe1539032017-05-16 10:13:31 +01004473 pr_warn_ratelimited("%s: mismatch sector in range "
4474 "%llu-%llu\n", mdname(conf->mddev),
4475 (unsigned long long) sh->sector,
4476 (unsigned long long) sh->sector +
Yufen Yuc911c462020-07-18 05:29:07 -04004477 RAID5_STRIPE_SECTORS(conf));
Nixe1539032017-05-16 10:13:31 +01004478 } else {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004479 int *target = &sh->ops.target;
4480
4481 sh->ops.target = -1;
4482 sh->ops.target2 = -1;
4483 sh->check_state = check_state_compute_run;
4484 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4485 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4486 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4487 set_bit(R5_Wantcompute,
4488 &sh->dev[pd_idx].flags);
4489 *target = pd_idx;
4490 target = &sh->ops.target2;
4491 s->uptodate++;
4492 }
4493 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4494 set_bit(R5_Wantcompute,
4495 &sh->dev[qd_idx].flags);
4496 *target = qd_idx;
4497 s->uptodate++;
4498 }
4499 }
4500 }
4501 break;
4502 case check_state_compute_run:
4503 break;
4504 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11004505 pr_warn("%s: unknown check_state: %d sector: %llu\n",
4506 __func__, sh->check_state,
4507 (unsigned long long) sh->sector);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004508 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07004509 }
4510}
4511
NeilBrownd1688a62011-10-11 16:49:52 +11004512static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07004513{
4514 int i;
4515
4516 /* We have read all the blocks in this stripe and now we need to
4517 * copy some of them into a target stripe for expand.
4518 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07004519 struct dma_async_tx_descriptor *tx = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +11004520 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07004521 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4522 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11004523 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11004524 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07004525 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07004526 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07004527
Shaohua Li6d036f72015-08-13 14:31:57 -07004528 sector_t bn = raid5_compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11004529 sector_t s = raid5_compute_sector(conf, bn, 0,
4530 &dd_idx, NULL);
Shaohua Li6d036f72015-08-13 14:31:57 -07004531 sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07004532 if (sh2 == NULL)
4533 /* so far only the early blocks of this stripe
4534 * have been requested. When later blocks
4535 * get requested, we will try again
4536 */
4537 continue;
4538 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
4539 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
4540 /* must have already done this block */
Shaohua Li6d036f72015-08-13 14:31:57 -07004541 raid5_release_stripe(sh2);
Dan Williamsa4456852007-07-09 11:56:43 -07004542 continue;
4543 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07004544
4545 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07004546 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004547 tx = async_memcpy(sh2->dev[dd_idx].page,
Yufen Yu7aba13b2020-08-20 09:22:06 -04004548 sh->dev[i].page, sh2->dev[dd_idx].offset,
4549 sh->dev[i].offset, RAID5_STRIPE_SIZE(conf),
Dan Williamsa08abd82009-06-03 11:43:59 -07004550 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004551
Dan Williamsa4456852007-07-09 11:56:43 -07004552 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
4553 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
4554 for (j = 0; j < conf->raid_disks; j++)
4555 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10004556 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07004557 !test_bit(R5_Expanded, &sh2->dev[j].flags))
4558 break;
4559 if (j == conf->raid_disks) {
4560 set_bit(STRIPE_EXPAND_READY, &sh2->state);
4561 set_bit(STRIPE_HANDLE, &sh2->state);
4562 }
Shaohua Li6d036f72015-08-13 14:31:57 -07004563 raid5_release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004564
Dan Williamsa4456852007-07-09 11:56:43 -07004565 }
NeilBrowna2e08552007-09-11 15:23:36 -07004566 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11004567 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07004568}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
4570/*
4571 * handle_stripe - do things to a stripe.
4572 *
NeilBrown9a3e1102011-12-23 10:17:53 +11004573 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4574 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11004576 * return some read requests which now have data
4577 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 * schedule a read on some buffers
4579 * schedule a write of some buffers
4580 * return confirmation of parity correctness
4581 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 */
Dan Williamsa4456852007-07-09 11:56:43 -07004583
NeilBrownacfe7262011-07-27 11:00:36 +10004584static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07004585{
NeilBrownd1688a62011-10-11 16:49:52 +11004586 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08004587 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004588 struct r5dev *dev;
4589 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11004590 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07004591
NeilBrownacfe7262011-07-27 11:00:36 +10004592 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07004593
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004594 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
4595 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
NeilBrownacfe7262011-07-27 11:00:36 +10004596 s->failed_num[0] = -1;
4597 s->failed_num[1] = -1;
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004598 s->log_failed = r5l_log_disk_error(conf);
NeilBrownacfe7262011-07-27 11:00:36 +10004599
4600 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07004601 rcu_read_lock();
4602 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004603 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10004604 sector_t first_bad;
4605 int bad_sectors;
4606 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10004607
NeilBrown16a53ec2006-06-26 00:27:38 -07004608 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07004609
Dan Williams45b42332007-07-09 11:56:43 -07004610 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11004611 i, dev->flags,
4612 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004613 /* maybe we can reply to a read
4614 *
4615 * new wantfill requests are only permitted while
4616 * ops_complete_biofill is guaranteed to be inactive
4617 */
4618 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4619 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4620 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07004621
4622 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10004623 if (test_bit(R5_LOCKED, &dev->flags))
4624 s->locked++;
4625 if (test_bit(R5_UPTODATE, &dev->flags))
4626 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004627 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004628 s->compute++;
4629 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004630 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004631
NeilBrownacfe7262011-07-27 11:00:36 +10004632 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004633 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10004634 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10004635 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004636 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10004637 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004638 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004639 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004640 }
Dan Williamsa4456852007-07-09 11:56:43 -07004641 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10004642 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11004643 /* Prefer to use the replacement for reads, but only
4644 * if it is recovered enough and has no bad blocks.
4645 */
4646 rdev = rcu_dereference(conf->disks[i].replacement);
4647 if (rdev && !test_bit(Faulty, &rdev->flags) &&
Yufen Yuc911c462020-07-18 05:29:07 -04004648 rdev->recovery_offset >= sh->sector + RAID5_STRIPE_SECTORS(conf) &&
4649 !is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
NeilBrown14a75d32011-12-23 10:17:52 +11004650 &first_bad, &bad_sectors))
4651 set_bit(R5_ReadRepl, &dev->flags);
4652 else {
NeilBrowne6030cb2015-07-17 13:26:23 +10004653 if (rdev && !test_bit(Faulty, &rdev->flags))
NeilBrown9a3e1102011-12-23 10:17:53 +11004654 set_bit(R5_NeedReplace, &dev->flags);
NeilBrowne6030cb2015-07-17 13:26:23 +10004655 else
4656 clear_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11004657 rdev = rcu_dereference(conf->disks[i].rdev);
4658 clear_bit(R5_ReadRepl, &dev->flags);
4659 }
NeilBrown9283d8c2011-12-08 16:27:57 +11004660 if (rdev && test_bit(Faulty, &rdev->flags))
4661 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10004662 if (rdev) {
Yufen Yuc911c462020-07-18 05:29:07 -04004663 is_bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
NeilBrown31c176e2011-07-28 11:39:22 +10004664 &first_bad, &bad_sectors);
4665 if (s->blocked_rdev == NULL
4666 && (test_bit(Blocked, &rdev->flags)
4667 || is_bad < 0)) {
4668 if (is_bad < 0)
4669 set_bit(BlockedBadBlocks,
4670 &rdev->flags);
4671 s->blocked_rdev = rdev;
4672 atomic_inc(&rdev->nr_pending);
4673 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004674 }
NeilBrown415e72d2010-06-17 17:25:21 +10004675 clear_bit(R5_Insync, &dev->flags);
4676 if (!rdev)
4677 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10004678 else if (is_bad) {
4679 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10004680 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4681 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10004682 /* treat as in-sync, but with a read error
4683 * which we can now try to correct
4684 */
4685 set_bit(R5_Insync, &dev->flags);
4686 set_bit(R5_ReadError, &dev->flags);
4687 }
4688 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10004689 set_bit(R5_Insync, &dev->flags);
Yufen Yuc911c462020-07-18 05:29:07 -04004690 else if (sh->sector + RAID5_STRIPE_SECTORS(conf) <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10004691 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11004692 set_bit(R5_Insync, &dev->flags);
4693 else if (test_bit(R5_UPTODATE, &dev->flags) &&
4694 test_bit(R5_Expanded, &dev->flags))
4695 /* If we've reshaped into here, we assume it is Insync.
4696 * We will shortly update recovery_offset to make
4697 * it official.
4698 */
4699 set_bit(R5_Insync, &dev->flags);
4700
NeilBrown1cc03eb2014-01-06 13:19:42 +11004701 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004702 /* This flag does not apply to '.replacement'
4703 * only to .rdev, so make sure to check that*/
4704 struct md_rdev *rdev2 = rcu_dereference(
4705 conf->disks[i].rdev);
4706 if (rdev2 == rdev)
4707 clear_bit(R5_Insync, &dev->flags);
4708 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004709 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004710 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10004711 } else
4712 clear_bit(R5_WriteError, &dev->flags);
4713 }
NeilBrown1cc03eb2014-01-06 13:19:42 +11004714 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004715 /* This flag does not apply to '.replacement'
4716 * only to .rdev, so make sure to check that*/
4717 struct md_rdev *rdev2 = rcu_dereference(
4718 conf->disks[i].rdev);
4719 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10004720 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004721 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10004722 } else
4723 clear_bit(R5_MadeGood, &dev->flags);
4724 }
NeilBrown977df362011-12-23 10:17:53 +11004725 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4726 struct md_rdev *rdev2 = rcu_dereference(
4727 conf->disks[i].replacement);
4728 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4729 s->handle_bad_blocks = 1;
4730 atomic_inc(&rdev2->nr_pending);
4731 } else
4732 clear_bit(R5_MadeGoodRepl, &dev->flags);
4733 }
NeilBrown415e72d2010-06-17 17:25:21 +10004734 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004735 /* The ReadError flag will just be confusing now */
4736 clear_bit(R5_ReadError, &dev->flags);
4737 clear_bit(R5_ReWrite, &dev->flags);
4738 }
NeilBrown415e72d2010-06-17 17:25:21 +10004739 if (test_bit(R5_ReadError, &dev->flags))
4740 clear_bit(R5_Insync, &dev->flags);
4741 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004742 if (s->failed < 2)
4743 s->failed_num[s->failed] = i;
4744 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11004745 if (rdev && !test_bit(Faulty, &rdev->flags))
4746 do_recovery = 1;
BingJing Changd63e2fc2018-08-01 17:08:36 +08004747 else if (!rdev) {
4748 rdev = rcu_dereference(
4749 conf->disks[i].replacement);
4750 if (rdev && !test_bit(Faulty, &rdev->flags))
4751 do_recovery = 1;
4752 }
NeilBrown415e72d2010-06-17 17:25:21 +10004753 }
Song Liu2ded3702016-11-17 15:24:38 -08004754
4755 if (test_bit(R5_InJournal, &dev->flags))
4756 s->injournal++;
Song Liu1e6d6902016-11-17 15:24:39 -08004757 if (test_bit(R5_InJournal, &dev->flags) && dev->written)
4758 s->just_cached++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004759 }
NeilBrown9a3e1102011-12-23 10:17:53 +11004760 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4761 /* If there is a failed device being replaced,
4762 * we must be recovering.
4763 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10004764 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11004765 * else we can only be replacing
4766 * sync and recovery both need to read all devices, and so
4767 * use the same flag.
4768 */
4769 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10004770 sh->sector >= conf->mddev->recovery_cp ||
4771 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11004772 s->syncing = 1;
4773 else
4774 s->replacing = 1;
4775 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004776 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10004777}
NeilBrownf4168852007-02-28 20:11:53 -08004778
Guoqing Jiangcb9902d2020-06-16 11:25:51 +02004779/*
4780 * Return '1' if this is a member of batch, or '0' if it is a lone stripe or
4781 * a head which can now be handled.
4782 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11004783static int clear_batch_ready(struct stripe_head *sh)
4784{
4785 struct stripe_head *tmp;
4786 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
NeilBrownb15a9db2015-05-22 15:20:04 +10004787 return (sh->batch_head && sh->batch_head != sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11004788 spin_lock(&sh->stripe_lock);
4789 if (!sh->batch_head) {
4790 spin_unlock(&sh->stripe_lock);
4791 return 0;
4792 }
4793
4794 /*
4795 * this stripe could be added to a batch list before we check
4796 * BATCH_READY, skips it
4797 */
4798 if (sh->batch_head != sh) {
4799 spin_unlock(&sh->stripe_lock);
4800 return 1;
4801 }
4802 spin_lock(&sh->batch_lock);
4803 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4804 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4805 spin_unlock(&sh->batch_lock);
4806 spin_unlock(&sh->stripe_lock);
4807
4808 /*
4809 * BATCH_READY is cleared, no new stripes can be added.
4810 * batch_list can be accessed without lock
4811 */
4812 return 0;
4813}
4814
NeilBrown3960ce72015-05-21 12:20:36 +10004815static void break_stripe_batch_list(struct stripe_head *head_sh,
4816 unsigned long handle_flags)
shli@kernel.org72ac7332014-12-15 12:57:03 +11004817{
NeilBrown4e3d62f2015-05-21 11:50:16 +10004818 struct stripe_head *sh, *next;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004819 int i;
NeilBrownfb642b92015-05-21 12:00:47 +10004820 int do_wakeup = 0;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004821
NeilBrownbb270512015-05-08 18:19:40 +10004822 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4823
shli@kernel.org72ac7332014-12-15 12:57:03 +11004824 list_del_init(&sh->batch_list);
4825
Shaohua Lifb3229d2016-03-09 10:08:38 -08004826 WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
NeilBrown1b956f72015-05-21 12:40:26 +10004827 (1 << STRIPE_SYNCING) |
4828 (1 << STRIPE_REPLACED) |
NeilBrown1b956f72015-05-21 12:40:26 +10004829 (1 << STRIPE_DELAYED) |
4830 (1 << STRIPE_BIT_DELAY) |
4831 (1 << STRIPE_FULL_WRITE) |
4832 (1 << STRIPE_BIOFILL_RUN) |
4833 (1 << STRIPE_COMPUTE_RUN) |
NeilBrown1b956f72015-05-21 12:40:26 +10004834 (1 << STRIPE_DISCARD) |
4835 (1 << STRIPE_BATCH_READY) |
4836 (1 << STRIPE_BATCH_ERR) |
Shaohua Lifb3229d2016-03-09 10:08:38 -08004837 (1 << STRIPE_BITMAP_PENDING)),
4838 "stripe state: %lx\n", sh->state);
4839 WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4840 (1 << STRIPE_REPLACED)),
4841 "head stripe state: %lx\n", head_sh->state);
NeilBrown1b956f72015-05-21 12:40:26 +10004842
4843 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
NeilBrown550da242016-03-09 12:58:25 +11004844 (1 << STRIPE_PREREAD_ACTIVE) |
Dennis Yang184a09e2017-09-06 11:02:35 +08004845 (1 << STRIPE_DEGRADED) |
4846 (1 << STRIPE_ON_UNPLUG_LIST)),
NeilBrown1b956f72015-05-21 12:40:26 +10004847 head_sh->state & (1 << STRIPE_INSYNC));
4848
shli@kernel.org72ac7332014-12-15 12:57:03 +11004849 sh->check_state = head_sh->check_state;
4850 sh->reconstruct_state = head_sh->reconstruct_state;
Amy Chiang448ec632018-05-16 18:59:35 +08004851 spin_lock_irq(&sh->stripe_lock);
4852 sh->batch_head = NULL;
4853 spin_unlock_irq(&sh->stripe_lock);
NeilBrownfb642b92015-05-21 12:00:47 +10004854 for (i = 0; i < sh->disks; i++) {
4855 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4856 do_wakeup = 1;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004857 sh->dev[i].flags = head_sh->dev[i].flags &
4858 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
NeilBrownfb642b92015-05-21 12:00:47 +10004859 }
NeilBrown3960ce72015-05-21 12:20:36 +10004860 if (handle_flags == 0 ||
4861 sh->state & handle_flags)
4862 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07004863 raid5_release_stripe(sh);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004864 }
NeilBrownfb642b92015-05-21 12:00:47 +10004865 spin_lock_irq(&head_sh->stripe_lock);
4866 head_sh->batch_head = NULL;
4867 spin_unlock_irq(&head_sh->stripe_lock);
4868 for (i = 0; i < head_sh->disks; i++)
4869 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4870 do_wakeup = 1;
NeilBrown3960ce72015-05-21 12:20:36 +10004871 if (head_sh->state & handle_flags)
4872 set_bit(STRIPE_HANDLE, &head_sh->state);
NeilBrownfb642b92015-05-21 12:00:47 +10004873
4874 if (do_wakeup)
4875 wake_up(&head_sh->raid_conf->wait_for_overlap);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004876}
4877
NeilBrowncc940152011-07-26 11:35:35 +10004878static void handle_stripe(struct stripe_head *sh)
4879{
4880 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11004881 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10004882 int i;
NeilBrown84789552011-07-27 11:00:36 +10004883 int prexor;
4884 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004885 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10004886
4887 clear_bit(STRIPE_HANDLE, &sh->state);
Guoqing Jianga377a472020-06-16 11:25:50 +02004888
4889 /*
4890 * handle_stripe should not continue handle the batched stripe, only
4891 * the head of batch list or lone stripe can continue. Otherwise we
4892 * could see break_stripe_batch_list warns about the STRIPE_ACTIVE
4893 * is set for the batched stripe.
4894 */
4895 if (clear_batch_ready(sh))
4896 return;
4897
Dan Williams257a4b42011-11-08 16:22:06 +11004898 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10004899 /* already being handled, ensure it gets handled
4900 * again when current action finishes */
4901 set_bit(STRIPE_HANDLE, &sh->state);
4902 return;
4903 }
4904
NeilBrown4e3d62f2015-05-21 11:50:16 +10004905 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
NeilBrown3960ce72015-05-21 12:20:36 +10004906 break_stripe_batch_list(sh, 0);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004907
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004908 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
NeilBrownf8dfcff2013-03-12 12:18:06 +11004909 spin_lock(&sh->stripe_lock);
Song Liu5ddf0442017-05-11 17:03:44 -07004910 /*
4911 * Cannot process 'sync' concurrently with 'discard'.
4912 * Flush data in r5cache before 'sync'.
4913 */
4914 if (!test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) &&
4915 !test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) &&
4916 !test_bit(STRIPE_DISCARD, &sh->state) &&
NeilBrownf8dfcff2013-03-12 12:18:06 +11004917 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4918 set_bit(STRIPE_SYNCING, &sh->state);
4919 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10004920 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004921 }
4922 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10004923 }
4924 clear_bit(STRIPE_DELAYED, &sh->state);
4925
4926 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4927 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4928 (unsigned long long)sh->sector, sh->state,
4929 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4930 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10004931
NeilBrownacfe7262011-07-27 11:00:36 +10004932 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10004933
Shaohua Lib70abcb2015-08-13 14:31:58 -07004934 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
4935 goto finish;
4936
NeilBrown16d997b2017-03-15 14:05:12 +11004937 if (s.handle_bad_blocks ||
4938 test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004939 set_bit(STRIPE_HANDLE, &sh->state);
4940 goto finish;
4941 }
4942
NeilBrown474af965fe2011-07-27 11:00:36 +10004943 if (unlikely(s.blocked_rdev)) {
4944 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11004945 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10004946 set_bit(STRIPE_HANDLE, &sh->state);
4947 goto finish;
4948 }
4949 /* There is nothing for the blocked_rdev to block */
4950 rdev_dec_pending(s.blocked_rdev, conf->mddev);
4951 s.blocked_rdev = NULL;
4952 }
4953
4954 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4955 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4956 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4957 }
4958
4959 pr_debug("locked=%d uptodate=%d to_read=%d"
4960 " to_write=%d failed=%d failed_num=%d,%d\n",
4961 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4962 s.failed_num[0], s.failed_num[1]);
Song Liu70d466f2017-05-11 15:28:28 -07004963 /*
4964 * check if the array has lost more than max_degraded devices and,
NeilBrown474af965fe2011-07-27 11:00:36 +10004965 * if so, some requests might need to be failed.
Song Liu70d466f2017-05-11 15:28:28 -07004966 *
4967 * When journal device failed (log_failed), we will only process
4968 * the stripe if there is data need write to raid disks
NeilBrown474af965fe2011-07-27 11:00:36 +10004969 */
Song Liu70d466f2017-05-11 15:28:28 -07004970 if (s.failed > conf->max_degraded ||
4971 (s.log_failed && s.injournal == 0)) {
NeilBrown9a3f5302011-11-08 16:22:01 +11004972 sh->check_state = 0;
4973 sh->reconstruct_state = 0;
NeilBrown626f2092015-05-22 14:03:10 +10004974 break_stripe_batch_list(sh, 0);
NeilBrown9a3f5302011-11-08 16:22:01 +11004975 if (s.to_read+s.to_write+s.written)
NeilBrownbd83d0a2017-03-15 14:05:12 +11004976 handle_failed_stripe(conf, sh, &s, disks);
NeilBrown9a3e1102011-12-23 10:17:53 +11004977 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11004978 handle_failed_sync(conf, sh, &s);
4979 }
NeilBrown474af965fe2011-07-27 11:00:36 +10004980
NeilBrown84789552011-07-27 11:00:36 +10004981 /* Now we check to see if any write operations have recently
4982 * completed
4983 */
4984 prexor = 0;
4985 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4986 prexor = 1;
4987 if (sh->reconstruct_state == reconstruct_state_drain_result ||
4988 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
4989 sh->reconstruct_state = reconstruct_state_idle;
4990
4991 /* All the 'written' buffers and the parity block are ready to
4992 * be written back to disk
4993 */
Shaohua Li9e4447682012-10-11 13:49:49 +11004994 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
4995 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004996 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11004997 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
4998 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004999 for (i = disks; i--; ) {
5000 struct r5dev *dev = &sh->dev[i];
5001 if (test_bit(R5_LOCKED, &dev->flags) &&
5002 (i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08005003 dev->written || test_bit(R5_InJournal,
5004 &dev->flags))) {
NeilBrown84789552011-07-27 11:00:36 +10005005 pr_debug("Writing block %d\n", i);
5006 set_bit(R5_Wantwrite, &dev->flags);
5007 if (prexor)
5008 continue;
NeilBrown9c4bdf62014-08-13 09:57:07 +10005009 if (s.failed > 1)
5010 continue;
NeilBrown84789552011-07-27 11:00:36 +10005011 if (!test_bit(R5_Insync, &dev->flags) ||
5012 ((i == sh->pd_idx || i == sh->qd_idx) &&
5013 s.failed == 0))
5014 set_bit(STRIPE_INSYNC, &sh->state);
5015 }
5016 }
5017 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5018 s.dec_preread_active = 1;
5019 }
5020
NeilBrownef5b7c62012-11-22 09:13:36 +11005021 /*
5022 * might be able to return some write requests if the parity blocks
5023 * are safe, or on a failed drive
5024 */
5025 pdev = &sh->dev[sh->pd_idx];
5026 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
5027 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
5028 qdev = &sh->dev[sh->qd_idx];
5029 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
5030 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
5031 || conf->level < 6;
5032
5033 if (s.written &&
5034 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
5035 && !test_bit(R5_LOCKED, &pdev->flags)
5036 && (test_bit(R5_UPTODATE, &pdev->flags) ||
5037 test_bit(R5_Discard, &pdev->flags))))) &&
5038 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
5039 && !test_bit(R5_LOCKED, &qdev->flags)
5040 && (test_bit(R5_UPTODATE, &qdev->flags) ||
5041 test_bit(R5_Discard, &qdev->flags))))))
NeilBrownbd83d0a2017-03-15 14:05:12 +11005042 handle_stripe_clean_event(conf, sh, disks);
NeilBrownef5b7c62012-11-22 09:13:36 +11005043
Song Liu1e6d6902016-11-17 15:24:39 -08005044 if (s.just_cached)
NeilBrownbd83d0a2017-03-15 14:05:12 +11005045 r5c_handle_cached_data_endio(conf, sh, disks);
Artur Paszkiewiczff875732017-03-09 09:59:58 +01005046 log_stripe_write_finished(sh);
Song Liu1e6d6902016-11-17 15:24:39 -08005047
NeilBrownef5b7c62012-11-22 09:13:36 +11005048 /* Now we might consider reading some blocks, either to check/generate
5049 * parity, or to satisfy requests
5050 * or to load a block that is being partially written.
5051 */
5052 if (s.to_read || s.non_overwrite
ChangSyun Penga1c6ae32020-07-31 17:50:17 +08005053 || (s.to_write && s.failed)
NeilBrownef5b7c62012-11-22 09:13:36 +11005054 || (s.syncing && (s.uptodate + s.compute < disks))
5055 || s.replacing
5056 || s.expanding)
5057 handle_stripe_fill(sh, &s, disks);
5058
Song Liu2ded3702016-11-17 15:24:38 -08005059 /*
5060 * When the stripe finishes full journal write cycle (write to journal
5061 * and raid disk), this is the clean up procedure so it is ready for
5062 * next operation.
5063 */
5064 r5c_finish_stripe_write_out(conf, sh, &s);
5065
5066 /*
5067 * Now to consider new write requests, cache write back and what else,
5068 * if anything should be read. We do not handle new writes when:
NeilBrown84789552011-07-27 11:00:36 +10005069 * 1/ A 'write' operation (copy+xor) is already in flight.
5070 * 2/ A 'check' operation is in flight, as it may clobber the parity
5071 * block.
Song Liu2ded3702016-11-17 15:24:38 -08005072 * 3/ A r5c cache log write is in flight.
NeilBrown84789552011-07-27 11:00:36 +10005073 */
Song Liu2ded3702016-11-17 15:24:38 -08005074
5075 if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
5076 if (!r5c_is_writeback(conf->log)) {
5077 if (s.to_write)
5078 handle_stripe_dirtying(conf, sh, &s, disks);
5079 } else { /* write back cache */
5080 int ret = 0;
5081
5082 /* First, try handle writes in caching phase */
5083 if (s.to_write)
5084 ret = r5c_try_caching_write(conf, sh, &s,
5085 disks);
5086 /*
5087 * If caching phase failed: ret == -EAGAIN
5088 * OR
5089 * stripe under reclaim: !caching && injournal
5090 *
5091 * fall back to handle_stripe_dirtying()
5092 */
5093 if (ret == -EAGAIN ||
5094 /* stripe under reclaim: !caching && injournal */
5095 (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
Song Liud7bd3982016-11-23 22:50:39 -08005096 s.injournal > 0)) {
5097 ret = handle_stripe_dirtying(conf, sh, &s,
5098 disks);
5099 if (ret == -EAGAIN)
5100 goto finish;
5101 }
Song Liu2ded3702016-11-17 15:24:38 -08005102 }
5103 }
NeilBrown84789552011-07-27 11:00:36 +10005104
5105 /* maybe we need to check and possibly fix the parity for this stripe
5106 * Any reads will already have been scheduled, so we just see if enough
5107 * data is available. The parity check is held off while parity
5108 * dependent operations are in flight.
5109 */
5110 if (sh->check_state ||
5111 (s.syncing && s.locked == 0 &&
5112 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
5113 !test_bit(STRIPE_INSYNC, &sh->state))) {
5114 if (conf->level == 6)
5115 handle_parity_checks6(conf, sh, &s, disks);
5116 else
5117 handle_parity_checks5(conf, sh, &s, disks);
5118 }
NeilBrownc5a31002011-07-27 11:00:36 +10005119
NeilBrownf94c0b62013-07-22 12:57:21 +10005120 if ((s.replacing || s.syncing) && s.locked == 0
5121 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
5122 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11005123 /* Write out to replacement devices where possible */
5124 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10005125 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
5126 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11005127 set_bit(R5_WantReplace, &sh->dev[i].flags);
5128 set_bit(R5_LOCKED, &sh->dev[i].flags);
5129 s.locked++;
5130 }
NeilBrownf94c0b62013-07-22 12:57:21 +10005131 if (s.replacing)
5132 set_bit(STRIPE_INSYNC, &sh->state);
5133 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11005134 }
5135 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10005136 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11005137 test_bit(STRIPE_INSYNC, &sh->state)) {
Yufen Yuc911c462020-07-18 05:29:07 -04005138 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1);
NeilBrownc5a31002011-07-27 11:00:36 +10005139 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005140 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
5141 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10005142 }
5143
5144 /* If the failed drives are just a ReadError, then we might need
5145 * to progress the repair/check process
5146 */
5147 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
5148 for (i = 0; i < s.failed; i++) {
5149 struct r5dev *dev = &sh->dev[s.failed_num[i]];
5150 if (test_bit(R5_ReadError, &dev->flags)
5151 && !test_bit(R5_LOCKED, &dev->flags)
5152 && test_bit(R5_UPTODATE, &dev->flags)
5153 ) {
5154 if (!test_bit(R5_ReWrite, &dev->flags)) {
5155 set_bit(R5_Wantwrite, &dev->flags);
5156 set_bit(R5_ReWrite, &dev->flags);
Guoqing Jiang3a31cf32020-07-28 12:01:43 +02005157 } else
NeilBrownc5a31002011-07-27 11:00:36 +10005158 /* let's read it back */
5159 set_bit(R5_Wantread, &dev->flags);
Guoqing Jiang3a31cf32020-07-28 12:01:43 +02005160 set_bit(R5_LOCKED, &dev->flags);
5161 s.locked++;
NeilBrownc5a31002011-07-27 11:00:36 +10005162 }
5163 }
5164
NeilBrown3687c062011-07-27 11:00:36 +10005165 /* Finish reconstruct operations initiated by the expansion process */
5166 if (sh->reconstruct_state == reconstruct_state_result) {
5167 struct stripe_head *sh_src
Shaohua Li6d036f72015-08-13 14:31:57 -07005168 = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrown3687c062011-07-27 11:00:36 +10005169 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
5170 /* sh cannot be written until sh_src has been read.
5171 * so arrange for sh to be delayed a little
5172 */
5173 set_bit(STRIPE_DELAYED, &sh->state);
5174 set_bit(STRIPE_HANDLE, &sh->state);
5175 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
5176 &sh_src->state))
5177 atomic_inc(&conf->preread_active_stripes);
Shaohua Li6d036f72015-08-13 14:31:57 -07005178 raid5_release_stripe(sh_src);
NeilBrown3687c062011-07-27 11:00:36 +10005179 goto finish;
5180 }
5181 if (sh_src)
Shaohua Li6d036f72015-08-13 14:31:57 -07005182 raid5_release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07005183
NeilBrown3687c062011-07-27 11:00:36 +10005184 sh->reconstruct_state = reconstruct_state_idle;
5185 clear_bit(STRIPE_EXPANDING, &sh->state);
5186 for (i = conf->raid_disks; i--; ) {
5187 set_bit(R5_Wantwrite, &sh->dev[i].flags);
5188 set_bit(R5_LOCKED, &sh->dev[i].flags);
5189 s.locked++;
5190 }
5191 }
5192
5193 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
5194 !sh->reconstruct_state) {
5195 /* Need to write out all blocks after computing parity */
5196 sh->disks = conf->raid_disks;
5197 stripe_set_idx(sh->sector, conf, 0, sh);
5198 schedule_reconstruction(sh, &s, 1, 1);
5199 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
5200 clear_bit(STRIPE_EXPAND_READY, &sh->state);
5201 atomic_dec(&conf->reshape_stripes);
5202 wake_up(&conf->wait_for_overlap);
Yufen Yuc911c462020-07-18 05:29:07 -04005203 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1);
NeilBrown3687c062011-07-27 11:00:36 +10005204 }
5205
5206 if (s.expanding && s.locked == 0 &&
5207 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
5208 handle_stripe_expansion(conf, sh);
5209
5210finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07005211 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10005212 if (unlikely(s.blocked_rdev)) {
5213 if (conf->mddev->external)
5214 md_wait_for_blocked_rdev(s.blocked_rdev,
5215 conf->mddev);
5216 else
5217 /* Internal metadata will immediately
5218 * be written by raid5d, so we don't
5219 * need to wait here.
5220 */
5221 rdev_dec_pending(s.blocked_rdev,
5222 conf->mddev);
5223 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07005224
NeilBrownbc2607f2011-07-28 11:39:22 +10005225 if (s.handle_bad_blocks)
5226 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11005227 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10005228 struct r5dev *dev = &sh->dev[i];
5229 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
5230 /* We own a safe reference to the rdev */
5231 rdev = conf->disks[i].rdev;
5232 if (!rdev_set_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04005233 RAID5_STRIPE_SECTORS(conf), 0))
NeilBrownbc2607f2011-07-28 11:39:22 +10005234 md_error(conf->mddev, rdev);
5235 rdev_dec_pending(rdev, conf->mddev);
5236 }
NeilBrownb84db562011-07-28 11:39:23 +10005237 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
5238 rdev = conf->disks[i].rdev;
5239 rdev_clear_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04005240 RAID5_STRIPE_SECTORS(conf), 0);
NeilBrownb84db562011-07-28 11:39:23 +10005241 rdev_dec_pending(rdev, conf->mddev);
5242 }
NeilBrown977df362011-12-23 10:17:53 +11005243 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
5244 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11005245 if (!rdev)
5246 /* rdev have been moved down */
5247 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11005248 rdev_clear_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04005249 RAID5_STRIPE_SECTORS(conf), 0);
NeilBrown977df362011-12-23 10:17:53 +11005250 rdev_dec_pending(rdev, conf->mddev);
5251 }
NeilBrownbc2607f2011-07-28 11:39:22 +10005252 }
5253
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07005254 if (s.ops_request)
5255 raid_run_ops(sh, s.ops_request);
5256
Dan Williamsf0e43bc2008-06-28 08:31:55 +10005257 ops_run_io(sh, &s);
5258
NeilBrownc5709ef2011-07-26 11:35:20 +10005259 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11005260 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02005261 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11005262 * have actually been submitted.
5263 */
5264 atomic_dec(&conf->preread_active_stripes);
5265 if (atomic_read(&conf->preread_active_stripes) <
5266 IO_THRESHOLD)
5267 md_wakeup_thread(conf->mddev->thread);
5268 }
5269
Dan Williams257a4b42011-11-08 16:22:06 +11005270 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07005271}
5272
NeilBrownd1688a62011-10-11 16:49:52 +11005273static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274{
5275 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
5276 while (!list_empty(&conf->delayed_list)) {
5277 struct list_head *l = conf->delayed_list.next;
5278 struct stripe_head *sh;
5279 sh = list_entry(l, struct stripe_head, lru);
5280 list_del_init(l);
5281 clear_bit(STRIPE_DELAYED, &sh->state);
5282 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5283 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005284 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08005285 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 }
NeilBrown482c0832011-04-18 18:25:42 +10005287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288}
5289
Shaohua Li566c09c2013-11-14 15:16:17 +11005290static void activate_bit_delay(struct r5conf *conf,
5291 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07005292{
5293 /* device_lock is held */
5294 struct list_head head;
5295 list_add(&head, &conf->bitmap_list);
5296 list_del_init(&conf->bitmap_list);
5297 while (!list_empty(&head)) {
5298 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11005299 int hash;
NeilBrown72626682005-09-09 16:23:54 -07005300 list_del_init(&sh->lru);
5301 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11005302 hash = sh->hash_lock_index;
5303 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07005304 }
5305}
5306
NeilBrownfd01b882011-10-11 16:47:53 +11005307static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005308{
NeilBrown3cb5edf2015-07-15 17:24:17 +10005309 struct r5conf *conf = mddev->private;
Christoph Hellwig10433d02017-08-23 19:10:28 +02005310 sector_t sector = bio->bi_iter.bi_sector;
NeilBrown3cb5edf2015-07-15 17:24:17 +10005311 unsigned int chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005312 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005313
Christoph Hellwig309dca302021-01-24 11:02:34 +01005314 WARN_ON_ONCE(bio->bi_bdev->bd_partno);
Christoph Hellwig10433d02017-08-23 19:10:28 +02005315
NeilBrown3cb5edf2015-07-15 17:24:17 +10005316 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005317 return chunk_sectors >=
5318 ((sector & (chunk_sectors - 1)) + bio_sectors);
5319}
5320
5321/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005322 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
5323 * later sampled by raid5d.
5324 */
NeilBrownd1688a62011-10-11 16:49:52 +11005325static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005326{
5327 unsigned long flags;
5328
5329 spin_lock_irqsave(&conf->device_lock, flags);
5330
5331 bi->bi_next = conf->retry_read_aligned_list;
5332 conf->retry_read_aligned_list = bi;
5333
5334 spin_unlock_irqrestore(&conf->device_lock, flags);
5335 md_wakeup_thread(conf->mddev->thread);
5336}
5337
NeilBrown0472a422017-03-15 14:05:13 +11005338static struct bio *remove_bio_from_retry(struct r5conf *conf,
5339 unsigned int *offset)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005340{
5341 struct bio *bi;
5342
5343 bi = conf->retry_read_aligned;
5344 if (bi) {
NeilBrown0472a422017-03-15 14:05:13 +11005345 *offset = conf->retry_read_offset;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005346 conf->retry_read_aligned = NULL;
5347 return bi;
5348 }
5349 bi = conf->retry_read_aligned_list;
5350 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08005351 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005352 bi->bi_next = NULL;
NeilBrown0472a422017-03-15 14:05:13 +11005353 *offset = 0;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005354 }
5355
5356 return bi;
5357}
5358
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005359/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005360 * The "raid5_align_endio" should check if the read succeeded and if it
5361 * did, call bio_endio on the original bio (having bio_put the new bio
5362 * first).
5363 * If the read failed..
5364 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005365static void raid5_align_endio(struct bio *bi)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005366{
5367 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11005368 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005369 struct r5conf *conf;
NeilBrown3cb03002011-10-11 16:45:26 +11005370 struct md_rdev *rdev;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02005371 blk_status_t error = bi->bi_status;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005372
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005373 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005374
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005375 rdev = (void*)raid_bi->bi_next;
5376 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11005377 mddev = rdev->mddev;
5378 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005379
5380 rdev_dec_pending(rdev, conf->mddev);
5381
Sasha Levin9b81c842015-08-10 19:05:18 -04005382 if (!error) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005383 bio_endio(raid_bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005384 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10005385 wake_up(&conf->wait_for_quiescent);
NeilBrown6712ecf2007-09-27 12:47:43 +02005386 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005387 }
5388
Dan Williams45b42332007-07-09 11:56:43 -07005389 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005390
5391 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005392}
5393
Ming Lin7ef6b122015-05-06 22:51:24 -07005394static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005395{
NeilBrownd1688a62011-10-11 16:49:52 +11005396 struct r5conf *conf = mddev->private;
Christoph Hellwige82ed3a2021-01-26 15:52:44 +01005397 struct bio *align_bio;
NeilBrown3cb03002011-10-11 16:45:26 +11005398 struct md_rdev *rdev;
Christoph Hellwige82ed3a2021-01-26 15:52:44 +01005399 sector_t sector, end_sector, first_bad;
5400 int bad_sectors, dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005401
5402 if (!in_chunk_boundary(mddev, raid_bio)) {
Ming Lin7ef6b122015-05-06 22:51:24 -07005403 pr_debug("%s: non aligned\n", __func__);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005404 return 0;
5405 }
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005406
Christoph Hellwige82ed3a2021-01-26 15:52:44 +01005407 sector = raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector, 0,
5408 &dd_idx, NULL);
5409 end_sector = bio_end_sector(raid_bio);
5410
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005411 rcu_read_lock();
Christoph Hellwige82ed3a2021-01-26 15:52:44 +01005412 if (r5c_big_stripe_cached(conf, sector))
5413 goto out_rcu_unlock;
5414
NeilBrown671488c2011-12-23 10:17:52 +11005415 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
5416 if (!rdev || test_bit(Faulty, &rdev->flags) ||
5417 rdev->recovery_offset < end_sector) {
5418 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
Christoph Hellwige82ed3a2021-01-26 15:52:44 +01005419 if (!rdev)
5420 goto out_rcu_unlock;
5421 if (test_bit(Faulty, &rdev->flags) ||
NeilBrown671488c2011-12-23 10:17:52 +11005422 !(test_bit(In_sync, &rdev->flags) ||
Christoph Hellwige82ed3a2021-01-26 15:52:44 +01005423 rdev->recovery_offset >= end_sector))
5424 goto out_rcu_unlock;
NeilBrown671488c2011-12-23 10:17:52 +11005425 }
Song Liu03b047f2017-01-11 13:39:14 -08005426
Christoph Hellwige82ed3a2021-01-26 15:52:44 +01005427 atomic_inc(&rdev->nr_pending);
5428 rcu_read_unlock();
5429
Guoqing Jiangc82aa1b2021-05-25 17:46:18 +08005430 if (is_badblock(rdev, sector, bio_sectors(raid_bio), &first_bad,
5431 &bad_sectors)) {
5432 bio_put(raid_bio);
5433 rdev_dec_pending(rdev, mddev);
5434 return 0;
5435 }
5436
Christoph Hellwige82ed3a2021-01-26 15:52:44 +01005437 align_bio = bio_clone_fast(raid_bio, GFP_NOIO, &mddev->bio_set);
5438 bio_set_dev(align_bio, rdev->bdev);
5439 align_bio->bi_end_io = raid5_align_endio;
5440 align_bio->bi_private = raid_bio;
5441 align_bio->bi_iter.bi_sector = sector;
5442
5443 raid_bio->bi_next = (void *)rdev;
5444
Christoph Hellwige82ed3a2021-01-26 15:52:44 +01005445 /* No reshape active, so we can trust rdev->data_offset */
5446 align_bio->bi_iter.bi_sector += rdev->data_offset;
NeilBrown31c176e2011-07-28 11:39:22 +10005447
Christoph Hellwige82ed3a2021-01-26 15:52:44 +01005448 spin_lock_irq(&conf->device_lock);
5449 wait_event_lock_irq(conf->wait_for_quiescent, conf->quiesce == 0,
5450 conf->device_lock);
5451 atomic_inc(&conf->active_aligned_reads);
5452 spin_unlock_irq(&conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005453
Christoph Hellwige82ed3a2021-01-26 15:52:44 +01005454 if (mddev->gendisk)
5455 trace_block_bio_remap(align_bio, disk_devt(mddev->gendisk),
5456 raid_bio->bi_iter.bi_sector);
5457 submit_bio_noacct(align_bio);
5458 return 1;
Neil Brown387bb172007-02-08 14:20:29 -08005459
Christoph Hellwige82ed3a2021-01-26 15:52:44 +01005460out_rcu_unlock:
5461 rcu_read_unlock();
5462 return 0;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005463}
5464
Ming Lin7ef6b122015-05-06 22:51:24 -07005465static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
5466{
5467 struct bio *split;
NeilBrowndd7a8f52017-04-05 14:05:51 +10005468 sector_t sector = raid_bio->bi_iter.bi_sector;
5469 unsigned chunk_sects = mddev->chunk_sectors;
5470 unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
Guoqing Jiang10764812021-05-25 17:46:17 +08005471 struct r5conf *conf = mddev->private;
Ming Lin7ef6b122015-05-06 22:51:24 -07005472
NeilBrowndd7a8f52017-04-05 14:05:51 +10005473 if (sectors < bio_sectors(raid_bio)) {
5474 struct r5conf *conf = mddev->private;
Kent Overstreetafeee512018-05-20 18:25:52 -04005475 split = bio_split(raid_bio, sectors, GFP_NOIO, &conf->bio_split);
NeilBrowndd7a8f52017-04-05 14:05:51 +10005476 bio_chain(split, raid_bio);
Christoph Hellwiged00aab2020-07-01 10:59:44 +02005477 submit_bio_noacct(raid_bio);
NeilBrowndd7a8f52017-04-05 14:05:51 +10005478 raid_bio = split;
5479 }
Ming Lin7ef6b122015-05-06 22:51:24 -07005480
Guoqing Jiang10764812021-05-25 17:46:17 +08005481 if (raid_bio->bi_pool != &conf->bio_split)
5482 md_account_bio(mddev, &raid_bio);
5483
NeilBrowndd7a8f52017-04-05 14:05:51 +10005484 if (!raid5_read_one_chunk(mddev, raid_bio))
5485 return raid_bio;
Ming Lin7ef6b122015-05-06 22:51:24 -07005486
5487 return NULL;
5488}
5489
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005490/* __get_priority_stripe - get the next stripe to process
5491 *
5492 * Full stripe writes are allowed to pass preread active stripes up until
5493 * the bypass_threshold is exceeded. In general the bypass_count
5494 * increments when the handle_list is handled before the hold_list; however, it
5495 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
5496 * stripe with in flight i/o. The bypass_count will be reset when the
5497 * head of the hold_list has changed, i.e. the head was promoted to the
5498 * handle_list.
5499 */
Shaohua Li851c30c2013-08-28 14:30:16 +08005500static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005501{
Shaohua Li535ae4e2017-02-15 19:37:32 -08005502 struct stripe_head *sh, *tmp;
Shaohua Li851c30c2013-08-28 14:30:16 +08005503 struct list_head *handle_list = NULL;
Shaohua Li535ae4e2017-02-15 19:37:32 -08005504 struct r5worker_group *wg;
Song Liu70d466f2017-05-11 15:28:28 -07005505 bool second_try = !r5c_is_writeback(conf->log) &&
5506 !r5l_log_disk_error(conf);
5507 bool try_loprio = test_bit(R5C_LOG_TIGHT, &conf->cache_state) ||
5508 r5l_log_disk_error(conf);
Shaohua Li851c30c2013-08-28 14:30:16 +08005509
Shaohua Li535ae4e2017-02-15 19:37:32 -08005510again:
5511 wg = NULL;
5512 sh = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005513 if (conf->worker_cnt_per_group == 0) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005514 handle_list = try_loprio ? &conf->loprio_list :
5515 &conf->handle_list;
Shaohua Li851c30c2013-08-28 14:30:16 +08005516 } else if (group != ANY_GROUP) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005517 handle_list = try_loprio ? &conf->worker_groups[group].loprio_list :
5518 &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005519 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08005520 } else {
5521 int i;
5522 for (i = 0; i < conf->group_cnt; i++) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005523 handle_list = try_loprio ? &conf->worker_groups[i].loprio_list :
5524 &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005525 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08005526 if (!list_empty(handle_list))
5527 break;
5528 }
5529 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005530
5531 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5532 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08005533 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005534 list_empty(&conf->hold_list) ? "empty" : "busy",
5535 atomic_read(&conf->pending_full_writes), conf->bypass_count);
5536
Shaohua Li851c30c2013-08-28 14:30:16 +08005537 if (!list_empty(handle_list)) {
5538 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005539
5540 if (list_empty(&conf->hold_list))
5541 conf->bypass_count = 0;
5542 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
5543 if (conf->hold_list.next == conf->last_hold)
5544 conf->bypass_count++;
5545 else {
5546 conf->last_hold = conf->hold_list.next;
5547 conf->bypass_count -= conf->bypass_threshold;
5548 if (conf->bypass_count < 0)
5549 conf->bypass_count = 0;
5550 }
5551 }
5552 } else if (!list_empty(&conf->hold_list) &&
5553 ((conf->bypass_threshold &&
5554 conf->bypass_count > conf->bypass_threshold) ||
5555 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08005556
5557 list_for_each_entry(tmp, &conf->hold_list, lru) {
5558 if (conf->worker_cnt_per_group == 0 ||
5559 group == ANY_GROUP ||
5560 !cpu_online(tmp->cpu) ||
5561 cpu_to_group(tmp->cpu) == group) {
5562 sh = tmp;
5563 break;
5564 }
5565 }
5566
5567 if (sh) {
5568 conf->bypass_count -= conf->bypass_threshold;
5569 if (conf->bypass_count < 0)
5570 conf->bypass_count = 0;
5571 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08005572 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005573 }
5574
Shaohua Li535ae4e2017-02-15 19:37:32 -08005575 if (!sh) {
5576 if (second_try)
5577 return NULL;
5578 second_try = true;
5579 try_loprio = !try_loprio;
5580 goto again;
5581 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005582
Shaohua Libfc90cb2013-08-29 15:40:32 +08005583 if (wg) {
5584 wg->stripes_cnt--;
5585 sh->group = NULL;
5586 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005587 list_del_init(&sh->lru);
Shaohua Lic7a6d352014-04-15 09:12:54 +08005588 BUG_ON(atomic_inc_return(&sh->count) != 1);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005589 return sh;
5590}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005591
Shaohua Li8811b592012-08-02 08:33:00 +10005592struct raid5_plug_cb {
5593 struct blk_plug_cb cb;
5594 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11005595 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10005596};
5597
5598static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
5599{
5600 struct raid5_plug_cb *cb = container_of(
5601 blk_cb, struct raid5_plug_cb, cb);
5602 struct stripe_head *sh;
5603 struct mddev *mddev = cb->cb.data;
5604 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11005605 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11005606 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10005607
5608 if (cb->list.next && !list_empty(&cb->list)) {
5609 spin_lock_irq(&conf->device_lock);
5610 while (!list_empty(&cb->list)) {
5611 sh = list_first_entry(&cb->list, struct stripe_head, lru);
5612 list_del_init(&sh->lru);
5613 /*
5614 * avoid race release_stripe_plug() sees
5615 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5616 * is still in our list
5617 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005618 smp_mb__before_atomic();
Shaohua Li8811b592012-08-02 08:33:00 +10005619 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08005620 /*
5621 * STRIPE_ON_RELEASE_LIST could be set here. In that
5622 * case, the count is always > 1 here
5623 */
Shaohua Li566c09c2013-11-14 15:16:17 +11005624 hash = sh->hash_lock_index;
5625 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11005626 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10005627 }
5628 spin_unlock_irq(&conf->device_lock);
5629 }
Shaohua Li566c09c2013-11-14 15:16:17 +11005630 release_inactive_stripe_list(conf, cb->temp_inactive_list,
5631 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005632 if (mddev->queue)
5633 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10005634 kfree(cb);
5635}
5636
5637static void release_stripe_plug(struct mddev *mddev,
5638 struct stripe_head *sh)
5639{
5640 struct blk_plug_cb *blk_cb = blk_check_plugged(
5641 raid5_unplug, mddev,
5642 sizeof(struct raid5_plug_cb));
5643 struct raid5_plug_cb *cb;
5644
5645 if (!blk_cb) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005646 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005647 return;
5648 }
5649
5650 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
5651
Shaohua Li566c09c2013-11-14 15:16:17 +11005652 if (cb->list.next == NULL) {
5653 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10005654 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11005655 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5656 INIT_LIST_HEAD(cb->temp_inactive_list + i);
5657 }
Shaohua Li8811b592012-08-02 08:33:00 +10005658
5659 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
5660 list_add_tail(&sh->lru, &cb->list);
5661 else
Shaohua Li6d036f72015-08-13 14:31:57 -07005662 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005663}
5664
Shaohua Li620125f2012-10-11 13:49:05 +11005665static void make_discard_request(struct mddev *mddev, struct bio *bi)
5666{
5667 struct r5conf *conf = mddev->private;
5668 sector_t logical_sector, last_sector;
5669 struct stripe_head *sh;
Shaohua Li620125f2012-10-11 13:49:05 +11005670 int stripe_sectors;
5671
5672 if (mddev->reshape_position != MaxSector)
5673 /* Skip discard while reshape is happening */
5674 return;
5675
Yufen Yuc911c462020-07-18 05:29:07 -04005676 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
Guoqing Jiangb0f01ec2019-09-03 11:41:03 +02005677 last_sector = bio_end_sector(bi);
Shaohua Li620125f2012-10-11 13:49:05 +11005678
5679 bi->bi_next = NULL;
Shaohua Li620125f2012-10-11 13:49:05 +11005680
5681 stripe_sectors = conf->chunk_sectors *
5682 (conf->raid_disks - conf->max_degraded);
5683 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5684 stripe_sectors);
5685 sector_div(last_sector, stripe_sectors);
5686
5687 logical_sector *= conf->chunk_sectors;
5688 last_sector *= conf->chunk_sectors;
5689
5690 for (; logical_sector < last_sector;
Yufen Yuc911c462020-07-18 05:29:07 -04005691 logical_sector += RAID5_STRIPE_SECTORS(conf)) {
Shaohua Li620125f2012-10-11 13:49:05 +11005692 DEFINE_WAIT(w);
5693 int d;
5694 again:
Shaohua Li6d036f72015-08-13 14:31:57 -07005695 sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
Shaohua Li620125f2012-10-11 13:49:05 +11005696 prepare_to_wait(&conf->wait_for_overlap, &w,
5697 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005698 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5699 if (test_bit(STRIPE_SYNCING, &sh->state)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005700 raid5_release_stripe(sh);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005701 schedule();
5702 goto again;
5703 }
5704 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11005705 spin_lock_irq(&sh->stripe_lock);
5706 for (d = 0; d < conf->raid_disks; d++) {
5707 if (d == sh->pd_idx || d == sh->qd_idx)
5708 continue;
5709 if (sh->dev[d].towrite || sh->dev[d].toread) {
5710 set_bit(R5_Overlap, &sh->dev[d].flags);
5711 spin_unlock_irq(&sh->stripe_lock);
Shaohua Li6d036f72015-08-13 14:31:57 -07005712 raid5_release_stripe(sh);
Shaohua Li620125f2012-10-11 13:49:05 +11005713 schedule();
5714 goto again;
5715 }
5716 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11005717 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11005718 finish_wait(&conf->wait_for_overlap, &w);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005719 sh->overwrite_disks = 0;
Shaohua Li620125f2012-10-11 13:49:05 +11005720 for (d = 0; d < conf->raid_disks; d++) {
5721 if (d == sh->pd_idx || d == sh->qd_idx)
5722 continue;
5723 sh->dev[d].towrite = bi;
5724 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
NeilBrown016c76a2017-03-15 14:05:13 +11005725 bio_inc_remaining(bi);
NeilBrown49728052017-03-15 14:05:12 +11005726 md_write_inc(mddev, bi);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005727 sh->overwrite_disks++;
Shaohua Li620125f2012-10-11 13:49:05 +11005728 }
5729 spin_unlock_irq(&sh->stripe_lock);
5730 if (conf->mddev->bitmap) {
5731 for (d = 0;
5732 d < conf->raid_disks - conf->max_degraded;
5733 d++)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005734 md_bitmap_startwrite(mddev->bitmap,
5735 sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04005736 RAID5_STRIPE_SECTORS(conf),
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005737 0);
Shaohua Li620125f2012-10-11 13:49:05 +11005738 sh->bm_seq = conf->seq_flush + 1;
5739 set_bit(STRIPE_BIT_DELAY, &sh->state);
5740 }
5741
5742 set_bit(STRIPE_HANDLE, &sh->state);
5743 clear_bit(STRIPE_DELAYED, &sh->state);
5744 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5745 atomic_inc(&conf->preread_active_stripes);
5746 release_stripe_plug(mddev, sh);
5747 }
5748
NeilBrown016c76a2017-03-15 14:05:13 +11005749 bio_endio(bi);
Shaohua Li620125f2012-10-11 13:49:05 +11005750}
5751
NeilBrowncc27b0c2017-06-05 16:49:39 +10005752static bool raid5_make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753{
NeilBrownd1688a62011-10-11 16:49:52 +11005754 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11005755 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 sector_t new_sector;
5757 sector_t logical_sector, last_sector;
5758 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01005759 const int rw = bio_data_dir(bi);
Shaohua Li27c0f682014-04-09 11:25:47 +08005760 DEFINE_WAIT(w);
5761 bool do_prepare;
Song Liu3bddb7f2016-11-18 16:46:50 -08005762 bool do_flush = false;
Guoqing Jiang10764812021-05-25 17:46:17 +08005763 bool do_clone = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764
Jens Axboe1eff9d32016-08-05 15:35:16 -06005765 if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01005766 int ret = log_handle_flush_request(conf, bi);
Shaohua Li828cbe92015-09-02 13:49:49 -07005767
5768 if (ret == 0)
NeilBrowncc27b0c2017-06-05 16:49:39 +10005769 return true;
Shaohua Li828cbe92015-09-02 13:49:49 -07005770 if (ret == -ENODEV) {
David Jeffery775d7832019-09-16 13:15:14 -04005771 if (md_flush_request(mddev, bi))
5772 return true;
Shaohua Li828cbe92015-09-02 13:49:49 -07005773 }
5774 /* ret == -EAGAIN, fallback */
Song Liu3bddb7f2016-11-18 16:46:50 -08005775 /*
5776 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
5777 * we need to flush journal device
5778 */
5779 do_flush = bi->bi_opf & REQ_PREFLUSH;
NeilBrowne5dcdd82005-09-09 16:23:41 -07005780 }
5781
NeilBrowncc27b0c2017-06-05 16:49:39 +10005782 if (!md_write_start(mddev, bi))
5783 return false;
Eric Mei9ffc8f72015-03-18 23:39:11 -06005784 /*
5785 * If array is degraded, better not do chunk aligned read because
5786 * later we might have to read it again in order to reconstruct
5787 * data on failed drives.
5788 */
5789 if (rw == READ && mddev->degraded == 0 &&
Ming Lin7ef6b122015-05-06 22:51:24 -07005790 mddev->reshape_position == MaxSector) {
5791 bi = chunk_aligned_read(mddev, bi);
Guoqing Jiang10764812021-05-25 17:46:17 +08005792 do_clone = true;
Ming Lin7ef6b122015-05-06 22:51:24 -07005793 if (!bi)
NeilBrowncc27b0c2017-06-05 16:49:39 +10005794 return true;
Ming Lin7ef6b122015-05-06 22:51:24 -07005795 }
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08005796
Mike Christie796a5cf2016-06-05 14:32:07 -05005797 if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
Shaohua Li620125f2012-10-11 13:49:05 +11005798 make_discard_request(mddev, bi);
NeilBrowncc27b0c2017-06-05 16:49:39 +10005799 md_write_end(mddev);
5800 return true;
Shaohua Li620125f2012-10-11 13:49:05 +11005801 }
5802
Yufen Yuc911c462020-07-18 05:29:07 -04005803 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005804 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 bi->bi_next = NULL;
NeilBrown06d91a52005-06-21 17:17:12 -07005806
Guoqing Jiang10764812021-05-25 17:46:17 +08005807 if (!do_clone)
5808 md_account_bio(mddev, &bi);
5809
Shaohua Li27c0f682014-04-09 11:25:47 +08005810 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
Yufen Yuc911c462020-07-18 05:29:07 -04005811 for (; logical_sector < last_sector; logical_sector += RAID5_STRIPE_SECTORS(conf)) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005812 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10005813 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08005814
Shaohua Li27c0f682014-04-09 11:25:47 +08005815 do_prepare = false;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005816 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10005817 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11005818 previous = 0;
Shaohua Li27c0f682014-04-09 11:25:47 +08005819 if (do_prepare)
5820 prepare_to_wait(&conf->wait_for_overlap, &w,
5821 TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005822 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11005823 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08005824 * 64bit on a 32bit platform, and so it might be
5825 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02005826 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08005827 * the lock is dropped, so once we get a reference
5828 * to the stripe that we think it is, we will have
5829 * to check again.
5830 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005831 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005832 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005833 ? logical_sector < conf->reshape_progress
5834 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005835 previous = 1;
5836 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10005837 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005838 ? logical_sector < conf->reshape_safe
5839 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08005840 spin_unlock_irq(&conf->device_lock);
5841 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005842 do_prepare = true;
NeilBrownb578d552006-03-27 01:18:12 -08005843 goto retry;
5844 }
5845 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005846 spin_unlock_irq(&conf->device_lock);
5847 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005848
NeilBrown112bf892009-03-31 14:39:38 +11005849 new_sector = raid5_compute_sector(conf, logical_sector,
5850 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11005851 &dd_idx, NULL);
Shaohua Li849674e2016-01-20 13:52:20 -08005852 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10005853 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 (unsigned long long)logical_sector);
5855
Shaohua Li6d036f72015-08-13 14:31:57 -07005856 sh = raid5_get_active_stripe(conf, new_sector, previous,
Jens Axboe1eff9d32016-08-05 15:35:16 -06005857 (bi->bi_opf & REQ_RAHEAD), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11005859 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005860 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08005861 * stripe, so we must do the range check again.
5862 * Expansion could still move past after this
5863 * test, but as we are holding a reference to
5864 * 'sh', we know that if that happens,
5865 * STRIPE_EXPANDING will get set and the expansion
5866 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005867 */
5868 int must_retry = 0;
5869 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005870 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11005871 ? logical_sector >= conf->reshape_progress
5872 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005873 /* mismatch, need to try again */
5874 must_retry = 1;
5875 spin_unlock_irq(&conf->device_lock);
5876 if (must_retry) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005877 raid5_release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07005878 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005879 do_prepare = true;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005880 goto retry;
5881 }
5882 }
NeilBrownc46501b2013-08-27 15:52:13 +10005883 if (read_seqcount_retry(&conf->gen_lock, seq)) {
5884 /* Might have got the wrong stripe_head
5885 * by accident
5886 */
Shaohua Li6d036f72015-08-13 14:31:57 -07005887 raid5_release_stripe(sh);
NeilBrownc46501b2013-08-27 15:52:13 +10005888 goto retry;
5889 }
NeilBrowne62e58a2009-07-01 13:15:35 +10005890
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005891 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
shli@kernel.orgda41ba62014-12-15 12:57:03 +11005892 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005893 /* Stripe is busy expanding or
5894 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 * and wait a while
5896 */
NeilBrown482c0832011-04-18 18:25:42 +10005897 md_wakeup_thread(mddev->thread);
Shaohua Li6d036f72015-08-13 14:31:57 -07005898 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005900 do_prepare = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 goto retry;
5902 }
Song Liu3bddb7f2016-11-18 16:46:50 -08005903 if (do_flush) {
5904 set_bit(STRIPE_R5C_PREFLUSH, &sh->state);
5905 /* we only need flush for one stripe */
5906 do_flush = false;
5907 }
5908
Guoqing Jiang1684e972020-06-16 11:25:52 +02005909 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrown6ed30032008-02-06 01:40:00 -08005910 clear_bit(STRIPE_DELAYED, &sh->state);
shli@kernel.org59fc6302014-12-15 12:57:03 +11005911 if ((!sh->batch_head || sh == sh->batch_head) &&
Jens Axboe1eff9d32016-08-05 15:35:16 -06005912 (bi->bi_opf & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11005913 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5914 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10005915 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 } else {
5917 /* cannot get stripe for read-ahead, just give-up */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02005918 bi->bi_status = BLK_STS_IOERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 break;
5920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 }
Shaohua Li27c0f682014-04-09 11:25:47 +08005922 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown7c13edc2011-04-18 18:25:43 +10005923
NeilBrown49728052017-03-15 14:05:12 +11005924 if (rw == WRITE)
5925 md_write_end(mddev);
NeilBrown016c76a2017-03-15 14:05:13 +11005926 bio_endio(bi);
NeilBrowncc27b0c2017-06-05 16:49:39 +10005927 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928}
5929
NeilBrownfd01b882011-10-11 16:47:53 +11005930static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11005931
NeilBrownfd01b882011-10-11 16:47:53 +11005932static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933{
NeilBrown52c03292006-06-26 00:27:43 -07005934 /* reshaping is quite different to recovery/resync so it is
5935 * handled quite separately ... here.
5936 *
5937 * On each call to sync_request, we gather one chunk worth of
5938 * destination stripes and flag them as expanding.
5939 * Then we find all the source stripes and request reads.
5940 * As the reads complete, handle_stripe will copy the data
5941 * into the destination stripe and release that stripe.
5942 */
NeilBrownd1688a62011-10-11 16:49:52 +11005943 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 struct stripe_head *sh;
NeilBrowndb0505d2017-10-17 16:18:36 +11005945 struct md_rdev *rdev;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005946 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08005947 int raid_disks = conf->previous_raid_disks;
5948 int data_disks = raid_disks - conf->max_degraded;
5949 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07005950 int i;
5951 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11005952 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11005953 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11005954 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11005955 struct list_head stripes;
NeilBrown92140482015-07-06 12:28:45 +10005956 sector_t retn;
NeilBrown52c03292006-06-26 00:27:43 -07005957
NeilBrownfef9c612009-03-31 15:16:46 +11005958 if (sector_nr == 0) {
5959 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10005960 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005961 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
5962 sector_nr = raid5_size(mddev, 0, 0)
5963 - conf->reshape_progress;
NeilBrown6cbd8142015-07-24 13:30:32 +10005964 } else if (mddev->reshape_backwards &&
5965 conf->reshape_progress == MaxSector) {
5966 /* shouldn't happen, but just in case, finish up.*/
5967 sector_nr = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +10005968 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005969 conf->reshape_progress > 0)
5970 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005971 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005972 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11005973 mddev->curr_resync_completed = sector_nr;
Junxiao Bie1a86db2020-07-14 16:10:26 -07005974 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrownfef9c612009-03-31 15:16:46 +11005975 *skipped = 1;
NeilBrown92140482015-07-06 12:28:45 +10005976 retn = sector_nr;
5977 goto finish;
NeilBrownfef9c612009-03-31 15:16:46 +11005978 }
NeilBrown52c03292006-06-26 00:27:43 -07005979 }
5980
NeilBrown7a661382009-03-31 15:21:40 +11005981 /* We need to process a full chunk at a time.
5982 * If old and new chunk sizes differ, we need to process the
5983 * largest of these
5984 */
NeilBrown3cb5edf2015-07-15 17:24:17 +10005985
5986 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
NeilBrown7a661382009-03-31 15:21:40 +11005987
NeilBrownb5254dd2012-05-21 09:27:01 +10005988 /* We update the metadata at least every 10 seconds, or when
5989 * the data about to be copied would over-write the source of
5990 * the data at the front of the range. i.e. one new_stripe
5991 * along from reshape_progress new_maps to after where
5992 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07005993 */
NeilBrownfef9c612009-03-31 15:16:46 +11005994 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005995 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11005996 readpos = conf->reshape_progress;
5997 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005998 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08005999 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10006000 if (mddev->reshape_backwards) {
NeilBrownc74c0d72015-07-15 17:54:15 +10006001 BUG_ON(writepos < reshape_sectors);
6002 writepos -= reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11006003 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11006004 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11006005 } else {
NeilBrown7a661382009-03-31 15:21:40 +11006006 writepos += reshape_sectors;
NeilBrownc74c0d72015-07-15 17:54:15 +10006007 /* readpos and safepos are worst-case calculations.
6008 * A negative number is overly pessimistic, and causes
6009 * obvious problems for unsigned storage. So clip to 0.
6010 */
NeilBrowned37d832009-05-27 21:39:05 +10006011 readpos -= min_t(sector_t, reshape_sectors, readpos);
6012 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11006013 }
NeilBrown52c03292006-06-26 00:27:43 -07006014
NeilBrownb5254dd2012-05-21 09:27:01 +10006015 /* Having calculated the 'writepos' possibly use it
6016 * to set 'stripe_addr' which is where we will write to.
6017 */
6018 if (mddev->reshape_backwards) {
6019 BUG_ON(conf->reshape_progress == 0);
6020 stripe_addr = writepos;
6021 BUG_ON((mddev->dev_sectors &
6022 ~((sector_t)reshape_sectors - 1))
6023 - reshape_sectors - stripe_addr
6024 != sector_nr);
6025 } else {
6026 BUG_ON(writepos != sector_nr + reshape_sectors);
6027 stripe_addr = sector_nr;
6028 }
6029
NeilBrownc8f517c2009-03-31 15:28:40 +11006030 /* 'writepos' is the most advanced device address we might write.
6031 * 'readpos' is the least advanced device address we might read.
6032 * 'safepos' is the least address recorded in the metadata as having
6033 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10006034 * If there is a min_offset_diff, these are adjusted either by
6035 * increasing the safepos/readpos if diff is negative, or
6036 * increasing writepos if diff is positive.
6037 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11006038 * ensure safety in the face of a crash - that must be done by userspace
6039 * making a backup of the data. So in that case there is no particular
6040 * rush to update metadata.
6041 * Otherwise if 'safepos' is behind 'writepos', then we really need to
6042 * update the metadata to advance 'safepos' to match 'readpos' so that
6043 * we can be safe in the event of a crash.
6044 * So we insist on updating metadata if safepos is behind writepos and
6045 * readpos is beyond writepos.
6046 * In any case, update the metadata every 10 seconds.
6047 * Maybe that number should be configurable, but I'm not sure it is
6048 * worth it.... maybe it could be a multiple of safemode_delay???
6049 */
NeilBrownb5254dd2012-05-21 09:27:01 +10006050 if (conf->min_offset_diff < 0) {
6051 safepos += -conf->min_offset_diff;
6052 readpos += -conf->min_offset_diff;
6053 } else
6054 writepos += conf->min_offset_diff;
6055
NeilBrown2c810cd2012-05-21 09:27:00 +10006056 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11006057 ? (safepos > writepos && readpos < writepos)
6058 : (safepos < writepos && readpos > writepos)) ||
6059 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07006060 /* Cannot proceed until we've updated the superblock... */
6061 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11006062 atomic_read(&conf->reshape_stripes)==0
6063 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6064 if (atomic_read(&conf->reshape_stripes) != 0)
6065 return 0;
NeilBrownfef9c612009-03-31 15:16:46 +11006066 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11006067 mddev->curr_resync_completed = sector_nr;
NeilBrowndb0505d2017-10-17 16:18:36 +11006068 if (!mddev->reshape_backwards)
6069 /* Can update recovery_offset */
6070 rdev_for_each(rdev, mddev)
6071 if (rdev->raid_disk >= 0 &&
6072 !test_bit(Journal, &rdev->flags) &&
6073 !test_bit(In_sync, &rdev->flags) &&
6074 rdev->recovery_offset < sector_nr)
6075 rdev->recovery_offset = sector_nr;
6076
NeilBrownc8f517c2009-03-31 15:28:40 +11006077 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08006078 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown52c03292006-06-26 00:27:43 -07006079 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08006080 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11006081 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6082 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6083 return 0;
NeilBrown52c03292006-06-26 00:27:43 -07006084 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11006085 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07006086 spin_unlock_irq(&conf->device_lock);
6087 wake_up(&conf->wait_for_overlap);
Junxiao Bie1a86db2020-07-14 16:10:26 -07006088 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown52c03292006-06-26 00:27:43 -07006089 }
6090
NeilBrownab69ae12009-03-31 15:26:47 +11006091 INIT_LIST_HEAD(&stripes);
Yufen Yuc911c462020-07-18 05:29:07 -04006092 for (i = 0; i < reshape_sectors; i += RAID5_STRIPE_SECTORS(conf)) {
NeilBrown52c03292006-06-26 00:27:43 -07006093 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10006094 int skipped_disk = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07006095 sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07006096 set_bit(STRIPE_EXPANDING, &sh->state);
6097 atomic_inc(&conf->reshape_stripes);
6098 /* If any of this stripe is beyond the end of the old
6099 * array, then we need to zero those blocks
6100 */
6101 for (j=sh->disks; j--;) {
6102 sector_t s;
6103 if (j == sh->pd_idx)
6104 continue;
NeilBrownf4168852007-02-28 20:11:53 -08006105 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11006106 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08006107 continue;
Shaohua Li6d036f72015-08-13 14:31:57 -07006108 s = raid5_compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11006109 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10006110 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07006111 continue;
6112 }
Yufen Yuc911c462020-07-18 05:29:07 -04006113 memset(page_address(sh->dev[j].page), 0, RAID5_STRIPE_SIZE(conf));
NeilBrown52c03292006-06-26 00:27:43 -07006114 set_bit(R5_Expanded, &sh->dev[j].flags);
6115 set_bit(R5_UPTODATE, &sh->dev[j].flags);
6116 }
NeilBrowna9f326e2009-09-23 18:06:41 +10006117 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07006118 set_bit(STRIPE_EXPAND_READY, &sh->state);
6119 set_bit(STRIPE_HANDLE, &sh->state);
6120 }
NeilBrownab69ae12009-03-31 15:26:47 +11006121 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07006122 }
6123 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10006124 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11006125 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11006126 else
NeilBrown7a661382009-03-31 15:21:40 +11006127 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07006128 spin_unlock_irq(&conf->device_lock);
6129 /* Ok, those stripe are ready. We can start scheduling
6130 * reads on the source stripes.
6131 * The source stripes are determined by mapping the first and last
6132 * block on the destination stripes.
6133 */
NeilBrown52c03292006-06-26 00:27:43 -07006134 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11006135 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11006136 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07006137 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10006138 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10006139 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11006140 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11006141 if (last_sector >= mddev->dev_sectors)
6142 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07006143 while (first_sector <= last_sector) {
Shaohua Li6d036f72015-08-13 14:31:57 -07006144 sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07006145 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
6146 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07006147 raid5_release_stripe(sh);
Yufen Yuc911c462020-07-18 05:29:07 -04006148 first_sector += RAID5_STRIPE_SECTORS(conf);
NeilBrown52c03292006-06-26 00:27:43 -07006149 }
NeilBrownab69ae12009-03-31 15:26:47 +11006150 /* Now that the sources are clearly marked, we can release
6151 * the destination stripes
6152 */
6153 while (!list_empty(&stripes)) {
6154 sh = list_entry(stripes.next, struct stripe_head, lru);
6155 list_del_init(&sh->lru);
Shaohua Li6d036f72015-08-13 14:31:57 -07006156 raid5_release_stripe(sh);
NeilBrownab69ae12009-03-31 15:26:47 +11006157 }
NeilBrownc6207272008-02-06 01:39:52 -08006158 /* If this takes us to the resync_max point where we have to pause,
6159 * then we need to write out the superblock.
6160 */
NeilBrown7a661382009-03-31 15:21:40 +11006161 sector_nr += reshape_sectors;
NeilBrown92140482015-07-06 12:28:45 +10006162 retn = reshape_sectors;
6163finish:
NeilBrownc5e19d92015-07-17 12:06:02 +10006164 if (mddev->curr_resync_completed > mddev->resync_max ||
6165 (sector_nr - mddev->curr_resync_completed) * 2
NeilBrownc03f6a12009-04-17 11:06:30 +10006166 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08006167 /* Cannot proceed until we've updated the superblock... */
6168 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11006169 atomic_read(&conf->reshape_stripes) == 0
6170 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6171 if (atomic_read(&conf->reshape_stripes) != 0)
6172 goto ret;
NeilBrownfef9c612009-03-31 15:16:46 +11006173 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11006174 mddev->curr_resync_completed = sector_nr;
NeilBrowndb0505d2017-10-17 16:18:36 +11006175 if (!mddev->reshape_backwards)
6176 /* Can update recovery_offset */
6177 rdev_for_each(rdev, mddev)
6178 if (rdev->raid_disk >= 0 &&
6179 !test_bit(Journal, &rdev->flags) &&
6180 !test_bit(In_sync, &rdev->flags) &&
6181 rdev->recovery_offset < sector_nr)
6182 rdev->recovery_offset = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11006183 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08006184 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownc6207272008-02-06 01:39:52 -08006185 md_wakeup_thread(mddev->thread);
6186 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08006187 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)
NeilBrownc91abf52013-11-19 12:02:01 +11006188 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6189 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6190 goto ret;
NeilBrownc6207272008-02-06 01:39:52 -08006191 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11006192 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08006193 spin_unlock_irq(&conf->device_lock);
6194 wake_up(&conf->wait_for_overlap);
Junxiao Bie1a86db2020-07-14 16:10:26 -07006195 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrownc6207272008-02-06 01:39:52 -08006196 }
NeilBrownc91abf52013-11-19 12:02:01 +11006197ret:
NeilBrown92140482015-07-06 12:28:45 +10006198 return retn;
NeilBrown52c03292006-06-26 00:27:43 -07006199}
6200
Shaohua Li849674e2016-01-20 13:52:20 -08006201static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
6202 int *skipped)
NeilBrown52c03292006-06-26 00:27:43 -07006203{
NeilBrownd1688a62011-10-11 16:49:52 +11006204 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07006205 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11006206 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11006207 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07006208 int still_degraded = 0;
6209 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210
NeilBrown72626682005-09-09 16:23:54 -07006211 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11006213
NeilBrown29269552006-03-27 01:18:10 -08006214 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
6215 end_reshape(conf);
6216 return 0;
6217 }
NeilBrown72626682005-09-09 16:23:54 -07006218
6219 if (mddev->curr_resync < max_sector) /* aborted */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006220 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
6221 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07006222 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07006223 conf->fullsync = 0;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006224 md_bitmap_close_sync(mddev->bitmap);
NeilBrown72626682005-09-09 16:23:54 -07006225
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 return 0;
6227 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08006228
NeilBrown64bd6602009-08-03 10:59:58 +10006229 /* Allow raid5_quiesce to complete */
6230 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
6231
NeilBrown52c03292006-06-26 00:27:43 -07006232 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6233 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08006234
NeilBrownc6207272008-02-06 01:39:52 -08006235 /* No need to check resync_max as we never do more than one
6236 * stripe, and as resync_max will always be on a chunk boundary,
6237 * if the check in md_do_sync didn't fire, there is no chance
6238 * of overstepping resync_max here
6239 */
6240
NeilBrown16a53ec2006-06-26 00:27:38 -07006241 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 * to resync, then assert that we are finished, because there is
6243 * nothing we can do.
6244 */
NeilBrown3285edf2006-06-26 00:27:55 -07006245 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07006246 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11006247 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07006248 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 return rv;
6250 }
majianpeng6f608042013-04-24 11:42:41 +10006251 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
6252 !conf->fullsync &&
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006253 !md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
Yufen Yuc911c462020-07-18 05:29:07 -04006254 sync_blocks >= RAID5_STRIPE_SECTORS(conf)) {
NeilBrown72626682005-09-09 16:23:54 -07006255 /* we can skip this block, and probably more */
Yufen Yu83c3e5e2020-07-22 23:29:05 -04006256 do_div(sync_blocks, RAID5_STRIPE_SECTORS(conf));
NeilBrown72626682005-09-09 16:23:54 -07006257 *skipped = 1;
Yufen Yuc911c462020-07-18 05:29:07 -04006258 /* keep things rounded to whole stripes */
6259 return sync_blocks * RAID5_STRIPE_SECTORS(conf);
NeilBrown72626682005-09-09 16:23:54 -07006260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006262 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
NeilBrownb47490c2008-02-06 01:39:50 -08006263
Shaohua Li6d036f72015-08-13 14:31:57 -07006264 sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 if (sh == NULL) {
Shaohua Li6d036f72015-08-13 14:31:57 -07006266 sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07006268 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08006270 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 }
NeilBrown16a53ec2006-06-26 00:27:38 -07006272 /* Need to check if array will still be degraded after recovery/resync
Eric Mei16d9cfa2015-01-06 09:35:02 -08006273 * Note in case of > 1 drive failures it's possible we're rebuilding
6274 * one drive while leaving another faulty drive in array.
NeilBrown16a53ec2006-06-26 00:27:38 -07006275 */
Eric Mei16d9cfa2015-01-06 09:35:02 -08006276 rcu_read_lock();
6277 for (i = 0; i < conf->raid_disks; i++) {
Mark Rutland6aa7de02017-10-23 14:07:29 -07006278 struct md_rdev *rdev = READ_ONCE(conf->disks[i].rdev);
Eric Mei16d9cfa2015-01-06 09:35:02 -08006279
6280 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
NeilBrown16a53ec2006-06-26 00:27:38 -07006281 still_degraded = 1;
Eric Mei16d9cfa2015-01-06 09:35:02 -08006282 }
6283 rcu_read_unlock();
NeilBrown16a53ec2006-06-26 00:27:38 -07006284
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006285 md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07006286
NeilBrown83206d62011-07-26 11:19:49 +10006287 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Eivind Sarto053f5b62014-06-09 17:06:19 -07006288 set_bit(STRIPE_HANDLE, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289
Shaohua Li6d036f72015-08-13 14:31:57 -07006290 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291
Yufen Yuc911c462020-07-18 05:29:07 -04006292 return RAID5_STRIPE_SECTORS(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293}
6294
NeilBrown0472a422017-03-15 14:05:13 +11006295static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio,
6296 unsigned int offset)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006297{
6298 /* We may not be able to submit a whole bio at once as there
6299 * may not be enough stripe_heads available.
6300 * We cannot pre-allocate enough stripe_heads as we may need
6301 * more than exist in the cache (if we allow ever large chunks).
6302 * So we do one stripe head at a time and record in
6303 * ->bi_hw_segments how many have been done.
6304 *
6305 * We *know* that this entire raid_bio is in one chunk, so
6306 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
6307 */
6308 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11006309 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006310 sector_t sector, logical_sector, last_sector;
6311 int scnt = 0;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006312 int handled = 0;
6313
Kent Overstreet4f024f32013-10-11 15:44:27 -07006314 logical_sector = raid_bio->bi_iter.bi_sector &
Yufen Yuc911c462020-07-18 05:29:07 -04006315 ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
NeilBrown112bf892009-03-31 14:39:38 +11006316 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11006317 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07006318 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006319
6320 for (; logical_sector < last_sector;
Yufen Yuc911c462020-07-18 05:29:07 -04006321 logical_sector += RAID5_STRIPE_SECTORS(conf),
6322 sector += RAID5_STRIPE_SECTORS(conf),
Neil Brown387bb172007-02-08 14:20:29 -08006323 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006324
NeilBrown0472a422017-03-15 14:05:13 +11006325 if (scnt < offset)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006326 /* already done this stripe */
6327 continue;
6328
Shaohua Li6d036f72015-08-13 14:31:57 -07006329 sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006330
6331 if (!sh) {
6332 /* failed to get a stripe - must wait */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006333 conf->retry_read_aligned = raid_bio;
NeilBrown0472a422017-03-15 14:05:13 +11006334 conf->retry_read_offset = scnt;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006335 return handled;
6336 }
6337
shli@kernel.orgda41ba62014-12-15 12:57:03 +11006338 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07006339 raid5_release_stripe(sh);
Neil Brown387bb172007-02-08 14:20:29 -08006340 conf->retry_read_aligned = raid_bio;
NeilBrown0472a422017-03-15 14:05:13 +11006341 conf->retry_read_offset = scnt;
Neil Brown387bb172007-02-08 14:20:29 -08006342 return handled;
6343 }
6344
majianpeng3f9e7c12012-07-31 10:04:21 +10006345 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07006346 handle_stripe(sh);
Shaohua Li6d036f72015-08-13 14:31:57 -07006347 raid5_release_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006348 handled++;
6349 }
NeilBrown016c76a2017-03-15 14:05:13 +11006350
6351 bio_endio(raid_bio);
6352
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006353 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10006354 wake_up(&conf->wait_for_quiescent);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006355 return handled;
6356}
6357
Shaohua Libfc90cb2013-08-29 15:40:32 +08006358static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11006359 struct r5worker *worker,
6360 struct list_head *temp_inactive_list)
Christoph Hellwigefcd4872019-04-04 18:56:16 +02006361 __releases(&conf->device_lock)
6362 __acquires(&conf->device_lock)
Shaohua Li46a06402012-08-02 08:33:15 +10006363{
6364 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11006365 int i, batch_size = 0, hash;
6366 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10006367
6368 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08006369 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10006370 batch[batch_size++] = sh;
6371
Shaohua Li566c09c2013-11-14 15:16:17 +11006372 if (batch_size == 0) {
6373 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6374 if (!list_empty(temp_inactive_list + i))
6375 break;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006376 if (i == NR_STRIPE_HASH_LOCKS) {
6377 spin_unlock_irq(&conf->device_lock);
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01006378 log_flush_stripe_to_raid(conf);
Shaohua Lia8c34f92015-09-02 13:49:46 -07006379 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006380 return batch_size;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006381 }
Shaohua Li566c09c2013-11-14 15:16:17 +11006382 release_inactive = true;
6383 }
Shaohua Li46a06402012-08-02 08:33:15 +10006384 spin_unlock_irq(&conf->device_lock);
6385
Shaohua Li566c09c2013-11-14 15:16:17 +11006386 release_inactive_stripe_list(conf, temp_inactive_list,
6387 NR_STRIPE_HASH_LOCKS);
6388
Shaohua Lia8c34f92015-09-02 13:49:46 -07006389 r5l_flush_stripe_to_raid(conf->log);
Shaohua Li566c09c2013-11-14 15:16:17 +11006390 if (release_inactive) {
6391 spin_lock_irq(&conf->device_lock);
6392 return 0;
6393 }
6394
Shaohua Li46a06402012-08-02 08:33:15 +10006395 for (i = 0; i < batch_size; i++)
6396 handle_stripe(batch[i]);
Artur Paszkiewiczff875732017-03-09 09:59:58 +01006397 log_write_stripe_run(conf);
Shaohua Li46a06402012-08-02 08:33:15 +10006398
6399 cond_resched();
6400
6401 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006402 for (i = 0; i < batch_size; i++) {
6403 hash = batch[i]->hash_lock_index;
6404 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
6405 }
Shaohua Li46a06402012-08-02 08:33:15 +10006406 return batch_size;
6407}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006408
Shaohua Li851c30c2013-08-28 14:30:16 +08006409static void raid5_do_work(struct work_struct *work)
6410{
6411 struct r5worker *worker = container_of(work, struct r5worker, work);
6412 struct r5worker_group *group = worker->group;
6413 struct r5conf *conf = group->conf;
NeilBrown16d997b2017-03-15 14:05:12 +11006414 struct mddev *mddev = conf->mddev;
Shaohua Li851c30c2013-08-28 14:30:16 +08006415 int group_id = group - conf->worker_groups;
6416 int handled;
6417 struct blk_plug plug;
6418
6419 pr_debug("+++ raid5worker active\n");
6420
6421 blk_start_plug(&plug);
6422 handled = 0;
6423 spin_lock_irq(&conf->device_lock);
6424 while (1) {
6425 int batch_size, released;
6426
Shaohua Li566c09c2013-11-14 15:16:17 +11006427 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006428
Shaohua Li566c09c2013-11-14 15:16:17 +11006429 batch_size = handle_active_stripes(conf, group_id, worker,
6430 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08006431 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08006432 if (!batch_size && !released)
6433 break;
6434 handled += batch_size;
NeilBrown16d997b2017-03-15 14:05:12 +11006435 wait_event_lock_irq(mddev->sb_wait,
6436 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags),
6437 conf->device_lock);
Shaohua Li851c30c2013-08-28 14:30:16 +08006438 }
6439 pr_debug("%d stripes handled\n", handled);
6440
6441 spin_unlock_irq(&conf->device_lock);
Ofer Heifetz7e96d552017-07-24 09:17:40 +03006442
Song Liu9c72a18e42017-08-24 09:53:59 -07006443 flush_deferred_bios(conf);
6444
6445 r5l_flush_stripe_to_raid(conf->log);
6446
Ofer Heifetz7e96d552017-07-24 09:17:40 +03006447 async_tx_issue_pending_all();
Shaohua Li851c30c2013-08-28 14:30:16 +08006448 blk_finish_plug(&plug);
6449
6450 pr_debug("--- raid5worker inactive\n");
6451}
6452
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453/*
6454 * This is our raid5 kernel thread.
6455 *
6456 * We scan the hash table for stripes which can be handled now.
6457 * During the scan, completed stripes are saved for us by the interrupt
6458 * handler, so that they will not have to wait for our next wakeup.
6459 */
Shaohua Li4ed87312012-10-11 13:34:00 +11006460static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461{
Shaohua Li4ed87312012-10-11 13:34:00 +11006462 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11006463 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006465 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466
Dan Williams45b42332007-07-09 11:56:43 -07006467 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468
6469 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006471 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 handled = 0;
6473 spin_lock_irq(&conf->device_lock);
6474 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006475 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08006476 int batch_size, released;
NeilBrown0472a422017-03-15 14:05:13 +11006477 unsigned int offset;
Shaohua Li773ca822013-08-27 17:50:39 +08006478
Shaohua Li566c09c2013-11-14 15:16:17 +11006479 released = release_stripe_list(conf, conf->temp_inactive_list);
NeilBrownedbe83a2015-02-26 12:47:56 +11006480 if (released)
6481 clear_bit(R5_DID_ALLOC, &conf->cache_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482
NeilBrown0021b7b2012-07-31 09:08:14 +02006483 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10006484 !list_empty(&conf->bitmap_list)) {
6485 /* Now is a good time to flush some bitmap updates */
6486 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08006487 spin_unlock_irq(&conf->device_lock);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006488 md_bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08006489 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10006490 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11006491 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07006492 }
NeilBrown0021b7b2012-07-31 09:08:14 +02006493 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07006494
NeilBrown0472a422017-03-15 14:05:13 +11006495 while ((bio = remove_bio_from_retry(conf, &offset))) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006496 int ok;
6497 spin_unlock_irq(&conf->device_lock);
NeilBrown0472a422017-03-15 14:05:13 +11006498 ok = retry_aligned_read(conf, bio, offset);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006499 spin_lock_irq(&conf->device_lock);
6500 if (!ok)
6501 break;
6502 handled++;
6503 }
6504
Shaohua Li566c09c2013-11-14 15:16:17 +11006505 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
6506 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08006507 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 break;
Shaohua Li46a06402012-08-02 08:33:15 +10006509 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510
Shaohua Li29530792016-12-08 15:48:19 -08006511 if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) {
Shaohua Li46a06402012-08-02 08:33:15 +10006512 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10006513 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10006514 spin_lock_irq(&conf->device_lock);
6515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516 }
Dan Williams45b42332007-07-09 11:56:43 -07006517 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518
6519 spin_unlock_irq(&conf->device_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006520 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
6521 mutex_trylock(&conf->cache_size_mutex)) {
NeilBrownedbe83a2015-02-26 12:47:56 +11006522 grow_one_stripe(conf, __GFP_NOWARN);
6523 /* Set flag even if allocation failed. This helps
6524 * slow down allocation requests when mem is short
6525 */
6526 set_bit(R5_DID_ALLOC, &conf->cache_state);
NeilBrown2d5b5692015-07-06 12:49:23 +10006527 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529
Shaohua Li765d7042017-01-04 09:33:23 -08006530 flush_deferred_bios(conf);
6531
Shaohua Li0576b1c2015-08-13 14:32:00 -07006532 r5l_flush_stripe_to_raid(conf->log);
6533
Dan Williamsc9f21aa2008-07-23 12:05:51 -07006534 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006535 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536
Dan Williams45b42332007-07-09 11:56:43 -07006537 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538}
6539
NeilBrown3f294f42005-11-08 21:39:25 -08006540static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006541raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006542{
NeilBrown7b1485b2014-12-15 12:56:59 +11006543 struct r5conf *conf;
6544 int ret = 0;
6545 spin_lock(&mddev->lock);
6546 conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006547 if (conf)
NeilBrownedbe83a2015-02-26 12:47:56 +11006548 ret = sprintf(page, "%d\n", conf->min_nr_stripes);
NeilBrown7b1485b2014-12-15 12:56:59 +11006549 spin_unlock(&mddev->lock);
6550 return ret;
NeilBrown3f294f42005-11-08 21:39:25 -08006551}
6552
NeilBrownc41d4ac2010-06-01 19:37:24 +10006553int
NeilBrownfd01b882011-10-11 16:47:53 +11006554raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10006555{
Alexei Naberezhnov483cbbe2018-03-27 16:54:16 -07006556 int result = 0;
NeilBrownd1688a62011-10-11 16:49:52 +11006557 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10006558
6559 if (size <= 16 || size > 32768)
6560 return -EINVAL;
NeilBrown486f0642015-02-25 12:10:35 +11006561
NeilBrownedbe83a2015-02-26 12:47:56 +11006562 conf->min_nr_stripes = size;
NeilBrown2d5b5692015-07-06 12:49:23 +10006563 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006564 while (size < conf->max_nr_stripes &&
6565 drop_one_stripe(conf))
6566 ;
NeilBrown2d5b5692015-07-06 12:49:23 +10006567 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006568
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02006569 md_allow_write(mddev);
NeilBrown486f0642015-02-25 12:10:35 +11006570
NeilBrown2d5b5692015-07-06 12:49:23 +10006571 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006572 while (size > conf->max_nr_stripes)
Alexei Naberezhnov483cbbe2018-03-27 16:54:16 -07006573 if (!grow_one_stripe(conf, GFP_KERNEL)) {
6574 conf->min_nr_stripes = conf->max_nr_stripes;
6575 result = -ENOMEM;
NeilBrown486f0642015-02-25 12:10:35 +11006576 break;
Alexei Naberezhnov483cbbe2018-03-27 16:54:16 -07006577 }
NeilBrown2d5b5692015-07-06 12:49:23 +10006578 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006579
Alexei Naberezhnov483cbbe2018-03-27 16:54:16 -07006580 return result;
NeilBrownc41d4ac2010-06-01 19:37:24 +10006581}
6582EXPORT_SYMBOL(raid5_set_cache_size);
6583
NeilBrown3f294f42005-11-08 21:39:25 -08006584static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006585raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08006586{
NeilBrown67918752014-12-15 12:57:01 +11006587 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006588 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07006589 int err;
6590
NeilBrown3f294f42005-11-08 21:39:25 -08006591 if (len >= PAGE_SIZE)
6592 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006593 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08006594 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006595 err = mddev_lock(mddev);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006596 if (err)
6597 return err;
NeilBrown67918752014-12-15 12:57:01 +11006598 conf = mddev->private;
6599 if (!conf)
6600 err = -ENODEV;
6601 else
6602 err = raid5_set_cache_size(mddev, new);
6603 mddev_unlock(mddev);
6604
6605 return err ?: len;
NeilBrown3f294f42005-11-08 21:39:25 -08006606}
NeilBrown007583c2005-11-08 21:39:30 -08006607
NeilBrown96de1e62005-11-08 21:39:39 -08006608static struct md_sysfs_entry
6609raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
6610 raid5_show_stripe_cache_size,
6611 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08006612
6613static ssize_t
Markus Stockhausend06f1912014-12-15 12:57:05 +11006614raid5_show_rmw_level(struct mddev *mddev, char *page)
6615{
6616 struct r5conf *conf = mddev->private;
6617 if (conf)
6618 return sprintf(page, "%d\n", conf->rmw_level);
6619 else
6620 return 0;
6621}
6622
6623static ssize_t
6624raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
6625{
6626 struct r5conf *conf = mddev->private;
6627 unsigned long new;
6628
6629 if (!conf)
6630 return -ENODEV;
6631
6632 if (len >= PAGE_SIZE)
6633 return -EINVAL;
6634
6635 if (kstrtoul(page, 10, &new))
6636 return -EINVAL;
6637
6638 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
6639 return -EINVAL;
6640
6641 if (new != PARITY_DISABLE_RMW &&
6642 new != PARITY_ENABLE_RMW &&
6643 new != PARITY_PREFER_RMW)
6644 return -EINVAL;
6645
6646 conf->rmw_level = new;
6647 return len;
6648}
6649
6650static struct md_sysfs_entry
6651raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
6652 raid5_show_rmw_level,
6653 raid5_store_rmw_level);
6654
Yufen Yu3b5408b2020-07-18 05:29:09 -04006655static ssize_t
6656raid5_show_stripe_size(struct mddev *mddev, char *page)
6657{
6658 struct r5conf *conf;
6659 int ret = 0;
6660
6661 spin_lock(&mddev->lock);
6662 conf = mddev->private;
6663 if (conf)
6664 ret = sprintf(page, "%lu\n", RAID5_STRIPE_SIZE(conf));
6665 spin_unlock(&mddev->lock);
6666 return ret;
6667}
6668
6669#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
6670static ssize_t
6671raid5_store_stripe_size(struct mddev *mddev, const char *page, size_t len)
6672{
6673 struct r5conf *conf;
6674 unsigned long new;
6675 int err;
Yufen Yu38912582020-08-20 09:22:14 -04006676 int size;
Yufen Yu3b5408b2020-07-18 05:29:09 -04006677
6678 if (len >= PAGE_SIZE)
6679 return -EINVAL;
6680 if (kstrtoul(page, 10, &new))
6681 return -EINVAL;
6682
6683 /*
6684 * The value should not be bigger than PAGE_SIZE. It requires to
Yufen Yu6af10a32020-08-20 09:22:05 -04006685 * be multiple of DEFAULT_STRIPE_SIZE and the value should be power
6686 * of two.
Yufen Yu3b5408b2020-07-18 05:29:09 -04006687 */
Yufen Yu6af10a32020-08-20 09:22:05 -04006688 if (new % DEFAULT_STRIPE_SIZE != 0 ||
6689 new > PAGE_SIZE || new == 0 ||
6690 new != roundup_pow_of_two(new))
Yufen Yu3b5408b2020-07-18 05:29:09 -04006691 return -EINVAL;
6692
6693 err = mddev_lock(mddev);
6694 if (err)
6695 return err;
6696
6697 conf = mddev->private;
6698 if (!conf) {
6699 err = -ENODEV;
6700 goto out_unlock;
6701 }
6702
6703 if (new == conf->stripe_size)
6704 goto out_unlock;
6705
6706 pr_debug("md/raid: change stripe_size from %lu to %lu\n",
6707 conf->stripe_size, new);
6708
Yufen Yu38912582020-08-20 09:22:14 -04006709 if (mddev->sync_thread ||
6710 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
6711 mddev->reshape_position != MaxSector ||
6712 mddev->sysfs_active) {
6713 err = -EBUSY;
6714 goto out_unlock;
6715 }
6716
Yufen Yu3b5408b2020-07-18 05:29:09 -04006717 mddev_suspend(mddev);
Yufen Yu38912582020-08-20 09:22:14 -04006718 mutex_lock(&conf->cache_size_mutex);
6719 size = conf->max_nr_stripes;
6720
6721 shrink_stripes(conf);
6722
Yufen Yu3b5408b2020-07-18 05:29:09 -04006723 conf->stripe_size = new;
6724 conf->stripe_shift = ilog2(new) - 9;
6725 conf->stripe_sectors = new >> 9;
Yufen Yu38912582020-08-20 09:22:14 -04006726 if (grow_stripes(conf, size)) {
6727 pr_warn("md/raid:%s: couldn't allocate buffers\n",
6728 mdname(mddev));
6729 err = -ENOMEM;
6730 }
6731 mutex_unlock(&conf->cache_size_mutex);
Yufen Yu3b5408b2020-07-18 05:29:09 -04006732 mddev_resume(mddev);
6733
6734out_unlock:
6735 mddev_unlock(mddev);
6736 return err ?: len;
6737}
6738
6739static struct md_sysfs_entry
6740raid5_stripe_size = __ATTR(stripe_size, 0644,
6741 raid5_show_stripe_size,
6742 raid5_store_stripe_size);
6743#else
6744static struct md_sysfs_entry
6745raid5_stripe_size = __ATTR(stripe_size, 0444,
6746 raid5_show_stripe_size,
6747 NULL);
6748#endif
Markus Stockhausend06f1912014-12-15 12:57:05 +11006749
6750static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006751raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006752{
NeilBrown7b1485b2014-12-15 12:56:59 +11006753 struct r5conf *conf;
6754 int ret = 0;
6755 spin_lock(&mddev->lock);
6756 conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006757 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006758 ret = sprintf(page, "%d\n", conf->bypass_threshold);
6759 spin_unlock(&mddev->lock);
6760 return ret;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006761}
6762
6763static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006764raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006765{
NeilBrown67918752014-12-15 12:57:01 +11006766 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006767 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006768 int err;
6769
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006770 if (len >= PAGE_SIZE)
6771 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006772 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006773 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006774
6775 err = mddev_lock(mddev);
6776 if (err)
6777 return err;
6778 conf = mddev->private;
6779 if (!conf)
6780 err = -ENODEV;
NeilBrownedbe83a2015-02-26 12:47:56 +11006781 else if (new > conf->min_nr_stripes)
NeilBrown67918752014-12-15 12:57:01 +11006782 err = -EINVAL;
6783 else
6784 conf->bypass_threshold = new;
6785 mddev_unlock(mddev);
6786 return err ?: len;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006787}
6788
6789static struct md_sysfs_entry
6790raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
6791 S_IRUGO | S_IWUSR,
6792 raid5_show_preread_threshold,
6793 raid5_store_preread_threshold);
6794
6795static ssize_t
Shaohua Lid592a992014-05-21 17:57:44 +08006796raid5_show_skip_copy(struct mddev *mddev, char *page)
6797{
NeilBrown7b1485b2014-12-15 12:56:59 +11006798 struct r5conf *conf;
6799 int ret = 0;
6800 spin_lock(&mddev->lock);
6801 conf = mddev->private;
Shaohua Lid592a992014-05-21 17:57:44 +08006802 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006803 ret = sprintf(page, "%d\n", conf->skip_copy);
6804 spin_unlock(&mddev->lock);
6805 return ret;
Shaohua Lid592a992014-05-21 17:57:44 +08006806}
6807
6808static ssize_t
6809raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
6810{
NeilBrown67918752014-12-15 12:57:01 +11006811 struct r5conf *conf;
Shaohua Lid592a992014-05-21 17:57:44 +08006812 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006813 int err;
6814
Shaohua Lid592a992014-05-21 17:57:44 +08006815 if (len >= PAGE_SIZE)
6816 return -EINVAL;
Shaohua Lid592a992014-05-21 17:57:44 +08006817 if (kstrtoul(page, 10, &new))
6818 return -EINVAL;
6819 new = !!new;
Shaohua Lid592a992014-05-21 17:57:44 +08006820
NeilBrown67918752014-12-15 12:57:01 +11006821 err = mddev_lock(mddev);
6822 if (err)
6823 return err;
6824 conf = mddev->private;
6825 if (!conf)
6826 err = -ENODEV;
6827 else if (new != conf->skip_copy) {
Christoph Hellwig1cb039f2020-09-24 08:51:38 +02006828 struct request_queue *q = mddev->queue;
6829
NeilBrown67918752014-12-15 12:57:01 +11006830 mddev_suspend(mddev);
6831 conf->skip_copy = new;
6832 if (new)
Christoph Hellwig1cb039f2020-09-24 08:51:38 +02006833 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, q);
NeilBrown67918752014-12-15 12:57:01 +11006834 else
Christoph Hellwig1cb039f2020-09-24 08:51:38 +02006835 blk_queue_flag_clear(QUEUE_FLAG_STABLE_WRITES, q);
NeilBrown67918752014-12-15 12:57:01 +11006836 mddev_resume(mddev);
6837 }
6838 mddev_unlock(mddev);
6839 return err ?: len;
Shaohua Lid592a992014-05-21 17:57:44 +08006840}
6841
6842static struct md_sysfs_entry
6843raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
6844 raid5_show_skip_copy,
6845 raid5_store_skip_copy);
6846
Shaohua Lid592a992014-05-21 17:57:44 +08006847static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006848stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006849{
NeilBrownd1688a62011-10-11 16:49:52 +11006850 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006851 if (conf)
6852 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
6853 else
6854 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08006855}
6856
NeilBrown96de1e62005-11-08 21:39:39 -08006857static struct md_sysfs_entry
6858raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08006859
Shaohua Lib7214202013-08-27 17:50:42 +08006860static ssize_t
6861raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
6862{
NeilBrown7b1485b2014-12-15 12:56:59 +11006863 struct r5conf *conf;
6864 int ret = 0;
6865 spin_lock(&mddev->lock);
6866 conf = mddev->private;
Shaohua Lib7214202013-08-27 17:50:42 +08006867 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006868 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
6869 spin_unlock(&mddev->lock);
6870 return ret;
Shaohua Lib7214202013-08-27 17:50:42 +08006871}
6872
majianpeng60aaf932013-11-14 15:16:20 +11006873static int alloc_thread_groups(struct r5conf *conf, int cnt,
6874 int *group_cnt,
majianpeng60aaf932013-11-14 15:16:20 +11006875 struct r5worker_group **worker_groups);
Shaohua Lib7214202013-08-27 17:50:42 +08006876static ssize_t
6877raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
6878{
NeilBrown67918752014-12-15 12:57:01 +11006879 struct r5conf *conf;
Shaohua Li7d5d7b52017-09-21 11:03:52 -07006880 unsigned int new;
Shaohua Lib7214202013-08-27 17:50:42 +08006881 int err;
majianpeng60aaf932013-11-14 15:16:20 +11006882 struct r5worker_group *new_groups, *old_groups;
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006883 int group_cnt;
Shaohua Lib7214202013-08-27 17:50:42 +08006884
6885 if (len >= PAGE_SIZE)
6886 return -EINVAL;
Shaohua Li7d5d7b52017-09-21 11:03:52 -07006887 if (kstrtouint(page, 10, &new))
6888 return -EINVAL;
6889 /* 8192 should be big enough */
6890 if (new > 8192)
Shaohua Lib7214202013-08-27 17:50:42 +08006891 return -EINVAL;
6892
NeilBrown67918752014-12-15 12:57:01 +11006893 err = mddev_lock(mddev);
Shaohua Lib7214202013-08-27 17:50:42 +08006894 if (err)
6895 return err;
NeilBrown67918752014-12-15 12:57:01 +11006896 conf = mddev->private;
6897 if (!conf)
6898 err = -ENODEV;
6899 else if (new != conf->worker_cnt_per_group) {
6900 mddev_suspend(mddev);
6901
6902 old_groups = conf->worker_groups;
6903 if (old_groups)
6904 flush_workqueue(raid5_wq);
6905
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006906 err = alloc_thread_groups(conf, new, &group_cnt, &new_groups);
NeilBrown67918752014-12-15 12:57:01 +11006907 if (!err) {
6908 spin_lock_irq(&conf->device_lock);
6909 conf->group_cnt = group_cnt;
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006910 conf->worker_cnt_per_group = new;
NeilBrown67918752014-12-15 12:57:01 +11006911 conf->worker_groups = new_groups;
6912 spin_unlock_irq(&conf->device_lock);
6913
6914 if (old_groups)
6915 kfree(old_groups[0].workers);
6916 kfree(old_groups);
6917 }
6918 mddev_resume(mddev);
6919 }
6920 mddev_unlock(mddev);
6921
6922 return err ?: len;
Shaohua Lib7214202013-08-27 17:50:42 +08006923}
6924
6925static struct md_sysfs_entry
6926raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
6927 raid5_show_group_thread_cnt,
6928 raid5_store_group_thread_cnt);
6929
NeilBrown007583c2005-11-08 21:39:30 -08006930static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08006931 &raid5_stripecache_size.attr,
6932 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006933 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08006934 &raid5_group_thread_cnt.attr,
Shaohua Lid592a992014-05-21 17:57:44 +08006935 &raid5_skip_copy.attr,
Markus Stockhausend06f1912014-12-15 12:57:05 +11006936 &raid5_rmw_level.attr,
Yufen Yu3b5408b2020-07-18 05:29:09 -04006937 &raid5_stripe_size.attr,
Song Liu2c7da142016-11-17 15:24:41 -08006938 &r5c_journal_mode.attr,
Mariusz Dabrowskia596d082019-02-18 15:04:09 +01006939 &ppl_write_hint.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08006940 NULL,
6941};
NeilBrown007583c2005-11-08 21:39:30 -08006942static struct attribute_group raid5_attrs_group = {
6943 .name = NULL,
6944 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08006945};
6946
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006947static int alloc_thread_groups(struct r5conf *conf, int cnt, int *group_cnt,
majianpeng60aaf932013-11-14 15:16:20 +11006948 struct r5worker_group **worker_groups)
Shaohua Li851c30c2013-08-28 14:30:16 +08006949{
Shaohua Li566c09c2013-11-14 15:16:17 +11006950 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08006951 ssize_t size;
6952 struct r5worker *workers;
6953
Shaohua Li851c30c2013-08-28 14:30:16 +08006954 if (cnt == 0) {
majianpeng60aaf932013-11-14 15:16:20 +11006955 *group_cnt = 0;
6956 *worker_groups = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08006957 return 0;
6958 }
majianpeng60aaf932013-11-14 15:16:20 +11006959 *group_cnt = num_possible_nodes();
Shaohua Li851c30c2013-08-28 14:30:16 +08006960 size = sizeof(struct r5worker) * cnt;
Kees Cook6396bb22018-06-12 14:03:40 -07006961 workers = kcalloc(size, *group_cnt, GFP_NOIO);
6962 *worker_groups = kcalloc(*group_cnt, sizeof(struct r5worker_group),
6963 GFP_NOIO);
majianpeng60aaf932013-11-14 15:16:20 +11006964 if (!*worker_groups || !workers) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006965 kfree(workers);
majianpeng60aaf932013-11-14 15:16:20 +11006966 kfree(*worker_groups);
Shaohua Li851c30c2013-08-28 14:30:16 +08006967 return -ENOMEM;
6968 }
6969
majianpeng60aaf932013-11-14 15:16:20 +11006970 for (i = 0; i < *group_cnt; i++) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006971 struct r5worker_group *group;
6972
NeilBrown0c775d52013-11-25 11:12:43 +11006973 group = &(*worker_groups)[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08006974 INIT_LIST_HEAD(&group->handle_list);
Shaohua Li535ae4e2017-02-15 19:37:32 -08006975 INIT_LIST_HEAD(&group->loprio_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006976 group->conf = conf;
6977 group->workers = workers + i * cnt;
6978
6979 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11006980 struct r5worker *worker = group->workers + j;
6981 worker->group = group;
6982 INIT_WORK(&worker->work, raid5_do_work);
6983
6984 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
6985 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08006986 }
6987 }
6988
6989 return 0;
6990}
6991
6992static void free_thread_groups(struct r5conf *conf)
6993{
6994 if (conf->worker_groups)
6995 kfree(conf->worker_groups[0].workers);
6996 kfree(conf->worker_groups);
6997 conf->worker_groups = NULL;
6998}
6999
Dan Williams80c3a6c2009-03-17 18:10:40 -07007000static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11007001raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07007002{
NeilBrownd1688a62011-10-11 16:49:52 +11007003 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07007004
7005 if (!sectors)
7006 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11007007 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11007008 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11007009 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07007010
NeilBrown3cb5edf2015-07-15 17:24:17 +10007011 sectors &= ~((sector_t)conf->chunk_sectors - 1);
7012 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07007013 return sectors * (raid_disks - conf->max_degraded);
7014}
7015
Oleg Nesterov789b5e02014-02-06 03:42:45 +05307016static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
7017{
7018 safe_put_page(percpu->spare_page);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05307019 percpu->spare_page = NULL;
Kent Overstreetb330e6a2019-03-11 23:31:06 -07007020 kvfree(percpu->scribble);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05307021 percpu->scribble = NULL;
7022}
7023
7024static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
7025{
Kent Overstreetb330e6a2019-03-11 23:31:06 -07007026 if (conf->level == 6 && !percpu->spare_page) {
Oleg Nesterov789b5e02014-02-06 03:42:45 +05307027 percpu->spare_page = alloc_page(GFP_KERNEL);
Kent Overstreetb330e6a2019-03-11 23:31:06 -07007028 if (!percpu->spare_page)
7029 return -ENOMEM;
7030 }
Oleg Nesterov789b5e02014-02-06 03:42:45 +05307031
Kent Overstreetb330e6a2019-03-11 23:31:06 -07007032 if (scribble_alloc(percpu,
7033 max(conf->raid_disks,
7034 conf->previous_raid_disks),
7035 max(conf->chunk_sectors,
7036 conf->prev_chunk_sectors)
Yufen Yuc911c462020-07-18 05:29:07 -04007037 / RAID5_STRIPE_SECTORS(conf))) {
Oleg Nesterov789b5e02014-02-06 03:42:45 +05307038 free_scratch_buffer(conf, percpu);
7039 return -ENOMEM;
7040 }
7041
7042 return 0;
7043}
7044
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02007045static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
7046{
7047 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
7048
7049 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
7050 return 0;
7051}
7052
NeilBrownd1688a62011-10-11 16:49:52 +11007053static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07007054{
Dan Williams36d1c642009-07-14 11:48:22 -07007055 if (!conf->percpu)
7056 return;
7057
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02007058 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Dan Williams36d1c642009-07-14 11:48:22 -07007059 free_percpu(conf->percpu);
7060}
7061
NeilBrownd1688a62011-10-11 16:49:52 +11007062static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10007063{
Song Liud7bd3982016-11-23 22:50:39 -08007064 int i;
7065
Artur Paszkiewiczff875732017-03-09 09:59:58 +01007066 log_exit(conf);
7067
Aliaksei Karaliou565e0452017-12-23 21:20:31 +03007068 unregister_shrinker(&conf->shrinker);
Shaohua Li851c30c2013-08-28 14:30:16 +08007069 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10007070 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07007071 raid5_free_percpu(conf);
Song Liud7bd3982016-11-23 22:50:39 -08007072 for (i = 0; i < conf->pool_size; i++)
7073 if (conf->disks[i].extra_page)
7074 put_page(conf->disks[i].extra_page);
Dan Williams95fc17a2009-07-31 12:39:15 +10007075 kfree(conf->disks);
Kent Overstreetafeee512018-05-20 18:25:52 -04007076 bioset_exit(&conf->bio_split);
Dan Williams95fc17a2009-07-31 12:39:15 +10007077 kfree(conf->stripe_hashtbl);
Shaohua Liaaf9f122017-03-03 22:06:12 -08007078 kfree(conf->pending_data);
Dan Williams95fc17a2009-07-31 12:39:15 +10007079 kfree(conf);
7080}
7081
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02007082static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
Dan Williams36d1c642009-07-14 11:48:22 -07007083{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02007084 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
Dan Williams36d1c642009-07-14 11:48:22 -07007085 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
7086
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02007087 if (alloc_scratch_buffer(conf, percpu)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007088 pr_warn("%s: failed memory allocation for cpu%u\n",
7089 __func__, cpu);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02007090 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07007091 }
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02007092 return 0;
Dan Williams36d1c642009-07-14 11:48:22 -07007093}
Dan Williams36d1c642009-07-14 11:48:22 -07007094
NeilBrownd1688a62011-10-11 16:49:52 +11007095static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07007096{
Oleg Nesterov789b5e02014-02-06 03:42:45 +05307097 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07007098
Oleg Nesterov789b5e02014-02-06 03:42:45 +05307099 conf->percpu = alloc_percpu(struct raid5_percpu);
7100 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07007101 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07007102
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02007103 err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Shaohua Li27a353c2016-02-24 17:38:28 -08007104 if (!err) {
7105 conf->scribble_disks = max(conf->raid_disks,
7106 conf->previous_raid_disks);
7107 conf->scribble_sectors = max(conf->chunk_sectors,
7108 conf->prev_chunk_sectors);
7109 }
Dan Williams36d1c642009-07-14 11:48:22 -07007110 return err;
7111}
7112
NeilBrownedbe83a2015-02-26 12:47:56 +11007113static unsigned long raid5_cache_scan(struct shrinker *shrink,
7114 struct shrink_control *sc)
7115{
7116 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
NeilBrown2d5b5692015-07-06 12:49:23 +10007117 unsigned long ret = SHRINK_STOP;
7118
7119 if (mutex_trylock(&conf->cache_size_mutex)) {
7120 ret= 0;
NeilBrown49895bc2015-08-03 17:09:57 +10007121 while (ret < sc->nr_to_scan &&
7122 conf->max_nr_stripes > conf->min_nr_stripes) {
NeilBrown2d5b5692015-07-06 12:49:23 +10007123 if (drop_one_stripe(conf) == 0) {
7124 ret = SHRINK_STOP;
7125 break;
7126 }
7127 ret++;
7128 }
7129 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11007130 }
7131 return ret;
7132}
7133
7134static unsigned long raid5_cache_count(struct shrinker *shrink,
7135 struct shrink_control *sc)
7136{
7137 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
7138
7139 if (conf->max_nr_stripes < conf->min_nr_stripes)
7140 /* unlikely, but not impossible */
7141 return 0;
7142 return conf->max_nr_stripes - conf->min_nr_stripes;
7143}
7144
NeilBrownd1688a62011-10-11 16:49:52 +11007145static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146{
NeilBrownd1688a62011-10-11 16:49:52 +11007147 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11007148 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11007149 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10007151 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11007152 int i;
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01007153 int group_cnt;
majianpeng60aaf932013-11-14 15:16:20 +11007154 struct r5worker_group *new_group;
Kent Overstreetafeee512018-05-20 18:25:52 -04007155 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156
NeilBrown91adb562009-03-31 14:39:39 +11007157 if (mddev->new_level != 5
7158 && mddev->new_level != 4
7159 && mddev->new_level != 6) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007160 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
7161 mdname(mddev), mddev->new_level);
NeilBrown91adb562009-03-31 14:39:39 +11007162 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 }
NeilBrown91adb562009-03-31 14:39:39 +11007164 if ((mddev->new_level == 5
7165 && !algorithm_valid_raid5(mddev->new_layout)) ||
7166 (mddev->new_level == 6
7167 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007168 pr_warn("md/raid:%s: layout %d not supported\n",
7169 mdname(mddev), mddev->new_layout);
NeilBrown91adb562009-03-31 14:39:39 +11007170 return ERR_PTR(-EIO);
7171 }
7172 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007173 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
7174 mdname(mddev), mddev->raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11007175 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11007176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177
Andre Noll664e7c42009-06-18 08:45:27 +10007178 if (!mddev->new_chunk_sectors ||
7179 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
7180 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007181 pr_warn("md/raid:%s: invalid chunk size %d\n",
7182 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11007183 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11007184 }
7185
NeilBrownd1688a62011-10-11 16:49:52 +11007186 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11007187 if (conf == NULL)
7188 goto abort;
Yufen Yuc911c462020-07-18 05:29:07 -04007189
Yufen Yue2368582020-07-18 05:29:08 -04007190#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
7191 conf->stripe_size = DEFAULT_STRIPE_SIZE;
7192 conf->stripe_shift = ilog2(DEFAULT_STRIPE_SIZE) - 9;
7193 conf->stripe_sectors = DEFAULT_STRIPE_SIZE >> 9;
7194#endif
Shaohua Liaaf9f122017-03-03 22:06:12 -08007195 INIT_LIST_HEAD(&conf->free_list);
7196 INIT_LIST_HEAD(&conf->pending_list);
Kees Cook6396bb22018-06-12 14:03:40 -07007197 conf->pending_data = kcalloc(PENDING_IO_MAX,
7198 sizeof(struct r5pending_data),
7199 GFP_KERNEL);
Shaohua Liaaf9f122017-03-03 22:06:12 -08007200 if (!conf->pending_data)
7201 goto abort;
7202 for (i = 0; i < PENDING_IO_MAX; i++)
7203 list_add(&conf->pending_data[i].sibling, &conf->free_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08007204 /* Don't enable multi-threading by default*/
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01007205 if (!alloc_thread_groups(conf, 0, &group_cnt, &new_group)) {
majianpeng60aaf932013-11-14 15:16:20 +11007206 conf->group_cnt = group_cnt;
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01007207 conf->worker_cnt_per_group = 0;
majianpeng60aaf932013-11-14 15:16:20 +11007208 conf->worker_groups = new_group;
7209 } else
Shaohua Li851c30c2013-08-28 14:30:16 +08007210 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11007211 spin_lock_init(&conf->device_lock);
Ahmed S. Darwish0a87b252020-07-20 17:55:25 +02007212 seqcount_spinlock_init(&conf->gen_lock, &conf->device_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10007213 mutex_init(&conf->cache_size_mutex);
Yuanhan Liub1b46482015-05-08 18:19:06 +10007214 init_waitqueue_head(&conf->wait_for_quiescent);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08007215 init_waitqueue_head(&conf->wait_for_stripe);
Dan Williamsf5efd452009-10-16 15:55:38 +11007216 init_waitqueue_head(&conf->wait_for_overlap);
7217 INIT_LIST_HEAD(&conf->handle_list);
Shaohua Li535ae4e2017-02-15 19:37:32 -08007218 INIT_LIST_HEAD(&conf->loprio_list);
Dan Williamsf5efd452009-10-16 15:55:38 +11007219 INIT_LIST_HEAD(&conf->hold_list);
7220 INIT_LIST_HEAD(&conf->delayed_list);
7221 INIT_LIST_HEAD(&conf->bitmap_list);
Shaohua Li773ca822013-08-27 17:50:39 +08007222 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11007223 atomic_set(&conf->active_stripes, 0);
7224 atomic_set(&conf->preread_active_stripes, 0);
7225 atomic_set(&conf->active_aligned_reads, 0);
Shaohua Li765d7042017-01-04 09:33:23 -08007226 spin_lock_init(&conf->pending_bios_lock);
7227 conf->batch_bio_dispatch = true;
7228 rdev_for_each(rdev, mddev) {
7229 if (test_bit(Journal, &rdev->flags))
7230 continue;
7231 if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
7232 conf->batch_bio_dispatch = false;
7233 break;
7234 }
7235 }
7236
Dan Williamsf5efd452009-10-16 15:55:38 +11007237 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11007238 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11007239
7240 conf->raid_disks = mddev->raid_disks;
7241 if (mddev->reshape_position == MaxSector)
7242 conf->previous_raid_disks = mddev->raid_disks;
7243 else
7244 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11007245 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11007246
Kees Cook6396bb22018-06-12 14:03:40 -07007247 conf->disks = kcalloc(max_disks, sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11007248 GFP_KERNEL);
Song Liud7bd3982016-11-23 22:50:39 -08007249
NeilBrown91adb562009-03-31 14:39:39 +11007250 if (!conf->disks)
7251 goto abort;
7252
Song Liud7bd3982016-11-23 22:50:39 -08007253 for (i = 0; i < max_disks; i++) {
7254 conf->disks[i].extra_page = alloc_page(GFP_KERNEL);
7255 if (!conf->disks[i].extra_page)
7256 goto abort;
7257 }
7258
Kent Overstreetafeee512018-05-20 18:25:52 -04007259 ret = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
7260 if (ret)
NeilBrowndd7a8f52017-04-05 14:05:51 +10007261 goto abort;
NeilBrown91adb562009-03-31 14:39:39 +11007262 conf->mddev = mddev;
7263
7264 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
7265 goto abort;
7266
Shaohua Li566c09c2013-11-14 15:16:17 +11007267 /* We init hash_locks[0] separately to that it can be used
7268 * as the reference lock in the spin_lock_nest_lock() call
7269 * in lock_all_device_hash_locks_irq in order to convince
7270 * lockdep that we know what we are doing.
7271 */
7272 spin_lock_init(conf->hash_locks);
7273 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
7274 spin_lock_init(conf->hash_locks + i);
7275
7276 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
7277 INIT_LIST_HEAD(conf->inactive_list + i);
7278
7279 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
7280 INIT_LIST_HEAD(conf->temp_inactive_list + i);
7281
Song Liu1e6d6902016-11-17 15:24:39 -08007282 atomic_set(&conf->r5c_cached_full_stripes, 0);
7283 INIT_LIST_HEAD(&conf->r5c_full_stripe_list);
7284 atomic_set(&conf->r5c_cached_partial_stripes, 0);
7285 INIT_LIST_HEAD(&conf->r5c_partial_stripe_list);
Shaohua Lie33fbb92017-02-10 16:18:09 -08007286 atomic_set(&conf->r5c_flushing_full_stripes, 0);
7287 atomic_set(&conf->r5c_flushing_partial_stripes, 0);
Song Liu1e6d6902016-11-17 15:24:39 -08007288
Dan Williams36d1c642009-07-14 11:48:22 -07007289 conf->level = mddev->new_level;
shli@kernel.org46d5b782014-12-15 12:57:02 +11007290 conf->chunk_sectors = mddev->new_chunk_sectors;
Dan Williams36d1c642009-07-14 11:48:22 -07007291 if (raid5_alloc_percpu(conf) != 0)
7292 goto abort;
7293
NeilBrown0c55e022010-05-03 14:09:02 +10007294 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11007295
NeilBrowndafb20f2012-03-19 12:46:39 +11007296 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11007297 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11007298 if (raid_disk >= max_disks
Shaohua Lif2076e72015-10-08 21:54:12 -07007299 || raid_disk < 0 || test_bit(Journal, &rdev->flags))
NeilBrown91adb562009-03-31 14:39:39 +11007300 continue;
7301 disk = conf->disks + raid_disk;
7302
NeilBrown17045f52011-12-23 10:17:53 +11007303 if (test_bit(Replacement, &rdev->flags)) {
7304 if (disk->replacement)
7305 goto abort;
7306 disk->replacement = rdev;
7307 } else {
7308 if (disk->rdev)
7309 goto abort;
7310 disk->rdev = rdev;
7311 }
NeilBrown91adb562009-03-31 14:39:39 +11007312
7313 if (test_bit(In_sync, &rdev->flags)) {
7314 char b[BDEVNAME_SIZE];
NeilBrowncc6167b2016-11-02 14:16:50 +11007315 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
7316 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05007317 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11007318 /* Cannot rely on bitmap to complete recovery */
7319 conf->fullsync = 1;
7320 }
7321
NeilBrown91adb562009-03-31 14:39:39 +11007322 conf->level = mddev->new_level;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11007323 if (conf->level == 6) {
NeilBrown91adb562009-03-31 14:39:39 +11007324 conf->max_degraded = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11007325 if (raid6_call.xor_syndrome)
7326 conf->rmw_level = PARITY_ENABLE_RMW;
7327 else
7328 conf->rmw_level = PARITY_DISABLE_RMW;
7329 } else {
NeilBrown91adb562009-03-31 14:39:39 +11007330 conf->max_degraded = 1;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11007331 conf->rmw_level = PARITY_ENABLE_RMW;
7332 }
NeilBrown91adb562009-03-31 14:39:39 +11007333 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11007334 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11007335 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10007336 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11007337 conf->prev_algo = mddev->layout;
NeilBrown5cac6bc2015-07-17 12:17:50 +10007338 } else {
7339 conf->prev_chunk_sectors = conf->chunk_sectors;
7340 conf->prev_algo = conf->algorithm;
NeilBrowne183eae2009-03-31 15:20:22 +11007341 }
NeilBrown91adb562009-03-31 14:39:39 +11007342
NeilBrownedbe83a2015-02-26 12:47:56 +11007343 conf->min_nr_stripes = NR_STRIPES;
Shaohua Liad5b0f72016-08-30 10:29:33 -07007344 if (mddev->reshape_position != MaxSector) {
7345 int stripes = max_t(int,
Yufen Yuc911c462020-07-18 05:29:07 -04007346 ((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4,
7347 ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4);
Shaohua Liad5b0f72016-08-30 10:29:33 -07007348 conf->min_nr_stripes = max(NR_STRIPES, stripes);
7349 if (conf->min_nr_stripes != NR_STRIPES)
NeilBrowncc6167b2016-11-02 14:16:50 +11007350 pr_info("md/raid:%s: force stripe size %d for reshape\n",
Shaohua Liad5b0f72016-08-30 10:29:33 -07007351 mdname(mddev), conf->min_nr_stripes);
7352 }
NeilBrownedbe83a2015-02-26 12:47:56 +11007353 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11007354 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li4bda5562013-11-14 15:16:17 +11007355 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
NeilBrownedbe83a2015-02-26 12:47:56 +11007356 if (grow_stripes(conf, conf->min_nr_stripes)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007357 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
7358 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11007359 goto abort;
7360 } else
NeilBrowncc6167b2016-11-02 14:16:50 +11007361 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory);
NeilBrownedbe83a2015-02-26 12:47:56 +11007362 /*
7363 * Losing a stripe head costs more than the time to refill it,
7364 * it reduces the queue depth and so can hurt throughput.
7365 * So set it rather large, scaled by number of devices.
7366 */
7367 conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
7368 conf->shrinker.scan_objects = raid5_cache_scan;
7369 conf->shrinker.count_objects = raid5_cache_count;
7370 conf->shrinker.batch = 128;
7371 conf->shrinker.flags = 0;
Chao Yu6a0f53f2016-09-20 10:33:57 +08007372 if (register_shrinker(&conf->shrinker)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007373 pr_warn("md/raid:%s: couldn't register shrinker.\n",
7374 mdname(mddev));
Chao Yu6a0f53f2016-09-20 10:33:57 +08007375 goto abort;
7376 }
NeilBrown91adb562009-03-31 14:39:39 +11007377
NeilBrown02326052012-07-03 15:56:52 +10007378 sprintf(pers_name, "raid%d", mddev->new_level);
7379 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11007380 if (!conf->thread) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007381 pr_warn("md/raid:%s: couldn't allocate thread.\n",
7382 mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11007383 goto abort;
7384 }
7385
7386 return conf;
7387
7388 abort:
7389 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10007390 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11007391 return ERR_PTR(-EIO);
7392 } else
7393 return ERR_PTR(-ENOMEM);
7394}
7395
NeilBrownc148ffd2009-11-13 17:47:00 +11007396static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
7397{
7398 switch (algo) {
7399 case ALGORITHM_PARITY_0:
7400 if (raid_disk < max_degraded)
7401 return 1;
7402 break;
7403 case ALGORITHM_PARITY_N:
7404 if (raid_disk >= raid_disks - max_degraded)
7405 return 1;
7406 break;
7407 case ALGORITHM_PARITY_0_6:
NeilBrownf72ffdd2014-09-30 14:23:59 +10007408 if (raid_disk == 0 ||
NeilBrownc148ffd2009-11-13 17:47:00 +11007409 raid_disk == raid_disks - 1)
7410 return 1;
7411 break;
7412 case ALGORITHM_LEFT_ASYMMETRIC_6:
7413 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7414 case ALGORITHM_LEFT_SYMMETRIC_6:
7415 case ALGORITHM_RIGHT_SYMMETRIC_6:
7416 if (raid_disk == raid_disks - 1)
7417 return 1;
7418 }
7419 return 0;
7420}
7421
Christoph Hellwig16ef5102020-09-24 08:51:33 +02007422static void raid5_set_io_opt(struct r5conf *conf)
7423{
7424 blk_queue_io_opt(conf->mddev->queue, (conf->chunk_sectors << 9) *
7425 (conf->raid_disks - conf->max_degraded));
7426}
7427
Shaohua Li849674e2016-01-20 13:52:20 -08007428static int raid5_run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11007429{
NeilBrownd1688a62011-10-11 16:49:52 +11007430 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10007431 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11007432 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007433 struct md_rdev *rdev;
Shaohua Li713cf5a2015-08-13 14:32:03 -07007434 struct md_rdev *journal_dev = NULL;
NeilBrownc148ffd2009-11-13 17:47:00 +11007435 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11007436 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10007437 long long min_offset_diff = 0;
7438 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11007439
NeilBrowna415c0f2017-06-05 16:05:13 +10007440 if (mddev_init_writes_pending(mddev) < 0)
7441 return -ENOMEM;
7442
Andre Noll8c6ac8682009-06-18 08:48:06 +10007443 if (mddev->recovery_cp != MaxSector)
NeilBrowncc6167b2016-11-02 14:16:50 +11007444 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
7445 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10007446
7447 rdev_for_each(rdev, mddev) {
7448 long long diff;
Shaohua Li713cf5a2015-08-13 14:32:03 -07007449
Shaohua Lif2076e72015-10-08 21:54:12 -07007450 if (test_bit(Journal, &rdev->flags)) {
Shaohua Li713cf5a2015-08-13 14:32:03 -07007451 journal_dev = rdev;
Shaohua Lif2076e72015-10-08 21:54:12 -07007452 continue;
7453 }
NeilBrownb5254dd2012-05-21 09:27:01 +10007454 if (rdev->raid_disk < 0)
7455 continue;
7456 diff = (rdev->new_data_offset - rdev->data_offset);
7457 if (first) {
7458 min_offset_diff = diff;
7459 first = 0;
7460 } else if (mddev->reshape_backwards &&
7461 diff < min_offset_diff)
7462 min_offset_diff = diff;
7463 else if (!mddev->reshape_backwards &&
7464 diff > min_offset_diff)
7465 min_offset_diff = diff;
7466 }
7467
NeilBrown230b55f2017-10-17 14:24:09 +11007468 if ((test_bit(MD_HAS_JOURNAL, &mddev->flags) || journal_dev) &&
7469 (mddev->bitmap_info.offset || mddev->bitmap_info.file)) {
7470 pr_notice("md/raid:%s: array cannot have both journal and bitmap\n",
7471 mdname(mddev));
7472 return -EINVAL;
7473 }
7474
NeilBrownf6705572006-03-27 01:18:11 -08007475 if (mddev->reshape_position != MaxSector) {
7476 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10007477 * Difficulties arise if the stripe we would write to
7478 * next is at or after the stripe we would read from next.
7479 * For a reshape that changes the number of devices, this
7480 * is only possible for a very short time, and mdadm makes
7481 * sure that time appears to have past before assembling
7482 * the array. So we fail if that time hasn't passed.
7483 * For a reshape that keeps the number of devices the same
7484 * mdadm must be monitoring the reshape can keeping the
7485 * critical areas read-only and backed up. It will start
7486 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08007487 */
7488 sector_t here_new, here_old;
7489 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11007490 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrown05256d92015-07-15 17:36:21 +10007491 int chunk_sectors;
7492 int new_data_disks;
NeilBrownf6705572006-03-27 01:18:11 -08007493
Shaohua Li713cf5a2015-08-13 14:32:03 -07007494 if (journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007495 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
7496 mdname(mddev));
Shaohua Li713cf5a2015-08-13 14:32:03 -07007497 return -EINVAL;
7498 }
7499
NeilBrown88ce4932009-03-31 15:24:23 +11007500 if (mddev->new_level != mddev->level) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007501 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
7502 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007503 return -EINVAL;
7504 }
NeilBrownf6705572006-03-27 01:18:11 -08007505 old_disks = mddev->raid_disks - mddev->delta_disks;
7506 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08007507 * further up in new geometry must map after here in old
7508 * geometry.
NeilBrown05256d92015-07-15 17:36:21 +10007509 * If the chunk sizes are different, then as we perform reshape
7510 * in units of the largest of the two, reshape_position needs
7511 * be a multiple of the largest chunk size times new data disks.
NeilBrownf6705572006-03-27 01:18:11 -08007512 */
7513 here_new = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007514 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
7515 new_data_disks = mddev->raid_disks - max_degraded;
7516 if (sector_div(here_new, chunk_sectors * new_data_disks)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007517 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
7518 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007519 return -EINVAL;
7520 }
NeilBrown05256d92015-07-15 17:36:21 +10007521 reshape_offset = here_new * chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08007522 /* here_new is the stripe we will write to */
7523 here_old = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007524 sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
NeilBrownf4168852007-02-28 20:11:53 -08007525 /* here_old is the first stripe that we might need to read
7526 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10007527 if (mddev->delta_disks == 0) {
7528 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10007529 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10007530 * and taking constant backups.
7531 * mdadm always starts a situation like this in
7532 * readonly mode so it can take control before
7533 * allowing any writes. So just check for that.
7534 */
NeilBrownb5254dd2012-05-21 09:27:01 +10007535 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
7536 abs(min_offset_diff) >= mddev->new_chunk_sectors)
7537 /* not really in-place - so OK */;
7538 else if (mddev->ro == 0) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007539 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
7540 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10007541 return -EINVAL;
7542 }
NeilBrown2c810cd2012-05-21 09:27:00 +10007543 } else if (mddev->reshape_backwards
NeilBrown05256d92015-07-15 17:36:21 +10007544 ? (here_new * chunk_sectors + min_offset_diff <=
7545 here_old * chunk_sectors)
7546 : (here_new * chunk_sectors >=
7547 here_old * chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08007548 /* Reading from the same stripe as writing to - bad */
NeilBrowncc6167b2016-11-02 14:16:50 +11007549 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
7550 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007551 return -EINVAL;
7552 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007553 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007554 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08007555 } else {
NeilBrown91adb562009-03-31 14:39:39 +11007556 BUG_ON(mddev->level != mddev->new_level);
7557 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10007558 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11007559 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08007560 }
7561
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01007562 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) &&
7563 test_bit(MD_HAS_PPL, &mddev->flags)) {
7564 pr_warn("md/raid:%s: using journal device and PPL not allowed - disabling PPL\n",
7565 mdname(mddev));
7566 clear_bit(MD_HAS_PPL, &mddev->flags);
Pawel Baldysiakddc08822017-08-16 17:13:45 +02007567 clear_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags);
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01007568 }
7569
NeilBrown245f46c2009-03-31 14:39:39 +11007570 if (mddev->private == NULL)
7571 conf = setup_conf(mddev);
7572 else
7573 conf = mddev->private;
7574
NeilBrown91adb562009-03-31 14:39:39 +11007575 if (IS_ERR(conf))
7576 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08007577
Song Liu486b0f72016-08-19 15:34:01 -07007578 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
7579 if (!journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007580 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
7581 mdname(mddev));
Song Liu486b0f72016-08-19 15:34:01 -07007582 mddev->ro = 1;
7583 set_disk_ro(mddev->gendisk, 1);
7584 } else if (mddev->recovery_cp == MaxSector)
7585 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
Shaohua Li7dde2ad2015-10-08 21:54:10 -07007586 }
7587
NeilBrownb5254dd2012-05-21 09:27:01 +10007588 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11007589 mddev->thread = conf->thread;
7590 conf->thread = NULL;
7591 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007592
NeilBrown17045f52011-12-23 10:17:53 +11007593 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
7594 i++) {
7595 rdev = conf->disks[i].rdev;
7596 if (!rdev && conf->disks[i].replacement) {
7597 /* The replacement is all we have yet */
7598 rdev = conf->disks[i].replacement;
7599 conf->disks[i].replacement = NULL;
7600 clear_bit(Replacement, &rdev->flags);
7601 conf->disks[i].rdev = rdev;
7602 }
7603 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11007604 continue;
NeilBrown17045f52011-12-23 10:17:53 +11007605 if (conf->disks[i].replacement &&
7606 conf->reshape_progress != MaxSector) {
7607 /* replacements and reshape simply do not mix. */
NeilBrowncc6167b2016-11-02 14:16:50 +11007608 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
NeilBrown17045f52011-12-23 10:17:53 +11007609 goto abort;
7610 }
NeilBrown2f115882010-06-17 17:41:03 +10007611 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11007612 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10007613 continue;
7614 }
NeilBrownc148ffd2009-11-13 17:47:00 +11007615 /* This disc is not fully in-sync. However if it
7616 * just stored parity (beyond the recovery_offset),
7617 * when we don't need to be concerned about the
7618 * array being dirty.
7619 * When reshape goes 'backwards', we never have
7620 * partially completed devices, so we only need
7621 * to worry about reshape going forwards.
7622 */
7623 /* Hack because v0.91 doesn't store recovery_offset properly. */
7624 if (mddev->major_version == 0 &&
7625 mddev->minor_version > 90)
7626 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007627
NeilBrownc148ffd2009-11-13 17:47:00 +11007628 if (rdev->recovery_offset < reshape_offset) {
7629 /* We need to check old and new layout */
7630 if (!only_parity(rdev->raid_disk,
7631 conf->algorithm,
7632 conf->raid_disks,
7633 conf->max_degraded))
7634 continue;
7635 }
7636 if (!only_parity(rdev->raid_disk,
7637 conf->prev_algo,
7638 conf->previous_raid_disks,
7639 conf->max_degraded))
7640 continue;
7641 dirty_parity_disks++;
7642 }
NeilBrown91adb562009-03-31 14:39:39 +11007643
NeilBrown17045f52011-12-23 10:17:53 +11007644 /*
7645 * 0 for a fully functional array, 1 or 2 for a degraded array.
7646 */
Song Liu2e38a372017-01-24 10:45:30 -08007647 mddev->degraded = raid5_calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648
NeilBrown674806d2010-06-16 17:17:53 +10007649 if (has_failed(conf)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007650 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07007651 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007652 goto abort;
7653 }
7654
NeilBrown91adb562009-03-31 14:39:39 +11007655 /* device size must be a multiple of chunk size */
Guoqing Jiangc5eec742020-12-16 02:26:22 +01007656 mddev->dev_sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11007657 mddev->resync_max_sectors = mddev->dev_sectors;
7658
NeilBrownc148ffd2009-11-13 17:47:00 +11007659 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660 mddev->recovery_cp != MaxSector) {
Artur Paszkiewicz4536bf9b2017-03-09 10:00:01 +01007661 if (test_bit(MD_HAS_PPL, &mddev->flags))
7662 pr_crit("md/raid:%s: starting dirty degraded array with PPL.\n",
7663 mdname(mddev));
7664 else if (mddev->ok_start_degraded)
NeilBrowncc6167b2016-11-02 14:16:50 +11007665 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
7666 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007667 else {
NeilBrowncc6167b2016-11-02 14:16:50 +11007668 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
7669 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007670 goto abort;
7671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672 }
7673
NeilBrowncc6167b2016-11-02 14:16:50 +11007674 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
7675 mdname(mddev), conf->level,
7676 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
7677 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007678
7679 print_raid5_conf(conf);
7680
NeilBrownfef9c612009-03-31 15:16:46 +11007681 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11007682 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08007683 atomic_set(&conf->reshape_stripes, 0);
7684 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7685 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7686 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7687 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7688 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007689 "reshape");
Aditya Pakkie406f122019-03-04 16:48:54 -06007690 if (!mddev->sync_thread)
7691 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08007692 }
7693
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10007695 if (mddev->to_remove == &raid5_attrs_group)
7696 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007697 else if (mddev->kobj.sd &&
7698 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrowncc6167b2016-11-02 14:16:50 +11007699 pr_warn("raid5: failed to create sysfs attributes for %s\n",
7700 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10007701 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
7702
7703 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007704 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10007705 /* read-ahead size must cover two whole stripes, which
7706 * is 2 * (datadisks) * chunksize where 'n' is the
7707 * number of raid devices
7708 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007709 int data_disks = conf->previous_raid_disks - conf->max_degraded;
7710 int stripe = data_disks *
7711 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
NeilBrown4a5add42010-06-01 19:37:28 +10007712
NeilBrown9f7c2222010-07-26 12:04:13 +10007713 chunk_size = mddev->chunk_sectors << 9;
7714 blk_queue_io_min(mddev->queue, chunk_size);
Christoph Hellwig16ef5102020-09-24 08:51:33 +02007715 raid5_set_io_opt(conf);
Kent Overstreetc78afc62013-07-11 22:39:53 -07007716 mddev->queue->limits.raid_partial_stripes_expensive = 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007717 /*
7718 * We can only discard a whole stripe. It doesn't make sense to
7719 * discard data disk but write parity disk
7720 */
7721 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11007722 /* Round up to power of 2, as discard handling
7723 * currently assumes that */
7724 while ((stripe-1) & stripe)
7725 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007726 mddev->queue->limits.discard_alignment = stripe;
7727 mddev->queue->limits.discard_granularity = stripe;
Konstantin Khlebnikove8d7c332016-11-27 19:32:32 +03007728
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007729 blk_queue_max_write_same_sectors(mddev->queue, 0);
Christoph Hellwig3deff1a2017-04-05 19:21:03 +02007730 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007731
NeilBrown05616be2012-05-21 09:27:00 +10007732 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007733 disk_stack_limits(mddev->gendisk, rdev->bdev,
7734 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10007735 disk_stack_limits(mddev->gendisk, rdev->bdev,
7736 rdev->new_data_offset << 9);
7737 }
Shaohua Li620125f2012-10-11 13:49:05 +11007738
Christoph Hellwig48920ff2017-04-05 19:21:23 +02007739 /*
7740 * zeroing is required, otherwise data
7741 * could be lost. Consider a scenario: discard a stripe
7742 * (the stripe could be inconsistent if
7743 * discard_zeroes_data is 0); write one disk of the
7744 * stripe (the stripe could be inconsistent again
7745 * depending on which disks are used to calculate
7746 * parity); the disk is broken; The stripe data of this
7747 * disk is lost.
7748 *
7749 * We only allow DISCARD if the sysadmin has confirmed that
7750 * only safe devices are in use by setting a module parameter.
7751 * A better idea might be to turn DISCARD into WRITE_ZEROES
7752 * requests, as that is required to be safe.
7753 */
7754 if (devices_handle_discard_safely &&
Jes Sorensene7597e62016-02-16 16:44:24 -05007755 mddev->queue->limits.max_discard_sectors >= (stripe >> 9) &&
7756 mddev->queue->limits.discard_granularity >= stripe)
Bart Van Assche8b904b52018-03-07 17:10:10 -08007757 blk_queue_flag_set(QUEUE_FLAG_DISCARD,
Shaohua Li620125f2012-10-11 13:49:05 +11007758 mddev->queue);
7759 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08007760 blk_queue_flag_clear(QUEUE_FLAG_DISCARD,
Shaohua Li620125f2012-10-11 13:49:05 +11007761 mddev->queue);
Shaohua Li1dffddd2016-09-08 10:49:06 -07007762
7763 blk_queue_max_hw_sectors(mddev->queue, UINT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007764 }
7765
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02007766 if (log_init(conf, journal_dev, raid5_has_ppl(conf)))
Artur Paszkiewiczff875732017-03-09 09:59:58 +01007767 goto abort;
Shaohua Li5c7e81c2015-08-13 14:32:04 -07007768
Linus Torvalds1da177e2005-04-16 15:20:36 -07007769 return 0;
7770abort:
NeilBrown01f96c02011-09-21 15:30:20 +10007771 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11007772 print_raid5_conf(conf);
7773 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007774 mddev->private = NULL;
NeilBrowncc6167b2016-11-02 14:16:50 +11007775 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007776 return -EIO;
7777}
7778
NeilBrownafa0f552014-12-15 12:56:58 +11007779static void raid5_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007780{
NeilBrownafa0f552014-12-15 12:56:58 +11007781 struct r5conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007782
Dan Williams95fc17a2009-07-31 12:39:15 +10007783 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10007784 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007785}
7786
Shaohua Li849674e2016-01-20 13:52:20 -08007787static void raid5_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007788{
NeilBrownd1688a62011-10-11 16:49:52 +11007789 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007790 int i;
7791
Andre Noll9d8f0362009-06-18 08:45:01 +10007792 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
NeilBrown3cb5edf2015-07-15 17:24:17 +10007793 conf->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07007794 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
NeilBrown5fd13352016-06-02 16:19:52 +10007795 rcu_read_lock();
7796 for (i = 0; i < conf->raid_disks; i++) {
7797 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
7798 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
7799 }
7800 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007801 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007802}
7803
NeilBrownd1688a62011-10-11 16:49:52 +11007804static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007805{
7806 int i;
7807 struct disk_info *tmp;
7808
NeilBrowncc6167b2016-11-02 14:16:50 +11007809 pr_debug("RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007810 if (!conf) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007811 pr_debug("(conf==NULL)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812 return;
7813 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007814 pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level,
NeilBrown0c55e022010-05-03 14:09:02 +10007815 conf->raid_disks,
7816 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007817
7818 for (i = 0; i < conf->raid_disks; i++) {
7819 char b[BDEVNAME_SIZE];
7820 tmp = conf->disks + i;
7821 if (tmp->rdev)
NeilBrowncc6167b2016-11-02 14:16:50 +11007822 pr_debug(" disk %d, o:%d, dev:%s\n",
NeilBrown0c55e022010-05-03 14:09:02 +10007823 i, !test_bit(Faulty, &tmp->rdev->flags),
7824 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007825 }
7826}
7827
NeilBrownfd01b882011-10-11 16:47:53 +11007828static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007829{
7830 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11007831 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007832 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10007833 int count = 0;
7834 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007835
7836 for (i = 0; i < conf->raid_disks; i++) {
7837 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11007838 if (tmp->replacement
7839 && tmp->replacement->recovery_offset == MaxSector
7840 && !test_bit(Faulty, &tmp->replacement->flags)
7841 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
7842 /* Replacement has just become active. */
7843 if (!tmp->rdev
7844 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
7845 count++;
7846 if (tmp->rdev) {
7847 /* Replaced device not technically faulty,
7848 * but we need to be sure it gets removed
7849 * and never re-added.
7850 */
7851 set_bit(Faulty, &tmp->rdev->flags);
7852 sysfs_notify_dirent_safe(
7853 tmp->rdev->sysfs_state);
7854 }
7855 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
7856 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10007857 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08007858 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07007859 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10007860 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11007861 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007862 }
7863 }
NeilBrown6b965622010-08-18 11:56:59 +10007864 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08007865 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007866 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007867 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007868 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007869}
7870
NeilBrownb8321b62011-12-23 10:17:51 +11007871static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007872{
NeilBrownd1688a62011-10-11 16:49:52 +11007873 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11007875 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11007876 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007877 struct disk_info *p = conf->disks + number;
7878
7879 print_raid5_conf(conf);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007880 if (test_bit(Journal, &rdev->flags) && conf->log) {
Shaohua Lic2bb6242015-10-08 21:54:07 -07007881 /*
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007882 * we can't wait pending write here, as this is called in
7883 * raid5d, wait will deadlock.
NeilBrown84dd97a2017-03-15 14:05:14 +11007884 * neilb: there is no locking about new writes here,
7885 * so this cannot be safe.
Shaohua Lic2bb6242015-10-08 21:54:07 -07007886 */
Song Liu70d466f2017-05-11 15:28:28 -07007887 if (atomic_read(&conf->active_stripes) ||
7888 atomic_read(&conf->r5c_cached_full_stripes) ||
7889 atomic_read(&conf->r5c_cached_partial_stripes)) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007890 return -EBUSY;
NeilBrown84dd97a2017-03-15 14:05:14 +11007891 }
Artur Paszkiewiczff875732017-03-09 09:59:58 +01007892 log_exit(conf);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007893 return 0;
Shaohua Lic2bb6242015-10-08 21:54:07 -07007894 }
NeilBrown657e3e42011-12-23 10:17:52 +11007895 if (rdev == p->rdev)
7896 rdevp = &p->rdev;
7897 else if (rdev == p->replacement)
7898 rdevp = &p->replacement;
7899 else
7900 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11007901
NeilBrown657e3e42011-12-23 10:17:52 +11007902 if (number >= conf->raid_disks &&
7903 conf->reshape_progress == MaxSector)
7904 clear_bit(In_sync, &rdev->flags);
7905
7906 if (test_bit(In_sync, &rdev->flags) ||
7907 atomic_read(&rdev->nr_pending)) {
7908 err = -EBUSY;
7909 goto abort;
7910 }
7911 /* Only remove non-faulty devices if recovery
7912 * isn't possible.
7913 */
7914 if (!test_bit(Faulty, &rdev->flags) &&
7915 mddev->recovery_disabled != conf->recovery_disabled &&
7916 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11007917 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11007918 number < conf->raid_disks) {
7919 err = -EBUSY;
7920 goto abort;
7921 }
7922 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10007923 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
7924 synchronize_rcu();
7925 if (atomic_read(&rdev->nr_pending)) {
7926 /* lost the race, try later */
7927 err = -EBUSY;
7928 *rdevp = rdev;
7929 }
7930 }
Artur Paszkiewicz6358c232017-03-09 10:00:02 +01007931 if (!err) {
7932 err = log_modify(conf, rdev, false);
7933 if (err)
7934 goto abort;
7935 }
NeilBrownd787be42016-06-02 16:19:53 +10007936 if (p->replacement) {
NeilBrowndd054fc2011-12-23 10:17:53 +11007937 /* We must have just cleared 'rdev' */
7938 p->rdev = p->replacement;
7939 clear_bit(Replacement, &p->replacement->flags);
7940 smp_mb(); /* Make sure other CPUs may see both as identical
7941 * but will never see neither - if they are careful
7942 */
7943 p->replacement = NULL;
Artur Paszkiewicz6358c232017-03-09 10:00:02 +01007944
7945 if (!err)
7946 err = log_modify(conf, p->rdev, true);
Guoqing Jiange5bc9c32017-04-24 15:58:04 +08007947 }
7948
7949 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007950abort:
7951
7952 print_raid5_conf(conf);
7953 return err;
7954}
7955
NeilBrownfd01b882011-10-11 16:47:53 +11007956static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007957{
NeilBrownd1688a62011-10-11 16:49:52 +11007958 struct r5conf *conf = mddev->private;
Xiao Nid9771f52019-06-14 15:41:05 -07007959 int ret, err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007960 int disk;
7961 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10007962 int first = 0;
7963 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007964
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007965 if (test_bit(Journal, &rdev->flags)) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007966 if (conf->log)
7967 return -EBUSY;
7968
7969 rdev->raid_disk = 0;
7970 /*
7971 * The array is in readonly mode if journal is missing, so no
7972 * write requests running. We should be safe
7973 */
Xiao Nid9771f52019-06-14 15:41:05 -07007974 ret = log_init(conf, rdev, false);
7975 if (ret)
7976 return ret;
7977
7978 ret = r5l_start(conf->log);
7979 if (ret)
7980 return ret;
7981
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007982 return 0;
7983 }
NeilBrown7f0da592011-07-28 11:39:22 +10007984 if (mddev->recovery_disabled == conf->recovery_disabled)
7985 return -EBUSY;
7986
NeilBrowndc10c642012-03-19 12:46:37 +11007987 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007988 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10007989 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007990
Neil Brown6c2fce22008-06-28 08:31:31 +10007991 if (rdev->raid_disk >= 0)
7992 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007993
7994 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07007995 * find the disk ... but prefer rdev->saved_raid_disk
7996 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007997 */
NeilBrown16a53ec2006-06-26 00:27:38 -07007998 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10007999 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07008000 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10008001 first = rdev->saved_raid_disk;
8002
8003 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11008004 p = conf->disks + disk;
8005 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08008006 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008007 rdev->raid_disk = disk;
NeilBrown72626682005-09-09 16:23:54 -07008008 if (rdev->saved_raid_disk != disk)
8009 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08008010 rcu_assign_pointer(p->rdev, rdev);
Artur Paszkiewicz6358c232017-03-09 10:00:02 +01008011
8012 err = log_modify(conf, rdev, true);
8013
NeilBrown5cfb22a2012-07-03 11:46:53 +10008014 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008015 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10008016 }
8017 for (disk = first; disk <= last; disk++) {
8018 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11008019 if (test_bit(WantReplacement, &p->rdev->flags) &&
8020 p->replacement == NULL) {
8021 clear_bit(In_sync, &rdev->flags);
8022 set_bit(Replacement, &rdev->flags);
8023 rdev->raid_disk = disk;
8024 err = 0;
8025 conf->fullsync = 1;
8026 rcu_assign_pointer(p->replacement, rdev);
8027 break;
8028 }
8029 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10008030out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07008031 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10008032 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008033}
8034
NeilBrownfd01b882011-10-11 16:47:53 +11008035static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008036{
8037 /* no resync is happening, and there is enough space
8038 * on all devices, so we can resize.
8039 * We need to make sure resync covers any new space.
8040 * If the array is shrinking we should possibly wait until
8041 * any io in the removed space completes, but it hardly seems
8042 * worth it.
8043 */
NeilBrowna4a61252012-05-22 13:55:27 +10008044 sector_t newsize;
NeilBrown3cb5edf2015-07-15 17:24:17 +10008045 struct r5conf *conf = mddev->private;
8046
Shaohua Lie254de62018-08-29 11:05:42 -07008047 if (raid5_has_log(conf) || raid5_has_ppl(conf))
Shaohua Li713cf5a2015-08-13 14:32:03 -07008048 return -EINVAL;
NeilBrown3cb5edf2015-07-15 17:24:17 +10008049 sectors &= ~((sector_t)conf->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10008050 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
8051 if (mddev->external_size &&
8052 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11008053 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10008054 if (mddev->bitmap) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07008055 int ret = md_bitmap_resize(mddev->bitmap, sectors, 0, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10008056 if (ret)
8057 return ret;
8058 }
8059 md_set_array_sectors(mddev, newsize);
NeilBrownb0986362011-05-11 15:52:21 +10008060 if (sectors > mddev->dev_sectors &&
8061 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11008062 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008063 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
8064 }
Andre Noll58c0fed2009-03-31 14:33:13 +11008065 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07008066 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008067 return 0;
8068}
8069
NeilBrownfd01b882011-10-11 16:47:53 +11008070static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10008071{
8072 /* Can only proceed if there are plenty of stripe_heads.
8073 * We need a minimum of one full stripe,, and for sensible progress
8074 * it is best to have about 4 times that.
8075 * If we require 4 times, then the default 256 4K stripe_heads will
8076 * allow for chunk sizes up to 256K, which is probably OK.
8077 * If the chunk size is greater, user-space should request more
8078 * stripe_heads first.
8079 */
NeilBrownd1688a62011-10-11 16:49:52 +11008080 struct r5conf *conf = mddev->private;
Yufen Yuc911c462020-07-18 05:29:07 -04008081 if (((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11008082 > conf->min_nr_stripes ||
Yufen Yuc911c462020-07-18 05:29:07 -04008083 ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11008084 > conf->min_nr_stripes) {
NeilBrowncc6167b2016-11-02 14:16:50 +11008085 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
8086 mdname(mddev),
8087 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
Yufen Yuc911c462020-07-18 05:29:07 -04008088 / RAID5_STRIPE_SIZE(conf))*4);
NeilBrown01ee22b2009-06-18 08:47:20 +10008089 return 0;
8090 }
8091 return 1;
8092}
8093
NeilBrownfd01b882011-10-11 16:47:53 +11008094static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08008095{
NeilBrownd1688a62011-10-11 16:49:52 +11008096 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08008097
Shaohua Lie254de62018-08-29 11:05:42 -07008098 if (raid5_has_log(conf) || raid5_has_ppl(conf))
Shaohua Li713cf5a2015-08-13 14:32:03 -07008099 return -EINVAL;
NeilBrown88ce4932009-03-31 15:24:23 +11008100 if (mddev->delta_disks == 0 &&
8101 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10008102 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10008103 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10008104 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11008105 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10008106 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11008107 /* We might be able to shrink, but the devices must
8108 * be made bigger first.
8109 * For raid6, 4 is the minimum size.
8110 * Otherwise 2 is the minimum
8111 */
8112 int min = 2;
8113 if (mddev->level == 6)
8114 min = 4;
8115 if (mddev->raid_disks + mddev->delta_disks < min)
8116 return -EINVAL;
8117 }
NeilBrown29269552006-03-27 01:18:10 -08008118
NeilBrown01ee22b2009-06-18 08:47:20 +10008119 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08008120 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08008121
NeilBrown738a2732015-05-08 18:19:39 +10008122 if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
8123 mddev->delta_disks > 0)
8124 if (resize_chunks(conf,
8125 conf->previous_raid_disks
8126 + max(0, mddev->delta_disks),
8127 max(mddev->new_chunk_sectors,
8128 mddev->chunk_sectors)
8129 ) < 0)
8130 return -ENOMEM;
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02008131
8132 if (conf->previous_raid_disks + mddev->delta_disks <= conf->pool_size)
8133 return 0; /* never bother to shrink */
NeilBrowne56108d62012-10-11 14:24:13 +11008134 return resize_stripes(conf, (conf->previous_raid_disks
8135 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08008136}
8137
NeilBrownfd01b882011-10-11 16:47:53 +11008138static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08008139{
NeilBrownd1688a62011-10-11 16:49:52 +11008140 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11008141 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08008142 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07008143 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08008144
NeilBrownf4168852007-02-28 20:11:53 -08008145 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08008146 return -EBUSY;
8147
NeilBrown01ee22b2009-06-18 08:47:20 +10008148 if (!check_stripe_cache(mddev))
8149 return -ENOSPC;
8150
NeilBrown30b67642012-05-22 13:55:28 +10008151 if (has_failed(conf))
8152 return -EINVAL;
8153
NeilBrownc6563a82012-05-21 09:27:00 +10008154 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11008155 if (!test_bit(In_sync, &rdev->flags)
8156 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08008157 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10008158 }
NeilBrown63c70c42006-03-27 01:18:13 -08008159
NeilBrownf4168852007-02-28 20:11:53 -08008160 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08008161 /* Not enough devices even to make a degraded array
8162 * of that size
8163 */
8164 return -EINVAL;
8165
NeilBrownec32a2b2009-03-31 15:17:38 +11008166 /* Refuse to reduce size of the array. Any reductions in
8167 * array size must be through explicit setting of array_size
8168 * attribute.
8169 */
8170 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
8171 < mddev->array_sectors) {
NeilBrowncc6167b2016-11-02 14:16:50 +11008172 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
8173 mdname(mddev));
NeilBrownec32a2b2009-03-31 15:17:38 +11008174 return -EINVAL;
8175 }
8176
NeilBrownf6705572006-03-27 01:18:11 -08008177 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08008178 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10008179 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08008180 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08008181 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10008182 conf->prev_chunk_sectors = conf->chunk_sectors;
8183 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11008184 conf->prev_algo = conf->algorithm;
8185 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10008186 conf->generation++;
8187 /* Code that selects data_offset needs to see the generation update
8188 * if reshape_progress has been set - so a memory barrier needed.
8189 */
8190 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10008191 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11008192 conf->reshape_progress = raid5_size(mddev, 0, 0);
8193 else
8194 conf->reshape_progress = 0;
8195 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10008196 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08008197 spin_unlock_irq(&conf->device_lock);
8198
NeilBrown4d77e3b2013-08-27 15:57:47 +10008199 /* Now make sure any requests that proceeded on the assumption
8200 * the reshape wasn't running - like Discard or Read - have
8201 * completed.
8202 */
8203 mddev_suspend(mddev);
8204 mddev_resume(mddev);
8205
NeilBrown29269552006-03-27 01:18:10 -08008206 /* Add some new drives, as many as will fit.
8207 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10008208 * Don't add devices if we are reducing the number of
8209 * devices in the array. This is because it is not possible
8210 * to correctly record the "partially reconstructed" state of
8211 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08008212 */
NeilBrown87a8dec2011-01-31 11:57:43 +11008213 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11008214 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11008215 if (rdev->raid_disk < 0 &&
8216 !test_bit(Faulty, &rdev->flags)) {
8217 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11008218 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11008219 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11008220 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11008221 else
NeilBrown87a8dec2011-01-31 11:57:43 +11008222 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10008223
Damien Le Moal2aada5b2020-07-16 13:54:42 +09008224 /* Failure here is OK */
8225 sysfs_link_rdev(mddev, rdev);
NeilBrown50da0842011-01-31 11:57:43 +11008226 }
NeilBrown87a8dec2011-01-31 11:57:43 +11008227 } else if (rdev->raid_disk >= conf->previous_raid_disks
8228 && !test_bit(Faulty, &rdev->flags)) {
8229 /* This is a spare that was manually added */
8230 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11008231 }
NeilBrown29269552006-03-27 01:18:10 -08008232
NeilBrown87a8dec2011-01-31 11:57:43 +11008233 /* When a reshape changes the number of devices,
8234 * ->degraded is measured against the larger of the
8235 * pre and post number of devices.
8236 */
NeilBrownec32a2b2009-03-31 15:17:38 +11008237 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08008238 mddev->degraded = raid5_calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11008239 spin_unlock_irqrestore(&conf->device_lock, flags);
8240 }
NeilBrown63c70c42006-03-27 01:18:13 -08008241 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10008242 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08008243 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownf6705572006-03-27 01:18:11 -08008244
NeilBrown29269552006-03-27 01:18:10 -08008245 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8246 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008247 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown29269552006-03-27 01:18:10 -08008248 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8249 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8250 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10008251 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08008252 if (!mddev->sync_thread) {
8253 mddev->recovery = 0;
8254 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11008255 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08008256 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11008257 mddev->new_chunk_sectors =
8258 conf->chunk_sectors = conf->prev_chunk_sectors;
8259 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10008260 rdev_for_each(rdev, mddev)
8261 rdev->new_data_offset = rdev->data_offset;
8262 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11008263 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11008264 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11008265 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11008266 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08008267 spin_unlock_irq(&conf->device_lock);
8268 return -EAGAIN;
8269 }
NeilBrownc8f517c2009-03-31 15:28:40 +11008270 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08008271 md_wakeup_thread(mddev->sync_thread);
8272 md_new_event(mddev);
8273 return 0;
8274}
NeilBrown29269552006-03-27 01:18:10 -08008275
NeilBrownec32a2b2009-03-31 15:17:38 +11008276/* This is called from the reshape thread and should make any
8277 * changes needed in 'conf'
8278 */
NeilBrownd1688a62011-10-11 16:49:52 +11008279static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08008280{
NeilBrown29269552006-03-27 01:18:10 -08008281
NeilBrownf6705572006-03-27 01:18:11 -08008282 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrowndb0505d2017-10-17 16:18:36 +11008283 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07008284
NeilBrownf6705572006-03-27 01:18:11 -08008285 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11008286 conf->previous_raid_disks = conf->raid_disks;
Xiao Nib5d27712017-07-05 17:34:04 +08008287 md_finish_reshape(conf->mddev);
NeilBrown05616be2012-05-21 09:27:00 +10008288 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11008289 conf->reshape_progress = MaxSector;
NeilBrown6cbd8142015-07-24 13:30:32 +10008290 conf->mddev->reshape_position = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11008291 rdev_for_each(rdev, conf->mddev)
8292 if (rdev->raid_disk >= 0 &&
8293 !test_bit(Journal, &rdev->flags) &&
8294 !test_bit(In_sync, &rdev->flags))
8295 rdev->recovery_offset = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08008296 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11008297 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07008298
Christoph Hellwigc2e4cd52020-09-24 08:51:34 +02008299 if (conf->mddev->queue)
Christoph Hellwig16ef5102020-09-24 08:51:33 +02008300 raid5_set_io_opt(conf);
NeilBrown29269552006-03-27 01:18:10 -08008301 }
NeilBrown29269552006-03-27 01:18:10 -08008302}
8303
NeilBrownec32a2b2009-03-31 15:17:38 +11008304/* This is called from the raid5d thread with mddev_lock held.
8305 * It makes config changes to the device.
8306 */
NeilBrownfd01b882011-10-11 16:47:53 +11008307static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11008308{
NeilBrownd1688a62011-10-11 16:49:52 +11008309 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11008310
8311 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8312
BingJing Chang88763912018-02-22 13:34:46 +08008313 if (mddev->delta_disks <= 0) {
NeilBrownec32a2b2009-03-31 15:17:38 +11008314 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11008315 spin_lock_irq(&conf->device_lock);
Song Liu2e38a372017-01-24 10:45:30 -08008316 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11008317 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11008318 for (d = conf->raid_disks ;
8319 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10008320 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11008321 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10008322 if (rdev)
8323 clear_bit(In_sync, &rdev->flags);
8324 rdev = conf->disks[d].replacement;
8325 if (rdev)
8326 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10008327 }
NeilBrowncea9c222009-03-31 15:15:05 +11008328 }
NeilBrown88ce4932009-03-31 15:24:23 +11008329 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10008330 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11008331 mddev->reshape_position = MaxSector;
8332 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10008333 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11008334 }
8335}
8336
NeilBrownb03e0cc2017-10-19 12:49:15 +11008337static void raid5_quiesce(struct mddev *mddev, int quiesce)
NeilBrown72626682005-09-09 16:23:54 -07008338{
NeilBrownd1688a62011-10-11 16:49:52 +11008339 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07008340
NeilBrownb03e0cc2017-10-19 12:49:15 +11008341 if (quiesce) {
8342 /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11008343 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10008344 /* '2' tells resync/reshape to pause so that all
8345 * active stripes can drain
8346 */
Song Liua39f7af2016-11-17 15:24:40 -08008347 r5c_flush_cache(conf, INT_MAX);
NeilBrown64bd6602009-08-03 10:59:58 +10008348 conf->quiesce = 2;
Yuanhan Liub1b46482015-05-08 18:19:06 +10008349 wait_event_cmd(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08008350 atomic_read(&conf->active_stripes) == 0 &&
8351 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11008352 unlock_all_device_hash_locks_irq(conf),
8353 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10008354 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11008355 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10008356 /* allow reshape to continue */
8357 wake_up(&conf->wait_for_overlap);
NeilBrownb03e0cc2017-10-19 12:49:15 +11008358 } else {
8359 /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11008360 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07008361 conf->quiesce = 0;
Yuanhan Liub1b46482015-05-08 18:19:06 +10008362 wake_up(&conf->wait_for_quiescent);
NeilBrowne464eaf2006-03-27 01:18:14 -08008363 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11008364 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07008365 }
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01008366 log_quiesce(conf, quiesce);
NeilBrown72626682005-09-09 16:23:54 -07008367}
NeilBrownb15c2e52006-01-06 00:20:16 -08008368
NeilBrownfd01b882011-10-11 16:47:53 +11008369static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11008370{
NeilBrowne373ab12011-10-11 16:48:59 +11008371 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07008372 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11008373
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008374 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11008375 if (raid0_conf->nr_strip_zones > 1) {
NeilBrowncc6167b2016-11-02 14:16:50 +11008376 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
8377 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008378 return ERR_PTR(-EINVAL);
8379 }
8380
NeilBrowne373ab12011-10-11 16:48:59 +11008381 sectors = raid0_conf->strip_zone[0].zone_end;
8382 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10008383 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008384 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11008385 mddev->new_layout = ALGORITHM_PARITY_N;
8386 mddev->new_chunk_sectors = mddev->chunk_sectors;
8387 mddev->raid_disks += 1;
8388 mddev->delta_disks = 1;
8389 /* make sure it will be not marked as dirty */
8390 mddev->recovery_cp = MaxSector;
8391
8392 return setup_conf(mddev);
8393}
8394
NeilBrownfd01b882011-10-11 16:47:53 +11008395static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11008396{
8397 int chunksect;
Shaohua Li6995f0b2016-12-08 15:48:17 -08008398 void *ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11008399
8400 if (mddev->raid_disks != 2 ||
8401 mddev->degraded > 1)
8402 return ERR_PTR(-EINVAL);
8403
8404 /* Should check if there are write-behind devices? */
8405
8406 chunksect = 64*2; /* 64K by default */
8407
8408 /* The array must be an exact multiple of chunksize */
8409 while (chunksect && (mddev->array_sectors & (chunksect-1)))
8410 chunksect >>= 1;
8411
Yufen Yuc911c462020-07-18 05:29:07 -04008412 if ((chunksect<<9) < RAID5_STRIPE_SIZE((struct r5conf *)mddev->private))
NeilBrownd562b0c2009-03-31 14:39:39 +11008413 /* array size does not allow a suitable chunk size */
8414 return ERR_PTR(-EINVAL);
8415
8416 mddev->new_level = 5;
8417 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10008418 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11008419
Shaohua Li6995f0b2016-12-08 15:48:17 -08008420 ret = setup_conf(mddev);
Jes Sorensen32cd7cb2017-01-06 19:31:35 -05008421 if (!IS_ERR(ret))
Shaohua Li394ed8e2017-01-04 16:10:19 -08008422 mddev_clear_unsupported_flags(mddev,
8423 UNSUPPORTED_MDDEV_FLAGS);
Shaohua Li6995f0b2016-12-08 15:48:17 -08008424 return ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11008425}
8426
NeilBrownfd01b882011-10-11 16:47:53 +11008427static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11008428{
8429 int new_layout;
8430
8431 switch (mddev->layout) {
8432 case ALGORITHM_LEFT_ASYMMETRIC_6:
8433 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
8434 break;
8435 case ALGORITHM_RIGHT_ASYMMETRIC_6:
8436 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
8437 break;
8438 case ALGORITHM_LEFT_SYMMETRIC_6:
8439 new_layout = ALGORITHM_LEFT_SYMMETRIC;
8440 break;
8441 case ALGORITHM_RIGHT_SYMMETRIC_6:
8442 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
8443 break;
8444 case ALGORITHM_PARITY_0_6:
8445 new_layout = ALGORITHM_PARITY_0;
8446 break;
8447 case ALGORITHM_PARITY_N:
8448 new_layout = ALGORITHM_PARITY_N;
8449 break;
8450 default:
8451 return ERR_PTR(-EINVAL);
8452 }
8453 mddev->new_level = 5;
8454 mddev->new_layout = new_layout;
8455 mddev->delta_disks = -1;
8456 mddev->raid_disks -= 1;
8457 return setup_conf(mddev);
8458}
8459
NeilBrownfd01b882011-10-11 16:47:53 +11008460static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11008461{
NeilBrown88ce4932009-03-31 15:24:23 +11008462 /* For a 2-drive array, the layout and chunk size can be changed
8463 * immediately as not restriping is needed.
8464 * For larger arrays we record the new value - after validation
8465 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11008466 */
NeilBrownd1688a62011-10-11 16:49:52 +11008467 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10008468 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11008469
NeilBrown597a7112009-06-18 08:47:42 +10008470 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11008471 return -EINVAL;
8472 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008473 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11008474 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008475 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11008476 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008477 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11008478 /* not factor of array size */
8479 return -EINVAL;
8480 }
8481
8482 /* They look valid */
8483
NeilBrown88ce4932009-03-31 15:24:23 +11008484 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10008485 /* can make the change immediately */
8486 if (mddev->new_layout >= 0) {
8487 conf->algorithm = mddev->new_layout;
8488 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11008489 }
8490 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10008491 conf->chunk_sectors = new_chunk ;
8492 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11008493 }
Shaohua Li29530792016-12-08 15:48:19 -08008494 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown88ce4932009-03-31 15:24:23 +11008495 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11008496 }
NeilBrown50ac1682009-06-18 08:47:55 +10008497 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11008498}
8499
NeilBrownfd01b882011-10-11 16:47:53 +11008500static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11008501{
NeilBrown597a7112009-06-18 08:47:42 +10008502 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10008503
NeilBrown597a7112009-06-18 08:47:42 +10008504 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11008505 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008506 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008507 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11008508 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008509 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11008510 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008511 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11008512 /* not factor of array size */
8513 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008514 }
NeilBrown88ce4932009-03-31 15:24:23 +11008515
8516 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10008517 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11008518}
8519
NeilBrownfd01b882011-10-11 16:47:53 +11008520static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11008521{
8522 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008523 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008524 * raid1 - if there are two drives. We need to know the chunk size
8525 * raid4 - trivial - just use a raid4 layout.
8526 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008527 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008528 if (mddev->level == 0)
8529 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11008530 if (mddev->level == 1)
8531 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11008532 if (mddev->level == 4) {
8533 mddev->new_layout = ALGORITHM_PARITY_N;
8534 mddev->new_level = 5;
8535 return setup_conf(mddev);
8536 }
NeilBrownfc9739c2009-03-31 14:57:20 +11008537 if (mddev->level == 6)
8538 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11008539
8540 return ERR_PTR(-EINVAL);
8541}
8542
NeilBrownfd01b882011-10-11 16:47:53 +11008543static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11008544{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008545 /* raid4 can take over:
8546 * raid0 - if there is only one strip zone
8547 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11008548 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008549 if (mddev->level == 0)
8550 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11008551 if (mddev->level == 5 &&
8552 mddev->layout == ALGORITHM_PARITY_N) {
8553 mddev->new_layout = 0;
8554 mddev->new_level = 4;
8555 return setup_conf(mddev);
8556 }
8557 return ERR_PTR(-EINVAL);
8558}
NeilBrownd562b0c2009-03-31 14:39:39 +11008559
NeilBrown84fc4b52011-10-11 16:49:58 +11008560static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11008561
NeilBrownfd01b882011-10-11 16:47:53 +11008562static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11008563{
8564 /* Currently can only take over a raid5. We map the
8565 * personality to an equivalent raid6 personality
8566 * with the Q block at the end.
8567 */
8568 int new_layout;
8569
8570 if (mddev->pers != &raid5_personality)
8571 return ERR_PTR(-EINVAL);
8572 if (mddev->degraded > 1)
8573 return ERR_PTR(-EINVAL);
8574 if (mddev->raid_disks > 253)
8575 return ERR_PTR(-EINVAL);
8576 if (mddev->raid_disks < 3)
8577 return ERR_PTR(-EINVAL);
8578
8579 switch (mddev->layout) {
8580 case ALGORITHM_LEFT_ASYMMETRIC:
8581 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
8582 break;
8583 case ALGORITHM_RIGHT_ASYMMETRIC:
8584 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
8585 break;
8586 case ALGORITHM_LEFT_SYMMETRIC:
8587 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
8588 break;
8589 case ALGORITHM_RIGHT_SYMMETRIC:
8590 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
8591 break;
8592 case ALGORITHM_PARITY_0:
8593 new_layout = ALGORITHM_PARITY_0_6;
8594 break;
8595 case ALGORITHM_PARITY_N:
8596 new_layout = ALGORITHM_PARITY_N;
8597 break;
8598 default:
8599 return ERR_PTR(-EINVAL);
8600 }
8601 mddev->new_level = 6;
8602 mddev->new_layout = new_layout;
8603 mddev->delta_disks = 1;
8604 mddev->raid_disks += 1;
8605 return setup_conf(mddev);
8606}
8607
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01008608static int raid5_change_consistency_policy(struct mddev *mddev, const char *buf)
8609{
8610 struct r5conf *conf;
8611 int err;
8612
8613 err = mddev_lock(mddev);
8614 if (err)
8615 return err;
8616 conf = mddev->private;
8617 if (!conf) {
8618 mddev_unlock(mddev);
8619 return -ENODEV;
8620 }
8621
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02008622 if (strncmp(buf, "ppl", 3) == 0) {
Song Liu0bb0c102017-03-27 10:51:33 -07008623 /* ppl only works with RAID 5 */
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02008624 if (!raid5_has_ppl(conf) && conf->level == 5) {
8625 err = log_init(conf, NULL, true);
8626 if (!err) {
8627 err = resize_stripes(conf, conf->pool_size);
8628 if (err)
8629 log_exit(conf);
8630 }
Song Liu0bb0c102017-03-27 10:51:33 -07008631 } else
8632 err = -EINVAL;
8633 } else if (strncmp(buf, "resync", 6) == 0) {
8634 if (raid5_has_ppl(conf)) {
8635 mddev_suspend(mddev);
8636 log_exit(conf);
Song Liu0bb0c102017-03-27 10:51:33 -07008637 mddev_resume(mddev);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02008638 err = resize_stripes(conf, conf->pool_size);
Song Liu0bb0c102017-03-27 10:51:33 -07008639 } else if (test_bit(MD_HAS_JOURNAL, &conf->mddev->flags) &&
8640 r5l_log_disk_error(conf)) {
8641 bool journal_dev_exists = false;
8642 struct md_rdev *rdev;
8643
8644 rdev_for_each(rdev, mddev)
8645 if (test_bit(Journal, &rdev->flags)) {
8646 journal_dev_exists = true;
8647 break;
8648 }
8649
8650 if (!journal_dev_exists) {
8651 mddev_suspend(mddev);
8652 clear_bit(MD_HAS_JOURNAL, &mddev->flags);
8653 mddev_resume(mddev);
8654 } else /* need remove journal device first */
8655 err = -EBUSY;
8656 } else
8657 err = -EINVAL;
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01008658 } else {
8659 err = -EINVAL;
8660 }
8661
8662 if (!err)
8663 md_update_sb(mddev, 1);
8664
8665 mddev_unlock(mddev);
8666
8667 return err;
8668}
8669
Song Liud5d885f2017-11-19 22:17:01 -08008670static int raid5_start(struct mddev *mddev)
8671{
8672 struct r5conf *conf = mddev->private;
8673
8674 return r5l_start(conf->log);
8675}
8676
NeilBrown84fc4b52011-10-11 16:49:58 +11008677static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07008678{
8679 .name = "raid6",
8680 .level = 6,
8681 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008682 .make_request = raid5_make_request,
8683 .run = raid5_run,
Song Liud5d885f2017-11-19 22:17:01 -08008684 .start = raid5_start,
NeilBrownafa0f552014-12-15 12:56:58 +11008685 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008686 .status = raid5_status,
8687 .error_handler = raid5_error,
NeilBrown16a53ec2006-06-26 00:27:38 -07008688 .hot_add_disk = raid5_add_disk,
8689 .hot_remove_disk= raid5_remove_disk,
8690 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008691 .sync_request = raid5_sync_request,
NeilBrown16a53ec2006-06-26 00:27:38 -07008692 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008693 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10008694 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08008695 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008696 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07008697 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11008698 .takeover = raid6_takeover,
Song Liu0bb0c102017-03-27 10:51:33 -07008699 .change_consistency_policy = raid5_change_consistency_policy,
NeilBrown16a53ec2006-06-26 00:27:38 -07008700};
NeilBrown84fc4b52011-10-11 16:49:58 +11008701static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008702{
8703 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08008704 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008705 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008706 .make_request = raid5_make_request,
8707 .run = raid5_run,
Song Liud5d885f2017-11-19 22:17:01 -08008708 .start = raid5_start,
NeilBrownafa0f552014-12-15 12:56:58 +11008709 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008710 .status = raid5_status,
8711 .error_handler = raid5_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008712 .hot_add_disk = raid5_add_disk,
8713 .hot_remove_disk= raid5_remove_disk,
8714 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008715 .sync_request = raid5_sync_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008716 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008717 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08008718 .check_reshape = raid5_check_reshape,
8719 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008720 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07008721 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11008722 .takeover = raid5_takeover,
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01008723 .change_consistency_policy = raid5_change_consistency_policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008724};
8725
NeilBrown84fc4b52011-10-11 16:49:58 +11008726static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008727{
NeilBrown2604b702006-01-06 00:20:36 -08008728 .name = "raid4",
8729 .level = 4,
8730 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008731 .make_request = raid5_make_request,
8732 .run = raid5_run,
Song Liud5d885f2017-11-19 22:17:01 -08008733 .start = raid5_start,
NeilBrownafa0f552014-12-15 12:56:58 +11008734 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008735 .status = raid5_status,
8736 .error_handler = raid5_error,
NeilBrown2604b702006-01-06 00:20:36 -08008737 .hot_add_disk = raid5_add_disk,
8738 .hot_remove_disk= raid5_remove_disk,
8739 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008740 .sync_request = raid5_sync_request,
NeilBrown2604b702006-01-06 00:20:36 -08008741 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008742 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08008743 .check_reshape = raid5_check_reshape,
8744 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008745 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08008746 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11008747 .takeover = raid4_takeover,
Song Liu0bb0c102017-03-27 10:51:33 -07008748 .change_consistency_policy = raid5_change_consistency_policy,
NeilBrown2604b702006-01-06 00:20:36 -08008749};
8750
8751static int __init raid5_init(void)
8752{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008753 int ret;
8754
Shaohua Li851c30c2013-08-28 14:30:16 +08008755 raid5_wq = alloc_workqueue("raid5wq",
8756 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
8757 if (!raid5_wq)
8758 return -ENOMEM;
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008759
8760 ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
8761 "md/raid5:prepare",
8762 raid456_cpu_up_prepare,
8763 raid456_cpu_dead);
8764 if (ret) {
8765 destroy_workqueue(raid5_wq);
8766 return ret;
8767 }
NeilBrown16a53ec2006-06-26 00:27:38 -07008768 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008769 register_md_personality(&raid5_personality);
8770 register_md_personality(&raid4_personality);
8771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008772}
8773
NeilBrown2604b702006-01-06 00:20:36 -08008774static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008775{
NeilBrown16a53ec2006-06-26 00:27:38 -07008776 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008777 unregister_md_personality(&raid5_personality);
8778 unregister_md_personality(&raid4_personality);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008779 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
Shaohua Li851c30c2013-08-28 14:30:16 +08008780 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008781}
8782
8783module_init(raid5_init);
8784module_exit(raid5_exit);
8785MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008786MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008787MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08008788MODULE_ALIAS("md-raid5");
8789MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08008790MODULE_ALIAS("md-level-5");
8791MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07008792MODULE_ALIAS("md-personality-8"); /* RAID6 */
8793MODULE_ALIAS("md-raid6");
8794MODULE_ALIAS("md-level-6");
8795
8796/* This used to be two separate modules, they were: */
8797MODULE_ALIAS("raid5");
8798MODULE_ALIAS("raid6");