blob: 2ce23b01dbb21da6ae17664df085c37c0a63e157 [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{
Byungchul Parkeae82632017-02-14 16:26:24 +0900356 struct stripe_head *sh, *t;
Shaohua Li773ca822013-08-27 17:50:39 +0800357 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);
Byungchul Parkeae82632017-02-14 16:26:24 +0900362 llist_for_each_entry_safe(sh, t, head, release_list) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100363 int hash;
364
Shaohua Li773ca822013-08-27 17:50:39 +0800365 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
366 smp_mb();
367 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
368 /*
369 * Don't worry the bit is set here, because if the bit is set
370 * again, the count is always > 1. This is true for
371 * STRIPE_ON_UNPLUG_LIST bit too.
372 */
Shaohua Li566c09c2013-11-14 15:16:17 +1100373 hash = sh->hash_lock_index;
374 __release_stripe(conf, sh, &temp_inactive_list[hash]);
Shaohua Li773ca822013-08-27 17:50:39 +0800375 count++;
376 }
377
378 return count;
379}
380
Shaohua Li6d036f72015-08-13 14:31:57 -0700381void raid5_release_stripe(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
NeilBrownd1688a62011-10-11 16:49:52 +1100383 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 unsigned long flags;
Shaohua Li566c09c2013-11-14 15:16:17 +1100385 struct list_head list;
386 int hash;
Shaohua Li773ca822013-08-27 17:50:39 +0800387 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700388
Eivind Sartocf170f32014-05-28 13:39:23 +1000389 /* Avoid release_list until the last reference.
390 */
391 if (atomic_add_unless(&sh->count, -1, 1))
392 return;
393
majianpengad4068d2013-11-14 15:16:15 +1100394 if (unlikely(!conf->mddev->thread) ||
395 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
Shaohua Li773ca822013-08-27 17:50:39 +0800396 goto slow_path;
397 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
398 if (wakeup)
399 md_wakeup_thread(conf->mddev->thread);
400 return;
401slow_path:
Shaohua Li4eb788d2012-07-19 16:01:31 +1000402 local_irq_save(flags);
Shaohua Li773ca822013-08-27 17:50:39 +0800403 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Shaohua Li4eb788d2012-07-19 16:01:31 +1000404 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100405 INIT_LIST_HEAD(&list);
406 hash = sh->hash_lock_index;
407 do_release_stripe(conf, sh, &list);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000408 spin_unlock(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +1100409 release_inactive_stripe_list(conf, &list, hash);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000410 }
411 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
NeilBrownfccddba2006-01-06 00:20:33 -0800414static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
Dan Williams45b42332007-07-09 11:56:43 -0700416 pr_debug("remove_hash(), stripe %llu\n",
417 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
NeilBrownfccddba2006-01-06 00:20:33 -0800419 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
NeilBrownd1688a62011-10-11 16:49:52 +1100422static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
NeilBrownfccddba2006-01-06 00:20:33 -0800424 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Dan Williams45b42332007-07-09 11:56:43 -0700426 pr_debug("insert_hash(), stripe %llu\n",
427 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
NeilBrownfccddba2006-01-06 00:20:33 -0800429 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432/* find an idle stripe, make sure it is unhashed, and return it. */
Shaohua Li566c09c2013-11-14 15:16:17 +1100433static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 struct stripe_head *sh = NULL;
436 struct list_head *first;
437
Shaohua Li566c09c2013-11-14 15:16:17 +1100438 if (list_empty(conf->inactive_list + hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 goto out;
Shaohua Li566c09c2013-11-14 15:16:17 +1100440 first = (conf->inactive_list + hash)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 sh = list_entry(first, struct stripe_head, lru);
442 list_del_init(first);
443 remove_hash(sh);
444 atomic_inc(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100445 BUG_ON(hash != sh->hash_lock_index);
Shaohua Li4bda5562013-11-14 15:16:17 +1100446 if (list_empty(conf->inactive_list + hash))
447 atomic_inc(&conf->empty_inactive_list_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448out:
449 return sh;
450}
451
NeilBrowne4e11e32010-06-16 16:45:16 +1000452static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct page *p;
455 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000456 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
NeilBrowne4e11e32010-06-16 16:45:16 +1000458 for (i = 0; i < num ; i++) {
Shaohua Lid592a992014-05-21 17:57:44 +0800459 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 p = sh->dev[i].page;
461 if (!p)
462 continue;
463 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800464 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466}
467
NeilBrowna9683a72015-02-25 12:02:51 +1100468static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000471 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
NeilBrowne4e11e32010-06-16 16:45:16 +1000473 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct page *page;
475
NeilBrowna9683a72015-02-25 12:02:51 +1100476 if (!(page = alloc_page(gfp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return 1;
478 }
479 sh->dev[i].page = page;
Shaohua Lid592a992014-05-21 17:57:44 +0800480 sh->dev[i].orig_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482 return 0;
483}
484
NeilBrown784052e2009-03-31 15:19:07 +1100485static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100486static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100487 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
NeilBrownb5663ba2009-03-31 14:39:38 +1100489static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
NeilBrownd1688a62011-10-11 16:49:52 +1100491 struct r5conf *conf = sh->raid_conf;
Shaohua Li566c09c2013-11-14 15:16:17 +1100492 int i, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200494 BUG_ON(atomic_read(&sh->count) != 0);
495 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000496 BUG_ON(stripe_operations_active(sh));
shli@kernel.org59fc6302014-12-15 12:57:03 +1100497 BUG_ON(sh->batch_head);
Dan Williamsd84e0f12007-01-02 13:52:30 -0700498
Dan Williams45b42332007-07-09 11:56:43 -0700499 pr_debug("init_stripe called, stripe %llu\n",
Markus Stockhausenb8e6a152014-08-23 20:19:27 +1000500 (unsigned long long)sector);
Shaohua Li566c09c2013-11-14 15:16:17 +1100501retry:
502 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100503 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100504 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100506 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 sh->state = 0;
508
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800509 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct r5dev *dev = &sh->dev[i];
511
Dan Williamsd84e0f12007-01-02 13:52:30 -0700512 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 test_bit(R5_LOCKED, &dev->flags)) {
NeilBrowncc6167b2016-11-02 14:16:50 +1100514 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700516 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000518 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100521 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100523 if (read_seqcount_retry(&conf->gen_lock, seq))
524 goto retry;
shli@kernel.org7a87f432014-12-15 12:57:03 +1100525 sh->overwrite_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800527 sh->cpu = smp_processor_id();
shli@kernel.orgda41ba62014-12-15 12:57:03 +1100528 set_bit(STRIPE_BATCH_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
NeilBrownd1688a62011-10-11 16:49:52 +1100531static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100532 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct stripe_head *sh;
535
Dan Williams45b42332007-07-09 11:56:43 -0700536 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800537 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100538 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700540 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return NULL;
542}
543
NeilBrown674806d2010-06-16 17:17:53 +1000544/*
545 * Need to check if array has failed when deciding whether to:
546 * - start an array
547 * - remove non-faulty devices
548 * - add a spare
549 * - allow a reshape
550 * This determination is simple when no reshape is happening.
551 * However if there is a reshape, we need to carefully check
552 * both the before and after sections.
553 * This is because some failed devices may only affect one
554 * of the two sections, and some non-in_sync devices may
555 * be insync in the section most affected by failed devices.
556 */
Song Liu2e38a372017-01-24 10:45:30 -0800557int raid5_calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000558{
NeilBrown908f4fb2011-12-23 10:17:50 +1100559 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000560 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000561
562 rcu_read_lock();
563 degraded = 0;
564 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100565 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000566 if (rdev && test_bit(Faulty, &rdev->flags))
567 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000568 if (!rdev || test_bit(Faulty, &rdev->flags))
569 degraded++;
570 else if (test_bit(In_sync, &rdev->flags))
571 ;
572 else
573 /* not in-sync or faulty.
574 * If the reshape increases the number of devices,
575 * this is being recovered by the reshape, so
576 * this 'previous' section is not in_sync.
577 * If the number of devices is being reduced however,
578 * the device can only be part of the array if
579 * we are reverting a reshape, so this section will
580 * be in-sync.
581 */
582 if (conf->raid_disks >= conf->previous_raid_disks)
583 degraded++;
584 }
585 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100586 if (conf->raid_disks == conf->previous_raid_disks)
587 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000588 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100589 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000590 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100591 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000592 if (rdev && test_bit(Faulty, &rdev->flags))
593 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000594 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100595 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000596 else if (test_bit(In_sync, &rdev->flags))
597 ;
598 else
599 /* not in-sync or faulty.
600 * If reshape increases the number of devices, this
601 * section has already been recovered, else it
602 * almost certainly hasn't.
603 */
604 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100605 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000606 }
607 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100608 if (degraded2 > degraded)
609 return degraded2;
610 return degraded;
611}
612
613static int has_failed(struct r5conf *conf)
614{
615 int degraded;
616
617 if (conf->mddev->reshape_position == MaxSector)
618 return conf->mddev->degraded > conf->max_degraded;
619
Song Liu2e38a372017-01-24 10:45:30 -0800620 degraded = raid5_calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000621 if (degraded > conf->max_degraded)
622 return 1;
623 return 0;
624}
625
Shaohua Li6d036f72015-08-13 14:31:57 -0700626struct stripe_head *
627raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
628 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 struct stripe_head *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +1100631 int hash = stripe_hash_locks_hash(sector);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800632 int inc_empty_inactive_list_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Dan Williams45b42332007-07-09 11:56:43 -0700634 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Shaohua Li566c09c2013-11-14 15:16:17 +1100636 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 do {
Yuanhan Liub1b46482015-05-08 18:19:06 +1000639 wait_event_lock_irq(conf->wait_for_quiescent,
NeilBrowna8c906c2009-06-09 14:39:59 +1000640 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100641 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100642 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!sh) {
NeilBrownedbe83a2015-02-26 12:47:56 +1100644 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100645 sh = get_free_stripe(conf, hash);
Shaohua Li713bc5c2015-05-28 17:33:47 -0700646 if (!sh && !test_bit(R5_DID_ALLOC,
647 &conf->cache_state))
NeilBrownedbe83a2015-02-26 12:47:56 +1100648 set_bit(R5_ALLOC_MORE,
649 &conf->cache_state);
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (noblock && sh == NULL)
652 break;
Song Liua39f7af2016-11-17 15:24:40 -0800653
654 r5c_check_stripe_cache_usage(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (!sh) {
NeilBrown54233992015-02-26 12:21:04 +1100656 set_bit(R5_INACTIVE_BLOCKED,
657 &conf->cache_state);
Song Liua39f7af2016-11-17 15:24:40 -0800658 r5l_wake_reclaim(conf->log, 0);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800659 wait_event_lock_irq(
660 conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +1100661 !list_empty(conf->inactive_list + hash) &&
662 (atomic_read(&conf->active_stripes)
663 < (conf->max_nr_stripes * 3 / 4)
NeilBrown54233992015-02-26 12:21:04 +1100664 || !test_bit(R5_INACTIVE_BLOCKED,
665 &conf->cache_state)),
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800666 *(conf->hash_locks + hash));
NeilBrown54233992015-02-26 12:21:04 +1100667 clear_bit(R5_INACTIVE_BLOCKED,
668 &conf->cache_state);
NeilBrown7da9d452014-01-22 11:45:03 +1100669 } else {
NeilBrownb5663ba2009-03-31 14:39:38 +1100670 init_stripe(sh, sector, previous);
NeilBrown7da9d452014-01-22 11:45:03 +1100671 atomic_inc(&sh->count);
672 }
Shaohua Lie240c182014-04-09 11:27:42 +0800673 } else if (!atomic_inc_not_zero(&sh->count)) {
NeilBrown6d183de2013-11-28 10:55:27 +1100674 spin_lock(&conf->device_lock);
Shaohua Lie240c182014-04-09 11:27:42 +0800675 if (!atomic_read(&sh->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (!test_bit(STRIPE_HANDLE, &sh->state))
677 atomic_inc(&conf->active_stripes);
NeilBrown5af9bef2014-01-14 15:16:10 +1100678 BUG_ON(list_empty(&sh->lru) &&
679 !test_bit(STRIPE_EXPANDING, &sh->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800680 inc_empty_inactive_list_flag = 0;
681 if (!list_empty(conf->inactive_list + hash))
682 inc_empty_inactive_list_flag = 1;
NeilBrown16a53ec2006-06-26 00:27:38 -0700683 list_del_init(&sh->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800684 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
685 atomic_inc(&conf->empty_inactive_list_nr);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800686 if (sh->group) {
687 sh->group->stripes_cnt--;
688 sh->group = NULL;
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
NeilBrown7da9d452014-01-22 11:45:03 +1100691 atomic_inc(&sh->count);
NeilBrown6d183de2013-11-28 10:55:27 +1100692 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 } while (sh == NULL);
695
Shaohua Li566c09c2013-11-14 15:16:17 +1100696 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return sh;
698}
699
shli@kernel.org7a87f432014-12-15 12:57:03 +1100700static bool is_full_stripe_write(struct stripe_head *sh)
701{
702 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
703 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
704}
705
shli@kernel.org59fc6302014-12-15 12:57:03 +1100706static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
707{
708 local_irq_disable();
709 if (sh1 > sh2) {
710 spin_lock(&sh2->stripe_lock);
711 spin_lock_nested(&sh1->stripe_lock, 1);
712 } else {
713 spin_lock(&sh1->stripe_lock);
714 spin_lock_nested(&sh2->stripe_lock, 1);
715 }
716}
717
718static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
719{
720 spin_unlock(&sh1->stripe_lock);
721 spin_unlock(&sh2->stripe_lock);
722 local_irq_enable();
723}
724
725/* Only freshly new full stripe normal write stripe can be added to a batch list */
726static bool stripe_can_batch(struct stripe_head *sh)
727{
Shaohua Li9c3e3332015-08-13 14:32:02 -0700728 struct r5conf *conf = sh->raid_conf;
729
730 if (conf->log)
731 return false;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100732 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
NeilBrownd0852df52015-05-27 08:43:45 +1000733 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
shli@kernel.org59fc6302014-12-15 12:57:03 +1100734 is_full_stripe_write(sh);
735}
736
737/* we only do back search */
738static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
739{
740 struct stripe_head *head;
741 sector_t head_sector, tmp_sec;
742 int hash;
743 int dd_idx;
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800744 int inc_empty_inactive_list_flag;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100745
shli@kernel.org59fc6302014-12-15 12:57:03 +1100746 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
747 tmp_sec = sh->sector;
748 if (!sector_div(tmp_sec, conf->chunk_sectors))
749 return;
750 head_sector = sh->sector - STRIPE_SECTORS;
751
752 hash = stripe_hash_locks_hash(head_sector);
753 spin_lock_irq(conf->hash_locks + hash);
754 head = __find_stripe(conf, head_sector, conf->generation);
755 if (head && !atomic_inc_not_zero(&head->count)) {
756 spin_lock(&conf->device_lock);
757 if (!atomic_read(&head->count)) {
758 if (!test_bit(STRIPE_HANDLE, &head->state))
759 atomic_inc(&conf->active_stripes);
760 BUG_ON(list_empty(&head->lru) &&
761 !test_bit(STRIPE_EXPANDING, &head->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800762 inc_empty_inactive_list_flag = 0;
763 if (!list_empty(conf->inactive_list + hash))
764 inc_empty_inactive_list_flag = 1;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100765 list_del_init(&head->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800766 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
767 atomic_inc(&conf->empty_inactive_list_nr);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100768 if (head->group) {
769 head->group->stripes_cnt--;
770 head->group = NULL;
771 }
772 }
773 atomic_inc(&head->count);
774 spin_unlock(&conf->device_lock);
775 }
776 spin_unlock_irq(conf->hash_locks + hash);
777
778 if (!head)
779 return;
780 if (!stripe_can_batch(head))
781 goto out;
782
783 lock_two_stripes(head, sh);
784 /* clear_batch_ready clear the flag */
785 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
786 goto unlock_out;
787
788 if (sh->batch_head)
789 goto unlock_out;
790
791 dd_idx = 0;
792 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
793 dd_idx++;
Jens Axboe1eff9d32016-08-05 15:35:16 -0600794 if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
Mike Christie796a5cf2016-06-05 14:32:07 -0500795 bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
shli@kernel.org59fc6302014-12-15 12:57:03 +1100796 goto unlock_out;
797
798 if (head->batch_head) {
799 spin_lock(&head->batch_head->batch_lock);
800 /* This batch list is already running */
801 if (!stripe_can_batch(head)) {
802 spin_unlock(&head->batch_head->batch_lock);
803 goto unlock_out;
804 }
805
806 /*
807 * at this point, head's BATCH_READY could be cleared, but we
808 * can still add the stripe to batch list
809 */
810 list_add(&sh->batch_list, &head->batch_list);
811 spin_unlock(&head->batch_head->batch_lock);
812
813 sh->batch_head = head->batch_head;
814 } else {
815 head->batch_head = head;
816 sh->batch_head = head->batch_head;
817 spin_lock(&head->batch_lock);
818 list_add_tail(&sh->batch_list, &head->batch_list);
819 spin_unlock(&head->batch_lock);
820 }
821
822 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
823 if (atomic_dec_return(&conf->preread_active_stripes)
824 < IO_THRESHOLD)
825 md_wakeup_thread(conf->mddev->thread);
826
NeilBrown2b6b2452015-05-21 15:10:01 +1000827 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
828 int seq = sh->bm_seq;
829 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
830 sh->batch_head->bm_seq > seq)
831 seq = sh->batch_head->bm_seq;
832 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
833 sh->batch_head->bm_seq = seq;
834 }
835
shli@kernel.org59fc6302014-12-15 12:57:03 +1100836 atomic_inc(&sh->count);
837unlock_out:
838 unlock_two_stripes(head, sh);
839out:
Shaohua Li6d036f72015-08-13 14:31:57 -0700840 raid5_release_stripe(head);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100841}
842
NeilBrown05616be2012-05-21 09:27:00 +1000843/* Determine if 'data_offset' or 'new_data_offset' should be used
844 * in this stripe_head.
845 */
846static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
847{
848 sector_t progress = conf->reshape_progress;
849 /* Need a memory barrier to make sure we see the value
850 * of conf->generation, or ->data_offset that was set before
851 * reshape_progress was updated.
852 */
853 smp_rmb();
854 if (progress == MaxSector)
855 return 0;
856 if (sh->generation == conf->generation - 1)
857 return 0;
858 /* We are in a reshape, and this is a new-generation stripe,
859 * so use new_data_offset.
860 */
861 return 1;
862}
863
Shaohua Li765d7042017-01-04 09:33:23 -0800864static void flush_deferred_bios(struct r5conf *conf)
865{
866 struct bio_list tmp;
867 struct bio *bio;
868
869 if (!conf->batch_bio_dispatch || !conf->group_cnt)
870 return;
871
872 bio_list_init(&tmp);
873 spin_lock(&conf->pending_bios_lock);
874 bio_list_merge(&tmp, &conf->pending_bios);
875 bio_list_init(&conf->pending_bios);
876 spin_unlock(&conf->pending_bios_lock);
877
878 while ((bio = bio_list_pop(&tmp)))
879 generic_make_request(bio);
880}
881
882static void defer_bio_issue(struct r5conf *conf, struct bio *bio)
883{
884 /*
885 * change group_cnt will drain all bios, so this is safe
886 *
887 * A read generally means a read-modify-write, which usually means a
888 * randwrite, so we don't delay it
889 */
890 if (!conf->batch_bio_dispatch || !conf->group_cnt ||
891 bio_op(bio) == REQ_OP_READ) {
892 generic_make_request(bio);
893 return;
894 }
895 spin_lock(&conf->pending_bios_lock);
896 bio_list_add(&conf->pending_bios, bio);
897 spin_unlock(&conf->pending_bios_lock);
898 md_wakeup_thread(conf->mddev->thread);
899}
900
NeilBrown6712ecf2007-09-27 12:47:43 +0200901static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200902raid5_end_read_request(struct bio *bi);
NeilBrown6712ecf2007-09-27 12:47:43 +0200903static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200904raid5_end_write_request(struct bio *bi);
Dan Williams91c00922007-01-02 13:52:30 -0700905
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000906static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700907{
NeilBrownd1688a62011-10-11 16:49:52 +1100908 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700909 int i, disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100910 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -0700911
912 might_sleep();
913
Song Liu1e6d6902016-11-17 15:24:39 -0800914 if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
915 /* writing out phase */
Song Liud7bd3982016-11-23 22:50:39 -0800916 if (s->waiting_extra_page)
917 return;
Song Liu1e6d6902016-11-17 15:24:39 -0800918 if (r5l_write_stripe(conf->log, sh) == 0)
919 return;
920 } else { /* caching phase */
921 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) {
922 r5c_cache_data(conf->log, sh, s);
923 return;
924 }
925 }
926
Dan Williams91c00922007-01-02 13:52:30 -0700927 for (i = disks; i--; ) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500928 int op, op_flags = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +1100929 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100930 struct bio *bi, *rbi;
931 struct md_rdev *rdev, *rrdev = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100932
933 sh = head_sh;
Tejun Heoe9c74692010-09-03 11:56:18 +0200934 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500935 op = REQ_OP_WRITE;
Tejun Heoe9c74692010-09-03 11:56:18 +0200936 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600937 op_flags = REQ_FUA;
Shaohua Li9e4447682012-10-11 13:49:49 +1100938 if (test_bit(R5_Discard, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -0500939 op = REQ_OP_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200940 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -0500941 op = REQ_OP_READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100942 else if (test_and_clear_bit(R5_WantReplace,
943 &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500944 op = REQ_OP_WRITE;
NeilBrown9a3e1102011-12-23 10:17:53 +1100945 replace_only = 1;
946 } else
Dan Williams91c00922007-01-02 13:52:30 -0700947 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000948 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -0500949 op_flags |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700950
shli@kernel.org59fc6302014-12-15 12:57:03 +1100951again:
Dan Williams91c00922007-01-02 13:52:30 -0700952 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100953 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700954
Dan Williams91c00922007-01-02 13:52:30 -0700955 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100956 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100957 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
958 rdev = rcu_dereference(conf->disks[i].rdev);
959 if (!rdev) {
960 rdev = rrdev;
961 rrdev = NULL;
962 }
Mike Christie796a5cf2016-06-05 14:32:07 -0500963 if (op_is_write(op)) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100964 if (replace_only)
965 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100966 if (rdev == rrdev)
967 /* We raced and saw duplicates */
968 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100969 } else {
shli@kernel.org59fc6302014-12-15 12:57:03 +1100970 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100971 rdev = rrdev;
972 rrdev = NULL;
973 }
NeilBrown977df362011-12-23 10:17:53 +1100974
Dan Williams91c00922007-01-02 13:52:30 -0700975 if (rdev && test_bit(Faulty, &rdev->flags))
976 rdev = NULL;
977 if (rdev)
978 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100979 if (rrdev && test_bit(Faulty, &rrdev->flags))
980 rrdev = NULL;
981 if (rrdev)
982 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700983 rcu_read_unlock();
984
NeilBrown73e92e52011-07-28 11:39:22 +1000985 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100986 * need to check for writes. We never accept write errors
987 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000988 */
Mike Christie796a5cf2016-06-05 14:32:07 -0500989 while (op_is_write(op) && rdev &&
NeilBrown73e92e52011-07-28 11:39:22 +1000990 test_bit(WriteErrorSeen, &rdev->flags)) {
991 sector_t first_bad;
992 int bad_sectors;
993 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
994 &first_bad, &bad_sectors);
995 if (!bad)
996 break;
997
998 if (bad < 0) {
999 set_bit(BlockedBadBlocks, &rdev->flags);
1000 if (!conf->mddev->external &&
Shaohua Li29530792016-12-08 15:48:19 -08001001 conf->mddev->sb_flags) {
NeilBrown73e92e52011-07-28 11:39:22 +10001002 /* It is very unlikely, but we might
1003 * still need to write out the
1004 * bad block log - better give it
1005 * a chance*/
1006 md_check_recovery(conf->mddev);
1007 }
majianpeng18507532012-07-03 12:11:54 +10001008 /*
1009 * Because md_wait_for_blocked_rdev
1010 * will dec nr_pending, we must
1011 * increment it first.
1012 */
1013 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +10001014 md_wait_for_blocked_rdev(rdev, conf->mddev);
1015 } else {
1016 /* Acknowledged bad block - skip the write */
1017 rdev_dec_pending(rdev, conf->mddev);
1018 rdev = NULL;
1019 }
1020 }
1021
Dan Williams91c00922007-01-02 13:52:30 -07001022 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001023 if (s->syncing || s->expanding || s->expanded
1024 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -07001025 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1026
Dan Williams2b7497f2008-06-28 08:31:52 +10001027 set_bit(STRIPE_IO_STARTED, &sh->state);
1028
Dan Williams91c00922007-01-02 13:52:30 -07001029 bi->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05001030 bio_set_op_attrs(bi, op, op_flags);
1031 bi->bi_end_io = op_is_write(op)
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001032 ? raid5_end_write_request
1033 : raid5_end_read_request;
1034 bi->bi_private = sh;
1035
Mike Christie6296b962016-06-05 14:32:21 -05001036 pr_debug("%s: for %llu schedule op %d on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001037 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001038 bi->bi_opf, i);
Dan Williams91c00922007-01-02 13:52:30 -07001039 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001040 if (sh != head_sh)
1041 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001042 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001043 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001044 + rdev->new_data_offset);
1045 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001046 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001047 + rdev->data_offset);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001048 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
Jens Axboe1eff9d32016-08-05 15:35:16 -06001049 bi->bi_opf |= REQ_NOMERGE;
majianpeng3f9e7c12012-07-31 10:04:21 +10001050
Shaohua Lid592a992014-05-21 17:57:44 +08001051 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1052 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
Song Liu86aa1392017-01-12 17:22:41 -08001053
1054 if (!op_is_write(op) &&
1055 test_bit(R5_InJournal, &sh->dev[i].flags))
1056 /*
1057 * issuing read for a page in journal, this
1058 * must be preparing for prexor in rmw; read
1059 * the data into orig_page
1060 */
1061 sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
1062 else
1063 sh->dev[i].vec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001064 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001065 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1066 bi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001067 bi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001068 /*
1069 * If this is discard request, set bi_vcnt 0. We don't
1070 * want to confuse SCSI because SCSI will replace payload
1071 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001072 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001073 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +11001074 if (rrdev)
1075 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001076
1077 if (conf->mddev->gendisk)
1078 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
1079 bi, disk_devt(conf->mddev->gendisk),
1080 sh->dev[i].sector);
Shaohua Li765d7042017-01-04 09:33:23 -08001081 defer_bio_issue(conf, bi);
NeilBrown977df362011-12-23 10:17:53 +11001082 }
1083 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001084 if (s->syncing || s->expanding || s->expanded
1085 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +11001086 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
1087
1088 set_bit(STRIPE_IO_STARTED, &sh->state);
1089
1090 rbi->bi_bdev = rrdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05001091 bio_set_op_attrs(rbi, op, op_flags);
1092 BUG_ON(!op_is_write(op));
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001093 rbi->bi_end_io = raid5_end_write_request;
1094 rbi->bi_private = sh;
1095
Mike Christie6296b962016-06-05 14:32:21 -05001096 pr_debug("%s: for %llu schedule op %d on "
NeilBrown977df362011-12-23 10:17:53 +11001097 "replacement disc %d\n",
1098 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001099 rbi->bi_opf, i);
NeilBrown977df362011-12-23 10:17:53 +11001100 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001101 if (sh != head_sh)
1102 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001103 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001104 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001105 + rrdev->new_data_offset);
1106 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001107 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001108 + rrdev->data_offset);
Shaohua Lid592a992014-05-21 17:57:44 +08001109 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1110 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1111 sh->dev[i].rvec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001112 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +11001113 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1114 rbi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001115 rbi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001116 /*
1117 * If this is discard request, set bi_vcnt 0. We don't
1118 * want to confuse SCSI because SCSI will replace payload
1119 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001120 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001121 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001122 if (conf->mddev->gendisk)
1123 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
1124 rbi, disk_devt(conf->mddev->gendisk),
1125 sh->dev[i].sector);
Shaohua Li765d7042017-01-04 09:33:23 -08001126 defer_bio_issue(conf, rbi);
NeilBrown977df362011-12-23 10:17:53 +11001127 }
1128 if (!rdev && !rrdev) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001129 if (op_is_write(op))
Dan Williams91c00922007-01-02 13:52:30 -07001130 set_bit(STRIPE_DEGRADED, &sh->state);
Mike Christie6296b962016-06-05 14:32:21 -05001131 pr_debug("skip op %d on disc %d for sector %llu\n",
Jens Axboe1eff9d32016-08-05 15:35:16 -06001132 bi->bi_opf, i, (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001133 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1134 set_bit(STRIPE_HANDLE, &sh->state);
1135 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001136
1137 if (!head_sh->batch_head)
1138 continue;
1139 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1140 batch_list);
1141 if (sh != head_sh)
1142 goto again;
Dan Williams91c00922007-01-02 13:52:30 -07001143 }
1144}
1145
1146static struct dma_async_tx_descriptor *
Shaohua Lid592a992014-05-21 17:57:44 +08001147async_copy_data(int frombio, struct bio *bio, struct page **page,
1148 sector_t sector, struct dma_async_tx_descriptor *tx,
Song Liu1e6d6902016-11-17 15:24:39 -08001149 struct stripe_head *sh, int no_skipcopy)
Dan Williams91c00922007-01-02 13:52:30 -07001150{
Kent Overstreet79886132013-11-23 17:19:00 -08001151 struct bio_vec bvl;
1152 struct bvec_iter iter;
Dan Williams91c00922007-01-02 13:52:30 -07001153 struct page *bio_page;
Dan Williams91c00922007-01-02 13:52:30 -07001154 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -07001155 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -07001156 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001157
Kent Overstreet4f024f32013-10-11 15:44:27 -07001158 if (bio->bi_iter.bi_sector >= sector)
1159 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
Dan Williams91c00922007-01-02 13:52:30 -07001160 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001161 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -07001162
Dan Williams0403e382009-09-08 17:42:50 -07001163 if (frombio)
1164 flags |= ASYNC_TX_FENCE;
1165 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1166
Kent Overstreet79886132013-11-23 17:19:00 -08001167 bio_for_each_segment(bvl, bio, iter) {
1168 int len = bvl.bv_len;
Dan Williams91c00922007-01-02 13:52:30 -07001169 int clen;
1170 int b_offset = 0;
1171
1172 if (page_offset < 0) {
1173 b_offset = -page_offset;
1174 page_offset += b_offset;
1175 len -= b_offset;
1176 }
1177
1178 if (len > 0 && page_offset + len > STRIPE_SIZE)
1179 clen = STRIPE_SIZE - page_offset;
1180 else
1181 clen = len;
1182
1183 if (clen > 0) {
Kent Overstreet79886132013-11-23 17:19:00 -08001184 b_offset += bvl.bv_offset;
1185 bio_page = bvl.bv_page;
Shaohua Lid592a992014-05-21 17:57:44 +08001186 if (frombio) {
1187 if (sh->raid_conf->skip_copy &&
1188 b_offset == 0 && page_offset == 0 &&
Song Liu1e6d6902016-11-17 15:24:39 -08001189 clen == STRIPE_SIZE &&
1190 !no_skipcopy)
Shaohua Lid592a992014-05-21 17:57:44 +08001191 *page = bio_page;
1192 else
1193 tx = async_memcpy(*page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001194 b_offset, clen, &submit);
Shaohua Lid592a992014-05-21 17:57:44 +08001195 } else
1196 tx = async_memcpy(bio_page, *page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001197 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001198 }
Dan Williamsa08abd82009-06-03 11:43:59 -07001199 /* chain the operations */
1200 submit.depend_tx = tx;
1201
Dan Williams91c00922007-01-02 13:52:30 -07001202 if (clen < len) /* hit end of page */
1203 break;
1204 page_offset += len;
1205 }
1206
1207 return tx;
1208}
1209
1210static void ops_complete_biofill(void *stripe_head_ref)
1211{
1212 struct stripe_head *sh = stripe_head_ref;
NeilBrown34a6f802015-08-14 12:07:57 +10001213 struct bio_list return_bi = BIO_EMPTY_LIST;
Dan Williamse4d84902007-09-24 10:06:13 -07001214 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001215
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001216 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001217 (unsigned long long)sh->sector);
1218
1219 /* clear completed biofills */
1220 for (i = sh->disks; i--; ) {
1221 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001222
1223 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001224 /* and check if we need to reply to a read request,
1225 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001226 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001227 */
1228 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001229 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001230
Dan Williams91c00922007-01-02 13:52:30 -07001231 BUG_ON(!dev->read);
1232 rbi = dev->read;
1233 dev->read = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001234 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001235 dev->sector + STRIPE_SECTORS) {
1236 rbi2 = r5_next_bio(rbi, dev->sector);
NeilBrown34a6f802015-08-14 12:07:57 +10001237 if (!raid5_dec_bi_active_stripes(rbi))
1238 bio_list_add(&return_bi, rbi);
Dan Williams91c00922007-01-02 13:52:30 -07001239 rbi = rbi2;
1240 }
1241 }
1242 }
Dan Williams83de75c2008-06-28 08:31:58 +10001243 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001244
NeilBrown34a6f802015-08-14 12:07:57 +10001245 return_io(&return_bi);
Dan Williams91c00922007-01-02 13:52:30 -07001246
Dan Williamse4d84902007-09-24 10:06:13 -07001247 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001248 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001249}
1250
1251static void ops_run_biofill(struct stripe_head *sh)
1252{
1253 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001254 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001255 int i;
1256
shli@kernel.org59fc6302014-12-15 12:57:03 +11001257 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001258 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001259 (unsigned long long)sh->sector);
1260
1261 for (i = sh->disks; i--; ) {
1262 struct r5dev *dev = &sh->dev[i];
1263 if (test_bit(R5_Wantfill, &dev->flags)) {
1264 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001265 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001266 dev->read = rbi = dev->toread;
1267 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001268 spin_unlock_irq(&sh->stripe_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001269 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001270 dev->sector + STRIPE_SECTORS) {
Shaohua Lid592a992014-05-21 17:57:44 +08001271 tx = async_copy_data(0, rbi, &dev->page,
Song Liu1e6d6902016-11-17 15:24:39 -08001272 dev->sector, tx, sh, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001273 rbi = r5_next_bio(rbi, dev->sector);
1274 }
1275 }
1276 }
1277
1278 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001279 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1280 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001281}
1282
Dan Williams4e7d2c02009-08-29 19:13:11 -07001283static void mark_target_uptodate(struct stripe_head *sh, int target)
1284{
1285 struct r5dev *tgt;
1286
1287 if (target < 0)
1288 return;
1289
1290 tgt = &sh->dev[target];
1291 set_bit(R5_UPTODATE, &tgt->flags);
1292 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1293 clear_bit(R5_Wantcompute, &tgt->flags);
1294}
1295
Dan Williamsac6b53b2009-07-14 13:40:19 -07001296static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001297{
1298 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001299
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001300 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001301 (unsigned long long)sh->sector);
1302
Dan Williamsac6b53b2009-07-14 13:40:19 -07001303 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001304 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001305 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001306
Dan Williamsecc65c92008-06-28 08:31:57 +10001307 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1308 if (sh->check_state == check_state_compute_run)
1309 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001310 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001311 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001312}
1313
Dan Williamsd6f38f32009-07-14 11:50:52 -07001314/* return a pointer to the address conversion region of the scribble buffer */
1315static addr_conv_t *to_addr_conv(struct stripe_head *sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001316 struct raid5_percpu *percpu, int i)
Dan Williams91c00922007-01-02 13:52:30 -07001317{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001318 void *addr;
1319
1320 addr = flex_array_get(percpu->scribble, i);
1321 return addr + sizeof(struct page *) * (sh->disks + 2);
1322}
1323
1324/* return a pointer to the address conversion region of the scribble buffer */
1325static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
1326{
1327 void *addr;
1328
1329 addr = flex_array_get(percpu->scribble, i);
1330 return addr;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001331}
1332
1333static struct dma_async_tx_descriptor *
1334ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1335{
Dan Williams91c00922007-01-02 13:52:30 -07001336 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001337 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001338 int target = sh->ops.target;
1339 struct r5dev *tgt = &sh->dev[target];
1340 struct page *xor_dest = tgt->page;
1341 int count = 0;
1342 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001343 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001344 int i;
1345
shli@kernel.org59fc6302014-12-15 12:57:03 +11001346 BUG_ON(sh->batch_head);
1347
Dan Williams91c00922007-01-02 13:52:30 -07001348 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001349 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001350 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1351
1352 for (i = disks; i--; )
1353 if (i != target)
1354 xor_srcs[count++] = sh->dev[i].page;
1355
1356 atomic_inc(&sh->count);
1357
Dan Williams0403e382009-09-08 17:42:50 -07001358 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001359 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
Dan Williams91c00922007-01-02 13:52:30 -07001360 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -07001361 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001362 else
Dan Williamsa08abd82009-06-03 11:43:59 -07001363 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001364
Dan Williams91c00922007-01-02 13:52:30 -07001365 return tx;
1366}
1367
Dan Williamsac6b53b2009-07-14 13:40:19 -07001368/* set_syndrome_sources - populate source buffers for gen_syndrome
1369 * @srcs - (struct page *) array of size sh->disks
1370 * @sh - stripe_head to parse
1371 *
1372 * Populates srcs in proper layout order for the stripe and returns the
1373 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1374 * destination buffer is recorded in srcs[count] and the Q destination
1375 * is recorded in srcs[count+1]].
1376 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001377static int set_syndrome_sources(struct page **srcs,
1378 struct stripe_head *sh,
1379 int srctype)
Dan Williamsac6b53b2009-07-14 13:40:19 -07001380{
1381 int disks = sh->disks;
1382 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1383 int d0_idx = raid6_d0(sh);
1384 int count;
1385 int i;
1386
1387 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001388 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001389
1390 count = 0;
1391 i = d0_idx;
1392 do {
1393 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001394 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001395
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001396 if (i == sh->qd_idx || i == sh->pd_idx ||
1397 (srctype == SYNDROME_SRC_ALL) ||
1398 (srctype == SYNDROME_SRC_WANT_DRAIN &&
Song Liu1e6d6902016-11-17 15:24:39 -08001399 (test_bit(R5_Wantdrain, &dev->flags) ||
1400 test_bit(R5_InJournal, &dev->flags))) ||
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001401 (srctype == SYNDROME_SRC_WRITTEN &&
Song Liu1e6d6902016-11-17 15:24:39 -08001402 dev->written)) {
1403 if (test_bit(R5_InJournal, &dev->flags))
1404 srcs[slot] = sh->dev[i].orig_page;
1405 else
1406 srcs[slot] = sh->dev[i].page;
1407 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001408 i = raid6_next_disk(i, disks);
1409 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001410
NeilBrowne4424fe2009-10-16 16:27:34 +11001411 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001412}
1413
1414static struct dma_async_tx_descriptor *
1415ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1416{
1417 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001418 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001419 int target;
1420 int qd_idx = sh->qd_idx;
1421 struct dma_async_tx_descriptor *tx;
1422 struct async_submit_ctl submit;
1423 struct r5dev *tgt;
1424 struct page *dest;
1425 int i;
1426 int count;
1427
shli@kernel.org59fc6302014-12-15 12:57:03 +11001428 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001429 if (sh->ops.target < 0)
1430 target = sh->ops.target2;
1431 else if (sh->ops.target2 < 0)
1432 target = sh->ops.target;
1433 else
1434 /* we should only have one valid target */
1435 BUG();
1436 BUG_ON(target < 0);
1437 pr_debug("%s: stripe %llu block: %d\n",
1438 __func__, (unsigned long long)sh->sector, target);
1439
1440 tgt = &sh->dev[target];
1441 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1442 dest = tgt->page;
1443
1444 atomic_inc(&sh->count);
1445
1446 if (target == qd_idx) {
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001447 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001448 blocks[count] = NULL; /* regenerating p is not necessary */
1449 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001450 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1451 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001452 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001453 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1454 } else {
1455 /* Compute any data- or p-drive using XOR */
1456 count = 0;
1457 for (i = disks; i-- ; ) {
1458 if (i == target || i == qd_idx)
1459 continue;
1460 blocks[count++] = sh->dev[i].page;
1461 }
1462
Dan Williams0403e382009-09-08 17:42:50 -07001463 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1464 NULL, ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001465 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001466 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1467 }
1468
1469 return tx;
1470}
1471
1472static struct dma_async_tx_descriptor *
1473ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1474{
1475 int i, count, disks = sh->disks;
1476 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1477 int d0_idx = raid6_d0(sh);
1478 int faila = -1, failb = -1;
1479 int target = sh->ops.target;
1480 int target2 = sh->ops.target2;
1481 struct r5dev *tgt = &sh->dev[target];
1482 struct r5dev *tgt2 = &sh->dev[target2];
1483 struct dma_async_tx_descriptor *tx;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001484 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001485 struct async_submit_ctl submit;
1486
shli@kernel.org59fc6302014-12-15 12:57:03 +11001487 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001488 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1489 __func__, (unsigned long long)sh->sector, target, target2);
1490 BUG_ON(target < 0 || target2 < 0);
1491 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1492 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1493
Dan Williams6c910a72009-09-16 12:24:54 -07001494 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001495 * slot number conversion for 'faila' and 'failb'
1496 */
1497 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001498 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001499 count = 0;
1500 i = d0_idx;
1501 do {
1502 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1503
1504 blocks[slot] = sh->dev[i].page;
1505
1506 if (i == target)
1507 faila = slot;
1508 if (i == target2)
1509 failb = slot;
1510 i = raid6_next_disk(i, disks);
1511 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001512
1513 BUG_ON(faila == failb);
1514 if (failb < faila)
1515 swap(faila, failb);
1516 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1517 __func__, (unsigned long long)sh->sector, faila, failb);
1518
1519 atomic_inc(&sh->count);
1520
1521 if (failb == syndrome_disks+1) {
1522 /* Q disk is one of the missing disks */
1523 if (faila == syndrome_disks) {
1524 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001525 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1526 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001527 to_addr_conv(sh, percpu, 0));
NeilBrowne4424fe2009-10-16 16:27:34 +11001528 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001529 STRIPE_SIZE, &submit);
1530 } else {
1531 struct page *dest;
1532 int data_target;
1533 int qd_idx = sh->qd_idx;
1534
1535 /* Missing D+Q: recompute D from P, then recompute Q */
1536 if (target == qd_idx)
1537 data_target = target2;
1538 else
1539 data_target = target;
1540
1541 count = 0;
1542 for (i = disks; i-- ; ) {
1543 if (i == data_target || i == qd_idx)
1544 continue;
1545 blocks[count++] = sh->dev[i].page;
1546 }
1547 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001548 init_async_submit(&submit,
1549 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1550 NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001551 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001552 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1553 &submit);
1554
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001555 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williams0403e382009-09-08 17:42:50 -07001556 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1557 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001558 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001559 return async_gen_syndrome(blocks, 0, count+2,
1560 STRIPE_SIZE, &submit);
1561 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001562 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001563 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1564 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001565 to_addr_conv(sh, percpu, 0));
Dan Williams6c910a72009-09-16 12:24:54 -07001566 if (failb == syndrome_disks) {
1567 /* We're missing D+P. */
1568 return async_raid6_datap_recov(syndrome_disks+2,
1569 STRIPE_SIZE, faila,
1570 blocks, &submit);
1571 } else {
1572 /* We're missing D+D. */
1573 return async_raid6_2data_recov(syndrome_disks+2,
1574 STRIPE_SIZE, faila, failb,
1575 blocks, &submit);
1576 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001577 }
1578}
1579
Dan Williams91c00922007-01-02 13:52:30 -07001580static void ops_complete_prexor(void *stripe_head_ref)
1581{
1582 struct stripe_head *sh = stripe_head_ref;
1583
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001584 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001585 (unsigned long long)sh->sector);
Song Liu1e6d6902016-11-17 15:24:39 -08001586
1587 if (r5c_is_writeback(sh->raid_conf->log))
1588 /*
1589 * raid5-cache write back uses orig_page during prexor.
1590 * After prexor, it is time to free orig_page
1591 */
1592 r5c_release_extra_page(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001593}
1594
1595static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001596ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1597 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001598{
Dan Williams91c00922007-01-02 13:52:30 -07001599 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001600 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001601 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001602 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001603
1604 /* existing parity data subtracted */
1605 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1606
shli@kernel.org59fc6302014-12-15 12:57:03 +11001607 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001608 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001609 (unsigned long long)sh->sector);
1610
1611 for (i = disks; i--; ) {
1612 struct r5dev *dev = &sh->dev[i];
1613 /* Only process blocks that are known to be uptodate */
Song Liu1e6d6902016-11-17 15:24:39 -08001614 if (test_bit(R5_InJournal, &dev->flags))
1615 xor_srcs[count++] = dev->orig_page;
1616 else if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001617 xor_srcs[count++] = dev->page;
1618 }
1619
Dan Williams0403e382009-09-08 17:42:50 -07001620 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001621 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Dan Williamsa08abd82009-06-03 11:43:59 -07001622 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001623
1624 return tx;
1625}
1626
1627static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001628ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1629 struct dma_async_tx_descriptor *tx)
1630{
1631 struct page **blocks = to_addr_page(percpu, 0);
1632 int count;
1633 struct async_submit_ctl submit;
1634
1635 pr_debug("%s: stripe %llu\n", __func__,
1636 (unsigned long long)sh->sector);
1637
1638 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN);
1639
1640 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1641 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1642 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1643
1644 return tx;
1645}
1646
1647static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001648ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001649{
Song Liu1e6d6902016-11-17 15:24:39 -08001650 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001651 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001652 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001653 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -07001654
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001655 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001656 (unsigned long long)sh->sector);
1657
1658 for (i = disks; i--; ) {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001659 struct r5dev *dev;
Dan Williams91c00922007-01-02 13:52:30 -07001660 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001661
shli@kernel.org59fc6302014-12-15 12:57:03 +11001662 sh = head_sh;
1663 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001664 struct bio *wbi;
1665
shli@kernel.org59fc6302014-12-15 12:57:03 +11001666again:
1667 dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001668 /*
1669 * clear R5_InJournal, so when rewriting a page in
1670 * journal, it is not skipped by r5l_log_stripe()
1671 */
1672 clear_bit(R5_InJournal, &dev->flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10001673 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001674 chosen = dev->towrite;
1675 dev->towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11001676 sh->overwrite_disks = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001677 BUG_ON(dev->written);
1678 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001679 spin_unlock_irq(&sh->stripe_lock);
Shaohua Lid592a992014-05-21 17:57:44 +08001680 WARN_ON(dev->page != dev->orig_page);
Dan Williams91c00922007-01-02 13:52:30 -07001681
Kent Overstreet4f024f32013-10-11 15:44:27 -07001682 while (wbi && wbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001683 dev->sector + STRIPE_SECTORS) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001684 if (wbi->bi_opf & REQ_FUA)
Tejun Heoe9c74692010-09-03 11:56:18 +02001685 set_bit(R5_WantFUA, &dev->flags);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001686 if (wbi->bi_opf & REQ_SYNC)
Shaohua Libc0934f2012-05-22 13:55:05 +10001687 set_bit(R5_SyncIO, &dev->flags);
Mike Christie796a5cf2016-06-05 14:32:07 -05001688 if (bio_op(wbi) == REQ_OP_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001689 set_bit(R5_Discard, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08001690 else {
1691 tx = async_copy_data(1, wbi, &dev->page,
Song Liu1e6d6902016-11-17 15:24:39 -08001692 dev->sector, tx, sh,
1693 r5c_is_writeback(conf->log));
1694 if (dev->page != dev->orig_page &&
1695 !r5c_is_writeback(conf->log)) {
Shaohua Lid592a992014-05-21 17:57:44 +08001696 set_bit(R5_SkipCopy, &dev->flags);
1697 clear_bit(R5_UPTODATE, &dev->flags);
1698 clear_bit(R5_OVERWRITE, &dev->flags);
1699 }
1700 }
Dan Williams91c00922007-01-02 13:52:30 -07001701 wbi = r5_next_bio(wbi, dev->sector);
1702 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001703
1704 if (head_sh->batch_head) {
1705 sh = list_first_entry(&sh->batch_list,
1706 struct stripe_head,
1707 batch_list);
1708 if (sh == head_sh)
1709 continue;
1710 goto again;
1711 }
Dan Williams91c00922007-01-02 13:52:30 -07001712 }
1713 }
1714
1715 return tx;
1716}
1717
Dan Williamsac6b53b2009-07-14 13:40:19 -07001718static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001719{
1720 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001721 int disks = sh->disks;
1722 int pd_idx = sh->pd_idx;
1723 int qd_idx = sh->qd_idx;
1724 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001725 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001726
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001727 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001728 (unsigned long long)sh->sector);
1729
Shaohua Libc0934f2012-05-22 13:55:05 +10001730 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001731 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001732 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001733 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001734 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001735
Dan Williams91c00922007-01-02 13:52:30 -07001736 for (i = disks; i--; ) {
1737 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001738
Tejun Heoe9c74692010-09-03 11:56:18 +02001739 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Lid592a992014-05-21 17:57:44 +08001740 if (!discard && !test_bit(R5_SkipCopy, &dev->flags))
Shaohua Li9e4447682012-10-11 13:49:49 +11001741 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001742 if (fua)
1743 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001744 if (sync)
1745 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001746 }
Dan Williams91c00922007-01-02 13:52:30 -07001747 }
1748
Dan Williamsd8ee0722008-06-28 08:32:06 +10001749 if (sh->reconstruct_state == reconstruct_state_drain_run)
1750 sh->reconstruct_state = reconstruct_state_drain_result;
1751 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1752 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1753 else {
1754 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1755 sh->reconstruct_state = reconstruct_state_result;
1756 }
Dan Williams91c00922007-01-02 13:52:30 -07001757
1758 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001759 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001760}
1761
1762static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001763ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1764 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001765{
Dan Williams91c00922007-01-02 13:52:30 -07001766 int disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001767 struct page **xor_srcs;
Dan Williamsa08abd82009-06-03 11:43:59 -07001768 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001769 int count, pd_idx = sh->pd_idx, i;
Dan Williams91c00922007-01-02 13:52:30 -07001770 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001771 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001772 unsigned long flags;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001773 int j = 0;
1774 struct stripe_head *head_sh = sh;
1775 int last_stripe;
Dan Williams91c00922007-01-02 13:52:30 -07001776
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001777 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001778 (unsigned long long)sh->sector);
1779
Shaohua Li620125f2012-10-11 13:49:05 +11001780 for (i = 0; i < sh->disks; i++) {
1781 if (pd_idx == i)
1782 continue;
1783 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1784 break;
1785 }
1786 if (i >= sh->disks) {
1787 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001788 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1789 ops_complete_reconstruct(sh);
1790 return;
1791 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001792again:
1793 count = 0;
1794 xor_srcs = to_addr_page(percpu, j);
Dan Williams91c00922007-01-02 13:52:30 -07001795 /* check if prexor is active which means only process blocks
1796 * that are part of a read-modify-write (written)
1797 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001798 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001799 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001800 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1801 for (i = disks; i--; ) {
1802 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001803 if (head_sh->dev[i].written ||
1804 test_bit(R5_InJournal, &head_sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -07001805 xor_srcs[count++] = dev->page;
1806 }
1807 } else {
1808 xor_dest = sh->dev[pd_idx].page;
1809 for (i = disks; i--; ) {
1810 struct r5dev *dev = &sh->dev[i];
1811 if (i != pd_idx)
1812 xor_srcs[count++] = dev->page;
1813 }
1814 }
1815
Dan Williams91c00922007-01-02 13:52:30 -07001816 /* 1/ if we prexor'd then the dest is reused as a source
1817 * 2/ if we did not prexor then we are redoing the parity
1818 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1819 * for the synchronous xor case
1820 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001821 last_stripe = !head_sh->batch_head ||
1822 list_first_entry(&sh->batch_list,
1823 struct stripe_head, batch_list) == head_sh;
1824 if (last_stripe) {
1825 flags = ASYNC_TX_ACK |
1826 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
Dan Williams91c00922007-01-02 13:52:30 -07001827
shli@kernel.org59fc6302014-12-15 12:57:03 +11001828 atomic_inc(&head_sh->count);
1829 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
1830 to_addr_conv(sh, percpu, j));
1831 } else {
1832 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
1833 init_async_submit(&submit, flags, tx, NULL, NULL,
1834 to_addr_conv(sh, percpu, j));
1835 }
Dan Williams91c00922007-01-02 13:52:30 -07001836
Dan Williamsa08abd82009-06-03 11:43:59 -07001837 if (unlikely(count == 1))
1838 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1839 else
1840 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001841 if (!last_stripe) {
1842 j++;
1843 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1844 batch_list);
1845 goto again;
1846 }
Dan Williams91c00922007-01-02 13:52:30 -07001847}
1848
Dan Williamsac6b53b2009-07-14 13:40:19 -07001849static void
1850ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1851 struct dma_async_tx_descriptor *tx)
1852{
1853 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001854 struct page **blocks;
1855 int count, i, j = 0;
1856 struct stripe_head *head_sh = sh;
1857 int last_stripe;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001858 int synflags;
1859 unsigned long txflags;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001860
1861 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1862
Shaohua Li620125f2012-10-11 13:49:05 +11001863 for (i = 0; i < sh->disks; i++) {
1864 if (sh->pd_idx == i || sh->qd_idx == i)
1865 continue;
1866 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1867 break;
1868 }
1869 if (i >= sh->disks) {
1870 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001871 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1872 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1873 ops_complete_reconstruct(sh);
1874 return;
1875 }
1876
shli@kernel.org59fc6302014-12-15 12:57:03 +11001877again:
1878 blocks = to_addr_page(percpu, j);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001879
1880 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1881 synflags = SYNDROME_SRC_WRITTEN;
1882 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
1883 } else {
1884 synflags = SYNDROME_SRC_ALL;
1885 txflags = ASYNC_TX_ACK;
1886 }
1887
1888 count = set_syndrome_sources(blocks, sh, synflags);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001889 last_stripe = !head_sh->batch_head ||
1890 list_first_entry(&sh->batch_list,
1891 struct stripe_head, batch_list) == head_sh;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001892
shli@kernel.org59fc6302014-12-15 12:57:03 +11001893 if (last_stripe) {
1894 atomic_inc(&head_sh->count);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001895 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
shli@kernel.org59fc6302014-12-15 12:57:03 +11001896 head_sh, to_addr_conv(sh, percpu, j));
1897 } else
1898 init_async_submit(&submit, 0, tx, NULL, NULL,
1899 to_addr_conv(sh, percpu, j));
Shaohua Li48769692015-05-13 09:30:08 -07001900 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001901 if (!last_stripe) {
1902 j++;
1903 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1904 batch_list);
1905 goto again;
1906 }
Dan Williams91c00922007-01-02 13:52:30 -07001907}
1908
1909static void ops_complete_check(void *stripe_head_ref)
1910{
1911 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001912
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001913 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001914 (unsigned long long)sh->sector);
1915
Dan Williamsecc65c92008-06-28 08:31:57 +10001916 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001917 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001918 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001919}
1920
Dan Williamsac6b53b2009-07-14 13:40:19 -07001921static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001922{
Dan Williams91c00922007-01-02 13:52:30 -07001923 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001924 int pd_idx = sh->pd_idx;
1925 int qd_idx = sh->qd_idx;
1926 struct page *xor_dest;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001927 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001928 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001929 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001930 int count;
1931 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001932
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001933 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001934 (unsigned long long)sh->sector);
1935
shli@kernel.org59fc6302014-12-15 12:57:03 +11001936 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001937 count = 0;
1938 xor_dest = sh->dev[pd_idx].page;
1939 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001940 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001941 if (i == pd_idx || i == qd_idx)
1942 continue;
1943 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001944 }
1945
Dan Williamsd6f38f32009-07-14 11:50:52 -07001946 init_async_submit(&submit, 0, NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001947 to_addr_conv(sh, percpu, 0));
Dan Williams099f53c2009-04-08 14:28:37 -07001948 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001949 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001950
Dan Williams91c00922007-01-02 13:52:30 -07001951 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001952 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1953 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001954}
1955
Dan Williamsac6b53b2009-07-14 13:40:19 -07001956static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1957{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001958 struct page **srcs = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001959 struct async_submit_ctl submit;
1960 int count;
1961
1962 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1963 (unsigned long long)sh->sector, checkp);
1964
shli@kernel.org59fc6302014-12-15 12:57:03 +11001965 BUG_ON(sh->batch_head);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001966 count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001967 if (!checkp)
1968 srcs[count] = NULL;
1969
1970 atomic_inc(&sh->count);
1971 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001972 sh, to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001973 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1974 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1975}
1976
NeilBrown51acbce2013-02-28 09:08:34 +11001977static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001978{
1979 int overlap_clear = 0, i, disks = sh->disks;
1980 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001981 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001982 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001983 struct raid5_percpu *percpu;
1984 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001985
Dan Williamsd6f38f32009-07-14 11:50:52 -07001986 cpu = get_cpu();
1987 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001988 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001989 ops_run_biofill(sh);
1990 overlap_clear++;
1991 }
1992
Dan Williams7b3a8712008-06-28 08:32:09 +10001993 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001994 if (level < 6)
1995 tx = ops_run_compute5(sh, percpu);
1996 else {
1997 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1998 tx = ops_run_compute6_1(sh, percpu);
1999 else
2000 tx = ops_run_compute6_2(sh, percpu);
2001 }
2002 /* terminate the chain if reconstruct is not set to be run */
2003 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10002004 async_tx_ack(tx);
2005 }
Dan Williams91c00922007-01-02 13:52:30 -07002006
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002007 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
2008 if (level < 6)
2009 tx = ops_run_prexor5(sh, percpu, tx);
2010 else
2011 tx = ops_run_prexor6(sh, percpu, tx);
2012 }
Dan Williams91c00922007-01-02 13:52:30 -07002013
Dan Williams600aa102008-06-28 08:32:05 +10002014 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10002015 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07002016 overlap_clear++;
2017 }
2018
Dan Williamsac6b53b2009-07-14 13:40:19 -07002019 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
2020 if (level < 6)
2021 ops_run_reconstruct5(sh, percpu, tx);
2022 else
2023 ops_run_reconstruct6(sh, percpu, tx);
2024 }
Dan Williams91c00922007-01-02 13:52:30 -07002025
Dan Williamsac6b53b2009-07-14 13:40:19 -07002026 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
2027 if (sh->check_state == check_state_run)
2028 ops_run_check_p(sh, percpu);
2029 else if (sh->check_state == check_state_run_q)
2030 ops_run_check_pq(sh, percpu, 0);
2031 else if (sh->check_state == check_state_run_pq)
2032 ops_run_check_pq(sh, percpu, 1);
2033 else
2034 BUG();
2035 }
Dan Williams91c00922007-01-02 13:52:30 -07002036
shli@kernel.org59fc6302014-12-15 12:57:03 +11002037 if (overlap_clear && !sh->batch_head)
Dan Williams91c00922007-01-02 13:52:30 -07002038 for (i = disks; i--; ) {
2039 struct r5dev *dev = &sh->dev[i];
2040 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2041 wake_up(&sh->raid_conf->wait_for_overlap);
2042 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07002043 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07002044}
2045
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002046static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
2047 int disks)
NeilBrownf18c1a32015-05-08 18:19:04 +10002048{
2049 struct stripe_head *sh;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002050 int i;
NeilBrownf18c1a32015-05-08 18:19:04 +10002051
2052 sh = kmem_cache_zalloc(sc, gfp);
2053 if (sh) {
2054 spin_lock_init(&sh->stripe_lock);
2055 spin_lock_init(&sh->batch_lock);
2056 INIT_LIST_HEAD(&sh->batch_list);
2057 INIT_LIST_HEAD(&sh->lru);
Song Liua39f7af2016-11-17 15:24:40 -08002058 INIT_LIST_HEAD(&sh->r5c);
Song Liud7bd3982016-11-23 22:50:39 -08002059 INIT_LIST_HEAD(&sh->log_list);
NeilBrownf18c1a32015-05-08 18:19:04 +10002060 atomic_set(&sh->count, 1);
Song Liua39f7af2016-11-17 15:24:40 -08002061 sh->log_start = MaxSector;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002062 for (i = 0; i < disks; i++) {
2063 struct r5dev *dev = &sh->dev[i];
2064
Ming Lei3a83f462016-11-22 08:57:21 -07002065 bio_init(&dev->req, &dev->vec, 1);
2066 bio_init(&dev->rreq, &dev->rvec, 1);
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002067 }
NeilBrownf18c1a32015-05-08 18:19:04 +10002068 }
2069 return sh;
2070}
NeilBrown486f0642015-02-25 12:10:35 +11002071static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
2073 struct stripe_head *sh;
NeilBrownf18c1a32015-05-08 18:19:04 +10002074
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002075 sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08002076 if (!sh)
2077 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10002078
NeilBrown3f294f42005-11-08 21:39:25 -08002079 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08002080
NeilBrowna9683a72015-02-25 12:02:51 +11002081 if (grow_buffers(sh, gfp)) {
NeilBrowne4e11e32010-06-16 16:45:16 +10002082 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002083 kmem_cache_free(conf->slab_cache, sh);
2084 return 0;
2085 }
NeilBrown486f0642015-02-25 12:10:35 +11002086 sh->hash_lock_index =
2087 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrown3f294f42005-11-08 21:39:25 -08002088 /* we just created an active stripe so... */
NeilBrown3f294f42005-11-08 21:39:25 -08002089 atomic_inc(&conf->active_stripes);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002090
Shaohua Li6d036f72015-08-13 14:31:57 -07002091 raid5_release_stripe(sh);
NeilBrown486f0642015-02-25 12:10:35 +11002092 conf->max_nr_stripes++;
NeilBrown3f294f42005-11-08 21:39:25 -08002093 return 1;
2094}
2095
NeilBrownd1688a62011-10-11 16:49:52 +11002096static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08002097{
Christoph Lametere18b8902006-12-06 20:33:20 -08002098 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11002099 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
NeilBrownf4be6b42010-06-01 19:37:25 +10002101 if (conf->mddev->gendisk)
2102 sprintf(conf->cache_name[0],
2103 "raid%d-%s", conf->level, mdname(conf->mddev));
2104 else
2105 sprintf(conf->cache_name[0],
2106 "raid%d-%p", conf->level, conf->mddev);
2107 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
2108
NeilBrownad01c9e2006-03-27 01:18:07 -08002109 conf->active_name = 0;
2110 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002112 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (!sc)
2114 return 1;
2115 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002116 conf->pool_size = devs;
NeilBrown486f0642015-02-25 12:10:35 +11002117 while (num--)
2118 if (!grow_one_stripe(conf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return 1;
NeilBrown486f0642015-02-25 12:10:35 +11002120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return 0;
2122}
NeilBrown29269552006-03-27 01:18:10 -08002123
Dan Williamsd6f38f32009-07-14 11:50:52 -07002124/**
2125 * scribble_len - return the required size of the scribble region
2126 * @num - total number of disks in the array
2127 *
2128 * The size must be enough to contain:
2129 * 1/ a struct page pointer for each device in the array +2
2130 * 2/ room to convert each entry in (1) to its corresponding dma
2131 * (dma_map_page()) or page (page_address()) address.
2132 *
2133 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2134 * calculate over all devices (not just the data blocks), using zeros in place
2135 * of the P and Q blocks.
2136 */
shli@kernel.org46d5b782014-12-15 12:57:02 +11002137static struct flex_array *scribble_alloc(int num, int cnt, gfp_t flags)
Dan Williamsd6f38f32009-07-14 11:50:52 -07002138{
shli@kernel.org46d5b782014-12-15 12:57:02 +11002139 struct flex_array *ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002140 size_t len;
2141
2142 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
shli@kernel.org46d5b782014-12-15 12:57:02 +11002143 ret = flex_array_alloc(len, cnt, flags);
2144 if (!ret)
2145 return NULL;
2146 /* always prealloc all elements, so no locking is required */
2147 if (flex_array_prealloc(ret, 0, cnt, flags)) {
2148 flex_array_free(ret);
2149 return NULL;
2150 }
2151 return ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002152}
2153
NeilBrown738a2732015-05-08 18:19:39 +10002154static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2155{
2156 unsigned long cpu;
2157 int err = 0;
2158
Shaohua Li27a353c2016-02-24 17:38:28 -08002159 /*
2160 * Never shrink. And mddev_suspend() could deadlock if this is called
2161 * from raid5d. In that case, scribble_disks and scribble_sectors
2162 * should equal to new_disks and new_sectors
2163 */
2164 if (conf->scribble_disks >= new_disks &&
2165 conf->scribble_sectors >= new_sectors)
2166 return 0;
NeilBrown738a2732015-05-08 18:19:39 +10002167 mddev_suspend(conf->mddev);
2168 get_online_cpus();
2169 for_each_present_cpu(cpu) {
2170 struct raid5_percpu *percpu;
2171 struct flex_array *scribble;
2172
2173 percpu = per_cpu_ptr(conf->percpu, cpu);
2174 scribble = scribble_alloc(new_disks,
2175 new_sectors / STRIPE_SECTORS,
2176 GFP_NOIO);
2177
2178 if (scribble) {
2179 flex_array_free(percpu->scribble);
2180 percpu->scribble = scribble;
2181 } else {
2182 err = -ENOMEM;
2183 break;
2184 }
2185 }
2186 put_online_cpus();
2187 mddev_resume(conf->mddev);
Shaohua Li27a353c2016-02-24 17:38:28 -08002188 if (!err) {
2189 conf->scribble_disks = new_disks;
2190 conf->scribble_sectors = new_sectors;
2191 }
NeilBrown738a2732015-05-08 18:19:39 +10002192 return err;
2193}
2194
NeilBrownd1688a62011-10-11 16:49:52 +11002195static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08002196{
2197 /* Make all the stripes able to hold 'newsize' devices.
2198 * New slots in each stripe get 'page' set to a new page.
2199 *
2200 * This happens in stages:
2201 * 1/ create a new kmem_cache and allocate the required number of
2202 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09002203 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08002204 * to the new stripe_heads. This will have the side effect of
2205 * freezing the array as once all stripe_heads have been collected,
2206 * no IO will be possible. Old stripe heads are freed once their
2207 * pages have been transferred over, and the old kmem_cache is
2208 * freed when all stripes are done.
2209 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
2210 * we simple return a failre status - no need to clean anything up.
2211 * 4/ allocate new pages for the new slots in the new stripe_heads.
2212 * If this fails, we don't bother trying the shrink the
2213 * stripe_heads down again, we just leave them as they are.
2214 * As each stripe_head is processed the new one is released into
2215 * active service.
2216 *
2217 * Once step2 is started, we cannot afford to wait for a write,
2218 * so we use GFP_NOIO allocations.
2219 */
2220 struct stripe_head *osh, *nsh;
2221 LIST_HEAD(newstripes);
2222 struct disk_info *ndisks;
Dan Williamsb5470dc2008-06-27 21:44:04 -07002223 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08002224 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002225 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11002226 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08002227
2228 if (newsize <= conf->pool_size)
2229 return 0; /* never bother to shrink */
2230
Dan Williamsb5470dc2008-06-27 21:44:04 -07002231 err = md_allow_write(conf->mddev);
2232 if (err)
2233 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08002234
NeilBrownad01c9e2006-03-27 01:18:07 -08002235 /* Step 1 */
2236 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2237 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002238 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08002239 if (!sc)
2240 return -ENOMEM;
2241
NeilBrown2d5b5692015-07-06 12:49:23 +10002242 /* Need to ensure auto-resizing doesn't interfere */
2243 mutex_lock(&conf->cache_size_mutex);
2244
NeilBrownad01c9e2006-03-27 01:18:07 -08002245 for (i = conf->max_nr_stripes; i; i--) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002246 nsh = alloc_stripe(sc, GFP_KERNEL, newsize);
NeilBrownad01c9e2006-03-27 01:18:07 -08002247 if (!nsh)
2248 break;
2249
NeilBrownad01c9e2006-03-27 01:18:07 -08002250 nsh->raid_conf = conf;
NeilBrownad01c9e2006-03-27 01:18:07 -08002251 list_add(&nsh->lru, &newstripes);
2252 }
2253 if (i) {
2254 /* didn't get enough, give up */
2255 while (!list_empty(&newstripes)) {
2256 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2257 list_del(&nsh->lru);
2258 kmem_cache_free(sc, nsh);
2259 }
2260 kmem_cache_destroy(sc);
NeilBrown2d5b5692015-07-06 12:49:23 +10002261 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002262 return -ENOMEM;
2263 }
2264 /* Step 2 - Must use GFP_NOIO now.
2265 * OK, we have enough stripes, start collecting inactive
2266 * stripes and copying them over
2267 */
Shaohua Li566c09c2013-11-14 15:16:17 +11002268 hash = 0;
2269 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08002270 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11002271 lock_device_hash_lock(conf, hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08002272 wait_event_cmd(conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +11002273 !list_empty(conf->inactive_list + hash),
2274 unlock_device_hash_lock(conf, hash),
2275 lock_device_hash_lock(conf, hash));
2276 osh = get_free_stripe(conf, hash);
2277 unlock_device_hash_lock(conf, hash);
NeilBrownf18c1a32015-05-08 18:19:04 +10002278
Shaohua Lid592a992014-05-21 17:57:44 +08002279 for(i=0; i<conf->pool_size; i++) {
NeilBrownad01c9e2006-03-27 01:18:07 -08002280 nsh->dev[i].page = osh->dev[i].page;
Shaohua Lid592a992014-05-21 17:57:44 +08002281 nsh->dev[i].orig_page = osh->dev[i].page;
2282 }
Shaohua Li566c09c2013-11-14 15:16:17 +11002283 nsh->hash_lock_index = hash;
NeilBrownad01c9e2006-03-27 01:18:07 -08002284 kmem_cache_free(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11002285 cnt++;
2286 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2287 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2288 hash++;
2289 cnt = 0;
2290 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002291 }
2292 kmem_cache_destroy(conf->slab_cache);
2293
2294 /* Step 3.
2295 * At this point, we are holding all the stripes so the array
2296 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07002297 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08002298 */
2299 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
2300 if (ndisks) {
Song Liud7bd3982016-11-23 22:50:39 -08002301 for (i = 0; i < conf->pool_size; i++)
NeilBrownad01c9e2006-03-27 01:18:07 -08002302 ndisks[i] = conf->disks[i];
Song Liud7bd3982016-11-23 22:50:39 -08002303
2304 for (i = conf->pool_size; i < newsize; i++) {
2305 ndisks[i].extra_page = alloc_page(GFP_NOIO);
2306 if (!ndisks[i].extra_page)
2307 err = -ENOMEM;
2308 }
2309
2310 if (err) {
2311 for (i = conf->pool_size; i < newsize; i++)
2312 if (ndisks[i].extra_page)
2313 put_page(ndisks[i].extra_page);
2314 kfree(ndisks);
2315 } else {
2316 kfree(conf->disks);
2317 conf->disks = ndisks;
2318 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002319 } else
2320 err = -ENOMEM;
2321
NeilBrown2d5b5692015-07-06 12:49:23 +10002322 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002323 /* Step 4, return new stripes to service */
2324 while(!list_empty(&newstripes)) {
2325 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2326 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07002327
NeilBrownad01c9e2006-03-27 01:18:07 -08002328 for (i=conf->raid_disks; i < newsize; i++)
2329 if (nsh->dev[i].page == NULL) {
2330 struct page *p = alloc_page(GFP_NOIO);
2331 nsh->dev[i].page = p;
Shaohua Lid592a992014-05-21 17:57:44 +08002332 nsh->dev[i].orig_page = p;
NeilBrownad01c9e2006-03-27 01:18:07 -08002333 if (!p)
2334 err = -ENOMEM;
2335 }
Shaohua Li6d036f72015-08-13 14:31:57 -07002336 raid5_release_stripe(nsh);
NeilBrownad01c9e2006-03-27 01:18:07 -08002337 }
2338 /* critical section pass, GFP_NOIO no longer needed */
2339
2340 conf->slab_cache = sc;
2341 conf->active_name = 1-conf->active_name;
NeilBrown6e9eac22015-05-08 18:19:34 +10002342 if (!err)
2343 conf->pool_size = newsize;
NeilBrownad01c9e2006-03-27 01:18:07 -08002344 return err;
2345}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
NeilBrown486f0642015-02-25 12:10:35 +11002347static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
2349 struct stripe_head *sh;
NeilBrown49895bc2015-08-03 17:09:57 +10002350 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Shaohua Li566c09c2013-11-14 15:16:17 +11002352 spin_lock_irq(conf->hash_locks + hash);
2353 sh = get_free_stripe(conf, hash);
2354 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08002355 if (!sh)
2356 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002357 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10002358 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002359 kmem_cache_free(conf->slab_cache, sh);
2360 atomic_dec(&conf->active_stripes);
NeilBrown486f0642015-02-25 12:10:35 +11002361 conf->max_nr_stripes--;
NeilBrown3f294f42005-11-08 21:39:25 -08002362 return 1;
2363}
2364
NeilBrownd1688a62011-10-11 16:49:52 +11002365static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08002366{
NeilBrown486f0642015-02-25 12:10:35 +11002367 while (conf->max_nr_stripes &&
2368 drop_one_stripe(conf))
2369 ;
NeilBrown3f294f42005-11-08 21:39:25 -08002370
Julia Lawall644df1a2015-09-13 14:15:10 +02002371 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 conf->slab_cache = NULL;
2373}
2374
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002375static void raid5_end_read_request(struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
NeilBrown99c0fb52009-03-31 14:39:38 +11002377 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002378 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002379 int disks = sh->disks, i;
NeilBrownd6950432006-07-10 04:44:20 -07002380 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11002381 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10002382 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 for (i=0 ; i<disks; i++)
2385 if (bi == &sh->dev[i].req)
2386 break;
2387
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002388 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
Dan Williams45b42332007-07-09 11:56:43 -07002389 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002390 bi->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002392 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002394 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 }
NeilBrown14a75d32011-12-23 10:17:52 +11002396 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11002397 /* If replacement finished while this request was outstanding,
2398 * 'replacement' might be NULL already.
2399 * In that case it moved down to 'rdev'.
2400 * rdev is not removed until all requests are finished.
2401 */
NeilBrown14a75d32011-12-23 10:17:52 +11002402 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002403 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11002404 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
NeilBrown05616be2012-05-21 09:27:00 +10002406 if (use_new_offset(conf, sh))
2407 s = sh->sector + rdev->new_data_offset;
2408 else
2409 s = sh->sector + rdev->data_offset;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002410 if (!bi->bi_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08002412 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11002413 /* Note that this cannot happen on a
2414 * replacement device. We just fail those on
2415 * any error
2416 */
NeilBrowncc6167b2016-11-02 14:16:50 +11002417 pr_info_ratelimited(
2418 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002419 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10002420 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002421 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10002422 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08002423 clear_bit(R5_ReadError, &sh->dev[i].flags);
2424 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002425 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2426 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2427
Song Liu86aa1392017-01-12 17:22:41 -08002428 if (test_bit(R5_InJournal, &sh->dev[i].flags))
2429 /*
2430 * end read for a page in journal, this
2431 * must be preparing for prexor in rmw
2432 */
2433 set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
2434
NeilBrown14a75d32011-12-23 10:17:52 +11002435 if (atomic_read(&rdev->read_errors))
2436 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002438 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002439 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002440 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07002443 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002444 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowncc6167b2016-11-02 14:16:50 +11002445 pr_warn_ratelimited(
2446 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
NeilBrown14a75d32011-12-23 10:17:52 +11002447 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002448 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002449 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002450 else if (conf->mddev->degraded >= conf->max_degraded) {
2451 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002452 pr_warn_ratelimited(
2453 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002454 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002455 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002456 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002457 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002458 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002459 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002460 pr_warn_ratelimited(
2461 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002462 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002463 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002464 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002465 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08002466 > conf->max_nr_stripes)
NeilBrowncc6167b2016-11-02 14:16:50 +11002467 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07002468 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08002469 else
2470 retry = 1;
Bian Yuedfa1f62013-11-14 15:16:17 +11002471 if (set_bad && test_bit(In_sync, &rdev->flags)
2472 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2473 retry = 1;
NeilBrownba22dcb2005-11-08 21:39:31 -08002474 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10002475 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2476 set_bit(R5_ReadError, &sh->dev[i].flags);
2477 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2478 } else
2479 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002480 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002481 clear_bit(R5_ReadError, &sh->dev[i].flags);
2482 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002483 if (!(set_bad
2484 && test_bit(In_sync, &rdev->flags)
2485 && rdev_set_badblocks(
2486 rdev, sh->sector, STRIPE_SECTORS, 0)))
2487 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
NeilBrown14a75d32011-12-23 10:17:52 +11002490 rdev_dec_pending(rdev, conf->mddev);
Shaohua Lic9445552016-09-08 10:43:58 -07002491 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2493 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002494 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002497static void raid5_end_write_request(struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
NeilBrown99c0fb52009-03-31 14:39:38 +11002499 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002500 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002501 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11002502 struct md_rdev *uninitialized_var(rdev);
NeilBrownb84db562011-07-28 11:39:23 +10002503 sector_t first_bad;
2504 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002505 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
NeilBrown977df362011-12-23 10:17:53 +11002507 for (i = 0 ; i < disks; i++) {
2508 if (bi == &sh->dev[i].req) {
2509 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 break;
NeilBrown977df362011-12-23 10:17:53 +11002511 }
2512 if (bi == &sh->dev[i].rreq) {
2513 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002514 if (rdev)
2515 replacement = 1;
2516 else
2517 /* rdev was removed and 'replacement'
2518 * replaced it. rdev is not removed
2519 * until all requests are finished.
2520 */
2521 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002522 break;
2523 }
2524 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002525 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002527 bi->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002529 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002531 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 }
2533
NeilBrown977df362011-12-23 10:17:53 +11002534 if (replacement) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002535 if (bi->bi_error)
NeilBrown977df362011-12-23 10:17:53 +11002536 md_error(conf->mddev, rdev);
2537 else if (is_badblock(rdev, sh->sector,
2538 STRIPE_SECTORS,
2539 &first_bad, &bad_sectors))
2540 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2541 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002542 if (bi->bi_error) {
NeilBrown9f97e4b2014-01-16 09:35:38 +11002543 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11002544 set_bit(WriteErrorSeen, &rdev->flags);
2545 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002546 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2547 set_bit(MD_RECOVERY_NEEDED,
2548 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002549 } else if (is_badblock(rdev, sh->sector,
2550 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10002551 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002552 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002553 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2554 /* That was a successful write so make
2555 * sure it looks like we already did
2556 * a re-write.
2557 */
2558 set_bit(R5_ReWrite, &sh->dev[i].flags);
2559 }
NeilBrown977df362011-12-23 10:17:53 +11002560 }
2561 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002563 if (sh->batch_head && bi->bi_error && !replacement)
shli@kernel.org72ac7332014-12-15 12:57:03 +11002564 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2565
Shaohua Lic9445552016-09-08 10:43:58 -07002566 bio_reset(bi);
NeilBrown977df362011-12-23 10:17:53 +11002567 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2568 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002570 raid5_release_stripe(sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002571
2572 if (sh->batch_head && sh != sh->batch_head)
Shaohua Li6d036f72015-08-13 14:31:57 -07002573 raid5_release_stripe(sh->batch_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
NeilBrown784052e2009-03-31 15:19:07 +11002576static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
2578 struct r5dev *dev = &sh->dev[i];
2579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 dev->flags = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07002581 dev->sector = raid5_compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582}
2583
Shaohua Li849674e2016-01-20 13:52:20 -08002584static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
2586 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002587 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002588 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002589 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
NeilBrown908f4fb2011-12-23 10:17:50 +11002591 spin_lock_irqsave(&conf->device_lock, flags);
2592 clear_bit(In_sync, &rdev->flags);
Song Liu2e38a372017-01-24 10:45:30 -08002593 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11002594 spin_unlock_irqrestore(&conf->device_lock, flags);
2595 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2596
NeilBrownde393cd2011-07-28 11:31:48 +10002597 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10002598 set_bit(Faulty, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08002599 set_mask_bits(&mddev->sb_flags, 0,
2600 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrowncc6167b2016-11-02 14:16:50 +11002601 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
2602 "md/raid:%s: Operation continuing on %d devices.\n",
2603 mdname(mddev),
2604 bdevname(rdev->bdev, b),
2605 mdname(mddev),
2606 conf->raid_disks - mddev->degraded);
Song Liu2e38a372017-01-24 10:45:30 -08002607 r5c_update_on_rdev_error(mddev);
NeilBrown16a53ec2006-06-26 00:27:38 -07002608}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610/*
2611 * Input: a 'big' sector number,
2612 * Output: index of the data and parity disk, and the sector # in them.
2613 */
Shaohua Li6d036f72015-08-13 14:31:57 -07002614sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2615 int previous, int *dd_idx,
2616 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002618 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002619 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002621 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002622 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002624 int algorithm = previous ? conf->prev_algo
2625 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002626 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2627 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002628 int raid_disks = previous ? conf->previous_raid_disks
2629 : conf->raid_disks;
2630 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632 /* First compute the information on this sector */
2633
2634 /*
2635 * Compute the chunk number and the sector offset inside the chunk
2636 */
2637 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2638 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
2640 /*
2641 * Compute the stripe number
2642 */
NeilBrown35f2a592010-04-20 14:13:34 +10002643 stripe = chunk_number;
2644 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002645 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 /*
2647 * Select the parity disk based on the user selected algorithm.
2648 */
NeilBrown84789552011-07-27 11:00:36 +10002649 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002650 switch(conf->level) {
2651 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002652 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002653 break;
2654 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002655 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002657 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002658 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 (*dd_idx)++;
2660 break;
2661 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002662 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002663 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 (*dd_idx)++;
2665 break;
2666 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002667 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002668 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 break;
2670 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002671 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002672 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002674 case ALGORITHM_PARITY_0:
2675 pd_idx = 0;
2676 (*dd_idx)++;
2677 break;
2678 case ALGORITHM_PARITY_N:
2679 pd_idx = data_disks;
2680 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002682 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002683 }
2684 break;
2685 case 6:
2686
NeilBrowne183eae2009-03-31 15:20:22 +11002687 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002688 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002689 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002690 qd_idx = pd_idx + 1;
2691 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002692 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002693 qd_idx = 0;
2694 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002695 (*dd_idx) += 2; /* D D P Q D */
2696 break;
2697 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002698 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002699 qd_idx = pd_idx + 1;
2700 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002701 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002702 qd_idx = 0;
2703 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002704 (*dd_idx) += 2; /* D D P Q D */
2705 break;
2706 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002707 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002708 qd_idx = (pd_idx + 1) % raid_disks;
2709 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002710 break;
2711 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002712 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002713 qd_idx = (pd_idx + 1) % raid_disks;
2714 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002715 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002716
2717 case ALGORITHM_PARITY_0:
2718 pd_idx = 0;
2719 qd_idx = 1;
2720 (*dd_idx) += 2;
2721 break;
2722 case ALGORITHM_PARITY_N:
2723 pd_idx = data_disks;
2724 qd_idx = data_disks + 1;
2725 break;
2726
2727 case ALGORITHM_ROTATING_ZERO_RESTART:
2728 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2729 * of blocks for computing Q is different.
2730 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002731 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002732 qd_idx = pd_idx + 1;
2733 if (pd_idx == raid_disks-1) {
2734 (*dd_idx)++; /* Q D D D P */
2735 qd_idx = 0;
2736 } else if (*dd_idx >= pd_idx)
2737 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002738 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002739 break;
2740
2741 case ALGORITHM_ROTATING_N_RESTART:
2742 /* Same a left_asymmetric, by first stripe is
2743 * D D D P Q rather than
2744 * Q D D D P
2745 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002746 stripe2 += 1;
2747 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002748 qd_idx = pd_idx + 1;
2749 if (pd_idx == raid_disks-1) {
2750 (*dd_idx)++; /* Q D D D P */
2751 qd_idx = 0;
2752 } else if (*dd_idx >= pd_idx)
2753 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002754 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002755 break;
2756
2757 case ALGORITHM_ROTATING_N_CONTINUE:
2758 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002759 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002760 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2761 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002762 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002763 break;
2764
2765 case ALGORITHM_LEFT_ASYMMETRIC_6:
2766 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002767 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002768 if (*dd_idx >= pd_idx)
2769 (*dd_idx)++;
2770 qd_idx = raid_disks - 1;
2771 break;
2772
2773 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002774 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002775 if (*dd_idx >= pd_idx)
2776 (*dd_idx)++;
2777 qd_idx = raid_disks - 1;
2778 break;
2779
2780 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002781 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002782 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2783 qd_idx = raid_disks - 1;
2784 break;
2785
2786 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002787 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002788 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2789 qd_idx = raid_disks - 1;
2790 break;
2791
2792 case ALGORITHM_PARITY_0_6:
2793 pd_idx = 0;
2794 (*dd_idx)++;
2795 qd_idx = raid_disks - 1;
2796 break;
2797
NeilBrown16a53ec2006-06-26 00:27:38 -07002798 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002799 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002800 }
2801 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 }
2803
NeilBrown911d4ee2009-03-31 14:39:38 +11002804 if (sh) {
2805 sh->pd_idx = pd_idx;
2806 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002807 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 /*
2810 * Finally, compute the new sector number
2811 */
2812 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2813 return new_sector;
2814}
2815
Shaohua Li6d036f72015-08-13 14:31:57 -07002816sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
NeilBrownd1688a62011-10-11 16:49:52 +11002818 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002819 int raid_disks = sh->disks;
2820 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002822 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2823 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002824 int algorithm = previous ? conf->prev_algo
2825 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 sector_t stripe;
2827 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002828 sector_t chunk_number;
2829 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002831 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2834 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
NeilBrown16a53ec2006-06-26 00:27:38 -07002836 if (i == sh->pd_idx)
2837 return 0;
2838 switch(conf->level) {
2839 case 4: break;
2840 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002841 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 case ALGORITHM_LEFT_ASYMMETRIC:
2843 case ALGORITHM_RIGHT_ASYMMETRIC:
2844 if (i > sh->pd_idx)
2845 i--;
2846 break;
2847 case ALGORITHM_LEFT_SYMMETRIC:
2848 case ALGORITHM_RIGHT_SYMMETRIC:
2849 if (i < sh->pd_idx)
2850 i += raid_disks;
2851 i -= (sh->pd_idx + 1);
2852 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002853 case ALGORITHM_PARITY_0:
2854 i -= 1;
2855 break;
2856 case ALGORITHM_PARITY_N:
2857 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002859 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002860 }
2861 break;
2862 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002863 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002864 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002865 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002866 case ALGORITHM_LEFT_ASYMMETRIC:
2867 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002868 case ALGORITHM_ROTATING_ZERO_RESTART:
2869 case ALGORITHM_ROTATING_N_RESTART:
2870 if (sh->pd_idx == raid_disks-1)
2871 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002872 else if (i > sh->pd_idx)
2873 i -= 2; /* D D P Q D */
2874 break;
2875 case ALGORITHM_LEFT_SYMMETRIC:
2876 case ALGORITHM_RIGHT_SYMMETRIC:
2877 if (sh->pd_idx == raid_disks-1)
2878 i--; /* Q D D D P */
2879 else {
2880 /* D D P Q D */
2881 if (i < sh->pd_idx)
2882 i += raid_disks;
2883 i -= (sh->pd_idx + 2);
2884 }
2885 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002886 case ALGORITHM_PARITY_0:
2887 i -= 2;
2888 break;
2889 case ALGORITHM_PARITY_N:
2890 break;
2891 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002892 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002893 if (sh->pd_idx == 0)
2894 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002895 else {
2896 /* D D Q P D */
2897 if (i < sh->pd_idx)
2898 i += raid_disks;
2899 i -= (sh->pd_idx + 1);
2900 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002901 break;
2902 case ALGORITHM_LEFT_ASYMMETRIC_6:
2903 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2904 if (i > sh->pd_idx)
2905 i--;
2906 break;
2907 case ALGORITHM_LEFT_SYMMETRIC_6:
2908 case ALGORITHM_RIGHT_SYMMETRIC_6:
2909 if (i < sh->pd_idx)
2910 i += data_disks + 1;
2911 i -= (sh->pd_idx + 1);
2912 break;
2913 case ALGORITHM_PARITY_0_6:
2914 i -= 1;
2915 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002916 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002917 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002918 }
2919 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 }
2921
2922 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002923 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
NeilBrown112bf892009-03-31 14:39:38 +11002925 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002926 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002927 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2928 || sh2.qd_idx != sh->qd_idx) {
NeilBrowncc6167b2016-11-02 14:16:50 +11002929 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
2930 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 return 0;
2932 }
2933 return r_sector;
2934}
2935
Song Liu07e83362017-01-23 17:12:58 -08002936/*
2937 * There are cases where we want handle_stripe_dirtying() and
2938 * schedule_reconstruction() to delay towrite to some dev of a stripe.
2939 *
2940 * This function checks whether we want to delay the towrite. Specifically,
2941 * we delay the towrite when:
2942 *
2943 * 1. degraded stripe has a non-overwrite to the missing dev, AND this
2944 * stripe has data in journal (for other devices).
2945 *
2946 * In this case, when reading data for the non-overwrite dev, it is
2947 * necessary to handle complex rmw of write back cache (prexor with
2948 * orig_page, and xor with page). To keep read path simple, we would
2949 * like to flush data in journal to RAID disks first, so complex rmw
2950 * is handled in the write patch (handle_stripe_dirtying).
2951 *
Song Liu39b99582017-01-24 14:08:23 -08002952 * 2. when journal space is critical (R5C_LOG_CRITICAL=1)
2953 *
2954 * It is important to be able to flush all stripes in raid5-cache.
2955 * Therefore, we need reserve some space on the journal device for
2956 * these flushes. If flush operation includes pending writes to the
2957 * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe
2958 * for the flush out. If we exclude these pending writes from flush
2959 * operation, we only need (conf->max_degraded + 1) pages per stripe.
2960 * Therefore, excluding pending writes in these cases enables more
2961 * efficient use of the journal device.
2962 *
2963 * Note: To make sure the stripe makes progress, we only delay
2964 * towrite for stripes with data already in journal (injournal > 0).
2965 * When LOG_CRITICAL, stripes with injournal == 0 will be sent to
2966 * no_space_stripes list.
2967 *
Song Liu07e83362017-01-23 17:12:58 -08002968 */
Song Liu39b99582017-01-24 14:08:23 -08002969static inline bool delay_towrite(struct r5conf *conf,
2970 struct r5dev *dev,
2971 struct stripe_head_state *s)
Song Liu07e83362017-01-23 17:12:58 -08002972{
Song Liu39b99582017-01-24 14:08:23 -08002973 /* case 1 above */
2974 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2975 !test_bit(R5_Insync, &dev->flags) && s->injournal)
2976 return true;
2977 /* case 2 above */
2978 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
2979 s->injournal > 0)
2980 return true;
2981 return false;
Song Liu07e83362017-01-23 17:12:58 -08002982}
2983
Dan Williams600aa102008-06-28 08:32:05 +10002984static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002985schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002986 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002987{
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002988 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002989 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002990 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002991
Dan Williamse33129d2007-01-02 13:52:30 -07002992 if (rcw) {
Song Liu1e6d6902016-11-17 15:24:39 -08002993 /*
2994 * In some cases, handle_stripe_dirtying initially decided to
2995 * run rmw and allocates extra page for prexor. However, rcw is
2996 * cheaper later on. We need to free the extra page now,
2997 * because we won't be able to do that in ops_complete_prexor().
2998 */
2999 r5c_release_extra_page(sh);
Dan Williamse33129d2007-01-02 13:52:30 -07003000
3001 for (i = disks; i--; ) {
3002 struct r5dev *dev = &sh->dev[i];
3003
Song Liu39b99582017-01-24 14:08:23 -08003004 if (dev->towrite && !delay_towrite(conf, dev, s)) {
Dan Williamse33129d2007-01-02 13:52:30 -07003005 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10003006 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003007 if (!expand)
3008 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003009 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003010 } else if (test_bit(R5_InJournal, &dev->flags)) {
3011 set_bit(R5_LOCKED, &dev->flags);
3012 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003013 }
3014 }
NeilBrownce7d3632013-03-04 12:37:14 +11003015 /* if we are not expanding this is a proper write request, and
3016 * there will be bios with new data to be drained into the
3017 * stripe cache
3018 */
3019 if (!expand) {
3020 if (!s->locked)
3021 /* False alarm, nothing to do */
3022 return;
3023 sh->reconstruct_state = reconstruct_state_drain_run;
3024 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3025 } else
3026 sh->reconstruct_state = reconstruct_state_run;
3027
3028 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3029
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003030 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003031 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003032 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07003033 } else {
3034 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
3035 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003036 BUG_ON(level == 6 &&
3037 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
3038 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
Dan Williamse33129d2007-01-02 13:52:30 -07003039
Dan Williamse33129d2007-01-02 13:52:30 -07003040 for (i = disks; i--; ) {
3041 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003042 if (i == pd_idx || i == qd_idx)
Dan Williamse33129d2007-01-02 13:52:30 -07003043 continue;
3044
Dan Williamse33129d2007-01-02 13:52:30 -07003045 if (dev->towrite &&
3046 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10003047 test_bit(R5_Wantcompute, &dev->flags))) {
3048 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003049 set_bit(R5_LOCKED, &dev->flags);
3050 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003051 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003052 } else if (test_bit(R5_InJournal, &dev->flags)) {
3053 set_bit(R5_LOCKED, &dev->flags);
3054 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003055 }
3056 }
NeilBrownce7d3632013-03-04 12:37:14 +11003057 if (!s->locked)
3058 /* False alarm - nothing to do */
3059 return;
3060 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
3061 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
3062 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3063 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003064 }
3065
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003066 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07003067 * are in flight
3068 */
3069 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
3070 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10003071 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003072
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003073 if (level == 6) {
3074 int qd_idx = sh->qd_idx;
3075 struct r5dev *dev = &sh->dev[qd_idx];
3076
3077 set_bit(R5_LOCKED, &dev->flags);
3078 clear_bit(R5_UPTODATE, &dev->flags);
3079 s->locked++;
3080 }
3081
Dan Williams600aa102008-06-28 08:32:05 +10003082 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07003083 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10003084 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003085}
NeilBrown16a53ec2006-06-26 00:27:38 -07003086
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087/*
3088 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07003089 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 * The bi_next chain must be in order.
3091 */
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003092static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
3093 int forwrite, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
3095 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11003096 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07003097 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
NeilBrowncbe47ec2011-07-26 11:20:35 +10003099 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003100 (unsigned long long)bi->bi_iter.bi_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 (unsigned long long)sh->sector);
3102
Shaohua Lib17459c2012-07-19 16:01:31 +10003103 /*
3104 * If several bio share a stripe. The bio bi_phys_segments acts as a
3105 * reference count to avoid race. The reference count should already be
3106 * increased before this function is called (for example, in
Shaohua Li849674e2016-01-20 13:52:20 -08003107 * raid5_make_request()), so other bio sharing this stripe will not free the
Shaohua Lib17459c2012-07-19 16:01:31 +10003108 * stripe. If a stripe is owned by one stripe, the stripe lock will
3109 * protect it.
3110 */
3111 spin_lock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003112 /* Don't allow new IO added to stripes in batch list */
3113 if (sh->batch_head)
3114 goto overlap;
NeilBrown72626682005-09-09 16:23:54 -07003115 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003117 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07003118 firstwrite = 1;
3119 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 bip = &sh->dev[dd_idx].toread;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003121 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
3122 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 goto overlap;
3124 bip = & (*bip)->bi_next;
3125 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07003126 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 goto overlap;
3128
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003129 if (!forwrite || previous)
3130 clear_bit(STRIPE_BATCH_READY, &sh->state);
3131
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02003132 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 if (*bip)
3134 bi->bi_next = *bip;
3135 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003136 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07003137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 if (forwrite) {
3139 /* check if page is covered */
3140 sector_t sector = sh->dev[dd_idx].sector;
3141 for (bi=sh->dev[dd_idx].towrite;
3142 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07003143 bi && bi->bi_iter.bi_sector <= sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07003145 if (bio_end_sector(bi) >= sector)
3146 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 }
3148 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
shli@kernel.org7a87f432014-12-15 12:57:03 +11003149 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
3150 sh->overwrite_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003152
3153 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003154 (unsigned long long)(*bip)->bi_iter.bi_sector,
NeilBrowncbe47ec2011-07-26 11:20:35 +10003155 (unsigned long long)sh->sector, dd_idx);
3156
3157 if (conf->mddev->bitmap && firstwrite) {
NeilBrownd0852df52015-05-27 08:43:45 +10003158 /* Cannot hold spinlock over bitmap_startwrite,
3159 * but must ensure this isn't added to a batch until
3160 * we have added to the bitmap and set bm_seq.
3161 * So set STRIPE_BITMAP_PENDING to prevent
3162 * batching.
3163 * If multiple add_stripe_bio() calls race here they
3164 * much all set STRIPE_BITMAP_PENDING. So only the first one
3165 * to complete "bitmap_startwrite" gets to set
3166 * STRIPE_BIT_DELAY. This is important as once a stripe
3167 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3168 * any more.
3169 */
3170 set_bit(STRIPE_BITMAP_PENDING, &sh->state);
3171 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10003172 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
3173 STRIPE_SECTORS, 0);
NeilBrownd0852df52015-05-27 08:43:45 +10003174 spin_lock_irq(&sh->stripe_lock);
3175 clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
3176 if (!sh->batch_head) {
3177 sh->bm_seq = conf->seq_flush+1;
3178 set_bit(STRIPE_BIT_DELAY, &sh->state);
3179 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003180 }
NeilBrownd0852df52015-05-27 08:43:45 +10003181 spin_unlock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003182
3183 if (stripe_can_batch(sh))
3184 stripe_add_to_batch_list(conf, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 return 1;
3186
3187 overlap:
3188 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10003189 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 return 0;
3191}
3192
NeilBrownd1688a62011-10-11 16:49:52 +11003193static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08003194
NeilBrownd1688a62011-10-11 16:49:52 +11003195static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003196 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08003197{
NeilBrown784052e2009-03-31 15:19:07 +11003198 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10003199 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11003200 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003201 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11003202 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003203
NeilBrown112bf892009-03-31 14:39:38 +11003204 raid5_compute_sector(conf,
3205 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08003206 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11003207 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003208 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003209}
3210
Dan Williamsa4456852007-07-09 11:56:43 -07003211static void
NeilBrownd1688a62011-10-11 16:49:52 +11003212handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003213 struct stripe_head_state *s, int disks,
NeilBrown34a6f802015-08-14 12:07:57 +10003214 struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003215{
3216 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003217 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003218 for (i = disks; i--; ) {
3219 struct bio *bi;
3220 int bitmap_end = 0;
3221
3222 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003223 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07003224 rcu_read_lock();
3225 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownf5b67ae2016-06-02 16:19:53 +10003226 if (rdev && test_bit(In_sync, &rdev->flags) &&
3227 !test_bit(Faulty, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10003228 atomic_inc(&rdev->nr_pending);
3229 else
3230 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003231 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10003232 if (rdev) {
3233 if (!rdev_set_badblocks(
3234 rdev,
3235 sh->sector,
3236 STRIPE_SECTORS, 0))
3237 md_error(conf->mddev, rdev);
3238 rdev_dec_pending(rdev, conf->mddev);
3239 }
Dan Williamsa4456852007-07-09 11:56:43 -07003240 }
Shaohua Lib17459c2012-07-19 16:01:31 +10003241 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003242 /* fail all writes first */
3243 bi = sh->dev[i].towrite;
3244 sh->dev[i].towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11003245 sh->overwrite_disks = 0;
Shaohua Lib17459c2012-07-19 16:01:31 +10003246 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11003247 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003248 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07003249
Shaohua Li0576b1c2015-08-13 14:32:00 -07003250 r5l_stripe_write_finished(sh);
3251
Dan Williamsa4456852007-07-09 11:56:43 -07003252 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3253 wake_up(&conf->wait_for_overlap);
3254
Kent Overstreet4f024f32013-10-11 15:44:27 -07003255 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003256 sh->dev[i].sector + STRIPE_SECTORS) {
3257 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003258
3259 bi->bi_error = -EIO;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003260 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003261 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003262 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003263 }
3264 bi = nextbi;
3265 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003266 if (bitmap_end)
3267 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3268 STRIPE_SECTORS, 0, 0);
3269 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003270 /* and fail all 'written' */
3271 bi = sh->dev[i].written;
3272 sh->dev[i].written = NULL;
Shaohua Lid592a992014-05-21 17:57:44 +08003273 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3274 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3275 sh->dev[i].page = sh->dev[i].orig_page;
3276 }
3277
Dan Williamsa4456852007-07-09 11:56:43 -07003278 if (bi) bitmap_end = 1;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003279 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003280 sh->dev[i].sector + STRIPE_SECTORS) {
3281 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003282
3283 bi->bi_error = -EIO;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003284 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003285 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003286 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003287 }
3288 bi = bi2;
3289 }
3290
Dan Williamsb5e98d62007-01-02 13:52:31 -07003291 /* fail any reads if this device is non-operational and
3292 * the data has not reached the cache yet.
3293 */
3294 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
Shaohua Li6e74a9c2015-10-08 21:54:08 -07003295 s->failed > conf->max_degraded &&
Dan Williamsb5e98d62007-01-02 13:52:31 -07003296 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3297 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11003298 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003299 bi = sh->dev[i].toread;
3300 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11003301 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003302 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3303 wake_up(&conf->wait_for_overlap);
Shaohua Liebda7802015-09-18 10:20:13 -07003304 if (bi)
3305 s->to_read--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003306 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003307 sh->dev[i].sector + STRIPE_SECTORS) {
3308 struct bio *nextbi =
3309 r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003310
3311 bi->bi_error = -EIO;
NeilBrown34a6f802015-08-14 12:07:57 +10003312 if (!raid5_dec_bi_active_stripes(bi))
3313 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003314 bi = nextbi;
3315 }
3316 }
Dan Williamsa4456852007-07-09 11:56:43 -07003317 if (bitmap_end)
3318 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3319 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10003320 /* If we were in the middle of a write the parity block might
3321 * still be locked - so just clear all R5_LOCKED flags
3322 */
3323 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003324 }
Shaohua Liebda7802015-09-18 10:20:13 -07003325 s->to_write = 0;
3326 s->written = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003327
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003328 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3329 if (atomic_dec_and_test(&conf->pending_full_writes))
3330 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003331}
3332
NeilBrown7f0da592011-07-28 11:39:22 +10003333static void
NeilBrownd1688a62011-10-11 16:49:52 +11003334handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10003335 struct stripe_head_state *s)
3336{
3337 int abort = 0;
3338 int i;
3339
shli@kernel.org59fc6302014-12-15 12:57:03 +11003340 BUG_ON(sh->batch_head);
NeilBrown7f0da592011-07-28 11:39:22 +10003341 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003342 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3343 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10003344 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11003345 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10003346 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10003347 * Don't even need to abort as that is handled elsewhere
3348 * if needed, and not always wanted e.g. if there is a known
3349 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11003350 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10003351 * non-sync devices, or abort the recovery
3352 */
NeilBrown18b98372012-04-01 23:48:38 +10003353 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3354 /* During recovery devices cannot be removed, so
3355 * locking and refcounting of rdevs is not needed
3356 */
NeilBrowne50d3992016-06-02 16:19:52 +10003357 rcu_read_lock();
NeilBrown18b98372012-04-01 23:48:38 +10003358 for (i = 0; i < conf->raid_disks; i++) {
NeilBrowne50d3992016-06-02 16:19:52 +10003359 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown18b98372012-04-01 23:48:38 +10003360 if (rdev
3361 && !test_bit(Faulty, &rdev->flags)
3362 && !test_bit(In_sync, &rdev->flags)
3363 && !rdev_set_badblocks(rdev, sh->sector,
3364 STRIPE_SECTORS, 0))
3365 abort = 1;
NeilBrowne50d3992016-06-02 16:19:52 +10003366 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown18b98372012-04-01 23:48:38 +10003367 if (rdev
3368 && !test_bit(Faulty, &rdev->flags)
3369 && !test_bit(In_sync, &rdev->flags)
3370 && !rdev_set_badblocks(rdev, sh->sector,
3371 STRIPE_SECTORS, 0))
3372 abort = 1;
3373 }
NeilBrowne50d3992016-06-02 16:19:52 +10003374 rcu_read_unlock();
NeilBrown18b98372012-04-01 23:48:38 +10003375 if (abort)
3376 conf->recovery_disabled =
3377 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10003378 }
NeilBrown18b98372012-04-01 23:48:38 +10003379 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10003380}
3381
NeilBrown9a3e1102011-12-23 10:17:53 +11003382static int want_replace(struct stripe_head *sh, int disk_idx)
3383{
3384 struct md_rdev *rdev;
3385 int rv = 0;
NeilBrown3f232d62016-06-02 16:19:52 +10003386
3387 rcu_read_lock();
3388 rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
NeilBrown9a3e1102011-12-23 10:17:53 +11003389 if (rdev
3390 && !test_bit(Faulty, &rdev->flags)
3391 && !test_bit(In_sync, &rdev->flags)
3392 && (rdev->recovery_offset <= sh->sector
3393 || rdev->mddev->recovery_cp <= sh->sector))
3394 rv = 1;
NeilBrown3f232d62016-06-02 16:19:52 +10003395 rcu_read_unlock();
NeilBrown9a3e1102011-12-23 10:17:53 +11003396 return rv;
3397}
3398
NeilBrown2c58f062015-02-02 11:32:23 +11003399static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3400 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07003401{
3402 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10003403 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3404 &sh->dev[s->failed_num[1]] };
NeilBrownea664c82015-02-02 14:03:28 +11003405 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07003406
NeilBrowna79cfe12015-02-02 11:37:59 +11003407
3408 if (test_bit(R5_LOCKED, &dev->flags) ||
3409 test_bit(R5_UPTODATE, &dev->flags))
3410 /* No point reading this as we already have it or have
3411 * decided to get it.
3412 */
3413 return 0;
3414
3415 if (dev->toread ||
3416 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3417 /* We need this block to directly satisfy a request */
3418 return 1;
3419
3420 if (s->syncing || s->expanding ||
3421 (s->replacing && want_replace(sh, disk_idx)))
3422 /* When syncing, or expanding we read everything.
3423 * When replacing, we need the replaced block.
3424 */
3425 return 1;
3426
3427 if ((s->failed >= 1 && fdev[0]->toread) ||
3428 (s->failed >= 2 && fdev[1]->toread))
3429 /* If we want to read from a failed device, then
3430 * we need to actually read every other device.
3431 */
3432 return 1;
3433
NeilBrowna9d56952015-02-02 11:49:10 +11003434 /* Sometimes neither read-modify-write nor reconstruct-write
3435 * cycles can work. In those cases we read every block we
3436 * can. Then the parity-update is certain to have enough to
3437 * work with.
3438 * This can only be a problem when we need to write something,
3439 * and some device has failed. If either of those tests
3440 * fail we need look no further.
3441 */
3442 if (!s->failed || !s->to_write)
3443 return 0;
3444
3445 if (test_bit(R5_Insync, &dev->flags) &&
3446 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3447 /* Pre-reads at not permitted until after short delay
3448 * to gather multiple requests. However if this
3449 * device is no Insync, the block could only be be computed
3450 * and there is no need to delay that.
3451 */
3452 return 0;
NeilBrownea664c82015-02-02 14:03:28 +11003453
NeilBrown36707bb2015-09-24 15:25:36 +10003454 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrownea664c82015-02-02 14:03:28 +11003455 if (fdev[i]->towrite &&
3456 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3457 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3458 /* If we have a partial write to a failed
3459 * device, then we will need to reconstruct
3460 * the content of that device, so all other
3461 * devices must be read.
3462 */
3463 return 1;
3464 }
3465
3466 /* If we are forced to do a reconstruct-write, either because
3467 * the current RAID6 implementation only supports that, or
3468 * or because parity cannot be trusted and we are currently
3469 * recovering it, there is extra need to be careful.
3470 * If one of the devices that we would need to read, because
3471 * it is not being overwritten (and maybe not written at all)
3472 * is missing/faulty, then we need to read everything we can.
3473 */
3474 if (sh->raid_conf->level != 6 &&
3475 sh->sector < sh->raid_conf->mddev->recovery_cp)
3476 /* reconstruct-write isn't being forced */
3477 return 0;
NeilBrown36707bb2015-09-24 15:25:36 +10003478 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrown10d82c52015-05-08 18:19:33 +10003479 if (s->failed_num[i] != sh->pd_idx &&
3480 s->failed_num[i] != sh->qd_idx &&
3481 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
NeilBrownea664c82015-02-02 14:03:28 +11003482 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3483 return 1;
3484 }
3485
NeilBrown2c58f062015-02-02 11:32:23 +11003486 return 0;
3487}
3488
Song Liuba026842017-01-12 17:22:42 -08003489/* fetch_block - checks the given member device to see if its data needs
3490 * to be read or computed to satisfy a request.
3491 *
3492 * Returns 1 when no more member devices need to be checked, otherwise returns
3493 * 0 to tell the loop in handle_stripe_fill to continue
3494 */
NeilBrown2c58f062015-02-02 11:32:23 +11003495static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3496 int disk_idx, int disks)
3497{
3498 struct r5dev *dev = &sh->dev[disk_idx];
3499
3500 /* is the data in this block needed, and can we get it? */
3501 if (need_this_block(sh, s, disk_idx, disks)) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003502 /* we would like to get this block, possibly by computing it,
3503 * otherwise read it if the backing disk is insync
3504 */
3505 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3506 BUG_ON(test_bit(R5_Wantread, &dev->flags));
NeilBrownb0c783b2015-05-08 18:19:32 +10003507 BUG_ON(sh->batch_head);
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003508 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10003509 (s->failed && (disk_idx == s->failed_num[0] ||
3510 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003511 /* have disk failed, and we're requested to fetch it;
3512 * do compute it
3513 */
3514 pr_debug("Computing stripe %llu block %d\n",
3515 (unsigned long long)sh->sector, disk_idx);
3516 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3517 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3518 set_bit(R5_Wantcompute, &dev->flags);
3519 sh->ops.target = disk_idx;
3520 sh->ops.target2 = -1; /* no 2nd target */
3521 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10003522 /* Careful: from this point on 'uptodate' is in the eye
3523 * of raid_run_ops which services 'compute' operations
3524 * before writes. R5_Wantcompute flags a block that will
3525 * be R5_UPTODATE by the time it is needed for a
3526 * subsequent operation.
3527 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003528 s->uptodate++;
3529 return 1;
3530 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3531 /* Computing 2-failure is *very* expensive; only
3532 * do it if failed >= 2
3533 */
3534 int other;
3535 for (other = disks; other--; ) {
3536 if (other == disk_idx)
3537 continue;
3538 if (!test_bit(R5_UPTODATE,
3539 &sh->dev[other].flags))
3540 break;
3541 }
3542 BUG_ON(other < 0);
3543 pr_debug("Computing stripe %llu blocks %d,%d\n",
3544 (unsigned long long)sh->sector,
3545 disk_idx, other);
3546 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3547 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3548 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3549 set_bit(R5_Wantcompute, &sh->dev[other].flags);
3550 sh->ops.target = disk_idx;
3551 sh->ops.target2 = other;
3552 s->uptodate += 2;
3553 s->req_compute = 1;
3554 return 1;
3555 } else if (test_bit(R5_Insync, &dev->flags)) {
3556 set_bit(R5_LOCKED, &dev->flags);
3557 set_bit(R5_Wantread, &dev->flags);
3558 s->locked++;
3559 pr_debug("Reading block %d (sync=%d)\n",
3560 disk_idx, s->syncing);
3561 }
3562 }
3563
3564 return 0;
3565}
3566
3567/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10003568 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003569 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10003570static void handle_stripe_fill(struct stripe_head *sh,
3571 struct stripe_head_state *s,
3572 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003573{
3574 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003575
3576 /* look for blocks to read/compute, skip this if a compute
3577 * is already in flight, or if the stripe contents are in the
3578 * midst of changing due to a write
3579 */
3580 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Song Liu07e83362017-01-23 17:12:58 -08003581 !sh->reconstruct_state) {
3582
3583 /*
3584 * For degraded stripe with data in journal, do not handle
3585 * read requests yet, instead, flush the stripe to raid
3586 * disks first, this avoids handling complex rmw of write
3587 * back cache (prexor with orig_page, and then xor with
3588 * page) in the read path
3589 */
3590 if (s->injournal && s->failed) {
3591 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
3592 r5c_make_stripe_write_out(sh);
3593 goto out;
3594 }
3595
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003596 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10003597 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003598 break;
Song Liu07e83362017-01-23 17:12:58 -08003599 }
3600out:
Dan Williamsa4456852007-07-09 11:56:43 -07003601 set_bit(STRIPE_HANDLE, &sh->state);
3602}
3603
NeilBrown787b76f2015-05-21 12:56:41 +10003604static void break_stripe_batch_list(struct stripe_head *head_sh,
3605 unsigned long handle_flags);
Dan Williams1fe797e2008-06-28 09:16:30 +10003606/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07003607 * any written block on an uptodate or failed drive can be returned.
3608 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3609 * never LOCKED, so we don't need to test 'failed' directly.
3610 */
NeilBrownd1688a62011-10-11 16:49:52 +11003611static void handle_stripe_clean_event(struct r5conf *conf,
NeilBrown34a6f802015-08-14 12:07:57 +10003612 struct stripe_head *sh, int disks, struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003613{
3614 int i;
3615 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003616 int discard_pending = 0;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003617 struct stripe_head *head_sh = sh;
3618 bool do_endio = false;
Dan Williamsa4456852007-07-09 11:56:43 -07003619
3620 for (i = disks; i--; )
3621 if (sh->dev[i].written) {
3622 dev = &sh->dev[i];
3623 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003624 (test_bit(R5_UPTODATE, &dev->flags) ||
Shaohua Lid592a992014-05-21 17:57:44 +08003625 test_bit(R5_Discard, &dev->flags) ||
3626 test_bit(R5_SkipCopy, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003627 /* We can return any write requests */
3628 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003629 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003630 if (test_and_clear_bit(R5_Discard, &dev->flags))
3631 clear_bit(R5_UPTODATE, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08003632 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3633 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
Shaohua Lid592a992014-05-21 17:57:44 +08003634 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003635 do_endio = true;
3636
3637returnbi:
3638 dev->page = dev->orig_page;
Dan Williamsa4456852007-07-09 11:56:43 -07003639 wbi = dev->written;
3640 dev->written = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003641 while (wbi && wbi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003642 dev->sector + STRIPE_SECTORS) {
3643 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10003644 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003645 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003646 bio_list_add(return_bi, wbi);
Dan Williamsa4456852007-07-09 11:56:43 -07003647 }
3648 wbi = wbi2;
3649 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003650 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3651 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07003652 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003653 0);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003654 if (head_sh->batch_head) {
3655 sh = list_first_entry(&sh->batch_list,
3656 struct stripe_head,
3657 batch_list);
3658 if (sh != head_sh) {
3659 dev = &sh->dev[i];
3660 goto returnbi;
3661 }
3662 }
3663 sh = head_sh;
3664 dev = &sh->dev[i];
NeilBrownf8dfcff2013-03-12 12:18:06 +11003665 } else if (test_bit(R5_Discard, &dev->flags))
3666 discard_pending = 1;
3667 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -07003668
Shaohua Li0576b1c2015-08-13 14:32:00 -07003669 r5l_stripe_write_finished(sh);
3670
NeilBrownf8dfcff2013-03-12 12:18:06 +11003671 if (!discard_pending &&
3672 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003673 int hash;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003674 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3675 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3676 if (sh->qd_idx >= 0) {
3677 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3678 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3679 }
3680 /* now that discard is done we can proceed with any sync */
3681 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08003682 /*
3683 * SCSI discard will change some bio fields and the stripe has
3684 * no updated data, so remove it from hash list and the stripe
3685 * will be reinitialized
3686 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11003687unhash:
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003688 hash = sh->hash_lock_index;
3689 spin_lock_irq(conf->hash_locks + hash);
Shaohua Lid47648f2013-10-19 14:51:42 +08003690 remove_hash(sh);
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003691 spin_unlock_irq(conf->hash_locks + hash);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003692 if (head_sh->batch_head) {
3693 sh = list_first_entry(&sh->batch_list,
3694 struct stripe_head, batch_list);
3695 if (sh != head_sh)
3696 goto unhash;
3697 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003698 sh = head_sh;
3699
NeilBrownf8dfcff2013-03-12 12:18:06 +11003700 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3701 set_bit(STRIPE_HANDLE, &sh->state);
3702
3703 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003704
3705 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3706 if (atomic_dec_and_test(&conf->pending_full_writes))
3707 md_wakeup_thread(conf->mddev->thread);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003708
NeilBrown787b76f2015-05-21 12:56:41 +10003709 if (head_sh->batch_head && do_endio)
3710 break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
Dan Williamsa4456852007-07-09 11:56:43 -07003711}
3712
Song Liu86aa1392017-01-12 17:22:41 -08003713/*
3714 * For RMW in write back cache, we need extra page in prexor to store the
3715 * old data. This page is stored in dev->orig_page.
3716 *
3717 * This function checks whether we have data for prexor. The exact logic
3718 * is:
3719 * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
3720 */
3721static inline bool uptodate_for_rmw(struct r5dev *dev)
3722{
3723 return (test_bit(R5_UPTODATE, &dev->flags)) &&
3724 (!test_bit(R5_InJournal, &dev->flags) ||
3725 test_bit(R5_OrigPageUPTDODATE, &dev->flags));
3726}
3727
Song Liud7bd3982016-11-23 22:50:39 -08003728static int handle_stripe_dirtying(struct r5conf *conf,
3729 struct stripe_head *sh,
3730 struct stripe_head_state *s,
3731 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003732{
3733 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003734 sector_t recovery_cp = conf->mddev->recovery_cp;
3735
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003736 /* Check whether resync is now happening or should start.
Alexander Lyakasa7854482012-10-11 13:50:12 +11003737 * If yes, then the array is dirty (after unclean shutdown or
3738 * initial creation), so parity in some stripes might be inconsistent.
3739 * In this case, we need to always do reconstruct-write, to ensure
3740 * that in case of drive failure or read-error correction, we
3741 * generate correct data from the parity.
3742 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003743 if (conf->rmw_level == PARITY_DISABLE_RMW ||
NeilBrown26ac1072015-02-18 11:35:14 +11003744 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
3745 s->failed == 0)) {
Alexander Lyakasa7854482012-10-11 13:50:12 +11003746 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10003747 * look like rcw is cheaper
3748 */
3749 rcw = 1; rmw = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003750 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
3751 conf->rmw_level, (unsigned long long)recovery_cp,
Alexander Lyakasa7854482012-10-11 13:50:12 +11003752 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10003753 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003754 /* would I have to read this buffer for read_modify_write */
3755 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08003756 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu07e83362017-01-23 17:12:58 -08003757 i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08003758 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003759 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08003760 !(uptodate_for_rmw(dev) ||
Dan Williamsf38e1212007-01-02 13:52:30 -07003761 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003762 if (test_bit(R5_Insync, &dev->flags))
3763 rmw++;
3764 else
3765 rmw += 2*disks; /* cannot read it */
3766 }
3767 /* Would I have to read this buffer for reconstruct_write */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003768 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3769 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003770 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003771 !(test_bit(R5_UPTODATE, &dev->flags) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003772 test_bit(R5_Wantcompute, &dev->flags))) {
NeilBrown67f45542014-05-28 13:39:22 +10003773 if (test_bit(R5_Insync, &dev->flags))
3774 rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07003775 else
3776 rcw += 2*disks;
3777 }
3778 }
Song Liu1e6d6902016-11-17 15:24:39 -08003779
Song Liu39b99582017-01-24 14:08:23 -08003780 pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n",
3781 (unsigned long long)sh->sector, sh->state, rmw, rcw);
Dan Williamsa4456852007-07-09 11:56:43 -07003782 set_bit(STRIPE_HANDLE, &sh->state);
Song Liu41257582016-05-23 17:25:06 -07003783 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003784 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003785 if (conf->mddev->queue)
3786 blk_add_trace_msg(conf->mddev->queue,
3787 "raid5 rmw %llu %d",
3788 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07003789 for (i = disks; i--; ) {
3790 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08003791 if (test_bit(R5_InJournal, &dev->flags) &&
3792 dev->page == dev->orig_page &&
3793 !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) {
3794 /* alloc page for prexor */
Song Liud7bd3982016-11-23 22:50:39 -08003795 struct page *p = alloc_page(GFP_NOIO);
Song Liu1e6d6902016-11-17 15:24:39 -08003796
Song Liud7bd3982016-11-23 22:50:39 -08003797 if (p) {
3798 dev->orig_page = p;
3799 continue;
3800 }
3801
3802 /*
3803 * alloc_page() failed, try use
3804 * disk_info->extra_page
3805 */
3806 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE,
3807 &conf->cache_state)) {
3808 r5c_use_extra_page(sh);
3809 break;
3810 }
3811
3812 /* extra_page in use, add to delayed_list */
3813 set_bit(STRIPE_DELAYED, &sh->state);
3814 s->waiting_extra_page = 1;
3815 return -EAGAIN;
Song Liu1e6d6902016-11-17 15:24:39 -08003816 }
Song Liud7bd3982016-11-23 22:50:39 -08003817 }
Song Liu1e6d6902016-11-17 15:24:39 -08003818
Song Liud7bd3982016-11-23 22:50:39 -08003819 for (i = disks; i--; ) {
3820 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08003821 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003822 i == sh->pd_idx || i == sh->qd_idx ||
3823 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003824 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08003825 !(uptodate_for_rmw(dev) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003826 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003827 test_bit(R5_Insync, &dev->flags)) {
NeilBrown67f45542014-05-28 13:39:22 +10003828 if (test_bit(STRIPE_PREREAD_ACTIVE,
3829 &sh->state)) {
3830 pr_debug("Read_old block %d for r-m-w\n",
3831 i);
Dan Williamsa4456852007-07-09 11:56:43 -07003832 set_bit(R5_LOCKED, &dev->flags);
3833 set_bit(R5_Wantread, &dev->flags);
3834 s->locked++;
3835 } else {
3836 set_bit(STRIPE_DELAYED, &sh->state);
3837 set_bit(STRIPE_HANDLE, &sh->state);
3838 }
3839 }
3840 }
NeilBrowna9add5d2012-10-31 11:59:09 +11003841 }
Song Liu41257582016-05-23 17:25:06 -07003842 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003843 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11003844 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10003845 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003846 for (i = disks; i--; ) {
3847 struct r5dev *dev = &sh->dev[i];
3848 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10003849 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003850 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003851 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10003852 test_bit(R5_Wantcompute, &dev->flags))) {
3853 rcw++;
NeilBrown67f45542014-05-28 13:39:22 +10003854 if (test_bit(R5_Insync, &dev->flags) &&
3855 test_bit(STRIPE_PREREAD_ACTIVE,
3856 &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07003857 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07003858 "%d for Reconstruct\n", i);
3859 set_bit(R5_LOCKED, &dev->flags);
3860 set_bit(R5_Wantread, &dev->flags);
3861 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11003862 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07003863 } else {
3864 set_bit(STRIPE_DELAYED, &sh->state);
3865 set_bit(STRIPE_HANDLE, &sh->state);
3866 }
3867 }
3868 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003869 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11003870 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3871 (unsigned long long)sh->sector,
3872 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10003873 }
NeilBrownb1b02fe2015-02-02 10:44:29 +11003874
3875 if (rcw > disks && rmw > disks &&
3876 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3877 set_bit(STRIPE_DELAYED, &sh->state);
3878
Dan Williamsa4456852007-07-09 11:56:43 -07003879 /* now if nothing is locked, and if we have enough data,
3880 * we can start a write request
3881 */
Dan Williamsf38e1212007-01-02 13:52:30 -07003882 /* since handle_stripe can be called at any time we need to handle the
3883 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07003884 * subsequent call wants to start a write request. raid_run_ops only
3885 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07003886 * simultaneously. If this is not the case then new writes need to be
3887 * held off until the compute completes.
3888 */
Dan Williams976ea8d2008-06-28 08:32:03 +10003889 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3890 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
Song Liu1e6d6902016-11-17 15:24:39 -08003891 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003892 schedule_reconstruction(sh, s, rcw == 0, 0);
Song Liud7bd3982016-11-23 22:50:39 -08003893 return 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003894}
3895
NeilBrownd1688a62011-10-11 16:49:52 +11003896static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003897 struct stripe_head_state *s, int disks)
3898{
Dan Williamsecc65c92008-06-28 08:31:57 +10003899 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07003900
shli@kernel.org59fc6302014-12-15 12:57:03 +11003901 BUG_ON(sh->batch_head);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003902 set_bit(STRIPE_HANDLE, &sh->state);
3903
Dan Williamsecc65c92008-06-28 08:31:57 +10003904 switch (sh->check_state) {
3905 case check_state_idle:
3906 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07003907 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07003908 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10003909 sh->check_state = check_state_run;
3910 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003911 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003912 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10003913 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07003914 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003915 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10003916 /* fall through */
3917 case check_state_compute_result:
3918 sh->check_state = check_state_idle;
3919 if (!dev)
3920 dev = &sh->dev[sh->pd_idx];
3921
3922 /* check that a write has not made the stripe insync */
3923 if (test_bit(STRIPE_INSYNC, &sh->state))
3924 break;
3925
3926 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07003927 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3928 BUG_ON(s->uptodate != disks);
3929
3930 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10003931 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003932 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003933
Dan Williamsa4456852007-07-09 11:56:43 -07003934 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003935 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003936 break;
3937 case check_state_run:
3938 break; /* we will be called again upon completion */
3939 case check_state_check_result:
3940 sh->check_state = check_state_idle;
3941
3942 /* if a failure occurred during the check operation, leave
3943 * STRIPE_INSYNC not set and let the stripe be handled again
3944 */
3945 if (s->failed)
3946 break;
3947
3948 /* handle a successful check operation, if parity is correct
3949 * we are done. Otherwise update the mismatch count and repair
3950 * parity if !MD_RECOVERY_CHECK
3951 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003952 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003953 /* parity is correct (on disc,
3954 * not in buffer any more)
3955 */
3956 set_bit(STRIPE_INSYNC, &sh->state);
3957 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003958 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003959 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3960 /* don't try to repair!! */
3961 set_bit(STRIPE_INSYNC, &sh->state);
3962 else {
3963 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003964 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003965 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3966 set_bit(R5_Wantcompute,
3967 &sh->dev[sh->pd_idx].flags);
3968 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003969 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003970 s->uptodate++;
3971 }
3972 }
3973 break;
3974 case check_state_compute_run:
3975 break;
3976 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11003977 pr_err("%s: unknown check_state: %d sector: %llu\n",
Dan Williamsecc65c92008-06-28 08:31:57 +10003978 __func__, sh->check_state,
3979 (unsigned long long) sh->sector);
3980 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003981 }
3982}
3983
NeilBrownd1688a62011-10-11 16:49:52 +11003984static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003985 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003986 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003987{
Dan Williamsa4456852007-07-09 11:56:43 -07003988 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003989 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003990 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003991
shli@kernel.org59fc6302014-12-15 12:57:03 +11003992 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003993 set_bit(STRIPE_HANDLE, &sh->state);
3994
3995 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003996
Dan Williamsa4456852007-07-09 11:56:43 -07003997 /* Want to check and possibly repair P and Q.
3998 * However there could be one 'failed' device, in which
3999 * case we can only check one of them, possibly using the
4000 * other to generate missing data
4001 */
4002
Dan Williamsd82dfee2009-07-14 13:40:57 -07004003 switch (sh->check_state) {
4004 case check_state_idle:
4005 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10004006 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004007 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07004008 * makes sense to check P (If anything else were failed,
4009 * we would have used P to recreate it).
4010 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004011 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07004012 }
NeilBrownf2b3b442011-07-26 11:35:19 +10004013 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004014 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07004015 * anything, so it makes sense to check it
4016 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004017 if (sh->check_state == check_state_run)
4018 sh->check_state = check_state_run_pq;
4019 else
4020 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07004021 }
Dan Williams36d1c642009-07-14 11:48:22 -07004022
Dan Williamsd82dfee2009-07-14 13:40:57 -07004023 /* discard potentially stale zero_sum_result */
4024 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07004025
Dan Williamsd82dfee2009-07-14 13:40:57 -07004026 if (sh->check_state == check_state_run) {
4027 /* async_xor_zero_sum destroys the contents of P */
4028 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
4029 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07004030 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004031 if (sh->check_state >= check_state_run &&
4032 sh->check_state <= check_state_run_pq) {
4033 /* async_syndrome_zero_sum preserves P and Q, so
4034 * no need to mark them !uptodate here
4035 */
4036 set_bit(STRIPE_OP_CHECK, &s->ops_request);
4037 break;
4038 }
Dan Williams36d1c642009-07-14 11:48:22 -07004039
Dan Williamsd82dfee2009-07-14 13:40:57 -07004040 /* we have 2-disk failure */
4041 BUG_ON(s->failed != 2);
4042 /* fall through */
4043 case check_state_compute_result:
4044 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07004045
Dan Williamsd82dfee2009-07-14 13:40:57 -07004046 /* check that a write has not made the stripe insync */
4047 if (test_bit(STRIPE_INSYNC, &sh->state))
4048 break;
Dan Williamsa4456852007-07-09 11:56:43 -07004049
4050 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07004051 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07004052 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004053 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07004054 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004055 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07004056 s->locked++;
4057 set_bit(R5_LOCKED, &dev->flags);
4058 set_bit(R5_Wantwrite, &dev->flags);
4059 }
4060 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004061 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07004062 s->locked++;
4063 set_bit(R5_LOCKED, &dev->flags);
4064 set_bit(R5_Wantwrite, &dev->flags);
4065 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004066 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004067 dev = &sh->dev[pd_idx];
4068 s->locked++;
4069 set_bit(R5_LOCKED, &dev->flags);
4070 set_bit(R5_Wantwrite, &dev->flags);
4071 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004072 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004073 dev = &sh->dev[qd_idx];
4074 s->locked++;
4075 set_bit(R5_LOCKED, &dev->flags);
4076 set_bit(R5_Wantwrite, &dev->flags);
4077 }
4078 clear_bit(STRIPE_DEGRADED, &sh->state);
4079
4080 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004081 break;
4082 case check_state_run:
4083 case check_state_run_q:
4084 case check_state_run_pq:
4085 break; /* we will be called again upon completion */
4086 case check_state_check_result:
4087 sh->check_state = check_state_idle;
4088
4089 /* handle a successful check operation, if parity is correct
4090 * we are done. Otherwise update the mismatch count and repair
4091 * parity if !MD_RECOVERY_CHECK
4092 */
4093 if (sh->ops.zero_sum_result == 0) {
4094 /* both parities are correct */
4095 if (!s->failed)
4096 set_bit(STRIPE_INSYNC, &sh->state);
4097 else {
4098 /* in contrast to the raid5 case we can validate
4099 * parity, but still have a failure to write
4100 * back
4101 */
4102 sh->check_state = check_state_compute_result;
4103 /* Returning at this point means that we may go
4104 * off and bring p and/or q uptodate again so
4105 * we make sure to check zero_sum_result again
4106 * to verify if p or q need writeback
4107 */
4108 }
4109 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004110 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004111 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
4112 /* don't try to repair!! */
4113 set_bit(STRIPE_INSYNC, &sh->state);
4114 else {
4115 int *target = &sh->ops.target;
4116
4117 sh->ops.target = -1;
4118 sh->ops.target2 = -1;
4119 sh->check_state = check_state_compute_run;
4120 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4121 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4122 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4123 set_bit(R5_Wantcompute,
4124 &sh->dev[pd_idx].flags);
4125 *target = pd_idx;
4126 target = &sh->ops.target2;
4127 s->uptodate++;
4128 }
4129 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4130 set_bit(R5_Wantcompute,
4131 &sh->dev[qd_idx].flags);
4132 *target = qd_idx;
4133 s->uptodate++;
4134 }
4135 }
4136 }
4137 break;
4138 case check_state_compute_run:
4139 break;
4140 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11004141 pr_warn("%s: unknown check_state: %d sector: %llu\n",
4142 __func__, sh->check_state,
4143 (unsigned long long) sh->sector);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004144 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07004145 }
4146}
4147
NeilBrownd1688a62011-10-11 16:49:52 +11004148static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07004149{
4150 int i;
4151
4152 /* We have read all the blocks in this stripe and now we need to
4153 * copy some of them into a target stripe for expand.
4154 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07004155 struct dma_async_tx_descriptor *tx = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +11004156 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07004157 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4158 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11004159 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11004160 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07004161 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07004162 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07004163
Shaohua Li6d036f72015-08-13 14:31:57 -07004164 sector_t bn = raid5_compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11004165 sector_t s = raid5_compute_sector(conf, bn, 0,
4166 &dd_idx, NULL);
Shaohua Li6d036f72015-08-13 14:31:57 -07004167 sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07004168 if (sh2 == NULL)
4169 /* so far only the early blocks of this stripe
4170 * have been requested. When later blocks
4171 * get requested, we will try again
4172 */
4173 continue;
4174 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
4175 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
4176 /* must have already done this block */
Shaohua Li6d036f72015-08-13 14:31:57 -07004177 raid5_release_stripe(sh2);
Dan Williamsa4456852007-07-09 11:56:43 -07004178 continue;
4179 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07004180
4181 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07004182 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004183 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07004184 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07004185 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004186
Dan Williamsa4456852007-07-09 11:56:43 -07004187 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
4188 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
4189 for (j = 0; j < conf->raid_disks; j++)
4190 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10004191 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07004192 !test_bit(R5_Expanded, &sh2->dev[j].flags))
4193 break;
4194 if (j == conf->raid_disks) {
4195 set_bit(STRIPE_EXPAND_READY, &sh2->state);
4196 set_bit(STRIPE_HANDLE, &sh2->state);
4197 }
Shaohua Li6d036f72015-08-13 14:31:57 -07004198 raid5_release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004199
Dan Williamsa4456852007-07-09 11:56:43 -07004200 }
NeilBrowna2e08552007-09-11 15:23:36 -07004201 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11004202 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07004203}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
4205/*
4206 * handle_stripe - do things to a stripe.
4207 *
NeilBrown9a3e1102011-12-23 10:17:53 +11004208 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4209 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11004211 * return some read requests which now have data
4212 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 * schedule a read on some buffers
4214 * schedule a write of some buffers
4215 * return confirmation of parity correctness
4216 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 */
Dan Williamsa4456852007-07-09 11:56:43 -07004218
NeilBrownacfe7262011-07-27 11:00:36 +10004219static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07004220{
NeilBrownd1688a62011-10-11 16:49:52 +11004221 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08004222 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004223 struct r5dev *dev;
4224 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11004225 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07004226
NeilBrownacfe7262011-07-27 11:00:36 +10004227 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07004228
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004229 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
4230 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
NeilBrownacfe7262011-07-27 11:00:36 +10004231 s->failed_num[0] = -1;
4232 s->failed_num[1] = -1;
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004233 s->log_failed = r5l_log_disk_error(conf);
NeilBrownacfe7262011-07-27 11:00:36 +10004234
4235 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07004236 rcu_read_lock();
4237 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004238 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10004239 sector_t first_bad;
4240 int bad_sectors;
4241 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10004242
NeilBrown16a53ec2006-06-26 00:27:38 -07004243 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07004244
Dan Williams45b42332007-07-09 11:56:43 -07004245 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11004246 i, dev->flags,
4247 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004248 /* maybe we can reply to a read
4249 *
4250 * new wantfill requests are only permitted while
4251 * ops_complete_biofill is guaranteed to be inactive
4252 */
4253 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4254 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4255 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07004256
4257 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10004258 if (test_bit(R5_LOCKED, &dev->flags))
4259 s->locked++;
4260 if (test_bit(R5_UPTODATE, &dev->flags))
4261 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004262 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004263 s->compute++;
4264 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004265 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004266
NeilBrownacfe7262011-07-27 11:00:36 +10004267 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004268 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10004269 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10004270 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004271 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10004272 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004273 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004274 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004275 }
Dan Williamsa4456852007-07-09 11:56:43 -07004276 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10004277 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11004278 /* Prefer to use the replacement for reads, but only
4279 * if it is recovered enough and has no bad blocks.
4280 */
4281 rdev = rcu_dereference(conf->disks[i].replacement);
4282 if (rdev && !test_bit(Faulty, &rdev->flags) &&
4283 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
4284 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4285 &first_bad, &bad_sectors))
4286 set_bit(R5_ReadRepl, &dev->flags);
4287 else {
NeilBrowne6030cb2015-07-17 13:26:23 +10004288 if (rdev && !test_bit(Faulty, &rdev->flags))
NeilBrown9a3e1102011-12-23 10:17:53 +11004289 set_bit(R5_NeedReplace, &dev->flags);
NeilBrowne6030cb2015-07-17 13:26:23 +10004290 else
4291 clear_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11004292 rdev = rcu_dereference(conf->disks[i].rdev);
4293 clear_bit(R5_ReadRepl, &dev->flags);
4294 }
NeilBrown9283d8c2011-12-08 16:27:57 +11004295 if (rdev && test_bit(Faulty, &rdev->flags))
4296 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10004297 if (rdev) {
4298 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4299 &first_bad, &bad_sectors);
4300 if (s->blocked_rdev == NULL
4301 && (test_bit(Blocked, &rdev->flags)
4302 || is_bad < 0)) {
4303 if (is_bad < 0)
4304 set_bit(BlockedBadBlocks,
4305 &rdev->flags);
4306 s->blocked_rdev = rdev;
4307 atomic_inc(&rdev->nr_pending);
4308 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004309 }
NeilBrown415e72d2010-06-17 17:25:21 +10004310 clear_bit(R5_Insync, &dev->flags);
4311 if (!rdev)
4312 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10004313 else if (is_bad) {
4314 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10004315 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4316 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10004317 /* treat as in-sync, but with a read error
4318 * which we can now try to correct
4319 */
4320 set_bit(R5_Insync, &dev->flags);
4321 set_bit(R5_ReadError, &dev->flags);
4322 }
4323 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10004324 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11004325 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10004326 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11004327 set_bit(R5_Insync, &dev->flags);
4328 else if (test_bit(R5_UPTODATE, &dev->flags) &&
4329 test_bit(R5_Expanded, &dev->flags))
4330 /* If we've reshaped into here, we assume it is Insync.
4331 * We will shortly update recovery_offset to make
4332 * it official.
4333 */
4334 set_bit(R5_Insync, &dev->flags);
4335
NeilBrown1cc03eb2014-01-06 13:19:42 +11004336 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004337 /* This flag does not apply to '.replacement'
4338 * only to .rdev, so make sure to check that*/
4339 struct md_rdev *rdev2 = rcu_dereference(
4340 conf->disks[i].rdev);
4341 if (rdev2 == rdev)
4342 clear_bit(R5_Insync, &dev->flags);
4343 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004344 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004345 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10004346 } else
4347 clear_bit(R5_WriteError, &dev->flags);
4348 }
NeilBrown1cc03eb2014-01-06 13:19:42 +11004349 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004350 /* This flag does not apply to '.replacement'
4351 * only to .rdev, so make sure to check that*/
4352 struct md_rdev *rdev2 = rcu_dereference(
4353 conf->disks[i].rdev);
4354 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10004355 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004356 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10004357 } else
4358 clear_bit(R5_MadeGood, &dev->flags);
4359 }
NeilBrown977df362011-12-23 10:17:53 +11004360 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4361 struct md_rdev *rdev2 = rcu_dereference(
4362 conf->disks[i].replacement);
4363 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4364 s->handle_bad_blocks = 1;
4365 atomic_inc(&rdev2->nr_pending);
4366 } else
4367 clear_bit(R5_MadeGoodRepl, &dev->flags);
4368 }
NeilBrown415e72d2010-06-17 17:25:21 +10004369 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004370 /* The ReadError flag will just be confusing now */
4371 clear_bit(R5_ReadError, &dev->flags);
4372 clear_bit(R5_ReWrite, &dev->flags);
4373 }
NeilBrown415e72d2010-06-17 17:25:21 +10004374 if (test_bit(R5_ReadError, &dev->flags))
4375 clear_bit(R5_Insync, &dev->flags);
4376 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004377 if (s->failed < 2)
4378 s->failed_num[s->failed] = i;
4379 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11004380 if (rdev && !test_bit(Faulty, &rdev->flags))
4381 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10004382 }
Song Liu2ded3702016-11-17 15:24:38 -08004383
4384 if (test_bit(R5_InJournal, &dev->flags))
4385 s->injournal++;
Song Liu1e6d6902016-11-17 15:24:39 -08004386 if (test_bit(R5_InJournal, &dev->flags) && dev->written)
4387 s->just_cached++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004388 }
NeilBrown9a3e1102011-12-23 10:17:53 +11004389 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4390 /* If there is a failed device being replaced,
4391 * we must be recovering.
4392 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10004393 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11004394 * else we can only be replacing
4395 * sync and recovery both need to read all devices, and so
4396 * use the same flag.
4397 */
4398 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10004399 sh->sector >= conf->mddev->recovery_cp ||
4400 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11004401 s->syncing = 1;
4402 else
4403 s->replacing = 1;
4404 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004405 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10004406}
NeilBrownf4168852007-02-28 20:11:53 -08004407
shli@kernel.org59fc6302014-12-15 12:57:03 +11004408static int clear_batch_ready(struct stripe_head *sh)
4409{
NeilBrownb15a9db2015-05-22 15:20:04 +10004410 /* Return '1' if this is a member of batch, or
4411 * '0' if it is a lone stripe or a head which can now be
4412 * handled.
4413 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11004414 struct stripe_head *tmp;
4415 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
NeilBrownb15a9db2015-05-22 15:20:04 +10004416 return (sh->batch_head && sh->batch_head != sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11004417 spin_lock(&sh->stripe_lock);
4418 if (!sh->batch_head) {
4419 spin_unlock(&sh->stripe_lock);
4420 return 0;
4421 }
4422
4423 /*
4424 * this stripe could be added to a batch list before we check
4425 * BATCH_READY, skips it
4426 */
4427 if (sh->batch_head != sh) {
4428 spin_unlock(&sh->stripe_lock);
4429 return 1;
4430 }
4431 spin_lock(&sh->batch_lock);
4432 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4433 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4434 spin_unlock(&sh->batch_lock);
4435 spin_unlock(&sh->stripe_lock);
4436
4437 /*
4438 * BATCH_READY is cleared, no new stripes can be added.
4439 * batch_list can be accessed without lock
4440 */
4441 return 0;
4442}
4443
NeilBrown3960ce72015-05-21 12:20:36 +10004444static void break_stripe_batch_list(struct stripe_head *head_sh,
4445 unsigned long handle_flags)
shli@kernel.org72ac7332014-12-15 12:57:03 +11004446{
NeilBrown4e3d62f2015-05-21 11:50:16 +10004447 struct stripe_head *sh, *next;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004448 int i;
NeilBrownfb642b92015-05-21 12:00:47 +10004449 int do_wakeup = 0;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004450
NeilBrownbb270512015-05-08 18:19:40 +10004451 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4452
shli@kernel.org72ac7332014-12-15 12:57:03 +11004453 list_del_init(&sh->batch_list);
4454
Shaohua Lifb3229d2016-03-09 10:08:38 -08004455 WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
NeilBrown1b956f72015-05-21 12:40:26 +10004456 (1 << STRIPE_SYNCING) |
4457 (1 << STRIPE_REPLACED) |
NeilBrown1b956f72015-05-21 12:40:26 +10004458 (1 << STRIPE_DELAYED) |
4459 (1 << STRIPE_BIT_DELAY) |
4460 (1 << STRIPE_FULL_WRITE) |
4461 (1 << STRIPE_BIOFILL_RUN) |
4462 (1 << STRIPE_COMPUTE_RUN) |
4463 (1 << STRIPE_OPS_REQ_PENDING) |
4464 (1 << STRIPE_DISCARD) |
4465 (1 << STRIPE_BATCH_READY) |
4466 (1 << STRIPE_BATCH_ERR) |
Shaohua Lifb3229d2016-03-09 10:08:38 -08004467 (1 << STRIPE_BITMAP_PENDING)),
4468 "stripe state: %lx\n", sh->state);
4469 WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4470 (1 << STRIPE_REPLACED)),
4471 "head stripe state: %lx\n", head_sh->state);
NeilBrown1b956f72015-05-21 12:40:26 +10004472
4473 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
NeilBrown550da242016-03-09 12:58:25 +11004474 (1 << STRIPE_PREREAD_ACTIVE) |
NeilBrown1b956f72015-05-21 12:40:26 +10004475 (1 << STRIPE_DEGRADED)),
4476 head_sh->state & (1 << STRIPE_INSYNC));
4477
shli@kernel.org72ac7332014-12-15 12:57:03 +11004478 sh->check_state = head_sh->check_state;
4479 sh->reconstruct_state = head_sh->reconstruct_state;
NeilBrownfb642b92015-05-21 12:00:47 +10004480 for (i = 0; i < sh->disks; i++) {
4481 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4482 do_wakeup = 1;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004483 sh->dev[i].flags = head_sh->dev[i].flags &
4484 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
NeilBrownfb642b92015-05-21 12:00:47 +10004485 }
shli@kernel.org72ac7332014-12-15 12:57:03 +11004486 spin_lock_irq(&sh->stripe_lock);
4487 sh->batch_head = NULL;
4488 spin_unlock_irq(&sh->stripe_lock);
NeilBrown3960ce72015-05-21 12:20:36 +10004489 if (handle_flags == 0 ||
4490 sh->state & handle_flags)
4491 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07004492 raid5_release_stripe(sh);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004493 }
NeilBrownfb642b92015-05-21 12:00:47 +10004494 spin_lock_irq(&head_sh->stripe_lock);
4495 head_sh->batch_head = NULL;
4496 spin_unlock_irq(&head_sh->stripe_lock);
4497 for (i = 0; i < head_sh->disks; i++)
4498 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4499 do_wakeup = 1;
NeilBrown3960ce72015-05-21 12:20:36 +10004500 if (head_sh->state & handle_flags)
4501 set_bit(STRIPE_HANDLE, &head_sh->state);
NeilBrownfb642b92015-05-21 12:00:47 +10004502
4503 if (do_wakeup)
4504 wake_up(&head_sh->raid_conf->wait_for_overlap);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004505}
4506
NeilBrowncc940152011-07-26 11:35:35 +10004507static void handle_stripe(struct stripe_head *sh)
4508{
4509 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11004510 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10004511 int i;
NeilBrown84789552011-07-27 11:00:36 +10004512 int prexor;
4513 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004514 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10004515
4516 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11004517 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10004518 /* already being handled, ensure it gets handled
4519 * again when current action finishes */
4520 set_bit(STRIPE_HANDLE, &sh->state);
4521 return;
4522 }
4523
shli@kernel.org59fc6302014-12-15 12:57:03 +11004524 if (clear_batch_ready(sh) ) {
4525 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
4526 return;
4527 }
4528
NeilBrown4e3d62f2015-05-21 11:50:16 +10004529 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
NeilBrown3960ce72015-05-21 12:20:36 +10004530 break_stripe_batch_list(sh, 0);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004531
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004532 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
NeilBrownf8dfcff2013-03-12 12:18:06 +11004533 spin_lock(&sh->stripe_lock);
4534 /* Cannot process 'sync' concurrently with 'discard' */
4535 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
4536 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4537 set_bit(STRIPE_SYNCING, &sh->state);
4538 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10004539 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004540 }
4541 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10004542 }
4543 clear_bit(STRIPE_DELAYED, &sh->state);
4544
4545 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4546 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4547 (unsigned long long)sh->sector, sh->state,
4548 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4549 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10004550
NeilBrownacfe7262011-07-27 11:00:36 +10004551 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10004552
Shaohua Lib70abcb2015-08-13 14:31:58 -07004553 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
4554 goto finish;
4555
NeilBrownbc2607f2011-07-28 11:39:22 +10004556 if (s.handle_bad_blocks) {
4557 set_bit(STRIPE_HANDLE, &sh->state);
4558 goto finish;
4559 }
4560
NeilBrown474af965fe2011-07-27 11:00:36 +10004561 if (unlikely(s.blocked_rdev)) {
4562 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11004563 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10004564 set_bit(STRIPE_HANDLE, &sh->state);
4565 goto finish;
4566 }
4567 /* There is nothing for the blocked_rdev to block */
4568 rdev_dec_pending(s.blocked_rdev, conf->mddev);
4569 s.blocked_rdev = NULL;
4570 }
4571
4572 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4573 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4574 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4575 }
4576
4577 pr_debug("locked=%d uptodate=%d to_read=%d"
4578 " to_write=%d failed=%d failed_num=%d,%d\n",
4579 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4580 s.failed_num[0], s.failed_num[1]);
4581 /* check if the array has lost more than max_degraded devices and,
4582 * if so, some requests might need to be failed.
4583 */
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004584 if (s.failed > conf->max_degraded || s.log_failed) {
NeilBrown9a3f5302011-11-08 16:22:01 +11004585 sh->check_state = 0;
4586 sh->reconstruct_state = 0;
NeilBrown626f2092015-05-22 14:03:10 +10004587 break_stripe_batch_list(sh, 0);
NeilBrown9a3f5302011-11-08 16:22:01 +11004588 if (s.to_read+s.to_write+s.written)
4589 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11004590 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11004591 handle_failed_sync(conf, sh, &s);
4592 }
NeilBrown474af965fe2011-07-27 11:00:36 +10004593
NeilBrown84789552011-07-27 11:00:36 +10004594 /* Now we check to see if any write operations have recently
4595 * completed
4596 */
4597 prexor = 0;
4598 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4599 prexor = 1;
4600 if (sh->reconstruct_state == reconstruct_state_drain_result ||
4601 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
4602 sh->reconstruct_state = reconstruct_state_idle;
4603
4604 /* All the 'written' buffers and the parity block are ready to
4605 * be written back to disk
4606 */
Shaohua Li9e4447682012-10-11 13:49:49 +11004607 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
4608 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004609 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11004610 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
4611 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004612 for (i = disks; i--; ) {
4613 struct r5dev *dev = &sh->dev[i];
4614 if (test_bit(R5_LOCKED, &dev->flags) &&
4615 (i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08004616 dev->written || test_bit(R5_InJournal,
4617 &dev->flags))) {
NeilBrown84789552011-07-27 11:00:36 +10004618 pr_debug("Writing block %d\n", i);
4619 set_bit(R5_Wantwrite, &dev->flags);
4620 if (prexor)
4621 continue;
NeilBrown9c4bdf62014-08-13 09:57:07 +10004622 if (s.failed > 1)
4623 continue;
NeilBrown84789552011-07-27 11:00:36 +10004624 if (!test_bit(R5_Insync, &dev->flags) ||
4625 ((i == sh->pd_idx || i == sh->qd_idx) &&
4626 s.failed == 0))
4627 set_bit(STRIPE_INSYNC, &sh->state);
4628 }
4629 }
4630 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4631 s.dec_preread_active = 1;
4632 }
4633
NeilBrownef5b7c62012-11-22 09:13:36 +11004634 /*
4635 * might be able to return some write requests if the parity blocks
4636 * are safe, or on a failed drive
4637 */
4638 pdev = &sh->dev[sh->pd_idx];
4639 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
4640 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
4641 qdev = &sh->dev[sh->qd_idx];
4642 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
4643 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
4644 || conf->level < 6;
4645
4646 if (s.written &&
4647 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
4648 && !test_bit(R5_LOCKED, &pdev->flags)
4649 && (test_bit(R5_UPTODATE, &pdev->flags) ||
4650 test_bit(R5_Discard, &pdev->flags))))) &&
4651 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
4652 && !test_bit(R5_LOCKED, &qdev->flags)
4653 && (test_bit(R5_UPTODATE, &qdev->flags) ||
4654 test_bit(R5_Discard, &qdev->flags))))))
4655 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
4656
Song Liu1e6d6902016-11-17 15:24:39 -08004657 if (s.just_cached)
4658 r5c_handle_cached_data_endio(conf, sh, disks, &s.return_bi);
4659 r5l_stripe_write_finished(sh);
4660
NeilBrownef5b7c62012-11-22 09:13:36 +11004661 /* Now we might consider reading some blocks, either to check/generate
4662 * parity, or to satisfy requests
4663 * or to load a block that is being partially written.
4664 */
4665 if (s.to_read || s.non_overwrite
4666 || (conf->level == 6 && s.to_write && s.failed)
4667 || (s.syncing && (s.uptodate + s.compute < disks))
4668 || s.replacing
4669 || s.expanding)
4670 handle_stripe_fill(sh, &s, disks);
4671
Song Liu2ded3702016-11-17 15:24:38 -08004672 /*
4673 * When the stripe finishes full journal write cycle (write to journal
4674 * and raid disk), this is the clean up procedure so it is ready for
4675 * next operation.
4676 */
4677 r5c_finish_stripe_write_out(conf, sh, &s);
4678
4679 /*
4680 * Now to consider new write requests, cache write back and what else,
4681 * if anything should be read. We do not handle new writes when:
NeilBrown84789552011-07-27 11:00:36 +10004682 * 1/ A 'write' operation (copy+xor) is already in flight.
4683 * 2/ A 'check' operation is in flight, as it may clobber the parity
4684 * block.
Song Liu2ded3702016-11-17 15:24:38 -08004685 * 3/ A r5c cache log write is in flight.
NeilBrown84789552011-07-27 11:00:36 +10004686 */
Song Liu2ded3702016-11-17 15:24:38 -08004687
4688 if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
4689 if (!r5c_is_writeback(conf->log)) {
4690 if (s.to_write)
4691 handle_stripe_dirtying(conf, sh, &s, disks);
4692 } else { /* write back cache */
4693 int ret = 0;
4694
4695 /* First, try handle writes in caching phase */
4696 if (s.to_write)
4697 ret = r5c_try_caching_write(conf, sh, &s,
4698 disks);
4699 /*
4700 * If caching phase failed: ret == -EAGAIN
4701 * OR
4702 * stripe under reclaim: !caching && injournal
4703 *
4704 * fall back to handle_stripe_dirtying()
4705 */
4706 if (ret == -EAGAIN ||
4707 /* stripe under reclaim: !caching && injournal */
4708 (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
Song Liud7bd3982016-11-23 22:50:39 -08004709 s.injournal > 0)) {
4710 ret = handle_stripe_dirtying(conf, sh, &s,
4711 disks);
4712 if (ret == -EAGAIN)
4713 goto finish;
4714 }
Song Liu2ded3702016-11-17 15:24:38 -08004715 }
4716 }
NeilBrown84789552011-07-27 11:00:36 +10004717
4718 /* maybe we need to check and possibly fix the parity for this stripe
4719 * Any reads will already have been scheduled, so we just see if enough
4720 * data is available. The parity check is held off while parity
4721 * dependent operations are in flight.
4722 */
4723 if (sh->check_state ||
4724 (s.syncing && s.locked == 0 &&
4725 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
4726 !test_bit(STRIPE_INSYNC, &sh->state))) {
4727 if (conf->level == 6)
4728 handle_parity_checks6(conf, sh, &s, disks);
4729 else
4730 handle_parity_checks5(conf, sh, &s, disks);
4731 }
NeilBrownc5a31002011-07-27 11:00:36 +10004732
NeilBrownf94c0b62013-07-22 12:57:21 +10004733 if ((s.replacing || s.syncing) && s.locked == 0
4734 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
4735 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11004736 /* Write out to replacement devices where possible */
4737 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10004738 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
4739 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11004740 set_bit(R5_WantReplace, &sh->dev[i].flags);
4741 set_bit(R5_LOCKED, &sh->dev[i].flags);
4742 s.locked++;
4743 }
NeilBrownf94c0b62013-07-22 12:57:21 +10004744 if (s.replacing)
4745 set_bit(STRIPE_INSYNC, &sh->state);
4746 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11004747 }
4748 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10004749 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11004750 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10004751 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4752 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004753 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
4754 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10004755 }
4756
4757 /* If the failed drives are just a ReadError, then we might need
4758 * to progress the repair/check process
4759 */
4760 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
4761 for (i = 0; i < s.failed; i++) {
4762 struct r5dev *dev = &sh->dev[s.failed_num[i]];
4763 if (test_bit(R5_ReadError, &dev->flags)
4764 && !test_bit(R5_LOCKED, &dev->flags)
4765 && test_bit(R5_UPTODATE, &dev->flags)
4766 ) {
4767 if (!test_bit(R5_ReWrite, &dev->flags)) {
4768 set_bit(R5_Wantwrite, &dev->flags);
4769 set_bit(R5_ReWrite, &dev->flags);
4770 set_bit(R5_LOCKED, &dev->flags);
4771 s.locked++;
4772 } else {
4773 /* let's read it back */
4774 set_bit(R5_Wantread, &dev->flags);
4775 set_bit(R5_LOCKED, &dev->flags);
4776 s.locked++;
4777 }
4778 }
4779 }
4780
NeilBrown3687c062011-07-27 11:00:36 +10004781 /* Finish reconstruct operations initiated by the expansion process */
4782 if (sh->reconstruct_state == reconstruct_state_result) {
4783 struct stripe_head *sh_src
Shaohua Li6d036f72015-08-13 14:31:57 -07004784 = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrown3687c062011-07-27 11:00:36 +10004785 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
4786 /* sh cannot be written until sh_src has been read.
4787 * so arrange for sh to be delayed a little
4788 */
4789 set_bit(STRIPE_DELAYED, &sh->state);
4790 set_bit(STRIPE_HANDLE, &sh->state);
4791 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
4792 &sh_src->state))
4793 atomic_inc(&conf->preread_active_stripes);
Shaohua Li6d036f72015-08-13 14:31:57 -07004794 raid5_release_stripe(sh_src);
NeilBrown3687c062011-07-27 11:00:36 +10004795 goto finish;
4796 }
4797 if (sh_src)
Shaohua Li6d036f72015-08-13 14:31:57 -07004798 raid5_release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07004799
NeilBrown3687c062011-07-27 11:00:36 +10004800 sh->reconstruct_state = reconstruct_state_idle;
4801 clear_bit(STRIPE_EXPANDING, &sh->state);
4802 for (i = conf->raid_disks; i--; ) {
4803 set_bit(R5_Wantwrite, &sh->dev[i].flags);
4804 set_bit(R5_LOCKED, &sh->dev[i].flags);
4805 s.locked++;
4806 }
4807 }
4808
4809 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
4810 !sh->reconstruct_state) {
4811 /* Need to write out all blocks after computing parity */
4812 sh->disks = conf->raid_disks;
4813 stripe_set_idx(sh->sector, conf, 0, sh);
4814 schedule_reconstruction(sh, &s, 1, 1);
4815 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
4816 clear_bit(STRIPE_EXPAND_READY, &sh->state);
4817 atomic_dec(&conf->reshape_stripes);
4818 wake_up(&conf->wait_for_overlap);
4819 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4820 }
4821
4822 if (s.expanding && s.locked == 0 &&
4823 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
4824 handle_stripe_expansion(conf, sh);
4825
4826finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07004827 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10004828 if (unlikely(s.blocked_rdev)) {
4829 if (conf->mddev->external)
4830 md_wait_for_blocked_rdev(s.blocked_rdev,
4831 conf->mddev);
4832 else
4833 /* Internal metadata will immediately
4834 * be written by raid5d, so we don't
4835 * need to wait here.
4836 */
4837 rdev_dec_pending(s.blocked_rdev,
4838 conf->mddev);
4839 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004840
NeilBrownbc2607f2011-07-28 11:39:22 +10004841 if (s.handle_bad_blocks)
4842 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004843 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10004844 struct r5dev *dev = &sh->dev[i];
4845 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
4846 /* We own a safe reference to the rdev */
4847 rdev = conf->disks[i].rdev;
4848 if (!rdev_set_badblocks(rdev, sh->sector,
4849 STRIPE_SECTORS, 0))
4850 md_error(conf->mddev, rdev);
4851 rdev_dec_pending(rdev, conf->mddev);
4852 }
NeilBrownb84db562011-07-28 11:39:23 +10004853 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
4854 rdev = conf->disks[i].rdev;
4855 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004856 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10004857 rdev_dec_pending(rdev, conf->mddev);
4858 }
NeilBrown977df362011-12-23 10:17:53 +11004859 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
4860 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11004861 if (!rdev)
4862 /* rdev have been moved down */
4863 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11004864 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004865 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11004866 rdev_dec_pending(rdev, conf->mddev);
4867 }
NeilBrownbc2607f2011-07-28 11:39:22 +10004868 }
4869
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004870 if (s.ops_request)
4871 raid_run_ops(sh, s.ops_request);
4872
Dan Williamsf0e43bc2008-06-28 08:31:55 +10004873 ops_run_io(sh, &s);
4874
NeilBrownc5709ef2011-07-26 11:35:20 +10004875 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11004876 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02004877 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11004878 * have actually been submitted.
4879 */
4880 atomic_dec(&conf->preread_active_stripes);
4881 if (atomic_read(&conf->preread_active_stripes) <
4882 IO_THRESHOLD)
4883 md_wakeup_thread(conf->mddev->thread);
4884 }
4885
NeilBrownc3cce6c2015-08-14 12:47:33 +10004886 if (!bio_list_empty(&s.return_bi)) {
Shaohua Li29530792016-12-08 15:48:19 -08004887 if (test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) {
NeilBrownc3cce6c2015-08-14 12:47:33 +10004888 spin_lock_irq(&conf->device_lock);
4889 bio_list_merge(&conf->return_bi, &s.return_bi);
4890 spin_unlock_irq(&conf->device_lock);
4891 md_wakeup_thread(conf->mddev->thread);
4892 } else
4893 return_io(&s.return_bi);
4894 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004895
Dan Williams257a4b42011-11-08 16:22:06 +11004896 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07004897}
4898
NeilBrownd1688a62011-10-11 16:49:52 +11004899static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900{
4901 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
4902 while (!list_empty(&conf->delayed_list)) {
4903 struct list_head *l = conf->delayed_list.next;
4904 struct stripe_head *sh;
4905 sh = list_entry(l, struct stripe_head, lru);
4906 list_del_init(l);
4907 clear_bit(STRIPE_DELAYED, &sh->state);
4908 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4909 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004910 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08004911 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 }
NeilBrown482c0832011-04-18 18:25:42 +10004913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914}
4915
Shaohua Li566c09c2013-11-14 15:16:17 +11004916static void activate_bit_delay(struct r5conf *conf,
4917 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07004918{
4919 /* device_lock is held */
4920 struct list_head head;
4921 list_add(&head, &conf->bitmap_list);
4922 list_del_init(&conf->bitmap_list);
4923 while (!list_empty(&head)) {
4924 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11004925 int hash;
NeilBrown72626682005-09-09 16:23:54 -07004926 list_del_init(&sh->lru);
4927 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11004928 hash = sh->hash_lock_index;
4929 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07004930 }
4931}
4932
NeilBrown5c675f82014-12-15 12:56:56 +11004933static int raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07004934{
NeilBrownd1688a62011-10-11 16:49:52 +11004935 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07004936
4937 /* No difference between reads and writes. Just check
4938 * how busy the stripe_cache is
4939 */
NeilBrown3fa841d2009-09-23 18:10:29 +10004940
NeilBrown54233992015-02-26 12:21:04 +11004941 if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state))
NeilBrownf022b2f2006-10-03 01:15:56 -07004942 return 1;
Song Liua39f7af2016-11-17 15:24:40 -08004943
4944 /* Also checks whether there is pressure on r5cache log space */
4945 if (test_bit(R5C_LOG_TIGHT, &conf->cache_state))
4946 return 1;
NeilBrownf022b2f2006-10-03 01:15:56 -07004947 if (conf->quiesce)
4948 return 1;
Shaohua Li4bda5562013-11-14 15:16:17 +11004949 if (atomic_read(&conf->empty_inactive_list_nr))
NeilBrownf022b2f2006-10-03 01:15:56 -07004950 return 1;
4951
4952 return 0;
4953}
4954
NeilBrownfd01b882011-10-11 16:47:53 +11004955static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004956{
NeilBrown3cb5edf2015-07-15 17:24:17 +10004957 struct r5conf *conf = mddev->private;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004958 sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev);
NeilBrown3cb5edf2015-07-15 17:24:17 +10004959 unsigned int chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004960 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004961
NeilBrown3cb5edf2015-07-15 17:24:17 +10004962 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004963 return chunk_sectors >=
4964 ((sector & (chunk_sectors - 1)) + bio_sectors);
4965}
4966
4967/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004968 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4969 * later sampled by raid5d.
4970 */
NeilBrownd1688a62011-10-11 16:49:52 +11004971static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004972{
4973 unsigned long flags;
4974
4975 spin_lock_irqsave(&conf->device_lock, flags);
4976
4977 bi->bi_next = conf->retry_read_aligned_list;
4978 conf->retry_read_aligned_list = bi;
4979
4980 spin_unlock_irqrestore(&conf->device_lock, flags);
4981 md_wakeup_thread(conf->mddev->thread);
4982}
4983
NeilBrownd1688a62011-10-11 16:49:52 +11004984static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004985{
4986 struct bio *bi;
4987
4988 bi = conf->retry_read_aligned;
4989 if (bi) {
4990 conf->retry_read_aligned = NULL;
4991 return bi;
4992 }
4993 bi = conf->retry_read_aligned_list;
4994 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08004995 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004996 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02004997 /*
4998 * this sets the active strip count to 1 and the processed
4999 * strip count to zero (upper 8 bits)
5000 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10005001 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005002 }
5003
5004 return bi;
5005}
5006
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005007/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005008 * The "raid5_align_endio" should check if the read succeeded and if it
5009 * did, call bio_endio on the original bio (having bio_put the new bio
5010 * first).
5011 * If the read failed..
5012 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005013static void raid5_align_endio(struct bio *bi)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005014{
5015 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11005016 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005017 struct r5conf *conf;
NeilBrown3cb03002011-10-11 16:45:26 +11005018 struct md_rdev *rdev;
Sasha Levin9b81c842015-08-10 19:05:18 -04005019 int error = bi->bi_error;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005020
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005021 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005022
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005023 rdev = (void*)raid_bi->bi_next;
5024 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11005025 mddev = rdev->mddev;
5026 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005027
5028 rdev_dec_pending(rdev, conf->mddev);
5029
Sasha Levin9b81c842015-08-10 19:05:18 -04005030 if (!error) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005031 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
5032 raid_bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005033 bio_endio(raid_bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005034 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10005035 wake_up(&conf->wait_for_quiescent);
NeilBrown6712ecf2007-09-27 12:47:43 +02005036 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005037 }
5038
Dan Williams45b42332007-07-09 11:56:43 -07005039 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005040
5041 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005042}
5043
Ming Lin7ef6b122015-05-06 22:51:24 -07005044static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005045{
NeilBrownd1688a62011-10-11 16:49:52 +11005046 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11005047 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005048 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11005049 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11005050 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005051
5052 if (!in_chunk_boundary(mddev, raid_bio)) {
Ming Lin7ef6b122015-05-06 22:51:24 -07005053 pr_debug("%s: non aligned\n", __func__);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005054 return 0;
5055 }
5056 /*
Ming Leid7a10302017-02-14 23:29:03 +08005057 * use bio_clone_fast to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005058 */
Ming Leid7a10302017-02-14 23:29:03 +08005059 align_bi = bio_clone_fast(raid_bio, GFP_NOIO, mddev->bio_set);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005060 if (!align_bi)
5061 return 0;
5062 /*
5063 * set bi_end_io to a new function, and set bi_private to the
5064 * original bio.
5065 */
5066 align_bi->bi_end_io = raid5_align_endio;
5067 align_bi->bi_private = raid_bio;
5068 /*
5069 * compute position
5070 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005071 align_bi->bi_iter.bi_sector =
5072 raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
5073 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005074
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005075 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005076 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11005077 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
5078 if (!rdev || test_bit(Faulty, &rdev->flags) ||
5079 rdev->recovery_offset < end_sector) {
5080 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
5081 if (rdev &&
5082 (test_bit(Faulty, &rdev->flags) ||
5083 !(test_bit(In_sync, &rdev->flags) ||
5084 rdev->recovery_offset >= end_sector)))
5085 rdev = NULL;
5086 }
Song Liu03b047f2017-01-11 13:39:14 -08005087
5088 if (r5c_big_stripe_cached(conf, align_bi->bi_iter.bi_sector)) {
5089 rcu_read_unlock();
5090 bio_put(align_bi);
5091 return 0;
5092 }
5093
NeilBrown671488c2011-12-23 10:17:52 +11005094 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10005095 sector_t first_bad;
5096 int bad_sectors;
5097
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005098 atomic_inc(&rdev->nr_pending);
5099 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005100 raid_bio->bi_next = (void*)rdev;
5101 align_bi->bi_bdev = rdev->bdev;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06005102 bio_clear_flag(align_bi, BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005103
Kent Overstreet7140aaf2013-09-25 13:37:01 -07005104 if (is_badblock(rdev, align_bi->bi_iter.bi_sector,
Kent Overstreet4f024f32013-10-11 15:44:27 -07005105 bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10005106 &first_bad, &bad_sectors)) {
Neil Brown387bb172007-02-08 14:20:29 -08005107 bio_put(align_bi);
5108 rdev_dec_pending(rdev, mddev);
5109 return 0;
5110 }
5111
majianpeng6c0544e2012-06-12 08:31:10 +08005112 /* No reshape active, so we can trust rdev->data_offset */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005113 align_bi->bi_iter.bi_sector += rdev->data_offset;
majianpeng6c0544e2012-06-12 08:31:10 +08005114
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005115 spin_lock_irq(&conf->device_lock);
Yuanhan Liub1b46482015-05-08 18:19:06 +10005116 wait_event_lock_irq(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005117 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01005118 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005119 atomic_inc(&conf->active_aligned_reads);
5120 spin_unlock_irq(&conf->device_lock);
5121
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005122 if (mddev->gendisk)
5123 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
5124 align_bi, disk_devt(mddev->gendisk),
Kent Overstreet4f024f32013-10-11 15:44:27 -07005125 raid_bio->bi_iter.bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005126 generic_make_request(align_bi);
5127 return 1;
5128 } else {
5129 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005130 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005131 return 0;
5132 }
5133}
5134
Ming Lin7ef6b122015-05-06 22:51:24 -07005135static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
5136{
5137 struct bio *split;
5138
5139 do {
5140 sector_t sector = raid_bio->bi_iter.bi_sector;
5141 unsigned chunk_sects = mddev->chunk_sectors;
5142 unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
5143
5144 if (sectors < bio_sectors(raid_bio)) {
5145 split = bio_split(raid_bio, sectors, GFP_NOIO, fs_bio_set);
5146 bio_chain(split, raid_bio);
5147 } else
5148 split = raid_bio;
5149
5150 if (!raid5_read_one_chunk(mddev, split)) {
5151 if (split != raid_bio)
5152 generic_make_request(raid_bio);
5153 return split;
5154 }
5155 } while (split != raid_bio);
5156
5157 return NULL;
5158}
5159
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005160/* __get_priority_stripe - get the next stripe to process
5161 *
5162 * Full stripe writes are allowed to pass preread active stripes up until
5163 * the bypass_threshold is exceeded. In general the bypass_count
5164 * increments when the handle_list is handled before the hold_list; however, it
5165 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
5166 * stripe with in flight i/o. The bypass_count will be reset when the
5167 * head of the hold_list has changed, i.e. the head was promoted to the
5168 * handle_list.
5169 */
Shaohua Li851c30c2013-08-28 14:30:16 +08005170static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005171{
Shaohua Li851c30c2013-08-28 14:30:16 +08005172 struct stripe_head *sh = NULL, *tmp;
5173 struct list_head *handle_list = NULL;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005174 struct r5worker_group *wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005175
5176 if (conf->worker_cnt_per_group == 0) {
5177 handle_list = &conf->handle_list;
5178 } else if (group != ANY_GROUP) {
5179 handle_list = &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005180 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08005181 } else {
5182 int i;
5183 for (i = 0; i < conf->group_cnt; i++) {
5184 handle_list = &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005185 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08005186 if (!list_empty(handle_list))
5187 break;
5188 }
5189 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005190
5191 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5192 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08005193 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005194 list_empty(&conf->hold_list) ? "empty" : "busy",
5195 atomic_read(&conf->pending_full_writes), conf->bypass_count);
5196
Shaohua Li851c30c2013-08-28 14:30:16 +08005197 if (!list_empty(handle_list)) {
5198 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005199
5200 if (list_empty(&conf->hold_list))
5201 conf->bypass_count = 0;
5202 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
5203 if (conf->hold_list.next == conf->last_hold)
5204 conf->bypass_count++;
5205 else {
5206 conf->last_hold = conf->hold_list.next;
5207 conf->bypass_count -= conf->bypass_threshold;
5208 if (conf->bypass_count < 0)
5209 conf->bypass_count = 0;
5210 }
5211 }
5212 } else if (!list_empty(&conf->hold_list) &&
5213 ((conf->bypass_threshold &&
5214 conf->bypass_count > conf->bypass_threshold) ||
5215 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08005216
5217 list_for_each_entry(tmp, &conf->hold_list, lru) {
5218 if (conf->worker_cnt_per_group == 0 ||
5219 group == ANY_GROUP ||
5220 !cpu_online(tmp->cpu) ||
5221 cpu_to_group(tmp->cpu) == group) {
5222 sh = tmp;
5223 break;
5224 }
5225 }
5226
5227 if (sh) {
5228 conf->bypass_count -= conf->bypass_threshold;
5229 if (conf->bypass_count < 0)
5230 conf->bypass_count = 0;
5231 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08005232 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005233 }
5234
5235 if (!sh)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005236 return NULL;
5237
Shaohua Libfc90cb2013-08-29 15:40:32 +08005238 if (wg) {
5239 wg->stripes_cnt--;
5240 sh->group = NULL;
5241 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005242 list_del_init(&sh->lru);
Shaohua Lic7a6d352014-04-15 09:12:54 +08005243 BUG_ON(atomic_inc_return(&sh->count) != 1);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005244 return sh;
5245}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005246
Shaohua Li8811b592012-08-02 08:33:00 +10005247struct raid5_plug_cb {
5248 struct blk_plug_cb cb;
5249 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11005250 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10005251};
5252
5253static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
5254{
5255 struct raid5_plug_cb *cb = container_of(
5256 blk_cb, struct raid5_plug_cb, cb);
5257 struct stripe_head *sh;
5258 struct mddev *mddev = cb->cb.data;
5259 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11005260 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11005261 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10005262
5263 if (cb->list.next && !list_empty(&cb->list)) {
5264 spin_lock_irq(&conf->device_lock);
5265 while (!list_empty(&cb->list)) {
5266 sh = list_first_entry(&cb->list, struct stripe_head, lru);
5267 list_del_init(&sh->lru);
5268 /*
5269 * avoid race release_stripe_plug() sees
5270 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5271 * is still in our list
5272 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005273 smp_mb__before_atomic();
Shaohua Li8811b592012-08-02 08:33:00 +10005274 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08005275 /*
5276 * STRIPE_ON_RELEASE_LIST could be set here. In that
5277 * case, the count is always > 1 here
5278 */
Shaohua Li566c09c2013-11-14 15:16:17 +11005279 hash = sh->hash_lock_index;
5280 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11005281 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10005282 }
5283 spin_unlock_irq(&conf->device_lock);
5284 }
Shaohua Li566c09c2013-11-14 15:16:17 +11005285 release_inactive_stripe_list(conf, cb->temp_inactive_list,
5286 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005287 if (mddev->queue)
5288 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10005289 kfree(cb);
5290}
5291
5292static void release_stripe_plug(struct mddev *mddev,
5293 struct stripe_head *sh)
5294{
5295 struct blk_plug_cb *blk_cb = blk_check_plugged(
5296 raid5_unplug, mddev,
5297 sizeof(struct raid5_plug_cb));
5298 struct raid5_plug_cb *cb;
5299
5300 if (!blk_cb) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005301 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005302 return;
5303 }
5304
5305 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
5306
Shaohua Li566c09c2013-11-14 15:16:17 +11005307 if (cb->list.next == NULL) {
5308 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10005309 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11005310 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5311 INIT_LIST_HEAD(cb->temp_inactive_list + i);
5312 }
Shaohua Li8811b592012-08-02 08:33:00 +10005313
5314 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
5315 list_add_tail(&sh->lru, &cb->list);
5316 else
Shaohua Li6d036f72015-08-13 14:31:57 -07005317 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005318}
5319
Shaohua Li620125f2012-10-11 13:49:05 +11005320static void make_discard_request(struct mddev *mddev, struct bio *bi)
5321{
5322 struct r5conf *conf = mddev->private;
5323 sector_t logical_sector, last_sector;
5324 struct stripe_head *sh;
5325 int remaining;
5326 int stripe_sectors;
5327
5328 if (mddev->reshape_position != MaxSector)
5329 /* Skip discard while reshape is happening */
5330 return;
5331
Kent Overstreet4f024f32013-10-11 15:44:27 -07005332 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
5333 last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9);
Shaohua Li620125f2012-10-11 13:49:05 +11005334
5335 bi->bi_next = NULL;
5336 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
5337
5338 stripe_sectors = conf->chunk_sectors *
5339 (conf->raid_disks - conf->max_degraded);
5340 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5341 stripe_sectors);
5342 sector_div(last_sector, stripe_sectors);
5343
5344 logical_sector *= conf->chunk_sectors;
5345 last_sector *= conf->chunk_sectors;
5346
5347 for (; logical_sector < last_sector;
5348 logical_sector += STRIPE_SECTORS) {
5349 DEFINE_WAIT(w);
5350 int d;
5351 again:
Shaohua Li6d036f72015-08-13 14:31:57 -07005352 sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
Shaohua Li620125f2012-10-11 13:49:05 +11005353 prepare_to_wait(&conf->wait_for_overlap, &w,
5354 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005355 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5356 if (test_bit(STRIPE_SYNCING, &sh->state)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005357 raid5_release_stripe(sh);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005358 schedule();
5359 goto again;
5360 }
5361 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11005362 spin_lock_irq(&sh->stripe_lock);
5363 for (d = 0; d < conf->raid_disks; d++) {
5364 if (d == sh->pd_idx || d == sh->qd_idx)
5365 continue;
5366 if (sh->dev[d].towrite || sh->dev[d].toread) {
5367 set_bit(R5_Overlap, &sh->dev[d].flags);
5368 spin_unlock_irq(&sh->stripe_lock);
Shaohua Li6d036f72015-08-13 14:31:57 -07005369 raid5_release_stripe(sh);
Shaohua Li620125f2012-10-11 13:49:05 +11005370 schedule();
5371 goto again;
5372 }
5373 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11005374 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11005375 finish_wait(&conf->wait_for_overlap, &w);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005376 sh->overwrite_disks = 0;
Shaohua Li620125f2012-10-11 13:49:05 +11005377 for (d = 0; d < conf->raid_disks; d++) {
5378 if (d == sh->pd_idx || d == sh->qd_idx)
5379 continue;
5380 sh->dev[d].towrite = bi;
5381 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
5382 raid5_inc_bi_active_stripes(bi);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005383 sh->overwrite_disks++;
Shaohua Li620125f2012-10-11 13:49:05 +11005384 }
5385 spin_unlock_irq(&sh->stripe_lock);
5386 if (conf->mddev->bitmap) {
5387 for (d = 0;
5388 d < conf->raid_disks - conf->max_degraded;
5389 d++)
5390 bitmap_startwrite(mddev->bitmap,
5391 sh->sector,
5392 STRIPE_SECTORS,
5393 0);
5394 sh->bm_seq = conf->seq_flush + 1;
5395 set_bit(STRIPE_BIT_DELAY, &sh->state);
5396 }
5397
5398 set_bit(STRIPE_HANDLE, &sh->state);
5399 clear_bit(STRIPE_DELAYED, &sh->state);
5400 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5401 atomic_inc(&conf->preread_active_stripes);
5402 release_stripe_plug(mddev, sh);
5403 }
5404
5405 remaining = raid5_dec_bi_active_stripes(bi);
5406 if (remaining == 0) {
5407 md_write_end(mddev);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005408 bio_endio(bi);
Shaohua Li620125f2012-10-11 13:49:05 +11005409 }
5410}
5411
Shaohua Li849674e2016-01-20 13:52:20 -08005412static void raid5_make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413{
NeilBrownd1688a62011-10-11 16:49:52 +11005414 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11005415 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 sector_t new_sector;
5417 sector_t logical_sector, last_sector;
5418 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01005419 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11005420 int remaining;
Shaohua Li27c0f682014-04-09 11:25:47 +08005421 DEFINE_WAIT(w);
5422 bool do_prepare;
Song Liu3bddb7f2016-11-18 16:46:50 -08005423 bool do_flush = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424
Jens Axboe1eff9d32016-08-05 15:35:16 -06005425 if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
Shaohua Li828cbe92015-09-02 13:49:49 -07005426 int ret = r5l_handle_flush_request(conf->log, bi);
5427
5428 if (ret == 0)
5429 return;
5430 if (ret == -ENODEV) {
5431 md_flush_request(mddev, bi);
5432 return;
5433 }
5434 /* ret == -EAGAIN, fallback */
Song Liu3bddb7f2016-11-18 16:46:50 -08005435 /*
5436 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
5437 * we need to flush journal device
5438 */
5439 do_flush = bi->bi_opf & REQ_PREFLUSH;
NeilBrowne5dcdd82005-09-09 16:23:41 -07005440 }
5441
NeilBrown3d310eb2005-06-21 17:17:26 -07005442 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07005443
Eric Mei9ffc8f72015-03-18 23:39:11 -06005444 /*
5445 * If array is degraded, better not do chunk aligned read because
5446 * later we might have to read it again in order to reconstruct
5447 * data on failed drives.
5448 */
5449 if (rw == READ && mddev->degraded == 0 &&
Ming Lin7ef6b122015-05-06 22:51:24 -07005450 mddev->reshape_position == MaxSector) {
5451 bi = chunk_aligned_read(mddev, bi);
5452 if (!bi)
5453 return;
5454 }
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08005455
Mike Christie796a5cf2016-06-05 14:32:07 -05005456 if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
Shaohua Li620125f2012-10-11 13:49:05 +11005457 make_discard_request(mddev, bi);
5458 return;
5459 }
5460
Kent Overstreet4f024f32013-10-11 15:44:27 -07005461 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005462 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 bi->bi_next = NULL;
5464 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07005465
Shaohua Li27c0f682014-04-09 11:25:47 +08005466 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005468 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10005469 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08005470
Shaohua Li27c0f682014-04-09 11:25:47 +08005471 do_prepare = false;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005472 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10005473 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11005474 previous = 0;
Shaohua Li27c0f682014-04-09 11:25:47 +08005475 if (do_prepare)
5476 prepare_to_wait(&conf->wait_for_overlap, &w,
5477 TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005478 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11005479 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08005480 * 64bit on a 32bit platform, and so it might be
5481 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02005482 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08005483 * the lock is dropped, so once we get a reference
5484 * to the stripe that we think it is, we will have
5485 * to check again.
5486 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005487 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005488 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005489 ? logical_sector < conf->reshape_progress
5490 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005491 previous = 1;
5492 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10005493 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005494 ? logical_sector < conf->reshape_safe
5495 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08005496 spin_unlock_irq(&conf->device_lock);
5497 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005498 do_prepare = true;
NeilBrownb578d552006-03-27 01:18:12 -08005499 goto retry;
5500 }
5501 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005502 spin_unlock_irq(&conf->device_lock);
5503 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005504
NeilBrown112bf892009-03-31 14:39:38 +11005505 new_sector = raid5_compute_sector(conf, logical_sector,
5506 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11005507 &dd_idx, NULL);
Shaohua Li849674e2016-01-20 13:52:20 -08005508 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10005509 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 (unsigned long long)logical_sector);
5511
Shaohua Li6d036f72015-08-13 14:31:57 -07005512 sh = raid5_get_active_stripe(conf, new_sector, previous,
Jens Axboe1eff9d32016-08-05 15:35:16 -06005513 (bi->bi_opf & REQ_RAHEAD), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11005515 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005516 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08005517 * stripe, so we must do the range check again.
5518 * Expansion could still move past after this
5519 * test, but as we are holding a reference to
5520 * 'sh', we know that if that happens,
5521 * STRIPE_EXPANDING will get set and the expansion
5522 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005523 */
5524 int must_retry = 0;
5525 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005526 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11005527 ? logical_sector >= conf->reshape_progress
5528 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005529 /* mismatch, need to try again */
5530 must_retry = 1;
5531 spin_unlock_irq(&conf->device_lock);
5532 if (must_retry) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005533 raid5_release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07005534 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005535 do_prepare = true;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005536 goto retry;
5537 }
5538 }
NeilBrownc46501b2013-08-27 15:52:13 +10005539 if (read_seqcount_retry(&conf->gen_lock, seq)) {
5540 /* Might have got the wrong stripe_head
5541 * by accident
5542 */
Shaohua Li6d036f72015-08-13 14:31:57 -07005543 raid5_release_stripe(sh);
NeilBrownc46501b2013-08-27 15:52:13 +10005544 goto retry;
5545 }
NeilBrowne62e58a2009-07-01 13:15:35 +10005546
Namhyung Kimffd96e32011-07-18 17:38:51 +10005547 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10005548 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08005549 logical_sector < mddev->suspend_hi) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005550 raid5_release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10005551 /* As the suspend_* range is controlled by
5552 * userspace, we want an interruptible
5553 * wait.
5554 */
5555 flush_signals(current);
5556 prepare_to_wait(&conf->wait_for_overlap,
5557 &w, TASK_INTERRUPTIBLE);
5558 if (logical_sector >= mddev->suspend_lo &&
Shaohua Li27c0f682014-04-09 11:25:47 +08005559 logical_sector < mddev->suspend_hi) {
NeilBrowne62e58a2009-07-01 13:15:35 +10005560 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005561 do_prepare = true;
5562 }
NeilBrowne464eaf2006-03-27 01:18:14 -08005563 goto retry;
5564 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005565
5566 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
shli@kernel.orgda41ba62014-12-15 12:57:03 +11005567 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005568 /* Stripe is busy expanding or
5569 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 * and wait a while
5571 */
NeilBrown482c0832011-04-18 18:25:42 +10005572 md_wakeup_thread(mddev->thread);
Shaohua Li6d036f72015-08-13 14:31:57 -07005573 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005575 do_prepare = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 goto retry;
5577 }
Song Liu3bddb7f2016-11-18 16:46:50 -08005578 if (do_flush) {
5579 set_bit(STRIPE_R5C_PREFLUSH, &sh->state);
5580 /* we only need flush for one stripe */
5581 do_flush = false;
5582 }
5583
NeilBrown6ed30032008-02-06 01:40:00 -08005584 set_bit(STRIPE_HANDLE, &sh->state);
5585 clear_bit(STRIPE_DELAYED, &sh->state);
shli@kernel.org59fc6302014-12-15 12:57:03 +11005586 if ((!sh->batch_head || sh == sh->batch_head) &&
Jens Axboe1eff9d32016-08-05 15:35:16 -06005587 (bi->bi_opf & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11005588 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5589 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10005590 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 } else {
5592 /* cannot get stripe for read-ahead, just give-up */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005593 bi->bi_error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 break;
5595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 }
Shaohua Li27c0f682014-04-09 11:25:47 +08005597 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown7c13edc2011-04-18 18:25:43 +10005598
Shaohua Lie7836bd62012-07-19 16:01:31 +10005599 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08005600 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
NeilBrown16a53ec2006-06-26 00:27:38 -07005602 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02005604
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005605 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
5606 bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005607 bio_endio(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609}
5610
NeilBrownfd01b882011-10-11 16:47:53 +11005611static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11005612
NeilBrownfd01b882011-10-11 16:47:53 +11005613static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614{
NeilBrown52c03292006-06-26 00:27:43 -07005615 /* reshaping is quite different to recovery/resync so it is
5616 * handled quite separately ... here.
5617 *
5618 * On each call to sync_request, we gather one chunk worth of
5619 * destination stripes and flag them as expanding.
5620 * Then we find all the source stripes and request reads.
5621 * As the reads complete, handle_stripe will copy the data
5622 * into the destination stripe and release that stripe.
5623 */
NeilBrownd1688a62011-10-11 16:49:52 +11005624 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005626 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08005627 int raid_disks = conf->previous_raid_disks;
5628 int data_disks = raid_disks - conf->max_degraded;
5629 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07005630 int i;
5631 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11005632 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11005633 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11005634 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11005635 struct list_head stripes;
NeilBrown92140482015-07-06 12:28:45 +10005636 sector_t retn;
NeilBrown52c03292006-06-26 00:27:43 -07005637
NeilBrownfef9c612009-03-31 15:16:46 +11005638 if (sector_nr == 0) {
5639 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10005640 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005641 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
5642 sector_nr = raid5_size(mddev, 0, 0)
5643 - conf->reshape_progress;
NeilBrown6cbd8142015-07-24 13:30:32 +10005644 } else if (mddev->reshape_backwards &&
5645 conf->reshape_progress == MaxSector) {
5646 /* shouldn't happen, but just in case, finish up.*/
5647 sector_nr = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +10005648 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005649 conf->reshape_progress > 0)
5650 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005651 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005652 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11005653 mddev->curr_resync_completed = sector_nr;
5654 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11005655 *skipped = 1;
NeilBrown92140482015-07-06 12:28:45 +10005656 retn = sector_nr;
5657 goto finish;
NeilBrownfef9c612009-03-31 15:16:46 +11005658 }
NeilBrown52c03292006-06-26 00:27:43 -07005659 }
5660
NeilBrown7a661382009-03-31 15:21:40 +11005661 /* We need to process a full chunk at a time.
5662 * If old and new chunk sizes differ, we need to process the
5663 * largest of these
5664 */
NeilBrown3cb5edf2015-07-15 17:24:17 +10005665
5666 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
NeilBrown7a661382009-03-31 15:21:40 +11005667
NeilBrownb5254dd2012-05-21 09:27:01 +10005668 /* We update the metadata at least every 10 seconds, or when
5669 * the data about to be copied would over-write the source of
5670 * the data at the front of the range. i.e. one new_stripe
5671 * along from reshape_progress new_maps to after where
5672 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07005673 */
NeilBrownfef9c612009-03-31 15:16:46 +11005674 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005675 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11005676 readpos = conf->reshape_progress;
5677 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005678 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08005679 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10005680 if (mddev->reshape_backwards) {
NeilBrownc74c0d72015-07-15 17:54:15 +10005681 BUG_ON(writepos < reshape_sectors);
5682 writepos -= reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11005683 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11005684 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11005685 } else {
NeilBrown7a661382009-03-31 15:21:40 +11005686 writepos += reshape_sectors;
NeilBrownc74c0d72015-07-15 17:54:15 +10005687 /* readpos and safepos are worst-case calculations.
5688 * A negative number is overly pessimistic, and causes
5689 * obvious problems for unsigned storage. So clip to 0.
5690 */
NeilBrowned37d832009-05-27 21:39:05 +10005691 readpos -= min_t(sector_t, reshape_sectors, readpos);
5692 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11005693 }
NeilBrown52c03292006-06-26 00:27:43 -07005694
NeilBrownb5254dd2012-05-21 09:27:01 +10005695 /* Having calculated the 'writepos' possibly use it
5696 * to set 'stripe_addr' which is where we will write to.
5697 */
5698 if (mddev->reshape_backwards) {
5699 BUG_ON(conf->reshape_progress == 0);
5700 stripe_addr = writepos;
5701 BUG_ON((mddev->dev_sectors &
5702 ~((sector_t)reshape_sectors - 1))
5703 - reshape_sectors - stripe_addr
5704 != sector_nr);
5705 } else {
5706 BUG_ON(writepos != sector_nr + reshape_sectors);
5707 stripe_addr = sector_nr;
5708 }
5709
NeilBrownc8f517c2009-03-31 15:28:40 +11005710 /* 'writepos' is the most advanced device address we might write.
5711 * 'readpos' is the least advanced device address we might read.
5712 * 'safepos' is the least address recorded in the metadata as having
5713 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10005714 * If there is a min_offset_diff, these are adjusted either by
5715 * increasing the safepos/readpos if diff is negative, or
5716 * increasing writepos if diff is positive.
5717 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11005718 * ensure safety in the face of a crash - that must be done by userspace
5719 * making a backup of the data. So in that case there is no particular
5720 * rush to update metadata.
5721 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5722 * update the metadata to advance 'safepos' to match 'readpos' so that
5723 * we can be safe in the event of a crash.
5724 * So we insist on updating metadata if safepos is behind writepos and
5725 * readpos is beyond writepos.
5726 * In any case, update the metadata every 10 seconds.
5727 * Maybe that number should be configurable, but I'm not sure it is
5728 * worth it.... maybe it could be a multiple of safemode_delay???
5729 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005730 if (conf->min_offset_diff < 0) {
5731 safepos += -conf->min_offset_diff;
5732 readpos += -conf->min_offset_diff;
5733 } else
5734 writepos += conf->min_offset_diff;
5735
NeilBrown2c810cd2012-05-21 09:27:00 +10005736 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11005737 ? (safepos > writepos && readpos < writepos)
5738 : (safepos < writepos && readpos > writepos)) ||
5739 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07005740 /* Cannot proceed until we've updated the superblock... */
5741 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005742 atomic_read(&conf->reshape_stripes)==0
5743 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5744 if (atomic_read(&conf->reshape_stripes) != 0)
5745 return 0;
NeilBrownfef9c612009-03-31 15:16:46 +11005746 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005747 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005748 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08005749 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown52c03292006-06-26 00:27:43 -07005750 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08005751 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11005752 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5753 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5754 return 0;
NeilBrown52c03292006-06-26 00:27:43 -07005755 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005756 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07005757 spin_unlock_irq(&conf->device_lock);
5758 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005759 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07005760 }
5761
NeilBrownab69ae12009-03-31 15:26:47 +11005762 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11005763 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07005764 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10005765 int skipped_disk = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07005766 sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005767 set_bit(STRIPE_EXPANDING, &sh->state);
5768 atomic_inc(&conf->reshape_stripes);
5769 /* If any of this stripe is beyond the end of the old
5770 * array, then we need to zero those blocks
5771 */
5772 for (j=sh->disks; j--;) {
5773 sector_t s;
5774 if (j == sh->pd_idx)
5775 continue;
NeilBrownf4168852007-02-28 20:11:53 -08005776 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11005777 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08005778 continue;
Shaohua Li6d036f72015-08-13 14:31:57 -07005779 s = raid5_compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11005780 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10005781 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07005782 continue;
5783 }
5784 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
5785 set_bit(R5_Expanded, &sh->dev[j].flags);
5786 set_bit(R5_UPTODATE, &sh->dev[j].flags);
5787 }
NeilBrowna9f326e2009-09-23 18:06:41 +10005788 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07005789 set_bit(STRIPE_EXPAND_READY, &sh->state);
5790 set_bit(STRIPE_HANDLE, &sh->state);
5791 }
NeilBrownab69ae12009-03-31 15:26:47 +11005792 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07005793 }
5794 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005795 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11005796 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005797 else
NeilBrown7a661382009-03-31 15:21:40 +11005798 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07005799 spin_unlock_irq(&conf->device_lock);
5800 /* Ok, those stripe are ready. We can start scheduling
5801 * reads on the source stripes.
5802 * The source stripes are determined by mapping the first and last
5803 * block on the destination stripes.
5804 */
NeilBrown52c03292006-06-26 00:27:43 -07005805 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11005806 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11005807 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07005808 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10005809 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10005810 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11005811 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11005812 if (last_sector >= mddev->dev_sectors)
5813 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07005814 while (first_sector <= last_sector) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005815 sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005816 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
5817 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07005818 raid5_release_stripe(sh);
NeilBrown52c03292006-06-26 00:27:43 -07005819 first_sector += STRIPE_SECTORS;
5820 }
NeilBrownab69ae12009-03-31 15:26:47 +11005821 /* Now that the sources are clearly marked, we can release
5822 * the destination stripes
5823 */
5824 while (!list_empty(&stripes)) {
5825 sh = list_entry(stripes.next, struct stripe_head, lru);
5826 list_del_init(&sh->lru);
Shaohua Li6d036f72015-08-13 14:31:57 -07005827 raid5_release_stripe(sh);
NeilBrownab69ae12009-03-31 15:26:47 +11005828 }
NeilBrownc6207272008-02-06 01:39:52 -08005829 /* If this takes us to the resync_max point where we have to pause,
5830 * then we need to write out the superblock.
5831 */
NeilBrown7a661382009-03-31 15:21:40 +11005832 sector_nr += reshape_sectors;
NeilBrown92140482015-07-06 12:28:45 +10005833 retn = reshape_sectors;
5834finish:
NeilBrownc5e19d92015-07-17 12:06:02 +10005835 if (mddev->curr_resync_completed > mddev->resync_max ||
5836 (sector_nr - mddev->curr_resync_completed) * 2
NeilBrownc03f6a12009-04-17 11:06:30 +10005837 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08005838 /* Cannot proceed until we've updated the superblock... */
5839 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005840 atomic_read(&conf->reshape_stripes) == 0
5841 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5842 if (atomic_read(&conf->reshape_stripes) != 0)
5843 goto ret;
NeilBrownfef9c612009-03-31 15:16:46 +11005844 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005845 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005846 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08005847 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownc6207272008-02-06 01:39:52 -08005848 md_wakeup_thread(mddev->thread);
5849 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005850 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)
NeilBrownc91abf52013-11-19 12:02:01 +11005851 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5852 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5853 goto ret;
NeilBrownc6207272008-02-06 01:39:52 -08005854 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005855 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08005856 spin_unlock_irq(&conf->device_lock);
5857 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005858 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08005859 }
NeilBrownc91abf52013-11-19 12:02:01 +11005860ret:
NeilBrown92140482015-07-06 12:28:45 +10005861 return retn;
NeilBrown52c03292006-06-26 00:27:43 -07005862}
5863
Shaohua Li849674e2016-01-20 13:52:20 -08005864static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
5865 int *skipped)
NeilBrown52c03292006-06-26 00:27:43 -07005866{
NeilBrownd1688a62011-10-11 16:49:52 +11005867 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07005868 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11005869 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11005870 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07005871 int still_degraded = 0;
5872 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873
NeilBrown72626682005-09-09 16:23:54 -07005874 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11005876
NeilBrown29269552006-03-27 01:18:10 -08005877 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
5878 end_reshape(conf);
5879 return 0;
5880 }
NeilBrown72626682005-09-09 16:23:54 -07005881
5882 if (mddev->curr_resync < max_sector) /* aborted */
5883 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
5884 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07005885 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07005886 conf->fullsync = 0;
5887 bitmap_close_sync(mddev->bitmap);
5888
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 return 0;
5890 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08005891
NeilBrown64bd6602009-08-03 10:59:58 +10005892 /* Allow raid5_quiesce to complete */
5893 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
5894
NeilBrown52c03292006-06-26 00:27:43 -07005895 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5896 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08005897
NeilBrownc6207272008-02-06 01:39:52 -08005898 /* No need to check resync_max as we never do more than one
5899 * stripe, and as resync_max will always be on a chunk boundary,
5900 * if the check in md_do_sync didn't fire, there is no chance
5901 * of overstepping resync_max here
5902 */
5903
NeilBrown16a53ec2006-06-26 00:27:38 -07005904 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 * to resync, then assert that we are finished, because there is
5906 * nothing we can do.
5907 */
NeilBrown3285edf2006-06-26 00:27:55 -07005908 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005909 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005910 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07005911 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 return rv;
5913 }
majianpeng6f608042013-04-24 11:42:41 +10005914 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
5915 !conf->fullsync &&
5916 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
5917 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07005918 /* we can skip this block, and probably more */
5919 sync_blocks /= STRIPE_SECTORS;
5920 *skipped = 1;
5921 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
5922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10005924 bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
NeilBrownb47490c2008-02-06 01:39:50 -08005925
Shaohua Li6d036f72015-08-13 14:31:57 -07005926 sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 if (sh == NULL) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005928 sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07005930 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08005932 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005934 /* Need to check if array will still be degraded after recovery/resync
Eric Mei16d9cfa2015-01-06 09:35:02 -08005935 * Note in case of > 1 drive failures it's possible we're rebuilding
5936 * one drive while leaving another faulty drive in array.
NeilBrown16a53ec2006-06-26 00:27:38 -07005937 */
Eric Mei16d9cfa2015-01-06 09:35:02 -08005938 rcu_read_lock();
5939 for (i = 0; i < conf->raid_disks; i++) {
5940 struct md_rdev *rdev = ACCESS_ONCE(conf->disks[i].rdev);
5941
5942 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
NeilBrown16a53ec2006-06-26 00:27:38 -07005943 still_degraded = 1;
Eric Mei16d9cfa2015-01-06 09:35:02 -08005944 }
5945 rcu_read_unlock();
NeilBrown16a53ec2006-06-26 00:27:38 -07005946
5947 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
5948
NeilBrown83206d62011-07-26 11:19:49 +10005949 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Eivind Sarto053f5b62014-06-09 17:06:19 -07005950 set_bit(STRIPE_HANDLE, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951
Shaohua Li6d036f72015-08-13 14:31:57 -07005952 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953
5954 return STRIPE_SECTORS;
5955}
5956
NeilBrownd1688a62011-10-11 16:49:52 +11005957static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005958{
5959 /* We may not be able to submit a whole bio at once as there
5960 * may not be enough stripe_heads available.
5961 * We cannot pre-allocate enough stripe_heads as we may need
5962 * more than exist in the cache (if we allow ever large chunks).
5963 * So we do one stripe head at a time and record in
5964 * ->bi_hw_segments how many have been done.
5965 *
5966 * We *know* that this entire raid_bio is in one chunk, so
5967 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5968 */
5969 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11005970 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005971 sector_t sector, logical_sector, last_sector;
5972 int scnt = 0;
5973 int remaining;
5974 int handled = 0;
5975
Kent Overstreet4f024f32013-10-11 15:44:27 -07005976 logical_sector = raid_bio->bi_iter.bi_sector &
5977 ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11005978 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11005979 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005980 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005981
5982 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08005983 logical_sector += STRIPE_SECTORS,
5984 sector += STRIPE_SECTORS,
5985 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005986
Shaohua Lie7836bd62012-07-19 16:01:31 +10005987 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005988 /* already done this stripe */
5989 continue;
5990
Shaohua Li6d036f72015-08-13 14:31:57 -07005991 sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005992
5993 if (!sh) {
5994 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10005995 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005996 conf->retry_read_aligned = raid_bio;
5997 return handled;
5998 }
5999
shli@kernel.orgda41ba62014-12-15 12:57:03 +11006000 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07006001 raid5_release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10006002 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08006003 conf->retry_read_aligned = raid_bio;
6004 return handled;
6005 }
6006
majianpeng3f9e7c12012-07-31 10:04:21 +10006007 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07006008 handle_stripe(sh);
Shaohua Li6d036f72015-08-13 14:31:57 -07006009 raid5_release_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006010 handled++;
6011 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10006012 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07006013 if (remaining == 0) {
6014 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
6015 raid_bio, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006016 bio_endio(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07006017 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006018 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10006019 wake_up(&conf->wait_for_quiescent);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006020 return handled;
6021}
6022
Shaohua Libfc90cb2013-08-29 15:40:32 +08006023static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11006024 struct r5worker *worker,
6025 struct list_head *temp_inactive_list)
Shaohua Li46a06402012-08-02 08:33:15 +10006026{
6027 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11006028 int i, batch_size = 0, hash;
6029 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10006030
6031 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08006032 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10006033 batch[batch_size++] = sh;
6034
Shaohua Li566c09c2013-11-14 15:16:17 +11006035 if (batch_size == 0) {
6036 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6037 if (!list_empty(temp_inactive_list + i))
6038 break;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006039 if (i == NR_STRIPE_HASH_LOCKS) {
6040 spin_unlock_irq(&conf->device_lock);
6041 r5l_flush_stripe_to_raid(conf->log);
6042 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006043 return batch_size;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006044 }
Shaohua Li566c09c2013-11-14 15:16:17 +11006045 release_inactive = true;
6046 }
Shaohua Li46a06402012-08-02 08:33:15 +10006047 spin_unlock_irq(&conf->device_lock);
6048
Shaohua Li566c09c2013-11-14 15:16:17 +11006049 release_inactive_stripe_list(conf, temp_inactive_list,
6050 NR_STRIPE_HASH_LOCKS);
6051
Shaohua Lia8c34f92015-09-02 13:49:46 -07006052 r5l_flush_stripe_to_raid(conf->log);
Shaohua Li566c09c2013-11-14 15:16:17 +11006053 if (release_inactive) {
6054 spin_lock_irq(&conf->device_lock);
6055 return 0;
6056 }
6057
Shaohua Li46a06402012-08-02 08:33:15 +10006058 for (i = 0; i < batch_size; i++)
6059 handle_stripe(batch[i]);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07006060 r5l_write_stripe_run(conf->log);
Shaohua Li46a06402012-08-02 08:33:15 +10006061
6062 cond_resched();
6063
6064 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006065 for (i = 0; i < batch_size; i++) {
6066 hash = batch[i]->hash_lock_index;
6067 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
6068 }
Shaohua Li46a06402012-08-02 08:33:15 +10006069 return batch_size;
6070}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006071
Shaohua Li851c30c2013-08-28 14:30:16 +08006072static void raid5_do_work(struct work_struct *work)
6073{
6074 struct r5worker *worker = container_of(work, struct r5worker, work);
6075 struct r5worker_group *group = worker->group;
6076 struct r5conf *conf = group->conf;
6077 int group_id = group - conf->worker_groups;
6078 int handled;
6079 struct blk_plug plug;
6080
6081 pr_debug("+++ raid5worker active\n");
6082
6083 blk_start_plug(&plug);
6084 handled = 0;
6085 spin_lock_irq(&conf->device_lock);
6086 while (1) {
6087 int batch_size, released;
6088
Shaohua Li566c09c2013-11-14 15:16:17 +11006089 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006090
Shaohua Li566c09c2013-11-14 15:16:17 +11006091 batch_size = handle_active_stripes(conf, group_id, worker,
6092 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08006093 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08006094 if (!batch_size && !released)
6095 break;
6096 handled += batch_size;
6097 }
6098 pr_debug("%d stripes handled\n", handled);
6099
6100 spin_unlock_irq(&conf->device_lock);
6101 blk_finish_plug(&plug);
6102
6103 pr_debug("--- raid5worker inactive\n");
6104}
6105
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106/*
6107 * This is our raid5 kernel thread.
6108 *
6109 * We scan the hash table for stripes which can be handled now.
6110 * During the scan, completed stripes are saved for us by the interrupt
6111 * handler, so that they will not have to wait for our next wakeup.
6112 */
Shaohua Li4ed87312012-10-11 13:34:00 +11006113static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114{
Shaohua Li4ed87312012-10-11 13:34:00 +11006115 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11006116 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006118 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119
Dan Williams45b42332007-07-09 11:56:43 -07006120 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121
6122 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123
NeilBrownc3cce6c2015-08-14 12:47:33 +10006124 if (!bio_list_empty(&conf->return_bi) &&
Shaohua Li29530792016-12-08 15:48:19 -08006125 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrownc3cce6c2015-08-14 12:47:33 +10006126 struct bio_list tmp = BIO_EMPTY_LIST;
6127 spin_lock_irq(&conf->device_lock);
Shaohua Li29530792016-12-08 15:48:19 -08006128 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrownc3cce6c2015-08-14 12:47:33 +10006129 bio_list_merge(&tmp, &conf->return_bi);
6130 bio_list_init(&conf->return_bi);
6131 }
6132 spin_unlock_irq(&conf->device_lock);
6133 return_io(&tmp);
6134 }
6135
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006136 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 handled = 0;
6138 spin_lock_irq(&conf->device_lock);
6139 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006140 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08006141 int batch_size, released;
6142
Shaohua Li566c09c2013-11-14 15:16:17 +11006143 released = release_stripe_list(conf, conf->temp_inactive_list);
NeilBrownedbe83a2015-02-26 12:47:56 +11006144 if (released)
6145 clear_bit(R5_DID_ALLOC, &conf->cache_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146
NeilBrown0021b7b2012-07-31 09:08:14 +02006147 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10006148 !list_empty(&conf->bitmap_list)) {
6149 /* Now is a good time to flush some bitmap updates */
6150 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08006151 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07006152 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08006153 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10006154 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11006155 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07006156 }
NeilBrown0021b7b2012-07-31 09:08:14 +02006157 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07006158
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006159 while ((bio = remove_bio_from_retry(conf))) {
6160 int ok;
6161 spin_unlock_irq(&conf->device_lock);
6162 ok = retry_aligned_read(conf, bio);
6163 spin_lock_irq(&conf->device_lock);
6164 if (!ok)
6165 break;
6166 handled++;
6167 }
6168
Shaohua Li566c09c2013-11-14 15:16:17 +11006169 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
6170 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08006171 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 break;
Shaohua Li46a06402012-08-02 08:33:15 +10006173 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174
Shaohua Li29530792016-12-08 15:48:19 -08006175 if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) {
Shaohua Li46a06402012-08-02 08:33:15 +10006176 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10006177 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10006178 spin_lock_irq(&conf->device_lock);
6179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180 }
Dan Williams45b42332007-07-09 11:56:43 -07006181 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182
6183 spin_unlock_irq(&conf->device_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006184 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
6185 mutex_trylock(&conf->cache_size_mutex)) {
NeilBrownedbe83a2015-02-26 12:47:56 +11006186 grow_one_stripe(conf, __GFP_NOWARN);
6187 /* Set flag even if allocation failed. This helps
6188 * slow down allocation requests when mem is short
6189 */
6190 set_bit(R5_DID_ALLOC, &conf->cache_state);
NeilBrown2d5b5692015-07-06 12:49:23 +10006191 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193
Shaohua Li765d7042017-01-04 09:33:23 -08006194 flush_deferred_bios(conf);
6195
Shaohua Li0576b1c2015-08-13 14:32:00 -07006196 r5l_flush_stripe_to_raid(conf->log);
6197
Dan Williamsc9f21aa2008-07-23 12:05:51 -07006198 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006199 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200
Dan Williams45b42332007-07-09 11:56:43 -07006201 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202}
6203
NeilBrown3f294f42005-11-08 21:39:25 -08006204static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006205raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006206{
NeilBrown7b1485b2014-12-15 12:56:59 +11006207 struct r5conf *conf;
6208 int ret = 0;
6209 spin_lock(&mddev->lock);
6210 conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006211 if (conf)
NeilBrownedbe83a2015-02-26 12:47:56 +11006212 ret = sprintf(page, "%d\n", conf->min_nr_stripes);
NeilBrown7b1485b2014-12-15 12:56:59 +11006213 spin_unlock(&mddev->lock);
6214 return ret;
NeilBrown3f294f42005-11-08 21:39:25 -08006215}
6216
NeilBrownc41d4ac2010-06-01 19:37:24 +10006217int
NeilBrownfd01b882011-10-11 16:47:53 +11006218raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10006219{
NeilBrownd1688a62011-10-11 16:49:52 +11006220 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10006221 int err;
6222
6223 if (size <= 16 || size > 32768)
6224 return -EINVAL;
NeilBrown486f0642015-02-25 12:10:35 +11006225
NeilBrownedbe83a2015-02-26 12:47:56 +11006226 conf->min_nr_stripes = size;
NeilBrown2d5b5692015-07-06 12:49:23 +10006227 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006228 while (size < conf->max_nr_stripes &&
6229 drop_one_stripe(conf))
6230 ;
NeilBrown2d5b5692015-07-06 12:49:23 +10006231 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006232
NeilBrownedbe83a2015-02-26 12:47:56 +11006233
NeilBrownc41d4ac2010-06-01 19:37:24 +10006234 err = md_allow_write(mddev);
6235 if (err)
6236 return err;
NeilBrown486f0642015-02-25 12:10:35 +11006237
NeilBrown2d5b5692015-07-06 12:49:23 +10006238 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006239 while (size > conf->max_nr_stripes)
6240 if (!grow_one_stripe(conf, GFP_KERNEL))
6241 break;
NeilBrown2d5b5692015-07-06 12:49:23 +10006242 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006243
NeilBrownc41d4ac2010-06-01 19:37:24 +10006244 return 0;
6245}
6246EXPORT_SYMBOL(raid5_set_cache_size);
6247
NeilBrown3f294f42005-11-08 21:39:25 -08006248static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006249raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08006250{
NeilBrown67918752014-12-15 12:57:01 +11006251 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006252 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07006253 int err;
6254
NeilBrown3f294f42005-11-08 21:39:25 -08006255 if (len >= PAGE_SIZE)
6256 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006257 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08006258 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006259 err = mddev_lock(mddev);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006260 if (err)
6261 return err;
NeilBrown67918752014-12-15 12:57:01 +11006262 conf = mddev->private;
6263 if (!conf)
6264 err = -ENODEV;
6265 else
6266 err = raid5_set_cache_size(mddev, new);
6267 mddev_unlock(mddev);
6268
6269 return err ?: len;
NeilBrown3f294f42005-11-08 21:39:25 -08006270}
NeilBrown007583c2005-11-08 21:39:30 -08006271
NeilBrown96de1e62005-11-08 21:39:39 -08006272static struct md_sysfs_entry
6273raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
6274 raid5_show_stripe_cache_size,
6275 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08006276
6277static ssize_t
Markus Stockhausend06f1912014-12-15 12:57:05 +11006278raid5_show_rmw_level(struct mddev *mddev, char *page)
6279{
6280 struct r5conf *conf = mddev->private;
6281 if (conf)
6282 return sprintf(page, "%d\n", conf->rmw_level);
6283 else
6284 return 0;
6285}
6286
6287static ssize_t
6288raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
6289{
6290 struct r5conf *conf = mddev->private;
6291 unsigned long new;
6292
6293 if (!conf)
6294 return -ENODEV;
6295
6296 if (len >= PAGE_SIZE)
6297 return -EINVAL;
6298
6299 if (kstrtoul(page, 10, &new))
6300 return -EINVAL;
6301
6302 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
6303 return -EINVAL;
6304
6305 if (new != PARITY_DISABLE_RMW &&
6306 new != PARITY_ENABLE_RMW &&
6307 new != PARITY_PREFER_RMW)
6308 return -EINVAL;
6309
6310 conf->rmw_level = new;
6311 return len;
6312}
6313
6314static struct md_sysfs_entry
6315raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
6316 raid5_show_rmw_level,
6317 raid5_store_rmw_level);
6318
6319
6320static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006321raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006322{
NeilBrown7b1485b2014-12-15 12:56:59 +11006323 struct r5conf *conf;
6324 int ret = 0;
6325 spin_lock(&mddev->lock);
6326 conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006327 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006328 ret = sprintf(page, "%d\n", conf->bypass_threshold);
6329 spin_unlock(&mddev->lock);
6330 return ret;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006331}
6332
6333static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006334raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006335{
NeilBrown67918752014-12-15 12:57:01 +11006336 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006337 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006338 int err;
6339
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006340 if (len >= PAGE_SIZE)
6341 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006342 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006343 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006344
6345 err = mddev_lock(mddev);
6346 if (err)
6347 return err;
6348 conf = mddev->private;
6349 if (!conf)
6350 err = -ENODEV;
NeilBrownedbe83a2015-02-26 12:47:56 +11006351 else if (new > conf->min_nr_stripes)
NeilBrown67918752014-12-15 12:57:01 +11006352 err = -EINVAL;
6353 else
6354 conf->bypass_threshold = new;
6355 mddev_unlock(mddev);
6356 return err ?: len;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006357}
6358
6359static struct md_sysfs_entry
6360raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
6361 S_IRUGO | S_IWUSR,
6362 raid5_show_preread_threshold,
6363 raid5_store_preread_threshold);
6364
6365static ssize_t
Shaohua Lid592a992014-05-21 17:57:44 +08006366raid5_show_skip_copy(struct mddev *mddev, char *page)
6367{
NeilBrown7b1485b2014-12-15 12:56:59 +11006368 struct r5conf *conf;
6369 int ret = 0;
6370 spin_lock(&mddev->lock);
6371 conf = mddev->private;
Shaohua Lid592a992014-05-21 17:57:44 +08006372 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006373 ret = sprintf(page, "%d\n", conf->skip_copy);
6374 spin_unlock(&mddev->lock);
6375 return ret;
Shaohua Lid592a992014-05-21 17:57:44 +08006376}
6377
6378static ssize_t
6379raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
6380{
NeilBrown67918752014-12-15 12:57:01 +11006381 struct r5conf *conf;
Shaohua Lid592a992014-05-21 17:57:44 +08006382 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006383 int err;
6384
Shaohua Lid592a992014-05-21 17:57:44 +08006385 if (len >= PAGE_SIZE)
6386 return -EINVAL;
Shaohua Lid592a992014-05-21 17:57:44 +08006387 if (kstrtoul(page, 10, &new))
6388 return -EINVAL;
6389 new = !!new;
Shaohua Lid592a992014-05-21 17:57:44 +08006390
NeilBrown67918752014-12-15 12:57:01 +11006391 err = mddev_lock(mddev);
6392 if (err)
6393 return err;
6394 conf = mddev->private;
6395 if (!conf)
6396 err = -ENODEV;
6397 else if (new != conf->skip_copy) {
6398 mddev_suspend(mddev);
6399 conf->skip_copy = new;
6400 if (new)
Jan Karadc3b17c2017-02-02 15:56:50 +01006401 mddev->queue->backing_dev_info->capabilities |=
NeilBrown67918752014-12-15 12:57:01 +11006402 BDI_CAP_STABLE_WRITES;
6403 else
Jan Karadc3b17c2017-02-02 15:56:50 +01006404 mddev->queue->backing_dev_info->capabilities &=
NeilBrown67918752014-12-15 12:57:01 +11006405 ~BDI_CAP_STABLE_WRITES;
6406 mddev_resume(mddev);
6407 }
6408 mddev_unlock(mddev);
6409 return err ?: len;
Shaohua Lid592a992014-05-21 17:57:44 +08006410}
6411
6412static struct md_sysfs_entry
6413raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
6414 raid5_show_skip_copy,
6415 raid5_store_skip_copy);
6416
Shaohua Lid592a992014-05-21 17:57:44 +08006417static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006418stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006419{
NeilBrownd1688a62011-10-11 16:49:52 +11006420 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006421 if (conf)
6422 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
6423 else
6424 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08006425}
6426
NeilBrown96de1e62005-11-08 21:39:39 -08006427static struct md_sysfs_entry
6428raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08006429
Shaohua Lib7214202013-08-27 17:50:42 +08006430static ssize_t
6431raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
6432{
NeilBrown7b1485b2014-12-15 12:56:59 +11006433 struct r5conf *conf;
6434 int ret = 0;
6435 spin_lock(&mddev->lock);
6436 conf = mddev->private;
Shaohua Lib7214202013-08-27 17:50:42 +08006437 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006438 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
6439 spin_unlock(&mddev->lock);
6440 return ret;
Shaohua Lib7214202013-08-27 17:50:42 +08006441}
6442
majianpeng60aaf932013-11-14 15:16:20 +11006443static int alloc_thread_groups(struct r5conf *conf, int cnt,
6444 int *group_cnt,
6445 int *worker_cnt_per_group,
6446 struct r5worker_group **worker_groups);
Shaohua Lib7214202013-08-27 17:50:42 +08006447static ssize_t
6448raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
6449{
NeilBrown67918752014-12-15 12:57:01 +11006450 struct r5conf *conf;
Shaohua Lib7214202013-08-27 17:50:42 +08006451 unsigned long new;
6452 int err;
majianpeng60aaf932013-11-14 15:16:20 +11006453 struct r5worker_group *new_groups, *old_groups;
6454 int group_cnt, worker_cnt_per_group;
Shaohua Lib7214202013-08-27 17:50:42 +08006455
6456 if (len >= PAGE_SIZE)
6457 return -EINVAL;
Shaohua Lib7214202013-08-27 17:50:42 +08006458 if (kstrtoul(page, 10, &new))
6459 return -EINVAL;
6460
NeilBrown67918752014-12-15 12:57:01 +11006461 err = mddev_lock(mddev);
Shaohua Lib7214202013-08-27 17:50:42 +08006462 if (err)
6463 return err;
NeilBrown67918752014-12-15 12:57:01 +11006464 conf = mddev->private;
6465 if (!conf)
6466 err = -ENODEV;
6467 else if (new != conf->worker_cnt_per_group) {
6468 mddev_suspend(mddev);
6469
6470 old_groups = conf->worker_groups;
6471 if (old_groups)
6472 flush_workqueue(raid5_wq);
6473
6474 err = alloc_thread_groups(conf, new,
6475 &group_cnt, &worker_cnt_per_group,
6476 &new_groups);
6477 if (!err) {
6478 spin_lock_irq(&conf->device_lock);
6479 conf->group_cnt = group_cnt;
6480 conf->worker_cnt_per_group = worker_cnt_per_group;
6481 conf->worker_groups = new_groups;
6482 spin_unlock_irq(&conf->device_lock);
6483
6484 if (old_groups)
6485 kfree(old_groups[0].workers);
6486 kfree(old_groups);
6487 }
6488 mddev_resume(mddev);
6489 }
6490 mddev_unlock(mddev);
6491
6492 return err ?: len;
Shaohua Lib7214202013-08-27 17:50:42 +08006493}
6494
6495static struct md_sysfs_entry
6496raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
6497 raid5_show_group_thread_cnt,
6498 raid5_store_group_thread_cnt);
6499
NeilBrown007583c2005-11-08 21:39:30 -08006500static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08006501 &raid5_stripecache_size.attr,
6502 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006503 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08006504 &raid5_group_thread_cnt.attr,
Shaohua Lid592a992014-05-21 17:57:44 +08006505 &raid5_skip_copy.attr,
Markus Stockhausend06f1912014-12-15 12:57:05 +11006506 &raid5_rmw_level.attr,
Song Liu2c7da142016-11-17 15:24:41 -08006507 &r5c_journal_mode.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08006508 NULL,
6509};
NeilBrown007583c2005-11-08 21:39:30 -08006510static struct attribute_group raid5_attrs_group = {
6511 .name = NULL,
6512 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08006513};
6514
majianpeng60aaf932013-11-14 15:16:20 +11006515static int alloc_thread_groups(struct r5conf *conf, int cnt,
6516 int *group_cnt,
6517 int *worker_cnt_per_group,
6518 struct r5worker_group **worker_groups)
Shaohua Li851c30c2013-08-28 14:30:16 +08006519{
Shaohua Li566c09c2013-11-14 15:16:17 +11006520 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08006521 ssize_t size;
6522 struct r5worker *workers;
6523
majianpeng60aaf932013-11-14 15:16:20 +11006524 *worker_cnt_per_group = cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +08006525 if (cnt == 0) {
majianpeng60aaf932013-11-14 15:16:20 +11006526 *group_cnt = 0;
6527 *worker_groups = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08006528 return 0;
6529 }
majianpeng60aaf932013-11-14 15:16:20 +11006530 *group_cnt = num_possible_nodes();
Shaohua Li851c30c2013-08-28 14:30:16 +08006531 size = sizeof(struct r5worker) * cnt;
majianpeng60aaf932013-11-14 15:16:20 +11006532 workers = kzalloc(size * *group_cnt, GFP_NOIO);
6533 *worker_groups = kzalloc(sizeof(struct r5worker_group) *
6534 *group_cnt, GFP_NOIO);
6535 if (!*worker_groups || !workers) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006536 kfree(workers);
majianpeng60aaf932013-11-14 15:16:20 +11006537 kfree(*worker_groups);
Shaohua Li851c30c2013-08-28 14:30:16 +08006538 return -ENOMEM;
6539 }
6540
majianpeng60aaf932013-11-14 15:16:20 +11006541 for (i = 0; i < *group_cnt; i++) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006542 struct r5worker_group *group;
6543
NeilBrown0c775d52013-11-25 11:12:43 +11006544 group = &(*worker_groups)[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08006545 INIT_LIST_HEAD(&group->handle_list);
6546 group->conf = conf;
6547 group->workers = workers + i * cnt;
6548
6549 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11006550 struct r5worker *worker = group->workers + j;
6551 worker->group = group;
6552 INIT_WORK(&worker->work, raid5_do_work);
6553
6554 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
6555 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08006556 }
6557 }
6558
6559 return 0;
6560}
6561
6562static void free_thread_groups(struct r5conf *conf)
6563{
6564 if (conf->worker_groups)
6565 kfree(conf->worker_groups[0].workers);
6566 kfree(conf->worker_groups);
6567 conf->worker_groups = NULL;
6568}
6569
Dan Williams80c3a6c2009-03-17 18:10:40 -07006570static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11006571raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07006572{
NeilBrownd1688a62011-10-11 16:49:52 +11006573 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006574
6575 if (!sectors)
6576 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006577 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11006578 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11006579 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006580
NeilBrown3cb5edf2015-07-15 17:24:17 +10006581 sectors &= ~((sector_t)conf->chunk_sectors - 1);
6582 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006583 return sectors * (raid_disks - conf->max_degraded);
6584}
6585
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306586static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6587{
6588 safe_put_page(percpu->spare_page);
shli@kernel.org46d5b782014-12-15 12:57:02 +11006589 if (percpu->scribble)
6590 flex_array_free(percpu->scribble);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306591 percpu->spare_page = NULL;
6592 percpu->scribble = NULL;
6593}
6594
6595static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6596{
6597 if (conf->level == 6 && !percpu->spare_page)
6598 percpu->spare_page = alloc_page(GFP_KERNEL);
6599 if (!percpu->scribble)
shli@kernel.org46d5b782014-12-15 12:57:02 +11006600 percpu->scribble = scribble_alloc(max(conf->raid_disks,
NeilBrown738a2732015-05-08 18:19:39 +10006601 conf->previous_raid_disks),
6602 max(conf->chunk_sectors,
6603 conf->prev_chunk_sectors)
6604 / STRIPE_SECTORS,
6605 GFP_KERNEL);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306606
6607 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
6608 free_scratch_buffer(conf, percpu);
6609 return -ENOMEM;
6610 }
6611
6612 return 0;
6613}
6614
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006615static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
6616{
6617 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
6618
6619 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
6620 return 0;
6621}
6622
NeilBrownd1688a62011-10-11 16:49:52 +11006623static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006624{
Dan Williams36d1c642009-07-14 11:48:22 -07006625 if (!conf->percpu)
6626 return;
6627
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006628 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Dan Williams36d1c642009-07-14 11:48:22 -07006629 free_percpu(conf->percpu);
6630}
6631
NeilBrownd1688a62011-10-11 16:49:52 +11006632static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10006633{
Song Liud7bd3982016-11-23 22:50:39 -08006634 int i;
6635
Shaohua Li5c7e81c2015-08-13 14:32:04 -07006636 if (conf->log)
6637 r5l_exit_log(conf->log);
Shaohua Li30c89462016-09-21 09:07:13 -07006638 if (conf->shrinker.nr_deferred)
NeilBrownedbe83a2015-02-26 12:47:56 +11006639 unregister_shrinker(&conf->shrinker);
Shaohua Li5c7e81c2015-08-13 14:32:04 -07006640
Shaohua Li851c30c2013-08-28 14:30:16 +08006641 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10006642 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07006643 raid5_free_percpu(conf);
Song Liud7bd3982016-11-23 22:50:39 -08006644 for (i = 0; i < conf->pool_size; i++)
6645 if (conf->disks[i].extra_page)
6646 put_page(conf->disks[i].extra_page);
Dan Williams95fc17a2009-07-31 12:39:15 +10006647 kfree(conf->disks);
6648 kfree(conf->stripe_hashtbl);
6649 kfree(conf);
6650}
6651
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006652static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
Dan Williams36d1c642009-07-14 11:48:22 -07006653{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006654 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
Dan Williams36d1c642009-07-14 11:48:22 -07006655 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
6656
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006657 if (alloc_scratch_buffer(conf, percpu)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006658 pr_warn("%s: failed memory allocation for cpu%u\n",
6659 __func__, cpu);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006660 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006661 }
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006662 return 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006663}
Dan Williams36d1c642009-07-14 11:48:22 -07006664
NeilBrownd1688a62011-10-11 16:49:52 +11006665static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006666{
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306667 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006668
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306669 conf->percpu = alloc_percpu(struct raid5_percpu);
6670 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07006671 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006672
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006673 err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Shaohua Li27a353c2016-02-24 17:38:28 -08006674 if (!err) {
6675 conf->scribble_disks = max(conf->raid_disks,
6676 conf->previous_raid_disks);
6677 conf->scribble_sectors = max(conf->chunk_sectors,
6678 conf->prev_chunk_sectors);
6679 }
Dan Williams36d1c642009-07-14 11:48:22 -07006680 return err;
6681}
6682
NeilBrownedbe83a2015-02-26 12:47:56 +11006683static unsigned long raid5_cache_scan(struct shrinker *shrink,
6684 struct shrink_control *sc)
6685{
6686 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
NeilBrown2d5b5692015-07-06 12:49:23 +10006687 unsigned long ret = SHRINK_STOP;
6688
6689 if (mutex_trylock(&conf->cache_size_mutex)) {
6690 ret= 0;
NeilBrown49895bc2015-08-03 17:09:57 +10006691 while (ret < sc->nr_to_scan &&
6692 conf->max_nr_stripes > conf->min_nr_stripes) {
NeilBrown2d5b5692015-07-06 12:49:23 +10006693 if (drop_one_stripe(conf) == 0) {
6694 ret = SHRINK_STOP;
6695 break;
6696 }
6697 ret++;
6698 }
6699 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006700 }
6701 return ret;
6702}
6703
6704static unsigned long raid5_cache_count(struct shrinker *shrink,
6705 struct shrink_control *sc)
6706{
6707 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
6708
6709 if (conf->max_nr_stripes < conf->min_nr_stripes)
6710 /* unlikely, but not impossible */
6711 return 0;
6712 return conf->max_nr_stripes - conf->min_nr_stripes;
6713}
6714
NeilBrownd1688a62011-10-11 16:49:52 +11006715static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716{
NeilBrownd1688a62011-10-11 16:49:52 +11006717 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006718 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11006719 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10006721 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11006722 int i;
majianpeng60aaf932013-11-14 15:16:20 +11006723 int group_cnt, worker_cnt_per_group;
6724 struct r5worker_group *new_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725
NeilBrown91adb562009-03-31 14:39:39 +11006726 if (mddev->new_level != 5
6727 && mddev->new_level != 4
6728 && mddev->new_level != 6) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006729 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
6730 mdname(mddev), mddev->new_level);
NeilBrown91adb562009-03-31 14:39:39 +11006731 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 }
NeilBrown91adb562009-03-31 14:39:39 +11006733 if ((mddev->new_level == 5
6734 && !algorithm_valid_raid5(mddev->new_layout)) ||
6735 (mddev->new_level == 6
6736 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006737 pr_warn("md/raid:%s: layout %d not supported\n",
6738 mdname(mddev), mddev->new_layout);
NeilBrown91adb562009-03-31 14:39:39 +11006739 return ERR_PTR(-EIO);
6740 }
6741 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006742 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
6743 mdname(mddev), mddev->raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006744 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11006745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746
Andre Noll664e7c42009-06-18 08:45:27 +10006747 if (!mddev->new_chunk_sectors ||
6748 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
6749 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006750 pr_warn("md/raid:%s: invalid chunk size %d\n",
6751 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11006752 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11006753 }
6754
NeilBrownd1688a62011-10-11 16:49:52 +11006755 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11006756 if (conf == NULL)
6757 goto abort;
Shaohua Li851c30c2013-08-28 14:30:16 +08006758 /* Don't enable multi-threading by default*/
majianpeng60aaf932013-11-14 15:16:20 +11006759 if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group,
6760 &new_group)) {
6761 conf->group_cnt = group_cnt;
6762 conf->worker_cnt_per_group = worker_cnt_per_group;
6763 conf->worker_groups = new_group;
6764 } else
Shaohua Li851c30c2013-08-28 14:30:16 +08006765 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11006766 spin_lock_init(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10006767 seqcount_init(&conf->gen_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006768 mutex_init(&conf->cache_size_mutex);
Yuanhan Liub1b46482015-05-08 18:19:06 +10006769 init_waitqueue_head(&conf->wait_for_quiescent);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08006770 init_waitqueue_head(&conf->wait_for_stripe);
Dan Williamsf5efd452009-10-16 15:55:38 +11006771 init_waitqueue_head(&conf->wait_for_overlap);
6772 INIT_LIST_HEAD(&conf->handle_list);
6773 INIT_LIST_HEAD(&conf->hold_list);
6774 INIT_LIST_HEAD(&conf->delayed_list);
6775 INIT_LIST_HEAD(&conf->bitmap_list);
NeilBrownc3cce6c2015-08-14 12:47:33 +10006776 bio_list_init(&conf->return_bi);
Shaohua Li773ca822013-08-27 17:50:39 +08006777 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11006778 atomic_set(&conf->active_stripes, 0);
6779 atomic_set(&conf->preread_active_stripes, 0);
6780 atomic_set(&conf->active_aligned_reads, 0);
Shaohua Li765d7042017-01-04 09:33:23 -08006781 bio_list_init(&conf->pending_bios);
6782 spin_lock_init(&conf->pending_bios_lock);
6783 conf->batch_bio_dispatch = true;
6784 rdev_for_each(rdev, mddev) {
6785 if (test_bit(Journal, &rdev->flags))
6786 continue;
6787 if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
6788 conf->batch_bio_dispatch = false;
6789 break;
6790 }
6791 }
6792
Dan Williamsf5efd452009-10-16 15:55:38 +11006793 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11006794 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11006795
6796 conf->raid_disks = mddev->raid_disks;
6797 if (mddev->reshape_position == MaxSector)
6798 conf->previous_raid_disks = mddev->raid_disks;
6799 else
6800 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006801 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006802
NeilBrown5e5e3e72009-10-16 16:35:30 +11006803 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11006804 GFP_KERNEL);
Song Liud7bd3982016-11-23 22:50:39 -08006805
NeilBrown91adb562009-03-31 14:39:39 +11006806 if (!conf->disks)
6807 goto abort;
6808
Song Liud7bd3982016-11-23 22:50:39 -08006809 for (i = 0; i < max_disks; i++) {
6810 conf->disks[i].extra_page = alloc_page(GFP_KERNEL);
6811 if (!conf->disks[i].extra_page)
6812 goto abort;
6813 }
6814
NeilBrown91adb562009-03-31 14:39:39 +11006815 conf->mddev = mddev;
6816
6817 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
6818 goto abort;
6819
Shaohua Li566c09c2013-11-14 15:16:17 +11006820 /* We init hash_locks[0] separately to that it can be used
6821 * as the reference lock in the spin_lock_nest_lock() call
6822 * in lock_all_device_hash_locks_irq in order to convince
6823 * lockdep that we know what we are doing.
6824 */
6825 spin_lock_init(conf->hash_locks);
6826 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
6827 spin_lock_init(conf->hash_locks + i);
6828
6829 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6830 INIT_LIST_HEAD(conf->inactive_list + i);
6831
6832 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6833 INIT_LIST_HEAD(conf->temp_inactive_list + i);
6834
Song Liu1e6d6902016-11-17 15:24:39 -08006835 atomic_set(&conf->r5c_cached_full_stripes, 0);
6836 INIT_LIST_HEAD(&conf->r5c_full_stripe_list);
6837 atomic_set(&conf->r5c_cached_partial_stripes, 0);
6838 INIT_LIST_HEAD(&conf->r5c_partial_stripe_list);
Shaohua Lie33fbb92017-02-10 16:18:09 -08006839 atomic_set(&conf->r5c_flushing_full_stripes, 0);
6840 atomic_set(&conf->r5c_flushing_partial_stripes, 0);
Song Liu1e6d6902016-11-17 15:24:39 -08006841
Dan Williams36d1c642009-07-14 11:48:22 -07006842 conf->level = mddev->new_level;
shli@kernel.org46d5b782014-12-15 12:57:02 +11006843 conf->chunk_sectors = mddev->new_chunk_sectors;
Dan Williams36d1c642009-07-14 11:48:22 -07006844 if (raid5_alloc_percpu(conf) != 0)
6845 goto abort;
6846
NeilBrown0c55e022010-05-03 14:09:02 +10006847 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11006848
NeilBrowndafb20f2012-03-19 12:46:39 +11006849 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11006850 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006851 if (raid_disk >= max_disks
Shaohua Lif2076e72015-10-08 21:54:12 -07006852 || raid_disk < 0 || test_bit(Journal, &rdev->flags))
NeilBrown91adb562009-03-31 14:39:39 +11006853 continue;
6854 disk = conf->disks + raid_disk;
6855
NeilBrown17045f52011-12-23 10:17:53 +11006856 if (test_bit(Replacement, &rdev->flags)) {
6857 if (disk->replacement)
6858 goto abort;
6859 disk->replacement = rdev;
6860 } else {
6861 if (disk->rdev)
6862 goto abort;
6863 disk->rdev = rdev;
6864 }
NeilBrown91adb562009-03-31 14:39:39 +11006865
6866 if (test_bit(In_sync, &rdev->flags)) {
6867 char b[BDEVNAME_SIZE];
NeilBrowncc6167b2016-11-02 14:16:50 +11006868 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
6869 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05006870 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11006871 /* Cannot rely on bitmap to complete recovery */
6872 conf->fullsync = 1;
6873 }
6874
NeilBrown91adb562009-03-31 14:39:39 +11006875 conf->level = mddev->new_level;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006876 if (conf->level == 6) {
NeilBrown91adb562009-03-31 14:39:39 +11006877 conf->max_degraded = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006878 if (raid6_call.xor_syndrome)
6879 conf->rmw_level = PARITY_ENABLE_RMW;
6880 else
6881 conf->rmw_level = PARITY_DISABLE_RMW;
6882 } else {
NeilBrown91adb562009-03-31 14:39:39 +11006883 conf->max_degraded = 1;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006884 conf->rmw_level = PARITY_ENABLE_RMW;
6885 }
NeilBrown91adb562009-03-31 14:39:39 +11006886 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11006887 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11006888 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10006889 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11006890 conf->prev_algo = mddev->layout;
NeilBrown5cac6bc2015-07-17 12:17:50 +10006891 } else {
6892 conf->prev_chunk_sectors = conf->chunk_sectors;
6893 conf->prev_algo = conf->algorithm;
NeilBrowne183eae2009-03-31 15:20:22 +11006894 }
NeilBrown91adb562009-03-31 14:39:39 +11006895
NeilBrownedbe83a2015-02-26 12:47:56 +11006896 conf->min_nr_stripes = NR_STRIPES;
Shaohua Liad5b0f72016-08-30 10:29:33 -07006897 if (mddev->reshape_position != MaxSector) {
6898 int stripes = max_t(int,
6899 ((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4,
6900 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4);
6901 conf->min_nr_stripes = max(NR_STRIPES, stripes);
6902 if (conf->min_nr_stripes != NR_STRIPES)
NeilBrowncc6167b2016-11-02 14:16:50 +11006903 pr_info("md/raid:%s: force stripe size %d for reshape\n",
Shaohua Liad5b0f72016-08-30 10:29:33 -07006904 mdname(mddev), conf->min_nr_stripes);
6905 }
NeilBrownedbe83a2015-02-26 12:47:56 +11006906 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11006907 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li4bda5562013-11-14 15:16:17 +11006908 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
NeilBrownedbe83a2015-02-26 12:47:56 +11006909 if (grow_stripes(conf, conf->min_nr_stripes)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006910 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
6911 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11006912 goto abort;
6913 } else
NeilBrowncc6167b2016-11-02 14:16:50 +11006914 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory);
NeilBrownedbe83a2015-02-26 12:47:56 +11006915 /*
6916 * Losing a stripe head costs more than the time to refill it,
6917 * it reduces the queue depth and so can hurt throughput.
6918 * So set it rather large, scaled by number of devices.
6919 */
6920 conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
6921 conf->shrinker.scan_objects = raid5_cache_scan;
6922 conf->shrinker.count_objects = raid5_cache_count;
6923 conf->shrinker.batch = 128;
6924 conf->shrinker.flags = 0;
Chao Yu6a0f53f2016-09-20 10:33:57 +08006925 if (register_shrinker(&conf->shrinker)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006926 pr_warn("md/raid:%s: couldn't register shrinker.\n",
6927 mdname(mddev));
Chao Yu6a0f53f2016-09-20 10:33:57 +08006928 goto abort;
6929 }
NeilBrown91adb562009-03-31 14:39:39 +11006930
NeilBrown02326052012-07-03 15:56:52 +10006931 sprintf(pers_name, "raid%d", mddev->new_level);
6932 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11006933 if (!conf->thread) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006934 pr_warn("md/raid:%s: couldn't allocate thread.\n",
6935 mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11006936 goto abort;
6937 }
6938
6939 return conf;
6940
6941 abort:
6942 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10006943 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11006944 return ERR_PTR(-EIO);
6945 } else
6946 return ERR_PTR(-ENOMEM);
6947}
6948
NeilBrownc148ffd2009-11-13 17:47:00 +11006949static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
6950{
6951 switch (algo) {
6952 case ALGORITHM_PARITY_0:
6953 if (raid_disk < max_degraded)
6954 return 1;
6955 break;
6956 case ALGORITHM_PARITY_N:
6957 if (raid_disk >= raid_disks - max_degraded)
6958 return 1;
6959 break;
6960 case ALGORITHM_PARITY_0_6:
NeilBrownf72ffdd2014-09-30 14:23:59 +10006961 if (raid_disk == 0 ||
NeilBrownc148ffd2009-11-13 17:47:00 +11006962 raid_disk == raid_disks - 1)
6963 return 1;
6964 break;
6965 case ALGORITHM_LEFT_ASYMMETRIC_6:
6966 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6967 case ALGORITHM_LEFT_SYMMETRIC_6:
6968 case ALGORITHM_RIGHT_SYMMETRIC_6:
6969 if (raid_disk == raid_disks - 1)
6970 return 1;
6971 }
6972 return 0;
6973}
6974
Shaohua Li849674e2016-01-20 13:52:20 -08006975static int raid5_run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11006976{
NeilBrownd1688a62011-10-11 16:49:52 +11006977 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10006978 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11006979 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006980 struct md_rdev *rdev;
Shaohua Li713cf5a2015-08-13 14:32:03 -07006981 struct md_rdev *journal_dev = NULL;
NeilBrownc148ffd2009-11-13 17:47:00 +11006982 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11006983 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10006984 long long min_offset_diff = 0;
6985 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11006986
Andre Noll8c6ac8682009-06-18 08:48:06 +10006987 if (mddev->recovery_cp != MaxSector)
NeilBrowncc6167b2016-11-02 14:16:50 +11006988 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
6989 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10006990
6991 rdev_for_each(rdev, mddev) {
6992 long long diff;
Shaohua Li713cf5a2015-08-13 14:32:03 -07006993
Shaohua Lif2076e72015-10-08 21:54:12 -07006994 if (test_bit(Journal, &rdev->flags)) {
Shaohua Li713cf5a2015-08-13 14:32:03 -07006995 journal_dev = rdev;
Shaohua Lif2076e72015-10-08 21:54:12 -07006996 continue;
6997 }
NeilBrownb5254dd2012-05-21 09:27:01 +10006998 if (rdev->raid_disk < 0)
6999 continue;
7000 diff = (rdev->new_data_offset - rdev->data_offset);
7001 if (first) {
7002 min_offset_diff = diff;
7003 first = 0;
7004 } else if (mddev->reshape_backwards &&
7005 diff < min_offset_diff)
7006 min_offset_diff = diff;
7007 else if (!mddev->reshape_backwards &&
7008 diff > min_offset_diff)
7009 min_offset_diff = diff;
7010 }
7011
NeilBrownf6705572006-03-27 01:18:11 -08007012 if (mddev->reshape_position != MaxSector) {
7013 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10007014 * Difficulties arise if the stripe we would write to
7015 * next is at or after the stripe we would read from next.
7016 * For a reshape that changes the number of devices, this
7017 * is only possible for a very short time, and mdadm makes
7018 * sure that time appears to have past before assembling
7019 * the array. So we fail if that time hasn't passed.
7020 * For a reshape that keeps the number of devices the same
7021 * mdadm must be monitoring the reshape can keeping the
7022 * critical areas read-only and backed up. It will start
7023 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08007024 */
7025 sector_t here_new, here_old;
7026 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11007027 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrown05256d92015-07-15 17:36:21 +10007028 int chunk_sectors;
7029 int new_data_disks;
NeilBrownf6705572006-03-27 01:18:11 -08007030
Shaohua Li713cf5a2015-08-13 14:32:03 -07007031 if (journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007032 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
7033 mdname(mddev));
Shaohua Li713cf5a2015-08-13 14:32:03 -07007034 return -EINVAL;
7035 }
7036
NeilBrown88ce4932009-03-31 15:24:23 +11007037 if (mddev->new_level != mddev->level) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007038 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
7039 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007040 return -EINVAL;
7041 }
NeilBrownf6705572006-03-27 01:18:11 -08007042 old_disks = mddev->raid_disks - mddev->delta_disks;
7043 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08007044 * further up in new geometry must map after here in old
7045 * geometry.
NeilBrown05256d92015-07-15 17:36:21 +10007046 * If the chunk sizes are different, then as we perform reshape
7047 * in units of the largest of the two, reshape_position needs
7048 * be a multiple of the largest chunk size times new data disks.
NeilBrownf6705572006-03-27 01:18:11 -08007049 */
7050 here_new = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007051 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
7052 new_data_disks = mddev->raid_disks - max_degraded;
7053 if (sector_div(here_new, chunk_sectors * new_data_disks)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007054 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
7055 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007056 return -EINVAL;
7057 }
NeilBrown05256d92015-07-15 17:36:21 +10007058 reshape_offset = here_new * chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08007059 /* here_new is the stripe we will write to */
7060 here_old = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007061 sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
NeilBrownf4168852007-02-28 20:11:53 -08007062 /* here_old is the first stripe that we might need to read
7063 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10007064 if (mddev->delta_disks == 0) {
7065 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10007066 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10007067 * and taking constant backups.
7068 * mdadm always starts a situation like this in
7069 * readonly mode so it can take control before
7070 * allowing any writes. So just check for that.
7071 */
NeilBrownb5254dd2012-05-21 09:27:01 +10007072 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
7073 abs(min_offset_diff) >= mddev->new_chunk_sectors)
7074 /* not really in-place - so OK */;
7075 else if (mddev->ro == 0) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007076 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
7077 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10007078 return -EINVAL;
7079 }
NeilBrown2c810cd2012-05-21 09:27:00 +10007080 } else if (mddev->reshape_backwards
NeilBrown05256d92015-07-15 17:36:21 +10007081 ? (here_new * chunk_sectors + min_offset_diff <=
7082 here_old * chunk_sectors)
7083 : (here_new * chunk_sectors >=
7084 here_old * chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08007085 /* Reading from the same stripe as writing to - bad */
NeilBrowncc6167b2016-11-02 14:16:50 +11007086 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
7087 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007088 return -EINVAL;
7089 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007090 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007091 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08007092 } else {
NeilBrown91adb562009-03-31 14:39:39 +11007093 BUG_ON(mddev->level != mddev->new_level);
7094 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10007095 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11007096 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08007097 }
7098
NeilBrown245f46c2009-03-31 14:39:39 +11007099 if (mddev->private == NULL)
7100 conf = setup_conf(mddev);
7101 else
7102 conf = mddev->private;
7103
NeilBrown91adb562009-03-31 14:39:39 +11007104 if (IS_ERR(conf))
7105 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08007106
Song Liu486b0f72016-08-19 15:34:01 -07007107 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
7108 if (!journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007109 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
7110 mdname(mddev));
Song Liu486b0f72016-08-19 15:34:01 -07007111 mddev->ro = 1;
7112 set_disk_ro(mddev->gendisk, 1);
7113 } else if (mddev->recovery_cp == MaxSector)
7114 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
Shaohua Li7dde2ad2015-10-08 21:54:10 -07007115 }
7116
NeilBrownb5254dd2012-05-21 09:27:01 +10007117 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11007118 mddev->thread = conf->thread;
7119 conf->thread = NULL;
7120 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121
NeilBrown17045f52011-12-23 10:17:53 +11007122 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
7123 i++) {
7124 rdev = conf->disks[i].rdev;
7125 if (!rdev && conf->disks[i].replacement) {
7126 /* The replacement is all we have yet */
7127 rdev = conf->disks[i].replacement;
7128 conf->disks[i].replacement = NULL;
7129 clear_bit(Replacement, &rdev->flags);
7130 conf->disks[i].rdev = rdev;
7131 }
7132 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11007133 continue;
NeilBrown17045f52011-12-23 10:17:53 +11007134 if (conf->disks[i].replacement &&
7135 conf->reshape_progress != MaxSector) {
7136 /* replacements and reshape simply do not mix. */
NeilBrowncc6167b2016-11-02 14:16:50 +11007137 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
NeilBrown17045f52011-12-23 10:17:53 +11007138 goto abort;
7139 }
NeilBrown2f115882010-06-17 17:41:03 +10007140 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11007141 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10007142 continue;
7143 }
NeilBrownc148ffd2009-11-13 17:47:00 +11007144 /* This disc is not fully in-sync. However if it
7145 * just stored parity (beyond the recovery_offset),
7146 * when we don't need to be concerned about the
7147 * array being dirty.
7148 * When reshape goes 'backwards', we never have
7149 * partially completed devices, so we only need
7150 * to worry about reshape going forwards.
7151 */
7152 /* Hack because v0.91 doesn't store recovery_offset properly. */
7153 if (mddev->major_version == 0 &&
7154 mddev->minor_version > 90)
7155 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007156
NeilBrownc148ffd2009-11-13 17:47:00 +11007157 if (rdev->recovery_offset < reshape_offset) {
7158 /* We need to check old and new layout */
7159 if (!only_parity(rdev->raid_disk,
7160 conf->algorithm,
7161 conf->raid_disks,
7162 conf->max_degraded))
7163 continue;
7164 }
7165 if (!only_parity(rdev->raid_disk,
7166 conf->prev_algo,
7167 conf->previous_raid_disks,
7168 conf->max_degraded))
7169 continue;
7170 dirty_parity_disks++;
7171 }
NeilBrown91adb562009-03-31 14:39:39 +11007172
NeilBrown17045f52011-12-23 10:17:53 +11007173 /*
7174 * 0 for a fully functional array, 1 or 2 for a degraded array.
7175 */
Song Liu2e38a372017-01-24 10:45:30 -08007176 mddev->degraded = raid5_calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177
NeilBrown674806d2010-06-16 17:17:53 +10007178 if (has_failed(conf)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007179 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07007180 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181 goto abort;
7182 }
7183
NeilBrown91adb562009-03-31 14:39:39 +11007184 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10007185 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11007186 mddev->resync_max_sectors = mddev->dev_sectors;
7187
NeilBrownc148ffd2009-11-13 17:47:00 +11007188 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007190 if (mddev->ok_start_degraded)
NeilBrowncc6167b2016-11-02 14:16:50 +11007191 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
7192 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007193 else {
NeilBrowncc6167b2016-11-02 14:16:50 +11007194 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
7195 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007196 goto abort;
7197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198 }
7199
NeilBrowncc6167b2016-11-02 14:16:50 +11007200 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
7201 mdname(mddev), conf->level,
7202 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
7203 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204
7205 print_raid5_conf(conf);
7206
NeilBrownfef9c612009-03-31 15:16:46 +11007207 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11007208 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08007209 atomic_set(&conf->reshape_stripes, 0);
7210 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7211 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7212 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7213 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7214 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007215 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08007216 }
7217
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10007219 if (mddev->to_remove == &raid5_attrs_group)
7220 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007221 else if (mddev->kobj.sd &&
7222 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrowncc6167b2016-11-02 14:16:50 +11007223 pr_warn("raid5: failed to create sysfs attributes for %s\n",
7224 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10007225 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
7226
7227 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007228 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11007229 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10007230 /* read-ahead size must cover two whole stripes, which
7231 * is 2 * (datadisks) * chunksize where 'n' is the
7232 * number of raid devices
7233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234 int data_disks = conf->previous_raid_disks - conf->max_degraded;
7235 int stripe = data_disks *
7236 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Jan Karadc3b17c2017-02-02 15:56:50 +01007237 if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
7238 mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10007239
NeilBrown9f7c2222010-07-26 12:04:13 +10007240 chunk_size = mddev->chunk_sectors << 9;
7241 blk_queue_io_min(mddev->queue, chunk_size);
7242 blk_queue_io_opt(mddev->queue, chunk_size *
7243 (conf->raid_disks - conf->max_degraded));
Kent Overstreetc78afc62013-07-11 22:39:53 -07007244 mddev->queue->limits.raid_partial_stripes_expensive = 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007245 /*
7246 * We can only discard a whole stripe. It doesn't make sense to
7247 * discard data disk but write parity disk
7248 */
7249 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11007250 /* Round up to power of 2, as discard handling
7251 * currently assumes that */
7252 while ((stripe-1) & stripe)
7253 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007254 mddev->queue->limits.discard_alignment = stripe;
7255 mddev->queue->limits.discard_granularity = stripe;
Konstantin Khlebnikove8d7c332016-11-27 19:32:32 +03007256
7257 /*
7258 * We use 16-bit counter of active stripes in bi_phys_segments
7259 * (minus one for over-loaded initialization)
7260 */
7261 blk_queue_max_hw_sectors(mddev->queue, 0xfffe * STRIPE_SECTORS);
7262 blk_queue_max_discard_sectors(mddev->queue,
7263 0xfffe * STRIPE_SECTORS);
7264
Shaohua Li620125f2012-10-11 13:49:05 +11007265 /*
7266 * unaligned part of discard request will be ignored, so can't
NeilBrown8e0e99b2014-10-02 13:45:00 +10007267 * guarantee discard_zeroes_data
Shaohua Li620125f2012-10-11 13:49:05 +11007268 */
7269 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10007270
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007271 blk_queue_max_write_same_sectors(mddev->queue, 0);
7272
NeilBrown05616be2012-05-21 09:27:00 +10007273 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007274 disk_stack_limits(mddev->gendisk, rdev->bdev,
7275 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10007276 disk_stack_limits(mddev->gendisk, rdev->bdev,
7277 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11007278 /*
7279 * discard_zeroes_data is required, otherwise data
7280 * could be lost. Consider a scenario: discard a stripe
7281 * (the stripe could be inconsistent if
7282 * discard_zeroes_data is 0); write one disk of the
7283 * stripe (the stripe could be inconsistent again
7284 * depending on which disks are used to calculate
7285 * parity); the disk is broken; The stripe data of this
7286 * disk is lost.
7287 */
7288 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
7289 !bdev_get_queue(rdev->bdev)->
7290 limits.discard_zeroes_data)
7291 discard_supported = false;
NeilBrown8e0e99b2014-10-02 13:45:00 +10007292 /* Unfortunately, discard_zeroes_data is not currently
7293 * a guarantee - just a hint. So we only allow DISCARD
7294 * if the sysadmin has confirmed that only safe devices
7295 * are in use by setting a module parameter.
7296 */
7297 if (!devices_handle_discard_safely) {
7298 if (discard_supported) {
7299 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
7300 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
7301 }
7302 discard_supported = false;
7303 }
NeilBrown05616be2012-05-21 09:27:00 +10007304 }
Shaohua Li620125f2012-10-11 13:49:05 +11007305
7306 if (discard_supported &&
Jes Sorensene7597e62016-02-16 16:44:24 -05007307 mddev->queue->limits.max_discard_sectors >= (stripe >> 9) &&
7308 mddev->queue->limits.discard_granularity >= stripe)
Shaohua Li620125f2012-10-11 13:49:05 +11007309 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
7310 mddev->queue);
7311 else
7312 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
7313 mddev->queue);
Shaohua Li1dffddd2016-09-08 10:49:06 -07007314
7315 blk_queue_max_hw_sectors(mddev->queue, UINT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316 }
7317
Shaohua Li5c7e81c2015-08-13 14:32:04 -07007318 if (journal_dev) {
7319 char b[BDEVNAME_SIZE];
7320
NeilBrowncc6167b2016-11-02 14:16:50 +11007321 pr_debug("md/raid:%s: using device %s as journal\n",
7322 mdname(mddev), bdevname(journal_dev->bdev, b));
Song Liu5aabf7c2016-11-17 15:24:44 -08007323 if (r5l_init_log(conf, journal_dev))
7324 goto abort;
Shaohua Li5c7e81c2015-08-13 14:32:04 -07007325 }
7326
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327 return 0;
7328abort:
NeilBrown01f96c02011-09-21 15:30:20 +10007329 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11007330 print_raid5_conf(conf);
7331 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007332 mddev->private = NULL;
NeilBrowncc6167b2016-11-02 14:16:50 +11007333 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 return -EIO;
7335}
7336
NeilBrownafa0f552014-12-15 12:56:58 +11007337static void raid5_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007338{
NeilBrownafa0f552014-12-15 12:56:58 +11007339 struct r5conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007340
Dan Williams95fc17a2009-07-31 12:39:15 +10007341 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10007342 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007343}
7344
Shaohua Li849674e2016-01-20 13:52:20 -08007345static void raid5_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346{
NeilBrownd1688a62011-10-11 16:49:52 +11007347 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007348 int i;
7349
Andre Noll9d8f0362009-06-18 08:45:01 +10007350 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
NeilBrown3cb5edf2015-07-15 17:24:17 +10007351 conf->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07007352 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
NeilBrown5fd13352016-06-02 16:19:52 +10007353 rcu_read_lock();
7354 for (i = 0; i < conf->raid_disks; i++) {
7355 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
7356 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
7357 }
7358 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360}
7361
NeilBrownd1688a62011-10-11 16:49:52 +11007362static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007363{
7364 int i;
7365 struct disk_info *tmp;
7366
NeilBrowncc6167b2016-11-02 14:16:50 +11007367 pr_debug("RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368 if (!conf) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007369 pr_debug("(conf==NULL)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370 return;
7371 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007372 pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level,
NeilBrown0c55e022010-05-03 14:09:02 +10007373 conf->raid_disks,
7374 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375
7376 for (i = 0; i < conf->raid_disks; i++) {
7377 char b[BDEVNAME_SIZE];
7378 tmp = conf->disks + i;
7379 if (tmp->rdev)
NeilBrowncc6167b2016-11-02 14:16:50 +11007380 pr_debug(" disk %d, o:%d, dev:%s\n",
NeilBrown0c55e022010-05-03 14:09:02 +10007381 i, !test_bit(Faulty, &tmp->rdev->flags),
7382 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007383 }
7384}
7385
NeilBrownfd01b882011-10-11 16:47:53 +11007386static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007387{
7388 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11007389 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007390 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10007391 int count = 0;
7392 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393
7394 for (i = 0; i < conf->raid_disks; i++) {
7395 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11007396 if (tmp->replacement
7397 && tmp->replacement->recovery_offset == MaxSector
7398 && !test_bit(Faulty, &tmp->replacement->flags)
7399 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
7400 /* Replacement has just become active. */
7401 if (!tmp->rdev
7402 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
7403 count++;
7404 if (tmp->rdev) {
7405 /* Replaced device not technically faulty,
7406 * but we need to be sure it gets removed
7407 * and never re-added.
7408 */
7409 set_bit(Faulty, &tmp->rdev->flags);
7410 sysfs_notify_dirent_safe(
7411 tmp->rdev->sysfs_state);
7412 }
7413 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
7414 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10007415 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08007416 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07007417 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10007418 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11007419 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420 }
7421 }
NeilBrown6b965622010-08-18 11:56:59 +10007422 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08007423 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007424 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007425 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007426 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007427}
7428
NeilBrownb8321b62011-12-23 10:17:51 +11007429static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007430{
NeilBrownd1688a62011-10-11 16:49:52 +11007431 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11007433 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11007434 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435 struct disk_info *p = conf->disks + number;
7436
7437 print_raid5_conf(conf);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007438 if (test_bit(Journal, &rdev->flags) && conf->log) {
7439 struct r5l_log *log;
Shaohua Lic2bb6242015-10-08 21:54:07 -07007440 /*
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007441 * we can't wait pending write here, as this is called in
7442 * raid5d, wait will deadlock.
Shaohua Lic2bb6242015-10-08 21:54:07 -07007443 */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007444 if (atomic_read(&mddev->writes_pending))
7445 return -EBUSY;
7446 log = conf->log;
7447 conf->log = NULL;
7448 synchronize_rcu();
7449 r5l_exit_log(log);
7450 return 0;
Shaohua Lic2bb6242015-10-08 21:54:07 -07007451 }
NeilBrown657e3e42011-12-23 10:17:52 +11007452 if (rdev == p->rdev)
7453 rdevp = &p->rdev;
7454 else if (rdev == p->replacement)
7455 rdevp = &p->replacement;
7456 else
7457 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11007458
NeilBrown657e3e42011-12-23 10:17:52 +11007459 if (number >= conf->raid_disks &&
7460 conf->reshape_progress == MaxSector)
7461 clear_bit(In_sync, &rdev->flags);
7462
7463 if (test_bit(In_sync, &rdev->flags) ||
7464 atomic_read(&rdev->nr_pending)) {
7465 err = -EBUSY;
7466 goto abort;
7467 }
7468 /* Only remove non-faulty devices if recovery
7469 * isn't possible.
7470 */
7471 if (!test_bit(Faulty, &rdev->flags) &&
7472 mddev->recovery_disabled != conf->recovery_disabled &&
7473 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11007474 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11007475 number < conf->raid_disks) {
7476 err = -EBUSY;
7477 goto abort;
7478 }
7479 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10007480 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
7481 synchronize_rcu();
7482 if (atomic_read(&rdev->nr_pending)) {
7483 /* lost the race, try later */
7484 err = -EBUSY;
7485 *rdevp = rdev;
7486 }
7487 }
7488 if (p->replacement) {
NeilBrowndd054fc2011-12-23 10:17:53 +11007489 /* We must have just cleared 'rdev' */
7490 p->rdev = p->replacement;
7491 clear_bit(Replacement, &p->replacement->flags);
7492 smp_mb(); /* Make sure other CPUs may see both as identical
7493 * but will never see neither - if they are careful
7494 */
7495 p->replacement = NULL;
7496 clear_bit(WantReplacement, &rdev->flags);
7497 } else
7498 /* We might have just removed the Replacement as faulty-
7499 * clear the bit just in case
7500 */
7501 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502abort:
7503
7504 print_raid5_conf(conf);
7505 return err;
7506}
7507
NeilBrownfd01b882011-10-11 16:47:53 +11007508static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509{
NeilBrownd1688a62011-10-11 16:49:52 +11007510 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10007511 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 int disk;
7513 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10007514 int first = 0;
7515 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007517 if (test_bit(Journal, &rdev->flags)) {
7518 char b[BDEVNAME_SIZE];
7519 if (conf->log)
7520 return -EBUSY;
7521
7522 rdev->raid_disk = 0;
7523 /*
7524 * The array is in readonly mode if journal is missing, so no
7525 * write requests running. We should be safe
7526 */
7527 r5l_init_log(conf, rdev);
NeilBrowncc6167b2016-11-02 14:16:50 +11007528 pr_debug("md/raid:%s: using device %s as journal\n",
7529 mdname(mddev), bdevname(rdev->bdev, b));
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007530 return 0;
7531 }
NeilBrown7f0da592011-07-28 11:39:22 +10007532 if (mddev->recovery_disabled == conf->recovery_disabled)
7533 return -EBUSY;
7534
NeilBrowndc10c642012-03-19 12:46:37 +11007535 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10007537 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007538
Neil Brown6c2fce22008-06-28 08:31:31 +10007539 if (rdev->raid_disk >= 0)
7540 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541
7542 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07007543 * find the disk ... but prefer rdev->saved_raid_disk
7544 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545 */
NeilBrown16a53ec2006-06-26 00:27:38 -07007546 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10007547 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07007548 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10007549 first = rdev->saved_raid_disk;
7550
7551 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007552 p = conf->disks + disk;
7553 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08007554 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007555 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10007556 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07007557 if (rdev->saved_raid_disk != disk)
7558 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08007559 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10007560 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007561 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007562 }
7563 for (disk = first; disk <= last; disk++) {
7564 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007565 if (test_bit(WantReplacement, &p->rdev->flags) &&
7566 p->replacement == NULL) {
7567 clear_bit(In_sync, &rdev->flags);
7568 set_bit(Replacement, &rdev->flags);
7569 rdev->raid_disk = disk;
7570 err = 0;
7571 conf->fullsync = 1;
7572 rcu_assign_pointer(p->replacement, rdev);
7573 break;
7574 }
7575 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007576out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007577 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10007578 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579}
7580
NeilBrownfd01b882011-10-11 16:47:53 +11007581static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007582{
7583 /* no resync is happening, and there is enough space
7584 * on all devices, so we can resize.
7585 * We need to make sure resync covers any new space.
7586 * If the array is shrinking we should possibly wait until
7587 * any io in the removed space completes, but it hardly seems
7588 * worth it.
7589 */
NeilBrowna4a61252012-05-22 13:55:27 +10007590 sector_t newsize;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007591 struct r5conf *conf = mddev->private;
7592
Shaohua Li713cf5a2015-08-13 14:32:03 -07007593 if (conf->log)
7594 return -EINVAL;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007595 sectors &= ~((sector_t)conf->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10007596 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
7597 if (mddev->external_size &&
7598 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11007599 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10007600 if (mddev->bitmap) {
7601 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
7602 if (ret)
7603 return ret;
7604 }
7605 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10007606 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10007607 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10007608 if (sectors > mddev->dev_sectors &&
7609 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11007610 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007611 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7612 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007613 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07007614 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615 return 0;
7616}
7617
NeilBrownfd01b882011-10-11 16:47:53 +11007618static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10007619{
7620 /* Can only proceed if there are plenty of stripe_heads.
7621 * We need a minimum of one full stripe,, and for sensible progress
7622 * it is best to have about 4 times that.
7623 * If we require 4 times, then the default 256 4K stripe_heads will
7624 * allow for chunk sizes up to 256K, which is probably OK.
7625 * If the chunk size is greater, user-space should request more
7626 * stripe_heads first.
7627 */
NeilBrownd1688a62011-10-11 16:49:52 +11007628 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10007629 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007630 > conf->min_nr_stripes ||
NeilBrown01ee22b2009-06-18 08:47:20 +10007631 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007632 > conf->min_nr_stripes) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007633 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7634 mdname(mddev),
7635 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
7636 / STRIPE_SIZE)*4);
NeilBrown01ee22b2009-06-18 08:47:20 +10007637 return 0;
7638 }
7639 return 1;
7640}
7641
NeilBrownfd01b882011-10-11 16:47:53 +11007642static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08007643{
NeilBrownd1688a62011-10-11 16:49:52 +11007644 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08007645
Shaohua Li713cf5a2015-08-13 14:32:03 -07007646 if (conf->log)
7647 return -EINVAL;
NeilBrown88ce4932009-03-31 15:24:23 +11007648 if (mddev->delta_disks == 0 &&
7649 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10007650 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10007651 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10007652 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11007653 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10007654 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11007655 /* We might be able to shrink, but the devices must
7656 * be made bigger first.
7657 * For raid6, 4 is the minimum size.
7658 * Otherwise 2 is the minimum
7659 */
7660 int min = 2;
7661 if (mddev->level == 6)
7662 min = 4;
7663 if (mddev->raid_disks + mddev->delta_disks < min)
7664 return -EINVAL;
7665 }
NeilBrown29269552006-03-27 01:18:10 -08007666
NeilBrown01ee22b2009-06-18 08:47:20 +10007667 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08007668 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08007669
NeilBrown738a2732015-05-08 18:19:39 +10007670 if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
7671 mddev->delta_disks > 0)
7672 if (resize_chunks(conf,
7673 conf->previous_raid_disks
7674 + max(0, mddev->delta_disks),
7675 max(mddev->new_chunk_sectors,
7676 mddev->chunk_sectors)
7677 ) < 0)
7678 return -ENOMEM;
NeilBrowne56108d62012-10-11 14:24:13 +11007679 return resize_stripes(conf, (conf->previous_raid_disks
7680 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08007681}
7682
NeilBrownfd01b882011-10-11 16:47:53 +11007683static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08007684{
NeilBrownd1688a62011-10-11 16:49:52 +11007685 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11007686 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08007687 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07007688 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08007689
NeilBrownf4168852007-02-28 20:11:53 -08007690 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08007691 return -EBUSY;
7692
NeilBrown01ee22b2009-06-18 08:47:20 +10007693 if (!check_stripe_cache(mddev))
7694 return -ENOSPC;
7695
NeilBrown30b67642012-05-22 13:55:28 +10007696 if (has_failed(conf))
7697 return -EINVAL;
7698
NeilBrownc6563a82012-05-21 09:27:00 +10007699 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11007700 if (!test_bit(In_sync, &rdev->flags)
7701 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08007702 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10007703 }
NeilBrown63c70c42006-03-27 01:18:13 -08007704
NeilBrownf4168852007-02-28 20:11:53 -08007705 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08007706 /* Not enough devices even to make a degraded array
7707 * of that size
7708 */
7709 return -EINVAL;
7710
NeilBrownec32a2b2009-03-31 15:17:38 +11007711 /* Refuse to reduce size of the array. Any reductions in
7712 * array size must be through explicit setting of array_size
7713 * attribute.
7714 */
7715 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
7716 < mddev->array_sectors) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007717 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
7718 mdname(mddev));
NeilBrownec32a2b2009-03-31 15:17:38 +11007719 return -EINVAL;
7720 }
7721
NeilBrownf6705572006-03-27 01:18:11 -08007722 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08007723 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10007724 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007725 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08007726 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007727 conf->prev_chunk_sectors = conf->chunk_sectors;
7728 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11007729 conf->prev_algo = conf->algorithm;
7730 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10007731 conf->generation++;
7732 /* Code that selects data_offset needs to see the generation update
7733 * if reshape_progress has been set - so a memory barrier needed.
7734 */
7735 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10007736 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11007737 conf->reshape_progress = raid5_size(mddev, 0, 0);
7738 else
7739 conf->reshape_progress = 0;
7740 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10007741 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007742 spin_unlock_irq(&conf->device_lock);
7743
NeilBrown4d77e3b2013-08-27 15:57:47 +10007744 /* Now make sure any requests that proceeded on the assumption
7745 * the reshape wasn't running - like Discard or Read - have
7746 * completed.
7747 */
7748 mddev_suspend(mddev);
7749 mddev_resume(mddev);
7750
NeilBrown29269552006-03-27 01:18:10 -08007751 /* Add some new drives, as many as will fit.
7752 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10007753 * Don't add devices if we are reducing the number of
7754 * devices in the array. This is because it is not possible
7755 * to correctly record the "partially reconstructed" state of
7756 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08007757 */
NeilBrown87a8dec2011-01-31 11:57:43 +11007758 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11007759 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11007760 if (rdev->raid_disk < 0 &&
7761 !test_bit(Faulty, &rdev->flags)) {
7762 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11007763 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11007764 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11007765 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11007766 else
NeilBrown87a8dec2011-01-31 11:57:43 +11007767 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10007768
7769 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11007770 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11007771 }
NeilBrown87a8dec2011-01-31 11:57:43 +11007772 } else if (rdev->raid_disk >= conf->previous_raid_disks
7773 && !test_bit(Faulty, &rdev->flags)) {
7774 /* This is a spare that was manually added */
7775 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11007776 }
NeilBrown29269552006-03-27 01:18:10 -08007777
NeilBrown87a8dec2011-01-31 11:57:43 +11007778 /* When a reshape changes the number of devices,
7779 * ->degraded is measured against the larger of the
7780 * pre and post number of devices.
7781 */
NeilBrownec32a2b2009-03-31 15:17:38 +11007782 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08007783 mddev->degraded = raid5_calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11007784 spin_unlock_irqrestore(&conf->device_lock, flags);
7785 }
NeilBrown63c70c42006-03-27 01:18:13 -08007786 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10007787 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08007788 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownf6705572006-03-27 01:18:11 -08007789
NeilBrown29269552006-03-27 01:18:10 -08007790 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7791 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10007792 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown29269552006-03-27 01:18:10 -08007793 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7794 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7795 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007796 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08007797 if (!mddev->sync_thread) {
7798 mddev->recovery = 0;
7799 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11007800 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007801 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11007802 mddev->new_chunk_sectors =
7803 conf->chunk_sectors = conf->prev_chunk_sectors;
7804 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10007805 rdev_for_each(rdev, mddev)
7806 rdev->new_data_offset = rdev->data_offset;
7807 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11007808 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11007809 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11007810 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11007811 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007812 spin_unlock_irq(&conf->device_lock);
7813 return -EAGAIN;
7814 }
NeilBrownc8f517c2009-03-31 15:28:40 +11007815 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08007816 md_wakeup_thread(mddev->sync_thread);
7817 md_new_event(mddev);
7818 return 0;
7819}
NeilBrown29269552006-03-27 01:18:10 -08007820
NeilBrownec32a2b2009-03-31 15:17:38 +11007821/* This is called from the reshape thread and should make any
7822 * changes needed in 'conf'
7823 */
NeilBrownd1688a62011-10-11 16:49:52 +11007824static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08007825{
NeilBrown29269552006-03-27 01:18:10 -08007826
NeilBrownf6705572006-03-27 01:18:11 -08007827 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10007828 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07007829
NeilBrownf6705572006-03-27 01:18:11 -08007830 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11007831 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10007832 rdev_for_each(rdev, conf->mddev)
7833 rdev->data_offset = rdev->new_data_offset;
7834 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11007835 conf->reshape_progress = MaxSector;
NeilBrown6cbd8142015-07-24 13:30:32 +10007836 conf->mddev->reshape_position = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08007837 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11007838 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07007839
7840 /* read-ahead size must cover two whole stripes, which is
7841 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
7842 */
NeilBrown4a5add42010-06-01 19:37:28 +10007843 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11007844 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007845 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11007846 / PAGE_SIZE);
Jan Karadc3b17c2017-02-02 15:56:50 +01007847 if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
7848 conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown16a53ec2006-06-26 00:27:38 -07007849 }
NeilBrown29269552006-03-27 01:18:10 -08007850 }
NeilBrown29269552006-03-27 01:18:10 -08007851}
7852
NeilBrownec32a2b2009-03-31 15:17:38 +11007853/* This is called from the raid5d thread with mddev_lock held.
7854 * It makes config changes to the device.
7855 */
NeilBrownfd01b882011-10-11 16:47:53 +11007856static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11007857{
NeilBrownd1688a62011-10-11 16:49:52 +11007858 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11007859
7860 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7861
NeilBrownec32a2b2009-03-31 15:17:38 +11007862 if (mddev->delta_disks > 0) {
7863 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
Heinz Mauelshagenfe67d192016-05-03 22:15:31 +02007864 if (mddev->queue) {
7865 set_capacity(mddev->gendisk, mddev->array_sectors);
7866 revalidate_disk(mddev->gendisk);
7867 }
NeilBrownec32a2b2009-03-31 15:17:38 +11007868 } else {
7869 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11007870 spin_lock_irq(&conf->device_lock);
Song Liu2e38a372017-01-24 10:45:30 -08007871 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11007872 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11007873 for (d = conf->raid_disks ;
7874 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10007875 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11007876 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10007877 if (rdev)
7878 clear_bit(In_sync, &rdev->flags);
7879 rdev = conf->disks[d].replacement;
7880 if (rdev)
7881 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10007882 }
NeilBrowncea9c222009-03-31 15:15:05 +11007883 }
NeilBrown88ce4932009-03-31 15:24:23 +11007884 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007885 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11007886 mddev->reshape_position = MaxSector;
7887 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007888 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11007889 }
7890}
7891
NeilBrownfd01b882011-10-11 16:47:53 +11007892static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07007893{
NeilBrownd1688a62011-10-11 16:49:52 +11007894 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07007895
7896 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08007897 case 2: /* resume for a suspend */
7898 wake_up(&conf->wait_for_overlap);
7899 break;
7900
NeilBrown72626682005-09-09 16:23:54 -07007901 case 1: /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11007902 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10007903 /* '2' tells resync/reshape to pause so that all
7904 * active stripes can drain
7905 */
Song Liua39f7af2016-11-17 15:24:40 -08007906 r5c_flush_cache(conf, INT_MAX);
NeilBrown64bd6602009-08-03 10:59:58 +10007907 conf->quiesce = 2;
Yuanhan Liub1b46482015-05-08 18:19:06 +10007908 wait_event_cmd(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08007909 atomic_read(&conf->active_stripes) == 0 &&
7910 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11007911 unlock_all_device_hash_locks_irq(conf),
7912 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10007913 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11007914 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10007915 /* allow reshape to continue */
7916 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07007917 break;
7918
7919 case 0: /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11007920 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07007921 conf->quiesce = 0;
Yuanhan Liub1b46482015-05-08 18:19:06 +10007922 wake_up(&conf->wait_for_quiescent);
NeilBrowne464eaf2006-03-27 01:18:14 -08007923 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11007924 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07007925 break;
7926 }
Shaohua Lie6c033f2015-10-04 09:20:12 -07007927 r5l_quiesce(conf->log, state);
NeilBrown72626682005-09-09 16:23:54 -07007928}
NeilBrownb15c2e52006-01-06 00:20:16 -08007929
NeilBrownfd01b882011-10-11 16:47:53 +11007930static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11007931{
NeilBrowne373ab12011-10-11 16:48:59 +11007932 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07007933 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11007934
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007935 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11007936 if (raid0_conf->nr_strip_zones > 1) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007937 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
7938 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007939 return ERR_PTR(-EINVAL);
7940 }
7941
NeilBrowne373ab12011-10-11 16:48:59 +11007942 sectors = raid0_conf->strip_zone[0].zone_end;
7943 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10007944 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007945 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11007946 mddev->new_layout = ALGORITHM_PARITY_N;
7947 mddev->new_chunk_sectors = mddev->chunk_sectors;
7948 mddev->raid_disks += 1;
7949 mddev->delta_disks = 1;
7950 /* make sure it will be not marked as dirty */
7951 mddev->recovery_cp = MaxSector;
7952
7953 return setup_conf(mddev);
7954}
7955
NeilBrownfd01b882011-10-11 16:47:53 +11007956static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11007957{
7958 int chunksect;
Shaohua Li6995f0b2016-12-08 15:48:17 -08007959 void *ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11007960
7961 if (mddev->raid_disks != 2 ||
7962 mddev->degraded > 1)
7963 return ERR_PTR(-EINVAL);
7964
7965 /* Should check if there are write-behind devices? */
7966
7967 chunksect = 64*2; /* 64K by default */
7968
7969 /* The array must be an exact multiple of chunksize */
7970 while (chunksect && (mddev->array_sectors & (chunksect-1)))
7971 chunksect >>= 1;
7972
7973 if ((chunksect<<9) < STRIPE_SIZE)
7974 /* array size does not allow a suitable chunk size */
7975 return ERR_PTR(-EINVAL);
7976
7977 mddev->new_level = 5;
7978 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10007979 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11007980
Shaohua Li6995f0b2016-12-08 15:48:17 -08007981 ret = setup_conf(mddev);
Jes Sorensen32cd7cb2017-01-06 19:31:35 -05007982 if (!IS_ERR(ret))
Shaohua Li394ed8e2017-01-04 16:10:19 -08007983 mddev_clear_unsupported_flags(mddev,
7984 UNSUPPORTED_MDDEV_FLAGS);
Shaohua Li6995f0b2016-12-08 15:48:17 -08007985 return ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11007986}
7987
NeilBrownfd01b882011-10-11 16:47:53 +11007988static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11007989{
7990 int new_layout;
7991
7992 switch (mddev->layout) {
7993 case ALGORITHM_LEFT_ASYMMETRIC_6:
7994 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
7995 break;
7996 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7997 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
7998 break;
7999 case ALGORITHM_LEFT_SYMMETRIC_6:
8000 new_layout = ALGORITHM_LEFT_SYMMETRIC;
8001 break;
8002 case ALGORITHM_RIGHT_SYMMETRIC_6:
8003 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
8004 break;
8005 case ALGORITHM_PARITY_0_6:
8006 new_layout = ALGORITHM_PARITY_0;
8007 break;
8008 case ALGORITHM_PARITY_N:
8009 new_layout = ALGORITHM_PARITY_N;
8010 break;
8011 default:
8012 return ERR_PTR(-EINVAL);
8013 }
8014 mddev->new_level = 5;
8015 mddev->new_layout = new_layout;
8016 mddev->delta_disks = -1;
8017 mddev->raid_disks -= 1;
8018 return setup_conf(mddev);
8019}
8020
NeilBrownfd01b882011-10-11 16:47:53 +11008021static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11008022{
NeilBrown88ce4932009-03-31 15:24:23 +11008023 /* For a 2-drive array, the layout and chunk size can be changed
8024 * immediately as not restriping is needed.
8025 * For larger arrays we record the new value - after validation
8026 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11008027 */
NeilBrownd1688a62011-10-11 16:49:52 +11008028 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10008029 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11008030
NeilBrown597a7112009-06-18 08:47:42 +10008031 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11008032 return -EINVAL;
8033 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008034 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11008035 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008036 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11008037 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008038 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11008039 /* not factor of array size */
8040 return -EINVAL;
8041 }
8042
8043 /* They look valid */
8044
NeilBrown88ce4932009-03-31 15:24:23 +11008045 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10008046 /* can make the change immediately */
8047 if (mddev->new_layout >= 0) {
8048 conf->algorithm = mddev->new_layout;
8049 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11008050 }
8051 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10008052 conf->chunk_sectors = new_chunk ;
8053 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11008054 }
Shaohua Li29530792016-12-08 15:48:19 -08008055 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown88ce4932009-03-31 15:24:23 +11008056 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11008057 }
NeilBrown50ac1682009-06-18 08:47:55 +10008058 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11008059}
8060
NeilBrownfd01b882011-10-11 16:47:53 +11008061static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11008062{
NeilBrown597a7112009-06-18 08:47:42 +10008063 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10008064
NeilBrown597a7112009-06-18 08:47:42 +10008065 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11008066 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008067 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008068 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11008069 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008070 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11008071 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008072 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11008073 /* not factor of array size */
8074 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008075 }
NeilBrown88ce4932009-03-31 15:24:23 +11008076
8077 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10008078 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11008079}
8080
NeilBrownfd01b882011-10-11 16:47:53 +11008081static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11008082{
8083 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008084 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008085 * raid1 - if there are two drives. We need to know the chunk size
8086 * raid4 - trivial - just use a raid4 layout.
8087 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008088 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008089 if (mddev->level == 0)
8090 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11008091 if (mddev->level == 1)
8092 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11008093 if (mddev->level == 4) {
8094 mddev->new_layout = ALGORITHM_PARITY_N;
8095 mddev->new_level = 5;
8096 return setup_conf(mddev);
8097 }
NeilBrownfc9739c2009-03-31 14:57:20 +11008098 if (mddev->level == 6)
8099 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11008100
8101 return ERR_PTR(-EINVAL);
8102}
8103
NeilBrownfd01b882011-10-11 16:47:53 +11008104static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11008105{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008106 /* raid4 can take over:
8107 * raid0 - if there is only one strip zone
8108 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11008109 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008110 if (mddev->level == 0)
8111 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11008112 if (mddev->level == 5 &&
8113 mddev->layout == ALGORITHM_PARITY_N) {
8114 mddev->new_layout = 0;
8115 mddev->new_level = 4;
8116 return setup_conf(mddev);
8117 }
8118 return ERR_PTR(-EINVAL);
8119}
NeilBrownd562b0c2009-03-31 14:39:39 +11008120
NeilBrown84fc4b52011-10-11 16:49:58 +11008121static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11008122
NeilBrownfd01b882011-10-11 16:47:53 +11008123static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11008124{
8125 /* Currently can only take over a raid5. We map the
8126 * personality to an equivalent raid6 personality
8127 * with the Q block at the end.
8128 */
8129 int new_layout;
8130
8131 if (mddev->pers != &raid5_personality)
8132 return ERR_PTR(-EINVAL);
8133 if (mddev->degraded > 1)
8134 return ERR_PTR(-EINVAL);
8135 if (mddev->raid_disks > 253)
8136 return ERR_PTR(-EINVAL);
8137 if (mddev->raid_disks < 3)
8138 return ERR_PTR(-EINVAL);
8139
8140 switch (mddev->layout) {
8141 case ALGORITHM_LEFT_ASYMMETRIC:
8142 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
8143 break;
8144 case ALGORITHM_RIGHT_ASYMMETRIC:
8145 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
8146 break;
8147 case ALGORITHM_LEFT_SYMMETRIC:
8148 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
8149 break;
8150 case ALGORITHM_RIGHT_SYMMETRIC:
8151 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
8152 break;
8153 case ALGORITHM_PARITY_0:
8154 new_layout = ALGORITHM_PARITY_0_6;
8155 break;
8156 case ALGORITHM_PARITY_N:
8157 new_layout = ALGORITHM_PARITY_N;
8158 break;
8159 default:
8160 return ERR_PTR(-EINVAL);
8161 }
8162 mddev->new_level = 6;
8163 mddev->new_layout = new_layout;
8164 mddev->delta_disks = 1;
8165 mddev->raid_disks += 1;
8166 return setup_conf(mddev);
8167}
8168
NeilBrown84fc4b52011-10-11 16:49:58 +11008169static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07008170{
8171 .name = "raid6",
8172 .level = 6,
8173 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008174 .make_request = raid5_make_request,
8175 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11008176 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008177 .status = raid5_status,
8178 .error_handler = raid5_error,
NeilBrown16a53ec2006-06-26 00:27:38 -07008179 .hot_add_disk = raid5_add_disk,
8180 .hot_remove_disk= raid5_remove_disk,
8181 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008182 .sync_request = raid5_sync_request,
NeilBrown16a53ec2006-06-26 00:27:38 -07008183 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008184 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10008185 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08008186 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008187 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07008188 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11008189 .takeover = raid6_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008190 .congested = raid5_congested,
NeilBrown16a53ec2006-06-26 00:27:38 -07008191};
NeilBrown84fc4b52011-10-11 16:49:58 +11008192static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193{
8194 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08008195 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008202 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008206 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008207 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18: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,
NeilBrown72626682005-09-09 16:23:54 -07008211 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11008212 .takeover = raid5_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008213 .congested = raid5_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214};
8215
NeilBrown84fc4b52011-10-11 16:49:58 +11008216static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008217{
NeilBrown2604b702006-01-06 00:20:36 -08008218 .name = "raid4",
8219 .level = 4,
8220 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008221 .make_request = raid5_make_request,
8222 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11008223 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008224 .status = raid5_status,
8225 .error_handler = raid5_error,
NeilBrown2604b702006-01-06 00:20:36 -08008226 .hot_add_disk = raid5_add_disk,
8227 .hot_remove_disk= raid5_remove_disk,
8228 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008229 .sync_request = raid5_sync_request,
NeilBrown2604b702006-01-06 00:20:36 -08008230 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008231 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08008232 .check_reshape = raid5_check_reshape,
8233 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008234 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08008235 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11008236 .takeover = raid4_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008237 .congested = raid5_congested,
NeilBrown2604b702006-01-06 00:20:36 -08008238};
8239
8240static int __init raid5_init(void)
8241{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008242 int ret;
8243
Shaohua Li851c30c2013-08-28 14:30:16 +08008244 raid5_wq = alloc_workqueue("raid5wq",
8245 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
8246 if (!raid5_wq)
8247 return -ENOMEM;
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008248
8249 ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
8250 "md/raid5:prepare",
8251 raid456_cpu_up_prepare,
8252 raid456_cpu_dead);
8253 if (ret) {
8254 destroy_workqueue(raid5_wq);
8255 return ret;
8256 }
NeilBrown16a53ec2006-06-26 00:27:38 -07008257 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008258 register_md_personality(&raid5_personality);
8259 register_md_personality(&raid4_personality);
8260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008261}
8262
NeilBrown2604b702006-01-06 00:20:36 -08008263static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264{
NeilBrown16a53ec2006-06-26 00:27:38 -07008265 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008266 unregister_md_personality(&raid5_personality);
8267 unregister_md_personality(&raid4_personality);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008268 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
Shaohua Li851c30c2013-08-28 14:30:16 +08008269 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008270}
8271
8272module_init(raid5_init);
8273module_exit(raid5_exit);
8274MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008275MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008276MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08008277MODULE_ALIAS("md-raid5");
8278MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08008279MODULE_ALIAS("md-level-5");
8280MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07008281MODULE_ALIAS("md-personality-8"); /* RAID6 */
8282MODULE_ALIAS("md-raid6");
8283MODULE_ALIAS("md-level-6");
8284
8285/* This used to be two separate modules, they were: */
8286MODULE_ALIAS("raid5");
8287MODULE_ALIAS("raid6");