blob: b62f671a93aba2452b487ff41fdb21824fc0a7ec [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>
NeilBrowna9add5d2012-10-31 11:59:09 +110058#include <trace/events/block.h>
59
NeilBrown43b2e5d2009-03-31 14:33:13 +110060#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110061#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110062#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110063#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070064
Shaohua Li394ed8e2017-01-04 16:10:19 -080065#define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED)
66
Shaohua Li851c30c2013-08-28 14:30:16 +080067#define cpu_to_group(cpu) cpu_to_node(cpu)
68#define ANY_GROUP NUMA_NO_NODE
69
NeilBrown8e0e99b2014-10-02 13:45:00 +100070static bool devices_handle_discard_safely = false;
71module_param(devices_handle_discard_safely, bool, 0644);
72MODULE_PARM_DESC(devices_handle_discard_safely,
73 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
Shaohua Li851c30c2013-08-28 14:30:16 +080074static struct workqueue_struct *raid5_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
NeilBrownd1688a62011-10-11 16:49:52 +110076static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110077{
78 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
79 return &conf->stripe_hashtbl[hash];
80}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Shaohua Li566c09c2013-11-14 15:16:17 +110082static inline int stripe_hash_locks_hash(sector_t sect)
83{
84 return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK;
85}
86
87static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
88{
89 spin_lock_irq(conf->hash_locks + hash);
90 spin_lock(&conf->device_lock);
91}
92
93static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
94{
95 spin_unlock(&conf->device_lock);
96 spin_unlock_irq(conf->hash_locks + hash);
97}
98
99static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
100{
101 int i;
102 local_irq_disable();
103 spin_lock(conf->hash_locks);
104 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
105 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
106 spin_lock(&conf->device_lock);
107}
108
109static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
110{
111 int i;
112 spin_unlock(&conf->device_lock);
113 for (i = NR_STRIPE_HASH_LOCKS; i; i--)
114 spin_unlock(conf->hash_locks + i - 1);
115 local_irq_enable();
116}
117
NeilBrownd0dabf72009-03-31 14:39:38 +1100118/* Find first data disk in a raid6 stripe */
119static inline int raid6_d0(struct stripe_head *sh)
120{
NeilBrown67cc2b82009-03-31 14:39:38 +1100121 if (sh->ddf_layout)
122 /* ddf always start from first device */
123 return 0;
124 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100125 if (sh->qd_idx == sh->disks - 1)
126 return 0;
127 else
128 return sh->qd_idx + 1;
129}
NeilBrown16a53ec2006-06-26 00:27:38 -0700130static inline int raid6_next_disk(int disk, int raid_disks)
131{
132 disk++;
133 return (disk < raid_disks) ? disk : 0;
134}
Dan Williamsa4456852007-07-09 11:56:43 -0700135
NeilBrownd0dabf72009-03-31 14:39:38 +1100136/* When walking through the disks in a raid5, starting at raid6_d0,
137 * We need to map each disk to a 'slot', where the data disks are slot
138 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
139 * is raid_disks-1. This help does that mapping.
140 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100141static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
142 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100143{
Dan Williams66295422009-10-19 18:09:32 -0700144 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100145
NeilBrowne4424fe2009-10-16 16:27:34 +1100146 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700147 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100148 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100149 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100150 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100151 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100152 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700153 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100154 return slot;
155}
156
NeilBrown34a6f802015-08-14 12:07:57 +1000157static void return_io(struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -0700158{
NeilBrown34a6f802015-08-14 12:07:57 +1000159 struct bio *bi;
160 while ((bi = bio_list_pop(return_bi)) != NULL) {
Kent Overstreet4f024f32013-10-11 15:44:27 -0700161 bi->bi_iter.bi_size = 0;
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700162 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
163 bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200164 bio_endio(bi);
Dan Williamsa4456852007-07-09 11:56:43 -0700165 }
166}
167
NeilBrownd1688a62011-10-11 16:49:52 +1100168static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Dan Williams600aa102008-06-28 08:32:05 +1000170static int stripe_operations_active(struct stripe_head *sh)
171{
172 return sh->check_state || sh->reconstruct_state ||
173 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
174 test_bit(STRIPE_COMPUTE_RUN, &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);
192 list_add_tail(&sh->lru, &group->handle_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800193 group->stripes_cnt++;
194 sh->group = group;
Shaohua Li851c30c2013-08-28 14:30:16 +0800195 }
196
197 if (conf->worker_cnt_per_group == 0) {
198 md_wakeup_thread(conf->mddev->thread);
199 return;
200 }
201
202 group = conf->worker_groups + cpu_to_group(sh->cpu);
203
Shaohua Libfc90cb2013-08-29 15:40:32 +0800204 group->workers[0].working = true;
205 /* at least one worker should run to avoid race */
206 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
207
208 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
209 /* wakeup more workers */
210 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
211 if (group->workers[i].working == false) {
212 group->workers[i].working = true;
213 queue_work_on(sh->cpu, raid5_wq,
214 &group->workers[i].work);
215 thread_cnt--;
216 }
217 }
Shaohua Li851c30c2013-08-28 14:30:16 +0800218}
219
Shaohua Li566c09c2013-11-14 15:16:17 +1100220static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
221 struct list_head *temp_inactive_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Song Liu1e6d6902016-11-17 15:24:39 -0800223 int i;
224 int injournal = 0; /* number of date pages with R5_InJournal */
225
Shaohua Li4eb788d2012-07-19 16:01:31 +1000226 BUG_ON(!list_empty(&sh->lru));
227 BUG_ON(atomic_read(&conf->active_stripes)==0);
Song Liu1e6d6902016-11-17 15:24:39 -0800228
229 if (r5c_is_writeback(conf->log))
230 for (i = sh->disks; i--; )
231 if (test_bit(R5_InJournal, &sh->dev[i].flags))
232 injournal++;
Song Liua39f7af2016-11-17 15:24:40 -0800233 /*
234 * When quiesce in r5c write back, set STRIPE_HANDLE for stripes with
235 * data in journal, so they are not released to cached lists
236 */
237 if (conf->quiesce && r5c_is_writeback(conf->log) &&
238 !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0) {
239 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
240 r5c_make_stripe_write_out(sh);
241 set_bit(STRIPE_HANDLE, &sh->state);
242 }
Song Liu1e6d6902016-11-17 15:24:39 -0800243
Shaohua Li4eb788d2012-07-19 16:01:31 +1000244 if (test_bit(STRIPE_HANDLE, &sh->state)) {
245 if (test_bit(STRIPE_DELAYED, &sh->state) &&
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500246 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
Shaohua Li4eb788d2012-07-19 16:01:31 +1000247 list_add_tail(&sh->lru, &conf->delayed_list);
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500248 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
Shaohua Li4eb788d2012-07-19 16:01:31 +1000249 sh->bm_seq - conf->seq_write > 0)
250 list_add_tail(&sh->lru, &conf->bitmap_list);
251 else {
252 clear_bit(STRIPE_DELAYED, &sh->state);
253 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Shaohua Li851c30c2013-08-28 14:30:16 +0800254 if (conf->worker_cnt_per_group == 0) {
255 list_add_tail(&sh->lru, &conf->handle_list);
256 } else {
257 raid5_wakeup_stripe_thread(sh);
258 return;
259 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000260 }
261 md_wakeup_thread(conf->mddev->thread);
262 } else {
263 BUG_ON(stripe_operations_active(sh));
264 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
265 if (atomic_dec_return(&conf->preread_active_stripes)
266 < IO_THRESHOLD)
267 md_wakeup_thread(conf->mddev->thread);
268 atomic_dec(&conf->active_stripes);
Song Liu1e6d6902016-11-17 15:24:39 -0800269 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
270 if (!r5c_is_writeback(conf->log))
271 list_add_tail(&sh->lru, temp_inactive_list);
272 else {
273 WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags));
274 if (injournal == 0)
275 list_add_tail(&sh->lru, temp_inactive_list);
276 else if (injournal == conf->raid_disks - conf->max_degraded) {
277 /* full stripe */
278 if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state))
279 atomic_inc(&conf->r5c_cached_full_stripes);
280 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state))
281 atomic_dec(&conf->r5c_cached_partial_stripes);
282 list_add_tail(&sh->lru, &conf->r5c_full_stripe_list);
Song Liua39f7af2016-11-17 15:24:40 -0800283 r5c_check_cached_full_stripe(conf);
Song Liu03b047f2017-01-11 13:39:14 -0800284 } else
285 /*
286 * STRIPE_R5C_PARTIAL_STRIPE is set in
287 * r5c_try_caching_write(). No need to
288 * set it again.
289 */
Song Liu1e6d6902016-11-17 15:24:39 -0800290 list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list);
Song Liu1e6d6902016-11-17 15:24:39 -0800291 }
292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294}
NeilBrownd0dabf72009-03-31 14:39:38 +1100295
Shaohua Li566c09c2013-11-14 15:16:17 +1100296static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
297 struct list_head *temp_inactive_list)
Shaohua Li4eb788d2012-07-19 16:01:31 +1000298{
299 if (atomic_dec_and_test(&sh->count))
Shaohua Li566c09c2013-11-14 15:16:17 +1100300 do_release_stripe(conf, sh, temp_inactive_list);
301}
302
303/*
304 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
305 *
306 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
307 * given time. Adding stripes only takes device lock, while deleting stripes
308 * only takes hash lock.
309 */
310static void release_inactive_stripe_list(struct r5conf *conf,
311 struct list_head *temp_inactive_list,
312 int hash)
313{
314 int size;
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800315 bool do_wakeup = false;
Shaohua Li566c09c2013-11-14 15:16:17 +1100316 unsigned long flags;
317
318 if (hash == NR_STRIPE_HASH_LOCKS) {
319 size = NR_STRIPE_HASH_LOCKS;
320 hash = NR_STRIPE_HASH_LOCKS - 1;
321 } else
322 size = 1;
323 while (size) {
324 struct list_head *list = &temp_inactive_list[size - 1];
325
326 /*
Shaohua Li6d036f72015-08-13 14:31:57 -0700327 * We don't hold any lock here yet, raid5_get_active_stripe() might
Shaohua Li566c09c2013-11-14 15:16:17 +1100328 * remove stripes from the list
329 */
330 if (!list_empty_careful(list)) {
331 spin_lock_irqsave(conf->hash_locks + hash, flags);
Shaohua Li4bda5562013-11-14 15:16:17 +1100332 if (list_empty(conf->inactive_list + hash) &&
333 !list_empty(list))
334 atomic_dec(&conf->empty_inactive_list_nr);
Shaohua Li566c09c2013-11-14 15:16:17 +1100335 list_splice_tail_init(list, conf->inactive_list + hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800336 do_wakeup = true;
Shaohua Li566c09c2013-11-14 15:16:17 +1100337 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
338 }
339 size--;
340 hash--;
341 }
342
343 if (do_wakeup) {
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800344 wake_up(&conf->wait_for_stripe);
Yuanhan Liub1b46482015-05-08 18:19:06 +1000345 if (atomic_read(&conf->active_stripes) == 0)
346 wake_up(&conf->wait_for_quiescent);
Shaohua Li566c09c2013-11-14 15:16:17 +1100347 if (conf->retry_read_aligned)
348 md_wakeup_thread(conf->mddev->thread);
349 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000350}
351
Shaohua Li773ca822013-08-27 17:50:39 +0800352/* should hold conf->device_lock already */
Shaohua Li566c09c2013-11-14 15:16:17 +1100353static int release_stripe_list(struct r5conf *conf,
354 struct list_head *temp_inactive_list)
Shaohua Li773ca822013-08-27 17:50:39 +0800355{
356 struct stripe_head *sh;
357 int count = 0;
358 struct llist_node *head;
359
360 head = llist_del_all(&conf->released_stripes);
Shaohua Lid265d9d2013-08-28 14:29:05 +0800361 head = llist_reverse_order(head);
Shaohua Li773ca822013-08-27 17:50:39 +0800362 while (head) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100363 int hash;
364
Shaohua Li773ca822013-08-27 17:50:39 +0800365 sh = llist_entry(head, struct stripe_head, release_list);
366 head = llist_next(head);
367 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
368 smp_mb();
369 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
370 /*
371 * Don't worry the bit is set here, because if the bit is set
372 * again, the count is always > 1. This is true for
373 * STRIPE_ON_UNPLUG_LIST bit too.
374 */
Shaohua Li566c09c2013-11-14 15:16:17 +1100375 hash = sh->hash_lock_index;
376 __release_stripe(conf, sh, &temp_inactive_list[hash]);
Shaohua Li773ca822013-08-27 17:50:39 +0800377 count++;
378 }
379
380 return count;
381}
382
Shaohua Li6d036f72015-08-13 14:31:57 -0700383void raid5_release_stripe(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
NeilBrownd1688a62011-10-11 16:49:52 +1100385 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 unsigned long flags;
Shaohua Li566c09c2013-11-14 15:16:17 +1100387 struct list_head list;
388 int hash;
Shaohua Li773ca822013-08-27 17:50:39 +0800389 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700390
Eivind Sartocf170f32014-05-28 13:39:23 +1000391 /* Avoid release_list until the last reference.
392 */
393 if (atomic_add_unless(&sh->count, -1, 1))
394 return;
395
majianpengad4068d2013-11-14 15:16:15 +1100396 if (unlikely(!conf->mddev->thread) ||
397 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
Shaohua Li773ca822013-08-27 17:50:39 +0800398 goto slow_path;
399 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
400 if (wakeup)
401 md_wakeup_thread(conf->mddev->thread);
402 return;
403slow_path:
Shaohua Li4eb788d2012-07-19 16:01:31 +1000404 local_irq_save(flags);
Shaohua Li773ca822013-08-27 17:50:39 +0800405 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Shaohua Li4eb788d2012-07-19 16:01:31 +1000406 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100407 INIT_LIST_HEAD(&list);
408 hash = sh->hash_lock_index;
409 do_release_stripe(conf, sh, &list);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000410 spin_unlock(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +1100411 release_inactive_stripe_list(conf, &list, hash);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000412 }
413 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
NeilBrownfccddba2006-01-06 00:20:33 -0800416static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Dan Williams45b42332007-07-09 11:56:43 -0700418 pr_debug("remove_hash(), stripe %llu\n",
419 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
NeilBrownfccddba2006-01-06 00:20:33 -0800421 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
NeilBrownd1688a62011-10-11 16:49:52 +1100424static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
NeilBrownfccddba2006-01-06 00:20:33 -0800426 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Dan Williams45b42332007-07-09 11:56:43 -0700428 pr_debug("insert_hash(), stripe %llu\n",
429 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
NeilBrownfccddba2006-01-06 00:20:33 -0800431 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/* find an idle stripe, make sure it is unhashed, and return it. */
Shaohua Li566c09c2013-11-14 15:16:17 +1100435static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 struct stripe_head *sh = NULL;
438 struct list_head *first;
439
Shaohua Li566c09c2013-11-14 15:16:17 +1100440 if (list_empty(conf->inactive_list + hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 goto out;
Shaohua Li566c09c2013-11-14 15:16:17 +1100442 first = (conf->inactive_list + hash)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 sh = list_entry(first, struct stripe_head, lru);
444 list_del_init(first);
445 remove_hash(sh);
446 atomic_inc(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100447 BUG_ON(hash != sh->hash_lock_index);
Shaohua Li4bda5562013-11-14 15:16:17 +1100448 if (list_empty(conf->inactive_list + hash))
449 atomic_inc(&conf->empty_inactive_list_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450out:
451 return sh;
452}
453
NeilBrowne4e11e32010-06-16 16:45:16 +1000454static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 struct page *p;
457 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000458 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
NeilBrowne4e11e32010-06-16 16:45:16 +1000460 for (i = 0; i < num ; i++) {
Shaohua Lid592a992014-05-21 17:57:44 +0800461 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 p = sh->dev[i].page;
463 if (!p)
464 continue;
465 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800466 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468}
469
NeilBrowna9683a72015-02-25 12:02:51 +1100470static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000473 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
NeilBrowne4e11e32010-06-16 16:45:16 +1000475 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 struct page *page;
477
NeilBrowna9683a72015-02-25 12:02:51 +1100478 if (!(page = alloc_page(gfp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 1;
480 }
481 sh->dev[i].page = page;
Shaohua Lid592a992014-05-21 17:57:44 +0800482 sh->dev[i].orig_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484 return 0;
485}
486
NeilBrown784052e2009-03-31 15:19:07 +1100487static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100488static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100489 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
NeilBrownb5663ba2009-03-31 14:39:38 +1100491static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
NeilBrownd1688a62011-10-11 16:49:52 +1100493 struct r5conf *conf = sh->raid_conf;
Shaohua Li566c09c2013-11-14 15:16:17 +1100494 int i, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200496 BUG_ON(atomic_read(&sh->count) != 0);
497 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000498 BUG_ON(stripe_operations_active(sh));
shli@kernel.org59fc6302014-12-15 12:57:03 +1100499 BUG_ON(sh->batch_head);
Dan Williamsd84e0f12007-01-02 13:52:30 -0700500
Dan Williams45b42332007-07-09 11:56:43 -0700501 pr_debug("init_stripe called, stripe %llu\n",
Markus Stockhausenb8e6a152014-08-23 20:19:27 +1000502 (unsigned long long)sector);
Shaohua Li566c09c2013-11-14 15:16:17 +1100503retry:
504 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100505 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100506 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100508 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 sh->state = 0;
510
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800511 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 struct r5dev *dev = &sh->dev[i];
513
Dan Williamsd84e0f12007-01-02 13:52:30 -0700514 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 test_bit(R5_LOCKED, &dev->flags)) {
NeilBrowncc6167b2016-11-02 14:16:50 +1100516 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700518 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000520 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100523 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100525 if (read_seqcount_retry(&conf->gen_lock, seq))
526 goto retry;
shli@kernel.org7a87f432014-12-15 12:57:03 +1100527 sh->overwrite_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800529 sh->cpu = smp_processor_id();
shli@kernel.orgda41ba62014-12-15 12:57:03 +1100530 set_bit(STRIPE_BATCH_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
NeilBrownd1688a62011-10-11 16:49:52 +1100533static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100534 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 struct stripe_head *sh;
537
Dan Williams45b42332007-07-09 11:56:43 -0700538 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800539 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100540 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700542 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return NULL;
544}
545
NeilBrown674806d2010-06-16 17:17:53 +1000546/*
547 * Need to check if array has failed when deciding whether to:
548 * - start an array
549 * - remove non-faulty devices
550 * - add a spare
551 * - allow a reshape
552 * This determination is simple when no reshape is happening.
553 * However if there is a reshape, we need to carefully check
554 * both the before and after sections.
555 * This is because some failed devices may only affect one
556 * of the two sections, and some non-in_sync devices may
557 * be insync in the section most affected by failed devices.
558 */
Song Liu2e38a372017-01-24 10:45:30 -0800559int raid5_calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000560{
NeilBrown908f4fb2011-12-23 10:17:50 +1100561 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000562 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000563
564 rcu_read_lock();
565 degraded = 0;
566 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100567 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000568 if (rdev && test_bit(Faulty, &rdev->flags))
569 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000570 if (!rdev || test_bit(Faulty, &rdev->flags))
571 degraded++;
572 else if (test_bit(In_sync, &rdev->flags))
573 ;
574 else
575 /* not in-sync or faulty.
576 * If the reshape increases the number of devices,
577 * this is being recovered by the reshape, so
578 * this 'previous' section is not in_sync.
579 * If the number of devices is being reduced however,
580 * the device can only be part of the array if
581 * we are reverting a reshape, so this section will
582 * be in-sync.
583 */
584 if (conf->raid_disks >= conf->previous_raid_disks)
585 degraded++;
586 }
587 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100588 if (conf->raid_disks == conf->previous_raid_disks)
589 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000590 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100591 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000592 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100593 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000594 if (rdev && test_bit(Faulty, &rdev->flags))
595 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000596 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100597 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000598 else if (test_bit(In_sync, &rdev->flags))
599 ;
600 else
601 /* not in-sync or faulty.
602 * If reshape increases the number of devices, this
603 * section has already been recovered, else it
604 * almost certainly hasn't.
605 */
606 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100607 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000608 }
609 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100610 if (degraded2 > degraded)
611 return degraded2;
612 return degraded;
613}
614
615static int has_failed(struct r5conf *conf)
616{
617 int degraded;
618
619 if (conf->mddev->reshape_position == MaxSector)
620 return conf->mddev->degraded > conf->max_degraded;
621
Song Liu2e38a372017-01-24 10:45:30 -0800622 degraded = raid5_calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000623 if (degraded > conf->max_degraded)
624 return 1;
625 return 0;
626}
627
Shaohua Li6d036f72015-08-13 14:31:57 -0700628struct stripe_head *
629raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
630 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 struct stripe_head *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +1100633 int hash = stripe_hash_locks_hash(sector);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800634 int inc_empty_inactive_list_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Dan Williams45b42332007-07-09 11:56:43 -0700636 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Shaohua Li566c09c2013-11-14 15:16:17 +1100638 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 do {
Yuanhan Liub1b46482015-05-08 18:19:06 +1000641 wait_event_lock_irq(conf->wait_for_quiescent,
NeilBrowna8c906c2009-06-09 14:39:59 +1000642 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100643 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100644 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (!sh) {
NeilBrownedbe83a2015-02-26 12:47:56 +1100646 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100647 sh = get_free_stripe(conf, hash);
Shaohua Li713bc5c2015-05-28 17:33:47 -0700648 if (!sh && !test_bit(R5_DID_ALLOC,
649 &conf->cache_state))
NeilBrownedbe83a2015-02-26 12:47:56 +1100650 set_bit(R5_ALLOC_MORE,
651 &conf->cache_state);
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (noblock && sh == NULL)
654 break;
Song Liua39f7af2016-11-17 15:24:40 -0800655
656 r5c_check_stripe_cache_usage(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (!sh) {
NeilBrown54233992015-02-26 12:21:04 +1100658 set_bit(R5_INACTIVE_BLOCKED,
659 &conf->cache_state);
Song Liua39f7af2016-11-17 15:24:40 -0800660 r5l_wake_reclaim(conf->log, 0);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800661 wait_event_lock_irq(
662 conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +1100663 !list_empty(conf->inactive_list + hash) &&
664 (atomic_read(&conf->active_stripes)
665 < (conf->max_nr_stripes * 3 / 4)
NeilBrown54233992015-02-26 12:21:04 +1100666 || !test_bit(R5_INACTIVE_BLOCKED,
667 &conf->cache_state)),
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800668 *(conf->hash_locks + hash));
NeilBrown54233992015-02-26 12:21:04 +1100669 clear_bit(R5_INACTIVE_BLOCKED,
670 &conf->cache_state);
NeilBrown7da9d452014-01-22 11:45:03 +1100671 } else {
NeilBrownb5663ba2009-03-31 14:39:38 +1100672 init_stripe(sh, sector, previous);
NeilBrown7da9d452014-01-22 11:45:03 +1100673 atomic_inc(&sh->count);
674 }
Shaohua Lie240c182014-04-09 11:27:42 +0800675 } else if (!atomic_inc_not_zero(&sh->count)) {
NeilBrown6d183de2013-11-28 10:55:27 +1100676 spin_lock(&conf->device_lock);
Shaohua Lie240c182014-04-09 11:27:42 +0800677 if (!atomic_read(&sh->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (!test_bit(STRIPE_HANDLE, &sh->state))
679 atomic_inc(&conf->active_stripes);
NeilBrown5af9bef2014-01-14 15:16:10 +1100680 BUG_ON(list_empty(&sh->lru) &&
681 !test_bit(STRIPE_EXPANDING, &sh->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800682 inc_empty_inactive_list_flag = 0;
683 if (!list_empty(conf->inactive_list + hash))
684 inc_empty_inactive_list_flag = 1;
NeilBrown16a53ec2006-06-26 00:27:38 -0700685 list_del_init(&sh->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800686 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
687 atomic_inc(&conf->empty_inactive_list_nr);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800688 if (sh->group) {
689 sh->group->stripes_cnt--;
690 sh->group = NULL;
691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
NeilBrown7da9d452014-01-22 11:45:03 +1100693 atomic_inc(&sh->count);
NeilBrown6d183de2013-11-28 10:55:27 +1100694 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696 } while (sh == NULL);
697
Shaohua Li566c09c2013-11-14 15:16:17 +1100698 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return sh;
700}
701
shli@kernel.org7a87f432014-12-15 12:57:03 +1100702static bool is_full_stripe_write(struct stripe_head *sh)
703{
704 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
705 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
706}
707
shli@kernel.org59fc6302014-12-15 12:57:03 +1100708static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
709{
710 local_irq_disable();
711 if (sh1 > sh2) {
712 spin_lock(&sh2->stripe_lock);
713 spin_lock_nested(&sh1->stripe_lock, 1);
714 } else {
715 spin_lock(&sh1->stripe_lock);
716 spin_lock_nested(&sh2->stripe_lock, 1);
717 }
718}
719
720static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
721{
722 spin_unlock(&sh1->stripe_lock);
723 spin_unlock(&sh2->stripe_lock);
724 local_irq_enable();
725}
726
727/* Only freshly new full stripe normal write stripe can be added to a batch list */
728static bool stripe_can_batch(struct stripe_head *sh)
729{
Shaohua Li9c3e3332015-08-13 14:32:02 -0700730 struct r5conf *conf = sh->raid_conf;
731
732 if (conf->log)
733 return false;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100734 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
NeilBrownd0852df52015-05-27 08:43:45 +1000735 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
shli@kernel.org59fc6302014-12-15 12:57:03 +1100736 is_full_stripe_write(sh);
737}
738
739/* we only do back search */
740static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
741{
742 struct stripe_head *head;
743 sector_t head_sector, tmp_sec;
744 int hash;
745 int dd_idx;
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800746 int inc_empty_inactive_list_flag;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100747
shli@kernel.org59fc6302014-12-15 12:57:03 +1100748 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
749 tmp_sec = sh->sector;
750 if (!sector_div(tmp_sec, conf->chunk_sectors))
751 return;
752 head_sector = sh->sector - STRIPE_SECTORS;
753
754 hash = stripe_hash_locks_hash(head_sector);
755 spin_lock_irq(conf->hash_locks + hash);
756 head = __find_stripe(conf, head_sector, conf->generation);
757 if (head && !atomic_inc_not_zero(&head->count)) {
758 spin_lock(&conf->device_lock);
759 if (!atomic_read(&head->count)) {
760 if (!test_bit(STRIPE_HANDLE, &head->state))
761 atomic_inc(&conf->active_stripes);
762 BUG_ON(list_empty(&head->lru) &&
763 !test_bit(STRIPE_EXPANDING, &head->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800764 inc_empty_inactive_list_flag = 0;
765 if (!list_empty(conf->inactive_list + hash))
766 inc_empty_inactive_list_flag = 1;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100767 list_del_init(&head->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800768 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
769 atomic_inc(&conf->empty_inactive_list_nr);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100770 if (head->group) {
771 head->group->stripes_cnt--;
772 head->group = NULL;
773 }
774 }
775 atomic_inc(&head->count);
776 spin_unlock(&conf->device_lock);
777 }
778 spin_unlock_irq(conf->hash_locks + hash);
779
780 if (!head)
781 return;
782 if (!stripe_can_batch(head))
783 goto out;
784
785 lock_two_stripes(head, sh);
786 /* clear_batch_ready clear the flag */
787 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
788 goto unlock_out;
789
790 if (sh->batch_head)
791 goto unlock_out;
792
793 dd_idx = 0;
794 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
795 dd_idx++;
Jens Axboe1eff9d32016-08-05 15:35:16 -0600796 if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
Mike Christie796a5cf2016-06-05 14:32:07 -0500797 bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
shli@kernel.org59fc6302014-12-15 12:57:03 +1100798 goto unlock_out;
799
800 if (head->batch_head) {
801 spin_lock(&head->batch_head->batch_lock);
802 /* This batch list is already running */
803 if (!stripe_can_batch(head)) {
804 spin_unlock(&head->batch_head->batch_lock);
805 goto unlock_out;
806 }
807
808 /*
809 * at this point, head's BATCH_READY could be cleared, but we
810 * can still add the stripe to batch list
811 */
812 list_add(&sh->batch_list, &head->batch_list);
813 spin_unlock(&head->batch_head->batch_lock);
814
815 sh->batch_head = head->batch_head;
816 } else {
817 head->batch_head = head;
818 sh->batch_head = head->batch_head;
819 spin_lock(&head->batch_lock);
820 list_add_tail(&sh->batch_list, &head->batch_list);
821 spin_unlock(&head->batch_lock);
822 }
823
824 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
825 if (atomic_dec_return(&conf->preread_active_stripes)
826 < IO_THRESHOLD)
827 md_wakeup_thread(conf->mddev->thread);
828
NeilBrown2b6b2452015-05-21 15:10:01 +1000829 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
830 int seq = sh->bm_seq;
831 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
832 sh->batch_head->bm_seq > seq)
833 seq = sh->batch_head->bm_seq;
834 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
835 sh->batch_head->bm_seq = seq;
836 }
837
shli@kernel.org59fc6302014-12-15 12:57:03 +1100838 atomic_inc(&sh->count);
839unlock_out:
840 unlock_two_stripes(head, sh);
841out:
Shaohua Li6d036f72015-08-13 14:31:57 -0700842 raid5_release_stripe(head);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100843}
844
NeilBrown05616be2012-05-21 09:27:00 +1000845/* Determine if 'data_offset' or 'new_data_offset' should be used
846 * in this stripe_head.
847 */
848static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
849{
850 sector_t progress = conf->reshape_progress;
851 /* Need a memory barrier to make sure we see the value
852 * of conf->generation, or ->data_offset that was set before
853 * reshape_progress was updated.
854 */
855 smp_rmb();
856 if (progress == MaxSector)
857 return 0;
858 if (sh->generation == conf->generation - 1)
859 return 0;
860 /* We are in a reshape, and this is a new-generation stripe,
861 * so use new_data_offset.
862 */
863 return 1;
864}
865
Shaohua Li765d7042017-01-04 09:33:23 -0800866static void flush_deferred_bios(struct r5conf *conf)
867{
868 struct bio_list tmp;
869 struct bio *bio;
870
871 if (!conf->batch_bio_dispatch || !conf->group_cnt)
872 return;
873
874 bio_list_init(&tmp);
875 spin_lock(&conf->pending_bios_lock);
876 bio_list_merge(&tmp, &conf->pending_bios);
877 bio_list_init(&conf->pending_bios);
878 spin_unlock(&conf->pending_bios_lock);
879
880 while ((bio = bio_list_pop(&tmp)))
881 generic_make_request(bio);
882}
883
884static void defer_bio_issue(struct r5conf *conf, struct bio *bio)
885{
886 /*
887 * change group_cnt will drain all bios, so this is safe
888 *
889 * A read generally means a read-modify-write, which usually means a
890 * randwrite, so we don't delay it
891 */
892 if (!conf->batch_bio_dispatch || !conf->group_cnt ||
893 bio_op(bio) == REQ_OP_READ) {
894 generic_make_request(bio);
895 return;
896 }
897 spin_lock(&conf->pending_bios_lock);
898 bio_list_add(&conf->pending_bios, bio);
899 spin_unlock(&conf->pending_bios_lock);
900 md_wakeup_thread(conf->mddev->thread);
901}
902
NeilBrown6712ecf2007-09-27 12:47:43 +0200903static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200904raid5_end_read_request(struct bio *bi);
NeilBrown6712ecf2007-09-27 12:47:43 +0200905static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200906raid5_end_write_request(struct bio *bi);
Dan Williams91c00922007-01-02 13:52:30 -0700907
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000908static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700909{
NeilBrownd1688a62011-10-11 16:49:52 +1100910 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700911 int i, disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100912 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -0700913
914 might_sleep();
915
Song Liu1e6d6902016-11-17 15:24:39 -0800916 if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
917 /* writing out phase */
Song Liud7bd3982016-11-23 22:50:39 -0800918 if (s->waiting_extra_page)
919 return;
Song Liu1e6d6902016-11-17 15:24:39 -0800920 if (r5l_write_stripe(conf->log, sh) == 0)
921 return;
922 } else { /* caching phase */
923 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) {
924 r5c_cache_data(conf->log, sh, s);
925 return;
926 }
927 }
928
Dan Williams91c00922007-01-02 13:52:30 -0700929 for (i = disks; i--; ) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500930 int op, op_flags = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +1100931 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100932 struct bio *bi, *rbi;
933 struct md_rdev *rdev, *rrdev = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100934
935 sh = head_sh;
Tejun Heoe9c74692010-09-03 11:56:18 +0200936 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500937 op = REQ_OP_WRITE;
Tejun Heoe9c74692010-09-03 11:56:18 +0200938 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600939 op_flags = REQ_FUA;
Shaohua Li9e4447682012-10-11 13:49:49 +1100940 if (test_bit(R5_Discard, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -0500941 op = REQ_OP_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200942 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -0500943 op = REQ_OP_READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100944 else if (test_and_clear_bit(R5_WantReplace,
945 &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500946 op = REQ_OP_WRITE;
NeilBrown9a3e1102011-12-23 10:17:53 +1100947 replace_only = 1;
948 } else
Dan Williams91c00922007-01-02 13:52:30 -0700949 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000950 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -0500951 op_flags |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700952
shli@kernel.org59fc6302014-12-15 12:57:03 +1100953again:
Dan Williams91c00922007-01-02 13:52:30 -0700954 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100955 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700956
Dan Williams91c00922007-01-02 13:52:30 -0700957 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100958 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100959 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
960 rdev = rcu_dereference(conf->disks[i].rdev);
961 if (!rdev) {
962 rdev = rrdev;
963 rrdev = NULL;
964 }
Mike Christie796a5cf2016-06-05 14:32:07 -0500965 if (op_is_write(op)) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100966 if (replace_only)
967 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100968 if (rdev == rrdev)
969 /* We raced and saw duplicates */
970 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100971 } else {
shli@kernel.org59fc6302014-12-15 12:57:03 +1100972 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100973 rdev = rrdev;
974 rrdev = NULL;
975 }
NeilBrown977df362011-12-23 10:17:53 +1100976
Dan Williams91c00922007-01-02 13:52:30 -0700977 if (rdev && test_bit(Faulty, &rdev->flags))
978 rdev = NULL;
979 if (rdev)
980 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100981 if (rrdev && test_bit(Faulty, &rrdev->flags))
982 rrdev = NULL;
983 if (rrdev)
984 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700985 rcu_read_unlock();
986
NeilBrown73e92e52011-07-28 11:39:22 +1000987 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100988 * need to check for writes. We never accept write errors
989 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000990 */
Mike Christie796a5cf2016-06-05 14:32:07 -0500991 while (op_is_write(op) && rdev &&
NeilBrown73e92e52011-07-28 11:39:22 +1000992 test_bit(WriteErrorSeen, &rdev->flags)) {
993 sector_t first_bad;
994 int bad_sectors;
995 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
996 &first_bad, &bad_sectors);
997 if (!bad)
998 break;
999
1000 if (bad < 0) {
1001 set_bit(BlockedBadBlocks, &rdev->flags);
1002 if (!conf->mddev->external &&
Shaohua Li29530792016-12-08 15:48:19 -08001003 conf->mddev->sb_flags) {
NeilBrown73e92e52011-07-28 11:39:22 +10001004 /* It is very unlikely, but we might
1005 * still need to write out the
1006 * bad block log - better give it
1007 * a chance*/
1008 md_check_recovery(conf->mddev);
1009 }
majianpeng18507532012-07-03 12:11:54 +10001010 /*
1011 * Because md_wait_for_blocked_rdev
1012 * will dec nr_pending, we must
1013 * increment it first.
1014 */
1015 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +10001016 md_wait_for_blocked_rdev(rdev, conf->mddev);
1017 } else {
1018 /* Acknowledged bad block - skip the write */
1019 rdev_dec_pending(rdev, conf->mddev);
1020 rdev = NULL;
1021 }
1022 }
1023
Dan Williams91c00922007-01-02 13:52:30 -07001024 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001025 if (s->syncing || s->expanding || s->expanded
1026 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -07001027 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1028
Dan Williams2b7497f2008-06-28 08:31:52 +10001029 set_bit(STRIPE_IO_STARTED, &sh->state);
1030
Dan Williams91c00922007-01-02 13:52:30 -07001031 bi->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05001032 bio_set_op_attrs(bi, op, op_flags);
1033 bi->bi_end_io = op_is_write(op)
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001034 ? raid5_end_write_request
1035 : raid5_end_read_request;
1036 bi->bi_private = sh;
1037
Mike Christie6296b962016-06-05 14:32:21 -05001038 pr_debug("%s: for %llu schedule op %d on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001039 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001040 bi->bi_opf, i);
Dan Williams91c00922007-01-02 13:52:30 -07001041 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001042 if (sh != head_sh)
1043 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001044 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001045 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001046 + rdev->new_data_offset);
1047 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001048 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001049 + rdev->data_offset);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001050 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
Jens Axboe1eff9d32016-08-05 15:35:16 -06001051 bi->bi_opf |= REQ_NOMERGE;
majianpeng3f9e7c12012-07-31 10:04:21 +10001052
Shaohua Lid592a992014-05-21 17:57:44 +08001053 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1054 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
Song Liu86aa1392017-01-12 17:22:41 -08001055
1056 if (!op_is_write(op) &&
1057 test_bit(R5_InJournal, &sh->dev[i].flags))
1058 /*
1059 * issuing read for a page in journal, this
1060 * must be preparing for prexor in rmw; read
1061 * the data into orig_page
1062 */
1063 sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
1064 else
1065 sh->dev[i].vec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001066 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001067 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1068 bi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001069 bi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001070 /*
1071 * If this is discard request, set bi_vcnt 0. We don't
1072 * want to confuse SCSI because SCSI will replace payload
1073 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001074 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001075 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +11001076 if (rrdev)
1077 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001078
1079 if (conf->mddev->gendisk)
1080 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
1081 bi, disk_devt(conf->mddev->gendisk),
1082 sh->dev[i].sector);
Shaohua Li765d7042017-01-04 09:33:23 -08001083 defer_bio_issue(conf, bi);
NeilBrown977df362011-12-23 10:17:53 +11001084 }
1085 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001086 if (s->syncing || s->expanding || s->expanded
1087 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +11001088 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
1089
1090 set_bit(STRIPE_IO_STARTED, &sh->state);
1091
1092 rbi->bi_bdev = rrdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05001093 bio_set_op_attrs(rbi, op, op_flags);
1094 BUG_ON(!op_is_write(op));
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001095 rbi->bi_end_io = raid5_end_write_request;
1096 rbi->bi_private = sh;
1097
Mike Christie6296b962016-06-05 14:32:21 -05001098 pr_debug("%s: for %llu schedule op %d on "
NeilBrown977df362011-12-23 10:17:53 +11001099 "replacement disc %d\n",
1100 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001101 rbi->bi_opf, i);
NeilBrown977df362011-12-23 10:17:53 +11001102 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001103 if (sh != head_sh)
1104 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001105 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001106 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001107 + rrdev->new_data_offset);
1108 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001109 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001110 + rrdev->data_offset);
Shaohua Lid592a992014-05-21 17:57:44 +08001111 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1112 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1113 sh->dev[i].rvec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001114 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +11001115 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1116 rbi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001117 rbi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001118 /*
1119 * If this is discard request, set bi_vcnt 0. We don't
1120 * want to confuse SCSI because SCSI will replace payload
1121 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001122 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001123 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001124 if (conf->mddev->gendisk)
1125 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
1126 rbi, disk_devt(conf->mddev->gendisk),
1127 sh->dev[i].sector);
Shaohua Li765d7042017-01-04 09:33:23 -08001128 defer_bio_issue(conf, rbi);
NeilBrown977df362011-12-23 10:17:53 +11001129 }
1130 if (!rdev && !rrdev) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001131 if (op_is_write(op))
Dan Williams91c00922007-01-02 13:52:30 -07001132 set_bit(STRIPE_DEGRADED, &sh->state);
Mike Christie6296b962016-06-05 14:32:21 -05001133 pr_debug("skip op %d on disc %d for sector %llu\n",
Jens Axboe1eff9d32016-08-05 15:35:16 -06001134 bi->bi_opf, i, (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001135 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1136 set_bit(STRIPE_HANDLE, &sh->state);
1137 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001138
1139 if (!head_sh->batch_head)
1140 continue;
1141 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1142 batch_list);
1143 if (sh != head_sh)
1144 goto again;
Dan Williams91c00922007-01-02 13:52:30 -07001145 }
1146}
1147
1148static struct dma_async_tx_descriptor *
Shaohua Lid592a992014-05-21 17:57:44 +08001149async_copy_data(int frombio, struct bio *bio, struct page **page,
1150 sector_t sector, struct dma_async_tx_descriptor *tx,
Song Liu1e6d6902016-11-17 15:24:39 -08001151 struct stripe_head *sh, int no_skipcopy)
Dan Williams91c00922007-01-02 13:52:30 -07001152{
Kent Overstreet79886132013-11-23 17:19:00 -08001153 struct bio_vec bvl;
1154 struct bvec_iter iter;
Dan Williams91c00922007-01-02 13:52:30 -07001155 struct page *bio_page;
Dan Williams91c00922007-01-02 13:52:30 -07001156 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -07001157 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -07001158 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001159
Kent Overstreet4f024f32013-10-11 15:44:27 -07001160 if (bio->bi_iter.bi_sector >= sector)
1161 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
Dan Williams91c00922007-01-02 13:52:30 -07001162 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001163 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -07001164
Dan Williams0403e382009-09-08 17:42:50 -07001165 if (frombio)
1166 flags |= ASYNC_TX_FENCE;
1167 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1168
Kent Overstreet79886132013-11-23 17:19:00 -08001169 bio_for_each_segment(bvl, bio, iter) {
1170 int len = bvl.bv_len;
Dan Williams91c00922007-01-02 13:52:30 -07001171 int clen;
1172 int b_offset = 0;
1173
1174 if (page_offset < 0) {
1175 b_offset = -page_offset;
1176 page_offset += b_offset;
1177 len -= b_offset;
1178 }
1179
1180 if (len > 0 && page_offset + len > STRIPE_SIZE)
1181 clen = STRIPE_SIZE - page_offset;
1182 else
1183 clen = len;
1184
1185 if (clen > 0) {
Kent Overstreet79886132013-11-23 17:19:00 -08001186 b_offset += bvl.bv_offset;
1187 bio_page = bvl.bv_page;
Shaohua Lid592a992014-05-21 17:57:44 +08001188 if (frombio) {
1189 if (sh->raid_conf->skip_copy &&
1190 b_offset == 0 && page_offset == 0 &&
Song Liu1e6d6902016-11-17 15:24:39 -08001191 clen == STRIPE_SIZE &&
1192 !no_skipcopy)
Shaohua Lid592a992014-05-21 17:57:44 +08001193 *page = bio_page;
1194 else
1195 tx = async_memcpy(*page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001196 b_offset, clen, &submit);
Shaohua Lid592a992014-05-21 17:57:44 +08001197 } else
1198 tx = async_memcpy(bio_page, *page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001199 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001200 }
Dan Williamsa08abd82009-06-03 11:43:59 -07001201 /* chain the operations */
1202 submit.depend_tx = tx;
1203
Dan Williams91c00922007-01-02 13:52:30 -07001204 if (clen < len) /* hit end of page */
1205 break;
1206 page_offset += len;
1207 }
1208
1209 return tx;
1210}
1211
1212static void ops_complete_biofill(void *stripe_head_ref)
1213{
1214 struct stripe_head *sh = stripe_head_ref;
NeilBrown34a6f802015-08-14 12:07:57 +10001215 struct bio_list return_bi = BIO_EMPTY_LIST;
Dan Williamse4d84902007-09-24 10:06:13 -07001216 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001217
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001218 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001219 (unsigned long long)sh->sector);
1220
1221 /* clear completed biofills */
1222 for (i = sh->disks; i--; ) {
1223 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001224
1225 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001226 /* and check if we need to reply to a read request,
1227 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001228 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001229 */
1230 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001231 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001232
Dan Williams91c00922007-01-02 13:52:30 -07001233 BUG_ON(!dev->read);
1234 rbi = dev->read;
1235 dev->read = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001236 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001237 dev->sector + STRIPE_SECTORS) {
1238 rbi2 = r5_next_bio(rbi, dev->sector);
NeilBrown34a6f802015-08-14 12:07:57 +10001239 if (!raid5_dec_bi_active_stripes(rbi))
1240 bio_list_add(&return_bi, rbi);
Dan Williams91c00922007-01-02 13:52:30 -07001241 rbi = rbi2;
1242 }
1243 }
1244 }
Dan Williams83de75c2008-06-28 08:31:58 +10001245 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001246
NeilBrown34a6f802015-08-14 12:07:57 +10001247 return_io(&return_bi);
Dan Williams91c00922007-01-02 13:52:30 -07001248
Dan Williamse4d84902007-09-24 10:06:13 -07001249 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001250 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001251}
1252
1253static void ops_run_biofill(struct stripe_head *sh)
1254{
1255 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001256 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001257 int i;
1258
shli@kernel.org59fc6302014-12-15 12:57:03 +11001259 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001260 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001261 (unsigned long long)sh->sector);
1262
1263 for (i = sh->disks; i--; ) {
1264 struct r5dev *dev = &sh->dev[i];
1265 if (test_bit(R5_Wantfill, &dev->flags)) {
1266 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001267 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001268 dev->read = rbi = dev->toread;
1269 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001270 spin_unlock_irq(&sh->stripe_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001271 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001272 dev->sector + STRIPE_SECTORS) {
Shaohua Lid592a992014-05-21 17:57:44 +08001273 tx = async_copy_data(0, rbi, &dev->page,
Song Liu1e6d6902016-11-17 15:24:39 -08001274 dev->sector, tx, sh, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001275 rbi = r5_next_bio(rbi, dev->sector);
1276 }
1277 }
1278 }
1279
1280 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001281 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1282 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001283}
1284
Dan Williams4e7d2c02009-08-29 19:13:11 -07001285static void mark_target_uptodate(struct stripe_head *sh, int target)
1286{
1287 struct r5dev *tgt;
1288
1289 if (target < 0)
1290 return;
1291
1292 tgt = &sh->dev[target];
1293 set_bit(R5_UPTODATE, &tgt->flags);
1294 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1295 clear_bit(R5_Wantcompute, &tgt->flags);
1296}
1297
Dan Williamsac6b53b2009-07-14 13:40:19 -07001298static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001299{
1300 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001301
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001302 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001303 (unsigned long long)sh->sector);
1304
Dan Williamsac6b53b2009-07-14 13:40:19 -07001305 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001306 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001307 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001308
Dan Williamsecc65c92008-06-28 08:31:57 +10001309 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1310 if (sh->check_state == check_state_compute_run)
1311 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001312 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001313 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001314}
1315
Dan Williamsd6f38f32009-07-14 11:50:52 -07001316/* return a pointer to the address conversion region of the scribble buffer */
1317static addr_conv_t *to_addr_conv(struct stripe_head *sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001318 struct raid5_percpu *percpu, int i)
Dan Williams91c00922007-01-02 13:52:30 -07001319{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001320 void *addr;
1321
1322 addr = flex_array_get(percpu->scribble, i);
1323 return addr + sizeof(struct page *) * (sh->disks + 2);
1324}
1325
1326/* return a pointer to the address conversion region of the scribble buffer */
1327static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
1328{
1329 void *addr;
1330
1331 addr = flex_array_get(percpu->scribble, i);
1332 return addr;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001333}
1334
1335static struct dma_async_tx_descriptor *
1336ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1337{
Dan Williams91c00922007-01-02 13:52:30 -07001338 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001339 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001340 int target = sh->ops.target;
1341 struct r5dev *tgt = &sh->dev[target];
1342 struct page *xor_dest = tgt->page;
1343 int count = 0;
1344 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001345 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001346 int i;
1347
shli@kernel.org59fc6302014-12-15 12:57:03 +11001348 BUG_ON(sh->batch_head);
1349
Dan Williams91c00922007-01-02 13:52:30 -07001350 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001351 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001352 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1353
1354 for (i = disks; i--; )
1355 if (i != target)
1356 xor_srcs[count++] = sh->dev[i].page;
1357
1358 atomic_inc(&sh->count);
1359
Dan Williams0403e382009-09-08 17:42:50 -07001360 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001361 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
Dan Williams91c00922007-01-02 13:52:30 -07001362 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -07001363 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001364 else
Dan Williamsa08abd82009-06-03 11:43:59 -07001365 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001366
Dan Williams91c00922007-01-02 13:52:30 -07001367 return tx;
1368}
1369
Dan Williamsac6b53b2009-07-14 13:40:19 -07001370/* set_syndrome_sources - populate source buffers for gen_syndrome
1371 * @srcs - (struct page *) array of size sh->disks
1372 * @sh - stripe_head to parse
1373 *
1374 * Populates srcs in proper layout order for the stripe and returns the
1375 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1376 * destination buffer is recorded in srcs[count] and the Q destination
1377 * is recorded in srcs[count+1]].
1378 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001379static int set_syndrome_sources(struct page **srcs,
1380 struct stripe_head *sh,
1381 int srctype)
Dan Williamsac6b53b2009-07-14 13:40:19 -07001382{
1383 int disks = sh->disks;
1384 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1385 int d0_idx = raid6_d0(sh);
1386 int count;
1387 int i;
1388
1389 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001390 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001391
1392 count = 0;
1393 i = d0_idx;
1394 do {
1395 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001396 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001397
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001398 if (i == sh->qd_idx || i == sh->pd_idx ||
1399 (srctype == SYNDROME_SRC_ALL) ||
1400 (srctype == SYNDROME_SRC_WANT_DRAIN &&
Song Liu1e6d6902016-11-17 15:24:39 -08001401 (test_bit(R5_Wantdrain, &dev->flags) ||
1402 test_bit(R5_InJournal, &dev->flags))) ||
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001403 (srctype == SYNDROME_SRC_WRITTEN &&
Song Liu1e6d6902016-11-17 15:24:39 -08001404 dev->written)) {
1405 if (test_bit(R5_InJournal, &dev->flags))
1406 srcs[slot] = sh->dev[i].orig_page;
1407 else
1408 srcs[slot] = sh->dev[i].page;
1409 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001410 i = raid6_next_disk(i, disks);
1411 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001412
NeilBrowne4424fe2009-10-16 16:27:34 +11001413 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001414}
1415
1416static struct dma_async_tx_descriptor *
1417ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1418{
1419 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001420 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001421 int target;
1422 int qd_idx = sh->qd_idx;
1423 struct dma_async_tx_descriptor *tx;
1424 struct async_submit_ctl submit;
1425 struct r5dev *tgt;
1426 struct page *dest;
1427 int i;
1428 int count;
1429
shli@kernel.org59fc6302014-12-15 12:57:03 +11001430 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001431 if (sh->ops.target < 0)
1432 target = sh->ops.target2;
1433 else if (sh->ops.target2 < 0)
1434 target = sh->ops.target;
1435 else
1436 /* we should only have one valid target */
1437 BUG();
1438 BUG_ON(target < 0);
1439 pr_debug("%s: stripe %llu block: %d\n",
1440 __func__, (unsigned long long)sh->sector, target);
1441
1442 tgt = &sh->dev[target];
1443 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1444 dest = tgt->page;
1445
1446 atomic_inc(&sh->count);
1447
1448 if (target == qd_idx) {
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001449 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001450 blocks[count] = NULL; /* regenerating p is not necessary */
1451 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001452 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1453 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001454 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001455 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1456 } else {
1457 /* Compute any data- or p-drive using XOR */
1458 count = 0;
1459 for (i = disks; i-- ; ) {
1460 if (i == target || i == qd_idx)
1461 continue;
1462 blocks[count++] = sh->dev[i].page;
1463 }
1464
Dan Williams0403e382009-09-08 17:42:50 -07001465 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1466 NULL, ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001467 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001468 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1469 }
1470
1471 return tx;
1472}
1473
1474static struct dma_async_tx_descriptor *
1475ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1476{
1477 int i, count, disks = sh->disks;
1478 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1479 int d0_idx = raid6_d0(sh);
1480 int faila = -1, failb = -1;
1481 int target = sh->ops.target;
1482 int target2 = sh->ops.target2;
1483 struct r5dev *tgt = &sh->dev[target];
1484 struct r5dev *tgt2 = &sh->dev[target2];
1485 struct dma_async_tx_descriptor *tx;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001486 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001487 struct async_submit_ctl submit;
1488
shli@kernel.org59fc6302014-12-15 12:57:03 +11001489 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001490 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1491 __func__, (unsigned long long)sh->sector, target, target2);
1492 BUG_ON(target < 0 || target2 < 0);
1493 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1494 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1495
Dan Williams6c910a72009-09-16 12:24:54 -07001496 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001497 * slot number conversion for 'faila' and 'failb'
1498 */
1499 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001500 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001501 count = 0;
1502 i = d0_idx;
1503 do {
1504 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1505
1506 blocks[slot] = sh->dev[i].page;
1507
1508 if (i == target)
1509 faila = slot;
1510 if (i == target2)
1511 failb = slot;
1512 i = raid6_next_disk(i, disks);
1513 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001514
1515 BUG_ON(faila == failb);
1516 if (failb < faila)
1517 swap(faila, failb);
1518 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1519 __func__, (unsigned long long)sh->sector, faila, failb);
1520
1521 atomic_inc(&sh->count);
1522
1523 if (failb == syndrome_disks+1) {
1524 /* Q disk is one of the missing disks */
1525 if (faila == syndrome_disks) {
1526 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001527 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1528 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001529 to_addr_conv(sh, percpu, 0));
NeilBrowne4424fe2009-10-16 16:27:34 +11001530 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001531 STRIPE_SIZE, &submit);
1532 } else {
1533 struct page *dest;
1534 int data_target;
1535 int qd_idx = sh->qd_idx;
1536
1537 /* Missing D+Q: recompute D from P, then recompute Q */
1538 if (target == qd_idx)
1539 data_target = target2;
1540 else
1541 data_target = target;
1542
1543 count = 0;
1544 for (i = disks; i-- ; ) {
1545 if (i == data_target || i == qd_idx)
1546 continue;
1547 blocks[count++] = sh->dev[i].page;
1548 }
1549 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001550 init_async_submit(&submit,
1551 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1552 NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001553 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001554 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1555 &submit);
1556
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001557 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williams0403e382009-09-08 17:42:50 -07001558 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1559 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001560 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001561 return async_gen_syndrome(blocks, 0, count+2,
1562 STRIPE_SIZE, &submit);
1563 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001564 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001565 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1566 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001567 to_addr_conv(sh, percpu, 0));
Dan Williams6c910a72009-09-16 12:24:54 -07001568 if (failb == syndrome_disks) {
1569 /* We're missing D+P. */
1570 return async_raid6_datap_recov(syndrome_disks+2,
1571 STRIPE_SIZE, faila,
1572 blocks, &submit);
1573 } else {
1574 /* We're missing D+D. */
1575 return async_raid6_2data_recov(syndrome_disks+2,
1576 STRIPE_SIZE, faila, failb,
1577 blocks, &submit);
1578 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001579 }
1580}
1581
Dan Williams91c00922007-01-02 13:52:30 -07001582static void ops_complete_prexor(void *stripe_head_ref)
1583{
1584 struct stripe_head *sh = stripe_head_ref;
1585
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001586 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001587 (unsigned long long)sh->sector);
Song Liu1e6d6902016-11-17 15:24:39 -08001588
1589 if (r5c_is_writeback(sh->raid_conf->log))
1590 /*
1591 * raid5-cache write back uses orig_page during prexor.
1592 * After prexor, it is time to free orig_page
1593 */
1594 r5c_release_extra_page(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001595}
1596
1597static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001598ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1599 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001600{
Dan Williams91c00922007-01-02 13:52:30 -07001601 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001602 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001603 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001604 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001605
1606 /* existing parity data subtracted */
1607 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1608
shli@kernel.org59fc6302014-12-15 12:57:03 +11001609 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001610 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001611 (unsigned long long)sh->sector);
1612
1613 for (i = disks; i--; ) {
1614 struct r5dev *dev = &sh->dev[i];
1615 /* Only process blocks that are known to be uptodate */
Song Liu1e6d6902016-11-17 15:24:39 -08001616 if (test_bit(R5_InJournal, &dev->flags))
1617 xor_srcs[count++] = dev->orig_page;
1618 else if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001619 xor_srcs[count++] = dev->page;
1620 }
1621
Dan Williams0403e382009-09-08 17:42:50 -07001622 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001623 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Dan Williamsa08abd82009-06-03 11:43:59 -07001624 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001625
1626 return tx;
1627}
1628
1629static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001630ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1631 struct dma_async_tx_descriptor *tx)
1632{
1633 struct page **blocks = to_addr_page(percpu, 0);
1634 int count;
1635 struct async_submit_ctl submit;
1636
1637 pr_debug("%s: stripe %llu\n", __func__,
1638 (unsigned long long)sh->sector);
1639
1640 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN);
1641
1642 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1643 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1644 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1645
1646 return tx;
1647}
1648
1649static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001650ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001651{
Song Liu1e6d6902016-11-17 15:24:39 -08001652 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001653 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001654 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001655 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -07001656
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001657 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001658 (unsigned long long)sh->sector);
1659
1660 for (i = disks; i--; ) {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001661 struct r5dev *dev;
Dan Williams91c00922007-01-02 13:52:30 -07001662 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001663
shli@kernel.org59fc6302014-12-15 12:57:03 +11001664 sh = head_sh;
1665 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001666 struct bio *wbi;
1667
shli@kernel.org59fc6302014-12-15 12:57:03 +11001668again:
1669 dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001670 /*
1671 * clear R5_InJournal, so when rewriting a page in
1672 * journal, it is not skipped by r5l_log_stripe()
1673 */
1674 clear_bit(R5_InJournal, &dev->flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10001675 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001676 chosen = dev->towrite;
1677 dev->towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11001678 sh->overwrite_disks = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001679 BUG_ON(dev->written);
1680 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001681 spin_unlock_irq(&sh->stripe_lock);
Shaohua Lid592a992014-05-21 17:57:44 +08001682 WARN_ON(dev->page != dev->orig_page);
Dan Williams91c00922007-01-02 13:52:30 -07001683
Kent Overstreet4f024f32013-10-11 15:44:27 -07001684 while (wbi && wbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001685 dev->sector + STRIPE_SECTORS) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001686 if (wbi->bi_opf & REQ_FUA)
Tejun Heoe9c74692010-09-03 11:56:18 +02001687 set_bit(R5_WantFUA, &dev->flags);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001688 if (wbi->bi_opf & REQ_SYNC)
Shaohua Libc0934f2012-05-22 13:55:05 +10001689 set_bit(R5_SyncIO, &dev->flags);
Mike Christie796a5cf2016-06-05 14:32:07 -05001690 if (bio_op(wbi) == REQ_OP_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001691 set_bit(R5_Discard, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08001692 else {
1693 tx = async_copy_data(1, wbi, &dev->page,
Song Liu1e6d6902016-11-17 15:24:39 -08001694 dev->sector, tx, sh,
1695 r5c_is_writeback(conf->log));
1696 if (dev->page != dev->orig_page &&
1697 !r5c_is_writeback(conf->log)) {
Shaohua Lid592a992014-05-21 17:57:44 +08001698 set_bit(R5_SkipCopy, &dev->flags);
1699 clear_bit(R5_UPTODATE, &dev->flags);
1700 clear_bit(R5_OVERWRITE, &dev->flags);
1701 }
1702 }
Dan Williams91c00922007-01-02 13:52:30 -07001703 wbi = r5_next_bio(wbi, dev->sector);
1704 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001705
1706 if (head_sh->batch_head) {
1707 sh = list_first_entry(&sh->batch_list,
1708 struct stripe_head,
1709 batch_list);
1710 if (sh == head_sh)
1711 continue;
1712 goto again;
1713 }
Dan Williams91c00922007-01-02 13:52:30 -07001714 }
1715 }
1716
1717 return tx;
1718}
1719
Dan Williamsac6b53b2009-07-14 13:40:19 -07001720static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001721{
1722 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001723 int disks = sh->disks;
1724 int pd_idx = sh->pd_idx;
1725 int qd_idx = sh->qd_idx;
1726 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001727 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001728
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001729 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001730 (unsigned long long)sh->sector);
1731
Shaohua Libc0934f2012-05-22 13:55:05 +10001732 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001733 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001734 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001735 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001736 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001737
Dan Williams91c00922007-01-02 13:52:30 -07001738 for (i = disks; i--; ) {
1739 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001740
Tejun Heoe9c74692010-09-03 11:56:18 +02001741 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Lid592a992014-05-21 17:57:44 +08001742 if (!discard && !test_bit(R5_SkipCopy, &dev->flags))
Shaohua Li9e4447682012-10-11 13:49:49 +11001743 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001744 if (fua)
1745 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001746 if (sync)
1747 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001748 }
Dan Williams91c00922007-01-02 13:52:30 -07001749 }
1750
Dan Williamsd8ee0722008-06-28 08:32:06 +10001751 if (sh->reconstruct_state == reconstruct_state_drain_run)
1752 sh->reconstruct_state = reconstruct_state_drain_result;
1753 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1754 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1755 else {
1756 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1757 sh->reconstruct_state = reconstruct_state_result;
1758 }
Dan Williams91c00922007-01-02 13:52:30 -07001759
1760 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001761 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001762}
1763
1764static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001765ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1766 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001767{
Dan Williams91c00922007-01-02 13:52:30 -07001768 int disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001769 struct page **xor_srcs;
Dan Williamsa08abd82009-06-03 11:43:59 -07001770 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001771 int count, pd_idx = sh->pd_idx, i;
Dan Williams91c00922007-01-02 13:52:30 -07001772 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001773 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001774 unsigned long flags;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001775 int j = 0;
1776 struct stripe_head *head_sh = sh;
1777 int last_stripe;
Dan Williams91c00922007-01-02 13:52:30 -07001778
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001779 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001780 (unsigned long long)sh->sector);
1781
Shaohua Li620125f2012-10-11 13:49:05 +11001782 for (i = 0; i < sh->disks; i++) {
1783 if (pd_idx == i)
1784 continue;
1785 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1786 break;
1787 }
1788 if (i >= sh->disks) {
1789 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001790 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1791 ops_complete_reconstruct(sh);
1792 return;
1793 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001794again:
1795 count = 0;
1796 xor_srcs = to_addr_page(percpu, j);
Dan Williams91c00922007-01-02 13:52:30 -07001797 /* check if prexor is active which means only process blocks
1798 * that are part of a read-modify-write (written)
1799 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001800 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001801 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001802 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1803 for (i = disks; i--; ) {
1804 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001805 if (head_sh->dev[i].written ||
1806 test_bit(R5_InJournal, &head_sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -07001807 xor_srcs[count++] = dev->page;
1808 }
1809 } else {
1810 xor_dest = sh->dev[pd_idx].page;
1811 for (i = disks; i--; ) {
1812 struct r5dev *dev = &sh->dev[i];
1813 if (i != pd_idx)
1814 xor_srcs[count++] = dev->page;
1815 }
1816 }
1817
Dan Williams91c00922007-01-02 13:52:30 -07001818 /* 1/ if we prexor'd then the dest is reused as a source
1819 * 2/ if we did not prexor then we are redoing the parity
1820 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1821 * for the synchronous xor case
1822 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001823 last_stripe = !head_sh->batch_head ||
1824 list_first_entry(&sh->batch_list,
1825 struct stripe_head, batch_list) == head_sh;
1826 if (last_stripe) {
1827 flags = ASYNC_TX_ACK |
1828 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
Dan Williams91c00922007-01-02 13:52:30 -07001829
shli@kernel.org59fc6302014-12-15 12:57:03 +11001830 atomic_inc(&head_sh->count);
1831 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
1832 to_addr_conv(sh, percpu, j));
1833 } else {
1834 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
1835 init_async_submit(&submit, flags, tx, NULL, NULL,
1836 to_addr_conv(sh, percpu, j));
1837 }
Dan Williams91c00922007-01-02 13:52:30 -07001838
Dan Williamsa08abd82009-06-03 11:43:59 -07001839 if (unlikely(count == 1))
1840 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1841 else
1842 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001843 if (!last_stripe) {
1844 j++;
1845 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1846 batch_list);
1847 goto again;
1848 }
Dan Williams91c00922007-01-02 13:52:30 -07001849}
1850
Dan Williamsac6b53b2009-07-14 13:40:19 -07001851static void
1852ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1853 struct dma_async_tx_descriptor *tx)
1854{
1855 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001856 struct page **blocks;
1857 int count, i, j = 0;
1858 struct stripe_head *head_sh = sh;
1859 int last_stripe;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001860 int synflags;
1861 unsigned long txflags;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001862
1863 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1864
Shaohua Li620125f2012-10-11 13:49:05 +11001865 for (i = 0; i < sh->disks; i++) {
1866 if (sh->pd_idx == i || sh->qd_idx == i)
1867 continue;
1868 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1869 break;
1870 }
1871 if (i >= sh->disks) {
1872 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001873 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1874 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1875 ops_complete_reconstruct(sh);
1876 return;
1877 }
1878
shli@kernel.org59fc6302014-12-15 12:57:03 +11001879again:
1880 blocks = to_addr_page(percpu, j);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001881
1882 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1883 synflags = SYNDROME_SRC_WRITTEN;
1884 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
1885 } else {
1886 synflags = SYNDROME_SRC_ALL;
1887 txflags = ASYNC_TX_ACK;
1888 }
1889
1890 count = set_syndrome_sources(blocks, sh, synflags);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001891 last_stripe = !head_sh->batch_head ||
1892 list_first_entry(&sh->batch_list,
1893 struct stripe_head, batch_list) == head_sh;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001894
shli@kernel.org59fc6302014-12-15 12:57:03 +11001895 if (last_stripe) {
1896 atomic_inc(&head_sh->count);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001897 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
shli@kernel.org59fc6302014-12-15 12:57:03 +11001898 head_sh, to_addr_conv(sh, percpu, j));
1899 } else
1900 init_async_submit(&submit, 0, tx, NULL, NULL,
1901 to_addr_conv(sh, percpu, j));
Shaohua Li48769692015-05-13 09:30:08 -07001902 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001903 if (!last_stripe) {
1904 j++;
1905 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1906 batch_list);
1907 goto again;
1908 }
Dan Williams91c00922007-01-02 13:52:30 -07001909}
1910
1911static void ops_complete_check(void *stripe_head_ref)
1912{
1913 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001914
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001915 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001916 (unsigned long long)sh->sector);
1917
Dan Williamsecc65c92008-06-28 08:31:57 +10001918 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001919 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001920 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001921}
1922
Dan Williamsac6b53b2009-07-14 13:40:19 -07001923static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001924{
Dan Williams91c00922007-01-02 13:52:30 -07001925 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001926 int pd_idx = sh->pd_idx;
1927 int qd_idx = sh->qd_idx;
1928 struct page *xor_dest;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001929 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001930 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001931 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001932 int count;
1933 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001934
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001935 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001936 (unsigned long long)sh->sector);
1937
shli@kernel.org59fc6302014-12-15 12:57:03 +11001938 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001939 count = 0;
1940 xor_dest = sh->dev[pd_idx].page;
1941 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001942 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001943 if (i == pd_idx || i == qd_idx)
1944 continue;
1945 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001946 }
1947
Dan Williamsd6f38f32009-07-14 11:50:52 -07001948 init_async_submit(&submit, 0, NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001949 to_addr_conv(sh, percpu, 0));
Dan Williams099f53c2009-04-08 14:28:37 -07001950 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001951 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001952
Dan Williams91c00922007-01-02 13:52:30 -07001953 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001954 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1955 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001956}
1957
Dan Williamsac6b53b2009-07-14 13:40:19 -07001958static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1959{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001960 struct page **srcs = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001961 struct async_submit_ctl submit;
1962 int count;
1963
1964 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1965 (unsigned long long)sh->sector, checkp);
1966
shli@kernel.org59fc6302014-12-15 12:57:03 +11001967 BUG_ON(sh->batch_head);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001968 count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001969 if (!checkp)
1970 srcs[count] = NULL;
1971
1972 atomic_inc(&sh->count);
1973 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001974 sh, to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001975 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1976 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1977}
1978
NeilBrown51acbce2013-02-28 09:08:34 +11001979static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001980{
1981 int overlap_clear = 0, i, disks = sh->disks;
1982 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001983 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001984 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001985 struct raid5_percpu *percpu;
1986 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001987
Dan Williamsd6f38f32009-07-14 11:50:52 -07001988 cpu = get_cpu();
1989 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001990 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001991 ops_run_biofill(sh);
1992 overlap_clear++;
1993 }
1994
Dan Williams7b3a8712008-06-28 08:32:09 +10001995 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001996 if (level < 6)
1997 tx = ops_run_compute5(sh, percpu);
1998 else {
1999 if (sh->ops.target2 < 0 || sh->ops.target < 0)
2000 tx = ops_run_compute6_1(sh, percpu);
2001 else
2002 tx = ops_run_compute6_2(sh, percpu);
2003 }
2004 /* terminate the chain if reconstruct is not set to be run */
2005 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10002006 async_tx_ack(tx);
2007 }
Dan Williams91c00922007-01-02 13:52:30 -07002008
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002009 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
2010 if (level < 6)
2011 tx = ops_run_prexor5(sh, percpu, tx);
2012 else
2013 tx = ops_run_prexor6(sh, percpu, tx);
2014 }
Dan Williams91c00922007-01-02 13:52:30 -07002015
Dan Williams600aa102008-06-28 08:32:05 +10002016 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10002017 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07002018 overlap_clear++;
2019 }
2020
Dan Williamsac6b53b2009-07-14 13:40:19 -07002021 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
2022 if (level < 6)
2023 ops_run_reconstruct5(sh, percpu, tx);
2024 else
2025 ops_run_reconstruct6(sh, percpu, tx);
2026 }
Dan Williams91c00922007-01-02 13:52:30 -07002027
Dan Williamsac6b53b2009-07-14 13:40:19 -07002028 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
2029 if (sh->check_state == check_state_run)
2030 ops_run_check_p(sh, percpu);
2031 else if (sh->check_state == check_state_run_q)
2032 ops_run_check_pq(sh, percpu, 0);
2033 else if (sh->check_state == check_state_run_pq)
2034 ops_run_check_pq(sh, percpu, 1);
2035 else
2036 BUG();
2037 }
Dan Williams91c00922007-01-02 13:52:30 -07002038
shli@kernel.org59fc6302014-12-15 12:57:03 +11002039 if (overlap_clear && !sh->batch_head)
Dan Williams91c00922007-01-02 13:52:30 -07002040 for (i = disks; i--; ) {
2041 struct r5dev *dev = &sh->dev[i];
2042 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2043 wake_up(&sh->raid_conf->wait_for_overlap);
2044 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07002045 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07002046}
2047
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002048static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
2049 int disks)
NeilBrownf18c1a32015-05-08 18:19:04 +10002050{
2051 struct stripe_head *sh;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002052 int i;
NeilBrownf18c1a32015-05-08 18:19:04 +10002053
2054 sh = kmem_cache_zalloc(sc, gfp);
2055 if (sh) {
2056 spin_lock_init(&sh->stripe_lock);
2057 spin_lock_init(&sh->batch_lock);
2058 INIT_LIST_HEAD(&sh->batch_list);
2059 INIT_LIST_HEAD(&sh->lru);
Song Liua39f7af2016-11-17 15:24:40 -08002060 INIT_LIST_HEAD(&sh->r5c);
Song Liud7bd3982016-11-23 22:50:39 -08002061 INIT_LIST_HEAD(&sh->log_list);
NeilBrownf18c1a32015-05-08 18:19:04 +10002062 atomic_set(&sh->count, 1);
Song Liua39f7af2016-11-17 15:24:40 -08002063 sh->log_start = MaxSector;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002064 for (i = 0; i < disks; i++) {
2065 struct r5dev *dev = &sh->dev[i];
2066
Ming Lei3a83f462016-11-22 08:57:21 -07002067 bio_init(&dev->req, &dev->vec, 1);
2068 bio_init(&dev->rreq, &dev->rvec, 1);
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002069 }
NeilBrownf18c1a32015-05-08 18:19:04 +10002070 }
2071 return sh;
2072}
NeilBrown486f0642015-02-25 12:10:35 +11002073static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
2075 struct stripe_head *sh;
NeilBrownf18c1a32015-05-08 18:19:04 +10002076
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002077 sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08002078 if (!sh)
2079 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10002080
NeilBrown3f294f42005-11-08 21:39:25 -08002081 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08002082
NeilBrowna9683a72015-02-25 12:02:51 +11002083 if (grow_buffers(sh, gfp)) {
NeilBrowne4e11e32010-06-16 16:45:16 +10002084 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002085 kmem_cache_free(conf->slab_cache, sh);
2086 return 0;
2087 }
NeilBrown486f0642015-02-25 12:10:35 +11002088 sh->hash_lock_index =
2089 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrown3f294f42005-11-08 21:39:25 -08002090 /* we just created an active stripe so... */
NeilBrown3f294f42005-11-08 21:39:25 -08002091 atomic_inc(&conf->active_stripes);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002092
Shaohua Li6d036f72015-08-13 14:31:57 -07002093 raid5_release_stripe(sh);
NeilBrown486f0642015-02-25 12:10:35 +11002094 conf->max_nr_stripes++;
NeilBrown3f294f42005-11-08 21:39:25 -08002095 return 1;
2096}
2097
NeilBrownd1688a62011-10-11 16:49:52 +11002098static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08002099{
Christoph Lametere18b8902006-12-06 20:33:20 -08002100 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11002101 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
NeilBrownf4be6b42010-06-01 19:37:25 +10002103 if (conf->mddev->gendisk)
2104 sprintf(conf->cache_name[0],
2105 "raid%d-%s", conf->level, mdname(conf->mddev));
2106 else
2107 sprintf(conf->cache_name[0],
2108 "raid%d-%p", conf->level, conf->mddev);
2109 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
2110
NeilBrownad01c9e2006-03-27 01:18:07 -08002111 conf->active_name = 0;
2112 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002114 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (!sc)
2116 return 1;
2117 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002118 conf->pool_size = devs;
NeilBrown486f0642015-02-25 12:10:35 +11002119 while (num--)
2120 if (!grow_one_stripe(conf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return 1;
NeilBrown486f0642015-02-25 12:10:35 +11002122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return 0;
2124}
NeilBrown29269552006-03-27 01:18:10 -08002125
Dan Williamsd6f38f32009-07-14 11:50:52 -07002126/**
2127 * scribble_len - return the required size of the scribble region
2128 * @num - total number of disks in the array
2129 *
2130 * The size must be enough to contain:
2131 * 1/ a struct page pointer for each device in the array +2
2132 * 2/ room to convert each entry in (1) to its corresponding dma
2133 * (dma_map_page()) or page (page_address()) address.
2134 *
2135 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2136 * calculate over all devices (not just the data blocks), using zeros in place
2137 * of the P and Q blocks.
2138 */
shli@kernel.org46d5b782014-12-15 12:57:02 +11002139static struct flex_array *scribble_alloc(int num, int cnt, gfp_t flags)
Dan Williamsd6f38f32009-07-14 11:50:52 -07002140{
shli@kernel.org46d5b782014-12-15 12:57:02 +11002141 struct flex_array *ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002142 size_t len;
2143
2144 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
shli@kernel.org46d5b782014-12-15 12:57:02 +11002145 ret = flex_array_alloc(len, cnt, flags);
2146 if (!ret)
2147 return NULL;
2148 /* always prealloc all elements, so no locking is required */
2149 if (flex_array_prealloc(ret, 0, cnt, flags)) {
2150 flex_array_free(ret);
2151 return NULL;
2152 }
2153 return ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002154}
2155
NeilBrown738a2732015-05-08 18:19:39 +10002156static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2157{
2158 unsigned long cpu;
2159 int err = 0;
2160
Shaohua Li27a353c2016-02-24 17:38:28 -08002161 /*
2162 * Never shrink. And mddev_suspend() could deadlock if this is called
2163 * from raid5d. In that case, scribble_disks and scribble_sectors
2164 * should equal to new_disks and new_sectors
2165 */
2166 if (conf->scribble_disks >= new_disks &&
2167 conf->scribble_sectors >= new_sectors)
2168 return 0;
NeilBrown738a2732015-05-08 18:19:39 +10002169 mddev_suspend(conf->mddev);
2170 get_online_cpus();
2171 for_each_present_cpu(cpu) {
2172 struct raid5_percpu *percpu;
2173 struct flex_array *scribble;
2174
2175 percpu = per_cpu_ptr(conf->percpu, cpu);
2176 scribble = scribble_alloc(new_disks,
2177 new_sectors / STRIPE_SECTORS,
2178 GFP_NOIO);
2179
2180 if (scribble) {
2181 flex_array_free(percpu->scribble);
2182 percpu->scribble = scribble;
2183 } else {
2184 err = -ENOMEM;
2185 break;
2186 }
2187 }
2188 put_online_cpus();
2189 mddev_resume(conf->mddev);
Shaohua Li27a353c2016-02-24 17:38:28 -08002190 if (!err) {
2191 conf->scribble_disks = new_disks;
2192 conf->scribble_sectors = new_sectors;
2193 }
NeilBrown738a2732015-05-08 18:19:39 +10002194 return err;
2195}
2196
NeilBrownd1688a62011-10-11 16:49:52 +11002197static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08002198{
2199 /* Make all the stripes able to hold 'newsize' devices.
2200 * New slots in each stripe get 'page' set to a new page.
2201 *
2202 * This happens in stages:
2203 * 1/ create a new kmem_cache and allocate the required number of
2204 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09002205 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08002206 * to the new stripe_heads. This will have the side effect of
2207 * freezing the array as once all stripe_heads have been collected,
2208 * no IO will be possible. Old stripe heads are freed once their
2209 * pages have been transferred over, and the old kmem_cache is
2210 * freed when all stripes are done.
2211 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
2212 * we simple return a failre status - no need to clean anything up.
2213 * 4/ allocate new pages for the new slots in the new stripe_heads.
2214 * If this fails, we don't bother trying the shrink the
2215 * stripe_heads down again, we just leave them as they are.
2216 * As each stripe_head is processed the new one is released into
2217 * active service.
2218 *
2219 * Once step2 is started, we cannot afford to wait for a write,
2220 * so we use GFP_NOIO allocations.
2221 */
2222 struct stripe_head *osh, *nsh;
2223 LIST_HEAD(newstripes);
2224 struct disk_info *ndisks;
Dan Williamsb5470dc2008-06-27 21:44:04 -07002225 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08002226 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002227 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11002228 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08002229
2230 if (newsize <= conf->pool_size)
2231 return 0; /* never bother to shrink */
2232
Dan Williamsb5470dc2008-06-27 21:44:04 -07002233 err = md_allow_write(conf->mddev);
2234 if (err)
2235 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08002236
NeilBrownad01c9e2006-03-27 01:18:07 -08002237 /* Step 1 */
2238 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2239 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002240 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08002241 if (!sc)
2242 return -ENOMEM;
2243
NeilBrown2d5b5692015-07-06 12:49:23 +10002244 /* Need to ensure auto-resizing doesn't interfere */
2245 mutex_lock(&conf->cache_size_mutex);
2246
NeilBrownad01c9e2006-03-27 01:18:07 -08002247 for (i = conf->max_nr_stripes; i; i--) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002248 nsh = alloc_stripe(sc, GFP_KERNEL, newsize);
NeilBrownad01c9e2006-03-27 01:18:07 -08002249 if (!nsh)
2250 break;
2251
NeilBrownad01c9e2006-03-27 01:18:07 -08002252 nsh->raid_conf = conf;
NeilBrownad01c9e2006-03-27 01:18:07 -08002253 list_add(&nsh->lru, &newstripes);
2254 }
2255 if (i) {
2256 /* didn't get enough, give up */
2257 while (!list_empty(&newstripes)) {
2258 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2259 list_del(&nsh->lru);
2260 kmem_cache_free(sc, nsh);
2261 }
2262 kmem_cache_destroy(sc);
NeilBrown2d5b5692015-07-06 12:49:23 +10002263 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002264 return -ENOMEM;
2265 }
2266 /* Step 2 - Must use GFP_NOIO now.
2267 * OK, we have enough stripes, start collecting inactive
2268 * stripes and copying them over
2269 */
Shaohua Li566c09c2013-11-14 15:16:17 +11002270 hash = 0;
2271 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08002272 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11002273 lock_device_hash_lock(conf, hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08002274 wait_event_cmd(conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +11002275 !list_empty(conf->inactive_list + hash),
2276 unlock_device_hash_lock(conf, hash),
2277 lock_device_hash_lock(conf, hash));
2278 osh = get_free_stripe(conf, hash);
2279 unlock_device_hash_lock(conf, hash);
NeilBrownf18c1a32015-05-08 18:19:04 +10002280
Shaohua Lid592a992014-05-21 17:57:44 +08002281 for(i=0; i<conf->pool_size; i++) {
NeilBrownad01c9e2006-03-27 01:18:07 -08002282 nsh->dev[i].page = osh->dev[i].page;
Shaohua Lid592a992014-05-21 17:57:44 +08002283 nsh->dev[i].orig_page = osh->dev[i].page;
2284 }
Shaohua Li566c09c2013-11-14 15:16:17 +11002285 nsh->hash_lock_index = hash;
NeilBrownad01c9e2006-03-27 01:18:07 -08002286 kmem_cache_free(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11002287 cnt++;
2288 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2289 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2290 hash++;
2291 cnt = 0;
2292 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002293 }
2294 kmem_cache_destroy(conf->slab_cache);
2295
2296 /* Step 3.
2297 * At this point, we are holding all the stripes so the array
2298 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07002299 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08002300 */
2301 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
2302 if (ndisks) {
Song Liud7bd3982016-11-23 22:50:39 -08002303 for (i = 0; i < conf->pool_size; i++)
NeilBrownad01c9e2006-03-27 01:18:07 -08002304 ndisks[i] = conf->disks[i];
Song Liud7bd3982016-11-23 22:50:39 -08002305
2306 for (i = conf->pool_size; i < newsize; i++) {
2307 ndisks[i].extra_page = alloc_page(GFP_NOIO);
2308 if (!ndisks[i].extra_page)
2309 err = -ENOMEM;
2310 }
2311
2312 if (err) {
2313 for (i = conf->pool_size; i < newsize; i++)
2314 if (ndisks[i].extra_page)
2315 put_page(ndisks[i].extra_page);
2316 kfree(ndisks);
2317 } else {
2318 kfree(conf->disks);
2319 conf->disks = ndisks;
2320 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002321 } else
2322 err = -ENOMEM;
2323
NeilBrown2d5b5692015-07-06 12:49:23 +10002324 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002325 /* Step 4, return new stripes to service */
2326 while(!list_empty(&newstripes)) {
2327 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2328 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07002329
NeilBrownad01c9e2006-03-27 01:18:07 -08002330 for (i=conf->raid_disks; i < newsize; i++)
2331 if (nsh->dev[i].page == NULL) {
2332 struct page *p = alloc_page(GFP_NOIO);
2333 nsh->dev[i].page = p;
Shaohua Lid592a992014-05-21 17:57:44 +08002334 nsh->dev[i].orig_page = p;
NeilBrownad01c9e2006-03-27 01:18:07 -08002335 if (!p)
2336 err = -ENOMEM;
2337 }
Shaohua Li6d036f72015-08-13 14:31:57 -07002338 raid5_release_stripe(nsh);
NeilBrownad01c9e2006-03-27 01:18:07 -08002339 }
2340 /* critical section pass, GFP_NOIO no longer needed */
2341
2342 conf->slab_cache = sc;
2343 conf->active_name = 1-conf->active_name;
NeilBrown6e9eac22015-05-08 18:19:34 +10002344 if (!err)
2345 conf->pool_size = newsize;
NeilBrownad01c9e2006-03-27 01:18:07 -08002346 return err;
2347}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
NeilBrown486f0642015-02-25 12:10:35 +11002349static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
2351 struct stripe_head *sh;
NeilBrown49895bc2015-08-03 17:09:57 +10002352 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Shaohua Li566c09c2013-11-14 15:16:17 +11002354 spin_lock_irq(conf->hash_locks + hash);
2355 sh = get_free_stripe(conf, hash);
2356 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08002357 if (!sh)
2358 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002359 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10002360 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002361 kmem_cache_free(conf->slab_cache, sh);
2362 atomic_dec(&conf->active_stripes);
NeilBrown486f0642015-02-25 12:10:35 +11002363 conf->max_nr_stripes--;
NeilBrown3f294f42005-11-08 21:39:25 -08002364 return 1;
2365}
2366
NeilBrownd1688a62011-10-11 16:49:52 +11002367static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08002368{
NeilBrown486f0642015-02-25 12:10:35 +11002369 while (conf->max_nr_stripes &&
2370 drop_one_stripe(conf))
2371 ;
NeilBrown3f294f42005-11-08 21:39:25 -08002372
Julia Lawall644df1a2015-09-13 14:15:10 +02002373 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 conf->slab_cache = NULL;
2375}
2376
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002377static void raid5_end_read_request(struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378{
NeilBrown99c0fb52009-03-31 14:39:38 +11002379 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002380 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002381 int disks = sh->disks, i;
NeilBrownd6950432006-07-10 04:44:20 -07002382 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11002383 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10002384 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
2386 for (i=0 ; i<disks; i++)
2387 if (bi == &sh->dev[i].req)
2388 break;
2389
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002390 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
Dan Williams45b42332007-07-09 11:56:43 -07002391 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002392 bi->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002394 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002396 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
NeilBrown14a75d32011-12-23 10:17:52 +11002398 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11002399 /* If replacement finished while this request was outstanding,
2400 * 'replacement' might be NULL already.
2401 * In that case it moved down to 'rdev'.
2402 * rdev is not removed until all requests are finished.
2403 */
NeilBrown14a75d32011-12-23 10:17:52 +11002404 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002405 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11002406 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
NeilBrown05616be2012-05-21 09:27:00 +10002408 if (use_new_offset(conf, sh))
2409 s = sh->sector + rdev->new_data_offset;
2410 else
2411 s = sh->sector + rdev->data_offset;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002412 if (!bi->bi_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08002414 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11002415 /* Note that this cannot happen on a
2416 * replacement device. We just fail those on
2417 * any error
2418 */
NeilBrowncc6167b2016-11-02 14:16:50 +11002419 pr_info_ratelimited(
2420 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002421 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10002422 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002423 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10002424 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08002425 clear_bit(R5_ReadError, &sh->dev[i].flags);
2426 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002427 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2428 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2429
Song Liu86aa1392017-01-12 17:22:41 -08002430 if (test_bit(R5_InJournal, &sh->dev[i].flags))
2431 /*
2432 * end read for a page in journal, this
2433 * must be preparing for prexor in rmw
2434 */
2435 set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
2436
NeilBrown14a75d32011-12-23 10:17:52 +11002437 if (atomic_read(&rdev->read_errors))
2438 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002440 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002441 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002442 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07002445 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002446 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowncc6167b2016-11-02 14:16:50 +11002447 pr_warn_ratelimited(
2448 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
NeilBrown14a75d32011-12-23 10:17:52 +11002449 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002450 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002451 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002452 else if (conf->mddev->degraded >= conf->max_degraded) {
2453 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002454 pr_warn_ratelimited(
2455 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002456 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002457 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002458 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002459 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002460 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002461 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002462 pr_warn_ratelimited(
2463 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002464 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002465 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002466 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002467 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08002468 > conf->max_nr_stripes)
NeilBrowncc6167b2016-11-02 14:16:50 +11002469 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07002470 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08002471 else
2472 retry = 1;
Bian Yuedfa1f62013-11-14 15:16:17 +11002473 if (set_bad && test_bit(In_sync, &rdev->flags)
2474 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2475 retry = 1;
NeilBrownba22dcb2005-11-08 21:39:31 -08002476 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10002477 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2478 set_bit(R5_ReadError, &sh->dev[i].flags);
2479 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2480 } else
2481 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002482 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002483 clear_bit(R5_ReadError, &sh->dev[i].flags);
2484 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002485 if (!(set_bad
2486 && test_bit(In_sync, &rdev->flags)
2487 && rdev_set_badblocks(
2488 rdev, sh->sector, STRIPE_SECTORS, 0)))
2489 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
NeilBrown14a75d32011-12-23 10:17:52 +11002492 rdev_dec_pending(rdev, conf->mddev);
Shaohua Lic9445552016-09-08 10:43:58 -07002493 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2495 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002496 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497}
2498
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002499static void raid5_end_write_request(struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
NeilBrown99c0fb52009-03-31 14:39:38 +11002501 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002502 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002503 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11002504 struct md_rdev *uninitialized_var(rdev);
NeilBrownb84db562011-07-28 11:39:23 +10002505 sector_t first_bad;
2506 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002507 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
NeilBrown977df362011-12-23 10:17:53 +11002509 for (i = 0 ; i < disks; i++) {
2510 if (bi == &sh->dev[i].req) {
2511 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 break;
NeilBrown977df362011-12-23 10:17:53 +11002513 }
2514 if (bi == &sh->dev[i].rreq) {
2515 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002516 if (rdev)
2517 replacement = 1;
2518 else
2519 /* rdev was removed and 'replacement'
2520 * replaced it. rdev is not removed
2521 * until all requests are finished.
2522 */
2523 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002524 break;
2525 }
2526 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002527 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002529 bi->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002531 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002533 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
2535
NeilBrown977df362011-12-23 10:17:53 +11002536 if (replacement) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002537 if (bi->bi_error)
NeilBrown977df362011-12-23 10:17:53 +11002538 md_error(conf->mddev, rdev);
2539 else if (is_badblock(rdev, sh->sector,
2540 STRIPE_SECTORS,
2541 &first_bad, &bad_sectors))
2542 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2543 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002544 if (bi->bi_error) {
NeilBrown9f97e4b2014-01-16 09:35:38 +11002545 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11002546 set_bit(WriteErrorSeen, &rdev->flags);
2547 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002548 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2549 set_bit(MD_RECOVERY_NEEDED,
2550 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002551 } else if (is_badblock(rdev, sh->sector,
2552 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10002553 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002554 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002555 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2556 /* That was a successful write so make
2557 * sure it looks like we already did
2558 * a re-write.
2559 */
2560 set_bit(R5_ReWrite, &sh->dev[i].flags);
2561 }
NeilBrown977df362011-12-23 10:17:53 +11002562 }
2563 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002565 if (sh->batch_head && bi->bi_error && !replacement)
shli@kernel.org72ac7332014-12-15 12:57:03 +11002566 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2567
Shaohua Lic9445552016-09-08 10:43:58 -07002568 bio_reset(bi);
NeilBrown977df362011-12-23 10:17:53 +11002569 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2570 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002572 raid5_release_stripe(sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002573
2574 if (sh->batch_head && sh != sh->batch_head)
Shaohua Li6d036f72015-08-13 14:31:57 -07002575 raid5_release_stripe(sh->batch_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
NeilBrown784052e2009-03-31 15:19:07 +11002578static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
2580 struct r5dev *dev = &sh->dev[i];
2581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 dev->flags = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07002583 dev->sector = raid5_compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Shaohua Li849674e2016-01-20 13:52:20 -08002586static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
2588 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002589 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002590 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002591 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
NeilBrown908f4fb2011-12-23 10:17:50 +11002593 spin_lock_irqsave(&conf->device_lock, flags);
2594 clear_bit(In_sync, &rdev->flags);
Song Liu2e38a372017-01-24 10:45:30 -08002595 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11002596 spin_unlock_irqrestore(&conf->device_lock, flags);
2597 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2598
NeilBrownde393cd2011-07-28 11:31:48 +10002599 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10002600 set_bit(Faulty, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08002601 set_mask_bits(&mddev->sb_flags, 0,
2602 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrowncc6167b2016-11-02 14:16:50 +11002603 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
2604 "md/raid:%s: Operation continuing on %d devices.\n",
2605 mdname(mddev),
2606 bdevname(rdev->bdev, b),
2607 mdname(mddev),
2608 conf->raid_disks - mddev->degraded);
Song Liu2e38a372017-01-24 10:45:30 -08002609 r5c_update_on_rdev_error(mddev);
NeilBrown16a53ec2006-06-26 00:27:38 -07002610}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
2612/*
2613 * Input: a 'big' sector number,
2614 * Output: index of the data and parity disk, and the sector # in them.
2615 */
Shaohua Li6d036f72015-08-13 14:31:57 -07002616sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2617 int previous, int *dd_idx,
2618 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002620 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002621 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002623 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002624 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002626 int algorithm = previous ? conf->prev_algo
2627 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002628 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2629 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002630 int raid_disks = previous ? conf->previous_raid_disks
2631 : conf->raid_disks;
2632 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634 /* First compute the information on this sector */
2635
2636 /*
2637 * Compute the chunk number and the sector offset inside the chunk
2638 */
2639 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2640 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642 /*
2643 * Compute the stripe number
2644 */
NeilBrown35f2a592010-04-20 14:13:34 +10002645 stripe = chunk_number;
2646 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002647 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 /*
2649 * Select the parity disk based on the user selected algorithm.
2650 */
NeilBrown84789552011-07-27 11:00:36 +10002651 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002652 switch(conf->level) {
2653 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002654 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002655 break;
2656 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002657 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002659 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002660 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 (*dd_idx)++;
2662 break;
2663 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002664 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002665 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 (*dd_idx)++;
2667 break;
2668 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002669 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002670 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 break;
2672 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002673 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002674 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002676 case ALGORITHM_PARITY_0:
2677 pd_idx = 0;
2678 (*dd_idx)++;
2679 break;
2680 case ALGORITHM_PARITY_N:
2681 pd_idx = data_disks;
2682 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002684 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002685 }
2686 break;
2687 case 6:
2688
NeilBrowne183eae2009-03-31 15:20:22 +11002689 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002690 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002691 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002692 qd_idx = pd_idx + 1;
2693 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002694 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002695 qd_idx = 0;
2696 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002697 (*dd_idx) += 2; /* D D P Q D */
2698 break;
2699 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002700 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002701 qd_idx = pd_idx + 1;
2702 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002703 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002704 qd_idx = 0;
2705 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002706 (*dd_idx) += 2; /* D D P Q D */
2707 break;
2708 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002709 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002710 qd_idx = (pd_idx + 1) % raid_disks;
2711 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002712 break;
2713 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002714 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002715 qd_idx = (pd_idx + 1) % raid_disks;
2716 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002717 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002718
2719 case ALGORITHM_PARITY_0:
2720 pd_idx = 0;
2721 qd_idx = 1;
2722 (*dd_idx) += 2;
2723 break;
2724 case ALGORITHM_PARITY_N:
2725 pd_idx = data_disks;
2726 qd_idx = data_disks + 1;
2727 break;
2728
2729 case ALGORITHM_ROTATING_ZERO_RESTART:
2730 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2731 * of blocks for computing Q is different.
2732 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002733 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002734 qd_idx = pd_idx + 1;
2735 if (pd_idx == raid_disks-1) {
2736 (*dd_idx)++; /* Q D D D P */
2737 qd_idx = 0;
2738 } else if (*dd_idx >= pd_idx)
2739 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002740 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002741 break;
2742
2743 case ALGORITHM_ROTATING_N_RESTART:
2744 /* Same a left_asymmetric, by first stripe is
2745 * D D D P Q rather than
2746 * Q D D D P
2747 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002748 stripe2 += 1;
2749 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002750 qd_idx = pd_idx + 1;
2751 if (pd_idx == raid_disks-1) {
2752 (*dd_idx)++; /* Q D D D P */
2753 qd_idx = 0;
2754 } else if (*dd_idx >= pd_idx)
2755 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002756 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002757 break;
2758
2759 case ALGORITHM_ROTATING_N_CONTINUE:
2760 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002761 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002762 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2763 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002764 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002765 break;
2766
2767 case ALGORITHM_LEFT_ASYMMETRIC_6:
2768 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002769 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002770 if (*dd_idx >= pd_idx)
2771 (*dd_idx)++;
2772 qd_idx = raid_disks - 1;
2773 break;
2774
2775 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002776 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002777 if (*dd_idx >= pd_idx)
2778 (*dd_idx)++;
2779 qd_idx = raid_disks - 1;
2780 break;
2781
2782 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002783 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002784 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2785 qd_idx = raid_disks - 1;
2786 break;
2787
2788 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002789 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002790 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2791 qd_idx = raid_disks - 1;
2792 break;
2793
2794 case ALGORITHM_PARITY_0_6:
2795 pd_idx = 0;
2796 (*dd_idx)++;
2797 qd_idx = raid_disks - 1;
2798 break;
2799
NeilBrown16a53ec2006-06-26 00:27:38 -07002800 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002801 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002802 }
2803 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 }
2805
NeilBrown911d4ee2009-03-31 14:39:38 +11002806 if (sh) {
2807 sh->pd_idx = pd_idx;
2808 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002809 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 /*
2812 * Finally, compute the new sector number
2813 */
2814 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2815 return new_sector;
2816}
2817
Shaohua Li6d036f72015-08-13 14:31:57 -07002818sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
NeilBrownd1688a62011-10-11 16:49:52 +11002820 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002821 int raid_disks = sh->disks;
2822 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002824 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2825 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002826 int algorithm = previous ? conf->prev_algo
2827 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 sector_t stripe;
2829 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002830 sector_t chunk_number;
2831 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002833 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
2835 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2836 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
NeilBrown16a53ec2006-06-26 00:27:38 -07002838 if (i == sh->pd_idx)
2839 return 0;
2840 switch(conf->level) {
2841 case 4: break;
2842 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002843 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 case ALGORITHM_LEFT_ASYMMETRIC:
2845 case ALGORITHM_RIGHT_ASYMMETRIC:
2846 if (i > sh->pd_idx)
2847 i--;
2848 break;
2849 case ALGORITHM_LEFT_SYMMETRIC:
2850 case ALGORITHM_RIGHT_SYMMETRIC:
2851 if (i < sh->pd_idx)
2852 i += raid_disks;
2853 i -= (sh->pd_idx + 1);
2854 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002855 case ALGORITHM_PARITY_0:
2856 i -= 1;
2857 break;
2858 case ALGORITHM_PARITY_N:
2859 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002861 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002862 }
2863 break;
2864 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002865 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002866 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002867 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002868 case ALGORITHM_LEFT_ASYMMETRIC:
2869 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002870 case ALGORITHM_ROTATING_ZERO_RESTART:
2871 case ALGORITHM_ROTATING_N_RESTART:
2872 if (sh->pd_idx == raid_disks-1)
2873 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002874 else if (i > sh->pd_idx)
2875 i -= 2; /* D D P Q D */
2876 break;
2877 case ALGORITHM_LEFT_SYMMETRIC:
2878 case ALGORITHM_RIGHT_SYMMETRIC:
2879 if (sh->pd_idx == raid_disks-1)
2880 i--; /* Q D D D P */
2881 else {
2882 /* D D P Q D */
2883 if (i < sh->pd_idx)
2884 i += raid_disks;
2885 i -= (sh->pd_idx + 2);
2886 }
2887 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002888 case ALGORITHM_PARITY_0:
2889 i -= 2;
2890 break;
2891 case ALGORITHM_PARITY_N:
2892 break;
2893 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002894 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002895 if (sh->pd_idx == 0)
2896 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002897 else {
2898 /* D D Q P D */
2899 if (i < sh->pd_idx)
2900 i += raid_disks;
2901 i -= (sh->pd_idx + 1);
2902 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002903 break;
2904 case ALGORITHM_LEFT_ASYMMETRIC_6:
2905 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2906 if (i > sh->pd_idx)
2907 i--;
2908 break;
2909 case ALGORITHM_LEFT_SYMMETRIC_6:
2910 case ALGORITHM_RIGHT_SYMMETRIC_6:
2911 if (i < sh->pd_idx)
2912 i += data_disks + 1;
2913 i -= (sh->pd_idx + 1);
2914 break;
2915 case ALGORITHM_PARITY_0_6:
2916 i -= 1;
2917 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002918 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002919 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002920 }
2921 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 }
2923
2924 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002925 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
NeilBrown112bf892009-03-31 14:39:38 +11002927 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002928 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002929 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2930 || sh2.qd_idx != sh->qd_idx) {
NeilBrowncc6167b2016-11-02 14:16:50 +11002931 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
2932 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 return 0;
2934 }
2935 return r_sector;
2936}
2937
Song Liu07e83362017-01-23 17:12:58 -08002938/*
2939 * There are cases where we want handle_stripe_dirtying() and
2940 * schedule_reconstruction() to delay towrite to some dev of a stripe.
2941 *
2942 * This function checks whether we want to delay the towrite. Specifically,
2943 * we delay the towrite when:
2944 *
2945 * 1. degraded stripe has a non-overwrite to the missing dev, AND this
2946 * stripe has data in journal (for other devices).
2947 *
2948 * In this case, when reading data for the non-overwrite dev, it is
2949 * necessary to handle complex rmw of write back cache (prexor with
2950 * orig_page, and xor with page). To keep read path simple, we would
2951 * like to flush data in journal to RAID disks first, so complex rmw
2952 * is handled in the write patch (handle_stripe_dirtying).
2953 *
2954 */
2955static inline bool delay_towrite(struct r5dev *dev,
2956 struct stripe_head_state *s)
2957{
2958 return !test_bit(R5_OVERWRITE, &dev->flags) &&
2959 !test_bit(R5_Insync, &dev->flags) && s->injournal;
2960}
2961
Dan Williams600aa102008-06-28 08:32:05 +10002962static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002963schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002964 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002965{
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002966 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002967 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002968 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002969
Dan Williamse33129d2007-01-02 13:52:30 -07002970 if (rcw) {
Song Liu1e6d6902016-11-17 15:24:39 -08002971 /*
2972 * In some cases, handle_stripe_dirtying initially decided to
2973 * run rmw and allocates extra page for prexor. However, rcw is
2974 * cheaper later on. We need to free the extra page now,
2975 * because we won't be able to do that in ops_complete_prexor().
2976 */
2977 r5c_release_extra_page(sh);
Dan Williamse33129d2007-01-02 13:52:30 -07002978
2979 for (i = disks; i--; ) {
2980 struct r5dev *dev = &sh->dev[i];
2981
Song Liu07e83362017-01-23 17:12:58 -08002982 if (dev->towrite && !delay_towrite(dev, s)) {
Dan Williamse33129d2007-01-02 13:52:30 -07002983 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002984 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002985 if (!expand)
2986 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002987 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08002988 } else if (test_bit(R5_InJournal, &dev->flags)) {
2989 set_bit(R5_LOCKED, &dev->flags);
2990 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002991 }
2992 }
NeilBrownce7d3632013-03-04 12:37:14 +11002993 /* if we are not expanding this is a proper write request, and
2994 * there will be bios with new data to be drained into the
2995 * stripe cache
2996 */
2997 if (!expand) {
2998 if (!s->locked)
2999 /* False alarm, nothing to do */
3000 return;
3001 sh->reconstruct_state = reconstruct_state_drain_run;
3002 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3003 } else
3004 sh->reconstruct_state = reconstruct_state_run;
3005
3006 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3007
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003008 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003009 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003010 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07003011 } else {
3012 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
3013 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003014 BUG_ON(level == 6 &&
3015 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
3016 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
Dan Williamse33129d2007-01-02 13:52:30 -07003017
Dan Williamse33129d2007-01-02 13:52:30 -07003018 for (i = disks; i--; ) {
3019 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003020 if (i == pd_idx || i == qd_idx)
Dan Williamse33129d2007-01-02 13:52:30 -07003021 continue;
3022
Dan Williamse33129d2007-01-02 13:52:30 -07003023 if (dev->towrite &&
3024 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10003025 test_bit(R5_Wantcompute, &dev->flags))) {
3026 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003027 set_bit(R5_LOCKED, &dev->flags);
3028 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003029 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003030 } else if (test_bit(R5_InJournal, &dev->flags)) {
3031 set_bit(R5_LOCKED, &dev->flags);
3032 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003033 }
3034 }
NeilBrownce7d3632013-03-04 12:37:14 +11003035 if (!s->locked)
3036 /* False alarm - nothing to do */
3037 return;
3038 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
3039 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
3040 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3041 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003042 }
3043
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003044 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07003045 * are in flight
3046 */
3047 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
3048 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10003049 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003050
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003051 if (level == 6) {
3052 int qd_idx = sh->qd_idx;
3053 struct r5dev *dev = &sh->dev[qd_idx];
3054
3055 set_bit(R5_LOCKED, &dev->flags);
3056 clear_bit(R5_UPTODATE, &dev->flags);
3057 s->locked++;
3058 }
3059
Dan Williams600aa102008-06-28 08:32:05 +10003060 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07003061 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10003062 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003063}
NeilBrown16a53ec2006-06-26 00:27:38 -07003064
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065/*
3066 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07003067 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 * The bi_next chain must be in order.
3069 */
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003070static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
3071 int forwrite, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072{
3073 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11003074 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07003075 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
NeilBrowncbe47ec2011-07-26 11:20:35 +10003077 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003078 (unsigned long long)bi->bi_iter.bi_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 (unsigned long long)sh->sector);
3080
Shaohua Lib17459c2012-07-19 16:01:31 +10003081 /*
3082 * If several bio share a stripe. The bio bi_phys_segments acts as a
3083 * reference count to avoid race. The reference count should already be
3084 * increased before this function is called (for example, in
Shaohua Li849674e2016-01-20 13:52:20 -08003085 * raid5_make_request()), so other bio sharing this stripe will not free the
Shaohua Lib17459c2012-07-19 16:01:31 +10003086 * stripe. If a stripe is owned by one stripe, the stripe lock will
3087 * protect it.
3088 */
3089 spin_lock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003090 /* Don't allow new IO added to stripes in batch list */
3091 if (sh->batch_head)
3092 goto overlap;
NeilBrown72626682005-09-09 16:23:54 -07003093 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003095 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07003096 firstwrite = 1;
3097 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 bip = &sh->dev[dd_idx].toread;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003099 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
3100 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 goto overlap;
3102 bip = & (*bip)->bi_next;
3103 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07003104 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 goto overlap;
3106
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003107 if (!forwrite || previous)
3108 clear_bit(STRIPE_BATCH_READY, &sh->state);
3109
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02003110 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 if (*bip)
3112 bi->bi_next = *bip;
3113 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003114 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07003115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 if (forwrite) {
3117 /* check if page is covered */
3118 sector_t sector = sh->dev[dd_idx].sector;
3119 for (bi=sh->dev[dd_idx].towrite;
3120 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07003121 bi && bi->bi_iter.bi_sector <= sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07003123 if (bio_end_sector(bi) >= sector)
3124 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 }
3126 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
shli@kernel.org7a87f432014-12-15 12:57:03 +11003127 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
3128 sh->overwrite_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003130
3131 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003132 (unsigned long long)(*bip)->bi_iter.bi_sector,
NeilBrowncbe47ec2011-07-26 11:20:35 +10003133 (unsigned long long)sh->sector, dd_idx);
3134
3135 if (conf->mddev->bitmap && firstwrite) {
NeilBrownd0852df52015-05-27 08:43:45 +10003136 /* Cannot hold spinlock over bitmap_startwrite,
3137 * but must ensure this isn't added to a batch until
3138 * we have added to the bitmap and set bm_seq.
3139 * So set STRIPE_BITMAP_PENDING to prevent
3140 * batching.
3141 * If multiple add_stripe_bio() calls race here they
3142 * much all set STRIPE_BITMAP_PENDING. So only the first one
3143 * to complete "bitmap_startwrite" gets to set
3144 * STRIPE_BIT_DELAY. This is important as once a stripe
3145 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3146 * any more.
3147 */
3148 set_bit(STRIPE_BITMAP_PENDING, &sh->state);
3149 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10003150 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
3151 STRIPE_SECTORS, 0);
NeilBrownd0852df52015-05-27 08:43:45 +10003152 spin_lock_irq(&sh->stripe_lock);
3153 clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
3154 if (!sh->batch_head) {
3155 sh->bm_seq = conf->seq_flush+1;
3156 set_bit(STRIPE_BIT_DELAY, &sh->state);
3157 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003158 }
NeilBrownd0852df52015-05-27 08:43:45 +10003159 spin_unlock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003160
3161 if (stripe_can_batch(sh))
3162 stripe_add_to_batch_list(conf, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 return 1;
3164
3165 overlap:
3166 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10003167 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 return 0;
3169}
3170
NeilBrownd1688a62011-10-11 16:49:52 +11003171static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08003172
NeilBrownd1688a62011-10-11 16:49:52 +11003173static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003174 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08003175{
NeilBrown784052e2009-03-31 15:19:07 +11003176 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10003177 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11003178 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003179 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11003180 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003181
NeilBrown112bf892009-03-31 14:39:38 +11003182 raid5_compute_sector(conf,
3183 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08003184 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11003185 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003186 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003187}
3188
Dan Williamsa4456852007-07-09 11:56:43 -07003189static void
NeilBrownd1688a62011-10-11 16:49:52 +11003190handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003191 struct stripe_head_state *s, int disks,
NeilBrown34a6f802015-08-14 12:07:57 +10003192 struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003193{
3194 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003195 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003196 for (i = disks; i--; ) {
3197 struct bio *bi;
3198 int bitmap_end = 0;
3199
3200 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003201 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07003202 rcu_read_lock();
3203 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownf5b67ae2016-06-02 16:19:53 +10003204 if (rdev && test_bit(In_sync, &rdev->flags) &&
3205 !test_bit(Faulty, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10003206 atomic_inc(&rdev->nr_pending);
3207 else
3208 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003209 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10003210 if (rdev) {
3211 if (!rdev_set_badblocks(
3212 rdev,
3213 sh->sector,
3214 STRIPE_SECTORS, 0))
3215 md_error(conf->mddev, rdev);
3216 rdev_dec_pending(rdev, conf->mddev);
3217 }
Dan Williamsa4456852007-07-09 11:56:43 -07003218 }
Shaohua Lib17459c2012-07-19 16:01:31 +10003219 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003220 /* fail all writes first */
3221 bi = sh->dev[i].towrite;
3222 sh->dev[i].towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11003223 sh->overwrite_disks = 0;
Shaohua Lib17459c2012-07-19 16:01:31 +10003224 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11003225 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003226 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07003227
Shaohua Li0576b1c2015-08-13 14:32:00 -07003228 r5l_stripe_write_finished(sh);
3229
Dan Williamsa4456852007-07-09 11:56:43 -07003230 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3231 wake_up(&conf->wait_for_overlap);
3232
Kent Overstreet4f024f32013-10-11 15:44:27 -07003233 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003234 sh->dev[i].sector + STRIPE_SECTORS) {
3235 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003236
3237 bi->bi_error = -EIO;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003238 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003239 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003240 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003241 }
3242 bi = nextbi;
3243 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003244 if (bitmap_end)
3245 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3246 STRIPE_SECTORS, 0, 0);
3247 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003248 /* and fail all 'written' */
3249 bi = sh->dev[i].written;
3250 sh->dev[i].written = NULL;
Shaohua Lid592a992014-05-21 17:57:44 +08003251 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3252 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3253 sh->dev[i].page = sh->dev[i].orig_page;
3254 }
3255
Dan Williamsa4456852007-07-09 11:56:43 -07003256 if (bi) bitmap_end = 1;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003257 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003258 sh->dev[i].sector + STRIPE_SECTORS) {
3259 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003260
3261 bi->bi_error = -EIO;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003262 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003263 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003264 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003265 }
3266 bi = bi2;
3267 }
3268
Dan Williamsb5e98d62007-01-02 13:52:31 -07003269 /* fail any reads if this device is non-operational and
3270 * the data has not reached the cache yet.
3271 */
3272 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
Shaohua Li6e74a9c2015-10-08 21:54:08 -07003273 s->failed > conf->max_degraded &&
Dan Williamsb5e98d62007-01-02 13:52:31 -07003274 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3275 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11003276 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003277 bi = sh->dev[i].toread;
3278 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11003279 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003280 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3281 wake_up(&conf->wait_for_overlap);
Shaohua Liebda7802015-09-18 10:20:13 -07003282 if (bi)
3283 s->to_read--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003284 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003285 sh->dev[i].sector + STRIPE_SECTORS) {
3286 struct bio *nextbi =
3287 r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003288
3289 bi->bi_error = -EIO;
NeilBrown34a6f802015-08-14 12:07:57 +10003290 if (!raid5_dec_bi_active_stripes(bi))
3291 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003292 bi = nextbi;
3293 }
3294 }
Dan Williamsa4456852007-07-09 11:56:43 -07003295 if (bitmap_end)
3296 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3297 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10003298 /* If we were in the middle of a write the parity block might
3299 * still be locked - so just clear all R5_LOCKED flags
3300 */
3301 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003302 }
Shaohua Liebda7802015-09-18 10:20:13 -07003303 s->to_write = 0;
3304 s->written = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003305
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003306 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3307 if (atomic_dec_and_test(&conf->pending_full_writes))
3308 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003309}
3310
NeilBrown7f0da592011-07-28 11:39:22 +10003311static void
NeilBrownd1688a62011-10-11 16:49:52 +11003312handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10003313 struct stripe_head_state *s)
3314{
3315 int abort = 0;
3316 int i;
3317
shli@kernel.org59fc6302014-12-15 12:57:03 +11003318 BUG_ON(sh->batch_head);
NeilBrown7f0da592011-07-28 11:39:22 +10003319 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003320 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3321 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10003322 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11003323 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10003324 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10003325 * Don't even need to abort as that is handled elsewhere
3326 * if needed, and not always wanted e.g. if there is a known
3327 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11003328 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10003329 * non-sync devices, or abort the recovery
3330 */
NeilBrown18b98372012-04-01 23:48:38 +10003331 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3332 /* During recovery devices cannot be removed, so
3333 * locking and refcounting of rdevs is not needed
3334 */
NeilBrowne50d3992016-06-02 16:19:52 +10003335 rcu_read_lock();
NeilBrown18b98372012-04-01 23:48:38 +10003336 for (i = 0; i < conf->raid_disks; i++) {
NeilBrowne50d3992016-06-02 16:19:52 +10003337 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown18b98372012-04-01 23:48:38 +10003338 if (rdev
3339 && !test_bit(Faulty, &rdev->flags)
3340 && !test_bit(In_sync, &rdev->flags)
3341 && !rdev_set_badblocks(rdev, sh->sector,
3342 STRIPE_SECTORS, 0))
3343 abort = 1;
NeilBrowne50d3992016-06-02 16:19:52 +10003344 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown18b98372012-04-01 23:48:38 +10003345 if (rdev
3346 && !test_bit(Faulty, &rdev->flags)
3347 && !test_bit(In_sync, &rdev->flags)
3348 && !rdev_set_badblocks(rdev, sh->sector,
3349 STRIPE_SECTORS, 0))
3350 abort = 1;
3351 }
NeilBrowne50d3992016-06-02 16:19:52 +10003352 rcu_read_unlock();
NeilBrown18b98372012-04-01 23:48:38 +10003353 if (abort)
3354 conf->recovery_disabled =
3355 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10003356 }
NeilBrown18b98372012-04-01 23:48:38 +10003357 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10003358}
3359
NeilBrown9a3e1102011-12-23 10:17:53 +11003360static int want_replace(struct stripe_head *sh, int disk_idx)
3361{
3362 struct md_rdev *rdev;
3363 int rv = 0;
NeilBrown3f232d62016-06-02 16:19:52 +10003364
3365 rcu_read_lock();
3366 rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
NeilBrown9a3e1102011-12-23 10:17:53 +11003367 if (rdev
3368 && !test_bit(Faulty, &rdev->flags)
3369 && !test_bit(In_sync, &rdev->flags)
3370 && (rdev->recovery_offset <= sh->sector
3371 || rdev->mddev->recovery_cp <= sh->sector))
3372 rv = 1;
NeilBrown3f232d62016-06-02 16:19:52 +10003373 rcu_read_unlock();
NeilBrown9a3e1102011-12-23 10:17:53 +11003374 return rv;
3375}
3376
NeilBrown2c58f062015-02-02 11:32:23 +11003377static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3378 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07003379{
3380 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10003381 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3382 &sh->dev[s->failed_num[1]] };
NeilBrownea664c82015-02-02 14:03:28 +11003383 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07003384
NeilBrowna79cfe12015-02-02 11:37:59 +11003385
3386 if (test_bit(R5_LOCKED, &dev->flags) ||
3387 test_bit(R5_UPTODATE, &dev->flags))
3388 /* No point reading this as we already have it or have
3389 * decided to get it.
3390 */
3391 return 0;
3392
3393 if (dev->toread ||
3394 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3395 /* We need this block to directly satisfy a request */
3396 return 1;
3397
3398 if (s->syncing || s->expanding ||
3399 (s->replacing && want_replace(sh, disk_idx)))
3400 /* When syncing, or expanding we read everything.
3401 * When replacing, we need the replaced block.
3402 */
3403 return 1;
3404
3405 if ((s->failed >= 1 && fdev[0]->toread) ||
3406 (s->failed >= 2 && fdev[1]->toread))
3407 /* If we want to read from a failed device, then
3408 * we need to actually read every other device.
3409 */
3410 return 1;
3411
NeilBrowna9d56952015-02-02 11:49:10 +11003412 /* Sometimes neither read-modify-write nor reconstruct-write
3413 * cycles can work. In those cases we read every block we
3414 * can. Then the parity-update is certain to have enough to
3415 * work with.
3416 * This can only be a problem when we need to write something,
3417 * and some device has failed. If either of those tests
3418 * fail we need look no further.
3419 */
3420 if (!s->failed || !s->to_write)
3421 return 0;
3422
3423 if (test_bit(R5_Insync, &dev->flags) &&
3424 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3425 /* Pre-reads at not permitted until after short delay
3426 * to gather multiple requests. However if this
3427 * device is no Insync, the block could only be be computed
3428 * and there is no need to delay that.
3429 */
3430 return 0;
NeilBrownea664c82015-02-02 14:03:28 +11003431
NeilBrown36707bb2015-09-24 15:25:36 +10003432 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrownea664c82015-02-02 14:03:28 +11003433 if (fdev[i]->towrite &&
3434 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3435 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3436 /* If we have a partial write to a failed
3437 * device, then we will need to reconstruct
3438 * the content of that device, so all other
3439 * devices must be read.
3440 */
3441 return 1;
3442 }
3443
3444 /* If we are forced to do a reconstruct-write, either because
3445 * the current RAID6 implementation only supports that, or
3446 * or because parity cannot be trusted and we are currently
3447 * recovering it, there is extra need to be careful.
3448 * If one of the devices that we would need to read, because
3449 * it is not being overwritten (and maybe not written at all)
3450 * is missing/faulty, then we need to read everything we can.
3451 */
3452 if (sh->raid_conf->level != 6 &&
3453 sh->sector < sh->raid_conf->mddev->recovery_cp)
3454 /* reconstruct-write isn't being forced */
3455 return 0;
NeilBrown36707bb2015-09-24 15:25:36 +10003456 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrown10d82c52015-05-08 18:19:33 +10003457 if (s->failed_num[i] != sh->pd_idx &&
3458 s->failed_num[i] != sh->qd_idx &&
3459 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
NeilBrownea664c82015-02-02 14:03:28 +11003460 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3461 return 1;
3462 }
3463
NeilBrown2c58f062015-02-02 11:32:23 +11003464 return 0;
3465}
3466
Song Liuba026842017-01-12 17:22:42 -08003467/* fetch_block - checks the given member device to see if its data needs
3468 * to be read or computed to satisfy a request.
3469 *
3470 * Returns 1 when no more member devices need to be checked, otherwise returns
3471 * 0 to tell the loop in handle_stripe_fill to continue
3472 */
NeilBrown2c58f062015-02-02 11:32:23 +11003473static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3474 int disk_idx, int disks)
3475{
3476 struct r5dev *dev = &sh->dev[disk_idx];
3477
3478 /* is the data in this block needed, and can we get it? */
3479 if (need_this_block(sh, s, disk_idx, disks)) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003480 /* we would like to get this block, possibly by computing it,
3481 * otherwise read it if the backing disk is insync
3482 */
3483 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3484 BUG_ON(test_bit(R5_Wantread, &dev->flags));
NeilBrownb0c783b2015-05-08 18:19:32 +10003485 BUG_ON(sh->batch_head);
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003486 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10003487 (s->failed && (disk_idx == s->failed_num[0] ||
3488 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003489 /* have disk failed, and we're requested to fetch it;
3490 * do compute it
3491 */
3492 pr_debug("Computing stripe %llu block %d\n",
3493 (unsigned long long)sh->sector, disk_idx);
3494 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3495 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3496 set_bit(R5_Wantcompute, &dev->flags);
3497 sh->ops.target = disk_idx;
3498 sh->ops.target2 = -1; /* no 2nd target */
3499 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10003500 /* Careful: from this point on 'uptodate' is in the eye
3501 * of raid_run_ops which services 'compute' operations
3502 * before writes. R5_Wantcompute flags a block that will
3503 * be R5_UPTODATE by the time it is needed for a
3504 * subsequent operation.
3505 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003506 s->uptodate++;
3507 return 1;
3508 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3509 /* Computing 2-failure is *very* expensive; only
3510 * do it if failed >= 2
3511 */
3512 int other;
3513 for (other = disks; other--; ) {
3514 if (other == disk_idx)
3515 continue;
3516 if (!test_bit(R5_UPTODATE,
3517 &sh->dev[other].flags))
3518 break;
3519 }
3520 BUG_ON(other < 0);
3521 pr_debug("Computing stripe %llu blocks %d,%d\n",
3522 (unsigned long long)sh->sector,
3523 disk_idx, other);
3524 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3525 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3526 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3527 set_bit(R5_Wantcompute, &sh->dev[other].flags);
3528 sh->ops.target = disk_idx;
3529 sh->ops.target2 = other;
3530 s->uptodate += 2;
3531 s->req_compute = 1;
3532 return 1;
3533 } else if (test_bit(R5_Insync, &dev->flags)) {
3534 set_bit(R5_LOCKED, &dev->flags);
3535 set_bit(R5_Wantread, &dev->flags);
3536 s->locked++;
3537 pr_debug("Reading block %d (sync=%d)\n",
3538 disk_idx, s->syncing);
3539 }
3540 }
3541
3542 return 0;
3543}
3544
3545/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10003546 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003547 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10003548static void handle_stripe_fill(struct stripe_head *sh,
3549 struct stripe_head_state *s,
3550 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003551{
3552 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003553
3554 /* look for blocks to read/compute, skip this if a compute
3555 * is already in flight, or if the stripe contents are in the
3556 * midst of changing due to a write
3557 */
3558 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Song Liu07e83362017-01-23 17:12:58 -08003559 !sh->reconstruct_state) {
3560
3561 /*
3562 * For degraded stripe with data in journal, do not handle
3563 * read requests yet, instead, flush the stripe to raid
3564 * disks first, this avoids handling complex rmw of write
3565 * back cache (prexor with orig_page, and then xor with
3566 * page) in the read path
3567 */
3568 if (s->injournal && s->failed) {
3569 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
3570 r5c_make_stripe_write_out(sh);
3571 goto out;
3572 }
3573
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003574 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10003575 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003576 break;
Song Liu07e83362017-01-23 17:12:58 -08003577 }
3578out:
Dan Williamsa4456852007-07-09 11:56:43 -07003579 set_bit(STRIPE_HANDLE, &sh->state);
3580}
3581
NeilBrown787b76f2015-05-21 12:56:41 +10003582static void break_stripe_batch_list(struct stripe_head *head_sh,
3583 unsigned long handle_flags);
Dan Williams1fe797e2008-06-28 09:16:30 +10003584/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07003585 * any written block on an uptodate or failed drive can be returned.
3586 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3587 * never LOCKED, so we don't need to test 'failed' directly.
3588 */
NeilBrownd1688a62011-10-11 16:49:52 +11003589static void handle_stripe_clean_event(struct r5conf *conf,
NeilBrown34a6f802015-08-14 12:07:57 +10003590 struct stripe_head *sh, int disks, struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003591{
3592 int i;
3593 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003594 int discard_pending = 0;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003595 struct stripe_head *head_sh = sh;
3596 bool do_endio = false;
Dan Williamsa4456852007-07-09 11:56:43 -07003597
3598 for (i = disks; i--; )
3599 if (sh->dev[i].written) {
3600 dev = &sh->dev[i];
3601 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003602 (test_bit(R5_UPTODATE, &dev->flags) ||
Shaohua Lid592a992014-05-21 17:57:44 +08003603 test_bit(R5_Discard, &dev->flags) ||
3604 test_bit(R5_SkipCopy, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003605 /* We can return any write requests */
3606 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003607 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003608 if (test_and_clear_bit(R5_Discard, &dev->flags))
3609 clear_bit(R5_UPTODATE, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08003610 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3611 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
Shaohua Lid592a992014-05-21 17:57:44 +08003612 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003613 do_endio = true;
3614
3615returnbi:
3616 dev->page = dev->orig_page;
Dan Williamsa4456852007-07-09 11:56:43 -07003617 wbi = dev->written;
3618 dev->written = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003619 while (wbi && wbi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003620 dev->sector + STRIPE_SECTORS) {
3621 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10003622 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003623 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003624 bio_list_add(return_bi, wbi);
Dan Williamsa4456852007-07-09 11:56:43 -07003625 }
3626 wbi = wbi2;
3627 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003628 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3629 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07003630 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003631 0);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003632 if (head_sh->batch_head) {
3633 sh = list_first_entry(&sh->batch_list,
3634 struct stripe_head,
3635 batch_list);
3636 if (sh != head_sh) {
3637 dev = &sh->dev[i];
3638 goto returnbi;
3639 }
3640 }
3641 sh = head_sh;
3642 dev = &sh->dev[i];
NeilBrownf8dfcff2013-03-12 12:18:06 +11003643 } else if (test_bit(R5_Discard, &dev->flags))
3644 discard_pending = 1;
3645 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -07003646
Shaohua Li0576b1c2015-08-13 14:32:00 -07003647 r5l_stripe_write_finished(sh);
3648
NeilBrownf8dfcff2013-03-12 12:18:06 +11003649 if (!discard_pending &&
3650 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003651 int hash;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003652 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3653 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3654 if (sh->qd_idx >= 0) {
3655 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3656 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3657 }
3658 /* now that discard is done we can proceed with any sync */
3659 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08003660 /*
3661 * SCSI discard will change some bio fields and the stripe has
3662 * no updated data, so remove it from hash list and the stripe
3663 * will be reinitialized
3664 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11003665unhash:
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003666 hash = sh->hash_lock_index;
3667 spin_lock_irq(conf->hash_locks + hash);
Shaohua Lid47648f2013-10-19 14:51:42 +08003668 remove_hash(sh);
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003669 spin_unlock_irq(conf->hash_locks + hash);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003670 if (head_sh->batch_head) {
3671 sh = list_first_entry(&sh->batch_list,
3672 struct stripe_head, batch_list);
3673 if (sh != head_sh)
3674 goto unhash;
3675 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003676 sh = head_sh;
3677
NeilBrownf8dfcff2013-03-12 12:18:06 +11003678 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3679 set_bit(STRIPE_HANDLE, &sh->state);
3680
3681 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003682
3683 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3684 if (atomic_dec_and_test(&conf->pending_full_writes))
3685 md_wakeup_thread(conf->mddev->thread);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003686
NeilBrown787b76f2015-05-21 12:56:41 +10003687 if (head_sh->batch_head && do_endio)
3688 break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
Dan Williamsa4456852007-07-09 11:56:43 -07003689}
3690
Song Liu86aa1392017-01-12 17:22:41 -08003691/*
3692 * For RMW in write back cache, we need extra page in prexor to store the
3693 * old data. This page is stored in dev->orig_page.
3694 *
3695 * This function checks whether we have data for prexor. The exact logic
3696 * is:
3697 * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
3698 */
3699static inline bool uptodate_for_rmw(struct r5dev *dev)
3700{
3701 return (test_bit(R5_UPTODATE, &dev->flags)) &&
3702 (!test_bit(R5_InJournal, &dev->flags) ||
3703 test_bit(R5_OrigPageUPTDODATE, &dev->flags));
3704}
3705
Song Liud7bd3982016-11-23 22:50:39 -08003706static int handle_stripe_dirtying(struct r5conf *conf,
3707 struct stripe_head *sh,
3708 struct stripe_head_state *s,
3709 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003710{
3711 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003712 sector_t recovery_cp = conf->mddev->recovery_cp;
3713
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003714 /* Check whether resync is now happening or should start.
Alexander Lyakasa7854482012-10-11 13:50:12 +11003715 * If yes, then the array is dirty (after unclean shutdown or
3716 * initial creation), so parity in some stripes might be inconsistent.
3717 * In this case, we need to always do reconstruct-write, to ensure
3718 * that in case of drive failure or read-error correction, we
3719 * generate correct data from the parity.
3720 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003721 if (conf->rmw_level == PARITY_DISABLE_RMW ||
NeilBrown26ac1072015-02-18 11:35:14 +11003722 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
3723 s->failed == 0)) {
Alexander Lyakasa7854482012-10-11 13:50:12 +11003724 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10003725 * look like rcw is cheaper
3726 */
3727 rcw = 1; rmw = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003728 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
3729 conf->rmw_level, (unsigned long long)recovery_cp,
Alexander Lyakasa7854482012-10-11 13:50:12 +11003730 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10003731 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003732 /* would I have to read this buffer for read_modify_write */
3733 struct r5dev *dev = &sh->dev[i];
Song Liu07e83362017-01-23 17:12:58 -08003734 if (((dev->towrite && !delay_towrite(dev, s)) ||
3735 i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08003736 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003737 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08003738 !(uptodate_for_rmw(dev) ||
Dan Williamsf38e1212007-01-02 13:52:30 -07003739 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003740 if (test_bit(R5_Insync, &dev->flags))
3741 rmw++;
3742 else
3743 rmw += 2*disks; /* cannot read it */
3744 }
3745 /* Would I have to read this buffer for reconstruct_write */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003746 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3747 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003748 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003749 !(test_bit(R5_UPTODATE, &dev->flags) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003750 test_bit(R5_Wantcompute, &dev->flags))) {
NeilBrown67f45542014-05-28 13:39:22 +10003751 if (test_bit(R5_Insync, &dev->flags))
3752 rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07003753 else
3754 rcw += 2*disks;
3755 }
3756 }
Song Liu1e6d6902016-11-17 15:24:39 -08003757
Dan Williams45b42332007-07-09 11:56:43 -07003758 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003759 (unsigned long long)sh->sector, rmw, rcw);
3760 set_bit(STRIPE_HANDLE, &sh->state);
Song Liu41257582016-05-23 17:25:06 -07003761 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003762 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003763 if (conf->mddev->queue)
3764 blk_add_trace_msg(conf->mddev->queue,
3765 "raid5 rmw %llu %d",
3766 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07003767 for (i = disks; i--; ) {
3768 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08003769 if (test_bit(R5_InJournal, &dev->flags) &&
3770 dev->page == dev->orig_page &&
3771 !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) {
3772 /* alloc page for prexor */
Song Liud7bd3982016-11-23 22:50:39 -08003773 struct page *p = alloc_page(GFP_NOIO);
Song Liu1e6d6902016-11-17 15:24:39 -08003774
Song Liud7bd3982016-11-23 22:50:39 -08003775 if (p) {
3776 dev->orig_page = p;
3777 continue;
3778 }
3779
3780 /*
3781 * alloc_page() failed, try use
3782 * disk_info->extra_page
3783 */
3784 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE,
3785 &conf->cache_state)) {
3786 r5c_use_extra_page(sh);
3787 break;
3788 }
3789
3790 /* extra_page in use, add to delayed_list */
3791 set_bit(STRIPE_DELAYED, &sh->state);
3792 s->waiting_extra_page = 1;
3793 return -EAGAIN;
Song Liu1e6d6902016-11-17 15:24:39 -08003794 }
Song Liud7bd3982016-11-23 22:50:39 -08003795 }
Song Liu1e6d6902016-11-17 15:24:39 -08003796
Song Liud7bd3982016-11-23 22:50:39 -08003797 for (i = disks; i--; ) {
3798 struct r5dev *dev = &sh->dev[i];
Song Liu07e83362017-01-23 17:12:58 -08003799 if (((dev->towrite && !delay_towrite(dev, s)) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003800 i == sh->pd_idx || i == sh->qd_idx ||
3801 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003802 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08003803 !(uptodate_for_rmw(dev) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003804 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003805 test_bit(R5_Insync, &dev->flags)) {
NeilBrown67f45542014-05-28 13:39:22 +10003806 if (test_bit(STRIPE_PREREAD_ACTIVE,
3807 &sh->state)) {
3808 pr_debug("Read_old block %d for r-m-w\n",
3809 i);
Dan Williamsa4456852007-07-09 11:56:43 -07003810 set_bit(R5_LOCKED, &dev->flags);
3811 set_bit(R5_Wantread, &dev->flags);
3812 s->locked++;
3813 } else {
3814 set_bit(STRIPE_DELAYED, &sh->state);
3815 set_bit(STRIPE_HANDLE, &sh->state);
3816 }
3817 }
3818 }
NeilBrowna9add5d2012-10-31 11:59:09 +11003819 }
Song Liu41257582016-05-23 17:25:06 -07003820 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003821 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11003822 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10003823 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003824 for (i = disks; i--; ) {
3825 struct r5dev *dev = &sh->dev[i];
3826 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10003827 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003828 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003829 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10003830 test_bit(R5_Wantcompute, &dev->flags))) {
3831 rcw++;
NeilBrown67f45542014-05-28 13:39:22 +10003832 if (test_bit(R5_Insync, &dev->flags) &&
3833 test_bit(STRIPE_PREREAD_ACTIVE,
3834 &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07003835 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07003836 "%d for Reconstruct\n", i);
3837 set_bit(R5_LOCKED, &dev->flags);
3838 set_bit(R5_Wantread, &dev->flags);
3839 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11003840 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07003841 } else {
3842 set_bit(STRIPE_DELAYED, &sh->state);
3843 set_bit(STRIPE_HANDLE, &sh->state);
3844 }
3845 }
3846 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003847 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11003848 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3849 (unsigned long long)sh->sector,
3850 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10003851 }
NeilBrownb1b02fe2015-02-02 10:44:29 +11003852
3853 if (rcw > disks && rmw > disks &&
3854 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3855 set_bit(STRIPE_DELAYED, &sh->state);
3856
Dan Williamsa4456852007-07-09 11:56:43 -07003857 /* now if nothing is locked, and if we have enough data,
3858 * we can start a write request
3859 */
Dan Williamsf38e1212007-01-02 13:52:30 -07003860 /* since handle_stripe can be called at any time we need to handle the
3861 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07003862 * subsequent call wants to start a write request. raid_run_ops only
3863 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07003864 * simultaneously. If this is not the case then new writes need to be
3865 * held off until the compute completes.
3866 */
Dan Williams976ea8d2008-06-28 08:32:03 +10003867 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3868 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
Song Liu1e6d6902016-11-17 15:24:39 -08003869 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003870 schedule_reconstruction(sh, s, rcw == 0, 0);
Song Liud7bd3982016-11-23 22:50:39 -08003871 return 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003872}
3873
NeilBrownd1688a62011-10-11 16:49:52 +11003874static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003875 struct stripe_head_state *s, int disks)
3876{
Dan Williamsecc65c92008-06-28 08:31:57 +10003877 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07003878
shli@kernel.org59fc6302014-12-15 12:57:03 +11003879 BUG_ON(sh->batch_head);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003880 set_bit(STRIPE_HANDLE, &sh->state);
3881
Dan Williamsecc65c92008-06-28 08:31:57 +10003882 switch (sh->check_state) {
3883 case check_state_idle:
3884 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07003885 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07003886 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10003887 sh->check_state = check_state_run;
3888 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003889 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003890 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10003891 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07003892 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003893 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10003894 /* fall through */
3895 case check_state_compute_result:
3896 sh->check_state = check_state_idle;
3897 if (!dev)
3898 dev = &sh->dev[sh->pd_idx];
3899
3900 /* check that a write has not made the stripe insync */
3901 if (test_bit(STRIPE_INSYNC, &sh->state))
3902 break;
3903
3904 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07003905 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3906 BUG_ON(s->uptodate != disks);
3907
3908 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10003909 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003910 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003911
Dan Williamsa4456852007-07-09 11:56:43 -07003912 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003913 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003914 break;
3915 case check_state_run:
3916 break; /* we will be called again upon completion */
3917 case check_state_check_result:
3918 sh->check_state = check_state_idle;
3919
3920 /* if a failure occurred during the check operation, leave
3921 * STRIPE_INSYNC not set and let the stripe be handled again
3922 */
3923 if (s->failed)
3924 break;
3925
3926 /* handle a successful check operation, if parity is correct
3927 * we are done. Otherwise update the mismatch count and repair
3928 * parity if !MD_RECOVERY_CHECK
3929 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003930 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003931 /* parity is correct (on disc,
3932 * not in buffer any more)
3933 */
3934 set_bit(STRIPE_INSYNC, &sh->state);
3935 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003936 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003937 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3938 /* don't try to repair!! */
3939 set_bit(STRIPE_INSYNC, &sh->state);
3940 else {
3941 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003942 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003943 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3944 set_bit(R5_Wantcompute,
3945 &sh->dev[sh->pd_idx].flags);
3946 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003947 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003948 s->uptodate++;
3949 }
3950 }
3951 break;
3952 case check_state_compute_run:
3953 break;
3954 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11003955 pr_err("%s: unknown check_state: %d sector: %llu\n",
Dan Williamsecc65c92008-06-28 08:31:57 +10003956 __func__, sh->check_state,
3957 (unsigned long long) sh->sector);
3958 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003959 }
3960}
3961
NeilBrownd1688a62011-10-11 16:49:52 +11003962static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003963 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003964 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003965{
Dan Williamsa4456852007-07-09 11:56:43 -07003966 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003967 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003968 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003969
shli@kernel.org59fc6302014-12-15 12:57:03 +11003970 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003971 set_bit(STRIPE_HANDLE, &sh->state);
3972
3973 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003974
Dan Williamsa4456852007-07-09 11:56:43 -07003975 /* Want to check and possibly repair P and Q.
3976 * However there could be one 'failed' device, in which
3977 * case we can only check one of them, possibly using the
3978 * other to generate missing data
3979 */
3980
Dan Williamsd82dfee2009-07-14 13:40:57 -07003981 switch (sh->check_state) {
3982 case check_state_idle:
3983 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003984 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003985 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003986 * makes sense to check P (If anything else were failed,
3987 * we would have used P to recreate it).
3988 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003989 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003990 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003991 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003992 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003993 * anything, so it makes sense to check it
3994 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003995 if (sh->check_state == check_state_run)
3996 sh->check_state = check_state_run_pq;
3997 else
3998 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003999 }
Dan Williams36d1c642009-07-14 11:48:22 -07004000
Dan Williamsd82dfee2009-07-14 13:40:57 -07004001 /* discard potentially stale zero_sum_result */
4002 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07004003
Dan Williamsd82dfee2009-07-14 13:40:57 -07004004 if (sh->check_state == check_state_run) {
4005 /* async_xor_zero_sum destroys the contents of P */
4006 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
4007 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07004008 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004009 if (sh->check_state >= check_state_run &&
4010 sh->check_state <= check_state_run_pq) {
4011 /* async_syndrome_zero_sum preserves P and Q, so
4012 * no need to mark them !uptodate here
4013 */
4014 set_bit(STRIPE_OP_CHECK, &s->ops_request);
4015 break;
4016 }
Dan Williams36d1c642009-07-14 11:48:22 -07004017
Dan Williamsd82dfee2009-07-14 13:40:57 -07004018 /* we have 2-disk failure */
4019 BUG_ON(s->failed != 2);
4020 /* fall through */
4021 case check_state_compute_result:
4022 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07004023
Dan Williamsd82dfee2009-07-14 13:40:57 -07004024 /* check that a write has not made the stripe insync */
4025 if (test_bit(STRIPE_INSYNC, &sh->state))
4026 break;
Dan Williamsa4456852007-07-09 11:56:43 -07004027
4028 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07004029 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07004030 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004031 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07004032 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004033 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07004034 s->locked++;
4035 set_bit(R5_LOCKED, &dev->flags);
4036 set_bit(R5_Wantwrite, &dev->flags);
4037 }
4038 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004039 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07004040 s->locked++;
4041 set_bit(R5_LOCKED, &dev->flags);
4042 set_bit(R5_Wantwrite, &dev->flags);
4043 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004044 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004045 dev = &sh->dev[pd_idx];
4046 s->locked++;
4047 set_bit(R5_LOCKED, &dev->flags);
4048 set_bit(R5_Wantwrite, &dev->flags);
4049 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004050 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004051 dev = &sh->dev[qd_idx];
4052 s->locked++;
4053 set_bit(R5_LOCKED, &dev->flags);
4054 set_bit(R5_Wantwrite, &dev->flags);
4055 }
4056 clear_bit(STRIPE_DEGRADED, &sh->state);
4057
4058 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004059 break;
4060 case check_state_run:
4061 case check_state_run_q:
4062 case check_state_run_pq:
4063 break; /* we will be called again upon completion */
4064 case check_state_check_result:
4065 sh->check_state = check_state_idle;
4066
4067 /* handle a successful check operation, if parity is correct
4068 * we are done. Otherwise update the mismatch count and repair
4069 * parity if !MD_RECOVERY_CHECK
4070 */
4071 if (sh->ops.zero_sum_result == 0) {
4072 /* both parities are correct */
4073 if (!s->failed)
4074 set_bit(STRIPE_INSYNC, &sh->state);
4075 else {
4076 /* in contrast to the raid5 case we can validate
4077 * parity, but still have a failure to write
4078 * back
4079 */
4080 sh->check_state = check_state_compute_result;
4081 /* Returning at this point means that we may go
4082 * off and bring p and/or q uptodate again so
4083 * we make sure to check zero_sum_result again
4084 * to verify if p or q need writeback
4085 */
4086 }
4087 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004088 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004089 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
4090 /* don't try to repair!! */
4091 set_bit(STRIPE_INSYNC, &sh->state);
4092 else {
4093 int *target = &sh->ops.target;
4094
4095 sh->ops.target = -1;
4096 sh->ops.target2 = -1;
4097 sh->check_state = check_state_compute_run;
4098 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4099 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4100 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4101 set_bit(R5_Wantcompute,
4102 &sh->dev[pd_idx].flags);
4103 *target = pd_idx;
4104 target = &sh->ops.target2;
4105 s->uptodate++;
4106 }
4107 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4108 set_bit(R5_Wantcompute,
4109 &sh->dev[qd_idx].flags);
4110 *target = qd_idx;
4111 s->uptodate++;
4112 }
4113 }
4114 }
4115 break;
4116 case check_state_compute_run:
4117 break;
4118 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11004119 pr_warn("%s: unknown check_state: %d sector: %llu\n",
4120 __func__, sh->check_state,
4121 (unsigned long long) sh->sector);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004122 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07004123 }
4124}
4125
NeilBrownd1688a62011-10-11 16:49:52 +11004126static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07004127{
4128 int i;
4129
4130 /* We have read all the blocks in this stripe and now we need to
4131 * copy some of them into a target stripe for expand.
4132 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07004133 struct dma_async_tx_descriptor *tx = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +11004134 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07004135 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4136 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11004137 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11004138 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07004139 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07004140 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07004141
Shaohua Li6d036f72015-08-13 14:31:57 -07004142 sector_t bn = raid5_compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11004143 sector_t s = raid5_compute_sector(conf, bn, 0,
4144 &dd_idx, NULL);
Shaohua Li6d036f72015-08-13 14:31:57 -07004145 sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07004146 if (sh2 == NULL)
4147 /* so far only the early blocks of this stripe
4148 * have been requested. When later blocks
4149 * get requested, we will try again
4150 */
4151 continue;
4152 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
4153 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
4154 /* must have already done this block */
Shaohua Li6d036f72015-08-13 14:31:57 -07004155 raid5_release_stripe(sh2);
Dan Williamsa4456852007-07-09 11:56:43 -07004156 continue;
4157 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07004158
4159 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07004160 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004161 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07004162 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07004163 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004164
Dan Williamsa4456852007-07-09 11:56:43 -07004165 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
4166 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
4167 for (j = 0; j < conf->raid_disks; j++)
4168 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10004169 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07004170 !test_bit(R5_Expanded, &sh2->dev[j].flags))
4171 break;
4172 if (j == conf->raid_disks) {
4173 set_bit(STRIPE_EXPAND_READY, &sh2->state);
4174 set_bit(STRIPE_HANDLE, &sh2->state);
4175 }
Shaohua Li6d036f72015-08-13 14:31:57 -07004176 raid5_release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004177
Dan Williamsa4456852007-07-09 11:56:43 -07004178 }
NeilBrowna2e08552007-09-11 15:23:36 -07004179 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11004180 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07004181}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
4183/*
4184 * handle_stripe - do things to a stripe.
4185 *
NeilBrown9a3e1102011-12-23 10:17:53 +11004186 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4187 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11004189 * return some read requests which now have data
4190 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 * schedule a read on some buffers
4192 * schedule a write of some buffers
4193 * return confirmation of parity correctness
4194 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 */
Dan Williamsa4456852007-07-09 11:56:43 -07004196
NeilBrownacfe7262011-07-27 11:00:36 +10004197static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07004198{
NeilBrownd1688a62011-10-11 16:49:52 +11004199 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08004200 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004201 struct r5dev *dev;
4202 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11004203 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07004204
NeilBrownacfe7262011-07-27 11:00:36 +10004205 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07004206
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004207 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
4208 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
NeilBrownacfe7262011-07-27 11:00:36 +10004209 s->failed_num[0] = -1;
4210 s->failed_num[1] = -1;
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004211 s->log_failed = r5l_log_disk_error(conf);
NeilBrownacfe7262011-07-27 11:00:36 +10004212
4213 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07004214 rcu_read_lock();
4215 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004216 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10004217 sector_t first_bad;
4218 int bad_sectors;
4219 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10004220
NeilBrown16a53ec2006-06-26 00:27:38 -07004221 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07004222
Dan Williams45b42332007-07-09 11:56:43 -07004223 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11004224 i, dev->flags,
4225 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004226 /* maybe we can reply to a read
4227 *
4228 * new wantfill requests are only permitted while
4229 * ops_complete_biofill is guaranteed to be inactive
4230 */
4231 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4232 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4233 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07004234
4235 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10004236 if (test_bit(R5_LOCKED, &dev->flags))
4237 s->locked++;
4238 if (test_bit(R5_UPTODATE, &dev->flags))
4239 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004240 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004241 s->compute++;
4242 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004243 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004244
NeilBrownacfe7262011-07-27 11:00:36 +10004245 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004246 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10004247 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10004248 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004249 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10004250 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004251 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004252 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004253 }
Dan Williamsa4456852007-07-09 11:56:43 -07004254 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10004255 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11004256 /* Prefer to use the replacement for reads, but only
4257 * if it is recovered enough and has no bad blocks.
4258 */
4259 rdev = rcu_dereference(conf->disks[i].replacement);
4260 if (rdev && !test_bit(Faulty, &rdev->flags) &&
4261 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
4262 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4263 &first_bad, &bad_sectors))
4264 set_bit(R5_ReadRepl, &dev->flags);
4265 else {
NeilBrowne6030cb2015-07-17 13:26:23 +10004266 if (rdev && !test_bit(Faulty, &rdev->flags))
NeilBrown9a3e1102011-12-23 10:17:53 +11004267 set_bit(R5_NeedReplace, &dev->flags);
NeilBrowne6030cb2015-07-17 13:26:23 +10004268 else
4269 clear_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11004270 rdev = rcu_dereference(conf->disks[i].rdev);
4271 clear_bit(R5_ReadRepl, &dev->flags);
4272 }
NeilBrown9283d8c2011-12-08 16:27:57 +11004273 if (rdev && test_bit(Faulty, &rdev->flags))
4274 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10004275 if (rdev) {
4276 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4277 &first_bad, &bad_sectors);
4278 if (s->blocked_rdev == NULL
4279 && (test_bit(Blocked, &rdev->flags)
4280 || is_bad < 0)) {
4281 if (is_bad < 0)
4282 set_bit(BlockedBadBlocks,
4283 &rdev->flags);
4284 s->blocked_rdev = rdev;
4285 atomic_inc(&rdev->nr_pending);
4286 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004287 }
NeilBrown415e72d2010-06-17 17:25:21 +10004288 clear_bit(R5_Insync, &dev->flags);
4289 if (!rdev)
4290 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10004291 else if (is_bad) {
4292 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10004293 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4294 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10004295 /* treat as in-sync, but with a read error
4296 * which we can now try to correct
4297 */
4298 set_bit(R5_Insync, &dev->flags);
4299 set_bit(R5_ReadError, &dev->flags);
4300 }
4301 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10004302 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11004303 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10004304 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11004305 set_bit(R5_Insync, &dev->flags);
4306 else if (test_bit(R5_UPTODATE, &dev->flags) &&
4307 test_bit(R5_Expanded, &dev->flags))
4308 /* If we've reshaped into here, we assume it is Insync.
4309 * We will shortly update recovery_offset to make
4310 * it official.
4311 */
4312 set_bit(R5_Insync, &dev->flags);
4313
NeilBrown1cc03eb2014-01-06 13:19:42 +11004314 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004315 /* This flag does not apply to '.replacement'
4316 * only to .rdev, so make sure to check that*/
4317 struct md_rdev *rdev2 = rcu_dereference(
4318 conf->disks[i].rdev);
4319 if (rdev2 == rdev)
4320 clear_bit(R5_Insync, &dev->flags);
4321 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004322 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004323 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10004324 } else
4325 clear_bit(R5_WriteError, &dev->flags);
4326 }
NeilBrown1cc03eb2014-01-06 13:19:42 +11004327 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004328 /* This flag does not apply to '.replacement'
4329 * only to .rdev, so make sure to check that*/
4330 struct md_rdev *rdev2 = rcu_dereference(
4331 conf->disks[i].rdev);
4332 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10004333 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004334 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10004335 } else
4336 clear_bit(R5_MadeGood, &dev->flags);
4337 }
NeilBrown977df362011-12-23 10:17:53 +11004338 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4339 struct md_rdev *rdev2 = rcu_dereference(
4340 conf->disks[i].replacement);
4341 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4342 s->handle_bad_blocks = 1;
4343 atomic_inc(&rdev2->nr_pending);
4344 } else
4345 clear_bit(R5_MadeGoodRepl, &dev->flags);
4346 }
NeilBrown415e72d2010-06-17 17:25:21 +10004347 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004348 /* The ReadError flag will just be confusing now */
4349 clear_bit(R5_ReadError, &dev->flags);
4350 clear_bit(R5_ReWrite, &dev->flags);
4351 }
NeilBrown415e72d2010-06-17 17:25:21 +10004352 if (test_bit(R5_ReadError, &dev->flags))
4353 clear_bit(R5_Insync, &dev->flags);
4354 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004355 if (s->failed < 2)
4356 s->failed_num[s->failed] = i;
4357 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11004358 if (rdev && !test_bit(Faulty, &rdev->flags))
4359 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10004360 }
Song Liu2ded3702016-11-17 15:24:38 -08004361
4362 if (test_bit(R5_InJournal, &dev->flags))
4363 s->injournal++;
Song Liu1e6d6902016-11-17 15:24:39 -08004364 if (test_bit(R5_InJournal, &dev->flags) && dev->written)
4365 s->just_cached++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004366 }
NeilBrown9a3e1102011-12-23 10:17:53 +11004367 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4368 /* If there is a failed device being replaced,
4369 * we must be recovering.
4370 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10004371 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11004372 * else we can only be replacing
4373 * sync and recovery both need to read all devices, and so
4374 * use the same flag.
4375 */
4376 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10004377 sh->sector >= conf->mddev->recovery_cp ||
4378 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11004379 s->syncing = 1;
4380 else
4381 s->replacing = 1;
4382 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004383 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10004384}
NeilBrownf4168852007-02-28 20:11:53 -08004385
shli@kernel.org59fc6302014-12-15 12:57:03 +11004386static int clear_batch_ready(struct stripe_head *sh)
4387{
NeilBrownb15a9db2015-05-22 15:20:04 +10004388 /* Return '1' if this is a member of batch, or
4389 * '0' if it is a lone stripe or a head which can now be
4390 * handled.
4391 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11004392 struct stripe_head *tmp;
4393 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
NeilBrownb15a9db2015-05-22 15:20:04 +10004394 return (sh->batch_head && sh->batch_head != sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11004395 spin_lock(&sh->stripe_lock);
4396 if (!sh->batch_head) {
4397 spin_unlock(&sh->stripe_lock);
4398 return 0;
4399 }
4400
4401 /*
4402 * this stripe could be added to a batch list before we check
4403 * BATCH_READY, skips it
4404 */
4405 if (sh->batch_head != sh) {
4406 spin_unlock(&sh->stripe_lock);
4407 return 1;
4408 }
4409 spin_lock(&sh->batch_lock);
4410 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4411 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4412 spin_unlock(&sh->batch_lock);
4413 spin_unlock(&sh->stripe_lock);
4414
4415 /*
4416 * BATCH_READY is cleared, no new stripes can be added.
4417 * batch_list can be accessed without lock
4418 */
4419 return 0;
4420}
4421
NeilBrown3960ce72015-05-21 12:20:36 +10004422static void break_stripe_batch_list(struct stripe_head *head_sh,
4423 unsigned long handle_flags)
shli@kernel.org72ac7332014-12-15 12:57:03 +11004424{
NeilBrown4e3d62f2015-05-21 11:50:16 +10004425 struct stripe_head *sh, *next;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004426 int i;
NeilBrownfb642b92015-05-21 12:00:47 +10004427 int do_wakeup = 0;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004428
NeilBrownbb270512015-05-08 18:19:40 +10004429 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4430
shli@kernel.org72ac7332014-12-15 12:57:03 +11004431 list_del_init(&sh->batch_list);
4432
Shaohua Lifb3229d2016-03-09 10:08:38 -08004433 WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
NeilBrown1b956f72015-05-21 12:40:26 +10004434 (1 << STRIPE_SYNCING) |
4435 (1 << STRIPE_REPLACED) |
NeilBrown1b956f72015-05-21 12:40:26 +10004436 (1 << STRIPE_DELAYED) |
4437 (1 << STRIPE_BIT_DELAY) |
4438 (1 << STRIPE_FULL_WRITE) |
4439 (1 << STRIPE_BIOFILL_RUN) |
4440 (1 << STRIPE_COMPUTE_RUN) |
4441 (1 << STRIPE_OPS_REQ_PENDING) |
4442 (1 << STRIPE_DISCARD) |
4443 (1 << STRIPE_BATCH_READY) |
4444 (1 << STRIPE_BATCH_ERR) |
Shaohua Lifb3229d2016-03-09 10:08:38 -08004445 (1 << STRIPE_BITMAP_PENDING)),
4446 "stripe state: %lx\n", sh->state);
4447 WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4448 (1 << STRIPE_REPLACED)),
4449 "head stripe state: %lx\n", head_sh->state);
NeilBrown1b956f72015-05-21 12:40:26 +10004450
4451 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
NeilBrown550da242016-03-09 12:58:25 +11004452 (1 << STRIPE_PREREAD_ACTIVE) |
NeilBrown1b956f72015-05-21 12:40:26 +10004453 (1 << STRIPE_DEGRADED)),
4454 head_sh->state & (1 << STRIPE_INSYNC));
4455
shli@kernel.org72ac7332014-12-15 12:57:03 +11004456 sh->check_state = head_sh->check_state;
4457 sh->reconstruct_state = head_sh->reconstruct_state;
NeilBrownfb642b92015-05-21 12:00:47 +10004458 for (i = 0; i < sh->disks; i++) {
4459 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4460 do_wakeup = 1;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004461 sh->dev[i].flags = head_sh->dev[i].flags &
4462 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
NeilBrownfb642b92015-05-21 12:00:47 +10004463 }
shli@kernel.org72ac7332014-12-15 12:57:03 +11004464 spin_lock_irq(&sh->stripe_lock);
4465 sh->batch_head = NULL;
4466 spin_unlock_irq(&sh->stripe_lock);
NeilBrown3960ce72015-05-21 12:20:36 +10004467 if (handle_flags == 0 ||
4468 sh->state & handle_flags)
4469 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07004470 raid5_release_stripe(sh);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004471 }
NeilBrownfb642b92015-05-21 12:00:47 +10004472 spin_lock_irq(&head_sh->stripe_lock);
4473 head_sh->batch_head = NULL;
4474 spin_unlock_irq(&head_sh->stripe_lock);
4475 for (i = 0; i < head_sh->disks; i++)
4476 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4477 do_wakeup = 1;
NeilBrown3960ce72015-05-21 12:20:36 +10004478 if (head_sh->state & handle_flags)
4479 set_bit(STRIPE_HANDLE, &head_sh->state);
NeilBrownfb642b92015-05-21 12:00:47 +10004480
4481 if (do_wakeup)
4482 wake_up(&head_sh->raid_conf->wait_for_overlap);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004483}
4484
NeilBrowncc940152011-07-26 11:35:35 +10004485static void handle_stripe(struct stripe_head *sh)
4486{
4487 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11004488 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10004489 int i;
NeilBrown84789552011-07-27 11:00:36 +10004490 int prexor;
4491 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004492 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10004493
4494 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11004495 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10004496 /* already being handled, ensure it gets handled
4497 * again when current action finishes */
4498 set_bit(STRIPE_HANDLE, &sh->state);
4499 return;
4500 }
4501
shli@kernel.org59fc6302014-12-15 12:57:03 +11004502 if (clear_batch_ready(sh) ) {
4503 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
4504 return;
4505 }
4506
NeilBrown4e3d62f2015-05-21 11:50:16 +10004507 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
NeilBrown3960ce72015-05-21 12:20:36 +10004508 break_stripe_batch_list(sh, 0);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004509
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004510 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
NeilBrownf8dfcff2013-03-12 12:18:06 +11004511 spin_lock(&sh->stripe_lock);
4512 /* Cannot process 'sync' concurrently with 'discard' */
4513 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
4514 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4515 set_bit(STRIPE_SYNCING, &sh->state);
4516 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10004517 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004518 }
4519 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10004520 }
4521 clear_bit(STRIPE_DELAYED, &sh->state);
4522
4523 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4524 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4525 (unsigned long long)sh->sector, sh->state,
4526 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4527 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10004528
NeilBrownacfe7262011-07-27 11:00:36 +10004529 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10004530
Shaohua Lib70abcb2015-08-13 14:31:58 -07004531 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
4532 goto finish;
4533
NeilBrownbc2607f2011-07-28 11:39:22 +10004534 if (s.handle_bad_blocks) {
4535 set_bit(STRIPE_HANDLE, &sh->state);
4536 goto finish;
4537 }
4538
NeilBrown474af965fe2011-07-27 11:00:36 +10004539 if (unlikely(s.blocked_rdev)) {
4540 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11004541 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10004542 set_bit(STRIPE_HANDLE, &sh->state);
4543 goto finish;
4544 }
4545 /* There is nothing for the blocked_rdev to block */
4546 rdev_dec_pending(s.blocked_rdev, conf->mddev);
4547 s.blocked_rdev = NULL;
4548 }
4549
4550 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4551 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4552 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4553 }
4554
4555 pr_debug("locked=%d uptodate=%d to_read=%d"
4556 " to_write=%d failed=%d failed_num=%d,%d\n",
4557 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4558 s.failed_num[0], s.failed_num[1]);
4559 /* check if the array has lost more than max_degraded devices and,
4560 * if so, some requests might need to be failed.
4561 */
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004562 if (s.failed > conf->max_degraded || s.log_failed) {
NeilBrown9a3f5302011-11-08 16:22:01 +11004563 sh->check_state = 0;
4564 sh->reconstruct_state = 0;
NeilBrown626f2092015-05-22 14:03:10 +10004565 break_stripe_batch_list(sh, 0);
NeilBrown9a3f5302011-11-08 16:22:01 +11004566 if (s.to_read+s.to_write+s.written)
4567 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11004568 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11004569 handle_failed_sync(conf, sh, &s);
4570 }
NeilBrown474af965fe2011-07-27 11:00:36 +10004571
NeilBrown84789552011-07-27 11:00:36 +10004572 /* Now we check to see if any write operations have recently
4573 * completed
4574 */
4575 prexor = 0;
4576 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4577 prexor = 1;
4578 if (sh->reconstruct_state == reconstruct_state_drain_result ||
4579 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
4580 sh->reconstruct_state = reconstruct_state_idle;
4581
4582 /* All the 'written' buffers and the parity block are ready to
4583 * be written back to disk
4584 */
Shaohua Li9e4447682012-10-11 13:49:49 +11004585 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
4586 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004587 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11004588 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
4589 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004590 for (i = disks; i--; ) {
4591 struct r5dev *dev = &sh->dev[i];
4592 if (test_bit(R5_LOCKED, &dev->flags) &&
4593 (i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08004594 dev->written || test_bit(R5_InJournal,
4595 &dev->flags))) {
NeilBrown84789552011-07-27 11:00:36 +10004596 pr_debug("Writing block %d\n", i);
4597 set_bit(R5_Wantwrite, &dev->flags);
4598 if (prexor)
4599 continue;
NeilBrown9c4bdf62014-08-13 09:57:07 +10004600 if (s.failed > 1)
4601 continue;
NeilBrown84789552011-07-27 11:00:36 +10004602 if (!test_bit(R5_Insync, &dev->flags) ||
4603 ((i == sh->pd_idx || i == sh->qd_idx) &&
4604 s.failed == 0))
4605 set_bit(STRIPE_INSYNC, &sh->state);
4606 }
4607 }
4608 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4609 s.dec_preread_active = 1;
4610 }
4611
NeilBrownef5b7c62012-11-22 09:13:36 +11004612 /*
4613 * might be able to return some write requests if the parity blocks
4614 * are safe, or on a failed drive
4615 */
4616 pdev = &sh->dev[sh->pd_idx];
4617 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
4618 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
4619 qdev = &sh->dev[sh->qd_idx];
4620 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
4621 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
4622 || conf->level < 6;
4623
4624 if (s.written &&
4625 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
4626 && !test_bit(R5_LOCKED, &pdev->flags)
4627 && (test_bit(R5_UPTODATE, &pdev->flags) ||
4628 test_bit(R5_Discard, &pdev->flags))))) &&
4629 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
4630 && !test_bit(R5_LOCKED, &qdev->flags)
4631 && (test_bit(R5_UPTODATE, &qdev->flags) ||
4632 test_bit(R5_Discard, &qdev->flags))))))
4633 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
4634
Song Liu1e6d6902016-11-17 15:24:39 -08004635 if (s.just_cached)
4636 r5c_handle_cached_data_endio(conf, sh, disks, &s.return_bi);
4637 r5l_stripe_write_finished(sh);
4638
NeilBrownef5b7c62012-11-22 09:13:36 +11004639 /* Now we might consider reading some blocks, either to check/generate
4640 * parity, or to satisfy requests
4641 * or to load a block that is being partially written.
4642 */
4643 if (s.to_read || s.non_overwrite
4644 || (conf->level == 6 && s.to_write && s.failed)
4645 || (s.syncing && (s.uptodate + s.compute < disks))
4646 || s.replacing
4647 || s.expanding)
4648 handle_stripe_fill(sh, &s, disks);
4649
Song Liu2ded3702016-11-17 15:24:38 -08004650 /*
4651 * When the stripe finishes full journal write cycle (write to journal
4652 * and raid disk), this is the clean up procedure so it is ready for
4653 * next operation.
4654 */
4655 r5c_finish_stripe_write_out(conf, sh, &s);
4656
4657 /*
4658 * Now to consider new write requests, cache write back and what else,
4659 * if anything should be read. We do not handle new writes when:
NeilBrown84789552011-07-27 11:00:36 +10004660 * 1/ A 'write' operation (copy+xor) is already in flight.
4661 * 2/ A 'check' operation is in flight, as it may clobber the parity
4662 * block.
Song Liu2ded3702016-11-17 15:24:38 -08004663 * 3/ A r5c cache log write is in flight.
NeilBrown84789552011-07-27 11:00:36 +10004664 */
Song Liu2ded3702016-11-17 15:24:38 -08004665
4666 if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
4667 if (!r5c_is_writeback(conf->log)) {
4668 if (s.to_write)
4669 handle_stripe_dirtying(conf, sh, &s, disks);
4670 } else { /* write back cache */
4671 int ret = 0;
4672
4673 /* First, try handle writes in caching phase */
4674 if (s.to_write)
4675 ret = r5c_try_caching_write(conf, sh, &s,
4676 disks);
4677 /*
4678 * If caching phase failed: ret == -EAGAIN
4679 * OR
4680 * stripe under reclaim: !caching && injournal
4681 *
4682 * fall back to handle_stripe_dirtying()
4683 */
4684 if (ret == -EAGAIN ||
4685 /* stripe under reclaim: !caching && injournal */
4686 (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
Song Liud7bd3982016-11-23 22:50:39 -08004687 s.injournal > 0)) {
4688 ret = handle_stripe_dirtying(conf, sh, &s,
4689 disks);
4690 if (ret == -EAGAIN)
4691 goto finish;
4692 }
Song Liu2ded3702016-11-17 15:24:38 -08004693 }
4694 }
NeilBrown84789552011-07-27 11:00:36 +10004695
4696 /* maybe we need to check and possibly fix the parity for this stripe
4697 * Any reads will already have been scheduled, so we just see if enough
4698 * data is available. The parity check is held off while parity
4699 * dependent operations are in flight.
4700 */
4701 if (sh->check_state ||
4702 (s.syncing && s.locked == 0 &&
4703 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
4704 !test_bit(STRIPE_INSYNC, &sh->state))) {
4705 if (conf->level == 6)
4706 handle_parity_checks6(conf, sh, &s, disks);
4707 else
4708 handle_parity_checks5(conf, sh, &s, disks);
4709 }
NeilBrownc5a31002011-07-27 11:00:36 +10004710
NeilBrownf94c0b62013-07-22 12:57:21 +10004711 if ((s.replacing || s.syncing) && s.locked == 0
4712 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
4713 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11004714 /* Write out to replacement devices where possible */
4715 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10004716 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
4717 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11004718 set_bit(R5_WantReplace, &sh->dev[i].flags);
4719 set_bit(R5_LOCKED, &sh->dev[i].flags);
4720 s.locked++;
4721 }
NeilBrownf94c0b62013-07-22 12:57:21 +10004722 if (s.replacing)
4723 set_bit(STRIPE_INSYNC, &sh->state);
4724 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11004725 }
4726 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10004727 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11004728 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10004729 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4730 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004731 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
4732 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10004733 }
4734
4735 /* If the failed drives are just a ReadError, then we might need
4736 * to progress the repair/check process
4737 */
4738 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
4739 for (i = 0; i < s.failed; i++) {
4740 struct r5dev *dev = &sh->dev[s.failed_num[i]];
4741 if (test_bit(R5_ReadError, &dev->flags)
4742 && !test_bit(R5_LOCKED, &dev->flags)
4743 && test_bit(R5_UPTODATE, &dev->flags)
4744 ) {
4745 if (!test_bit(R5_ReWrite, &dev->flags)) {
4746 set_bit(R5_Wantwrite, &dev->flags);
4747 set_bit(R5_ReWrite, &dev->flags);
4748 set_bit(R5_LOCKED, &dev->flags);
4749 s.locked++;
4750 } else {
4751 /* let's read it back */
4752 set_bit(R5_Wantread, &dev->flags);
4753 set_bit(R5_LOCKED, &dev->flags);
4754 s.locked++;
4755 }
4756 }
4757 }
4758
NeilBrown3687c062011-07-27 11:00:36 +10004759 /* Finish reconstruct operations initiated by the expansion process */
4760 if (sh->reconstruct_state == reconstruct_state_result) {
4761 struct stripe_head *sh_src
Shaohua Li6d036f72015-08-13 14:31:57 -07004762 = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrown3687c062011-07-27 11:00:36 +10004763 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
4764 /* sh cannot be written until sh_src has been read.
4765 * so arrange for sh to be delayed a little
4766 */
4767 set_bit(STRIPE_DELAYED, &sh->state);
4768 set_bit(STRIPE_HANDLE, &sh->state);
4769 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
4770 &sh_src->state))
4771 atomic_inc(&conf->preread_active_stripes);
Shaohua Li6d036f72015-08-13 14:31:57 -07004772 raid5_release_stripe(sh_src);
NeilBrown3687c062011-07-27 11:00:36 +10004773 goto finish;
4774 }
4775 if (sh_src)
Shaohua Li6d036f72015-08-13 14:31:57 -07004776 raid5_release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07004777
NeilBrown3687c062011-07-27 11:00:36 +10004778 sh->reconstruct_state = reconstruct_state_idle;
4779 clear_bit(STRIPE_EXPANDING, &sh->state);
4780 for (i = conf->raid_disks; i--; ) {
4781 set_bit(R5_Wantwrite, &sh->dev[i].flags);
4782 set_bit(R5_LOCKED, &sh->dev[i].flags);
4783 s.locked++;
4784 }
4785 }
4786
4787 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
4788 !sh->reconstruct_state) {
4789 /* Need to write out all blocks after computing parity */
4790 sh->disks = conf->raid_disks;
4791 stripe_set_idx(sh->sector, conf, 0, sh);
4792 schedule_reconstruction(sh, &s, 1, 1);
4793 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
4794 clear_bit(STRIPE_EXPAND_READY, &sh->state);
4795 atomic_dec(&conf->reshape_stripes);
4796 wake_up(&conf->wait_for_overlap);
4797 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4798 }
4799
4800 if (s.expanding && s.locked == 0 &&
4801 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
4802 handle_stripe_expansion(conf, sh);
4803
4804finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07004805 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10004806 if (unlikely(s.blocked_rdev)) {
4807 if (conf->mddev->external)
4808 md_wait_for_blocked_rdev(s.blocked_rdev,
4809 conf->mddev);
4810 else
4811 /* Internal metadata will immediately
4812 * be written by raid5d, so we don't
4813 * need to wait here.
4814 */
4815 rdev_dec_pending(s.blocked_rdev,
4816 conf->mddev);
4817 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004818
NeilBrownbc2607f2011-07-28 11:39:22 +10004819 if (s.handle_bad_blocks)
4820 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004821 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10004822 struct r5dev *dev = &sh->dev[i];
4823 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
4824 /* We own a safe reference to the rdev */
4825 rdev = conf->disks[i].rdev;
4826 if (!rdev_set_badblocks(rdev, sh->sector,
4827 STRIPE_SECTORS, 0))
4828 md_error(conf->mddev, rdev);
4829 rdev_dec_pending(rdev, conf->mddev);
4830 }
NeilBrownb84db562011-07-28 11:39:23 +10004831 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
4832 rdev = conf->disks[i].rdev;
4833 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004834 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10004835 rdev_dec_pending(rdev, conf->mddev);
4836 }
NeilBrown977df362011-12-23 10:17:53 +11004837 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
4838 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11004839 if (!rdev)
4840 /* rdev have been moved down */
4841 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11004842 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004843 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11004844 rdev_dec_pending(rdev, conf->mddev);
4845 }
NeilBrownbc2607f2011-07-28 11:39:22 +10004846 }
4847
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004848 if (s.ops_request)
4849 raid_run_ops(sh, s.ops_request);
4850
Dan Williamsf0e43bc2008-06-28 08:31:55 +10004851 ops_run_io(sh, &s);
4852
NeilBrownc5709ef2011-07-26 11:35:20 +10004853 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11004854 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02004855 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11004856 * have actually been submitted.
4857 */
4858 atomic_dec(&conf->preread_active_stripes);
4859 if (atomic_read(&conf->preread_active_stripes) <
4860 IO_THRESHOLD)
4861 md_wakeup_thread(conf->mddev->thread);
4862 }
4863
NeilBrownc3cce6c2015-08-14 12:47:33 +10004864 if (!bio_list_empty(&s.return_bi)) {
Shaohua Li29530792016-12-08 15:48:19 -08004865 if (test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) {
NeilBrownc3cce6c2015-08-14 12:47:33 +10004866 spin_lock_irq(&conf->device_lock);
4867 bio_list_merge(&conf->return_bi, &s.return_bi);
4868 spin_unlock_irq(&conf->device_lock);
4869 md_wakeup_thread(conf->mddev->thread);
4870 } else
4871 return_io(&s.return_bi);
4872 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004873
Dan Williams257a4b42011-11-08 16:22:06 +11004874 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07004875}
4876
NeilBrownd1688a62011-10-11 16:49:52 +11004877static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878{
4879 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
4880 while (!list_empty(&conf->delayed_list)) {
4881 struct list_head *l = conf->delayed_list.next;
4882 struct stripe_head *sh;
4883 sh = list_entry(l, struct stripe_head, lru);
4884 list_del_init(l);
4885 clear_bit(STRIPE_DELAYED, &sh->state);
4886 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4887 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004888 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08004889 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 }
NeilBrown482c0832011-04-18 18:25:42 +10004891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892}
4893
Shaohua Li566c09c2013-11-14 15:16:17 +11004894static void activate_bit_delay(struct r5conf *conf,
4895 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07004896{
4897 /* device_lock is held */
4898 struct list_head head;
4899 list_add(&head, &conf->bitmap_list);
4900 list_del_init(&conf->bitmap_list);
4901 while (!list_empty(&head)) {
4902 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11004903 int hash;
NeilBrown72626682005-09-09 16:23:54 -07004904 list_del_init(&sh->lru);
4905 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11004906 hash = sh->hash_lock_index;
4907 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07004908 }
4909}
4910
NeilBrown5c675f82014-12-15 12:56:56 +11004911static int raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07004912{
NeilBrownd1688a62011-10-11 16:49:52 +11004913 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07004914
4915 /* No difference between reads and writes. Just check
4916 * how busy the stripe_cache is
4917 */
NeilBrown3fa841d2009-09-23 18:10:29 +10004918
NeilBrown54233992015-02-26 12:21:04 +11004919 if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state))
NeilBrownf022b2f2006-10-03 01:15:56 -07004920 return 1;
Song Liua39f7af2016-11-17 15:24:40 -08004921
4922 /* Also checks whether there is pressure on r5cache log space */
4923 if (test_bit(R5C_LOG_TIGHT, &conf->cache_state))
4924 return 1;
NeilBrownf022b2f2006-10-03 01:15:56 -07004925 if (conf->quiesce)
4926 return 1;
Shaohua Li4bda5562013-11-14 15:16:17 +11004927 if (atomic_read(&conf->empty_inactive_list_nr))
NeilBrownf022b2f2006-10-03 01:15:56 -07004928 return 1;
4929
4930 return 0;
4931}
4932
NeilBrownfd01b882011-10-11 16:47:53 +11004933static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004934{
NeilBrown3cb5edf2015-07-15 17:24:17 +10004935 struct r5conf *conf = mddev->private;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004936 sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev);
NeilBrown3cb5edf2015-07-15 17:24:17 +10004937 unsigned int chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004938 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004939
NeilBrown3cb5edf2015-07-15 17:24:17 +10004940 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004941 return chunk_sectors >=
4942 ((sector & (chunk_sectors - 1)) + bio_sectors);
4943}
4944
4945/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004946 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4947 * later sampled by raid5d.
4948 */
NeilBrownd1688a62011-10-11 16:49:52 +11004949static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004950{
4951 unsigned long flags;
4952
4953 spin_lock_irqsave(&conf->device_lock, flags);
4954
4955 bi->bi_next = conf->retry_read_aligned_list;
4956 conf->retry_read_aligned_list = bi;
4957
4958 spin_unlock_irqrestore(&conf->device_lock, flags);
4959 md_wakeup_thread(conf->mddev->thread);
4960}
4961
NeilBrownd1688a62011-10-11 16:49:52 +11004962static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004963{
4964 struct bio *bi;
4965
4966 bi = conf->retry_read_aligned;
4967 if (bi) {
4968 conf->retry_read_aligned = NULL;
4969 return bi;
4970 }
4971 bi = conf->retry_read_aligned_list;
4972 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08004973 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004974 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02004975 /*
4976 * this sets the active strip count to 1 and the processed
4977 * strip count to zero (upper 8 bits)
4978 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004979 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004980 }
4981
4982 return bi;
4983}
4984
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004985/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004986 * The "raid5_align_endio" should check if the read succeeded and if it
4987 * did, call bio_endio on the original bio (having bio_put the new bio
4988 * first).
4989 * If the read failed..
4990 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004991static void raid5_align_endio(struct bio *bi)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004992{
4993 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11004994 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004995 struct r5conf *conf;
NeilBrown3cb03002011-10-11 16:45:26 +11004996 struct md_rdev *rdev;
Sasha Levin9b81c842015-08-10 19:05:18 -04004997 int error = bi->bi_error;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004998
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004999 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005000
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005001 rdev = (void*)raid_bi->bi_next;
5002 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11005003 mddev = rdev->mddev;
5004 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005005
5006 rdev_dec_pending(rdev, conf->mddev);
5007
Sasha Levin9b81c842015-08-10 19:05:18 -04005008 if (!error) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005009 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
5010 raid_bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005011 bio_endio(raid_bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005012 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10005013 wake_up(&conf->wait_for_quiescent);
NeilBrown6712ecf2007-09-27 12:47:43 +02005014 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005015 }
5016
Dan Williams45b42332007-07-09 11:56:43 -07005017 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005018
5019 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005020}
5021
Ming Lin7ef6b122015-05-06 22:51:24 -07005022static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005023{
NeilBrownd1688a62011-10-11 16:49:52 +11005024 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11005025 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005026 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11005027 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11005028 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005029
5030 if (!in_chunk_boundary(mddev, raid_bio)) {
Ming Lin7ef6b122015-05-06 22:51:24 -07005031 pr_debug("%s: non aligned\n", __func__);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005032 return 0;
5033 }
5034 /*
NeilBrowna167f662010-10-26 18:31:13 +11005035 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005036 */
NeilBrowna167f662010-10-26 18:31:13 +11005037 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005038 if (!align_bi)
5039 return 0;
5040 /*
5041 * set bi_end_io to a new function, and set bi_private to the
5042 * original bio.
5043 */
5044 align_bi->bi_end_io = raid5_align_endio;
5045 align_bi->bi_private = raid_bio;
5046 /*
5047 * compute position
5048 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005049 align_bi->bi_iter.bi_sector =
5050 raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
5051 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005052
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005053 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005054 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11005055 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
5056 if (!rdev || test_bit(Faulty, &rdev->flags) ||
5057 rdev->recovery_offset < end_sector) {
5058 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
5059 if (rdev &&
5060 (test_bit(Faulty, &rdev->flags) ||
5061 !(test_bit(In_sync, &rdev->flags) ||
5062 rdev->recovery_offset >= end_sector)))
5063 rdev = NULL;
5064 }
Song Liu03b047f2017-01-11 13:39:14 -08005065
5066 if (r5c_big_stripe_cached(conf, align_bi->bi_iter.bi_sector)) {
5067 rcu_read_unlock();
5068 bio_put(align_bi);
5069 return 0;
5070 }
5071
NeilBrown671488c2011-12-23 10:17:52 +11005072 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10005073 sector_t first_bad;
5074 int bad_sectors;
5075
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005076 atomic_inc(&rdev->nr_pending);
5077 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005078 raid_bio->bi_next = (void*)rdev;
5079 align_bi->bi_bdev = rdev->bdev;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06005080 bio_clear_flag(align_bi, BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005081
Kent Overstreet7140aaf2013-09-25 13:37:01 -07005082 if (is_badblock(rdev, align_bi->bi_iter.bi_sector,
Kent Overstreet4f024f32013-10-11 15:44:27 -07005083 bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10005084 &first_bad, &bad_sectors)) {
Neil Brown387bb172007-02-08 14:20:29 -08005085 bio_put(align_bi);
5086 rdev_dec_pending(rdev, mddev);
5087 return 0;
5088 }
5089
majianpeng6c0544e2012-06-12 08:31:10 +08005090 /* No reshape active, so we can trust rdev->data_offset */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005091 align_bi->bi_iter.bi_sector += rdev->data_offset;
majianpeng6c0544e2012-06-12 08:31:10 +08005092
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005093 spin_lock_irq(&conf->device_lock);
Yuanhan Liub1b46482015-05-08 18:19:06 +10005094 wait_event_lock_irq(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005095 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01005096 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005097 atomic_inc(&conf->active_aligned_reads);
5098 spin_unlock_irq(&conf->device_lock);
5099
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005100 if (mddev->gendisk)
5101 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
5102 align_bi, disk_devt(mddev->gendisk),
Kent Overstreet4f024f32013-10-11 15:44:27 -07005103 raid_bio->bi_iter.bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005104 generic_make_request(align_bi);
5105 return 1;
5106 } else {
5107 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005108 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005109 return 0;
5110 }
5111}
5112
Ming Lin7ef6b122015-05-06 22:51:24 -07005113static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
5114{
5115 struct bio *split;
5116
5117 do {
5118 sector_t sector = raid_bio->bi_iter.bi_sector;
5119 unsigned chunk_sects = mddev->chunk_sectors;
5120 unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
5121
5122 if (sectors < bio_sectors(raid_bio)) {
5123 split = bio_split(raid_bio, sectors, GFP_NOIO, fs_bio_set);
5124 bio_chain(split, raid_bio);
5125 } else
5126 split = raid_bio;
5127
5128 if (!raid5_read_one_chunk(mddev, split)) {
5129 if (split != raid_bio)
5130 generic_make_request(raid_bio);
5131 return split;
5132 }
5133 } while (split != raid_bio);
5134
5135 return NULL;
5136}
5137
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005138/* __get_priority_stripe - get the next stripe to process
5139 *
5140 * Full stripe writes are allowed to pass preread active stripes up until
5141 * the bypass_threshold is exceeded. In general the bypass_count
5142 * increments when the handle_list is handled before the hold_list; however, it
5143 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
5144 * stripe with in flight i/o. The bypass_count will be reset when the
5145 * head of the hold_list has changed, i.e. the head was promoted to the
5146 * handle_list.
5147 */
Shaohua Li851c30c2013-08-28 14:30:16 +08005148static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005149{
Shaohua Li851c30c2013-08-28 14:30:16 +08005150 struct stripe_head *sh = NULL, *tmp;
5151 struct list_head *handle_list = NULL;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005152 struct r5worker_group *wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005153
5154 if (conf->worker_cnt_per_group == 0) {
5155 handle_list = &conf->handle_list;
5156 } else if (group != ANY_GROUP) {
5157 handle_list = &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005158 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08005159 } else {
5160 int i;
5161 for (i = 0; i < conf->group_cnt; i++) {
5162 handle_list = &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005163 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08005164 if (!list_empty(handle_list))
5165 break;
5166 }
5167 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005168
5169 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5170 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08005171 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005172 list_empty(&conf->hold_list) ? "empty" : "busy",
5173 atomic_read(&conf->pending_full_writes), conf->bypass_count);
5174
Shaohua Li851c30c2013-08-28 14:30:16 +08005175 if (!list_empty(handle_list)) {
5176 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005177
5178 if (list_empty(&conf->hold_list))
5179 conf->bypass_count = 0;
5180 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
5181 if (conf->hold_list.next == conf->last_hold)
5182 conf->bypass_count++;
5183 else {
5184 conf->last_hold = conf->hold_list.next;
5185 conf->bypass_count -= conf->bypass_threshold;
5186 if (conf->bypass_count < 0)
5187 conf->bypass_count = 0;
5188 }
5189 }
5190 } else if (!list_empty(&conf->hold_list) &&
5191 ((conf->bypass_threshold &&
5192 conf->bypass_count > conf->bypass_threshold) ||
5193 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08005194
5195 list_for_each_entry(tmp, &conf->hold_list, lru) {
5196 if (conf->worker_cnt_per_group == 0 ||
5197 group == ANY_GROUP ||
5198 !cpu_online(tmp->cpu) ||
5199 cpu_to_group(tmp->cpu) == group) {
5200 sh = tmp;
5201 break;
5202 }
5203 }
5204
5205 if (sh) {
5206 conf->bypass_count -= conf->bypass_threshold;
5207 if (conf->bypass_count < 0)
5208 conf->bypass_count = 0;
5209 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08005210 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005211 }
5212
5213 if (!sh)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005214 return NULL;
5215
Shaohua Libfc90cb2013-08-29 15:40:32 +08005216 if (wg) {
5217 wg->stripes_cnt--;
5218 sh->group = NULL;
5219 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005220 list_del_init(&sh->lru);
Shaohua Lic7a6d352014-04-15 09:12:54 +08005221 BUG_ON(atomic_inc_return(&sh->count) != 1);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005222 return sh;
5223}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005224
Shaohua Li8811b592012-08-02 08:33:00 +10005225struct raid5_plug_cb {
5226 struct blk_plug_cb cb;
5227 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11005228 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10005229};
5230
5231static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
5232{
5233 struct raid5_plug_cb *cb = container_of(
5234 blk_cb, struct raid5_plug_cb, cb);
5235 struct stripe_head *sh;
5236 struct mddev *mddev = cb->cb.data;
5237 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11005238 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11005239 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10005240
5241 if (cb->list.next && !list_empty(&cb->list)) {
5242 spin_lock_irq(&conf->device_lock);
5243 while (!list_empty(&cb->list)) {
5244 sh = list_first_entry(&cb->list, struct stripe_head, lru);
5245 list_del_init(&sh->lru);
5246 /*
5247 * avoid race release_stripe_plug() sees
5248 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5249 * is still in our list
5250 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005251 smp_mb__before_atomic();
Shaohua Li8811b592012-08-02 08:33:00 +10005252 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08005253 /*
5254 * STRIPE_ON_RELEASE_LIST could be set here. In that
5255 * case, the count is always > 1 here
5256 */
Shaohua Li566c09c2013-11-14 15:16:17 +11005257 hash = sh->hash_lock_index;
5258 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11005259 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10005260 }
5261 spin_unlock_irq(&conf->device_lock);
5262 }
Shaohua Li566c09c2013-11-14 15:16:17 +11005263 release_inactive_stripe_list(conf, cb->temp_inactive_list,
5264 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005265 if (mddev->queue)
5266 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10005267 kfree(cb);
5268}
5269
5270static void release_stripe_plug(struct mddev *mddev,
5271 struct stripe_head *sh)
5272{
5273 struct blk_plug_cb *blk_cb = blk_check_plugged(
5274 raid5_unplug, mddev,
5275 sizeof(struct raid5_plug_cb));
5276 struct raid5_plug_cb *cb;
5277
5278 if (!blk_cb) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005279 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005280 return;
5281 }
5282
5283 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
5284
Shaohua Li566c09c2013-11-14 15:16:17 +11005285 if (cb->list.next == NULL) {
5286 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10005287 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11005288 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5289 INIT_LIST_HEAD(cb->temp_inactive_list + i);
5290 }
Shaohua Li8811b592012-08-02 08:33:00 +10005291
5292 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
5293 list_add_tail(&sh->lru, &cb->list);
5294 else
Shaohua Li6d036f72015-08-13 14:31:57 -07005295 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005296}
5297
Shaohua Li620125f2012-10-11 13:49:05 +11005298static void make_discard_request(struct mddev *mddev, struct bio *bi)
5299{
5300 struct r5conf *conf = mddev->private;
5301 sector_t logical_sector, last_sector;
5302 struct stripe_head *sh;
5303 int remaining;
5304 int stripe_sectors;
5305
5306 if (mddev->reshape_position != MaxSector)
5307 /* Skip discard while reshape is happening */
5308 return;
5309
Kent Overstreet4f024f32013-10-11 15:44:27 -07005310 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
5311 last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9);
Shaohua Li620125f2012-10-11 13:49:05 +11005312
5313 bi->bi_next = NULL;
5314 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
5315
5316 stripe_sectors = conf->chunk_sectors *
5317 (conf->raid_disks - conf->max_degraded);
5318 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5319 stripe_sectors);
5320 sector_div(last_sector, stripe_sectors);
5321
5322 logical_sector *= conf->chunk_sectors;
5323 last_sector *= conf->chunk_sectors;
5324
5325 for (; logical_sector < last_sector;
5326 logical_sector += STRIPE_SECTORS) {
5327 DEFINE_WAIT(w);
5328 int d;
5329 again:
Shaohua Li6d036f72015-08-13 14:31:57 -07005330 sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
Shaohua Li620125f2012-10-11 13:49:05 +11005331 prepare_to_wait(&conf->wait_for_overlap, &w,
5332 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005333 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5334 if (test_bit(STRIPE_SYNCING, &sh->state)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005335 raid5_release_stripe(sh);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005336 schedule();
5337 goto again;
5338 }
5339 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11005340 spin_lock_irq(&sh->stripe_lock);
5341 for (d = 0; d < conf->raid_disks; d++) {
5342 if (d == sh->pd_idx || d == sh->qd_idx)
5343 continue;
5344 if (sh->dev[d].towrite || sh->dev[d].toread) {
5345 set_bit(R5_Overlap, &sh->dev[d].flags);
5346 spin_unlock_irq(&sh->stripe_lock);
Shaohua Li6d036f72015-08-13 14:31:57 -07005347 raid5_release_stripe(sh);
Shaohua Li620125f2012-10-11 13:49:05 +11005348 schedule();
5349 goto again;
5350 }
5351 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11005352 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11005353 finish_wait(&conf->wait_for_overlap, &w);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005354 sh->overwrite_disks = 0;
Shaohua Li620125f2012-10-11 13:49:05 +11005355 for (d = 0; d < conf->raid_disks; d++) {
5356 if (d == sh->pd_idx || d == sh->qd_idx)
5357 continue;
5358 sh->dev[d].towrite = bi;
5359 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
5360 raid5_inc_bi_active_stripes(bi);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005361 sh->overwrite_disks++;
Shaohua Li620125f2012-10-11 13:49:05 +11005362 }
5363 spin_unlock_irq(&sh->stripe_lock);
5364 if (conf->mddev->bitmap) {
5365 for (d = 0;
5366 d < conf->raid_disks - conf->max_degraded;
5367 d++)
5368 bitmap_startwrite(mddev->bitmap,
5369 sh->sector,
5370 STRIPE_SECTORS,
5371 0);
5372 sh->bm_seq = conf->seq_flush + 1;
5373 set_bit(STRIPE_BIT_DELAY, &sh->state);
5374 }
5375
5376 set_bit(STRIPE_HANDLE, &sh->state);
5377 clear_bit(STRIPE_DELAYED, &sh->state);
5378 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5379 atomic_inc(&conf->preread_active_stripes);
5380 release_stripe_plug(mddev, sh);
5381 }
5382
5383 remaining = raid5_dec_bi_active_stripes(bi);
5384 if (remaining == 0) {
5385 md_write_end(mddev);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005386 bio_endio(bi);
Shaohua Li620125f2012-10-11 13:49:05 +11005387 }
5388}
5389
Shaohua Li849674e2016-01-20 13:52:20 -08005390static void raid5_make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391{
NeilBrownd1688a62011-10-11 16:49:52 +11005392 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11005393 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 sector_t new_sector;
5395 sector_t logical_sector, last_sector;
5396 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01005397 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11005398 int remaining;
Shaohua Li27c0f682014-04-09 11:25:47 +08005399 DEFINE_WAIT(w);
5400 bool do_prepare;
Song Liu3bddb7f2016-11-18 16:46:50 -08005401 bool do_flush = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402
Jens Axboe1eff9d32016-08-05 15:35:16 -06005403 if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
Shaohua Li828cbe92015-09-02 13:49:49 -07005404 int ret = r5l_handle_flush_request(conf->log, bi);
5405
5406 if (ret == 0)
5407 return;
5408 if (ret == -ENODEV) {
5409 md_flush_request(mddev, bi);
5410 return;
5411 }
5412 /* ret == -EAGAIN, fallback */
Song Liu3bddb7f2016-11-18 16:46:50 -08005413 /*
5414 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
5415 * we need to flush journal device
5416 */
5417 do_flush = bi->bi_opf & REQ_PREFLUSH;
NeilBrowne5dcdd82005-09-09 16:23:41 -07005418 }
5419
NeilBrown3d310eb2005-06-21 17:17:26 -07005420 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07005421
Eric Mei9ffc8f72015-03-18 23:39:11 -06005422 /*
5423 * If array is degraded, better not do chunk aligned read because
5424 * later we might have to read it again in order to reconstruct
5425 * data on failed drives.
5426 */
5427 if (rw == READ && mddev->degraded == 0 &&
Ming Lin7ef6b122015-05-06 22:51:24 -07005428 mddev->reshape_position == MaxSector) {
5429 bi = chunk_aligned_read(mddev, bi);
5430 if (!bi)
5431 return;
5432 }
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08005433
Mike Christie796a5cf2016-06-05 14:32:07 -05005434 if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
Shaohua Li620125f2012-10-11 13:49:05 +11005435 make_discard_request(mddev, bi);
5436 return;
5437 }
5438
Kent Overstreet4f024f32013-10-11 15:44:27 -07005439 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005440 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 bi->bi_next = NULL;
5442 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07005443
Shaohua Li27c0f682014-04-09 11:25:47 +08005444 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005446 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10005447 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08005448
Shaohua Li27c0f682014-04-09 11:25:47 +08005449 do_prepare = false;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005450 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10005451 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11005452 previous = 0;
Shaohua Li27c0f682014-04-09 11:25:47 +08005453 if (do_prepare)
5454 prepare_to_wait(&conf->wait_for_overlap, &w,
5455 TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005456 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11005457 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08005458 * 64bit on a 32bit platform, and so it might be
5459 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02005460 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08005461 * the lock is dropped, so once we get a reference
5462 * to the stripe that we think it is, we will have
5463 * to check again.
5464 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005465 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005466 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005467 ? logical_sector < conf->reshape_progress
5468 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005469 previous = 1;
5470 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10005471 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005472 ? logical_sector < conf->reshape_safe
5473 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08005474 spin_unlock_irq(&conf->device_lock);
5475 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005476 do_prepare = true;
NeilBrownb578d552006-03-27 01:18:12 -08005477 goto retry;
5478 }
5479 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005480 spin_unlock_irq(&conf->device_lock);
5481 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005482
NeilBrown112bf892009-03-31 14:39:38 +11005483 new_sector = raid5_compute_sector(conf, logical_sector,
5484 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11005485 &dd_idx, NULL);
Shaohua Li849674e2016-01-20 13:52:20 -08005486 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10005487 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 (unsigned long long)logical_sector);
5489
Shaohua Li6d036f72015-08-13 14:31:57 -07005490 sh = raid5_get_active_stripe(conf, new_sector, previous,
Jens Axboe1eff9d32016-08-05 15:35:16 -06005491 (bi->bi_opf & REQ_RAHEAD), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11005493 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005494 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08005495 * stripe, so we must do the range check again.
5496 * Expansion could still move past after this
5497 * test, but as we are holding a reference to
5498 * 'sh', we know that if that happens,
5499 * STRIPE_EXPANDING will get set and the expansion
5500 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005501 */
5502 int must_retry = 0;
5503 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005504 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11005505 ? logical_sector >= conf->reshape_progress
5506 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005507 /* mismatch, need to try again */
5508 must_retry = 1;
5509 spin_unlock_irq(&conf->device_lock);
5510 if (must_retry) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005511 raid5_release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07005512 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005513 do_prepare = true;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005514 goto retry;
5515 }
5516 }
NeilBrownc46501b2013-08-27 15:52:13 +10005517 if (read_seqcount_retry(&conf->gen_lock, seq)) {
5518 /* Might have got the wrong stripe_head
5519 * by accident
5520 */
Shaohua Li6d036f72015-08-13 14:31:57 -07005521 raid5_release_stripe(sh);
NeilBrownc46501b2013-08-27 15:52:13 +10005522 goto retry;
5523 }
NeilBrowne62e58a2009-07-01 13:15:35 +10005524
Namhyung Kimffd96e32011-07-18 17:38:51 +10005525 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10005526 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08005527 logical_sector < mddev->suspend_hi) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005528 raid5_release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10005529 /* As the suspend_* range is controlled by
5530 * userspace, we want an interruptible
5531 * wait.
5532 */
5533 flush_signals(current);
5534 prepare_to_wait(&conf->wait_for_overlap,
5535 &w, TASK_INTERRUPTIBLE);
5536 if (logical_sector >= mddev->suspend_lo &&
Shaohua Li27c0f682014-04-09 11:25:47 +08005537 logical_sector < mddev->suspend_hi) {
NeilBrowne62e58a2009-07-01 13:15:35 +10005538 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005539 do_prepare = true;
5540 }
NeilBrowne464eaf2006-03-27 01:18:14 -08005541 goto retry;
5542 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005543
5544 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
shli@kernel.orgda41ba62014-12-15 12:57:03 +11005545 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005546 /* Stripe is busy expanding or
5547 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 * and wait a while
5549 */
NeilBrown482c0832011-04-18 18:25:42 +10005550 md_wakeup_thread(mddev->thread);
Shaohua Li6d036f72015-08-13 14:31:57 -07005551 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005553 do_prepare = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 goto retry;
5555 }
Song Liu3bddb7f2016-11-18 16:46:50 -08005556 if (do_flush) {
5557 set_bit(STRIPE_R5C_PREFLUSH, &sh->state);
5558 /* we only need flush for one stripe */
5559 do_flush = false;
5560 }
5561
NeilBrown6ed30032008-02-06 01:40:00 -08005562 set_bit(STRIPE_HANDLE, &sh->state);
5563 clear_bit(STRIPE_DELAYED, &sh->state);
shli@kernel.org59fc6302014-12-15 12:57:03 +11005564 if ((!sh->batch_head || sh == sh->batch_head) &&
Jens Axboe1eff9d32016-08-05 15:35:16 -06005565 (bi->bi_opf & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11005566 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5567 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10005568 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 } else {
5570 /* cannot get stripe for read-ahead, just give-up */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005571 bi->bi_error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 break;
5573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 }
Shaohua Li27c0f682014-04-09 11:25:47 +08005575 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown7c13edc2011-04-18 18:25:43 +10005576
Shaohua Lie7836bd62012-07-19 16:01:31 +10005577 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08005578 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579
NeilBrown16a53ec2006-06-26 00:27:38 -07005580 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02005582
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005583 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
5584 bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005585 bio_endio(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587}
5588
NeilBrownfd01b882011-10-11 16:47:53 +11005589static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11005590
NeilBrownfd01b882011-10-11 16:47:53 +11005591static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592{
NeilBrown52c03292006-06-26 00:27:43 -07005593 /* reshaping is quite different to recovery/resync so it is
5594 * handled quite separately ... here.
5595 *
5596 * On each call to sync_request, we gather one chunk worth of
5597 * destination stripes and flag them as expanding.
5598 * Then we find all the source stripes and request reads.
5599 * As the reads complete, handle_stripe will copy the data
5600 * into the destination stripe and release that stripe.
5601 */
NeilBrownd1688a62011-10-11 16:49:52 +11005602 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005604 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08005605 int raid_disks = conf->previous_raid_disks;
5606 int data_disks = raid_disks - conf->max_degraded;
5607 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07005608 int i;
5609 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11005610 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11005611 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11005612 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11005613 struct list_head stripes;
NeilBrown92140482015-07-06 12:28:45 +10005614 sector_t retn;
NeilBrown52c03292006-06-26 00:27:43 -07005615
NeilBrownfef9c612009-03-31 15:16:46 +11005616 if (sector_nr == 0) {
5617 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10005618 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005619 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
5620 sector_nr = raid5_size(mddev, 0, 0)
5621 - conf->reshape_progress;
NeilBrown6cbd8142015-07-24 13:30:32 +10005622 } else if (mddev->reshape_backwards &&
5623 conf->reshape_progress == MaxSector) {
5624 /* shouldn't happen, but just in case, finish up.*/
5625 sector_nr = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +10005626 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005627 conf->reshape_progress > 0)
5628 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005629 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005630 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11005631 mddev->curr_resync_completed = sector_nr;
5632 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11005633 *skipped = 1;
NeilBrown92140482015-07-06 12:28:45 +10005634 retn = sector_nr;
5635 goto finish;
NeilBrownfef9c612009-03-31 15:16:46 +11005636 }
NeilBrown52c03292006-06-26 00:27:43 -07005637 }
5638
NeilBrown7a661382009-03-31 15:21:40 +11005639 /* We need to process a full chunk at a time.
5640 * If old and new chunk sizes differ, we need to process the
5641 * largest of these
5642 */
NeilBrown3cb5edf2015-07-15 17:24:17 +10005643
5644 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
NeilBrown7a661382009-03-31 15:21:40 +11005645
NeilBrownb5254dd2012-05-21 09:27:01 +10005646 /* We update the metadata at least every 10 seconds, or when
5647 * the data about to be copied would over-write the source of
5648 * the data at the front of the range. i.e. one new_stripe
5649 * along from reshape_progress new_maps to after where
5650 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07005651 */
NeilBrownfef9c612009-03-31 15:16:46 +11005652 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005653 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11005654 readpos = conf->reshape_progress;
5655 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005656 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08005657 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10005658 if (mddev->reshape_backwards) {
NeilBrownc74c0d72015-07-15 17:54:15 +10005659 BUG_ON(writepos < reshape_sectors);
5660 writepos -= reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11005661 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11005662 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11005663 } else {
NeilBrown7a661382009-03-31 15:21:40 +11005664 writepos += reshape_sectors;
NeilBrownc74c0d72015-07-15 17:54:15 +10005665 /* readpos and safepos are worst-case calculations.
5666 * A negative number is overly pessimistic, and causes
5667 * obvious problems for unsigned storage. So clip to 0.
5668 */
NeilBrowned37d832009-05-27 21:39:05 +10005669 readpos -= min_t(sector_t, reshape_sectors, readpos);
5670 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11005671 }
NeilBrown52c03292006-06-26 00:27:43 -07005672
NeilBrownb5254dd2012-05-21 09:27:01 +10005673 /* Having calculated the 'writepos' possibly use it
5674 * to set 'stripe_addr' which is where we will write to.
5675 */
5676 if (mddev->reshape_backwards) {
5677 BUG_ON(conf->reshape_progress == 0);
5678 stripe_addr = writepos;
5679 BUG_ON((mddev->dev_sectors &
5680 ~((sector_t)reshape_sectors - 1))
5681 - reshape_sectors - stripe_addr
5682 != sector_nr);
5683 } else {
5684 BUG_ON(writepos != sector_nr + reshape_sectors);
5685 stripe_addr = sector_nr;
5686 }
5687
NeilBrownc8f517c2009-03-31 15:28:40 +11005688 /* 'writepos' is the most advanced device address we might write.
5689 * 'readpos' is the least advanced device address we might read.
5690 * 'safepos' is the least address recorded in the metadata as having
5691 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10005692 * If there is a min_offset_diff, these are adjusted either by
5693 * increasing the safepos/readpos if diff is negative, or
5694 * increasing writepos if diff is positive.
5695 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11005696 * ensure safety in the face of a crash - that must be done by userspace
5697 * making a backup of the data. So in that case there is no particular
5698 * rush to update metadata.
5699 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5700 * update the metadata to advance 'safepos' to match 'readpos' so that
5701 * we can be safe in the event of a crash.
5702 * So we insist on updating metadata if safepos is behind writepos and
5703 * readpos is beyond writepos.
5704 * In any case, update the metadata every 10 seconds.
5705 * Maybe that number should be configurable, but I'm not sure it is
5706 * worth it.... maybe it could be a multiple of safemode_delay???
5707 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005708 if (conf->min_offset_diff < 0) {
5709 safepos += -conf->min_offset_diff;
5710 readpos += -conf->min_offset_diff;
5711 } else
5712 writepos += conf->min_offset_diff;
5713
NeilBrown2c810cd2012-05-21 09:27:00 +10005714 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11005715 ? (safepos > writepos && readpos < writepos)
5716 : (safepos < writepos && readpos > writepos)) ||
5717 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07005718 /* Cannot proceed until we've updated the superblock... */
5719 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005720 atomic_read(&conf->reshape_stripes)==0
5721 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5722 if (atomic_read(&conf->reshape_stripes) != 0)
5723 return 0;
NeilBrownfef9c612009-03-31 15:16:46 +11005724 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005725 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005726 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08005727 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown52c03292006-06-26 00:27:43 -07005728 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08005729 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11005730 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5731 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5732 return 0;
NeilBrown52c03292006-06-26 00:27:43 -07005733 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005734 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07005735 spin_unlock_irq(&conf->device_lock);
5736 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005737 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07005738 }
5739
NeilBrownab69ae12009-03-31 15:26:47 +11005740 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11005741 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07005742 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10005743 int skipped_disk = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07005744 sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005745 set_bit(STRIPE_EXPANDING, &sh->state);
5746 atomic_inc(&conf->reshape_stripes);
5747 /* If any of this stripe is beyond the end of the old
5748 * array, then we need to zero those blocks
5749 */
5750 for (j=sh->disks; j--;) {
5751 sector_t s;
5752 if (j == sh->pd_idx)
5753 continue;
NeilBrownf4168852007-02-28 20:11:53 -08005754 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11005755 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08005756 continue;
Shaohua Li6d036f72015-08-13 14:31:57 -07005757 s = raid5_compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11005758 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10005759 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07005760 continue;
5761 }
5762 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
5763 set_bit(R5_Expanded, &sh->dev[j].flags);
5764 set_bit(R5_UPTODATE, &sh->dev[j].flags);
5765 }
NeilBrowna9f326e2009-09-23 18:06:41 +10005766 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07005767 set_bit(STRIPE_EXPAND_READY, &sh->state);
5768 set_bit(STRIPE_HANDLE, &sh->state);
5769 }
NeilBrownab69ae12009-03-31 15:26:47 +11005770 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07005771 }
5772 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005773 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11005774 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005775 else
NeilBrown7a661382009-03-31 15:21:40 +11005776 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07005777 spin_unlock_irq(&conf->device_lock);
5778 /* Ok, those stripe are ready. We can start scheduling
5779 * reads on the source stripes.
5780 * The source stripes are determined by mapping the first and last
5781 * block on the destination stripes.
5782 */
NeilBrown52c03292006-06-26 00:27:43 -07005783 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11005784 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11005785 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07005786 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10005787 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10005788 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11005789 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11005790 if (last_sector >= mddev->dev_sectors)
5791 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07005792 while (first_sector <= last_sector) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005793 sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005794 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
5795 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07005796 raid5_release_stripe(sh);
NeilBrown52c03292006-06-26 00:27:43 -07005797 first_sector += STRIPE_SECTORS;
5798 }
NeilBrownab69ae12009-03-31 15:26:47 +11005799 /* Now that the sources are clearly marked, we can release
5800 * the destination stripes
5801 */
5802 while (!list_empty(&stripes)) {
5803 sh = list_entry(stripes.next, struct stripe_head, lru);
5804 list_del_init(&sh->lru);
Shaohua Li6d036f72015-08-13 14:31:57 -07005805 raid5_release_stripe(sh);
NeilBrownab69ae12009-03-31 15:26:47 +11005806 }
NeilBrownc6207272008-02-06 01:39:52 -08005807 /* If this takes us to the resync_max point where we have to pause,
5808 * then we need to write out the superblock.
5809 */
NeilBrown7a661382009-03-31 15:21:40 +11005810 sector_nr += reshape_sectors;
NeilBrown92140482015-07-06 12:28:45 +10005811 retn = reshape_sectors;
5812finish:
NeilBrownc5e19d92015-07-17 12:06:02 +10005813 if (mddev->curr_resync_completed > mddev->resync_max ||
5814 (sector_nr - mddev->curr_resync_completed) * 2
NeilBrownc03f6a12009-04-17 11:06:30 +10005815 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08005816 /* Cannot proceed until we've updated the superblock... */
5817 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005818 atomic_read(&conf->reshape_stripes) == 0
5819 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5820 if (atomic_read(&conf->reshape_stripes) != 0)
5821 goto ret;
NeilBrownfef9c612009-03-31 15:16:46 +11005822 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005823 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005824 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08005825 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownc6207272008-02-06 01:39:52 -08005826 md_wakeup_thread(mddev->thread);
5827 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005828 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)
NeilBrownc91abf52013-11-19 12:02:01 +11005829 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5830 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5831 goto ret;
NeilBrownc6207272008-02-06 01:39:52 -08005832 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005833 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08005834 spin_unlock_irq(&conf->device_lock);
5835 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005836 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08005837 }
NeilBrownc91abf52013-11-19 12:02:01 +11005838ret:
NeilBrown92140482015-07-06 12:28:45 +10005839 return retn;
NeilBrown52c03292006-06-26 00:27:43 -07005840}
5841
Shaohua Li849674e2016-01-20 13:52:20 -08005842static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
5843 int *skipped)
NeilBrown52c03292006-06-26 00:27:43 -07005844{
NeilBrownd1688a62011-10-11 16:49:52 +11005845 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07005846 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11005847 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11005848 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07005849 int still_degraded = 0;
5850 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
NeilBrown72626682005-09-09 16:23:54 -07005852 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11005854
NeilBrown29269552006-03-27 01:18:10 -08005855 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
5856 end_reshape(conf);
5857 return 0;
5858 }
NeilBrown72626682005-09-09 16:23:54 -07005859
5860 if (mddev->curr_resync < max_sector) /* aborted */
5861 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
5862 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07005863 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07005864 conf->fullsync = 0;
5865 bitmap_close_sync(mddev->bitmap);
5866
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 return 0;
5868 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08005869
NeilBrown64bd6602009-08-03 10:59:58 +10005870 /* Allow raid5_quiesce to complete */
5871 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
5872
NeilBrown52c03292006-06-26 00:27:43 -07005873 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5874 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08005875
NeilBrownc6207272008-02-06 01:39:52 -08005876 /* No need to check resync_max as we never do more than one
5877 * stripe, and as resync_max will always be on a chunk boundary,
5878 * if the check in md_do_sync didn't fire, there is no chance
5879 * of overstepping resync_max here
5880 */
5881
NeilBrown16a53ec2006-06-26 00:27:38 -07005882 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 * to resync, then assert that we are finished, because there is
5884 * nothing we can do.
5885 */
NeilBrown3285edf2006-06-26 00:27:55 -07005886 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005887 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005888 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07005889 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 return rv;
5891 }
majianpeng6f608042013-04-24 11:42:41 +10005892 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
5893 !conf->fullsync &&
5894 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
5895 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07005896 /* we can skip this block, and probably more */
5897 sync_blocks /= STRIPE_SECTORS;
5898 *skipped = 1;
5899 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
5900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10005902 bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
NeilBrownb47490c2008-02-06 01:39:50 -08005903
Shaohua Li6d036f72015-08-13 14:31:57 -07005904 sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 if (sh == NULL) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005906 sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07005908 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08005910 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005912 /* Need to check if array will still be degraded after recovery/resync
Eric Mei16d9cfa2015-01-06 09:35:02 -08005913 * Note in case of > 1 drive failures it's possible we're rebuilding
5914 * one drive while leaving another faulty drive in array.
NeilBrown16a53ec2006-06-26 00:27:38 -07005915 */
Eric Mei16d9cfa2015-01-06 09:35:02 -08005916 rcu_read_lock();
5917 for (i = 0; i < conf->raid_disks; i++) {
5918 struct md_rdev *rdev = ACCESS_ONCE(conf->disks[i].rdev);
5919
5920 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
NeilBrown16a53ec2006-06-26 00:27:38 -07005921 still_degraded = 1;
Eric Mei16d9cfa2015-01-06 09:35:02 -08005922 }
5923 rcu_read_unlock();
NeilBrown16a53ec2006-06-26 00:27:38 -07005924
5925 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
5926
NeilBrown83206d62011-07-26 11:19:49 +10005927 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Eivind Sarto053f5b62014-06-09 17:06:19 -07005928 set_bit(STRIPE_HANDLE, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929
Shaohua Li6d036f72015-08-13 14:31:57 -07005930 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
5932 return STRIPE_SECTORS;
5933}
5934
NeilBrownd1688a62011-10-11 16:49:52 +11005935static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005936{
5937 /* We may not be able to submit a whole bio at once as there
5938 * may not be enough stripe_heads available.
5939 * We cannot pre-allocate enough stripe_heads as we may need
5940 * more than exist in the cache (if we allow ever large chunks).
5941 * So we do one stripe head at a time and record in
5942 * ->bi_hw_segments how many have been done.
5943 *
5944 * We *know* that this entire raid_bio is in one chunk, so
5945 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5946 */
5947 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11005948 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005949 sector_t sector, logical_sector, last_sector;
5950 int scnt = 0;
5951 int remaining;
5952 int handled = 0;
5953
Kent Overstreet4f024f32013-10-11 15:44:27 -07005954 logical_sector = raid_bio->bi_iter.bi_sector &
5955 ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11005956 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11005957 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005958 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005959
5960 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08005961 logical_sector += STRIPE_SECTORS,
5962 sector += STRIPE_SECTORS,
5963 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005964
Shaohua Lie7836bd62012-07-19 16:01:31 +10005965 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005966 /* already done this stripe */
5967 continue;
5968
Shaohua Li6d036f72015-08-13 14:31:57 -07005969 sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005970
5971 if (!sh) {
5972 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10005973 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005974 conf->retry_read_aligned = raid_bio;
5975 return handled;
5976 }
5977
shli@kernel.orgda41ba62014-12-15 12:57:03 +11005978 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005979 raid5_release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10005980 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08005981 conf->retry_read_aligned = raid_bio;
5982 return handled;
5983 }
5984
majianpeng3f9e7c12012-07-31 10:04:21 +10005985 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07005986 handle_stripe(sh);
Shaohua Li6d036f72015-08-13 14:31:57 -07005987 raid5_release_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005988 handled++;
5989 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10005990 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005991 if (remaining == 0) {
5992 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
5993 raid_bio, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005994 bio_endio(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005995 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005996 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10005997 wake_up(&conf->wait_for_quiescent);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005998 return handled;
5999}
6000
Shaohua Libfc90cb2013-08-29 15:40:32 +08006001static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11006002 struct r5worker *worker,
6003 struct list_head *temp_inactive_list)
Shaohua Li46a06402012-08-02 08:33:15 +10006004{
6005 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11006006 int i, batch_size = 0, hash;
6007 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10006008
6009 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08006010 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10006011 batch[batch_size++] = sh;
6012
Shaohua Li566c09c2013-11-14 15:16:17 +11006013 if (batch_size == 0) {
6014 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6015 if (!list_empty(temp_inactive_list + i))
6016 break;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006017 if (i == NR_STRIPE_HASH_LOCKS) {
6018 spin_unlock_irq(&conf->device_lock);
6019 r5l_flush_stripe_to_raid(conf->log);
6020 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006021 return batch_size;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006022 }
Shaohua Li566c09c2013-11-14 15:16:17 +11006023 release_inactive = true;
6024 }
Shaohua Li46a06402012-08-02 08:33:15 +10006025 spin_unlock_irq(&conf->device_lock);
6026
Shaohua Li566c09c2013-11-14 15:16:17 +11006027 release_inactive_stripe_list(conf, temp_inactive_list,
6028 NR_STRIPE_HASH_LOCKS);
6029
Shaohua Lia8c34f92015-09-02 13:49:46 -07006030 r5l_flush_stripe_to_raid(conf->log);
Shaohua Li566c09c2013-11-14 15:16:17 +11006031 if (release_inactive) {
6032 spin_lock_irq(&conf->device_lock);
6033 return 0;
6034 }
6035
Shaohua Li46a06402012-08-02 08:33:15 +10006036 for (i = 0; i < batch_size; i++)
6037 handle_stripe(batch[i]);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07006038 r5l_write_stripe_run(conf->log);
Shaohua Li46a06402012-08-02 08:33:15 +10006039
6040 cond_resched();
6041
6042 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006043 for (i = 0; i < batch_size; i++) {
6044 hash = batch[i]->hash_lock_index;
6045 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
6046 }
Shaohua Li46a06402012-08-02 08:33:15 +10006047 return batch_size;
6048}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006049
Shaohua Li851c30c2013-08-28 14:30:16 +08006050static void raid5_do_work(struct work_struct *work)
6051{
6052 struct r5worker *worker = container_of(work, struct r5worker, work);
6053 struct r5worker_group *group = worker->group;
6054 struct r5conf *conf = group->conf;
6055 int group_id = group - conf->worker_groups;
6056 int handled;
6057 struct blk_plug plug;
6058
6059 pr_debug("+++ raid5worker active\n");
6060
6061 blk_start_plug(&plug);
6062 handled = 0;
6063 spin_lock_irq(&conf->device_lock);
6064 while (1) {
6065 int batch_size, released;
6066
Shaohua Li566c09c2013-11-14 15:16:17 +11006067 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006068
Shaohua Li566c09c2013-11-14 15:16:17 +11006069 batch_size = handle_active_stripes(conf, group_id, worker,
6070 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08006071 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08006072 if (!batch_size && !released)
6073 break;
6074 handled += batch_size;
6075 }
6076 pr_debug("%d stripes handled\n", handled);
6077
6078 spin_unlock_irq(&conf->device_lock);
6079 blk_finish_plug(&plug);
6080
6081 pr_debug("--- raid5worker inactive\n");
6082}
6083
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084/*
6085 * This is our raid5 kernel thread.
6086 *
6087 * We scan the hash table for stripes which can be handled now.
6088 * During the scan, completed stripes are saved for us by the interrupt
6089 * handler, so that they will not have to wait for our next wakeup.
6090 */
Shaohua Li4ed87312012-10-11 13:34:00 +11006091static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092{
Shaohua Li4ed87312012-10-11 13:34:00 +11006093 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11006094 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006096 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097
Dan Williams45b42332007-07-09 11:56:43 -07006098 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
6100 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101
NeilBrownc3cce6c2015-08-14 12:47:33 +10006102 if (!bio_list_empty(&conf->return_bi) &&
Shaohua Li29530792016-12-08 15:48:19 -08006103 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrownc3cce6c2015-08-14 12:47:33 +10006104 struct bio_list tmp = BIO_EMPTY_LIST;
6105 spin_lock_irq(&conf->device_lock);
Shaohua Li29530792016-12-08 15:48:19 -08006106 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrownc3cce6c2015-08-14 12:47:33 +10006107 bio_list_merge(&tmp, &conf->return_bi);
6108 bio_list_init(&conf->return_bi);
6109 }
6110 spin_unlock_irq(&conf->device_lock);
6111 return_io(&tmp);
6112 }
6113
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006114 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 handled = 0;
6116 spin_lock_irq(&conf->device_lock);
6117 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006118 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08006119 int batch_size, released;
6120
Shaohua Li566c09c2013-11-14 15:16:17 +11006121 released = release_stripe_list(conf, conf->temp_inactive_list);
NeilBrownedbe83a2015-02-26 12:47:56 +11006122 if (released)
6123 clear_bit(R5_DID_ALLOC, &conf->cache_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124
NeilBrown0021b7b2012-07-31 09:08:14 +02006125 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10006126 !list_empty(&conf->bitmap_list)) {
6127 /* Now is a good time to flush some bitmap updates */
6128 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08006129 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07006130 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08006131 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10006132 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11006133 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07006134 }
NeilBrown0021b7b2012-07-31 09:08:14 +02006135 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07006136
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006137 while ((bio = remove_bio_from_retry(conf))) {
6138 int ok;
6139 spin_unlock_irq(&conf->device_lock);
6140 ok = retry_aligned_read(conf, bio);
6141 spin_lock_irq(&conf->device_lock);
6142 if (!ok)
6143 break;
6144 handled++;
6145 }
6146
Shaohua Li566c09c2013-11-14 15:16:17 +11006147 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
6148 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08006149 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 break;
Shaohua Li46a06402012-08-02 08:33:15 +10006151 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152
Shaohua Li29530792016-12-08 15:48:19 -08006153 if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) {
Shaohua Li46a06402012-08-02 08:33:15 +10006154 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10006155 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10006156 spin_lock_irq(&conf->device_lock);
6157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158 }
Dan Williams45b42332007-07-09 11:56:43 -07006159 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160
6161 spin_unlock_irq(&conf->device_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006162 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
6163 mutex_trylock(&conf->cache_size_mutex)) {
NeilBrownedbe83a2015-02-26 12:47:56 +11006164 grow_one_stripe(conf, __GFP_NOWARN);
6165 /* Set flag even if allocation failed. This helps
6166 * slow down allocation requests when mem is short
6167 */
6168 set_bit(R5_DID_ALLOC, &conf->cache_state);
NeilBrown2d5b5692015-07-06 12:49:23 +10006169 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171
Shaohua Li765d7042017-01-04 09:33:23 -08006172 flush_deferred_bios(conf);
6173
Shaohua Li0576b1c2015-08-13 14:32:00 -07006174 r5l_flush_stripe_to_raid(conf->log);
6175
Dan Williamsc9f21aa2008-07-23 12:05:51 -07006176 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006177 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178
Dan Williams45b42332007-07-09 11:56:43 -07006179 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180}
6181
NeilBrown3f294f42005-11-08 21:39:25 -08006182static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006183raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006184{
NeilBrown7b1485b2014-12-15 12:56:59 +11006185 struct r5conf *conf;
6186 int ret = 0;
6187 spin_lock(&mddev->lock);
6188 conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006189 if (conf)
NeilBrownedbe83a2015-02-26 12:47:56 +11006190 ret = sprintf(page, "%d\n", conf->min_nr_stripes);
NeilBrown7b1485b2014-12-15 12:56:59 +11006191 spin_unlock(&mddev->lock);
6192 return ret;
NeilBrown3f294f42005-11-08 21:39:25 -08006193}
6194
NeilBrownc41d4ac2010-06-01 19:37:24 +10006195int
NeilBrownfd01b882011-10-11 16:47:53 +11006196raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10006197{
NeilBrownd1688a62011-10-11 16:49:52 +11006198 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10006199 int err;
6200
6201 if (size <= 16 || size > 32768)
6202 return -EINVAL;
NeilBrown486f0642015-02-25 12:10:35 +11006203
NeilBrownedbe83a2015-02-26 12:47:56 +11006204 conf->min_nr_stripes = size;
NeilBrown2d5b5692015-07-06 12:49:23 +10006205 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006206 while (size < conf->max_nr_stripes &&
6207 drop_one_stripe(conf))
6208 ;
NeilBrown2d5b5692015-07-06 12:49:23 +10006209 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006210
NeilBrownedbe83a2015-02-26 12:47:56 +11006211
NeilBrownc41d4ac2010-06-01 19:37:24 +10006212 err = md_allow_write(mddev);
6213 if (err)
6214 return err;
NeilBrown486f0642015-02-25 12:10:35 +11006215
NeilBrown2d5b5692015-07-06 12:49:23 +10006216 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006217 while (size > conf->max_nr_stripes)
6218 if (!grow_one_stripe(conf, GFP_KERNEL))
6219 break;
NeilBrown2d5b5692015-07-06 12:49:23 +10006220 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006221
NeilBrownc41d4ac2010-06-01 19:37:24 +10006222 return 0;
6223}
6224EXPORT_SYMBOL(raid5_set_cache_size);
6225
NeilBrown3f294f42005-11-08 21:39:25 -08006226static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006227raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08006228{
NeilBrown67918752014-12-15 12:57:01 +11006229 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006230 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07006231 int err;
6232
NeilBrown3f294f42005-11-08 21:39:25 -08006233 if (len >= PAGE_SIZE)
6234 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006235 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08006236 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006237 err = mddev_lock(mddev);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006238 if (err)
6239 return err;
NeilBrown67918752014-12-15 12:57:01 +11006240 conf = mddev->private;
6241 if (!conf)
6242 err = -ENODEV;
6243 else
6244 err = raid5_set_cache_size(mddev, new);
6245 mddev_unlock(mddev);
6246
6247 return err ?: len;
NeilBrown3f294f42005-11-08 21:39:25 -08006248}
NeilBrown007583c2005-11-08 21:39:30 -08006249
NeilBrown96de1e62005-11-08 21:39:39 -08006250static struct md_sysfs_entry
6251raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
6252 raid5_show_stripe_cache_size,
6253 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08006254
6255static ssize_t
Markus Stockhausend06f1912014-12-15 12:57:05 +11006256raid5_show_rmw_level(struct mddev *mddev, char *page)
6257{
6258 struct r5conf *conf = mddev->private;
6259 if (conf)
6260 return sprintf(page, "%d\n", conf->rmw_level);
6261 else
6262 return 0;
6263}
6264
6265static ssize_t
6266raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
6267{
6268 struct r5conf *conf = mddev->private;
6269 unsigned long new;
6270
6271 if (!conf)
6272 return -ENODEV;
6273
6274 if (len >= PAGE_SIZE)
6275 return -EINVAL;
6276
6277 if (kstrtoul(page, 10, &new))
6278 return -EINVAL;
6279
6280 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
6281 return -EINVAL;
6282
6283 if (new != PARITY_DISABLE_RMW &&
6284 new != PARITY_ENABLE_RMW &&
6285 new != PARITY_PREFER_RMW)
6286 return -EINVAL;
6287
6288 conf->rmw_level = new;
6289 return len;
6290}
6291
6292static struct md_sysfs_entry
6293raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
6294 raid5_show_rmw_level,
6295 raid5_store_rmw_level);
6296
6297
6298static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006299raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006300{
NeilBrown7b1485b2014-12-15 12:56:59 +11006301 struct r5conf *conf;
6302 int ret = 0;
6303 spin_lock(&mddev->lock);
6304 conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006305 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006306 ret = sprintf(page, "%d\n", conf->bypass_threshold);
6307 spin_unlock(&mddev->lock);
6308 return ret;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006309}
6310
6311static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006312raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006313{
NeilBrown67918752014-12-15 12:57:01 +11006314 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006315 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006316 int err;
6317
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006318 if (len >= PAGE_SIZE)
6319 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006320 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006321 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006322
6323 err = mddev_lock(mddev);
6324 if (err)
6325 return err;
6326 conf = mddev->private;
6327 if (!conf)
6328 err = -ENODEV;
NeilBrownedbe83a2015-02-26 12:47:56 +11006329 else if (new > conf->min_nr_stripes)
NeilBrown67918752014-12-15 12:57:01 +11006330 err = -EINVAL;
6331 else
6332 conf->bypass_threshold = new;
6333 mddev_unlock(mddev);
6334 return err ?: len;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006335}
6336
6337static struct md_sysfs_entry
6338raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
6339 S_IRUGO | S_IWUSR,
6340 raid5_show_preread_threshold,
6341 raid5_store_preread_threshold);
6342
6343static ssize_t
Shaohua Lid592a992014-05-21 17:57:44 +08006344raid5_show_skip_copy(struct mddev *mddev, char *page)
6345{
NeilBrown7b1485b2014-12-15 12:56:59 +11006346 struct r5conf *conf;
6347 int ret = 0;
6348 spin_lock(&mddev->lock);
6349 conf = mddev->private;
Shaohua Lid592a992014-05-21 17:57:44 +08006350 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006351 ret = sprintf(page, "%d\n", conf->skip_copy);
6352 spin_unlock(&mddev->lock);
6353 return ret;
Shaohua Lid592a992014-05-21 17:57:44 +08006354}
6355
6356static ssize_t
6357raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
6358{
NeilBrown67918752014-12-15 12:57:01 +11006359 struct r5conf *conf;
Shaohua Lid592a992014-05-21 17:57:44 +08006360 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006361 int err;
6362
Shaohua Lid592a992014-05-21 17:57:44 +08006363 if (len >= PAGE_SIZE)
6364 return -EINVAL;
Shaohua Lid592a992014-05-21 17:57:44 +08006365 if (kstrtoul(page, 10, &new))
6366 return -EINVAL;
6367 new = !!new;
Shaohua Lid592a992014-05-21 17:57:44 +08006368
NeilBrown67918752014-12-15 12:57:01 +11006369 err = mddev_lock(mddev);
6370 if (err)
6371 return err;
6372 conf = mddev->private;
6373 if (!conf)
6374 err = -ENODEV;
6375 else if (new != conf->skip_copy) {
6376 mddev_suspend(mddev);
6377 conf->skip_copy = new;
6378 if (new)
6379 mddev->queue->backing_dev_info.capabilities |=
6380 BDI_CAP_STABLE_WRITES;
6381 else
6382 mddev->queue->backing_dev_info.capabilities &=
6383 ~BDI_CAP_STABLE_WRITES;
6384 mddev_resume(mddev);
6385 }
6386 mddev_unlock(mddev);
6387 return err ?: len;
Shaohua Lid592a992014-05-21 17:57:44 +08006388}
6389
6390static struct md_sysfs_entry
6391raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
6392 raid5_show_skip_copy,
6393 raid5_store_skip_copy);
6394
Shaohua Lid592a992014-05-21 17:57:44 +08006395static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006396stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006397{
NeilBrownd1688a62011-10-11 16:49:52 +11006398 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006399 if (conf)
6400 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
6401 else
6402 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08006403}
6404
NeilBrown96de1e62005-11-08 21:39:39 -08006405static struct md_sysfs_entry
6406raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08006407
Shaohua Lib7214202013-08-27 17:50:42 +08006408static ssize_t
6409raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
6410{
NeilBrown7b1485b2014-12-15 12:56:59 +11006411 struct r5conf *conf;
6412 int ret = 0;
6413 spin_lock(&mddev->lock);
6414 conf = mddev->private;
Shaohua Lib7214202013-08-27 17:50:42 +08006415 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006416 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
6417 spin_unlock(&mddev->lock);
6418 return ret;
Shaohua Lib7214202013-08-27 17:50:42 +08006419}
6420
majianpeng60aaf932013-11-14 15:16:20 +11006421static int alloc_thread_groups(struct r5conf *conf, int cnt,
6422 int *group_cnt,
6423 int *worker_cnt_per_group,
6424 struct r5worker_group **worker_groups);
Shaohua Lib7214202013-08-27 17:50:42 +08006425static ssize_t
6426raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
6427{
NeilBrown67918752014-12-15 12:57:01 +11006428 struct r5conf *conf;
Shaohua Lib7214202013-08-27 17:50:42 +08006429 unsigned long new;
6430 int err;
majianpeng60aaf932013-11-14 15:16:20 +11006431 struct r5worker_group *new_groups, *old_groups;
6432 int group_cnt, worker_cnt_per_group;
Shaohua Lib7214202013-08-27 17:50:42 +08006433
6434 if (len >= PAGE_SIZE)
6435 return -EINVAL;
Shaohua Lib7214202013-08-27 17:50:42 +08006436 if (kstrtoul(page, 10, &new))
6437 return -EINVAL;
6438
NeilBrown67918752014-12-15 12:57:01 +11006439 err = mddev_lock(mddev);
Shaohua Lib7214202013-08-27 17:50:42 +08006440 if (err)
6441 return err;
NeilBrown67918752014-12-15 12:57:01 +11006442 conf = mddev->private;
6443 if (!conf)
6444 err = -ENODEV;
6445 else if (new != conf->worker_cnt_per_group) {
6446 mddev_suspend(mddev);
6447
6448 old_groups = conf->worker_groups;
6449 if (old_groups)
6450 flush_workqueue(raid5_wq);
6451
6452 err = alloc_thread_groups(conf, new,
6453 &group_cnt, &worker_cnt_per_group,
6454 &new_groups);
6455 if (!err) {
6456 spin_lock_irq(&conf->device_lock);
6457 conf->group_cnt = group_cnt;
6458 conf->worker_cnt_per_group = worker_cnt_per_group;
6459 conf->worker_groups = new_groups;
6460 spin_unlock_irq(&conf->device_lock);
6461
6462 if (old_groups)
6463 kfree(old_groups[0].workers);
6464 kfree(old_groups);
6465 }
6466 mddev_resume(mddev);
6467 }
6468 mddev_unlock(mddev);
6469
6470 return err ?: len;
Shaohua Lib7214202013-08-27 17:50:42 +08006471}
6472
6473static struct md_sysfs_entry
6474raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
6475 raid5_show_group_thread_cnt,
6476 raid5_store_group_thread_cnt);
6477
NeilBrown007583c2005-11-08 21:39:30 -08006478static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08006479 &raid5_stripecache_size.attr,
6480 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006481 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08006482 &raid5_group_thread_cnt.attr,
Shaohua Lid592a992014-05-21 17:57:44 +08006483 &raid5_skip_copy.attr,
Markus Stockhausend06f1912014-12-15 12:57:05 +11006484 &raid5_rmw_level.attr,
Song Liu2c7da142016-11-17 15:24:41 -08006485 &r5c_journal_mode.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08006486 NULL,
6487};
NeilBrown007583c2005-11-08 21:39:30 -08006488static struct attribute_group raid5_attrs_group = {
6489 .name = NULL,
6490 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08006491};
6492
majianpeng60aaf932013-11-14 15:16:20 +11006493static int alloc_thread_groups(struct r5conf *conf, int cnt,
6494 int *group_cnt,
6495 int *worker_cnt_per_group,
6496 struct r5worker_group **worker_groups)
Shaohua Li851c30c2013-08-28 14:30:16 +08006497{
Shaohua Li566c09c2013-11-14 15:16:17 +11006498 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08006499 ssize_t size;
6500 struct r5worker *workers;
6501
majianpeng60aaf932013-11-14 15:16:20 +11006502 *worker_cnt_per_group = cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +08006503 if (cnt == 0) {
majianpeng60aaf932013-11-14 15:16:20 +11006504 *group_cnt = 0;
6505 *worker_groups = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08006506 return 0;
6507 }
majianpeng60aaf932013-11-14 15:16:20 +11006508 *group_cnt = num_possible_nodes();
Shaohua Li851c30c2013-08-28 14:30:16 +08006509 size = sizeof(struct r5worker) * cnt;
majianpeng60aaf932013-11-14 15:16:20 +11006510 workers = kzalloc(size * *group_cnt, GFP_NOIO);
6511 *worker_groups = kzalloc(sizeof(struct r5worker_group) *
6512 *group_cnt, GFP_NOIO);
6513 if (!*worker_groups || !workers) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006514 kfree(workers);
majianpeng60aaf932013-11-14 15:16:20 +11006515 kfree(*worker_groups);
Shaohua Li851c30c2013-08-28 14:30:16 +08006516 return -ENOMEM;
6517 }
6518
majianpeng60aaf932013-11-14 15:16:20 +11006519 for (i = 0; i < *group_cnt; i++) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006520 struct r5worker_group *group;
6521
NeilBrown0c775d52013-11-25 11:12:43 +11006522 group = &(*worker_groups)[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08006523 INIT_LIST_HEAD(&group->handle_list);
6524 group->conf = conf;
6525 group->workers = workers + i * cnt;
6526
6527 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11006528 struct r5worker *worker = group->workers + j;
6529 worker->group = group;
6530 INIT_WORK(&worker->work, raid5_do_work);
6531
6532 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
6533 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08006534 }
6535 }
6536
6537 return 0;
6538}
6539
6540static void free_thread_groups(struct r5conf *conf)
6541{
6542 if (conf->worker_groups)
6543 kfree(conf->worker_groups[0].workers);
6544 kfree(conf->worker_groups);
6545 conf->worker_groups = NULL;
6546}
6547
Dan Williams80c3a6c2009-03-17 18:10:40 -07006548static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11006549raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07006550{
NeilBrownd1688a62011-10-11 16:49:52 +11006551 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006552
6553 if (!sectors)
6554 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006555 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11006556 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11006557 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006558
NeilBrown3cb5edf2015-07-15 17:24:17 +10006559 sectors &= ~((sector_t)conf->chunk_sectors - 1);
6560 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006561 return sectors * (raid_disks - conf->max_degraded);
6562}
6563
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306564static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6565{
6566 safe_put_page(percpu->spare_page);
shli@kernel.org46d5b782014-12-15 12:57:02 +11006567 if (percpu->scribble)
6568 flex_array_free(percpu->scribble);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306569 percpu->spare_page = NULL;
6570 percpu->scribble = NULL;
6571}
6572
6573static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6574{
6575 if (conf->level == 6 && !percpu->spare_page)
6576 percpu->spare_page = alloc_page(GFP_KERNEL);
6577 if (!percpu->scribble)
shli@kernel.org46d5b782014-12-15 12:57:02 +11006578 percpu->scribble = scribble_alloc(max(conf->raid_disks,
NeilBrown738a2732015-05-08 18:19:39 +10006579 conf->previous_raid_disks),
6580 max(conf->chunk_sectors,
6581 conf->prev_chunk_sectors)
6582 / STRIPE_SECTORS,
6583 GFP_KERNEL);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306584
6585 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
6586 free_scratch_buffer(conf, percpu);
6587 return -ENOMEM;
6588 }
6589
6590 return 0;
6591}
6592
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006593static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
6594{
6595 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
6596
6597 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
6598 return 0;
6599}
6600
NeilBrownd1688a62011-10-11 16:49:52 +11006601static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006602{
Dan Williams36d1c642009-07-14 11:48:22 -07006603 if (!conf->percpu)
6604 return;
6605
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006606 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Dan Williams36d1c642009-07-14 11:48:22 -07006607 free_percpu(conf->percpu);
6608}
6609
NeilBrownd1688a62011-10-11 16:49:52 +11006610static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10006611{
Song Liud7bd3982016-11-23 22:50:39 -08006612 int i;
6613
Shaohua Li5c7e81c2015-08-13 14:32:04 -07006614 if (conf->log)
6615 r5l_exit_log(conf->log);
Shaohua Li30c89462016-09-21 09:07:13 -07006616 if (conf->shrinker.nr_deferred)
NeilBrownedbe83a2015-02-26 12:47:56 +11006617 unregister_shrinker(&conf->shrinker);
Shaohua Li5c7e81c2015-08-13 14:32:04 -07006618
Shaohua Li851c30c2013-08-28 14:30:16 +08006619 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10006620 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07006621 raid5_free_percpu(conf);
Song Liud7bd3982016-11-23 22:50:39 -08006622 for (i = 0; i < conf->pool_size; i++)
6623 if (conf->disks[i].extra_page)
6624 put_page(conf->disks[i].extra_page);
Dan Williams95fc17a2009-07-31 12:39:15 +10006625 kfree(conf->disks);
6626 kfree(conf->stripe_hashtbl);
6627 kfree(conf);
6628}
6629
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006630static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
Dan Williams36d1c642009-07-14 11:48:22 -07006631{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006632 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
Dan Williams36d1c642009-07-14 11:48:22 -07006633 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
6634
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006635 if (alloc_scratch_buffer(conf, percpu)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006636 pr_warn("%s: failed memory allocation for cpu%u\n",
6637 __func__, cpu);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006638 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006639 }
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006640 return 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006641}
Dan Williams36d1c642009-07-14 11:48:22 -07006642
NeilBrownd1688a62011-10-11 16:49:52 +11006643static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006644{
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306645 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006646
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306647 conf->percpu = alloc_percpu(struct raid5_percpu);
6648 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07006649 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006650
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006651 err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Shaohua Li27a353c2016-02-24 17:38:28 -08006652 if (!err) {
6653 conf->scribble_disks = max(conf->raid_disks,
6654 conf->previous_raid_disks);
6655 conf->scribble_sectors = max(conf->chunk_sectors,
6656 conf->prev_chunk_sectors);
6657 }
Dan Williams36d1c642009-07-14 11:48:22 -07006658 return err;
6659}
6660
NeilBrownedbe83a2015-02-26 12:47:56 +11006661static unsigned long raid5_cache_scan(struct shrinker *shrink,
6662 struct shrink_control *sc)
6663{
6664 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
NeilBrown2d5b5692015-07-06 12:49:23 +10006665 unsigned long ret = SHRINK_STOP;
6666
6667 if (mutex_trylock(&conf->cache_size_mutex)) {
6668 ret= 0;
NeilBrown49895bc2015-08-03 17:09:57 +10006669 while (ret < sc->nr_to_scan &&
6670 conf->max_nr_stripes > conf->min_nr_stripes) {
NeilBrown2d5b5692015-07-06 12:49:23 +10006671 if (drop_one_stripe(conf) == 0) {
6672 ret = SHRINK_STOP;
6673 break;
6674 }
6675 ret++;
6676 }
6677 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006678 }
6679 return ret;
6680}
6681
6682static unsigned long raid5_cache_count(struct shrinker *shrink,
6683 struct shrink_control *sc)
6684{
6685 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
6686
6687 if (conf->max_nr_stripes < conf->min_nr_stripes)
6688 /* unlikely, but not impossible */
6689 return 0;
6690 return conf->max_nr_stripes - conf->min_nr_stripes;
6691}
6692
NeilBrownd1688a62011-10-11 16:49:52 +11006693static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694{
NeilBrownd1688a62011-10-11 16:49:52 +11006695 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006696 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11006697 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10006699 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11006700 int i;
majianpeng60aaf932013-11-14 15:16:20 +11006701 int group_cnt, worker_cnt_per_group;
6702 struct r5worker_group *new_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703
NeilBrown91adb562009-03-31 14:39:39 +11006704 if (mddev->new_level != 5
6705 && mddev->new_level != 4
6706 && mddev->new_level != 6) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006707 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
6708 mdname(mddev), mddev->new_level);
NeilBrown91adb562009-03-31 14:39:39 +11006709 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710 }
NeilBrown91adb562009-03-31 14:39:39 +11006711 if ((mddev->new_level == 5
6712 && !algorithm_valid_raid5(mddev->new_layout)) ||
6713 (mddev->new_level == 6
6714 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006715 pr_warn("md/raid:%s: layout %d not supported\n",
6716 mdname(mddev), mddev->new_layout);
NeilBrown91adb562009-03-31 14:39:39 +11006717 return ERR_PTR(-EIO);
6718 }
6719 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006720 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
6721 mdname(mddev), mddev->raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006722 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11006723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724
Andre Noll664e7c42009-06-18 08:45:27 +10006725 if (!mddev->new_chunk_sectors ||
6726 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
6727 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006728 pr_warn("md/raid:%s: invalid chunk size %d\n",
6729 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11006730 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11006731 }
6732
NeilBrownd1688a62011-10-11 16:49:52 +11006733 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11006734 if (conf == NULL)
6735 goto abort;
Shaohua Li851c30c2013-08-28 14:30:16 +08006736 /* Don't enable multi-threading by default*/
majianpeng60aaf932013-11-14 15:16:20 +11006737 if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group,
6738 &new_group)) {
6739 conf->group_cnt = group_cnt;
6740 conf->worker_cnt_per_group = worker_cnt_per_group;
6741 conf->worker_groups = new_group;
6742 } else
Shaohua Li851c30c2013-08-28 14:30:16 +08006743 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11006744 spin_lock_init(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10006745 seqcount_init(&conf->gen_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006746 mutex_init(&conf->cache_size_mutex);
Yuanhan Liub1b46482015-05-08 18:19:06 +10006747 init_waitqueue_head(&conf->wait_for_quiescent);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08006748 init_waitqueue_head(&conf->wait_for_stripe);
Dan Williamsf5efd452009-10-16 15:55:38 +11006749 init_waitqueue_head(&conf->wait_for_overlap);
6750 INIT_LIST_HEAD(&conf->handle_list);
6751 INIT_LIST_HEAD(&conf->hold_list);
6752 INIT_LIST_HEAD(&conf->delayed_list);
6753 INIT_LIST_HEAD(&conf->bitmap_list);
NeilBrownc3cce6c2015-08-14 12:47:33 +10006754 bio_list_init(&conf->return_bi);
Shaohua Li773ca822013-08-27 17:50:39 +08006755 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11006756 atomic_set(&conf->active_stripes, 0);
6757 atomic_set(&conf->preread_active_stripes, 0);
6758 atomic_set(&conf->active_aligned_reads, 0);
Shaohua Li765d7042017-01-04 09:33:23 -08006759 bio_list_init(&conf->pending_bios);
6760 spin_lock_init(&conf->pending_bios_lock);
6761 conf->batch_bio_dispatch = true;
6762 rdev_for_each(rdev, mddev) {
6763 if (test_bit(Journal, &rdev->flags))
6764 continue;
6765 if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
6766 conf->batch_bio_dispatch = false;
6767 break;
6768 }
6769 }
6770
Dan Williamsf5efd452009-10-16 15:55:38 +11006771 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11006772 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11006773
6774 conf->raid_disks = mddev->raid_disks;
6775 if (mddev->reshape_position == MaxSector)
6776 conf->previous_raid_disks = mddev->raid_disks;
6777 else
6778 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006779 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006780
NeilBrown5e5e3e72009-10-16 16:35:30 +11006781 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11006782 GFP_KERNEL);
Song Liud7bd3982016-11-23 22:50:39 -08006783
NeilBrown91adb562009-03-31 14:39:39 +11006784 if (!conf->disks)
6785 goto abort;
6786
Song Liud7bd3982016-11-23 22:50:39 -08006787 for (i = 0; i < max_disks; i++) {
6788 conf->disks[i].extra_page = alloc_page(GFP_KERNEL);
6789 if (!conf->disks[i].extra_page)
6790 goto abort;
6791 }
6792
NeilBrown91adb562009-03-31 14:39:39 +11006793 conf->mddev = mddev;
6794
6795 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
6796 goto abort;
6797
Shaohua Li566c09c2013-11-14 15:16:17 +11006798 /* We init hash_locks[0] separately to that it can be used
6799 * as the reference lock in the spin_lock_nest_lock() call
6800 * in lock_all_device_hash_locks_irq in order to convince
6801 * lockdep that we know what we are doing.
6802 */
6803 spin_lock_init(conf->hash_locks);
6804 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
6805 spin_lock_init(conf->hash_locks + i);
6806
6807 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6808 INIT_LIST_HEAD(conf->inactive_list + i);
6809
6810 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6811 INIT_LIST_HEAD(conf->temp_inactive_list + i);
6812
Song Liu1e6d6902016-11-17 15:24:39 -08006813 atomic_set(&conf->r5c_cached_full_stripes, 0);
6814 INIT_LIST_HEAD(&conf->r5c_full_stripe_list);
6815 atomic_set(&conf->r5c_cached_partial_stripes, 0);
6816 INIT_LIST_HEAD(&conf->r5c_partial_stripe_list);
6817
Dan Williams36d1c642009-07-14 11:48:22 -07006818 conf->level = mddev->new_level;
shli@kernel.org46d5b782014-12-15 12:57:02 +11006819 conf->chunk_sectors = mddev->new_chunk_sectors;
Dan Williams36d1c642009-07-14 11:48:22 -07006820 if (raid5_alloc_percpu(conf) != 0)
6821 goto abort;
6822
NeilBrown0c55e022010-05-03 14:09:02 +10006823 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11006824
NeilBrowndafb20f2012-03-19 12:46:39 +11006825 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11006826 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006827 if (raid_disk >= max_disks
Shaohua Lif2076e72015-10-08 21:54:12 -07006828 || raid_disk < 0 || test_bit(Journal, &rdev->flags))
NeilBrown91adb562009-03-31 14:39:39 +11006829 continue;
6830 disk = conf->disks + raid_disk;
6831
NeilBrown17045f52011-12-23 10:17:53 +11006832 if (test_bit(Replacement, &rdev->flags)) {
6833 if (disk->replacement)
6834 goto abort;
6835 disk->replacement = rdev;
6836 } else {
6837 if (disk->rdev)
6838 goto abort;
6839 disk->rdev = rdev;
6840 }
NeilBrown91adb562009-03-31 14:39:39 +11006841
6842 if (test_bit(In_sync, &rdev->flags)) {
6843 char b[BDEVNAME_SIZE];
NeilBrowncc6167b2016-11-02 14:16:50 +11006844 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
6845 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05006846 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11006847 /* Cannot rely on bitmap to complete recovery */
6848 conf->fullsync = 1;
6849 }
6850
NeilBrown91adb562009-03-31 14:39:39 +11006851 conf->level = mddev->new_level;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006852 if (conf->level == 6) {
NeilBrown91adb562009-03-31 14:39:39 +11006853 conf->max_degraded = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006854 if (raid6_call.xor_syndrome)
6855 conf->rmw_level = PARITY_ENABLE_RMW;
6856 else
6857 conf->rmw_level = PARITY_DISABLE_RMW;
6858 } else {
NeilBrown91adb562009-03-31 14:39:39 +11006859 conf->max_degraded = 1;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006860 conf->rmw_level = PARITY_ENABLE_RMW;
6861 }
NeilBrown91adb562009-03-31 14:39:39 +11006862 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11006863 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11006864 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10006865 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11006866 conf->prev_algo = mddev->layout;
NeilBrown5cac6bc2015-07-17 12:17:50 +10006867 } else {
6868 conf->prev_chunk_sectors = conf->chunk_sectors;
6869 conf->prev_algo = conf->algorithm;
NeilBrowne183eae2009-03-31 15:20:22 +11006870 }
NeilBrown91adb562009-03-31 14:39:39 +11006871
NeilBrownedbe83a2015-02-26 12:47:56 +11006872 conf->min_nr_stripes = NR_STRIPES;
Shaohua Liad5b0f72016-08-30 10:29:33 -07006873 if (mddev->reshape_position != MaxSector) {
6874 int stripes = max_t(int,
6875 ((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4,
6876 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4);
6877 conf->min_nr_stripes = max(NR_STRIPES, stripes);
6878 if (conf->min_nr_stripes != NR_STRIPES)
NeilBrowncc6167b2016-11-02 14:16:50 +11006879 pr_info("md/raid:%s: force stripe size %d for reshape\n",
Shaohua Liad5b0f72016-08-30 10:29:33 -07006880 mdname(mddev), conf->min_nr_stripes);
6881 }
NeilBrownedbe83a2015-02-26 12:47:56 +11006882 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11006883 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li4bda5562013-11-14 15:16:17 +11006884 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
NeilBrownedbe83a2015-02-26 12:47:56 +11006885 if (grow_stripes(conf, conf->min_nr_stripes)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006886 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
6887 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11006888 goto abort;
6889 } else
NeilBrowncc6167b2016-11-02 14:16:50 +11006890 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory);
NeilBrownedbe83a2015-02-26 12:47:56 +11006891 /*
6892 * Losing a stripe head costs more than the time to refill it,
6893 * it reduces the queue depth and so can hurt throughput.
6894 * So set it rather large, scaled by number of devices.
6895 */
6896 conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
6897 conf->shrinker.scan_objects = raid5_cache_scan;
6898 conf->shrinker.count_objects = raid5_cache_count;
6899 conf->shrinker.batch = 128;
6900 conf->shrinker.flags = 0;
Chao Yu6a0f53f2016-09-20 10:33:57 +08006901 if (register_shrinker(&conf->shrinker)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006902 pr_warn("md/raid:%s: couldn't register shrinker.\n",
6903 mdname(mddev));
Chao Yu6a0f53f2016-09-20 10:33:57 +08006904 goto abort;
6905 }
NeilBrown91adb562009-03-31 14:39:39 +11006906
NeilBrown02326052012-07-03 15:56:52 +10006907 sprintf(pers_name, "raid%d", mddev->new_level);
6908 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11006909 if (!conf->thread) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006910 pr_warn("md/raid:%s: couldn't allocate thread.\n",
6911 mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11006912 goto abort;
6913 }
6914
6915 return conf;
6916
6917 abort:
6918 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10006919 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11006920 return ERR_PTR(-EIO);
6921 } else
6922 return ERR_PTR(-ENOMEM);
6923}
6924
NeilBrownc148ffd2009-11-13 17:47:00 +11006925static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
6926{
6927 switch (algo) {
6928 case ALGORITHM_PARITY_0:
6929 if (raid_disk < max_degraded)
6930 return 1;
6931 break;
6932 case ALGORITHM_PARITY_N:
6933 if (raid_disk >= raid_disks - max_degraded)
6934 return 1;
6935 break;
6936 case ALGORITHM_PARITY_0_6:
NeilBrownf72ffdd2014-09-30 14:23:59 +10006937 if (raid_disk == 0 ||
NeilBrownc148ffd2009-11-13 17:47:00 +11006938 raid_disk == raid_disks - 1)
6939 return 1;
6940 break;
6941 case ALGORITHM_LEFT_ASYMMETRIC_6:
6942 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6943 case ALGORITHM_LEFT_SYMMETRIC_6:
6944 case ALGORITHM_RIGHT_SYMMETRIC_6:
6945 if (raid_disk == raid_disks - 1)
6946 return 1;
6947 }
6948 return 0;
6949}
6950
Shaohua Li849674e2016-01-20 13:52:20 -08006951static int raid5_run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11006952{
NeilBrownd1688a62011-10-11 16:49:52 +11006953 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10006954 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11006955 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006956 struct md_rdev *rdev;
Shaohua Li713cf5a2015-08-13 14:32:03 -07006957 struct md_rdev *journal_dev = NULL;
NeilBrownc148ffd2009-11-13 17:47:00 +11006958 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11006959 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10006960 long long min_offset_diff = 0;
6961 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11006962
Andre Noll8c6ac8682009-06-18 08:48:06 +10006963 if (mddev->recovery_cp != MaxSector)
NeilBrowncc6167b2016-11-02 14:16:50 +11006964 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
6965 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10006966
6967 rdev_for_each(rdev, mddev) {
6968 long long diff;
Shaohua Li713cf5a2015-08-13 14:32:03 -07006969
Shaohua Lif2076e72015-10-08 21:54:12 -07006970 if (test_bit(Journal, &rdev->flags)) {
Shaohua Li713cf5a2015-08-13 14:32:03 -07006971 journal_dev = rdev;
Shaohua Lif2076e72015-10-08 21:54:12 -07006972 continue;
6973 }
NeilBrownb5254dd2012-05-21 09:27:01 +10006974 if (rdev->raid_disk < 0)
6975 continue;
6976 diff = (rdev->new_data_offset - rdev->data_offset);
6977 if (first) {
6978 min_offset_diff = diff;
6979 first = 0;
6980 } else if (mddev->reshape_backwards &&
6981 diff < min_offset_diff)
6982 min_offset_diff = diff;
6983 else if (!mddev->reshape_backwards &&
6984 diff > min_offset_diff)
6985 min_offset_diff = diff;
6986 }
6987
NeilBrownf6705572006-03-27 01:18:11 -08006988 if (mddev->reshape_position != MaxSector) {
6989 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10006990 * Difficulties arise if the stripe we would write to
6991 * next is at or after the stripe we would read from next.
6992 * For a reshape that changes the number of devices, this
6993 * is only possible for a very short time, and mdadm makes
6994 * sure that time appears to have past before assembling
6995 * the array. So we fail if that time hasn't passed.
6996 * For a reshape that keeps the number of devices the same
6997 * mdadm must be monitoring the reshape can keeping the
6998 * critical areas read-only and backed up. It will start
6999 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08007000 */
7001 sector_t here_new, here_old;
7002 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11007003 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrown05256d92015-07-15 17:36:21 +10007004 int chunk_sectors;
7005 int new_data_disks;
NeilBrownf6705572006-03-27 01:18:11 -08007006
Shaohua Li713cf5a2015-08-13 14:32:03 -07007007 if (journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007008 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
7009 mdname(mddev));
Shaohua Li713cf5a2015-08-13 14:32:03 -07007010 return -EINVAL;
7011 }
7012
NeilBrown88ce4932009-03-31 15:24:23 +11007013 if (mddev->new_level != mddev->level) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007014 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
7015 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007016 return -EINVAL;
7017 }
NeilBrownf6705572006-03-27 01:18:11 -08007018 old_disks = mddev->raid_disks - mddev->delta_disks;
7019 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08007020 * further up in new geometry must map after here in old
7021 * geometry.
NeilBrown05256d92015-07-15 17:36:21 +10007022 * If the chunk sizes are different, then as we perform reshape
7023 * in units of the largest of the two, reshape_position needs
7024 * be a multiple of the largest chunk size times new data disks.
NeilBrownf6705572006-03-27 01:18:11 -08007025 */
7026 here_new = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007027 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
7028 new_data_disks = mddev->raid_disks - max_degraded;
7029 if (sector_div(here_new, chunk_sectors * new_data_disks)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007030 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
7031 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007032 return -EINVAL;
7033 }
NeilBrown05256d92015-07-15 17:36:21 +10007034 reshape_offset = here_new * chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08007035 /* here_new is the stripe we will write to */
7036 here_old = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007037 sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
NeilBrownf4168852007-02-28 20:11:53 -08007038 /* here_old is the first stripe that we might need to read
7039 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10007040 if (mddev->delta_disks == 0) {
7041 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10007042 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10007043 * and taking constant backups.
7044 * mdadm always starts a situation like this in
7045 * readonly mode so it can take control before
7046 * allowing any writes. So just check for that.
7047 */
NeilBrownb5254dd2012-05-21 09:27:01 +10007048 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
7049 abs(min_offset_diff) >= mddev->new_chunk_sectors)
7050 /* not really in-place - so OK */;
7051 else if (mddev->ro == 0) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007052 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
7053 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10007054 return -EINVAL;
7055 }
NeilBrown2c810cd2012-05-21 09:27:00 +10007056 } else if (mddev->reshape_backwards
NeilBrown05256d92015-07-15 17:36:21 +10007057 ? (here_new * chunk_sectors + min_offset_diff <=
7058 here_old * chunk_sectors)
7059 : (here_new * chunk_sectors >=
7060 here_old * chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08007061 /* Reading from the same stripe as writing to - bad */
NeilBrowncc6167b2016-11-02 14:16:50 +11007062 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
7063 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007064 return -EINVAL;
7065 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007066 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007067 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08007068 } else {
NeilBrown91adb562009-03-31 14:39:39 +11007069 BUG_ON(mddev->level != mddev->new_level);
7070 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10007071 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11007072 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08007073 }
7074
NeilBrown245f46c2009-03-31 14:39:39 +11007075 if (mddev->private == NULL)
7076 conf = setup_conf(mddev);
7077 else
7078 conf = mddev->private;
7079
NeilBrown91adb562009-03-31 14:39:39 +11007080 if (IS_ERR(conf))
7081 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08007082
Song Liu486b0f72016-08-19 15:34:01 -07007083 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
7084 if (!journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007085 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
7086 mdname(mddev));
Song Liu486b0f72016-08-19 15:34:01 -07007087 mddev->ro = 1;
7088 set_disk_ro(mddev->gendisk, 1);
7089 } else if (mddev->recovery_cp == MaxSector)
7090 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
Shaohua Li7dde2ad2015-10-08 21:54:10 -07007091 }
7092
NeilBrownb5254dd2012-05-21 09:27:01 +10007093 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11007094 mddev->thread = conf->thread;
7095 conf->thread = NULL;
7096 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097
NeilBrown17045f52011-12-23 10:17:53 +11007098 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
7099 i++) {
7100 rdev = conf->disks[i].rdev;
7101 if (!rdev && conf->disks[i].replacement) {
7102 /* The replacement is all we have yet */
7103 rdev = conf->disks[i].replacement;
7104 conf->disks[i].replacement = NULL;
7105 clear_bit(Replacement, &rdev->flags);
7106 conf->disks[i].rdev = rdev;
7107 }
7108 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11007109 continue;
NeilBrown17045f52011-12-23 10:17:53 +11007110 if (conf->disks[i].replacement &&
7111 conf->reshape_progress != MaxSector) {
7112 /* replacements and reshape simply do not mix. */
NeilBrowncc6167b2016-11-02 14:16:50 +11007113 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
NeilBrown17045f52011-12-23 10:17:53 +11007114 goto abort;
7115 }
NeilBrown2f115882010-06-17 17:41:03 +10007116 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11007117 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10007118 continue;
7119 }
NeilBrownc148ffd2009-11-13 17:47:00 +11007120 /* This disc is not fully in-sync. However if it
7121 * just stored parity (beyond the recovery_offset),
7122 * when we don't need to be concerned about the
7123 * array being dirty.
7124 * When reshape goes 'backwards', we never have
7125 * partially completed devices, so we only need
7126 * to worry about reshape going forwards.
7127 */
7128 /* Hack because v0.91 doesn't store recovery_offset properly. */
7129 if (mddev->major_version == 0 &&
7130 mddev->minor_version > 90)
7131 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007132
NeilBrownc148ffd2009-11-13 17:47:00 +11007133 if (rdev->recovery_offset < reshape_offset) {
7134 /* We need to check old and new layout */
7135 if (!only_parity(rdev->raid_disk,
7136 conf->algorithm,
7137 conf->raid_disks,
7138 conf->max_degraded))
7139 continue;
7140 }
7141 if (!only_parity(rdev->raid_disk,
7142 conf->prev_algo,
7143 conf->previous_raid_disks,
7144 conf->max_degraded))
7145 continue;
7146 dirty_parity_disks++;
7147 }
NeilBrown91adb562009-03-31 14:39:39 +11007148
NeilBrown17045f52011-12-23 10:17:53 +11007149 /*
7150 * 0 for a fully functional array, 1 or 2 for a degraded array.
7151 */
Song Liu2e38a372017-01-24 10:45:30 -08007152 mddev->degraded = raid5_calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153
NeilBrown674806d2010-06-16 17:17:53 +10007154 if (has_failed(conf)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007155 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07007156 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157 goto abort;
7158 }
7159
NeilBrown91adb562009-03-31 14:39:39 +11007160 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10007161 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11007162 mddev->resync_max_sectors = mddev->dev_sectors;
7163
NeilBrownc148ffd2009-11-13 17:47:00 +11007164 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007166 if (mddev->ok_start_degraded)
NeilBrowncc6167b2016-11-02 14:16:50 +11007167 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
7168 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007169 else {
NeilBrowncc6167b2016-11-02 14:16:50 +11007170 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
7171 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007172 goto abort;
7173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 }
7175
NeilBrowncc6167b2016-11-02 14:16:50 +11007176 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
7177 mdname(mddev), conf->level,
7178 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
7179 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180
7181 print_raid5_conf(conf);
7182
NeilBrownfef9c612009-03-31 15:16:46 +11007183 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11007184 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08007185 atomic_set(&conf->reshape_stripes, 0);
7186 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7187 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7188 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7189 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7190 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007191 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08007192 }
7193
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10007195 if (mddev->to_remove == &raid5_attrs_group)
7196 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007197 else if (mddev->kobj.sd &&
7198 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrowncc6167b2016-11-02 14:16:50 +11007199 pr_warn("raid5: failed to create sysfs attributes for %s\n",
7200 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10007201 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
7202
7203 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007204 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11007205 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10007206 /* read-ahead size must cover two whole stripes, which
7207 * is 2 * (datadisks) * chunksize where 'n' is the
7208 * number of raid devices
7209 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 int data_disks = conf->previous_raid_disks - conf->max_degraded;
7211 int stripe = data_disks *
7212 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
7213 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
7214 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10007215
NeilBrown9f7c2222010-07-26 12:04:13 +10007216 chunk_size = mddev->chunk_sectors << 9;
7217 blk_queue_io_min(mddev->queue, chunk_size);
7218 blk_queue_io_opt(mddev->queue, chunk_size *
7219 (conf->raid_disks - conf->max_degraded));
Kent Overstreetc78afc62013-07-11 22:39:53 -07007220 mddev->queue->limits.raid_partial_stripes_expensive = 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007221 /*
7222 * We can only discard a whole stripe. It doesn't make sense to
7223 * discard data disk but write parity disk
7224 */
7225 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11007226 /* Round up to power of 2, as discard handling
7227 * currently assumes that */
7228 while ((stripe-1) & stripe)
7229 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007230 mddev->queue->limits.discard_alignment = stripe;
7231 mddev->queue->limits.discard_granularity = stripe;
Konstantin Khlebnikove8d7c332016-11-27 19:32:32 +03007232
7233 /*
7234 * We use 16-bit counter of active stripes in bi_phys_segments
7235 * (minus one for over-loaded initialization)
7236 */
7237 blk_queue_max_hw_sectors(mddev->queue, 0xfffe * STRIPE_SECTORS);
7238 blk_queue_max_discard_sectors(mddev->queue,
7239 0xfffe * STRIPE_SECTORS);
7240
Shaohua Li620125f2012-10-11 13:49:05 +11007241 /*
7242 * unaligned part of discard request will be ignored, so can't
NeilBrown8e0e99b2014-10-02 13:45:00 +10007243 * guarantee discard_zeroes_data
Shaohua Li620125f2012-10-11 13:49:05 +11007244 */
7245 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10007246
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007247 blk_queue_max_write_same_sectors(mddev->queue, 0);
7248
NeilBrown05616be2012-05-21 09:27:00 +10007249 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007250 disk_stack_limits(mddev->gendisk, rdev->bdev,
7251 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10007252 disk_stack_limits(mddev->gendisk, rdev->bdev,
7253 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11007254 /*
7255 * discard_zeroes_data is required, otherwise data
7256 * could be lost. Consider a scenario: discard a stripe
7257 * (the stripe could be inconsistent if
7258 * discard_zeroes_data is 0); write one disk of the
7259 * stripe (the stripe could be inconsistent again
7260 * depending on which disks are used to calculate
7261 * parity); the disk is broken; The stripe data of this
7262 * disk is lost.
7263 */
7264 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
7265 !bdev_get_queue(rdev->bdev)->
7266 limits.discard_zeroes_data)
7267 discard_supported = false;
NeilBrown8e0e99b2014-10-02 13:45:00 +10007268 /* Unfortunately, discard_zeroes_data is not currently
7269 * a guarantee - just a hint. So we only allow DISCARD
7270 * if the sysadmin has confirmed that only safe devices
7271 * are in use by setting a module parameter.
7272 */
7273 if (!devices_handle_discard_safely) {
7274 if (discard_supported) {
7275 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
7276 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
7277 }
7278 discard_supported = false;
7279 }
NeilBrown05616be2012-05-21 09:27:00 +10007280 }
Shaohua Li620125f2012-10-11 13:49:05 +11007281
7282 if (discard_supported &&
Jes Sorensene7597e62016-02-16 16:44:24 -05007283 mddev->queue->limits.max_discard_sectors >= (stripe >> 9) &&
7284 mddev->queue->limits.discard_granularity >= stripe)
Shaohua Li620125f2012-10-11 13:49:05 +11007285 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
7286 mddev->queue);
7287 else
7288 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
7289 mddev->queue);
Shaohua Li1dffddd2016-09-08 10:49:06 -07007290
7291 blk_queue_max_hw_sectors(mddev->queue, UINT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292 }
7293
Shaohua Li5c7e81c2015-08-13 14:32:04 -07007294 if (journal_dev) {
7295 char b[BDEVNAME_SIZE];
7296
NeilBrowncc6167b2016-11-02 14:16:50 +11007297 pr_debug("md/raid:%s: using device %s as journal\n",
7298 mdname(mddev), bdevname(journal_dev->bdev, b));
Song Liu5aabf7c2016-11-17 15:24:44 -08007299 if (r5l_init_log(conf, journal_dev))
7300 goto abort;
Shaohua Li5c7e81c2015-08-13 14:32:04 -07007301 }
7302
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303 return 0;
7304abort:
NeilBrown01f96c02011-09-21 15:30:20 +10007305 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11007306 print_raid5_conf(conf);
7307 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007308 mddev->private = NULL;
NeilBrowncc6167b2016-11-02 14:16:50 +11007309 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310 return -EIO;
7311}
7312
NeilBrownafa0f552014-12-15 12:56:58 +11007313static void raid5_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314{
NeilBrownafa0f552014-12-15 12:56:58 +11007315 struct r5conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316
Dan Williams95fc17a2009-07-31 12:39:15 +10007317 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10007318 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319}
7320
Shaohua Li849674e2016-01-20 13:52:20 -08007321static void raid5_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322{
NeilBrownd1688a62011-10-11 16:49:52 +11007323 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324 int i;
7325
Andre Noll9d8f0362009-06-18 08:45:01 +10007326 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
NeilBrown3cb5edf2015-07-15 17:24:17 +10007327 conf->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07007328 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
NeilBrown5fd13352016-06-02 16:19:52 +10007329 rcu_read_lock();
7330 for (i = 0; i < conf->raid_disks; i++) {
7331 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
7332 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
7333 }
7334 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336}
7337
NeilBrownd1688a62011-10-11 16:49:52 +11007338static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339{
7340 int i;
7341 struct disk_info *tmp;
7342
NeilBrowncc6167b2016-11-02 14:16:50 +11007343 pr_debug("RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344 if (!conf) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007345 pr_debug("(conf==NULL)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346 return;
7347 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007348 pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level,
NeilBrown0c55e022010-05-03 14:09:02 +10007349 conf->raid_disks,
7350 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007351
7352 for (i = 0; i < conf->raid_disks; i++) {
7353 char b[BDEVNAME_SIZE];
7354 tmp = conf->disks + i;
7355 if (tmp->rdev)
NeilBrowncc6167b2016-11-02 14:16:50 +11007356 pr_debug(" disk %d, o:%d, dev:%s\n",
NeilBrown0c55e022010-05-03 14:09:02 +10007357 i, !test_bit(Faulty, &tmp->rdev->flags),
7358 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359 }
7360}
7361
NeilBrownfd01b882011-10-11 16:47:53 +11007362static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363{
7364 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11007365 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007366 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10007367 int count = 0;
7368 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369
7370 for (i = 0; i < conf->raid_disks; i++) {
7371 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11007372 if (tmp->replacement
7373 && tmp->replacement->recovery_offset == MaxSector
7374 && !test_bit(Faulty, &tmp->replacement->flags)
7375 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
7376 /* Replacement has just become active. */
7377 if (!tmp->rdev
7378 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
7379 count++;
7380 if (tmp->rdev) {
7381 /* Replaced device not technically faulty,
7382 * but we need to be sure it gets removed
7383 * and never re-added.
7384 */
7385 set_bit(Faulty, &tmp->rdev->flags);
7386 sysfs_notify_dirent_safe(
7387 tmp->rdev->sysfs_state);
7388 }
7389 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
7390 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10007391 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08007392 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07007393 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10007394 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11007395 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396 }
7397 }
NeilBrown6b965622010-08-18 11:56:59 +10007398 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08007399 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007400 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007402 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403}
7404
NeilBrownb8321b62011-12-23 10:17:51 +11007405static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007406{
NeilBrownd1688a62011-10-11 16:49:52 +11007407 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11007409 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11007410 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411 struct disk_info *p = conf->disks + number;
7412
7413 print_raid5_conf(conf);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007414 if (test_bit(Journal, &rdev->flags) && conf->log) {
7415 struct r5l_log *log;
Shaohua Lic2bb6242015-10-08 21:54:07 -07007416 /*
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007417 * we can't wait pending write here, as this is called in
7418 * raid5d, wait will deadlock.
Shaohua Lic2bb6242015-10-08 21:54:07 -07007419 */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007420 if (atomic_read(&mddev->writes_pending))
7421 return -EBUSY;
7422 log = conf->log;
7423 conf->log = NULL;
7424 synchronize_rcu();
7425 r5l_exit_log(log);
7426 return 0;
Shaohua Lic2bb6242015-10-08 21:54:07 -07007427 }
NeilBrown657e3e42011-12-23 10:17:52 +11007428 if (rdev == p->rdev)
7429 rdevp = &p->rdev;
7430 else if (rdev == p->replacement)
7431 rdevp = &p->replacement;
7432 else
7433 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11007434
NeilBrown657e3e42011-12-23 10:17:52 +11007435 if (number >= conf->raid_disks &&
7436 conf->reshape_progress == MaxSector)
7437 clear_bit(In_sync, &rdev->flags);
7438
7439 if (test_bit(In_sync, &rdev->flags) ||
7440 atomic_read(&rdev->nr_pending)) {
7441 err = -EBUSY;
7442 goto abort;
7443 }
7444 /* Only remove non-faulty devices if recovery
7445 * isn't possible.
7446 */
7447 if (!test_bit(Faulty, &rdev->flags) &&
7448 mddev->recovery_disabled != conf->recovery_disabled &&
7449 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11007450 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11007451 number < conf->raid_disks) {
7452 err = -EBUSY;
7453 goto abort;
7454 }
7455 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10007456 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
7457 synchronize_rcu();
7458 if (atomic_read(&rdev->nr_pending)) {
7459 /* lost the race, try later */
7460 err = -EBUSY;
7461 *rdevp = rdev;
7462 }
7463 }
7464 if (p->replacement) {
NeilBrowndd054fc2011-12-23 10:17:53 +11007465 /* We must have just cleared 'rdev' */
7466 p->rdev = p->replacement;
7467 clear_bit(Replacement, &p->replacement->flags);
7468 smp_mb(); /* Make sure other CPUs may see both as identical
7469 * but will never see neither - if they are careful
7470 */
7471 p->replacement = NULL;
7472 clear_bit(WantReplacement, &rdev->flags);
7473 } else
7474 /* We might have just removed the Replacement as faulty-
7475 * clear the bit just in case
7476 */
7477 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007478abort:
7479
7480 print_raid5_conf(conf);
7481 return err;
7482}
7483
NeilBrownfd01b882011-10-11 16:47:53 +11007484static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485{
NeilBrownd1688a62011-10-11 16:49:52 +11007486 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10007487 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488 int disk;
7489 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10007490 int first = 0;
7491 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007492
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007493 if (test_bit(Journal, &rdev->flags)) {
7494 char b[BDEVNAME_SIZE];
7495 if (conf->log)
7496 return -EBUSY;
7497
7498 rdev->raid_disk = 0;
7499 /*
7500 * The array is in readonly mode if journal is missing, so no
7501 * write requests running. We should be safe
7502 */
7503 r5l_init_log(conf, rdev);
NeilBrowncc6167b2016-11-02 14:16:50 +11007504 pr_debug("md/raid:%s: using device %s as journal\n",
7505 mdname(mddev), bdevname(rdev->bdev, b));
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007506 return 0;
7507 }
NeilBrown7f0da592011-07-28 11:39:22 +10007508 if (mddev->recovery_disabled == conf->recovery_disabled)
7509 return -EBUSY;
7510
NeilBrowndc10c642012-03-19 12:46:37 +11007511 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10007513 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514
Neil Brown6c2fce22008-06-28 08:31:31 +10007515 if (rdev->raid_disk >= 0)
7516 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007517
7518 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07007519 * find the disk ... but prefer rdev->saved_raid_disk
7520 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521 */
NeilBrown16a53ec2006-06-26 00:27:38 -07007522 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10007523 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07007524 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10007525 first = rdev->saved_raid_disk;
7526
7527 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007528 p = conf->disks + disk;
7529 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08007530 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007531 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10007532 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07007533 if (rdev->saved_raid_disk != disk)
7534 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08007535 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10007536 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007538 }
7539 for (disk = first; disk <= last; disk++) {
7540 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007541 if (test_bit(WantReplacement, &p->rdev->flags) &&
7542 p->replacement == NULL) {
7543 clear_bit(In_sync, &rdev->flags);
7544 set_bit(Replacement, &rdev->flags);
7545 rdev->raid_disk = disk;
7546 err = 0;
7547 conf->fullsync = 1;
7548 rcu_assign_pointer(p->replacement, rdev);
7549 break;
7550 }
7551 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007552out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10007554 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555}
7556
NeilBrownfd01b882011-10-11 16:47:53 +11007557static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007558{
7559 /* no resync is happening, and there is enough space
7560 * on all devices, so we can resize.
7561 * We need to make sure resync covers any new space.
7562 * If the array is shrinking we should possibly wait until
7563 * any io in the removed space completes, but it hardly seems
7564 * worth it.
7565 */
NeilBrowna4a61252012-05-22 13:55:27 +10007566 sector_t newsize;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007567 struct r5conf *conf = mddev->private;
7568
Shaohua Li713cf5a2015-08-13 14:32:03 -07007569 if (conf->log)
7570 return -EINVAL;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007571 sectors &= ~((sector_t)conf->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10007572 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
7573 if (mddev->external_size &&
7574 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11007575 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10007576 if (mddev->bitmap) {
7577 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
7578 if (ret)
7579 return ret;
7580 }
7581 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10007582 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10007583 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10007584 if (sectors > mddev->dev_sectors &&
7585 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11007586 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007587 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7588 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007589 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07007590 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591 return 0;
7592}
7593
NeilBrownfd01b882011-10-11 16:47:53 +11007594static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10007595{
7596 /* Can only proceed if there are plenty of stripe_heads.
7597 * We need a minimum of one full stripe,, and for sensible progress
7598 * it is best to have about 4 times that.
7599 * If we require 4 times, then the default 256 4K stripe_heads will
7600 * allow for chunk sizes up to 256K, which is probably OK.
7601 * If the chunk size is greater, user-space should request more
7602 * stripe_heads first.
7603 */
NeilBrownd1688a62011-10-11 16:49:52 +11007604 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10007605 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007606 > conf->min_nr_stripes ||
NeilBrown01ee22b2009-06-18 08:47:20 +10007607 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007608 > conf->min_nr_stripes) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007609 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7610 mdname(mddev),
7611 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
7612 / STRIPE_SIZE)*4);
NeilBrown01ee22b2009-06-18 08:47:20 +10007613 return 0;
7614 }
7615 return 1;
7616}
7617
NeilBrownfd01b882011-10-11 16:47:53 +11007618static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08007619{
NeilBrownd1688a62011-10-11 16:49:52 +11007620 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08007621
Shaohua Li713cf5a2015-08-13 14:32:03 -07007622 if (conf->log)
7623 return -EINVAL;
NeilBrown88ce4932009-03-31 15:24:23 +11007624 if (mddev->delta_disks == 0 &&
7625 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10007626 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10007627 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10007628 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11007629 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10007630 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11007631 /* We might be able to shrink, but the devices must
7632 * be made bigger first.
7633 * For raid6, 4 is the minimum size.
7634 * Otherwise 2 is the minimum
7635 */
7636 int min = 2;
7637 if (mddev->level == 6)
7638 min = 4;
7639 if (mddev->raid_disks + mddev->delta_disks < min)
7640 return -EINVAL;
7641 }
NeilBrown29269552006-03-27 01:18:10 -08007642
NeilBrown01ee22b2009-06-18 08:47:20 +10007643 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08007644 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08007645
NeilBrown738a2732015-05-08 18:19:39 +10007646 if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
7647 mddev->delta_disks > 0)
7648 if (resize_chunks(conf,
7649 conf->previous_raid_disks
7650 + max(0, mddev->delta_disks),
7651 max(mddev->new_chunk_sectors,
7652 mddev->chunk_sectors)
7653 ) < 0)
7654 return -ENOMEM;
NeilBrowne56108d62012-10-11 14:24:13 +11007655 return resize_stripes(conf, (conf->previous_raid_disks
7656 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08007657}
7658
NeilBrownfd01b882011-10-11 16:47:53 +11007659static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08007660{
NeilBrownd1688a62011-10-11 16:49:52 +11007661 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11007662 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08007663 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07007664 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08007665
NeilBrownf4168852007-02-28 20:11:53 -08007666 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08007667 return -EBUSY;
7668
NeilBrown01ee22b2009-06-18 08:47:20 +10007669 if (!check_stripe_cache(mddev))
7670 return -ENOSPC;
7671
NeilBrown30b67642012-05-22 13:55:28 +10007672 if (has_failed(conf))
7673 return -EINVAL;
7674
NeilBrownc6563a82012-05-21 09:27:00 +10007675 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11007676 if (!test_bit(In_sync, &rdev->flags)
7677 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08007678 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10007679 }
NeilBrown63c70c42006-03-27 01:18:13 -08007680
NeilBrownf4168852007-02-28 20:11:53 -08007681 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08007682 /* Not enough devices even to make a degraded array
7683 * of that size
7684 */
7685 return -EINVAL;
7686
NeilBrownec32a2b2009-03-31 15:17:38 +11007687 /* Refuse to reduce size of the array. Any reductions in
7688 * array size must be through explicit setting of array_size
7689 * attribute.
7690 */
7691 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
7692 < mddev->array_sectors) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007693 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
7694 mdname(mddev));
NeilBrownec32a2b2009-03-31 15:17:38 +11007695 return -EINVAL;
7696 }
7697
NeilBrownf6705572006-03-27 01:18:11 -08007698 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08007699 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10007700 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007701 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08007702 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007703 conf->prev_chunk_sectors = conf->chunk_sectors;
7704 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11007705 conf->prev_algo = conf->algorithm;
7706 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10007707 conf->generation++;
7708 /* Code that selects data_offset needs to see the generation update
7709 * if reshape_progress has been set - so a memory barrier needed.
7710 */
7711 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10007712 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11007713 conf->reshape_progress = raid5_size(mddev, 0, 0);
7714 else
7715 conf->reshape_progress = 0;
7716 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10007717 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007718 spin_unlock_irq(&conf->device_lock);
7719
NeilBrown4d77e3b2013-08-27 15:57:47 +10007720 /* Now make sure any requests that proceeded on the assumption
7721 * the reshape wasn't running - like Discard or Read - have
7722 * completed.
7723 */
7724 mddev_suspend(mddev);
7725 mddev_resume(mddev);
7726
NeilBrown29269552006-03-27 01:18:10 -08007727 /* Add some new drives, as many as will fit.
7728 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10007729 * Don't add devices if we are reducing the number of
7730 * devices in the array. This is because it is not possible
7731 * to correctly record the "partially reconstructed" state of
7732 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08007733 */
NeilBrown87a8dec2011-01-31 11:57:43 +11007734 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11007735 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11007736 if (rdev->raid_disk < 0 &&
7737 !test_bit(Faulty, &rdev->flags)) {
7738 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11007739 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11007740 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11007741 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11007742 else
NeilBrown87a8dec2011-01-31 11:57:43 +11007743 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10007744
7745 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11007746 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11007747 }
NeilBrown87a8dec2011-01-31 11:57:43 +11007748 } else if (rdev->raid_disk >= conf->previous_raid_disks
7749 && !test_bit(Faulty, &rdev->flags)) {
7750 /* This is a spare that was manually added */
7751 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11007752 }
NeilBrown29269552006-03-27 01:18:10 -08007753
NeilBrown87a8dec2011-01-31 11:57:43 +11007754 /* When a reshape changes the number of devices,
7755 * ->degraded is measured against the larger of the
7756 * pre and post number of devices.
7757 */
NeilBrownec32a2b2009-03-31 15:17:38 +11007758 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08007759 mddev->degraded = raid5_calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11007760 spin_unlock_irqrestore(&conf->device_lock, flags);
7761 }
NeilBrown63c70c42006-03-27 01:18:13 -08007762 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10007763 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08007764 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownf6705572006-03-27 01:18:11 -08007765
NeilBrown29269552006-03-27 01:18:10 -08007766 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7767 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10007768 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown29269552006-03-27 01:18:10 -08007769 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7770 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7771 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007772 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08007773 if (!mddev->sync_thread) {
7774 mddev->recovery = 0;
7775 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11007776 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007777 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11007778 mddev->new_chunk_sectors =
7779 conf->chunk_sectors = conf->prev_chunk_sectors;
7780 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10007781 rdev_for_each(rdev, mddev)
7782 rdev->new_data_offset = rdev->data_offset;
7783 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11007784 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11007785 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11007786 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11007787 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007788 spin_unlock_irq(&conf->device_lock);
7789 return -EAGAIN;
7790 }
NeilBrownc8f517c2009-03-31 15:28:40 +11007791 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08007792 md_wakeup_thread(mddev->sync_thread);
7793 md_new_event(mddev);
7794 return 0;
7795}
NeilBrown29269552006-03-27 01:18:10 -08007796
NeilBrownec32a2b2009-03-31 15:17:38 +11007797/* This is called from the reshape thread and should make any
7798 * changes needed in 'conf'
7799 */
NeilBrownd1688a62011-10-11 16:49:52 +11007800static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08007801{
NeilBrown29269552006-03-27 01:18:10 -08007802
NeilBrownf6705572006-03-27 01:18:11 -08007803 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10007804 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07007805
NeilBrownf6705572006-03-27 01:18:11 -08007806 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11007807 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10007808 rdev_for_each(rdev, conf->mddev)
7809 rdev->data_offset = rdev->new_data_offset;
7810 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11007811 conf->reshape_progress = MaxSector;
NeilBrown6cbd8142015-07-24 13:30:32 +10007812 conf->mddev->reshape_position = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08007813 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11007814 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07007815
7816 /* read-ahead size must cover two whole stripes, which is
7817 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
7818 */
NeilBrown4a5add42010-06-01 19:37:28 +10007819 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11007820 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007821 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11007822 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07007823 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
7824 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
7825 }
NeilBrown29269552006-03-27 01:18:10 -08007826 }
NeilBrown29269552006-03-27 01:18:10 -08007827}
7828
NeilBrownec32a2b2009-03-31 15:17:38 +11007829/* This is called from the raid5d thread with mddev_lock held.
7830 * It makes config changes to the device.
7831 */
NeilBrownfd01b882011-10-11 16:47:53 +11007832static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11007833{
NeilBrownd1688a62011-10-11 16:49:52 +11007834 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11007835
7836 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7837
NeilBrownec32a2b2009-03-31 15:17:38 +11007838 if (mddev->delta_disks > 0) {
7839 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
Heinz Mauelshagenfe67d192016-05-03 22:15:31 +02007840 if (mddev->queue) {
7841 set_capacity(mddev->gendisk, mddev->array_sectors);
7842 revalidate_disk(mddev->gendisk);
7843 }
NeilBrownec32a2b2009-03-31 15:17:38 +11007844 } else {
7845 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11007846 spin_lock_irq(&conf->device_lock);
Song Liu2e38a372017-01-24 10:45:30 -08007847 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11007848 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11007849 for (d = conf->raid_disks ;
7850 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10007851 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11007852 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10007853 if (rdev)
7854 clear_bit(In_sync, &rdev->flags);
7855 rdev = conf->disks[d].replacement;
7856 if (rdev)
7857 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10007858 }
NeilBrowncea9c222009-03-31 15:15:05 +11007859 }
NeilBrown88ce4932009-03-31 15:24:23 +11007860 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007861 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11007862 mddev->reshape_position = MaxSector;
7863 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007864 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11007865 }
7866}
7867
NeilBrownfd01b882011-10-11 16:47:53 +11007868static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07007869{
NeilBrownd1688a62011-10-11 16:49:52 +11007870 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07007871
7872 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08007873 case 2: /* resume for a suspend */
7874 wake_up(&conf->wait_for_overlap);
7875 break;
7876
NeilBrown72626682005-09-09 16:23:54 -07007877 case 1: /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11007878 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10007879 /* '2' tells resync/reshape to pause so that all
7880 * active stripes can drain
7881 */
Song Liua39f7af2016-11-17 15:24:40 -08007882 r5c_flush_cache(conf, INT_MAX);
NeilBrown64bd6602009-08-03 10:59:58 +10007883 conf->quiesce = 2;
Yuanhan Liub1b46482015-05-08 18:19:06 +10007884 wait_event_cmd(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08007885 atomic_read(&conf->active_stripes) == 0 &&
7886 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11007887 unlock_all_device_hash_locks_irq(conf),
7888 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10007889 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11007890 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10007891 /* allow reshape to continue */
7892 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07007893 break;
7894
7895 case 0: /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11007896 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07007897 conf->quiesce = 0;
Yuanhan Liub1b46482015-05-08 18:19:06 +10007898 wake_up(&conf->wait_for_quiescent);
NeilBrowne464eaf2006-03-27 01:18:14 -08007899 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11007900 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07007901 break;
7902 }
Shaohua Lie6c033f2015-10-04 09:20:12 -07007903 r5l_quiesce(conf->log, state);
NeilBrown72626682005-09-09 16:23:54 -07007904}
NeilBrownb15c2e52006-01-06 00:20:16 -08007905
NeilBrownfd01b882011-10-11 16:47:53 +11007906static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11007907{
NeilBrowne373ab12011-10-11 16:48:59 +11007908 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07007909 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11007910
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007911 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11007912 if (raid0_conf->nr_strip_zones > 1) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007913 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
7914 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007915 return ERR_PTR(-EINVAL);
7916 }
7917
NeilBrowne373ab12011-10-11 16:48:59 +11007918 sectors = raid0_conf->strip_zone[0].zone_end;
7919 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10007920 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007921 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11007922 mddev->new_layout = ALGORITHM_PARITY_N;
7923 mddev->new_chunk_sectors = mddev->chunk_sectors;
7924 mddev->raid_disks += 1;
7925 mddev->delta_disks = 1;
7926 /* make sure it will be not marked as dirty */
7927 mddev->recovery_cp = MaxSector;
7928
7929 return setup_conf(mddev);
7930}
7931
NeilBrownfd01b882011-10-11 16:47:53 +11007932static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11007933{
7934 int chunksect;
Shaohua Li6995f0b2016-12-08 15:48:17 -08007935 void *ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11007936
7937 if (mddev->raid_disks != 2 ||
7938 mddev->degraded > 1)
7939 return ERR_PTR(-EINVAL);
7940
7941 /* Should check if there are write-behind devices? */
7942
7943 chunksect = 64*2; /* 64K by default */
7944
7945 /* The array must be an exact multiple of chunksize */
7946 while (chunksect && (mddev->array_sectors & (chunksect-1)))
7947 chunksect >>= 1;
7948
7949 if ((chunksect<<9) < STRIPE_SIZE)
7950 /* array size does not allow a suitable chunk size */
7951 return ERR_PTR(-EINVAL);
7952
7953 mddev->new_level = 5;
7954 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10007955 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11007956
Shaohua Li6995f0b2016-12-08 15:48:17 -08007957 ret = setup_conf(mddev);
Jes Sorensen32cd7cb2017-01-06 19:31:35 -05007958 if (!IS_ERR(ret))
Shaohua Li394ed8e2017-01-04 16:10:19 -08007959 mddev_clear_unsupported_flags(mddev,
7960 UNSUPPORTED_MDDEV_FLAGS);
Shaohua Li6995f0b2016-12-08 15:48:17 -08007961 return ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11007962}
7963
NeilBrownfd01b882011-10-11 16:47:53 +11007964static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11007965{
7966 int new_layout;
7967
7968 switch (mddev->layout) {
7969 case ALGORITHM_LEFT_ASYMMETRIC_6:
7970 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
7971 break;
7972 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7973 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
7974 break;
7975 case ALGORITHM_LEFT_SYMMETRIC_6:
7976 new_layout = ALGORITHM_LEFT_SYMMETRIC;
7977 break;
7978 case ALGORITHM_RIGHT_SYMMETRIC_6:
7979 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
7980 break;
7981 case ALGORITHM_PARITY_0_6:
7982 new_layout = ALGORITHM_PARITY_0;
7983 break;
7984 case ALGORITHM_PARITY_N:
7985 new_layout = ALGORITHM_PARITY_N;
7986 break;
7987 default:
7988 return ERR_PTR(-EINVAL);
7989 }
7990 mddev->new_level = 5;
7991 mddev->new_layout = new_layout;
7992 mddev->delta_disks = -1;
7993 mddev->raid_disks -= 1;
7994 return setup_conf(mddev);
7995}
7996
NeilBrownfd01b882011-10-11 16:47:53 +11007997static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11007998{
NeilBrown88ce4932009-03-31 15:24:23 +11007999 /* For a 2-drive array, the layout and chunk size can be changed
8000 * immediately as not restriping is needed.
8001 * For larger arrays we record the new value - after validation
8002 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11008003 */
NeilBrownd1688a62011-10-11 16:49:52 +11008004 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10008005 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11008006
NeilBrown597a7112009-06-18 08:47:42 +10008007 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11008008 return -EINVAL;
8009 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008010 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11008011 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008012 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11008013 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008014 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11008015 /* not factor of array size */
8016 return -EINVAL;
8017 }
8018
8019 /* They look valid */
8020
NeilBrown88ce4932009-03-31 15:24:23 +11008021 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10008022 /* can make the change immediately */
8023 if (mddev->new_layout >= 0) {
8024 conf->algorithm = mddev->new_layout;
8025 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11008026 }
8027 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10008028 conf->chunk_sectors = new_chunk ;
8029 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11008030 }
Shaohua Li29530792016-12-08 15:48:19 -08008031 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown88ce4932009-03-31 15:24:23 +11008032 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11008033 }
NeilBrown50ac1682009-06-18 08:47:55 +10008034 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11008035}
8036
NeilBrownfd01b882011-10-11 16:47:53 +11008037static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11008038{
NeilBrown597a7112009-06-18 08:47:42 +10008039 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10008040
NeilBrown597a7112009-06-18 08:47:42 +10008041 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11008042 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008043 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008044 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11008045 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008046 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11008047 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008048 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11008049 /* not factor of array size */
8050 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008051 }
NeilBrown88ce4932009-03-31 15:24:23 +11008052
8053 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10008054 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11008055}
8056
NeilBrownfd01b882011-10-11 16:47:53 +11008057static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11008058{
8059 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008060 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008061 * raid1 - if there are two drives. We need to know the chunk size
8062 * raid4 - trivial - just use a raid4 layout.
8063 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008064 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008065 if (mddev->level == 0)
8066 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11008067 if (mddev->level == 1)
8068 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11008069 if (mddev->level == 4) {
8070 mddev->new_layout = ALGORITHM_PARITY_N;
8071 mddev->new_level = 5;
8072 return setup_conf(mddev);
8073 }
NeilBrownfc9739c2009-03-31 14:57:20 +11008074 if (mddev->level == 6)
8075 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11008076
8077 return ERR_PTR(-EINVAL);
8078}
8079
NeilBrownfd01b882011-10-11 16:47:53 +11008080static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11008081{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008082 /* raid4 can take over:
8083 * raid0 - if there is only one strip zone
8084 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11008085 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008086 if (mddev->level == 0)
8087 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11008088 if (mddev->level == 5 &&
8089 mddev->layout == ALGORITHM_PARITY_N) {
8090 mddev->new_layout = 0;
8091 mddev->new_level = 4;
8092 return setup_conf(mddev);
8093 }
8094 return ERR_PTR(-EINVAL);
8095}
NeilBrownd562b0c2009-03-31 14:39:39 +11008096
NeilBrown84fc4b52011-10-11 16:49:58 +11008097static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11008098
NeilBrownfd01b882011-10-11 16:47:53 +11008099static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11008100{
8101 /* Currently can only take over a raid5. We map the
8102 * personality to an equivalent raid6 personality
8103 * with the Q block at the end.
8104 */
8105 int new_layout;
8106
8107 if (mddev->pers != &raid5_personality)
8108 return ERR_PTR(-EINVAL);
8109 if (mddev->degraded > 1)
8110 return ERR_PTR(-EINVAL);
8111 if (mddev->raid_disks > 253)
8112 return ERR_PTR(-EINVAL);
8113 if (mddev->raid_disks < 3)
8114 return ERR_PTR(-EINVAL);
8115
8116 switch (mddev->layout) {
8117 case ALGORITHM_LEFT_ASYMMETRIC:
8118 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
8119 break;
8120 case ALGORITHM_RIGHT_ASYMMETRIC:
8121 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
8122 break;
8123 case ALGORITHM_LEFT_SYMMETRIC:
8124 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
8125 break;
8126 case ALGORITHM_RIGHT_SYMMETRIC:
8127 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
8128 break;
8129 case ALGORITHM_PARITY_0:
8130 new_layout = ALGORITHM_PARITY_0_6;
8131 break;
8132 case ALGORITHM_PARITY_N:
8133 new_layout = ALGORITHM_PARITY_N;
8134 break;
8135 default:
8136 return ERR_PTR(-EINVAL);
8137 }
8138 mddev->new_level = 6;
8139 mddev->new_layout = new_layout;
8140 mddev->delta_disks = 1;
8141 mddev->raid_disks += 1;
8142 return setup_conf(mddev);
8143}
8144
NeilBrown84fc4b52011-10-11 16:49:58 +11008145static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07008146{
8147 .name = "raid6",
8148 .level = 6,
8149 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008150 .make_request = raid5_make_request,
8151 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11008152 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008153 .status = raid5_status,
8154 .error_handler = raid5_error,
NeilBrown16a53ec2006-06-26 00:27:38 -07008155 .hot_add_disk = raid5_add_disk,
8156 .hot_remove_disk= raid5_remove_disk,
8157 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008158 .sync_request = raid5_sync_request,
NeilBrown16a53ec2006-06-26 00:27:38 -07008159 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008160 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10008161 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08008162 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008163 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07008164 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11008165 .takeover = raid6_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008166 .congested = raid5_congested,
NeilBrown16a53ec2006-06-26 00:27:38 -07008167};
NeilBrown84fc4b52011-10-11 16:49:58 +11008168static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008169{
8170 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08008171 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008172 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008173 .make_request = raid5_make_request,
8174 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11008175 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008176 .status = raid5_status,
8177 .error_handler = raid5_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008178 .hot_add_disk = raid5_add_disk,
8179 .hot_remove_disk= raid5_remove_disk,
8180 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008181 .sync_request = raid5_sync_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008182 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008183 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08008184 .check_reshape = raid5_check_reshape,
8185 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008186 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07008187 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11008188 .takeover = raid5_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008189 .congested = raid5_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008190};
8191
NeilBrown84fc4b52011-10-11 16:49:58 +11008192static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193{
NeilBrown2604b702006-01-06 00:20:36 -08008194 .name = "raid4",
8195 .level = 4,
8196 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008197 .make_request = raid5_make_request,
8198 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11008199 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008200 .status = raid5_status,
8201 .error_handler = raid5_error,
NeilBrown2604b702006-01-06 00:20:36 -08008202 .hot_add_disk = raid5_add_disk,
8203 .hot_remove_disk= raid5_remove_disk,
8204 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008205 .sync_request = raid5_sync_request,
NeilBrown2604b702006-01-06 00:20:36 -08008206 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008207 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08008208 .check_reshape = raid5_check_reshape,
8209 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008210 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08008211 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11008212 .takeover = raid4_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008213 .congested = raid5_congested,
NeilBrown2604b702006-01-06 00:20:36 -08008214};
8215
8216static int __init raid5_init(void)
8217{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008218 int ret;
8219
Shaohua Li851c30c2013-08-28 14:30:16 +08008220 raid5_wq = alloc_workqueue("raid5wq",
8221 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
8222 if (!raid5_wq)
8223 return -ENOMEM;
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008224
8225 ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
8226 "md/raid5:prepare",
8227 raid456_cpu_up_prepare,
8228 raid456_cpu_dead);
8229 if (ret) {
8230 destroy_workqueue(raid5_wq);
8231 return ret;
8232 }
NeilBrown16a53ec2006-06-26 00:27:38 -07008233 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008234 register_md_personality(&raid5_personality);
8235 register_md_personality(&raid4_personality);
8236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008237}
8238
NeilBrown2604b702006-01-06 00:20:36 -08008239static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008240{
NeilBrown16a53ec2006-06-26 00:27:38 -07008241 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008242 unregister_md_personality(&raid5_personality);
8243 unregister_md_personality(&raid4_personality);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008244 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
Shaohua Li851c30c2013-08-28 14:30:16 +08008245 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008246}
8247
8248module_init(raid5_init);
8249module_exit(raid5_exit);
8250MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008251MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008252MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08008253MODULE_ALIAS("md-raid5");
8254MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08008255MODULE_ALIAS("md-level-5");
8256MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07008257MODULE_ALIAS("md-personality-8"); /* RAID6 */
8258MODULE_ALIAS("md-raid6");
8259MODULE_ALIAS("md-level-6");
8260
8261/* This used to be two separate modules, they were: */
8262MODULE_ALIAS("raid5");
8263MODULE_ALIAS("raid6");