blob: 356cd9c7c753eb656030d68cc4435f911d3ba18c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
Shaohua Li851c30c2013-08-28 14:30:16 +080056#include <linux/nodemask.h>
shli@kernel.org46d5b782014-12-15 12:57:02 +110057#include <linux/flex_array.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010058#include <linux/sched/signal.h>
59
NeilBrowna9add5d2012-10-31 11:59:09 +110060#include <trace/events/block.h>
Shaohua Liaaf9f122017-03-03 22:06:12 -080061#include <linux/list_sort.h>
NeilBrowna9add5d2012-10-31 11:59:09 +110062
NeilBrown43b2e5d2009-03-31 14:33:13 +110063#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110064#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110065#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110066#include "bitmap.h"
Artur Paszkiewiczff875732017-03-09 09:59:58 +010067#include "raid5-log.h"
NeilBrown72626682005-09-09 16:23:54 -070068
Shaohua Li394ed8e2017-01-04 16:10:19 -080069#define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED)
70
Shaohua Li851c30c2013-08-28 14:30:16 +080071#define cpu_to_group(cpu) cpu_to_node(cpu)
72#define ANY_GROUP NUMA_NO_NODE
73
NeilBrown8e0e99b2014-10-02 13:45:00 +100074static bool devices_handle_discard_safely = false;
75module_param(devices_handle_discard_safely, bool, 0644);
76MODULE_PARM_DESC(devices_handle_discard_safely,
77 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
Shaohua Li851c30c2013-08-28 14:30:16 +080078static struct workqueue_struct *raid5_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
NeilBrownd1688a62011-10-11 16:49:52 +110080static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110081{
82 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
83 return &conf->stripe_hashtbl[hash];
84}
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Shaohua Li566c09c2013-11-14 15:16:17 +110086static inline int stripe_hash_locks_hash(sector_t sect)
87{
88 return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK;
89}
90
91static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
92{
93 spin_lock_irq(conf->hash_locks + hash);
94 spin_lock(&conf->device_lock);
95}
96
97static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
98{
99 spin_unlock(&conf->device_lock);
100 spin_unlock_irq(conf->hash_locks + hash);
101}
102
103static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
104{
105 int i;
106 local_irq_disable();
107 spin_lock(conf->hash_locks);
108 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
109 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
110 spin_lock(&conf->device_lock);
111}
112
113static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
114{
115 int i;
116 spin_unlock(&conf->device_lock);
117 for (i = NR_STRIPE_HASH_LOCKS; i; i--)
118 spin_unlock(conf->hash_locks + i - 1);
119 local_irq_enable();
120}
121
NeilBrownd0dabf72009-03-31 14:39:38 +1100122/* Find first data disk in a raid6 stripe */
123static inline int raid6_d0(struct stripe_head *sh)
124{
NeilBrown67cc2b82009-03-31 14:39:38 +1100125 if (sh->ddf_layout)
126 /* ddf always start from first device */
127 return 0;
128 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100129 if (sh->qd_idx == sh->disks - 1)
130 return 0;
131 else
132 return sh->qd_idx + 1;
133}
NeilBrown16a53ec2006-06-26 00:27:38 -0700134static inline int raid6_next_disk(int disk, int raid_disks)
135{
136 disk++;
137 return (disk < raid_disks) ? disk : 0;
138}
Dan Williamsa4456852007-07-09 11:56:43 -0700139
NeilBrownd0dabf72009-03-31 14:39:38 +1100140/* When walking through the disks in a raid5, starting at raid6_d0,
141 * We need to map each disk to a 'slot', where the data disks are slot
142 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
143 * is raid_disks-1. This help does that mapping.
144 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100145static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
146 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100147{
Dan Williams66295422009-10-19 18:09:32 -0700148 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100149
NeilBrowne4424fe2009-10-16 16:27:34 +1100150 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700151 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100152 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100153 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100154 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100155 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100156 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700157 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100158 return slot;
159}
160
NeilBrownd1688a62011-10-11 16:49:52 +1100161static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Dan Williams600aa102008-06-28 08:32:05 +1000163static int stripe_operations_active(struct stripe_head *sh)
164{
165 return sh->check_state || sh->reconstruct_state ||
166 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
167 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
168}
169
Shaohua Li535ae4e2017-02-15 19:37:32 -0800170static bool stripe_is_lowprio(struct stripe_head *sh)
171{
172 return (test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) ||
173 test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) &&
174 !test_bit(STRIPE_R5C_CACHING, &sh->state);
175}
176
Shaohua Li851c30c2013-08-28 14:30:16 +0800177static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
178{
179 struct r5conf *conf = sh->raid_conf;
180 struct r5worker_group *group;
Shaohua Libfc90cb2013-08-29 15:40:32 +0800181 int thread_cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +0800182 int i, cpu = sh->cpu;
183
184 if (!cpu_online(cpu)) {
185 cpu = cpumask_any(cpu_online_mask);
186 sh->cpu = cpu;
187 }
188
189 if (list_empty(&sh->lru)) {
190 struct r5worker_group *group;
191 group = conf->worker_groups + cpu_to_group(cpu);
Shaohua Li535ae4e2017-02-15 19:37:32 -0800192 if (stripe_is_lowprio(sh))
193 list_add_tail(&sh->lru, &group->loprio_list);
194 else
195 list_add_tail(&sh->lru, &group->handle_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800196 group->stripes_cnt++;
197 sh->group = group;
Shaohua Li851c30c2013-08-28 14:30:16 +0800198 }
199
200 if (conf->worker_cnt_per_group == 0) {
201 md_wakeup_thread(conf->mddev->thread);
202 return;
203 }
204
205 group = conf->worker_groups + cpu_to_group(sh->cpu);
206
Shaohua Libfc90cb2013-08-29 15:40:32 +0800207 group->workers[0].working = true;
208 /* at least one worker should run to avoid race */
209 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
210
211 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
212 /* wakeup more workers */
213 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
214 if (group->workers[i].working == false) {
215 group->workers[i].working = true;
216 queue_work_on(sh->cpu, raid5_wq,
217 &group->workers[i].work);
218 thread_cnt--;
219 }
220 }
Shaohua Li851c30c2013-08-28 14:30:16 +0800221}
222
Shaohua Li566c09c2013-11-14 15:16:17 +1100223static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
224 struct list_head *temp_inactive_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Song Liu1e6d6902016-11-17 15:24:39 -0800226 int i;
227 int injournal = 0; /* number of date pages with R5_InJournal */
228
Shaohua Li4eb788d2012-07-19 16:01:31 +1000229 BUG_ON(!list_empty(&sh->lru));
230 BUG_ON(atomic_read(&conf->active_stripes)==0);
Song Liu1e6d6902016-11-17 15:24:39 -0800231
232 if (r5c_is_writeback(conf->log))
233 for (i = sh->disks; i--; )
234 if (test_bit(R5_InJournal, &sh->dev[i].flags))
235 injournal++;
Song Liua39f7af2016-11-17 15:24:40 -0800236 /*
237 * When quiesce in r5c write back, set STRIPE_HANDLE for stripes with
238 * data in journal, so they are not released to cached lists
239 */
240 if (conf->quiesce && r5c_is_writeback(conf->log) &&
241 !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0) {
242 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
243 r5c_make_stripe_write_out(sh);
244 set_bit(STRIPE_HANDLE, &sh->state);
245 }
Song Liu1e6d6902016-11-17 15:24:39 -0800246
Shaohua Li4eb788d2012-07-19 16:01:31 +1000247 if (test_bit(STRIPE_HANDLE, &sh->state)) {
248 if (test_bit(STRIPE_DELAYED, &sh->state) &&
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500249 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
Shaohua Li4eb788d2012-07-19 16:01:31 +1000250 list_add_tail(&sh->lru, &conf->delayed_list);
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500251 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
Shaohua Li4eb788d2012-07-19 16:01:31 +1000252 sh->bm_seq - conf->seq_write > 0)
253 list_add_tail(&sh->lru, &conf->bitmap_list);
254 else {
255 clear_bit(STRIPE_DELAYED, &sh->state);
256 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Shaohua Li851c30c2013-08-28 14:30:16 +0800257 if (conf->worker_cnt_per_group == 0) {
Shaohua Li535ae4e2017-02-15 19:37:32 -0800258 if (stripe_is_lowprio(sh))
259 list_add_tail(&sh->lru,
260 &conf->loprio_list);
261 else
262 list_add_tail(&sh->lru,
263 &conf->handle_list);
Shaohua Li851c30c2013-08-28 14:30:16 +0800264 } else {
265 raid5_wakeup_stripe_thread(sh);
266 return;
267 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000268 }
269 md_wakeup_thread(conf->mddev->thread);
270 } else {
271 BUG_ON(stripe_operations_active(sh));
272 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
273 if (atomic_dec_return(&conf->preread_active_stripes)
274 < IO_THRESHOLD)
275 md_wakeup_thread(conf->mddev->thread);
276 atomic_dec(&conf->active_stripes);
Song Liu1e6d6902016-11-17 15:24:39 -0800277 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
278 if (!r5c_is_writeback(conf->log))
279 list_add_tail(&sh->lru, temp_inactive_list);
280 else {
281 WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags));
282 if (injournal == 0)
283 list_add_tail(&sh->lru, temp_inactive_list);
284 else if (injournal == conf->raid_disks - conf->max_degraded) {
285 /* full stripe */
286 if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state))
287 atomic_inc(&conf->r5c_cached_full_stripes);
288 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state))
289 atomic_dec(&conf->r5c_cached_partial_stripes);
290 list_add_tail(&sh->lru, &conf->r5c_full_stripe_list);
Song Liua39f7af2016-11-17 15:24:40 -0800291 r5c_check_cached_full_stripe(conf);
Song Liu03b047f2017-01-11 13:39:14 -0800292 } else
293 /*
294 * STRIPE_R5C_PARTIAL_STRIPE is set in
295 * r5c_try_caching_write(). No need to
296 * set it again.
297 */
Song Liu1e6d6902016-11-17 15:24:39 -0800298 list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list);
Song Liu1e6d6902016-11-17 15:24:39 -0800299 }
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302}
NeilBrownd0dabf72009-03-31 14:39:38 +1100303
Shaohua Li566c09c2013-11-14 15:16:17 +1100304static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
305 struct list_head *temp_inactive_list)
Shaohua Li4eb788d2012-07-19 16:01:31 +1000306{
307 if (atomic_dec_and_test(&sh->count))
Shaohua Li566c09c2013-11-14 15:16:17 +1100308 do_release_stripe(conf, sh, temp_inactive_list);
309}
310
311/*
312 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
313 *
314 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
315 * given time. Adding stripes only takes device lock, while deleting stripes
316 * only takes hash lock.
317 */
318static void release_inactive_stripe_list(struct r5conf *conf,
319 struct list_head *temp_inactive_list,
320 int hash)
321{
322 int size;
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800323 bool do_wakeup = false;
Shaohua Li566c09c2013-11-14 15:16:17 +1100324 unsigned long flags;
325
326 if (hash == NR_STRIPE_HASH_LOCKS) {
327 size = NR_STRIPE_HASH_LOCKS;
328 hash = NR_STRIPE_HASH_LOCKS - 1;
329 } else
330 size = 1;
331 while (size) {
332 struct list_head *list = &temp_inactive_list[size - 1];
333
334 /*
Shaohua Li6d036f72015-08-13 14:31:57 -0700335 * We don't hold any lock here yet, raid5_get_active_stripe() might
Shaohua Li566c09c2013-11-14 15:16:17 +1100336 * remove stripes from the list
337 */
338 if (!list_empty_careful(list)) {
339 spin_lock_irqsave(conf->hash_locks + hash, flags);
Shaohua Li4bda5562013-11-14 15:16:17 +1100340 if (list_empty(conf->inactive_list + hash) &&
341 !list_empty(list))
342 atomic_dec(&conf->empty_inactive_list_nr);
Shaohua Li566c09c2013-11-14 15:16:17 +1100343 list_splice_tail_init(list, conf->inactive_list + hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800344 do_wakeup = true;
Shaohua Li566c09c2013-11-14 15:16:17 +1100345 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
346 }
347 size--;
348 hash--;
349 }
350
351 if (do_wakeup) {
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800352 wake_up(&conf->wait_for_stripe);
Yuanhan Liub1b46482015-05-08 18:19:06 +1000353 if (atomic_read(&conf->active_stripes) == 0)
354 wake_up(&conf->wait_for_quiescent);
Shaohua Li566c09c2013-11-14 15:16:17 +1100355 if (conf->retry_read_aligned)
356 md_wakeup_thread(conf->mddev->thread);
357 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000358}
359
Shaohua Li773ca822013-08-27 17:50:39 +0800360/* should hold conf->device_lock already */
Shaohua Li566c09c2013-11-14 15:16:17 +1100361static int release_stripe_list(struct r5conf *conf,
362 struct list_head *temp_inactive_list)
Shaohua Li773ca822013-08-27 17:50:39 +0800363{
Byungchul Parkeae82632017-02-14 16:26:24 +0900364 struct stripe_head *sh, *t;
Shaohua Li773ca822013-08-27 17:50:39 +0800365 int count = 0;
366 struct llist_node *head;
367
368 head = llist_del_all(&conf->released_stripes);
Shaohua Lid265d9d2013-08-28 14:29:05 +0800369 head = llist_reverse_order(head);
Byungchul Parkeae82632017-02-14 16:26:24 +0900370 llist_for_each_entry_safe(sh, t, head, release_list) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100371 int hash;
372
Shaohua Li773ca822013-08-27 17:50:39 +0800373 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
374 smp_mb();
375 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
376 /*
377 * Don't worry the bit is set here, because if the bit is set
378 * again, the count is always > 1. This is true for
379 * STRIPE_ON_UNPLUG_LIST bit too.
380 */
Shaohua Li566c09c2013-11-14 15:16:17 +1100381 hash = sh->hash_lock_index;
382 __release_stripe(conf, sh, &temp_inactive_list[hash]);
Shaohua Li773ca822013-08-27 17:50:39 +0800383 count++;
384 }
385
386 return count;
387}
388
Shaohua Li6d036f72015-08-13 14:31:57 -0700389void raid5_release_stripe(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
NeilBrownd1688a62011-10-11 16:49:52 +1100391 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 unsigned long flags;
Shaohua Li566c09c2013-11-14 15:16:17 +1100393 struct list_head list;
394 int hash;
Shaohua Li773ca822013-08-27 17:50:39 +0800395 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700396
Eivind Sartocf170f32014-05-28 13:39:23 +1000397 /* Avoid release_list until the last reference.
398 */
399 if (atomic_add_unless(&sh->count, -1, 1))
400 return;
401
majianpengad4068d2013-11-14 15:16:15 +1100402 if (unlikely(!conf->mddev->thread) ||
403 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
Shaohua Li773ca822013-08-27 17:50:39 +0800404 goto slow_path;
405 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
406 if (wakeup)
407 md_wakeup_thread(conf->mddev->thread);
408 return;
409slow_path:
Shaohua Li4eb788d2012-07-19 16:01:31 +1000410 local_irq_save(flags);
Shaohua Li773ca822013-08-27 17:50:39 +0800411 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Shaohua Li4eb788d2012-07-19 16:01:31 +1000412 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100413 INIT_LIST_HEAD(&list);
414 hash = sh->hash_lock_index;
415 do_release_stripe(conf, sh, &list);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000416 spin_unlock(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +1100417 release_inactive_stripe_list(conf, &list, hash);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000418 }
419 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
NeilBrownfccddba2006-01-06 00:20:33 -0800422static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Dan Williams45b42332007-07-09 11:56:43 -0700424 pr_debug("remove_hash(), stripe %llu\n",
425 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
NeilBrownfccddba2006-01-06 00:20:33 -0800427 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
NeilBrownd1688a62011-10-11 16:49:52 +1100430static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
NeilBrownfccddba2006-01-06 00:20:33 -0800432 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Dan Williams45b42332007-07-09 11:56:43 -0700434 pr_debug("insert_hash(), stripe %llu\n",
435 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
NeilBrownfccddba2006-01-06 00:20:33 -0800437 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/* find an idle stripe, make sure it is unhashed, and return it. */
Shaohua Li566c09c2013-11-14 15:16:17 +1100441static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 struct stripe_head *sh = NULL;
444 struct list_head *first;
445
Shaohua Li566c09c2013-11-14 15:16:17 +1100446 if (list_empty(conf->inactive_list + hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 goto out;
Shaohua Li566c09c2013-11-14 15:16:17 +1100448 first = (conf->inactive_list + hash)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 sh = list_entry(first, struct stripe_head, lru);
450 list_del_init(first);
451 remove_hash(sh);
452 atomic_inc(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100453 BUG_ON(hash != sh->hash_lock_index);
Shaohua Li4bda5562013-11-14 15:16:17 +1100454 if (list_empty(conf->inactive_list + hash))
455 atomic_inc(&conf->empty_inactive_list_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456out:
457 return sh;
458}
459
NeilBrowne4e11e32010-06-16 16:45:16 +1000460static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 struct page *p;
463 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000464 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
NeilBrowne4e11e32010-06-16 16:45:16 +1000466 for (i = 0; i < num ; i++) {
Shaohua Lid592a992014-05-21 17:57:44 +0800467 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 p = sh->dev[i].page;
469 if (!p)
470 continue;
471 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800472 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474}
475
NeilBrowna9683a72015-02-25 12:02:51 +1100476static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000479 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
NeilBrowne4e11e32010-06-16 16:45:16 +1000481 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 struct page *page;
483
NeilBrowna9683a72015-02-25 12:02:51 +1100484 if (!(page = alloc_page(gfp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 1;
486 }
487 sh->dev[i].page = page;
Shaohua Lid592a992014-05-21 17:57:44 +0800488 sh->dev[i].orig_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Artur Paszkiewicz3418d032017-03-09 09:59:59 +0100490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return 0;
492}
493
NeilBrown784052e2009-03-31 15:19:07 +1100494static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100495static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100496 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
NeilBrownb5663ba2009-03-31 14:39:38 +1100498static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
NeilBrownd1688a62011-10-11 16:49:52 +1100500 struct r5conf *conf = sh->raid_conf;
Shaohua Li566c09c2013-11-14 15:16:17 +1100501 int i, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200503 BUG_ON(atomic_read(&sh->count) != 0);
504 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000505 BUG_ON(stripe_operations_active(sh));
shli@kernel.org59fc6302014-12-15 12:57:03 +1100506 BUG_ON(sh->batch_head);
Dan Williamsd84e0f12007-01-02 13:52:30 -0700507
Dan Williams45b42332007-07-09 11:56:43 -0700508 pr_debug("init_stripe called, stripe %llu\n",
Markus Stockhausenb8e6a152014-08-23 20:19:27 +1000509 (unsigned long long)sector);
Shaohua Li566c09c2013-11-14 15:16:17 +1100510retry:
511 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100512 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100513 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100515 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 sh->state = 0;
517
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800518 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 struct r5dev *dev = &sh->dev[i];
520
Dan Williamsd84e0f12007-01-02 13:52:30 -0700521 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 test_bit(R5_LOCKED, &dev->flags)) {
NeilBrowncc6167b2016-11-02 14:16:50 +1100523 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700525 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000527 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100530 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100532 if (read_seqcount_retry(&conf->gen_lock, seq))
533 goto retry;
shli@kernel.org7a87f432014-12-15 12:57:03 +1100534 sh->overwrite_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800536 sh->cpu = smp_processor_id();
shli@kernel.orgda41ba62014-12-15 12:57:03 +1100537 set_bit(STRIPE_BATCH_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
NeilBrownd1688a62011-10-11 16:49:52 +1100540static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100541 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 struct stripe_head *sh;
544
Dan Williams45b42332007-07-09 11:56:43 -0700545 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800546 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100547 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700549 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return NULL;
551}
552
NeilBrown674806d2010-06-16 17:17:53 +1000553/*
554 * Need to check if array has failed when deciding whether to:
555 * - start an array
556 * - remove non-faulty devices
557 * - add a spare
558 * - allow a reshape
559 * This determination is simple when no reshape is happening.
560 * However if there is a reshape, we need to carefully check
561 * both the before and after sections.
562 * This is because some failed devices may only affect one
563 * of the two sections, and some non-in_sync devices may
564 * be insync in the section most affected by failed devices.
565 */
Song Liu2e38a372017-01-24 10:45:30 -0800566int raid5_calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000567{
NeilBrown908f4fb2011-12-23 10:17:50 +1100568 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000569 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000570
571 rcu_read_lock();
572 degraded = 0;
573 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100574 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000575 if (rdev && test_bit(Faulty, &rdev->flags))
576 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000577 if (!rdev || test_bit(Faulty, &rdev->flags))
578 degraded++;
579 else if (test_bit(In_sync, &rdev->flags))
580 ;
581 else
582 /* not in-sync or faulty.
583 * If the reshape increases the number of devices,
584 * this is being recovered by the reshape, so
585 * this 'previous' section is not in_sync.
586 * If the number of devices is being reduced however,
587 * the device can only be part of the array if
588 * we are reverting a reshape, so this section will
589 * be in-sync.
590 */
591 if (conf->raid_disks >= conf->previous_raid_disks)
592 degraded++;
593 }
594 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100595 if (conf->raid_disks == conf->previous_raid_disks)
596 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000597 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100598 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000599 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100600 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000601 if (rdev && test_bit(Faulty, &rdev->flags))
602 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000603 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100604 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000605 else if (test_bit(In_sync, &rdev->flags))
606 ;
607 else
608 /* not in-sync or faulty.
609 * If reshape increases the number of devices, this
610 * section has already been recovered, else it
611 * almost certainly hasn't.
612 */
613 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100614 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000615 }
616 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100617 if (degraded2 > degraded)
618 return degraded2;
619 return degraded;
620}
621
622static int has_failed(struct r5conf *conf)
623{
624 int degraded;
625
626 if (conf->mddev->reshape_position == MaxSector)
627 return conf->mddev->degraded > conf->max_degraded;
628
Song Liu2e38a372017-01-24 10:45:30 -0800629 degraded = raid5_calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000630 if (degraded > conf->max_degraded)
631 return 1;
632 return 0;
633}
634
Shaohua Li6d036f72015-08-13 14:31:57 -0700635struct stripe_head *
636raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
637 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct stripe_head *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +1100640 int hash = stripe_hash_locks_hash(sector);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800641 int inc_empty_inactive_list_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Dan Williams45b42332007-07-09 11:56:43 -0700643 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Shaohua Li566c09c2013-11-14 15:16:17 +1100645 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 do {
Yuanhan Liub1b46482015-05-08 18:19:06 +1000648 wait_event_lock_irq(conf->wait_for_quiescent,
NeilBrowna8c906c2009-06-09 14:39:59 +1000649 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100650 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100651 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (!sh) {
NeilBrownedbe83a2015-02-26 12:47:56 +1100653 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100654 sh = get_free_stripe(conf, hash);
Shaohua Li713bc5c2015-05-28 17:33:47 -0700655 if (!sh && !test_bit(R5_DID_ALLOC,
656 &conf->cache_state))
NeilBrownedbe83a2015-02-26 12:47:56 +1100657 set_bit(R5_ALLOC_MORE,
658 &conf->cache_state);
659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (noblock && sh == NULL)
661 break;
Song Liua39f7af2016-11-17 15:24:40 -0800662
663 r5c_check_stripe_cache_usage(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (!sh) {
NeilBrown54233992015-02-26 12:21:04 +1100665 set_bit(R5_INACTIVE_BLOCKED,
666 &conf->cache_state);
Song Liua39f7af2016-11-17 15:24:40 -0800667 r5l_wake_reclaim(conf->log, 0);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800668 wait_event_lock_irq(
669 conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +1100670 !list_empty(conf->inactive_list + hash) &&
671 (atomic_read(&conf->active_stripes)
672 < (conf->max_nr_stripes * 3 / 4)
NeilBrown54233992015-02-26 12:21:04 +1100673 || !test_bit(R5_INACTIVE_BLOCKED,
674 &conf->cache_state)),
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800675 *(conf->hash_locks + hash));
NeilBrown54233992015-02-26 12:21:04 +1100676 clear_bit(R5_INACTIVE_BLOCKED,
677 &conf->cache_state);
NeilBrown7da9d452014-01-22 11:45:03 +1100678 } else {
NeilBrownb5663ba2009-03-31 14:39:38 +1100679 init_stripe(sh, sector, previous);
NeilBrown7da9d452014-01-22 11:45:03 +1100680 atomic_inc(&sh->count);
681 }
Shaohua Lie240c182014-04-09 11:27:42 +0800682 } else if (!atomic_inc_not_zero(&sh->count)) {
NeilBrown6d183de2013-11-28 10:55:27 +1100683 spin_lock(&conf->device_lock);
Shaohua Lie240c182014-04-09 11:27:42 +0800684 if (!atomic_read(&sh->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (!test_bit(STRIPE_HANDLE, &sh->state))
686 atomic_inc(&conf->active_stripes);
NeilBrown5af9bef2014-01-14 15:16:10 +1100687 BUG_ON(list_empty(&sh->lru) &&
688 !test_bit(STRIPE_EXPANDING, &sh->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800689 inc_empty_inactive_list_flag = 0;
690 if (!list_empty(conf->inactive_list + hash))
691 inc_empty_inactive_list_flag = 1;
NeilBrown16a53ec2006-06-26 00:27:38 -0700692 list_del_init(&sh->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800693 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
694 atomic_inc(&conf->empty_inactive_list_nr);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800695 if (sh->group) {
696 sh->group->stripes_cnt--;
697 sh->group = NULL;
698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
NeilBrown7da9d452014-01-22 11:45:03 +1100700 atomic_inc(&sh->count);
NeilBrown6d183de2013-11-28 10:55:27 +1100701 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 }
703 } while (sh == NULL);
704
Shaohua Li566c09c2013-11-14 15:16:17 +1100705 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return sh;
707}
708
shli@kernel.org7a87f432014-12-15 12:57:03 +1100709static bool is_full_stripe_write(struct stripe_head *sh)
710{
711 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
712 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
713}
714
shli@kernel.org59fc6302014-12-15 12:57:03 +1100715static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
716{
717 local_irq_disable();
718 if (sh1 > sh2) {
719 spin_lock(&sh2->stripe_lock);
720 spin_lock_nested(&sh1->stripe_lock, 1);
721 } else {
722 spin_lock(&sh1->stripe_lock);
723 spin_lock_nested(&sh2->stripe_lock, 1);
724 }
725}
726
727static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
728{
729 spin_unlock(&sh1->stripe_lock);
730 spin_unlock(&sh2->stripe_lock);
731 local_irq_enable();
732}
733
734/* Only freshly new full stripe normal write stripe can be added to a batch list */
735static bool stripe_can_batch(struct stripe_head *sh)
736{
Shaohua Li9c3e3332015-08-13 14:32:02 -0700737 struct r5conf *conf = sh->raid_conf;
738
Artur Paszkiewicz3418d032017-03-09 09:59:59 +0100739 if (conf->log || raid5_has_ppl(conf))
Shaohua Li9c3e3332015-08-13 14:32:02 -0700740 return false;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100741 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
NeilBrownd0852df52015-05-27 08:43:45 +1000742 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
shli@kernel.org59fc6302014-12-15 12:57:03 +1100743 is_full_stripe_write(sh);
744}
745
746/* we only do back search */
747static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
748{
749 struct stripe_head *head;
750 sector_t head_sector, tmp_sec;
751 int hash;
752 int dd_idx;
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800753 int inc_empty_inactive_list_flag;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100754
shli@kernel.org59fc6302014-12-15 12:57:03 +1100755 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
756 tmp_sec = sh->sector;
757 if (!sector_div(tmp_sec, conf->chunk_sectors))
758 return;
759 head_sector = sh->sector - STRIPE_SECTORS;
760
761 hash = stripe_hash_locks_hash(head_sector);
762 spin_lock_irq(conf->hash_locks + hash);
763 head = __find_stripe(conf, head_sector, conf->generation);
764 if (head && !atomic_inc_not_zero(&head->count)) {
765 spin_lock(&conf->device_lock);
766 if (!atomic_read(&head->count)) {
767 if (!test_bit(STRIPE_HANDLE, &head->state))
768 atomic_inc(&conf->active_stripes);
769 BUG_ON(list_empty(&head->lru) &&
770 !test_bit(STRIPE_EXPANDING, &head->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800771 inc_empty_inactive_list_flag = 0;
772 if (!list_empty(conf->inactive_list + hash))
773 inc_empty_inactive_list_flag = 1;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100774 list_del_init(&head->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800775 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
776 atomic_inc(&conf->empty_inactive_list_nr);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100777 if (head->group) {
778 head->group->stripes_cnt--;
779 head->group = NULL;
780 }
781 }
782 atomic_inc(&head->count);
783 spin_unlock(&conf->device_lock);
784 }
785 spin_unlock_irq(conf->hash_locks + hash);
786
787 if (!head)
788 return;
789 if (!stripe_can_batch(head))
790 goto out;
791
792 lock_two_stripes(head, sh);
793 /* clear_batch_ready clear the flag */
794 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
795 goto unlock_out;
796
797 if (sh->batch_head)
798 goto unlock_out;
799
800 dd_idx = 0;
801 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
802 dd_idx++;
Jens Axboe1eff9d32016-08-05 15:35:16 -0600803 if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
Mike Christie796a5cf2016-06-05 14:32:07 -0500804 bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
shli@kernel.org59fc6302014-12-15 12:57:03 +1100805 goto unlock_out;
806
807 if (head->batch_head) {
808 spin_lock(&head->batch_head->batch_lock);
809 /* This batch list is already running */
810 if (!stripe_can_batch(head)) {
811 spin_unlock(&head->batch_head->batch_lock);
812 goto unlock_out;
813 }
814
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);
821
822 sh->batch_head = head->batch_head;
823 } else {
824 head->batch_head = head;
825 sh->batch_head = head->batch_head;
826 spin_lock(&head->batch_lock);
827 list_add_tail(&sh->batch_list, &head->batch_list);
828 spin_unlock(&head->batch_lock);
829 }
830
831 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
832 if (atomic_dec_return(&conf->preread_active_stripes)
833 < IO_THRESHOLD)
834 md_wakeup_thread(conf->mddev->thread);
835
NeilBrown2b6b2452015-05-21 15:10:01 +1000836 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
837 int seq = sh->bm_seq;
838 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
839 sh->batch_head->bm_seq > seq)
840 seq = sh->batch_head->bm_seq;
841 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
842 sh->batch_head->bm_seq = seq;
843 }
844
shli@kernel.org59fc6302014-12-15 12:57:03 +1100845 atomic_inc(&sh->count);
846unlock_out:
847 unlock_two_stripes(head, sh);
848out:
Shaohua Li6d036f72015-08-13 14:31:57 -0700849 raid5_release_stripe(head);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100850}
851
NeilBrown05616be2012-05-21 09:27:00 +1000852/* Determine if 'data_offset' or 'new_data_offset' should be used
853 * in this stripe_head.
854 */
855static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
856{
857 sector_t progress = conf->reshape_progress;
858 /* Need a memory barrier to make sure we see the value
859 * of conf->generation, or ->data_offset that was set before
860 * reshape_progress was updated.
861 */
862 smp_rmb();
863 if (progress == MaxSector)
864 return 0;
865 if (sh->generation == conf->generation - 1)
866 return 0;
867 /* We are in a reshape, and this is a new-generation stripe,
868 * so use new_data_offset.
869 */
870 return 1;
871}
872
Shaohua Liaaf9f122017-03-03 22:06:12 -0800873static void dispatch_bio_list(struct bio_list *tmp)
Shaohua Li765d7042017-01-04 09:33:23 -0800874{
Shaohua Li765d7042017-01-04 09:33:23 -0800875 struct bio *bio;
876
Shaohua Liaaf9f122017-03-03 22:06:12 -0800877 while ((bio = bio_list_pop(tmp)))
Shaohua Li765d7042017-01-04 09:33:23 -0800878 generic_make_request(bio);
879}
880
Shaohua Liaaf9f122017-03-03 22:06:12 -0800881static int cmp_stripe(void *priv, struct list_head *a, struct list_head *b)
Shaohua Li765d7042017-01-04 09:33:23 -0800882{
Shaohua Liaaf9f122017-03-03 22:06:12 -0800883 const struct r5pending_data *da = list_entry(a,
884 struct r5pending_data, sibling);
885 const struct r5pending_data *db = list_entry(b,
886 struct r5pending_data, sibling);
887 if (da->sector > db->sector)
888 return 1;
889 if (da->sector < db->sector)
890 return -1;
891 return 0;
892}
893
894static void dispatch_defer_bios(struct r5conf *conf, int target,
895 struct bio_list *list)
896{
897 struct r5pending_data *data;
898 struct list_head *first, *next = NULL;
899 int cnt = 0;
900
901 if (conf->pending_data_cnt == 0)
Shaohua Li765d7042017-01-04 09:33:23 -0800902 return;
Shaohua Liaaf9f122017-03-03 22:06:12 -0800903
904 list_sort(NULL, &conf->pending_list, cmp_stripe);
905
906 first = conf->pending_list.next;
907
908 /* temporarily move the head */
909 if (conf->next_pending_data)
910 list_move_tail(&conf->pending_list,
911 &conf->next_pending_data->sibling);
912
913 while (!list_empty(&conf->pending_list)) {
914 data = list_first_entry(&conf->pending_list,
915 struct r5pending_data, sibling);
916 if (&data->sibling == first)
917 first = data->sibling.next;
918 next = data->sibling.next;
919
920 bio_list_merge(list, &data->bios);
921 list_move(&data->sibling, &conf->free_list);
922 cnt++;
923 if (cnt >= target)
924 break;
Shaohua Li765d7042017-01-04 09:33:23 -0800925 }
Shaohua Liaaf9f122017-03-03 22:06:12 -0800926 conf->pending_data_cnt -= cnt;
927 BUG_ON(conf->pending_data_cnt < 0 || cnt < target);
928
929 if (next != &conf->pending_list)
930 conf->next_pending_data = list_entry(next,
931 struct r5pending_data, sibling);
932 else
933 conf->next_pending_data = NULL;
934 /* list isn't empty */
935 if (first != &conf->pending_list)
936 list_move_tail(&conf->pending_list, first);
937}
938
939static void flush_deferred_bios(struct r5conf *conf)
940{
941 struct bio_list tmp = BIO_EMPTY_LIST;
942
943 if (conf->pending_data_cnt == 0)
944 return;
945
Shaohua Li765d7042017-01-04 09:33:23 -0800946 spin_lock(&conf->pending_bios_lock);
Shaohua Liaaf9f122017-03-03 22:06:12 -0800947 dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp);
948 BUG_ON(conf->pending_data_cnt != 0);
Shaohua Li765d7042017-01-04 09:33:23 -0800949 spin_unlock(&conf->pending_bios_lock);
Shaohua Liaaf9f122017-03-03 22:06:12 -0800950
951 dispatch_bio_list(&tmp);
952}
953
954static void defer_issue_bios(struct r5conf *conf, sector_t sector,
955 struct bio_list *bios)
956{
957 struct bio_list tmp = BIO_EMPTY_LIST;
958 struct r5pending_data *ent;
959
960 spin_lock(&conf->pending_bios_lock);
961 ent = list_first_entry(&conf->free_list, struct r5pending_data,
962 sibling);
963 list_move_tail(&ent->sibling, &conf->pending_list);
964 ent->sector = sector;
965 bio_list_init(&ent->bios);
966 bio_list_merge(&ent->bios, bios);
967 conf->pending_data_cnt++;
968 if (conf->pending_data_cnt >= PENDING_IO_MAX)
969 dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp);
970
971 spin_unlock(&conf->pending_bios_lock);
972
973 dispatch_bio_list(&tmp);
Shaohua Li765d7042017-01-04 09:33:23 -0800974}
975
NeilBrown6712ecf2007-09-27 12:47:43 +0200976static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200977raid5_end_read_request(struct bio *bi);
NeilBrown6712ecf2007-09-27 12:47:43 +0200978static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200979raid5_end_write_request(struct bio *bi);
Dan Williams91c00922007-01-02 13:52:30 -0700980
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000981static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700982{
NeilBrownd1688a62011-10-11 16:49:52 +1100983 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700984 int i, disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100985 struct stripe_head *head_sh = sh;
Shaohua Liaaf9f122017-03-03 22:06:12 -0800986 struct bio_list pending_bios = BIO_EMPTY_LIST;
987 bool should_defer;
Dan Williams91c00922007-01-02 13:52:30 -0700988
989 might_sleep();
990
Artur Paszkiewiczff875732017-03-09 09:59:58 +0100991 if (log_stripe(sh, s) == 0)
992 return;
Song Liu1e6d6902016-11-17 15:24:39 -0800993
Shaohua Liaaf9f122017-03-03 22:06:12 -0800994 should_defer = conf->batch_bio_dispatch && conf->group_cnt;
995
Dan Williams91c00922007-01-02 13:52:30 -0700996 for (i = disks; i--; ) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500997 int op, op_flags = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +1100998 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100999 struct bio *bi, *rbi;
1000 struct md_rdev *rdev, *rrdev = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001001
1002 sh = head_sh;
Tejun Heoe9c74692010-09-03 11:56:18 +02001003 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001004 op = REQ_OP_WRITE;
Tejun Heoe9c74692010-09-03 11:56:18 +02001005 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001006 op_flags = REQ_FUA;
Shaohua Li9e4447682012-10-11 13:49:49 +11001007 if (test_bit(R5_Discard, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001008 op = REQ_OP_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +02001009 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001010 op = REQ_OP_READ;
NeilBrown9a3e1102011-12-23 10:17:53 +11001011 else if (test_and_clear_bit(R5_WantReplace,
1012 &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001013 op = REQ_OP_WRITE;
NeilBrown9a3e1102011-12-23 10:17:53 +11001014 replace_only = 1;
1015 } else
Dan Williams91c00922007-01-02 13:52:30 -07001016 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +10001017 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001018 op_flags |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -07001019
shli@kernel.org59fc6302014-12-15 12:57:03 +11001020again:
Dan Williams91c00922007-01-02 13:52:30 -07001021 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +11001022 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -07001023
Dan Williams91c00922007-01-02 13:52:30 -07001024 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +11001025 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +11001026 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
1027 rdev = rcu_dereference(conf->disks[i].rdev);
1028 if (!rdev) {
1029 rdev = rrdev;
1030 rrdev = NULL;
1031 }
Mike Christie796a5cf2016-06-05 14:32:07 -05001032 if (op_is_write(op)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001033 if (replace_only)
1034 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +11001035 if (rdev == rrdev)
1036 /* We raced and saw duplicates */
1037 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +11001038 } else {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001039 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +11001040 rdev = rrdev;
1041 rrdev = NULL;
1042 }
NeilBrown977df362011-12-23 10:17:53 +11001043
Dan Williams91c00922007-01-02 13:52:30 -07001044 if (rdev && test_bit(Faulty, &rdev->flags))
1045 rdev = NULL;
1046 if (rdev)
1047 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +11001048 if (rrdev && test_bit(Faulty, &rrdev->flags))
1049 rrdev = NULL;
1050 if (rrdev)
1051 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -07001052 rcu_read_unlock();
1053
NeilBrown73e92e52011-07-28 11:39:22 +10001054 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +11001055 * need to check for writes. We never accept write errors
1056 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +10001057 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001058 while (op_is_write(op) && rdev &&
NeilBrown73e92e52011-07-28 11:39:22 +10001059 test_bit(WriteErrorSeen, &rdev->flags)) {
1060 sector_t first_bad;
1061 int bad_sectors;
1062 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
1063 &first_bad, &bad_sectors);
1064 if (!bad)
1065 break;
1066
1067 if (bad < 0) {
1068 set_bit(BlockedBadBlocks, &rdev->flags);
1069 if (!conf->mddev->external &&
Shaohua Li29530792016-12-08 15:48:19 -08001070 conf->mddev->sb_flags) {
NeilBrown73e92e52011-07-28 11:39:22 +10001071 /* It is very unlikely, but we might
1072 * still need to write out the
1073 * bad block log - better give it
1074 * a chance*/
1075 md_check_recovery(conf->mddev);
1076 }
majianpeng18507532012-07-03 12:11:54 +10001077 /*
1078 * Because md_wait_for_blocked_rdev
1079 * will dec nr_pending, we must
1080 * increment it first.
1081 */
1082 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +10001083 md_wait_for_blocked_rdev(rdev, conf->mddev);
1084 } else {
1085 /* Acknowledged bad block - skip the write */
1086 rdev_dec_pending(rdev, conf->mddev);
1087 rdev = NULL;
1088 }
1089 }
1090
Dan Williams91c00922007-01-02 13:52:30 -07001091 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001092 if (s->syncing || s->expanding || s->expanded
1093 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -07001094 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1095
Dan Williams2b7497f2008-06-28 08:31:52 +10001096 set_bit(STRIPE_IO_STARTED, &sh->state);
1097
Dan Williams91c00922007-01-02 13:52:30 -07001098 bi->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05001099 bio_set_op_attrs(bi, op, op_flags);
1100 bi->bi_end_io = op_is_write(op)
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001101 ? raid5_end_write_request
1102 : raid5_end_read_request;
1103 bi->bi_private = sh;
1104
Mike Christie6296b962016-06-05 14:32:21 -05001105 pr_debug("%s: for %llu schedule op %d on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001106 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001107 bi->bi_opf, i);
Dan Williams91c00922007-01-02 13:52:30 -07001108 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001109 if (sh != head_sh)
1110 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001111 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001112 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001113 + rdev->new_data_offset);
1114 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001115 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001116 + rdev->data_offset);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001117 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
Jens Axboe1eff9d32016-08-05 15:35:16 -06001118 bi->bi_opf |= REQ_NOMERGE;
majianpeng3f9e7c12012-07-31 10:04:21 +10001119
Shaohua Lid592a992014-05-21 17:57:44 +08001120 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1121 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
Song Liu86aa1392017-01-12 17:22:41 -08001122
1123 if (!op_is_write(op) &&
1124 test_bit(R5_InJournal, &sh->dev[i].flags))
1125 /*
1126 * issuing read for a page in journal, this
1127 * must be preparing for prexor in rmw; read
1128 * the data into orig_page
1129 */
1130 sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
1131 else
1132 sh->dev[i].vec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001133 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001134 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1135 bi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001136 bi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001137 /*
1138 * If this is discard request, set bi_vcnt 0. We don't
1139 * want to confuse SCSI because SCSI will replace payload
1140 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001141 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001142 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +11001143 if (rrdev)
1144 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001145
1146 if (conf->mddev->gendisk)
1147 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
1148 bi, disk_devt(conf->mddev->gendisk),
1149 sh->dev[i].sector);
Shaohua Liaaf9f122017-03-03 22:06:12 -08001150 if (should_defer && op_is_write(op))
1151 bio_list_add(&pending_bios, bi);
1152 else
1153 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +11001154 }
1155 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001156 if (s->syncing || s->expanding || s->expanded
1157 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +11001158 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
1159
1160 set_bit(STRIPE_IO_STARTED, &sh->state);
1161
1162 rbi->bi_bdev = rrdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05001163 bio_set_op_attrs(rbi, op, op_flags);
1164 BUG_ON(!op_is_write(op));
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001165 rbi->bi_end_io = raid5_end_write_request;
1166 rbi->bi_private = sh;
1167
Mike Christie6296b962016-06-05 14:32:21 -05001168 pr_debug("%s: for %llu schedule op %d on "
NeilBrown977df362011-12-23 10:17:53 +11001169 "replacement disc %d\n",
1170 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001171 rbi->bi_opf, i);
NeilBrown977df362011-12-23 10:17:53 +11001172 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001173 if (sh != head_sh)
1174 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001175 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001176 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001177 + rrdev->new_data_offset);
1178 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001179 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001180 + rrdev->data_offset);
Shaohua Lid592a992014-05-21 17:57:44 +08001181 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1182 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1183 sh->dev[i].rvec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001184 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +11001185 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1186 rbi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001187 rbi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001188 /*
1189 * If this is discard request, set bi_vcnt 0. We don't
1190 * want to confuse SCSI because SCSI will replace payload
1191 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001192 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001193 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001194 if (conf->mddev->gendisk)
1195 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
1196 rbi, disk_devt(conf->mddev->gendisk),
1197 sh->dev[i].sector);
Shaohua Liaaf9f122017-03-03 22:06:12 -08001198 if (should_defer && op_is_write(op))
1199 bio_list_add(&pending_bios, rbi);
1200 else
1201 generic_make_request(rbi);
NeilBrown977df362011-12-23 10:17:53 +11001202 }
1203 if (!rdev && !rrdev) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001204 if (op_is_write(op))
Dan Williams91c00922007-01-02 13:52:30 -07001205 set_bit(STRIPE_DEGRADED, &sh->state);
Mike Christie6296b962016-06-05 14:32:21 -05001206 pr_debug("skip op %d on disc %d for sector %llu\n",
Jens Axboe1eff9d32016-08-05 15:35:16 -06001207 bi->bi_opf, i, (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001208 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1209 set_bit(STRIPE_HANDLE, &sh->state);
1210 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001211
1212 if (!head_sh->batch_head)
1213 continue;
1214 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1215 batch_list);
1216 if (sh != head_sh)
1217 goto again;
Dan Williams91c00922007-01-02 13:52:30 -07001218 }
Shaohua Liaaf9f122017-03-03 22:06:12 -08001219
1220 if (should_defer && !bio_list_empty(&pending_bios))
1221 defer_issue_bios(conf, head_sh->sector, &pending_bios);
Dan Williams91c00922007-01-02 13:52:30 -07001222}
1223
1224static struct dma_async_tx_descriptor *
Shaohua Lid592a992014-05-21 17:57:44 +08001225async_copy_data(int frombio, struct bio *bio, struct page **page,
1226 sector_t sector, struct dma_async_tx_descriptor *tx,
Song Liu1e6d6902016-11-17 15:24:39 -08001227 struct stripe_head *sh, int no_skipcopy)
Dan Williams91c00922007-01-02 13:52:30 -07001228{
Kent Overstreet79886132013-11-23 17:19:00 -08001229 struct bio_vec bvl;
1230 struct bvec_iter iter;
Dan Williams91c00922007-01-02 13:52:30 -07001231 struct page *bio_page;
Dan Williams91c00922007-01-02 13:52:30 -07001232 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -07001233 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -07001234 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001235
Kent Overstreet4f024f32013-10-11 15:44:27 -07001236 if (bio->bi_iter.bi_sector >= sector)
1237 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
Dan Williams91c00922007-01-02 13:52:30 -07001238 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001239 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -07001240
Dan Williams0403e382009-09-08 17:42:50 -07001241 if (frombio)
1242 flags |= ASYNC_TX_FENCE;
1243 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1244
Kent Overstreet79886132013-11-23 17:19:00 -08001245 bio_for_each_segment(bvl, bio, iter) {
1246 int len = bvl.bv_len;
Dan Williams91c00922007-01-02 13:52:30 -07001247 int clen;
1248 int b_offset = 0;
1249
1250 if (page_offset < 0) {
1251 b_offset = -page_offset;
1252 page_offset += b_offset;
1253 len -= b_offset;
1254 }
1255
1256 if (len > 0 && page_offset + len > STRIPE_SIZE)
1257 clen = STRIPE_SIZE - page_offset;
1258 else
1259 clen = len;
1260
1261 if (clen > 0) {
Kent Overstreet79886132013-11-23 17:19:00 -08001262 b_offset += bvl.bv_offset;
1263 bio_page = bvl.bv_page;
Shaohua Lid592a992014-05-21 17:57:44 +08001264 if (frombio) {
1265 if (sh->raid_conf->skip_copy &&
1266 b_offset == 0 && page_offset == 0 &&
Song Liu1e6d6902016-11-17 15:24:39 -08001267 clen == STRIPE_SIZE &&
1268 !no_skipcopy)
Shaohua Lid592a992014-05-21 17:57:44 +08001269 *page = bio_page;
1270 else
1271 tx = async_memcpy(*page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001272 b_offset, clen, &submit);
Shaohua Lid592a992014-05-21 17:57:44 +08001273 } else
1274 tx = async_memcpy(bio_page, *page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001275 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001276 }
Dan Williamsa08abd82009-06-03 11:43:59 -07001277 /* chain the operations */
1278 submit.depend_tx = tx;
1279
Dan Williams91c00922007-01-02 13:52:30 -07001280 if (clen < len) /* hit end of page */
1281 break;
1282 page_offset += len;
1283 }
1284
1285 return tx;
1286}
1287
1288static void ops_complete_biofill(void *stripe_head_ref)
1289{
1290 struct stripe_head *sh = stripe_head_ref;
Dan Williamse4d84902007-09-24 10:06:13 -07001291 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001292
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001293 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001294 (unsigned long long)sh->sector);
1295
1296 /* clear completed biofills */
1297 for (i = sh->disks; i--; ) {
1298 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001299
1300 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001301 /* and check if we need to reply to a read request,
1302 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001303 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001304 */
1305 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001306 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001307
Dan Williams91c00922007-01-02 13:52:30 -07001308 BUG_ON(!dev->read);
1309 rbi = dev->read;
1310 dev->read = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001311 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001312 dev->sector + STRIPE_SECTORS) {
1313 rbi2 = r5_next_bio(rbi, dev->sector);
NeilBrown016c76a2017-03-15 14:05:13 +11001314 bio_endio(rbi);
Dan Williams91c00922007-01-02 13:52:30 -07001315 rbi = rbi2;
1316 }
1317 }
1318 }
Dan Williams83de75c2008-06-28 08:31:58 +10001319 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001320
Dan Williamse4d84902007-09-24 10:06:13 -07001321 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001322 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001323}
1324
1325static void ops_run_biofill(struct stripe_head *sh)
1326{
1327 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001328 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001329 int i;
1330
shli@kernel.org59fc6302014-12-15 12:57:03 +11001331 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001332 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001333 (unsigned long long)sh->sector);
1334
1335 for (i = sh->disks; i--; ) {
1336 struct r5dev *dev = &sh->dev[i];
1337 if (test_bit(R5_Wantfill, &dev->flags)) {
1338 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001339 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001340 dev->read = rbi = dev->toread;
1341 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001342 spin_unlock_irq(&sh->stripe_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001343 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001344 dev->sector + STRIPE_SECTORS) {
Shaohua Lid592a992014-05-21 17:57:44 +08001345 tx = async_copy_data(0, rbi, &dev->page,
Song Liu1e6d6902016-11-17 15:24:39 -08001346 dev->sector, tx, sh, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001347 rbi = r5_next_bio(rbi, dev->sector);
1348 }
1349 }
1350 }
1351
1352 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001353 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1354 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001355}
1356
Dan Williams4e7d2c02009-08-29 19:13:11 -07001357static void mark_target_uptodate(struct stripe_head *sh, int target)
1358{
1359 struct r5dev *tgt;
1360
1361 if (target < 0)
1362 return;
1363
1364 tgt = &sh->dev[target];
1365 set_bit(R5_UPTODATE, &tgt->flags);
1366 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1367 clear_bit(R5_Wantcompute, &tgt->flags);
1368}
1369
Dan Williamsac6b53b2009-07-14 13:40:19 -07001370static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001371{
1372 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001373
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001374 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001375 (unsigned long long)sh->sector);
1376
Dan Williamsac6b53b2009-07-14 13:40:19 -07001377 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001378 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001379 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001380
Dan Williamsecc65c92008-06-28 08:31:57 +10001381 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1382 if (sh->check_state == check_state_compute_run)
1383 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001384 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001385 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001386}
1387
Dan Williamsd6f38f32009-07-14 11:50:52 -07001388/* return a pointer to the address conversion region of the scribble buffer */
1389static addr_conv_t *to_addr_conv(struct stripe_head *sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001390 struct raid5_percpu *percpu, int i)
Dan Williams91c00922007-01-02 13:52:30 -07001391{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001392 void *addr;
1393
1394 addr = flex_array_get(percpu->scribble, i);
1395 return addr + sizeof(struct page *) * (sh->disks + 2);
1396}
1397
1398/* return a pointer to the address conversion region of the scribble buffer */
1399static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
1400{
1401 void *addr;
1402
1403 addr = flex_array_get(percpu->scribble, i);
1404 return addr;
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))
Dan Williamsa08abd82009-06-03 11:43:59 -07001435 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001436 else
Dan Williamsa08abd82009-06-03 11:43:59 -07001437 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001438
Dan Williams91c00922007-01-02 13:52:30 -07001439 return tx;
1440}
1441
Dan Williamsac6b53b2009-07-14 13:40:19 -07001442/* set_syndrome_sources - populate source buffers for gen_syndrome
1443 * @srcs - (struct page *) array of size sh->disks
1444 * @sh - stripe_head to parse
1445 *
1446 * Populates srcs in proper layout order for the stripe and returns the
1447 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1448 * destination buffer is recorded in srcs[count] and the Q destination
1449 * is recorded in srcs[count+1]].
1450 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001451static int set_syndrome_sources(struct page **srcs,
1452 struct stripe_head *sh,
1453 int srctype)
Dan Williamsac6b53b2009-07-14 13:40:19 -07001454{
1455 int disks = sh->disks;
1456 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1457 int d0_idx = raid6_d0(sh);
1458 int count;
1459 int i;
1460
1461 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001462 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001463
1464 count = 0;
1465 i = d0_idx;
1466 do {
1467 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001468 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001469
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001470 if (i == sh->qd_idx || i == sh->pd_idx ||
1471 (srctype == SYNDROME_SRC_ALL) ||
1472 (srctype == SYNDROME_SRC_WANT_DRAIN &&
Song Liu1e6d6902016-11-17 15:24:39 -08001473 (test_bit(R5_Wantdrain, &dev->flags) ||
1474 test_bit(R5_InJournal, &dev->flags))) ||
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001475 (srctype == SYNDROME_SRC_WRITTEN &&
Song Liu09777622017-03-13 13:44:35 -07001476 (dev->written ||
1477 test_bit(R5_InJournal, &dev->flags)))) {
Song Liu1e6d6902016-11-17 15:24:39 -08001478 if (test_bit(R5_InJournal, &dev->flags))
1479 srcs[slot] = sh->dev[i].orig_page;
1480 else
1481 srcs[slot] = sh->dev[i].page;
1482 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001483 i = raid6_next_disk(i, disks);
1484 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001485
NeilBrowne4424fe2009-10-16 16:27:34 +11001486 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001487}
1488
1489static struct dma_async_tx_descriptor *
1490ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1491{
1492 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001493 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001494 int target;
1495 int qd_idx = sh->qd_idx;
1496 struct dma_async_tx_descriptor *tx;
1497 struct async_submit_ctl submit;
1498 struct r5dev *tgt;
1499 struct page *dest;
1500 int i;
1501 int count;
1502
shli@kernel.org59fc6302014-12-15 12:57:03 +11001503 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001504 if (sh->ops.target < 0)
1505 target = sh->ops.target2;
1506 else if (sh->ops.target2 < 0)
1507 target = sh->ops.target;
1508 else
1509 /* we should only have one valid target */
1510 BUG();
1511 BUG_ON(target < 0);
1512 pr_debug("%s: stripe %llu block: %d\n",
1513 __func__, (unsigned long long)sh->sector, target);
1514
1515 tgt = &sh->dev[target];
1516 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1517 dest = tgt->page;
1518
1519 atomic_inc(&sh->count);
1520
1521 if (target == qd_idx) {
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001522 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001523 blocks[count] = NULL; /* regenerating p is not necessary */
1524 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001525 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1526 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001527 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001528 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1529 } else {
1530 /* Compute any data- or p-drive using XOR */
1531 count = 0;
1532 for (i = disks; i-- ; ) {
1533 if (i == target || i == qd_idx)
1534 continue;
1535 blocks[count++] = sh->dev[i].page;
1536 }
1537
Dan Williams0403e382009-09-08 17:42:50 -07001538 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1539 NULL, ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001540 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001541 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1542 }
1543
1544 return tx;
1545}
1546
1547static struct dma_async_tx_descriptor *
1548ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1549{
1550 int i, count, disks = sh->disks;
1551 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1552 int d0_idx = raid6_d0(sh);
1553 int faila = -1, failb = -1;
1554 int target = sh->ops.target;
1555 int target2 = sh->ops.target2;
1556 struct r5dev *tgt = &sh->dev[target];
1557 struct r5dev *tgt2 = &sh->dev[target2];
1558 struct dma_async_tx_descriptor *tx;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001559 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001560 struct async_submit_ctl submit;
1561
shli@kernel.org59fc6302014-12-15 12:57:03 +11001562 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001563 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1564 __func__, (unsigned long long)sh->sector, target, target2);
1565 BUG_ON(target < 0 || target2 < 0);
1566 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1567 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1568
Dan Williams6c910a72009-09-16 12:24:54 -07001569 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001570 * slot number conversion for 'faila' and 'failb'
1571 */
1572 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001573 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001574 count = 0;
1575 i = d0_idx;
1576 do {
1577 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1578
1579 blocks[slot] = sh->dev[i].page;
1580
1581 if (i == target)
1582 faila = slot;
1583 if (i == target2)
1584 failb = slot;
1585 i = raid6_next_disk(i, disks);
1586 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001587
1588 BUG_ON(faila == failb);
1589 if (failb < faila)
1590 swap(faila, failb);
1591 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1592 __func__, (unsigned long long)sh->sector, faila, failb);
1593
1594 atomic_inc(&sh->count);
1595
1596 if (failb == syndrome_disks+1) {
1597 /* Q disk is one of the missing disks */
1598 if (faila == syndrome_disks) {
1599 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001600 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1601 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001602 to_addr_conv(sh, percpu, 0));
NeilBrowne4424fe2009-10-16 16:27:34 +11001603 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001604 STRIPE_SIZE, &submit);
1605 } else {
1606 struct page *dest;
1607 int data_target;
1608 int qd_idx = sh->qd_idx;
1609
1610 /* Missing D+Q: recompute D from P, then recompute Q */
1611 if (target == qd_idx)
1612 data_target = target2;
1613 else
1614 data_target = target;
1615
1616 count = 0;
1617 for (i = disks; i-- ; ) {
1618 if (i == data_target || i == qd_idx)
1619 continue;
1620 blocks[count++] = sh->dev[i].page;
1621 }
1622 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001623 init_async_submit(&submit,
1624 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1625 NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001626 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001627 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1628 &submit);
1629
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001630 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williams0403e382009-09-08 17:42:50 -07001631 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1632 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001633 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001634 return async_gen_syndrome(blocks, 0, count+2,
1635 STRIPE_SIZE, &submit);
1636 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001637 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001638 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1639 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001640 to_addr_conv(sh, percpu, 0));
Dan Williams6c910a72009-09-16 12:24:54 -07001641 if (failb == syndrome_disks) {
1642 /* We're missing D+P. */
1643 return async_raid6_datap_recov(syndrome_disks+2,
1644 STRIPE_SIZE, faila,
1645 blocks, &submit);
1646 } else {
1647 /* We're missing D+D. */
1648 return async_raid6_2data_recov(syndrome_disks+2,
1649 STRIPE_SIZE, faila, failb,
1650 blocks, &submit);
1651 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001652 }
1653}
1654
Dan Williams91c00922007-01-02 13:52:30 -07001655static void ops_complete_prexor(void *stripe_head_ref)
1656{
1657 struct stripe_head *sh = stripe_head_ref;
1658
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001659 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001660 (unsigned long long)sh->sector);
Song Liu1e6d6902016-11-17 15:24:39 -08001661
1662 if (r5c_is_writeback(sh->raid_conf->log))
1663 /*
1664 * raid5-cache write back uses orig_page during prexor.
1665 * After prexor, it is time to free orig_page
1666 */
1667 r5c_release_extra_page(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001668}
1669
1670static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001671ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1672 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001673{
Dan Williams91c00922007-01-02 13:52:30 -07001674 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001675 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001676 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001677 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001678
1679 /* existing parity data subtracted */
1680 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1681
shli@kernel.org59fc6302014-12-15 12:57:03 +11001682 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001683 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001684 (unsigned long long)sh->sector);
1685
1686 for (i = disks; i--; ) {
1687 struct r5dev *dev = &sh->dev[i];
1688 /* Only process blocks that are known to be uptodate */
Song Liu1e6d6902016-11-17 15:24:39 -08001689 if (test_bit(R5_InJournal, &dev->flags))
1690 xor_srcs[count++] = dev->orig_page;
1691 else if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001692 xor_srcs[count++] = dev->page;
1693 }
1694
Dan Williams0403e382009-09-08 17:42:50 -07001695 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001696 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Dan Williamsa08abd82009-06-03 11:43:59 -07001697 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001698
1699 return tx;
1700}
1701
1702static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001703ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1704 struct dma_async_tx_descriptor *tx)
1705{
1706 struct page **blocks = to_addr_page(percpu, 0);
1707 int count;
1708 struct async_submit_ctl submit;
1709
1710 pr_debug("%s: stripe %llu\n", __func__,
1711 (unsigned long long)sh->sector);
1712
1713 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN);
1714
1715 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1716 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1717 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1718
1719 return tx;
1720}
1721
1722static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001723ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001724{
Song Liu1e6d6902016-11-17 15:24:39 -08001725 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001726 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001727 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001728 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -07001729
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001730 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001731 (unsigned long long)sh->sector);
1732
1733 for (i = disks; i--; ) {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001734 struct r5dev *dev;
Dan Williams91c00922007-01-02 13:52:30 -07001735 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001736
shli@kernel.org59fc6302014-12-15 12:57:03 +11001737 sh = head_sh;
1738 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001739 struct bio *wbi;
1740
shli@kernel.org59fc6302014-12-15 12:57:03 +11001741again:
1742 dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001743 /*
1744 * clear R5_InJournal, so when rewriting a page in
1745 * journal, it is not skipped by r5l_log_stripe()
1746 */
1747 clear_bit(R5_InJournal, &dev->flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10001748 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001749 chosen = dev->towrite;
1750 dev->towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11001751 sh->overwrite_disks = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001752 BUG_ON(dev->written);
1753 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001754 spin_unlock_irq(&sh->stripe_lock);
Shaohua Lid592a992014-05-21 17:57:44 +08001755 WARN_ON(dev->page != dev->orig_page);
Dan Williams91c00922007-01-02 13:52:30 -07001756
Kent Overstreet4f024f32013-10-11 15:44:27 -07001757 while (wbi && wbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001758 dev->sector + STRIPE_SECTORS) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001759 if (wbi->bi_opf & REQ_FUA)
Tejun Heoe9c74692010-09-03 11:56:18 +02001760 set_bit(R5_WantFUA, &dev->flags);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001761 if (wbi->bi_opf & REQ_SYNC)
Shaohua Libc0934f2012-05-22 13:55:05 +10001762 set_bit(R5_SyncIO, &dev->flags);
Mike Christie796a5cf2016-06-05 14:32:07 -05001763 if (bio_op(wbi) == REQ_OP_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001764 set_bit(R5_Discard, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08001765 else {
1766 tx = async_copy_data(1, wbi, &dev->page,
Song Liu1e6d6902016-11-17 15:24:39 -08001767 dev->sector, tx, sh,
1768 r5c_is_writeback(conf->log));
1769 if (dev->page != dev->orig_page &&
1770 !r5c_is_writeback(conf->log)) {
Shaohua Lid592a992014-05-21 17:57:44 +08001771 set_bit(R5_SkipCopy, &dev->flags);
1772 clear_bit(R5_UPTODATE, &dev->flags);
1773 clear_bit(R5_OVERWRITE, &dev->flags);
1774 }
1775 }
Dan Williams91c00922007-01-02 13:52:30 -07001776 wbi = r5_next_bio(wbi, dev->sector);
1777 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001778
1779 if (head_sh->batch_head) {
1780 sh = list_first_entry(&sh->batch_list,
1781 struct stripe_head,
1782 batch_list);
1783 if (sh == head_sh)
1784 continue;
1785 goto again;
1786 }
Dan Williams91c00922007-01-02 13:52:30 -07001787 }
1788 }
1789
1790 return tx;
1791}
1792
Dan Williamsac6b53b2009-07-14 13:40:19 -07001793static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001794{
1795 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001796 int disks = sh->disks;
1797 int pd_idx = sh->pd_idx;
1798 int qd_idx = sh->qd_idx;
1799 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001800 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001801
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001802 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001803 (unsigned long long)sh->sector);
1804
Shaohua Libc0934f2012-05-22 13:55:05 +10001805 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001806 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001807 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001808 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001809 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001810
Dan Williams91c00922007-01-02 13:52:30 -07001811 for (i = disks; i--; ) {
1812 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001813
Tejun Heoe9c74692010-09-03 11:56:18 +02001814 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Lid592a992014-05-21 17:57:44 +08001815 if (!discard && !test_bit(R5_SkipCopy, &dev->flags))
Shaohua Li9e4447682012-10-11 13:49:49 +11001816 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001817 if (fua)
1818 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001819 if (sync)
1820 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001821 }
Dan Williams91c00922007-01-02 13:52:30 -07001822 }
1823
Dan Williamsd8ee0722008-06-28 08:32:06 +10001824 if (sh->reconstruct_state == reconstruct_state_drain_run)
1825 sh->reconstruct_state = reconstruct_state_drain_result;
1826 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1827 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1828 else {
1829 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1830 sh->reconstruct_state = reconstruct_state_result;
1831 }
Dan Williams91c00922007-01-02 13:52:30 -07001832
1833 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001834 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001835}
1836
1837static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001838ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1839 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001840{
Dan Williams91c00922007-01-02 13:52:30 -07001841 int disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001842 struct page **xor_srcs;
Dan Williamsa08abd82009-06-03 11:43:59 -07001843 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001844 int count, pd_idx = sh->pd_idx, i;
Dan Williams91c00922007-01-02 13:52:30 -07001845 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001846 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001847 unsigned long flags;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001848 int j = 0;
1849 struct stripe_head *head_sh = sh;
1850 int last_stripe;
Dan Williams91c00922007-01-02 13:52:30 -07001851
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001852 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001853 (unsigned long long)sh->sector);
1854
Shaohua Li620125f2012-10-11 13:49:05 +11001855 for (i = 0; i < sh->disks; i++) {
1856 if (pd_idx == i)
1857 continue;
1858 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1859 break;
1860 }
1861 if (i >= sh->disks) {
1862 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001863 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1864 ops_complete_reconstruct(sh);
1865 return;
1866 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001867again:
1868 count = 0;
1869 xor_srcs = to_addr_page(percpu, j);
Dan Williams91c00922007-01-02 13:52:30 -07001870 /* check if prexor is active which means only process blocks
1871 * that are part of a read-modify-write (written)
1872 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001873 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001874 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001875 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1876 for (i = disks; i--; ) {
1877 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001878 if (head_sh->dev[i].written ||
1879 test_bit(R5_InJournal, &head_sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -07001880 xor_srcs[count++] = dev->page;
1881 }
1882 } else {
1883 xor_dest = sh->dev[pd_idx].page;
1884 for (i = disks; i--; ) {
1885 struct r5dev *dev = &sh->dev[i];
1886 if (i != pd_idx)
1887 xor_srcs[count++] = dev->page;
1888 }
1889 }
1890
Dan Williams91c00922007-01-02 13:52:30 -07001891 /* 1/ if we prexor'd then the dest is reused as a source
1892 * 2/ if we did not prexor then we are redoing the parity
1893 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1894 * for the synchronous xor case
1895 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001896 last_stripe = !head_sh->batch_head ||
1897 list_first_entry(&sh->batch_list,
1898 struct stripe_head, batch_list) == head_sh;
1899 if (last_stripe) {
1900 flags = ASYNC_TX_ACK |
1901 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
Dan Williams91c00922007-01-02 13:52:30 -07001902
shli@kernel.org59fc6302014-12-15 12:57:03 +11001903 atomic_inc(&head_sh->count);
1904 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
1905 to_addr_conv(sh, percpu, j));
1906 } else {
1907 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
1908 init_async_submit(&submit, flags, tx, NULL, NULL,
1909 to_addr_conv(sh, percpu, j));
1910 }
Dan Williams91c00922007-01-02 13:52:30 -07001911
Dan Williamsa08abd82009-06-03 11:43:59 -07001912 if (unlikely(count == 1))
1913 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1914 else
1915 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001916 if (!last_stripe) {
1917 j++;
1918 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1919 batch_list);
1920 goto again;
1921 }
Dan Williams91c00922007-01-02 13:52:30 -07001922}
1923
Dan Williamsac6b53b2009-07-14 13:40:19 -07001924static void
1925ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1926 struct dma_async_tx_descriptor *tx)
1927{
1928 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001929 struct page **blocks;
1930 int count, i, j = 0;
1931 struct stripe_head *head_sh = sh;
1932 int last_stripe;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001933 int synflags;
1934 unsigned long txflags;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001935
1936 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1937
Shaohua Li620125f2012-10-11 13:49:05 +11001938 for (i = 0; i < sh->disks; i++) {
1939 if (sh->pd_idx == i || sh->qd_idx == i)
1940 continue;
1941 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1942 break;
1943 }
1944 if (i >= sh->disks) {
1945 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001946 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1947 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1948 ops_complete_reconstruct(sh);
1949 return;
1950 }
1951
shli@kernel.org59fc6302014-12-15 12:57:03 +11001952again:
1953 blocks = to_addr_page(percpu, j);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001954
1955 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1956 synflags = SYNDROME_SRC_WRITTEN;
1957 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
1958 } else {
1959 synflags = SYNDROME_SRC_ALL;
1960 txflags = ASYNC_TX_ACK;
1961 }
1962
1963 count = set_syndrome_sources(blocks, sh, synflags);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001964 last_stripe = !head_sh->batch_head ||
1965 list_first_entry(&sh->batch_list,
1966 struct stripe_head, batch_list) == head_sh;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001967
shli@kernel.org59fc6302014-12-15 12:57:03 +11001968 if (last_stripe) {
1969 atomic_inc(&head_sh->count);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001970 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
shli@kernel.org59fc6302014-12-15 12:57:03 +11001971 head_sh, to_addr_conv(sh, percpu, j));
1972 } else
1973 init_async_submit(&submit, 0, tx, NULL, NULL,
1974 to_addr_conv(sh, percpu, j));
Shaohua Li48769692015-05-13 09:30:08 -07001975 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001976 if (!last_stripe) {
1977 j++;
1978 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1979 batch_list);
1980 goto again;
1981 }
Dan Williams91c00922007-01-02 13:52:30 -07001982}
1983
1984static void ops_complete_check(void *stripe_head_ref)
1985{
1986 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001987
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001988 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001989 (unsigned long long)sh->sector);
1990
Dan Williamsecc65c92008-06-28 08:31:57 +10001991 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001992 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001993 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001994}
1995
Dan Williamsac6b53b2009-07-14 13:40:19 -07001996static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001997{
Dan Williams91c00922007-01-02 13:52:30 -07001998 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001999 int pd_idx = sh->pd_idx;
2000 int qd_idx = sh->qd_idx;
2001 struct page *xor_dest;
shli@kernel.org46d5b782014-12-15 12:57:02 +11002002 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07002003 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07002004 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002005 int count;
2006 int i;
Dan Williams91c00922007-01-02 13:52:30 -07002007
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002008 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07002009 (unsigned long long)sh->sector);
2010
shli@kernel.org59fc6302014-12-15 12:57:03 +11002011 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002012 count = 0;
2013 xor_dest = sh->dev[pd_idx].page;
2014 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07002015 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07002016 if (i == pd_idx || i == qd_idx)
2017 continue;
2018 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07002019 }
2020
Dan Williamsd6f38f32009-07-14 11:50:52 -07002021 init_async_submit(&submit, 0, NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11002022 to_addr_conv(sh, percpu, 0));
Dan Williams099f53c2009-04-08 14:28:37 -07002023 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07002024 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07002025
Dan Williams91c00922007-01-02 13:52:30 -07002026 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07002027 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
2028 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07002029}
2030
Dan Williamsac6b53b2009-07-14 13:40:19 -07002031static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
2032{
shli@kernel.org46d5b782014-12-15 12:57:02 +11002033 struct page **srcs = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002034 struct async_submit_ctl submit;
2035 int count;
2036
2037 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
2038 (unsigned long long)sh->sector, checkp);
2039
shli@kernel.org59fc6302014-12-15 12:57:03 +11002040 BUG_ON(sh->batch_head);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002041 count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002042 if (!checkp)
2043 srcs[count] = NULL;
2044
2045 atomic_inc(&sh->count);
2046 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
shli@kernel.org46d5b782014-12-15 12:57:02 +11002047 sh, to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07002048 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
2049 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
2050}
2051
NeilBrown51acbce2013-02-28 09:08:34 +11002052static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07002053{
2054 int overlap_clear = 0, i, disks = sh->disks;
2055 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11002056 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002057 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002058 struct raid5_percpu *percpu;
2059 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07002060
Dan Williamsd6f38f32009-07-14 11:50:52 -07002061 cpu = get_cpu();
2062 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10002063 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07002064 ops_run_biofill(sh);
2065 overlap_clear++;
2066 }
2067
Dan Williams7b3a8712008-06-28 08:32:09 +10002068 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07002069 if (level < 6)
2070 tx = ops_run_compute5(sh, percpu);
2071 else {
2072 if (sh->ops.target2 < 0 || sh->ops.target < 0)
2073 tx = ops_run_compute6_1(sh, percpu);
2074 else
2075 tx = ops_run_compute6_2(sh, percpu);
2076 }
2077 /* terminate the chain if reconstruct is not set to be run */
2078 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10002079 async_tx_ack(tx);
2080 }
Dan Williams91c00922007-01-02 13:52:30 -07002081
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002082 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
2083 if (level < 6)
2084 tx = ops_run_prexor5(sh, percpu, tx);
2085 else
2086 tx = ops_run_prexor6(sh, percpu, tx);
2087 }
Dan Williams91c00922007-01-02 13:52:30 -07002088
Artur Paszkiewiczae1713e2017-04-04 13:13:58 +02002089 if (test_bit(STRIPE_OP_PARTIAL_PARITY, &ops_request))
2090 tx = ops_run_partial_parity(sh, percpu, tx);
2091
Dan Williams600aa102008-06-28 08:32:05 +10002092 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10002093 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07002094 overlap_clear++;
2095 }
2096
Dan Williamsac6b53b2009-07-14 13:40:19 -07002097 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
2098 if (level < 6)
2099 ops_run_reconstruct5(sh, percpu, tx);
2100 else
2101 ops_run_reconstruct6(sh, percpu, tx);
2102 }
Dan Williams91c00922007-01-02 13:52:30 -07002103
Dan Williamsac6b53b2009-07-14 13:40:19 -07002104 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
2105 if (sh->check_state == check_state_run)
2106 ops_run_check_p(sh, percpu);
2107 else if (sh->check_state == check_state_run_q)
2108 ops_run_check_pq(sh, percpu, 0);
2109 else if (sh->check_state == check_state_run_pq)
2110 ops_run_check_pq(sh, percpu, 1);
2111 else
2112 BUG();
2113 }
Dan Williams91c00922007-01-02 13:52:30 -07002114
shli@kernel.org59fc6302014-12-15 12:57:03 +11002115 if (overlap_clear && !sh->batch_head)
Dan Williams91c00922007-01-02 13:52:30 -07002116 for (i = disks; i--; ) {
2117 struct r5dev *dev = &sh->dev[i];
2118 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2119 wake_up(&sh->raid_conf->wait_for_overlap);
2120 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07002121 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07002122}
2123
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002124static void free_stripe(struct kmem_cache *sc, struct stripe_head *sh)
2125{
2126 if (sh->ppl_page)
2127 __free_page(sh->ppl_page);
2128 kmem_cache_free(sc, sh);
2129}
2130
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002131static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002132 int disks, struct r5conf *conf)
NeilBrownf18c1a32015-05-08 18:19:04 +10002133{
2134 struct stripe_head *sh;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002135 int i;
NeilBrownf18c1a32015-05-08 18:19:04 +10002136
2137 sh = kmem_cache_zalloc(sc, gfp);
2138 if (sh) {
2139 spin_lock_init(&sh->stripe_lock);
2140 spin_lock_init(&sh->batch_lock);
2141 INIT_LIST_HEAD(&sh->batch_list);
2142 INIT_LIST_HEAD(&sh->lru);
Song Liua39f7af2016-11-17 15:24:40 -08002143 INIT_LIST_HEAD(&sh->r5c);
Song Liud7bd3982016-11-23 22:50:39 -08002144 INIT_LIST_HEAD(&sh->log_list);
NeilBrownf18c1a32015-05-08 18:19:04 +10002145 atomic_set(&sh->count, 1);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002146 sh->raid_conf = conf;
Song Liua39f7af2016-11-17 15:24:40 -08002147 sh->log_start = MaxSector;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002148 for (i = 0; i < disks; i++) {
2149 struct r5dev *dev = &sh->dev[i];
2150
Ming Lei3a83f462016-11-22 08:57:21 -07002151 bio_init(&dev->req, &dev->vec, 1);
2152 bio_init(&dev->rreq, &dev->rvec, 1);
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002153 }
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002154
2155 if (raid5_has_ppl(conf)) {
2156 sh->ppl_page = alloc_page(gfp);
2157 if (!sh->ppl_page) {
2158 free_stripe(sc, sh);
2159 sh = NULL;
2160 }
2161 }
NeilBrownf18c1a32015-05-08 18:19:04 +10002162 }
2163 return sh;
2164}
NeilBrown486f0642015-02-25 12:10:35 +11002165static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 struct stripe_head *sh;
NeilBrownf18c1a32015-05-08 18:19:04 +10002168
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002169 sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size, conf);
NeilBrown3f294f42005-11-08 21:39:25 -08002170 if (!sh)
2171 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10002172
NeilBrowna9683a72015-02-25 12:02:51 +11002173 if (grow_buffers(sh, gfp)) {
NeilBrowne4e11e32010-06-16 16:45:16 +10002174 shrink_buffers(sh);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002175 free_stripe(conf->slab_cache, sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002176 return 0;
2177 }
NeilBrown486f0642015-02-25 12:10:35 +11002178 sh->hash_lock_index =
2179 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrown3f294f42005-11-08 21:39:25 -08002180 /* we just created an active stripe so... */
NeilBrown3f294f42005-11-08 21:39:25 -08002181 atomic_inc(&conf->active_stripes);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002182
Shaohua Li6d036f72015-08-13 14:31:57 -07002183 raid5_release_stripe(sh);
NeilBrown486f0642015-02-25 12:10:35 +11002184 conf->max_nr_stripes++;
NeilBrown3f294f42005-11-08 21:39:25 -08002185 return 1;
2186}
2187
NeilBrownd1688a62011-10-11 16:49:52 +11002188static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08002189{
Christoph Lametere18b8902006-12-06 20:33:20 -08002190 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11002191 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
NeilBrownf4be6b42010-06-01 19:37:25 +10002193 if (conf->mddev->gendisk)
2194 sprintf(conf->cache_name[0],
2195 "raid%d-%s", conf->level, mdname(conf->mddev));
2196 else
2197 sprintf(conf->cache_name[0],
2198 "raid%d-%p", conf->level, conf->mddev);
2199 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
2200
NeilBrownad01c9e2006-03-27 01:18:07 -08002201 conf->active_name = 0;
2202 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002204 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 if (!sc)
2206 return 1;
2207 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002208 conf->pool_size = devs;
NeilBrown486f0642015-02-25 12:10:35 +11002209 while (num--)
2210 if (!grow_one_stripe(conf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 return 1;
NeilBrown486f0642015-02-25 12:10:35 +11002212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 return 0;
2214}
NeilBrown29269552006-03-27 01:18:10 -08002215
Dan Williamsd6f38f32009-07-14 11:50:52 -07002216/**
2217 * scribble_len - return the required size of the scribble region
2218 * @num - total number of disks in the array
2219 *
2220 * The size must be enough to contain:
2221 * 1/ a struct page pointer for each device in the array +2
2222 * 2/ room to convert each entry in (1) to its corresponding dma
2223 * (dma_map_page()) or page (page_address()) address.
2224 *
2225 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2226 * calculate over all devices (not just the data blocks), using zeros in place
2227 * of the P and Q blocks.
2228 */
shli@kernel.org46d5b782014-12-15 12:57:02 +11002229static struct flex_array *scribble_alloc(int num, int cnt, gfp_t flags)
Dan Williamsd6f38f32009-07-14 11:50:52 -07002230{
shli@kernel.org46d5b782014-12-15 12:57:02 +11002231 struct flex_array *ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002232 size_t len;
2233
2234 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
shli@kernel.org46d5b782014-12-15 12:57:02 +11002235 ret = flex_array_alloc(len, cnt, flags);
2236 if (!ret)
2237 return NULL;
2238 /* always prealloc all elements, so no locking is required */
2239 if (flex_array_prealloc(ret, 0, cnt, flags)) {
2240 flex_array_free(ret);
2241 return NULL;
2242 }
2243 return ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002244}
2245
NeilBrown738a2732015-05-08 18:19:39 +10002246static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2247{
2248 unsigned long cpu;
2249 int err = 0;
2250
Shaohua Li27a353c2016-02-24 17:38:28 -08002251 /*
2252 * Never shrink. And mddev_suspend() could deadlock if this is called
2253 * from raid5d. In that case, scribble_disks and scribble_sectors
2254 * should equal to new_disks and new_sectors
2255 */
2256 if (conf->scribble_disks >= new_disks &&
2257 conf->scribble_sectors >= new_sectors)
2258 return 0;
NeilBrown738a2732015-05-08 18:19:39 +10002259 mddev_suspend(conf->mddev);
2260 get_online_cpus();
2261 for_each_present_cpu(cpu) {
2262 struct raid5_percpu *percpu;
2263 struct flex_array *scribble;
2264
2265 percpu = per_cpu_ptr(conf->percpu, cpu);
2266 scribble = scribble_alloc(new_disks,
2267 new_sectors / STRIPE_SECTORS,
2268 GFP_NOIO);
2269
2270 if (scribble) {
2271 flex_array_free(percpu->scribble);
2272 percpu->scribble = scribble;
2273 } else {
2274 err = -ENOMEM;
2275 break;
2276 }
2277 }
2278 put_online_cpus();
2279 mddev_resume(conf->mddev);
Shaohua Li27a353c2016-02-24 17:38:28 -08002280 if (!err) {
2281 conf->scribble_disks = new_disks;
2282 conf->scribble_sectors = new_sectors;
2283 }
NeilBrown738a2732015-05-08 18:19:39 +10002284 return err;
2285}
2286
NeilBrownd1688a62011-10-11 16:49:52 +11002287static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08002288{
2289 /* Make all the stripes able to hold 'newsize' devices.
2290 * New slots in each stripe get 'page' set to a new page.
2291 *
2292 * This happens in stages:
2293 * 1/ create a new kmem_cache and allocate the required number of
2294 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09002295 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08002296 * to the new stripe_heads. This will have the side effect of
2297 * freezing the array as once all stripe_heads have been collected,
2298 * no IO will be possible. Old stripe heads are freed once their
2299 * pages have been transferred over, and the old kmem_cache is
2300 * freed when all stripes are done.
2301 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
Zhilong Liu3560741e2017-03-15 16:14:53 +08002302 * we simple return a failure status - no need to clean anything up.
NeilBrownad01c9e2006-03-27 01:18:07 -08002303 * 4/ allocate new pages for the new slots in the new stripe_heads.
2304 * If this fails, we don't bother trying the shrink the
2305 * stripe_heads down again, we just leave them as they are.
2306 * As each stripe_head is processed the new one is released into
2307 * active service.
2308 *
2309 * Once step2 is started, we cannot afford to wait for a write,
2310 * so we use GFP_NOIO allocations.
2311 */
2312 struct stripe_head *osh, *nsh;
2313 LIST_HEAD(newstripes);
2314 struct disk_info *ndisks;
Dan Williamsb5470dc2008-06-27 21:44:04 -07002315 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08002316 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002317 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11002318 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08002319
Dan Williamsb5470dc2008-06-27 21:44:04 -07002320 err = md_allow_write(conf->mddev);
2321 if (err)
2322 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08002323
NeilBrownad01c9e2006-03-27 01:18:07 -08002324 /* Step 1 */
2325 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2326 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002327 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08002328 if (!sc)
2329 return -ENOMEM;
2330
NeilBrown2d5b5692015-07-06 12:49:23 +10002331 /* Need to ensure auto-resizing doesn't interfere */
2332 mutex_lock(&conf->cache_size_mutex);
2333
NeilBrownad01c9e2006-03-27 01:18:07 -08002334 for (i = conf->max_nr_stripes; i; i--) {
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002335 nsh = alloc_stripe(sc, GFP_KERNEL, newsize, conf);
NeilBrownad01c9e2006-03-27 01:18:07 -08002336 if (!nsh)
2337 break;
2338
NeilBrownad01c9e2006-03-27 01:18:07 -08002339 list_add(&nsh->lru, &newstripes);
2340 }
2341 if (i) {
2342 /* didn't get enough, give up */
2343 while (!list_empty(&newstripes)) {
2344 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2345 list_del(&nsh->lru);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002346 free_stripe(sc, nsh);
NeilBrownad01c9e2006-03-27 01:18:07 -08002347 }
2348 kmem_cache_destroy(sc);
NeilBrown2d5b5692015-07-06 12:49:23 +10002349 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002350 return -ENOMEM;
2351 }
2352 /* Step 2 - Must use GFP_NOIO now.
2353 * OK, we have enough stripes, start collecting inactive
2354 * stripes and copying them over
2355 */
Shaohua Li566c09c2013-11-14 15:16:17 +11002356 hash = 0;
2357 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08002358 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11002359 lock_device_hash_lock(conf, hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08002360 wait_event_cmd(conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +11002361 !list_empty(conf->inactive_list + hash),
2362 unlock_device_hash_lock(conf, hash),
2363 lock_device_hash_lock(conf, hash));
2364 osh = get_free_stripe(conf, hash);
2365 unlock_device_hash_lock(conf, hash);
NeilBrownf18c1a32015-05-08 18:19:04 +10002366
Shaohua Lid592a992014-05-21 17:57:44 +08002367 for(i=0; i<conf->pool_size; i++) {
NeilBrownad01c9e2006-03-27 01:18:07 -08002368 nsh->dev[i].page = osh->dev[i].page;
Shaohua Lid592a992014-05-21 17:57:44 +08002369 nsh->dev[i].orig_page = osh->dev[i].page;
2370 }
Shaohua Li566c09c2013-11-14 15:16:17 +11002371 nsh->hash_lock_index = hash;
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002372 free_stripe(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11002373 cnt++;
2374 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2375 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2376 hash++;
2377 cnt = 0;
2378 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002379 }
2380 kmem_cache_destroy(conf->slab_cache);
2381
2382 /* Step 3.
2383 * At this point, we are holding all the stripes so the array
2384 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07002385 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08002386 */
2387 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
2388 if (ndisks) {
Song Liud7bd3982016-11-23 22:50:39 -08002389 for (i = 0; i < conf->pool_size; i++)
NeilBrownad01c9e2006-03-27 01:18:07 -08002390 ndisks[i] = conf->disks[i];
Song Liud7bd3982016-11-23 22:50:39 -08002391
2392 for (i = conf->pool_size; i < newsize; i++) {
2393 ndisks[i].extra_page = alloc_page(GFP_NOIO);
2394 if (!ndisks[i].extra_page)
2395 err = -ENOMEM;
2396 }
2397
2398 if (err) {
2399 for (i = conf->pool_size; i < newsize; i++)
2400 if (ndisks[i].extra_page)
2401 put_page(ndisks[i].extra_page);
2402 kfree(ndisks);
2403 } else {
2404 kfree(conf->disks);
2405 conf->disks = ndisks;
2406 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002407 } else
2408 err = -ENOMEM;
2409
NeilBrown2d5b5692015-07-06 12:49:23 +10002410 mutex_unlock(&conf->cache_size_mutex);
Dennis Yang583da482017-03-29 15:46:13 +08002411
2412 conf->slab_cache = sc;
2413 conf->active_name = 1-conf->active_name;
2414
NeilBrownad01c9e2006-03-27 01:18:07 -08002415 /* Step 4, return new stripes to service */
2416 while(!list_empty(&newstripes)) {
2417 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2418 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07002419
NeilBrownad01c9e2006-03-27 01:18:07 -08002420 for (i=conf->raid_disks; i < newsize; i++)
2421 if (nsh->dev[i].page == NULL) {
2422 struct page *p = alloc_page(GFP_NOIO);
2423 nsh->dev[i].page = p;
Shaohua Lid592a992014-05-21 17:57:44 +08002424 nsh->dev[i].orig_page = p;
NeilBrownad01c9e2006-03-27 01:18:07 -08002425 if (!p)
2426 err = -ENOMEM;
2427 }
Shaohua Li6d036f72015-08-13 14:31:57 -07002428 raid5_release_stripe(nsh);
NeilBrownad01c9e2006-03-27 01:18:07 -08002429 }
2430 /* critical section pass, GFP_NOIO no longer needed */
2431
NeilBrown6e9eac22015-05-08 18:19:34 +10002432 if (!err)
2433 conf->pool_size = newsize;
NeilBrownad01c9e2006-03-27 01:18:07 -08002434 return err;
2435}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
NeilBrown486f0642015-02-25 12:10:35 +11002437static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
2439 struct stripe_head *sh;
NeilBrown49895bc2015-08-03 17:09:57 +10002440 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
Shaohua Li566c09c2013-11-14 15:16:17 +11002442 spin_lock_irq(conf->hash_locks + hash);
2443 sh = get_free_stripe(conf, hash);
2444 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08002445 if (!sh)
2446 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002447 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10002448 shrink_buffers(sh);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002449 free_stripe(conf->slab_cache, sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002450 atomic_dec(&conf->active_stripes);
NeilBrown486f0642015-02-25 12:10:35 +11002451 conf->max_nr_stripes--;
NeilBrown3f294f42005-11-08 21:39:25 -08002452 return 1;
2453}
2454
NeilBrownd1688a62011-10-11 16:49:52 +11002455static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08002456{
NeilBrown486f0642015-02-25 12:10:35 +11002457 while (conf->max_nr_stripes &&
2458 drop_one_stripe(conf))
2459 ;
NeilBrown3f294f42005-11-08 21:39:25 -08002460
Julia Lawall644df1a2015-09-13 14:15:10 +02002461 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 conf->slab_cache = NULL;
2463}
2464
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002465static void raid5_end_read_request(struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466{
NeilBrown99c0fb52009-03-31 14:39:38 +11002467 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002468 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002469 int disks = sh->disks, i;
NeilBrownd6950432006-07-10 04:44:20 -07002470 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11002471 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10002472 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
2474 for (i=0 ; i<disks; i++)
2475 if (bi == &sh->dev[i].req)
2476 break;
2477
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002478 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
Dan Williams45b42332007-07-09 11:56:43 -07002479 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002480 bi->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002482 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002484 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 }
NeilBrown14a75d32011-12-23 10:17:52 +11002486 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11002487 /* If replacement finished while this request was outstanding,
2488 * 'replacement' might be NULL already.
2489 * In that case it moved down to 'rdev'.
2490 * rdev is not removed until all requests are finished.
2491 */
NeilBrown14a75d32011-12-23 10:17:52 +11002492 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002493 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11002494 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
NeilBrown05616be2012-05-21 09:27:00 +10002496 if (use_new_offset(conf, sh))
2497 s = sh->sector + rdev->new_data_offset;
2498 else
2499 s = sh->sector + rdev->data_offset;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002500 if (!bi->bi_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08002502 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11002503 /* Note that this cannot happen on a
2504 * replacement device. We just fail those on
2505 * any error
2506 */
NeilBrowncc6167b2016-11-02 14:16:50 +11002507 pr_info_ratelimited(
2508 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002509 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10002510 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002511 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10002512 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08002513 clear_bit(R5_ReadError, &sh->dev[i].flags);
2514 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002515 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2516 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2517
Song Liu86aa1392017-01-12 17:22:41 -08002518 if (test_bit(R5_InJournal, &sh->dev[i].flags))
2519 /*
2520 * end read for a page in journal, this
2521 * must be preparing for prexor in rmw
2522 */
2523 set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
2524
NeilBrown14a75d32011-12-23 10:17:52 +11002525 if (atomic_read(&rdev->read_errors))
2526 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002528 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002529 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002530 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07002533 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002534 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowncc6167b2016-11-02 14:16:50 +11002535 pr_warn_ratelimited(
2536 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
NeilBrown14a75d32011-12-23 10:17:52 +11002537 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002538 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002539 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002540 else if (conf->mddev->degraded >= conf->max_degraded) {
2541 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002542 pr_warn_ratelimited(
2543 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002544 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002545 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002546 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002547 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002548 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002549 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002550 pr_warn_ratelimited(
2551 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002552 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002553 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002554 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002555 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08002556 > conf->max_nr_stripes)
NeilBrowncc6167b2016-11-02 14:16:50 +11002557 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07002558 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08002559 else
2560 retry = 1;
Bian Yuedfa1f62013-11-14 15:16:17 +11002561 if (set_bad && test_bit(In_sync, &rdev->flags)
2562 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2563 retry = 1;
NeilBrownba22dcb2005-11-08 21:39:31 -08002564 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10002565 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2566 set_bit(R5_ReadError, &sh->dev[i].flags);
2567 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2568 } else
2569 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002570 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002571 clear_bit(R5_ReadError, &sh->dev[i].flags);
2572 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002573 if (!(set_bad
2574 && test_bit(In_sync, &rdev->flags)
2575 && rdev_set_badblocks(
2576 rdev, sh->sector, STRIPE_SECTORS, 0)))
2577 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
NeilBrown14a75d32011-12-23 10:17:52 +11002580 rdev_dec_pending(rdev, conf->mddev);
Shaohua Lic9445552016-09-08 10:43:58 -07002581 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2583 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002584 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585}
2586
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002587static void raid5_end_write_request(struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
NeilBrown99c0fb52009-03-31 14:39:38 +11002589 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002590 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002591 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11002592 struct md_rdev *uninitialized_var(rdev);
NeilBrownb84db562011-07-28 11:39:23 +10002593 sector_t first_bad;
2594 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002595 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
NeilBrown977df362011-12-23 10:17:53 +11002597 for (i = 0 ; i < disks; i++) {
2598 if (bi == &sh->dev[i].req) {
2599 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 break;
NeilBrown977df362011-12-23 10:17:53 +11002601 }
2602 if (bi == &sh->dev[i].rreq) {
2603 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002604 if (rdev)
2605 replacement = 1;
2606 else
2607 /* rdev was removed and 'replacement'
2608 * replaced it. rdev is not removed
2609 * until all requests are finished.
2610 */
2611 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002612 break;
2613 }
2614 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002615 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002617 bi->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002619 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002621 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
2623
NeilBrown977df362011-12-23 10:17:53 +11002624 if (replacement) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002625 if (bi->bi_error)
NeilBrown977df362011-12-23 10:17:53 +11002626 md_error(conf->mddev, rdev);
2627 else if (is_badblock(rdev, sh->sector,
2628 STRIPE_SECTORS,
2629 &first_bad, &bad_sectors))
2630 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2631 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002632 if (bi->bi_error) {
NeilBrown9f97e4b2014-01-16 09:35:38 +11002633 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11002634 set_bit(WriteErrorSeen, &rdev->flags);
2635 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002636 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2637 set_bit(MD_RECOVERY_NEEDED,
2638 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002639 } else if (is_badblock(rdev, sh->sector,
2640 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10002641 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002642 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002643 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2644 /* That was a successful write so make
2645 * sure it looks like we already did
2646 * a re-write.
2647 */
2648 set_bit(R5_ReWrite, &sh->dev[i].flags);
2649 }
NeilBrown977df362011-12-23 10:17:53 +11002650 }
2651 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002653 if (sh->batch_head && bi->bi_error && !replacement)
shli@kernel.org72ac7332014-12-15 12:57:03 +11002654 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2655
Shaohua Lic9445552016-09-08 10:43:58 -07002656 bio_reset(bi);
NeilBrown977df362011-12-23 10:17:53 +11002657 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2658 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002660 raid5_release_stripe(sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002661
2662 if (sh->batch_head && sh != sh->batch_head)
Shaohua Li6d036f72015-08-13 14:31:57 -07002663 raid5_release_stripe(sh->batch_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664}
2665
NeilBrown784052e2009-03-31 15:19:07 +11002666static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
2668 struct r5dev *dev = &sh->dev[i];
2669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 dev->flags = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07002671 dev->sector = raid5_compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
2673
Shaohua Li849674e2016-01-20 13:52:20 -08002674static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
2676 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002677 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002678 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002679 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
NeilBrown908f4fb2011-12-23 10:17:50 +11002681 spin_lock_irqsave(&conf->device_lock, flags);
2682 clear_bit(In_sync, &rdev->flags);
Song Liu2e38a372017-01-24 10:45:30 -08002683 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11002684 spin_unlock_irqrestore(&conf->device_lock, flags);
2685 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2686
NeilBrownde393cd2011-07-28 11:31:48 +10002687 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10002688 set_bit(Faulty, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08002689 set_mask_bits(&mddev->sb_flags, 0,
2690 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrowncc6167b2016-11-02 14:16:50 +11002691 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
2692 "md/raid:%s: Operation continuing on %d devices.\n",
2693 mdname(mddev),
2694 bdevname(rdev->bdev, b),
2695 mdname(mddev),
2696 conf->raid_disks - mddev->degraded);
Song Liu2e38a372017-01-24 10:45:30 -08002697 r5c_update_on_rdev_error(mddev);
NeilBrown16a53ec2006-06-26 00:27:38 -07002698}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700/*
2701 * Input: a 'big' sector number,
2702 * Output: index of the data and parity disk, and the sector # in them.
2703 */
Shaohua Li6d036f72015-08-13 14:31:57 -07002704sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2705 int previous, int *dd_idx,
2706 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002708 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002709 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002711 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002712 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002714 int algorithm = previous ? conf->prev_algo
2715 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002716 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2717 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002718 int raid_disks = previous ? conf->previous_raid_disks
2719 : conf->raid_disks;
2720 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
2722 /* First compute the information on this sector */
2723
2724 /*
2725 * Compute the chunk number and the sector offset inside the chunk
2726 */
2727 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2728 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
2730 /*
2731 * Compute the stripe number
2732 */
NeilBrown35f2a592010-04-20 14:13:34 +10002733 stripe = chunk_number;
2734 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002735 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 /*
2737 * Select the parity disk based on the user selected algorithm.
2738 */
NeilBrown84789552011-07-27 11:00:36 +10002739 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002740 switch(conf->level) {
2741 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002742 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002743 break;
2744 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002745 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002747 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002748 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 (*dd_idx)++;
2750 break;
2751 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002752 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002753 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 (*dd_idx)++;
2755 break;
2756 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002757 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002758 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 break;
2760 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002761 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002762 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002764 case ALGORITHM_PARITY_0:
2765 pd_idx = 0;
2766 (*dd_idx)++;
2767 break;
2768 case ALGORITHM_PARITY_N:
2769 pd_idx = data_disks;
2770 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002772 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002773 }
2774 break;
2775 case 6:
2776
NeilBrowne183eae2009-03-31 15:20:22 +11002777 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002778 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002779 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002780 qd_idx = pd_idx + 1;
2781 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002782 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002783 qd_idx = 0;
2784 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002785 (*dd_idx) += 2; /* D D P Q D */
2786 break;
2787 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002788 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002789 qd_idx = pd_idx + 1;
2790 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002791 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002792 qd_idx = 0;
2793 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002794 (*dd_idx) += 2; /* D D P Q D */
2795 break;
2796 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002797 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002798 qd_idx = (pd_idx + 1) % raid_disks;
2799 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002800 break;
2801 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002802 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002803 qd_idx = (pd_idx + 1) % raid_disks;
2804 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002805 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002806
2807 case ALGORITHM_PARITY_0:
2808 pd_idx = 0;
2809 qd_idx = 1;
2810 (*dd_idx) += 2;
2811 break;
2812 case ALGORITHM_PARITY_N:
2813 pd_idx = data_disks;
2814 qd_idx = data_disks + 1;
2815 break;
2816
2817 case ALGORITHM_ROTATING_ZERO_RESTART:
2818 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2819 * of blocks for computing Q is different.
2820 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002821 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002822 qd_idx = pd_idx + 1;
2823 if (pd_idx == raid_disks-1) {
2824 (*dd_idx)++; /* Q D D D P */
2825 qd_idx = 0;
2826 } else if (*dd_idx >= pd_idx)
2827 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002828 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002829 break;
2830
2831 case ALGORITHM_ROTATING_N_RESTART:
2832 /* Same a left_asymmetric, by first stripe is
2833 * D D D P Q rather than
2834 * Q D D D P
2835 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002836 stripe2 += 1;
2837 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002838 qd_idx = pd_idx + 1;
2839 if (pd_idx == raid_disks-1) {
2840 (*dd_idx)++; /* Q D D D P */
2841 qd_idx = 0;
2842 } else if (*dd_idx >= pd_idx)
2843 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002844 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002845 break;
2846
2847 case ALGORITHM_ROTATING_N_CONTINUE:
2848 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002849 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002850 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2851 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002852 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002853 break;
2854
2855 case ALGORITHM_LEFT_ASYMMETRIC_6:
2856 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002857 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002858 if (*dd_idx >= pd_idx)
2859 (*dd_idx)++;
2860 qd_idx = raid_disks - 1;
2861 break;
2862
2863 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002864 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002865 if (*dd_idx >= pd_idx)
2866 (*dd_idx)++;
2867 qd_idx = raid_disks - 1;
2868 break;
2869
2870 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002871 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002872 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2873 qd_idx = raid_disks - 1;
2874 break;
2875
2876 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002877 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002878 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2879 qd_idx = raid_disks - 1;
2880 break;
2881
2882 case ALGORITHM_PARITY_0_6:
2883 pd_idx = 0;
2884 (*dd_idx)++;
2885 qd_idx = raid_disks - 1;
2886 break;
2887
NeilBrown16a53ec2006-06-26 00:27:38 -07002888 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002889 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002890 }
2891 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 }
2893
NeilBrown911d4ee2009-03-31 14:39:38 +11002894 if (sh) {
2895 sh->pd_idx = pd_idx;
2896 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002897 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 /*
2900 * Finally, compute the new sector number
2901 */
2902 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2903 return new_sector;
2904}
2905
Shaohua Li6d036f72015-08-13 14:31:57 -07002906sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
NeilBrownd1688a62011-10-11 16:49:52 +11002908 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002909 int raid_disks = sh->disks;
2910 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002912 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2913 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002914 int algorithm = previous ? conf->prev_algo
2915 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 sector_t stripe;
2917 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002918 sector_t chunk_number;
2919 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002921 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
2923 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2924 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
NeilBrown16a53ec2006-06-26 00:27:38 -07002926 if (i == sh->pd_idx)
2927 return 0;
2928 switch(conf->level) {
2929 case 4: break;
2930 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002931 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 case ALGORITHM_LEFT_ASYMMETRIC:
2933 case ALGORITHM_RIGHT_ASYMMETRIC:
2934 if (i > sh->pd_idx)
2935 i--;
2936 break;
2937 case ALGORITHM_LEFT_SYMMETRIC:
2938 case ALGORITHM_RIGHT_SYMMETRIC:
2939 if (i < sh->pd_idx)
2940 i += raid_disks;
2941 i -= (sh->pd_idx + 1);
2942 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002943 case ALGORITHM_PARITY_0:
2944 i -= 1;
2945 break;
2946 case ALGORITHM_PARITY_N:
2947 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002949 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002950 }
2951 break;
2952 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002953 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002954 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002955 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002956 case ALGORITHM_LEFT_ASYMMETRIC:
2957 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002958 case ALGORITHM_ROTATING_ZERO_RESTART:
2959 case ALGORITHM_ROTATING_N_RESTART:
2960 if (sh->pd_idx == raid_disks-1)
2961 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002962 else if (i > sh->pd_idx)
2963 i -= 2; /* D D P Q D */
2964 break;
2965 case ALGORITHM_LEFT_SYMMETRIC:
2966 case ALGORITHM_RIGHT_SYMMETRIC:
2967 if (sh->pd_idx == raid_disks-1)
2968 i--; /* Q D D D P */
2969 else {
2970 /* D D P Q D */
2971 if (i < sh->pd_idx)
2972 i += raid_disks;
2973 i -= (sh->pd_idx + 2);
2974 }
2975 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002976 case ALGORITHM_PARITY_0:
2977 i -= 2;
2978 break;
2979 case ALGORITHM_PARITY_N:
2980 break;
2981 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002982 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002983 if (sh->pd_idx == 0)
2984 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002985 else {
2986 /* D D Q P D */
2987 if (i < sh->pd_idx)
2988 i += raid_disks;
2989 i -= (sh->pd_idx + 1);
2990 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002991 break;
2992 case ALGORITHM_LEFT_ASYMMETRIC_6:
2993 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2994 if (i > sh->pd_idx)
2995 i--;
2996 break;
2997 case ALGORITHM_LEFT_SYMMETRIC_6:
2998 case ALGORITHM_RIGHT_SYMMETRIC_6:
2999 if (i < sh->pd_idx)
3000 i += data_disks + 1;
3001 i -= (sh->pd_idx + 1);
3002 break;
3003 case ALGORITHM_PARITY_0_6:
3004 i -= 1;
3005 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07003006 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11003007 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07003008 }
3009 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 }
3011
3012 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10003013 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
NeilBrown112bf892009-03-31 14:39:38 +11003015 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11003016 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11003017 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
3018 || sh2.qd_idx != sh->qd_idx) {
NeilBrowncc6167b2016-11-02 14:16:50 +11003019 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
3020 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return 0;
3022 }
3023 return r_sector;
3024}
3025
Song Liu07e83362017-01-23 17:12:58 -08003026/*
3027 * There are cases where we want handle_stripe_dirtying() and
3028 * schedule_reconstruction() to delay towrite to some dev of a stripe.
3029 *
3030 * This function checks whether we want to delay the towrite. Specifically,
3031 * we delay the towrite when:
3032 *
3033 * 1. degraded stripe has a non-overwrite to the missing dev, AND this
3034 * stripe has data in journal (for other devices).
3035 *
3036 * In this case, when reading data for the non-overwrite dev, it is
3037 * necessary to handle complex rmw of write back cache (prexor with
3038 * orig_page, and xor with page). To keep read path simple, we would
3039 * like to flush data in journal to RAID disks first, so complex rmw
3040 * is handled in the write patch (handle_stripe_dirtying).
3041 *
Song Liu39b99582017-01-24 14:08:23 -08003042 * 2. when journal space is critical (R5C_LOG_CRITICAL=1)
3043 *
3044 * It is important to be able to flush all stripes in raid5-cache.
3045 * Therefore, we need reserve some space on the journal device for
3046 * these flushes. If flush operation includes pending writes to the
3047 * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe
3048 * for the flush out. If we exclude these pending writes from flush
3049 * operation, we only need (conf->max_degraded + 1) pages per stripe.
3050 * Therefore, excluding pending writes in these cases enables more
3051 * efficient use of the journal device.
3052 *
3053 * Note: To make sure the stripe makes progress, we only delay
3054 * towrite for stripes with data already in journal (injournal > 0).
3055 * When LOG_CRITICAL, stripes with injournal == 0 will be sent to
3056 * no_space_stripes list.
3057 *
Song Liu07e83362017-01-23 17:12:58 -08003058 */
Song Liu39b99582017-01-24 14:08:23 -08003059static inline bool delay_towrite(struct r5conf *conf,
3060 struct r5dev *dev,
3061 struct stripe_head_state *s)
Song Liu07e83362017-01-23 17:12:58 -08003062{
Song Liu39b99582017-01-24 14:08:23 -08003063 /* case 1 above */
3064 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3065 !test_bit(R5_Insync, &dev->flags) && s->injournal)
3066 return true;
3067 /* case 2 above */
3068 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
3069 s->injournal > 0)
3070 return true;
3071 return false;
Song Liu07e83362017-01-23 17:12:58 -08003072}
3073
Dan Williams600aa102008-06-28 08:32:05 +10003074static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003075schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10003076 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07003077{
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003078 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11003079 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003080 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07003081
Dan Williamse33129d2007-01-02 13:52:30 -07003082 if (rcw) {
Song Liu1e6d6902016-11-17 15:24:39 -08003083 /*
3084 * In some cases, handle_stripe_dirtying initially decided to
3085 * run rmw and allocates extra page for prexor. However, rcw is
3086 * cheaper later on. We need to free the extra page now,
3087 * because we won't be able to do that in ops_complete_prexor().
3088 */
3089 r5c_release_extra_page(sh);
Dan Williamse33129d2007-01-02 13:52:30 -07003090
3091 for (i = disks; i--; ) {
3092 struct r5dev *dev = &sh->dev[i];
3093
Song Liu39b99582017-01-24 14:08:23 -08003094 if (dev->towrite && !delay_towrite(conf, dev, s)) {
Dan Williamse33129d2007-01-02 13:52:30 -07003095 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10003096 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003097 if (!expand)
3098 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003099 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003100 } else if (test_bit(R5_InJournal, &dev->flags)) {
3101 set_bit(R5_LOCKED, &dev->flags);
3102 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003103 }
3104 }
NeilBrownce7d3632013-03-04 12:37:14 +11003105 /* if we are not expanding this is a proper write request, and
3106 * there will be bios with new data to be drained into the
3107 * stripe cache
3108 */
3109 if (!expand) {
3110 if (!s->locked)
3111 /* False alarm, nothing to do */
3112 return;
3113 sh->reconstruct_state = reconstruct_state_drain_run;
3114 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3115 } else
3116 sh->reconstruct_state = reconstruct_state_run;
3117
3118 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3119
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003120 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003121 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003122 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07003123 } else {
3124 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
3125 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003126 BUG_ON(level == 6 &&
3127 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
3128 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
Dan Williamse33129d2007-01-02 13:52:30 -07003129
Dan Williamse33129d2007-01-02 13:52:30 -07003130 for (i = disks; i--; ) {
3131 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003132 if (i == pd_idx || i == qd_idx)
Dan Williamse33129d2007-01-02 13:52:30 -07003133 continue;
3134
Dan Williamse33129d2007-01-02 13:52:30 -07003135 if (dev->towrite &&
3136 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10003137 test_bit(R5_Wantcompute, &dev->flags))) {
3138 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003139 set_bit(R5_LOCKED, &dev->flags);
3140 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003141 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003142 } else if (test_bit(R5_InJournal, &dev->flags)) {
3143 set_bit(R5_LOCKED, &dev->flags);
3144 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003145 }
3146 }
NeilBrownce7d3632013-03-04 12:37:14 +11003147 if (!s->locked)
3148 /* False alarm - nothing to do */
3149 return;
3150 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
3151 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
3152 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3153 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003154 }
3155
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003156 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07003157 * are in flight
3158 */
3159 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
3160 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10003161 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003162
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003163 if (level == 6) {
3164 int qd_idx = sh->qd_idx;
3165 struct r5dev *dev = &sh->dev[qd_idx];
3166
3167 set_bit(R5_LOCKED, &dev->flags);
3168 clear_bit(R5_UPTODATE, &dev->flags);
3169 s->locked++;
3170 }
3171
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02003172 if (raid5_has_ppl(sh->raid_conf) && sh->ppl_page &&
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01003173 test_bit(STRIPE_OP_BIODRAIN, &s->ops_request) &&
3174 !test_bit(STRIPE_FULL_WRITE, &sh->state) &&
3175 test_bit(R5_Insync, &sh->dev[pd_idx].flags))
3176 set_bit(STRIPE_OP_PARTIAL_PARITY, &s->ops_request);
3177
Dan Williams600aa102008-06-28 08:32:05 +10003178 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07003179 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10003180 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003181}
NeilBrown16a53ec2006-06-26 00:27:38 -07003182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183/*
3184 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07003185 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 * The bi_next chain must be in order.
3187 */
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003188static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
3189 int forwrite, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
3191 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11003192 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07003193 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
NeilBrowncbe47ec2011-07-26 11:20:35 +10003195 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003196 (unsigned long long)bi->bi_iter.bi_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 (unsigned long long)sh->sector);
3198
Shaohua Lib17459c2012-07-19 16:01:31 +10003199 spin_lock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003200 /* Don't allow new IO added to stripes in batch list */
3201 if (sh->batch_head)
3202 goto overlap;
NeilBrown72626682005-09-09 16:23:54 -07003203 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003205 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07003206 firstwrite = 1;
3207 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 bip = &sh->dev[dd_idx].toread;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003209 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
3210 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 goto overlap;
3212 bip = & (*bip)->bi_next;
3213 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07003214 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 goto overlap;
3216
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01003217 if (forwrite && raid5_has_ppl(conf)) {
3218 /*
3219 * With PPL only writes to consecutive data chunks within a
3220 * stripe are allowed because for a single stripe_head we can
3221 * only have one PPL entry at a time, which describes one data
3222 * range. Not really an overlap, but wait_for_overlap can be
3223 * used to handle this.
3224 */
3225 sector_t sector;
3226 sector_t first = 0;
3227 sector_t last = 0;
3228 int count = 0;
3229 int i;
3230
3231 for (i = 0; i < sh->disks; i++) {
3232 if (i != sh->pd_idx &&
3233 (i == dd_idx || sh->dev[i].towrite)) {
3234 sector = sh->dev[i].sector;
3235 if (count == 0 || sector < first)
3236 first = sector;
3237 if (sector > last)
3238 last = sector;
3239 count++;
3240 }
3241 }
3242
3243 if (first + conf->chunk_sectors * (count - 1) != last)
3244 goto overlap;
3245 }
3246
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003247 if (!forwrite || previous)
3248 clear_bit(STRIPE_BATCH_READY, &sh->state);
3249
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02003250 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 if (*bip)
3252 bi->bi_next = *bip;
3253 *bip = bi;
NeilBrown016c76a2017-03-15 14:05:13 +11003254 bio_inc_remaining(bi);
NeilBrown49728052017-03-15 14:05:12 +11003255 md_write_inc(conf->mddev, bi);
NeilBrown72626682005-09-09 16:23:54 -07003256
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 if (forwrite) {
3258 /* check if page is covered */
3259 sector_t sector = sh->dev[dd_idx].sector;
3260 for (bi=sh->dev[dd_idx].towrite;
3261 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07003262 bi && bi->bi_iter.bi_sector <= sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07003264 if (bio_end_sector(bi) >= sector)
3265 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 }
3267 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
shli@kernel.org7a87f432014-12-15 12:57:03 +11003268 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
3269 sh->overwrite_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003271
3272 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003273 (unsigned long long)(*bip)->bi_iter.bi_sector,
NeilBrowncbe47ec2011-07-26 11:20:35 +10003274 (unsigned long long)sh->sector, dd_idx);
3275
3276 if (conf->mddev->bitmap && firstwrite) {
NeilBrownd0852df52015-05-27 08:43:45 +10003277 /* Cannot hold spinlock over bitmap_startwrite,
3278 * but must ensure this isn't added to a batch until
3279 * we have added to the bitmap and set bm_seq.
3280 * So set STRIPE_BITMAP_PENDING to prevent
3281 * batching.
3282 * If multiple add_stripe_bio() calls race here they
3283 * much all set STRIPE_BITMAP_PENDING. So only the first one
3284 * to complete "bitmap_startwrite" gets to set
3285 * STRIPE_BIT_DELAY. This is important as once a stripe
3286 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3287 * any more.
3288 */
3289 set_bit(STRIPE_BITMAP_PENDING, &sh->state);
3290 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10003291 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
3292 STRIPE_SECTORS, 0);
NeilBrownd0852df52015-05-27 08:43:45 +10003293 spin_lock_irq(&sh->stripe_lock);
3294 clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
3295 if (!sh->batch_head) {
3296 sh->bm_seq = conf->seq_flush+1;
3297 set_bit(STRIPE_BIT_DELAY, &sh->state);
3298 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003299 }
NeilBrownd0852df52015-05-27 08:43:45 +10003300 spin_unlock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003301
3302 if (stripe_can_batch(sh))
3303 stripe_add_to_batch_list(conf, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 return 1;
3305
3306 overlap:
3307 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10003308 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 return 0;
3310}
3311
NeilBrownd1688a62011-10-11 16:49:52 +11003312static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08003313
NeilBrownd1688a62011-10-11 16:49:52 +11003314static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003315 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08003316{
NeilBrown784052e2009-03-31 15:19:07 +11003317 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10003318 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11003319 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003320 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11003321 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003322
NeilBrown112bf892009-03-31 14:39:38 +11003323 raid5_compute_sector(conf,
3324 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08003325 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11003326 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003327 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003328}
3329
Dan Williamsa4456852007-07-09 11:56:43 -07003330static void
NeilBrownd1688a62011-10-11 16:49:52 +11003331handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
NeilBrownbd83d0a2017-03-15 14:05:12 +11003332 struct stripe_head_state *s, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003333{
3334 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003335 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003336 for (i = disks; i--; ) {
3337 struct bio *bi;
3338 int bitmap_end = 0;
3339
3340 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003341 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07003342 rcu_read_lock();
3343 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownf5b67ae2016-06-02 16:19:53 +10003344 if (rdev && test_bit(In_sync, &rdev->flags) &&
3345 !test_bit(Faulty, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10003346 atomic_inc(&rdev->nr_pending);
3347 else
3348 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003349 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10003350 if (rdev) {
3351 if (!rdev_set_badblocks(
3352 rdev,
3353 sh->sector,
3354 STRIPE_SECTORS, 0))
3355 md_error(conf->mddev, rdev);
3356 rdev_dec_pending(rdev, conf->mddev);
3357 }
Dan Williamsa4456852007-07-09 11:56:43 -07003358 }
Shaohua Lib17459c2012-07-19 16:01:31 +10003359 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003360 /* fail all writes first */
3361 bi = sh->dev[i].towrite;
3362 sh->dev[i].towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11003363 sh->overwrite_disks = 0;
Shaohua Lib17459c2012-07-19 16:01:31 +10003364 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11003365 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003366 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07003367
Artur Paszkiewiczff875732017-03-09 09:59:58 +01003368 log_stripe_write_finished(sh);
Shaohua Li0576b1c2015-08-13 14:32:00 -07003369
Dan Williamsa4456852007-07-09 11:56:43 -07003370 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3371 wake_up(&conf->wait_for_overlap);
3372
Kent Overstreet4f024f32013-10-11 15:44:27 -07003373 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003374 sh->dev[i].sector + STRIPE_SECTORS) {
3375 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003376
3377 bi->bi_error = -EIO;
NeilBrown49728052017-03-15 14:05:12 +11003378 md_write_end(conf->mddev);
NeilBrown016c76a2017-03-15 14:05:13 +11003379 bio_endio(bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003380 bi = nextbi;
3381 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003382 if (bitmap_end)
3383 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3384 STRIPE_SECTORS, 0, 0);
3385 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003386 /* and fail all 'written' */
3387 bi = sh->dev[i].written;
3388 sh->dev[i].written = NULL;
Shaohua Lid592a992014-05-21 17:57:44 +08003389 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3390 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3391 sh->dev[i].page = sh->dev[i].orig_page;
3392 }
3393
Dan Williamsa4456852007-07-09 11:56:43 -07003394 if (bi) bitmap_end = 1;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003395 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003396 sh->dev[i].sector + STRIPE_SECTORS) {
3397 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003398
3399 bi->bi_error = -EIO;
NeilBrown49728052017-03-15 14:05:12 +11003400 md_write_end(conf->mddev);
NeilBrown016c76a2017-03-15 14:05:13 +11003401 bio_endio(bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003402 bi = bi2;
3403 }
3404
Dan Williamsb5e98d62007-01-02 13:52:31 -07003405 /* fail any reads if this device is non-operational and
3406 * the data has not reached the cache yet.
3407 */
3408 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
Shaohua Li6e74a9c2015-10-08 21:54:08 -07003409 s->failed > conf->max_degraded &&
Dan Williamsb5e98d62007-01-02 13:52:31 -07003410 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3411 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11003412 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003413 bi = sh->dev[i].toread;
3414 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11003415 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003416 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3417 wake_up(&conf->wait_for_overlap);
Shaohua Liebda7802015-09-18 10:20:13 -07003418 if (bi)
3419 s->to_read--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003420 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003421 sh->dev[i].sector + STRIPE_SECTORS) {
3422 struct bio *nextbi =
3423 r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003424
3425 bi->bi_error = -EIO;
NeilBrown016c76a2017-03-15 14:05:13 +11003426 bio_endio(bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003427 bi = nextbi;
3428 }
3429 }
Dan Williamsa4456852007-07-09 11:56:43 -07003430 if (bitmap_end)
3431 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3432 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10003433 /* If we were in the middle of a write the parity block might
3434 * still be locked - so just clear all R5_LOCKED flags
3435 */
3436 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003437 }
Shaohua Liebda7802015-09-18 10:20:13 -07003438 s->to_write = 0;
3439 s->written = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003440
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003441 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3442 if (atomic_dec_and_test(&conf->pending_full_writes))
3443 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003444}
3445
NeilBrown7f0da592011-07-28 11:39:22 +10003446static void
NeilBrownd1688a62011-10-11 16:49:52 +11003447handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10003448 struct stripe_head_state *s)
3449{
3450 int abort = 0;
3451 int i;
3452
shli@kernel.org59fc6302014-12-15 12:57:03 +11003453 BUG_ON(sh->batch_head);
NeilBrown7f0da592011-07-28 11:39:22 +10003454 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003455 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3456 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10003457 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11003458 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10003459 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10003460 * Don't even need to abort as that is handled elsewhere
3461 * if needed, and not always wanted e.g. if there is a known
3462 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11003463 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10003464 * non-sync devices, or abort the recovery
3465 */
NeilBrown18b98372012-04-01 23:48:38 +10003466 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3467 /* During recovery devices cannot be removed, so
3468 * locking and refcounting of rdevs is not needed
3469 */
NeilBrowne50d3992016-06-02 16:19:52 +10003470 rcu_read_lock();
NeilBrown18b98372012-04-01 23:48:38 +10003471 for (i = 0; i < conf->raid_disks; i++) {
NeilBrowne50d3992016-06-02 16:19:52 +10003472 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown18b98372012-04-01 23:48:38 +10003473 if (rdev
3474 && !test_bit(Faulty, &rdev->flags)
3475 && !test_bit(In_sync, &rdev->flags)
3476 && !rdev_set_badblocks(rdev, sh->sector,
3477 STRIPE_SECTORS, 0))
3478 abort = 1;
NeilBrowne50d3992016-06-02 16:19:52 +10003479 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown18b98372012-04-01 23:48:38 +10003480 if (rdev
3481 && !test_bit(Faulty, &rdev->flags)
3482 && !test_bit(In_sync, &rdev->flags)
3483 && !rdev_set_badblocks(rdev, sh->sector,
3484 STRIPE_SECTORS, 0))
3485 abort = 1;
3486 }
NeilBrowne50d3992016-06-02 16:19:52 +10003487 rcu_read_unlock();
NeilBrown18b98372012-04-01 23:48:38 +10003488 if (abort)
3489 conf->recovery_disabled =
3490 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10003491 }
NeilBrown18b98372012-04-01 23:48:38 +10003492 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10003493}
3494
NeilBrown9a3e1102011-12-23 10:17:53 +11003495static int want_replace(struct stripe_head *sh, int disk_idx)
3496{
3497 struct md_rdev *rdev;
3498 int rv = 0;
NeilBrown3f232d62016-06-02 16:19:52 +10003499
3500 rcu_read_lock();
3501 rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
NeilBrown9a3e1102011-12-23 10:17:53 +11003502 if (rdev
3503 && !test_bit(Faulty, &rdev->flags)
3504 && !test_bit(In_sync, &rdev->flags)
3505 && (rdev->recovery_offset <= sh->sector
3506 || rdev->mddev->recovery_cp <= sh->sector))
3507 rv = 1;
NeilBrown3f232d62016-06-02 16:19:52 +10003508 rcu_read_unlock();
NeilBrown9a3e1102011-12-23 10:17:53 +11003509 return rv;
3510}
3511
NeilBrown2c58f062015-02-02 11:32:23 +11003512static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3513 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07003514{
3515 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10003516 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3517 &sh->dev[s->failed_num[1]] };
NeilBrownea664c82015-02-02 14:03:28 +11003518 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07003519
NeilBrowna79cfe12015-02-02 11:37:59 +11003520
3521 if (test_bit(R5_LOCKED, &dev->flags) ||
3522 test_bit(R5_UPTODATE, &dev->flags))
3523 /* No point reading this as we already have it or have
3524 * decided to get it.
3525 */
3526 return 0;
3527
3528 if (dev->toread ||
3529 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3530 /* We need this block to directly satisfy a request */
3531 return 1;
3532
3533 if (s->syncing || s->expanding ||
3534 (s->replacing && want_replace(sh, disk_idx)))
3535 /* When syncing, or expanding we read everything.
3536 * When replacing, we need the replaced block.
3537 */
3538 return 1;
3539
3540 if ((s->failed >= 1 && fdev[0]->toread) ||
3541 (s->failed >= 2 && fdev[1]->toread))
3542 /* If we want to read from a failed device, then
3543 * we need to actually read every other device.
3544 */
3545 return 1;
3546
NeilBrowna9d56952015-02-02 11:49:10 +11003547 /* Sometimes neither read-modify-write nor reconstruct-write
3548 * cycles can work. In those cases we read every block we
3549 * can. Then the parity-update is certain to have enough to
3550 * work with.
3551 * This can only be a problem when we need to write something,
3552 * and some device has failed. If either of those tests
3553 * fail we need look no further.
3554 */
3555 if (!s->failed || !s->to_write)
3556 return 0;
3557
3558 if (test_bit(R5_Insync, &dev->flags) &&
3559 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3560 /* Pre-reads at not permitted until after short delay
3561 * to gather multiple requests. However if this
Zhilong Liu3560741e2017-03-15 16:14:53 +08003562 * device is no Insync, the block could only be computed
NeilBrowna9d56952015-02-02 11:49:10 +11003563 * and there is no need to delay that.
3564 */
3565 return 0;
NeilBrownea664c82015-02-02 14:03:28 +11003566
NeilBrown36707bb2015-09-24 15:25:36 +10003567 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrownea664c82015-02-02 14:03:28 +11003568 if (fdev[i]->towrite &&
3569 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3570 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3571 /* If we have a partial write to a failed
3572 * device, then we will need to reconstruct
3573 * the content of that device, so all other
3574 * devices must be read.
3575 */
3576 return 1;
3577 }
3578
3579 /* If we are forced to do a reconstruct-write, either because
3580 * the current RAID6 implementation only supports that, or
Zhilong Liu3560741e2017-03-15 16:14:53 +08003581 * because parity cannot be trusted and we are currently
NeilBrownea664c82015-02-02 14:03:28 +11003582 * recovering it, there is extra need to be careful.
3583 * If one of the devices that we would need to read, because
3584 * it is not being overwritten (and maybe not written at all)
3585 * is missing/faulty, then we need to read everything we can.
3586 */
3587 if (sh->raid_conf->level != 6 &&
3588 sh->sector < sh->raid_conf->mddev->recovery_cp)
3589 /* reconstruct-write isn't being forced */
3590 return 0;
NeilBrown36707bb2015-09-24 15:25:36 +10003591 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrown10d82c52015-05-08 18:19:33 +10003592 if (s->failed_num[i] != sh->pd_idx &&
3593 s->failed_num[i] != sh->qd_idx &&
3594 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
NeilBrownea664c82015-02-02 14:03:28 +11003595 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3596 return 1;
3597 }
3598
NeilBrown2c58f062015-02-02 11:32:23 +11003599 return 0;
3600}
3601
Song Liuba026842017-01-12 17:22:42 -08003602/* fetch_block - checks the given member device to see if its data needs
3603 * to be read or computed to satisfy a request.
3604 *
3605 * Returns 1 when no more member devices need to be checked, otherwise returns
3606 * 0 to tell the loop in handle_stripe_fill to continue
3607 */
NeilBrown2c58f062015-02-02 11:32:23 +11003608static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3609 int disk_idx, int disks)
3610{
3611 struct r5dev *dev = &sh->dev[disk_idx];
3612
3613 /* is the data in this block needed, and can we get it? */
3614 if (need_this_block(sh, s, disk_idx, disks)) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003615 /* we would like to get this block, possibly by computing it,
3616 * otherwise read it if the backing disk is insync
3617 */
3618 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3619 BUG_ON(test_bit(R5_Wantread, &dev->flags));
NeilBrownb0c783b2015-05-08 18:19:32 +10003620 BUG_ON(sh->batch_head);
NeilBrown7471fb72017-04-03 12:11:32 +10003621
3622 /*
3623 * In the raid6 case if the only non-uptodate disk is P
3624 * then we already trusted P to compute the other failed
3625 * drives. It is safe to compute rather than re-read P.
3626 * In other cases we only compute blocks from failed
3627 * devices, otherwise check/repair might fail to detect
3628 * a real inconsistency.
3629 */
3630
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003631 if ((s->uptodate == disks - 1) &&
NeilBrown7471fb72017-04-03 12:11:32 +10003632 ((sh->qd_idx >= 0 && sh->pd_idx == disk_idx) ||
NeilBrownf2b3b442011-07-26 11:35:19 +10003633 (s->failed && (disk_idx == s->failed_num[0] ||
NeilBrown7471fb72017-04-03 12:11:32 +10003634 disk_idx == s->failed_num[1])))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003635 /* have disk failed, and we're requested to fetch it;
3636 * do compute it
3637 */
3638 pr_debug("Computing stripe %llu block %d\n",
3639 (unsigned long long)sh->sector, disk_idx);
3640 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3641 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3642 set_bit(R5_Wantcompute, &dev->flags);
3643 sh->ops.target = disk_idx;
3644 sh->ops.target2 = -1; /* no 2nd target */
3645 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10003646 /* Careful: from this point on 'uptodate' is in the eye
3647 * of raid_run_ops which services 'compute' operations
3648 * before writes. R5_Wantcompute flags a block that will
3649 * be R5_UPTODATE by the time it is needed for a
3650 * subsequent operation.
3651 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003652 s->uptodate++;
3653 return 1;
3654 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3655 /* Computing 2-failure is *very* expensive; only
3656 * do it if failed >= 2
3657 */
3658 int other;
3659 for (other = disks; other--; ) {
3660 if (other == disk_idx)
3661 continue;
3662 if (!test_bit(R5_UPTODATE,
3663 &sh->dev[other].flags))
3664 break;
3665 }
3666 BUG_ON(other < 0);
3667 pr_debug("Computing stripe %llu blocks %d,%d\n",
3668 (unsigned long long)sh->sector,
3669 disk_idx, other);
3670 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3671 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3672 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3673 set_bit(R5_Wantcompute, &sh->dev[other].flags);
3674 sh->ops.target = disk_idx;
3675 sh->ops.target2 = other;
3676 s->uptodate += 2;
3677 s->req_compute = 1;
3678 return 1;
3679 } else if (test_bit(R5_Insync, &dev->flags)) {
3680 set_bit(R5_LOCKED, &dev->flags);
3681 set_bit(R5_Wantread, &dev->flags);
3682 s->locked++;
3683 pr_debug("Reading block %d (sync=%d)\n",
3684 disk_idx, s->syncing);
3685 }
3686 }
3687
3688 return 0;
3689}
3690
3691/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10003692 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003693 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10003694static void handle_stripe_fill(struct stripe_head *sh,
3695 struct stripe_head_state *s,
3696 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003697{
3698 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003699
3700 /* look for blocks to read/compute, skip this if a compute
3701 * is already in flight, or if the stripe contents are in the
3702 * midst of changing due to a write
3703 */
3704 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Song Liu07e83362017-01-23 17:12:58 -08003705 !sh->reconstruct_state) {
3706
3707 /*
3708 * For degraded stripe with data in journal, do not handle
3709 * read requests yet, instead, flush the stripe to raid
3710 * disks first, this avoids handling complex rmw of write
3711 * back cache (prexor with orig_page, and then xor with
3712 * page) in the read path
3713 */
3714 if (s->injournal && s->failed) {
3715 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
3716 r5c_make_stripe_write_out(sh);
3717 goto out;
3718 }
3719
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003720 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10003721 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003722 break;
Song Liu07e83362017-01-23 17:12:58 -08003723 }
3724out:
Dan Williamsa4456852007-07-09 11:56:43 -07003725 set_bit(STRIPE_HANDLE, &sh->state);
3726}
3727
NeilBrown787b76f2015-05-21 12:56:41 +10003728static void break_stripe_batch_list(struct stripe_head *head_sh,
3729 unsigned long handle_flags);
Dan Williams1fe797e2008-06-28 09:16:30 +10003730/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07003731 * any written block on an uptodate or failed drive can be returned.
3732 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3733 * never LOCKED, so we don't need to test 'failed' directly.
3734 */
NeilBrownd1688a62011-10-11 16:49:52 +11003735static void handle_stripe_clean_event(struct r5conf *conf,
NeilBrownbd83d0a2017-03-15 14:05:12 +11003736 struct stripe_head *sh, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003737{
3738 int i;
3739 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003740 int discard_pending = 0;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003741 struct stripe_head *head_sh = sh;
3742 bool do_endio = false;
Dan Williamsa4456852007-07-09 11:56:43 -07003743
3744 for (i = disks; i--; )
3745 if (sh->dev[i].written) {
3746 dev = &sh->dev[i];
3747 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003748 (test_bit(R5_UPTODATE, &dev->flags) ||
Shaohua Lid592a992014-05-21 17:57:44 +08003749 test_bit(R5_Discard, &dev->flags) ||
3750 test_bit(R5_SkipCopy, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003751 /* We can return any write requests */
3752 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003753 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003754 if (test_and_clear_bit(R5_Discard, &dev->flags))
3755 clear_bit(R5_UPTODATE, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08003756 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3757 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
Shaohua Lid592a992014-05-21 17:57:44 +08003758 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003759 do_endio = true;
3760
3761returnbi:
3762 dev->page = dev->orig_page;
Dan Williamsa4456852007-07-09 11:56:43 -07003763 wbi = dev->written;
3764 dev->written = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003765 while (wbi && wbi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003766 dev->sector + STRIPE_SECTORS) {
3767 wbi2 = r5_next_bio(wbi, dev->sector);
NeilBrown49728052017-03-15 14:05:12 +11003768 md_write_end(conf->mddev);
NeilBrown016c76a2017-03-15 14:05:13 +11003769 bio_endio(wbi);
Dan Williamsa4456852007-07-09 11:56:43 -07003770 wbi = wbi2;
3771 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003772 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3773 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07003774 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003775 0);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003776 if (head_sh->batch_head) {
3777 sh = list_first_entry(&sh->batch_list,
3778 struct stripe_head,
3779 batch_list);
3780 if (sh != head_sh) {
3781 dev = &sh->dev[i];
3782 goto returnbi;
3783 }
3784 }
3785 sh = head_sh;
3786 dev = &sh->dev[i];
NeilBrownf8dfcff2013-03-12 12:18:06 +11003787 } else if (test_bit(R5_Discard, &dev->flags))
3788 discard_pending = 1;
3789 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -07003790
Artur Paszkiewiczff875732017-03-09 09:59:58 +01003791 log_stripe_write_finished(sh);
Shaohua Li0576b1c2015-08-13 14:32:00 -07003792
NeilBrownf8dfcff2013-03-12 12:18:06 +11003793 if (!discard_pending &&
3794 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003795 int hash;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003796 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3797 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3798 if (sh->qd_idx >= 0) {
3799 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3800 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3801 }
3802 /* now that discard is done we can proceed with any sync */
3803 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08003804 /*
3805 * SCSI discard will change some bio fields and the stripe has
3806 * no updated data, so remove it from hash list and the stripe
3807 * will be reinitialized
3808 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11003809unhash:
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003810 hash = sh->hash_lock_index;
3811 spin_lock_irq(conf->hash_locks + hash);
Shaohua Lid47648f2013-10-19 14:51:42 +08003812 remove_hash(sh);
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003813 spin_unlock_irq(conf->hash_locks + hash);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003814 if (head_sh->batch_head) {
3815 sh = list_first_entry(&sh->batch_list,
3816 struct stripe_head, batch_list);
3817 if (sh != head_sh)
3818 goto unhash;
3819 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003820 sh = head_sh;
3821
NeilBrownf8dfcff2013-03-12 12:18:06 +11003822 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3823 set_bit(STRIPE_HANDLE, &sh->state);
3824
3825 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003826
3827 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3828 if (atomic_dec_and_test(&conf->pending_full_writes))
3829 md_wakeup_thread(conf->mddev->thread);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003830
NeilBrown787b76f2015-05-21 12:56:41 +10003831 if (head_sh->batch_head && do_endio)
3832 break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
Dan Williamsa4456852007-07-09 11:56:43 -07003833}
3834
Song Liu86aa1392017-01-12 17:22:41 -08003835/*
3836 * For RMW in write back cache, we need extra page in prexor to store the
3837 * old data. This page is stored in dev->orig_page.
3838 *
3839 * This function checks whether we have data for prexor. The exact logic
3840 * is:
3841 * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
3842 */
3843static inline bool uptodate_for_rmw(struct r5dev *dev)
3844{
3845 return (test_bit(R5_UPTODATE, &dev->flags)) &&
3846 (!test_bit(R5_InJournal, &dev->flags) ||
3847 test_bit(R5_OrigPageUPTDODATE, &dev->flags));
3848}
3849
Song Liud7bd3982016-11-23 22:50:39 -08003850static int handle_stripe_dirtying(struct r5conf *conf,
3851 struct stripe_head *sh,
3852 struct stripe_head_state *s,
3853 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003854{
3855 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003856 sector_t recovery_cp = conf->mddev->recovery_cp;
3857
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003858 /* Check whether resync is now happening or should start.
Alexander Lyakasa7854482012-10-11 13:50:12 +11003859 * If yes, then the array is dirty (after unclean shutdown or
3860 * initial creation), so parity in some stripes might be inconsistent.
3861 * In this case, we need to always do reconstruct-write, to ensure
3862 * that in case of drive failure or read-error correction, we
3863 * generate correct data from the parity.
3864 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003865 if (conf->rmw_level == PARITY_DISABLE_RMW ||
NeilBrown26ac1072015-02-18 11:35:14 +11003866 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
3867 s->failed == 0)) {
Alexander Lyakasa7854482012-10-11 13:50:12 +11003868 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10003869 * look like rcw is cheaper
3870 */
3871 rcw = 1; rmw = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003872 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
3873 conf->rmw_level, (unsigned long long)recovery_cp,
Alexander Lyakasa7854482012-10-11 13:50:12 +11003874 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10003875 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003876 /* would I have to read this buffer for read_modify_write */
3877 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08003878 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu07e83362017-01-23 17:12:58 -08003879 i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08003880 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003881 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08003882 !(uptodate_for_rmw(dev) ||
Dan Williamsf38e1212007-01-02 13:52:30 -07003883 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003884 if (test_bit(R5_Insync, &dev->flags))
3885 rmw++;
3886 else
3887 rmw += 2*disks; /* cannot read it */
3888 }
3889 /* Would I have to read this buffer for reconstruct_write */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003890 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3891 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003892 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003893 !(test_bit(R5_UPTODATE, &dev->flags) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003894 test_bit(R5_Wantcompute, &dev->flags))) {
NeilBrown67f45542014-05-28 13:39:22 +10003895 if (test_bit(R5_Insync, &dev->flags))
3896 rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07003897 else
3898 rcw += 2*disks;
3899 }
3900 }
Song Liu1e6d6902016-11-17 15:24:39 -08003901
Song Liu39b99582017-01-24 14:08:23 -08003902 pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n",
3903 (unsigned long long)sh->sector, sh->state, rmw, rcw);
Dan Williamsa4456852007-07-09 11:56:43 -07003904 set_bit(STRIPE_HANDLE, &sh->state);
Song Liu41257582016-05-23 17:25:06 -07003905 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003906 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003907 if (conf->mddev->queue)
3908 blk_add_trace_msg(conf->mddev->queue,
3909 "raid5 rmw %llu %d",
3910 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07003911 for (i = disks; i--; ) {
3912 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08003913 if (test_bit(R5_InJournal, &dev->flags) &&
3914 dev->page == dev->orig_page &&
3915 !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) {
3916 /* alloc page for prexor */
Song Liud7bd3982016-11-23 22:50:39 -08003917 struct page *p = alloc_page(GFP_NOIO);
Song Liu1e6d6902016-11-17 15:24:39 -08003918
Song Liud7bd3982016-11-23 22:50:39 -08003919 if (p) {
3920 dev->orig_page = p;
3921 continue;
3922 }
3923
3924 /*
3925 * alloc_page() failed, try use
3926 * disk_info->extra_page
3927 */
3928 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE,
3929 &conf->cache_state)) {
3930 r5c_use_extra_page(sh);
3931 break;
3932 }
3933
3934 /* extra_page in use, add to delayed_list */
3935 set_bit(STRIPE_DELAYED, &sh->state);
3936 s->waiting_extra_page = 1;
3937 return -EAGAIN;
Song Liu1e6d6902016-11-17 15:24:39 -08003938 }
Song Liud7bd3982016-11-23 22:50:39 -08003939 }
Song Liu1e6d6902016-11-17 15:24:39 -08003940
Song Liud7bd3982016-11-23 22:50:39 -08003941 for (i = disks; i--; ) {
3942 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08003943 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003944 i == sh->pd_idx || i == sh->qd_idx ||
3945 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003946 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08003947 !(uptodate_for_rmw(dev) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003948 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003949 test_bit(R5_Insync, &dev->flags)) {
NeilBrown67f45542014-05-28 13:39:22 +10003950 if (test_bit(STRIPE_PREREAD_ACTIVE,
3951 &sh->state)) {
3952 pr_debug("Read_old block %d for r-m-w\n",
3953 i);
Dan Williamsa4456852007-07-09 11:56:43 -07003954 set_bit(R5_LOCKED, &dev->flags);
3955 set_bit(R5_Wantread, &dev->flags);
3956 s->locked++;
3957 } else {
3958 set_bit(STRIPE_DELAYED, &sh->state);
3959 set_bit(STRIPE_HANDLE, &sh->state);
3960 }
3961 }
3962 }
NeilBrowna9add5d2012-10-31 11:59:09 +11003963 }
Song Liu41257582016-05-23 17:25:06 -07003964 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003965 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11003966 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10003967 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003968 for (i = disks; i--; ) {
3969 struct r5dev *dev = &sh->dev[i];
3970 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10003971 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003972 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003973 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10003974 test_bit(R5_Wantcompute, &dev->flags))) {
3975 rcw++;
NeilBrown67f45542014-05-28 13:39:22 +10003976 if (test_bit(R5_Insync, &dev->flags) &&
3977 test_bit(STRIPE_PREREAD_ACTIVE,
3978 &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07003979 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07003980 "%d for Reconstruct\n", i);
3981 set_bit(R5_LOCKED, &dev->flags);
3982 set_bit(R5_Wantread, &dev->flags);
3983 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11003984 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07003985 } else {
3986 set_bit(STRIPE_DELAYED, &sh->state);
3987 set_bit(STRIPE_HANDLE, &sh->state);
3988 }
3989 }
3990 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003991 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11003992 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3993 (unsigned long long)sh->sector,
3994 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10003995 }
NeilBrownb1b02fe2015-02-02 10:44:29 +11003996
3997 if (rcw > disks && rmw > disks &&
3998 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3999 set_bit(STRIPE_DELAYED, &sh->state);
4000
Dan Williamsa4456852007-07-09 11:56:43 -07004001 /* now if nothing is locked, and if we have enough data,
4002 * we can start a write request
4003 */
Dan Williamsf38e1212007-01-02 13:52:30 -07004004 /* since handle_stripe can be called at any time we need to handle the
4005 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07004006 * subsequent call wants to start a write request. raid_run_ops only
4007 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07004008 * simultaneously. If this is not the case then new writes need to be
4009 * held off until the compute completes.
4010 */
Dan Williams976ea8d2008-06-28 08:32:03 +10004011 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
4012 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
Song Liu1e6d6902016-11-17 15:24:39 -08004013 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07004014 schedule_reconstruction(sh, s, rcw == 0, 0);
Song Liud7bd3982016-11-23 22:50:39 -08004015 return 0;
Dan Williamsa4456852007-07-09 11:56:43 -07004016}
4017
NeilBrownd1688a62011-10-11 16:49:52 +11004018static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07004019 struct stripe_head_state *s, int disks)
4020{
Dan Williamsecc65c92008-06-28 08:31:57 +10004021 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07004022
shli@kernel.org59fc6302014-12-15 12:57:03 +11004023 BUG_ON(sh->batch_head);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004024 set_bit(STRIPE_HANDLE, &sh->state);
4025
Dan Williamsecc65c92008-06-28 08:31:57 +10004026 switch (sh->check_state) {
4027 case check_state_idle:
4028 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07004029 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07004030 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10004031 sh->check_state = check_state_run;
4032 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004033 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004034 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10004035 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07004036 }
NeilBrownf2b3b442011-07-26 11:35:19 +10004037 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10004038 /* fall through */
4039 case check_state_compute_result:
4040 sh->check_state = check_state_idle;
4041 if (!dev)
4042 dev = &sh->dev[sh->pd_idx];
4043
4044 /* check that a write has not made the stripe insync */
4045 if (test_bit(STRIPE_INSYNC, &sh->state))
4046 break;
4047
4048 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07004049 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
4050 BUG_ON(s->uptodate != disks);
4051
4052 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10004053 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07004054 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07004055
Dan Williamsa4456852007-07-09 11:56:43 -07004056 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07004057 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10004058 break;
4059 case check_state_run:
4060 break; /* we will be called again upon completion */
4061 case check_state_check_result:
4062 sh->check_state = check_state_idle;
4063
4064 /* if a failure occurred during the check operation, leave
4065 * STRIPE_INSYNC not set and let the stripe be handled again
4066 */
4067 if (s->failed)
4068 break;
4069
4070 /* handle a successful check operation, if parity is correct
4071 * we are done. Otherwise update the mismatch count and repair
4072 * parity if !MD_RECOVERY_CHECK
4073 */
Dan Williamsad283ea2009-08-29 19:09:26 -07004074 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10004075 /* parity is correct (on disc,
4076 * not in buffer any more)
4077 */
4078 set_bit(STRIPE_INSYNC, &sh->state);
4079 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004080 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10004081 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
4082 /* don't try to repair!! */
4083 set_bit(STRIPE_INSYNC, &sh->state);
4084 else {
4085 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10004086 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10004087 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4088 set_bit(R5_Wantcompute,
4089 &sh->dev[sh->pd_idx].flags);
4090 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07004091 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10004092 s->uptodate++;
4093 }
4094 }
4095 break;
4096 case check_state_compute_run:
4097 break;
4098 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11004099 pr_err("%s: unknown check_state: %d sector: %llu\n",
Dan Williamsecc65c92008-06-28 08:31:57 +10004100 __func__, sh->check_state,
4101 (unsigned long long) sh->sector);
4102 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07004103 }
4104}
4105
NeilBrownd1688a62011-10-11 16:49:52 +11004106static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07004107 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10004108 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07004109{
Dan Williamsa4456852007-07-09 11:56:43 -07004110 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11004111 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07004112 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07004113
shli@kernel.org59fc6302014-12-15 12:57:03 +11004114 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07004115 set_bit(STRIPE_HANDLE, &sh->state);
4116
4117 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004118
Dan Williamsa4456852007-07-09 11:56:43 -07004119 /* Want to check and possibly repair P and Q.
4120 * However there could be one 'failed' device, in which
4121 * case we can only check one of them, possibly using the
4122 * other to generate missing data
4123 */
4124
Dan Williamsd82dfee2009-07-14 13:40:57 -07004125 switch (sh->check_state) {
4126 case check_state_idle:
4127 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10004128 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004129 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07004130 * makes sense to check P (If anything else were failed,
4131 * we would have used P to recreate it).
4132 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004133 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07004134 }
NeilBrownf2b3b442011-07-26 11:35:19 +10004135 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004136 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07004137 * anything, so it makes sense to check it
4138 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004139 if (sh->check_state == check_state_run)
4140 sh->check_state = check_state_run_pq;
4141 else
4142 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07004143 }
Dan Williams36d1c642009-07-14 11:48:22 -07004144
Dan Williamsd82dfee2009-07-14 13:40:57 -07004145 /* discard potentially stale zero_sum_result */
4146 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07004147
Dan Williamsd82dfee2009-07-14 13:40:57 -07004148 if (sh->check_state == check_state_run) {
4149 /* async_xor_zero_sum destroys the contents of P */
4150 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
4151 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07004152 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004153 if (sh->check_state >= check_state_run &&
4154 sh->check_state <= check_state_run_pq) {
4155 /* async_syndrome_zero_sum preserves P and Q, so
4156 * no need to mark them !uptodate here
4157 */
4158 set_bit(STRIPE_OP_CHECK, &s->ops_request);
4159 break;
4160 }
Dan Williams36d1c642009-07-14 11:48:22 -07004161
Dan Williamsd82dfee2009-07-14 13:40:57 -07004162 /* we have 2-disk failure */
4163 BUG_ON(s->failed != 2);
4164 /* fall through */
4165 case check_state_compute_result:
4166 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07004167
Dan Williamsd82dfee2009-07-14 13:40:57 -07004168 /* check that a write has not made the stripe insync */
4169 if (test_bit(STRIPE_INSYNC, &sh->state))
4170 break;
Dan Williamsa4456852007-07-09 11:56:43 -07004171
4172 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07004173 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07004174 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004175 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07004176 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004177 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07004178 s->locked++;
4179 set_bit(R5_LOCKED, &dev->flags);
4180 set_bit(R5_Wantwrite, &dev->flags);
4181 }
4182 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004183 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07004184 s->locked++;
4185 set_bit(R5_LOCKED, &dev->flags);
4186 set_bit(R5_Wantwrite, &dev->flags);
4187 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004188 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004189 dev = &sh->dev[pd_idx];
4190 s->locked++;
4191 set_bit(R5_LOCKED, &dev->flags);
4192 set_bit(R5_Wantwrite, &dev->flags);
4193 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004194 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004195 dev = &sh->dev[qd_idx];
4196 s->locked++;
4197 set_bit(R5_LOCKED, &dev->flags);
4198 set_bit(R5_Wantwrite, &dev->flags);
4199 }
4200 clear_bit(STRIPE_DEGRADED, &sh->state);
4201
4202 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004203 break;
4204 case check_state_run:
4205 case check_state_run_q:
4206 case check_state_run_pq:
4207 break; /* we will be called again upon completion */
4208 case check_state_check_result:
4209 sh->check_state = check_state_idle;
4210
4211 /* handle a successful check operation, if parity is correct
4212 * we are done. Otherwise update the mismatch count and repair
4213 * parity if !MD_RECOVERY_CHECK
4214 */
4215 if (sh->ops.zero_sum_result == 0) {
4216 /* both parities are correct */
4217 if (!s->failed)
4218 set_bit(STRIPE_INSYNC, &sh->state);
4219 else {
4220 /* in contrast to the raid5 case we can validate
4221 * parity, but still have a failure to write
4222 * back
4223 */
4224 sh->check_state = check_state_compute_result;
4225 /* Returning at this point means that we may go
4226 * off and bring p and/or q uptodate again so
4227 * we make sure to check zero_sum_result again
4228 * to verify if p or q need writeback
4229 */
4230 }
4231 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004232 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004233 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
4234 /* don't try to repair!! */
4235 set_bit(STRIPE_INSYNC, &sh->state);
4236 else {
4237 int *target = &sh->ops.target;
4238
4239 sh->ops.target = -1;
4240 sh->ops.target2 = -1;
4241 sh->check_state = check_state_compute_run;
4242 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4243 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4244 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4245 set_bit(R5_Wantcompute,
4246 &sh->dev[pd_idx].flags);
4247 *target = pd_idx;
4248 target = &sh->ops.target2;
4249 s->uptodate++;
4250 }
4251 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4252 set_bit(R5_Wantcompute,
4253 &sh->dev[qd_idx].flags);
4254 *target = qd_idx;
4255 s->uptodate++;
4256 }
4257 }
4258 }
4259 break;
4260 case check_state_compute_run:
4261 break;
4262 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11004263 pr_warn("%s: unknown check_state: %d sector: %llu\n",
4264 __func__, sh->check_state,
4265 (unsigned long long) sh->sector);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004266 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07004267 }
4268}
4269
NeilBrownd1688a62011-10-11 16:49:52 +11004270static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07004271{
4272 int i;
4273
4274 /* We have read all the blocks in this stripe and now we need to
4275 * copy some of them into a target stripe for expand.
4276 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07004277 struct dma_async_tx_descriptor *tx = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +11004278 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07004279 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4280 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11004281 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11004282 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07004283 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07004284 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07004285
Shaohua Li6d036f72015-08-13 14:31:57 -07004286 sector_t bn = raid5_compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11004287 sector_t s = raid5_compute_sector(conf, bn, 0,
4288 &dd_idx, NULL);
Shaohua Li6d036f72015-08-13 14:31:57 -07004289 sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07004290 if (sh2 == NULL)
4291 /* so far only the early blocks of this stripe
4292 * have been requested. When later blocks
4293 * get requested, we will try again
4294 */
4295 continue;
4296 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
4297 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
4298 /* must have already done this block */
Shaohua Li6d036f72015-08-13 14:31:57 -07004299 raid5_release_stripe(sh2);
Dan Williamsa4456852007-07-09 11:56:43 -07004300 continue;
4301 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07004302
4303 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07004304 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004305 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07004306 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07004307 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004308
Dan Williamsa4456852007-07-09 11:56:43 -07004309 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
4310 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
4311 for (j = 0; j < conf->raid_disks; j++)
4312 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10004313 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07004314 !test_bit(R5_Expanded, &sh2->dev[j].flags))
4315 break;
4316 if (j == conf->raid_disks) {
4317 set_bit(STRIPE_EXPAND_READY, &sh2->state);
4318 set_bit(STRIPE_HANDLE, &sh2->state);
4319 }
Shaohua Li6d036f72015-08-13 14:31:57 -07004320 raid5_release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004321
Dan Williamsa4456852007-07-09 11:56:43 -07004322 }
NeilBrowna2e08552007-09-11 15:23:36 -07004323 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11004324 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07004325}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
4327/*
4328 * handle_stripe - do things to a stripe.
4329 *
NeilBrown9a3e1102011-12-23 10:17:53 +11004330 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4331 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11004333 * return some read requests which now have data
4334 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 * schedule a read on some buffers
4336 * schedule a write of some buffers
4337 * return confirmation of parity correctness
4338 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 */
Dan Williamsa4456852007-07-09 11:56:43 -07004340
NeilBrownacfe7262011-07-27 11:00:36 +10004341static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07004342{
NeilBrownd1688a62011-10-11 16:49:52 +11004343 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08004344 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004345 struct r5dev *dev;
4346 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11004347 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07004348
NeilBrownacfe7262011-07-27 11:00:36 +10004349 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07004350
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004351 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
4352 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
NeilBrownacfe7262011-07-27 11:00:36 +10004353 s->failed_num[0] = -1;
4354 s->failed_num[1] = -1;
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004355 s->log_failed = r5l_log_disk_error(conf);
NeilBrownacfe7262011-07-27 11:00:36 +10004356
4357 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07004358 rcu_read_lock();
4359 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004360 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10004361 sector_t first_bad;
4362 int bad_sectors;
4363 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10004364
NeilBrown16a53ec2006-06-26 00:27:38 -07004365 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07004366
Dan Williams45b42332007-07-09 11:56:43 -07004367 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11004368 i, dev->flags,
4369 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004370 /* maybe we can reply to a read
4371 *
4372 * new wantfill requests are only permitted while
4373 * ops_complete_biofill is guaranteed to be inactive
4374 */
4375 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4376 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4377 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07004378
4379 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10004380 if (test_bit(R5_LOCKED, &dev->flags))
4381 s->locked++;
4382 if (test_bit(R5_UPTODATE, &dev->flags))
4383 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004384 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004385 s->compute++;
4386 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004387 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004388
NeilBrownacfe7262011-07-27 11:00:36 +10004389 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004390 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10004391 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10004392 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004393 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10004394 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004395 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004396 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004397 }
Dan Williamsa4456852007-07-09 11:56:43 -07004398 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10004399 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11004400 /* Prefer to use the replacement for reads, but only
4401 * if it is recovered enough and has no bad blocks.
4402 */
4403 rdev = rcu_dereference(conf->disks[i].replacement);
4404 if (rdev && !test_bit(Faulty, &rdev->flags) &&
4405 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
4406 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4407 &first_bad, &bad_sectors))
4408 set_bit(R5_ReadRepl, &dev->flags);
4409 else {
NeilBrowne6030cb2015-07-17 13:26:23 +10004410 if (rdev && !test_bit(Faulty, &rdev->flags))
NeilBrown9a3e1102011-12-23 10:17:53 +11004411 set_bit(R5_NeedReplace, &dev->flags);
NeilBrowne6030cb2015-07-17 13:26:23 +10004412 else
4413 clear_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11004414 rdev = rcu_dereference(conf->disks[i].rdev);
4415 clear_bit(R5_ReadRepl, &dev->flags);
4416 }
NeilBrown9283d8c2011-12-08 16:27:57 +11004417 if (rdev && test_bit(Faulty, &rdev->flags))
4418 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10004419 if (rdev) {
4420 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4421 &first_bad, &bad_sectors);
4422 if (s->blocked_rdev == NULL
4423 && (test_bit(Blocked, &rdev->flags)
4424 || is_bad < 0)) {
4425 if (is_bad < 0)
4426 set_bit(BlockedBadBlocks,
4427 &rdev->flags);
4428 s->blocked_rdev = rdev;
4429 atomic_inc(&rdev->nr_pending);
4430 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004431 }
NeilBrown415e72d2010-06-17 17:25:21 +10004432 clear_bit(R5_Insync, &dev->flags);
4433 if (!rdev)
4434 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10004435 else if (is_bad) {
4436 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10004437 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4438 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10004439 /* treat as in-sync, but with a read error
4440 * which we can now try to correct
4441 */
4442 set_bit(R5_Insync, &dev->flags);
4443 set_bit(R5_ReadError, &dev->flags);
4444 }
4445 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10004446 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11004447 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10004448 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11004449 set_bit(R5_Insync, &dev->flags);
4450 else if (test_bit(R5_UPTODATE, &dev->flags) &&
4451 test_bit(R5_Expanded, &dev->flags))
4452 /* If we've reshaped into here, we assume it is Insync.
4453 * We will shortly update recovery_offset to make
4454 * it official.
4455 */
4456 set_bit(R5_Insync, &dev->flags);
4457
NeilBrown1cc03eb2014-01-06 13:19:42 +11004458 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004459 /* This flag does not apply to '.replacement'
4460 * only to .rdev, so make sure to check that*/
4461 struct md_rdev *rdev2 = rcu_dereference(
4462 conf->disks[i].rdev);
4463 if (rdev2 == rdev)
4464 clear_bit(R5_Insync, &dev->flags);
4465 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004466 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004467 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10004468 } else
4469 clear_bit(R5_WriteError, &dev->flags);
4470 }
NeilBrown1cc03eb2014-01-06 13:19:42 +11004471 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004472 /* This flag does not apply to '.replacement'
4473 * only to .rdev, so make sure to check that*/
4474 struct md_rdev *rdev2 = rcu_dereference(
4475 conf->disks[i].rdev);
4476 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10004477 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004478 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10004479 } else
4480 clear_bit(R5_MadeGood, &dev->flags);
4481 }
NeilBrown977df362011-12-23 10:17:53 +11004482 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4483 struct md_rdev *rdev2 = rcu_dereference(
4484 conf->disks[i].replacement);
4485 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4486 s->handle_bad_blocks = 1;
4487 atomic_inc(&rdev2->nr_pending);
4488 } else
4489 clear_bit(R5_MadeGoodRepl, &dev->flags);
4490 }
NeilBrown415e72d2010-06-17 17:25:21 +10004491 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004492 /* The ReadError flag will just be confusing now */
4493 clear_bit(R5_ReadError, &dev->flags);
4494 clear_bit(R5_ReWrite, &dev->flags);
4495 }
NeilBrown415e72d2010-06-17 17:25:21 +10004496 if (test_bit(R5_ReadError, &dev->flags))
4497 clear_bit(R5_Insync, &dev->flags);
4498 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004499 if (s->failed < 2)
4500 s->failed_num[s->failed] = i;
4501 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11004502 if (rdev && !test_bit(Faulty, &rdev->flags))
4503 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10004504 }
Song Liu2ded3702016-11-17 15:24:38 -08004505
4506 if (test_bit(R5_InJournal, &dev->flags))
4507 s->injournal++;
Song Liu1e6d6902016-11-17 15:24:39 -08004508 if (test_bit(R5_InJournal, &dev->flags) && dev->written)
4509 s->just_cached++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004510 }
NeilBrown9a3e1102011-12-23 10:17:53 +11004511 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4512 /* If there is a failed device being replaced,
4513 * we must be recovering.
4514 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10004515 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11004516 * else we can only be replacing
4517 * sync and recovery both need to read all devices, and so
4518 * use the same flag.
4519 */
4520 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10004521 sh->sector >= conf->mddev->recovery_cp ||
4522 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11004523 s->syncing = 1;
4524 else
4525 s->replacing = 1;
4526 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004527 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10004528}
NeilBrownf4168852007-02-28 20:11:53 -08004529
shli@kernel.org59fc6302014-12-15 12:57:03 +11004530static int clear_batch_ready(struct stripe_head *sh)
4531{
NeilBrownb15a9db2015-05-22 15:20:04 +10004532 /* Return '1' if this is a member of batch, or
4533 * '0' if it is a lone stripe or a head which can now be
4534 * handled.
4535 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11004536 struct stripe_head *tmp;
4537 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
NeilBrownb15a9db2015-05-22 15:20:04 +10004538 return (sh->batch_head && sh->batch_head != sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11004539 spin_lock(&sh->stripe_lock);
4540 if (!sh->batch_head) {
4541 spin_unlock(&sh->stripe_lock);
4542 return 0;
4543 }
4544
4545 /*
4546 * this stripe could be added to a batch list before we check
4547 * BATCH_READY, skips it
4548 */
4549 if (sh->batch_head != sh) {
4550 spin_unlock(&sh->stripe_lock);
4551 return 1;
4552 }
4553 spin_lock(&sh->batch_lock);
4554 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4555 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4556 spin_unlock(&sh->batch_lock);
4557 spin_unlock(&sh->stripe_lock);
4558
4559 /*
4560 * BATCH_READY is cleared, no new stripes can be added.
4561 * batch_list can be accessed without lock
4562 */
4563 return 0;
4564}
4565
NeilBrown3960ce72015-05-21 12:20:36 +10004566static void break_stripe_batch_list(struct stripe_head *head_sh,
4567 unsigned long handle_flags)
shli@kernel.org72ac7332014-12-15 12:57:03 +11004568{
NeilBrown4e3d62f2015-05-21 11:50:16 +10004569 struct stripe_head *sh, *next;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004570 int i;
NeilBrownfb642b92015-05-21 12:00:47 +10004571 int do_wakeup = 0;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004572
NeilBrownbb270512015-05-08 18:19:40 +10004573 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4574
shli@kernel.org72ac7332014-12-15 12:57:03 +11004575 list_del_init(&sh->batch_list);
4576
Shaohua Lifb3229d2016-03-09 10:08:38 -08004577 WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
NeilBrown1b956f72015-05-21 12:40:26 +10004578 (1 << STRIPE_SYNCING) |
4579 (1 << STRIPE_REPLACED) |
NeilBrown1b956f72015-05-21 12:40:26 +10004580 (1 << STRIPE_DELAYED) |
4581 (1 << STRIPE_BIT_DELAY) |
4582 (1 << STRIPE_FULL_WRITE) |
4583 (1 << STRIPE_BIOFILL_RUN) |
4584 (1 << STRIPE_COMPUTE_RUN) |
4585 (1 << STRIPE_OPS_REQ_PENDING) |
4586 (1 << STRIPE_DISCARD) |
4587 (1 << STRIPE_BATCH_READY) |
4588 (1 << STRIPE_BATCH_ERR) |
Shaohua Lifb3229d2016-03-09 10:08:38 -08004589 (1 << STRIPE_BITMAP_PENDING)),
4590 "stripe state: %lx\n", sh->state);
4591 WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4592 (1 << STRIPE_REPLACED)),
4593 "head stripe state: %lx\n", head_sh->state);
NeilBrown1b956f72015-05-21 12:40:26 +10004594
4595 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
NeilBrown550da242016-03-09 12:58:25 +11004596 (1 << STRIPE_PREREAD_ACTIVE) |
NeilBrown1b956f72015-05-21 12:40:26 +10004597 (1 << STRIPE_DEGRADED)),
4598 head_sh->state & (1 << STRIPE_INSYNC));
4599
shli@kernel.org72ac7332014-12-15 12:57:03 +11004600 sh->check_state = head_sh->check_state;
4601 sh->reconstruct_state = head_sh->reconstruct_state;
NeilBrownfb642b92015-05-21 12:00:47 +10004602 for (i = 0; i < sh->disks; i++) {
4603 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4604 do_wakeup = 1;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004605 sh->dev[i].flags = head_sh->dev[i].flags &
4606 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
NeilBrownfb642b92015-05-21 12:00:47 +10004607 }
shli@kernel.org72ac7332014-12-15 12:57:03 +11004608 spin_lock_irq(&sh->stripe_lock);
4609 sh->batch_head = NULL;
4610 spin_unlock_irq(&sh->stripe_lock);
NeilBrown3960ce72015-05-21 12:20:36 +10004611 if (handle_flags == 0 ||
4612 sh->state & handle_flags)
4613 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07004614 raid5_release_stripe(sh);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004615 }
NeilBrownfb642b92015-05-21 12:00:47 +10004616 spin_lock_irq(&head_sh->stripe_lock);
4617 head_sh->batch_head = NULL;
4618 spin_unlock_irq(&head_sh->stripe_lock);
4619 for (i = 0; i < head_sh->disks; i++)
4620 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4621 do_wakeup = 1;
NeilBrown3960ce72015-05-21 12:20:36 +10004622 if (head_sh->state & handle_flags)
4623 set_bit(STRIPE_HANDLE, &head_sh->state);
NeilBrownfb642b92015-05-21 12:00:47 +10004624
4625 if (do_wakeup)
4626 wake_up(&head_sh->raid_conf->wait_for_overlap);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004627}
4628
NeilBrowncc940152011-07-26 11:35:35 +10004629static void handle_stripe(struct stripe_head *sh)
4630{
4631 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11004632 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10004633 int i;
NeilBrown84789552011-07-27 11:00:36 +10004634 int prexor;
4635 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004636 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10004637
4638 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11004639 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10004640 /* already being handled, ensure it gets handled
4641 * again when current action finishes */
4642 set_bit(STRIPE_HANDLE, &sh->state);
4643 return;
4644 }
4645
shli@kernel.org59fc6302014-12-15 12:57:03 +11004646 if (clear_batch_ready(sh) ) {
4647 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
4648 return;
4649 }
4650
NeilBrown4e3d62f2015-05-21 11:50:16 +10004651 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
NeilBrown3960ce72015-05-21 12:20:36 +10004652 break_stripe_batch_list(sh, 0);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004653
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004654 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
NeilBrownf8dfcff2013-03-12 12:18:06 +11004655 spin_lock(&sh->stripe_lock);
4656 /* Cannot process 'sync' concurrently with 'discard' */
4657 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
4658 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4659 set_bit(STRIPE_SYNCING, &sh->state);
4660 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10004661 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004662 }
4663 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10004664 }
4665 clear_bit(STRIPE_DELAYED, &sh->state);
4666
4667 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4668 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4669 (unsigned long long)sh->sector, sh->state,
4670 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4671 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10004672
NeilBrownacfe7262011-07-27 11:00:36 +10004673 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10004674
Shaohua Lib70abcb2015-08-13 14:31:58 -07004675 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
4676 goto finish;
4677
NeilBrown16d997b2017-03-15 14:05:12 +11004678 if (s.handle_bad_blocks ||
4679 test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004680 set_bit(STRIPE_HANDLE, &sh->state);
4681 goto finish;
4682 }
4683
NeilBrown474af965fe2011-07-27 11:00:36 +10004684 if (unlikely(s.blocked_rdev)) {
4685 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11004686 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10004687 set_bit(STRIPE_HANDLE, &sh->state);
4688 goto finish;
4689 }
4690 /* There is nothing for the blocked_rdev to block */
4691 rdev_dec_pending(s.blocked_rdev, conf->mddev);
4692 s.blocked_rdev = NULL;
4693 }
4694
4695 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4696 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4697 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4698 }
4699
4700 pr_debug("locked=%d uptodate=%d to_read=%d"
4701 " to_write=%d failed=%d failed_num=%d,%d\n",
4702 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4703 s.failed_num[0], s.failed_num[1]);
4704 /* check if the array has lost more than max_degraded devices and,
4705 * if so, some requests might need to be failed.
4706 */
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004707 if (s.failed > conf->max_degraded || s.log_failed) {
NeilBrown9a3f5302011-11-08 16:22:01 +11004708 sh->check_state = 0;
4709 sh->reconstruct_state = 0;
NeilBrown626f2092015-05-22 14:03:10 +10004710 break_stripe_batch_list(sh, 0);
NeilBrown9a3f5302011-11-08 16:22:01 +11004711 if (s.to_read+s.to_write+s.written)
NeilBrownbd83d0a2017-03-15 14:05:12 +11004712 handle_failed_stripe(conf, sh, &s, disks);
NeilBrown9a3e1102011-12-23 10:17:53 +11004713 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11004714 handle_failed_sync(conf, sh, &s);
4715 }
NeilBrown474af965fe2011-07-27 11:00:36 +10004716
NeilBrown84789552011-07-27 11:00:36 +10004717 /* Now we check to see if any write operations have recently
4718 * completed
4719 */
4720 prexor = 0;
4721 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4722 prexor = 1;
4723 if (sh->reconstruct_state == reconstruct_state_drain_result ||
4724 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
4725 sh->reconstruct_state = reconstruct_state_idle;
4726
4727 /* All the 'written' buffers and the parity block are ready to
4728 * be written back to disk
4729 */
Shaohua Li9e4447682012-10-11 13:49:49 +11004730 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
4731 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004732 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11004733 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
4734 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004735 for (i = disks; i--; ) {
4736 struct r5dev *dev = &sh->dev[i];
4737 if (test_bit(R5_LOCKED, &dev->flags) &&
4738 (i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08004739 dev->written || test_bit(R5_InJournal,
4740 &dev->flags))) {
NeilBrown84789552011-07-27 11:00:36 +10004741 pr_debug("Writing block %d\n", i);
4742 set_bit(R5_Wantwrite, &dev->flags);
4743 if (prexor)
4744 continue;
NeilBrown9c4bdf62014-08-13 09:57:07 +10004745 if (s.failed > 1)
4746 continue;
NeilBrown84789552011-07-27 11:00:36 +10004747 if (!test_bit(R5_Insync, &dev->flags) ||
4748 ((i == sh->pd_idx || i == sh->qd_idx) &&
4749 s.failed == 0))
4750 set_bit(STRIPE_INSYNC, &sh->state);
4751 }
4752 }
4753 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4754 s.dec_preread_active = 1;
4755 }
4756
NeilBrownef5b7c62012-11-22 09:13:36 +11004757 /*
4758 * might be able to return some write requests if the parity blocks
4759 * are safe, or on a failed drive
4760 */
4761 pdev = &sh->dev[sh->pd_idx];
4762 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
4763 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
4764 qdev = &sh->dev[sh->qd_idx];
4765 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
4766 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
4767 || conf->level < 6;
4768
4769 if (s.written &&
4770 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
4771 && !test_bit(R5_LOCKED, &pdev->flags)
4772 && (test_bit(R5_UPTODATE, &pdev->flags) ||
4773 test_bit(R5_Discard, &pdev->flags))))) &&
4774 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
4775 && !test_bit(R5_LOCKED, &qdev->flags)
4776 && (test_bit(R5_UPTODATE, &qdev->flags) ||
4777 test_bit(R5_Discard, &qdev->flags))))))
NeilBrownbd83d0a2017-03-15 14:05:12 +11004778 handle_stripe_clean_event(conf, sh, disks);
NeilBrownef5b7c62012-11-22 09:13:36 +11004779
Song Liu1e6d6902016-11-17 15:24:39 -08004780 if (s.just_cached)
NeilBrownbd83d0a2017-03-15 14:05:12 +11004781 r5c_handle_cached_data_endio(conf, sh, disks);
Artur Paszkiewiczff875732017-03-09 09:59:58 +01004782 log_stripe_write_finished(sh);
Song Liu1e6d6902016-11-17 15:24:39 -08004783
NeilBrownef5b7c62012-11-22 09:13:36 +11004784 /* Now we might consider reading some blocks, either to check/generate
4785 * parity, or to satisfy requests
4786 * or to load a block that is being partially written.
4787 */
4788 if (s.to_read || s.non_overwrite
4789 || (conf->level == 6 && s.to_write && s.failed)
4790 || (s.syncing && (s.uptodate + s.compute < disks))
4791 || s.replacing
4792 || s.expanding)
4793 handle_stripe_fill(sh, &s, disks);
4794
Song Liu2ded3702016-11-17 15:24:38 -08004795 /*
4796 * When the stripe finishes full journal write cycle (write to journal
4797 * and raid disk), this is the clean up procedure so it is ready for
4798 * next operation.
4799 */
4800 r5c_finish_stripe_write_out(conf, sh, &s);
4801
4802 /*
4803 * Now to consider new write requests, cache write back and what else,
4804 * if anything should be read. We do not handle new writes when:
NeilBrown84789552011-07-27 11:00:36 +10004805 * 1/ A 'write' operation (copy+xor) is already in flight.
4806 * 2/ A 'check' operation is in flight, as it may clobber the parity
4807 * block.
Song Liu2ded3702016-11-17 15:24:38 -08004808 * 3/ A r5c cache log write is in flight.
NeilBrown84789552011-07-27 11:00:36 +10004809 */
Song Liu2ded3702016-11-17 15:24:38 -08004810
4811 if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
4812 if (!r5c_is_writeback(conf->log)) {
4813 if (s.to_write)
4814 handle_stripe_dirtying(conf, sh, &s, disks);
4815 } else { /* write back cache */
4816 int ret = 0;
4817
4818 /* First, try handle writes in caching phase */
4819 if (s.to_write)
4820 ret = r5c_try_caching_write(conf, sh, &s,
4821 disks);
4822 /*
4823 * If caching phase failed: ret == -EAGAIN
4824 * OR
4825 * stripe under reclaim: !caching && injournal
4826 *
4827 * fall back to handle_stripe_dirtying()
4828 */
4829 if (ret == -EAGAIN ||
4830 /* stripe under reclaim: !caching && injournal */
4831 (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
Song Liud7bd3982016-11-23 22:50:39 -08004832 s.injournal > 0)) {
4833 ret = handle_stripe_dirtying(conf, sh, &s,
4834 disks);
4835 if (ret == -EAGAIN)
4836 goto finish;
4837 }
Song Liu2ded3702016-11-17 15:24:38 -08004838 }
4839 }
NeilBrown84789552011-07-27 11:00:36 +10004840
4841 /* maybe we need to check and possibly fix the parity for this stripe
4842 * Any reads will already have been scheduled, so we just see if enough
4843 * data is available. The parity check is held off while parity
4844 * dependent operations are in flight.
4845 */
4846 if (sh->check_state ||
4847 (s.syncing && s.locked == 0 &&
4848 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
4849 !test_bit(STRIPE_INSYNC, &sh->state))) {
4850 if (conf->level == 6)
4851 handle_parity_checks6(conf, sh, &s, disks);
4852 else
4853 handle_parity_checks5(conf, sh, &s, disks);
4854 }
NeilBrownc5a31002011-07-27 11:00:36 +10004855
NeilBrownf94c0b62013-07-22 12:57:21 +10004856 if ((s.replacing || s.syncing) && s.locked == 0
4857 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
4858 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11004859 /* Write out to replacement devices where possible */
4860 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10004861 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
4862 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11004863 set_bit(R5_WantReplace, &sh->dev[i].flags);
4864 set_bit(R5_LOCKED, &sh->dev[i].flags);
4865 s.locked++;
4866 }
NeilBrownf94c0b62013-07-22 12:57:21 +10004867 if (s.replacing)
4868 set_bit(STRIPE_INSYNC, &sh->state);
4869 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11004870 }
4871 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10004872 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11004873 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10004874 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4875 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004876 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
4877 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10004878 }
4879
4880 /* If the failed drives are just a ReadError, then we might need
4881 * to progress the repair/check process
4882 */
4883 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
4884 for (i = 0; i < s.failed; i++) {
4885 struct r5dev *dev = &sh->dev[s.failed_num[i]];
4886 if (test_bit(R5_ReadError, &dev->flags)
4887 && !test_bit(R5_LOCKED, &dev->flags)
4888 && test_bit(R5_UPTODATE, &dev->flags)
4889 ) {
4890 if (!test_bit(R5_ReWrite, &dev->flags)) {
4891 set_bit(R5_Wantwrite, &dev->flags);
4892 set_bit(R5_ReWrite, &dev->flags);
4893 set_bit(R5_LOCKED, &dev->flags);
4894 s.locked++;
4895 } else {
4896 /* let's read it back */
4897 set_bit(R5_Wantread, &dev->flags);
4898 set_bit(R5_LOCKED, &dev->flags);
4899 s.locked++;
4900 }
4901 }
4902 }
4903
NeilBrown3687c062011-07-27 11:00:36 +10004904 /* Finish reconstruct operations initiated by the expansion process */
4905 if (sh->reconstruct_state == reconstruct_state_result) {
4906 struct stripe_head *sh_src
Shaohua Li6d036f72015-08-13 14:31:57 -07004907 = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrown3687c062011-07-27 11:00:36 +10004908 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
4909 /* sh cannot be written until sh_src has been read.
4910 * so arrange for sh to be delayed a little
4911 */
4912 set_bit(STRIPE_DELAYED, &sh->state);
4913 set_bit(STRIPE_HANDLE, &sh->state);
4914 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
4915 &sh_src->state))
4916 atomic_inc(&conf->preread_active_stripes);
Shaohua Li6d036f72015-08-13 14:31:57 -07004917 raid5_release_stripe(sh_src);
NeilBrown3687c062011-07-27 11:00:36 +10004918 goto finish;
4919 }
4920 if (sh_src)
Shaohua Li6d036f72015-08-13 14:31:57 -07004921 raid5_release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07004922
NeilBrown3687c062011-07-27 11:00:36 +10004923 sh->reconstruct_state = reconstruct_state_idle;
4924 clear_bit(STRIPE_EXPANDING, &sh->state);
4925 for (i = conf->raid_disks; i--; ) {
4926 set_bit(R5_Wantwrite, &sh->dev[i].flags);
4927 set_bit(R5_LOCKED, &sh->dev[i].flags);
4928 s.locked++;
4929 }
4930 }
4931
4932 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
4933 !sh->reconstruct_state) {
4934 /* Need to write out all blocks after computing parity */
4935 sh->disks = conf->raid_disks;
4936 stripe_set_idx(sh->sector, conf, 0, sh);
4937 schedule_reconstruction(sh, &s, 1, 1);
4938 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
4939 clear_bit(STRIPE_EXPAND_READY, &sh->state);
4940 atomic_dec(&conf->reshape_stripes);
4941 wake_up(&conf->wait_for_overlap);
4942 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4943 }
4944
4945 if (s.expanding && s.locked == 0 &&
4946 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
4947 handle_stripe_expansion(conf, sh);
4948
4949finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07004950 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10004951 if (unlikely(s.blocked_rdev)) {
4952 if (conf->mddev->external)
4953 md_wait_for_blocked_rdev(s.blocked_rdev,
4954 conf->mddev);
4955 else
4956 /* Internal metadata will immediately
4957 * be written by raid5d, so we don't
4958 * need to wait here.
4959 */
4960 rdev_dec_pending(s.blocked_rdev,
4961 conf->mddev);
4962 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004963
NeilBrownbc2607f2011-07-28 11:39:22 +10004964 if (s.handle_bad_blocks)
4965 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004966 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10004967 struct r5dev *dev = &sh->dev[i];
4968 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
4969 /* We own a safe reference to the rdev */
4970 rdev = conf->disks[i].rdev;
4971 if (!rdev_set_badblocks(rdev, sh->sector,
4972 STRIPE_SECTORS, 0))
4973 md_error(conf->mddev, rdev);
4974 rdev_dec_pending(rdev, conf->mddev);
4975 }
NeilBrownb84db562011-07-28 11:39:23 +10004976 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
4977 rdev = conf->disks[i].rdev;
4978 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004979 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10004980 rdev_dec_pending(rdev, conf->mddev);
4981 }
NeilBrown977df362011-12-23 10:17:53 +11004982 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
4983 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11004984 if (!rdev)
4985 /* rdev have been moved down */
4986 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11004987 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004988 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11004989 rdev_dec_pending(rdev, conf->mddev);
4990 }
NeilBrownbc2607f2011-07-28 11:39:22 +10004991 }
4992
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004993 if (s.ops_request)
4994 raid_run_ops(sh, s.ops_request);
4995
Dan Williamsf0e43bc2008-06-28 08:31:55 +10004996 ops_run_io(sh, &s);
4997
NeilBrownc5709ef2011-07-26 11:35:20 +10004998 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11004999 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02005000 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11005001 * have actually been submitted.
5002 */
5003 atomic_dec(&conf->preread_active_stripes);
5004 if (atomic_read(&conf->preread_active_stripes) <
5005 IO_THRESHOLD)
5006 md_wakeup_thread(conf->mddev->thread);
5007 }
5008
Dan Williams257a4b42011-11-08 16:22:06 +11005009 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07005010}
5011
NeilBrownd1688a62011-10-11 16:49:52 +11005012static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013{
5014 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
5015 while (!list_empty(&conf->delayed_list)) {
5016 struct list_head *l = conf->delayed_list.next;
5017 struct stripe_head *sh;
5018 sh = list_entry(l, struct stripe_head, lru);
5019 list_del_init(l);
5020 clear_bit(STRIPE_DELAYED, &sh->state);
5021 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5022 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005023 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08005024 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 }
NeilBrown482c0832011-04-18 18:25:42 +10005026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027}
5028
Shaohua Li566c09c2013-11-14 15:16:17 +11005029static void activate_bit_delay(struct r5conf *conf,
5030 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07005031{
5032 /* device_lock is held */
5033 struct list_head head;
5034 list_add(&head, &conf->bitmap_list);
5035 list_del_init(&conf->bitmap_list);
5036 while (!list_empty(&head)) {
5037 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11005038 int hash;
NeilBrown72626682005-09-09 16:23:54 -07005039 list_del_init(&sh->lru);
5040 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11005041 hash = sh->hash_lock_index;
5042 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07005043 }
5044}
5045
NeilBrown5c675f82014-12-15 12:56:56 +11005046static int raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07005047{
NeilBrownd1688a62011-10-11 16:49:52 +11005048 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07005049
5050 /* No difference between reads and writes. Just check
5051 * how busy the stripe_cache is
5052 */
NeilBrown3fa841d2009-09-23 18:10:29 +10005053
NeilBrown54233992015-02-26 12:21:04 +11005054 if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state))
NeilBrownf022b2f2006-10-03 01:15:56 -07005055 return 1;
Song Liua39f7af2016-11-17 15:24:40 -08005056
5057 /* Also checks whether there is pressure on r5cache log space */
5058 if (test_bit(R5C_LOG_TIGHT, &conf->cache_state))
5059 return 1;
NeilBrownf022b2f2006-10-03 01:15:56 -07005060 if (conf->quiesce)
5061 return 1;
Shaohua Li4bda5562013-11-14 15:16:17 +11005062 if (atomic_read(&conf->empty_inactive_list_nr))
NeilBrownf022b2f2006-10-03 01:15:56 -07005063 return 1;
5064
5065 return 0;
5066}
5067
NeilBrownfd01b882011-10-11 16:47:53 +11005068static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005069{
NeilBrown3cb5edf2015-07-15 17:24:17 +10005070 struct r5conf *conf = mddev->private;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005071 sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev);
NeilBrown3cb5edf2015-07-15 17:24:17 +10005072 unsigned int chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005073 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005074
NeilBrown3cb5edf2015-07-15 17:24:17 +10005075 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005076 return chunk_sectors >=
5077 ((sector & (chunk_sectors - 1)) + bio_sectors);
5078}
5079
5080/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005081 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
5082 * later sampled by raid5d.
5083 */
NeilBrownd1688a62011-10-11 16:49:52 +11005084static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005085{
5086 unsigned long flags;
5087
5088 spin_lock_irqsave(&conf->device_lock, flags);
5089
5090 bi->bi_next = conf->retry_read_aligned_list;
5091 conf->retry_read_aligned_list = bi;
5092
5093 spin_unlock_irqrestore(&conf->device_lock, flags);
5094 md_wakeup_thread(conf->mddev->thread);
5095}
5096
NeilBrown0472a422017-03-15 14:05:13 +11005097static struct bio *remove_bio_from_retry(struct r5conf *conf,
5098 unsigned int *offset)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005099{
5100 struct bio *bi;
5101
5102 bi = conf->retry_read_aligned;
5103 if (bi) {
NeilBrown0472a422017-03-15 14:05:13 +11005104 *offset = conf->retry_read_offset;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005105 conf->retry_read_aligned = NULL;
5106 return bi;
5107 }
5108 bi = conf->retry_read_aligned_list;
5109 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08005110 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005111 bi->bi_next = NULL;
NeilBrown0472a422017-03-15 14:05:13 +11005112 *offset = 0;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005113 }
5114
5115 return bi;
5116}
5117
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005118/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005119 * The "raid5_align_endio" should check if the read succeeded and if it
5120 * did, call bio_endio on the original bio (having bio_put the new bio
5121 * first).
5122 * If the read failed..
5123 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005124static void raid5_align_endio(struct bio *bi)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005125{
5126 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11005127 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005128 struct r5conf *conf;
NeilBrown3cb03002011-10-11 16:45:26 +11005129 struct md_rdev *rdev;
Sasha Levin9b81c842015-08-10 19:05:18 -04005130 int error = bi->bi_error;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005131
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005132 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005133
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005134 rdev = (void*)raid_bi->bi_next;
5135 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11005136 mddev = rdev->mddev;
5137 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005138
5139 rdev_dec_pending(rdev, conf->mddev);
5140
Sasha Levin9b81c842015-08-10 19:05:18 -04005141 if (!error) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005142 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
5143 raid_bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005144 bio_endio(raid_bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005145 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10005146 wake_up(&conf->wait_for_quiescent);
NeilBrown6712ecf2007-09-27 12:47:43 +02005147 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005148 }
5149
Dan Williams45b42332007-07-09 11:56:43 -07005150 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005151
5152 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005153}
5154
Ming Lin7ef6b122015-05-06 22:51:24 -07005155static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005156{
NeilBrownd1688a62011-10-11 16:49:52 +11005157 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11005158 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005159 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11005160 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11005161 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005162
5163 if (!in_chunk_boundary(mddev, raid_bio)) {
Ming Lin7ef6b122015-05-06 22:51:24 -07005164 pr_debug("%s: non aligned\n", __func__);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005165 return 0;
5166 }
5167 /*
Ming Leid7a10302017-02-14 23:29:03 +08005168 * use bio_clone_fast to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005169 */
Ming Leid7a10302017-02-14 23:29:03 +08005170 align_bi = bio_clone_fast(raid_bio, GFP_NOIO, mddev->bio_set);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005171 if (!align_bi)
5172 return 0;
5173 /*
5174 * set bi_end_io to a new function, and set bi_private to the
5175 * original bio.
5176 */
5177 align_bi->bi_end_io = raid5_align_endio;
5178 align_bi->bi_private = raid_bio;
5179 /*
5180 * compute position
5181 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005182 align_bi->bi_iter.bi_sector =
5183 raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
5184 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005185
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005186 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005187 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11005188 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
5189 if (!rdev || test_bit(Faulty, &rdev->flags) ||
5190 rdev->recovery_offset < end_sector) {
5191 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
5192 if (rdev &&
5193 (test_bit(Faulty, &rdev->flags) ||
5194 !(test_bit(In_sync, &rdev->flags) ||
5195 rdev->recovery_offset >= end_sector)))
5196 rdev = NULL;
5197 }
Song Liu03b047f2017-01-11 13:39:14 -08005198
5199 if (r5c_big_stripe_cached(conf, align_bi->bi_iter.bi_sector)) {
5200 rcu_read_unlock();
5201 bio_put(align_bi);
5202 return 0;
5203 }
5204
NeilBrown671488c2011-12-23 10:17:52 +11005205 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10005206 sector_t first_bad;
5207 int bad_sectors;
5208
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005209 atomic_inc(&rdev->nr_pending);
5210 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005211 raid_bio->bi_next = (void*)rdev;
5212 align_bi->bi_bdev = rdev->bdev;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06005213 bio_clear_flag(align_bi, BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005214
Kent Overstreet7140aaf2013-09-25 13:37:01 -07005215 if (is_badblock(rdev, align_bi->bi_iter.bi_sector,
Kent Overstreet4f024f32013-10-11 15:44:27 -07005216 bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10005217 &first_bad, &bad_sectors)) {
Neil Brown387bb172007-02-08 14:20:29 -08005218 bio_put(align_bi);
5219 rdev_dec_pending(rdev, mddev);
5220 return 0;
5221 }
5222
majianpeng6c0544e2012-06-12 08:31:10 +08005223 /* No reshape active, so we can trust rdev->data_offset */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005224 align_bi->bi_iter.bi_sector += rdev->data_offset;
majianpeng6c0544e2012-06-12 08:31:10 +08005225
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005226 spin_lock_irq(&conf->device_lock);
Yuanhan Liub1b46482015-05-08 18:19:06 +10005227 wait_event_lock_irq(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005228 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01005229 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005230 atomic_inc(&conf->active_aligned_reads);
5231 spin_unlock_irq(&conf->device_lock);
5232
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005233 if (mddev->gendisk)
5234 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
5235 align_bi, disk_devt(mddev->gendisk),
Kent Overstreet4f024f32013-10-11 15:44:27 -07005236 raid_bio->bi_iter.bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005237 generic_make_request(align_bi);
5238 return 1;
5239 } else {
5240 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005241 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005242 return 0;
5243 }
5244}
5245
Ming Lin7ef6b122015-05-06 22:51:24 -07005246static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
5247{
5248 struct bio *split;
NeilBrowndd7a8f52017-04-05 14:05:51 +10005249 sector_t sector = raid_bio->bi_iter.bi_sector;
5250 unsigned chunk_sects = mddev->chunk_sectors;
5251 unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
Ming Lin7ef6b122015-05-06 22:51:24 -07005252
NeilBrowndd7a8f52017-04-05 14:05:51 +10005253 if (sectors < bio_sectors(raid_bio)) {
5254 struct r5conf *conf = mddev->private;
5255 split = bio_split(raid_bio, sectors, GFP_NOIO, conf->bio_split);
5256 bio_chain(split, raid_bio);
5257 generic_make_request(raid_bio);
5258 raid_bio = split;
5259 }
Ming Lin7ef6b122015-05-06 22:51:24 -07005260
NeilBrowndd7a8f52017-04-05 14:05:51 +10005261 if (!raid5_read_one_chunk(mddev, raid_bio))
5262 return raid_bio;
Ming Lin7ef6b122015-05-06 22:51:24 -07005263
5264 return NULL;
5265}
5266
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005267/* __get_priority_stripe - get the next stripe to process
5268 *
5269 * Full stripe writes are allowed to pass preread active stripes up until
5270 * the bypass_threshold is exceeded. In general the bypass_count
5271 * increments when the handle_list is handled before the hold_list; however, it
5272 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
5273 * stripe with in flight i/o. The bypass_count will be reset when the
5274 * head of the hold_list has changed, i.e. the head was promoted to the
5275 * handle_list.
5276 */
Shaohua Li851c30c2013-08-28 14:30:16 +08005277static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005278{
Shaohua Li535ae4e2017-02-15 19:37:32 -08005279 struct stripe_head *sh, *tmp;
Shaohua Li851c30c2013-08-28 14:30:16 +08005280 struct list_head *handle_list = NULL;
Shaohua Li535ae4e2017-02-15 19:37:32 -08005281 struct r5worker_group *wg;
5282 bool second_try = !r5c_is_writeback(conf->log);
5283 bool try_loprio = test_bit(R5C_LOG_TIGHT, &conf->cache_state);
Shaohua Li851c30c2013-08-28 14:30:16 +08005284
Shaohua Li535ae4e2017-02-15 19:37:32 -08005285again:
5286 wg = NULL;
5287 sh = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005288 if (conf->worker_cnt_per_group == 0) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005289 handle_list = try_loprio ? &conf->loprio_list :
5290 &conf->handle_list;
Shaohua Li851c30c2013-08-28 14:30:16 +08005291 } else if (group != ANY_GROUP) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005292 handle_list = try_loprio ? &conf->worker_groups[group].loprio_list :
5293 &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005294 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08005295 } else {
5296 int i;
5297 for (i = 0; i < conf->group_cnt; i++) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005298 handle_list = try_loprio ? &conf->worker_groups[i].loprio_list :
5299 &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005300 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08005301 if (!list_empty(handle_list))
5302 break;
5303 }
5304 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005305
5306 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5307 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08005308 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005309 list_empty(&conf->hold_list) ? "empty" : "busy",
5310 atomic_read(&conf->pending_full_writes), conf->bypass_count);
5311
Shaohua Li851c30c2013-08-28 14:30:16 +08005312 if (!list_empty(handle_list)) {
5313 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005314
5315 if (list_empty(&conf->hold_list))
5316 conf->bypass_count = 0;
5317 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
5318 if (conf->hold_list.next == conf->last_hold)
5319 conf->bypass_count++;
5320 else {
5321 conf->last_hold = conf->hold_list.next;
5322 conf->bypass_count -= conf->bypass_threshold;
5323 if (conf->bypass_count < 0)
5324 conf->bypass_count = 0;
5325 }
5326 }
5327 } else if (!list_empty(&conf->hold_list) &&
5328 ((conf->bypass_threshold &&
5329 conf->bypass_count > conf->bypass_threshold) ||
5330 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08005331
5332 list_for_each_entry(tmp, &conf->hold_list, lru) {
5333 if (conf->worker_cnt_per_group == 0 ||
5334 group == ANY_GROUP ||
5335 !cpu_online(tmp->cpu) ||
5336 cpu_to_group(tmp->cpu) == group) {
5337 sh = tmp;
5338 break;
5339 }
5340 }
5341
5342 if (sh) {
5343 conf->bypass_count -= conf->bypass_threshold;
5344 if (conf->bypass_count < 0)
5345 conf->bypass_count = 0;
5346 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08005347 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005348 }
5349
Shaohua Li535ae4e2017-02-15 19:37:32 -08005350 if (!sh) {
5351 if (second_try)
5352 return NULL;
5353 second_try = true;
5354 try_loprio = !try_loprio;
5355 goto again;
5356 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005357
Shaohua Libfc90cb2013-08-29 15:40:32 +08005358 if (wg) {
5359 wg->stripes_cnt--;
5360 sh->group = NULL;
5361 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005362 list_del_init(&sh->lru);
Shaohua Lic7a6d352014-04-15 09:12:54 +08005363 BUG_ON(atomic_inc_return(&sh->count) != 1);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005364 return sh;
5365}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005366
Shaohua Li8811b592012-08-02 08:33:00 +10005367struct raid5_plug_cb {
5368 struct blk_plug_cb cb;
5369 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11005370 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10005371};
5372
5373static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
5374{
5375 struct raid5_plug_cb *cb = container_of(
5376 blk_cb, struct raid5_plug_cb, cb);
5377 struct stripe_head *sh;
5378 struct mddev *mddev = cb->cb.data;
5379 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11005380 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11005381 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10005382
5383 if (cb->list.next && !list_empty(&cb->list)) {
5384 spin_lock_irq(&conf->device_lock);
5385 while (!list_empty(&cb->list)) {
5386 sh = list_first_entry(&cb->list, struct stripe_head, lru);
5387 list_del_init(&sh->lru);
5388 /*
5389 * avoid race release_stripe_plug() sees
5390 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5391 * is still in our list
5392 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005393 smp_mb__before_atomic();
Shaohua Li8811b592012-08-02 08:33:00 +10005394 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08005395 /*
5396 * STRIPE_ON_RELEASE_LIST could be set here. In that
5397 * case, the count is always > 1 here
5398 */
Shaohua Li566c09c2013-11-14 15:16:17 +11005399 hash = sh->hash_lock_index;
5400 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11005401 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10005402 }
5403 spin_unlock_irq(&conf->device_lock);
5404 }
Shaohua Li566c09c2013-11-14 15:16:17 +11005405 release_inactive_stripe_list(conf, cb->temp_inactive_list,
5406 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005407 if (mddev->queue)
5408 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10005409 kfree(cb);
5410}
5411
5412static void release_stripe_plug(struct mddev *mddev,
5413 struct stripe_head *sh)
5414{
5415 struct blk_plug_cb *blk_cb = blk_check_plugged(
5416 raid5_unplug, mddev,
5417 sizeof(struct raid5_plug_cb));
5418 struct raid5_plug_cb *cb;
5419
5420 if (!blk_cb) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005421 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005422 return;
5423 }
5424
5425 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
5426
Shaohua Li566c09c2013-11-14 15:16:17 +11005427 if (cb->list.next == NULL) {
5428 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10005429 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11005430 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5431 INIT_LIST_HEAD(cb->temp_inactive_list + i);
5432 }
Shaohua Li8811b592012-08-02 08:33:00 +10005433
5434 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
5435 list_add_tail(&sh->lru, &cb->list);
5436 else
Shaohua Li6d036f72015-08-13 14:31:57 -07005437 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005438}
5439
Shaohua Li620125f2012-10-11 13:49:05 +11005440static void make_discard_request(struct mddev *mddev, struct bio *bi)
5441{
5442 struct r5conf *conf = mddev->private;
5443 sector_t logical_sector, last_sector;
5444 struct stripe_head *sh;
Shaohua Li620125f2012-10-11 13:49:05 +11005445 int stripe_sectors;
5446
5447 if (mddev->reshape_position != MaxSector)
5448 /* Skip discard while reshape is happening */
5449 return;
5450
Kent Overstreet4f024f32013-10-11 15:44:27 -07005451 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
5452 last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9);
Shaohua Li620125f2012-10-11 13:49:05 +11005453
5454 bi->bi_next = NULL;
NeilBrown49728052017-03-15 14:05:12 +11005455 md_write_start(mddev, bi);
Shaohua Li620125f2012-10-11 13:49:05 +11005456
5457 stripe_sectors = conf->chunk_sectors *
5458 (conf->raid_disks - conf->max_degraded);
5459 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5460 stripe_sectors);
5461 sector_div(last_sector, stripe_sectors);
5462
5463 logical_sector *= conf->chunk_sectors;
5464 last_sector *= conf->chunk_sectors;
5465
5466 for (; logical_sector < last_sector;
5467 logical_sector += STRIPE_SECTORS) {
5468 DEFINE_WAIT(w);
5469 int d;
5470 again:
Shaohua Li6d036f72015-08-13 14:31:57 -07005471 sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
Shaohua Li620125f2012-10-11 13:49:05 +11005472 prepare_to_wait(&conf->wait_for_overlap, &w,
5473 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005474 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5475 if (test_bit(STRIPE_SYNCING, &sh->state)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005476 raid5_release_stripe(sh);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005477 schedule();
5478 goto again;
5479 }
5480 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11005481 spin_lock_irq(&sh->stripe_lock);
5482 for (d = 0; d < conf->raid_disks; d++) {
5483 if (d == sh->pd_idx || d == sh->qd_idx)
5484 continue;
5485 if (sh->dev[d].towrite || sh->dev[d].toread) {
5486 set_bit(R5_Overlap, &sh->dev[d].flags);
5487 spin_unlock_irq(&sh->stripe_lock);
Shaohua Li6d036f72015-08-13 14:31:57 -07005488 raid5_release_stripe(sh);
Shaohua Li620125f2012-10-11 13:49:05 +11005489 schedule();
5490 goto again;
5491 }
5492 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11005493 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11005494 finish_wait(&conf->wait_for_overlap, &w);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005495 sh->overwrite_disks = 0;
Shaohua Li620125f2012-10-11 13:49:05 +11005496 for (d = 0; d < conf->raid_disks; d++) {
5497 if (d == sh->pd_idx || d == sh->qd_idx)
5498 continue;
5499 sh->dev[d].towrite = bi;
5500 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
NeilBrown016c76a2017-03-15 14:05:13 +11005501 bio_inc_remaining(bi);
NeilBrown49728052017-03-15 14:05:12 +11005502 md_write_inc(mddev, bi);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005503 sh->overwrite_disks++;
Shaohua Li620125f2012-10-11 13:49:05 +11005504 }
5505 spin_unlock_irq(&sh->stripe_lock);
5506 if (conf->mddev->bitmap) {
5507 for (d = 0;
5508 d < conf->raid_disks - conf->max_degraded;
5509 d++)
5510 bitmap_startwrite(mddev->bitmap,
5511 sh->sector,
5512 STRIPE_SECTORS,
5513 0);
5514 sh->bm_seq = conf->seq_flush + 1;
5515 set_bit(STRIPE_BIT_DELAY, &sh->state);
5516 }
5517
5518 set_bit(STRIPE_HANDLE, &sh->state);
5519 clear_bit(STRIPE_DELAYED, &sh->state);
5520 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5521 atomic_inc(&conf->preread_active_stripes);
5522 release_stripe_plug(mddev, sh);
5523 }
5524
NeilBrown49728052017-03-15 14:05:12 +11005525 md_write_end(mddev);
NeilBrown016c76a2017-03-15 14:05:13 +11005526 bio_endio(bi);
Shaohua Li620125f2012-10-11 13:49:05 +11005527}
5528
Shaohua Li849674e2016-01-20 13:52:20 -08005529static void raid5_make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530{
NeilBrownd1688a62011-10-11 16:49:52 +11005531 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11005532 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 sector_t new_sector;
5534 sector_t logical_sector, last_sector;
5535 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01005536 const int rw = bio_data_dir(bi);
Shaohua Li27c0f682014-04-09 11:25:47 +08005537 DEFINE_WAIT(w);
5538 bool do_prepare;
Song Liu3bddb7f2016-11-18 16:46:50 -08005539 bool do_flush = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
Jens Axboe1eff9d32016-08-05 15:35:16 -06005541 if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
Shaohua Li828cbe92015-09-02 13:49:49 -07005542 int ret = r5l_handle_flush_request(conf->log, bi);
5543
5544 if (ret == 0)
5545 return;
5546 if (ret == -ENODEV) {
5547 md_flush_request(mddev, bi);
5548 return;
5549 }
5550 /* ret == -EAGAIN, fallback */
Song Liu3bddb7f2016-11-18 16:46:50 -08005551 /*
5552 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
5553 * we need to flush journal device
5554 */
5555 do_flush = bi->bi_opf & REQ_PREFLUSH;
NeilBrowne5dcdd82005-09-09 16:23:41 -07005556 }
5557
Eric Mei9ffc8f72015-03-18 23:39:11 -06005558 /*
5559 * If array is degraded, better not do chunk aligned read because
5560 * later we might have to read it again in order to reconstruct
5561 * data on failed drives.
5562 */
5563 if (rw == READ && mddev->degraded == 0 &&
Ming Lin7ef6b122015-05-06 22:51:24 -07005564 mddev->reshape_position == MaxSector) {
5565 bi = chunk_aligned_read(mddev, bi);
5566 if (!bi)
5567 return;
5568 }
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08005569
Mike Christie796a5cf2016-06-05 14:32:07 -05005570 if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
Shaohua Li620125f2012-10-11 13:49:05 +11005571 make_discard_request(mddev, bi);
5572 return;
5573 }
5574
Kent Overstreet4f024f32013-10-11 15:44:27 -07005575 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005576 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 bi->bi_next = NULL;
NeilBrown49728052017-03-15 14:05:12 +11005578 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07005579
Shaohua Li27c0f682014-04-09 11:25:47 +08005580 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005582 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10005583 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08005584
Shaohua Li27c0f682014-04-09 11:25:47 +08005585 do_prepare = false;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005586 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10005587 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11005588 previous = 0;
Shaohua Li27c0f682014-04-09 11:25:47 +08005589 if (do_prepare)
5590 prepare_to_wait(&conf->wait_for_overlap, &w,
5591 TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005592 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11005593 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08005594 * 64bit on a 32bit platform, and so it might be
5595 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02005596 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08005597 * the lock is dropped, so once we get a reference
5598 * to the stripe that we think it is, we will have
5599 * to check again.
5600 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005601 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005602 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005603 ? logical_sector < conf->reshape_progress
5604 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005605 previous = 1;
5606 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10005607 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005608 ? logical_sector < conf->reshape_safe
5609 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08005610 spin_unlock_irq(&conf->device_lock);
5611 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005612 do_prepare = true;
NeilBrownb578d552006-03-27 01:18:12 -08005613 goto retry;
5614 }
5615 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005616 spin_unlock_irq(&conf->device_lock);
5617 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005618
NeilBrown112bf892009-03-31 14:39:38 +11005619 new_sector = raid5_compute_sector(conf, logical_sector,
5620 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11005621 &dd_idx, NULL);
Shaohua Li849674e2016-01-20 13:52:20 -08005622 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10005623 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 (unsigned long long)logical_sector);
5625
Shaohua Li6d036f72015-08-13 14:31:57 -07005626 sh = raid5_get_active_stripe(conf, new_sector, previous,
Jens Axboe1eff9d32016-08-05 15:35:16 -06005627 (bi->bi_opf & REQ_RAHEAD), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11005629 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005630 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08005631 * stripe, so we must do the range check again.
5632 * Expansion could still move past after this
5633 * test, but as we are holding a reference to
5634 * 'sh', we know that if that happens,
5635 * STRIPE_EXPANDING will get set and the expansion
5636 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005637 */
5638 int must_retry = 0;
5639 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005640 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11005641 ? logical_sector >= conf->reshape_progress
5642 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005643 /* mismatch, need to try again */
5644 must_retry = 1;
5645 spin_unlock_irq(&conf->device_lock);
5646 if (must_retry) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005647 raid5_release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07005648 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005649 do_prepare = true;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005650 goto retry;
5651 }
5652 }
NeilBrownc46501b2013-08-27 15:52:13 +10005653 if (read_seqcount_retry(&conf->gen_lock, seq)) {
5654 /* Might have got the wrong stripe_head
5655 * by accident
5656 */
Shaohua Li6d036f72015-08-13 14:31:57 -07005657 raid5_release_stripe(sh);
NeilBrownc46501b2013-08-27 15:52:13 +10005658 goto retry;
5659 }
NeilBrowne62e58a2009-07-01 13:15:35 +10005660
Namhyung Kimffd96e32011-07-18 17:38:51 +10005661 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10005662 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08005663 logical_sector < mddev->suspend_hi) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005664 raid5_release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10005665 /* As the suspend_* range is controlled by
5666 * userspace, we want an interruptible
5667 * wait.
5668 */
5669 flush_signals(current);
5670 prepare_to_wait(&conf->wait_for_overlap,
5671 &w, TASK_INTERRUPTIBLE);
5672 if (logical_sector >= mddev->suspend_lo &&
Shaohua Li27c0f682014-04-09 11:25:47 +08005673 logical_sector < mddev->suspend_hi) {
NeilBrowne62e58a2009-07-01 13:15:35 +10005674 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005675 do_prepare = true;
5676 }
NeilBrowne464eaf2006-03-27 01:18:14 -08005677 goto retry;
5678 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005679
5680 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
shli@kernel.orgda41ba62014-12-15 12:57:03 +11005681 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005682 /* Stripe is busy expanding or
5683 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 * and wait a while
5685 */
NeilBrown482c0832011-04-18 18:25:42 +10005686 md_wakeup_thread(mddev->thread);
Shaohua Li6d036f72015-08-13 14:31:57 -07005687 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005689 do_prepare = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 goto retry;
5691 }
Song Liu3bddb7f2016-11-18 16:46:50 -08005692 if (do_flush) {
5693 set_bit(STRIPE_R5C_PREFLUSH, &sh->state);
5694 /* we only need flush for one stripe */
5695 do_flush = false;
5696 }
5697
NeilBrown6ed30032008-02-06 01:40:00 -08005698 set_bit(STRIPE_HANDLE, &sh->state);
5699 clear_bit(STRIPE_DELAYED, &sh->state);
shli@kernel.org59fc6302014-12-15 12:57:03 +11005700 if ((!sh->batch_head || sh == sh->batch_head) &&
Jens Axboe1eff9d32016-08-05 15:35:16 -06005701 (bi->bi_opf & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11005702 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5703 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10005704 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 } else {
5706 /* cannot get stripe for read-ahead, just give-up */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005707 bi->bi_error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 break;
5709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 }
Shaohua Li27c0f682014-04-09 11:25:47 +08005711 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown7c13edc2011-04-18 18:25:43 +10005712
NeilBrown49728052017-03-15 14:05:12 +11005713 if (rw == WRITE)
5714 md_write_end(mddev);
NeilBrown016c76a2017-03-15 14:05:13 +11005715 bio_endio(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716}
5717
NeilBrownfd01b882011-10-11 16:47:53 +11005718static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11005719
NeilBrownfd01b882011-10-11 16:47:53 +11005720static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721{
NeilBrown52c03292006-06-26 00:27:43 -07005722 /* reshaping is quite different to recovery/resync so it is
5723 * handled quite separately ... here.
5724 *
5725 * On each call to sync_request, we gather one chunk worth of
5726 * destination stripes and flag them as expanding.
5727 * Then we find all the source stripes and request reads.
5728 * As the reads complete, handle_stripe will copy the data
5729 * into the destination stripe and release that stripe.
5730 */
NeilBrownd1688a62011-10-11 16:49:52 +11005731 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005733 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08005734 int raid_disks = conf->previous_raid_disks;
5735 int data_disks = raid_disks - conf->max_degraded;
5736 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07005737 int i;
5738 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11005739 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11005740 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11005741 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11005742 struct list_head stripes;
NeilBrown92140482015-07-06 12:28:45 +10005743 sector_t retn;
NeilBrown52c03292006-06-26 00:27:43 -07005744
NeilBrownfef9c612009-03-31 15:16:46 +11005745 if (sector_nr == 0) {
5746 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10005747 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005748 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
5749 sector_nr = raid5_size(mddev, 0, 0)
5750 - conf->reshape_progress;
NeilBrown6cbd8142015-07-24 13:30:32 +10005751 } else if (mddev->reshape_backwards &&
5752 conf->reshape_progress == MaxSector) {
5753 /* shouldn't happen, but just in case, finish up.*/
5754 sector_nr = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +10005755 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005756 conf->reshape_progress > 0)
5757 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005758 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005759 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11005760 mddev->curr_resync_completed = sector_nr;
5761 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11005762 *skipped = 1;
NeilBrown92140482015-07-06 12:28:45 +10005763 retn = sector_nr;
5764 goto finish;
NeilBrownfef9c612009-03-31 15:16:46 +11005765 }
NeilBrown52c03292006-06-26 00:27:43 -07005766 }
5767
NeilBrown7a661382009-03-31 15:21:40 +11005768 /* We need to process a full chunk at a time.
5769 * If old and new chunk sizes differ, we need to process the
5770 * largest of these
5771 */
NeilBrown3cb5edf2015-07-15 17:24:17 +10005772
5773 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
NeilBrown7a661382009-03-31 15:21:40 +11005774
NeilBrownb5254dd2012-05-21 09:27:01 +10005775 /* We update the metadata at least every 10 seconds, or when
5776 * the data about to be copied would over-write the source of
5777 * the data at the front of the range. i.e. one new_stripe
5778 * along from reshape_progress new_maps to after where
5779 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07005780 */
NeilBrownfef9c612009-03-31 15:16:46 +11005781 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005782 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11005783 readpos = conf->reshape_progress;
5784 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005785 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08005786 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10005787 if (mddev->reshape_backwards) {
NeilBrownc74c0d72015-07-15 17:54:15 +10005788 BUG_ON(writepos < reshape_sectors);
5789 writepos -= reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11005790 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11005791 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11005792 } else {
NeilBrown7a661382009-03-31 15:21:40 +11005793 writepos += reshape_sectors;
NeilBrownc74c0d72015-07-15 17:54:15 +10005794 /* readpos and safepos are worst-case calculations.
5795 * A negative number is overly pessimistic, and causes
5796 * obvious problems for unsigned storage. So clip to 0.
5797 */
NeilBrowned37d832009-05-27 21:39:05 +10005798 readpos -= min_t(sector_t, reshape_sectors, readpos);
5799 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11005800 }
NeilBrown52c03292006-06-26 00:27:43 -07005801
NeilBrownb5254dd2012-05-21 09:27:01 +10005802 /* Having calculated the 'writepos' possibly use it
5803 * to set 'stripe_addr' which is where we will write to.
5804 */
5805 if (mddev->reshape_backwards) {
5806 BUG_ON(conf->reshape_progress == 0);
5807 stripe_addr = writepos;
5808 BUG_ON((mddev->dev_sectors &
5809 ~((sector_t)reshape_sectors - 1))
5810 - reshape_sectors - stripe_addr
5811 != sector_nr);
5812 } else {
5813 BUG_ON(writepos != sector_nr + reshape_sectors);
5814 stripe_addr = sector_nr;
5815 }
5816
NeilBrownc8f517c2009-03-31 15:28:40 +11005817 /* 'writepos' is the most advanced device address we might write.
5818 * 'readpos' is the least advanced device address we might read.
5819 * 'safepos' is the least address recorded in the metadata as having
5820 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10005821 * If there is a min_offset_diff, these are adjusted either by
5822 * increasing the safepos/readpos if diff is negative, or
5823 * increasing writepos if diff is positive.
5824 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11005825 * ensure safety in the face of a crash - that must be done by userspace
5826 * making a backup of the data. So in that case there is no particular
5827 * rush to update metadata.
5828 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5829 * update the metadata to advance 'safepos' to match 'readpos' so that
5830 * we can be safe in the event of a crash.
5831 * So we insist on updating metadata if safepos is behind writepos and
5832 * readpos is beyond writepos.
5833 * In any case, update the metadata every 10 seconds.
5834 * Maybe that number should be configurable, but I'm not sure it is
5835 * worth it.... maybe it could be a multiple of safemode_delay???
5836 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005837 if (conf->min_offset_diff < 0) {
5838 safepos += -conf->min_offset_diff;
5839 readpos += -conf->min_offset_diff;
5840 } else
5841 writepos += conf->min_offset_diff;
5842
NeilBrown2c810cd2012-05-21 09:27:00 +10005843 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11005844 ? (safepos > writepos && readpos < writepos)
5845 : (safepos < writepos && readpos > writepos)) ||
5846 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07005847 /* Cannot proceed until we've updated the superblock... */
5848 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005849 atomic_read(&conf->reshape_stripes)==0
5850 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5851 if (atomic_read(&conf->reshape_stripes) != 0)
5852 return 0;
NeilBrownfef9c612009-03-31 15:16:46 +11005853 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005854 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005855 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08005856 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown52c03292006-06-26 00:27:43 -07005857 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08005858 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11005859 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5860 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5861 return 0;
NeilBrown52c03292006-06-26 00:27:43 -07005862 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005863 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07005864 spin_unlock_irq(&conf->device_lock);
5865 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005866 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07005867 }
5868
NeilBrownab69ae12009-03-31 15:26:47 +11005869 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11005870 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07005871 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10005872 int skipped_disk = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07005873 sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005874 set_bit(STRIPE_EXPANDING, &sh->state);
5875 atomic_inc(&conf->reshape_stripes);
5876 /* If any of this stripe is beyond the end of the old
5877 * array, then we need to zero those blocks
5878 */
5879 for (j=sh->disks; j--;) {
5880 sector_t s;
5881 if (j == sh->pd_idx)
5882 continue;
NeilBrownf4168852007-02-28 20:11:53 -08005883 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11005884 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08005885 continue;
Shaohua Li6d036f72015-08-13 14:31:57 -07005886 s = raid5_compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11005887 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10005888 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07005889 continue;
5890 }
5891 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
5892 set_bit(R5_Expanded, &sh->dev[j].flags);
5893 set_bit(R5_UPTODATE, &sh->dev[j].flags);
5894 }
NeilBrowna9f326e2009-09-23 18:06:41 +10005895 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07005896 set_bit(STRIPE_EXPAND_READY, &sh->state);
5897 set_bit(STRIPE_HANDLE, &sh->state);
5898 }
NeilBrownab69ae12009-03-31 15:26:47 +11005899 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07005900 }
5901 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005902 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11005903 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005904 else
NeilBrown7a661382009-03-31 15:21:40 +11005905 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07005906 spin_unlock_irq(&conf->device_lock);
5907 /* Ok, those stripe are ready. We can start scheduling
5908 * reads on the source stripes.
5909 * The source stripes are determined by mapping the first and last
5910 * block on the destination stripes.
5911 */
NeilBrown52c03292006-06-26 00:27:43 -07005912 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11005913 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11005914 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07005915 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10005916 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10005917 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11005918 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11005919 if (last_sector >= mddev->dev_sectors)
5920 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07005921 while (first_sector <= last_sector) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005922 sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005923 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
5924 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07005925 raid5_release_stripe(sh);
NeilBrown52c03292006-06-26 00:27:43 -07005926 first_sector += STRIPE_SECTORS;
5927 }
NeilBrownab69ae12009-03-31 15:26:47 +11005928 /* Now that the sources are clearly marked, we can release
5929 * the destination stripes
5930 */
5931 while (!list_empty(&stripes)) {
5932 sh = list_entry(stripes.next, struct stripe_head, lru);
5933 list_del_init(&sh->lru);
Shaohua Li6d036f72015-08-13 14:31:57 -07005934 raid5_release_stripe(sh);
NeilBrownab69ae12009-03-31 15:26:47 +11005935 }
NeilBrownc6207272008-02-06 01:39:52 -08005936 /* If this takes us to the resync_max point where we have to pause,
5937 * then we need to write out the superblock.
5938 */
NeilBrown7a661382009-03-31 15:21:40 +11005939 sector_nr += reshape_sectors;
NeilBrown92140482015-07-06 12:28:45 +10005940 retn = reshape_sectors;
5941finish:
NeilBrownc5e19d92015-07-17 12:06:02 +10005942 if (mddev->curr_resync_completed > mddev->resync_max ||
5943 (sector_nr - mddev->curr_resync_completed) * 2
NeilBrownc03f6a12009-04-17 11:06:30 +10005944 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08005945 /* Cannot proceed until we've updated the superblock... */
5946 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005947 atomic_read(&conf->reshape_stripes) == 0
5948 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5949 if (atomic_read(&conf->reshape_stripes) != 0)
5950 goto ret;
NeilBrownfef9c612009-03-31 15:16:46 +11005951 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005952 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005953 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08005954 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownc6207272008-02-06 01:39:52 -08005955 md_wakeup_thread(mddev->thread);
5956 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005957 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)
NeilBrownc91abf52013-11-19 12:02:01 +11005958 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5959 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5960 goto ret;
NeilBrownc6207272008-02-06 01:39:52 -08005961 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005962 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08005963 spin_unlock_irq(&conf->device_lock);
5964 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005965 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08005966 }
NeilBrownc91abf52013-11-19 12:02:01 +11005967ret:
NeilBrown92140482015-07-06 12:28:45 +10005968 return retn;
NeilBrown52c03292006-06-26 00:27:43 -07005969}
5970
Shaohua Li849674e2016-01-20 13:52:20 -08005971static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
5972 int *skipped)
NeilBrown52c03292006-06-26 00:27:43 -07005973{
NeilBrownd1688a62011-10-11 16:49:52 +11005974 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07005975 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11005976 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11005977 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07005978 int still_degraded = 0;
5979 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980
NeilBrown72626682005-09-09 16:23:54 -07005981 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11005983
NeilBrown29269552006-03-27 01:18:10 -08005984 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
5985 end_reshape(conf);
5986 return 0;
5987 }
NeilBrown72626682005-09-09 16:23:54 -07005988
5989 if (mddev->curr_resync < max_sector) /* aborted */
5990 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
5991 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07005992 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07005993 conf->fullsync = 0;
5994 bitmap_close_sync(mddev->bitmap);
5995
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 return 0;
5997 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08005998
NeilBrown64bd6602009-08-03 10:59:58 +10005999 /* Allow raid5_quiesce to complete */
6000 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
6001
NeilBrown52c03292006-06-26 00:27:43 -07006002 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6003 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08006004
NeilBrownc6207272008-02-06 01:39:52 -08006005 /* No need to check resync_max as we never do more than one
6006 * stripe, and as resync_max will always be on a chunk boundary,
6007 * if the check in md_do_sync didn't fire, there is no chance
6008 * of overstepping resync_max here
6009 */
6010
NeilBrown16a53ec2006-06-26 00:27:38 -07006011 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 * to resync, then assert that we are finished, because there is
6013 * nothing we can do.
6014 */
NeilBrown3285edf2006-06-26 00:27:55 -07006015 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07006016 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11006017 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07006018 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 return rv;
6020 }
majianpeng6f608042013-04-24 11:42:41 +10006021 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
6022 !conf->fullsync &&
6023 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
6024 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07006025 /* we can skip this block, and probably more */
6026 sync_blocks /= STRIPE_SECTORS;
6027 *skipped = 1;
6028 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
6029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10006031 bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
NeilBrownb47490c2008-02-06 01:39:50 -08006032
Shaohua Li6d036f72015-08-13 14:31:57 -07006033 sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 if (sh == NULL) {
Shaohua Li6d036f72015-08-13 14:31:57 -07006035 sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07006037 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08006039 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 }
NeilBrown16a53ec2006-06-26 00:27:38 -07006041 /* Need to check if array will still be degraded after recovery/resync
Eric Mei16d9cfa2015-01-06 09:35:02 -08006042 * Note in case of > 1 drive failures it's possible we're rebuilding
6043 * one drive while leaving another faulty drive in array.
NeilBrown16a53ec2006-06-26 00:27:38 -07006044 */
Eric Mei16d9cfa2015-01-06 09:35:02 -08006045 rcu_read_lock();
6046 for (i = 0; i < conf->raid_disks; i++) {
6047 struct md_rdev *rdev = ACCESS_ONCE(conf->disks[i].rdev);
6048
6049 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
NeilBrown16a53ec2006-06-26 00:27:38 -07006050 still_degraded = 1;
Eric Mei16d9cfa2015-01-06 09:35:02 -08006051 }
6052 rcu_read_unlock();
NeilBrown16a53ec2006-06-26 00:27:38 -07006053
6054 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
6055
NeilBrown83206d62011-07-26 11:19:49 +10006056 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Eivind Sarto053f5b62014-06-09 17:06:19 -07006057 set_bit(STRIPE_HANDLE, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058
Shaohua Li6d036f72015-08-13 14:31:57 -07006059 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060
6061 return STRIPE_SECTORS;
6062}
6063
NeilBrown0472a422017-03-15 14:05:13 +11006064static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio,
6065 unsigned int offset)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006066{
6067 /* We may not be able to submit a whole bio at once as there
6068 * may not be enough stripe_heads available.
6069 * We cannot pre-allocate enough stripe_heads as we may need
6070 * more than exist in the cache (if we allow ever large chunks).
6071 * So we do one stripe head at a time and record in
6072 * ->bi_hw_segments how many have been done.
6073 *
6074 * We *know* that this entire raid_bio is in one chunk, so
6075 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
6076 */
6077 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11006078 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006079 sector_t sector, logical_sector, last_sector;
6080 int scnt = 0;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006081 int handled = 0;
6082
Kent Overstreet4f024f32013-10-11 15:44:27 -07006083 logical_sector = raid_bio->bi_iter.bi_sector &
6084 ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11006085 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11006086 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07006087 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006088
6089 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08006090 logical_sector += STRIPE_SECTORS,
6091 sector += STRIPE_SECTORS,
6092 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006093
NeilBrown0472a422017-03-15 14:05:13 +11006094 if (scnt < offset)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006095 /* already done this stripe */
6096 continue;
6097
Shaohua Li6d036f72015-08-13 14:31:57 -07006098 sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006099
6100 if (!sh) {
6101 /* failed to get a stripe - must wait */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006102 conf->retry_read_aligned = raid_bio;
NeilBrown0472a422017-03-15 14:05:13 +11006103 conf->retry_read_offset = scnt;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006104 return handled;
6105 }
6106
shli@kernel.orgda41ba62014-12-15 12:57:03 +11006107 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07006108 raid5_release_stripe(sh);
Neil Brown387bb172007-02-08 14:20:29 -08006109 conf->retry_read_aligned = raid_bio;
NeilBrown0472a422017-03-15 14:05:13 +11006110 conf->retry_read_offset = scnt;
Neil Brown387bb172007-02-08 14:20:29 -08006111 return handled;
6112 }
6113
majianpeng3f9e7c12012-07-31 10:04:21 +10006114 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07006115 handle_stripe(sh);
Shaohua Li6d036f72015-08-13 14:31:57 -07006116 raid5_release_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006117 handled++;
6118 }
NeilBrown016c76a2017-03-15 14:05:13 +11006119
6120 bio_endio(raid_bio);
6121
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006122 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10006123 wake_up(&conf->wait_for_quiescent);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006124 return handled;
6125}
6126
Shaohua Libfc90cb2013-08-29 15:40:32 +08006127static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11006128 struct r5worker *worker,
6129 struct list_head *temp_inactive_list)
Shaohua Li46a06402012-08-02 08:33:15 +10006130{
6131 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11006132 int i, batch_size = 0, hash;
6133 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10006134
6135 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08006136 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10006137 batch[batch_size++] = sh;
6138
Shaohua Li566c09c2013-11-14 15:16:17 +11006139 if (batch_size == 0) {
6140 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6141 if (!list_empty(temp_inactive_list + i))
6142 break;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006143 if (i == NR_STRIPE_HASH_LOCKS) {
6144 spin_unlock_irq(&conf->device_lock);
6145 r5l_flush_stripe_to_raid(conf->log);
6146 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006147 return batch_size;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006148 }
Shaohua Li566c09c2013-11-14 15:16:17 +11006149 release_inactive = true;
6150 }
Shaohua Li46a06402012-08-02 08:33:15 +10006151 spin_unlock_irq(&conf->device_lock);
6152
Shaohua Li566c09c2013-11-14 15:16:17 +11006153 release_inactive_stripe_list(conf, temp_inactive_list,
6154 NR_STRIPE_HASH_LOCKS);
6155
Shaohua Lia8c34f92015-09-02 13:49:46 -07006156 r5l_flush_stripe_to_raid(conf->log);
Shaohua Li566c09c2013-11-14 15:16:17 +11006157 if (release_inactive) {
6158 spin_lock_irq(&conf->device_lock);
6159 return 0;
6160 }
6161
Shaohua Li46a06402012-08-02 08:33:15 +10006162 for (i = 0; i < batch_size; i++)
6163 handle_stripe(batch[i]);
Artur Paszkiewiczff875732017-03-09 09:59:58 +01006164 log_write_stripe_run(conf);
Shaohua Li46a06402012-08-02 08:33:15 +10006165
6166 cond_resched();
6167
6168 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006169 for (i = 0; i < batch_size; i++) {
6170 hash = batch[i]->hash_lock_index;
6171 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
6172 }
Shaohua Li46a06402012-08-02 08:33:15 +10006173 return batch_size;
6174}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006175
Shaohua Li851c30c2013-08-28 14:30:16 +08006176static void raid5_do_work(struct work_struct *work)
6177{
6178 struct r5worker *worker = container_of(work, struct r5worker, work);
6179 struct r5worker_group *group = worker->group;
6180 struct r5conf *conf = group->conf;
NeilBrown16d997b2017-03-15 14:05:12 +11006181 struct mddev *mddev = conf->mddev;
Shaohua Li851c30c2013-08-28 14:30:16 +08006182 int group_id = group - conf->worker_groups;
6183 int handled;
6184 struct blk_plug plug;
6185
6186 pr_debug("+++ raid5worker active\n");
6187
6188 blk_start_plug(&plug);
6189 handled = 0;
6190 spin_lock_irq(&conf->device_lock);
6191 while (1) {
6192 int batch_size, released;
6193
Shaohua Li566c09c2013-11-14 15:16:17 +11006194 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006195
Shaohua Li566c09c2013-11-14 15:16:17 +11006196 batch_size = handle_active_stripes(conf, group_id, worker,
6197 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08006198 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08006199 if (!batch_size && !released)
6200 break;
6201 handled += batch_size;
NeilBrown16d997b2017-03-15 14:05:12 +11006202 wait_event_lock_irq(mddev->sb_wait,
6203 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags),
6204 conf->device_lock);
Shaohua Li851c30c2013-08-28 14:30:16 +08006205 }
6206 pr_debug("%d stripes handled\n", handled);
6207
6208 spin_unlock_irq(&conf->device_lock);
6209 blk_finish_plug(&plug);
6210
6211 pr_debug("--- raid5worker inactive\n");
6212}
6213
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214/*
6215 * This is our raid5 kernel thread.
6216 *
6217 * We scan the hash table for stripes which can be handled now.
6218 * During the scan, completed stripes are saved for us by the interrupt
6219 * handler, so that they will not have to wait for our next wakeup.
6220 */
Shaohua Li4ed87312012-10-11 13:34:00 +11006221static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222{
Shaohua Li4ed87312012-10-11 13:34:00 +11006223 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11006224 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006226 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227
Dan Williams45b42332007-07-09 11:56:43 -07006228 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229
6230 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006232 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 handled = 0;
6234 spin_lock_irq(&conf->device_lock);
6235 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006236 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08006237 int batch_size, released;
NeilBrown0472a422017-03-15 14:05:13 +11006238 unsigned int offset;
Shaohua Li773ca822013-08-27 17:50:39 +08006239
Shaohua Li566c09c2013-11-14 15:16:17 +11006240 released = release_stripe_list(conf, conf->temp_inactive_list);
NeilBrownedbe83a2015-02-26 12:47:56 +11006241 if (released)
6242 clear_bit(R5_DID_ALLOC, &conf->cache_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
NeilBrown0021b7b2012-07-31 09:08:14 +02006244 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10006245 !list_empty(&conf->bitmap_list)) {
6246 /* Now is a good time to flush some bitmap updates */
6247 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08006248 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07006249 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08006250 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10006251 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11006252 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07006253 }
NeilBrown0021b7b2012-07-31 09:08:14 +02006254 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07006255
NeilBrown0472a422017-03-15 14:05:13 +11006256 while ((bio = remove_bio_from_retry(conf, &offset))) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006257 int ok;
6258 spin_unlock_irq(&conf->device_lock);
NeilBrown0472a422017-03-15 14:05:13 +11006259 ok = retry_aligned_read(conf, bio, offset);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006260 spin_lock_irq(&conf->device_lock);
6261 if (!ok)
6262 break;
6263 handled++;
6264 }
6265
Shaohua Li566c09c2013-11-14 15:16:17 +11006266 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
6267 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08006268 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 break;
Shaohua Li46a06402012-08-02 08:33:15 +10006270 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271
Shaohua Li29530792016-12-08 15:48:19 -08006272 if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) {
Shaohua Li46a06402012-08-02 08:33:15 +10006273 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10006274 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10006275 spin_lock_irq(&conf->device_lock);
6276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 }
Dan Williams45b42332007-07-09 11:56:43 -07006278 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279
6280 spin_unlock_irq(&conf->device_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006281 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
6282 mutex_trylock(&conf->cache_size_mutex)) {
NeilBrownedbe83a2015-02-26 12:47:56 +11006283 grow_one_stripe(conf, __GFP_NOWARN);
6284 /* Set flag even if allocation failed. This helps
6285 * slow down allocation requests when mem is short
6286 */
6287 set_bit(R5_DID_ALLOC, &conf->cache_state);
NeilBrown2d5b5692015-07-06 12:49:23 +10006288 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290
Shaohua Li765d7042017-01-04 09:33:23 -08006291 flush_deferred_bios(conf);
6292
Shaohua Li0576b1c2015-08-13 14:32:00 -07006293 r5l_flush_stripe_to_raid(conf->log);
6294
Dan Williamsc9f21aa2008-07-23 12:05:51 -07006295 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006296 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297
Dan Williams45b42332007-07-09 11:56:43 -07006298 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299}
6300
NeilBrown3f294f42005-11-08 21:39:25 -08006301static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006302raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006303{
NeilBrown7b1485b2014-12-15 12:56:59 +11006304 struct r5conf *conf;
6305 int ret = 0;
6306 spin_lock(&mddev->lock);
6307 conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006308 if (conf)
NeilBrownedbe83a2015-02-26 12:47:56 +11006309 ret = sprintf(page, "%d\n", conf->min_nr_stripes);
NeilBrown7b1485b2014-12-15 12:56:59 +11006310 spin_unlock(&mddev->lock);
6311 return ret;
NeilBrown3f294f42005-11-08 21:39:25 -08006312}
6313
NeilBrownc41d4ac2010-06-01 19:37:24 +10006314int
NeilBrownfd01b882011-10-11 16:47:53 +11006315raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10006316{
NeilBrownd1688a62011-10-11 16:49:52 +11006317 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10006318 int err;
6319
6320 if (size <= 16 || size > 32768)
6321 return -EINVAL;
NeilBrown486f0642015-02-25 12:10:35 +11006322
NeilBrownedbe83a2015-02-26 12:47:56 +11006323 conf->min_nr_stripes = size;
NeilBrown2d5b5692015-07-06 12:49:23 +10006324 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006325 while (size < conf->max_nr_stripes &&
6326 drop_one_stripe(conf))
6327 ;
NeilBrown2d5b5692015-07-06 12:49:23 +10006328 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006329
NeilBrownedbe83a2015-02-26 12:47:56 +11006330
NeilBrownc41d4ac2010-06-01 19:37:24 +10006331 err = md_allow_write(mddev);
6332 if (err)
6333 return err;
NeilBrown486f0642015-02-25 12:10:35 +11006334
NeilBrown2d5b5692015-07-06 12:49:23 +10006335 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006336 while (size > conf->max_nr_stripes)
6337 if (!grow_one_stripe(conf, GFP_KERNEL))
6338 break;
NeilBrown2d5b5692015-07-06 12:49:23 +10006339 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006340
NeilBrownc41d4ac2010-06-01 19:37:24 +10006341 return 0;
6342}
6343EXPORT_SYMBOL(raid5_set_cache_size);
6344
NeilBrown3f294f42005-11-08 21:39:25 -08006345static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006346raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08006347{
NeilBrown67918752014-12-15 12:57:01 +11006348 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006349 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07006350 int err;
6351
NeilBrown3f294f42005-11-08 21:39:25 -08006352 if (len >= PAGE_SIZE)
6353 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006354 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08006355 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006356 err = mddev_lock(mddev);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006357 if (err)
6358 return err;
NeilBrown67918752014-12-15 12:57:01 +11006359 conf = mddev->private;
6360 if (!conf)
6361 err = -ENODEV;
6362 else
6363 err = raid5_set_cache_size(mddev, new);
6364 mddev_unlock(mddev);
6365
6366 return err ?: len;
NeilBrown3f294f42005-11-08 21:39:25 -08006367}
NeilBrown007583c2005-11-08 21:39:30 -08006368
NeilBrown96de1e62005-11-08 21:39:39 -08006369static struct md_sysfs_entry
6370raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
6371 raid5_show_stripe_cache_size,
6372 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08006373
6374static ssize_t
Markus Stockhausend06f1912014-12-15 12:57:05 +11006375raid5_show_rmw_level(struct mddev *mddev, char *page)
6376{
6377 struct r5conf *conf = mddev->private;
6378 if (conf)
6379 return sprintf(page, "%d\n", conf->rmw_level);
6380 else
6381 return 0;
6382}
6383
6384static ssize_t
6385raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
6386{
6387 struct r5conf *conf = mddev->private;
6388 unsigned long new;
6389
6390 if (!conf)
6391 return -ENODEV;
6392
6393 if (len >= PAGE_SIZE)
6394 return -EINVAL;
6395
6396 if (kstrtoul(page, 10, &new))
6397 return -EINVAL;
6398
6399 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
6400 return -EINVAL;
6401
6402 if (new != PARITY_DISABLE_RMW &&
6403 new != PARITY_ENABLE_RMW &&
6404 new != PARITY_PREFER_RMW)
6405 return -EINVAL;
6406
6407 conf->rmw_level = new;
6408 return len;
6409}
6410
6411static struct md_sysfs_entry
6412raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
6413 raid5_show_rmw_level,
6414 raid5_store_rmw_level);
6415
6416
6417static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006418raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006419{
NeilBrown7b1485b2014-12-15 12:56:59 +11006420 struct r5conf *conf;
6421 int ret = 0;
6422 spin_lock(&mddev->lock);
6423 conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006424 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006425 ret = sprintf(page, "%d\n", conf->bypass_threshold);
6426 spin_unlock(&mddev->lock);
6427 return ret;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006428}
6429
6430static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006431raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006432{
NeilBrown67918752014-12-15 12:57:01 +11006433 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006434 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006435 int err;
6436
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006437 if (len >= PAGE_SIZE)
6438 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006439 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006440 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006441
6442 err = mddev_lock(mddev);
6443 if (err)
6444 return err;
6445 conf = mddev->private;
6446 if (!conf)
6447 err = -ENODEV;
NeilBrownedbe83a2015-02-26 12:47:56 +11006448 else if (new > conf->min_nr_stripes)
NeilBrown67918752014-12-15 12:57:01 +11006449 err = -EINVAL;
6450 else
6451 conf->bypass_threshold = new;
6452 mddev_unlock(mddev);
6453 return err ?: len;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006454}
6455
6456static struct md_sysfs_entry
6457raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
6458 S_IRUGO | S_IWUSR,
6459 raid5_show_preread_threshold,
6460 raid5_store_preread_threshold);
6461
6462static ssize_t
Shaohua Lid592a992014-05-21 17:57:44 +08006463raid5_show_skip_copy(struct mddev *mddev, char *page)
6464{
NeilBrown7b1485b2014-12-15 12:56:59 +11006465 struct r5conf *conf;
6466 int ret = 0;
6467 spin_lock(&mddev->lock);
6468 conf = mddev->private;
Shaohua Lid592a992014-05-21 17:57:44 +08006469 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006470 ret = sprintf(page, "%d\n", conf->skip_copy);
6471 spin_unlock(&mddev->lock);
6472 return ret;
Shaohua Lid592a992014-05-21 17:57:44 +08006473}
6474
6475static ssize_t
6476raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
6477{
NeilBrown67918752014-12-15 12:57:01 +11006478 struct r5conf *conf;
Shaohua Lid592a992014-05-21 17:57:44 +08006479 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006480 int err;
6481
Shaohua Lid592a992014-05-21 17:57:44 +08006482 if (len >= PAGE_SIZE)
6483 return -EINVAL;
Shaohua Lid592a992014-05-21 17:57:44 +08006484 if (kstrtoul(page, 10, &new))
6485 return -EINVAL;
6486 new = !!new;
Shaohua Lid592a992014-05-21 17:57:44 +08006487
NeilBrown67918752014-12-15 12:57:01 +11006488 err = mddev_lock(mddev);
6489 if (err)
6490 return err;
6491 conf = mddev->private;
6492 if (!conf)
6493 err = -ENODEV;
6494 else if (new != conf->skip_copy) {
6495 mddev_suspend(mddev);
6496 conf->skip_copy = new;
6497 if (new)
Jan Karadc3b17c2017-02-02 15:56:50 +01006498 mddev->queue->backing_dev_info->capabilities |=
NeilBrown67918752014-12-15 12:57:01 +11006499 BDI_CAP_STABLE_WRITES;
6500 else
Jan Karadc3b17c2017-02-02 15:56:50 +01006501 mddev->queue->backing_dev_info->capabilities &=
NeilBrown67918752014-12-15 12:57:01 +11006502 ~BDI_CAP_STABLE_WRITES;
6503 mddev_resume(mddev);
6504 }
6505 mddev_unlock(mddev);
6506 return err ?: len;
Shaohua Lid592a992014-05-21 17:57:44 +08006507}
6508
6509static struct md_sysfs_entry
6510raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
6511 raid5_show_skip_copy,
6512 raid5_store_skip_copy);
6513
Shaohua Lid592a992014-05-21 17:57:44 +08006514static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006515stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006516{
NeilBrownd1688a62011-10-11 16:49:52 +11006517 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006518 if (conf)
6519 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
6520 else
6521 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08006522}
6523
NeilBrown96de1e62005-11-08 21:39:39 -08006524static struct md_sysfs_entry
6525raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08006526
Shaohua Lib7214202013-08-27 17:50:42 +08006527static ssize_t
6528raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
6529{
NeilBrown7b1485b2014-12-15 12:56:59 +11006530 struct r5conf *conf;
6531 int ret = 0;
6532 spin_lock(&mddev->lock);
6533 conf = mddev->private;
Shaohua Lib7214202013-08-27 17:50:42 +08006534 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006535 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
6536 spin_unlock(&mddev->lock);
6537 return ret;
Shaohua Lib7214202013-08-27 17:50:42 +08006538}
6539
majianpeng60aaf932013-11-14 15:16:20 +11006540static int alloc_thread_groups(struct r5conf *conf, int cnt,
6541 int *group_cnt,
6542 int *worker_cnt_per_group,
6543 struct r5worker_group **worker_groups);
Shaohua Lib7214202013-08-27 17:50:42 +08006544static ssize_t
6545raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
6546{
NeilBrown67918752014-12-15 12:57:01 +11006547 struct r5conf *conf;
Shaohua Lib7214202013-08-27 17:50:42 +08006548 unsigned long new;
6549 int err;
majianpeng60aaf932013-11-14 15:16:20 +11006550 struct r5worker_group *new_groups, *old_groups;
6551 int group_cnt, worker_cnt_per_group;
Shaohua Lib7214202013-08-27 17:50:42 +08006552
6553 if (len >= PAGE_SIZE)
6554 return -EINVAL;
Shaohua Lib7214202013-08-27 17:50:42 +08006555 if (kstrtoul(page, 10, &new))
6556 return -EINVAL;
6557
NeilBrown67918752014-12-15 12:57:01 +11006558 err = mddev_lock(mddev);
Shaohua Lib7214202013-08-27 17:50:42 +08006559 if (err)
6560 return err;
NeilBrown67918752014-12-15 12:57:01 +11006561 conf = mddev->private;
6562 if (!conf)
6563 err = -ENODEV;
6564 else if (new != conf->worker_cnt_per_group) {
6565 mddev_suspend(mddev);
6566
6567 old_groups = conf->worker_groups;
6568 if (old_groups)
6569 flush_workqueue(raid5_wq);
6570
6571 err = alloc_thread_groups(conf, new,
6572 &group_cnt, &worker_cnt_per_group,
6573 &new_groups);
6574 if (!err) {
6575 spin_lock_irq(&conf->device_lock);
6576 conf->group_cnt = group_cnt;
6577 conf->worker_cnt_per_group = worker_cnt_per_group;
6578 conf->worker_groups = new_groups;
6579 spin_unlock_irq(&conf->device_lock);
6580
6581 if (old_groups)
6582 kfree(old_groups[0].workers);
6583 kfree(old_groups);
6584 }
6585 mddev_resume(mddev);
6586 }
6587 mddev_unlock(mddev);
6588
6589 return err ?: len;
Shaohua Lib7214202013-08-27 17:50:42 +08006590}
6591
6592static struct md_sysfs_entry
6593raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
6594 raid5_show_group_thread_cnt,
6595 raid5_store_group_thread_cnt);
6596
NeilBrown007583c2005-11-08 21:39:30 -08006597static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08006598 &raid5_stripecache_size.attr,
6599 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006600 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08006601 &raid5_group_thread_cnt.attr,
Shaohua Lid592a992014-05-21 17:57:44 +08006602 &raid5_skip_copy.attr,
Markus Stockhausend06f1912014-12-15 12:57:05 +11006603 &raid5_rmw_level.attr,
Song Liu2c7da142016-11-17 15:24:41 -08006604 &r5c_journal_mode.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08006605 NULL,
6606};
NeilBrown007583c2005-11-08 21:39:30 -08006607static struct attribute_group raid5_attrs_group = {
6608 .name = NULL,
6609 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08006610};
6611
majianpeng60aaf932013-11-14 15:16:20 +11006612static int alloc_thread_groups(struct r5conf *conf, int cnt,
6613 int *group_cnt,
6614 int *worker_cnt_per_group,
6615 struct r5worker_group **worker_groups)
Shaohua Li851c30c2013-08-28 14:30:16 +08006616{
Shaohua Li566c09c2013-11-14 15:16:17 +11006617 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08006618 ssize_t size;
6619 struct r5worker *workers;
6620
majianpeng60aaf932013-11-14 15:16:20 +11006621 *worker_cnt_per_group = cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +08006622 if (cnt == 0) {
majianpeng60aaf932013-11-14 15:16:20 +11006623 *group_cnt = 0;
6624 *worker_groups = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08006625 return 0;
6626 }
majianpeng60aaf932013-11-14 15:16:20 +11006627 *group_cnt = num_possible_nodes();
Shaohua Li851c30c2013-08-28 14:30:16 +08006628 size = sizeof(struct r5worker) * cnt;
majianpeng60aaf932013-11-14 15:16:20 +11006629 workers = kzalloc(size * *group_cnt, GFP_NOIO);
6630 *worker_groups = kzalloc(sizeof(struct r5worker_group) *
6631 *group_cnt, GFP_NOIO);
6632 if (!*worker_groups || !workers) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006633 kfree(workers);
majianpeng60aaf932013-11-14 15:16:20 +11006634 kfree(*worker_groups);
Shaohua Li851c30c2013-08-28 14:30:16 +08006635 return -ENOMEM;
6636 }
6637
majianpeng60aaf932013-11-14 15:16:20 +11006638 for (i = 0; i < *group_cnt; i++) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006639 struct r5worker_group *group;
6640
NeilBrown0c775d52013-11-25 11:12:43 +11006641 group = &(*worker_groups)[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08006642 INIT_LIST_HEAD(&group->handle_list);
Shaohua Li535ae4e2017-02-15 19:37:32 -08006643 INIT_LIST_HEAD(&group->loprio_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006644 group->conf = conf;
6645 group->workers = workers + i * cnt;
6646
6647 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11006648 struct r5worker *worker = group->workers + j;
6649 worker->group = group;
6650 INIT_WORK(&worker->work, raid5_do_work);
6651
6652 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
6653 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08006654 }
6655 }
6656
6657 return 0;
6658}
6659
6660static void free_thread_groups(struct r5conf *conf)
6661{
6662 if (conf->worker_groups)
6663 kfree(conf->worker_groups[0].workers);
6664 kfree(conf->worker_groups);
6665 conf->worker_groups = NULL;
6666}
6667
Dan Williams80c3a6c2009-03-17 18:10:40 -07006668static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11006669raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07006670{
NeilBrownd1688a62011-10-11 16:49:52 +11006671 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006672
6673 if (!sectors)
6674 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006675 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11006676 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11006677 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006678
NeilBrown3cb5edf2015-07-15 17:24:17 +10006679 sectors &= ~((sector_t)conf->chunk_sectors - 1);
6680 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006681 return sectors * (raid_disks - conf->max_degraded);
6682}
6683
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306684static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6685{
6686 safe_put_page(percpu->spare_page);
shli@kernel.org46d5b782014-12-15 12:57:02 +11006687 if (percpu->scribble)
6688 flex_array_free(percpu->scribble);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306689 percpu->spare_page = NULL;
6690 percpu->scribble = NULL;
6691}
6692
6693static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6694{
6695 if (conf->level == 6 && !percpu->spare_page)
6696 percpu->spare_page = alloc_page(GFP_KERNEL);
6697 if (!percpu->scribble)
shli@kernel.org46d5b782014-12-15 12:57:02 +11006698 percpu->scribble = scribble_alloc(max(conf->raid_disks,
NeilBrown738a2732015-05-08 18:19:39 +10006699 conf->previous_raid_disks),
6700 max(conf->chunk_sectors,
6701 conf->prev_chunk_sectors)
6702 / STRIPE_SECTORS,
6703 GFP_KERNEL);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306704
6705 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
6706 free_scratch_buffer(conf, percpu);
6707 return -ENOMEM;
6708 }
6709
6710 return 0;
6711}
6712
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006713static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
6714{
6715 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
6716
6717 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
6718 return 0;
6719}
6720
NeilBrownd1688a62011-10-11 16:49:52 +11006721static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006722{
Dan Williams36d1c642009-07-14 11:48:22 -07006723 if (!conf->percpu)
6724 return;
6725
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006726 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Dan Williams36d1c642009-07-14 11:48:22 -07006727 free_percpu(conf->percpu);
6728}
6729
NeilBrownd1688a62011-10-11 16:49:52 +11006730static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10006731{
Song Liud7bd3982016-11-23 22:50:39 -08006732 int i;
6733
Artur Paszkiewiczff875732017-03-09 09:59:58 +01006734 log_exit(conf);
6735
Shaohua Li30c89462016-09-21 09:07:13 -07006736 if (conf->shrinker.nr_deferred)
NeilBrownedbe83a2015-02-26 12:47:56 +11006737 unregister_shrinker(&conf->shrinker);
Shaohua Li5c7e81c2015-08-13 14:32:04 -07006738
Shaohua Li851c30c2013-08-28 14:30:16 +08006739 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10006740 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07006741 raid5_free_percpu(conf);
Song Liud7bd3982016-11-23 22:50:39 -08006742 for (i = 0; i < conf->pool_size; i++)
6743 if (conf->disks[i].extra_page)
6744 put_page(conf->disks[i].extra_page);
Dan Williams95fc17a2009-07-31 12:39:15 +10006745 kfree(conf->disks);
NeilBrowndd7a8f52017-04-05 14:05:51 +10006746 if (conf->bio_split)
6747 bioset_free(conf->bio_split);
Dan Williams95fc17a2009-07-31 12:39:15 +10006748 kfree(conf->stripe_hashtbl);
Shaohua Liaaf9f122017-03-03 22:06:12 -08006749 kfree(conf->pending_data);
Dan Williams95fc17a2009-07-31 12:39:15 +10006750 kfree(conf);
6751}
6752
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006753static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
Dan Williams36d1c642009-07-14 11:48:22 -07006754{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006755 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
Dan Williams36d1c642009-07-14 11:48:22 -07006756 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
6757
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006758 if (alloc_scratch_buffer(conf, percpu)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006759 pr_warn("%s: failed memory allocation for cpu%u\n",
6760 __func__, cpu);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006761 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006762 }
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006763 return 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006764}
Dan Williams36d1c642009-07-14 11:48:22 -07006765
NeilBrownd1688a62011-10-11 16:49:52 +11006766static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006767{
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306768 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006769
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306770 conf->percpu = alloc_percpu(struct raid5_percpu);
6771 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07006772 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006773
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006774 err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Shaohua Li27a353c2016-02-24 17:38:28 -08006775 if (!err) {
6776 conf->scribble_disks = max(conf->raid_disks,
6777 conf->previous_raid_disks);
6778 conf->scribble_sectors = max(conf->chunk_sectors,
6779 conf->prev_chunk_sectors);
6780 }
Dan Williams36d1c642009-07-14 11:48:22 -07006781 return err;
6782}
6783
NeilBrownedbe83a2015-02-26 12:47:56 +11006784static unsigned long raid5_cache_scan(struct shrinker *shrink,
6785 struct shrink_control *sc)
6786{
6787 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
NeilBrown2d5b5692015-07-06 12:49:23 +10006788 unsigned long ret = SHRINK_STOP;
6789
6790 if (mutex_trylock(&conf->cache_size_mutex)) {
6791 ret= 0;
NeilBrown49895bc2015-08-03 17:09:57 +10006792 while (ret < sc->nr_to_scan &&
6793 conf->max_nr_stripes > conf->min_nr_stripes) {
NeilBrown2d5b5692015-07-06 12:49:23 +10006794 if (drop_one_stripe(conf) == 0) {
6795 ret = SHRINK_STOP;
6796 break;
6797 }
6798 ret++;
6799 }
6800 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006801 }
6802 return ret;
6803}
6804
6805static unsigned long raid5_cache_count(struct shrinker *shrink,
6806 struct shrink_control *sc)
6807{
6808 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
6809
6810 if (conf->max_nr_stripes < conf->min_nr_stripes)
6811 /* unlikely, but not impossible */
6812 return 0;
6813 return conf->max_nr_stripes - conf->min_nr_stripes;
6814}
6815
NeilBrownd1688a62011-10-11 16:49:52 +11006816static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817{
NeilBrownd1688a62011-10-11 16:49:52 +11006818 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006819 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11006820 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10006822 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11006823 int i;
majianpeng60aaf932013-11-14 15:16:20 +11006824 int group_cnt, worker_cnt_per_group;
6825 struct r5worker_group *new_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826
NeilBrown91adb562009-03-31 14:39:39 +11006827 if (mddev->new_level != 5
6828 && mddev->new_level != 4
6829 && mddev->new_level != 6) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006830 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
6831 mdname(mddev), mddev->new_level);
NeilBrown91adb562009-03-31 14:39:39 +11006832 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 }
NeilBrown91adb562009-03-31 14:39:39 +11006834 if ((mddev->new_level == 5
6835 && !algorithm_valid_raid5(mddev->new_layout)) ||
6836 (mddev->new_level == 6
6837 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006838 pr_warn("md/raid:%s: layout %d not supported\n",
6839 mdname(mddev), mddev->new_layout);
NeilBrown91adb562009-03-31 14:39:39 +11006840 return ERR_PTR(-EIO);
6841 }
6842 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006843 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
6844 mdname(mddev), mddev->raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006845 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11006846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847
Andre Noll664e7c42009-06-18 08:45:27 +10006848 if (!mddev->new_chunk_sectors ||
6849 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
6850 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006851 pr_warn("md/raid:%s: invalid chunk size %d\n",
6852 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11006853 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11006854 }
6855
NeilBrownd1688a62011-10-11 16:49:52 +11006856 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11006857 if (conf == NULL)
6858 goto abort;
Shaohua Liaaf9f122017-03-03 22:06:12 -08006859 INIT_LIST_HEAD(&conf->free_list);
6860 INIT_LIST_HEAD(&conf->pending_list);
6861 conf->pending_data = kzalloc(sizeof(struct r5pending_data) *
6862 PENDING_IO_MAX, GFP_KERNEL);
6863 if (!conf->pending_data)
6864 goto abort;
6865 for (i = 0; i < PENDING_IO_MAX; i++)
6866 list_add(&conf->pending_data[i].sibling, &conf->free_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006867 /* Don't enable multi-threading by default*/
majianpeng60aaf932013-11-14 15:16:20 +11006868 if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group,
6869 &new_group)) {
6870 conf->group_cnt = group_cnt;
6871 conf->worker_cnt_per_group = worker_cnt_per_group;
6872 conf->worker_groups = new_group;
6873 } else
Shaohua Li851c30c2013-08-28 14:30:16 +08006874 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11006875 spin_lock_init(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10006876 seqcount_init(&conf->gen_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006877 mutex_init(&conf->cache_size_mutex);
Yuanhan Liub1b46482015-05-08 18:19:06 +10006878 init_waitqueue_head(&conf->wait_for_quiescent);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08006879 init_waitqueue_head(&conf->wait_for_stripe);
Dan Williamsf5efd452009-10-16 15:55:38 +11006880 init_waitqueue_head(&conf->wait_for_overlap);
6881 INIT_LIST_HEAD(&conf->handle_list);
Shaohua Li535ae4e2017-02-15 19:37:32 -08006882 INIT_LIST_HEAD(&conf->loprio_list);
Dan Williamsf5efd452009-10-16 15:55:38 +11006883 INIT_LIST_HEAD(&conf->hold_list);
6884 INIT_LIST_HEAD(&conf->delayed_list);
6885 INIT_LIST_HEAD(&conf->bitmap_list);
Shaohua Li773ca822013-08-27 17:50:39 +08006886 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11006887 atomic_set(&conf->active_stripes, 0);
6888 atomic_set(&conf->preread_active_stripes, 0);
6889 atomic_set(&conf->active_aligned_reads, 0);
Shaohua Li765d7042017-01-04 09:33:23 -08006890 spin_lock_init(&conf->pending_bios_lock);
6891 conf->batch_bio_dispatch = true;
6892 rdev_for_each(rdev, mddev) {
6893 if (test_bit(Journal, &rdev->flags))
6894 continue;
6895 if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
6896 conf->batch_bio_dispatch = false;
6897 break;
6898 }
6899 }
6900
Dan Williamsf5efd452009-10-16 15:55:38 +11006901 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11006902 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11006903
6904 conf->raid_disks = mddev->raid_disks;
6905 if (mddev->reshape_position == MaxSector)
6906 conf->previous_raid_disks = mddev->raid_disks;
6907 else
6908 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006909 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006910
NeilBrown5e5e3e72009-10-16 16:35:30 +11006911 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11006912 GFP_KERNEL);
Song Liud7bd3982016-11-23 22:50:39 -08006913
NeilBrown91adb562009-03-31 14:39:39 +11006914 if (!conf->disks)
6915 goto abort;
6916
Song Liud7bd3982016-11-23 22:50:39 -08006917 for (i = 0; i < max_disks; i++) {
6918 conf->disks[i].extra_page = alloc_page(GFP_KERNEL);
6919 if (!conf->disks[i].extra_page)
6920 goto abort;
6921 }
6922
NeilBrowndd7a8f52017-04-05 14:05:51 +10006923 conf->bio_split = bioset_create(BIO_POOL_SIZE, 0);
6924 if (!conf->bio_split)
6925 goto abort;
NeilBrown91adb562009-03-31 14:39:39 +11006926 conf->mddev = mddev;
6927
6928 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
6929 goto abort;
6930
Shaohua Li566c09c2013-11-14 15:16:17 +11006931 /* We init hash_locks[0] separately to that it can be used
6932 * as the reference lock in the spin_lock_nest_lock() call
6933 * in lock_all_device_hash_locks_irq in order to convince
6934 * lockdep that we know what we are doing.
6935 */
6936 spin_lock_init(conf->hash_locks);
6937 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
6938 spin_lock_init(conf->hash_locks + i);
6939
6940 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6941 INIT_LIST_HEAD(conf->inactive_list + i);
6942
6943 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6944 INIT_LIST_HEAD(conf->temp_inactive_list + i);
6945
Song Liu1e6d6902016-11-17 15:24:39 -08006946 atomic_set(&conf->r5c_cached_full_stripes, 0);
6947 INIT_LIST_HEAD(&conf->r5c_full_stripe_list);
6948 atomic_set(&conf->r5c_cached_partial_stripes, 0);
6949 INIT_LIST_HEAD(&conf->r5c_partial_stripe_list);
Shaohua Lie33fbb92017-02-10 16:18:09 -08006950 atomic_set(&conf->r5c_flushing_full_stripes, 0);
6951 atomic_set(&conf->r5c_flushing_partial_stripes, 0);
Song Liu1e6d6902016-11-17 15:24:39 -08006952
Dan Williams36d1c642009-07-14 11:48:22 -07006953 conf->level = mddev->new_level;
shli@kernel.org46d5b782014-12-15 12:57:02 +11006954 conf->chunk_sectors = mddev->new_chunk_sectors;
Dan Williams36d1c642009-07-14 11:48:22 -07006955 if (raid5_alloc_percpu(conf) != 0)
6956 goto abort;
6957
NeilBrown0c55e022010-05-03 14:09:02 +10006958 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11006959
NeilBrowndafb20f2012-03-19 12:46:39 +11006960 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11006961 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006962 if (raid_disk >= max_disks
Shaohua Lif2076e72015-10-08 21:54:12 -07006963 || raid_disk < 0 || test_bit(Journal, &rdev->flags))
NeilBrown91adb562009-03-31 14:39:39 +11006964 continue;
6965 disk = conf->disks + raid_disk;
6966
NeilBrown17045f52011-12-23 10:17:53 +11006967 if (test_bit(Replacement, &rdev->flags)) {
6968 if (disk->replacement)
6969 goto abort;
6970 disk->replacement = rdev;
6971 } else {
6972 if (disk->rdev)
6973 goto abort;
6974 disk->rdev = rdev;
6975 }
NeilBrown91adb562009-03-31 14:39:39 +11006976
6977 if (test_bit(In_sync, &rdev->flags)) {
6978 char b[BDEVNAME_SIZE];
NeilBrowncc6167b2016-11-02 14:16:50 +11006979 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
6980 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05006981 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11006982 /* Cannot rely on bitmap to complete recovery */
6983 conf->fullsync = 1;
6984 }
6985
NeilBrown91adb562009-03-31 14:39:39 +11006986 conf->level = mddev->new_level;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006987 if (conf->level == 6) {
NeilBrown91adb562009-03-31 14:39:39 +11006988 conf->max_degraded = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006989 if (raid6_call.xor_syndrome)
6990 conf->rmw_level = PARITY_ENABLE_RMW;
6991 else
6992 conf->rmw_level = PARITY_DISABLE_RMW;
6993 } else {
NeilBrown91adb562009-03-31 14:39:39 +11006994 conf->max_degraded = 1;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006995 conf->rmw_level = PARITY_ENABLE_RMW;
6996 }
NeilBrown91adb562009-03-31 14:39:39 +11006997 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11006998 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11006999 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10007000 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11007001 conf->prev_algo = mddev->layout;
NeilBrown5cac6bc2015-07-17 12:17:50 +10007002 } else {
7003 conf->prev_chunk_sectors = conf->chunk_sectors;
7004 conf->prev_algo = conf->algorithm;
NeilBrowne183eae2009-03-31 15:20:22 +11007005 }
NeilBrown91adb562009-03-31 14:39:39 +11007006
NeilBrownedbe83a2015-02-26 12:47:56 +11007007 conf->min_nr_stripes = NR_STRIPES;
Shaohua Liad5b0f72016-08-30 10:29:33 -07007008 if (mddev->reshape_position != MaxSector) {
7009 int stripes = max_t(int,
7010 ((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4,
7011 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4);
7012 conf->min_nr_stripes = max(NR_STRIPES, stripes);
7013 if (conf->min_nr_stripes != NR_STRIPES)
NeilBrowncc6167b2016-11-02 14:16:50 +11007014 pr_info("md/raid:%s: force stripe size %d for reshape\n",
Shaohua Liad5b0f72016-08-30 10:29:33 -07007015 mdname(mddev), conf->min_nr_stripes);
7016 }
NeilBrownedbe83a2015-02-26 12:47:56 +11007017 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11007018 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li4bda5562013-11-14 15:16:17 +11007019 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
NeilBrownedbe83a2015-02-26 12:47:56 +11007020 if (grow_stripes(conf, conf->min_nr_stripes)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007021 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
7022 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11007023 goto abort;
7024 } else
NeilBrowncc6167b2016-11-02 14:16:50 +11007025 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory);
NeilBrownedbe83a2015-02-26 12:47:56 +11007026 /*
7027 * Losing a stripe head costs more than the time to refill it,
7028 * it reduces the queue depth and so can hurt throughput.
7029 * So set it rather large, scaled by number of devices.
7030 */
7031 conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
7032 conf->shrinker.scan_objects = raid5_cache_scan;
7033 conf->shrinker.count_objects = raid5_cache_count;
7034 conf->shrinker.batch = 128;
7035 conf->shrinker.flags = 0;
Chao Yu6a0f53f2016-09-20 10:33:57 +08007036 if (register_shrinker(&conf->shrinker)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007037 pr_warn("md/raid:%s: couldn't register shrinker.\n",
7038 mdname(mddev));
Chao Yu6a0f53f2016-09-20 10:33:57 +08007039 goto abort;
7040 }
NeilBrown91adb562009-03-31 14:39:39 +11007041
NeilBrown02326052012-07-03 15:56:52 +10007042 sprintf(pers_name, "raid%d", mddev->new_level);
7043 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11007044 if (!conf->thread) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007045 pr_warn("md/raid:%s: couldn't allocate thread.\n",
7046 mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11007047 goto abort;
7048 }
7049
7050 return conf;
7051
7052 abort:
7053 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10007054 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11007055 return ERR_PTR(-EIO);
7056 } else
7057 return ERR_PTR(-ENOMEM);
7058}
7059
NeilBrownc148ffd2009-11-13 17:47:00 +11007060static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
7061{
7062 switch (algo) {
7063 case ALGORITHM_PARITY_0:
7064 if (raid_disk < max_degraded)
7065 return 1;
7066 break;
7067 case ALGORITHM_PARITY_N:
7068 if (raid_disk >= raid_disks - max_degraded)
7069 return 1;
7070 break;
7071 case ALGORITHM_PARITY_0_6:
NeilBrownf72ffdd2014-09-30 14:23:59 +10007072 if (raid_disk == 0 ||
NeilBrownc148ffd2009-11-13 17:47:00 +11007073 raid_disk == raid_disks - 1)
7074 return 1;
7075 break;
7076 case ALGORITHM_LEFT_ASYMMETRIC_6:
7077 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7078 case ALGORITHM_LEFT_SYMMETRIC_6:
7079 case ALGORITHM_RIGHT_SYMMETRIC_6:
7080 if (raid_disk == raid_disks - 1)
7081 return 1;
7082 }
7083 return 0;
7084}
7085
Shaohua Li849674e2016-01-20 13:52:20 -08007086static int raid5_run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11007087{
NeilBrownd1688a62011-10-11 16:49:52 +11007088 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10007089 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11007090 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007091 struct md_rdev *rdev;
Shaohua Li713cf5a2015-08-13 14:32:03 -07007092 struct md_rdev *journal_dev = NULL;
NeilBrownc148ffd2009-11-13 17:47:00 +11007093 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11007094 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10007095 long long min_offset_diff = 0;
7096 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11007097
Andre Noll8c6ac8682009-06-18 08:48:06 +10007098 if (mddev->recovery_cp != MaxSector)
NeilBrowncc6167b2016-11-02 14:16:50 +11007099 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
7100 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10007101
7102 rdev_for_each(rdev, mddev) {
7103 long long diff;
Shaohua Li713cf5a2015-08-13 14:32:03 -07007104
Shaohua Lif2076e72015-10-08 21:54:12 -07007105 if (test_bit(Journal, &rdev->flags)) {
Shaohua Li713cf5a2015-08-13 14:32:03 -07007106 journal_dev = rdev;
Shaohua Lif2076e72015-10-08 21:54:12 -07007107 continue;
7108 }
NeilBrownb5254dd2012-05-21 09:27:01 +10007109 if (rdev->raid_disk < 0)
7110 continue;
7111 diff = (rdev->new_data_offset - rdev->data_offset);
7112 if (first) {
7113 min_offset_diff = diff;
7114 first = 0;
7115 } else if (mddev->reshape_backwards &&
7116 diff < min_offset_diff)
7117 min_offset_diff = diff;
7118 else if (!mddev->reshape_backwards &&
7119 diff > min_offset_diff)
7120 min_offset_diff = diff;
7121 }
7122
NeilBrownf6705572006-03-27 01:18:11 -08007123 if (mddev->reshape_position != MaxSector) {
7124 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10007125 * Difficulties arise if the stripe we would write to
7126 * next is at or after the stripe we would read from next.
7127 * For a reshape that changes the number of devices, this
7128 * is only possible for a very short time, and mdadm makes
7129 * sure that time appears to have past before assembling
7130 * the array. So we fail if that time hasn't passed.
7131 * For a reshape that keeps the number of devices the same
7132 * mdadm must be monitoring the reshape can keeping the
7133 * critical areas read-only and backed up. It will start
7134 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08007135 */
7136 sector_t here_new, here_old;
7137 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11007138 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrown05256d92015-07-15 17:36:21 +10007139 int chunk_sectors;
7140 int new_data_disks;
NeilBrownf6705572006-03-27 01:18:11 -08007141
Shaohua Li713cf5a2015-08-13 14:32:03 -07007142 if (journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007143 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
7144 mdname(mddev));
Shaohua Li713cf5a2015-08-13 14:32:03 -07007145 return -EINVAL;
7146 }
7147
NeilBrown88ce4932009-03-31 15:24:23 +11007148 if (mddev->new_level != mddev->level) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007149 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
7150 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007151 return -EINVAL;
7152 }
NeilBrownf6705572006-03-27 01:18:11 -08007153 old_disks = mddev->raid_disks - mddev->delta_disks;
7154 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08007155 * further up in new geometry must map after here in old
7156 * geometry.
NeilBrown05256d92015-07-15 17:36:21 +10007157 * If the chunk sizes are different, then as we perform reshape
7158 * in units of the largest of the two, reshape_position needs
7159 * be a multiple of the largest chunk size times new data disks.
NeilBrownf6705572006-03-27 01:18:11 -08007160 */
7161 here_new = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007162 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
7163 new_data_disks = mddev->raid_disks - max_degraded;
7164 if (sector_div(here_new, chunk_sectors * new_data_disks)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007165 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
7166 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007167 return -EINVAL;
7168 }
NeilBrown05256d92015-07-15 17:36:21 +10007169 reshape_offset = here_new * chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08007170 /* here_new is the stripe we will write to */
7171 here_old = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007172 sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
NeilBrownf4168852007-02-28 20:11:53 -08007173 /* here_old is the first stripe that we might need to read
7174 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10007175 if (mddev->delta_disks == 0) {
7176 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10007177 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10007178 * and taking constant backups.
7179 * mdadm always starts a situation like this in
7180 * readonly mode so it can take control before
7181 * allowing any writes. So just check for that.
7182 */
NeilBrownb5254dd2012-05-21 09:27:01 +10007183 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
7184 abs(min_offset_diff) >= mddev->new_chunk_sectors)
7185 /* not really in-place - so OK */;
7186 else if (mddev->ro == 0) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007187 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
7188 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10007189 return -EINVAL;
7190 }
NeilBrown2c810cd2012-05-21 09:27:00 +10007191 } else if (mddev->reshape_backwards
NeilBrown05256d92015-07-15 17:36:21 +10007192 ? (here_new * chunk_sectors + min_offset_diff <=
7193 here_old * chunk_sectors)
7194 : (here_new * chunk_sectors >=
7195 here_old * chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08007196 /* Reading from the same stripe as writing to - bad */
NeilBrowncc6167b2016-11-02 14:16:50 +11007197 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
7198 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007199 return -EINVAL;
7200 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007201 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007202 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08007203 } else {
NeilBrown91adb562009-03-31 14:39:39 +11007204 BUG_ON(mddev->level != mddev->new_level);
7205 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10007206 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11007207 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08007208 }
7209
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01007210 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) &&
7211 test_bit(MD_HAS_PPL, &mddev->flags)) {
7212 pr_warn("md/raid:%s: using journal device and PPL not allowed - disabling PPL\n",
7213 mdname(mddev));
7214 clear_bit(MD_HAS_PPL, &mddev->flags);
7215 }
7216
NeilBrown245f46c2009-03-31 14:39:39 +11007217 if (mddev->private == NULL)
7218 conf = setup_conf(mddev);
7219 else
7220 conf = mddev->private;
7221
NeilBrown91adb562009-03-31 14:39:39 +11007222 if (IS_ERR(conf))
7223 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08007224
Song Liu486b0f72016-08-19 15:34:01 -07007225 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
7226 if (!journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007227 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
7228 mdname(mddev));
Song Liu486b0f72016-08-19 15:34:01 -07007229 mddev->ro = 1;
7230 set_disk_ro(mddev->gendisk, 1);
7231 } else if (mddev->recovery_cp == MaxSector)
7232 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
Shaohua Li7dde2ad2015-10-08 21:54:10 -07007233 }
7234
NeilBrownb5254dd2012-05-21 09:27:01 +10007235 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11007236 mddev->thread = conf->thread;
7237 conf->thread = NULL;
7238 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239
NeilBrown17045f52011-12-23 10:17:53 +11007240 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
7241 i++) {
7242 rdev = conf->disks[i].rdev;
7243 if (!rdev && conf->disks[i].replacement) {
7244 /* The replacement is all we have yet */
7245 rdev = conf->disks[i].replacement;
7246 conf->disks[i].replacement = NULL;
7247 clear_bit(Replacement, &rdev->flags);
7248 conf->disks[i].rdev = rdev;
7249 }
7250 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11007251 continue;
NeilBrown17045f52011-12-23 10:17:53 +11007252 if (conf->disks[i].replacement &&
7253 conf->reshape_progress != MaxSector) {
7254 /* replacements and reshape simply do not mix. */
NeilBrowncc6167b2016-11-02 14:16:50 +11007255 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
NeilBrown17045f52011-12-23 10:17:53 +11007256 goto abort;
7257 }
NeilBrown2f115882010-06-17 17:41:03 +10007258 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11007259 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10007260 continue;
7261 }
NeilBrownc148ffd2009-11-13 17:47:00 +11007262 /* This disc is not fully in-sync. However if it
7263 * just stored parity (beyond the recovery_offset),
7264 * when we don't need to be concerned about the
7265 * array being dirty.
7266 * When reshape goes 'backwards', we never have
7267 * partially completed devices, so we only need
7268 * to worry about reshape going forwards.
7269 */
7270 /* Hack because v0.91 doesn't store recovery_offset properly. */
7271 if (mddev->major_version == 0 &&
7272 mddev->minor_version > 90)
7273 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007274
NeilBrownc148ffd2009-11-13 17:47:00 +11007275 if (rdev->recovery_offset < reshape_offset) {
7276 /* We need to check old and new layout */
7277 if (!only_parity(rdev->raid_disk,
7278 conf->algorithm,
7279 conf->raid_disks,
7280 conf->max_degraded))
7281 continue;
7282 }
7283 if (!only_parity(rdev->raid_disk,
7284 conf->prev_algo,
7285 conf->previous_raid_disks,
7286 conf->max_degraded))
7287 continue;
7288 dirty_parity_disks++;
7289 }
NeilBrown91adb562009-03-31 14:39:39 +11007290
NeilBrown17045f52011-12-23 10:17:53 +11007291 /*
7292 * 0 for a fully functional array, 1 or 2 for a degraded array.
7293 */
Song Liu2e38a372017-01-24 10:45:30 -08007294 mddev->degraded = raid5_calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295
NeilBrown674806d2010-06-16 17:17:53 +10007296 if (has_failed(conf)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007297 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07007298 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007299 goto abort;
7300 }
7301
NeilBrown91adb562009-03-31 14:39:39 +11007302 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10007303 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11007304 mddev->resync_max_sectors = mddev->dev_sectors;
7305
NeilBrownc148ffd2009-11-13 17:47:00 +11007306 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 mddev->recovery_cp != MaxSector) {
Artur Paszkiewicz4536bf9b2017-03-09 10:00:01 +01007308 if (test_bit(MD_HAS_PPL, &mddev->flags))
7309 pr_crit("md/raid:%s: starting dirty degraded array with PPL.\n",
7310 mdname(mddev));
7311 else if (mddev->ok_start_degraded)
NeilBrowncc6167b2016-11-02 14:16:50 +11007312 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
7313 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007314 else {
NeilBrowncc6167b2016-11-02 14:16:50 +11007315 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
7316 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007317 goto abort;
7318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319 }
7320
NeilBrowncc6167b2016-11-02 14:16:50 +11007321 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
7322 mdname(mddev), conf->level,
7323 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
7324 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325
7326 print_raid5_conf(conf);
7327
NeilBrownfef9c612009-03-31 15:16:46 +11007328 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11007329 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08007330 atomic_set(&conf->reshape_stripes, 0);
7331 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7332 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7333 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7334 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7335 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007336 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08007337 }
7338
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10007340 if (mddev->to_remove == &raid5_attrs_group)
7341 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007342 else if (mddev->kobj.sd &&
7343 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrowncc6167b2016-11-02 14:16:50 +11007344 pr_warn("raid5: failed to create sysfs attributes for %s\n",
7345 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10007346 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
7347
7348 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007349 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11007350 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10007351 /* read-ahead size must cover two whole stripes, which
7352 * is 2 * (datadisks) * chunksize where 'n' is the
7353 * number of raid devices
7354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 int data_disks = conf->previous_raid_disks - conf->max_degraded;
7356 int stripe = data_disks *
7357 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Jan Karadc3b17c2017-02-02 15:56:50 +01007358 if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
7359 mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10007360
NeilBrown9f7c2222010-07-26 12:04:13 +10007361 chunk_size = mddev->chunk_sectors << 9;
7362 blk_queue_io_min(mddev->queue, chunk_size);
7363 blk_queue_io_opt(mddev->queue, chunk_size *
7364 (conf->raid_disks - conf->max_degraded));
Kent Overstreetc78afc62013-07-11 22:39:53 -07007365 mddev->queue->limits.raid_partial_stripes_expensive = 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007366 /*
7367 * We can only discard a whole stripe. It doesn't make sense to
7368 * discard data disk but write parity disk
7369 */
7370 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11007371 /* Round up to power of 2, as discard handling
7372 * currently assumes that */
7373 while ((stripe-1) & stripe)
7374 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007375 mddev->queue->limits.discard_alignment = stripe;
7376 mddev->queue->limits.discard_granularity = stripe;
7377 /*
7378 * unaligned part of discard request will be ignored, so can't
NeilBrown8e0e99b2014-10-02 13:45:00 +10007379 * guarantee discard_zeroes_data
Shaohua Li620125f2012-10-11 13:49:05 +11007380 */
7381 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10007382
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007383 blk_queue_max_write_same_sectors(mddev->queue, 0);
7384
NeilBrown05616be2012-05-21 09:27:00 +10007385 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007386 disk_stack_limits(mddev->gendisk, rdev->bdev,
7387 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10007388 disk_stack_limits(mddev->gendisk, rdev->bdev,
7389 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11007390 /*
7391 * discard_zeroes_data is required, otherwise data
7392 * could be lost. Consider a scenario: discard a stripe
7393 * (the stripe could be inconsistent if
7394 * discard_zeroes_data is 0); write one disk of the
7395 * stripe (the stripe could be inconsistent again
7396 * depending on which disks are used to calculate
7397 * parity); the disk is broken; The stripe data of this
7398 * disk is lost.
7399 */
7400 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
7401 !bdev_get_queue(rdev->bdev)->
7402 limits.discard_zeroes_data)
7403 discard_supported = false;
NeilBrown8e0e99b2014-10-02 13:45:00 +10007404 /* Unfortunately, discard_zeroes_data is not currently
7405 * a guarantee - just a hint. So we only allow DISCARD
7406 * if the sysadmin has confirmed that only safe devices
7407 * are in use by setting a module parameter.
7408 */
7409 if (!devices_handle_discard_safely) {
7410 if (discard_supported) {
7411 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
7412 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
7413 }
7414 discard_supported = false;
7415 }
NeilBrown05616be2012-05-21 09:27:00 +10007416 }
Shaohua Li620125f2012-10-11 13:49:05 +11007417
7418 if (discard_supported &&
Jes Sorensene7597e62016-02-16 16:44:24 -05007419 mddev->queue->limits.max_discard_sectors >= (stripe >> 9) &&
7420 mddev->queue->limits.discard_granularity >= stripe)
Shaohua Li620125f2012-10-11 13:49:05 +11007421 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
7422 mddev->queue);
7423 else
7424 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
7425 mddev->queue);
Shaohua Li1dffddd2016-09-08 10:49:06 -07007426
7427 blk_queue_max_hw_sectors(mddev->queue, UINT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007428 }
7429
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02007430 if (log_init(conf, journal_dev, raid5_has_ppl(conf)))
Artur Paszkiewiczff875732017-03-09 09:59:58 +01007431 goto abort;
Shaohua Li5c7e81c2015-08-13 14:32:04 -07007432
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 return 0;
7434abort:
NeilBrown01f96c02011-09-21 15:30:20 +10007435 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11007436 print_raid5_conf(conf);
7437 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438 mddev->private = NULL;
NeilBrowncc6167b2016-11-02 14:16:50 +11007439 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007440 return -EIO;
7441}
7442
NeilBrownafa0f552014-12-15 12:56:58 +11007443static void raid5_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007444{
NeilBrownafa0f552014-12-15 12:56:58 +11007445 struct r5conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007446
Dan Williams95fc17a2009-07-31 12:39:15 +10007447 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10007448 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007449}
7450
Shaohua Li849674e2016-01-20 13:52:20 -08007451static void raid5_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007452{
NeilBrownd1688a62011-10-11 16:49:52 +11007453 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007454 int i;
7455
Andre Noll9d8f0362009-06-18 08:45:01 +10007456 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
NeilBrown3cb5edf2015-07-15 17:24:17 +10007457 conf->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07007458 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
NeilBrown5fd13352016-06-02 16:19:52 +10007459 rcu_read_lock();
7460 for (i = 0; i < conf->raid_disks; i++) {
7461 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
7462 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
7463 }
7464 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007466}
7467
NeilBrownd1688a62011-10-11 16:49:52 +11007468static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469{
7470 int i;
7471 struct disk_info *tmp;
7472
NeilBrowncc6167b2016-11-02 14:16:50 +11007473 pr_debug("RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474 if (!conf) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007475 pr_debug("(conf==NULL)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007476 return;
7477 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007478 pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level,
NeilBrown0c55e022010-05-03 14:09:02 +10007479 conf->raid_disks,
7480 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481
7482 for (i = 0; i < conf->raid_disks; i++) {
7483 char b[BDEVNAME_SIZE];
7484 tmp = conf->disks + i;
7485 if (tmp->rdev)
NeilBrowncc6167b2016-11-02 14:16:50 +11007486 pr_debug(" disk %d, o:%d, dev:%s\n",
NeilBrown0c55e022010-05-03 14:09:02 +10007487 i, !test_bit(Faulty, &tmp->rdev->flags),
7488 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 }
7490}
7491
NeilBrownfd01b882011-10-11 16:47:53 +11007492static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493{
7494 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11007495 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10007497 int count = 0;
7498 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007499
7500 for (i = 0; i < conf->raid_disks; i++) {
7501 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11007502 if (tmp->replacement
7503 && tmp->replacement->recovery_offset == MaxSector
7504 && !test_bit(Faulty, &tmp->replacement->flags)
7505 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
7506 /* Replacement has just become active. */
7507 if (!tmp->rdev
7508 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
7509 count++;
7510 if (tmp->rdev) {
7511 /* Replaced device not technically faulty,
7512 * but we need to be sure it gets removed
7513 * and never re-added.
7514 */
7515 set_bit(Faulty, &tmp->rdev->flags);
7516 sysfs_notify_dirent_safe(
7517 tmp->rdev->sysfs_state);
7518 }
7519 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
7520 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10007521 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08007522 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07007523 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10007524 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11007525 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007526 }
7527 }
NeilBrown6b965622010-08-18 11:56:59 +10007528 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08007529 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007530 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007532 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533}
7534
NeilBrownb8321b62011-12-23 10:17:51 +11007535static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
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 err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11007539 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11007540 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541 struct disk_info *p = conf->disks + number;
7542
7543 print_raid5_conf(conf);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007544 if (test_bit(Journal, &rdev->flags) && conf->log) {
Shaohua Lic2bb6242015-10-08 21:54:07 -07007545 /*
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007546 * we can't wait pending write here, as this is called in
7547 * raid5d, wait will deadlock.
NeilBrown84dd97a2017-03-15 14:05:14 +11007548 * neilb: there is no locking about new writes here,
7549 * so this cannot be safe.
Shaohua Lic2bb6242015-10-08 21:54:07 -07007550 */
NeilBrown84dd97a2017-03-15 14:05:14 +11007551 if (atomic_read(&conf->active_stripes)) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007552 return -EBUSY;
NeilBrown84dd97a2017-03-15 14:05:14 +11007553 }
Artur Paszkiewiczff875732017-03-09 09:59:58 +01007554 log_exit(conf);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007555 return 0;
Shaohua Lic2bb6242015-10-08 21:54:07 -07007556 }
NeilBrown657e3e42011-12-23 10:17:52 +11007557 if (rdev == p->rdev)
7558 rdevp = &p->rdev;
7559 else if (rdev == p->replacement)
7560 rdevp = &p->replacement;
7561 else
7562 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11007563
NeilBrown657e3e42011-12-23 10:17:52 +11007564 if (number >= conf->raid_disks &&
7565 conf->reshape_progress == MaxSector)
7566 clear_bit(In_sync, &rdev->flags);
7567
7568 if (test_bit(In_sync, &rdev->flags) ||
7569 atomic_read(&rdev->nr_pending)) {
7570 err = -EBUSY;
7571 goto abort;
7572 }
7573 /* Only remove non-faulty devices if recovery
7574 * isn't possible.
7575 */
7576 if (!test_bit(Faulty, &rdev->flags) &&
7577 mddev->recovery_disabled != conf->recovery_disabled &&
7578 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11007579 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11007580 number < conf->raid_disks) {
7581 err = -EBUSY;
7582 goto abort;
7583 }
7584 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10007585 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
7586 synchronize_rcu();
7587 if (atomic_read(&rdev->nr_pending)) {
7588 /* lost the race, try later */
7589 err = -EBUSY;
7590 *rdevp = rdev;
7591 }
7592 }
Artur Paszkiewicz6358c232017-03-09 10:00:02 +01007593 if (!err) {
7594 err = log_modify(conf, rdev, false);
7595 if (err)
7596 goto abort;
7597 }
NeilBrownd787be42016-06-02 16:19:53 +10007598 if (p->replacement) {
NeilBrowndd054fc2011-12-23 10:17:53 +11007599 /* We must have just cleared 'rdev' */
7600 p->rdev = p->replacement;
7601 clear_bit(Replacement, &p->replacement->flags);
7602 smp_mb(); /* Make sure other CPUs may see both as identical
7603 * but will never see neither - if they are careful
7604 */
7605 p->replacement = NULL;
7606 clear_bit(WantReplacement, &rdev->flags);
Artur Paszkiewicz6358c232017-03-09 10:00:02 +01007607
7608 if (!err)
7609 err = log_modify(conf, p->rdev, true);
NeilBrowndd054fc2011-12-23 10:17:53 +11007610 } else
7611 /* We might have just removed the Replacement as faulty-
7612 * clear the bit just in case
7613 */
7614 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615abort:
7616
7617 print_raid5_conf(conf);
7618 return err;
7619}
7620
NeilBrownfd01b882011-10-11 16:47:53 +11007621static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622{
NeilBrownd1688a62011-10-11 16:49:52 +11007623 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10007624 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625 int disk;
7626 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10007627 int first = 0;
7628 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007629
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007630 if (test_bit(Journal, &rdev->flags)) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007631 if (conf->log)
7632 return -EBUSY;
7633
7634 rdev->raid_disk = 0;
7635 /*
7636 * The array is in readonly mode if journal is missing, so no
7637 * write requests running. We should be safe
7638 */
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02007639 log_init(conf, rdev, false);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007640 return 0;
7641 }
NeilBrown7f0da592011-07-28 11:39:22 +10007642 if (mddev->recovery_disabled == conf->recovery_disabled)
7643 return -EBUSY;
7644
NeilBrowndc10c642012-03-19 12:46:37 +11007645 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007646 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10007647 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648
Neil Brown6c2fce22008-06-28 08:31:31 +10007649 if (rdev->raid_disk >= 0)
7650 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007651
7652 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07007653 * find the disk ... but prefer rdev->saved_raid_disk
7654 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655 */
NeilBrown16a53ec2006-06-26 00:27:38 -07007656 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10007657 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07007658 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10007659 first = rdev->saved_raid_disk;
7660
7661 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007662 p = conf->disks + disk;
7663 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08007664 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007665 rdev->raid_disk = disk;
NeilBrown72626682005-09-09 16:23:54 -07007666 if (rdev->saved_raid_disk != disk)
7667 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08007668 rcu_assign_pointer(p->rdev, rdev);
Artur Paszkiewicz6358c232017-03-09 10:00:02 +01007669
7670 err = log_modify(conf, rdev, true);
7671
NeilBrown5cfb22a2012-07-03 11:46:53 +10007672 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007673 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007674 }
7675 for (disk = first; disk <= last; disk++) {
7676 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007677 if (test_bit(WantReplacement, &p->rdev->flags) &&
7678 p->replacement == NULL) {
7679 clear_bit(In_sync, &rdev->flags);
7680 set_bit(Replacement, &rdev->flags);
7681 rdev->raid_disk = disk;
7682 err = 0;
7683 conf->fullsync = 1;
7684 rcu_assign_pointer(p->replacement, rdev);
7685 break;
7686 }
7687 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007688out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007689 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10007690 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007691}
7692
NeilBrownfd01b882011-10-11 16:47:53 +11007693static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007694{
7695 /* no resync is happening, and there is enough space
7696 * on all devices, so we can resize.
7697 * We need to make sure resync covers any new space.
7698 * If the array is shrinking we should possibly wait until
7699 * any io in the removed space completes, but it hardly seems
7700 * worth it.
7701 */
NeilBrowna4a61252012-05-22 13:55:27 +10007702 sector_t newsize;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007703 struct r5conf *conf = mddev->private;
7704
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01007705 if (conf->log || raid5_has_ppl(conf))
Shaohua Li713cf5a2015-08-13 14:32:03 -07007706 return -EINVAL;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007707 sectors &= ~((sector_t)conf->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10007708 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
7709 if (mddev->external_size &&
7710 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11007711 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10007712 if (mddev->bitmap) {
7713 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
7714 if (ret)
7715 return ret;
7716 }
7717 md_set_array_sectors(mddev, newsize);
NeilBrownb0986362011-05-11 15:52:21 +10007718 if (sectors > mddev->dev_sectors &&
7719 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11007720 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007721 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7722 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007723 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07007724 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007725 return 0;
7726}
7727
NeilBrownfd01b882011-10-11 16:47:53 +11007728static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10007729{
7730 /* Can only proceed if there are plenty of stripe_heads.
7731 * We need a minimum of one full stripe,, and for sensible progress
7732 * it is best to have about 4 times that.
7733 * If we require 4 times, then the default 256 4K stripe_heads will
7734 * allow for chunk sizes up to 256K, which is probably OK.
7735 * If the chunk size is greater, user-space should request more
7736 * stripe_heads first.
7737 */
NeilBrownd1688a62011-10-11 16:49:52 +11007738 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10007739 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007740 > conf->min_nr_stripes ||
NeilBrown01ee22b2009-06-18 08:47:20 +10007741 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007742 > conf->min_nr_stripes) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007743 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7744 mdname(mddev),
7745 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
7746 / STRIPE_SIZE)*4);
NeilBrown01ee22b2009-06-18 08:47:20 +10007747 return 0;
7748 }
7749 return 1;
7750}
7751
NeilBrownfd01b882011-10-11 16:47:53 +11007752static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08007753{
NeilBrownd1688a62011-10-11 16:49:52 +11007754 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08007755
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01007756 if (conf->log || raid5_has_ppl(conf))
Shaohua Li713cf5a2015-08-13 14:32:03 -07007757 return -EINVAL;
NeilBrown88ce4932009-03-31 15:24:23 +11007758 if (mddev->delta_disks == 0 &&
7759 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10007760 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10007761 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10007762 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11007763 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10007764 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11007765 /* We might be able to shrink, but the devices must
7766 * be made bigger first.
7767 * For raid6, 4 is the minimum size.
7768 * Otherwise 2 is the minimum
7769 */
7770 int min = 2;
7771 if (mddev->level == 6)
7772 min = 4;
7773 if (mddev->raid_disks + mddev->delta_disks < min)
7774 return -EINVAL;
7775 }
NeilBrown29269552006-03-27 01:18:10 -08007776
NeilBrown01ee22b2009-06-18 08:47:20 +10007777 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08007778 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08007779
NeilBrown738a2732015-05-08 18:19:39 +10007780 if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
7781 mddev->delta_disks > 0)
7782 if (resize_chunks(conf,
7783 conf->previous_raid_disks
7784 + max(0, mddev->delta_disks),
7785 max(mddev->new_chunk_sectors,
7786 mddev->chunk_sectors)
7787 ) < 0)
7788 return -ENOMEM;
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02007789
7790 if (conf->previous_raid_disks + mddev->delta_disks <= conf->pool_size)
7791 return 0; /* never bother to shrink */
NeilBrowne56108d62012-10-11 14:24:13 +11007792 return resize_stripes(conf, (conf->previous_raid_disks
7793 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08007794}
7795
NeilBrownfd01b882011-10-11 16:47:53 +11007796static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08007797{
NeilBrownd1688a62011-10-11 16:49:52 +11007798 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11007799 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08007800 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07007801 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08007802
NeilBrownf4168852007-02-28 20:11:53 -08007803 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08007804 return -EBUSY;
7805
NeilBrown01ee22b2009-06-18 08:47:20 +10007806 if (!check_stripe_cache(mddev))
7807 return -ENOSPC;
7808
NeilBrown30b67642012-05-22 13:55:28 +10007809 if (has_failed(conf))
7810 return -EINVAL;
7811
NeilBrownc6563a82012-05-21 09:27:00 +10007812 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11007813 if (!test_bit(In_sync, &rdev->flags)
7814 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08007815 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10007816 }
NeilBrown63c70c42006-03-27 01:18:13 -08007817
NeilBrownf4168852007-02-28 20:11:53 -08007818 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08007819 /* Not enough devices even to make a degraded array
7820 * of that size
7821 */
7822 return -EINVAL;
7823
NeilBrownec32a2b2009-03-31 15:17:38 +11007824 /* Refuse to reduce size of the array. Any reductions in
7825 * array size must be through explicit setting of array_size
7826 * attribute.
7827 */
7828 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
7829 < mddev->array_sectors) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007830 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
7831 mdname(mddev));
NeilBrownec32a2b2009-03-31 15:17:38 +11007832 return -EINVAL;
7833 }
7834
NeilBrownf6705572006-03-27 01:18:11 -08007835 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08007836 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10007837 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007838 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08007839 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007840 conf->prev_chunk_sectors = conf->chunk_sectors;
7841 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11007842 conf->prev_algo = conf->algorithm;
7843 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10007844 conf->generation++;
7845 /* Code that selects data_offset needs to see the generation update
7846 * if reshape_progress has been set - so a memory barrier needed.
7847 */
7848 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10007849 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11007850 conf->reshape_progress = raid5_size(mddev, 0, 0);
7851 else
7852 conf->reshape_progress = 0;
7853 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10007854 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007855 spin_unlock_irq(&conf->device_lock);
7856
NeilBrown4d77e3b2013-08-27 15:57:47 +10007857 /* Now make sure any requests that proceeded on the assumption
7858 * the reshape wasn't running - like Discard or Read - have
7859 * completed.
7860 */
7861 mddev_suspend(mddev);
7862 mddev_resume(mddev);
7863
NeilBrown29269552006-03-27 01:18:10 -08007864 /* Add some new drives, as many as will fit.
7865 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10007866 * Don't add devices if we are reducing the number of
7867 * devices in the array. This is because it is not possible
7868 * to correctly record the "partially reconstructed" state of
7869 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08007870 */
NeilBrown87a8dec2011-01-31 11:57:43 +11007871 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11007872 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11007873 if (rdev->raid_disk < 0 &&
7874 !test_bit(Faulty, &rdev->flags)) {
7875 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11007876 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11007877 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11007878 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11007879 else
NeilBrown87a8dec2011-01-31 11:57:43 +11007880 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10007881
7882 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11007883 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11007884 }
NeilBrown87a8dec2011-01-31 11:57:43 +11007885 } else if (rdev->raid_disk >= conf->previous_raid_disks
7886 && !test_bit(Faulty, &rdev->flags)) {
7887 /* This is a spare that was manually added */
7888 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11007889 }
NeilBrown29269552006-03-27 01:18:10 -08007890
NeilBrown87a8dec2011-01-31 11:57:43 +11007891 /* When a reshape changes the number of devices,
7892 * ->degraded is measured against the larger of the
7893 * pre and post number of devices.
7894 */
NeilBrownec32a2b2009-03-31 15:17:38 +11007895 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08007896 mddev->degraded = raid5_calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11007897 spin_unlock_irqrestore(&conf->device_lock, flags);
7898 }
NeilBrown63c70c42006-03-27 01:18:13 -08007899 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10007900 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08007901 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownf6705572006-03-27 01:18:11 -08007902
NeilBrown29269552006-03-27 01:18:10 -08007903 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7904 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10007905 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown29269552006-03-27 01:18:10 -08007906 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7907 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7908 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007909 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08007910 if (!mddev->sync_thread) {
7911 mddev->recovery = 0;
7912 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11007913 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007914 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11007915 mddev->new_chunk_sectors =
7916 conf->chunk_sectors = conf->prev_chunk_sectors;
7917 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10007918 rdev_for_each(rdev, mddev)
7919 rdev->new_data_offset = rdev->data_offset;
7920 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11007921 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11007922 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11007923 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11007924 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007925 spin_unlock_irq(&conf->device_lock);
7926 return -EAGAIN;
7927 }
NeilBrownc8f517c2009-03-31 15:28:40 +11007928 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08007929 md_wakeup_thread(mddev->sync_thread);
7930 md_new_event(mddev);
7931 return 0;
7932}
NeilBrown29269552006-03-27 01:18:10 -08007933
NeilBrownec32a2b2009-03-31 15:17:38 +11007934/* This is called from the reshape thread and should make any
7935 * changes needed in 'conf'
7936 */
NeilBrownd1688a62011-10-11 16:49:52 +11007937static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08007938{
NeilBrown29269552006-03-27 01:18:10 -08007939
NeilBrownf6705572006-03-27 01:18:11 -08007940 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10007941 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07007942
NeilBrownf6705572006-03-27 01:18:11 -08007943 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11007944 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10007945 rdev_for_each(rdev, conf->mddev)
7946 rdev->data_offset = rdev->new_data_offset;
7947 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11007948 conf->reshape_progress = MaxSector;
NeilBrown6cbd8142015-07-24 13:30:32 +10007949 conf->mddev->reshape_position = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08007950 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11007951 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07007952
7953 /* read-ahead size must cover two whole stripes, which is
7954 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
7955 */
NeilBrown4a5add42010-06-01 19:37:28 +10007956 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11007957 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007958 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11007959 / PAGE_SIZE);
Jan Karadc3b17c2017-02-02 15:56:50 +01007960 if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
7961 conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown16a53ec2006-06-26 00:27:38 -07007962 }
NeilBrown29269552006-03-27 01:18:10 -08007963 }
NeilBrown29269552006-03-27 01:18:10 -08007964}
7965
NeilBrownec32a2b2009-03-31 15:17:38 +11007966/* This is called from the raid5d thread with mddev_lock held.
7967 * It makes config changes to the device.
7968 */
NeilBrownfd01b882011-10-11 16:47:53 +11007969static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11007970{
NeilBrownd1688a62011-10-11 16:49:52 +11007971 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11007972
7973 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7974
NeilBrownec32a2b2009-03-31 15:17:38 +11007975 if (mddev->delta_disks > 0) {
7976 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
Heinz Mauelshagenfe67d192016-05-03 22:15:31 +02007977 if (mddev->queue) {
7978 set_capacity(mddev->gendisk, mddev->array_sectors);
7979 revalidate_disk(mddev->gendisk);
7980 }
NeilBrownec32a2b2009-03-31 15:17:38 +11007981 } else {
7982 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11007983 spin_lock_irq(&conf->device_lock);
Song Liu2e38a372017-01-24 10:45:30 -08007984 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11007985 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11007986 for (d = conf->raid_disks ;
7987 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10007988 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11007989 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10007990 if (rdev)
7991 clear_bit(In_sync, &rdev->flags);
7992 rdev = conf->disks[d].replacement;
7993 if (rdev)
7994 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10007995 }
NeilBrowncea9c222009-03-31 15:15:05 +11007996 }
NeilBrown88ce4932009-03-31 15:24:23 +11007997 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007998 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11007999 mddev->reshape_position = MaxSector;
8000 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10008001 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11008002 }
8003}
8004
NeilBrownfd01b882011-10-11 16:47:53 +11008005static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07008006{
NeilBrownd1688a62011-10-11 16:49:52 +11008007 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07008008
8009 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08008010 case 2: /* resume for a suspend */
8011 wake_up(&conf->wait_for_overlap);
8012 break;
8013
NeilBrown72626682005-09-09 16:23:54 -07008014 case 1: /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11008015 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10008016 /* '2' tells resync/reshape to pause so that all
8017 * active stripes can drain
8018 */
Song Liua39f7af2016-11-17 15:24:40 -08008019 r5c_flush_cache(conf, INT_MAX);
NeilBrown64bd6602009-08-03 10:59:58 +10008020 conf->quiesce = 2;
Yuanhan Liub1b46482015-05-08 18:19:06 +10008021 wait_event_cmd(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08008022 atomic_read(&conf->active_stripes) == 0 &&
8023 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11008024 unlock_all_device_hash_locks_irq(conf),
8025 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10008026 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11008027 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10008028 /* allow reshape to continue */
8029 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07008030 break;
8031
8032 case 0: /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11008033 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07008034 conf->quiesce = 0;
Yuanhan Liub1b46482015-05-08 18:19:06 +10008035 wake_up(&conf->wait_for_quiescent);
NeilBrowne464eaf2006-03-27 01:18:14 -08008036 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11008037 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07008038 break;
8039 }
Shaohua Lie6c033f2015-10-04 09:20:12 -07008040 r5l_quiesce(conf->log, state);
NeilBrown72626682005-09-09 16:23:54 -07008041}
NeilBrownb15c2e52006-01-06 00:20:16 -08008042
NeilBrownfd01b882011-10-11 16:47:53 +11008043static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11008044{
NeilBrowne373ab12011-10-11 16:48:59 +11008045 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07008046 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11008047
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008048 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11008049 if (raid0_conf->nr_strip_zones > 1) {
NeilBrowncc6167b2016-11-02 14:16:50 +11008050 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
8051 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008052 return ERR_PTR(-EINVAL);
8053 }
8054
NeilBrowne373ab12011-10-11 16:48:59 +11008055 sectors = raid0_conf->strip_zone[0].zone_end;
8056 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10008057 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008058 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11008059 mddev->new_layout = ALGORITHM_PARITY_N;
8060 mddev->new_chunk_sectors = mddev->chunk_sectors;
8061 mddev->raid_disks += 1;
8062 mddev->delta_disks = 1;
8063 /* make sure it will be not marked as dirty */
8064 mddev->recovery_cp = MaxSector;
8065
8066 return setup_conf(mddev);
8067}
8068
NeilBrownfd01b882011-10-11 16:47:53 +11008069static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11008070{
8071 int chunksect;
Shaohua Li6995f0b2016-12-08 15:48:17 -08008072 void *ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11008073
8074 if (mddev->raid_disks != 2 ||
8075 mddev->degraded > 1)
8076 return ERR_PTR(-EINVAL);
8077
8078 /* Should check if there are write-behind devices? */
8079
8080 chunksect = 64*2; /* 64K by default */
8081
8082 /* The array must be an exact multiple of chunksize */
8083 while (chunksect && (mddev->array_sectors & (chunksect-1)))
8084 chunksect >>= 1;
8085
8086 if ((chunksect<<9) < STRIPE_SIZE)
8087 /* array size does not allow a suitable chunk size */
8088 return ERR_PTR(-EINVAL);
8089
8090 mddev->new_level = 5;
8091 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10008092 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11008093
Shaohua Li6995f0b2016-12-08 15:48:17 -08008094 ret = setup_conf(mddev);
Jes Sorensen32cd7cb2017-01-06 19:31:35 -05008095 if (!IS_ERR(ret))
Shaohua Li394ed8e2017-01-04 16:10:19 -08008096 mddev_clear_unsupported_flags(mddev,
8097 UNSUPPORTED_MDDEV_FLAGS);
Shaohua Li6995f0b2016-12-08 15:48:17 -08008098 return ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11008099}
8100
NeilBrownfd01b882011-10-11 16:47:53 +11008101static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11008102{
8103 int new_layout;
8104
8105 switch (mddev->layout) {
8106 case ALGORITHM_LEFT_ASYMMETRIC_6:
8107 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
8108 break;
8109 case ALGORITHM_RIGHT_ASYMMETRIC_6:
8110 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
8111 break;
8112 case ALGORITHM_LEFT_SYMMETRIC_6:
8113 new_layout = ALGORITHM_LEFT_SYMMETRIC;
8114 break;
8115 case ALGORITHM_RIGHT_SYMMETRIC_6:
8116 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
8117 break;
8118 case ALGORITHM_PARITY_0_6:
8119 new_layout = ALGORITHM_PARITY_0;
8120 break;
8121 case ALGORITHM_PARITY_N:
8122 new_layout = ALGORITHM_PARITY_N;
8123 break;
8124 default:
8125 return ERR_PTR(-EINVAL);
8126 }
8127 mddev->new_level = 5;
8128 mddev->new_layout = new_layout;
8129 mddev->delta_disks = -1;
8130 mddev->raid_disks -= 1;
8131 return setup_conf(mddev);
8132}
8133
NeilBrownfd01b882011-10-11 16:47:53 +11008134static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11008135{
NeilBrown88ce4932009-03-31 15:24:23 +11008136 /* For a 2-drive array, the layout and chunk size can be changed
8137 * immediately as not restriping is needed.
8138 * For larger arrays we record the new value - after validation
8139 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11008140 */
NeilBrownd1688a62011-10-11 16:49:52 +11008141 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10008142 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11008143
NeilBrown597a7112009-06-18 08:47:42 +10008144 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11008145 return -EINVAL;
8146 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008147 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11008148 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008149 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11008150 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008151 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11008152 /* not factor of array size */
8153 return -EINVAL;
8154 }
8155
8156 /* They look valid */
8157
NeilBrown88ce4932009-03-31 15:24:23 +11008158 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10008159 /* can make the change immediately */
8160 if (mddev->new_layout >= 0) {
8161 conf->algorithm = mddev->new_layout;
8162 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11008163 }
8164 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10008165 conf->chunk_sectors = new_chunk ;
8166 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11008167 }
Shaohua Li29530792016-12-08 15:48:19 -08008168 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown88ce4932009-03-31 15:24:23 +11008169 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11008170 }
NeilBrown50ac1682009-06-18 08:47:55 +10008171 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11008172}
8173
NeilBrownfd01b882011-10-11 16:47:53 +11008174static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11008175{
NeilBrown597a7112009-06-18 08:47:42 +10008176 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10008177
NeilBrown597a7112009-06-18 08:47:42 +10008178 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11008179 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008180 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008181 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11008182 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008183 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11008184 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008185 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11008186 /* not factor of array size */
8187 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008188 }
NeilBrown88ce4932009-03-31 15:24:23 +11008189
8190 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10008191 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11008192}
8193
NeilBrownfd01b882011-10-11 16:47:53 +11008194static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11008195{
8196 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008197 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008198 * raid1 - if there are two drives. We need to know the chunk size
8199 * raid4 - trivial - just use a raid4 layout.
8200 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008201 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008202 if (mddev->level == 0)
8203 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11008204 if (mddev->level == 1)
8205 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11008206 if (mddev->level == 4) {
8207 mddev->new_layout = ALGORITHM_PARITY_N;
8208 mddev->new_level = 5;
8209 return setup_conf(mddev);
8210 }
NeilBrownfc9739c2009-03-31 14:57:20 +11008211 if (mddev->level == 6)
8212 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11008213
8214 return ERR_PTR(-EINVAL);
8215}
8216
NeilBrownfd01b882011-10-11 16:47:53 +11008217static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11008218{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008219 /* raid4 can take over:
8220 * raid0 - if there is only one strip zone
8221 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11008222 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008223 if (mddev->level == 0)
8224 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11008225 if (mddev->level == 5 &&
8226 mddev->layout == ALGORITHM_PARITY_N) {
8227 mddev->new_layout = 0;
8228 mddev->new_level = 4;
8229 return setup_conf(mddev);
8230 }
8231 return ERR_PTR(-EINVAL);
8232}
NeilBrownd562b0c2009-03-31 14:39:39 +11008233
NeilBrown84fc4b52011-10-11 16:49:58 +11008234static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11008235
NeilBrownfd01b882011-10-11 16:47:53 +11008236static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11008237{
8238 /* Currently can only take over a raid5. We map the
8239 * personality to an equivalent raid6 personality
8240 * with the Q block at the end.
8241 */
8242 int new_layout;
8243
8244 if (mddev->pers != &raid5_personality)
8245 return ERR_PTR(-EINVAL);
8246 if (mddev->degraded > 1)
8247 return ERR_PTR(-EINVAL);
8248 if (mddev->raid_disks > 253)
8249 return ERR_PTR(-EINVAL);
8250 if (mddev->raid_disks < 3)
8251 return ERR_PTR(-EINVAL);
8252
8253 switch (mddev->layout) {
8254 case ALGORITHM_LEFT_ASYMMETRIC:
8255 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
8256 break;
8257 case ALGORITHM_RIGHT_ASYMMETRIC:
8258 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
8259 break;
8260 case ALGORITHM_LEFT_SYMMETRIC:
8261 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
8262 break;
8263 case ALGORITHM_RIGHT_SYMMETRIC:
8264 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
8265 break;
8266 case ALGORITHM_PARITY_0:
8267 new_layout = ALGORITHM_PARITY_0_6;
8268 break;
8269 case ALGORITHM_PARITY_N:
8270 new_layout = ALGORITHM_PARITY_N;
8271 break;
8272 default:
8273 return ERR_PTR(-EINVAL);
8274 }
8275 mddev->new_level = 6;
8276 mddev->new_layout = new_layout;
8277 mddev->delta_disks = 1;
8278 mddev->raid_disks += 1;
8279 return setup_conf(mddev);
8280}
8281
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01008282static int raid5_change_consistency_policy(struct mddev *mddev, const char *buf)
8283{
8284 struct r5conf *conf;
8285 int err;
8286
8287 err = mddev_lock(mddev);
8288 if (err)
8289 return err;
8290 conf = mddev->private;
8291 if (!conf) {
8292 mddev_unlock(mddev);
8293 return -ENODEV;
8294 }
8295
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02008296 if (strncmp(buf, "ppl", 3) == 0) {
Song Liu0bb0c102017-03-27 10:51:33 -07008297 /* ppl only works with RAID 5 */
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02008298 if (!raid5_has_ppl(conf) && conf->level == 5) {
8299 err = log_init(conf, NULL, true);
8300 if (!err) {
8301 err = resize_stripes(conf, conf->pool_size);
8302 if (err)
8303 log_exit(conf);
8304 }
Song Liu0bb0c102017-03-27 10:51:33 -07008305 } else
8306 err = -EINVAL;
8307 } else if (strncmp(buf, "resync", 6) == 0) {
8308 if (raid5_has_ppl(conf)) {
8309 mddev_suspend(mddev);
8310 log_exit(conf);
Song Liu0bb0c102017-03-27 10:51:33 -07008311 mddev_resume(mddev);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02008312 err = resize_stripes(conf, conf->pool_size);
Song Liu0bb0c102017-03-27 10:51:33 -07008313 } else if (test_bit(MD_HAS_JOURNAL, &conf->mddev->flags) &&
8314 r5l_log_disk_error(conf)) {
8315 bool journal_dev_exists = false;
8316 struct md_rdev *rdev;
8317
8318 rdev_for_each(rdev, mddev)
8319 if (test_bit(Journal, &rdev->flags)) {
8320 journal_dev_exists = true;
8321 break;
8322 }
8323
8324 if (!journal_dev_exists) {
8325 mddev_suspend(mddev);
8326 clear_bit(MD_HAS_JOURNAL, &mddev->flags);
8327 mddev_resume(mddev);
8328 } else /* need remove journal device first */
8329 err = -EBUSY;
8330 } else
8331 err = -EINVAL;
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01008332 } else {
8333 err = -EINVAL;
8334 }
8335
8336 if (!err)
8337 md_update_sb(mddev, 1);
8338
8339 mddev_unlock(mddev);
8340
8341 return err;
8342}
8343
NeilBrown84fc4b52011-10-11 16:49:58 +11008344static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07008345{
8346 .name = "raid6",
8347 .level = 6,
8348 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008349 .make_request = raid5_make_request,
8350 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11008351 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008352 .status = raid5_status,
8353 .error_handler = raid5_error,
NeilBrown16a53ec2006-06-26 00:27:38 -07008354 .hot_add_disk = raid5_add_disk,
8355 .hot_remove_disk= raid5_remove_disk,
8356 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008357 .sync_request = raid5_sync_request,
NeilBrown16a53ec2006-06-26 00:27:38 -07008358 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008359 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10008360 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08008361 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008362 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07008363 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11008364 .takeover = raid6_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008365 .congested = raid5_congested,
Song Liu0bb0c102017-03-27 10:51:33 -07008366 .change_consistency_policy = raid5_change_consistency_policy,
NeilBrown16a53ec2006-06-26 00:27:38 -07008367};
NeilBrown84fc4b52011-10-11 16:49:58 +11008368static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008369{
8370 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08008371 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008372 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008373 .make_request = raid5_make_request,
8374 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11008375 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008376 .status = raid5_status,
8377 .error_handler = raid5_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008378 .hot_add_disk = raid5_add_disk,
8379 .hot_remove_disk= raid5_remove_disk,
8380 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008381 .sync_request = raid5_sync_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008382 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008383 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08008384 .check_reshape = raid5_check_reshape,
8385 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008386 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07008387 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11008388 .takeover = raid5_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008389 .congested = raid5_congested,
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01008390 .change_consistency_policy = raid5_change_consistency_policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008391};
8392
NeilBrown84fc4b52011-10-11 16:49:58 +11008393static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394{
NeilBrown2604b702006-01-06 00:20:36 -08008395 .name = "raid4",
8396 .level = 4,
8397 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008398 .make_request = raid5_make_request,
8399 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11008400 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008401 .status = raid5_status,
8402 .error_handler = raid5_error,
NeilBrown2604b702006-01-06 00:20:36 -08008403 .hot_add_disk = raid5_add_disk,
8404 .hot_remove_disk= raid5_remove_disk,
8405 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008406 .sync_request = raid5_sync_request,
NeilBrown2604b702006-01-06 00:20:36 -08008407 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008408 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08008409 .check_reshape = raid5_check_reshape,
8410 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008411 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08008412 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11008413 .takeover = raid4_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008414 .congested = raid5_congested,
Song Liu0bb0c102017-03-27 10:51:33 -07008415 .change_consistency_policy = raid5_change_consistency_policy,
NeilBrown2604b702006-01-06 00:20:36 -08008416};
8417
8418static int __init raid5_init(void)
8419{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008420 int ret;
8421
Shaohua Li851c30c2013-08-28 14:30:16 +08008422 raid5_wq = alloc_workqueue("raid5wq",
8423 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
8424 if (!raid5_wq)
8425 return -ENOMEM;
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008426
8427 ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
8428 "md/raid5:prepare",
8429 raid456_cpu_up_prepare,
8430 raid456_cpu_dead);
8431 if (ret) {
8432 destroy_workqueue(raid5_wq);
8433 return ret;
8434 }
NeilBrown16a53ec2006-06-26 00:27:38 -07008435 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008436 register_md_personality(&raid5_personality);
8437 register_md_personality(&raid4_personality);
8438 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439}
8440
NeilBrown2604b702006-01-06 00:20:36 -08008441static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442{
NeilBrown16a53ec2006-06-26 00:27:38 -07008443 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008444 unregister_md_personality(&raid5_personality);
8445 unregister_md_personality(&raid4_personality);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008446 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
Shaohua Li851c30c2013-08-28 14:30:16 +08008447 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008448}
8449
8450module_init(raid5_init);
8451module_exit(raid5_exit);
8452MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008453MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008454MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08008455MODULE_ALIAS("md-raid5");
8456MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08008457MODULE_ALIAS("md-level-5");
8458MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07008459MODULE_ALIAS("md-personality-8"); /* RAID6 */
8460MODULE_ALIAS("md-raid6");
8461MODULE_ALIAS("md-level-6");
8462
8463/* This used to be two separate modules, they were: */
8464MODULE_ALIAS("raid5");
8465MODULE_ALIAS("raid6");