blob: 18f20f3d9664811f7ae96a0d5e830b8a95f3f1fd [file] [log] [blame]
Thomas Gleixneraf1a8892019-05-20 19:08:12 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * raid5.c : Multiple Devices driver for Linux
4 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
5 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07006 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
NeilBrown16a53ec2006-06-26 00:27:38 -07008 * RAID-4/5/6 management functions.
9 * Thanks to Penguin Computing for making the RAID-6 development possible
10 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
NeilBrownae3c20c2006-07-10 04:44:17 -070013/*
14 * BITMAP UNPLUGGING:
15 *
16 * The sequencing for updating the bitmap reliably is a little
17 * subtle (and I got it wrong the first time) so it deserves some
18 * explanation.
19 *
20 * We group bitmap updates into batches. Each batch has a number.
21 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100022 * conf->seq_write is the number of the last batch successfully written.
23 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070024 * new additions.
25 * When we discover that we will need to write to any block in a stripe
26 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100027 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070028 * When we are ready to do a write, if that batch hasn't been written yet,
29 * we plug the array and queue the stripe for later.
30 * When an unplug happens, we increment bm_flush, thus closing the current
31 * batch.
32 * When we notice that bm_flush > bm_write, we write out all pending updates
33 * to the bitmap, and advance bm_write to where bm_flush was.
34 * This may occasionally write a bit out twice, but is sure never to
35 * miss any bits.
36 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080039#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110040#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070041#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040042#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070043#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110044#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070045#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100047#include <linux/ratelimit.h>
Shaohua Li851c30c2013-08-28 14:30:16 +080048#include <linux/nodemask.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010049
NeilBrowna9add5d2012-10-31 11:59:09 +110050#include <trace/events/block.h>
Shaohua Liaaf9f122017-03-03 22:06:12 -080051#include <linux/list_sort.h>
NeilBrowna9add5d2012-10-31 11:59:09 +110052
NeilBrown43b2e5d2009-03-31 14:33:13 +110053#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110054#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110055#include "raid0.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040056#include "md-bitmap.h"
Artur Paszkiewiczff875732017-03-09 09:59:58 +010057#include "raid5-log.h"
NeilBrown72626682005-09-09 16:23:54 -070058
Shaohua Li394ed8e2017-01-04 16:10:19 -080059#define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED)
60
Shaohua Li851c30c2013-08-28 14:30:16 +080061#define cpu_to_group(cpu) cpu_to_node(cpu)
62#define ANY_GROUP NUMA_NO_NODE
63
NeilBrown8e0e99b2014-10-02 13:45:00 +100064static bool devices_handle_discard_safely = false;
65module_param(devices_handle_discard_safely, bool, 0644);
66MODULE_PARM_DESC(devices_handle_discard_safely,
67 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
Shaohua Li851c30c2013-08-28 14:30:16 +080068static struct workqueue_struct *raid5_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
NeilBrownd1688a62011-10-11 16:49:52 +110070static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110071{
Yufen Yuc911c462020-07-18 05:29:07 -040072 int hash = (sect >> RAID5_STRIPE_SHIFT(conf)) & HASH_MASK;
NeilBrowndb298e12011-10-07 14:23:00 +110073 return &conf->stripe_hashtbl[hash];
74}
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Yufen Yuc911c462020-07-18 05:29:07 -040076static inline int stripe_hash_locks_hash(struct r5conf *conf, sector_t sect)
Shaohua Li566c09c2013-11-14 15:16:17 +110077{
Yufen Yuc911c462020-07-18 05:29:07 -040078 return (sect >> RAID5_STRIPE_SHIFT(conf)) & STRIPE_HASH_LOCKS_MASK;
Shaohua Li566c09c2013-11-14 15:16:17 +110079}
80
81static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
82{
83 spin_lock_irq(conf->hash_locks + hash);
84 spin_lock(&conf->device_lock);
85}
86
87static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
88{
89 spin_unlock(&conf->device_lock);
90 spin_unlock_irq(conf->hash_locks + hash);
91}
92
93static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
94{
95 int i;
Julia Cartwright3d05f3a2017-04-28 12:41:02 -050096 spin_lock_irq(conf->hash_locks);
Shaohua Li566c09c2013-11-14 15:16:17 +110097 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
98 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
99 spin_lock(&conf->device_lock);
100}
101
102static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
103{
104 int i;
105 spin_unlock(&conf->device_lock);
Julia Cartwright3d05f3a2017-04-28 12:41:02 -0500106 for (i = NR_STRIPE_HASH_LOCKS - 1; i; i--)
107 spin_unlock(conf->hash_locks + i);
108 spin_unlock_irq(conf->hash_locks);
Shaohua Li566c09c2013-11-14 15:16:17 +1100109}
110
NeilBrownd0dabf72009-03-31 14:39:38 +1100111/* Find first data disk in a raid6 stripe */
112static inline int raid6_d0(struct stripe_head *sh)
113{
NeilBrown67cc2b82009-03-31 14:39:38 +1100114 if (sh->ddf_layout)
115 /* ddf always start from first device */
116 return 0;
117 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100118 if (sh->qd_idx == sh->disks - 1)
119 return 0;
120 else
121 return sh->qd_idx + 1;
122}
NeilBrown16a53ec2006-06-26 00:27:38 -0700123static inline int raid6_next_disk(int disk, int raid_disks)
124{
125 disk++;
126 return (disk < raid_disks) ? disk : 0;
127}
Dan Williamsa4456852007-07-09 11:56:43 -0700128
NeilBrownd0dabf72009-03-31 14:39:38 +1100129/* When walking through the disks in a raid5, starting at raid6_d0,
130 * We need to map each disk to a 'slot', where the data disks are slot
131 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
132 * is raid_disks-1. This help does that mapping.
133 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100134static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
135 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100136{
Dan Williams66295422009-10-19 18:09:32 -0700137 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100138
NeilBrowne4424fe2009-10-16 16:27:34 +1100139 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700140 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100141 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100142 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100143 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100144 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100145 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700146 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100147 return slot;
148}
149
NeilBrownd1688a62011-10-11 16:49:52 +1100150static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Dan Williams600aa102008-06-28 08:32:05 +1000152static int stripe_operations_active(struct stripe_head *sh)
153{
154 return sh->check_state || sh->reconstruct_state ||
155 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
156 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
157}
158
Shaohua Li535ae4e2017-02-15 19:37:32 -0800159static bool stripe_is_lowprio(struct stripe_head *sh)
160{
161 return (test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) ||
162 test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) &&
163 !test_bit(STRIPE_R5C_CACHING, &sh->state);
164}
165
Shaohua Li851c30c2013-08-28 14:30:16 +0800166static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
167{
168 struct r5conf *conf = sh->raid_conf;
169 struct r5worker_group *group;
Shaohua Libfc90cb2013-08-29 15:40:32 +0800170 int thread_cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +0800171 int i, cpu = sh->cpu;
172
173 if (!cpu_online(cpu)) {
174 cpu = cpumask_any(cpu_online_mask);
175 sh->cpu = cpu;
176 }
177
178 if (list_empty(&sh->lru)) {
179 struct r5worker_group *group;
180 group = conf->worker_groups + cpu_to_group(cpu);
Shaohua Li535ae4e2017-02-15 19:37:32 -0800181 if (stripe_is_lowprio(sh))
182 list_add_tail(&sh->lru, &group->loprio_list);
183 else
184 list_add_tail(&sh->lru, &group->handle_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800185 group->stripes_cnt++;
186 sh->group = group;
Shaohua Li851c30c2013-08-28 14:30:16 +0800187 }
188
189 if (conf->worker_cnt_per_group == 0) {
190 md_wakeup_thread(conf->mddev->thread);
191 return;
192 }
193
194 group = conf->worker_groups + cpu_to_group(sh->cpu);
195
Shaohua Libfc90cb2013-08-29 15:40:32 +0800196 group->workers[0].working = true;
197 /* at least one worker should run to avoid race */
198 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
199
200 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
201 /* wakeup more workers */
202 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
203 if (group->workers[i].working == false) {
204 group->workers[i].working = true;
205 queue_work_on(sh->cpu, raid5_wq,
206 &group->workers[i].work);
207 thread_cnt--;
208 }
209 }
Shaohua Li851c30c2013-08-28 14:30:16 +0800210}
211
Shaohua Li566c09c2013-11-14 15:16:17 +1100212static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
213 struct list_head *temp_inactive_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Song Liu1e6d6902016-11-17 15:24:39 -0800215 int i;
216 int injournal = 0; /* number of date pages with R5_InJournal */
217
Shaohua Li4eb788d2012-07-19 16:01:31 +1000218 BUG_ON(!list_empty(&sh->lru));
219 BUG_ON(atomic_read(&conf->active_stripes)==0);
Song Liu1e6d6902016-11-17 15:24:39 -0800220
221 if (r5c_is_writeback(conf->log))
222 for (i = sh->disks; i--; )
223 if (test_bit(R5_InJournal, &sh->dev[i].flags))
224 injournal++;
Song Liua39f7af2016-11-17 15:24:40 -0800225 /*
Song Liu5ddf0442017-05-11 17:03:44 -0700226 * In the following cases, the stripe cannot be released to cached
227 * lists. Therefore, we make the stripe write out and set
228 * STRIPE_HANDLE:
229 * 1. when quiesce in r5c write back;
230 * 2. when resync is requested fot the stripe.
Song Liua39f7af2016-11-17 15:24:40 -0800231 */
Song Liu5ddf0442017-05-11 17:03:44 -0700232 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) ||
233 (conf->quiesce && r5c_is_writeback(conf->log) &&
234 !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0)) {
Song Liua39f7af2016-11-17 15:24:40 -0800235 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
236 r5c_make_stripe_write_out(sh);
237 set_bit(STRIPE_HANDLE, &sh->state);
238 }
Song Liu1e6d6902016-11-17 15:24:39 -0800239
Shaohua Li4eb788d2012-07-19 16:01:31 +1000240 if (test_bit(STRIPE_HANDLE, &sh->state)) {
241 if (test_bit(STRIPE_DELAYED, &sh->state) &&
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500242 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
Shaohua Li4eb788d2012-07-19 16:01:31 +1000243 list_add_tail(&sh->lru, &conf->delayed_list);
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500244 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
Shaohua Li4eb788d2012-07-19 16:01:31 +1000245 sh->bm_seq - conf->seq_write > 0)
246 list_add_tail(&sh->lru, &conf->bitmap_list);
247 else {
248 clear_bit(STRIPE_DELAYED, &sh->state);
249 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Shaohua Li851c30c2013-08-28 14:30:16 +0800250 if (conf->worker_cnt_per_group == 0) {
Shaohua Li535ae4e2017-02-15 19:37:32 -0800251 if (stripe_is_lowprio(sh))
252 list_add_tail(&sh->lru,
253 &conf->loprio_list);
254 else
255 list_add_tail(&sh->lru,
256 &conf->handle_list);
Shaohua Li851c30c2013-08-28 14:30:16 +0800257 } else {
258 raid5_wakeup_stripe_thread(sh);
259 return;
260 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000261 }
262 md_wakeup_thread(conf->mddev->thread);
263 } else {
264 BUG_ON(stripe_operations_active(sh));
265 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
266 if (atomic_dec_return(&conf->preread_active_stripes)
267 < IO_THRESHOLD)
268 md_wakeup_thread(conf->mddev->thread);
269 atomic_dec(&conf->active_stripes);
Song Liu1e6d6902016-11-17 15:24:39 -0800270 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
271 if (!r5c_is_writeback(conf->log))
272 list_add_tail(&sh->lru, temp_inactive_list);
273 else {
274 WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags));
275 if (injournal == 0)
276 list_add_tail(&sh->lru, temp_inactive_list);
277 else if (injournal == conf->raid_disks - conf->max_degraded) {
278 /* full stripe */
279 if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state))
280 atomic_inc(&conf->r5c_cached_full_stripes);
281 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state))
282 atomic_dec(&conf->r5c_cached_partial_stripes);
283 list_add_tail(&sh->lru, &conf->r5c_full_stripe_list);
Song Liua39f7af2016-11-17 15:24:40 -0800284 r5c_check_cached_full_stripe(conf);
Song Liu03b047f2017-01-11 13:39:14 -0800285 } else
286 /*
287 * STRIPE_R5C_PARTIAL_STRIPE is set in
288 * r5c_try_caching_write(). No need to
289 * set it again.
290 */
Song Liu1e6d6902016-11-17 15:24:39 -0800291 list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list);
Song Liu1e6d6902016-11-17 15:24:39 -0800292 }
293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295}
NeilBrownd0dabf72009-03-31 14:39:38 +1100296
Shaohua Li566c09c2013-11-14 15:16:17 +1100297static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
298 struct list_head *temp_inactive_list)
Shaohua Li4eb788d2012-07-19 16:01:31 +1000299{
300 if (atomic_dec_and_test(&sh->count))
Shaohua Li566c09c2013-11-14 15:16:17 +1100301 do_release_stripe(conf, sh, temp_inactive_list);
302}
303
304/*
305 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
306 *
307 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
308 * given time. Adding stripes only takes device lock, while deleting stripes
309 * only takes hash lock.
310 */
311static void release_inactive_stripe_list(struct r5conf *conf,
312 struct list_head *temp_inactive_list,
313 int hash)
314{
315 int size;
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800316 bool do_wakeup = false;
Shaohua Li566c09c2013-11-14 15:16:17 +1100317 unsigned long flags;
318
319 if (hash == NR_STRIPE_HASH_LOCKS) {
320 size = NR_STRIPE_HASH_LOCKS;
321 hash = NR_STRIPE_HASH_LOCKS - 1;
322 } else
323 size = 1;
324 while (size) {
325 struct list_head *list = &temp_inactive_list[size - 1];
326
327 /*
Shaohua Li6d036f72015-08-13 14:31:57 -0700328 * We don't hold any lock here yet, raid5_get_active_stripe() might
Shaohua Li566c09c2013-11-14 15:16:17 +1100329 * remove stripes from the list
330 */
331 if (!list_empty_careful(list)) {
332 spin_lock_irqsave(conf->hash_locks + hash, flags);
Shaohua Li4bda5562013-11-14 15:16:17 +1100333 if (list_empty(conf->inactive_list + hash) &&
334 !list_empty(list))
335 atomic_dec(&conf->empty_inactive_list_nr);
Shaohua Li566c09c2013-11-14 15:16:17 +1100336 list_splice_tail_init(list, conf->inactive_list + hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800337 do_wakeup = true;
Shaohua Li566c09c2013-11-14 15:16:17 +1100338 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
339 }
340 size--;
341 hash--;
342 }
343
344 if (do_wakeup) {
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800345 wake_up(&conf->wait_for_stripe);
Yuanhan Liub1b46482015-05-08 18:19:06 +1000346 if (atomic_read(&conf->active_stripes) == 0)
347 wake_up(&conf->wait_for_quiescent);
Shaohua Li566c09c2013-11-14 15:16:17 +1100348 if (conf->retry_read_aligned)
349 md_wakeup_thread(conf->mddev->thread);
350 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000351}
352
Shaohua Li773ca822013-08-27 17:50:39 +0800353/* should hold conf->device_lock already */
Shaohua Li566c09c2013-11-14 15:16:17 +1100354static int release_stripe_list(struct r5conf *conf,
355 struct list_head *temp_inactive_list)
Shaohua Li773ca822013-08-27 17:50:39 +0800356{
Byungchul Parkeae82632017-02-14 16:26:24 +0900357 struct stripe_head *sh, *t;
Shaohua Li773ca822013-08-27 17:50:39 +0800358 int count = 0;
359 struct llist_node *head;
360
361 head = llist_del_all(&conf->released_stripes);
Shaohua Lid265d9d2013-08-28 14:29:05 +0800362 head = llist_reverse_order(head);
Byungchul Parkeae82632017-02-14 16:26:24 +0900363 llist_for_each_entry_safe(sh, t, head, release_list) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100364 int hash;
365
Shaohua Li773ca822013-08-27 17:50:39 +0800366 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
367 smp_mb();
368 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
369 /*
370 * Don't worry the bit is set here, because if the bit is set
371 * again, the count is always > 1. This is true for
372 * STRIPE_ON_UNPLUG_LIST bit too.
373 */
Shaohua Li566c09c2013-11-14 15:16:17 +1100374 hash = sh->hash_lock_index;
375 __release_stripe(conf, sh, &temp_inactive_list[hash]);
Shaohua Li773ca822013-08-27 17:50:39 +0800376 count++;
377 }
378
379 return count;
380}
381
Shaohua Li6d036f72015-08-13 14:31:57 -0700382void raid5_release_stripe(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
NeilBrownd1688a62011-10-11 16:49:52 +1100384 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 unsigned long flags;
Shaohua Li566c09c2013-11-14 15:16:17 +1100386 struct list_head list;
387 int hash;
Shaohua Li773ca822013-08-27 17:50:39 +0800388 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700389
Eivind Sartocf170f32014-05-28 13:39:23 +1000390 /* Avoid release_list until the last reference.
391 */
392 if (atomic_add_unless(&sh->count, -1, 1))
393 return;
394
majianpengad4068d2013-11-14 15:16:15 +1100395 if (unlikely(!conf->mddev->thread) ||
396 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
Shaohua Li773ca822013-08-27 17:50:39 +0800397 goto slow_path;
398 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
399 if (wakeup)
400 md_wakeup_thread(conf->mddev->thread);
401 return;
402slow_path:
Shaohua Li773ca822013-08-27 17:50:39 +0800403 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Anna-Maria Gleixner685dbca2018-07-03 22:01:36 +0200404 if (atomic_dec_and_lock_irqsave(&sh->count, &conf->device_lock, flags)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100405 INIT_LIST_HEAD(&list);
406 hash = sh->hash_lock_index;
407 do_release_stripe(conf, sh, &list);
Anna-Maria Gleixner08edaaa2018-07-03 22:01:37 +0200408 spin_unlock_irqrestore(&conf->device_lock, flags);
Shaohua Li566c09c2013-11-14 15:16:17 +1100409 release_inactive_stripe_list(conf, &list, hash);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
NeilBrownfccddba2006-01-06 00:20:33 -0800413static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Dan Williams45b42332007-07-09 11:56:43 -0700415 pr_debug("remove_hash(), stripe %llu\n",
416 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
NeilBrownfccddba2006-01-06 00:20:33 -0800418 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
NeilBrownd1688a62011-10-11 16:49:52 +1100421static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
NeilBrownfccddba2006-01-06 00:20:33 -0800423 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Dan Williams45b42332007-07-09 11:56:43 -0700425 pr_debug("insert_hash(), stripe %llu\n",
426 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
NeilBrownfccddba2006-01-06 00:20:33 -0800428 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/* find an idle stripe, make sure it is unhashed, and return it. */
Shaohua Li566c09c2013-11-14 15:16:17 +1100432static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct stripe_head *sh = NULL;
435 struct list_head *first;
436
Shaohua Li566c09c2013-11-14 15:16:17 +1100437 if (list_empty(conf->inactive_list + hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 goto out;
Shaohua Li566c09c2013-11-14 15:16:17 +1100439 first = (conf->inactive_list + hash)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 sh = list_entry(first, struct stripe_head, lru);
441 list_del_init(first);
442 remove_hash(sh);
443 atomic_inc(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100444 BUG_ON(hash != sh->hash_lock_index);
Shaohua Li4bda5562013-11-14 15:16:17 +1100445 if (list_empty(conf->inactive_list + hash))
446 atomic_inc(&conf->empty_inactive_list_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447out:
448 return sh;
449}
450
NeilBrowne4e11e32010-06-16 16:45:16 +1000451static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 struct page *p;
454 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000455 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
NeilBrowne4e11e32010-06-16 16:45:16 +1000457 for (i = 0; i < num ; i++) {
Shaohua Lid592a992014-05-21 17:57:44 +0800458 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 p = sh->dev[i].page;
460 if (!p)
461 continue;
462 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800463 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465}
466
NeilBrowna9683a72015-02-25 12:02:51 +1100467static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000470 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
NeilBrowne4e11e32010-06-16 16:45:16 +1000472 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 struct page *page;
474
NeilBrowna9683a72015-02-25 12:02:51 +1100475 if (!(page = alloc_page(gfp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return 1;
477 }
478 sh->dev[i].page = page;
Shaohua Lid592a992014-05-21 17:57:44 +0800479 sh->dev[i].orig_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Artur Paszkiewicz3418d032017-03-09 09:59:59 +0100481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 0;
483}
484
NeilBrownd1688a62011-10-11 16:49:52 +1100485static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100486 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
NeilBrownb5663ba2009-03-31 14:39:38 +1100488static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
NeilBrownd1688a62011-10-11 16:49:52 +1100490 struct r5conf *conf = sh->raid_conf;
Shaohua Li566c09c2013-11-14 15:16:17 +1100491 int i, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200493 BUG_ON(atomic_read(&sh->count) != 0);
494 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000495 BUG_ON(stripe_operations_active(sh));
shli@kernel.org59fc6302014-12-15 12:57:03 +1100496 BUG_ON(sh->batch_head);
Dan Williamsd84e0f12007-01-02 13:52:30 -0700497
Dan Williams45b42332007-07-09 11:56:43 -0700498 pr_debug("init_stripe called, stripe %llu\n",
Markus Stockhausenb8e6a152014-08-23 20:19:27 +1000499 (unsigned long long)sector);
Shaohua Li566c09c2013-11-14 15:16:17 +1100500retry:
501 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100502 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100503 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100505 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 sh->state = 0;
507
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800508 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 struct r5dev *dev = &sh->dev[i];
510
Dan Williamsd84e0f12007-01-02 13:52:30 -0700511 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 test_bit(R5_LOCKED, &dev->flags)) {
NeilBrowncc6167b2016-11-02 14:16:50 +1100513 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700515 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000517 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519 dev->flags = 0;
Guoqing Jiang27a4ff82017-08-10 16:12:17 +0800520 dev->sector = raid5_compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100522 if (read_seqcount_retry(&conf->gen_lock, seq))
523 goto retry;
shli@kernel.org7a87f432014-12-15 12:57:03 +1100524 sh->overwrite_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800526 sh->cpu = smp_processor_id();
shli@kernel.orgda41ba62014-12-15 12:57:03 +1100527 set_bit(STRIPE_BATCH_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
NeilBrownd1688a62011-10-11 16:49:52 +1100530static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100531 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 struct stripe_head *sh;
534
Dan Williams45b42332007-07-09 11:56:43 -0700535 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800536 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100537 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700539 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return NULL;
541}
542
NeilBrown674806d2010-06-16 17:17:53 +1000543/*
544 * Need to check if array has failed when deciding whether to:
545 * - start an array
546 * - remove non-faulty devices
547 * - add a spare
548 * - allow a reshape
549 * This determination is simple when no reshape is happening.
550 * However if there is a reshape, we need to carefully check
551 * both the before and after sections.
552 * This is because some failed devices may only affect one
553 * of the two sections, and some non-in_sync devices may
554 * be insync in the section most affected by failed devices.
555 */
Song Liu2e38a372017-01-24 10:45:30 -0800556int raid5_calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000557{
NeilBrown908f4fb2011-12-23 10:17:50 +1100558 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000559 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000560
561 rcu_read_lock();
562 degraded = 0;
563 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100564 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000565 if (rdev && test_bit(Faulty, &rdev->flags))
566 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000567 if (!rdev || test_bit(Faulty, &rdev->flags))
568 degraded++;
569 else if (test_bit(In_sync, &rdev->flags))
570 ;
571 else
572 /* not in-sync or faulty.
573 * If the reshape increases the number of devices,
574 * this is being recovered by the reshape, so
575 * this 'previous' section is not in_sync.
576 * If the number of devices is being reduced however,
577 * the device can only be part of the array if
578 * we are reverting a reshape, so this section will
579 * be in-sync.
580 */
581 if (conf->raid_disks >= conf->previous_raid_disks)
582 degraded++;
583 }
584 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100585 if (conf->raid_disks == conf->previous_raid_disks)
586 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000587 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100588 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000589 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100590 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000591 if (rdev && test_bit(Faulty, &rdev->flags))
592 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000593 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100594 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000595 else if (test_bit(In_sync, &rdev->flags))
596 ;
597 else
598 /* not in-sync or faulty.
599 * If reshape increases the number of devices, this
600 * section has already been recovered, else it
601 * almost certainly hasn't.
602 */
603 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100604 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000605 }
606 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100607 if (degraded2 > degraded)
608 return degraded2;
609 return degraded;
610}
611
612static int has_failed(struct r5conf *conf)
613{
614 int degraded;
615
616 if (conf->mddev->reshape_position == MaxSector)
617 return conf->mddev->degraded > conf->max_degraded;
618
Song Liu2e38a372017-01-24 10:45:30 -0800619 degraded = raid5_calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000620 if (degraded > conf->max_degraded)
621 return 1;
622 return 0;
623}
624
Shaohua Li6d036f72015-08-13 14:31:57 -0700625struct stripe_head *
626raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
627 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 struct stripe_head *sh;
Yufen Yuc911c462020-07-18 05:29:07 -0400630 int hash = stripe_hash_locks_hash(conf, sector);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800631 int inc_empty_inactive_list_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Dan Williams45b42332007-07-09 11:56:43 -0700633 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Shaohua Li566c09c2013-11-14 15:16:17 +1100635 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 do {
Yuanhan Liub1b46482015-05-08 18:19:06 +1000638 wait_event_lock_irq(conf->wait_for_quiescent,
NeilBrowna8c906c2009-06-09 14:39:59 +1000639 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100640 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100641 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (!sh) {
NeilBrownedbe83a2015-02-26 12:47:56 +1100643 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100644 sh = get_free_stripe(conf, hash);
Shaohua Li713bc5c2015-05-28 17:33:47 -0700645 if (!sh && !test_bit(R5_DID_ALLOC,
646 &conf->cache_state))
NeilBrownedbe83a2015-02-26 12:47:56 +1100647 set_bit(R5_ALLOC_MORE,
648 &conf->cache_state);
649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (noblock && sh == NULL)
651 break;
Song Liua39f7af2016-11-17 15:24:40 -0800652
653 r5c_check_stripe_cache_usage(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (!sh) {
NeilBrown54233992015-02-26 12:21:04 +1100655 set_bit(R5_INACTIVE_BLOCKED,
656 &conf->cache_state);
Song Liua39f7af2016-11-17 15:24:40 -0800657 r5l_wake_reclaim(conf->log, 0);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800658 wait_event_lock_irq(
659 conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +1100660 !list_empty(conf->inactive_list + hash) &&
661 (atomic_read(&conf->active_stripes)
662 < (conf->max_nr_stripes * 3 / 4)
NeilBrown54233992015-02-26 12:21:04 +1100663 || !test_bit(R5_INACTIVE_BLOCKED,
664 &conf->cache_state)),
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800665 *(conf->hash_locks + hash));
NeilBrown54233992015-02-26 12:21:04 +1100666 clear_bit(R5_INACTIVE_BLOCKED,
667 &conf->cache_state);
NeilBrown7da9d452014-01-22 11:45:03 +1100668 } else {
NeilBrownb5663ba2009-03-31 14:39:38 +1100669 init_stripe(sh, sector, previous);
NeilBrown7da9d452014-01-22 11:45:03 +1100670 atomic_inc(&sh->count);
671 }
Shaohua Lie240c182014-04-09 11:27:42 +0800672 } else if (!atomic_inc_not_zero(&sh->count)) {
NeilBrown6d183de2013-11-28 10:55:27 +1100673 spin_lock(&conf->device_lock);
Shaohua Lie240c182014-04-09 11:27:42 +0800674 if (!atomic_read(&sh->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (!test_bit(STRIPE_HANDLE, &sh->state))
676 atomic_inc(&conf->active_stripes);
NeilBrown5af9bef2014-01-14 15:16:10 +1100677 BUG_ON(list_empty(&sh->lru) &&
678 !test_bit(STRIPE_EXPANDING, &sh->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800679 inc_empty_inactive_list_flag = 0;
680 if (!list_empty(conf->inactive_list + hash))
681 inc_empty_inactive_list_flag = 1;
NeilBrown16a53ec2006-06-26 00:27:38 -0700682 list_del_init(&sh->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800683 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
684 atomic_inc(&conf->empty_inactive_list_nr);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800685 if (sh->group) {
686 sh->group->stripes_cnt--;
687 sh->group = NULL;
688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
NeilBrown7da9d452014-01-22 11:45:03 +1100690 atomic_inc(&sh->count);
NeilBrown6d183de2013-11-28 10:55:27 +1100691 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693 } while (sh == NULL);
694
Shaohua Li566c09c2013-11-14 15:16:17 +1100695 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return sh;
697}
698
shli@kernel.org7a87f432014-12-15 12:57:03 +1100699static bool is_full_stripe_write(struct stripe_head *sh)
700{
701 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
702 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
703}
704
shli@kernel.org59fc6302014-12-15 12:57:03 +1100705static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
Christoph Hellwig368ecad2019-04-04 18:56:15 +0200706 __acquires(&sh1->stripe_lock)
707 __acquires(&sh2->stripe_lock)
shli@kernel.org59fc6302014-12-15 12:57:03 +1100708{
shli@kernel.org59fc6302014-12-15 12:57:03 +1100709 if (sh1 > sh2) {
Julia Cartwright3d05f3a2017-04-28 12:41:02 -0500710 spin_lock_irq(&sh2->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100711 spin_lock_nested(&sh1->stripe_lock, 1);
712 } else {
Julia Cartwright3d05f3a2017-04-28 12:41:02 -0500713 spin_lock_irq(&sh1->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100714 spin_lock_nested(&sh2->stripe_lock, 1);
715 }
716}
717
718static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
Christoph Hellwig368ecad2019-04-04 18:56:15 +0200719 __releases(&sh1->stripe_lock)
720 __releases(&sh2->stripe_lock)
shli@kernel.org59fc6302014-12-15 12:57:03 +1100721{
722 spin_unlock(&sh1->stripe_lock);
Julia Cartwright3d05f3a2017-04-28 12:41:02 -0500723 spin_unlock_irq(&sh2->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100724}
725
726/* Only freshly new full stripe normal write stripe can be added to a batch list */
727static bool stripe_can_batch(struct stripe_head *sh)
728{
Shaohua Li9c3e3332015-08-13 14:32:02 -0700729 struct r5conf *conf = sh->raid_conf;
730
Shaohua Lie254de62018-08-29 11:05:42 -0700731 if (raid5_has_log(conf) || raid5_has_ppl(conf))
Shaohua Li9c3e3332015-08-13 14:32:02 -0700732 return false;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100733 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
NeilBrownd0852df52015-05-27 08:43:45 +1000734 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
shli@kernel.org59fc6302014-12-15 12:57:03 +1100735 is_full_stripe_write(sh);
736}
737
738/* we only do back search */
739static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
740{
741 struct stripe_head *head;
742 sector_t head_sector, tmp_sec;
743 int hash;
744 int dd_idx;
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800745 int inc_empty_inactive_list_flag;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100746
shli@kernel.org59fc6302014-12-15 12:57:03 +1100747 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
748 tmp_sec = sh->sector;
749 if (!sector_div(tmp_sec, conf->chunk_sectors))
750 return;
Yufen Yuc911c462020-07-18 05:29:07 -0400751 head_sector = sh->sector - RAID5_STRIPE_SECTORS(conf);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100752
Yufen Yuc911c462020-07-18 05:29:07 -0400753 hash = stripe_hash_locks_hash(conf, head_sector);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100754 spin_lock_irq(conf->hash_locks + hash);
755 head = __find_stripe(conf, head_sector, conf->generation);
756 if (head && !atomic_inc_not_zero(&head->count)) {
757 spin_lock(&conf->device_lock);
758 if (!atomic_read(&head->count)) {
759 if (!test_bit(STRIPE_HANDLE, &head->state))
760 atomic_inc(&conf->active_stripes);
761 BUG_ON(list_empty(&head->lru) &&
762 !test_bit(STRIPE_EXPANDING, &head->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800763 inc_empty_inactive_list_flag = 0;
764 if (!list_empty(conf->inactive_list + hash))
765 inc_empty_inactive_list_flag = 1;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100766 list_del_init(&head->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800767 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
768 atomic_inc(&conf->empty_inactive_list_nr);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100769 if (head->group) {
770 head->group->stripes_cnt--;
771 head->group = NULL;
772 }
773 }
774 atomic_inc(&head->count);
775 spin_unlock(&conf->device_lock);
776 }
777 spin_unlock_irq(conf->hash_locks + hash);
778
779 if (!head)
780 return;
781 if (!stripe_can_batch(head))
782 goto out;
783
784 lock_two_stripes(head, sh);
785 /* clear_batch_ready clear the flag */
786 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
787 goto unlock_out;
788
789 if (sh->batch_head)
790 goto unlock_out;
791
792 dd_idx = 0;
793 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
794 dd_idx++;
Jens Axboe1eff9d32016-08-05 15:35:16 -0600795 if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
Mike Christie796a5cf2016-06-05 14:32:07 -0500796 bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
shli@kernel.org59fc6302014-12-15 12:57:03 +1100797 goto unlock_out;
798
799 if (head->batch_head) {
800 spin_lock(&head->batch_head->batch_lock);
801 /* This batch list is already running */
802 if (!stripe_can_batch(head)) {
803 spin_unlock(&head->batch_head->batch_lock);
804 goto unlock_out;
805 }
Shaohua Li36648472017-08-25 10:40:02 -0700806 /*
807 * We must assign batch_head of this stripe within the
808 * batch_lock, otherwise clear_batch_ready of batch head
809 * stripe could clear BATCH_READY bit of this stripe and
810 * this stripe->batch_head doesn't get assigned, which
811 * could confuse clear_batch_ready for this stripe
812 */
813 sh->batch_head = head->batch_head;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100814
815 /*
816 * at this point, head's BATCH_READY could be cleared, but we
817 * can still add the stripe to batch list
818 */
819 list_add(&sh->batch_list, &head->batch_list);
820 spin_unlock(&head->batch_head->batch_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100821 } else {
822 head->batch_head = head;
823 sh->batch_head = head->batch_head;
824 spin_lock(&head->batch_lock);
825 list_add_tail(&sh->batch_list, &head->batch_list);
826 spin_unlock(&head->batch_lock);
827 }
828
829 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
830 if (atomic_dec_return(&conf->preread_active_stripes)
831 < IO_THRESHOLD)
832 md_wakeup_thread(conf->mddev->thread);
833
NeilBrown2b6b2452015-05-21 15:10:01 +1000834 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
835 int seq = sh->bm_seq;
836 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
837 sh->batch_head->bm_seq > seq)
838 seq = sh->batch_head->bm_seq;
839 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
840 sh->batch_head->bm_seq = seq;
841 }
842
shli@kernel.org59fc6302014-12-15 12:57:03 +1100843 atomic_inc(&sh->count);
844unlock_out:
845 unlock_two_stripes(head, sh);
846out:
Shaohua Li6d036f72015-08-13 14:31:57 -0700847 raid5_release_stripe(head);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100848}
849
NeilBrown05616be2012-05-21 09:27:00 +1000850/* Determine if 'data_offset' or 'new_data_offset' should be used
851 * in this stripe_head.
852 */
853static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
854{
855 sector_t progress = conf->reshape_progress;
856 /* Need a memory barrier to make sure we see the value
857 * of conf->generation, or ->data_offset that was set before
858 * reshape_progress was updated.
859 */
860 smp_rmb();
861 if (progress == MaxSector)
862 return 0;
863 if (sh->generation == conf->generation - 1)
864 return 0;
865 /* We are in a reshape, and this is a new-generation stripe,
866 * so use new_data_offset.
867 */
868 return 1;
869}
870
Shaohua Liaaf9f122017-03-03 22:06:12 -0800871static void dispatch_bio_list(struct bio_list *tmp)
Shaohua Li765d7042017-01-04 09:33:23 -0800872{
Shaohua Li765d7042017-01-04 09:33:23 -0800873 struct bio *bio;
874
Shaohua Liaaf9f122017-03-03 22:06:12 -0800875 while ((bio = bio_list_pop(tmp)))
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200876 submit_bio_noacct(bio);
Shaohua Li765d7042017-01-04 09:33:23 -0800877}
878
Shaohua Liaaf9f122017-03-03 22:06:12 -0800879static int cmp_stripe(void *priv, struct list_head *a, struct list_head *b)
Shaohua Li765d7042017-01-04 09:33:23 -0800880{
Shaohua Liaaf9f122017-03-03 22:06:12 -0800881 const struct r5pending_data *da = list_entry(a,
882 struct r5pending_data, sibling);
883 const struct r5pending_data *db = list_entry(b,
884 struct r5pending_data, sibling);
885 if (da->sector > db->sector)
886 return 1;
887 if (da->sector < db->sector)
888 return -1;
889 return 0;
890}
891
892static void dispatch_defer_bios(struct r5conf *conf, int target,
893 struct bio_list *list)
894{
895 struct r5pending_data *data;
896 struct list_head *first, *next = NULL;
897 int cnt = 0;
898
899 if (conf->pending_data_cnt == 0)
Shaohua Li765d7042017-01-04 09:33:23 -0800900 return;
Shaohua Liaaf9f122017-03-03 22:06:12 -0800901
902 list_sort(NULL, &conf->pending_list, cmp_stripe);
903
904 first = conf->pending_list.next;
905
906 /* temporarily move the head */
907 if (conf->next_pending_data)
908 list_move_tail(&conf->pending_list,
909 &conf->next_pending_data->sibling);
910
911 while (!list_empty(&conf->pending_list)) {
912 data = list_first_entry(&conf->pending_list,
913 struct r5pending_data, sibling);
914 if (&data->sibling == first)
915 first = data->sibling.next;
916 next = data->sibling.next;
917
918 bio_list_merge(list, &data->bios);
919 list_move(&data->sibling, &conf->free_list);
920 cnt++;
921 if (cnt >= target)
922 break;
Shaohua Li765d7042017-01-04 09:33:23 -0800923 }
Shaohua Liaaf9f122017-03-03 22:06:12 -0800924 conf->pending_data_cnt -= cnt;
925 BUG_ON(conf->pending_data_cnt < 0 || cnt < target);
926
927 if (next != &conf->pending_list)
928 conf->next_pending_data = list_entry(next,
929 struct r5pending_data, sibling);
930 else
931 conf->next_pending_data = NULL;
932 /* list isn't empty */
933 if (first != &conf->pending_list)
934 list_move_tail(&conf->pending_list, first);
935}
936
937static void flush_deferred_bios(struct r5conf *conf)
938{
939 struct bio_list tmp = BIO_EMPTY_LIST;
940
941 if (conf->pending_data_cnt == 0)
942 return;
943
Shaohua Li765d7042017-01-04 09:33:23 -0800944 spin_lock(&conf->pending_bios_lock);
Shaohua Liaaf9f122017-03-03 22:06:12 -0800945 dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp);
946 BUG_ON(conf->pending_data_cnt != 0);
Shaohua Li765d7042017-01-04 09:33:23 -0800947 spin_unlock(&conf->pending_bios_lock);
Shaohua Liaaf9f122017-03-03 22:06:12 -0800948
949 dispatch_bio_list(&tmp);
950}
951
952static void defer_issue_bios(struct r5conf *conf, sector_t sector,
953 struct bio_list *bios)
954{
955 struct bio_list tmp = BIO_EMPTY_LIST;
956 struct r5pending_data *ent;
957
958 spin_lock(&conf->pending_bios_lock);
959 ent = list_first_entry(&conf->free_list, struct r5pending_data,
960 sibling);
961 list_move_tail(&ent->sibling, &conf->pending_list);
962 ent->sector = sector;
963 bio_list_init(&ent->bios);
964 bio_list_merge(&ent->bios, bios);
965 conf->pending_data_cnt++;
966 if (conf->pending_data_cnt >= PENDING_IO_MAX)
967 dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp);
968
969 spin_unlock(&conf->pending_bios_lock);
970
971 dispatch_bio_list(&tmp);
Shaohua Li765d7042017-01-04 09:33:23 -0800972}
973
NeilBrown6712ecf2007-09-27 12:47:43 +0200974static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200975raid5_end_read_request(struct bio *bi);
NeilBrown6712ecf2007-09-27 12:47:43 +0200976static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200977raid5_end_write_request(struct bio *bi);
Dan Williams91c00922007-01-02 13:52:30 -0700978
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000979static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700980{
NeilBrownd1688a62011-10-11 16:49:52 +1100981 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700982 int i, disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100983 struct stripe_head *head_sh = sh;
Shaohua Liaaf9f122017-03-03 22:06:12 -0800984 struct bio_list pending_bios = BIO_EMPTY_LIST;
985 bool should_defer;
Dan Williams91c00922007-01-02 13:52:30 -0700986
987 might_sleep();
988
Artur Paszkiewiczff875732017-03-09 09:59:58 +0100989 if (log_stripe(sh, s) == 0)
990 return;
Song Liu1e6d6902016-11-17 15:24:39 -0800991
Shaohua Liaaf9f122017-03-03 22:06:12 -0800992 should_defer = conf->batch_bio_dispatch && conf->group_cnt;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700993
Dan Williams91c00922007-01-02 13:52:30 -0700994 for (i = disks; i--; ) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500995 int op, op_flags = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +1100996 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100997 struct bio *bi, *rbi;
998 struct md_rdev *rdev, *rrdev = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100999
1000 sh = head_sh;
Tejun Heoe9c74692010-09-03 11:56:18 +02001001 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001002 op = REQ_OP_WRITE;
Tejun Heoe9c74692010-09-03 11:56:18 +02001003 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001004 op_flags = REQ_FUA;
Shaohua Li9e4447682012-10-11 13:49:49 +11001005 if (test_bit(R5_Discard, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001006 op = REQ_OP_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +02001007 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001008 op = REQ_OP_READ;
NeilBrown9a3e1102011-12-23 10:17:53 +11001009 else if (test_and_clear_bit(R5_WantReplace,
1010 &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001011 op = REQ_OP_WRITE;
NeilBrown9a3e1102011-12-23 10:17:53 +11001012 replace_only = 1;
1013 } else
Dan Williams91c00922007-01-02 13:52:30 -07001014 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +10001015 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001016 op_flags |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -07001017
shli@kernel.org59fc6302014-12-15 12:57:03 +11001018again:
Dan Williams91c00922007-01-02 13:52:30 -07001019 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +11001020 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -07001021
Dan Williams91c00922007-01-02 13:52:30 -07001022 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +11001023 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +11001024 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
1025 rdev = rcu_dereference(conf->disks[i].rdev);
1026 if (!rdev) {
1027 rdev = rrdev;
1028 rrdev = NULL;
1029 }
Mike Christie796a5cf2016-06-05 14:32:07 -05001030 if (op_is_write(op)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001031 if (replace_only)
1032 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +11001033 if (rdev == rrdev)
1034 /* We raced and saw duplicates */
1035 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +11001036 } else {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001037 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +11001038 rdev = rrdev;
1039 rrdev = NULL;
1040 }
NeilBrown977df362011-12-23 10:17:53 +11001041
Dan Williams91c00922007-01-02 13:52:30 -07001042 if (rdev && test_bit(Faulty, &rdev->flags))
1043 rdev = NULL;
1044 if (rdev)
1045 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +11001046 if (rrdev && test_bit(Faulty, &rrdev->flags))
1047 rrdev = NULL;
1048 if (rrdev)
1049 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -07001050 rcu_read_unlock();
1051
NeilBrown73e92e52011-07-28 11:39:22 +10001052 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +11001053 * need to check for writes. We never accept write errors
1054 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +10001055 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001056 while (op_is_write(op) && rdev &&
NeilBrown73e92e52011-07-28 11:39:22 +10001057 test_bit(WriteErrorSeen, &rdev->flags)) {
1058 sector_t first_bad;
1059 int bad_sectors;
Yufen Yuc911c462020-07-18 05:29:07 -04001060 int bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
NeilBrown73e92e52011-07-28 11:39:22 +10001061 &first_bad, &bad_sectors);
1062 if (!bad)
1063 break;
1064
1065 if (bad < 0) {
1066 set_bit(BlockedBadBlocks, &rdev->flags);
1067 if (!conf->mddev->external &&
Shaohua Li29530792016-12-08 15:48:19 -08001068 conf->mddev->sb_flags) {
NeilBrown73e92e52011-07-28 11:39:22 +10001069 /* It is very unlikely, but we might
1070 * still need to write out the
1071 * bad block log - better give it
1072 * a chance*/
1073 md_check_recovery(conf->mddev);
1074 }
majianpeng18507532012-07-03 12:11:54 +10001075 /*
1076 * Because md_wait_for_blocked_rdev
1077 * will dec nr_pending, we must
1078 * increment it first.
1079 */
1080 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +10001081 md_wait_for_blocked_rdev(rdev, conf->mddev);
1082 } else {
1083 /* Acknowledged bad block - skip the write */
1084 rdev_dec_pending(rdev, conf->mddev);
1085 rdev = NULL;
1086 }
1087 }
1088
Dan Williams91c00922007-01-02 13:52:30 -07001089 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001090 if (s->syncing || s->expanding || s->expanded
1091 || s->replacing)
Yufen Yuc911c462020-07-18 05:29:07 -04001092 md_sync_acct(rdev->bdev, RAID5_STRIPE_SECTORS(conf));
Dan Williams91c00922007-01-02 13:52:30 -07001093
Dan Williams2b7497f2008-06-28 08:31:52 +10001094 set_bit(STRIPE_IO_STARTED, &sh->state);
1095
Christoph Hellwig74d46992017-08-23 19:10:32 +02001096 bio_set_dev(bi, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -05001097 bio_set_op_attrs(bi, op, op_flags);
1098 bi->bi_end_io = op_is_write(op)
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001099 ? raid5_end_write_request
1100 : raid5_end_read_request;
1101 bi->bi_private = sh;
1102
Mike Christie6296b962016-06-05 14:32:21 -05001103 pr_debug("%s: for %llu schedule op %d on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001104 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001105 bi->bi_opf, i);
Dan Williams91c00922007-01-02 13:52:30 -07001106 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001107 if (sh != head_sh)
1108 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001109 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001110 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001111 + rdev->new_data_offset);
1112 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001113 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001114 + rdev->data_offset);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001115 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
Jens Axboe1eff9d32016-08-05 15:35:16 -06001116 bi->bi_opf |= REQ_NOMERGE;
majianpeng3f9e7c12012-07-31 10:04:21 +10001117
Shaohua Lid592a992014-05-21 17:57:44 +08001118 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1119 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
Song Liu86aa1392017-01-12 17:22:41 -08001120
1121 if (!op_is_write(op) &&
1122 test_bit(R5_InJournal, &sh->dev[i].flags))
1123 /*
1124 * issuing read for a page in journal, this
1125 * must be preparing for prexor in rmw; read
1126 * the data into orig_page
1127 */
1128 sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
1129 else
1130 sh->dev[i].vec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001131 bi->bi_vcnt = 1;
Yufen Yuc911c462020-07-18 05:29:07 -04001132 bi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf);
Dan Williams91c00922007-01-02 13:52:30 -07001133 bi->bi_io_vec[0].bv_offset = 0;
Yufen Yuc911c462020-07-18 05:29:07 -04001134 bi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf);
Mariusz Dabrowski2cd259a2018-04-19 19:28:10 +02001135 bi->bi_write_hint = sh->dev[i].write_hint;
1136 if (!rrdev)
Eugene Syromiatnikovf1934892019-09-20 17:58:28 +02001137 sh->dev[i].write_hint = RWH_WRITE_LIFE_NOT_SET;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001138 /*
1139 * If this is discard request, set bi_vcnt 0. We don't
1140 * want to confuse SCSI because SCSI will replace payload
1141 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001142 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001143 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +11001144 if (rrdev)
1145 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001146
1147 if (conf->mddev->gendisk)
Christoph Hellwig74d46992017-08-23 19:10:32 +02001148 trace_block_bio_remap(bi->bi_disk->queue,
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001149 bi, disk_devt(conf->mddev->gendisk),
1150 sh->dev[i].sector);
Shaohua Liaaf9f122017-03-03 22:06:12 -08001151 if (should_defer && op_is_write(op))
1152 bio_list_add(&pending_bios, bi);
1153 else
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001154 submit_bio_noacct(bi);
NeilBrown977df362011-12-23 10:17:53 +11001155 }
1156 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001157 if (s->syncing || s->expanding || s->expanded
1158 || s->replacing)
Yufen Yuc911c462020-07-18 05:29:07 -04001159 md_sync_acct(rrdev->bdev, RAID5_STRIPE_SECTORS(conf));
NeilBrown977df362011-12-23 10:17:53 +11001160
1161 set_bit(STRIPE_IO_STARTED, &sh->state);
1162
Christoph Hellwig74d46992017-08-23 19:10:32 +02001163 bio_set_dev(rbi, rrdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -05001164 bio_set_op_attrs(rbi, op, op_flags);
1165 BUG_ON(!op_is_write(op));
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001166 rbi->bi_end_io = raid5_end_write_request;
1167 rbi->bi_private = sh;
1168
Mike Christie6296b962016-06-05 14:32:21 -05001169 pr_debug("%s: for %llu schedule op %d on "
NeilBrown977df362011-12-23 10:17:53 +11001170 "replacement disc %d\n",
1171 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001172 rbi->bi_opf, i);
NeilBrown977df362011-12-23 10:17:53 +11001173 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001174 if (sh != head_sh)
1175 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001176 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001177 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001178 + rrdev->new_data_offset);
1179 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001180 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001181 + rrdev->data_offset);
Shaohua Lid592a992014-05-21 17:57:44 +08001182 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1183 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1184 sh->dev[i].rvec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001185 rbi->bi_vcnt = 1;
Yufen Yuc911c462020-07-18 05:29:07 -04001186 rbi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf);
NeilBrown977df362011-12-23 10:17:53 +11001187 rbi->bi_io_vec[0].bv_offset = 0;
Yufen Yuc911c462020-07-18 05:29:07 -04001188 rbi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf);
Mariusz Dabrowski2cd259a2018-04-19 19:28:10 +02001189 rbi->bi_write_hint = sh->dev[i].write_hint;
Eugene Syromiatnikovf1934892019-09-20 17:58:28 +02001190 sh->dev[i].write_hint = RWH_WRITE_LIFE_NOT_SET;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001191 /*
1192 * If this is discard request, set bi_vcnt 0. We don't
1193 * want to confuse SCSI because SCSI will replace payload
1194 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001195 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001196 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001197 if (conf->mddev->gendisk)
Christoph Hellwig74d46992017-08-23 19:10:32 +02001198 trace_block_bio_remap(rbi->bi_disk->queue,
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001199 rbi, disk_devt(conf->mddev->gendisk),
1200 sh->dev[i].sector);
Shaohua Liaaf9f122017-03-03 22:06:12 -08001201 if (should_defer && op_is_write(op))
1202 bio_list_add(&pending_bios, rbi);
1203 else
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001204 submit_bio_noacct(rbi);
NeilBrown977df362011-12-23 10:17:53 +11001205 }
1206 if (!rdev && !rrdev) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001207 if (op_is_write(op))
Dan Williams91c00922007-01-02 13:52:30 -07001208 set_bit(STRIPE_DEGRADED, &sh->state);
Mike Christie6296b962016-06-05 14:32:21 -05001209 pr_debug("skip op %d on disc %d for sector %llu\n",
Jens Axboe1eff9d32016-08-05 15:35:16 -06001210 bi->bi_opf, i, (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001211 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1212 set_bit(STRIPE_HANDLE, &sh->state);
1213 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001214
1215 if (!head_sh->batch_head)
1216 continue;
1217 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1218 batch_list);
1219 if (sh != head_sh)
1220 goto again;
Dan Williams91c00922007-01-02 13:52:30 -07001221 }
Shaohua Liaaf9f122017-03-03 22:06:12 -08001222
1223 if (should_defer && !bio_list_empty(&pending_bios))
1224 defer_issue_bios(conf, head_sh->sector, &pending_bios);
Dan Williams91c00922007-01-02 13:52:30 -07001225}
1226
1227static struct dma_async_tx_descriptor *
Shaohua Lid592a992014-05-21 17:57:44 +08001228async_copy_data(int frombio, struct bio *bio, struct page **page,
1229 sector_t sector, struct dma_async_tx_descriptor *tx,
Song Liu1e6d6902016-11-17 15:24:39 -08001230 struct stripe_head *sh, int no_skipcopy)
Dan Williams91c00922007-01-02 13:52:30 -07001231{
Kent Overstreet79886132013-11-23 17:19:00 -08001232 struct bio_vec bvl;
1233 struct bvec_iter iter;
Dan Williams91c00922007-01-02 13:52:30 -07001234 struct page *bio_page;
Dan Williams91c00922007-01-02 13:52:30 -07001235 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -07001236 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -07001237 enum async_tx_flags flags = 0;
Yufen Yuc911c462020-07-18 05:29:07 -04001238 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001239
Kent Overstreet4f024f32013-10-11 15:44:27 -07001240 if (bio->bi_iter.bi_sector >= sector)
1241 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
Dan Williams91c00922007-01-02 13:52:30 -07001242 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001243 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -07001244
Dan Williams0403e382009-09-08 17:42:50 -07001245 if (frombio)
1246 flags |= ASYNC_TX_FENCE;
1247 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1248
Kent Overstreet79886132013-11-23 17:19:00 -08001249 bio_for_each_segment(bvl, bio, iter) {
1250 int len = bvl.bv_len;
Dan Williams91c00922007-01-02 13:52:30 -07001251 int clen;
1252 int b_offset = 0;
1253
1254 if (page_offset < 0) {
1255 b_offset = -page_offset;
1256 page_offset += b_offset;
1257 len -= b_offset;
1258 }
1259
Yufen Yuc911c462020-07-18 05:29:07 -04001260 if (len > 0 && page_offset + len > RAID5_STRIPE_SIZE(conf))
1261 clen = RAID5_STRIPE_SIZE(conf) - page_offset;
Dan Williams91c00922007-01-02 13:52:30 -07001262 else
1263 clen = len;
1264
1265 if (clen > 0) {
Kent Overstreet79886132013-11-23 17:19:00 -08001266 b_offset += bvl.bv_offset;
1267 bio_page = bvl.bv_page;
Shaohua Lid592a992014-05-21 17:57:44 +08001268 if (frombio) {
Yufen Yuc911c462020-07-18 05:29:07 -04001269 if (conf->skip_copy &&
Shaohua Lid592a992014-05-21 17:57:44 +08001270 b_offset == 0 && page_offset == 0 &&
Yufen Yuc911c462020-07-18 05:29:07 -04001271 clen == RAID5_STRIPE_SIZE(conf) &&
Song Liu1e6d6902016-11-17 15:24:39 -08001272 !no_skipcopy)
Shaohua Lid592a992014-05-21 17:57:44 +08001273 *page = bio_page;
1274 else
1275 tx = async_memcpy(*page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001276 b_offset, clen, &submit);
Shaohua Lid592a992014-05-21 17:57:44 +08001277 } else
1278 tx = async_memcpy(bio_page, *page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001279 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001280 }
Dan Williamsa08abd82009-06-03 11:43:59 -07001281 /* chain the operations */
1282 submit.depend_tx = tx;
1283
Dan Williams91c00922007-01-02 13:52:30 -07001284 if (clen < len) /* hit end of page */
1285 break;
1286 page_offset += len;
1287 }
1288
1289 return tx;
1290}
1291
1292static void ops_complete_biofill(void *stripe_head_ref)
1293{
1294 struct stripe_head *sh = stripe_head_ref;
Dan Williamse4d84902007-09-24 10:06:13 -07001295 int i;
Yufen Yuc911c462020-07-18 05:29:07 -04001296 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001297
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001298 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001299 (unsigned long long)sh->sector);
1300
1301 /* clear completed biofills */
1302 for (i = sh->disks; i--; ) {
1303 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001304
1305 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001306 /* and check if we need to reply to a read request,
1307 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001308 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001309 */
1310 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001311 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001312
Dan Williams91c00922007-01-02 13:52:30 -07001313 BUG_ON(!dev->read);
1314 rbi = dev->read;
1315 dev->read = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001316 while (rbi && rbi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04001317 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
1318 rbi2 = r5_next_bio(conf, rbi, dev->sector);
NeilBrown016c76a2017-03-15 14:05:13 +11001319 bio_endio(rbi);
Dan Williams91c00922007-01-02 13:52:30 -07001320 rbi = rbi2;
1321 }
1322 }
1323 }
Dan Williams83de75c2008-06-28 08:31:58 +10001324 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001325
Dan Williamse4d84902007-09-24 10:06:13 -07001326 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001327 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001328}
1329
1330static void ops_run_biofill(struct stripe_head *sh)
1331{
1332 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001333 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001334 int i;
Yufen Yuc911c462020-07-18 05:29:07 -04001335 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001336
shli@kernel.org59fc6302014-12-15 12:57:03 +11001337 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001338 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001339 (unsigned long long)sh->sector);
1340
1341 for (i = sh->disks; i--; ) {
1342 struct r5dev *dev = &sh->dev[i];
1343 if (test_bit(R5_Wantfill, &dev->flags)) {
1344 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001345 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001346 dev->read = rbi = dev->toread;
1347 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001348 spin_unlock_irq(&sh->stripe_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001349 while (rbi && rbi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04001350 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
Shaohua Lid592a992014-05-21 17:57:44 +08001351 tx = async_copy_data(0, rbi, &dev->page,
Song Liu1e6d6902016-11-17 15:24:39 -08001352 dev->sector, tx, sh, 0);
Yufen Yuc911c462020-07-18 05:29:07 -04001353 rbi = r5_next_bio(conf, rbi, dev->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001354 }
1355 }
1356 }
1357
1358 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001359 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1360 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001361}
1362
Dan Williams4e7d2c02009-08-29 19:13:11 -07001363static void mark_target_uptodate(struct stripe_head *sh, int target)
1364{
1365 struct r5dev *tgt;
1366
1367 if (target < 0)
1368 return;
1369
1370 tgt = &sh->dev[target];
1371 set_bit(R5_UPTODATE, &tgt->flags);
1372 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1373 clear_bit(R5_Wantcompute, &tgt->flags);
1374}
1375
Dan Williamsac6b53b2009-07-14 13:40:19 -07001376static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001377{
1378 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001379
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001380 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001381 (unsigned long long)sh->sector);
1382
Dan Williamsac6b53b2009-07-14 13:40:19 -07001383 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001384 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001385 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001386
Dan Williamsecc65c92008-06-28 08:31:57 +10001387 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1388 if (sh->check_state == check_state_compute_run)
1389 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001390 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001391 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001392}
1393
Dan Williamsd6f38f32009-07-14 11:50:52 -07001394/* return a pointer to the address conversion region of the scribble buffer */
Kent Overstreetb330e6a2019-03-11 23:31:06 -07001395static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
Dan Williams91c00922007-01-02 13:52:30 -07001396{
Kent Overstreetb330e6a2019-03-11 23:31:06 -07001397 return percpu->scribble + i * percpu->scribble_obj_size;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001398}
1399
1400/* return a pointer to the address conversion region of the scribble buffer */
Kent Overstreetb330e6a2019-03-11 23:31:06 -07001401static addr_conv_t *to_addr_conv(struct stripe_head *sh,
1402 struct raid5_percpu *percpu, int i)
shli@kernel.org46d5b782014-12-15 12:57:02 +11001403{
Kent Overstreetb330e6a2019-03-11 23:31:06 -07001404 return (void *) (to_addr_page(percpu, i) + sh->disks + 2);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001405}
1406
1407static struct dma_async_tx_descriptor *
1408ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1409{
Dan Williams91c00922007-01-02 13:52:30 -07001410 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001411 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001412 int target = sh->ops.target;
1413 struct r5dev *tgt = &sh->dev[target];
1414 struct page *xor_dest = tgt->page;
1415 int count = 0;
1416 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001417 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001418 int i;
1419
shli@kernel.org59fc6302014-12-15 12:57:03 +11001420 BUG_ON(sh->batch_head);
1421
Dan Williams91c00922007-01-02 13:52:30 -07001422 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001423 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001424 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1425
1426 for (i = disks; i--; )
1427 if (i != target)
1428 xor_srcs[count++] = sh->dev[i].page;
1429
1430 atomic_inc(&sh->count);
1431
Dan Williams0403e382009-09-08 17:42:50 -07001432 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001433 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
Dan Williams91c00922007-01-02 13:52:30 -07001434 if (unlikely(count == 1))
Yufen Yuc911c462020-07-18 05:29:07 -04001435 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0,
1436 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001437 else
Yufen Yuc911c462020-07-18 05:29:07 -04001438 tx = async_xor(xor_dest, xor_srcs, 0, count,
1439 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001440
Dan Williams91c00922007-01-02 13:52:30 -07001441 return tx;
1442}
1443
Dan Williamsac6b53b2009-07-14 13:40:19 -07001444/* set_syndrome_sources - populate source buffers for gen_syndrome
1445 * @srcs - (struct page *) array of size sh->disks
1446 * @sh - stripe_head to parse
1447 *
1448 * Populates srcs in proper layout order for the stripe and returns the
1449 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1450 * destination buffer is recorded in srcs[count] and the Q destination
1451 * is recorded in srcs[count+1]].
1452 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001453static int set_syndrome_sources(struct page **srcs,
1454 struct stripe_head *sh,
1455 int srctype)
Dan Williamsac6b53b2009-07-14 13:40:19 -07001456{
1457 int disks = sh->disks;
1458 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1459 int d0_idx = raid6_d0(sh);
1460 int count;
1461 int i;
1462
1463 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001464 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001465
1466 count = 0;
1467 i = d0_idx;
1468 do {
1469 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001470 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001471
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001472 if (i == sh->qd_idx || i == sh->pd_idx ||
1473 (srctype == SYNDROME_SRC_ALL) ||
1474 (srctype == SYNDROME_SRC_WANT_DRAIN &&
Song Liu1e6d6902016-11-17 15:24:39 -08001475 (test_bit(R5_Wantdrain, &dev->flags) ||
1476 test_bit(R5_InJournal, &dev->flags))) ||
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001477 (srctype == SYNDROME_SRC_WRITTEN &&
Song Liu09777622017-03-13 13:44:35 -07001478 (dev->written ||
1479 test_bit(R5_InJournal, &dev->flags)))) {
Song Liu1e6d6902016-11-17 15:24:39 -08001480 if (test_bit(R5_InJournal, &dev->flags))
1481 srcs[slot] = sh->dev[i].orig_page;
1482 else
1483 srcs[slot] = sh->dev[i].page;
1484 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001485 i = raid6_next_disk(i, disks);
1486 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001487
NeilBrowne4424fe2009-10-16 16:27:34 +11001488 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001489}
1490
1491static struct dma_async_tx_descriptor *
1492ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1493{
1494 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001495 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001496 int target;
1497 int qd_idx = sh->qd_idx;
1498 struct dma_async_tx_descriptor *tx;
1499 struct async_submit_ctl submit;
1500 struct r5dev *tgt;
1501 struct page *dest;
1502 int i;
1503 int count;
1504
shli@kernel.org59fc6302014-12-15 12:57:03 +11001505 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001506 if (sh->ops.target < 0)
1507 target = sh->ops.target2;
1508 else if (sh->ops.target2 < 0)
1509 target = sh->ops.target;
1510 else
1511 /* we should only have one valid target */
1512 BUG();
1513 BUG_ON(target < 0);
1514 pr_debug("%s: stripe %llu block: %d\n",
1515 __func__, (unsigned long long)sh->sector, target);
1516
1517 tgt = &sh->dev[target];
1518 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1519 dest = tgt->page;
1520
1521 atomic_inc(&sh->count);
1522
1523 if (target == qd_idx) {
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001524 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001525 blocks[count] = NULL; /* regenerating p is not necessary */
1526 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001527 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1528 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001529 to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04001530 tx = async_gen_syndrome(blocks, 0, count+2,
1531 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001532 } else {
1533 /* Compute any data- or p-drive using XOR */
1534 count = 0;
1535 for (i = disks; i-- ; ) {
1536 if (i == target || i == qd_idx)
1537 continue;
1538 blocks[count++] = sh->dev[i].page;
1539 }
1540
Dan Williams0403e382009-09-08 17:42:50 -07001541 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1542 NULL, ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001543 to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04001544 tx = async_xor(dest, blocks, 0, count,
1545 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001546 }
1547
1548 return tx;
1549}
1550
1551static struct dma_async_tx_descriptor *
1552ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1553{
1554 int i, count, disks = sh->disks;
1555 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1556 int d0_idx = raid6_d0(sh);
1557 int faila = -1, failb = -1;
1558 int target = sh->ops.target;
1559 int target2 = sh->ops.target2;
1560 struct r5dev *tgt = &sh->dev[target];
1561 struct r5dev *tgt2 = &sh->dev[target2];
1562 struct dma_async_tx_descriptor *tx;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001563 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001564 struct async_submit_ctl submit;
1565
shli@kernel.org59fc6302014-12-15 12:57:03 +11001566 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001567 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1568 __func__, (unsigned long long)sh->sector, target, target2);
1569 BUG_ON(target < 0 || target2 < 0);
1570 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1571 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1572
Dan Williams6c910a72009-09-16 12:24:54 -07001573 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001574 * slot number conversion for 'faila' and 'failb'
1575 */
1576 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001577 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001578 count = 0;
1579 i = d0_idx;
1580 do {
1581 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1582
1583 blocks[slot] = sh->dev[i].page;
1584
1585 if (i == target)
1586 faila = slot;
1587 if (i == target2)
1588 failb = slot;
1589 i = raid6_next_disk(i, disks);
1590 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001591
1592 BUG_ON(faila == failb);
1593 if (failb < faila)
1594 swap(faila, failb);
1595 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1596 __func__, (unsigned long long)sh->sector, faila, failb);
1597
1598 atomic_inc(&sh->count);
1599
1600 if (failb == syndrome_disks+1) {
1601 /* Q disk is one of the missing disks */
1602 if (faila == syndrome_disks) {
1603 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001604 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1605 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001606 to_addr_conv(sh, percpu, 0));
NeilBrowne4424fe2009-10-16 16:27:34 +11001607 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001608 RAID5_STRIPE_SIZE(sh->raid_conf),
1609 &submit);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001610 } else {
1611 struct page *dest;
1612 int data_target;
1613 int qd_idx = sh->qd_idx;
1614
1615 /* Missing D+Q: recompute D from P, then recompute Q */
1616 if (target == qd_idx)
1617 data_target = target2;
1618 else
1619 data_target = target;
1620
1621 count = 0;
1622 for (i = disks; i-- ; ) {
1623 if (i == data_target || i == qd_idx)
1624 continue;
1625 blocks[count++] = sh->dev[i].page;
1626 }
1627 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001628 init_async_submit(&submit,
1629 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1630 NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001631 to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04001632 tx = async_xor(dest, blocks, 0, count,
1633 RAID5_STRIPE_SIZE(sh->raid_conf),
Dan Williamsac6b53b2009-07-14 13:40:19 -07001634 &submit);
1635
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001636 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williams0403e382009-09-08 17:42:50 -07001637 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1638 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001639 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001640 return async_gen_syndrome(blocks, 0, count+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001641 RAID5_STRIPE_SIZE(sh->raid_conf),
1642 &submit);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001643 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001644 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001645 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1646 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001647 to_addr_conv(sh, percpu, 0));
Dan Williams6c910a72009-09-16 12:24:54 -07001648 if (failb == syndrome_disks) {
1649 /* We're missing D+P. */
1650 return async_raid6_datap_recov(syndrome_disks+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001651 RAID5_STRIPE_SIZE(sh->raid_conf),
1652 faila,
1653 blocks, &submit);
Dan Williams6c910a72009-09-16 12:24:54 -07001654 } else {
1655 /* We're missing D+D. */
1656 return async_raid6_2data_recov(syndrome_disks+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001657 RAID5_STRIPE_SIZE(sh->raid_conf),
1658 faila, failb,
1659 blocks, &submit);
Dan Williams6c910a72009-09-16 12:24:54 -07001660 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001661 }
1662}
1663
Dan Williams91c00922007-01-02 13:52:30 -07001664static void ops_complete_prexor(void *stripe_head_ref)
1665{
1666 struct stripe_head *sh = stripe_head_ref;
1667
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001668 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001669 (unsigned long long)sh->sector);
Song Liu1e6d6902016-11-17 15:24:39 -08001670
1671 if (r5c_is_writeback(sh->raid_conf->log))
1672 /*
1673 * raid5-cache write back uses orig_page during prexor.
1674 * After prexor, it is time to free orig_page
1675 */
1676 r5c_release_extra_page(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001677}
1678
1679static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001680ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1681 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001682{
Dan Williams91c00922007-01-02 13:52:30 -07001683 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001684 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001685 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001686 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001687
1688 /* existing parity data subtracted */
1689 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1690
shli@kernel.org59fc6302014-12-15 12:57:03 +11001691 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001692 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001693 (unsigned long long)sh->sector);
1694
1695 for (i = disks; i--; ) {
1696 struct r5dev *dev = &sh->dev[i];
1697 /* Only process blocks that are known to be uptodate */
Song Liu1e6d6902016-11-17 15:24:39 -08001698 if (test_bit(R5_InJournal, &dev->flags))
1699 xor_srcs[count++] = dev->orig_page;
1700 else if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001701 xor_srcs[count++] = dev->page;
1702 }
1703
Dan Williams0403e382009-09-08 17:42:50 -07001704 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001705 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04001706 tx = async_xor(xor_dest, xor_srcs, 0, count,
1707 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001708
1709 return tx;
1710}
1711
1712static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001713ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1714 struct dma_async_tx_descriptor *tx)
1715{
1716 struct page **blocks = to_addr_page(percpu, 0);
1717 int count;
1718 struct async_submit_ctl submit;
1719
1720 pr_debug("%s: stripe %llu\n", __func__,
1721 (unsigned long long)sh->sector);
1722
1723 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN);
1724
1725 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1726 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04001727 tx = async_gen_syndrome(blocks, 0, count+2,
1728 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001729
1730 return tx;
1731}
1732
1733static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001734ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001735{
Song Liu1e6d6902016-11-17 15:24:39 -08001736 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001737 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001738 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001739 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -07001740
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001741 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001742 (unsigned long long)sh->sector);
1743
1744 for (i = disks; i--; ) {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001745 struct r5dev *dev;
Dan Williams91c00922007-01-02 13:52:30 -07001746 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001747
shli@kernel.org59fc6302014-12-15 12:57:03 +11001748 sh = head_sh;
1749 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001750 struct bio *wbi;
1751
shli@kernel.org59fc6302014-12-15 12:57:03 +11001752again:
1753 dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001754 /*
1755 * clear R5_InJournal, so when rewriting a page in
1756 * journal, it is not skipped by r5l_log_stripe()
1757 */
1758 clear_bit(R5_InJournal, &dev->flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10001759 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001760 chosen = dev->towrite;
1761 dev->towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11001762 sh->overwrite_disks = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001763 BUG_ON(dev->written);
1764 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001765 spin_unlock_irq(&sh->stripe_lock);
Shaohua Lid592a992014-05-21 17:57:44 +08001766 WARN_ON(dev->page != dev->orig_page);
Dan Williams91c00922007-01-02 13:52:30 -07001767
Kent Overstreet4f024f32013-10-11 15:44:27 -07001768 while (wbi && wbi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04001769 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001770 if (wbi->bi_opf & REQ_FUA)
Tejun Heoe9c74692010-09-03 11:56:18 +02001771 set_bit(R5_WantFUA, &dev->flags);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001772 if (wbi->bi_opf & REQ_SYNC)
Shaohua Libc0934f2012-05-22 13:55:05 +10001773 set_bit(R5_SyncIO, &dev->flags);
Mike Christie796a5cf2016-06-05 14:32:07 -05001774 if (bio_op(wbi) == REQ_OP_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001775 set_bit(R5_Discard, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08001776 else {
1777 tx = async_copy_data(1, wbi, &dev->page,
Song Liu1e6d6902016-11-17 15:24:39 -08001778 dev->sector, tx, sh,
1779 r5c_is_writeback(conf->log));
1780 if (dev->page != dev->orig_page &&
1781 !r5c_is_writeback(conf->log)) {
Shaohua Lid592a992014-05-21 17:57:44 +08001782 set_bit(R5_SkipCopy, &dev->flags);
1783 clear_bit(R5_UPTODATE, &dev->flags);
1784 clear_bit(R5_OVERWRITE, &dev->flags);
1785 }
1786 }
Yufen Yuc911c462020-07-18 05:29:07 -04001787 wbi = r5_next_bio(conf, wbi, dev->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001788 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001789
1790 if (head_sh->batch_head) {
1791 sh = list_first_entry(&sh->batch_list,
1792 struct stripe_head,
1793 batch_list);
1794 if (sh == head_sh)
1795 continue;
1796 goto again;
1797 }
Dan Williams91c00922007-01-02 13:52:30 -07001798 }
1799 }
1800
1801 return tx;
1802}
1803
Dan Williamsac6b53b2009-07-14 13:40:19 -07001804static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001805{
1806 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001807 int disks = sh->disks;
1808 int pd_idx = sh->pd_idx;
1809 int qd_idx = sh->qd_idx;
1810 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001811 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001812
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001813 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001814 (unsigned long long)sh->sector);
1815
Shaohua Libc0934f2012-05-22 13:55:05 +10001816 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001817 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001818 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001819 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001820 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001821
Dan Williams91c00922007-01-02 13:52:30 -07001822 for (i = disks; i--; ) {
1823 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001824
Tejun Heoe9c74692010-09-03 11:56:18 +02001825 if (dev->written || i == pd_idx || i == qd_idx) {
NeilBrown235b6002017-10-17 16:18:36 +11001826 if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001827 set_bit(R5_UPTODATE, &dev->flags);
NeilBrown235b6002017-10-17 16:18:36 +11001828 if (test_bit(STRIPE_EXPAND_READY, &sh->state))
1829 set_bit(R5_Expanded, &dev->flags);
1830 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001831 if (fua)
1832 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001833 if (sync)
1834 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001835 }
Dan Williams91c00922007-01-02 13:52:30 -07001836 }
1837
Dan Williamsd8ee0722008-06-28 08:32:06 +10001838 if (sh->reconstruct_state == reconstruct_state_drain_run)
1839 sh->reconstruct_state = reconstruct_state_drain_result;
1840 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1841 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1842 else {
1843 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1844 sh->reconstruct_state = reconstruct_state_result;
1845 }
Dan Williams91c00922007-01-02 13:52:30 -07001846
1847 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001848 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001849}
1850
1851static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001852ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1853 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001854{
Dan Williams91c00922007-01-02 13:52:30 -07001855 int disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001856 struct page **xor_srcs;
Dan Williamsa08abd82009-06-03 11:43:59 -07001857 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001858 int count, pd_idx = sh->pd_idx, i;
Dan Williams91c00922007-01-02 13:52:30 -07001859 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001860 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001861 unsigned long flags;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001862 int j = 0;
1863 struct stripe_head *head_sh = sh;
1864 int last_stripe;
Dan Williams91c00922007-01-02 13:52:30 -07001865
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001866 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001867 (unsigned long long)sh->sector);
1868
Shaohua Li620125f2012-10-11 13:49:05 +11001869 for (i = 0; i < sh->disks; i++) {
1870 if (pd_idx == i)
1871 continue;
1872 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1873 break;
1874 }
1875 if (i >= sh->disks) {
1876 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001877 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1878 ops_complete_reconstruct(sh);
1879 return;
1880 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001881again:
1882 count = 0;
1883 xor_srcs = to_addr_page(percpu, j);
Dan Williams91c00922007-01-02 13:52:30 -07001884 /* check if prexor is active which means only process blocks
1885 * that are part of a read-modify-write (written)
1886 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001887 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001888 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001889 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1890 for (i = disks; i--; ) {
1891 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001892 if (head_sh->dev[i].written ||
1893 test_bit(R5_InJournal, &head_sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -07001894 xor_srcs[count++] = dev->page;
1895 }
1896 } else {
1897 xor_dest = sh->dev[pd_idx].page;
1898 for (i = disks; i--; ) {
1899 struct r5dev *dev = &sh->dev[i];
1900 if (i != pd_idx)
1901 xor_srcs[count++] = dev->page;
1902 }
1903 }
1904
Dan Williams91c00922007-01-02 13:52:30 -07001905 /* 1/ if we prexor'd then the dest is reused as a source
1906 * 2/ if we did not prexor then we are redoing the parity
1907 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1908 * for the synchronous xor case
1909 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001910 last_stripe = !head_sh->batch_head ||
1911 list_first_entry(&sh->batch_list,
1912 struct stripe_head, batch_list) == head_sh;
1913 if (last_stripe) {
1914 flags = ASYNC_TX_ACK |
1915 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
Dan Williams91c00922007-01-02 13:52:30 -07001916
shli@kernel.org59fc6302014-12-15 12:57:03 +11001917 atomic_inc(&head_sh->count);
1918 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
1919 to_addr_conv(sh, percpu, j));
1920 } else {
1921 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
1922 init_async_submit(&submit, flags, tx, NULL, NULL,
1923 to_addr_conv(sh, percpu, j));
1924 }
Dan Williams91c00922007-01-02 13:52:30 -07001925
Dan Williamsa08abd82009-06-03 11:43:59 -07001926 if (unlikely(count == 1))
Yufen Yuc911c462020-07-18 05:29:07 -04001927 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0,
1928 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williamsa08abd82009-06-03 11:43:59 -07001929 else
Yufen Yuc911c462020-07-18 05:29:07 -04001930 tx = async_xor(xor_dest, xor_srcs, 0, count,
1931 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001932 if (!last_stripe) {
1933 j++;
1934 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1935 batch_list);
1936 goto again;
1937 }
Dan Williams91c00922007-01-02 13:52:30 -07001938}
1939
Dan Williamsac6b53b2009-07-14 13:40:19 -07001940static void
1941ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1942 struct dma_async_tx_descriptor *tx)
1943{
1944 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001945 struct page **blocks;
1946 int count, i, j = 0;
1947 struct stripe_head *head_sh = sh;
1948 int last_stripe;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001949 int synflags;
1950 unsigned long txflags;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001951
1952 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1953
Shaohua Li620125f2012-10-11 13:49:05 +11001954 for (i = 0; i < sh->disks; i++) {
1955 if (sh->pd_idx == i || sh->qd_idx == i)
1956 continue;
1957 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1958 break;
1959 }
1960 if (i >= sh->disks) {
1961 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001962 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1963 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1964 ops_complete_reconstruct(sh);
1965 return;
1966 }
1967
shli@kernel.org59fc6302014-12-15 12:57:03 +11001968again:
1969 blocks = to_addr_page(percpu, j);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001970
1971 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1972 synflags = SYNDROME_SRC_WRITTEN;
1973 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
1974 } else {
1975 synflags = SYNDROME_SRC_ALL;
1976 txflags = ASYNC_TX_ACK;
1977 }
1978
1979 count = set_syndrome_sources(blocks, sh, synflags);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001980 last_stripe = !head_sh->batch_head ||
1981 list_first_entry(&sh->batch_list,
1982 struct stripe_head, batch_list) == head_sh;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001983
shli@kernel.org59fc6302014-12-15 12:57:03 +11001984 if (last_stripe) {
1985 atomic_inc(&head_sh->count);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001986 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
shli@kernel.org59fc6302014-12-15 12:57:03 +11001987 head_sh, to_addr_conv(sh, percpu, j));
1988 } else
1989 init_async_submit(&submit, 0, tx, NULL, NULL,
1990 to_addr_conv(sh, percpu, j));
Yufen Yuc911c462020-07-18 05:29:07 -04001991 tx = async_gen_syndrome(blocks, 0, count+2,
1992 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001993 if (!last_stripe) {
1994 j++;
1995 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1996 batch_list);
1997 goto again;
1998 }
Dan Williams91c00922007-01-02 13:52:30 -07001999}
2000
2001static void ops_complete_check(void *stripe_head_ref)
2002{
2003 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07002004
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002005 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07002006 (unsigned long long)sh->sector);
2007
Dan Williamsecc65c92008-06-28 08:31:57 +10002008 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07002009 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002010 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07002011}
2012
Dan Williamsac6b53b2009-07-14 13:40:19 -07002013static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07002014{
Dan Williams91c00922007-01-02 13:52:30 -07002015 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002016 int pd_idx = sh->pd_idx;
2017 int qd_idx = sh->qd_idx;
2018 struct page *xor_dest;
shli@kernel.org46d5b782014-12-15 12:57:02 +11002019 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07002020 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07002021 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002022 int count;
2023 int i;
Dan Williams91c00922007-01-02 13:52:30 -07002024
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002025 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07002026 (unsigned long long)sh->sector);
2027
shli@kernel.org59fc6302014-12-15 12:57:03 +11002028 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002029 count = 0;
2030 xor_dest = sh->dev[pd_idx].page;
2031 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07002032 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07002033 if (i == pd_idx || i == qd_idx)
2034 continue;
2035 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07002036 }
2037
Dan Williamsd6f38f32009-07-14 11:50:52 -07002038 init_async_submit(&submit, 0, NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11002039 to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04002040 tx = async_xor_val(xor_dest, xor_srcs, 0, count,
2041 RAID5_STRIPE_SIZE(sh->raid_conf),
Dan Williamsa08abd82009-06-03 11:43:59 -07002042 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07002043
Dan Williams91c00922007-01-02 13:52:30 -07002044 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07002045 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
2046 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07002047}
2048
Dan Williamsac6b53b2009-07-14 13:40:19 -07002049static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
2050{
shli@kernel.org46d5b782014-12-15 12:57:02 +11002051 struct page **srcs = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002052 struct async_submit_ctl submit;
2053 int count;
2054
2055 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
2056 (unsigned long long)sh->sector, checkp);
2057
shli@kernel.org59fc6302014-12-15 12:57:03 +11002058 BUG_ON(sh->batch_head);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002059 count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002060 if (!checkp)
2061 srcs[count] = NULL;
2062
2063 atomic_inc(&sh->count);
2064 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
shli@kernel.org46d5b782014-12-15 12:57:02 +11002065 sh, to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04002066 async_syndrome_val(srcs, 0, count+2,
2067 RAID5_STRIPE_SIZE(sh->raid_conf),
Dan Williamsac6b53b2009-07-14 13:40:19 -07002068 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
2069}
2070
NeilBrown51acbce2013-02-28 09:08:34 +11002071static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07002072{
2073 int overlap_clear = 0, i, disks = sh->disks;
2074 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11002075 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002076 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002077 struct raid5_percpu *percpu;
2078 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07002079
Dan Williamsd6f38f32009-07-14 11:50:52 -07002080 cpu = get_cpu();
2081 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10002082 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07002083 ops_run_biofill(sh);
2084 overlap_clear++;
2085 }
2086
Dan Williams7b3a8712008-06-28 08:32:09 +10002087 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07002088 if (level < 6)
2089 tx = ops_run_compute5(sh, percpu);
2090 else {
2091 if (sh->ops.target2 < 0 || sh->ops.target < 0)
2092 tx = ops_run_compute6_1(sh, percpu);
2093 else
2094 tx = ops_run_compute6_2(sh, percpu);
2095 }
2096 /* terminate the chain if reconstruct is not set to be run */
2097 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10002098 async_tx_ack(tx);
2099 }
Dan Williams91c00922007-01-02 13:52:30 -07002100
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002101 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
2102 if (level < 6)
2103 tx = ops_run_prexor5(sh, percpu, tx);
2104 else
2105 tx = ops_run_prexor6(sh, percpu, tx);
2106 }
Dan Williams91c00922007-01-02 13:52:30 -07002107
Artur Paszkiewiczae1713e2017-04-04 13:13:58 +02002108 if (test_bit(STRIPE_OP_PARTIAL_PARITY, &ops_request))
2109 tx = ops_run_partial_parity(sh, percpu, tx);
2110
Dan Williams600aa102008-06-28 08:32:05 +10002111 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10002112 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07002113 overlap_clear++;
2114 }
2115
Dan Williamsac6b53b2009-07-14 13:40:19 -07002116 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
2117 if (level < 6)
2118 ops_run_reconstruct5(sh, percpu, tx);
2119 else
2120 ops_run_reconstruct6(sh, percpu, tx);
2121 }
Dan Williams91c00922007-01-02 13:52:30 -07002122
Dan Williamsac6b53b2009-07-14 13:40:19 -07002123 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
2124 if (sh->check_state == check_state_run)
2125 ops_run_check_p(sh, percpu);
2126 else if (sh->check_state == check_state_run_q)
2127 ops_run_check_pq(sh, percpu, 0);
2128 else if (sh->check_state == check_state_run_pq)
2129 ops_run_check_pq(sh, percpu, 1);
2130 else
2131 BUG();
2132 }
Dan Williams91c00922007-01-02 13:52:30 -07002133
shli@kernel.org59fc6302014-12-15 12:57:03 +11002134 if (overlap_clear && !sh->batch_head)
Dan Williams91c00922007-01-02 13:52:30 -07002135 for (i = disks; i--; ) {
2136 struct r5dev *dev = &sh->dev[i];
2137 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2138 wake_up(&sh->raid_conf->wait_for_overlap);
2139 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07002140 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07002141}
2142
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002143static void free_stripe(struct kmem_cache *sc, struct stripe_head *sh)
2144{
2145 if (sh->ppl_page)
2146 __free_page(sh->ppl_page);
2147 kmem_cache_free(sc, sh);
2148}
2149
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002150static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002151 int disks, struct r5conf *conf)
NeilBrownf18c1a32015-05-08 18:19:04 +10002152{
2153 struct stripe_head *sh;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002154 int i;
NeilBrownf18c1a32015-05-08 18:19:04 +10002155
2156 sh = kmem_cache_zalloc(sc, gfp);
2157 if (sh) {
2158 spin_lock_init(&sh->stripe_lock);
2159 spin_lock_init(&sh->batch_lock);
2160 INIT_LIST_HEAD(&sh->batch_list);
2161 INIT_LIST_HEAD(&sh->lru);
Song Liua39f7af2016-11-17 15:24:40 -08002162 INIT_LIST_HEAD(&sh->r5c);
Song Liud7bd3982016-11-23 22:50:39 -08002163 INIT_LIST_HEAD(&sh->log_list);
NeilBrownf18c1a32015-05-08 18:19:04 +10002164 atomic_set(&sh->count, 1);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002165 sh->raid_conf = conf;
Song Liua39f7af2016-11-17 15:24:40 -08002166 sh->log_start = MaxSector;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002167 for (i = 0; i < disks; i++) {
2168 struct r5dev *dev = &sh->dev[i];
2169
Ming Lei3a83f462016-11-22 08:57:21 -07002170 bio_init(&dev->req, &dev->vec, 1);
2171 bio_init(&dev->rreq, &dev->rvec, 1);
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002172 }
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002173
2174 if (raid5_has_ppl(conf)) {
2175 sh->ppl_page = alloc_page(gfp);
2176 if (!sh->ppl_page) {
2177 free_stripe(sc, sh);
2178 sh = NULL;
2179 }
2180 }
NeilBrownf18c1a32015-05-08 18:19:04 +10002181 }
2182 return sh;
2183}
NeilBrown486f0642015-02-25 12:10:35 +11002184static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 struct stripe_head *sh;
NeilBrownf18c1a32015-05-08 18:19:04 +10002187
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002188 sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size, conf);
NeilBrown3f294f42005-11-08 21:39:25 -08002189 if (!sh)
2190 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10002191
NeilBrowna9683a72015-02-25 12:02:51 +11002192 if (grow_buffers(sh, gfp)) {
NeilBrowne4e11e32010-06-16 16:45:16 +10002193 shrink_buffers(sh);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002194 free_stripe(conf->slab_cache, sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002195 return 0;
2196 }
NeilBrown486f0642015-02-25 12:10:35 +11002197 sh->hash_lock_index =
2198 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrown3f294f42005-11-08 21:39:25 -08002199 /* we just created an active stripe so... */
NeilBrown3f294f42005-11-08 21:39:25 -08002200 atomic_inc(&conf->active_stripes);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002201
Shaohua Li6d036f72015-08-13 14:31:57 -07002202 raid5_release_stripe(sh);
NeilBrown486f0642015-02-25 12:10:35 +11002203 conf->max_nr_stripes++;
NeilBrown3f294f42005-11-08 21:39:25 -08002204 return 1;
2205}
2206
NeilBrownd1688a62011-10-11 16:49:52 +11002207static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08002208{
Christoph Lametere18b8902006-12-06 20:33:20 -08002209 struct kmem_cache *sc;
Arnd Bergmann53b8d892018-02-20 14:09:11 +01002210 size_t namelen = sizeof(conf->cache_name[0]);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002211 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
NeilBrownf4be6b42010-06-01 19:37:25 +10002213 if (conf->mddev->gendisk)
Arnd Bergmann53b8d892018-02-20 14:09:11 +01002214 snprintf(conf->cache_name[0], namelen,
NeilBrownf4be6b42010-06-01 19:37:25 +10002215 "raid%d-%s", conf->level, mdname(conf->mddev));
2216 else
Arnd Bergmann53b8d892018-02-20 14:09:11 +01002217 snprintf(conf->cache_name[0], namelen,
NeilBrownf4be6b42010-06-01 19:37:25 +10002218 "raid%d-%p", conf->level, conf->mddev);
Arnd Bergmann53b8d892018-02-20 14:09:11 +01002219 snprintf(conf->cache_name[1], namelen, "%.27s-alt", conf->cache_name[0]);
NeilBrownf4be6b42010-06-01 19:37:25 +10002220
NeilBrownad01c9e2006-03-27 01:18:07 -08002221 conf->active_name = 0;
2222 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002224 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 if (!sc)
2226 return 1;
2227 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002228 conf->pool_size = devs;
NeilBrown486f0642015-02-25 12:10:35 +11002229 while (num--)
2230 if (!grow_one_stripe(conf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 return 1;
NeilBrown486f0642015-02-25 12:10:35 +11002232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 return 0;
2234}
NeilBrown29269552006-03-27 01:18:10 -08002235
Dan Williamsd6f38f32009-07-14 11:50:52 -07002236/**
Coly Li7f8a30e2020-04-09 22:17:22 +08002237 * scribble_alloc - allocate percpu scribble buffer for required size
2238 * of the scribble region
Damien Le Moal2aada5b2020-07-16 13:54:42 +09002239 * @percpu: from for_each_present_cpu() of the caller
2240 * @num: total number of disks in the array
2241 * @cnt: scribble objs count for required size of the scribble region
Dan Williamsd6f38f32009-07-14 11:50:52 -07002242 *
Coly Li7f8a30e2020-04-09 22:17:22 +08002243 * The scribble buffer size must be enough to contain:
Dan Williamsd6f38f32009-07-14 11:50:52 -07002244 * 1/ a struct page pointer for each device in the array +2
2245 * 2/ room to convert each entry in (1) to its corresponding dma
2246 * (dma_map_page()) or page (page_address()) address.
2247 *
2248 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2249 * calculate over all devices (not just the data blocks), using zeros in place
2250 * of the P and Q blocks.
2251 */
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002252static int scribble_alloc(struct raid5_percpu *percpu,
Coly Liba54d4d2020-04-09 22:17:21 +08002253 int num, int cnt)
Dan Williamsd6f38f32009-07-14 11:50:52 -07002254{
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002255 size_t obj_size =
2256 sizeof(struct page *) * (num+2) +
2257 sizeof(addr_conv_t) * (num+2);
2258 void *scribble;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002259
Coly Liba54d4d2020-04-09 22:17:21 +08002260 /*
2261 * If here is in raid array suspend context, it is in memalloc noio
2262 * context as well, there is no potential recursive memory reclaim
2263 * I/Os with the GFP_KERNEL flag.
2264 */
2265 scribble = kvmalloc_array(cnt, obj_size, GFP_KERNEL);
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002266 if (!scribble)
2267 return -ENOMEM;
2268
2269 kvfree(percpu->scribble);
2270
2271 percpu->scribble = scribble;
2272 percpu->scribble_obj_size = obj_size;
2273 return 0;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002274}
2275
NeilBrown738a2732015-05-08 18:19:39 +10002276static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2277{
2278 unsigned long cpu;
2279 int err = 0;
2280
Shaohua Li27a353c2016-02-24 17:38:28 -08002281 /*
2282 * Never shrink. And mddev_suspend() could deadlock if this is called
2283 * from raid5d. In that case, scribble_disks and scribble_sectors
2284 * should equal to new_disks and new_sectors
2285 */
2286 if (conf->scribble_disks >= new_disks &&
2287 conf->scribble_sectors >= new_sectors)
2288 return 0;
NeilBrown738a2732015-05-08 18:19:39 +10002289 mddev_suspend(conf->mddev);
2290 get_online_cpus();
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002291
NeilBrown738a2732015-05-08 18:19:39 +10002292 for_each_present_cpu(cpu) {
2293 struct raid5_percpu *percpu;
NeilBrown738a2732015-05-08 18:19:39 +10002294
2295 percpu = per_cpu_ptr(conf->percpu, cpu);
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002296 err = scribble_alloc(percpu, new_disks,
Yufen Yuc911c462020-07-18 05:29:07 -04002297 new_sectors / RAID5_STRIPE_SECTORS(conf));
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002298 if (err)
NeilBrown738a2732015-05-08 18:19:39 +10002299 break;
NeilBrown738a2732015-05-08 18:19:39 +10002300 }
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002301
NeilBrown738a2732015-05-08 18:19:39 +10002302 put_online_cpus();
2303 mddev_resume(conf->mddev);
Shaohua Li27a353c2016-02-24 17:38:28 -08002304 if (!err) {
2305 conf->scribble_disks = new_disks;
2306 conf->scribble_sectors = new_sectors;
2307 }
NeilBrown738a2732015-05-08 18:19:39 +10002308 return err;
2309}
2310
NeilBrownd1688a62011-10-11 16:49:52 +11002311static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08002312{
2313 /* Make all the stripes able to hold 'newsize' devices.
2314 * New slots in each stripe get 'page' set to a new page.
2315 *
2316 * This happens in stages:
2317 * 1/ create a new kmem_cache and allocate the required number of
2318 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09002319 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08002320 * to the new stripe_heads. This will have the side effect of
2321 * freezing the array as once all stripe_heads have been collected,
2322 * no IO will be possible. Old stripe heads are freed once their
2323 * pages have been transferred over, and the old kmem_cache is
2324 * freed when all stripes are done.
2325 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
Zhilong Liu3560741e2017-03-15 16:14:53 +08002326 * we simple return a failure status - no need to clean anything up.
NeilBrownad01c9e2006-03-27 01:18:07 -08002327 * 4/ allocate new pages for the new slots in the new stripe_heads.
2328 * If this fails, we don't bother trying the shrink the
2329 * stripe_heads down again, we just leave them as they are.
2330 * As each stripe_head is processed the new one is released into
2331 * active service.
2332 *
2333 * Once step2 is started, we cannot afford to wait for a write,
2334 * so we use GFP_NOIO allocations.
2335 */
2336 struct stripe_head *osh, *nsh;
2337 LIST_HEAD(newstripes);
2338 struct disk_info *ndisks;
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02002339 int err = 0;
Christoph Lametere18b8902006-12-06 20:33:20 -08002340 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002341 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11002342 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08002343
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02002344 md_allow_write(conf->mddev);
NeilBrown2a2275d2007-01-26 00:57:11 -08002345
NeilBrownad01c9e2006-03-27 01:18:07 -08002346 /* Step 1 */
2347 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2348 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002349 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08002350 if (!sc)
2351 return -ENOMEM;
2352
NeilBrown2d5b5692015-07-06 12:49:23 +10002353 /* Need to ensure auto-resizing doesn't interfere */
2354 mutex_lock(&conf->cache_size_mutex);
2355
NeilBrownad01c9e2006-03-27 01:18:07 -08002356 for (i = conf->max_nr_stripes; i; i--) {
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002357 nsh = alloc_stripe(sc, GFP_KERNEL, newsize, conf);
NeilBrownad01c9e2006-03-27 01:18:07 -08002358 if (!nsh)
2359 break;
2360
NeilBrownad01c9e2006-03-27 01:18:07 -08002361 list_add(&nsh->lru, &newstripes);
2362 }
2363 if (i) {
2364 /* didn't get enough, give up */
2365 while (!list_empty(&newstripes)) {
2366 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2367 list_del(&nsh->lru);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002368 free_stripe(sc, nsh);
NeilBrownad01c9e2006-03-27 01:18:07 -08002369 }
2370 kmem_cache_destroy(sc);
NeilBrown2d5b5692015-07-06 12:49:23 +10002371 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002372 return -ENOMEM;
2373 }
2374 /* Step 2 - Must use GFP_NOIO now.
2375 * OK, we have enough stripes, start collecting inactive
2376 * stripes and copying them over
2377 */
Shaohua Li566c09c2013-11-14 15:16:17 +11002378 hash = 0;
2379 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08002380 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11002381 lock_device_hash_lock(conf, hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08002382 wait_event_cmd(conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +11002383 !list_empty(conf->inactive_list + hash),
2384 unlock_device_hash_lock(conf, hash),
2385 lock_device_hash_lock(conf, hash));
2386 osh = get_free_stripe(conf, hash);
2387 unlock_device_hash_lock(conf, hash);
NeilBrownf18c1a32015-05-08 18:19:04 +10002388
Shaohua Lid592a992014-05-21 17:57:44 +08002389 for(i=0; i<conf->pool_size; i++) {
NeilBrownad01c9e2006-03-27 01:18:07 -08002390 nsh->dev[i].page = osh->dev[i].page;
Shaohua Lid592a992014-05-21 17:57:44 +08002391 nsh->dev[i].orig_page = osh->dev[i].page;
2392 }
Shaohua Li566c09c2013-11-14 15:16:17 +11002393 nsh->hash_lock_index = hash;
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002394 free_stripe(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11002395 cnt++;
2396 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2397 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2398 hash++;
2399 cnt = 0;
2400 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002401 }
2402 kmem_cache_destroy(conf->slab_cache);
2403
2404 /* Step 3.
2405 * At this point, we are holding all the stripes so the array
2406 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07002407 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08002408 */
Kees Cook6396bb22018-06-12 14:03:40 -07002409 ndisks = kcalloc(newsize, sizeof(struct disk_info), GFP_NOIO);
NeilBrownad01c9e2006-03-27 01:18:07 -08002410 if (ndisks) {
Song Liud7bd3982016-11-23 22:50:39 -08002411 for (i = 0; i < conf->pool_size; i++)
NeilBrownad01c9e2006-03-27 01:18:07 -08002412 ndisks[i] = conf->disks[i];
Song Liud7bd3982016-11-23 22:50:39 -08002413
2414 for (i = conf->pool_size; i < newsize; i++) {
2415 ndisks[i].extra_page = alloc_page(GFP_NOIO);
2416 if (!ndisks[i].extra_page)
2417 err = -ENOMEM;
2418 }
2419
2420 if (err) {
2421 for (i = conf->pool_size; i < newsize; i++)
2422 if (ndisks[i].extra_page)
2423 put_page(ndisks[i].extra_page);
2424 kfree(ndisks);
2425 } else {
2426 kfree(conf->disks);
2427 conf->disks = ndisks;
2428 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002429 } else
2430 err = -ENOMEM;
2431
NeilBrown2d5b5692015-07-06 12:49:23 +10002432 mutex_unlock(&conf->cache_size_mutex);
Dennis Yang583da482017-03-29 15:46:13 +08002433
2434 conf->slab_cache = sc;
2435 conf->active_name = 1-conf->active_name;
2436
NeilBrownad01c9e2006-03-27 01:18:07 -08002437 /* Step 4, return new stripes to service */
2438 while(!list_empty(&newstripes)) {
2439 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2440 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07002441
NeilBrownad01c9e2006-03-27 01:18:07 -08002442 for (i=conf->raid_disks; i < newsize; i++)
2443 if (nsh->dev[i].page == NULL) {
2444 struct page *p = alloc_page(GFP_NOIO);
2445 nsh->dev[i].page = p;
Shaohua Lid592a992014-05-21 17:57:44 +08002446 nsh->dev[i].orig_page = p;
NeilBrownad01c9e2006-03-27 01:18:07 -08002447 if (!p)
2448 err = -ENOMEM;
2449 }
Shaohua Li6d036f72015-08-13 14:31:57 -07002450 raid5_release_stripe(nsh);
NeilBrownad01c9e2006-03-27 01:18:07 -08002451 }
2452 /* critical section pass, GFP_NOIO no longer needed */
2453
NeilBrown6e9eac22015-05-08 18:19:34 +10002454 if (!err)
2455 conf->pool_size = newsize;
NeilBrownad01c9e2006-03-27 01:18:07 -08002456 return err;
2457}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
NeilBrown486f0642015-02-25 12:10:35 +11002459static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
2461 struct stripe_head *sh;
NeilBrown49895bc2015-08-03 17:09:57 +10002462 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Shaohua Li566c09c2013-11-14 15:16:17 +11002464 spin_lock_irq(conf->hash_locks + hash);
2465 sh = get_free_stripe(conf, hash);
2466 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08002467 if (!sh)
2468 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002469 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10002470 shrink_buffers(sh);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002471 free_stripe(conf->slab_cache, sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002472 atomic_dec(&conf->active_stripes);
NeilBrown486f0642015-02-25 12:10:35 +11002473 conf->max_nr_stripes--;
NeilBrown3f294f42005-11-08 21:39:25 -08002474 return 1;
2475}
2476
NeilBrownd1688a62011-10-11 16:49:52 +11002477static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08002478{
NeilBrown486f0642015-02-25 12:10:35 +11002479 while (conf->max_nr_stripes &&
2480 drop_one_stripe(conf))
2481 ;
NeilBrown3f294f42005-11-08 21:39:25 -08002482
Julia Lawall644df1a2015-09-13 14:15:10 +02002483 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 conf->slab_cache = NULL;
2485}
2486
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002487static void raid5_end_read_request(struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
NeilBrown99c0fb52009-03-31 14:39:38 +11002489 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002490 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002491 int disks = sh->disks, i;
NeilBrownd6950432006-07-10 04:44:20 -07002492 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11002493 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10002494 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
2496 for (i=0 ; i<disks; i++)
2497 if (bi == &sh->dev[i].req)
2498 break;
2499
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002500 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
Dan Williams45b42332007-07-09 11:56:43 -07002501 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002502 bi->bi_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002504 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002506 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 }
NeilBrown14a75d32011-12-23 10:17:52 +11002508 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11002509 /* If replacement finished while this request was outstanding,
2510 * 'replacement' might be NULL already.
2511 * In that case it moved down to 'rdev'.
2512 * rdev is not removed until all requests are finished.
2513 */
NeilBrown14a75d32011-12-23 10:17:52 +11002514 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002515 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11002516 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
NeilBrown05616be2012-05-21 09:27:00 +10002518 if (use_new_offset(conf, sh))
2519 s = sh->sector + rdev->new_data_offset;
2520 else
2521 s = sh->sector + rdev->data_offset;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002522 if (!bi->bi_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08002524 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11002525 /* Note that this cannot happen on a
2526 * replacement device. We just fail those on
2527 * any error
2528 */
NeilBrowncc6167b2016-11-02 14:16:50 +11002529 pr_info_ratelimited(
2530 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
Yufen Yuc911c462020-07-18 05:29:07 -04002531 mdname(conf->mddev), RAID5_STRIPE_SECTORS(conf),
NeilBrown05616be2012-05-21 09:27:00 +10002532 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002533 bdevname(rdev->bdev, b));
Yufen Yuc911c462020-07-18 05:29:07 -04002534 atomic_add(RAID5_STRIPE_SECTORS(conf), &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08002535 clear_bit(R5_ReadError, &sh->dev[i].flags);
2536 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002537 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2538 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2539
Song Liu86aa1392017-01-12 17:22:41 -08002540 if (test_bit(R5_InJournal, &sh->dev[i].flags))
2541 /*
2542 * end read for a page in journal, this
2543 * must be preparing for prexor in rmw
2544 */
2545 set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
2546
NeilBrown14a75d32011-12-23 10:17:52 +11002547 if (atomic_read(&rdev->read_errors))
2548 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002550 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002551 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002552 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
Nigel Croxonb76b4712019-09-06 09:21:33 -04002555 if (!(bi->bi_status == BLK_STS_PROTECTION))
2556 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002557 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowncc6167b2016-11-02 14:16:50 +11002558 pr_warn_ratelimited(
2559 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
NeilBrown14a75d32011-12-23 10:17:52 +11002560 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002561 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002562 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002563 else if (conf->mddev->degraded >= conf->max_degraded) {
2564 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002565 pr_warn_ratelimited(
2566 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002567 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002568 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002569 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002570 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002571 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002572 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002573 pr_warn_ratelimited(
2574 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002575 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002576 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002577 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002578 } else if (atomic_read(&rdev->read_errors)
Nigel Croxon0009fad2019-08-21 09:27:08 -04002579 > conf->max_nr_stripes) {
2580 if (!test_bit(Faulty, &rdev->flags)) {
2581 pr_warn("md/raid:%s: %d read_errors > %d stripes\n",
2582 mdname(conf->mddev),
2583 atomic_read(&rdev->read_errors),
2584 conf->max_nr_stripes);
2585 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
2586 mdname(conf->mddev), bdn);
2587 }
2588 } else
NeilBrownba22dcb2005-11-08 21:39:31 -08002589 retry = 1;
Bian Yuedfa1f62013-11-14 15:16:17 +11002590 if (set_bad && test_bit(In_sync, &rdev->flags)
2591 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2592 retry = 1;
NeilBrownba22dcb2005-11-08 21:39:31 -08002593 if (retry)
Xiao Ni143f6e72019-07-08 10:14:32 +08002594 if (sh->qd_idx >= 0 && sh->pd_idx == i)
2595 set_bit(R5_ReadError, &sh->dev[i].flags);
2596 else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
majianpeng3f9e7c12012-07-31 10:04:21 +10002597 set_bit(R5_ReadError, &sh->dev[i].flags);
2598 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2599 } else
2600 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002601 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002602 clear_bit(R5_ReadError, &sh->dev[i].flags);
2603 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002604 if (!(set_bad
2605 && test_bit(In_sync, &rdev->flags)
2606 && rdev_set_badblocks(
Yufen Yuc911c462020-07-18 05:29:07 -04002607 rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), 0)))
majianpeng2e8ac3032012-07-03 15:57:02 +10002608 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 }
NeilBrown14a75d32011-12-23 10:17:52 +11002611 rdev_dec_pending(rdev, conf->mddev);
Shaohua Lic9445552016-09-08 10:43:58 -07002612 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2614 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002615 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616}
2617
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002618static void raid5_end_write_request(struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
NeilBrown99c0fb52009-03-31 14:39:38 +11002620 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002621 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002622 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11002623 struct md_rdev *uninitialized_var(rdev);
NeilBrownb84db562011-07-28 11:39:23 +10002624 sector_t first_bad;
2625 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002626 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
NeilBrown977df362011-12-23 10:17:53 +11002628 for (i = 0 ; i < disks; i++) {
2629 if (bi == &sh->dev[i].req) {
2630 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 break;
NeilBrown977df362011-12-23 10:17:53 +11002632 }
2633 if (bi == &sh->dev[i].rreq) {
2634 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002635 if (rdev)
2636 replacement = 1;
2637 else
2638 /* rdev was removed and 'replacement'
2639 * replaced it. rdev is not removed
2640 * until all requests are finished.
2641 */
2642 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002643 break;
2644 }
2645 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002646 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002648 bi->bi_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002650 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002652 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
2654
NeilBrown977df362011-12-23 10:17:53 +11002655 if (replacement) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002656 if (bi->bi_status)
NeilBrown977df362011-12-23 10:17:53 +11002657 md_error(conf->mddev, rdev);
2658 else if (is_badblock(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04002659 RAID5_STRIPE_SECTORS(conf),
NeilBrown977df362011-12-23 10:17:53 +11002660 &first_bad, &bad_sectors))
2661 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2662 } else {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002663 if (bi->bi_status) {
NeilBrown9f97e4b2014-01-16 09:35:38 +11002664 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11002665 set_bit(WriteErrorSeen, &rdev->flags);
2666 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002667 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2668 set_bit(MD_RECOVERY_NEEDED,
2669 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002670 } else if (is_badblock(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04002671 RAID5_STRIPE_SECTORS(conf),
NeilBrownc0b32972013-04-24 11:42:42 +10002672 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002673 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002674 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2675 /* That was a successful write so make
2676 * sure it looks like we already did
2677 * a re-write.
2678 */
2679 set_bit(R5_ReWrite, &sh->dev[i].flags);
2680 }
NeilBrown977df362011-12-23 10:17:53 +11002681 }
2682 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002684 if (sh->batch_head && bi->bi_status && !replacement)
shli@kernel.org72ac7332014-12-15 12:57:03 +11002685 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2686
Shaohua Lic9445552016-09-08 10:43:58 -07002687 bio_reset(bi);
NeilBrown977df362011-12-23 10:17:53 +11002688 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2689 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002691 raid5_release_stripe(sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002692
2693 if (sh->batch_head && sh != sh->batch_head)
Shaohua Li6d036f72015-08-13 14:31:57 -07002694 raid5_release_stripe(sh->batch_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
2696
Shaohua Li849674e2016-01-20 13:52:20 -08002697static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
2699 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002700 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002701 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002702 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
NeilBrown908f4fb2011-12-23 10:17:50 +11002704 spin_lock_irqsave(&conf->device_lock, flags);
Mariusz Tkaczykfb73b352018-09-04 15:08:30 +02002705
2706 if (test_bit(In_sync, &rdev->flags) &&
2707 mddev->degraded == conf->max_degraded) {
2708 /*
2709 * Don't allow to achieve failed state
2710 * Don't try to recover this device
2711 */
2712 conf->recovery_disabled = mddev->recovery_disabled;
2713 spin_unlock_irqrestore(&conf->device_lock, flags);
2714 return;
2715 }
2716
bingjingcaff69d82017-11-17 10:57:44 +08002717 set_bit(Faulty, &rdev->flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11002718 clear_bit(In_sync, &rdev->flags);
Song Liu2e38a372017-01-24 10:45:30 -08002719 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11002720 spin_unlock_irqrestore(&conf->device_lock, flags);
2721 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2722
NeilBrownde393cd2011-07-28 11:31:48 +10002723 set_bit(Blocked, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08002724 set_mask_bits(&mddev->sb_flags, 0,
2725 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrowncc6167b2016-11-02 14:16:50 +11002726 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
2727 "md/raid:%s: Operation continuing on %d devices.\n",
2728 mdname(mddev),
2729 bdevname(rdev->bdev, b),
2730 mdname(mddev),
2731 conf->raid_disks - mddev->degraded);
Song Liu70d466f2017-05-11 15:28:28 -07002732 r5c_update_on_rdev_error(mddev, rdev);
NeilBrown16a53ec2006-06-26 00:27:38 -07002733}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
2735/*
2736 * Input: a 'big' sector number,
2737 * Output: index of the data and parity disk, and the sector # in them.
2738 */
Shaohua Li6d036f72015-08-13 14:31:57 -07002739sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2740 int previous, int *dd_idx,
2741 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002743 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002744 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002746 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002747 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002749 int algorithm = previous ? conf->prev_algo
2750 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002751 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2752 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002753 int raid_disks = previous ? conf->previous_raid_disks
2754 : conf->raid_disks;
2755 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 /* First compute the information on this sector */
2758
2759 /*
2760 * Compute the chunk number and the sector offset inside the chunk
2761 */
2762 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2763 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
2765 /*
2766 * Compute the stripe number
2767 */
NeilBrown35f2a592010-04-20 14:13:34 +10002768 stripe = chunk_number;
2769 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002770 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 /*
2772 * Select the parity disk based on the user selected algorithm.
2773 */
NeilBrown84789552011-07-27 11:00:36 +10002774 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002775 switch(conf->level) {
2776 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002777 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002778 break;
2779 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002780 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002782 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002783 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 (*dd_idx)++;
2785 break;
2786 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002787 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002788 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 (*dd_idx)++;
2790 break;
2791 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002792 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002793 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 break;
2795 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002796 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002797 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002799 case ALGORITHM_PARITY_0:
2800 pd_idx = 0;
2801 (*dd_idx)++;
2802 break;
2803 case ALGORITHM_PARITY_N:
2804 pd_idx = data_disks;
2805 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002807 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002808 }
2809 break;
2810 case 6:
2811
NeilBrowne183eae2009-03-31 15:20:22 +11002812 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002813 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002814 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002815 qd_idx = pd_idx + 1;
2816 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002817 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002818 qd_idx = 0;
2819 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002820 (*dd_idx) += 2; /* D D P Q D */
2821 break;
2822 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002823 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002824 qd_idx = pd_idx + 1;
2825 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002826 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002827 qd_idx = 0;
2828 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002829 (*dd_idx) += 2; /* D D P Q D */
2830 break;
2831 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002832 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002833 qd_idx = (pd_idx + 1) % raid_disks;
2834 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002835 break;
2836 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002837 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002838 qd_idx = (pd_idx + 1) % raid_disks;
2839 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002840 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002841
2842 case ALGORITHM_PARITY_0:
2843 pd_idx = 0;
2844 qd_idx = 1;
2845 (*dd_idx) += 2;
2846 break;
2847 case ALGORITHM_PARITY_N:
2848 pd_idx = data_disks;
2849 qd_idx = data_disks + 1;
2850 break;
2851
2852 case ALGORITHM_ROTATING_ZERO_RESTART:
2853 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2854 * of blocks for computing Q is different.
2855 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002856 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002857 qd_idx = pd_idx + 1;
2858 if (pd_idx == raid_disks-1) {
2859 (*dd_idx)++; /* Q D D D P */
2860 qd_idx = 0;
2861 } else if (*dd_idx >= pd_idx)
2862 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002863 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002864 break;
2865
2866 case ALGORITHM_ROTATING_N_RESTART:
2867 /* Same a left_asymmetric, by first stripe is
2868 * D D D P Q rather than
2869 * Q D D D P
2870 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002871 stripe2 += 1;
2872 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002873 qd_idx = pd_idx + 1;
2874 if (pd_idx == raid_disks-1) {
2875 (*dd_idx)++; /* Q D D D P */
2876 qd_idx = 0;
2877 } else if (*dd_idx >= pd_idx)
2878 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002879 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002880 break;
2881
2882 case ALGORITHM_ROTATING_N_CONTINUE:
2883 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002884 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002885 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2886 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002887 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002888 break;
2889
2890 case ALGORITHM_LEFT_ASYMMETRIC_6:
2891 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002892 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002893 if (*dd_idx >= pd_idx)
2894 (*dd_idx)++;
2895 qd_idx = raid_disks - 1;
2896 break;
2897
2898 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002899 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002900 if (*dd_idx >= pd_idx)
2901 (*dd_idx)++;
2902 qd_idx = raid_disks - 1;
2903 break;
2904
2905 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002906 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002907 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2908 qd_idx = raid_disks - 1;
2909 break;
2910
2911 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002912 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002913 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2914 qd_idx = raid_disks - 1;
2915 break;
2916
2917 case ALGORITHM_PARITY_0_6:
2918 pd_idx = 0;
2919 (*dd_idx)++;
2920 qd_idx = raid_disks - 1;
2921 break;
2922
NeilBrown16a53ec2006-06-26 00:27:38 -07002923 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002924 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002925 }
2926 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 }
2928
NeilBrown911d4ee2009-03-31 14:39:38 +11002929 if (sh) {
2930 sh->pd_idx = pd_idx;
2931 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002932 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 /*
2935 * Finally, compute the new sector number
2936 */
2937 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2938 return new_sector;
2939}
2940
Shaohua Li6d036f72015-08-13 14:31:57 -07002941sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
NeilBrownd1688a62011-10-11 16:49:52 +11002943 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002944 int raid_disks = sh->disks;
2945 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002947 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2948 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002949 int algorithm = previous ? conf->prev_algo
2950 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 sector_t stripe;
2952 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002953 sector_t chunk_number;
2954 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002956 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
2958 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2959 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
NeilBrown16a53ec2006-06-26 00:27:38 -07002961 if (i == sh->pd_idx)
2962 return 0;
2963 switch(conf->level) {
2964 case 4: break;
2965 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002966 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 case ALGORITHM_LEFT_ASYMMETRIC:
2968 case ALGORITHM_RIGHT_ASYMMETRIC:
2969 if (i > sh->pd_idx)
2970 i--;
2971 break;
2972 case ALGORITHM_LEFT_SYMMETRIC:
2973 case ALGORITHM_RIGHT_SYMMETRIC:
2974 if (i < sh->pd_idx)
2975 i += raid_disks;
2976 i -= (sh->pd_idx + 1);
2977 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002978 case ALGORITHM_PARITY_0:
2979 i -= 1;
2980 break;
2981 case ALGORITHM_PARITY_N:
2982 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002984 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002985 }
2986 break;
2987 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002988 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002989 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002990 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002991 case ALGORITHM_LEFT_ASYMMETRIC:
2992 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002993 case ALGORITHM_ROTATING_ZERO_RESTART:
2994 case ALGORITHM_ROTATING_N_RESTART:
2995 if (sh->pd_idx == raid_disks-1)
2996 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002997 else if (i > sh->pd_idx)
2998 i -= 2; /* D D P Q D */
2999 break;
3000 case ALGORITHM_LEFT_SYMMETRIC:
3001 case ALGORITHM_RIGHT_SYMMETRIC:
3002 if (sh->pd_idx == raid_disks-1)
3003 i--; /* Q D D D P */
3004 else {
3005 /* D D P Q D */
3006 if (i < sh->pd_idx)
3007 i += raid_disks;
3008 i -= (sh->pd_idx + 2);
3009 }
3010 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11003011 case ALGORITHM_PARITY_0:
3012 i -= 2;
3013 break;
3014 case ALGORITHM_PARITY_N:
3015 break;
3016 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11003017 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11003018 if (sh->pd_idx == 0)
3019 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11003020 else {
3021 /* D D Q P D */
3022 if (i < sh->pd_idx)
3023 i += raid_disks;
3024 i -= (sh->pd_idx + 1);
3025 }
NeilBrown99c0fb52009-03-31 14:39:38 +11003026 break;
3027 case ALGORITHM_LEFT_ASYMMETRIC_6:
3028 case ALGORITHM_RIGHT_ASYMMETRIC_6:
3029 if (i > sh->pd_idx)
3030 i--;
3031 break;
3032 case ALGORITHM_LEFT_SYMMETRIC_6:
3033 case ALGORITHM_RIGHT_SYMMETRIC_6:
3034 if (i < sh->pd_idx)
3035 i += data_disks + 1;
3036 i -= (sh->pd_idx + 1);
3037 break;
3038 case ALGORITHM_PARITY_0_6:
3039 i -= 1;
3040 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07003041 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11003042 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07003043 }
3044 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 }
3046
3047 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10003048 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
NeilBrown112bf892009-03-31 14:39:38 +11003050 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11003051 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11003052 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
3053 || sh2.qd_idx != sh->qd_idx) {
NeilBrowncc6167b2016-11-02 14:16:50 +11003054 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
3055 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return 0;
3057 }
3058 return r_sector;
3059}
3060
Song Liu07e83362017-01-23 17:12:58 -08003061/*
3062 * There are cases where we want handle_stripe_dirtying() and
3063 * schedule_reconstruction() to delay towrite to some dev of a stripe.
3064 *
3065 * This function checks whether we want to delay the towrite. Specifically,
3066 * we delay the towrite when:
3067 *
3068 * 1. degraded stripe has a non-overwrite to the missing dev, AND this
3069 * stripe has data in journal (for other devices).
3070 *
3071 * In this case, when reading data for the non-overwrite dev, it is
3072 * necessary to handle complex rmw of write back cache (prexor with
3073 * orig_page, and xor with page). To keep read path simple, we would
3074 * like to flush data in journal to RAID disks first, so complex rmw
3075 * is handled in the write patch (handle_stripe_dirtying).
3076 *
Song Liu39b99582017-01-24 14:08:23 -08003077 * 2. when journal space is critical (R5C_LOG_CRITICAL=1)
3078 *
3079 * It is important to be able to flush all stripes in raid5-cache.
3080 * Therefore, we need reserve some space on the journal device for
3081 * these flushes. If flush operation includes pending writes to the
3082 * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe
3083 * for the flush out. If we exclude these pending writes from flush
3084 * operation, we only need (conf->max_degraded + 1) pages per stripe.
3085 * Therefore, excluding pending writes in these cases enables more
3086 * efficient use of the journal device.
3087 *
3088 * Note: To make sure the stripe makes progress, we only delay
3089 * towrite for stripes with data already in journal (injournal > 0).
3090 * When LOG_CRITICAL, stripes with injournal == 0 will be sent to
3091 * no_space_stripes list.
3092 *
Song Liu70d466f2017-05-11 15:28:28 -07003093 * 3. during journal failure
3094 * In journal failure, we try to flush all cached data to raid disks
3095 * based on data in stripe cache. The array is read-only to upper
3096 * layers, so we would skip all pending writes.
3097 *
Song Liu07e83362017-01-23 17:12:58 -08003098 */
Song Liu39b99582017-01-24 14:08:23 -08003099static inline bool delay_towrite(struct r5conf *conf,
3100 struct r5dev *dev,
3101 struct stripe_head_state *s)
Song Liu07e83362017-01-23 17:12:58 -08003102{
Song Liu39b99582017-01-24 14:08:23 -08003103 /* case 1 above */
3104 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3105 !test_bit(R5_Insync, &dev->flags) && s->injournal)
3106 return true;
3107 /* case 2 above */
3108 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
3109 s->injournal > 0)
3110 return true;
Song Liu70d466f2017-05-11 15:28:28 -07003111 /* case 3 above */
3112 if (s->log_failed && s->injournal)
3113 return true;
Song Liu39b99582017-01-24 14:08:23 -08003114 return false;
Song Liu07e83362017-01-23 17:12:58 -08003115}
3116
Dan Williams600aa102008-06-28 08:32:05 +10003117static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003118schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10003119 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07003120{
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003121 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11003122 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003123 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07003124
Dan Williamse33129d2007-01-02 13:52:30 -07003125 if (rcw) {
Song Liu1e6d6902016-11-17 15:24:39 -08003126 /*
3127 * In some cases, handle_stripe_dirtying initially decided to
3128 * run rmw and allocates extra page for prexor. However, rcw is
3129 * cheaper later on. We need to free the extra page now,
3130 * because we won't be able to do that in ops_complete_prexor().
3131 */
3132 r5c_release_extra_page(sh);
Dan Williamse33129d2007-01-02 13:52:30 -07003133
3134 for (i = disks; i--; ) {
3135 struct r5dev *dev = &sh->dev[i];
3136
Song Liu39b99582017-01-24 14:08:23 -08003137 if (dev->towrite && !delay_towrite(conf, dev, s)) {
Dan Williamse33129d2007-01-02 13:52:30 -07003138 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10003139 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003140 if (!expand)
3141 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003142 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003143 } else if (test_bit(R5_InJournal, &dev->flags)) {
3144 set_bit(R5_LOCKED, &dev->flags);
3145 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003146 }
3147 }
NeilBrownce7d3632013-03-04 12:37:14 +11003148 /* if we are not expanding this is a proper write request, and
3149 * there will be bios with new data to be drained into the
3150 * stripe cache
3151 */
3152 if (!expand) {
3153 if (!s->locked)
3154 /* False alarm, nothing to do */
3155 return;
3156 sh->reconstruct_state = reconstruct_state_drain_run;
3157 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3158 } else
3159 sh->reconstruct_state = reconstruct_state_run;
3160
3161 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3162
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003163 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003164 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003165 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07003166 } else {
3167 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
3168 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003169 BUG_ON(level == 6 &&
3170 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
3171 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
Dan Williamse33129d2007-01-02 13:52:30 -07003172
Dan Williamse33129d2007-01-02 13:52:30 -07003173 for (i = disks; i--; ) {
3174 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003175 if (i == pd_idx || i == qd_idx)
Dan Williamse33129d2007-01-02 13:52:30 -07003176 continue;
3177
Dan Williamse33129d2007-01-02 13:52:30 -07003178 if (dev->towrite &&
3179 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10003180 test_bit(R5_Wantcompute, &dev->flags))) {
3181 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003182 set_bit(R5_LOCKED, &dev->flags);
3183 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003184 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003185 } else if (test_bit(R5_InJournal, &dev->flags)) {
3186 set_bit(R5_LOCKED, &dev->flags);
3187 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003188 }
3189 }
NeilBrownce7d3632013-03-04 12:37:14 +11003190 if (!s->locked)
3191 /* False alarm - nothing to do */
3192 return;
3193 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
3194 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
3195 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3196 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003197 }
3198
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003199 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07003200 * are in flight
3201 */
3202 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
3203 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10003204 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003205
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003206 if (level == 6) {
3207 int qd_idx = sh->qd_idx;
3208 struct r5dev *dev = &sh->dev[qd_idx];
3209
3210 set_bit(R5_LOCKED, &dev->flags);
3211 clear_bit(R5_UPTODATE, &dev->flags);
3212 s->locked++;
3213 }
3214
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02003215 if (raid5_has_ppl(sh->raid_conf) && sh->ppl_page &&
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01003216 test_bit(STRIPE_OP_BIODRAIN, &s->ops_request) &&
3217 !test_bit(STRIPE_FULL_WRITE, &sh->state) &&
3218 test_bit(R5_Insync, &sh->dev[pd_idx].flags))
3219 set_bit(STRIPE_OP_PARTIAL_PARITY, &s->ops_request);
3220
Dan Williams600aa102008-06-28 08:32:05 +10003221 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07003222 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10003223 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003224}
NeilBrown16a53ec2006-06-26 00:27:38 -07003225
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226/*
3227 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07003228 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 * The bi_next chain must be in order.
3230 */
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003231static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
3232 int forwrite, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233{
3234 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11003235 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07003236 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
NeilBrowncbe47ec2011-07-26 11:20:35 +10003238 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003239 (unsigned long long)bi->bi_iter.bi_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 (unsigned long long)sh->sector);
3241
Shaohua Lib17459c2012-07-19 16:01:31 +10003242 spin_lock_irq(&sh->stripe_lock);
Mariusz Dabrowski2cd259a2018-04-19 19:28:10 +02003243 sh->dev[dd_idx].write_hint = bi->bi_write_hint;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003244 /* Don't allow new IO added to stripes in batch list */
3245 if (sh->batch_head)
3246 goto overlap;
NeilBrown72626682005-09-09 16:23:54 -07003247 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003249 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07003250 firstwrite = 1;
3251 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 bip = &sh->dev[dd_idx].toread;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003253 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
3254 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 goto overlap;
3256 bip = & (*bip)->bi_next;
3257 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07003258 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 goto overlap;
3260
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01003261 if (forwrite && raid5_has_ppl(conf)) {
3262 /*
3263 * With PPL only writes to consecutive data chunks within a
3264 * stripe are allowed because for a single stripe_head we can
3265 * only have one PPL entry at a time, which describes one data
3266 * range. Not really an overlap, but wait_for_overlap can be
3267 * used to handle this.
3268 */
3269 sector_t sector;
3270 sector_t first = 0;
3271 sector_t last = 0;
3272 int count = 0;
3273 int i;
3274
3275 for (i = 0; i < sh->disks; i++) {
3276 if (i != sh->pd_idx &&
3277 (i == dd_idx || sh->dev[i].towrite)) {
3278 sector = sh->dev[i].sector;
3279 if (count == 0 || sector < first)
3280 first = sector;
3281 if (sector > last)
3282 last = sector;
3283 count++;
3284 }
3285 }
3286
3287 if (first + conf->chunk_sectors * (count - 1) != last)
3288 goto overlap;
3289 }
3290
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003291 if (!forwrite || previous)
3292 clear_bit(STRIPE_BATCH_READY, &sh->state);
3293
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02003294 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 if (*bip)
3296 bi->bi_next = *bip;
3297 *bip = bi;
NeilBrown016c76a2017-03-15 14:05:13 +11003298 bio_inc_remaining(bi);
NeilBrown49728052017-03-15 14:05:12 +11003299 md_write_inc(conf->mddev, bi);
NeilBrown72626682005-09-09 16:23:54 -07003300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 if (forwrite) {
3302 /* check if page is covered */
3303 sector_t sector = sh->dev[dd_idx].sector;
3304 for (bi=sh->dev[dd_idx].towrite;
Yufen Yuc911c462020-07-18 05:29:07 -04003305 sector < sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07003306 bi && bi->bi_iter.bi_sector <= sector;
Yufen Yuc911c462020-07-18 05:29:07 -04003307 bi = r5_next_bio(conf, bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07003308 if (bio_end_sector(bi) >= sector)
3309 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 }
Yufen Yuc911c462020-07-18 05:29:07 -04003311 if (sector >= sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf))
shli@kernel.org7a87f432014-12-15 12:57:03 +11003312 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
3313 sh->overwrite_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003315
3316 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003317 (unsigned long long)(*bip)->bi_iter.bi_sector,
NeilBrowncbe47ec2011-07-26 11:20:35 +10003318 (unsigned long long)sh->sector, dd_idx);
3319
3320 if (conf->mddev->bitmap && firstwrite) {
NeilBrownd0852df52015-05-27 08:43:45 +10003321 /* Cannot hold spinlock over bitmap_startwrite,
3322 * but must ensure this isn't added to a batch until
3323 * we have added to the bitmap and set bm_seq.
3324 * So set STRIPE_BITMAP_PENDING to prevent
3325 * batching.
3326 * If multiple add_stripe_bio() calls race here they
3327 * much all set STRIPE_BITMAP_PENDING. So only the first one
3328 * to complete "bitmap_startwrite" gets to set
3329 * STRIPE_BIT_DELAY. This is important as once a stripe
3330 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3331 * any more.
3332 */
3333 set_bit(STRIPE_BITMAP_PENDING, &sh->state);
3334 spin_unlock_irq(&sh->stripe_lock);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003335 md_bitmap_startwrite(conf->mddev->bitmap, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003336 RAID5_STRIPE_SECTORS(conf), 0);
NeilBrownd0852df52015-05-27 08:43:45 +10003337 spin_lock_irq(&sh->stripe_lock);
3338 clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
3339 if (!sh->batch_head) {
3340 sh->bm_seq = conf->seq_flush+1;
3341 set_bit(STRIPE_BIT_DELAY, &sh->state);
3342 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003343 }
NeilBrownd0852df52015-05-27 08:43:45 +10003344 spin_unlock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003345
3346 if (stripe_can_batch(sh))
3347 stripe_add_to_batch_list(conf, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 return 1;
3349
3350 overlap:
3351 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10003352 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 return 0;
3354}
3355
NeilBrownd1688a62011-10-11 16:49:52 +11003356static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08003357
NeilBrownd1688a62011-10-11 16:49:52 +11003358static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003359 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08003360{
NeilBrown784052e2009-03-31 15:19:07 +11003361 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10003362 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11003363 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003364 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11003365 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003366
NeilBrown112bf892009-03-31 14:39:38 +11003367 raid5_compute_sector(conf,
3368 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08003369 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11003370 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003371 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003372}
3373
Dan Williamsa4456852007-07-09 11:56:43 -07003374static void
NeilBrownd1688a62011-10-11 16:49:52 +11003375handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
NeilBrownbd83d0a2017-03-15 14:05:12 +11003376 struct stripe_head_state *s, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003377{
3378 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003379 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003380 for (i = disks; i--; ) {
3381 struct bio *bi;
3382 int bitmap_end = 0;
3383
3384 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003385 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07003386 rcu_read_lock();
3387 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownf5b67ae2016-06-02 16:19:53 +10003388 if (rdev && test_bit(In_sync, &rdev->flags) &&
3389 !test_bit(Faulty, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10003390 atomic_inc(&rdev->nr_pending);
3391 else
3392 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003393 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10003394 if (rdev) {
3395 if (!rdev_set_badblocks(
3396 rdev,
3397 sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003398 RAID5_STRIPE_SECTORS(conf), 0))
NeilBrown7f0da592011-07-28 11:39:22 +10003399 md_error(conf->mddev, rdev);
3400 rdev_dec_pending(rdev, conf->mddev);
3401 }
Dan Williamsa4456852007-07-09 11:56:43 -07003402 }
Shaohua Lib17459c2012-07-19 16:01:31 +10003403 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003404 /* fail all writes first */
3405 bi = sh->dev[i].towrite;
3406 sh->dev[i].towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11003407 sh->overwrite_disks = 0;
Shaohua Lib17459c2012-07-19 16:01:31 +10003408 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11003409 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003410 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07003411
Artur Paszkiewiczff875732017-03-09 09:59:58 +01003412 log_stripe_write_finished(sh);
Shaohua Li0576b1c2015-08-13 14:32:00 -07003413
Dan Williamsa4456852007-07-09 11:56:43 -07003414 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3415 wake_up(&conf->wait_for_overlap);
3416
Kent Overstreet4f024f32013-10-11 15:44:27 -07003417 while (bi && bi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04003418 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3419 struct bio *nextbi = r5_next_bio(conf, bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003420
NeilBrown49728052017-03-15 14:05:12 +11003421 md_write_end(conf->mddev);
Guoqing Jiang6308d8e2017-07-21 16:33:44 +08003422 bio_io_error(bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003423 bi = nextbi;
3424 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003425 if (bitmap_end)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003426 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003427 RAID5_STRIPE_SECTORS(conf), 0, 0);
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003428 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003429 /* and fail all 'written' */
3430 bi = sh->dev[i].written;
3431 sh->dev[i].written = NULL;
Shaohua Lid592a992014-05-21 17:57:44 +08003432 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3433 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3434 sh->dev[i].page = sh->dev[i].orig_page;
3435 }
3436
Dan Williamsa4456852007-07-09 11:56:43 -07003437 if (bi) bitmap_end = 1;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003438 while (bi && bi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04003439 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3440 struct bio *bi2 = r5_next_bio(conf, bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003441
NeilBrown49728052017-03-15 14:05:12 +11003442 md_write_end(conf->mddev);
Guoqing Jiang6308d8e2017-07-21 16:33:44 +08003443 bio_io_error(bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003444 bi = bi2;
3445 }
3446
Dan Williamsb5e98d62007-01-02 13:52:31 -07003447 /* fail any reads if this device is non-operational and
3448 * the data has not reached the cache yet.
3449 */
3450 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
Shaohua Li6e74a9c2015-10-08 21:54:08 -07003451 s->failed > conf->max_degraded &&
Dan Williamsb5e98d62007-01-02 13:52:31 -07003452 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3453 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11003454 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003455 bi = sh->dev[i].toread;
3456 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11003457 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003458 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3459 wake_up(&conf->wait_for_overlap);
Shaohua Liebda7802015-09-18 10:20:13 -07003460 if (bi)
3461 s->to_read--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003462 while (bi && bi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04003463 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003464 struct bio *nextbi =
Yufen Yuc911c462020-07-18 05:29:07 -04003465 r5_next_bio(conf, bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003466
Guoqing Jiang6308d8e2017-07-21 16:33:44 +08003467 bio_io_error(bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003468 bi = nextbi;
3469 }
3470 }
Dan Williamsa4456852007-07-09 11:56:43 -07003471 if (bitmap_end)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003472 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003473 RAID5_STRIPE_SECTORS(conf), 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10003474 /* If we were in the middle of a write the parity block might
3475 * still be locked - so just clear all R5_LOCKED flags
3476 */
3477 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003478 }
Shaohua Liebda7802015-09-18 10:20:13 -07003479 s->to_write = 0;
3480 s->written = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003481
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003482 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3483 if (atomic_dec_and_test(&conf->pending_full_writes))
3484 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003485}
3486
NeilBrown7f0da592011-07-28 11:39:22 +10003487static void
NeilBrownd1688a62011-10-11 16:49:52 +11003488handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10003489 struct stripe_head_state *s)
3490{
3491 int abort = 0;
3492 int i;
3493
shli@kernel.org59fc6302014-12-15 12:57:03 +11003494 BUG_ON(sh->batch_head);
NeilBrown7f0da592011-07-28 11:39:22 +10003495 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003496 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3497 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10003498 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11003499 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10003500 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10003501 * Don't even need to abort as that is handled elsewhere
3502 * if needed, and not always wanted e.g. if there is a known
3503 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11003504 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10003505 * non-sync devices, or abort the recovery
3506 */
NeilBrown18b98372012-04-01 23:48:38 +10003507 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3508 /* During recovery devices cannot be removed, so
3509 * locking and refcounting of rdevs is not needed
3510 */
NeilBrowne50d3992016-06-02 16:19:52 +10003511 rcu_read_lock();
NeilBrown18b98372012-04-01 23:48:38 +10003512 for (i = 0; i < conf->raid_disks; i++) {
NeilBrowne50d3992016-06-02 16:19:52 +10003513 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown18b98372012-04-01 23:48:38 +10003514 if (rdev
3515 && !test_bit(Faulty, &rdev->flags)
3516 && !test_bit(In_sync, &rdev->flags)
3517 && !rdev_set_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003518 RAID5_STRIPE_SECTORS(conf), 0))
NeilBrown18b98372012-04-01 23:48:38 +10003519 abort = 1;
NeilBrowne50d3992016-06-02 16:19:52 +10003520 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown18b98372012-04-01 23:48:38 +10003521 if (rdev
3522 && !test_bit(Faulty, &rdev->flags)
3523 && !test_bit(In_sync, &rdev->flags)
3524 && !rdev_set_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003525 RAID5_STRIPE_SECTORS(conf), 0))
NeilBrown18b98372012-04-01 23:48:38 +10003526 abort = 1;
3527 }
NeilBrowne50d3992016-06-02 16:19:52 +10003528 rcu_read_unlock();
NeilBrown18b98372012-04-01 23:48:38 +10003529 if (abort)
3530 conf->recovery_disabled =
3531 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10003532 }
Yufen Yuc911c462020-07-18 05:29:07 -04003533 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10003534}
3535
NeilBrown9a3e1102011-12-23 10:17:53 +11003536static int want_replace(struct stripe_head *sh, int disk_idx)
3537{
3538 struct md_rdev *rdev;
3539 int rv = 0;
NeilBrown3f232d62016-06-02 16:19:52 +10003540
3541 rcu_read_lock();
3542 rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
NeilBrown9a3e1102011-12-23 10:17:53 +11003543 if (rdev
3544 && !test_bit(Faulty, &rdev->flags)
3545 && !test_bit(In_sync, &rdev->flags)
3546 && (rdev->recovery_offset <= sh->sector
3547 || rdev->mddev->recovery_cp <= sh->sector))
3548 rv = 1;
NeilBrown3f232d62016-06-02 16:19:52 +10003549 rcu_read_unlock();
NeilBrown9a3e1102011-12-23 10:17:53 +11003550 return rv;
3551}
3552
NeilBrown2c58f062015-02-02 11:32:23 +11003553static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3554 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07003555{
3556 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10003557 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3558 &sh->dev[s->failed_num[1]] };
NeilBrownea664c82015-02-02 14:03:28 +11003559 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07003560
NeilBrowna79cfe12015-02-02 11:37:59 +11003561
3562 if (test_bit(R5_LOCKED, &dev->flags) ||
3563 test_bit(R5_UPTODATE, &dev->flags))
3564 /* No point reading this as we already have it or have
3565 * decided to get it.
3566 */
3567 return 0;
3568
3569 if (dev->toread ||
3570 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3571 /* We need this block to directly satisfy a request */
3572 return 1;
3573
3574 if (s->syncing || s->expanding ||
3575 (s->replacing && want_replace(sh, disk_idx)))
3576 /* When syncing, or expanding we read everything.
3577 * When replacing, we need the replaced block.
3578 */
3579 return 1;
3580
3581 if ((s->failed >= 1 && fdev[0]->toread) ||
3582 (s->failed >= 2 && fdev[1]->toread))
3583 /* If we want to read from a failed device, then
3584 * we need to actually read every other device.
3585 */
3586 return 1;
3587
NeilBrowna9d56952015-02-02 11:49:10 +11003588 /* Sometimes neither read-modify-write nor reconstruct-write
3589 * cycles can work. In those cases we read every block we
3590 * can. Then the parity-update is certain to have enough to
3591 * work with.
3592 * This can only be a problem when we need to write something,
3593 * and some device has failed. If either of those tests
3594 * fail we need look no further.
3595 */
3596 if (!s->failed || !s->to_write)
3597 return 0;
3598
3599 if (test_bit(R5_Insync, &dev->flags) &&
3600 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3601 /* Pre-reads at not permitted until after short delay
3602 * to gather multiple requests. However if this
Zhilong Liu3560741e2017-03-15 16:14:53 +08003603 * device is no Insync, the block could only be computed
NeilBrowna9d56952015-02-02 11:49:10 +11003604 * and there is no need to delay that.
3605 */
3606 return 0;
NeilBrownea664c82015-02-02 14:03:28 +11003607
NeilBrown36707bb2015-09-24 15:25:36 +10003608 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrownea664c82015-02-02 14:03:28 +11003609 if (fdev[i]->towrite &&
3610 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3611 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3612 /* If we have a partial write to a failed
3613 * device, then we will need to reconstruct
3614 * the content of that device, so all other
3615 * devices must be read.
3616 */
3617 return 1;
3618 }
3619
3620 /* If we are forced to do a reconstruct-write, either because
3621 * the current RAID6 implementation only supports that, or
Zhilong Liu3560741e2017-03-15 16:14:53 +08003622 * because parity cannot be trusted and we are currently
NeilBrownea664c82015-02-02 14:03:28 +11003623 * recovering it, there is extra need to be careful.
3624 * If one of the devices that we would need to read, because
3625 * it is not being overwritten (and maybe not written at all)
3626 * is missing/faulty, then we need to read everything we can.
3627 */
3628 if (sh->raid_conf->level != 6 &&
3629 sh->sector < sh->raid_conf->mddev->recovery_cp)
3630 /* reconstruct-write isn't being forced */
3631 return 0;
NeilBrown36707bb2015-09-24 15:25:36 +10003632 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrown10d82c52015-05-08 18:19:33 +10003633 if (s->failed_num[i] != sh->pd_idx &&
3634 s->failed_num[i] != sh->qd_idx &&
3635 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
NeilBrownea664c82015-02-02 14:03:28 +11003636 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3637 return 1;
3638 }
3639
NeilBrown2c58f062015-02-02 11:32:23 +11003640 return 0;
3641}
3642
Song Liuba026842017-01-12 17:22:42 -08003643/* fetch_block - checks the given member device to see if its data needs
3644 * to be read or computed to satisfy a request.
3645 *
3646 * Returns 1 when no more member devices need to be checked, otherwise returns
3647 * 0 to tell the loop in handle_stripe_fill to continue
3648 */
NeilBrown2c58f062015-02-02 11:32:23 +11003649static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3650 int disk_idx, int disks)
3651{
3652 struct r5dev *dev = &sh->dev[disk_idx];
3653
3654 /* is the data in this block needed, and can we get it? */
3655 if (need_this_block(sh, s, disk_idx, disks)) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003656 /* we would like to get this block, possibly by computing it,
3657 * otherwise read it if the backing disk is insync
3658 */
3659 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3660 BUG_ON(test_bit(R5_Wantread, &dev->flags));
NeilBrownb0c783b2015-05-08 18:19:32 +10003661 BUG_ON(sh->batch_head);
NeilBrown7471fb72017-04-03 12:11:32 +10003662
3663 /*
3664 * In the raid6 case if the only non-uptodate disk is P
3665 * then we already trusted P to compute the other failed
3666 * drives. It is safe to compute rather than re-read P.
3667 * In other cases we only compute blocks from failed
3668 * devices, otherwise check/repair might fail to detect
3669 * a real inconsistency.
3670 */
3671
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003672 if ((s->uptodate == disks - 1) &&
NeilBrown7471fb72017-04-03 12:11:32 +10003673 ((sh->qd_idx >= 0 && sh->pd_idx == disk_idx) ||
NeilBrownf2b3b442011-07-26 11:35:19 +10003674 (s->failed && (disk_idx == s->failed_num[0] ||
NeilBrown7471fb72017-04-03 12:11:32 +10003675 disk_idx == s->failed_num[1])))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003676 /* have disk failed, and we're requested to fetch it;
3677 * do compute it
3678 */
3679 pr_debug("Computing stripe %llu block %d\n",
3680 (unsigned long long)sh->sector, disk_idx);
3681 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3682 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3683 set_bit(R5_Wantcompute, &dev->flags);
3684 sh->ops.target = disk_idx;
3685 sh->ops.target2 = -1; /* no 2nd target */
3686 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10003687 /* Careful: from this point on 'uptodate' is in the eye
3688 * of raid_run_ops which services 'compute' operations
3689 * before writes. R5_Wantcompute flags a block that will
3690 * be R5_UPTODATE by the time it is needed for a
3691 * subsequent operation.
3692 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003693 s->uptodate++;
3694 return 1;
3695 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3696 /* Computing 2-failure is *very* expensive; only
3697 * do it if failed >= 2
3698 */
3699 int other;
3700 for (other = disks; other--; ) {
3701 if (other == disk_idx)
3702 continue;
3703 if (!test_bit(R5_UPTODATE,
3704 &sh->dev[other].flags))
3705 break;
3706 }
3707 BUG_ON(other < 0);
3708 pr_debug("Computing stripe %llu blocks %d,%d\n",
3709 (unsigned long long)sh->sector,
3710 disk_idx, other);
3711 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3712 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3713 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3714 set_bit(R5_Wantcompute, &sh->dev[other].flags);
3715 sh->ops.target = disk_idx;
3716 sh->ops.target2 = other;
3717 s->uptodate += 2;
3718 s->req_compute = 1;
3719 return 1;
3720 } else if (test_bit(R5_Insync, &dev->flags)) {
3721 set_bit(R5_LOCKED, &dev->flags);
3722 set_bit(R5_Wantread, &dev->flags);
3723 s->locked++;
3724 pr_debug("Reading block %d (sync=%d)\n",
3725 disk_idx, s->syncing);
3726 }
3727 }
3728
3729 return 0;
3730}
3731
Damien Le Moal2aada5b2020-07-16 13:54:42 +09003732/*
NeilBrown93b3dbc2011-07-27 11:00:36 +10003733 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003734 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10003735static void handle_stripe_fill(struct stripe_head *sh,
3736 struct stripe_head_state *s,
3737 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003738{
3739 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003740
3741 /* look for blocks to read/compute, skip this if a compute
3742 * is already in flight, or if the stripe contents are in the
3743 * midst of changing due to a write
3744 */
3745 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Song Liu07e83362017-01-23 17:12:58 -08003746 !sh->reconstruct_state) {
3747
3748 /*
3749 * For degraded stripe with data in journal, do not handle
3750 * read requests yet, instead, flush the stripe to raid
3751 * disks first, this avoids handling complex rmw of write
3752 * back cache (prexor with orig_page, and then xor with
3753 * page) in the read path
3754 */
3755 if (s->injournal && s->failed) {
3756 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
3757 r5c_make_stripe_write_out(sh);
3758 goto out;
3759 }
3760
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003761 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10003762 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003763 break;
Song Liu07e83362017-01-23 17:12:58 -08003764 }
3765out:
Dan Williamsa4456852007-07-09 11:56:43 -07003766 set_bit(STRIPE_HANDLE, &sh->state);
3767}
3768
NeilBrown787b76f2015-05-21 12:56:41 +10003769static void break_stripe_batch_list(struct stripe_head *head_sh,
3770 unsigned long handle_flags);
Dan Williams1fe797e2008-06-28 09:16:30 +10003771/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07003772 * any written block on an uptodate or failed drive can be returned.
3773 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3774 * never LOCKED, so we don't need to test 'failed' directly.
3775 */
NeilBrownd1688a62011-10-11 16:49:52 +11003776static void handle_stripe_clean_event(struct r5conf *conf,
NeilBrownbd83d0a2017-03-15 14:05:12 +11003777 struct stripe_head *sh, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003778{
3779 int i;
3780 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003781 int discard_pending = 0;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003782 struct stripe_head *head_sh = sh;
3783 bool do_endio = false;
Dan Williamsa4456852007-07-09 11:56:43 -07003784
3785 for (i = disks; i--; )
3786 if (sh->dev[i].written) {
3787 dev = &sh->dev[i];
3788 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003789 (test_bit(R5_UPTODATE, &dev->flags) ||
Shaohua Lid592a992014-05-21 17:57:44 +08003790 test_bit(R5_Discard, &dev->flags) ||
3791 test_bit(R5_SkipCopy, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003792 /* We can return any write requests */
3793 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003794 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003795 if (test_and_clear_bit(R5_Discard, &dev->flags))
3796 clear_bit(R5_UPTODATE, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08003797 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3798 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
Shaohua Lid592a992014-05-21 17:57:44 +08003799 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003800 do_endio = true;
3801
3802returnbi:
3803 dev->page = dev->orig_page;
Dan Williamsa4456852007-07-09 11:56:43 -07003804 wbi = dev->written;
3805 dev->written = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003806 while (wbi && wbi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04003807 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
3808 wbi2 = r5_next_bio(conf, wbi, dev->sector);
NeilBrown49728052017-03-15 14:05:12 +11003809 md_write_end(conf->mddev);
NeilBrown016c76a2017-03-15 14:05:13 +11003810 bio_endio(wbi);
Dan Williamsa4456852007-07-09 11:56:43 -07003811 wbi = wbi2;
3812 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003813 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003814 RAID5_STRIPE_SECTORS(conf),
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003815 !test_bit(STRIPE_DEGRADED, &sh->state),
3816 0);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003817 if (head_sh->batch_head) {
3818 sh = list_first_entry(&sh->batch_list,
3819 struct stripe_head,
3820 batch_list);
3821 if (sh != head_sh) {
3822 dev = &sh->dev[i];
3823 goto returnbi;
3824 }
3825 }
3826 sh = head_sh;
3827 dev = &sh->dev[i];
NeilBrownf8dfcff2013-03-12 12:18:06 +11003828 } else if (test_bit(R5_Discard, &dev->flags))
3829 discard_pending = 1;
3830 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -07003831
Artur Paszkiewiczff875732017-03-09 09:59:58 +01003832 log_stripe_write_finished(sh);
Shaohua Li0576b1c2015-08-13 14:32:00 -07003833
NeilBrownf8dfcff2013-03-12 12:18:06 +11003834 if (!discard_pending &&
3835 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003836 int hash;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003837 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3838 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3839 if (sh->qd_idx >= 0) {
3840 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3841 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3842 }
3843 /* now that discard is done we can proceed with any sync */
3844 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08003845 /*
3846 * SCSI discard will change some bio fields and the stripe has
3847 * no updated data, so remove it from hash list and the stripe
3848 * will be reinitialized
3849 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11003850unhash:
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003851 hash = sh->hash_lock_index;
3852 spin_lock_irq(conf->hash_locks + hash);
Shaohua Lid47648f2013-10-19 14:51:42 +08003853 remove_hash(sh);
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003854 spin_unlock_irq(conf->hash_locks + hash);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003855 if (head_sh->batch_head) {
3856 sh = list_first_entry(&sh->batch_list,
3857 struct stripe_head, batch_list);
3858 if (sh != head_sh)
3859 goto unhash;
3860 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003861 sh = head_sh;
3862
NeilBrownf8dfcff2013-03-12 12:18:06 +11003863 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3864 set_bit(STRIPE_HANDLE, &sh->state);
3865
3866 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003867
3868 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3869 if (atomic_dec_and_test(&conf->pending_full_writes))
3870 md_wakeup_thread(conf->mddev->thread);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003871
NeilBrown787b76f2015-05-21 12:56:41 +10003872 if (head_sh->batch_head && do_endio)
3873 break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
Dan Williamsa4456852007-07-09 11:56:43 -07003874}
3875
Song Liu86aa1392017-01-12 17:22:41 -08003876/*
3877 * For RMW in write back cache, we need extra page in prexor to store the
3878 * old data. This page is stored in dev->orig_page.
3879 *
3880 * This function checks whether we have data for prexor. The exact logic
3881 * is:
3882 * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
3883 */
3884static inline bool uptodate_for_rmw(struct r5dev *dev)
3885{
3886 return (test_bit(R5_UPTODATE, &dev->flags)) &&
3887 (!test_bit(R5_InJournal, &dev->flags) ||
3888 test_bit(R5_OrigPageUPTDODATE, &dev->flags));
3889}
3890
Song Liud7bd3982016-11-23 22:50:39 -08003891static int handle_stripe_dirtying(struct r5conf *conf,
3892 struct stripe_head *sh,
3893 struct stripe_head_state *s,
3894 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003895{
3896 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003897 sector_t recovery_cp = conf->mddev->recovery_cp;
3898
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003899 /* Check whether resync is now happening or should start.
Alexander Lyakasa7854482012-10-11 13:50:12 +11003900 * If yes, then the array is dirty (after unclean shutdown or
3901 * initial creation), so parity in some stripes might be inconsistent.
3902 * In this case, we need to always do reconstruct-write, to ensure
3903 * that in case of drive failure or read-error correction, we
3904 * generate correct data from the parity.
3905 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003906 if (conf->rmw_level == PARITY_DISABLE_RMW ||
NeilBrown26ac1072015-02-18 11:35:14 +11003907 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
3908 s->failed == 0)) {
Alexander Lyakasa7854482012-10-11 13:50:12 +11003909 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10003910 * look like rcw is cheaper
3911 */
3912 rcw = 1; rmw = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003913 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
3914 conf->rmw_level, (unsigned long long)recovery_cp,
Alexander Lyakasa7854482012-10-11 13:50:12 +11003915 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10003916 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003917 /* would I have to read this buffer for read_modify_write */
3918 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08003919 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu07e83362017-01-23 17:12:58 -08003920 i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08003921 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003922 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08003923 !(uptodate_for_rmw(dev) ||
Dan Williamsf38e1212007-01-02 13:52:30 -07003924 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003925 if (test_bit(R5_Insync, &dev->flags))
3926 rmw++;
3927 else
3928 rmw += 2*disks; /* cannot read it */
3929 }
3930 /* Would I have to read this buffer for reconstruct_write */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003931 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3932 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003933 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003934 !(test_bit(R5_UPTODATE, &dev->flags) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003935 test_bit(R5_Wantcompute, &dev->flags))) {
NeilBrown67f45542014-05-28 13:39:22 +10003936 if (test_bit(R5_Insync, &dev->flags))
3937 rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07003938 else
3939 rcw += 2*disks;
3940 }
3941 }
Song Liu1e6d6902016-11-17 15:24:39 -08003942
Song Liu39b99582017-01-24 14:08:23 -08003943 pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n",
3944 (unsigned long long)sh->sector, sh->state, rmw, rcw);
Dan Williamsa4456852007-07-09 11:56:43 -07003945 set_bit(STRIPE_HANDLE, &sh->state);
Song Liu41257582016-05-23 17:25:06 -07003946 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003947 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003948 if (conf->mddev->queue)
3949 blk_add_trace_msg(conf->mddev->queue,
3950 "raid5 rmw %llu %d",
3951 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07003952 for (i = disks; i--; ) {
3953 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08003954 if (test_bit(R5_InJournal, &dev->flags) &&
3955 dev->page == dev->orig_page &&
3956 !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) {
3957 /* alloc page for prexor */
Song Liud7bd3982016-11-23 22:50:39 -08003958 struct page *p = alloc_page(GFP_NOIO);
Song Liu1e6d6902016-11-17 15:24:39 -08003959
Song Liud7bd3982016-11-23 22:50:39 -08003960 if (p) {
3961 dev->orig_page = p;
3962 continue;
3963 }
3964
3965 /*
3966 * alloc_page() failed, try use
3967 * disk_info->extra_page
3968 */
3969 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE,
3970 &conf->cache_state)) {
3971 r5c_use_extra_page(sh);
3972 break;
3973 }
3974
3975 /* extra_page in use, add to delayed_list */
3976 set_bit(STRIPE_DELAYED, &sh->state);
3977 s->waiting_extra_page = 1;
3978 return -EAGAIN;
Song Liu1e6d6902016-11-17 15:24:39 -08003979 }
Song Liud7bd3982016-11-23 22:50:39 -08003980 }
Song Liu1e6d6902016-11-17 15:24:39 -08003981
Song Liud7bd3982016-11-23 22:50:39 -08003982 for (i = disks; i--; ) {
3983 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08003984 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003985 i == sh->pd_idx || i == sh->qd_idx ||
3986 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003987 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08003988 !(uptodate_for_rmw(dev) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003989 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003990 test_bit(R5_Insync, &dev->flags)) {
NeilBrown67f45542014-05-28 13:39:22 +10003991 if (test_bit(STRIPE_PREREAD_ACTIVE,
3992 &sh->state)) {
3993 pr_debug("Read_old block %d for r-m-w\n",
3994 i);
Dan Williamsa4456852007-07-09 11:56:43 -07003995 set_bit(R5_LOCKED, &dev->flags);
3996 set_bit(R5_Wantread, &dev->flags);
3997 s->locked++;
3998 } else {
3999 set_bit(STRIPE_DELAYED, &sh->state);
4000 set_bit(STRIPE_HANDLE, &sh->state);
4001 }
4002 }
4003 }
NeilBrowna9add5d2012-10-31 11:59:09 +11004004 }
Song Liu41257582016-05-23 17:25:06 -07004005 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07004006 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11004007 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10004008 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07004009 for (i = disks; i--; ) {
4010 struct r5dev *dev = &sh->dev[i];
4011 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10004012 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07004013 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07004014 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10004015 test_bit(R5_Wantcompute, &dev->flags))) {
4016 rcw++;
NeilBrown67f45542014-05-28 13:39:22 +10004017 if (test_bit(R5_Insync, &dev->flags) &&
4018 test_bit(STRIPE_PREREAD_ACTIVE,
4019 &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07004020 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07004021 "%d for Reconstruct\n", i);
4022 set_bit(R5_LOCKED, &dev->flags);
4023 set_bit(R5_Wantread, &dev->flags);
4024 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11004025 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07004026 } else {
4027 set_bit(STRIPE_DELAYED, &sh->state);
4028 set_bit(STRIPE_HANDLE, &sh->state);
4029 }
4030 }
4031 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004032 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11004033 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
4034 (unsigned long long)sh->sector,
4035 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10004036 }
NeilBrownb1b02fe2015-02-02 10:44:29 +11004037
4038 if (rcw > disks && rmw > disks &&
4039 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4040 set_bit(STRIPE_DELAYED, &sh->state);
4041
Dan Williamsa4456852007-07-09 11:56:43 -07004042 /* now if nothing is locked, and if we have enough data,
4043 * we can start a write request
4044 */
Dan Williamsf38e1212007-01-02 13:52:30 -07004045 /* since handle_stripe can be called at any time we need to handle the
4046 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07004047 * subsequent call wants to start a write request. raid_run_ops only
4048 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07004049 * simultaneously. If this is not the case then new writes need to be
4050 * held off until the compute completes.
4051 */
Dan Williams976ea8d2008-06-28 08:32:03 +10004052 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
4053 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
Song Liu1e6d6902016-11-17 15:24:39 -08004054 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07004055 schedule_reconstruction(sh, s, rcw == 0, 0);
Song Liud7bd3982016-11-23 22:50:39 -08004056 return 0;
Dan Williamsa4456852007-07-09 11:56:43 -07004057}
4058
NeilBrownd1688a62011-10-11 16:49:52 +11004059static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07004060 struct stripe_head_state *s, int disks)
4061{
Dan Williamsecc65c92008-06-28 08:31:57 +10004062 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07004063
shli@kernel.org59fc6302014-12-15 12:57:03 +11004064 BUG_ON(sh->batch_head);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004065 set_bit(STRIPE_HANDLE, &sh->state);
4066
Dan Williamsecc65c92008-06-28 08:31:57 +10004067 switch (sh->check_state) {
4068 case check_state_idle:
4069 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07004070 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07004071 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10004072 sh->check_state = check_state_run;
4073 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004074 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004075 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10004076 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07004077 }
NeilBrownf2b3b442011-07-26 11:35:19 +10004078 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10004079 /* fall through */
4080 case check_state_compute_result:
4081 sh->check_state = check_state_idle;
4082 if (!dev)
4083 dev = &sh->dev[sh->pd_idx];
4084
4085 /* check that a write has not made the stripe insync */
4086 if (test_bit(STRIPE_INSYNC, &sh->state))
4087 break;
4088
4089 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07004090 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
4091 BUG_ON(s->uptodate != disks);
4092
4093 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10004094 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07004095 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07004096
Dan Williamsa4456852007-07-09 11:56:43 -07004097 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07004098 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10004099 break;
4100 case check_state_run:
4101 break; /* we will be called again upon completion */
4102 case check_state_check_result:
4103 sh->check_state = check_state_idle;
4104
4105 /* if a failure occurred during the check operation, leave
4106 * STRIPE_INSYNC not set and let the stripe be handled again
4107 */
4108 if (s->failed)
4109 break;
4110
4111 /* handle a successful check operation, if parity is correct
4112 * we are done. Otherwise update the mismatch count and repair
4113 * parity if !MD_RECOVERY_CHECK
4114 */
Dan Williamsad283ea2009-08-29 19:09:26 -07004115 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10004116 /* parity is correct (on disc,
4117 * not in buffer any more)
4118 */
4119 set_bit(STRIPE_INSYNC, &sh->state);
4120 else {
Yufen Yuc911c462020-07-18 05:29:07 -04004121 atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches);
Nixe1539032017-05-16 10:13:31 +01004122 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
Dan Williamsecc65c92008-06-28 08:31:57 +10004123 /* don't try to repair!! */
4124 set_bit(STRIPE_INSYNC, &sh->state);
Nixe1539032017-05-16 10:13:31 +01004125 pr_warn_ratelimited("%s: mismatch sector in range "
4126 "%llu-%llu\n", mdname(conf->mddev),
4127 (unsigned long long) sh->sector,
4128 (unsigned long long) sh->sector +
Yufen Yuc911c462020-07-18 05:29:07 -04004129 RAID5_STRIPE_SECTORS(conf));
Nixe1539032017-05-16 10:13:31 +01004130 } else {
Dan Williamsecc65c92008-06-28 08:31:57 +10004131 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10004132 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10004133 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4134 set_bit(R5_Wantcompute,
4135 &sh->dev[sh->pd_idx].flags);
4136 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07004137 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10004138 s->uptodate++;
4139 }
4140 }
4141 break;
4142 case check_state_compute_run:
4143 break;
4144 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11004145 pr_err("%s: unknown check_state: %d sector: %llu\n",
Dan Williamsecc65c92008-06-28 08:31:57 +10004146 __func__, sh->check_state,
4147 (unsigned long long) sh->sector);
4148 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07004149 }
4150}
4151
NeilBrownd1688a62011-10-11 16:49:52 +11004152static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07004153 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10004154 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07004155{
Dan Williamsa4456852007-07-09 11:56:43 -07004156 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11004157 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07004158 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07004159
shli@kernel.org59fc6302014-12-15 12:57:03 +11004160 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07004161 set_bit(STRIPE_HANDLE, &sh->state);
4162
4163 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004164
Dan Williamsa4456852007-07-09 11:56:43 -07004165 /* Want to check and possibly repair P and Q.
4166 * However there could be one 'failed' device, in which
4167 * case we can only check one of them, possibly using the
4168 * other to generate missing data
4169 */
4170
Dan Williamsd82dfee2009-07-14 13:40:57 -07004171 switch (sh->check_state) {
4172 case check_state_idle:
4173 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10004174 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004175 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07004176 * makes sense to check P (If anything else were failed,
4177 * we would have used P to recreate it).
4178 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004179 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07004180 }
NeilBrownf2b3b442011-07-26 11:35:19 +10004181 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004182 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07004183 * anything, so it makes sense to check it
4184 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004185 if (sh->check_state == check_state_run)
4186 sh->check_state = check_state_run_pq;
4187 else
4188 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07004189 }
Dan Williams36d1c642009-07-14 11:48:22 -07004190
Dan Williamsd82dfee2009-07-14 13:40:57 -07004191 /* discard potentially stale zero_sum_result */
4192 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07004193
Dan Williamsd82dfee2009-07-14 13:40:57 -07004194 if (sh->check_state == check_state_run) {
4195 /* async_xor_zero_sum destroys the contents of P */
4196 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
4197 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07004198 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004199 if (sh->check_state >= check_state_run &&
4200 sh->check_state <= check_state_run_pq) {
4201 /* async_syndrome_zero_sum preserves P and Q, so
4202 * no need to mark them !uptodate here
4203 */
4204 set_bit(STRIPE_OP_CHECK, &s->ops_request);
4205 break;
4206 }
Dan Williams36d1c642009-07-14 11:48:22 -07004207
Dan Williamsd82dfee2009-07-14 13:40:57 -07004208 /* we have 2-disk failure */
4209 BUG_ON(s->failed != 2);
4210 /* fall through */
4211 case check_state_compute_result:
4212 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07004213
Dan Williamsd82dfee2009-07-14 13:40:57 -07004214 /* check that a write has not made the stripe insync */
4215 if (test_bit(STRIPE_INSYNC, &sh->state))
4216 break;
Dan Williamsa4456852007-07-09 11:56:43 -07004217
4218 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07004219 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07004220 */
Nigel Croxonb2176a12019-04-16 09:50:09 -07004221 dev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07004222 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004223 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07004224 s->locked++;
4225 set_bit(R5_LOCKED, &dev->flags);
4226 set_bit(R5_Wantwrite, &dev->flags);
4227 }
4228 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004229 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07004230 s->locked++;
4231 set_bit(R5_LOCKED, &dev->flags);
4232 set_bit(R5_Wantwrite, &dev->flags);
4233 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004234 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004235 dev = &sh->dev[pd_idx];
4236 s->locked++;
4237 set_bit(R5_LOCKED, &dev->flags);
4238 set_bit(R5_Wantwrite, &dev->flags);
4239 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004240 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004241 dev = &sh->dev[qd_idx];
4242 s->locked++;
4243 set_bit(R5_LOCKED, &dev->flags);
4244 set_bit(R5_Wantwrite, &dev->flags);
4245 }
Nigel Croxonb2176a12019-04-16 09:50:09 -07004246 if (WARN_ONCE(dev && !test_bit(R5_UPTODATE, &dev->flags),
4247 "%s: disk%td not up to date\n",
4248 mdname(conf->mddev),
4249 dev - (struct r5dev *) &sh->dev)) {
4250 clear_bit(R5_LOCKED, &dev->flags);
4251 clear_bit(R5_Wantwrite, &dev->flags);
4252 s->locked--;
4253 }
Dan Williamsa4456852007-07-09 11:56:43 -07004254 clear_bit(STRIPE_DEGRADED, &sh->state);
4255
4256 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004257 break;
4258 case check_state_run:
4259 case check_state_run_q:
4260 case check_state_run_pq:
4261 break; /* we will be called again upon completion */
4262 case check_state_check_result:
4263 sh->check_state = check_state_idle;
4264
4265 /* handle a successful check operation, if parity is correct
4266 * we are done. Otherwise update the mismatch count and repair
4267 * parity if !MD_RECOVERY_CHECK
4268 */
4269 if (sh->ops.zero_sum_result == 0) {
Song Liua25d8c32019-04-16 09:34:21 -07004270 /* both parities are correct */
4271 if (!s->failed)
4272 set_bit(STRIPE_INSYNC, &sh->state);
4273 else {
4274 /* in contrast to the raid5 case we can validate
4275 * parity, but still have a failure to write
4276 * back
4277 */
4278 sh->check_state = check_state_compute_result;
4279 /* Returning at this point means that we may go
4280 * off and bring p and/or q uptodate again so
4281 * we make sure to check zero_sum_result again
4282 * to verify if p or q need writeback
4283 */
4284 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004285 } else {
Yufen Yuc911c462020-07-18 05:29:07 -04004286 atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches);
Nixe1539032017-05-16 10:13:31 +01004287 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004288 /* don't try to repair!! */
4289 set_bit(STRIPE_INSYNC, &sh->state);
Nixe1539032017-05-16 10:13:31 +01004290 pr_warn_ratelimited("%s: mismatch sector in range "
4291 "%llu-%llu\n", mdname(conf->mddev),
4292 (unsigned long long) sh->sector,
4293 (unsigned long long) sh->sector +
Yufen Yuc911c462020-07-18 05:29:07 -04004294 RAID5_STRIPE_SECTORS(conf));
Nixe1539032017-05-16 10:13:31 +01004295 } else {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004296 int *target = &sh->ops.target;
4297
4298 sh->ops.target = -1;
4299 sh->ops.target2 = -1;
4300 sh->check_state = check_state_compute_run;
4301 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4302 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4303 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4304 set_bit(R5_Wantcompute,
4305 &sh->dev[pd_idx].flags);
4306 *target = pd_idx;
4307 target = &sh->ops.target2;
4308 s->uptodate++;
4309 }
4310 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4311 set_bit(R5_Wantcompute,
4312 &sh->dev[qd_idx].flags);
4313 *target = qd_idx;
4314 s->uptodate++;
4315 }
4316 }
4317 }
4318 break;
4319 case check_state_compute_run:
4320 break;
4321 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11004322 pr_warn("%s: unknown check_state: %d sector: %llu\n",
4323 __func__, sh->check_state,
4324 (unsigned long long) sh->sector);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004325 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07004326 }
4327}
4328
NeilBrownd1688a62011-10-11 16:49:52 +11004329static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07004330{
4331 int i;
4332
4333 /* We have read all the blocks in this stripe and now we need to
4334 * copy some of them into a target stripe for expand.
4335 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07004336 struct dma_async_tx_descriptor *tx = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +11004337 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07004338 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4339 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11004340 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11004341 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07004342 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07004343 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07004344
Shaohua Li6d036f72015-08-13 14:31:57 -07004345 sector_t bn = raid5_compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11004346 sector_t s = raid5_compute_sector(conf, bn, 0,
4347 &dd_idx, NULL);
Shaohua Li6d036f72015-08-13 14:31:57 -07004348 sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07004349 if (sh2 == NULL)
4350 /* so far only the early blocks of this stripe
4351 * have been requested. When later blocks
4352 * get requested, we will try again
4353 */
4354 continue;
4355 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
4356 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
4357 /* must have already done this block */
Shaohua Li6d036f72015-08-13 14:31:57 -07004358 raid5_release_stripe(sh2);
Dan Williamsa4456852007-07-09 11:56:43 -07004359 continue;
4360 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07004361
4362 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07004363 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004364 tx = async_memcpy(sh2->dev[dd_idx].page,
Yufen Yuc911c462020-07-18 05:29:07 -04004365 sh->dev[i].page, 0, 0, RAID5_STRIPE_SIZE(conf),
Dan Williamsa08abd82009-06-03 11:43:59 -07004366 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004367
Dan Williamsa4456852007-07-09 11:56:43 -07004368 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
4369 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
4370 for (j = 0; j < conf->raid_disks; j++)
4371 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10004372 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07004373 !test_bit(R5_Expanded, &sh2->dev[j].flags))
4374 break;
4375 if (j == conf->raid_disks) {
4376 set_bit(STRIPE_EXPAND_READY, &sh2->state);
4377 set_bit(STRIPE_HANDLE, &sh2->state);
4378 }
Shaohua Li6d036f72015-08-13 14:31:57 -07004379 raid5_release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004380
Dan Williamsa4456852007-07-09 11:56:43 -07004381 }
NeilBrowna2e08552007-09-11 15:23:36 -07004382 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11004383 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07004384}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385
4386/*
4387 * handle_stripe - do things to a stripe.
4388 *
NeilBrown9a3e1102011-12-23 10:17:53 +11004389 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4390 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11004392 * return some read requests which now have data
4393 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 * schedule a read on some buffers
4395 * schedule a write of some buffers
4396 * return confirmation of parity correctness
4397 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 */
Dan Williamsa4456852007-07-09 11:56:43 -07004399
NeilBrownacfe7262011-07-27 11:00:36 +10004400static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07004401{
NeilBrownd1688a62011-10-11 16:49:52 +11004402 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08004403 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004404 struct r5dev *dev;
4405 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11004406 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07004407
NeilBrownacfe7262011-07-27 11:00:36 +10004408 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07004409
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004410 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
4411 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
NeilBrownacfe7262011-07-27 11:00:36 +10004412 s->failed_num[0] = -1;
4413 s->failed_num[1] = -1;
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004414 s->log_failed = r5l_log_disk_error(conf);
NeilBrownacfe7262011-07-27 11:00:36 +10004415
4416 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07004417 rcu_read_lock();
4418 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004419 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10004420 sector_t first_bad;
4421 int bad_sectors;
4422 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10004423
NeilBrown16a53ec2006-06-26 00:27:38 -07004424 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07004425
Dan Williams45b42332007-07-09 11:56:43 -07004426 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11004427 i, dev->flags,
4428 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004429 /* maybe we can reply to a read
4430 *
4431 * new wantfill requests are only permitted while
4432 * ops_complete_biofill is guaranteed to be inactive
4433 */
4434 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4435 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4436 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07004437
4438 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10004439 if (test_bit(R5_LOCKED, &dev->flags))
4440 s->locked++;
4441 if (test_bit(R5_UPTODATE, &dev->flags))
4442 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004443 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004444 s->compute++;
4445 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004446 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004447
NeilBrownacfe7262011-07-27 11:00:36 +10004448 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004449 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10004450 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10004451 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004452 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10004453 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004454 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004455 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004456 }
Dan Williamsa4456852007-07-09 11:56:43 -07004457 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10004458 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11004459 /* Prefer to use the replacement for reads, but only
4460 * if it is recovered enough and has no bad blocks.
4461 */
4462 rdev = rcu_dereference(conf->disks[i].replacement);
4463 if (rdev && !test_bit(Faulty, &rdev->flags) &&
Yufen Yuc911c462020-07-18 05:29:07 -04004464 rdev->recovery_offset >= sh->sector + RAID5_STRIPE_SECTORS(conf) &&
4465 !is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
NeilBrown14a75d32011-12-23 10:17:52 +11004466 &first_bad, &bad_sectors))
4467 set_bit(R5_ReadRepl, &dev->flags);
4468 else {
NeilBrowne6030cb2015-07-17 13:26:23 +10004469 if (rdev && !test_bit(Faulty, &rdev->flags))
NeilBrown9a3e1102011-12-23 10:17:53 +11004470 set_bit(R5_NeedReplace, &dev->flags);
NeilBrowne6030cb2015-07-17 13:26:23 +10004471 else
4472 clear_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11004473 rdev = rcu_dereference(conf->disks[i].rdev);
4474 clear_bit(R5_ReadRepl, &dev->flags);
4475 }
NeilBrown9283d8c2011-12-08 16:27:57 +11004476 if (rdev && test_bit(Faulty, &rdev->flags))
4477 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10004478 if (rdev) {
Yufen Yuc911c462020-07-18 05:29:07 -04004479 is_bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
NeilBrown31c176e2011-07-28 11:39:22 +10004480 &first_bad, &bad_sectors);
4481 if (s->blocked_rdev == NULL
4482 && (test_bit(Blocked, &rdev->flags)
4483 || is_bad < 0)) {
4484 if (is_bad < 0)
4485 set_bit(BlockedBadBlocks,
4486 &rdev->flags);
4487 s->blocked_rdev = rdev;
4488 atomic_inc(&rdev->nr_pending);
4489 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004490 }
NeilBrown415e72d2010-06-17 17:25:21 +10004491 clear_bit(R5_Insync, &dev->flags);
4492 if (!rdev)
4493 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10004494 else if (is_bad) {
4495 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10004496 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4497 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10004498 /* treat as in-sync, but with a read error
4499 * which we can now try to correct
4500 */
4501 set_bit(R5_Insync, &dev->flags);
4502 set_bit(R5_ReadError, &dev->flags);
4503 }
4504 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10004505 set_bit(R5_Insync, &dev->flags);
Yufen Yuc911c462020-07-18 05:29:07 -04004506 else if (sh->sector + RAID5_STRIPE_SECTORS(conf) <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10004507 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11004508 set_bit(R5_Insync, &dev->flags);
4509 else if (test_bit(R5_UPTODATE, &dev->flags) &&
4510 test_bit(R5_Expanded, &dev->flags))
4511 /* If we've reshaped into here, we assume it is Insync.
4512 * We will shortly update recovery_offset to make
4513 * it official.
4514 */
4515 set_bit(R5_Insync, &dev->flags);
4516
NeilBrown1cc03eb2014-01-06 13:19:42 +11004517 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004518 /* This flag does not apply to '.replacement'
4519 * only to .rdev, so make sure to check that*/
4520 struct md_rdev *rdev2 = rcu_dereference(
4521 conf->disks[i].rdev);
4522 if (rdev2 == rdev)
4523 clear_bit(R5_Insync, &dev->flags);
4524 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004525 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004526 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10004527 } else
4528 clear_bit(R5_WriteError, &dev->flags);
4529 }
NeilBrown1cc03eb2014-01-06 13:19:42 +11004530 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004531 /* This flag does not apply to '.replacement'
4532 * only to .rdev, so make sure to check that*/
4533 struct md_rdev *rdev2 = rcu_dereference(
4534 conf->disks[i].rdev);
4535 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10004536 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004537 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10004538 } else
4539 clear_bit(R5_MadeGood, &dev->flags);
4540 }
NeilBrown977df362011-12-23 10:17:53 +11004541 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4542 struct md_rdev *rdev2 = rcu_dereference(
4543 conf->disks[i].replacement);
4544 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4545 s->handle_bad_blocks = 1;
4546 atomic_inc(&rdev2->nr_pending);
4547 } else
4548 clear_bit(R5_MadeGoodRepl, &dev->flags);
4549 }
NeilBrown415e72d2010-06-17 17:25:21 +10004550 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004551 /* The ReadError flag will just be confusing now */
4552 clear_bit(R5_ReadError, &dev->flags);
4553 clear_bit(R5_ReWrite, &dev->flags);
4554 }
NeilBrown415e72d2010-06-17 17:25:21 +10004555 if (test_bit(R5_ReadError, &dev->flags))
4556 clear_bit(R5_Insync, &dev->flags);
4557 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004558 if (s->failed < 2)
4559 s->failed_num[s->failed] = i;
4560 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11004561 if (rdev && !test_bit(Faulty, &rdev->flags))
4562 do_recovery = 1;
BingJing Changd63e2fc2018-08-01 17:08:36 +08004563 else if (!rdev) {
4564 rdev = rcu_dereference(
4565 conf->disks[i].replacement);
4566 if (rdev && !test_bit(Faulty, &rdev->flags))
4567 do_recovery = 1;
4568 }
NeilBrown415e72d2010-06-17 17:25:21 +10004569 }
Song Liu2ded3702016-11-17 15:24:38 -08004570
4571 if (test_bit(R5_InJournal, &dev->flags))
4572 s->injournal++;
Song Liu1e6d6902016-11-17 15:24:39 -08004573 if (test_bit(R5_InJournal, &dev->flags) && dev->written)
4574 s->just_cached++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004575 }
NeilBrown9a3e1102011-12-23 10:17:53 +11004576 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4577 /* If there is a failed device being replaced,
4578 * we must be recovering.
4579 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10004580 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11004581 * else we can only be replacing
4582 * sync and recovery both need to read all devices, and so
4583 * use the same flag.
4584 */
4585 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10004586 sh->sector >= conf->mddev->recovery_cp ||
4587 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11004588 s->syncing = 1;
4589 else
4590 s->replacing = 1;
4591 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004592 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10004593}
NeilBrownf4168852007-02-28 20:11:53 -08004594
Guoqing Jiangcb9902d2020-06-16 11:25:51 +02004595/*
4596 * Return '1' if this is a member of batch, or '0' if it is a lone stripe or
4597 * a head which can now be handled.
4598 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11004599static int clear_batch_ready(struct stripe_head *sh)
4600{
4601 struct stripe_head *tmp;
4602 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
NeilBrownb15a9db2015-05-22 15:20:04 +10004603 return (sh->batch_head && sh->batch_head != sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11004604 spin_lock(&sh->stripe_lock);
4605 if (!sh->batch_head) {
4606 spin_unlock(&sh->stripe_lock);
4607 return 0;
4608 }
4609
4610 /*
4611 * this stripe could be added to a batch list before we check
4612 * BATCH_READY, skips it
4613 */
4614 if (sh->batch_head != sh) {
4615 spin_unlock(&sh->stripe_lock);
4616 return 1;
4617 }
4618 spin_lock(&sh->batch_lock);
4619 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4620 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4621 spin_unlock(&sh->batch_lock);
4622 spin_unlock(&sh->stripe_lock);
4623
4624 /*
4625 * BATCH_READY is cleared, no new stripes can be added.
4626 * batch_list can be accessed without lock
4627 */
4628 return 0;
4629}
4630
NeilBrown3960ce72015-05-21 12:20:36 +10004631static void break_stripe_batch_list(struct stripe_head *head_sh,
4632 unsigned long handle_flags)
shli@kernel.org72ac7332014-12-15 12:57:03 +11004633{
NeilBrown4e3d62f2015-05-21 11:50:16 +10004634 struct stripe_head *sh, *next;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004635 int i;
NeilBrownfb642b92015-05-21 12:00:47 +10004636 int do_wakeup = 0;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004637
NeilBrownbb270512015-05-08 18:19:40 +10004638 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4639
shli@kernel.org72ac7332014-12-15 12:57:03 +11004640 list_del_init(&sh->batch_list);
4641
Shaohua Lifb3229d2016-03-09 10:08:38 -08004642 WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
NeilBrown1b956f72015-05-21 12:40:26 +10004643 (1 << STRIPE_SYNCING) |
4644 (1 << STRIPE_REPLACED) |
NeilBrown1b956f72015-05-21 12:40:26 +10004645 (1 << STRIPE_DELAYED) |
4646 (1 << STRIPE_BIT_DELAY) |
4647 (1 << STRIPE_FULL_WRITE) |
4648 (1 << STRIPE_BIOFILL_RUN) |
4649 (1 << STRIPE_COMPUTE_RUN) |
NeilBrown1b956f72015-05-21 12:40:26 +10004650 (1 << STRIPE_DISCARD) |
4651 (1 << STRIPE_BATCH_READY) |
4652 (1 << STRIPE_BATCH_ERR) |
Shaohua Lifb3229d2016-03-09 10:08:38 -08004653 (1 << STRIPE_BITMAP_PENDING)),
4654 "stripe state: %lx\n", sh->state);
4655 WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4656 (1 << STRIPE_REPLACED)),
4657 "head stripe state: %lx\n", head_sh->state);
NeilBrown1b956f72015-05-21 12:40:26 +10004658
4659 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
NeilBrown550da242016-03-09 12:58:25 +11004660 (1 << STRIPE_PREREAD_ACTIVE) |
Dennis Yang184a09e2017-09-06 11:02:35 +08004661 (1 << STRIPE_DEGRADED) |
4662 (1 << STRIPE_ON_UNPLUG_LIST)),
NeilBrown1b956f72015-05-21 12:40:26 +10004663 head_sh->state & (1 << STRIPE_INSYNC));
4664
shli@kernel.org72ac7332014-12-15 12:57:03 +11004665 sh->check_state = head_sh->check_state;
4666 sh->reconstruct_state = head_sh->reconstruct_state;
Amy Chiang448ec632018-05-16 18:59:35 +08004667 spin_lock_irq(&sh->stripe_lock);
4668 sh->batch_head = NULL;
4669 spin_unlock_irq(&sh->stripe_lock);
NeilBrownfb642b92015-05-21 12:00:47 +10004670 for (i = 0; i < sh->disks; i++) {
4671 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4672 do_wakeup = 1;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004673 sh->dev[i].flags = head_sh->dev[i].flags &
4674 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
NeilBrownfb642b92015-05-21 12:00:47 +10004675 }
NeilBrown3960ce72015-05-21 12:20:36 +10004676 if (handle_flags == 0 ||
4677 sh->state & handle_flags)
4678 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07004679 raid5_release_stripe(sh);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004680 }
NeilBrownfb642b92015-05-21 12:00:47 +10004681 spin_lock_irq(&head_sh->stripe_lock);
4682 head_sh->batch_head = NULL;
4683 spin_unlock_irq(&head_sh->stripe_lock);
4684 for (i = 0; i < head_sh->disks; i++)
4685 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4686 do_wakeup = 1;
NeilBrown3960ce72015-05-21 12:20:36 +10004687 if (head_sh->state & handle_flags)
4688 set_bit(STRIPE_HANDLE, &head_sh->state);
NeilBrownfb642b92015-05-21 12:00:47 +10004689
4690 if (do_wakeup)
4691 wake_up(&head_sh->raid_conf->wait_for_overlap);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004692}
4693
NeilBrowncc940152011-07-26 11:35:35 +10004694static void handle_stripe(struct stripe_head *sh)
4695{
4696 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11004697 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10004698 int i;
NeilBrown84789552011-07-27 11:00:36 +10004699 int prexor;
4700 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004701 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10004702
4703 clear_bit(STRIPE_HANDLE, &sh->state);
Guoqing Jianga377a472020-06-16 11:25:50 +02004704
4705 /*
4706 * handle_stripe should not continue handle the batched stripe, only
4707 * the head of batch list or lone stripe can continue. Otherwise we
4708 * could see break_stripe_batch_list warns about the STRIPE_ACTIVE
4709 * is set for the batched stripe.
4710 */
4711 if (clear_batch_ready(sh))
4712 return;
4713
Dan Williams257a4b42011-11-08 16:22:06 +11004714 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10004715 /* already being handled, ensure it gets handled
4716 * again when current action finishes */
4717 set_bit(STRIPE_HANDLE, &sh->state);
4718 return;
4719 }
4720
NeilBrown4e3d62f2015-05-21 11:50:16 +10004721 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
NeilBrown3960ce72015-05-21 12:20:36 +10004722 break_stripe_batch_list(sh, 0);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004723
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004724 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
NeilBrownf8dfcff2013-03-12 12:18:06 +11004725 spin_lock(&sh->stripe_lock);
Song Liu5ddf0442017-05-11 17:03:44 -07004726 /*
4727 * Cannot process 'sync' concurrently with 'discard'.
4728 * Flush data in r5cache before 'sync'.
4729 */
4730 if (!test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) &&
4731 !test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) &&
4732 !test_bit(STRIPE_DISCARD, &sh->state) &&
NeilBrownf8dfcff2013-03-12 12:18:06 +11004733 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4734 set_bit(STRIPE_SYNCING, &sh->state);
4735 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10004736 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004737 }
4738 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10004739 }
4740 clear_bit(STRIPE_DELAYED, &sh->state);
4741
4742 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4743 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4744 (unsigned long long)sh->sector, sh->state,
4745 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4746 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10004747
NeilBrownacfe7262011-07-27 11:00:36 +10004748 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10004749
Shaohua Lib70abcb2015-08-13 14:31:58 -07004750 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
4751 goto finish;
4752
NeilBrown16d997b2017-03-15 14:05:12 +11004753 if (s.handle_bad_blocks ||
4754 test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004755 set_bit(STRIPE_HANDLE, &sh->state);
4756 goto finish;
4757 }
4758
NeilBrown474af965fe2011-07-27 11:00:36 +10004759 if (unlikely(s.blocked_rdev)) {
4760 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11004761 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10004762 set_bit(STRIPE_HANDLE, &sh->state);
4763 goto finish;
4764 }
4765 /* There is nothing for the blocked_rdev to block */
4766 rdev_dec_pending(s.blocked_rdev, conf->mddev);
4767 s.blocked_rdev = NULL;
4768 }
4769
4770 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4771 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4772 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4773 }
4774
4775 pr_debug("locked=%d uptodate=%d to_read=%d"
4776 " to_write=%d failed=%d failed_num=%d,%d\n",
4777 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4778 s.failed_num[0], s.failed_num[1]);
Song Liu70d466f2017-05-11 15:28:28 -07004779 /*
4780 * check if the array has lost more than max_degraded devices and,
NeilBrown474af965fe2011-07-27 11:00:36 +10004781 * if so, some requests might need to be failed.
Song Liu70d466f2017-05-11 15:28:28 -07004782 *
4783 * When journal device failed (log_failed), we will only process
4784 * the stripe if there is data need write to raid disks
NeilBrown474af965fe2011-07-27 11:00:36 +10004785 */
Song Liu70d466f2017-05-11 15:28:28 -07004786 if (s.failed > conf->max_degraded ||
4787 (s.log_failed && s.injournal == 0)) {
NeilBrown9a3f5302011-11-08 16:22:01 +11004788 sh->check_state = 0;
4789 sh->reconstruct_state = 0;
NeilBrown626f2092015-05-22 14:03:10 +10004790 break_stripe_batch_list(sh, 0);
NeilBrown9a3f5302011-11-08 16:22:01 +11004791 if (s.to_read+s.to_write+s.written)
NeilBrownbd83d0a2017-03-15 14:05:12 +11004792 handle_failed_stripe(conf, sh, &s, disks);
NeilBrown9a3e1102011-12-23 10:17:53 +11004793 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11004794 handle_failed_sync(conf, sh, &s);
4795 }
NeilBrown474af965fe2011-07-27 11:00:36 +10004796
NeilBrown84789552011-07-27 11:00:36 +10004797 /* Now we check to see if any write operations have recently
4798 * completed
4799 */
4800 prexor = 0;
4801 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4802 prexor = 1;
4803 if (sh->reconstruct_state == reconstruct_state_drain_result ||
4804 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
4805 sh->reconstruct_state = reconstruct_state_idle;
4806
4807 /* All the 'written' buffers and the parity block are ready to
4808 * be written back to disk
4809 */
Shaohua Li9e4447682012-10-11 13:49:49 +11004810 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
4811 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004812 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11004813 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
4814 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004815 for (i = disks; i--; ) {
4816 struct r5dev *dev = &sh->dev[i];
4817 if (test_bit(R5_LOCKED, &dev->flags) &&
4818 (i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08004819 dev->written || test_bit(R5_InJournal,
4820 &dev->flags))) {
NeilBrown84789552011-07-27 11:00:36 +10004821 pr_debug("Writing block %d\n", i);
4822 set_bit(R5_Wantwrite, &dev->flags);
4823 if (prexor)
4824 continue;
NeilBrown9c4bdf62014-08-13 09:57:07 +10004825 if (s.failed > 1)
4826 continue;
NeilBrown84789552011-07-27 11:00:36 +10004827 if (!test_bit(R5_Insync, &dev->flags) ||
4828 ((i == sh->pd_idx || i == sh->qd_idx) &&
4829 s.failed == 0))
4830 set_bit(STRIPE_INSYNC, &sh->state);
4831 }
4832 }
4833 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4834 s.dec_preread_active = 1;
4835 }
4836
NeilBrownef5b7c62012-11-22 09:13:36 +11004837 /*
4838 * might be able to return some write requests if the parity blocks
4839 * are safe, or on a failed drive
4840 */
4841 pdev = &sh->dev[sh->pd_idx];
4842 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
4843 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
4844 qdev = &sh->dev[sh->qd_idx];
4845 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
4846 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
4847 || conf->level < 6;
4848
4849 if (s.written &&
4850 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
4851 && !test_bit(R5_LOCKED, &pdev->flags)
4852 && (test_bit(R5_UPTODATE, &pdev->flags) ||
4853 test_bit(R5_Discard, &pdev->flags))))) &&
4854 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
4855 && !test_bit(R5_LOCKED, &qdev->flags)
4856 && (test_bit(R5_UPTODATE, &qdev->flags) ||
4857 test_bit(R5_Discard, &qdev->flags))))))
NeilBrownbd83d0a2017-03-15 14:05:12 +11004858 handle_stripe_clean_event(conf, sh, disks);
NeilBrownef5b7c62012-11-22 09:13:36 +11004859
Song Liu1e6d6902016-11-17 15:24:39 -08004860 if (s.just_cached)
NeilBrownbd83d0a2017-03-15 14:05:12 +11004861 r5c_handle_cached_data_endio(conf, sh, disks);
Artur Paszkiewiczff875732017-03-09 09:59:58 +01004862 log_stripe_write_finished(sh);
Song Liu1e6d6902016-11-17 15:24:39 -08004863
NeilBrownef5b7c62012-11-22 09:13:36 +11004864 /* Now we might consider reading some blocks, either to check/generate
4865 * parity, or to satisfy requests
4866 * or to load a block that is being partially written.
4867 */
4868 if (s.to_read || s.non_overwrite
4869 || (conf->level == 6 && s.to_write && s.failed)
4870 || (s.syncing && (s.uptodate + s.compute < disks))
4871 || s.replacing
4872 || s.expanding)
4873 handle_stripe_fill(sh, &s, disks);
4874
Song Liu2ded3702016-11-17 15:24:38 -08004875 /*
4876 * When the stripe finishes full journal write cycle (write to journal
4877 * and raid disk), this is the clean up procedure so it is ready for
4878 * next operation.
4879 */
4880 r5c_finish_stripe_write_out(conf, sh, &s);
4881
4882 /*
4883 * Now to consider new write requests, cache write back and what else,
4884 * if anything should be read. We do not handle new writes when:
NeilBrown84789552011-07-27 11:00:36 +10004885 * 1/ A 'write' operation (copy+xor) is already in flight.
4886 * 2/ A 'check' operation is in flight, as it may clobber the parity
4887 * block.
Song Liu2ded3702016-11-17 15:24:38 -08004888 * 3/ A r5c cache log write is in flight.
NeilBrown84789552011-07-27 11:00:36 +10004889 */
Song Liu2ded3702016-11-17 15:24:38 -08004890
4891 if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
4892 if (!r5c_is_writeback(conf->log)) {
4893 if (s.to_write)
4894 handle_stripe_dirtying(conf, sh, &s, disks);
4895 } else { /* write back cache */
4896 int ret = 0;
4897
4898 /* First, try handle writes in caching phase */
4899 if (s.to_write)
4900 ret = r5c_try_caching_write(conf, sh, &s,
4901 disks);
4902 /*
4903 * If caching phase failed: ret == -EAGAIN
4904 * OR
4905 * stripe under reclaim: !caching && injournal
4906 *
4907 * fall back to handle_stripe_dirtying()
4908 */
4909 if (ret == -EAGAIN ||
4910 /* stripe under reclaim: !caching && injournal */
4911 (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
Song Liud7bd3982016-11-23 22:50:39 -08004912 s.injournal > 0)) {
4913 ret = handle_stripe_dirtying(conf, sh, &s,
4914 disks);
4915 if (ret == -EAGAIN)
4916 goto finish;
4917 }
Song Liu2ded3702016-11-17 15:24:38 -08004918 }
4919 }
NeilBrown84789552011-07-27 11:00:36 +10004920
4921 /* maybe we need to check and possibly fix the parity for this stripe
4922 * Any reads will already have been scheduled, so we just see if enough
4923 * data is available. The parity check is held off while parity
4924 * dependent operations are in flight.
4925 */
4926 if (sh->check_state ||
4927 (s.syncing && s.locked == 0 &&
4928 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
4929 !test_bit(STRIPE_INSYNC, &sh->state))) {
4930 if (conf->level == 6)
4931 handle_parity_checks6(conf, sh, &s, disks);
4932 else
4933 handle_parity_checks5(conf, sh, &s, disks);
4934 }
NeilBrownc5a31002011-07-27 11:00:36 +10004935
NeilBrownf94c0b62013-07-22 12:57:21 +10004936 if ((s.replacing || s.syncing) && s.locked == 0
4937 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
4938 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11004939 /* Write out to replacement devices where possible */
4940 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10004941 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
4942 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11004943 set_bit(R5_WantReplace, &sh->dev[i].flags);
4944 set_bit(R5_LOCKED, &sh->dev[i].flags);
4945 s.locked++;
4946 }
NeilBrownf94c0b62013-07-22 12:57:21 +10004947 if (s.replacing)
4948 set_bit(STRIPE_INSYNC, &sh->state);
4949 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11004950 }
4951 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10004952 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11004953 test_bit(STRIPE_INSYNC, &sh->state)) {
Yufen Yuc911c462020-07-18 05:29:07 -04004954 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1);
NeilBrownc5a31002011-07-27 11:00:36 +10004955 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004956 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
4957 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10004958 }
4959
4960 /* If the failed drives are just a ReadError, then we might need
4961 * to progress the repair/check process
4962 */
4963 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
4964 for (i = 0; i < s.failed; i++) {
4965 struct r5dev *dev = &sh->dev[s.failed_num[i]];
4966 if (test_bit(R5_ReadError, &dev->flags)
4967 && !test_bit(R5_LOCKED, &dev->flags)
4968 && test_bit(R5_UPTODATE, &dev->flags)
4969 ) {
4970 if (!test_bit(R5_ReWrite, &dev->flags)) {
4971 set_bit(R5_Wantwrite, &dev->flags);
4972 set_bit(R5_ReWrite, &dev->flags);
4973 set_bit(R5_LOCKED, &dev->flags);
4974 s.locked++;
4975 } else {
4976 /* let's read it back */
4977 set_bit(R5_Wantread, &dev->flags);
4978 set_bit(R5_LOCKED, &dev->flags);
4979 s.locked++;
4980 }
4981 }
4982 }
4983
NeilBrown3687c062011-07-27 11:00:36 +10004984 /* Finish reconstruct operations initiated by the expansion process */
4985 if (sh->reconstruct_state == reconstruct_state_result) {
4986 struct stripe_head *sh_src
Shaohua Li6d036f72015-08-13 14:31:57 -07004987 = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrown3687c062011-07-27 11:00:36 +10004988 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
4989 /* sh cannot be written until sh_src has been read.
4990 * so arrange for sh to be delayed a little
4991 */
4992 set_bit(STRIPE_DELAYED, &sh->state);
4993 set_bit(STRIPE_HANDLE, &sh->state);
4994 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
4995 &sh_src->state))
4996 atomic_inc(&conf->preread_active_stripes);
Shaohua Li6d036f72015-08-13 14:31:57 -07004997 raid5_release_stripe(sh_src);
NeilBrown3687c062011-07-27 11:00:36 +10004998 goto finish;
4999 }
5000 if (sh_src)
Shaohua Li6d036f72015-08-13 14:31:57 -07005001 raid5_release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07005002
NeilBrown3687c062011-07-27 11:00:36 +10005003 sh->reconstruct_state = reconstruct_state_idle;
5004 clear_bit(STRIPE_EXPANDING, &sh->state);
5005 for (i = conf->raid_disks; i--; ) {
5006 set_bit(R5_Wantwrite, &sh->dev[i].flags);
5007 set_bit(R5_LOCKED, &sh->dev[i].flags);
5008 s.locked++;
5009 }
5010 }
5011
5012 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
5013 !sh->reconstruct_state) {
5014 /* Need to write out all blocks after computing parity */
5015 sh->disks = conf->raid_disks;
5016 stripe_set_idx(sh->sector, conf, 0, sh);
5017 schedule_reconstruction(sh, &s, 1, 1);
5018 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
5019 clear_bit(STRIPE_EXPAND_READY, &sh->state);
5020 atomic_dec(&conf->reshape_stripes);
5021 wake_up(&conf->wait_for_overlap);
Yufen Yuc911c462020-07-18 05:29:07 -04005022 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1);
NeilBrown3687c062011-07-27 11:00:36 +10005023 }
5024
5025 if (s.expanding && s.locked == 0 &&
5026 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
5027 handle_stripe_expansion(conf, sh);
5028
5029finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07005030 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10005031 if (unlikely(s.blocked_rdev)) {
5032 if (conf->mddev->external)
5033 md_wait_for_blocked_rdev(s.blocked_rdev,
5034 conf->mddev);
5035 else
5036 /* Internal metadata will immediately
5037 * be written by raid5d, so we don't
5038 * need to wait here.
5039 */
5040 rdev_dec_pending(s.blocked_rdev,
5041 conf->mddev);
5042 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07005043
NeilBrownbc2607f2011-07-28 11:39:22 +10005044 if (s.handle_bad_blocks)
5045 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11005046 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10005047 struct r5dev *dev = &sh->dev[i];
5048 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
5049 /* We own a safe reference to the rdev */
5050 rdev = conf->disks[i].rdev;
5051 if (!rdev_set_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04005052 RAID5_STRIPE_SECTORS(conf), 0))
NeilBrownbc2607f2011-07-28 11:39:22 +10005053 md_error(conf->mddev, rdev);
5054 rdev_dec_pending(rdev, conf->mddev);
5055 }
NeilBrownb84db562011-07-28 11:39:23 +10005056 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
5057 rdev = conf->disks[i].rdev;
5058 rdev_clear_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04005059 RAID5_STRIPE_SECTORS(conf), 0);
NeilBrownb84db562011-07-28 11:39:23 +10005060 rdev_dec_pending(rdev, conf->mddev);
5061 }
NeilBrown977df362011-12-23 10:17:53 +11005062 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
5063 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11005064 if (!rdev)
5065 /* rdev have been moved down */
5066 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11005067 rdev_clear_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04005068 RAID5_STRIPE_SECTORS(conf), 0);
NeilBrown977df362011-12-23 10:17:53 +11005069 rdev_dec_pending(rdev, conf->mddev);
5070 }
NeilBrownbc2607f2011-07-28 11:39:22 +10005071 }
5072
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07005073 if (s.ops_request)
5074 raid_run_ops(sh, s.ops_request);
5075
Dan Williamsf0e43bc2008-06-28 08:31:55 +10005076 ops_run_io(sh, &s);
5077
NeilBrownc5709ef2011-07-26 11:35:20 +10005078 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11005079 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02005080 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11005081 * have actually been submitted.
5082 */
5083 atomic_dec(&conf->preread_active_stripes);
5084 if (atomic_read(&conf->preread_active_stripes) <
5085 IO_THRESHOLD)
5086 md_wakeup_thread(conf->mddev->thread);
5087 }
5088
Dan Williams257a4b42011-11-08 16:22:06 +11005089 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07005090}
5091
NeilBrownd1688a62011-10-11 16:49:52 +11005092static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093{
5094 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
5095 while (!list_empty(&conf->delayed_list)) {
5096 struct list_head *l = conf->delayed_list.next;
5097 struct stripe_head *sh;
5098 sh = list_entry(l, struct stripe_head, lru);
5099 list_del_init(l);
5100 clear_bit(STRIPE_DELAYED, &sh->state);
5101 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5102 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005103 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08005104 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 }
NeilBrown482c0832011-04-18 18:25:42 +10005106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107}
5108
Shaohua Li566c09c2013-11-14 15:16:17 +11005109static void activate_bit_delay(struct r5conf *conf,
5110 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07005111{
5112 /* device_lock is held */
5113 struct list_head head;
5114 list_add(&head, &conf->bitmap_list);
5115 list_del_init(&conf->bitmap_list);
5116 while (!list_empty(&head)) {
5117 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11005118 int hash;
NeilBrown72626682005-09-09 16:23:54 -07005119 list_del_init(&sh->lru);
5120 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11005121 hash = sh->hash_lock_index;
5122 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07005123 }
5124}
5125
NeilBrown5c675f82014-12-15 12:56:56 +11005126static int raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07005127{
NeilBrownd1688a62011-10-11 16:49:52 +11005128 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07005129
5130 /* No difference between reads and writes. Just check
5131 * how busy the stripe_cache is
5132 */
NeilBrown3fa841d2009-09-23 18:10:29 +10005133
NeilBrown54233992015-02-26 12:21:04 +11005134 if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state))
NeilBrownf022b2f2006-10-03 01:15:56 -07005135 return 1;
Song Liua39f7af2016-11-17 15:24:40 -08005136
5137 /* Also checks whether there is pressure on r5cache log space */
5138 if (test_bit(R5C_LOG_TIGHT, &conf->cache_state))
5139 return 1;
NeilBrownf022b2f2006-10-03 01:15:56 -07005140 if (conf->quiesce)
5141 return 1;
Shaohua Li4bda5562013-11-14 15:16:17 +11005142 if (atomic_read(&conf->empty_inactive_list_nr))
NeilBrownf022b2f2006-10-03 01:15:56 -07005143 return 1;
5144
5145 return 0;
5146}
5147
NeilBrownfd01b882011-10-11 16:47:53 +11005148static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005149{
NeilBrown3cb5edf2015-07-15 17:24:17 +10005150 struct r5conf *conf = mddev->private;
Christoph Hellwig10433d02017-08-23 19:10:28 +02005151 sector_t sector = bio->bi_iter.bi_sector;
NeilBrown3cb5edf2015-07-15 17:24:17 +10005152 unsigned int chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005153 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005154
Christoph Hellwig10433d02017-08-23 19:10:28 +02005155 WARN_ON_ONCE(bio->bi_partno);
5156
NeilBrown3cb5edf2015-07-15 17:24:17 +10005157 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005158 return chunk_sectors >=
5159 ((sector & (chunk_sectors - 1)) + bio_sectors);
5160}
5161
5162/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005163 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
5164 * later sampled by raid5d.
5165 */
NeilBrownd1688a62011-10-11 16:49:52 +11005166static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005167{
5168 unsigned long flags;
5169
5170 spin_lock_irqsave(&conf->device_lock, flags);
5171
5172 bi->bi_next = conf->retry_read_aligned_list;
5173 conf->retry_read_aligned_list = bi;
5174
5175 spin_unlock_irqrestore(&conf->device_lock, flags);
5176 md_wakeup_thread(conf->mddev->thread);
5177}
5178
NeilBrown0472a422017-03-15 14:05:13 +11005179static struct bio *remove_bio_from_retry(struct r5conf *conf,
5180 unsigned int *offset)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005181{
5182 struct bio *bi;
5183
5184 bi = conf->retry_read_aligned;
5185 if (bi) {
NeilBrown0472a422017-03-15 14:05:13 +11005186 *offset = conf->retry_read_offset;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005187 conf->retry_read_aligned = NULL;
5188 return bi;
5189 }
5190 bi = conf->retry_read_aligned_list;
5191 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08005192 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005193 bi->bi_next = NULL;
NeilBrown0472a422017-03-15 14:05:13 +11005194 *offset = 0;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005195 }
5196
5197 return bi;
5198}
5199
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005200/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005201 * The "raid5_align_endio" should check if the read succeeded and if it
5202 * did, call bio_endio on the original bio (having bio_put the new bio
5203 * first).
5204 * If the read failed..
5205 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005206static void raid5_align_endio(struct bio *bi)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005207{
5208 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11005209 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005210 struct r5conf *conf;
NeilBrown3cb03002011-10-11 16:45:26 +11005211 struct md_rdev *rdev;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02005212 blk_status_t error = bi->bi_status;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005213
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005214 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005215
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005216 rdev = (void*)raid_bi->bi_next;
5217 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11005218 mddev = rdev->mddev;
5219 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005220
5221 rdev_dec_pending(rdev, conf->mddev);
5222
Sasha Levin9b81c842015-08-10 19:05:18 -04005223 if (!error) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005224 bio_endio(raid_bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005225 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10005226 wake_up(&conf->wait_for_quiescent);
NeilBrown6712ecf2007-09-27 12:47:43 +02005227 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005228 }
5229
Dan Williams45b42332007-07-09 11:56:43 -07005230 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005231
5232 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005233}
5234
Ming Lin7ef6b122015-05-06 22:51:24 -07005235static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005236{
NeilBrownd1688a62011-10-11 16:49:52 +11005237 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11005238 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005239 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11005240 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11005241 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005242
5243 if (!in_chunk_boundary(mddev, raid_bio)) {
Ming Lin7ef6b122015-05-06 22:51:24 -07005244 pr_debug("%s: non aligned\n", __func__);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005245 return 0;
5246 }
5247 /*
Ming Leid7a10302017-02-14 23:29:03 +08005248 * use bio_clone_fast to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005249 */
Kent Overstreetafeee512018-05-20 18:25:52 -04005250 align_bi = bio_clone_fast(raid_bio, GFP_NOIO, &mddev->bio_set);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005251 if (!align_bi)
5252 return 0;
5253 /*
5254 * set bi_end_io to a new function, and set bi_private to the
5255 * original bio.
5256 */
5257 align_bi->bi_end_io = raid5_align_endio;
5258 align_bi->bi_private = raid_bio;
5259 /*
5260 * compute position
5261 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005262 align_bi->bi_iter.bi_sector =
5263 raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
5264 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005265
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005266 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005267 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11005268 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
5269 if (!rdev || test_bit(Faulty, &rdev->flags) ||
5270 rdev->recovery_offset < end_sector) {
5271 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
5272 if (rdev &&
5273 (test_bit(Faulty, &rdev->flags) ||
5274 !(test_bit(In_sync, &rdev->flags) ||
5275 rdev->recovery_offset >= end_sector)))
5276 rdev = NULL;
5277 }
Song Liu03b047f2017-01-11 13:39:14 -08005278
5279 if (r5c_big_stripe_cached(conf, align_bi->bi_iter.bi_sector)) {
5280 rcu_read_unlock();
5281 bio_put(align_bi);
5282 return 0;
5283 }
5284
NeilBrown671488c2011-12-23 10:17:52 +11005285 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10005286 sector_t first_bad;
5287 int bad_sectors;
5288
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005289 atomic_inc(&rdev->nr_pending);
5290 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005291 raid_bio->bi_next = (void*)rdev;
Christoph Hellwig74d46992017-08-23 19:10:32 +02005292 bio_set_dev(align_bi, rdev->bdev);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005293
Kent Overstreet7140aaf2013-09-25 13:37:01 -07005294 if (is_badblock(rdev, align_bi->bi_iter.bi_sector,
Kent Overstreet4f024f32013-10-11 15:44:27 -07005295 bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10005296 &first_bad, &bad_sectors)) {
Neil Brown387bb172007-02-08 14:20:29 -08005297 bio_put(align_bi);
5298 rdev_dec_pending(rdev, mddev);
5299 return 0;
5300 }
5301
majianpeng6c0544e2012-06-12 08:31:10 +08005302 /* No reshape active, so we can trust rdev->data_offset */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005303 align_bi->bi_iter.bi_sector += rdev->data_offset;
majianpeng6c0544e2012-06-12 08:31:10 +08005304
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005305 spin_lock_irq(&conf->device_lock);
Yuanhan Liub1b46482015-05-08 18:19:06 +10005306 wait_event_lock_irq(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005307 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01005308 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005309 atomic_inc(&conf->active_aligned_reads);
5310 spin_unlock_irq(&conf->device_lock);
5311
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005312 if (mddev->gendisk)
Christoph Hellwig74d46992017-08-23 19:10:32 +02005313 trace_block_bio_remap(align_bi->bi_disk->queue,
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005314 align_bi, disk_devt(mddev->gendisk),
Kent Overstreet4f024f32013-10-11 15:44:27 -07005315 raid_bio->bi_iter.bi_sector);
Christoph Hellwiged00aab2020-07-01 10:59:44 +02005316 submit_bio_noacct(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005317 return 1;
5318 } else {
5319 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005320 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005321 return 0;
5322 }
5323}
5324
Ming Lin7ef6b122015-05-06 22:51:24 -07005325static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
5326{
5327 struct bio *split;
NeilBrowndd7a8f52017-04-05 14:05:51 +10005328 sector_t sector = raid_bio->bi_iter.bi_sector;
5329 unsigned chunk_sects = mddev->chunk_sectors;
5330 unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
Ming Lin7ef6b122015-05-06 22:51:24 -07005331
NeilBrowndd7a8f52017-04-05 14:05:51 +10005332 if (sectors < bio_sectors(raid_bio)) {
5333 struct r5conf *conf = mddev->private;
Kent Overstreetafeee512018-05-20 18:25:52 -04005334 split = bio_split(raid_bio, sectors, GFP_NOIO, &conf->bio_split);
NeilBrowndd7a8f52017-04-05 14:05:51 +10005335 bio_chain(split, raid_bio);
Christoph Hellwiged00aab2020-07-01 10:59:44 +02005336 submit_bio_noacct(raid_bio);
NeilBrowndd7a8f52017-04-05 14:05:51 +10005337 raid_bio = split;
5338 }
Ming Lin7ef6b122015-05-06 22:51:24 -07005339
NeilBrowndd7a8f52017-04-05 14:05:51 +10005340 if (!raid5_read_one_chunk(mddev, raid_bio))
5341 return raid_bio;
Ming Lin7ef6b122015-05-06 22:51:24 -07005342
5343 return NULL;
5344}
5345
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005346/* __get_priority_stripe - get the next stripe to process
5347 *
5348 * Full stripe writes are allowed to pass preread active stripes up until
5349 * the bypass_threshold is exceeded. In general the bypass_count
5350 * increments when the handle_list is handled before the hold_list; however, it
5351 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
5352 * stripe with in flight i/o. The bypass_count will be reset when the
5353 * head of the hold_list has changed, i.e. the head was promoted to the
5354 * handle_list.
5355 */
Shaohua Li851c30c2013-08-28 14:30:16 +08005356static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005357{
Shaohua Li535ae4e2017-02-15 19:37:32 -08005358 struct stripe_head *sh, *tmp;
Shaohua Li851c30c2013-08-28 14:30:16 +08005359 struct list_head *handle_list = NULL;
Shaohua Li535ae4e2017-02-15 19:37:32 -08005360 struct r5worker_group *wg;
Song Liu70d466f2017-05-11 15:28:28 -07005361 bool second_try = !r5c_is_writeback(conf->log) &&
5362 !r5l_log_disk_error(conf);
5363 bool try_loprio = test_bit(R5C_LOG_TIGHT, &conf->cache_state) ||
5364 r5l_log_disk_error(conf);
Shaohua Li851c30c2013-08-28 14:30:16 +08005365
Shaohua Li535ae4e2017-02-15 19:37:32 -08005366again:
5367 wg = NULL;
5368 sh = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005369 if (conf->worker_cnt_per_group == 0) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005370 handle_list = try_loprio ? &conf->loprio_list :
5371 &conf->handle_list;
Shaohua Li851c30c2013-08-28 14:30:16 +08005372 } else if (group != ANY_GROUP) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005373 handle_list = try_loprio ? &conf->worker_groups[group].loprio_list :
5374 &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005375 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08005376 } else {
5377 int i;
5378 for (i = 0; i < conf->group_cnt; i++) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005379 handle_list = try_loprio ? &conf->worker_groups[i].loprio_list :
5380 &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005381 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08005382 if (!list_empty(handle_list))
5383 break;
5384 }
5385 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005386
5387 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5388 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08005389 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005390 list_empty(&conf->hold_list) ? "empty" : "busy",
5391 atomic_read(&conf->pending_full_writes), conf->bypass_count);
5392
Shaohua Li851c30c2013-08-28 14:30:16 +08005393 if (!list_empty(handle_list)) {
5394 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005395
5396 if (list_empty(&conf->hold_list))
5397 conf->bypass_count = 0;
5398 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
5399 if (conf->hold_list.next == conf->last_hold)
5400 conf->bypass_count++;
5401 else {
5402 conf->last_hold = conf->hold_list.next;
5403 conf->bypass_count -= conf->bypass_threshold;
5404 if (conf->bypass_count < 0)
5405 conf->bypass_count = 0;
5406 }
5407 }
5408 } else if (!list_empty(&conf->hold_list) &&
5409 ((conf->bypass_threshold &&
5410 conf->bypass_count > conf->bypass_threshold) ||
5411 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08005412
5413 list_for_each_entry(tmp, &conf->hold_list, lru) {
5414 if (conf->worker_cnt_per_group == 0 ||
5415 group == ANY_GROUP ||
5416 !cpu_online(tmp->cpu) ||
5417 cpu_to_group(tmp->cpu) == group) {
5418 sh = tmp;
5419 break;
5420 }
5421 }
5422
5423 if (sh) {
5424 conf->bypass_count -= conf->bypass_threshold;
5425 if (conf->bypass_count < 0)
5426 conf->bypass_count = 0;
5427 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08005428 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005429 }
5430
Shaohua Li535ae4e2017-02-15 19:37:32 -08005431 if (!sh) {
5432 if (second_try)
5433 return NULL;
5434 second_try = true;
5435 try_loprio = !try_loprio;
5436 goto again;
5437 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005438
Shaohua Libfc90cb2013-08-29 15:40:32 +08005439 if (wg) {
5440 wg->stripes_cnt--;
5441 sh->group = NULL;
5442 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005443 list_del_init(&sh->lru);
Shaohua Lic7a6d352014-04-15 09:12:54 +08005444 BUG_ON(atomic_inc_return(&sh->count) != 1);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005445 return sh;
5446}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005447
Shaohua Li8811b592012-08-02 08:33:00 +10005448struct raid5_plug_cb {
5449 struct blk_plug_cb cb;
5450 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11005451 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10005452};
5453
5454static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
5455{
5456 struct raid5_plug_cb *cb = container_of(
5457 blk_cb, struct raid5_plug_cb, cb);
5458 struct stripe_head *sh;
5459 struct mddev *mddev = cb->cb.data;
5460 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11005461 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11005462 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10005463
5464 if (cb->list.next && !list_empty(&cb->list)) {
5465 spin_lock_irq(&conf->device_lock);
5466 while (!list_empty(&cb->list)) {
5467 sh = list_first_entry(&cb->list, struct stripe_head, lru);
5468 list_del_init(&sh->lru);
5469 /*
5470 * avoid race release_stripe_plug() sees
5471 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5472 * is still in our list
5473 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005474 smp_mb__before_atomic();
Shaohua Li8811b592012-08-02 08:33:00 +10005475 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08005476 /*
5477 * STRIPE_ON_RELEASE_LIST could be set here. In that
5478 * case, the count is always > 1 here
5479 */
Shaohua Li566c09c2013-11-14 15:16:17 +11005480 hash = sh->hash_lock_index;
5481 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11005482 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10005483 }
5484 spin_unlock_irq(&conf->device_lock);
5485 }
Shaohua Li566c09c2013-11-14 15:16:17 +11005486 release_inactive_stripe_list(conf, cb->temp_inactive_list,
5487 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005488 if (mddev->queue)
5489 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10005490 kfree(cb);
5491}
5492
5493static void release_stripe_plug(struct mddev *mddev,
5494 struct stripe_head *sh)
5495{
5496 struct blk_plug_cb *blk_cb = blk_check_plugged(
5497 raid5_unplug, mddev,
5498 sizeof(struct raid5_plug_cb));
5499 struct raid5_plug_cb *cb;
5500
5501 if (!blk_cb) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005502 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005503 return;
5504 }
5505
5506 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
5507
Shaohua Li566c09c2013-11-14 15:16:17 +11005508 if (cb->list.next == NULL) {
5509 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10005510 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11005511 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5512 INIT_LIST_HEAD(cb->temp_inactive_list + i);
5513 }
Shaohua Li8811b592012-08-02 08:33:00 +10005514
5515 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
5516 list_add_tail(&sh->lru, &cb->list);
5517 else
Shaohua Li6d036f72015-08-13 14:31:57 -07005518 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005519}
5520
Shaohua Li620125f2012-10-11 13:49:05 +11005521static void make_discard_request(struct mddev *mddev, struct bio *bi)
5522{
5523 struct r5conf *conf = mddev->private;
5524 sector_t logical_sector, last_sector;
5525 struct stripe_head *sh;
Shaohua Li620125f2012-10-11 13:49:05 +11005526 int stripe_sectors;
5527
5528 if (mddev->reshape_position != MaxSector)
5529 /* Skip discard while reshape is happening */
5530 return;
5531
Yufen Yuc911c462020-07-18 05:29:07 -04005532 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
Guoqing Jiangb0f01ec2019-09-03 11:41:03 +02005533 last_sector = bio_end_sector(bi);
Shaohua Li620125f2012-10-11 13:49:05 +11005534
5535 bi->bi_next = NULL;
Shaohua Li620125f2012-10-11 13:49:05 +11005536
5537 stripe_sectors = conf->chunk_sectors *
5538 (conf->raid_disks - conf->max_degraded);
5539 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5540 stripe_sectors);
5541 sector_div(last_sector, stripe_sectors);
5542
5543 logical_sector *= conf->chunk_sectors;
5544 last_sector *= conf->chunk_sectors;
5545
5546 for (; logical_sector < last_sector;
Yufen Yuc911c462020-07-18 05:29:07 -04005547 logical_sector += RAID5_STRIPE_SECTORS(conf)) {
Shaohua Li620125f2012-10-11 13:49:05 +11005548 DEFINE_WAIT(w);
5549 int d;
5550 again:
Shaohua Li6d036f72015-08-13 14:31:57 -07005551 sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
Shaohua Li620125f2012-10-11 13:49:05 +11005552 prepare_to_wait(&conf->wait_for_overlap, &w,
5553 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005554 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5555 if (test_bit(STRIPE_SYNCING, &sh->state)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005556 raid5_release_stripe(sh);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005557 schedule();
5558 goto again;
5559 }
5560 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11005561 spin_lock_irq(&sh->stripe_lock);
5562 for (d = 0; d < conf->raid_disks; d++) {
5563 if (d == sh->pd_idx || d == sh->qd_idx)
5564 continue;
5565 if (sh->dev[d].towrite || sh->dev[d].toread) {
5566 set_bit(R5_Overlap, &sh->dev[d].flags);
5567 spin_unlock_irq(&sh->stripe_lock);
Shaohua Li6d036f72015-08-13 14:31:57 -07005568 raid5_release_stripe(sh);
Shaohua Li620125f2012-10-11 13:49:05 +11005569 schedule();
5570 goto again;
5571 }
5572 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11005573 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11005574 finish_wait(&conf->wait_for_overlap, &w);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005575 sh->overwrite_disks = 0;
Shaohua Li620125f2012-10-11 13:49:05 +11005576 for (d = 0; d < conf->raid_disks; d++) {
5577 if (d == sh->pd_idx || d == sh->qd_idx)
5578 continue;
5579 sh->dev[d].towrite = bi;
5580 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
NeilBrown016c76a2017-03-15 14:05:13 +11005581 bio_inc_remaining(bi);
NeilBrown49728052017-03-15 14:05:12 +11005582 md_write_inc(mddev, bi);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005583 sh->overwrite_disks++;
Shaohua Li620125f2012-10-11 13:49:05 +11005584 }
5585 spin_unlock_irq(&sh->stripe_lock);
5586 if (conf->mddev->bitmap) {
5587 for (d = 0;
5588 d < conf->raid_disks - conf->max_degraded;
5589 d++)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005590 md_bitmap_startwrite(mddev->bitmap,
5591 sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04005592 RAID5_STRIPE_SECTORS(conf),
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005593 0);
Shaohua Li620125f2012-10-11 13:49:05 +11005594 sh->bm_seq = conf->seq_flush + 1;
5595 set_bit(STRIPE_BIT_DELAY, &sh->state);
5596 }
5597
5598 set_bit(STRIPE_HANDLE, &sh->state);
5599 clear_bit(STRIPE_DELAYED, &sh->state);
5600 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5601 atomic_inc(&conf->preread_active_stripes);
5602 release_stripe_plug(mddev, sh);
5603 }
5604
NeilBrown016c76a2017-03-15 14:05:13 +11005605 bio_endio(bi);
Shaohua Li620125f2012-10-11 13:49:05 +11005606}
5607
NeilBrowncc27b0c2017-06-05 16:49:39 +10005608static bool raid5_make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609{
NeilBrownd1688a62011-10-11 16:49:52 +11005610 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11005611 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 sector_t new_sector;
5613 sector_t logical_sector, last_sector;
5614 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01005615 const int rw = bio_data_dir(bi);
Shaohua Li27c0f682014-04-09 11:25:47 +08005616 DEFINE_WAIT(w);
5617 bool do_prepare;
Song Liu3bddb7f2016-11-18 16:46:50 -08005618 bool do_flush = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
Jens Axboe1eff9d32016-08-05 15:35:16 -06005620 if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01005621 int ret = log_handle_flush_request(conf, bi);
Shaohua Li828cbe92015-09-02 13:49:49 -07005622
5623 if (ret == 0)
NeilBrowncc27b0c2017-06-05 16:49:39 +10005624 return true;
Shaohua Li828cbe92015-09-02 13:49:49 -07005625 if (ret == -ENODEV) {
David Jeffery775d7832019-09-16 13:15:14 -04005626 if (md_flush_request(mddev, bi))
5627 return true;
Shaohua Li828cbe92015-09-02 13:49:49 -07005628 }
5629 /* ret == -EAGAIN, fallback */
Song Liu3bddb7f2016-11-18 16:46:50 -08005630 /*
5631 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
5632 * we need to flush journal device
5633 */
5634 do_flush = bi->bi_opf & REQ_PREFLUSH;
NeilBrowne5dcdd82005-09-09 16:23:41 -07005635 }
5636
NeilBrowncc27b0c2017-06-05 16:49:39 +10005637 if (!md_write_start(mddev, bi))
5638 return false;
Eric Mei9ffc8f72015-03-18 23:39:11 -06005639 /*
5640 * If array is degraded, better not do chunk aligned read because
5641 * later we might have to read it again in order to reconstruct
5642 * data on failed drives.
5643 */
5644 if (rw == READ && mddev->degraded == 0 &&
Ming Lin7ef6b122015-05-06 22:51:24 -07005645 mddev->reshape_position == MaxSector) {
5646 bi = chunk_aligned_read(mddev, bi);
5647 if (!bi)
NeilBrowncc27b0c2017-06-05 16:49:39 +10005648 return true;
Ming Lin7ef6b122015-05-06 22:51:24 -07005649 }
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08005650
Mike Christie796a5cf2016-06-05 14:32:07 -05005651 if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
Shaohua Li620125f2012-10-11 13:49:05 +11005652 make_discard_request(mddev, bi);
NeilBrowncc27b0c2017-06-05 16:49:39 +10005653 md_write_end(mddev);
5654 return true;
Shaohua Li620125f2012-10-11 13:49:05 +11005655 }
5656
Yufen Yuc911c462020-07-18 05:29:07 -04005657 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005658 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 bi->bi_next = NULL;
NeilBrown06d91a52005-06-21 17:17:12 -07005660
Shaohua Li27c0f682014-04-09 11:25:47 +08005661 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
Yufen Yuc911c462020-07-18 05:29:07 -04005662 for (; logical_sector < last_sector; logical_sector += RAID5_STRIPE_SECTORS(conf)) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005663 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10005664 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08005665
Shaohua Li27c0f682014-04-09 11:25:47 +08005666 do_prepare = false;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005667 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10005668 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11005669 previous = 0;
Shaohua Li27c0f682014-04-09 11:25:47 +08005670 if (do_prepare)
5671 prepare_to_wait(&conf->wait_for_overlap, &w,
5672 TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005673 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11005674 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08005675 * 64bit on a 32bit platform, and so it might be
5676 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02005677 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08005678 * the lock is dropped, so once we get a reference
5679 * to the stripe that we think it is, we will have
5680 * to check again.
5681 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005682 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005683 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005684 ? logical_sector < conf->reshape_progress
5685 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005686 previous = 1;
5687 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10005688 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005689 ? logical_sector < conf->reshape_safe
5690 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08005691 spin_unlock_irq(&conf->device_lock);
5692 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005693 do_prepare = true;
NeilBrownb578d552006-03-27 01:18:12 -08005694 goto retry;
5695 }
5696 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005697 spin_unlock_irq(&conf->device_lock);
5698 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005699
NeilBrown112bf892009-03-31 14:39:38 +11005700 new_sector = raid5_compute_sector(conf, logical_sector,
5701 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11005702 &dd_idx, NULL);
Shaohua Li849674e2016-01-20 13:52:20 -08005703 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10005704 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 (unsigned long long)logical_sector);
5706
Shaohua Li6d036f72015-08-13 14:31:57 -07005707 sh = raid5_get_active_stripe(conf, new_sector, previous,
Jens Axboe1eff9d32016-08-05 15:35:16 -06005708 (bi->bi_opf & REQ_RAHEAD), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11005710 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005711 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08005712 * stripe, so we must do the range check again.
5713 * Expansion could still move past after this
5714 * test, but as we are holding a reference to
5715 * 'sh', we know that if that happens,
5716 * STRIPE_EXPANDING will get set and the expansion
5717 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005718 */
5719 int must_retry = 0;
5720 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005721 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11005722 ? logical_sector >= conf->reshape_progress
5723 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005724 /* mismatch, need to try again */
5725 must_retry = 1;
5726 spin_unlock_irq(&conf->device_lock);
5727 if (must_retry) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005728 raid5_release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07005729 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005730 do_prepare = true;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005731 goto retry;
5732 }
5733 }
NeilBrownc46501b2013-08-27 15:52:13 +10005734 if (read_seqcount_retry(&conf->gen_lock, seq)) {
5735 /* Might have got the wrong stripe_head
5736 * by accident
5737 */
Shaohua Li6d036f72015-08-13 14:31:57 -07005738 raid5_release_stripe(sh);
NeilBrownc46501b2013-08-27 15:52:13 +10005739 goto retry;
5740 }
NeilBrowne62e58a2009-07-01 13:15:35 +10005741
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005742 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
shli@kernel.orgda41ba62014-12-15 12:57:03 +11005743 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005744 /* Stripe is busy expanding or
5745 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 * and wait a while
5747 */
NeilBrown482c0832011-04-18 18:25:42 +10005748 md_wakeup_thread(mddev->thread);
Shaohua Li6d036f72015-08-13 14:31:57 -07005749 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005751 do_prepare = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 goto retry;
5753 }
Song Liu3bddb7f2016-11-18 16:46:50 -08005754 if (do_flush) {
5755 set_bit(STRIPE_R5C_PREFLUSH, &sh->state);
5756 /* we only need flush for one stripe */
5757 do_flush = false;
5758 }
5759
Guoqing Jiang1684e972020-06-16 11:25:52 +02005760 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrown6ed30032008-02-06 01:40:00 -08005761 clear_bit(STRIPE_DELAYED, &sh->state);
shli@kernel.org59fc6302014-12-15 12:57:03 +11005762 if ((!sh->batch_head || sh == sh->batch_head) &&
Jens Axboe1eff9d32016-08-05 15:35:16 -06005763 (bi->bi_opf & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11005764 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5765 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10005766 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 } else {
5768 /* cannot get stripe for read-ahead, just give-up */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02005769 bi->bi_status = BLK_STS_IOERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 break;
5771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 }
Shaohua Li27c0f682014-04-09 11:25:47 +08005773 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown7c13edc2011-04-18 18:25:43 +10005774
NeilBrown49728052017-03-15 14:05:12 +11005775 if (rw == WRITE)
5776 md_write_end(mddev);
NeilBrown016c76a2017-03-15 14:05:13 +11005777 bio_endio(bi);
NeilBrowncc27b0c2017-06-05 16:49:39 +10005778 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779}
5780
NeilBrownfd01b882011-10-11 16:47:53 +11005781static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11005782
NeilBrownfd01b882011-10-11 16:47:53 +11005783static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784{
NeilBrown52c03292006-06-26 00:27:43 -07005785 /* reshaping is quite different to recovery/resync so it is
5786 * handled quite separately ... here.
5787 *
5788 * On each call to sync_request, we gather one chunk worth of
5789 * destination stripes and flag them as expanding.
5790 * Then we find all the source stripes and request reads.
5791 * As the reads complete, handle_stripe will copy the data
5792 * into the destination stripe and release that stripe.
5793 */
NeilBrownd1688a62011-10-11 16:49:52 +11005794 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 struct stripe_head *sh;
NeilBrowndb0505d2017-10-17 16:18:36 +11005796 struct md_rdev *rdev;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005797 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08005798 int raid_disks = conf->previous_raid_disks;
5799 int data_disks = raid_disks - conf->max_degraded;
5800 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07005801 int i;
5802 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11005803 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11005804 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11005805 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11005806 struct list_head stripes;
NeilBrown92140482015-07-06 12:28:45 +10005807 sector_t retn;
NeilBrown52c03292006-06-26 00:27:43 -07005808
NeilBrownfef9c612009-03-31 15:16:46 +11005809 if (sector_nr == 0) {
5810 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10005811 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005812 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
5813 sector_nr = raid5_size(mddev, 0, 0)
5814 - conf->reshape_progress;
NeilBrown6cbd8142015-07-24 13:30:32 +10005815 } else if (mddev->reshape_backwards &&
5816 conf->reshape_progress == MaxSector) {
5817 /* shouldn't happen, but just in case, finish up.*/
5818 sector_nr = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +10005819 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005820 conf->reshape_progress > 0)
5821 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005822 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005823 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11005824 mddev->curr_resync_completed = sector_nr;
Junxiao Bie1a86db2020-07-14 16:10:26 -07005825 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrownfef9c612009-03-31 15:16:46 +11005826 *skipped = 1;
NeilBrown92140482015-07-06 12:28:45 +10005827 retn = sector_nr;
5828 goto finish;
NeilBrownfef9c612009-03-31 15:16:46 +11005829 }
NeilBrown52c03292006-06-26 00:27:43 -07005830 }
5831
NeilBrown7a661382009-03-31 15:21:40 +11005832 /* We need to process a full chunk at a time.
5833 * If old and new chunk sizes differ, we need to process the
5834 * largest of these
5835 */
NeilBrown3cb5edf2015-07-15 17:24:17 +10005836
5837 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
NeilBrown7a661382009-03-31 15:21:40 +11005838
NeilBrownb5254dd2012-05-21 09:27:01 +10005839 /* We update the metadata at least every 10 seconds, or when
5840 * the data about to be copied would over-write the source of
5841 * the data at the front of the range. i.e. one new_stripe
5842 * along from reshape_progress new_maps to after where
5843 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07005844 */
NeilBrownfef9c612009-03-31 15:16:46 +11005845 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005846 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11005847 readpos = conf->reshape_progress;
5848 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005849 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08005850 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10005851 if (mddev->reshape_backwards) {
NeilBrownc74c0d72015-07-15 17:54:15 +10005852 BUG_ON(writepos < reshape_sectors);
5853 writepos -= reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11005854 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11005855 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11005856 } else {
NeilBrown7a661382009-03-31 15:21:40 +11005857 writepos += reshape_sectors;
NeilBrownc74c0d72015-07-15 17:54:15 +10005858 /* readpos and safepos are worst-case calculations.
5859 * A negative number is overly pessimistic, and causes
5860 * obvious problems for unsigned storage. So clip to 0.
5861 */
NeilBrowned37d832009-05-27 21:39:05 +10005862 readpos -= min_t(sector_t, reshape_sectors, readpos);
5863 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11005864 }
NeilBrown52c03292006-06-26 00:27:43 -07005865
NeilBrownb5254dd2012-05-21 09:27:01 +10005866 /* Having calculated the 'writepos' possibly use it
5867 * to set 'stripe_addr' which is where we will write to.
5868 */
5869 if (mddev->reshape_backwards) {
5870 BUG_ON(conf->reshape_progress == 0);
5871 stripe_addr = writepos;
5872 BUG_ON((mddev->dev_sectors &
5873 ~((sector_t)reshape_sectors - 1))
5874 - reshape_sectors - stripe_addr
5875 != sector_nr);
5876 } else {
5877 BUG_ON(writepos != sector_nr + reshape_sectors);
5878 stripe_addr = sector_nr;
5879 }
5880
NeilBrownc8f517c2009-03-31 15:28:40 +11005881 /* 'writepos' is the most advanced device address we might write.
5882 * 'readpos' is the least advanced device address we might read.
5883 * 'safepos' is the least address recorded in the metadata as having
5884 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10005885 * If there is a min_offset_diff, these are adjusted either by
5886 * increasing the safepos/readpos if diff is negative, or
5887 * increasing writepos if diff is positive.
5888 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11005889 * ensure safety in the face of a crash - that must be done by userspace
5890 * making a backup of the data. So in that case there is no particular
5891 * rush to update metadata.
5892 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5893 * update the metadata to advance 'safepos' to match 'readpos' so that
5894 * we can be safe in the event of a crash.
5895 * So we insist on updating metadata if safepos is behind writepos and
5896 * readpos is beyond writepos.
5897 * In any case, update the metadata every 10 seconds.
5898 * Maybe that number should be configurable, but I'm not sure it is
5899 * worth it.... maybe it could be a multiple of safemode_delay???
5900 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005901 if (conf->min_offset_diff < 0) {
5902 safepos += -conf->min_offset_diff;
5903 readpos += -conf->min_offset_diff;
5904 } else
5905 writepos += conf->min_offset_diff;
5906
NeilBrown2c810cd2012-05-21 09:27:00 +10005907 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11005908 ? (safepos > writepos && readpos < writepos)
5909 : (safepos < writepos && readpos > writepos)) ||
5910 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07005911 /* Cannot proceed until we've updated the superblock... */
5912 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005913 atomic_read(&conf->reshape_stripes)==0
5914 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5915 if (atomic_read(&conf->reshape_stripes) != 0)
5916 return 0;
NeilBrownfef9c612009-03-31 15:16:46 +11005917 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005918 mddev->curr_resync_completed = sector_nr;
NeilBrowndb0505d2017-10-17 16:18:36 +11005919 if (!mddev->reshape_backwards)
5920 /* Can update recovery_offset */
5921 rdev_for_each(rdev, mddev)
5922 if (rdev->raid_disk >= 0 &&
5923 !test_bit(Journal, &rdev->flags) &&
5924 !test_bit(In_sync, &rdev->flags) &&
5925 rdev->recovery_offset < sector_nr)
5926 rdev->recovery_offset = sector_nr;
5927
NeilBrownc8f517c2009-03-31 15:28:40 +11005928 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08005929 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown52c03292006-06-26 00:27:43 -07005930 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08005931 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11005932 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5933 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5934 return 0;
NeilBrown52c03292006-06-26 00:27:43 -07005935 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005936 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07005937 spin_unlock_irq(&conf->device_lock);
5938 wake_up(&conf->wait_for_overlap);
Junxiao Bie1a86db2020-07-14 16:10:26 -07005939 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown52c03292006-06-26 00:27:43 -07005940 }
5941
NeilBrownab69ae12009-03-31 15:26:47 +11005942 INIT_LIST_HEAD(&stripes);
Yufen Yuc911c462020-07-18 05:29:07 -04005943 for (i = 0; i < reshape_sectors; i += RAID5_STRIPE_SECTORS(conf)) {
NeilBrown52c03292006-06-26 00:27:43 -07005944 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10005945 int skipped_disk = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07005946 sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005947 set_bit(STRIPE_EXPANDING, &sh->state);
5948 atomic_inc(&conf->reshape_stripes);
5949 /* If any of this stripe is beyond the end of the old
5950 * array, then we need to zero those blocks
5951 */
5952 for (j=sh->disks; j--;) {
5953 sector_t s;
5954 if (j == sh->pd_idx)
5955 continue;
NeilBrownf4168852007-02-28 20:11:53 -08005956 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11005957 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08005958 continue;
Shaohua Li6d036f72015-08-13 14:31:57 -07005959 s = raid5_compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11005960 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10005961 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07005962 continue;
5963 }
Yufen Yuc911c462020-07-18 05:29:07 -04005964 memset(page_address(sh->dev[j].page), 0, RAID5_STRIPE_SIZE(conf));
NeilBrown52c03292006-06-26 00:27:43 -07005965 set_bit(R5_Expanded, &sh->dev[j].flags);
5966 set_bit(R5_UPTODATE, &sh->dev[j].flags);
5967 }
NeilBrowna9f326e2009-09-23 18:06:41 +10005968 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07005969 set_bit(STRIPE_EXPAND_READY, &sh->state);
5970 set_bit(STRIPE_HANDLE, &sh->state);
5971 }
NeilBrownab69ae12009-03-31 15:26:47 +11005972 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07005973 }
5974 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005975 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11005976 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005977 else
NeilBrown7a661382009-03-31 15:21:40 +11005978 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07005979 spin_unlock_irq(&conf->device_lock);
5980 /* Ok, those stripe are ready. We can start scheduling
5981 * reads on the source stripes.
5982 * The source stripes are determined by mapping the first and last
5983 * block on the destination stripes.
5984 */
NeilBrown52c03292006-06-26 00:27:43 -07005985 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11005986 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11005987 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07005988 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10005989 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10005990 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11005991 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11005992 if (last_sector >= mddev->dev_sectors)
5993 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07005994 while (first_sector <= last_sector) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005995 sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005996 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
5997 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07005998 raid5_release_stripe(sh);
Yufen Yuc911c462020-07-18 05:29:07 -04005999 first_sector += RAID5_STRIPE_SECTORS(conf);
NeilBrown52c03292006-06-26 00:27:43 -07006000 }
NeilBrownab69ae12009-03-31 15:26:47 +11006001 /* Now that the sources are clearly marked, we can release
6002 * the destination stripes
6003 */
6004 while (!list_empty(&stripes)) {
6005 sh = list_entry(stripes.next, struct stripe_head, lru);
6006 list_del_init(&sh->lru);
Shaohua Li6d036f72015-08-13 14:31:57 -07006007 raid5_release_stripe(sh);
NeilBrownab69ae12009-03-31 15:26:47 +11006008 }
NeilBrownc6207272008-02-06 01:39:52 -08006009 /* If this takes us to the resync_max point where we have to pause,
6010 * then we need to write out the superblock.
6011 */
NeilBrown7a661382009-03-31 15:21:40 +11006012 sector_nr += reshape_sectors;
NeilBrown92140482015-07-06 12:28:45 +10006013 retn = reshape_sectors;
6014finish:
NeilBrownc5e19d92015-07-17 12:06:02 +10006015 if (mddev->curr_resync_completed > mddev->resync_max ||
6016 (sector_nr - mddev->curr_resync_completed) * 2
NeilBrownc03f6a12009-04-17 11:06:30 +10006017 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08006018 /* Cannot proceed until we've updated the superblock... */
6019 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11006020 atomic_read(&conf->reshape_stripes) == 0
6021 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6022 if (atomic_read(&conf->reshape_stripes) != 0)
6023 goto ret;
NeilBrownfef9c612009-03-31 15:16:46 +11006024 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11006025 mddev->curr_resync_completed = sector_nr;
NeilBrowndb0505d2017-10-17 16:18:36 +11006026 if (!mddev->reshape_backwards)
6027 /* Can update recovery_offset */
6028 rdev_for_each(rdev, mddev)
6029 if (rdev->raid_disk >= 0 &&
6030 !test_bit(Journal, &rdev->flags) &&
6031 !test_bit(In_sync, &rdev->flags) &&
6032 rdev->recovery_offset < sector_nr)
6033 rdev->recovery_offset = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11006034 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08006035 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownc6207272008-02-06 01:39:52 -08006036 md_wakeup_thread(mddev->thread);
6037 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08006038 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)
NeilBrownc91abf52013-11-19 12:02:01 +11006039 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6040 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6041 goto ret;
NeilBrownc6207272008-02-06 01:39:52 -08006042 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11006043 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08006044 spin_unlock_irq(&conf->device_lock);
6045 wake_up(&conf->wait_for_overlap);
Junxiao Bie1a86db2020-07-14 16:10:26 -07006046 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrownc6207272008-02-06 01:39:52 -08006047 }
NeilBrownc91abf52013-11-19 12:02:01 +11006048ret:
NeilBrown92140482015-07-06 12:28:45 +10006049 return retn;
NeilBrown52c03292006-06-26 00:27:43 -07006050}
6051
Shaohua Li849674e2016-01-20 13:52:20 -08006052static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
6053 int *skipped)
NeilBrown52c03292006-06-26 00:27:43 -07006054{
NeilBrownd1688a62011-10-11 16:49:52 +11006055 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07006056 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11006057 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11006058 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07006059 int still_degraded = 0;
6060 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
NeilBrown72626682005-09-09 16:23:54 -07006062 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11006064
NeilBrown29269552006-03-27 01:18:10 -08006065 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
6066 end_reshape(conf);
6067 return 0;
6068 }
NeilBrown72626682005-09-09 16:23:54 -07006069
6070 if (mddev->curr_resync < max_sector) /* aborted */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006071 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
6072 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07006073 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07006074 conf->fullsync = 0;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006075 md_bitmap_close_sync(mddev->bitmap);
NeilBrown72626682005-09-09 16:23:54 -07006076
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 return 0;
6078 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08006079
NeilBrown64bd6602009-08-03 10:59:58 +10006080 /* Allow raid5_quiesce to complete */
6081 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
6082
NeilBrown52c03292006-06-26 00:27:43 -07006083 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6084 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08006085
NeilBrownc6207272008-02-06 01:39:52 -08006086 /* No need to check resync_max as we never do more than one
6087 * stripe, and as resync_max will always be on a chunk boundary,
6088 * if the check in md_do_sync didn't fire, there is no chance
6089 * of overstepping resync_max here
6090 */
6091
NeilBrown16a53ec2006-06-26 00:27:38 -07006092 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 * to resync, then assert that we are finished, because there is
6094 * nothing we can do.
6095 */
NeilBrown3285edf2006-06-26 00:27:55 -07006096 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07006097 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11006098 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07006099 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 return rv;
6101 }
majianpeng6f608042013-04-24 11:42:41 +10006102 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
6103 !conf->fullsync &&
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006104 !md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
Yufen Yuc911c462020-07-18 05:29:07 -04006105 sync_blocks >= RAID5_STRIPE_SECTORS(conf)) {
NeilBrown72626682005-09-09 16:23:54 -07006106 /* we can skip this block, and probably more */
Yufen Yuc911c462020-07-18 05:29:07 -04006107 sync_blocks /= RAID5_STRIPE_SECTORS(conf);
NeilBrown72626682005-09-09 16:23:54 -07006108 *skipped = 1;
Yufen Yuc911c462020-07-18 05:29:07 -04006109 /* keep things rounded to whole stripes */
6110 return sync_blocks * RAID5_STRIPE_SECTORS(conf);
NeilBrown72626682005-09-09 16:23:54 -07006111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006113 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
NeilBrownb47490c2008-02-06 01:39:50 -08006114
Shaohua Li6d036f72015-08-13 14:31:57 -07006115 sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 if (sh == NULL) {
Shaohua Li6d036f72015-08-13 14:31:57 -07006117 sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07006119 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08006121 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 }
NeilBrown16a53ec2006-06-26 00:27:38 -07006123 /* Need to check if array will still be degraded after recovery/resync
Eric Mei16d9cfa2015-01-06 09:35:02 -08006124 * Note in case of > 1 drive failures it's possible we're rebuilding
6125 * one drive while leaving another faulty drive in array.
NeilBrown16a53ec2006-06-26 00:27:38 -07006126 */
Eric Mei16d9cfa2015-01-06 09:35:02 -08006127 rcu_read_lock();
6128 for (i = 0; i < conf->raid_disks; i++) {
Mark Rutland6aa7de02017-10-23 14:07:29 -07006129 struct md_rdev *rdev = READ_ONCE(conf->disks[i].rdev);
Eric Mei16d9cfa2015-01-06 09:35:02 -08006130
6131 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
NeilBrown16a53ec2006-06-26 00:27:38 -07006132 still_degraded = 1;
Eric Mei16d9cfa2015-01-06 09:35:02 -08006133 }
6134 rcu_read_unlock();
NeilBrown16a53ec2006-06-26 00:27:38 -07006135
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006136 md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07006137
NeilBrown83206d62011-07-26 11:19:49 +10006138 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Eivind Sarto053f5b62014-06-09 17:06:19 -07006139 set_bit(STRIPE_HANDLE, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140
Shaohua Li6d036f72015-08-13 14:31:57 -07006141 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142
Yufen Yuc911c462020-07-18 05:29:07 -04006143 return RAID5_STRIPE_SECTORS(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144}
6145
NeilBrown0472a422017-03-15 14:05:13 +11006146static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio,
6147 unsigned int offset)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006148{
6149 /* We may not be able to submit a whole bio at once as there
6150 * may not be enough stripe_heads available.
6151 * We cannot pre-allocate enough stripe_heads as we may need
6152 * more than exist in the cache (if we allow ever large chunks).
6153 * So we do one stripe head at a time and record in
6154 * ->bi_hw_segments how many have been done.
6155 *
6156 * We *know* that this entire raid_bio is in one chunk, so
6157 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
6158 */
6159 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11006160 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006161 sector_t sector, logical_sector, last_sector;
6162 int scnt = 0;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006163 int handled = 0;
6164
Kent Overstreet4f024f32013-10-11 15:44:27 -07006165 logical_sector = raid_bio->bi_iter.bi_sector &
Yufen Yuc911c462020-07-18 05:29:07 -04006166 ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
NeilBrown112bf892009-03-31 14:39:38 +11006167 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11006168 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07006169 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006170
6171 for (; logical_sector < last_sector;
Yufen Yuc911c462020-07-18 05:29:07 -04006172 logical_sector += RAID5_STRIPE_SECTORS(conf),
6173 sector += RAID5_STRIPE_SECTORS(conf),
Neil Brown387bb172007-02-08 14:20:29 -08006174 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006175
NeilBrown0472a422017-03-15 14:05:13 +11006176 if (scnt < offset)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006177 /* already done this stripe */
6178 continue;
6179
Shaohua Li6d036f72015-08-13 14:31:57 -07006180 sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006181
6182 if (!sh) {
6183 /* failed to get a stripe - must wait */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006184 conf->retry_read_aligned = raid_bio;
NeilBrown0472a422017-03-15 14:05:13 +11006185 conf->retry_read_offset = scnt;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006186 return handled;
6187 }
6188
shli@kernel.orgda41ba62014-12-15 12:57:03 +11006189 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07006190 raid5_release_stripe(sh);
Neil Brown387bb172007-02-08 14:20:29 -08006191 conf->retry_read_aligned = raid_bio;
NeilBrown0472a422017-03-15 14:05:13 +11006192 conf->retry_read_offset = scnt;
Neil Brown387bb172007-02-08 14:20:29 -08006193 return handled;
6194 }
6195
majianpeng3f9e7c12012-07-31 10:04:21 +10006196 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07006197 handle_stripe(sh);
Shaohua Li6d036f72015-08-13 14:31:57 -07006198 raid5_release_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006199 handled++;
6200 }
NeilBrown016c76a2017-03-15 14:05:13 +11006201
6202 bio_endio(raid_bio);
6203
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006204 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10006205 wake_up(&conf->wait_for_quiescent);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006206 return handled;
6207}
6208
Shaohua Libfc90cb2013-08-29 15:40:32 +08006209static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11006210 struct r5worker *worker,
6211 struct list_head *temp_inactive_list)
Christoph Hellwigefcd4872019-04-04 18:56:16 +02006212 __releases(&conf->device_lock)
6213 __acquires(&conf->device_lock)
Shaohua Li46a06402012-08-02 08:33:15 +10006214{
6215 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11006216 int i, batch_size = 0, hash;
6217 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10006218
6219 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08006220 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10006221 batch[batch_size++] = sh;
6222
Shaohua Li566c09c2013-11-14 15:16:17 +11006223 if (batch_size == 0) {
6224 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6225 if (!list_empty(temp_inactive_list + i))
6226 break;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006227 if (i == NR_STRIPE_HASH_LOCKS) {
6228 spin_unlock_irq(&conf->device_lock);
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01006229 log_flush_stripe_to_raid(conf);
Shaohua Lia8c34f92015-09-02 13:49:46 -07006230 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006231 return batch_size;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006232 }
Shaohua Li566c09c2013-11-14 15:16:17 +11006233 release_inactive = true;
6234 }
Shaohua Li46a06402012-08-02 08:33:15 +10006235 spin_unlock_irq(&conf->device_lock);
6236
Shaohua Li566c09c2013-11-14 15:16:17 +11006237 release_inactive_stripe_list(conf, temp_inactive_list,
6238 NR_STRIPE_HASH_LOCKS);
6239
Shaohua Lia8c34f92015-09-02 13:49:46 -07006240 r5l_flush_stripe_to_raid(conf->log);
Shaohua Li566c09c2013-11-14 15:16:17 +11006241 if (release_inactive) {
6242 spin_lock_irq(&conf->device_lock);
6243 return 0;
6244 }
6245
Shaohua Li46a06402012-08-02 08:33:15 +10006246 for (i = 0; i < batch_size; i++)
6247 handle_stripe(batch[i]);
Artur Paszkiewiczff875732017-03-09 09:59:58 +01006248 log_write_stripe_run(conf);
Shaohua Li46a06402012-08-02 08:33:15 +10006249
6250 cond_resched();
6251
6252 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006253 for (i = 0; i < batch_size; i++) {
6254 hash = batch[i]->hash_lock_index;
6255 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
6256 }
Shaohua Li46a06402012-08-02 08:33:15 +10006257 return batch_size;
6258}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006259
Shaohua Li851c30c2013-08-28 14:30:16 +08006260static void raid5_do_work(struct work_struct *work)
6261{
6262 struct r5worker *worker = container_of(work, struct r5worker, work);
6263 struct r5worker_group *group = worker->group;
6264 struct r5conf *conf = group->conf;
NeilBrown16d997b2017-03-15 14:05:12 +11006265 struct mddev *mddev = conf->mddev;
Shaohua Li851c30c2013-08-28 14:30:16 +08006266 int group_id = group - conf->worker_groups;
6267 int handled;
6268 struct blk_plug plug;
6269
6270 pr_debug("+++ raid5worker active\n");
6271
6272 blk_start_plug(&plug);
6273 handled = 0;
6274 spin_lock_irq(&conf->device_lock);
6275 while (1) {
6276 int batch_size, released;
6277
Shaohua Li566c09c2013-11-14 15:16:17 +11006278 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006279
Shaohua Li566c09c2013-11-14 15:16:17 +11006280 batch_size = handle_active_stripes(conf, group_id, worker,
6281 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08006282 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08006283 if (!batch_size && !released)
6284 break;
6285 handled += batch_size;
NeilBrown16d997b2017-03-15 14:05:12 +11006286 wait_event_lock_irq(mddev->sb_wait,
6287 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags),
6288 conf->device_lock);
Shaohua Li851c30c2013-08-28 14:30:16 +08006289 }
6290 pr_debug("%d stripes handled\n", handled);
6291
6292 spin_unlock_irq(&conf->device_lock);
Ofer Heifetz7e96d552017-07-24 09:17:40 +03006293
Song Liu9c72a18e42017-08-24 09:53:59 -07006294 flush_deferred_bios(conf);
6295
6296 r5l_flush_stripe_to_raid(conf->log);
6297
Ofer Heifetz7e96d552017-07-24 09:17:40 +03006298 async_tx_issue_pending_all();
Shaohua Li851c30c2013-08-28 14:30:16 +08006299 blk_finish_plug(&plug);
6300
6301 pr_debug("--- raid5worker inactive\n");
6302}
6303
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304/*
6305 * This is our raid5 kernel thread.
6306 *
6307 * We scan the hash table for stripes which can be handled now.
6308 * During the scan, completed stripes are saved for us by the interrupt
6309 * handler, so that they will not have to wait for our next wakeup.
6310 */
Shaohua Li4ed87312012-10-11 13:34:00 +11006311static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312{
Shaohua Li4ed87312012-10-11 13:34:00 +11006313 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11006314 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006316 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317
Dan Williams45b42332007-07-09 11:56:43 -07006318 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319
6320 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006322 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 handled = 0;
6324 spin_lock_irq(&conf->device_lock);
6325 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006326 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08006327 int batch_size, released;
NeilBrown0472a422017-03-15 14:05:13 +11006328 unsigned int offset;
Shaohua Li773ca822013-08-27 17:50:39 +08006329
Shaohua Li566c09c2013-11-14 15:16:17 +11006330 released = release_stripe_list(conf, conf->temp_inactive_list);
NeilBrownedbe83a2015-02-26 12:47:56 +11006331 if (released)
6332 clear_bit(R5_DID_ALLOC, &conf->cache_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333
NeilBrown0021b7b2012-07-31 09:08:14 +02006334 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10006335 !list_empty(&conf->bitmap_list)) {
6336 /* Now is a good time to flush some bitmap updates */
6337 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08006338 spin_unlock_irq(&conf->device_lock);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006339 md_bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08006340 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10006341 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11006342 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07006343 }
NeilBrown0021b7b2012-07-31 09:08:14 +02006344 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07006345
NeilBrown0472a422017-03-15 14:05:13 +11006346 while ((bio = remove_bio_from_retry(conf, &offset))) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006347 int ok;
6348 spin_unlock_irq(&conf->device_lock);
NeilBrown0472a422017-03-15 14:05:13 +11006349 ok = retry_aligned_read(conf, bio, offset);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006350 spin_lock_irq(&conf->device_lock);
6351 if (!ok)
6352 break;
6353 handled++;
6354 }
6355
Shaohua Li566c09c2013-11-14 15:16:17 +11006356 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
6357 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08006358 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 break;
Shaohua Li46a06402012-08-02 08:33:15 +10006360 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361
Shaohua Li29530792016-12-08 15:48:19 -08006362 if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) {
Shaohua Li46a06402012-08-02 08:33:15 +10006363 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10006364 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10006365 spin_lock_irq(&conf->device_lock);
6366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 }
Dan Williams45b42332007-07-09 11:56:43 -07006368 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369
6370 spin_unlock_irq(&conf->device_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006371 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
6372 mutex_trylock(&conf->cache_size_mutex)) {
NeilBrownedbe83a2015-02-26 12:47:56 +11006373 grow_one_stripe(conf, __GFP_NOWARN);
6374 /* Set flag even if allocation failed. This helps
6375 * slow down allocation requests when mem is short
6376 */
6377 set_bit(R5_DID_ALLOC, &conf->cache_state);
NeilBrown2d5b5692015-07-06 12:49:23 +10006378 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380
Shaohua Li765d7042017-01-04 09:33:23 -08006381 flush_deferred_bios(conf);
6382
Shaohua Li0576b1c2015-08-13 14:32:00 -07006383 r5l_flush_stripe_to_raid(conf->log);
6384
Dan Williamsc9f21aa2008-07-23 12:05:51 -07006385 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006386 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387
Dan Williams45b42332007-07-09 11:56:43 -07006388 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389}
6390
NeilBrown3f294f42005-11-08 21:39:25 -08006391static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006392raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006393{
NeilBrown7b1485b2014-12-15 12:56:59 +11006394 struct r5conf *conf;
6395 int ret = 0;
6396 spin_lock(&mddev->lock);
6397 conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006398 if (conf)
NeilBrownedbe83a2015-02-26 12:47:56 +11006399 ret = sprintf(page, "%d\n", conf->min_nr_stripes);
NeilBrown7b1485b2014-12-15 12:56:59 +11006400 spin_unlock(&mddev->lock);
6401 return ret;
NeilBrown3f294f42005-11-08 21:39:25 -08006402}
6403
NeilBrownc41d4ac2010-06-01 19:37:24 +10006404int
NeilBrownfd01b882011-10-11 16:47:53 +11006405raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10006406{
Alexei Naberezhnov483cbbe2018-03-27 16:54:16 -07006407 int result = 0;
NeilBrownd1688a62011-10-11 16:49:52 +11006408 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10006409
6410 if (size <= 16 || size > 32768)
6411 return -EINVAL;
NeilBrown486f0642015-02-25 12:10:35 +11006412
NeilBrownedbe83a2015-02-26 12:47:56 +11006413 conf->min_nr_stripes = size;
NeilBrown2d5b5692015-07-06 12:49:23 +10006414 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006415 while (size < conf->max_nr_stripes &&
6416 drop_one_stripe(conf))
6417 ;
NeilBrown2d5b5692015-07-06 12:49:23 +10006418 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006419
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02006420 md_allow_write(mddev);
NeilBrown486f0642015-02-25 12:10:35 +11006421
NeilBrown2d5b5692015-07-06 12:49:23 +10006422 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006423 while (size > conf->max_nr_stripes)
Alexei Naberezhnov483cbbe2018-03-27 16:54:16 -07006424 if (!grow_one_stripe(conf, GFP_KERNEL)) {
6425 conf->min_nr_stripes = conf->max_nr_stripes;
6426 result = -ENOMEM;
NeilBrown486f0642015-02-25 12:10:35 +11006427 break;
Alexei Naberezhnov483cbbe2018-03-27 16:54:16 -07006428 }
NeilBrown2d5b5692015-07-06 12:49:23 +10006429 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006430
Alexei Naberezhnov483cbbe2018-03-27 16:54:16 -07006431 return result;
NeilBrownc41d4ac2010-06-01 19:37:24 +10006432}
6433EXPORT_SYMBOL(raid5_set_cache_size);
6434
NeilBrown3f294f42005-11-08 21:39:25 -08006435static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006436raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08006437{
NeilBrown67918752014-12-15 12:57:01 +11006438 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006439 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07006440 int err;
6441
NeilBrown3f294f42005-11-08 21:39:25 -08006442 if (len >= PAGE_SIZE)
6443 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006444 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08006445 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006446 err = mddev_lock(mddev);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006447 if (err)
6448 return err;
NeilBrown67918752014-12-15 12:57:01 +11006449 conf = mddev->private;
6450 if (!conf)
6451 err = -ENODEV;
6452 else
6453 err = raid5_set_cache_size(mddev, new);
6454 mddev_unlock(mddev);
6455
6456 return err ?: len;
NeilBrown3f294f42005-11-08 21:39:25 -08006457}
NeilBrown007583c2005-11-08 21:39:30 -08006458
NeilBrown96de1e62005-11-08 21:39:39 -08006459static struct md_sysfs_entry
6460raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
6461 raid5_show_stripe_cache_size,
6462 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08006463
6464static ssize_t
Markus Stockhausend06f1912014-12-15 12:57:05 +11006465raid5_show_rmw_level(struct mddev *mddev, char *page)
6466{
6467 struct r5conf *conf = mddev->private;
6468 if (conf)
6469 return sprintf(page, "%d\n", conf->rmw_level);
6470 else
6471 return 0;
6472}
6473
6474static ssize_t
6475raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
6476{
6477 struct r5conf *conf = mddev->private;
6478 unsigned long new;
6479
6480 if (!conf)
6481 return -ENODEV;
6482
6483 if (len >= PAGE_SIZE)
6484 return -EINVAL;
6485
6486 if (kstrtoul(page, 10, &new))
6487 return -EINVAL;
6488
6489 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
6490 return -EINVAL;
6491
6492 if (new != PARITY_DISABLE_RMW &&
6493 new != PARITY_ENABLE_RMW &&
6494 new != PARITY_PREFER_RMW)
6495 return -EINVAL;
6496
6497 conf->rmw_level = new;
6498 return len;
6499}
6500
6501static struct md_sysfs_entry
6502raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
6503 raid5_show_rmw_level,
6504 raid5_store_rmw_level);
6505
6506
6507static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006508raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006509{
NeilBrown7b1485b2014-12-15 12:56:59 +11006510 struct r5conf *conf;
6511 int ret = 0;
6512 spin_lock(&mddev->lock);
6513 conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006514 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006515 ret = sprintf(page, "%d\n", conf->bypass_threshold);
6516 spin_unlock(&mddev->lock);
6517 return ret;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006518}
6519
6520static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006521raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006522{
NeilBrown67918752014-12-15 12:57:01 +11006523 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006524 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006525 int err;
6526
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006527 if (len >= PAGE_SIZE)
6528 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006529 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006530 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006531
6532 err = mddev_lock(mddev);
6533 if (err)
6534 return err;
6535 conf = mddev->private;
6536 if (!conf)
6537 err = -ENODEV;
NeilBrownedbe83a2015-02-26 12:47:56 +11006538 else if (new > conf->min_nr_stripes)
NeilBrown67918752014-12-15 12:57:01 +11006539 err = -EINVAL;
6540 else
6541 conf->bypass_threshold = new;
6542 mddev_unlock(mddev);
6543 return err ?: len;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006544}
6545
6546static struct md_sysfs_entry
6547raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
6548 S_IRUGO | S_IWUSR,
6549 raid5_show_preread_threshold,
6550 raid5_store_preread_threshold);
6551
6552static ssize_t
Shaohua Lid592a992014-05-21 17:57:44 +08006553raid5_show_skip_copy(struct mddev *mddev, char *page)
6554{
NeilBrown7b1485b2014-12-15 12:56:59 +11006555 struct r5conf *conf;
6556 int ret = 0;
6557 spin_lock(&mddev->lock);
6558 conf = mddev->private;
Shaohua Lid592a992014-05-21 17:57:44 +08006559 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006560 ret = sprintf(page, "%d\n", conf->skip_copy);
6561 spin_unlock(&mddev->lock);
6562 return ret;
Shaohua Lid592a992014-05-21 17:57:44 +08006563}
6564
6565static ssize_t
6566raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
6567{
NeilBrown67918752014-12-15 12:57:01 +11006568 struct r5conf *conf;
Shaohua Lid592a992014-05-21 17:57:44 +08006569 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006570 int err;
6571
Shaohua Lid592a992014-05-21 17:57:44 +08006572 if (len >= PAGE_SIZE)
6573 return -EINVAL;
Shaohua Lid592a992014-05-21 17:57:44 +08006574 if (kstrtoul(page, 10, &new))
6575 return -EINVAL;
6576 new = !!new;
Shaohua Lid592a992014-05-21 17:57:44 +08006577
NeilBrown67918752014-12-15 12:57:01 +11006578 err = mddev_lock(mddev);
6579 if (err)
6580 return err;
6581 conf = mddev->private;
6582 if (!conf)
6583 err = -ENODEV;
6584 else if (new != conf->skip_copy) {
6585 mddev_suspend(mddev);
6586 conf->skip_copy = new;
6587 if (new)
Jan Karadc3b17c2017-02-02 15:56:50 +01006588 mddev->queue->backing_dev_info->capabilities |=
NeilBrown67918752014-12-15 12:57:01 +11006589 BDI_CAP_STABLE_WRITES;
6590 else
Jan Karadc3b17c2017-02-02 15:56:50 +01006591 mddev->queue->backing_dev_info->capabilities &=
NeilBrown67918752014-12-15 12:57:01 +11006592 ~BDI_CAP_STABLE_WRITES;
6593 mddev_resume(mddev);
6594 }
6595 mddev_unlock(mddev);
6596 return err ?: len;
Shaohua Lid592a992014-05-21 17:57:44 +08006597}
6598
6599static struct md_sysfs_entry
6600raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
6601 raid5_show_skip_copy,
6602 raid5_store_skip_copy);
6603
Shaohua Lid592a992014-05-21 17:57:44 +08006604static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006605stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006606{
NeilBrownd1688a62011-10-11 16:49:52 +11006607 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006608 if (conf)
6609 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
6610 else
6611 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08006612}
6613
NeilBrown96de1e62005-11-08 21:39:39 -08006614static struct md_sysfs_entry
6615raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08006616
Shaohua Lib7214202013-08-27 17:50:42 +08006617static ssize_t
6618raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
6619{
NeilBrown7b1485b2014-12-15 12:56:59 +11006620 struct r5conf *conf;
6621 int ret = 0;
6622 spin_lock(&mddev->lock);
6623 conf = mddev->private;
Shaohua Lib7214202013-08-27 17:50:42 +08006624 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006625 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
6626 spin_unlock(&mddev->lock);
6627 return ret;
Shaohua Lib7214202013-08-27 17:50:42 +08006628}
6629
majianpeng60aaf932013-11-14 15:16:20 +11006630static int alloc_thread_groups(struct r5conf *conf, int cnt,
6631 int *group_cnt,
majianpeng60aaf932013-11-14 15:16:20 +11006632 struct r5worker_group **worker_groups);
Shaohua Lib7214202013-08-27 17:50:42 +08006633static ssize_t
6634raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
6635{
NeilBrown67918752014-12-15 12:57:01 +11006636 struct r5conf *conf;
Shaohua Li7d5d7b52017-09-21 11:03:52 -07006637 unsigned int new;
Shaohua Lib7214202013-08-27 17:50:42 +08006638 int err;
majianpeng60aaf932013-11-14 15:16:20 +11006639 struct r5worker_group *new_groups, *old_groups;
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006640 int group_cnt;
Shaohua Lib7214202013-08-27 17:50:42 +08006641
6642 if (len >= PAGE_SIZE)
6643 return -EINVAL;
Shaohua Li7d5d7b52017-09-21 11:03:52 -07006644 if (kstrtouint(page, 10, &new))
6645 return -EINVAL;
6646 /* 8192 should be big enough */
6647 if (new > 8192)
Shaohua Lib7214202013-08-27 17:50:42 +08006648 return -EINVAL;
6649
NeilBrown67918752014-12-15 12:57:01 +11006650 err = mddev_lock(mddev);
Shaohua Lib7214202013-08-27 17:50:42 +08006651 if (err)
6652 return err;
NeilBrown67918752014-12-15 12:57:01 +11006653 conf = mddev->private;
6654 if (!conf)
6655 err = -ENODEV;
6656 else if (new != conf->worker_cnt_per_group) {
6657 mddev_suspend(mddev);
6658
6659 old_groups = conf->worker_groups;
6660 if (old_groups)
6661 flush_workqueue(raid5_wq);
6662
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006663 err = alloc_thread_groups(conf, new, &group_cnt, &new_groups);
NeilBrown67918752014-12-15 12:57:01 +11006664 if (!err) {
6665 spin_lock_irq(&conf->device_lock);
6666 conf->group_cnt = group_cnt;
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006667 conf->worker_cnt_per_group = new;
NeilBrown67918752014-12-15 12:57:01 +11006668 conf->worker_groups = new_groups;
6669 spin_unlock_irq(&conf->device_lock);
6670
6671 if (old_groups)
6672 kfree(old_groups[0].workers);
6673 kfree(old_groups);
6674 }
6675 mddev_resume(mddev);
6676 }
6677 mddev_unlock(mddev);
6678
6679 return err ?: len;
Shaohua Lib7214202013-08-27 17:50:42 +08006680}
6681
6682static struct md_sysfs_entry
6683raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
6684 raid5_show_group_thread_cnt,
6685 raid5_store_group_thread_cnt);
6686
NeilBrown007583c2005-11-08 21:39:30 -08006687static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08006688 &raid5_stripecache_size.attr,
6689 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006690 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08006691 &raid5_group_thread_cnt.attr,
Shaohua Lid592a992014-05-21 17:57:44 +08006692 &raid5_skip_copy.attr,
Markus Stockhausend06f1912014-12-15 12:57:05 +11006693 &raid5_rmw_level.attr,
Song Liu2c7da142016-11-17 15:24:41 -08006694 &r5c_journal_mode.attr,
Mariusz Dabrowskia596d082019-02-18 15:04:09 +01006695 &ppl_write_hint.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08006696 NULL,
6697};
NeilBrown007583c2005-11-08 21:39:30 -08006698static struct attribute_group raid5_attrs_group = {
6699 .name = NULL,
6700 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08006701};
6702
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006703static int alloc_thread_groups(struct r5conf *conf, int cnt, int *group_cnt,
majianpeng60aaf932013-11-14 15:16:20 +11006704 struct r5worker_group **worker_groups)
Shaohua Li851c30c2013-08-28 14:30:16 +08006705{
Shaohua Li566c09c2013-11-14 15:16:17 +11006706 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08006707 ssize_t size;
6708 struct r5worker *workers;
6709
Shaohua Li851c30c2013-08-28 14:30:16 +08006710 if (cnt == 0) {
majianpeng60aaf932013-11-14 15:16:20 +11006711 *group_cnt = 0;
6712 *worker_groups = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08006713 return 0;
6714 }
majianpeng60aaf932013-11-14 15:16:20 +11006715 *group_cnt = num_possible_nodes();
Shaohua Li851c30c2013-08-28 14:30:16 +08006716 size = sizeof(struct r5worker) * cnt;
Kees Cook6396bb22018-06-12 14:03:40 -07006717 workers = kcalloc(size, *group_cnt, GFP_NOIO);
6718 *worker_groups = kcalloc(*group_cnt, sizeof(struct r5worker_group),
6719 GFP_NOIO);
majianpeng60aaf932013-11-14 15:16:20 +11006720 if (!*worker_groups || !workers) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006721 kfree(workers);
majianpeng60aaf932013-11-14 15:16:20 +11006722 kfree(*worker_groups);
Shaohua Li851c30c2013-08-28 14:30:16 +08006723 return -ENOMEM;
6724 }
6725
majianpeng60aaf932013-11-14 15:16:20 +11006726 for (i = 0; i < *group_cnt; i++) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006727 struct r5worker_group *group;
6728
NeilBrown0c775d52013-11-25 11:12:43 +11006729 group = &(*worker_groups)[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08006730 INIT_LIST_HEAD(&group->handle_list);
Shaohua Li535ae4e2017-02-15 19:37:32 -08006731 INIT_LIST_HEAD(&group->loprio_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006732 group->conf = conf;
6733 group->workers = workers + i * cnt;
6734
6735 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11006736 struct r5worker *worker = group->workers + j;
6737 worker->group = group;
6738 INIT_WORK(&worker->work, raid5_do_work);
6739
6740 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
6741 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08006742 }
6743 }
6744
6745 return 0;
6746}
6747
6748static void free_thread_groups(struct r5conf *conf)
6749{
6750 if (conf->worker_groups)
6751 kfree(conf->worker_groups[0].workers);
6752 kfree(conf->worker_groups);
6753 conf->worker_groups = NULL;
6754}
6755
Dan Williams80c3a6c2009-03-17 18:10:40 -07006756static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11006757raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07006758{
NeilBrownd1688a62011-10-11 16:49:52 +11006759 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006760
6761 if (!sectors)
6762 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006763 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11006764 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11006765 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006766
NeilBrown3cb5edf2015-07-15 17:24:17 +10006767 sectors &= ~((sector_t)conf->chunk_sectors - 1);
6768 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006769 return sectors * (raid_disks - conf->max_degraded);
6770}
6771
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306772static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6773{
6774 safe_put_page(percpu->spare_page);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306775 percpu->spare_page = NULL;
Kent Overstreetb330e6a2019-03-11 23:31:06 -07006776 kvfree(percpu->scribble);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306777 percpu->scribble = NULL;
6778}
6779
6780static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6781{
Kent Overstreetb330e6a2019-03-11 23:31:06 -07006782 if (conf->level == 6 && !percpu->spare_page) {
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306783 percpu->spare_page = alloc_page(GFP_KERNEL);
Kent Overstreetb330e6a2019-03-11 23:31:06 -07006784 if (!percpu->spare_page)
6785 return -ENOMEM;
6786 }
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306787
Kent Overstreetb330e6a2019-03-11 23:31:06 -07006788 if (scribble_alloc(percpu,
6789 max(conf->raid_disks,
6790 conf->previous_raid_disks),
6791 max(conf->chunk_sectors,
6792 conf->prev_chunk_sectors)
Yufen Yuc911c462020-07-18 05:29:07 -04006793 / RAID5_STRIPE_SECTORS(conf))) {
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306794 free_scratch_buffer(conf, percpu);
6795 return -ENOMEM;
6796 }
6797
6798 return 0;
6799}
6800
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006801static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
6802{
6803 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
6804
6805 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
6806 return 0;
6807}
6808
NeilBrownd1688a62011-10-11 16:49:52 +11006809static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006810{
Dan Williams36d1c642009-07-14 11:48:22 -07006811 if (!conf->percpu)
6812 return;
6813
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006814 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Dan Williams36d1c642009-07-14 11:48:22 -07006815 free_percpu(conf->percpu);
6816}
6817
NeilBrownd1688a62011-10-11 16:49:52 +11006818static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10006819{
Song Liud7bd3982016-11-23 22:50:39 -08006820 int i;
6821
Artur Paszkiewiczff875732017-03-09 09:59:58 +01006822 log_exit(conf);
6823
Aliaksei Karaliou565e0452017-12-23 21:20:31 +03006824 unregister_shrinker(&conf->shrinker);
Shaohua Li851c30c2013-08-28 14:30:16 +08006825 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10006826 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07006827 raid5_free_percpu(conf);
Song Liud7bd3982016-11-23 22:50:39 -08006828 for (i = 0; i < conf->pool_size; i++)
6829 if (conf->disks[i].extra_page)
6830 put_page(conf->disks[i].extra_page);
Dan Williams95fc17a2009-07-31 12:39:15 +10006831 kfree(conf->disks);
Kent Overstreetafeee512018-05-20 18:25:52 -04006832 bioset_exit(&conf->bio_split);
Dan Williams95fc17a2009-07-31 12:39:15 +10006833 kfree(conf->stripe_hashtbl);
Shaohua Liaaf9f122017-03-03 22:06:12 -08006834 kfree(conf->pending_data);
Dan Williams95fc17a2009-07-31 12:39:15 +10006835 kfree(conf);
6836}
6837
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006838static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
Dan Williams36d1c642009-07-14 11:48:22 -07006839{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006840 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
Dan Williams36d1c642009-07-14 11:48:22 -07006841 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
6842
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006843 if (alloc_scratch_buffer(conf, percpu)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006844 pr_warn("%s: failed memory allocation for cpu%u\n",
6845 __func__, cpu);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006846 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006847 }
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006848 return 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006849}
Dan Williams36d1c642009-07-14 11:48:22 -07006850
NeilBrownd1688a62011-10-11 16:49:52 +11006851static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006852{
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306853 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006854
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306855 conf->percpu = alloc_percpu(struct raid5_percpu);
6856 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07006857 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006858
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006859 err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Shaohua Li27a353c2016-02-24 17:38:28 -08006860 if (!err) {
6861 conf->scribble_disks = max(conf->raid_disks,
6862 conf->previous_raid_disks);
6863 conf->scribble_sectors = max(conf->chunk_sectors,
6864 conf->prev_chunk_sectors);
6865 }
Dan Williams36d1c642009-07-14 11:48:22 -07006866 return err;
6867}
6868
NeilBrownedbe83a2015-02-26 12:47:56 +11006869static unsigned long raid5_cache_scan(struct shrinker *shrink,
6870 struct shrink_control *sc)
6871{
6872 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
NeilBrown2d5b5692015-07-06 12:49:23 +10006873 unsigned long ret = SHRINK_STOP;
6874
6875 if (mutex_trylock(&conf->cache_size_mutex)) {
6876 ret= 0;
NeilBrown49895bc2015-08-03 17:09:57 +10006877 while (ret < sc->nr_to_scan &&
6878 conf->max_nr_stripes > conf->min_nr_stripes) {
NeilBrown2d5b5692015-07-06 12:49:23 +10006879 if (drop_one_stripe(conf) == 0) {
6880 ret = SHRINK_STOP;
6881 break;
6882 }
6883 ret++;
6884 }
6885 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006886 }
6887 return ret;
6888}
6889
6890static unsigned long raid5_cache_count(struct shrinker *shrink,
6891 struct shrink_control *sc)
6892{
6893 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
6894
6895 if (conf->max_nr_stripes < conf->min_nr_stripes)
6896 /* unlikely, but not impossible */
6897 return 0;
6898 return conf->max_nr_stripes - conf->min_nr_stripes;
6899}
6900
NeilBrownd1688a62011-10-11 16:49:52 +11006901static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902{
NeilBrownd1688a62011-10-11 16:49:52 +11006903 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006904 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11006905 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10006907 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11006908 int i;
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006909 int group_cnt;
majianpeng60aaf932013-11-14 15:16:20 +11006910 struct r5worker_group *new_group;
Kent Overstreetafeee512018-05-20 18:25:52 -04006911 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912
NeilBrown91adb562009-03-31 14:39:39 +11006913 if (mddev->new_level != 5
6914 && mddev->new_level != 4
6915 && mddev->new_level != 6) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006916 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
6917 mdname(mddev), mddev->new_level);
NeilBrown91adb562009-03-31 14:39:39 +11006918 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919 }
NeilBrown91adb562009-03-31 14:39:39 +11006920 if ((mddev->new_level == 5
6921 && !algorithm_valid_raid5(mddev->new_layout)) ||
6922 (mddev->new_level == 6
6923 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006924 pr_warn("md/raid:%s: layout %d not supported\n",
6925 mdname(mddev), mddev->new_layout);
NeilBrown91adb562009-03-31 14:39:39 +11006926 return ERR_PTR(-EIO);
6927 }
6928 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006929 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
6930 mdname(mddev), mddev->raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006931 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11006932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933
Andre Noll664e7c42009-06-18 08:45:27 +10006934 if (!mddev->new_chunk_sectors ||
6935 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
6936 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006937 pr_warn("md/raid:%s: invalid chunk size %d\n",
6938 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11006939 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11006940 }
6941
NeilBrownd1688a62011-10-11 16:49:52 +11006942 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11006943 if (conf == NULL)
6944 goto abort;
Yufen Yuc911c462020-07-18 05:29:07 -04006945
Shaohua Liaaf9f122017-03-03 22:06:12 -08006946 INIT_LIST_HEAD(&conf->free_list);
6947 INIT_LIST_HEAD(&conf->pending_list);
Kees Cook6396bb22018-06-12 14:03:40 -07006948 conf->pending_data = kcalloc(PENDING_IO_MAX,
6949 sizeof(struct r5pending_data),
6950 GFP_KERNEL);
Shaohua Liaaf9f122017-03-03 22:06:12 -08006951 if (!conf->pending_data)
6952 goto abort;
6953 for (i = 0; i < PENDING_IO_MAX; i++)
6954 list_add(&conf->pending_data[i].sibling, &conf->free_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006955 /* Don't enable multi-threading by default*/
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006956 if (!alloc_thread_groups(conf, 0, &group_cnt, &new_group)) {
majianpeng60aaf932013-11-14 15:16:20 +11006957 conf->group_cnt = group_cnt;
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006958 conf->worker_cnt_per_group = 0;
majianpeng60aaf932013-11-14 15:16:20 +11006959 conf->worker_groups = new_group;
6960 } else
Shaohua Li851c30c2013-08-28 14:30:16 +08006961 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11006962 spin_lock_init(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10006963 seqcount_init(&conf->gen_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006964 mutex_init(&conf->cache_size_mutex);
Yuanhan Liub1b46482015-05-08 18:19:06 +10006965 init_waitqueue_head(&conf->wait_for_quiescent);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08006966 init_waitqueue_head(&conf->wait_for_stripe);
Dan Williamsf5efd452009-10-16 15:55:38 +11006967 init_waitqueue_head(&conf->wait_for_overlap);
6968 INIT_LIST_HEAD(&conf->handle_list);
Shaohua Li535ae4e2017-02-15 19:37:32 -08006969 INIT_LIST_HEAD(&conf->loprio_list);
Dan Williamsf5efd452009-10-16 15:55:38 +11006970 INIT_LIST_HEAD(&conf->hold_list);
6971 INIT_LIST_HEAD(&conf->delayed_list);
6972 INIT_LIST_HEAD(&conf->bitmap_list);
Shaohua Li773ca822013-08-27 17:50:39 +08006973 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11006974 atomic_set(&conf->active_stripes, 0);
6975 atomic_set(&conf->preread_active_stripes, 0);
6976 atomic_set(&conf->active_aligned_reads, 0);
Shaohua Li765d7042017-01-04 09:33:23 -08006977 spin_lock_init(&conf->pending_bios_lock);
6978 conf->batch_bio_dispatch = true;
6979 rdev_for_each(rdev, mddev) {
6980 if (test_bit(Journal, &rdev->flags))
6981 continue;
6982 if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
6983 conf->batch_bio_dispatch = false;
6984 break;
6985 }
6986 }
6987
Dan Williamsf5efd452009-10-16 15:55:38 +11006988 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11006989 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11006990
6991 conf->raid_disks = mddev->raid_disks;
6992 if (mddev->reshape_position == MaxSector)
6993 conf->previous_raid_disks = mddev->raid_disks;
6994 else
6995 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006996 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006997
Kees Cook6396bb22018-06-12 14:03:40 -07006998 conf->disks = kcalloc(max_disks, sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11006999 GFP_KERNEL);
Song Liud7bd3982016-11-23 22:50:39 -08007000
NeilBrown91adb562009-03-31 14:39:39 +11007001 if (!conf->disks)
7002 goto abort;
7003
Song Liud7bd3982016-11-23 22:50:39 -08007004 for (i = 0; i < max_disks; i++) {
7005 conf->disks[i].extra_page = alloc_page(GFP_KERNEL);
7006 if (!conf->disks[i].extra_page)
7007 goto abort;
7008 }
7009
Kent Overstreetafeee512018-05-20 18:25:52 -04007010 ret = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
7011 if (ret)
NeilBrowndd7a8f52017-04-05 14:05:51 +10007012 goto abort;
NeilBrown91adb562009-03-31 14:39:39 +11007013 conf->mddev = mddev;
7014
7015 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
7016 goto abort;
7017
Shaohua Li566c09c2013-11-14 15:16:17 +11007018 /* We init hash_locks[0] separately to that it can be used
7019 * as the reference lock in the spin_lock_nest_lock() call
7020 * in lock_all_device_hash_locks_irq in order to convince
7021 * lockdep that we know what we are doing.
7022 */
7023 spin_lock_init(conf->hash_locks);
7024 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
7025 spin_lock_init(conf->hash_locks + i);
7026
7027 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
7028 INIT_LIST_HEAD(conf->inactive_list + i);
7029
7030 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
7031 INIT_LIST_HEAD(conf->temp_inactive_list + i);
7032
Song Liu1e6d6902016-11-17 15:24:39 -08007033 atomic_set(&conf->r5c_cached_full_stripes, 0);
7034 INIT_LIST_HEAD(&conf->r5c_full_stripe_list);
7035 atomic_set(&conf->r5c_cached_partial_stripes, 0);
7036 INIT_LIST_HEAD(&conf->r5c_partial_stripe_list);
Shaohua Lie33fbb92017-02-10 16:18:09 -08007037 atomic_set(&conf->r5c_flushing_full_stripes, 0);
7038 atomic_set(&conf->r5c_flushing_partial_stripes, 0);
Song Liu1e6d6902016-11-17 15:24:39 -08007039
Dan Williams36d1c642009-07-14 11:48:22 -07007040 conf->level = mddev->new_level;
shli@kernel.org46d5b782014-12-15 12:57:02 +11007041 conf->chunk_sectors = mddev->new_chunk_sectors;
Dan Williams36d1c642009-07-14 11:48:22 -07007042 if (raid5_alloc_percpu(conf) != 0)
7043 goto abort;
7044
NeilBrown0c55e022010-05-03 14:09:02 +10007045 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11007046
NeilBrowndafb20f2012-03-19 12:46:39 +11007047 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11007048 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11007049 if (raid_disk >= max_disks
Shaohua Lif2076e72015-10-08 21:54:12 -07007050 || raid_disk < 0 || test_bit(Journal, &rdev->flags))
NeilBrown91adb562009-03-31 14:39:39 +11007051 continue;
7052 disk = conf->disks + raid_disk;
7053
NeilBrown17045f52011-12-23 10:17:53 +11007054 if (test_bit(Replacement, &rdev->flags)) {
7055 if (disk->replacement)
7056 goto abort;
7057 disk->replacement = rdev;
7058 } else {
7059 if (disk->rdev)
7060 goto abort;
7061 disk->rdev = rdev;
7062 }
NeilBrown91adb562009-03-31 14:39:39 +11007063
7064 if (test_bit(In_sync, &rdev->flags)) {
7065 char b[BDEVNAME_SIZE];
NeilBrowncc6167b2016-11-02 14:16:50 +11007066 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
7067 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05007068 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11007069 /* Cannot rely on bitmap to complete recovery */
7070 conf->fullsync = 1;
7071 }
7072
NeilBrown91adb562009-03-31 14:39:39 +11007073 conf->level = mddev->new_level;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11007074 if (conf->level == 6) {
NeilBrown91adb562009-03-31 14:39:39 +11007075 conf->max_degraded = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11007076 if (raid6_call.xor_syndrome)
7077 conf->rmw_level = PARITY_ENABLE_RMW;
7078 else
7079 conf->rmw_level = PARITY_DISABLE_RMW;
7080 } else {
NeilBrown91adb562009-03-31 14:39:39 +11007081 conf->max_degraded = 1;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11007082 conf->rmw_level = PARITY_ENABLE_RMW;
7083 }
NeilBrown91adb562009-03-31 14:39:39 +11007084 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11007085 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11007086 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10007087 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11007088 conf->prev_algo = mddev->layout;
NeilBrown5cac6bc2015-07-17 12:17:50 +10007089 } else {
7090 conf->prev_chunk_sectors = conf->chunk_sectors;
7091 conf->prev_algo = conf->algorithm;
NeilBrowne183eae2009-03-31 15:20:22 +11007092 }
NeilBrown91adb562009-03-31 14:39:39 +11007093
NeilBrownedbe83a2015-02-26 12:47:56 +11007094 conf->min_nr_stripes = NR_STRIPES;
Shaohua Liad5b0f72016-08-30 10:29:33 -07007095 if (mddev->reshape_position != MaxSector) {
7096 int stripes = max_t(int,
Yufen Yuc911c462020-07-18 05:29:07 -04007097 ((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4,
7098 ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4);
Shaohua Liad5b0f72016-08-30 10:29:33 -07007099 conf->min_nr_stripes = max(NR_STRIPES, stripes);
7100 if (conf->min_nr_stripes != NR_STRIPES)
NeilBrowncc6167b2016-11-02 14:16:50 +11007101 pr_info("md/raid:%s: force stripe size %d for reshape\n",
Shaohua Liad5b0f72016-08-30 10:29:33 -07007102 mdname(mddev), conf->min_nr_stripes);
7103 }
NeilBrownedbe83a2015-02-26 12:47:56 +11007104 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11007105 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li4bda5562013-11-14 15:16:17 +11007106 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
NeilBrownedbe83a2015-02-26 12:47:56 +11007107 if (grow_stripes(conf, conf->min_nr_stripes)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007108 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
7109 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11007110 goto abort;
7111 } else
NeilBrowncc6167b2016-11-02 14:16:50 +11007112 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory);
NeilBrownedbe83a2015-02-26 12:47:56 +11007113 /*
7114 * Losing a stripe head costs more than the time to refill it,
7115 * it reduces the queue depth and so can hurt throughput.
7116 * So set it rather large, scaled by number of devices.
7117 */
7118 conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
7119 conf->shrinker.scan_objects = raid5_cache_scan;
7120 conf->shrinker.count_objects = raid5_cache_count;
7121 conf->shrinker.batch = 128;
7122 conf->shrinker.flags = 0;
Chao Yu6a0f53f2016-09-20 10:33:57 +08007123 if (register_shrinker(&conf->shrinker)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007124 pr_warn("md/raid:%s: couldn't register shrinker.\n",
7125 mdname(mddev));
Chao Yu6a0f53f2016-09-20 10:33:57 +08007126 goto abort;
7127 }
NeilBrown91adb562009-03-31 14:39:39 +11007128
NeilBrown02326052012-07-03 15:56:52 +10007129 sprintf(pers_name, "raid%d", mddev->new_level);
7130 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11007131 if (!conf->thread) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007132 pr_warn("md/raid:%s: couldn't allocate thread.\n",
7133 mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11007134 goto abort;
7135 }
7136
7137 return conf;
7138
7139 abort:
7140 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10007141 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11007142 return ERR_PTR(-EIO);
7143 } else
7144 return ERR_PTR(-ENOMEM);
7145}
7146
NeilBrownc148ffd2009-11-13 17:47:00 +11007147static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
7148{
7149 switch (algo) {
7150 case ALGORITHM_PARITY_0:
7151 if (raid_disk < max_degraded)
7152 return 1;
7153 break;
7154 case ALGORITHM_PARITY_N:
7155 if (raid_disk >= raid_disks - max_degraded)
7156 return 1;
7157 break;
7158 case ALGORITHM_PARITY_0_6:
NeilBrownf72ffdd2014-09-30 14:23:59 +10007159 if (raid_disk == 0 ||
NeilBrownc148ffd2009-11-13 17:47:00 +11007160 raid_disk == raid_disks - 1)
7161 return 1;
7162 break;
7163 case ALGORITHM_LEFT_ASYMMETRIC_6:
7164 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7165 case ALGORITHM_LEFT_SYMMETRIC_6:
7166 case ALGORITHM_RIGHT_SYMMETRIC_6:
7167 if (raid_disk == raid_disks - 1)
7168 return 1;
7169 }
7170 return 0;
7171}
7172
Shaohua Li849674e2016-01-20 13:52:20 -08007173static int raid5_run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11007174{
NeilBrownd1688a62011-10-11 16:49:52 +11007175 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10007176 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11007177 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007178 struct md_rdev *rdev;
Shaohua Li713cf5a2015-08-13 14:32:03 -07007179 struct md_rdev *journal_dev = NULL;
NeilBrownc148ffd2009-11-13 17:47:00 +11007180 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11007181 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10007182 long long min_offset_diff = 0;
7183 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11007184
NeilBrowna415c0f2017-06-05 16:05:13 +10007185 if (mddev_init_writes_pending(mddev) < 0)
7186 return -ENOMEM;
7187
Andre Noll8c6ac8682009-06-18 08:48:06 +10007188 if (mddev->recovery_cp != MaxSector)
NeilBrowncc6167b2016-11-02 14:16:50 +11007189 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
7190 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10007191
7192 rdev_for_each(rdev, mddev) {
7193 long long diff;
Shaohua Li713cf5a2015-08-13 14:32:03 -07007194
Shaohua Lif2076e72015-10-08 21:54:12 -07007195 if (test_bit(Journal, &rdev->flags)) {
Shaohua Li713cf5a2015-08-13 14:32:03 -07007196 journal_dev = rdev;
Shaohua Lif2076e72015-10-08 21:54:12 -07007197 continue;
7198 }
NeilBrownb5254dd2012-05-21 09:27:01 +10007199 if (rdev->raid_disk < 0)
7200 continue;
7201 diff = (rdev->new_data_offset - rdev->data_offset);
7202 if (first) {
7203 min_offset_diff = diff;
7204 first = 0;
7205 } else if (mddev->reshape_backwards &&
7206 diff < min_offset_diff)
7207 min_offset_diff = diff;
7208 else if (!mddev->reshape_backwards &&
7209 diff > min_offset_diff)
7210 min_offset_diff = diff;
7211 }
7212
NeilBrown230b55f2017-10-17 14:24:09 +11007213 if ((test_bit(MD_HAS_JOURNAL, &mddev->flags) || journal_dev) &&
7214 (mddev->bitmap_info.offset || mddev->bitmap_info.file)) {
7215 pr_notice("md/raid:%s: array cannot have both journal and bitmap\n",
7216 mdname(mddev));
7217 return -EINVAL;
7218 }
7219
NeilBrownf6705572006-03-27 01:18:11 -08007220 if (mddev->reshape_position != MaxSector) {
7221 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10007222 * Difficulties arise if the stripe we would write to
7223 * next is at or after the stripe we would read from next.
7224 * For a reshape that changes the number of devices, this
7225 * is only possible for a very short time, and mdadm makes
7226 * sure that time appears to have past before assembling
7227 * the array. So we fail if that time hasn't passed.
7228 * For a reshape that keeps the number of devices the same
7229 * mdadm must be monitoring the reshape can keeping the
7230 * critical areas read-only and backed up. It will start
7231 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08007232 */
7233 sector_t here_new, here_old;
7234 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11007235 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrown05256d92015-07-15 17:36:21 +10007236 int chunk_sectors;
7237 int new_data_disks;
NeilBrownf6705572006-03-27 01:18:11 -08007238
Shaohua Li713cf5a2015-08-13 14:32:03 -07007239 if (journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007240 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
7241 mdname(mddev));
Shaohua Li713cf5a2015-08-13 14:32:03 -07007242 return -EINVAL;
7243 }
7244
NeilBrown88ce4932009-03-31 15:24:23 +11007245 if (mddev->new_level != mddev->level) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007246 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
7247 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007248 return -EINVAL;
7249 }
NeilBrownf6705572006-03-27 01:18:11 -08007250 old_disks = mddev->raid_disks - mddev->delta_disks;
7251 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08007252 * further up in new geometry must map after here in old
7253 * geometry.
NeilBrown05256d92015-07-15 17:36:21 +10007254 * If the chunk sizes are different, then as we perform reshape
7255 * in units of the largest of the two, reshape_position needs
7256 * be a multiple of the largest chunk size times new data disks.
NeilBrownf6705572006-03-27 01:18:11 -08007257 */
7258 here_new = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007259 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
7260 new_data_disks = mddev->raid_disks - max_degraded;
7261 if (sector_div(here_new, chunk_sectors * new_data_disks)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007262 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
7263 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007264 return -EINVAL;
7265 }
NeilBrown05256d92015-07-15 17:36:21 +10007266 reshape_offset = here_new * chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08007267 /* here_new is the stripe we will write to */
7268 here_old = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007269 sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
NeilBrownf4168852007-02-28 20:11:53 -08007270 /* here_old is the first stripe that we might need to read
7271 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10007272 if (mddev->delta_disks == 0) {
7273 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10007274 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10007275 * and taking constant backups.
7276 * mdadm always starts a situation like this in
7277 * readonly mode so it can take control before
7278 * allowing any writes. So just check for that.
7279 */
NeilBrownb5254dd2012-05-21 09:27:01 +10007280 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
7281 abs(min_offset_diff) >= mddev->new_chunk_sectors)
7282 /* not really in-place - so OK */;
7283 else if (mddev->ro == 0) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007284 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
7285 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10007286 return -EINVAL;
7287 }
NeilBrown2c810cd2012-05-21 09:27:00 +10007288 } else if (mddev->reshape_backwards
NeilBrown05256d92015-07-15 17:36:21 +10007289 ? (here_new * chunk_sectors + min_offset_diff <=
7290 here_old * chunk_sectors)
7291 : (here_new * chunk_sectors >=
7292 here_old * chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08007293 /* Reading from the same stripe as writing to - bad */
NeilBrowncc6167b2016-11-02 14:16:50 +11007294 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
7295 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007296 return -EINVAL;
7297 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007298 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007299 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08007300 } else {
NeilBrown91adb562009-03-31 14:39:39 +11007301 BUG_ON(mddev->level != mddev->new_level);
7302 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10007303 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11007304 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08007305 }
7306
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01007307 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) &&
7308 test_bit(MD_HAS_PPL, &mddev->flags)) {
7309 pr_warn("md/raid:%s: using journal device and PPL not allowed - disabling PPL\n",
7310 mdname(mddev));
7311 clear_bit(MD_HAS_PPL, &mddev->flags);
Pawel Baldysiakddc08822017-08-16 17:13:45 +02007312 clear_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags);
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01007313 }
7314
NeilBrown245f46c2009-03-31 14:39:39 +11007315 if (mddev->private == NULL)
7316 conf = setup_conf(mddev);
7317 else
7318 conf = mddev->private;
7319
NeilBrown91adb562009-03-31 14:39:39 +11007320 if (IS_ERR(conf))
7321 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08007322
Song Liu486b0f72016-08-19 15:34:01 -07007323 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
7324 if (!journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007325 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
7326 mdname(mddev));
Song Liu486b0f72016-08-19 15:34:01 -07007327 mddev->ro = 1;
7328 set_disk_ro(mddev->gendisk, 1);
7329 } else if (mddev->recovery_cp == MaxSector)
7330 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
Shaohua Li7dde2ad2015-10-08 21:54:10 -07007331 }
7332
NeilBrownb5254dd2012-05-21 09:27:01 +10007333 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11007334 mddev->thread = conf->thread;
7335 conf->thread = NULL;
7336 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007337
NeilBrown17045f52011-12-23 10:17:53 +11007338 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
7339 i++) {
7340 rdev = conf->disks[i].rdev;
7341 if (!rdev && conf->disks[i].replacement) {
7342 /* The replacement is all we have yet */
7343 rdev = conf->disks[i].replacement;
7344 conf->disks[i].replacement = NULL;
7345 clear_bit(Replacement, &rdev->flags);
7346 conf->disks[i].rdev = rdev;
7347 }
7348 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11007349 continue;
NeilBrown17045f52011-12-23 10:17:53 +11007350 if (conf->disks[i].replacement &&
7351 conf->reshape_progress != MaxSector) {
7352 /* replacements and reshape simply do not mix. */
NeilBrowncc6167b2016-11-02 14:16:50 +11007353 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
NeilBrown17045f52011-12-23 10:17:53 +11007354 goto abort;
7355 }
NeilBrown2f115882010-06-17 17:41:03 +10007356 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11007357 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10007358 continue;
7359 }
NeilBrownc148ffd2009-11-13 17:47:00 +11007360 /* This disc is not fully in-sync. However if it
7361 * just stored parity (beyond the recovery_offset),
7362 * when we don't need to be concerned about the
7363 * array being dirty.
7364 * When reshape goes 'backwards', we never have
7365 * partially completed devices, so we only need
7366 * to worry about reshape going forwards.
7367 */
7368 /* Hack because v0.91 doesn't store recovery_offset properly. */
7369 if (mddev->major_version == 0 &&
7370 mddev->minor_version > 90)
7371 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007372
NeilBrownc148ffd2009-11-13 17:47:00 +11007373 if (rdev->recovery_offset < reshape_offset) {
7374 /* We need to check old and new layout */
7375 if (!only_parity(rdev->raid_disk,
7376 conf->algorithm,
7377 conf->raid_disks,
7378 conf->max_degraded))
7379 continue;
7380 }
7381 if (!only_parity(rdev->raid_disk,
7382 conf->prev_algo,
7383 conf->previous_raid_disks,
7384 conf->max_degraded))
7385 continue;
7386 dirty_parity_disks++;
7387 }
NeilBrown91adb562009-03-31 14:39:39 +11007388
NeilBrown17045f52011-12-23 10:17:53 +11007389 /*
7390 * 0 for a fully functional array, 1 or 2 for a degraded array.
7391 */
Song Liu2e38a372017-01-24 10:45:30 -08007392 mddev->degraded = raid5_calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393
NeilBrown674806d2010-06-16 17:17:53 +10007394 if (has_failed(conf)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007395 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07007396 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007397 goto abort;
7398 }
7399
NeilBrown91adb562009-03-31 14:39:39 +11007400 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10007401 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11007402 mddev->resync_max_sectors = mddev->dev_sectors;
7403
NeilBrownc148ffd2009-11-13 17:47:00 +11007404 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07007405 mddev->recovery_cp != MaxSector) {
Artur Paszkiewicz4536bf9b2017-03-09 10:00:01 +01007406 if (test_bit(MD_HAS_PPL, &mddev->flags))
7407 pr_crit("md/raid:%s: starting dirty degraded array with PPL.\n",
7408 mdname(mddev));
7409 else if (mddev->ok_start_degraded)
NeilBrowncc6167b2016-11-02 14:16:50 +11007410 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
7411 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007412 else {
NeilBrowncc6167b2016-11-02 14:16:50 +11007413 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
7414 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007415 goto abort;
7416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417 }
7418
NeilBrowncc6167b2016-11-02 14:16:50 +11007419 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
7420 mdname(mddev), conf->level,
7421 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
7422 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423
7424 print_raid5_conf(conf);
7425
NeilBrownfef9c612009-03-31 15:16:46 +11007426 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11007427 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08007428 atomic_set(&conf->reshape_stripes, 0);
7429 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7430 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7431 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7432 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7433 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007434 "reshape");
Aditya Pakkie406f122019-03-04 16:48:54 -06007435 if (!mddev->sync_thread)
7436 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08007437 }
7438
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10007440 if (mddev->to_remove == &raid5_attrs_group)
7441 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007442 else if (mddev->kobj.sd &&
7443 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrowncc6167b2016-11-02 14:16:50 +11007444 pr_warn("raid5: failed to create sysfs attributes for %s\n",
7445 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10007446 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
7447
7448 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007449 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10007450 /* read-ahead size must cover two whole stripes, which
7451 * is 2 * (datadisks) * chunksize where 'n' is the
7452 * number of raid devices
7453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454 int data_disks = conf->previous_raid_disks - conf->max_degraded;
7455 int stripe = data_disks *
7456 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Jan Karadc3b17c2017-02-02 15:56:50 +01007457 if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
7458 mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10007459
NeilBrown9f7c2222010-07-26 12:04:13 +10007460 chunk_size = mddev->chunk_sectors << 9;
7461 blk_queue_io_min(mddev->queue, chunk_size);
7462 blk_queue_io_opt(mddev->queue, chunk_size *
7463 (conf->raid_disks - conf->max_degraded));
Kent Overstreetc78afc62013-07-11 22:39:53 -07007464 mddev->queue->limits.raid_partial_stripes_expensive = 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007465 /*
7466 * We can only discard a whole stripe. It doesn't make sense to
7467 * discard data disk but write parity disk
7468 */
7469 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11007470 /* Round up to power of 2, as discard handling
7471 * currently assumes that */
7472 while ((stripe-1) & stripe)
7473 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007474 mddev->queue->limits.discard_alignment = stripe;
7475 mddev->queue->limits.discard_granularity = stripe;
Konstantin Khlebnikove8d7c332016-11-27 19:32:32 +03007476
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007477 blk_queue_max_write_same_sectors(mddev->queue, 0);
Christoph Hellwig3deff1a2017-04-05 19:21:03 +02007478 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007479
NeilBrown05616be2012-05-21 09:27:00 +10007480 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007481 disk_stack_limits(mddev->gendisk, rdev->bdev,
7482 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10007483 disk_stack_limits(mddev->gendisk, rdev->bdev,
7484 rdev->new_data_offset << 9);
7485 }
Shaohua Li620125f2012-10-11 13:49:05 +11007486
Christoph Hellwig48920ff2017-04-05 19:21:23 +02007487 /*
7488 * zeroing is required, otherwise data
7489 * could be lost. Consider a scenario: discard a stripe
7490 * (the stripe could be inconsistent if
7491 * discard_zeroes_data is 0); write one disk of the
7492 * stripe (the stripe could be inconsistent again
7493 * depending on which disks are used to calculate
7494 * parity); the disk is broken; The stripe data of this
7495 * disk is lost.
7496 *
7497 * We only allow DISCARD if the sysadmin has confirmed that
7498 * only safe devices are in use by setting a module parameter.
7499 * A better idea might be to turn DISCARD into WRITE_ZEROES
7500 * requests, as that is required to be safe.
7501 */
7502 if (devices_handle_discard_safely &&
Jes Sorensene7597e62016-02-16 16:44:24 -05007503 mddev->queue->limits.max_discard_sectors >= (stripe >> 9) &&
7504 mddev->queue->limits.discard_granularity >= stripe)
Bart Van Assche8b904b52018-03-07 17:10:10 -08007505 blk_queue_flag_set(QUEUE_FLAG_DISCARD,
Shaohua Li620125f2012-10-11 13:49:05 +11007506 mddev->queue);
7507 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08007508 blk_queue_flag_clear(QUEUE_FLAG_DISCARD,
Shaohua Li620125f2012-10-11 13:49:05 +11007509 mddev->queue);
Shaohua Li1dffddd2016-09-08 10:49:06 -07007510
7511 blk_queue_max_hw_sectors(mddev->queue, UINT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 }
7513
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02007514 if (log_init(conf, journal_dev, raid5_has_ppl(conf)))
Artur Paszkiewiczff875732017-03-09 09:59:58 +01007515 goto abort;
Shaohua Li5c7e81c2015-08-13 14:32:04 -07007516
Linus Torvalds1da177e2005-04-16 15:20:36 -07007517 return 0;
7518abort:
NeilBrown01f96c02011-09-21 15:30:20 +10007519 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11007520 print_raid5_conf(conf);
7521 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522 mddev->private = NULL;
NeilBrowncc6167b2016-11-02 14:16:50 +11007523 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524 return -EIO;
7525}
7526
NeilBrownafa0f552014-12-15 12:56:58 +11007527static void raid5_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528{
NeilBrownafa0f552014-12-15 12:56:58 +11007529 struct r5conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007530
Dan Williams95fc17a2009-07-31 12:39:15 +10007531 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10007532 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533}
7534
Shaohua Li849674e2016-01-20 13:52:20 -08007535static void raid5_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536{
NeilBrownd1688a62011-10-11 16:49:52 +11007537 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538 int i;
7539
Andre Noll9d8f0362009-06-18 08:45:01 +10007540 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
NeilBrown3cb5edf2015-07-15 17:24:17 +10007541 conf->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07007542 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
NeilBrown5fd13352016-06-02 16:19:52 +10007543 rcu_read_lock();
7544 for (i = 0; i < conf->raid_disks; i++) {
7545 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
7546 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
7547 }
7548 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007550}
7551
NeilBrownd1688a62011-10-11 16:49:52 +11007552static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553{
7554 int i;
7555 struct disk_info *tmp;
7556
NeilBrowncc6167b2016-11-02 14:16:50 +11007557 pr_debug("RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007558 if (!conf) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007559 pr_debug("(conf==NULL)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560 return;
7561 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007562 pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level,
NeilBrown0c55e022010-05-03 14:09:02 +10007563 conf->raid_disks,
7564 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007565
7566 for (i = 0; i < conf->raid_disks; i++) {
7567 char b[BDEVNAME_SIZE];
7568 tmp = conf->disks + i;
7569 if (tmp->rdev)
NeilBrowncc6167b2016-11-02 14:16:50 +11007570 pr_debug(" disk %d, o:%d, dev:%s\n",
NeilBrown0c55e022010-05-03 14:09:02 +10007571 i, !test_bit(Faulty, &tmp->rdev->flags),
7572 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007573 }
7574}
7575
NeilBrownfd01b882011-10-11 16:47:53 +11007576static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577{
7578 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11007579 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10007581 int count = 0;
7582 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583
7584 for (i = 0; i < conf->raid_disks; i++) {
7585 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11007586 if (tmp->replacement
7587 && tmp->replacement->recovery_offset == MaxSector
7588 && !test_bit(Faulty, &tmp->replacement->flags)
7589 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
7590 /* Replacement has just become active. */
7591 if (!tmp->rdev
7592 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
7593 count++;
7594 if (tmp->rdev) {
7595 /* Replaced device not technically faulty,
7596 * but we need to be sure it gets removed
7597 * and never re-added.
7598 */
7599 set_bit(Faulty, &tmp->rdev->flags);
7600 sysfs_notify_dirent_safe(
7601 tmp->rdev->sysfs_state);
7602 }
7603 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
7604 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10007605 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08007606 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07007607 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10007608 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11007609 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610 }
7611 }
NeilBrown6b965622010-08-18 11:56:59 +10007612 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08007613 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007614 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007616 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617}
7618
NeilBrownb8321b62011-12-23 10:17:51 +11007619static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007620{
NeilBrownd1688a62011-10-11 16:49:52 +11007621 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11007623 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11007624 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625 struct disk_info *p = conf->disks + number;
7626
7627 print_raid5_conf(conf);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007628 if (test_bit(Journal, &rdev->flags) && conf->log) {
Shaohua Lic2bb6242015-10-08 21:54:07 -07007629 /*
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007630 * we can't wait pending write here, as this is called in
7631 * raid5d, wait will deadlock.
NeilBrown84dd97a2017-03-15 14:05:14 +11007632 * neilb: there is no locking about new writes here,
7633 * so this cannot be safe.
Shaohua Lic2bb6242015-10-08 21:54:07 -07007634 */
Song Liu70d466f2017-05-11 15:28:28 -07007635 if (atomic_read(&conf->active_stripes) ||
7636 atomic_read(&conf->r5c_cached_full_stripes) ||
7637 atomic_read(&conf->r5c_cached_partial_stripes)) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007638 return -EBUSY;
NeilBrown84dd97a2017-03-15 14:05:14 +11007639 }
Artur Paszkiewiczff875732017-03-09 09:59:58 +01007640 log_exit(conf);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007641 return 0;
Shaohua Lic2bb6242015-10-08 21:54:07 -07007642 }
NeilBrown657e3e42011-12-23 10:17:52 +11007643 if (rdev == p->rdev)
7644 rdevp = &p->rdev;
7645 else if (rdev == p->replacement)
7646 rdevp = &p->replacement;
7647 else
7648 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11007649
NeilBrown657e3e42011-12-23 10:17:52 +11007650 if (number >= conf->raid_disks &&
7651 conf->reshape_progress == MaxSector)
7652 clear_bit(In_sync, &rdev->flags);
7653
7654 if (test_bit(In_sync, &rdev->flags) ||
7655 atomic_read(&rdev->nr_pending)) {
7656 err = -EBUSY;
7657 goto abort;
7658 }
7659 /* Only remove non-faulty devices if recovery
7660 * isn't possible.
7661 */
7662 if (!test_bit(Faulty, &rdev->flags) &&
7663 mddev->recovery_disabled != conf->recovery_disabled &&
7664 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11007665 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11007666 number < conf->raid_disks) {
7667 err = -EBUSY;
7668 goto abort;
7669 }
7670 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10007671 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
7672 synchronize_rcu();
7673 if (atomic_read(&rdev->nr_pending)) {
7674 /* lost the race, try later */
7675 err = -EBUSY;
7676 *rdevp = rdev;
7677 }
7678 }
Artur Paszkiewicz6358c232017-03-09 10:00:02 +01007679 if (!err) {
7680 err = log_modify(conf, rdev, false);
7681 if (err)
7682 goto abort;
7683 }
NeilBrownd787be42016-06-02 16:19:53 +10007684 if (p->replacement) {
NeilBrowndd054fc2011-12-23 10:17:53 +11007685 /* We must have just cleared 'rdev' */
7686 p->rdev = p->replacement;
7687 clear_bit(Replacement, &p->replacement->flags);
7688 smp_mb(); /* Make sure other CPUs may see both as identical
7689 * but will never see neither - if they are careful
7690 */
7691 p->replacement = NULL;
Artur Paszkiewicz6358c232017-03-09 10:00:02 +01007692
7693 if (!err)
7694 err = log_modify(conf, p->rdev, true);
Guoqing Jiange5bc9c32017-04-24 15:58:04 +08007695 }
7696
7697 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698abort:
7699
7700 print_raid5_conf(conf);
7701 return err;
7702}
7703
NeilBrownfd01b882011-10-11 16:47:53 +11007704static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705{
NeilBrownd1688a62011-10-11 16:49:52 +11007706 struct r5conf *conf = mddev->private;
Xiao Nid9771f52019-06-14 15:41:05 -07007707 int ret, err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007708 int disk;
7709 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10007710 int first = 0;
7711 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007713 if (test_bit(Journal, &rdev->flags)) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007714 if (conf->log)
7715 return -EBUSY;
7716
7717 rdev->raid_disk = 0;
7718 /*
7719 * The array is in readonly mode if journal is missing, so no
7720 * write requests running. We should be safe
7721 */
Xiao Nid9771f52019-06-14 15:41:05 -07007722 ret = log_init(conf, rdev, false);
7723 if (ret)
7724 return ret;
7725
7726 ret = r5l_start(conf->log);
7727 if (ret)
7728 return ret;
7729
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007730 return 0;
7731 }
NeilBrown7f0da592011-07-28 11:39:22 +10007732 if (mddev->recovery_disabled == conf->recovery_disabled)
7733 return -EBUSY;
7734
NeilBrowndc10c642012-03-19 12:46:37 +11007735 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007736 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10007737 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007738
Neil Brown6c2fce22008-06-28 08:31:31 +10007739 if (rdev->raid_disk >= 0)
7740 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007741
7742 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07007743 * find the disk ... but prefer rdev->saved_raid_disk
7744 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007745 */
NeilBrown16a53ec2006-06-26 00:27:38 -07007746 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10007747 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07007748 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10007749 first = rdev->saved_raid_disk;
7750
7751 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007752 p = conf->disks + disk;
7753 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08007754 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007755 rdev->raid_disk = disk;
NeilBrown72626682005-09-09 16:23:54 -07007756 if (rdev->saved_raid_disk != disk)
7757 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08007758 rcu_assign_pointer(p->rdev, rdev);
Artur Paszkiewicz6358c232017-03-09 10:00:02 +01007759
7760 err = log_modify(conf, rdev, true);
7761
NeilBrown5cfb22a2012-07-03 11:46:53 +10007762 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007763 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007764 }
7765 for (disk = first; disk <= last; disk++) {
7766 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007767 if (test_bit(WantReplacement, &p->rdev->flags) &&
7768 p->replacement == NULL) {
7769 clear_bit(In_sync, &rdev->flags);
7770 set_bit(Replacement, &rdev->flags);
7771 rdev->raid_disk = disk;
7772 err = 0;
7773 conf->fullsync = 1;
7774 rcu_assign_pointer(p->replacement, rdev);
7775 break;
7776 }
7777 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007778out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007779 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10007780 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007781}
7782
NeilBrownfd01b882011-10-11 16:47:53 +11007783static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007784{
7785 /* no resync is happening, and there is enough space
7786 * on all devices, so we can resize.
7787 * We need to make sure resync covers any new space.
7788 * If the array is shrinking we should possibly wait until
7789 * any io in the removed space completes, but it hardly seems
7790 * worth it.
7791 */
NeilBrowna4a61252012-05-22 13:55:27 +10007792 sector_t newsize;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007793 struct r5conf *conf = mddev->private;
7794
Shaohua Lie254de62018-08-29 11:05:42 -07007795 if (raid5_has_log(conf) || raid5_has_ppl(conf))
Shaohua Li713cf5a2015-08-13 14:32:03 -07007796 return -EINVAL;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007797 sectors &= ~((sector_t)conf->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10007798 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
7799 if (mddev->external_size &&
7800 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11007801 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10007802 if (mddev->bitmap) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007803 int ret = md_bitmap_resize(mddev->bitmap, sectors, 0, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10007804 if (ret)
7805 return ret;
7806 }
7807 md_set_array_sectors(mddev, newsize);
NeilBrownb0986362011-05-11 15:52:21 +10007808 if (sectors > mddev->dev_sectors &&
7809 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11007810 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007811 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7812 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007813 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07007814 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007815 return 0;
7816}
7817
NeilBrownfd01b882011-10-11 16:47:53 +11007818static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10007819{
7820 /* Can only proceed if there are plenty of stripe_heads.
7821 * We need a minimum of one full stripe,, and for sensible progress
7822 * it is best to have about 4 times that.
7823 * If we require 4 times, then the default 256 4K stripe_heads will
7824 * allow for chunk sizes up to 256K, which is probably OK.
7825 * If the chunk size is greater, user-space should request more
7826 * stripe_heads first.
7827 */
NeilBrownd1688a62011-10-11 16:49:52 +11007828 struct r5conf *conf = mddev->private;
Yufen Yuc911c462020-07-18 05:29:07 -04007829 if (((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007830 > conf->min_nr_stripes ||
Yufen Yuc911c462020-07-18 05:29:07 -04007831 ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007832 > conf->min_nr_stripes) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007833 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7834 mdname(mddev),
7835 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
Yufen Yuc911c462020-07-18 05:29:07 -04007836 / RAID5_STRIPE_SIZE(conf))*4);
NeilBrown01ee22b2009-06-18 08:47:20 +10007837 return 0;
7838 }
7839 return 1;
7840}
7841
NeilBrownfd01b882011-10-11 16:47:53 +11007842static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08007843{
NeilBrownd1688a62011-10-11 16:49:52 +11007844 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08007845
Shaohua Lie254de62018-08-29 11:05:42 -07007846 if (raid5_has_log(conf) || raid5_has_ppl(conf))
Shaohua Li713cf5a2015-08-13 14:32:03 -07007847 return -EINVAL;
NeilBrown88ce4932009-03-31 15:24:23 +11007848 if (mddev->delta_disks == 0 &&
7849 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10007850 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10007851 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10007852 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11007853 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10007854 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11007855 /* We might be able to shrink, but the devices must
7856 * be made bigger first.
7857 * For raid6, 4 is the minimum size.
7858 * Otherwise 2 is the minimum
7859 */
7860 int min = 2;
7861 if (mddev->level == 6)
7862 min = 4;
7863 if (mddev->raid_disks + mddev->delta_disks < min)
7864 return -EINVAL;
7865 }
NeilBrown29269552006-03-27 01:18:10 -08007866
NeilBrown01ee22b2009-06-18 08:47:20 +10007867 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08007868 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08007869
NeilBrown738a2732015-05-08 18:19:39 +10007870 if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
7871 mddev->delta_disks > 0)
7872 if (resize_chunks(conf,
7873 conf->previous_raid_disks
7874 + max(0, mddev->delta_disks),
7875 max(mddev->new_chunk_sectors,
7876 mddev->chunk_sectors)
7877 ) < 0)
7878 return -ENOMEM;
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02007879
7880 if (conf->previous_raid_disks + mddev->delta_disks <= conf->pool_size)
7881 return 0; /* never bother to shrink */
NeilBrowne56108d62012-10-11 14:24:13 +11007882 return resize_stripes(conf, (conf->previous_raid_disks
7883 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08007884}
7885
NeilBrownfd01b882011-10-11 16:47:53 +11007886static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08007887{
NeilBrownd1688a62011-10-11 16:49:52 +11007888 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11007889 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08007890 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07007891 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08007892
NeilBrownf4168852007-02-28 20:11:53 -08007893 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08007894 return -EBUSY;
7895
NeilBrown01ee22b2009-06-18 08:47:20 +10007896 if (!check_stripe_cache(mddev))
7897 return -ENOSPC;
7898
NeilBrown30b67642012-05-22 13:55:28 +10007899 if (has_failed(conf))
7900 return -EINVAL;
7901
NeilBrownc6563a82012-05-21 09:27:00 +10007902 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11007903 if (!test_bit(In_sync, &rdev->flags)
7904 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08007905 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10007906 }
NeilBrown63c70c42006-03-27 01:18:13 -08007907
NeilBrownf4168852007-02-28 20:11:53 -08007908 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08007909 /* Not enough devices even to make a degraded array
7910 * of that size
7911 */
7912 return -EINVAL;
7913
NeilBrownec32a2b2009-03-31 15:17:38 +11007914 /* Refuse to reduce size of the array. Any reductions in
7915 * array size must be through explicit setting of array_size
7916 * attribute.
7917 */
7918 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
7919 < mddev->array_sectors) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007920 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
7921 mdname(mddev));
NeilBrownec32a2b2009-03-31 15:17:38 +11007922 return -EINVAL;
7923 }
7924
NeilBrownf6705572006-03-27 01:18:11 -08007925 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08007926 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10007927 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007928 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08007929 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007930 conf->prev_chunk_sectors = conf->chunk_sectors;
7931 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11007932 conf->prev_algo = conf->algorithm;
7933 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10007934 conf->generation++;
7935 /* Code that selects data_offset needs to see the generation update
7936 * if reshape_progress has been set - so a memory barrier needed.
7937 */
7938 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10007939 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11007940 conf->reshape_progress = raid5_size(mddev, 0, 0);
7941 else
7942 conf->reshape_progress = 0;
7943 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10007944 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007945 spin_unlock_irq(&conf->device_lock);
7946
NeilBrown4d77e3b2013-08-27 15:57:47 +10007947 /* Now make sure any requests that proceeded on the assumption
7948 * the reshape wasn't running - like Discard or Read - have
7949 * completed.
7950 */
7951 mddev_suspend(mddev);
7952 mddev_resume(mddev);
7953
NeilBrown29269552006-03-27 01:18:10 -08007954 /* Add some new drives, as many as will fit.
7955 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10007956 * Don't add devices if we are reducing the number of
7957 * devices in the array. This is because it is not possible
7958 * to correctly record the "partially reconstructed" state of
7959 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08007960 */
NeilBrown87a8dec2011-01-31 11:57:43 +11007961 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11007962 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11007963 if (rdev->raid_disk < 0 &&
7964 !test_bit(Faulty, &rdev->flags)) {
7965 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11007966 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11007967 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11007968 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11007969 else
NeilBrown87a8dec2011-01-31 11:57:43 +11007970 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10007971
Damien Le Moal2aada5b2020-07-16 13:54:42 +09007972 /* Failure here is OK */
7973 sysfs_link_rdev(mddev, rdev);
NeilBrown50da0842011-01-31 11:57:43 +11007974 }
NeilBrown87a8dec2011-01-31 11:57:43 +11007975 } else if (rdev->raid_disk >= conf->previous_raid_disks
7976 && !test_bit(Faulty, &rdev->flags)) {
7977 /* This is a spare that was manually added */
7978 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11007979 }
NeilBrown29269552006-03-27 01:18:10 -08007980
NeilBrown87a8dec2011-01-31 11:57:43 +11007981 /* When a reshape changes the number of devices,
7982 * ->degraded is measured against the larger of the
7983 * pre and post number of devices.
7984 */
NeilBrownec32a2b2009-03-31 15:17:38 +11007985 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08007986 mddev->degraded = raid5_calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11007987 spin_unlock_irqrestore(&conf->device_lock, flags);
7988 }
NeilBrown63c70c42006-03-27 01:18:13 -08007989 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10007990 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08007991 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownf6705572006-03-27 01:18:11 -08007992
NeilBrown29269552006-03-27 01:18:10 -08007993 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7994 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10007995 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown29269552006-03-27 01:18:10 -08007996 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7997 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7998 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007999 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08008000 if (!mddev->sync_thread) {
8001 mddev->recovery = 0;
8002 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11008003 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08008004 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11008005 mddev->new_chunk_sectors =
8006 conf->chunk_sectors = conf->prev_chunk_sectors;
8007 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10008008 rdev_for_each(rdev, mddev)
8009 rdev->new_data_offset = rdev->data_offset;
8010 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11008011 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11008012 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11008013 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11008014 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08008015 spin_unlock_irq(&conf->device_lock);
8016 return -EAGAIN;
8017 }
NeilBrownc8f517c2009-03-31 15:28:40 +11008018 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08008019 md_wakeup_thread(mddev->sync_thread);
8020 md_new_event(mddev);
8021 return 0;
8022}
NeilBrown29269552006-03-27 01:18:10 -08008023
NeilBrownec32a2b2009-03-31 15:17:38 +11008024/* This is called from the reshape thread and should make any
8025 * changes needed in 'conf'
8026 */
NeilBrownd1688a62011-10-11 16:49:52 +11008027static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08008028{
NeilBrown29269552006-03-27 01:18:10 -08008029
NeilBrownf6705572006-03-27 01:18:11 -08008030 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrowndb0505d2017-10-17 16:18:36 +11008031 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07008032
NeilBrownf6705572006-03-27 01:18:11 -08008033 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11008034 conf->previous_raid_disks = conf->raid_disks;
Xiao Nib5d27712017-07-05 17:34:04 +08008035 md_finish_reshape(conf->mddev);
NeilBrown05616be2012-05-21 09:27:00 +10008036 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11008037 conf->reshape_progress = MaxSector;
NeilBrown6cbd8142015-07-24 13:30:32 +10008038 conf->mddev->reshape_position = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11008039 rdev_for_each(rdev, conf->mddev)
8040 if (rdev->raid_disk >= 0 &&
8041 !test_bit(Journal, &rdev->flags) &&
8042 !test_bit(In_sync, &rdev->flags))
8043 rdev->recovery_offset = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08008044 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11008045 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07008046
8047 /* read-ahead size must cover two whole stripes, which is
8048 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
8049 */
NeilBrown4a5add42010-06-01 19:37:28 +10008050 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11008051 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10008052 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11008053 / PAGE_SIZE);
Jan Karadc3b17c2017-02-02 15:56:50 +01008054 if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
8055 conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown16a53ec2006-06-26 00:27:38 -07008056 }
NeilBrown29269552006-03-27 01:18:10 -08008057 }
NeilBrown29269552006-03-27 01:18:10 -08008058}
8059
NeilBrownec32a2b2009-03-31 15:17:38 +11008060/* This is called from the raid5d thread with mddev_lock held.
8061 * It makes config changes to the device.
8062 */
NeilBrownfd01b882011-10-11 16:47:53 +11008063static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11008064{
NeilBrownd1688a62011-10-11 16:49:52 +11008065 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11008066
8067 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8068
BingJing Chang88763912018-02-22 13:34:46 +08008069 if (mddev->delta_disks <= 0) {
NeilBrownec32a2b2009-03-31 15:17:38 +11008070 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11008071 spin_lock_irq(&conf->device_lock);
Song Liu2e38a372017-01-24 10:45:30 -08008072 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11008073 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11008074 for (d = conf->raid_disks ;
8075 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10008076 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11008077 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10008078 if (rdev)
8079 clear_bit(In_sync, &rdev->flags);
8080 rdev = conf->disks[d].replacement;
8081 if (rdev)
8082 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10008083 }
NeilBrowncea9c222009-03-31 15:15:05 +11008084 }
NeilBrown88ce4932009-03-31 15:24:23 +11008085 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10008086 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11008087 mddev->reshape_position = MaxSector;
8088 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10008089 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11008090 }
8091}
8092
NeilBrownb03e0cc2017-10-19 12:49:15 +11008093static void raid5_quiesce(struct mddev *mddev, int quiesce)
NeilBrown72626682005-09-09 16:23:54 -07008094{
NeilBrownd1688a62011-10-11 16:49:52 +11008095 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07008096
NeilBrownb03e0cc2017-10-19 12:49:15 +11008097 if (quiesce) {
8098 /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11008099 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10008100 /* '2' tells resync/reshape to pause so that all
8101 * active stripes can drain
8102 */
Song Liua39f7af2016-11-17 15:24:40 -08008103 r5c_flush_cache(conf, INT_MAX);
NeilBrown64bd6602009-08-03 10:59:58 +10008104 conf->quiesce = 2;
Yuanhan Liub1b46482015-05-08 18:19:06 +10008105 wait_event_cmd(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08008106 atomic_read(&conf->active_stripes) == 0 &&
8107 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11008108 unlock_all_device_hash_locks_irq(conf),
8109 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10008110 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11008111 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10008112 /* allow reshape to continue */
8113 wake_up(&conf->wait_for_overlap);
NeilBrownb03e0cc2017-10-19 12:49:15 +11008114 } else {
8115 /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11008116 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07008117 conf->quiesce = 0;
Yuanhan Liub1b46482015-05-08 18:19:06 +10008118 wake_up(&conf->wait_for_quiescent);
NeilBrowne464eaf2006-03-27 01:18:14 -08008119 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11008120 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07008121 }
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01008122 log_quiesce(conf, quiesce);
NeilBrown72626682005-09-09 16:23:54 -07008123}
NeilBrownb15c2e52006-01-06 00:20:16 -08008124
NeilBrownfd01b882011-10-11 16:47:53 +11008125static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11008126{
NeilBrowne373ab12011-10-11 16:48:59 +11008127 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07008128 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11008129
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008130 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11008131 if (raid0_conf->nr_strip_zones > 1) {
NeilBrowncc6167b2016-11-02 14:16:50 +11008132 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
8133 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008134 return ERR_PTR(-EINVAL);
8135 }
8136
NeilBrowne373ab12011-10-11 16:48:59 +11008137 sectors = raid0_conf->strip_zone[0].zone_end;
8138 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10008139 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008140 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11008141 mddev->new_layout = ALGORITHM_PARITY_N;
8142 mddev->new_chunk_sectors = mddev->chunk_sectors;
8143 mddev->raid_disks += 1;
8144 mddev->delta_disks = 1;
8145 /* make sure it will be not marked as dirty */
8146 mddev->recovery_cp = MaxSector;
8147
8148 return setup_conf(mddev);
8149}
8150
NeilBrownfd01b882011-10-11 16:47:53 +11008151static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11008152{
8153 int chunksect;
Shaohua Li6995f0b2016-12-08 15:48:17 -08008154 void *ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11008155
8156 if (mddev->raid_disks != 2 ||
8157 mddev->degraded > 1)
8158 return ERR_PTR(-EINVAL);
8159
8160 /* Should check if there are write-behind devices? */
8161
8162 chunksect = 64*2; /* 64K by default */
8163
8164 /* The array must be an exact multiple of chunksize */
8165 while (chunksect && (mddev->array_sectors & (chunksect-1)))
8166 chunksect >>= 1;
8167
Yufen Yuc911c462020-07-18 05:29:07 -04008168 if ((chunksect<<9) < RAID5_STRIPE_SIZE((struct r5conf *)mddev->private))
NeilBrownd562b0c2009-03-31 14:39:39 +11008169 /* array size does not allow a suitable chunk size */
8170 return ERR_PTR(-EINVAL);
8171
8172 mddev->new_level = 5;
8173 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10008174 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11008175
Shaohua Li6995f0b2016-12-08 15:48:17 -08008176 ret = setup_conf(mddev);
Jes Sorensen32cd7cb2017-01-06 19:31:35 -05008177 if (!IS_ERR(ret))
Shaohua Li394ed8e2017-01-04 16:10:19 -08008178 mddev_clear_unsupported_flags(mddev,
8179 UNSUPPORTED_MDDEV_FLAGS);
Shaohua Li6995f0b2016-12-08 15:48:17 -08008180 return ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11008181}
8182
NeilBrownfd01b882011-10-11 16:47:53 +11008183static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11008184{
8185 int new_layout;
8186
8187 switch (mddev->layout) {
8188 case ALGORITHM_LEFT_ASYMMETRIC_6:
8189 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
8190 break;
8191 case ALGORITHM_RIGHT_ASYMMETRIC_6:
8192 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
8193 break;
8194 case ALGORITHM_LEFT_SYMMETRIC_6:
8195 new_layout = ALGORITHM_LEFT_SYMMETRIC;
8196 break;
8197 case ALGORITHM_RIGHT_SYMMETRIC_6:
8198 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
8199 break;
8200 case ALGORITHM_PARITY_0_6:
8201 new_layout = ALGORITHM_PARITY_0;
8202 break;
8203 case ALGORITHM_PARITY_N:
8204 new_layout = ALGORITHM_PARITY_N;
8205 break;
8206 default:
8207 return ERR_PTR(-EINVAL);
8208 }
8209 mddev->new_level = 5;
8210 mddev->new_layout = new_layout;
8211 mddev->delta_disks = -1;
8212 mddev->raid_disks -= 1;
8213 return setup_conf(mddev);
8214}
8215
NeilBrownfd01b882011-10-11 16:47:53 +11008216static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11008217{
NeilBrown88ce4932009-03-31 15:24:23 +11008218 /* For a 2-drive array, the layout and chunk size can be changed
8219 * immediately as not restriping is needed.
8220 * For larger arrays we record the new value - after validation
8221 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11008222 */
NeilBrownd1688a62011-10-11 16:49:52 +11008223 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10008224 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11008225
NeilBrown597a7112009-06-18 08:47:42 +10008226 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11008227 return -EINVAL;
8228 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008229 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11008230 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008231 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11008232 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008233 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11008234 /* not factor of array size */
8235 return -EINVAL;
8236 }
8237
8238 /* They look valid */
8239
NeilBrown88ce4932009-03-31 15:24:23 +11008240 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10008241 /* can make the change immediately */
8242 if (mddev->new_layout >= 0) {
8243 conf->algorithm = mddev->new_layout;
8244 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11008245 }
8246 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10008247 conf->chunk_sectors = new_chunk ;
8248 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11008249 }
Shaohua Li29530792016-12-08 15:48:19 -08008250 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown88ce4932009-03-31 15:24:23 +11008251 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11008252 }
NeilBrown50ac1682009-06-18 08:47:55 +10008253 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11008254}
8255
NeilBrownfd01b882011-10-11 16:47:53 +11008256static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11008257{
NeilBrown597a7112009-06-18 08:47:42 +10008258 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10008259
NeilBrown597a7112009-06-18 08:47:42 +10008260 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11008261 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008262 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008263 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11008264 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008265 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11008266 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008267 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11008268 /* not factor of array size */
8269 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008270 }
NeilBrown88ce4932009-03-31 15:24:23 +11008271
8272 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10008273 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11008274}
8275
NeilBrownfd01b882011-10-11 16:47:53 +11008276static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11008277{
8278 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008279 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008280 * raid1 - if there are two drives. We need to know the chunk size
8281 * raid4 - trivial - just use a raid4 layout.
8282 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008283 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008284 if (mddev->level == 0)
8285 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11008286 if (mddev->level == 1)
8287 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11008288 if (mddev->level == 4) {
8289 mddev->new_layout = ALGORITHM_PARITY_N;
8290 mddev->new_level = 5;
8291 return setup_conf(mddev);
8292 }
NeilBrownfc9739c2009-03-31 14:57:20 +11008293 if (mddev->level == 6)
8294 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11008295
8296 return ERR_PTR(-EINVAL);
8297}
8298
NeilBrownfd01b882011-10-11 16:47:53 +11008299static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11008300{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008301 /* raid4 can take over:
8302 * raid0 - if there is only one strip zone
8303 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11008304 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008305 if (mddev->level == 0)
8306 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11008307 if (mddev->level == 5 &&
8308 mddev->layout == ALGORITHM_PARITY_N) {
8309 mddev->new_layout = 0;
8310 mddev->new_level = 4;
8311 return setup_conf(mddev);
8312 }
8313 return ERR_PTR(-EINVAL);
8314}
NeilBrownd562b0c2009-03-31 14:39:39 +11008315
NeilBrown84fc4b52011-10-11 16:49:58 +11008316static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11008317
NeilBrownfd01b882011-10-11 16:47:53 +11008318static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11008319{
8320 /* Currently can only take over a raid5. We map the
8321 * personality to an equivalent raid6 personality
8322 * with the Q block at the end.
8323 */
8324 int new_layout;
8325
8326 if (mddev->pers != &raid5_personality)
8327 return ERR_PTR(-EINVAL);
8328 if (mddev->degraded > 1)
8329 return ERR_PTR(-EINVAL);
8330 if (mddev->raid_disks > 253)
8331 return ERR_PTR(-EINVAL);
8332 if (mddev->raid_disks < 3)
8333 return ERR_PTR(-EINVAL);
8334
8335 switch (mddev->layout) {
8336 case ALGORITHM_LEFT_ASYMMETRIC:
8337 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
8338 break;
8339 case ALGORITHM_RIGHT_ASYMMETRIC:
8340 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
8341 break;
8342 case ALGORITHM_LEFT_SYMMETRIC:
8343 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
8344 break;
8345 case ALGORITHM_RIGHT_SYMMETRIC:
8346 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
8347 break;
8348 case ALGORITHM_PARITY_0:
8349 new_layout = ALGORITHM_PARITY_0_6;
8350 break;
8351 case ALGORITHM_PARITY_N:
8352 new_layout = ALGORITHM_PARITY_N;
8353 break;
8354 default:
8355 return ERR_PTR(-EINVAL);
8356 }
8357 mddev->new_level = 6;
8358 mddev->new_layout = new_layout;
8359 mddev->delta_disks = 1;
8360 mddev->raid_disks += 1;
8361 return setup_conf(mddev);
8362}
8363
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01008364static int raid5_change_consistency_policy(struct mddev *mddev, const char *buf)
8365{
8366 struct r5conf *conf;
8367 int err;
8368
8369 err = mddev_lock(mddev);
8370 if (err)
8371 return err;
8372 conf = mddev->private;
8373 if (!conf) {
8374 mddev_unlock(mddev);
8375 return -ENODEV;
8376 }
8377
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02008378 if (strncmp(buf, "ppl", 3) == 0) {
Song Liu0bb0c102017-03-27 10:51:33 -07008379 /* ppl only works with RAID 5 */
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02008380 if (!raid5_has_ppl(conf) && conf->level == 5) {
8381 err = log_init(conf, NULL, true);
8382 if (!err) {
8383 err = resize_stripes(conf, conf->pool_size);
8384 if (err)
8385 log_exit(conf);
8386 }
Song Liu0bb0c102017-03-27 10:51:33 -07008387 } else
8388 err = -EINVAL;
8389 } else if (strncmp(buf, "resync", 6) == 0) {
8390 if (raid5_has_ppl(conf)) {
8391 mddev_suspend(mddev);
8392 log_exit(conf);
Song Liu0bb0c102017-03-27 10:51:33 -07008393 mddev_resume(mddev);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02008394 err = resize_stripes(conf, conf->pool_size);
Song Liu0bb0c102017-03-27 10:51:33 -07008395 } else if (test_bit(MD_HAS_JOURNAL, &conf->mddev->flags) &&
8396 r5l_log_disk_error(conf)) {
8397 bool journal_dev_exists = false;
8398 struct md_rdev *rdev;
8399
8400 rdev_for_each(rdev, mddev)
8401 if (test_bit(Journal, &rdev->flags)) {
8402 journal_dev_exists = true;
8403 break;
8404 }
8405
8406 if (!journal_dev_exists) {
8407 mddev_suspend(mddev);
8408 clear_bit(MD_HAS_JOURNAL, &mddev->flags);
8409 mddev_resume(mddev);
8410 } else /* need remove journal device first */
8411 err = -EBUSY;
8412 } else
8413 err = -EINVAL;
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01008414 } else {
8415 err = -EINVAL;
8416 }
8417
8418 if (!err)
8419 md_update_sb(mddev, 1);
8420
8421 mddev_unlock(mddev);
8422
8423 return err;
8424}
8425
Song Liud5d885f2017-11-19 22:17:01 -08008426static int raid5_start(struct mddev *mddev)
8427{
8428 struct r5conf *conf = mddev->private;
8429
8430 return r5l_start(conf->log);
8431}
8432
NeilBrown84fc4b52011-10-11 16:49:58 +11008433static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07008434{
8435 .name = "raid6",
8436 .level = 6,
8437 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008438 .make_request = raid5_make_request,
8439 .run = raid5_run,
Song Liud5d885f2017-11-19 22:17:01 -08008440 .start = raid5_start,
NeilBrownafa0f552014-12-15 12:56:58 +11008441 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008442 .status = raid5_status,
8443 .error_handler = raid5_error,
NeilBrown16a53ec2006-06-26 00:27:38 -07008444 .hot_add_disk = raid5_add_disk,
8445 .hot_remove_disk= raid5_remove_disk,
8446 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008447 .sync_request = raid5_sync_request,
NeilBrown16a53ec2006-06-26 00:27:38 -07008448 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008449 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10008450 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08008451 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008452 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07008453 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11008454 .takeover = raid6_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008455 .congested = raid5_congested,
Song Liu0bb0c102017-03-27 10:51:33 -07008456 .change_consistency_policy = raid5_change_consistency_policy,
NeilBrown16a53ec2006-06-26 00:27:38 -07008457};
NeilBrown84fc4b52011-10-11 16:49:58 +11008458static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008459{
8460 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08008461 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008462 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008463 .make_request = raid5_make_request,
8464 .run = raid5_run,
Song Liud5d885f2017-11-19 22:17:01 -08008465 .start = raid5_start,
NeilBrownafa0f552014-12-15 12:56:58 +11008466 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008467 .status = raid5_status,
8468 .error_handler = raid5_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008469 .hot_add_disk = raid5_add_disk,
8470 .hot_remove_disk= raid5_remove_disk,
8471 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008472 .sync_request = raid5_sync_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008473 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008474 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08008475 .check_reshape = raid5_check_reshape,
8476 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008477 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07008478 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11008479 .takeover = raid5_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008480 .congested = raid5_congested,
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01008481 .change_consistency_policy = raid5_change_consistency_policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008482};
8483
NeilBrown84fc4b52011-10-11 16:49:58 +11008484static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008485{
NeilBrown2604b702006-01-06 00:20:36 -08008486 .name = "raid4",
8487 .level = 4,
8488 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008489 .make_request = raid5_make_request,
8490 .run = raid5_run,
Song Liud5d885f2017-11-19 22:17:01 -08008491 .start = raid5_start,
NeilBrownafa0f552014-12-15 12:56:58 +11008492 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008493 .status = raid5_status,
8494 .error_handler = raid5_error,
NeilBrown2604b702006-01-06 00:20:36 -08008495 .hot_add_disk = raid5_add_disk,
8496 .hot_remove_disk= raid5_remove_disk,
8497 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008498 .sync_request = raid5_sync_request,
NeilBrown2604b702006-01-06 00:20:36 -08008499 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008500 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08008501 .check_reshape = raid5_check_reshape,
8502 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008503 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08008504 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11008505 .takeover = raid4_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008506 .congested = raid5_congested,
Song Liu0bb0c102017-03-27 10:51:33 -07008507 .change_consistency_policy = raid5_change_consistency_policy,
NeilBrown2604b702006-01-06 00:20:36 -08008508};
8509
8510static int __init raid5_init(void)
8511{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008512 int ret;
8513
Shaohua Li851c30c2013-08-28 14:30:16 +08008514 raid5_wq = alloc_workqueue("raid5wq",
8515 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
8516 if (!raid5_wq)
8517 return -ENOMEM;
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008518
8519 ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
8520 "md/raid5:prepare",
8521 raid456_cpu_up_prepare,
8522 raid456_cpu_dead);
8523 if (ret) {
8524 destroy_workqueue(raid5_wq);
8525 return ret;
8526 }
NeilBrown16a53ec2006-06-26 00:27:38 -07008527 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008528 register_md_personality(&raid5_personality);
8529 register_md_personality(&raid4_personality);
8530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008531}
8532
NeilBrown2604b702006-01-06 00:20:36 -08008533static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008534{
NeilBrown16a53ec2006-06-26 00:27:38 -07008535 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008536 unregister_md_personality(&raid5_personality);
8537 unregister_md_personality(&raid4_personality);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008538 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
Shaohua Li851c30c2013-08-28 14:30:16 +08008539 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008540}
8541
8542module_init(raid5_init);
8543module_exit(raid5_exit);
8544MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008545MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008546MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08008547MODULE_ALIAS("md-raid5");
8548MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08008549MODULE_ALIAS("md-level-5");
8550MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07008551MODULE_ALIAS("md-personality-8"); /* RAID6 */
8552MODULE_ALIAS("md-raid6");
8553MODULE_ALIAS("md-level-6");
8554
8555/* This used to be two separate modules, they were: */
8556MODULE_ALIAS("raid5");
8557MODULE_ALIAS("raid6");