blob: 7aeb9691c2e1c7c3180343a16eb759363aae9592 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
Shaohua Li851c30c2013-08-28 14:30:16 +080056#include <linux/nodemask.h>
shli@kernel.org46d5b782014-12-15 12:57:02 +110057#include <linux/flex_array.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010058#include <linux/sched/signal.h>
59
NeilBrowna9add5d2012-10-31 11:59:09 +110060#include <trace/events/block.h>
61
NeilBrown43b2e5d2009-03-31 14:33:13 +110062#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110063#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110064#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110065#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070066
Shaohua Li394ed8e2017-01-04 16:10:19 -080067#define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED)
68
Shaohua Li851c30c2013-08-28 14:30:16 +080069#define cpu_to_group(cpu) cpu_to_node(cpu)
70#define ANY_GROUP NUMA_NO_NODE
71
NeilBrown8e0e99b2014-10-02 13:45:00 +100072static bool devices_handle_discard_safely = false;
73module_param(devices_handle_discard_safely, bool, 0644);
74MODULE_PARM_DESC(devices_handle_discard_safely,
75 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
Shaohua Li851c30c2013-08-28 14:30:16 +080076static struct workqueue_struct *raid5_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
NeilBrownd1688a62011-10-11 16:49:52 +110078static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110079{
80 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
81 return &conf->stripe_hashtbl[hash];
82}
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Shaohua Li566c09c2013-11-14 15:16:17 +110084static inline int stripe_hash_locks_hash(sector_t sect)
85{
86 return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK;
87}
88
89static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
90{
91 spin_lock_irq(conf->hash_locks + hash);
92 spin_lock(&conf->device_lock);
93}
94
95static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
96{
97 spin_unlock(&conf->device_lock);
98 spin_unlock_irq(conf->hash_locks + hash);
99}
100
101static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
102{
103 int i;
104 local_irq_disable();
105 spin_lock(conf->hash_locks);
106 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
107 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
108 spin_lock(&conf->device_lock);
109}
110
111static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
112{
113 int i;
114 spin_unlock(&conf->device_lock);
115 for (i = NR_STRIPE_HASH_LOCKS; i; i--)
116 spin_unlock(conf->hash_locks + i - 1);
117 local_irq_enable();
118}
119
NeilBrownd0dabf72009-03-31 14:39:38 +1100120/* Find first data disk in a raid6 stripe */
121static inline int raid6_d0(struct stripe_head *sh)
122{
NeilBrown67cc2b82009-03-31 14:39:38 +1100123 if (sh->ddf_layout)
124 /* ddf always start from first device */
125 return 0;
126 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100127 if (sh->qd_idx == sh->disks - 1)
128 return 0;
129 else
130 return sh->qd_idx + 1;
131}
NeilBrown16a53ec2006-06-26 00:27:38 -0700132static inline int raid6_next_disk(int disk, int raid_disks)
133{
134 disk++;
135 return (disk < raid_disks) ? disk : 0;
136}
Dan Williamsa4456852007-07-09 11:56:43 -0700137
NeilBrownd0dabf72009-03-31 14:39:38 +1100138/* When walking through the disks in a raid5, starting at raid6_d0,
139 * We need to map each disk to a 'slot', where the data disks are slot
140 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
141 * is raid_disks-1. This help does that mapping.
142 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100143static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
144 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100145{
Dan Williams66295422009-10-19 18:09:32 -0700146 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100147
NeilBrowne4424fe2009-10-16 16:27:34 +1100148 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700149 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100150 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100151 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100152 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100153 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100154 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700155 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100156 return slot;
157}
158
NeilBrown34a6f802015-08-14 12:07:57 +1000159static void return_io(struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -0700160{
NeilBrown34a6f802015-08-14 12:07:57 +1000161 struct bio *bi;
162 while ((bi = bio_list_pop(return_bi)) != NULL) {
Kent Overstreet4f024f32013-10-11 15:44:27 -0700163 bi->bi_iter.bi_size = 0;
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700164 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
165 bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200166 bio_endio(bi);
Dan Williamsa4456852007-07-09 11:56:43 -0700167 }
168}
169
NeilBrownd1688a62011-10-11 16:49:52 +1100170static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Dan Williams600aa102008-06-28 08:32:05 +1000172static int stripe_operations_active(struct stripe_head *sh)
173{
174 return sh->check_state || sh->reconstruct_state ||
175 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
176 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
177}
178
Shaohua Li851c30c2013-08-28 14:30:16 +0800179static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
180{
181 struct r5conf *conf = sh->raid_conf;
182 struct r5worker_group *group;
Shaohua Libfc90cb2013-08-29 15:40:32 +0800183 int thread_cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +0800184 int i, cpu = sh->cpu;
185
186 if (!cpu_online(cpu)) {
187 cpu = cpumask_any(cpu_online_mask);
188 sh->cpu = cpu;
189 }
190
191 if (list_empty(&sh->lru)) {
192 struct r5worker_group *group;
193 group = conf->worker_groups + cpu_to_group(cpu);
194 list_add_tail(&sh->lru, &group->handle_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800195 group->stripes_cnt++;
196 sh->group = group;
Shaohua Li851c30c2013-08-28 14:30:16 +0800197 }
198
199 if (conf->worker_cnt_per_group == 0) {
200 md_wakeup_thread(conf->mddev->thread);
201 return;
202 }
203
204 group = conf->worker_groups + cpu_to_group(sh->cpu);
205
Shaohua Libfc90cb2013-08-29 15:40:32 +0800206 group->workers[0].working = true;
207 /* at least one worker should run to avoid race */
208 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
209
210 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
211 /* wakeup more workers */
212 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
213 if (group->workers[i].working == false) {
214 group->workers[i].working = true;
215 queue_work_on(sh->cpu, raid5_wq,
216 &group->workers[i].work);
217 thread_cnt--;
218 }
219 }
Shaohua Li851c30c2013-08-28 14:30:16 +0800220}
221
Shaohua Li566c09c2013-11-14 15:16:17 +1100222static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
223 struct list_head *temp_inactive_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Song Liu1e6d6902016-11-17 15:24:39 -0800225 int i;
226 int injournal = 0; /* number of date pages with R5_InJournal */
227
Shaohua Li4eb788d2012-07-19 16:01:31 +1000228 BUG_ON(!list_empty(&sh->lru));
229 BUG_ON(atomic_read(&conf->active_stripes)==0);
Song Liu1e6d6902016-11-17 15:24:39 -0800230
231 if (r5c_is_writeback(conf->log))
232 for (i = sh->disks; i--; )
233 if (test_bit(R5_InJournal, &sh->dev[i].flags))
234 injournal++;
Song Liua39f7af2016-11-17 15:24:40 -0800235 /*
236 * When quiesce in r5c write back, set STRIPE_HANDLE for stripes with
237 * data in journal, so they are not released to cached lists
238 */
239 if (conf->quiesce && r5c_is_writeback(conf->log) &&
240 !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0) {
241 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
242 r5c_make_stripe_write_out(sh);
243 set_bit(STRIPE_HANDLE, &sh->state);
244 }
Song Liu1e6d6902016-11-17 15:24:39 -0800245
Shaohua Li4eb788d2012-07-19 16:01:31 +1000246 if (test_bit(STRIPE_HANDLE, &sh->state)) {
247 if (test_bit(STRIPE_DELAYED, &sh->state) &&
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500248 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
Shaohua Li4eb788d2012-07-19 16:01:31 +1000249 list_add_tail(&sh->lru, &conf->delayed_list);
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500250 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
Shaohua Li4eb788d2012-07-19 16:01:31 +1000251 sh->bm_seq - conf->seq_write > 0)
252 list_add_tail(&sh->lru, &conf->bitmap_list);
253 else {
254 clear_bit(STRIPE_DELAYED, &sh->state);
255 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Shaohua Li851c30c2013-08-28 14:30:16 +0800256 if (conf->worker_cnt_per_group == 0) {
257 list_add_tail(&sh->lru, &conf->handle_list);
258 } else {
259 raid5_wakeup_stripe_thread(sh);
260 return;
261 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000262 }
263 md_wakeup_thread(conf->mddev->thread);
264 } else {
265 BUG_ON(stripe_operations_active(sh));
266 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
267 if (atomic_dec_return(&conf->preread_active_stripes)
268 < IO_THRESHOLD)
269 md_wakeup_thread(conf->mddev->thread);
270 atomic_dec(&conf->active_stripes);
Song Liu1e6d6902016-11-17 15:24:39 -0800271 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
272 if (!r5c_is_writeback(conf->log))
273 list_add_tail(&sh->lru, temp_inactive_list);
274 else {
275 WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags));
276 if (injournal == 0)
277 list_add_tail(&sh->lru, temp_inactive_list);
278 else if (injournal == conf->raid_disks - conf->max_degraded) {
279 /* full stripe */
280 if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state))
281 atomic_inc(&conf->r5c_cached_full_stripes);
282 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state))
283 atomic_dec(&conf->r5c_cached_partial_stripes);
284 list_add_tail(&sh->lru, &conf->r5c_full_stripe_list);
Song Liua39f7af2016-11-17 15:24:40 -0800285 r5c_check_cached_full_stripe(conf);
Song Liu03b047f2017-01-11 13:39:14 -0800286 } else
287 /*
288 * STRIPE_R5C_PARTIAL_STRIPE is set in
289 * r5c_try_caching_write(). No need to
290 * set it again.
291 */
Song Liu1e6d6902016-11-17 15:24:39 -0800292 list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list);
Song Liu1e6d6902016-11-17 15:24:39 -0800293 }
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296}
NeilBrownd0dabf72009-03-31 14:39:38 +1100297
Shaohua Li566c09c2013-11-14 15:16:17 +1100298static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
299 struct list_head *temp_inactive_list)
Shaohua Li4eb788d2012-07-19 16:01:31 +1000300{
301 if (atomic_dec_and_test(&sh->count))
Shaohua Li566c09c2013-11-14 15:16:17 +1100302 do_release_stripe(conf, sh, temp_inactive_list);
303}
304
305/*
306 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
307 *
308 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
309 * given time. Adding stripes only takes device lock, while deleting stripes
310 * only takes hash lock.
311 */
312static void release_inactive_stripe_list(struct r5conf *conf,
313 struct list_head *temp_inactive_list,
314 int hash)
315{
316 int size;
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800317 bool do_wakeup = false;
Shaohua Li566c09c2013-11-14 15:16:17 +1100318 unsigned long flags;
319
320 if (hash == NR_STRIPE_HASH_LOCKS) {
321 size = NR_STRIPE_HASH_LOCKS;
322 hash = NR_STRIPE_HASH_LOCKS - 1;
323 } else
324 size = 1;
325 while (size) {
326 struct list_head *list = &temp_inactive_list[size - 1];
327
328 /*
Shaohua Li6d036f72015-08-13 14:31:57 -0700329 * We don't hold any lock here yet, raid5_get_active_stripe() might
Shaohua Li566c09c2013-11-14 15:16:17 +1100330 * remove stripes from the list
331 */
332 if (!list_empty_careful(list)) {
333 spin_lock_irqsave(conf->hash_locks + hash, flags);
Shaohua Li4bda5562013-11-14 15:16:17 +1100334 if (list_empty(conf->inactive_list + hash) &&
335 !list_empty(list))
336 atomic_dec(&conf->empty_inactive_list_nr);
Shaohua Li566c09c2013-11-14 15:16:17 +1100337 list_splice_tail_init(list, conf->inactive_list + hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800338 do_wakeup = true;
Shaohua Li566c09c2013-11-14 15:16:17 +1100339 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
340 }
341 size--;
342 hash--;
343 }
344
345 if (do_wakeup) {
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800346 wake_up(&conf->wait_for_stripe);
Yuanhan Liub1b46482015-05-08 18:19:06 +1000347 if (atomic_read(&conf->active_stripes) == 0)
348 wake_up(&conf->wait_for_quiescent);
Shaohua Li566c09c2013-11-14 15:16:17 +1100349 if (conf->retry_read_aligned)
350 md_wakeup_thread(conf->mddev->thread);
351 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000352}
353
Shaohua Li773ca822013-08-27 17:50:39 +0800354/* should hold conf->device_lock already */
Shaohua Li566c09c2013-11-14 15:16:17 +1100355static int release_stripe_list(struct r5conf *conf,
356 struct list_head *temp_inactive_list)
Shaohua Li773ca822013-08-27 17:50:39 +0800357{
Byungchul Parkeae82632017-02-14 16:26:24 +0900358 struct stripe_head *sh, *t;
Shaohua Li773ca822013-08-27 17:50:39 +0800359 int count = 0;
360 struct llist_node *head;
361
362 head = llist_del_all(&conf->released_stripes);
Shaohua Lid265d9d2013-08-28 14:29:05 +0800363 head = llist_reverse_order(head);
Byungchul Parkeae82632017-02-14 16:26:24 +0900364 llist_for_each_entry_safe(sh, t, head, release_list) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100365 int hash;
366
Shaohua Li773ca822013-08-27 17:50:39 +0800367 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
368 smp_mb();
369 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
370 /*
371 * Don't worry the bit is set here, because if the bit is set
372 * again, the count is always > 1. This is true for
373 * STRIPE_ON_UNPLUG_LIST bit too.
374 */
Shaohua Li566c09c2013-11-14 15:16:17 +1100375 hash = sh->hash_lock_index;
376 __release_stripe(conf, sh, &temp_inactive_list[hash]);
Shaohua Li773ca822013-08-27 17:50:39 +0800377 count++;
378 }
379
380 return count;
381}
382
Shaohua Li6d036f72015-08-13 14:31:57 -0700383void raid5_release_stripe(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
NeilBrownd1688a62011-10-11 16:49:52 +1100385 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 unsigned long flags;
Shaohua Li566c09c2013-11-14 15:16:17 +1100387 struct list_head list;
388 int hash;
Shaohua Li773ca822013-08-27 17:50:39 +0800389 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700390
Eivind Sartocf170f32014-05-28 13:39:23 +1000391 /* Avoid release_list until the last reference.
392 */
393 if (atomic_add_unless(&sh->count, -1, 1))
394 return;
395
majianpengad4068d2013-11-14 15:16:15 +1100396 if (unlikely(!conf->mddev->thread) ||
397 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
Shaohua Li773ca822013-08-27 17:50:39 +0800398 goto slow_path;
399 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
400 if (wakeup)
401 md_wakeup_thread(conf->mddev->thread);
402 return;
403slow_path:
Shaohua Li4eb788d2012-07-19 16:01:31 +1000404 local_irq_save(flags);
Shaohua Li773ca822013-08-27 17:50:39 +0800405 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Shaohua Li4eb788d2012-07-19 16:01:31 +1000406 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100407 INIT_LIST_HEAD(&list);
408 hash = sh->hash_lock_index;
409 do_release_stripe(conf, sh, &list);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000410 spin_unlock(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +1100411 release_inactive_stripe_list(conf, &list, hash);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000412 }
413 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
NeilBrownfccddba2006-01-06 00:20:33 -0800416static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Dan Williams45b42332007-07-09 11:56:43 -0700418 pr_debug("remove_hash(), stripe %llu\n",
419 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
NeilBrownfccddba2006-01-06 00:20:33 -0800421 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
NeilBrownd1688a62011-10-11 16:49:52 +1100424static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
NeilBrownfccddba2006-01-06 00:20:33 -0800426 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Dan Williams45b42332007-07-09 11:56:43 -0700428 pr_debug("insert_hash(), stripe %llu\n",
429 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
NeilBrownfccddba2006-01-06 00:20:33 -0800431 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/* find an idle stripe, make sure it is unhashed, and return it. */
Shaohua Li566c09c2013-11-14 15:16:17 +1100435static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 struct stripe_head *sh = NULL;
438 struct list_head *first;
439
Shaohua Li566c09c2013-11-14 15:16:17 +1100440 if (list_empty(conf->inactive_list + hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 goto out;
Shaohua Li566c09c2013-11-14 15:16:17 +1100442 first = (conf->inactive_list + hash)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 sh = list_entry(first, struct stripe_head, lru);
444 list_del_init(first);
445 remove_hash(sh);
446 atomic_inc(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100447 BUG_ON(hash != sh->hash_lock_index);
Shaohua Li4bda5562013-11-14 15:16:17 +1100448 if (list_empty(conf->inactive_list + hash))
449 atomic_inc(&conf->empty_inactive_list_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450out:
451 return sh;
452}
453
NeilBrowne4e11e32010-06-16 16:45:16 +1000454static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 struct page *p;
457 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000458 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
NeilBrowne4e11e32010-06-16 16:45:16 +1000460 for (i = 0; i < num ; i++) {
Shaohua Lid592a992014-05-21 17:57:44 +0800461 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 p = sh->dev[i].page;
463 if (!p)
464 continue;
465 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800466 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468}
469
NeilBrowna9683a72015-02-25 12:02:51 +1100470static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000473 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
NeilBrowne4e11e32010-06-16 16:45:16 +1000475 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 struct page *page;
477
NeilBrowna9683a72015-02-25 12:02:51 +1100478 if (!(page = alloc_page(gfp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 1;
480 }
481 sh->dev[i].page = page;
Shaohua Lid592a992014-05-21 17:57:44 +0800482 sh->dev[i].orig_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484 return 0;
485}
486
NeilBrown784052e2009-03-31 15:19:07 +1100487static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100488static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100489 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
NeilBrownb5663ba2009-03-31 14:39:38 +1100491static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
NeilBrownd1688a62011-10-11 16:49:52 +1100493 struct r5conf *conf = sh->raid_conf;
Shaohua Li566c09c2013-11-14 15:16:17 +1100494 int i, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200496 BUG_ON(atomic_read(&sh->count) != 0);
497 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000498 BUG_ON(stripe_operations_active(sh));
shli@kernel.org59fc6302014-12-15 12:57:03 +1100499 BUG_ON(sh->batch_head);
Dan Williamsd84e0f12007-01-02 13:52:30 -0700500
Dan Williams45b42332007-07-09 11:56:43 -0700501 pr_debug("init_stripe called, stripe %llu\n",
Markus Stockhausenb8e6a152014-08-23 20:19:27 +1000502 (unsigned long long)sector);
Shaohua Li566c09c2013-11-14 15:16:17 +1100503retry:
504 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100505 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100506 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100508 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 sh->state = 0;
510
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800511 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 struct r5dev *dev = &sh->dev[i];
513
Dan Williamsd84e0f12007-01-02 13:52:30 -0700514 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 test_bit(R5_LOCKED, &dev->flags)) {
NeilBrowncc6167b2016-11-02 14:16:50 +1100516 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700518 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000520 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100523 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100525 if (read_seqcount_retry(&conf->gen_lock, seq))
526 goto retry;
shli@kernel.org7a87f432014-12-15 12:57:03 +1100527 sh->overwrite_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800529 sh->cpu = smp_processor_id();
shli@kernel.orgda41ba62014-12-15 12:57:03 +1100530 set_bit(STRIPE_BATCH_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
NeilBrownd1688a62011-10-11 16:49:52 +1100533static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100534 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 struct stripe_head *sh;
537
Dan Williams45b42332007-07-09 11:56:43 -0700538 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800539 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100540 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700542 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return NULL;
544}
545
NeilBrown674806d2010-06-16 17:17:53 +1000546/*
547 * Need to check if array has failed when deciding whether to:
548 * - start an array
549 * - remove non-faulty devices
550 * - add a spare
551 * - allow a reshape
552 * This determination is simple when no reshape is happening.
553 * However if there is a reshape, we need to carefully check
554 * both the before and after sections.
555 * This is because some failed devices may only affect one
556 * of the two sections, and some non-in_sync devices may
557 * be insync in the section most affected by failed devices.
558 */
Song Liu2e38a372017-01-24 10:45:30 -0800559int raid5_calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000560{
NeilBrown908f4fb2011-12-23 10:17:50 +1100561 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000562 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000563
564 rcu_read_lock();
565 degraded = 0;
566 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100567 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000568 if (rdev && test_bit(Faulty, &rdev->flags))
569 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000570 if (!rdev || test_bit(Faulty, &rdev->flags))
571 degraded++;
572 else if (test_bit(In_sync, &rdev->flags))
573 ;
574 else
575 /* not in-sync or faulty.
576 * If the reshape increases the number of devices,
577 * this is being recovered by the reshape, so
578 * this 'previous' section is not in_sync.
579 * If the number of devices is being reduced however,
580 * the device can only be part of the array if
581 * we are reverting a reshape, so this section will
582 * be in-sync.
583 */
584 if (conf->raid_disks >= conf->previous_raid_disks)
585 degraded++;
586 }
587 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100588 if (conf->raid_disks == conf->previous_raid_disks)
589 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000590 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100591 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000592 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100593 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000594 if (rdev && test_bit(Faulty, &rdev->flags))
595 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000596 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100597 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000598 else if (test_bit(In_sync, &rdev->flags))
599 ;
600 else
601 /* not in-sync or faulty.
602 * If reshape increases the number of devices, this
603 * section has already been recovered, else it
604 * almost certainly hasn't.
605 */
606 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100607 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000608 }
609 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100610 if (degraded2 > degraded)
611 return degraded2;
612 return degraded;
613}
614
615static int has_failed(struct r5conf *conf)
616{
617 int degraded;
618
619 if (conf->mddev->reshape_position == MaxSector)
620 return conf->mddev->degraded > conf->max_degraded;
621
Song Liu2e38a372017-01-24 10:45:30 -0800622 degraded = raid5_calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000623 if (degraded > conf->max_degraded)
624 return 1;
625 return 0;
626}
627
Shaohua Li6d036f72015-08-13 14:31:57 -0700628struct stripe_head *
629raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
630 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
632 struct stripe_head *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +1100633 int hash = stripe_hash_locks_hash(sector);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800634 int inc_empty_inactive_list_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Dan Williams45b42332007-07-09 11:56:43 -0700636 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Shaohua Li566c09c2013-11-14 15:16:17 +1100638 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 do {
Yuanhan Liub1b46482015-05-08 18:19:06 +1000641 wait_event_lock_irq(conf->wait_for_quiescent,
NeilBrowna8c906c2009-06-09 14:39:59 +1000642 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100643 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100644 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (!sh) {
NeilBrownedbe83a2015-02-26 12:47:56 +1100646 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100647 sh = get_free_stripe(conf, hash);
Shaohua Li713bc5c2015-05-28 17:33:47 -0700648 if (!sh && !test_bit(R5_DID_ALLOC,
649 &conf->cache_state))
NeilBrownedbe83a2015-02-26 12:47:56 +1100650 set_bit(R5_ALLOC_MORE,
651 &conf->cache_state);
652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (noblock && sh == NULL)
654 break;
Song Liua39f7af2016-11-17 15:24:40 -0800655
656 r5c_check_stripe_cache_usage(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (!sh) {
NeilBrown54233992015-02-26 12:21:04 +1100658 set_bit(R5_INACTIVE_BLOCKED,
659 &conf->cache_state);
Song Liua39f7af2016-11-17 15:24:40 -0800660 r5l_wake_reclaim(conf->log, 0);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800661 wait_event_lock_irq(
662 conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +1100663 !list_empty(conf->inactive_list + hash) &&
664 (atomic_read(&conf->active_stripes)
665 < (conf->max_nr_stripes * 3 / 4)
NeilBrown54233992015-02-26 12:21:04 +1100666 || !test_bit(R5_INACTIVE_BLOCKED,
667 &conf->cache_state)),
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800668 *(conf->hash_locks + hash));
NeilBrown54233992015-02-26 12:21:04 +1100669 clear_bit(R5_INACTIVE_BLOCKED,
670 &conf->cache_state);
NeilBrown7da9d452014-01-22 11:45:03 +1100671 } else {
NeilBrownb5663ba2009-03-31 14:39:38 +1100672 init_stripe(sh, sector, previous);
NeilBrown7da9d452014-01-22 11:45:03 +1100673 atomic_inc(&sh->count);
674 }
Shaohua Lie240c182014-04-09 11:27:42 +0800675 } else if (!atomic_inc_not_zero(&sh->count)) {
NeilBrown6d183de2013-11-28 10:55:27 +1100676 spin_lock(&conf->device_lock);
Shaohua Lie240c182014-04-09 11:27:42 +0800677 if (!atomic_read(&sh->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (!test_bit(STRIPE_HANDLE, &sh->state))
679 atomic_inc(&conf->active_stripes);
NeilBrown5af9bef2014-01-14 15:16:10 +1100680 BUG_ON(list_empty(&sh->lru) &&
681 !test_bit(STRIPE_EXPANDING, &sh->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800682 inc_empty_inactive_list_flag = 0;
683 if (!list_empty(conf->inactive_list + hash))
684 inc_empty_inactive_list_flag = 1;
NeilBrown16a53ec2006-06-26 00:27:38 -0700685 list_del_init(&sh->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800686 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
687 atomic_inc(&conf->empty_inactive_list_nr);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800688 if (sh->group) {
689 sh->group->stripes_cnt--;
690 sh->group = NULL;
691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
NeilBrown7da9d452014-01-22 11:45:03 +1100693 atomic_inc(&sh->count);
NeilBrown6d183de2013-11-28 10:55:27 +1100694 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696 } while (sh == NULL);
697
Shaohua Li566c09c2013-11-14 15:16:17 +1100698 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return sh;
700}
701
shli@kernel.org7a87f432014-12-15 12:57:03 +1100702static bool is_full_stripe_write(struct stripe_head *sh)
703{
704 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
705 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
706}
707
shli@kernel.org59fc6302014-12-15 12:57:03 +1100708static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
709{
710 local_irq_disable();
711 if (sh1 > sh2) {
712 spin_lock(&sh2->stripe_lock);
713 spin_lock_nested(&sh1->stripe_lock, 1);
714 } else {
715 spin_lock(&sh1->stripe_lock);
716 spin_lock_nested(&sh2->stripe_lock, 1);
717 }
718}
719
720static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
721{
722 spin_unlock(&sh1->stripe_lock);
723 spin_unlock(&sh2->stripe_lock);
724 local_irq_enable();
725}
726
727/* Only freshly new full stripe normal write stripe can be added to a batch list */
728static bool stripe_can_batch(struct stripe_head *sh)
729{
Shaohua Li9c3e3332015-08-13 14:32:02 -0700730 struct r5conf *conf = sh->raid_conf;
731
732 if (conf->log)
733 return false;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100734 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
NeilBrownd0852df52015-05-27 08:43:45 +1000735 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
shli@kernel.org59fc6302014-12-15 12:57:03 +1100736 is_full_stripe_write(sh);
737}
738
739/* we only do back search */
740static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
741{
742 struct stripe_head *head;
743 sector_t head_sector, tmp_sec;
744 int hash;
745 int dd_idx;
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800746 int inc_empty_inactive_list_flag;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100747
shli@kernel.org59fc6302014-12-15 12:57:03 +1100748 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
749 tmp_sec = sh->sector;
750 if (!sector_div(tmp_sec, conf->chunk_sectors))
751 return;
752 head_sector = sh->sector - STRIPE_SECTORS;
753
754 hash = stripe_hash_locks_hash(head_sector);
755 spin_lock_irq(conf->hash_locks + hash);
756 head = __find_stripe(conf, head_sector, conf->generation);
757 if (head && !atomic_inc_not_zero(&head->count)) {
758 spin_lock(&conf->device_lock);
759 if (!atomic_read(&head->count)) {
760 if (!test_bit(STRIPE_HANDLE, &head->state))
761 atomic_inc(&conf->active_stripes);
762 BUG_ON(list_empty(&head->lru) &&
763 !test_bit(STRIPE_EXPANDING, &head->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800764 inc_empty_inactive_list_flag = 0;
765 if (!list_empty(conf->inactive_list + hash))
766 inc_empty_inactive_list_flag = 1;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100767 list_del_init(&head->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800768 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
769 atomic_inc(&conf->empty_inactive_list_nr);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100770 if (head->group) {
771 head->group->stripes_cnt--;
772 head->group = NULL;
773 }
774 }
775 atomic_inc(&head->count);
776 spin_unlock(&conf->device_lock);
777 }
778 spin_unlock_irq(conf->hash_locks + hash);
779
780 if (!head)
781 return;
782 if (!stripe_can_batch(head))
783 goto out;
784
785 lock_two_stripes(head, sh);
786 /* clear_batch_ready clear the flag */
787 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
788 goto unlock_out;
789
790 if (sh->batch_head)
791 goto unlock_out;
792
793 dd_idx = 0;
794 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
795 dd_idx++;
Jens Axboe1eff9d32016-08-05 15:35:16 -0600796 if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
Mike Christie796a5cf2016-06-05 14:32:07 -0500797 bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
shli@kernel.org59fc6302014-12-15 12:57:03 +1100798 goto unlock_out;
799
800 if (head->batch_head) {
801 spin_lock(&head->batch_head->batch_lock);
802 /* This batch list is already running */
803 if (!stripe_can_batch(head)) {
804 spin_unlock(&head->batch_head->batch_lock);
805 goto unlock_out;
806 }
807
808 /*
809 * at this point, head's BATCH_READY could be cleared, but we
810 * can still add the stripe to batch list
811 */
812 list_add(&sh->batch_list, &head->batch_list);
813 spin_unlock(&head->batch_head->batch_lock);
814
815 sh->batch_head = head->batch_head;
816 } else {
817 head->batch_head = head;
818 sh->batch_head = head->batch_head;
819 spin_lock(&head->batch_lock);
820 list_add_tail(&sh->batch_list, &head->batch_list);
821 spin_unlock(&head->batch_lock);
822 }
823
824 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
825 if (atomic_dec_return(&conf->preread_active_stripes)
826 < IO_THRESHOLD)
827 md_wakeup_thread(conf->mddev->thread);
828
NeilBrown2b6b2452015-05-21 15:10:01 +1000829 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
830 int seq = sh->bm_seq;
831 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
832 sh->batch_head->bm_seq > seq)
833 seq = sh->batch_head->bm_seq;
834 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
835 sh->batch_head->bm_seq = seq;
836 }
837
shli@kernel.org59fc6302014-12-15 12:57:03 +1100838 atomic_inc(&sh->count);
839unlock_out:
840 unlock_two_stripes(head, sh);
841out:
Shaohua Li6d036f72015-08-13 14:31:57 -0700842 raid5_release_stripe(head);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100843}
844
NeilBrown05616be2012-05-21 09:27:00 +1000845/* Determine if 'data_offset' or 'new_data_offset' should be used
846 * in this stripe_head.
847 */
848static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
849{
850 sector_t progress = conf->reshape_progress;
851 /* Need a memory barrier to make sure we see the value
852 * of conf->generation, or ->data_offset that was set before
853 * reshape_progress was updated.
854 */
855 smp_rmb();
856 if (progress == MaxSector)
857 return 0;
858 if (sh->generation == conf->generation - 1)
859 return 0;
860 /* We are in a reshape, and this is a new-generation stripe,
861 * so use new_data_offset.
862 */
863 return 1;
864}
865
Shaohua Li765d7042017-01-04 09:33:23 -0800866static void flush_deferred_bios(struct r5conf *conf)
867{
868 struct bio_list tmp;
869 struct bio *bio;
870
871 if (!conf->batch_bio_dispatch || !conf->group_cnt)
872 return;
873
874 bio_list_init(&tmp);
875 spin_lock(&conf->pending_bios_lock);
876 bio_list_merge(&tmp, &conf->pending_bios);
877 bio_list_init(&conf->pending_bios);
878 spin_unlock(&conf->pending_bios_lock);
879
880 while ((bio = bio_list_pop(&tmp)))
881 generic_make_request(bio);
882}
883
884static void defer_bio_issue(struct r5conf *conf, struct bio *bio)
885{
886 /*
887 * change group_cnt will drain all bios, so this is safe
888 *
889 * A read generally means a read-modify-write, which usually means a
890 * randwrite, so we don't delay it
891 */
892 if (!conf->batch_bio_dispatch || !conf->group_cnt ||
893 bio_op(bio) == REQ_OP_READ) {
894 generic_make_request(bio);
895 return;
896 }
897 spin_lock(&conf->pending_bios_lock);
898 bio_list_add(&conf->pending_bios, bio);
899 spin_unlock(&conf->pending_bios_lock);
900 md_wakeup_thread(conf->mddev->thread);
901}
902
NeilBrown6712ecf2007-09-27 12:47:43 +0200903static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200904raid5_end_read_request(struct bio *bi);
NeilBrown6712ecf2007-09-27 12:47:43 +0200905static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200906raid5_end_write_request(struct bio *bi);
Dan Williams91c00922007-01-02 13:52:30 -0700907
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000908static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700909{
NeilBrownd1688a62011-10-11 16:49:52 +1100910 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700911 int i, disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100912 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -0700913
914 might_sleep();
915
Song Liu1e6d6902016-11-17 15:24:39 -0800916 if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
917 /* writing out phase */
Song Liud7bd3982016-11-23 22:50:39 -0800918 if (s->waiting_extra_page)
919 return;
Song Liu1e6d6902016-11-17 15:24:39 -0800920 if (r5l_write_stripe(conf->log, sh) == 0)
921 return;
922 } else { /* caching phase */
923 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) {
924 r5c_cache_data(conf->log, sh, s);
925 return;
926 }
927 }
928
Dan Williams91c00922007-01-02 13:52:30 -0700929 for (i = disks; i--; ) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500930 int op, op_flags = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +1100931 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100932 struct bio *bi, *rbi;
933 struct md_rdev *rdev, *rrdev = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100934
935 sh = head_sh;
Tejun Heoe9c74692010-09-03 11:56:18 +0200936 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500937 op = REQ_OP_WRITE;
Tejun Heoe9c74692010-09-03 11:56:18 +0200938 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600939 op_flags = REQ_FUA;
Shaohua Li9e4447682012-10-11 13:49:49 +1100940 if (test_bit(R5_Discard, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -0500941 op = REQ_OP_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200942 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -0500943 op = REQ_OP_READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100944 else if (test_and_clear_bit(R5_WantReplace,
945 &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500946 op = REQ_OP_WRITE;
NeilBrown9a3e1102011-12-23 10:17:53 +1100947 replace_only = 1;
948 } else
Dan Williams91c00922007-01-02 13:52:30 -0700949 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000950 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -0500951 op_flags |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700952
shli@kernel.org59fc6302014-12-15 12:57:03 +1100953again:
Dan Williams91c00922007-01-02 13:52:30 -0700954 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100955 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700956
Dan Williams91c00922007-01-02 13:52:30 -0700957 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100958 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100959 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
960 rdev = rcu_dereference(conf->disks[i].rdev);
961 if (!rdev) {
962 rdev = rrdev;
963 rrdev = NULL;
964 }
Mike Christie796a5cf2016-06-05 14:32:07 -0500965 if (op_is_write(op)) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100966 if (replace_only)
967 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100968 if (rdev == rrdev)
969 /* We raced and saw duplicates */
970 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100971 } else {
shli@kernel.org59fc6302014-12-15 12:57:03 +1100972 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100973 rdev = rrdev;
974 rrdev = NULL;
975 }
NeilBrown977df362011-12-23 10:17:53 +1100976
Dan Williams91c00922007-01-02 13:52:30 -0700977 if (rdev && test_bit(Faulty, &rdev->flags))
978 rdev = NULL;
979 if (rdev)
980 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100981 if (rrdev && test_bit(Faulty, &rrdev->flags))
982 rrdev = NULL;
983 if (rrdev)
984 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700985 rcu_read_unlock();
986
NeilBrown73e92e52011-07-28 11:39:22 +1000987 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100988 * need to check for writes. We never accept write errors
989 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000990 */
Mike Christie796a5cf2016-06-05 14:32:07 -0500991 while (op_is_write(op) && rdev &&
NeilBrown73e92e52011-07-28 11:39:22 +1000992 test_bit(WriteErrorSeen, &rdev->flags)) {
993 sector_t first_bad;
994 int bad_sectors;
995 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
996 &first_bad, &bad_sectors);
997 if (!bad)
998 break;
999
1000 if (bad < 0) {
1001 set_bit(BlockedBadBlocks, &rdev->flags);
1002 if (!conf->mddev->external &&
Shaohua Li29530792016-12-08 15:48:19 -08001003 conf->mddev->sb_flags) {
NeilBrown73e92e52011-07-28 11:39:22 +10001004 /* It is very unlikely, but we might
1005 * still need to write out the
1006 * bad block log - better give it
1007 * a chance*/
1008 md_check_recovery(conf->mddev);
1009 }
majianpeng18507532012-07-03 12:11:54 +10001010 /*
1011 * Because md_wait_for_blocked_rdev
1012 * will dec nr_pending, we must
1013 * increment it first.
1014 */
1015 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +10001016 md_wait_for_blocked_rdev(rdev, conf->mddev);
1017 } else {
1018 /* Acknowledged bad block - skip the write */
1019 rdev_dec_pending(rdev, conf->mddev);
1020 rdev = NULL;
1021 }
1022 }
1023
Dan Williams91c00922007-01-02 13:52:30 -07001024 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001025 if (s->syncing || s->expanding || s->expanded
1026 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -07001027 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1028
Dan Williams2b7497f2008-06-28 08:31:52 +10001029 set_bit(STRIPE_IO_STARTED, &sh->state);
1030
Dan Williams91c00922007-01-02 13:52:30 -07001031 bi->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05001032 bio_set_op_attrs(bi, op, op_flags);
1033 bi->bi_end_io = op_is_write(op)
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001034 ? raid5_end_write_request
1035 : raid5_end_read_request;
1036 bi->bi_private = sh;
1037
Mike Christie6296b962016-06-05 14:32:21 -05001038 pr_debug("%s: for %llu schedule op %d on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07001039 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001040 bi->bi_opf, i);
Dan Williams91c00922007-01-02 13:52:30 -07001041 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001042 if (sh != head_sh)
1043 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001044 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001045 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001046 + rdev->new_data_offset);
1047 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001048 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001049 + rdev->data_offset);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001050 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
Jens Axboe1eff9d32016-08-05 15:35:16 -06001051 bi->bi_opf |= REQ_NOMERGE;
majianpeng3f9e7c12012-07-31 10:04:21 +10001052
Shaohua Lid592a992014-05-21 17:57:44 +08001053 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1054 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
Song Liu86aa1392017-01-12 17:22:41 -08001055
1056 if (!op_is_write(op) &&
1057 test_bit(R5_InJournal, &sh->dev[i].flags))
1058 /*
1059 * issuing read for a page in journal, this
1060 * must be preparing for prexor in rmw; read
1061 * the data into orig_page
1062 */
1063 sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
1064 else
1065 sh->dev[i].vec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001066 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001067 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1068 bi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001069 bi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001070 /*
1071 * If this is discard request, set bi_vcnt 0. We don't
1072 * want to confuse SCSI because SCSI will replace payload
1073 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001074 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001075 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +11001076 if (rrdev)
1077 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001078
1079 if (conf->mddev->gendisk)
1080 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
1081 bi, disk_devt(conf->mddev->gendisk),
1082 sh->dev[i].sector);
Shaohua Li765d7042017-01-04 09:33:23 -08001083 defer_bio_issue(conf, bi);
NeilBrown977df362011-12-23 10:17:53 +11001084 }
1085 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001086 if (s->syncing || s->expanding || s->expanded
1087 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +11001088 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
1089
1090 set_bit(STRIPE_IO_STARTED, &sh->state);
1091
1092 rbi->bi_bdev = rrdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05001093 bio_set_op_attrs(rbi, op, op_flags);
1094 BUG_ON(!op_is_write(op));
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001095 rbi->bi_end_io = raid5_end_write_request;
1096 rbi->bi_private = sh;
1097
Mike Christie6296b962016-06-05 14:32:21 -05001098 pr_debug("%s: for %llu schedule op %d on "
NeilBrown977df362011-12-23 10:17:53 +11001099 "replacement disc %d\n",
1100 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001101 rbi->bi_opf, i);
NeilBrown977df362011-12-23 10:17:53 +11001102 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001103 if (sh != head_sh)
1104 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001105 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001106 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001107 + rrdev->new_data_offset);
1108 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001109 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001110 + rrdev->data_offset);
Shaohua Lid592a992014-05-21 17:57:44 +08001111 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1112 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1113 sh->dev[i].rvec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001114 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +11001115 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1116 rbi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001117 rbi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001118 /*
1119 * If this is discard request, set bi_vcnt 0. We don't
1120 * want to confuse SCSI because SCSI will replace payload
1121 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001122 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001123 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001124 if (conf->mddev->gendisk)
1125 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
1126 rbi, disk_devt(conf->mddev->gendisk),
1127 sh->dev[i].sector);
Shaohua Li765d7042017-01-04 09:33:23 -08001128 defer_bio_issue(conf, rbi);
NeilBrown977df362011-12-23 10:17:53 +11001129 }
1130 if (!rdev && !rrdev) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001131 if (op_is_write(op))
Dan Williams91c00922007-01-02 13:52:30 -07001132 set_bit(STRIPE_DEGRADED, &sh->state);
Mike Christie6296b962016-06-05 14:32:21 -05001133 pr_debug("skip op %d on disc %d for sector %llu\n",
Jens Axboe1eff9d32016-08-05 15:35:16 -06001134 bi->bi_opf, i, (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001135 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1136 set_bit(STRIPE_HANDLE, &sh->state);
1137 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001138
1139 if (!head_sh->batch_head)
1140 continue;
1141 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1142 batch_list);
1143 if (sh != head_sh)
1144 goto again;
Dan Williams91c00922007-01-02 13:52:30 -07001145 }
1146}
1147
1148static struct dma_async_tx_descriptor *
Shaohua Lid592a992014-05-21 17:57:44 +08001149async_copy_data(int frombio, struct bio *bio, struct page **page,
1150 sector_t sector, struct dma_async_tx_descriptor *tx,
Song Liu1e6d6902016-11-17 15:24:39 -08001151 struct stripe_head *sh, int no_skipcopy)
Dan Williams91c00922007-01-02 13:52:30 -07001152{
Kent Overstreet79886132013-11-23 17:19:00 -08001153 struct bio_vec bvl;
1154 struct bvec_iter iter;
Dan Williams91c00922007-01-02 13:52:30 -07001155 struct page *bio_page;
Dan Williams91c00922007-01-02 13:52:30 -07001156 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -07001157 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -07001158 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001159
Kent Overstreet4f024f32013-10-11 15:44:27 -07001160 if (bio->bi_iter.bi_sector >= sector)
1161 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
Dan Williams91c00922007-01-02 13:52:30 -07001162 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001163 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -07001164
Dan Williams0403e382009-09-08 17:42:50 -07001165 if (frombio)
1166 flags |= ASYNC_TX_FENCE;
1167 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1168
Kent Overstreet79886132013-11-23 17:19:00 -08001169 bio_for_each_segment(bvl, bio, iter) {
1170 int len = bvl.bv_len;
Dan Williams91c00922007-01-02 13:52:30 -07001171 int clen;
1172 int b_offset = 0;
1173
1174 if (page_offset < 0) {
1175 b_offset = -page_offset;
1176 page_offset += b_offset;
1177 len -= b_offset;
1178 }
1179
1180 if (len > 0 && page_offset + len > STRIPE_SIZE)
1181 clen = STRIPE_SIZE - page_offset;
1182 else
1183 clen = len;
1184
1185 if (clen > 0) {
Kent Overstreet79886132013-11-23 17:19:00 -08001186 b_offset += bvl.bv_offset;
1187 bio_page = bvl.bv_page;
Shaohua Lid592a992014-05-21 17:57:44 +08001188 if (frombio) {
1189 if (sh->raid_conf->skip_copy &&
1190 b_offset == 0 && page_offset == 0 &&
Song Liu1e6d6902016-11-17 15:24:39 -08001191 clen == STRIPE_SIZE &&
1192 !no_skipcopy)
Shaohua Lid592a992014-05-21 17:57:44 +08001193 *page = bio_page;
1194 else
1195 tx = async_memcpy(*page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001196 b_offset, clen, &submit);
Shaohua Lid592a992014-05-21 17:57:44 +08001197 } else
1198 tx = async_memcpy(bio_page, *page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001199 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001200 }
Dan Williamsa08abd82009-06-03 11:43:59 -07001201 /* chain the operations */
1202 submit.depend_tx = tx;
1203
Dan Williams91c00922007-01-02 13:52:30 -07001204 if (clen < len) /* hit end of page */
1205 break;
1206 page_offset += len;
1207 }
1208
1209 return tx;
1210}
1211
1212static void ops_complete_biofill(void *stripe_head_ref)
1213{
1214 struct stripe_head *sh = stripe_head_ref;
NeilBrown34a6f802015-08-14 12:07:57 +10001215 struct bio_list return_bi = BIO_EMPTY_LIST;
Dan Williamse4d84902007-09-24 10:06:13 -07001216 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001217
Harvey Harrisone46b2722008-04-28 02:15:50 -07001218 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001219 (unsigned long long)sh->sector);
1220
1221 /* clear completed biofills */
1222 for (i = sh->disks; i--; ) {
1223 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001224
1225 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001226 /* and check if we need to reply to a read request,
1227 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001228 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001229 */
1230 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001231 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001232
Dan Williams91c00922007-01-02 13:52:30 -07001233 BUG_ON(!dev->read);
1234 rbi = dev->read;
1235 dev->read = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001236 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001237 dev->sector + STRIPE_SECTORS) {
1238 rbi2 = r5_next_bio(rbi, dev->sector);
NeilBrown34a6f802015-08-14 12:07:57 +10001239 if (!raid5_dec_bi_active_stripes(rbi))
1240 bio_list_add(&return_bi, rbi);
Dan Williams91c00922007-01-02 13:52:30 -07001241 rbi = rbi2;
1242 }
1243 }
1244 }
Dan Williams83de75c2008-06-28 08:31:58 +10001245 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001246
NeilBrown34a6f802015-08-14 12:07:57 +10001247 return_io(&return_bi);
Dan Williams91c00922007-01-02 13:52:30 -07001248
Dan Williamse4d84902007-09-24 10:06:13 -07001249 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001250 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001251}
1252
1253static void ops_run_biofill(struct stripe_head *sh)
1254{
1255 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001256 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001257 int i;
1258
shli@kernel.org59fc6302014-12-15 12:57:03 +11001259 BUG_ON(sh->batch_head);
Harvey Harrisone46b2722008-04-28 02:15:50 -07001260 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001261 (unsigned long long)sh->sector);
1262
1263 for (i = sh->disks; i--; ) {
1264 struct r5dev *dev = &sh->dev[i];
1265 if (test_bit(R5_Wantfill, &dev->flags)) {
1266 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001267 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001268 dev->read = rbi = dev->toread;
1269 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001270 spin_unlock_irq(&sh->stripe_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001271 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001272 dev->sector + STRIPE_SECTORS) {
Shaohua Lid592a992014-05-21 17:57:44 +08001273 tx = async_copy_data(0, rbi, &dev->page,
Song Liu1e6d6902016-11-17 15:24:39 -08001274 dev->sector, tx, sh, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001275 rbi = r5_next_bio(rbi, dev->sector);
1276 }
1277 }
1278 }
1279
1280 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001281 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1282 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001283}
1284
Dan Williams4e7d2c02009-08-29 19:13:11 -07001285static void mark_target_uptodate(struct stripe_head *sh, int target)
1286{
1287 struct r5dev *tgt;
1288
1289 if (target < 0)
1290 return;
1291
1292 tgt = &sh->dev[target];
1293 set_bit(R5_UPTODATE, &tgt->flags);
1294 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1295 clear_bit(R5_Wantcompute, &tgt->flags);
1296}
1297
Dan Williamsac6b53b2009-07-14 13:40:19 -07001298static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001299{
1300 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001301
Harvey Harrisone46b2722008-04-28 02:15:50 -07001302 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001303 (unsigned long long)sh->sector);
1304
Dan Williamsac6b53b2009-07-14 13:40:19 -07001305 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001306 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001307 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001308
Dan Williamsecc65c92008-06-28 08:31:57 +10001309 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1310 if (sh->check_state == check_state_compute_run)
1311 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001312 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001313 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001314}
1315
Dan Williamsd6f38f32009-07-14 11:50:52 -07001316/* return a pointer to the address conversion region of the scribble buffer */
1317static addr_conv_t *to_addr_conv(struct stripe_head *sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001318 struct raid5_percpu *percpu, int i)
Dan Williams91c00922007-01-02 13:52:30 -07001319{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001320 void *addr;
1321
1322 addr = flex_array_get(percpu->scribble, i);
1323 return addr + sizeof(struct page *) * (sh->disks + 2);
1324}
1325
1326/* return a pointer to the address conversion region of the scribble buffer */
1327static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
1328{
1329 void *addr;
1330
1331 addr = flex_array_get(percpu->scribble, i);
1332 return addr;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001333}
1334
1335static struct dma_async_tx_descriptor *
1336ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1337{
Dan Williams91c00922007-01-02 13:52:30 -07001338 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001339 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001340 int target = sh->ops.target;
1341 struct r5dev *tgt = &sh->dev[target];
1342 struct page *xor_dest = tgt->page;
1343 int count = 0;
1344 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001345 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001346 int i;
1347
shli@kernel.org59fc6302014-12-15 12:57:03 +11001348 BUG_ON(sh->batch_head);
1349
Dan Williams91c00922007-01-02 13:52:30 -07001350 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07001351 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001352 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1353
1354 for (i = disks; i--; )
1355 if (i != target)
1356 xor_srcs[count++] = sh->dev[i].page;
1357
1358 atomic_inc(&sh->count);
1359
Dan Williams0403e382009-09-08 17:42:50 -07001360 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001361 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
Dan Williams91c00922007-01-02 13:52:30 -07001362 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -07001363 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001364 else
Dan Williamsa08abd82009-06-03 11:43:59 -07001365 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001366
Dan Williams91c00922007-01-02 13:52:30 -07001367 return tx;
1368}
1369
Dan Williamsac6b53b2009-07-14 13:40:19 -07001370/* set_syndrome_sources - populate source buffers for gen_syndrome
1371 * @srcs - (struct page *) array of size sh->disks
1372 * @sh - stripe_head to parse
1373 *
1374 * Populates srcs in proper layout order for the stripe and returns the
1375 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1376 * destination buffer is recorded in srcs[count] and the Q destination
1377 * is recorded in srcs[count+1]].
1378 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001379static int set_syndrome_sources(struct page **srcs,
1380 struct stripe_head *sh,
1381 int srctype)
Dan Williamsac6b53b2009-07-14 13:40:19 -07001382{
1383 int disks = sh->disks;
1384 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1385 int d0_idx = raid6_d0(sh);
1386 int count;
1387 int i;
1388
1389 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001390 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001391
1392 count = 0;
1393 i = d0_idx;
1394 do {
1395 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001396 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001397
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001398 if (i == sh->qd_idx || i == sh->pd_idx ||
1399 (srctype == SYNDROME_SRC_ALL) ||
1400 (srctype == SYNDROME_SRC_WANT_DRAIN &&
Song Liu1e6d6902016-11-17 15:24:39 -08001401 (test_bit(R5_Wantdrain, &dev->flags) ||
1402 test_bit(R5_InJournal, &dev->flags))) ||
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001403 (srctype == SYNDROME_SRC_WRITTEN &&
Song Liu09777622017-03-13 13:44:35 -07001404 (dev->written ||
1405 test_bit(R5_InJournal, &dev->flags)))) {
Song Liu1e6d6902016-11-17 15:24:39 -08001406 if (test_bit(R5_InJournal, &dev->flags))
1407 srcs[slot] = sh->dev[i].orig_page;
1408 else
1409 srcs[slot] = sh->dev[i].page;
1410 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001411 i = raid6_next_disk(i, disks);
1412 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001413
NeilBrowne4424fe2009-10-16 16:27:34 +11001414 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001415}
1416
1417static struct dma_async_tx_descriptor *
1418ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1419{
1420 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001421 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001422 int target;
1423 int qd_idx = sh->qd_idx;
1424 struct dma_async_tx_descriptor *tx;
1425 struct async_submit_ctl submit;
1426 struct r5dev *tgt;
1427 struct page *dest;
1428 int i;
1429 int count;
1430
shli@kernel.org59fc6302014-12-15 12:57:03 +11001431 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001432 if (sh->ops.target < 0)
1433 target = sh->ops.target2;
1434 else if (sh->ops.target2 < 0)
1435 target = sh->ops.target;
1436 else
1437 /* we should only have one valid target */
1438 BUG();
1439 BUG_ON(target < 0);
1440 pr_debug("%s: stripe %llu block: %d\n",
1441 __func__, (unsigned long long)sh->sector, target);
1442
1443 tgt = &sh->dev[target];
1444 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1445 dest = tgt->page;
1446
1447 atomic_inc(&sh->count);
1448
1449 if (target == qd_idx) {
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001450 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001451 blocks[count] = NULL; /* regenerating p is not necessary */
1452 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001453 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1454 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001455 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001456 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1457 } else {
1458 /* Compute any data- or p-drive using XOR */
1459 count = 0;
1460 for (i = disks; i-- ; ) {
1461 if (i == target || i == qd_idx)
1462 continue;
1463 blocks[count++] = sh->dev[i].page;
1464 }
1465
Dan Williams0403e382009-09-08 17:42:50 -07001466 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1467 NULL, ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001468 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001469 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1470 }
1471
1472 return tx;
1473}
1474
1475static struct dma_async_tx_descriptor *
1476ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1477{
1478 int i, count, disks = sh->disks;
1479 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1480 int d0_idx = raid6_d0(sh);
1481 int faila = -1, failb = -1;
1482 int target = sh->ops.target;
1483 int target2 = sh->ops.target2;
1484 struct r5dev *tgt = &sh->dev[target];
1485 struct r5dev *tgt2 = &sh->dev[target2];
1486 struct dma_async_tx_descriptor *tx;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001487 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001488 struct async_submit_ctl submit;
1489
shli@kernel.org59fc6302014-12-15 12:57:03 +11001490 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001491 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1492 __func__, (unsigned long long)sh->sector, target, target2);
1493 BUG_ON(target < 0 || target2 < 0);
1494 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1495 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1496
Dan Williams6c910a72009-09-16 12:24:54 -07001497 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001498 * slot number conversion for 'faila' and 'failb'
1499 */
1500 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001501 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001502 count = 0;
1503 i = d0_idx;
1504 do {
1505 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1506
1507 blocks[slot] = sh->dev[i].page;
1508
1509 if (i == target)
1510 faila = slot;
1511 if (i == target2)
1512 failb = slot;
1513 i = raid6_next_disk(i, disks);
1514 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001515
1516 BUG_ON(faila == failb);
1517 if (failb < faila)
1518 swap(faila, failb);
1519 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1520 __func__, (unsigned long long)sh->sector, faila, failb);
1521
1522 atomic_inc(&sh->count);
1523
1524 if (failb == syndrome_disks+1) {
1525 /* Q disk is one of the missing disks */
1526 if (faila == syndrome_disks) {
1527 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001528 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1529 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001530 to_addr_conv(sh, percpu, 0));
NeilBrowne4424fe2009-10-16 16:27:34 +11001531 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001532 STRIPE_SIZE, &submit);
1533 } else {
1534 struct page *dest;
1535 int data_target;
1536 int qd_idx = sh->qd_idx;
1537
1538 /* Missing D+Q: recompute D from P, then recompute Q */
1539 if (target == qd_idx)
1540 data_target = target2;
1541 else
1542 data_target = target;
1543
1544 count = 0;
1545 for (i = disks; i-- ; ) {
1546 if (i == data_target || i == qd_idx)
1547 continue;
1548 blocks[count++] = sh->dev[i].page;
1549 }
1550 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001551 init_async_submit(&submit,
1552 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1553 NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001554 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001555 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1556 &submit);
1557
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001558 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williams0403e382009-09-08 17:42:50 -07001559 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1560 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001561 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001562 return async_gen_syndrome(blocks, 0, count+2,
1563 STRIPE_SIZE, &submit);
1564 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001565 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001566 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1567 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001568 to_addr_conv(sh, percpu, 0));
Dan Williams6c910a72009-09-16 12:24:54 -07001569 if (failb == syndrome_disks) {
1570 /* We're missing D+P. */
1571 return async_raid6_datap_recov(syndrome_disks+2,
1572 STRIPE_SIZE, faila,
1573 blocks, &submit);
1574 } else {
1575 /* We're missing D+D. */
1576 return async_raid6_2data_recov(syndrome_disks+2,
1577 STRIPE_SIZE, faila, failb,
1578 blocks, &submit);
1579 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001580 }
1581}
1582
Dan Williams91c00922007-01-02 13:52:30 -07001583static void ops_complete_prexor(void *stripe_head_ref)
1584{
1585 struct stripe_head *sh = stripe_head_ref;
1586
Harvey Harrisone46b2722008-04-28 02:15:50 -07001587 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001588 (unsigned long long)sh->sector);
Song Liu1e6d6902016-11-17 15:24:39 -08001589
1590 if (r5c_is_writeback(sh->raid_conf->log))
1591 /*
1592 * raid5-cache write back uses orig_page during prexor.
1593 * After prexor, it is time to free orig_page
1594 */
1595 r5c_release_extra_page(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001596}
1597
1598static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001599ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1600 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001601{
Dan Williams91c00922007-01-02 13:52:30 -07001602 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001603 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001604 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001605 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001606
1607 /* existing parity data subtracted */
1608 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1609
shli@kernel.org59fc6302014-12-15 12:57:03 +11001610 BUG_ON(sh->batch_head);
Harvey Harrisone46b2722008-04-28 02:15:50 -07001611 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001612 (unsigned long long)sh->sector);
1613
1614 for (i = disks; i--; ) {
1615 struct r5dev *dev = &sh->dev[i];
1616 /* Only process blocks that are known to be uptodate */
Song Liu1e6d6902016-11-17 15:24:39 -08001617 if (test_bit(R5_InJournal, &dev->flags))
1618 xor_srcs[count++] = dev->orig_page;
1619 else if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001620 xor_srcs[count++] = dev->page;
1621 }
1622
Dan Williams0403e382009-09-08 17:42:50 -07001623 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001624 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Dan Williamsa08abd82009-06-03 11:43:59 -07001625 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001626
1627 return tx;
1628}
1629
1630static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001631ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1632 struct dma_async_tx_descriptor *tx)
1633{
1634 struct page **blocks = to_addr_page(percpu, 0);
1635 int count;
1636 struct async_submit_ctl submit;
1637
1638 pr_debug("%s: stripe %llu\n", __func__,
1639 (unsigned long long)sh->sector);
1640
1641 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN);
1642
1643 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1644 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1645 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1646
1647 return tx;
1648}
1649
1650static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001651ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001652{
Song Liu1e6d6902016-11-17 15:24:39 -08001653 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001654 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001655 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001656 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -07001657
Harvey Harrisone46b2722008-04-28 02:15:50 -07001658 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001659 (unsigned long long)sh->sector);
1660
1661 for (i = disks; i--; ) {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001662 struct r5dev *dev;
Dan Williams91c00922007-01-02 13:52:30 -07001663 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001664
shli@kernel.org59fc6302014-12-15 12:57:03 +11001665 sh = head_sh;
1666 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001667 struct bio *wbi;
1668
shli@kernel.org59fc6302014-12-15 12:57:03 +11001669again:
1670 dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001671 /*
1672 * clear R5_InJournal, so when rewriting a page in
1673 * journal, it is not skipped by r5l_log_stripe()
1674 */
1675 clear_bit(R5_InJournal, &dev->flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10001676 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001677 chosen = dev->towrite;
1678 dev->towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11001679 sh->overwrite_disks = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001680 BUG_ON(dev->written);
1681 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001682 spin_unlock_irq(&sh->stripe_lock);
Shaohua Lid592a992014-05-21 17:57:44 +08001683 WARN_ON(dev->page != dev->orig_page);
Dan Williams91c00922007-01-02 13:52:30 -07001684
Kent Overstreet4f024f32013-10-11 15:44:27 -07001685 while (wbi && wbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001686 dev->sector + STRIPE_SECTORS) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001687 if (wbi->bi_opf & REQ_FUA)
Tejun Heoe9c74692010-09-03 11:56:18 +02001688 set_bit(R5_WantFUA, &dev->flags);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001689 if (wbi->bi_opf & REQ_SYNC)
Shaohua Libc0934f2012-05-22 13:55:05 +10001690 set_bit(R5_SyncIO, &dev->flags);
Mike Christie796a5cf2016-06-05 14:32:07 -05001691 if (bio_op(wbi) == REQ_OP_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001692 set_bit(R5_Discard, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08001693 else {
1694 tx = async_copy_data(1, wbi, &dev->page,
Song Liu1e6d6902016-11-17 15:24:39 -08001695 dev->sector, tx, sh,
1696 r5c_is_writeback(conf->log));
1697 if (dev->page != dev->orig_page &&
1698 !r5c_is_writeback(conf->log)) {
Shaohua Lid592a992014-05-21 17:57:44 +08001699 set_bit(R5_SkipCopy, &dev->flags);
1700 clear_bit(R5_UPTODATE, &dev->flags);
1701 clear_bit(R5_OVERWRITE, &dev->flags);
1702 }
1703 }
Dan Williams91c00922007-01-02 13:52:30 -07001704 wbi = r5_next_bio(wbi, dev->sector);
1705 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001706
1707 if (head_sh->batch_head) {
1708 sh = list_first_entry(&sh->batch_list,
1709 struct stripe_head,
1710 batch_list);
1711 if (sh == head_sh)
1712 continue;
1713 goto again;
1714 }
Dan Williams91c00922007-01-02 13:52:30 -07001715 }
1716 }
1717
1718 return tx;
1719}
1720
Dan Williamsac6b53b2009-07-14 13:40:19 -07001721static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001722{
1723 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001724 int disks = sh->disks;
1725 int pd_idx = sh->pd_idx;
1726 int qd_idx = sh->qd_idx;
1727 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001728 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001729
Harvey Harrisone46b2722008-04-28 02:15:50 -07001730 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001731 (unsigned long long)sh->sector);
1732
Shaohua Libc0934f2012-05-22 13:55:05 +10001733 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001734 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001735 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001736 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001737 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001738
Dan Williams91c00922007-01-02 13:52:30 -07001739 for (i = disks; i--; ) {
1740 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001741
Tejun Heoe9c74692010-09-03 11:56:18 +02001742 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Lid592a992014-05-21 17:57:44 +08001743 if (!discard && !test_bit(R5_SkipCopy, &dev->flags))
Shaohua Li9e4447682012-10-11 13:49:49 +11001744 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001745 if (fua)
1746 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001747 if (sync)
1748 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001749 }
Dan Williams91c00922007-01-02 13:52:30 -07001750 }
1751
Dan Williamsd8ee0722008-06-28 08:32:06 +10001752 if (sh->reconstruct_state == reconstruct_state_drain_run)
1753 sh->reconstruct_state = reconstruct_state_drain_result;
1754 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1755 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1756 else {
1757 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1758 sh->reconstruct_state = reconstruct_state_result;
1759 }
Dan Williams91c00922007-01-02 13:52:30 -07001760
1761 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001762 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001763}
1764
1765static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001766ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1767 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001768{
Dan Williams91c00922007-01-02 13:52:30 -07001769 int disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001770 struct page **xor_srcs;
Dan Williamsa08abd82009-06-03 11:43:59 -07001771 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001772 int count, pd_idx = sh->pd_idx, i;
Dan Williams91c00922007-01-02 13:52:30 -07001773 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001774 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001775 unsigned long flags;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001776 int j = 0;
1777 struct stripe_head *head_sh = sh;
1778 int last_stripe;
Dan Williams91c00922007-01-02 13:52:30 -07001779
Harvey Harrisone46b2722008-04-28 02:15:50 -07001780 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001781 (unsigned long long)sh->sector);
1782
Shaohua Li620125f2012-10-11 13:49:05 +11001783 for (i = 0; i < sh->disks; i++) {
1784 if (pd_idx == i)
1785 continue;
1786 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1787 break;
1788 }
1789 if (i >= sh->disks) {
1790 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001791 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1792 ops_complete_reconstruct(sh);
1793 return;
1794 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001795again:
1796 count = 0;
1797 xor_srcs = to_addr_page(percpu, j);
Dan Williams91c00922007-01-02 13:52:30 -07001798 /* check if prexor is active which means only process blocks
1799 * that are part of a read-modify-write (written)
1800 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001801 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001802 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001803 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1804 for (i = disks; i--; ) {
1805 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001806 if (head_sh->dev[i].written ||
1807 test_bit(R5_InJournal, &head_sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -07001808 xor_srcs[count++] = dev->page;
1809 }
1810 } else {
1811 xor_dest = sh->dev[pd_idx].page;
1812 for (i = disks; i--; ) {
1813 struct r5dev *dev = &sh->dev[i];
1814 if (i != pd_idx)
1815 xor_srcs[count++] = dev->page;
1816 }
1817 }
1818
Dan Williams91c00922007-01-02 13:52:30 -07001819 /* 1/ if we prexor'd then the dest is reused as a source
1820 * 2/ if we did not prexor then we are redoing the parity
1821 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1822 * for the synchronous xor case
1823 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001824 last_stripe = !head_sh->batch_head ||
1825 list_first_entry(&sh->batch_list,
1826 struct stripe_head, batch_list) == head_sh;
1827 if (last_stripe) {
1828 flags = ASYNC_TX_ACK |
1829 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
Dan Williams91c00922007-01-02 13:52:30 -07001830
shli@kernel.org59fc6302014-12-15 12:57:03 +11001831 atomic_inc(&head_sh->count);
1832 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
1833 to_addr_conv(sh, percpu, j));
1834 } else {
1835 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
1836 init_async_submit(&submit, flags, tx, NULL, NULL,
1837 to_addr_conv(sh, percpu, j));
1838 }
Dan Williams91c00922007-01-02 13:52:30 -07001839
Dan Williamsa08abd82009-06-03 11:43:59 -07001840 if (unlikely(count == 1))
1841 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1842 else
1843 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001844 if (!last_stripe) {
1845 j++;
1846 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1847 batch_list);
1848 goto again;
1849 }
Dan Williams91c00922007-01-02 13:52:30 -07001850}
1851
Dan Williamsac6b53b2009-07-14 13:40:19 -07001852static void
1853ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1854 struct dma_async_tx_descriptor *tx)
1855{
1856 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001857 struct page **blocks;
1858 int count, i, j = 0;
1859 struct stripe_head *head_sh = sh;
1860 int last_stripe;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001861 int synflags;
1862 unsigned long txflags;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001863
1864 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1865
Shaohua Li620125f2012-10-11 13:49:05 +11001866 for (i = 0; i < sh->disks; i++) {
1867 if (sh->pd_idx == i || sh->qd_idx == i)
1868 continue;
1869 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1870 break;
1871 }
1872 if (i >= sh->disks) {
1873 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001874 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1875 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1876 ops_complete_reconstruct(sh);
1877 return;
1878 }
1879
shli@kernel.org59fc6302014-12-15 12:57:03 +11001880again:
1881 blocks = to_addr_page(percpu, j);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001882
1883 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1884 synflags = SYNDROME_SRC_WRITTEN;
1885 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
1886 } else {
1887 synflags = SYNDROME_SRC_ALL;
1888 txflags = ASYNC_TX_ACK;
1889 }
1890
1891 count = set_syndrome_sources(blocks, sh, synflags);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001892 last_stripe = !head_sh->batch_head ||
1893 list_first_entry(&sh->batch_list,
1894 struct stripe_head, batch_list) == head_sh;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001895
shli@kernel.org59fc6302014-12-15 12:57:03 +11001896 if (last_stripe) {
1897 atomic_inc(&head_sh->count);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001898 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
shli@kernel.org59fc6302014-12-15 12:57:03 +11001899 head_sh, to_addr_conv(sh, percpu, j));
1900 } else
1901 init_async_submit(&submit, 0, tx, NULL, NULL,
1902 to_addr_conv(sh, percpu, j));
Shaohua Li48769692015-05-13 09:30:08 -07001903 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001904 if (!last_stripe) {
1905 j++;
1906 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1907 batch_list);
1908 goto again;
1909 }
Dan Williams91c00922007-01-02 13:52:30 -07001910}
1911
1912static void ops_complete_check(void *stripe_head_ref)
1913{
1914 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001915
Harvey Harrisone46b2722008-04-28 02:15:50 -07001916 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001917 (unsigned long long)sh->sector);
1918
Dan Williamsecc65c92008-06-28 08:31:57 +10001919 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001920 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001921 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001922}
1923
Dan Williamsac6b53b2009-07-14 13:40:19 -07001924static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001925{
Dan Williams91c00922007-01-02 13:52:30 -07001926 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001927 int pd_idx = sh->pd_idx;
1928 int qd_idx = sh->qd_idx;
1929 struct page *xor_dest;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001930 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001931 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001932 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001933 int count;
1934 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001935
Harvey Harrisone46b2722008-04-28 02:15:50 -07001936 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001937 (unsigned long long)sh->sector);
1938
shli@kernel.org59fc6302014-12-15 12:57:03 +11001939 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001940 count = 0;
1941 xor_dest = sh->dev[pd_idx].page;
1942 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001943 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001944 if (i == pd_idx || i == qd_idx)
1945 continue;
1946 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001947 }
1948
Dan Williamsd6f38f32009-07-14 11:50:52 -07001949 init_async_submit(&submit, 0, NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001950 to_addr_conv(sh, percpu, 0));
Dan Williams099f53c2009-04-08 14:28:37 -07001951 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001952 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001953
Dan Williams91c00922007-01-02 13:52:30 -07001954 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001955 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1956 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001957}
1958
Dan Williamsac6b53b2009-07-14 13:40:19 -07001959static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1960{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001961 struct page **srcs = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001962 struct async_submit_ctl submit;
1963 int count;
1964
1965 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1966 (unsigned long long)sh->sector, checkp);
1967
shli@kernel.org59fc6302014-12-15 12:57:03 +11001968 BUG_ON(sh->batch_head);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001969 count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001970 if (!checkp)
1971 srcs[count] = NULL;
1972
1973 atomic_inc(&sh->count);
1974 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001975 sh, to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001976 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1977 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1978}
1979
NeilBrown51acbce2013-02-28 09:08:34 +11001980static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001981{
1982 int overlap_clear = 0, i, disks = sh->disks;
1983 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001984 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001985 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001986 struct raid5_percpu *percpu;
1987 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001988
Dan Williamsd6f38f32009-07-14 11:50:52 -07001989 cpu = get_cpu();
1990 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001991 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001992 ops_run_biofill(sh);
1993 overlap_clear++;
1994 }
1995
Dan Williams7b3a8712008-06-28 08:32:09 +10001996 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001997 if (level < 6)
1998 tx = ops_run_compute5(sh, percpu);
1999 else {
2000 if (sh->ops.target2 < 0 || sh->ops.target < 0)
2001 tx = ops_run_compute6_1(sh, percpu);
2002 else
2003 tx = ops_run_compute6_2(sh, percpu);
2004 }
2005 /* terminate the chain if reconstruct is not set to be run */
2006 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10002007 async_tx_ack(tx);
2008 }
Dan Williams91c00922007-01-02 13:52:30 -07002009
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002010 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
2011 if (level < 6)
2012 tx = ops_run_prexor5(sh, percpu, tx);
2013 else
2014 tx = ops_run_prexor6(sh, percpu, tx);
2015 }
Dan Williams91c00922007-01-02 13:52:30 -07002016
Dan Williams600aa102008-06-28 08:32:05 +10002017 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10002018 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07002019 overlap_clear++;
2020 }
2021
Dan Williamsac6b53b2009-07-14 13:40:19 -07002022 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
2023 if (level < 6)
2024 ops_run_reconstruct5(sh, percpu, tx);
2025 else
2026 ops_run_reconstruct6(sh, percpu, tx);
2027 }
Dan Williams91c00922007-01-02 13:52:30 -07002028
Dan Williamsac6b53b2009-07-14 13:40:19 -07002029 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
2030 if (sh->check_state == check_state_run)
2031 ops_run_check_p(sh, percpu);
2032 else if (sh->check_state == check_state_run_q)
2033 ops_run_check_pq(sh, percpu, 0);
2034 else if (sh->check_state == check_state_run_pq)
2035 ops_run_check_pq(sh, percpu, 1);
2036 else
2037 BUG();
2038 }
Dan Williams91c00922007-01-02 13:52:30 -07002039
shli@kernel.org59fc6302014-12-15 12:57:03 +11002040 if (overlap_clear && !sh->batch_head)
Dan Williams91c00922007-01-02 13:52:30 -07002041 for (i = disks; i--; ) {
2042 struct r5dev *dev = &sh->dev[i];
2043 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2044 wake_up(&sh->raid_conf->wait_for_overlap);
2045 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07002046 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07002047}
2048
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002049static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
2050 int disks)
NeilBrownf18c1a32015-05-08 18:19:04 +10002051{
2052 struct stripe_head *sh;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002053 int i;
NeilBrownf18c1a32015-05-08 18:19:04 +10002054
2055 sh = kmem_cache_zalloc(sc, gfp);
2056 if (sh) {
2057 spin_lock_init(&sh->stripe_lock);
2058 spin_lock_init(&sh->batch_lock);
2059 INIT_LIST_HEAD(&sh->batch_list);
2060 INIT_LIST_HEAD(&sh->lru);
Song Liua39f7af2016-11-17 15:24:40 -08002061 INIT_LIST_HEAD(&sh->r5c);
Song Liud7bd3982016-11-23 22:50:39 -08002062 INIT_LIST_HEAD(&sh->log_list);
NeilBrownf18c1a32015-05-08 18:19:04 +10002063 atomic_set(&sh->count, 1);
Song Liua39f7af2016-11-17 15:24:40 -08002064 sh->log_start = MaxSector;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002065 for (i = 0; i < disks; i++) {
2066 struct r5dev *dev = &sh->dev[i];
2067
Ming Lei3a83f462016-11-22 08:57:21 -07002068 bio_init(&dev->req, &dev->vec, 1);
2069 bio_init(&dev->rreq, &dev->rvec, 1);
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002070 }
NeilBrownf18c1a32015-05-08 18:19:04 +10002071 }
2072 return sh;
2073}
NeilBrown486f0642015-02-25 12:10:35 +11002074static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
2076 struct stripe_head *sh;
NeilBrownf18c1a32015-05-08 18:19:04 +10002077
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002078 sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08002079 if (!sh)
2080 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10002081
NeilBrown3f294f42005-11-08 21:39:25 -08002082 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08002083
NeilBrowna9683a72015-02-25 12:02:51 +11002084 if (grow_buffers(sh, gfp)) {
NeilBrowne4e11e32010-06-16 16:45:16 +10002085 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002086 kmem_cache_free(conf->slab_cache, sh);
2087 return 0;
2088 }
NeilBrown486f0642015-02-25 12:10:35 +11002089 sh->hash_lock_index =
2090 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrown3f294f42005-11-08 21:39:25 -08002091 /* we just created an active stripe so... */
NeilBrown3f294f42005-11-08 21:39:25 -08002092 atomic_inc(&conf->active_stripes);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002093
Shaohua Li6d036f72015-08-13 14:31:57 -07002094 raid5_release_stripe(sh);
NeilBrown486f0642015-02-25 12:10:35 +11002095 conf->max_nr_stripes++;
NeilBrown3f294f42005-11-08 21:39:25 -08002096 return 1;
2097}
2098
NeilBrownd1688a62011-10-11 16:49:52 +11002099static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08002100{
Christoph Lametere18b8902006-12-06 20:33:20 -08002101 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11002102 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
NeilBrownf4be6b42010-06-01 19:37:25 +10002104 if (conf->mddev->gendisk)
2105 sprintf(conf->cache_name[0],
2106 "raid%d-%s", conf->level, mdname(conf->mddev));
2107 else
2108 sprintf(conf->cache_name[0],
2109 "raid%d-%p", conf->level, conf->mddev);
2110 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
2111
NeilBrownad01c9e2006-03-27 01:18:07 -08002112 conf->active_name = 0;
2113 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002115 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (!sc)
2117 return 1;
2118 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002119 conf->pool_size = devs;
NeilBrown486f0642015-02-25 12:10:35 +11002120 while (num--)
2121 if (!grow_one_stripe(conf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return 1;
NeilBrown486f0642015-02-25 12:10:35 +11002123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return 0;
2125}
NeilBrown29269552006-03-27 01:18:10 -08002126
Dan Williamsd6f38f32009-07-14 11:50:52 -07002127/**
2128 * scribble_len - return the required size of the scribble region
2129 * @num - total number of disks in the array
2130 *
2131 * The size must be enough to contain:
2132 * 1/ a struct page pointer for each device in the array +2
2133 * 2/ room to convert each entry in (1) to its corresponding dma
2134 * (dma_map_page()) or page (page_address()) address.
2135 *
2136 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2137 * calculate over all devices (not just the data blocks), using zeros in place
2138 * of the P and Q blocks.
2139 */
shli@kernel.org46d5b782014-12-15 12:57:02 +11002140static struct flex_array *scribble_alloc(int num, int cnt, gfp_t flags)
Dan Williamsd6f38f32009-07-14 11:50:52 -07002141{
shli@kernel.org46d5b782014-12-15 12:57:02 +11002142 struct flex_array *ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002143 size_t len;
2144
2145 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
shli@kernel.org46d5b782014-12-15 12:57:02 +11002146 ret = flex_array_alloc(len, cnt, flags);
2147 if (!ret)
2148 return NULL;
2149 /* always prealloc all elements, so no locking is required */
2150 if (flex_array_prealloc(ret, 0, cnt, flags)) {
2151 flex_array_free(ret);
2152 return NULL;
2153 }
2154 return ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002155}
2156
NeilBrown738a2732015-05-08 18:19:39 +10002157static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2158{
2159 unsigned long cpu;
2160 int err = 0;
2161
Shaohua Li27a353c2016-02-24 17:38:28 -08002162 /*
2163 * Never shrink. And mddev_suspend() could deadlock if this is called
2164 * from raid5d. In that case, scribble_disks and scribble_sectors
2165 * should equal to new_disks and new_sectors
2166 */
2167 if (conf->scribble_disks >= new_disks &&
2168 conf->scribble_sectors >= new_sectors)
2169 return 0;
NeilBrown738a2732015-05-08 18:19:39 +10002170 mddev_suspend(conf->mddev);
2171 get_online_cpus();
2172 for_each_present_cpu(cpu) {
2173 struct raid5_percpu *percpu;
2174 struct flex_array *scribble;
2175
2176 percpu = per_cpu_ptr(conf->percpu, cpu);
2177 scribble = scribble_alloc(new_disks,
2178 new_sectors / STRIPE_SECTORS,
2179 GFP_NOIO);
2180
2181 if (scribble) {
2182 flex_array_free(percpu->scribble);
2183 percpu->scribble = scribble;
2184 } else {
2185 err = -ENOMEM;
2186 break;
2187 }
2188 }
2189 put_online_cpus();
2190 mddev_resume(conf->mddev);
Shaohua Li27a353c2016-02-24 17:38:28 -08002191 if (!err) {
2192 conf->scribble_disks = new_disks;
2193 conf->scribble_sectors = new_sectors;
2194 }
NeilBrown738a2732015-05-08 18:19:39 +10002195 return err;
2196}
2197
NeilBrownd1688a62011-10-11 16:49:52 +11002198static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08002199{
2200 /* Make all the stripes able to hold 'newsize' devices.
2201 * New slots in each stripe get 'page' set to a new page.
2202 *
2203 * This happens in stages:
2204 * 1/ create a new kmem_cache and allocate the required number of
2205 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09002206 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08002207 * to the new stripe_heads. This will have the side effect of
2208 * freezing the array as once all stripe_heads have been collected,
2209 * no IO will be possible. Old stripe heads are freed once their
2210 * pages have been transferred over, and the old kmem_cache is
2211 * freed when all stripes are done.
2212 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
2213 * we simple return a failre status - no need to clean anything up.
2214 * 4/ allocate new pages for the new slots in the new stripe_heads.
2215 * If this fails, we don't bother trying the shrink the
2216 * stripe_heads down again, we just leave them as they are.
2217 * As each stripe_head is processed the new one is released into
2218 * active service.
2219 *
2220 * Once step2 is started, we cannot afford to wait for a write,
2221 * so we use GFP_NOIO allocations.
2222 */
2223 struct stripe_head *osh, *nsh;
2224 LIST_HEAD(newstripes);
2225 struct disk_info *ndisks;
Dan Williamsb5470dc2008-06-27 21:44:04 -07002226 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08002227 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002228 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11002229 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08002230
2231 if (newsize <= conf->pool_size)
2232 return 0; /* never bother to shrink */
2233
Dan Williamsb5470dc2008-06-27 21:44:04 -07002234 err = md_allow_write(conf->mddev);
2235 if (err)
2236 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08002237
NeilBrownad01c9e2006-03-27 01:18:07 -08002238 /* Step 1 */
2239 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2240 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002241 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08002242 if (!sc)
2243 return -ENOMEM;
2244
NeilBrown2d5b5692015-07-06 12:49:23 +10002245 /* Need to ensure auto-resizing doesn't interfere */
2246 mutex_lock(&conf->cache_size_mutex);
2247
NeilBrownad01c9e2006-03-27 01:18:07 -08002248 for (i = conf->max_nr_stripes; i; i--) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002249 nsh = alloc_stripe(sc, GFP_KERNEL, newsize);
NeilBrownad01c9e2006-03-27 01:18:07 -08002250 if (!nsh)
2251 break;
2252
NeilBrownad01c9e2006-03-27 01:18:07 -08002253 nsh->raid_conf = conf;
NeilBrownad01c9e2006-03-27 01:18:07 -08002254 list_add(&nsh->lru, &newstripes);
2255 }
2256 if (i) {
2257 /* didn't get enough, give up */
2258 while (!list_empty(&newstripes)) {
2259 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2260 list_del(&nsh->lru);
2261 kmem_cache_free(sc, nsh);
2262 }
2263 kmem_cache_destroy(sc);
NeilBrown2d5b5692015-07-06 12:49:23 +10002264 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002265 return -ENOMEM;
2266 }
2267 /* Step 2 - Must use GFP_NOIO now.
2268 * OK, we have enough stripes, start collecting inactive
2269 * stripes and copying them over
2270 */
Shaohua Li566c09c2013-11-14 15:16:17 +11002271 hash = 0;
2272 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08002273 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11002274 lock_device_hash_lock(conf, hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08002275 wait_event_cmd(conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +11002276 !list_empty(conf->inactive_list + hash),
2277 unlock_device_hash_lock(conf, hash),
2278 lock_device_hash_lock(conf, hash));
2279 osh = get_free_stripe(conf, hash);
2280 unlock_device_hash_lock(conf, hash);
NeilBrownf18c1a32015-05-08 18:19:04 +10002281
Shaohua Lid592a992014-05-21 17:57:44 +08002282 for(i=0; i<conf->pool_size; i++) {
NeilBrownad01c9e2006-03-27 01:18:07 -08002283 nsh->dev[i].page = osh->dev[i].page;
Shaohua Lid592a992014-05-21 17:57:44 +08002284 nsh->dev[i].orig_page = osh->dev[i].page;
2285 }
Shaohua Li566c09c2013-11-14 15:16:17 +11002286 nsh->hash_lock_index = hash;
NeilBrownad01c9e2006-03-27 01:18:07 -08002287 kmem_cache_free(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11002288 cnt++;
2289 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2290 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2291 hash++;
2292 cnt = 0;
2293 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002294 }
2295 kmem_cache_destroy(conf->slab_cache);
2296
2297 /* Step 3.
2298 * At this point, we are holding all the stripes so the array
2299 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07002300 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08002301 */
2302 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
2303 if (ndisks) {
Song Liud7bd3982016-11-23 22:50:39 -08002304 for (i = 0; i < conf->pool_size; i++)
NeilBrownad01c9e2006-03-27 01:18:07 -08002305 ndisks[i] = conf->disks[i];
Song Liud7bd3982016-11-23 22:50:39 -08002306
2307 for (i = conf->pool_size; i < newsize; i++) {
2308 ndisks[i].extra_page = alloc_page(GFP_NOIO);
2309 if (!ndisks[i].extra_page)
2310 err = -ENOMEM;
2311 }
2312
2313 if (err) {
2314 for (i = conf->pool_size; i < newsize; i++)
2315 if (ndisks[i].extra_page)
2316 put_page(ndisks[i].extra_page);
2317 kfree(ndisks);
2318 } else {
2319 kfree(conf->disks);
2320 conf->disks = ndisks;
2321 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002322 } else
2323 err = -ENOMEM;
2324
NeilBrown2d5b5692015-07-06 12:49:23 +10002325 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002326 /* Step 4, return new stripes to service */
2327 while(!list_empty(&newstripes)) {
2328 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2329 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07002330
NeilBrownad01c9e2006-03-27 01:18:07 -08002331 for (i=conf->raid_disks; i < newsize; i++)
2332 if (nsh->dev[i].page == NULL) {
2333 struct page *p = alloc_page(GFP_NOIO);
2334 nsh->dev[i].page = p;
Shaohua Lid592a992014-05-21 17:57:44 +08002335 nsh->dev[i].orig_page = p;
NeilBrownad01c9e2006-03-27 01:18:07 -08002336 if (!p)
2337 err = -ENOMEM;
2338 }
Shaohua Li6d036f72015-08-13 14:31:57 -07002339 raid5_release_stripe(nsh);
NeilBrownad01c9e2006-03-27 01:18:07 -08002340 }
2341 /* critical section pass, GFP_NOIO no longer needed */
2342
2343 conf->slab_cache = sc;
2344 conf->active_name = 1-conf->active_name;
NeilBrown6e9eac22015-05-08 18:19:34 +10002345 if (!err)
2346 conf->pool_size = newsize;
NeilBrownad01c9e2006-03-27 01:18:07 -08002347 return err;
2348}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
NeilBrown486f0642015-02-25 12:10:35 +11002350static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
2352 struct stripe_head *sh;
NeilBrown49895bc2015-08-03 17:09:57 +10002353 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Shaohua Li566c09c2013-11-14 15:16:17 +11002355 spin_lock_irq(conf->hash_locks + hash);
2356 sh = get_free_stripe(conf, hash);
2357 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08002358 if (!sh)
2359 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002360 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10002361 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002362 kmem_cache_free(conf->slab_cache, sh);
2363 atomic_dec(&conf->active_stripes);
NeilBrown486f0642015-02-25 12:10:35 +11002364 conf->max_nr_stripes--;
NeilBrown3f294f42005-11-08 21:39:25 -08002365 return 1;
2366}
2367
NeilBrownd1688a62011-10-11 16:49:52 +11002368static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08002369{
NeilBrown486f0642015-02-25 12:10:35 +11002370 while (conf->max_nr_stripes &&
2371 drop_one_stripe(conf))
2372 ;
NeilBrown3f294f42005-11-08 21:39:25 -08002373
Julia Lawall644df1a2015-09-13 14:15:10 +02002374 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 conf->slab_cache = NULL;
2376}
2377
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002378static void raid5_end_read_request(struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
NeilBrown99c0fb52009-03-31 14:39:38 +11002380 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002381 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002382 int disks = sh->disks, i;
NeilBrownd6950432006-07-10 04:44:20 -07002383 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11002384 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10002385 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387 for (i=0 ; i<disks; i++)
2388 if (bi == &sh->dev[i].req)
2389 break;
2390
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002391 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
Dan Williams45b42332007-07-09 11:56:43 -07002392 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002393 bi->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002395 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002397 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
NeilBrown14a75d32011-12-23 10:17:52 +11002399 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11002400 /* If replacement finished while this request was outstanding,
2401 * 'replacement' might be NULL already.
2402 * In that case it moved down to 'rdev'.
2403 * rdev is not removed until all requests are finished.
2404 */
NeilBrown14a75d32011-12-23 10:17:52 +11002405 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002406 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11002407 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
NeilBrown05616be2012-05-21 09:27:00 +10002409 if (use_new_offset(conf, sh))
2410 s = sh->sector + rdev->new_data_offset;
2411 else
2412 s = sh->sector + rdev->data_offset;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002413 if (!bi->bi_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08002415 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11002416 /* Note that this cannot happen on a
2417 * replacement device. We just fail those on
2418 * any error
2419 */
NeilBrowncc6167b2016-11-02 14:16:50 +11002420 pr_info_ratelimited(
2421 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002422 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10002423 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002424 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10002425 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08002426 clear_bit(R5_ReadError, &sh->dev[i].flags);
2427 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002428 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2429 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2430
Song Liu86aa1392017-01-12 17:22:41 -08002431 if (test_bit(R5_InJournal, &sh->dev[i].flags))
2432 /*
2433 * end read for a page in journal, this
2434 * must be preparing for prexor in rmw
2435 */
2436 set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
2437
NeilBrown14a75d32011-12-23 10:17:52 +11002438 if (atomic_read(&rdev->read_errors))
2439 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002441 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002442 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002443 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07002446 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002447 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowncc6167b2016-11-02 14:16:50 +11002448 pr_warn_ratelimited(
2449 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
NeilBrown14a75d32011-12-23 10:17:52 +11002450 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002451 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002452 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002453 else if (conf->mddev->degraded >= conf->max_degraded) {
2454 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002455 pr_warn_ratelimited(
2456 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002457 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002458 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002459 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002460 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002461 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002462 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002463 pr_warn_ratelimited(
2464 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002465 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002466 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002467 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002468 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08002469 > conf->max_nr_stripes)
NeilBrowncc6167b2016-11-02 14:16:50 +11002470 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07002471 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08002472 else
2473 retry = 1;
Bian Yuedfa1f62013-11-14 15:16:17 +11002474 if (set_bad && test_bit(In_sync, &rdev->flags)
2475 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2476 retry = 1;
NeilBrownba22dcb2005-11-08 21:39:31 -08002477 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10002478 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2479 set_bit(R5_ReadError, &sh->dev[i].flags);
2480 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2481 } else
2482 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002483 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002484 clear_bit(R5_ReadError, &sh->dev[i].flags);
2485 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002486 if (!(set_bad
2487 && test_bit(In_sync, &rdev->flags)
2488 && rdev_set_badblocks(
2489 rdev, sh->sector, STRIPE_SECTORS, 0)))
2490 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
NeilBrown14a75d32011-12-23 10:17:52 +11002493 rdev_dec_pending(rdev, conf->mddev);
Shaohua Lic9445552016-09-08 10:43:58 -07002494 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2496 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002497 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
2499
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002500static void raid5_end_write_request(struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
NeilBrown99c0fb52009-03-31 14:39:38 +11002502 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002503 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002504 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11002505 struct md_rdev *uninitialized_var(rdev);
NeilBrownb84db562011-07-28 11:39:23 +10002506 sector_t first_bad;
2507 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002508 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
NeilBrown977df362011-12-23 10:17:53 +11002510 for (i = 0 ; i < disks; i++) {
2511 if (bi == &sh->dev[i].req) {
2512 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 break;
NeilBrown977df362011-12-23 10:17:53 +11002514 }
2515 if (bi == &sh->dev[i].rreq) {
2516 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002517 if (rdev)
2518 replacement = 1;
2519 else
2520 /* rdev was removed and 'replacement'
2521 * replaced it. rdev is not removed
2522 * until all requests are finished.
2523 */
2524 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002525 break;
2526 }
2527 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002528 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002530 bi->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002532 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002534 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
2536
NeilBrown977df362011-12-23 10:17:53 +11002537 if (replacement) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002538 if (bi->bi_error)
NeilBrown977df362011-12-23 10:17:53 +11002539 md_error(conf->mddev, rdev);
2540 else if (is_badblock(rdev, sh->sector,
2541 STRIPE_SECTORS,
2542 &first_bad, &bad_sectors))
2543 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2544 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002545 if (bi->bi_error) {
NeilBrown9f97e4b2014-01-16 09:35:38 +11002546 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11002547 set_bit(WriteErrorSeen, &rdev->flags);
2548 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002549 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2550 set_bit(MD_RECOVERY_NEEDED,
2551 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002552 } else if (is_badblock(rdev, sh->sector,
2553 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10002554 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002555 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002556 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2557 /* That was a successful write so make
2558 * sure it looks like we already did
2559 * a re-write.
2560 */
2561 set_bit(R5_ReWrite, &sh->dev[i].flags);
2562 }
NeilBrown977df362011-12-23 10:17:53 +11002563 }
2564 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002566 if (sh->batch_head && bi->bi_error && !replacement)
shli@kernel.org72ac7332014-12-15 12:57:03 +11002567 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2568
Shaohua Lic9445552016-09-08 10:43:58 -07002569 bio_reset(bi);
NeilBrown977df362011-12-23 10:17:53 +11002570 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2571 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002573 raid5_release_stripe(sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002574
2575 if (sh->batch_head && sh != sh->batch_head)
Shaohua Li6d036f72015-08-13 14:31:57 -07002576 raid5_release_stripe(sh->batch_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
2578
NeilBrown784052e2009-03-31 15:19:07 +11002579static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
2581 struct r5dev *dev = &sh->dev[i];
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 dev->flags = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07002584 dev->sector = raid5_compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585}
2586
Shaohua Li849674e2016-01-20 13:52:20 -08002587static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
2589 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002590 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002591 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002592 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
NeilBrown908f4fb2011-12-23 10:17:50 +11002594 spin_lock_irqsave(&conf->device_lock, flags);
2595 clear_bit(In_sync, &rdev->flags);
Song Liu2e38a372017-01-24 10:45:30 -08002596 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11002597 spin_unlock_irqrestore(&conf->device_lock, flags);
2598 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2599
NeilBrownde393cd2011-07-28 11:31:48 +10002600 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10002601 set_bit(Faulty, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08002602 set_mask_bits(&mddev->sb_flags, 0,
2603 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrowncc6167b2016-11-02 14:16:50 +11002604 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
2605 "md/raid:%s: Operation continuing on %d devices.\n",
2606 mdname(mddev),
2607 bdevname(rdev->bdev, b),
2608 mdname(mddev),
2609 conf->raid_disks - mddev->degraded);
Song Liu2e38a372017-01-24 10:45:30 -08002610 r5c_update_on_rdev_error(mddev);
NeilBrown16a53ec2006-06-26 00:27:38 -07002611}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
2613/*
2614 * Input: a 'big' sector number,
2615 * Output: index of the data and parity disk, and the sector # in them.
2616 */
Shaohua Li6d036f72015-08-13 14:31:57 -07002617sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2618 int previous, int *dd_idx,
2619 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002621 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002622 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002624 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002625 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002627 int algorithm = previous ? conf->prev_algo
2628 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002629 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2630 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002631 int raid_disks = previous ? conf->previous_raid_disks
2632 : conf->raid_disks;
2633 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
2635 /* First compute the information on this sector */
2636
2637 /*
2638 * Compute the chunk number and the sector offset inside the chunk
2639 */
2640 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2641 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 /*
2644 * Compute the stripe number
2645 */
NeilBrown35f2a592010-04-20 14:13:34 +10002646 stripe = chunk_number;
2647 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002648 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 /*
2650 * Select the parity disk based on the user selected algorithm.
2651 */
NeilBrown84789552011-07-27 11:00:36 +10002652 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002653 switch(conf->level) {
2654 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002655 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002656 break;
2657 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002658 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002660 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002661 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 (*dd_idx)++;
2663 break;
2664 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002665 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002666 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 (*dd_idx)++;
2668 break;
2669 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002670 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002671 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 break;
2673 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002674 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002675 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002677 case ALGORITHM_PARITY_0:
2678 pd_idx = 0;
2679 (*dd_idx)++;
2680 break;
2681 case ALGORITHM_PARITY_N:
2682 pd_idx = data_disks;
2683 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002685 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002686 }
2687 break;
2688 case 6:
2689
NeilBrowne183eae2009-03-31 15:20:22 +11002690 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002691 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002692 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002693 qd_idx = pd_idx + 1;
2694 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002695 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002696 qd_idx = 0;
2697 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002698 (*dd_idx) += 2; /* D D P Q D */
2699 break;
2700 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002701 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002702 qd_idx = pd_idx + 1;
2703 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002704 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002705 qd_idx = 0;
2706 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002707 (*dd_idx) += 2; /* D D P Q D */
2708 break;
2709 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002710 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002711 qd_idx = (pd_idx + 1) % raid_disks;
2712 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002713 break;
2714 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002715 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002716 qd_idx = (pd_idx + 1) % raid_disks;
2717 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002718 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002719
2720 case ALGORITHM_PARITY_0:
2721 pd_idx = 0;
2722 qd_idx = 1;
2723 (*dd_idx) += 2;
2724 break;
2725 case ALGORITHM_PARITY_N:
2726 pd_idx = data_disks;
2727 qd_idx = data_disks + 1;
2728 break;
2729
2730 case ALGORITHM_ROTATING_ZERO_RESTART:
2731 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2732 * of blocks for computing Q is different.
2733 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002734 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002735 qd_idx = pd_idx + 1;
2736 if (pd_idx == raid_disks-1) {
2737 (*dd_idx)++; /* Q D D D P */
2738 qd_idx = 0;
2739 } else if (*dd_idx >= pd_idx)
2740 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002741 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002742 break;
2743
2744 case ALGORITHM_ROTATING_N_RESTART:
2745 /* Same a left_asymmetric, by first stripe is
2746 * D D D P Q rather than
2747 * Q D D D P
2748 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002749 stripe2 += 1;
2750 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002751 qd_idx = pd_idx + 1;
2752 if (pd_idx == raid_disks-1) {
2753 (*dd_idx)++; /* Q D D D P */
2754 qd_idx = 0;
2755 } else if (*dd_idx >= pd_idx)
2756 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002757 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002758 break;
2759
2760 case ALGORITHM_ROTATING_N_CONTINUE:
2761 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002762 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002763 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2764 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002765 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002766 break;
2767
2768 case ALGORITHM_LEFT_ASYMMETRIC_6:
2769 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002770 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002771 if (*dd_idx >= pd_idx)
2772 (*dd_idx)++;
2773 qd_idx = raid_disks - 1;
2774 break;
2775
2776 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002777 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002778 if (*dd_idx >= pd_idx)
2779 (*dd_idx)++;
2780 qd_idx = raid_disks - 1;
2781 break;
2782
2783 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002784 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002785 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2786 qd_idx = raid_disks - 1;
2787 break;
2788
2789 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002790 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002791 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2792 qd_idx = raid_disks - 1;
2793 break;
2794
2795 case ALGORITHM_PARITY_0_6:
2796 pd_idx = 0;
2797 (*dd_idx)++;
2798 qd_idx = raid_disks - 1;
2799 break;
2800
NeilBrown16a53ec2006-06-26 00:27:38 -07002801 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002802 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002803 }
2804 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 }
2806
NeilBrown911d4ee2009-03-31 14:39:38 +11002807 if (sh) {
2808 sh->pd_idx = pd_idx;
2809 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002810 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 /*
2813 * Finally, compute the new sector number
2814 */
2815 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2816 return new_sector;
2817}
2818
Shaohua Li6d036f72015-08-13 14:31:57 -07002819sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
NeilBrownd1688a62011-10-11 16:49:52 +11002821 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002822 int raid_disks = sh->disks;
2823 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002825 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2826 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002827 int algorithm = previous ? conf->prev_algo
2828 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 sector_t stripe;
2830 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002831 sector_t chunk_number;
2832 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002834 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
2836 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2837 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
NeilBrown16a53ec2006-06-26 00:27:38 -07002839 if (i == sh->pd_idx)
2840 return 0;
2841 switch(conf->level) {
2842 case 4: break;
2843 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002844 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 case ALGORITHM_LEFT_ASYMMETRIC:
2846 case ALGORITHM_RIGHT_ASYMMETRIC:
2847 if (i > sh->pd_idx)
2848 i--;
2849 break;
2850 case ALGORITHM_LEFT_SYMMETRIC:
2851 case ALGORITHM_RIGHT_SYMMETRIC:
2852 if (i < sh->pd_idx)
2853 i += raid_disks;
2854 i -= (sh->pd_idx + 1);
2855 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002856 case ALGORITHM_PARITY_0:
2857 i -= 1;
2858 break;
2859 case ALGORITHM_PARITY_N:
2860 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002862 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002863 }
2864 break;
2865 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002866 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002867 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002868 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002869 case ALGORITHM_LEFT_ASYMMETRIC:
2870 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002871 case ALGORITHM_ROTATING_ZERO_RESTART:
2872 case ALGORITHM_ROTATING_N_RESTART:
2873 if (sh->pd_idx == raid_disks-1)
2874 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002875 else if (i > sh->pd_idx)
2876 i -= 2; /* D D P Q D */
2877 break;
2878 case ALGORITHM_LEFT_SYMMETRIC:
2879 case ALGORITHM_RIGHT_SYMMETRIC:
2880 if (sh->pd_idx == raid_disks-1)
2881 i--; /* Q D D D P */
2882 else {
2883 /* D D P Q D */
2884 if (i < sh->pd_idx)
2885 i += raid_disks;
2886 i -= (sh->pd_idx + 2);
2887 }
2888 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002889 case ALGORITHM_PARITY_0:
2890 i -= 2;
2891 break;
2892 case ALGORITHM_PARITY_N:
2893 break;
2894 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002895 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002896 if (sh->pd_idx == 0)
2897 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002898 else {
2899 /* D D Q P D */
2900 if (i < sh->pd_idx)
2901 i += raid_disks;
2902 i -= (sh->pd_idx + 1);
2903 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002904 break;
2905 case ALGORITHM_LEFT_ASYMMETRIC_6:
2906 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2907 if (i > sh->pd_idx)
2908 i--;
2909 break;
2910 case ALGORITHM_LEFT_SYMMETRIC_6:
2911 case ALGORITHM_RIGHT_SYMMETRIC_6:
2912 if (i < sh->pd_idx)
2913 i += data_disks + 1;
2914 i -= (sh->pd_idx + 1);
2915 break;
2916 case ALGORITHM_PARITY_0_6:
2917 i -= 1;
2918 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002919 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002920 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002921 }
2922 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 }
2924
2925 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002926 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
NeilBrown112bf892009-03-31 14:39:38 +11002928 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002929 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002930 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2931 || sh2.qd_idx != sh->qd_idx) {
NeilBrowncc6167b2016-11-02 14:16:50 +11002932 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
2933 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 return 0;
2935 }
2936 return r_sector;
2937}
2938
Song Liu07e83362017-01-23 17:12:58 -08002939/*
2940 * There are cases where we want handle_stripe_dirtying() and
2941 * schedule_reconstruction() to delay towrite to some dev of a stripe.
2942 *
2943 * This function checks whether we want to delay the towrite. Specifically,
2944 * we delay the towrite when:
2945 *
2946 * 1. degraded stripe has a non-overwrite to the missing dev, AND this
2947 * stripe has data in journal (for other devices).
2948 *
2949 * In this case, when reading data for the non-overwrite dev, it is
2950 * necessary to handle complex rmw of write back cache (prexor with
2951 * orig_page, and xor with page). To keep read path simple, we would
2952 * like to flush data in journal to RAID disks first, so complex rmw
2953 * is handled in the write patch (handle_stripe_dirtying).
2954 *
Song Liu39b99582017-01-24 14:08:23 -08002955 * 2. when journal space is critical (R5C_LOG_CRITICAL=1)
2956 *
2957 * It is important to be able to flush all stripes in raid5-cache.
2958 * Therefore, we need reserve some space on the journal device for
2959 * these flushes. If flush operation includes pending writes to the
2960 * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe
2961 * for the flush out. If we exclude these pending writes from flush
2962 * operation, we only need (conf->max_degraded + 1) pages per stripe.
2963 * Therefore, excluding pending writes in these cases enables more
2964 * efficient use of the journal device.
2965 *
2966 * Note: To make sure the stripe makes progress, we only delay
2967 * towrite for stripes with data already in journal (injournal > 0).
2968 * When LOG_CRITICAL, stripes with injournal == 0 will be sent to
2969 * no_space_stripes list.
2970 *
Song Liu07e83362017-01-23 17:12:58 -08002971 */
Song Liu39b99582017-01-24 14:08:23 -08002972static inline bool delay_towrite(struct r5conf *conf,
2973 struct r5dev *dev,
2974 struct stripe_head_state *s)
Song Liu07e83362017-01-23 17:12:58 -08002975{
Song Liu39b99582017-01-24 14:08:23 -08002976 /* case 1 above */
2977 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2978 !test_bit(R5_Insync, &dev->flags) && s->injournal)
2979 return true;
2980 /* case 2 above */
2981 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
2982 s->injournal > 0)
2983 return true;
2984 return false;
Song Liu07e83362017-01-23 17:12:58 -08002985}
2986
Dan Williams600aa102008-06-28 08:32:05 +10002987static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002988schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002989 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002990{
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002991 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002992 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002993 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002994
Dan Williamse33129d2007-01-02 13:52:30 -07002995 if (rcw) {
Song Liu1e6d6902016-11-17 15:24:39 -08002996 /*
2997 * In some cases, handle_stripe_dirtying initially decided to
2998 * run rmw and allocates extra page for prexor. However, rcw is
2999 * cheaper later on. We need to free the extra page now,
3000 * because we won't be able to do that in ops_complete_prexor().
3001 */
3002 r5c_release_extra_page(sh);
Dan Williamse33129d2007-01-02 13:52:30 -07003003
3004 for (i = disks; i--; ) {
3005 struct r5dev *dev = &sh->dev[i];
3006
Song Liu39b99582017-01-24 14:08:23 -08003007 if (dev->towrite && !delay_towrite(conf, dev, s)) {
Dan Williamse33129d2007-01-02 13:52:30 -07003008 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10003009 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003010 if (!expand)
3011 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003012 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003013 } else if (test_bit(R5_InJournal, &dev->flags)) {
3014 set_bit(R5_LOCKED, &dev->flags);
3015 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003016 }
3017 }
NeilBrownce7d3632013-03-04 12:37:14 +11003018 /* if we are not expanding this is a proper write request, and
3019 * there will be bios with new data to be drained into the
3020 * stripe cache
3021 */
3022 if (!expand) {
3023 if (!s->locked)
3024 /* False alarm, nothing to do */
3025 return;
3026 sh->reconstruct_state = reconstruct_state_drain_run;
3027 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3028 } else
3029 sh->reconstruct_state = reconstruct_state_run;
3030
3031 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3032
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003033 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003034 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003035 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07003036 } else {
3037 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
3038 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003039 BUG_ON(level == 6 &&
3040 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
3041 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
Dan Williamse33129d2007-01-02 13:52:30 -07003042
Dan Williamse33129d2007-01-02 13:52:30 -07003043 for (i = disks; i--; ) {
3044 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003045 if (i == pd_idx || i == qd_idx)
Dan Williamse33129d2007-01-02 13:52:30 -07003046 continue;
3047
Dan Williamse33129d2007-01-02 13:52:30 -07003048 if (dev->towrite &&
3049 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10003050 test_bit(R5_Wantcompute, &dev->flags))) {
3051 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003052 set_bit(R5_LOCKED, &dev->flags);
3053 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003054 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003055 } else if (test_bit(R5_InJournal, &dev->flags)) {
3056 set_bit(R5_LOCKED, &dev->flags);
3057 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003058 }
3059 }
NeilBrownce7d3632013-03-04 12:37:14 +11003060 if (!s->locked)
3061 /* False alarm - nothing to do */
3062 return;
3063 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
3064 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
3065 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3066 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003067 }
3068
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003069 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07003070 * are in flight
3071 */
3072 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
3073 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10003074 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003075
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003076 if (level == 6) {
3077 int qd_idx = sh->qd_idx;
3078 struct r5dev *dev = &sh->dev[qd_idx];
3079
3080 set_bit(R5_LOCKED, &dev->flags);
3081 clear_bit(R5_UPTODATE, &dev->flags);
3082 s->locked++;
3083 }
3084
Dan Williams600aa102008-06-28 08:32:05 +10003085 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07003086 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10003087 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003088}
NeilBrown16a53ec2006-06-26 00:27:38 -07003089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090/*
3091 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07003092 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 * The bi_next chain must be in order.
3094 */
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003095static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
3096 int forwrite, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
3098 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11003099 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07003100 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
NeilBrowncbe47ec2011-07-26 11:20:35 +10003102 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003103 (unsigned long long)bi->bi_iter.bi_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 (unsigned long long)sh->sector);
3105
Shaohua Lib17459c2012-07-19 16:01:31 +10003106 /*
3107 * If several bio share a stripe. The bio bi_phys_segments acts as a
3108 * reference count to avoid race. The reference count should already be
3109 * increased before this function is called (for example, in
Shaohua Li849674e2016-01-20 13:52:20 -08003110 * raid5_make_request()), so other bio sharing this stripe will not free the
Shaohua Lib17459c2012-07-19 16:01:31 +10003111 * stripe. If a stripe is owned by one stripe, the stripe lock will
3112 * protect it.
3113 */
3114 spin_lock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003115 /* Don't allow new IO added to stripes in batch list */
3116 if (sh->batch_head)
3117 goto overlap;
NeilBrown72626682005-09-09 16:23:54 -07003118 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003120 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07003121 firstwrite = 1;
3122 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 bip = &sh->dev[dd_idx].toread;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003124 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
3125 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 goto overlap;
3127 bip = & (*bip)->bi_next;
3128 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07003129 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 goto overlap;
3131
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003132 if (!forwrite || previous)
3133 clear_bit(STRIPE_BATCH_READY, &sh->state);
3134
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02003135 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 if (*bip)
3137 bi->bi_next = *bip;
3138 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003139 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07003140
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 if (forwrite) {
3142 /* check if page is covered */
3143 sector_t sector = sh->dev[dd_idx].sector;
3144 for (bi=sh->dev[dd_idx].towrite;
3145 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07003146 bi && bi->bi_iter.bi_sector <= sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07003148 if (bio_end_sector(bi) >= sector)
3149 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 }
3151 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
shli@kernel.org7a87f432014-12-15 12:57:03 +11003152 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
3153 sh->overwrite_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003155
3156 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003157 (unsigned long long)(*bip)->bi_iter.bi_sector,
NeilBrowncbe47ec2011-07-26 11:20:35 +10003158 (unsigned long long)sh->sector, dd_idx);
3159
3160 if (conf->mddev->bitmap && firstwrite) {
NeilBrownd0852df52015-05-27 08:43:45 +10003161 /* Cannot hold spinlock over bitmap_startwrite,
3162 * but must ensure this isn't added to a batch until
3163 * we have added to the bitmap and set bm_seq.
3164 * So set STRIPE_BITMAP_PENDING to prevent
3165 * batching.
3166 * If multiple add_stripe_bio() calls race here they
3167 * much all set STRIPE_BITMAP_PENDING. So only the first one
3168 * to complete "bitmap_startwrite" gets to set
3169 * STRIPE_BIT_DELAY. This is important as once a stripe
3170 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3171 * any more.
3172 */
3173 set_bit(STRIPE_BITMAP_PENDING, &sh->state);
3174 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10003175 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
3176 STRIPE_SECTORS, 0);
NeilBrownd0852df52015-05-27 08:43:45 +10003177 spin_lock_irq(&sh->stripe_lock);
3178 clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
3179 if (!sh->batch_head) {
3180 sh->bm_seq = conf->seq_flush+1;
3181 set_bit(STRIPE_BIT_DELAY, &sh->state);
3182 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003183 }
NeilBrownd0852df52015-05-27 08:43:45 +10003184 spin_unlock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003185
3186 if (stripe_can_batch(sh))
3187 stripe_add_to_batch_list(conf, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 return 1;
3189
3190 overlap:
3191 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10003192 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 return 0;
3194}
3195
NeilBrownd1688a62011-10-11 16:49:52 +11003196static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08003197
NeilBrownd1688a62011-10-11 16:49:52 +11003198static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003199 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08003200{
NeilBrown784052e2009-03-31 15:19:07 +11003201 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10003202 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11003203 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003204 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11003205 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003206
NeilBrown112bf892009-03-31 14:39:38 +11003207 raid5_compute_sector(conf,
3208 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08003209 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11003210 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003211 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003212}
3213
Dan Williamsa4456852007-07-09 11:56:43 -07003214static void
NeilBrownd1688a62011-10-11 16:49:52 +11003215handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003216 struct stripe_head_state *s, int disks,
NeilBrown34a6f802015-08-14 12:07:57 +10003217 struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003218{
3219 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003220 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003221 for (i = disks; i--; ) {
3222 struct bio *bi;
3223 int bitmap_end = 0;
3224
3225 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003226 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07003227 rcu_read_lock();
3228 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownf5b67ae2016-06-02 16:19:53 +10003229 if (rdev && test_bit(In_sync, &rdev->flags) &&
3230 !test_bit(Faulty, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10003231 atomic_inc(&rdev->nr_pending);
3232 else
3233 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003234 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10003235 if (rdev) {
3236 if (!rdev_set_badblocks(
3237 rdev,
3238 sh->sector,
3239 STRIPE_SECTORS, 0))
3240 md_error(conf->mddev, rdev);
3241 rdev_dec_pending(rdev, conf->mddev);
3242 }
Dan Williamsa4456852007-07-09 11:56:43 -07003243 }
Shaohua Lib17459c2012-07-19 16:01:31 +10003244 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003245 /* fail all writes first */
3246 bi = sh->dev[i].towrite;
3247 sh->dev[i].towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11003248 sh->overwrite_disks = 0;
Shaohua Lib17459c2012-07-19 16:01:31 +10003249 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11003250 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003251 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07003252
Shaohua Li0576b1c2015-08-13 14:32:00 -07003253 r5l_stripe_write_finished(sh);
3254
Dan Williamsa4456852007-07-09 11:56:43 -07003255 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3256 wake_up(&conf->wait_for_overlap);
3257
Kent Overstreet4f024f32013-10-11 15:44:27 -07003258 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003259 sh->dev[i].sector + STRIPE_SECTORS) {
3260 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003261
3262 bi->bi_error = -EIO;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003263 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003264 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003265 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003266 }
3267 bi = nextbi;
3268 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003269 if (bitmap_end)
3270 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3271 STRIPE_SECTORS, 0, 0);
3272 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003273 /* and fail all 'written' */
3274 bi = sh->dev[i].written;
3275 sh->dev[i].written = NULL;
Shaohua Lid592a992014-05-21 17:57:44 +08003276 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3277 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3278 sh->dev[i].page = sh->dev[i].orig_page;
3279 }
3280
Dan Williamsa4456852007-07-09 11:56:43 -07003281 if (bi) bitmap_end = 1;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003282 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003283 sh->dev[i].sector + STRIPE_SECTORS) {
3284 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003285
3286 bi->bi_error = -EIO;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003287 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003288 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003289 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003290 }
3291 bi = bi2;
3292 }
3293
Dan Williamsb5e98d62007-01-02 13:52:31 -07003294 /* fail any reads if this device is non-operational and
3295 * the data has not reached the cache yet.
3296 */
3297 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
Shaohua Li6e74a9c2015-10-08 21:54:08 -07003298 s->failed > conf->max_degraded &&
Dan Williamsb5e98d62007-01-02 13:52:31 -07003299 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3300 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11003301 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003302 bi = sh->dev[i].toread;
3303 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11003304 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003305 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3306 wake_up(&conf->wait_for_overlap);
Shaohua Liebda7802015-09-18 10:20:13 -07003307 if (bi)
3308 s->to_read--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003309 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003310 sh->dev[i].sector + STRIPE_SECTORS) {
3311 struct bio *nextbi =
3312 r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003313
3314 bi->bi_error = -EIO;
NeilBrown34a6f802015-08-14 12:07:57 +10003315 if (!raid5_dec_bi_active_stripes(bi))
3316 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003317 bi = nextbi;
3318 }
3319 }
Dan Williamsa4456852007-07-09 11:56:43 -07003320 if (bitmap_end)
3321 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3322 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10003323 /* If we were in the middle of a write the parity block might
3324 * still be locked - so just clear all R5_LOCKED flags
3325 */
3326 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003327 }
Shaohua Liebda7802015-09-18 10:20:13 -07003328 s->to_write = 0;
3329 s->written = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003330
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003331 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3332 if (atomic_dec_and_test(&conf->pending_full_writes))
3333 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003334}
3335
NeilBrown7f0da592011-07-28 11:39:22 +10003336static void
NeilBrownd1688a62011-10-11 16:49:52 +11003337handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10003338 struct stripe_head_state *s)
3339{
3340 int abort = 0;
3341 int i;
3342
shli@kernel.org59fc6302014-12-15 12:57:03 +11003343 BUG_ON(sh->batch_head);
NeilBrown7f0da592011-07-28 11:39:22 +10003344 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003345 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3346 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10003347 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11003348 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10003349 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10003350 * Don't even need to abort as that is handled elsewhere
3351 * if needed, and not always wanted e.g. if there is a known
3352 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11003353 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10003354 * non-sync devices, or abort the recovery
3355 */
NeilBrown18b98372012-04-01 23:48:38 +10003356 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3357 /* During recovery devices cannot be removed, so
3358 * locking and refcounting of rdevs is not needed
3359 */
NeilBrowne50d3992016-06-02 16:19:52 +10003360 rcu_read_lock();
NeilBrown18b98372012-04-01 23:48:38 +10003361 for (i = 0; i < conf->raid_disks; i++) {
NeilBrowne50d3992016-06-02 16:19:52 +10003362 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown18b98372012-04-01 23:48:38 +10003363 if (rdev
3364 && !test_bit(Faulty, &rdev->flags)
3365 && !test_bit(In_sync, &rdev->flags)
3366 && !rdev_set_badblocks(rdev, sh->sector,
3367 STRIPE_SECTORS, 0))
3368 abort = 1;
NeilBrowne50d3992016-06-02 16:19:52 +10003369 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown18b98372012-04-01 23:48:38 +10003370 if (rdev
3371 && !test_bit(Faulty, &rdev->flags)
3372 && !test_bit(In_sync, &rdev->flags)
3373 && !rdev_set_badblocks(rdev, sh->sector,
3374 STRIPE_SECTORS, 0))
3375 abort = 1;
3376 }
NeilBrowne50d3992016-06-02 16:19:52 +10003377 rcu_read_unlock();
NeilBrown18b98372012-04-01 23:48:38 +10003378 if (abort)
3379 conf->recovery_disabled =
3380 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10003381 }
NeilBrown18b98372012-04-01 23:48:38 +10003382 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10003383}
3384
NeilBrown9a3e1102011-12-23 10:17:53 +11003385static int want_replace(struct stripe_head *sh, int disk_idx)
3386{
3387 struct md_rdev *rdev;
3388 int rv = 0;
NeilBrown3f232d62016-06-02 16:19:52 +10003389
3390 rcu_read_lock();
3391 rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
NeilBrown9a3e1102011-12-23 10:17:53 +11003392 if (rdev
3393 && !test_bit(Faulty, &rdev->flags)
3394 && !test_bit(In_sync, &rdev->flags)
3395 && (rdev->recovery_offset <= sh->sector
3396 || rdev->mddev->recovery_cp <= sh->sector))
3397 rv = 1;
NeilBrown3f232d62016-06-02 16:19:52 +10003398 rcu_read_unlock();
NeilBrown9a3e1102011-12-23 10:17:53 +11003399 return rv;
3400}
3401
NeilBrown2c58f062015-02-02 11:32:23 +11003402static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3403 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07003404{
3405 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10003406 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3407 &sh->dev[s->failed_num[1]] };
NeilBrownea664c82015-02-02 14:03:28 +11003408 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07003409
NeilBrowna79cfe12015-02-02 11:37:59 +11003410
3411 if (test_bit(R5_LOCKED, &dev->flags) ||
3412 test_bit(R5_UPTODATE, &dev->flags))
3413 /* No point reading this as we already have it or have
3414 * decided to get it.
3415 */
3416 return 0;
3417
3418 if (dev->toread ||
3419 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3420 /* We need this block to directly satisfy a request */
3421 return 1;
3422
3423 if (s->syncing || s->expanding ||
3424 (s->replacing && want_replace(sh, disk_idx)))
3425 /* When syncing, or expanding we read everything.
3426 * When replacing, we need the replaced block.
3427 */
3428 return 1;
3429
3430 if ((s->failed >= 1 && fdev[0]->toread) ||
3431 (s->failed >= 2 && fdev[1]->toread))
3432 /* If we want to read from a failed device, then
3433 * we need to actually read every other device.
3434 */
3435 return 1;
3436
NeilBrowna9d56952015-02-02 11:49:10 +11003437 /* Sometimes neither read-modify-write nor reconstruct-write
3438 * cycles can work. In those cases we read every block we
3439 * can. Then the parity-update is certain to have enough to
3440 * work with.
3441 * This can only be a problem when we need to write something,
3442 * and some device has failed. If either of those tests
3443 * fail we need look no further.
3444 */
3445 if (!s->failed || !s->to_write)
3446 return 0;
3447
3448 if (test_bit(R5_Insync, &dev->flags) &&
3449 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3450 /* Pre-reads at not permitted until after short delay
3451 * to gather multiple requests. However if this
3452 * device is no Insync, the block could only be be computed
3453 * and there is no need to delay that.
3454 */
3455 return 0;
NeilBrownea664c82015-02-02 14:03:28 +11003456
NeilBrown36707bb2015-09-24 15:25:36 +10003457 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrownea664c82015-02-02 14:03:28 +11003458 if (fdev[i]->towrite &&
3459 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3460 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3461 /* If we have a partial write to a failed
3462 * device, then we will need to reconstruct
3463 * the content of that device, so all other
3464 * devices must be read.
3465 */
3466 return 1;
3467 }
3468
3469 /* If we are forced to do a reconstruct-write, either because
3470 * the current RAID6 implementation only supports that, or
3471 * or because parity cannot be trusted and we are currently
3472 * recovering it, there is extra need to be careful.
3473 * If one of the devices that we would need to read, because
3474 * it is not being overwritten (and maybe not written at all)
3475 * is missing/faulty, then we need to read everything we can.
3476 */
3477 if (sh->raid_conf->level != 6 &&
3478 sh->sector < sh->raid_conf->mddev->recovery_cp)
3479 /* reconstruct-write isn't being forced */
3480 return 0;
NeilBrown36707bb2015-09-24 15:25:36 +10003481 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrown10d82c52015-05-08 18:19:33 +10003482 if (s->failed_num[i] != sh->pd_idx &&
3483 s->failed_num[i] != sh->qd_idx &&
3484 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
NeilBrownea664c82015-02-02 14:03:28 +11003485 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3486 return 1;
3487 }
3488
NeilBrown2c58f062015-02-02 11:32:23 +11003489 return 0;
3490}
3491
Song Liuba026842017-01-12 17:22:42 -08003492/* fetch_block - checks the given member device to see if its data needs
3493 * to be read or computed to satisfy a request.
3494 *
3495 * Returns 1 when no more member devices need to be checked, otherwise returns
3496 * 0 to tell the loop in handle_stripe_fill to continue
3497 */
NeilBrown2c58f062015-02-02 11:32:23 +11003498static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3499 int disk_idx, int disks)
3500{
3501 struct r5dev *dev = &sh->dev[disk_idx];
3502
3503 /* is the data in this block needed, and can we get it? */
3504 if (need_this_block(sh, s, disk_idx, disks)) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003505 /* we would like to get this block, possibly by computing it,
3506 * otherwise read it if the backing disk is insync
3507 */
3508 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3509 BUG_ON(test_bit(R5_Wantread, &dev->flags));
NeilBrownb0c783b2015-05-08 18:19:32 +10003510 BUG_ON(sh->batch_head);
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003511 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10003512 (s->failed && (disk_idx == s->failed_num[0] ||
3513 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003514 /* have disk failed, and we're requested to fetch it;
3515 * do compute it
3516 */
3517 pr_debug("Computing stripe %llu block %d\n",
3518 (unsigned long long)sh->sector, disk_idx);
3519 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3520 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3521 set_bit(R5_Wantcompute, &dev->flags);
3522 sh->ops.target = disk_idx;
3523 sh->ops.target2 = -1; /* no 2nd target */
3524 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10003525 /* Careful: from this point on 'uptodate' is in the eye
3526 * of raid_run_ops which services 'compute' operations
3527 * before writes. R5_Wantcompute flags a block that will
3528 * be R5_UPTODATE by the time it is needed for a
3529 * subsequent operation.
3530 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003531 s->uptodate++;
3532 return 1;
3533 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3534 /* Computing 2-failure is *very* expensive; only
3535 * do it if failed >= 2
3536 */
3537 int other;
3538 for (other = disks; other--; ) {
3539 if (other == disk_idx)
3540 continue;
3541 if (!test_bit(R5_UPTODATE,
3542 &sh->dev[other].flags))
3543 break;
3544 }
3545 BUG_ON(other < 0);
3546 pr_debug("Computing stripe %llu blocks %d,%d\n",
3547 (unsigned long long)sh->sector,
3548 disk_idx, other);
3549 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3550 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3551 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3552 set_bit(R5_Wantcompute, &sh->dev[other].flags);
3553 sh->ops.target = disk_idx;
3554 sh->ops.target2 = other;
3555 s->uptodate += 2;
3556 s->req_compute = 1;
3557 return 1;
3558 } else if (test_bit(R5_Insync, &dev->flags)) {
3559 set_bit(R5_LOCKED, &dev->flags);
3560 set_bit(R5_Wantread, &dev->flags);
3561 s->locked++;
3562 pr_debug("Reading block %d (sync=%d)\n",
3563 disk_idx, s->syncing);
3564 }
3565 }
3566
3567 return 0;
3568}
3569
3570/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10003571 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003572 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10003573static void handle_stripe_fill(struct stripe_head *sh,
3574 struct stripe_head_state *s,
3575 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003576{
3577 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003578
3579 /* look for blocks to read/compute, skip this if a compute
3580 * is already in flight, or if the stripe contents are in the
3581 * midst of changing due to a write
3582 */
3583 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Song Liu07e83362017-01-23 17:12:58 -08003584 !sh->reconstruct_state) {
3585
3586 /*
3587 * For degraded stripe with data in journal, do not handle
3588 * read requests yet, instead, flush the stripe to raid
3589 * disks first, this avoids handling complex rmw of write
3590 * back cache (prexor with orig_page, and then xor with
3591 * page) in the read path
3592 */
3593 if (s->injournal && s->failed) {
3594 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
3595 r5c_make_stripe_write_out(sh);
3596 goto out;
3597 }
3598
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003599 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10003600 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003601 break;
Song Liu07e83362017-01-23 17:12:58 -08003602 }
3603out:
Dan Williamsa4456852007-07-09 11:56:43 -07003604 set_bit(STRIPE_HANDLE, &sh->state);
3605}
3606
NeilBrown787b76f2015-05-21 12:56:41 +10003607static void break_stripe_batch_list(struct stripe_head *head_sh,
3608 unsigned long handle_flags);
Dan Williams1fe797e2008-06-28 09:16:30 +10003609/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07003610 * any written block on an uptodate or failed drive can be returned.
3611 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3612 * never LOCKED, so we don't need to test 'failed' directly.
3613 */
NeilBrownd1688a62011-10-11 16:49:52 +11003614static void handle_stripe_clean_event(struct r5conf *conf,
NeilBrown34a6f802015-08-14 12:07:57 +10003615 struct stripe_head *sh, int disks, struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003616{
3617 int i;
3618 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003619 int discard_pending = 0;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003620 struct stripe_head *head_sh = sh;
3621 bool do_endio = false;
Dan Williamsa4456852007-07-09 11:56:43 -07003622
3623 for (i = disks; i--; )
3624 if (sh->dev[i].written) {
3625 dev = &sh->dev[i];
3626 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003627 (test_bit(R5_UPTODATE, &dev->flags) ||
Shaohua Lid592a992014-05-21 17:57:44 +08003628 test_bit(R5_Discard, &dev->flags) ||
3629 test_bit(R5_SkipCopy, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003630 /* We can return any write requests */
3631 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003632 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003633 if (test_and_clear_bit(R5_Discard, &dev->flags))
3634 clear_bit(R5_UPTODATE, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08003635 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3636 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
Shaohua Lid592a992014-05-21 17:57:44 +08003637 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003638 do_endio = true;
3639
3640returnbi:
3641 dev->page = dev->orig_page;
Dan Williamsa4456852007-07-09 11:56:43 -07003642 wbi = dev->written;
3643 dev->written = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003644 while (wbi && wbi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003645 dev->sector + STRIPE_SECTORS) {
3646 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10003647 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003648 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003649 bio_list_add(return_bi, wbi);
Dan Williamsa4456852007-07-09 11:56:43 -07003650 }
3651 wbi = wbi2;
3652 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003653 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3654 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07003655 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003656 0);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003657 if (head_sh->batch_head) {
3658 sh = list_first_entry(&sh->batch_list,
3659 struct stripe_head,
3660 batch_list);
3661 if (sh != head_sh) {
3662 dev = &sh->dev[i];
3663 goto returnbi;
3664 }
3665 }
3666 sh = head_sh;
3667 dev = &sh->dev[i];
NeilBrownf8dfcff2013-03-12 12:18:06 +11003668 } else if (test_bit(R5_Discard, &dev->flags))
3669 discard_pending = 1;
3670 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -07003671
Shaohua Li0576b1c2015-08-13 14:32:00 -07003672 r5l_stripe_write_finished(sh);
3673
NeilBrownf8dfcff2013-03-12 12:18:06 +11003674 if (!discard_pending &&
3675 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003676 int hash;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003677 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3678 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3679 if (sh->qd_idx >= 0) {
3680 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3681 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3682 }
3683 /* now that discard is done we can proceed with any sync */
3684 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08003685 /*
3686 * SCSI discard will change some bio fields and the stripe has
3687 * no updated data, so remove it from hash list and the stripe
3688 * will be reinitialized
3689 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11003690unhash:
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003691 hash = sh->hash_lock_index;
3692 spin_lock_irq(conf->hash_locks + hash);
Shaohua Lid47648f2013-10-19 14:51:42 +08003693 remove_hash(sh);
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003694 spin_unlock_irq(conf->hash_locks + hash);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003695 if (head_sh->batch_head) {
3696 sh = list_first_entry(&sh->batch_list,
3697 struct stripe_head, batch_list);
3698 if (sh != head_sh)
3699 goto unhash;
3700 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003701 sh = head_sh;
3702
NeilBrownf8dfcff2013-03-12 12:18:06 +11003703 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3704 set_bit(STRIPE_HANDLE, &sh->state);
3705
3706 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003707
3708 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3709 if (atomic_dec_and_test(&conf->pending_full_writes))
3710 md_wakeup_thread(conf->mddev->thread);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003711
NeilBrown787b76f2015-05-21 12:56:41 +10003712 if (head_sh->batch_head && do_endio)
3713 break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
Dan Williamsa4456852007-07-09 11:56:43 -07003714}
3715
Song Liu86aa1392017-01-12 17:22:41 -08003716/*
3717 * For RMW in write back cache, we need extra page in prexor to store the
3718 * old data. This page is stored in dev->orig_page.
3719 *
3720 * This function checks whether we have data for prexor. The exact logic
3721 * is:
3722 * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
3723 */
3724static inline bool uptodate_for_rmw(struct r5dev *dev)
3725{
3726 return (test_bit(R5_UPTODATE, &dev->flags)) &&
3727 (!test_bit(R5_InJournal, &dev->flags) ||
3728 test_bit(R5_OrigPageUPTDODATE, &dev->flags));
3729}
3730
Song Liud7bd3982016-11-23 22:50:39 -08003731static int handle_stripe_dirtying(struct r5conf *conf,
3732 struct stripe_head *sh,
3733 struct stripe_head_state *s,
3734 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003735{
3736 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003737 sector_t recovery_cp = conf->mddev->recovery_cp;
3738
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003739 /* Check whether resync is now happening or should start.
Alexander Lyakasa7854482012-10-11 13:50:12 +11003740 * If yes, then the array is dirty (after unclean shutdown or
3741 * initial creation), so parity in some stripes might be inconsistent.
3742 * In this case, we need to always do reconstruct-write, to ensure
3743 * that in case of drive failure or read-error correction, we
3744 * generate correct data from the parity.
3745 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003746 if (conf->rmw_level == PARITY_DISABLE_RMW ||
NeilBrown26ac1072015-02-18 11:35:14 +11003747 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
3748 s->failed == 0)) {
Alexander Lyakasa7854482012-10-11 13:50:12 +11003749 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10003750 * look like rcw is cheaper
3751 */
3752 rcw = 1; rmw = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003753 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
3754 conf->rmw_level, (unsigned long long)recovery_cp,
Alexander Lyakasa7854482012-10-11 13:50:12 +11003755 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10003756 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003757 /* would I have to read this buffer for read_modify_write */
3758 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08003759 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu07e83362017-01-23 17:12:58 -08003760 i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08003761 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003762 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08003763 !(uptodate_for_rmw(dev) ||
Dan Williamsf38e1212007-01-02 13:52:30 -07003764 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003765 if (test_bit(R5_Insync, &dev->flags))
3766 rmw++;
3767 else
3768 rmw += 2*disks; /* cannot read it */
3769 }
3770 /* Would I have to read this buffer for reconstruct_write */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003771 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3772 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003773 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003774 !(test_bit(R5_UPTODATE, &dev->flags) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003775 test_bit(R5_Wantcompute, &dev->flags))) {
NeilBrown67f45542014-05-28 13:39:22 +10003776 if (test_bit(R5_Insync, &dev->flags))
3777 rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07003778 else
3779 rcw += 2*disks;
3780 }
3781 }
Song Liu1e6d6902016-11-17 15:24:39 -08003782
Song Liu39b99582017-01-24 14:08:23 -08003783 pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n",
3784 (unsigned long long)sh->sector, sh->state, rmw, rcw);
Dan Williamsa4456852007-07-09 11:56:43 -07003785 set_bit(STRIPE_HANDLE, &sh->state);
Song Liu41257582016-05-23 17:25:06 -07003786 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003787 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003788 if (conf->mddev->queue)
3789 blk_add_trace_msg(conf->mddev->queue,
3790 "raid5 rmw %llu %d",
3791 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07003792 for (i = disks; i--; ) {
3793 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08003794 if (test_bit(R5_InJournal, &dev->flags) &&
3795 dev->page == dev->orig_page &&
3796 !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) {
3797 /* alloc page for prexor */
Song Liud7bd3982016-11-23 22:50:39 -08003798 struct page *p = alloc_page(GFP_NOIO);
Song Liu1e6d6902016-11-17 15:24:39 -08003799
Song Liud7bd3982016-11-23 22:50:39 -08003800 if (p) {
3801 dev->orig_page = p;
3802 continue;
3803 }
3804
3805 /*
3806 * alloc_page() failed, try use
3807 * disk_info->extra_page
3808 */
3809 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE,
3810 &conf->cache_state)) {
3811 r5c_use_extra_page(sh);
3812 break;
3813 }
3814
3815 /* extra_page in use, add to delayed_list */
3816 set_bit(STRIPE_DELAYED, &sh->state);
3817 s->waiting_extra_page = 1;
3818 return -EAGAIN;
Song Liu1e6d6902016-11-17 15:24:39 -08003819 }
Song Liud7bd3982016-11-23 22:50:39 -08003820 }
Song Liu1e6d6902016-11-17 15:24:39 -08003821
Song Liud7bd3982016-11-23 22:50:39 -08003822 for (i = disks; i--; ) {
3823 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08003824 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003825 i == sh->pd_idx || i == sh->qd_idx ||
3826 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003827 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08003828 !(uptodate_for_rmw(dev) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003829 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003830 test_bit(R5_Insync, &dev->flags)) {
NeilBrown67f45542014-05-28 13:39:22 +10003831 if (test_bit(STRIPE_PREREAD_ACTIVE,
3832 &sh->state)) {
3833 pr_debug("Read_old block %d for r-m-w\n",
3834 i);
Dan Williamsa4456852007-07-09 11:56:43 -07003835 set_bit(R5_LOCKED, &dev->flags);
3836 set_bit(R5_Wantread, &dev->flags);
3837 s->locked++;
3838 } else {
3839 set_bit(STRIPE_DELAYED, &sh->state);
3840 set_bit(STRIPE_HANDLE, &sh->state);
3841 }
3842 }
3843 }
NeilBrowna9add5d2012-10-31 11:59:09 +11003844 }
Song Liu41257582016-05-23 17:25:06 -07003845 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003846 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11003847 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10003848 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003849 for (i = disks; i--; ) {
3850 struct r5dev *dev = &sh->dev[i];
3851 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10003852 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003853 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003854 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10003855 test_bit(R5_Wantcompute, &dev->flags))) {
3856 rcw++;
NeilBrown67f45542014-05-28 13:39:22 +10003857 if (test_bit(R5_Insync, &dev->flags) &&
3858 test_bit(STRIPE_PREREAD_ACTIVE,
3859 &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07003860 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07003861 "%d for Reconstruct\n", i);
3862 set_bit(R5_LOCKED, &dev->flags);
3863 set_bit(R5_Wantread, &dev->flags);
3864 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11003865 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07003866 } else {
3867 set_bit(STRIPE_DELAYED, &sh->state);
3868 set_bit(STRIPE_HANDLE, &sh->state);
3869 }
3870 }
3871 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003872 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11003873 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3874 (unsigned long long)sh->sector,
3875 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10003876 }
NeilBrownb1b02fe2015-02-02 10:44:29 +11003877
3878 if (rcw > disks && rmw > disks &&
3879 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3880 set_bit(STRIPE_DELAYED, &sh->state);
3881
Dan Williamsa4456852007-07-09 11:56:43 -07003882 /* now if nothing is locked, and if we have enough data,
3883 * we can start a write request
3884 */
Dan Williamsf38e1212007-01-02 13:52:30 -07003885 /* since handle_stripe can be called at any time we need to handle the
3886 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07003887 * subsequent call wants to start a write request. raid_run_ops only
3888 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07003889 * simultaneously. If this is not the case then new writes need to be
3890 * held off until the compute completes.
3891 */
Dan Williams976ea8d2008-06-28 08:32:03 +10003892 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3893 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
Song Liu1e6d6902016-11-17 15:24:39 -08003894 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003895 schedule_reconstruction(sh, s, rcw == 0, 0);
Song Liud7bd3982016-11-23 22:50:39 -08003896 return 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003897}
3898
NeilBrownd1688a62011-10-11 16:49:52 +11003899static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003900 struct stripe_head_state *s, int disks)
3901{
Dan Williamsecc65c92008-06-28 08:31:57 +10003902 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07003903
shli@kernel.org59fc6302014-12-15 12:57:03 +11003904 BUG_ON(sh->batch_head);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003905 set_bit(STRIPE_HANDLE, &sh->state);
3906
Dan Williamsecc65c92008-06-28 08:31:57 +10003907 switch (sh->check_state) {
3908 case check_state_idle:
3909 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07003910 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07003911 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10003912 sh->check_state = check_state_run;
3913 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003914 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003915 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10003916 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07003917 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003918 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10003919 /* fall through */
3920 case check_state_compute_result:
3921 sh->check_state = check_state_idle;
3922 if (!dev)
3923 dev = &sh->dev[sh->pd_idx];
3924
3925 /* check that a write has not made the stripe insync */
3926 if (test_bit(STRIPE_INSYNC, &sh->state))
3927 break;
3928
3929 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07003930 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3931 BUG_ON(s->uptodate != disks);
3932
3933 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10003934 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003935 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003936
Dan Williamsa4456852007-07-09 11:56:43 -07003937 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003938 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003939 break;
3940 case check_state_run:
3941 break; /* we will be called again upon completion */
3942 case check_state_check_result:
3943 sh->check_state = check_state_idle;
3944
3945 /* if a failure occurred during the check operation, leave
3946 * STRIPE_INSYNC not set and let the stripe be handled again
3947 */
3948 if (s->failed)
3949 break;
3950
3951 /* handle a successful check operation, if parity is correct
3952 * we are done. Otherwise update the mismatch count and repair
3953 * parity if !MD_RECOVERY_CHECK
3954 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003955 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003956 /* parity is correct (on disc,
3957 * not in buffer any more)
3958 */
3959 set_bit(STRIPE_INSYNC, &sh->state);
3960 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003961 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003962 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3963 /* don't try to repair!! */
3964 set_bit(STRIPE_INSYNC, &sh->state);
3965 else {
3966 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003967 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003968 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3969 set_bit(R5_Wantcompute,
3970 &sh->dev[sh->pd_idx].flags);
3971 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003972 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003973 s->uptodate++;
3974 }
3975 }
3976 break;
3977 case check_state_compute_run:
3978 break;
3979 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11003980 pr_err("%s: unknown check_state: %d sector: %llu\n",
Dan Williamsecc65c92008-06-28 08:31:57 +10003981 __func__, sh->check_state,
3982 (unsigned long long) sh->sector);
3983 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003984 }
3985}
3986
NeilBrownd1688a62011-10-11 16:49:52 +11003987static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003988 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003989 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003990{
Dan Williamsa4456852007-07-09 11:56:43 -07003991 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003992 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003993 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003994
shli@kernel.org59fc6302014-12-15 12:57:03 +11003995 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003996 set_bit(STRIPE_HANDLE, &sh->state);
3997
3998 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003999
Dan Williamsa4456852007-07-09 11:56:43 -07004000 /* Want to check and possibly repair P and Q.
4001 * However there could be one 'failed' device, in which
4002 * case we can only check one of them, possibly using the
4003 * other to generate missing data
4004 */
4005
Dan Williamsd82dfee2009-07-14 13:40:57 -07004006 switch (sh->check_state) {
4007 case check_state_idle:
4008 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10004009 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004010 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07004011 * makes sense to check P (If anything else were failed,
4012 * we would have used P to recreate it).
4013 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004014 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07004015 }
NeilBrownf2b3b442011-07-26 11:35:19 +10004016 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004017 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07004018 * anything, so it makes sense to check it
4019 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004020 if (sh->check_state == check_state_run)
4021 sh->check_state = check_state_run_pq;
4022 else
4023 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07004024 }
Dan Williams36d1c642009-07-14 11:48:22 -07004025
Dan Williamsd82dfee2009-07-14 13:40:57 -07004026 /* discard potentially stale zero_sum_result */
4027 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07004028
Dan Williamsd82dfee2009-07-14 13:40:57 -07004029 if (sh->check_state == check_state_run) {
4030 /* async_xor_zero_sum destroys the contents of P */
4031 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
4032 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07004033 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004034 if (sh->check_state >= check_state_run &&
4035 sh->check_state <= check_state_run_pq) {
4036 /* async_syndrome_zero_sum preserves P and Q, so
4037 * no need to mark them !uptodate here
4038 */
4039 set_bit(STRIPE_OP_CHECK, &s->ops_request);
4040 break;
4041 }
Dan Williams36d1c642009-07-14 11:48:22 -07004042
Dan Williamsd82dfee2009-07-14 13:40:57 -07004043 /* we have 2-disk failure */
4044 BUG_ON(s->failed != 2);
4045 /* fall through */
4046 case check_state_compute_result:
4047 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07004048
Dan Williamsd82dfee2009-07-14 13:40:57 -07004049 /* check that a write has not made the stripe insync */
4050 if (test_bit(STRIPE_INSYNC, &sh->state))
4051 break;
Dan Williamsa4456852007-07-09 11:56:43 -07004052
4053 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07004054 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07004055 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004056 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07004057 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004058 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07004059 s->locked++;
4060 set_bit(R5_LOCKED, &dev->flags);
4061 set_bit(R5_Wantwrite, &dev->flags);
4062 }
4063 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004064 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07004065 s->locked++;
4066 set_bit(R5_LOCKED, &dev->flags);
4067 set_bit(R5_Wantwrite, &dev->flags);
4068 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004069 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004070 dev = &sh->dev[pd_idx];
4071 s->locked++;
4072 set_bit(R5_LOCKED, &dev->flags);
4073 set_bit(R5_Wantwrite, &dev->flags);
4074 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004075 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004076 dev = &sh->dev[qd_idx];
4077 s->locked++;
4078 set_bit(R5_LOCKED, &dev->flags);
4079 set_bit(R5_Wantwrite, &dev->flags);
4080 }
4081 clear_bit(STRIPE_DEGRADED, &sh->state);
4082
4083 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004084 break;
4085 case check_state_run:
4086 case check_state_run_q:
4087 case check_state_run_pq:
4088 break; /* we will be called again upon completion */
4089 case check_state_check_result:
4090 sh->check_state = check_state_idle;
4091
4092 /* handle a successful check operation, if parity is correct
4093 * we are done. Otherwise update the mismatch count and repair
4094 * parity if !MD_RECOVERY_CHECK
4095 */
4096 if (sh->ops.zero_sum_result == 0) {
4097 /* both parities are correct */
4098 if (!s->failed)
4099 set_bit(STRIPE_INSYNC, &sh->state);
4100 else {
4101 /* in contrast to the raid5 case we can validate
4102 * parity, but still have a failure to write
4103 * back
4104 */
4105 sh->check_state = check_state_compute_result;
4106 /* Returning at this point means that we may go
4107 * off and bring p and/or q uptodate again so
4108 * we make sure to check zero_sum_result again
4109 * to verify if p or q need writeback
4110 */
4111 }
4112 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004113 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004114 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
4115 /* don't try to repair!! */
4116 set_bit(STRIPE_INSYNC, &sh->state);
4117 else {
4118 int *target = &sh->ops.target;
4119
4120 sh->ops.target = -1;
4121 sh->ops.target2 = -1;
4122 sh->check_state = check_state_compute_run;
4123 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4124 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4125 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4126 set_bit(R5_Wantcompute,
4127 &sh->dev[pd_idx].flags);
4128 *target = pd_idx;
4129 target = &sh->ops.target2;
4130 s->uptodate++;
4131 }
4132 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4133 set_bit(R5_Wantcompute,
4134 &sh->dev[qd_idx].flags);
4135 *target = qd_idx;
4136 s->uptodate++;
4137 }
4138 }
4139 }
4140 break;
4141 case check_state_compute_run:
4142 break;
4143 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11004144 pr_warn("%s: unknown check_state: %d sector: %llu\n",
4145 __func__, sh->check_state,
4146 (unsigned long long) sh->sector);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004147 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07004148 }
4149}
4150
NeilBrownd1688a62011-10-11 16:49:52 +11004151static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07004152{
4153 int i;
4154
4155 /* We have read all the blocks in this stripe and now we need to
4156 * copy some of them into a target stripe for expand.
4157 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07004158 struct dma_async_tx_descriptor *tx = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +11004159 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07004160 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4161 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11004162 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11004163 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07004164 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07004165 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07004166
Shaohua Li6d036f72015-08-13 14:31:57 -07004167 sector_t bn = raid5_compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11004168 sector_t s = raid5_compute_sector(conf, bn, 0,
4169 &dd_idx, NULL);
Shaohua Li6d036f72015-08-13 14:31:57 -07004170 sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07004171 if (sh2 == NULL)
4172 /* so far only the early blocks of this stripe
4173 * have been requested. When later blocks
4174 * get requested, we will try again
4175 */
4176 continue;
4177 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
4178 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
4179 /* must have already done this block */
Shaohua Li6d036f72015-08-13 14:31:57 -07004180 raid5_release_stripe(sh2);
Dan Williamsa4456852007-07-09 11:56:43 -07004181 continue;
4182 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07004183
4184 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07004185 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004186 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07004187 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07004188 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004189
Dan Williamsa4456852007-07-09 11:56:43 -07004190 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
4191 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
4192 for (j = 0; j < conf->raid_disks; j++)
4193 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10004194 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07004195 !test_bit(R5_Expanded, &sh2->dev[j].flags))
4196 break;
4197 if (j == conf->raid_disks) {
4198 set_bit(STRIPE_EXPAND_READY, &sh2->state);
4199 set_bit(STRIPE_HANDLE, &sh2->state);
4200 }
Shaohua Li6d036f72015-08-13 14:31:57 -07004201 raid5_release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004202
Dan Williamsa4456852007-07-09 11:56:43 -07004203 }
NeilBrowna2e08552007-09-11 15:23:36 -07004204 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11004205 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07004206}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
4208/*
4209 * handle_stripe - do things to a stripe.
4210 *
NeilBrown9a3e1102011-12-23 10:17:53 +11004211 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4212 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11004214 * return some read requests which now have data
4215 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 * schedule a read on some buffers
4217 * schedule a write of some buffers
4218 * return confirmation of parity correctness
4219 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 */
Dan Williamsa4456852007-07-09 11:56:43 -07004221
NeilBrownacfe7262011-07-27 11:00:36 +10004222static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07004223{
NeilBrownd1688a62011-10-11 16:49:52 +11004224 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08004225 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004226 struct r5dev *dev;
4227 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11004228 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07004229
NeilBrownacfe7262011-07-27 11:00:36 +10004230 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07004231
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004232 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
4233 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
NeilBrownacfe7262011-07-27 11:00:36 +10004234 s->failed_num[0] = -1;
4235 s->failed_num[1] = -1;
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004236 s->log_failed = r5l_log_disk_error(conf);
NeilBrownacfe7262011-07-27 11:00:36 +10004237
4238 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07004239 rcu_read_lock();
4240 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004241 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10004242 sector_t first_bad;
4243 int bad_sectors;
4244 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10004245
NeilBrown16a53ec2006-06-26 00:27:38 -07004246 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07004247
Dan Williams45b42332007-07-09 11:56:43 -07004248 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11004249 i, dev->flags,
4250 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004251 /* maybe we can reply to a read
4252 *
4253 * new wantfill requests are only permitted while
4254 * ops_complete_biofill is guaranteed to be inactive
4255 */
4256 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4257 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4258 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07004259
4260 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10004261 if (test_bit(R5_LOCKED, &dev->flags))
4262 s->locked++;
4263 if (test_bit(R5_UPTODATE, &dev->flags))
4264 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004265 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004266 s->compute++;
4267 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004268 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004269
NeilBrownacfe7262011-07-27 11:00:36 +10004270 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004271 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10004272 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10004273 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004274 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10004275 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004276 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004277 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004278 }
Dan Williamsa4456852007-07-09 11:56:43 -07004279 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10004280 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11004281 /* Prefer to use the replacement for reads, but only
4282 * if it is recovered enough and has no bad blocks.
4283 */
4284 rdev = rcu_dereference(conf->disks[i].replacement);
4285 if (rdev && !test_bit(Faulty, &rdev->flags) &&
4286 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
4287 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4288 &first_bad, &bad_sectors))
4289 set_bit(R5_ReadRepl, &dev->flags);
4290 else {
NeilBrowne6030cb2015-07-17 13:26:23 +10004291 if (rdev && !test_bit(Faulty, &rdev->flags))
NeilBrown9a3e1102011-12-23 10:17:53 +11004292 set_bit(R5_NeedReplace, &dev->flags);
NeilBrowne6030cb2015-07-17 13:26:23 +10004293 else
4294 clear_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11004295 rdev = rcu_dereference(conf->disks[i].rdev);
4296 clear_bit(R5_ReadRepl, &dev->flags);
4297 }
NeilBrown9283d8c2011-12-08 16:27:57 +11004298 if (rdev && test_bit(Faulty, &rdev->flags))
4299 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10004300 if (rdev) {
4301 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4302 &first_bad, &bad_sectors);
4303 if (s->blocked_rdev == NULL
4304 && (test_bit(Blocked, &rdev->flags)
4305 || is_bad < 0)) {
4306 if (is_bad < 0)
4307 set_bit(BlockedBadBlocks,
4308 &rdev->flags);
4309 s->blocked_rdev = rdev;
4310 atomic_inc(&rdev->nr_pending);
4311 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004312 }
NeilBrown415e72d2010-06-17 17:25:21 +10004313 clear_bit(R5_Insync, &dev->flags);
4314 if (!rdev)
4315 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10004316 else if (is_bad) {
4317 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10004318 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4319 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10004320 /* treat as in-sync, but with a read error
4321 * which we can now try to correct
4322 */
4323 set_bit(R5_Insync, &dev->flags);
4324 set_bit(R5_ReadError, &dev->flags);
4325 }
4326 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10004327 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11004328 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10004329 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11004330 set_bit(R5_Insync, &dev->flags);
4331 else if (test_bit(R5_UPTODATE, &dev->flags) &&
4332 test_bit(R5_Expanded, &dev->flags))
4333 /* If we've reshaped into here, we assume it is Insync.
4334 * We will shortly update recovery_offset to make
4335 * it official.
4336 */
4337 set_bit(R5_Insync, &dev->flags);
4338
NeilBrown1cc03eb2014-01-06 13:19:42 +11004339 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004340 /* This flag does not apply to '.replacement'
4341 * only to .rdev, so make sure to check that*/
4342 struct md_rdev *rdev2 = rcu_dereference(
4343 conf->disks[i].rdev);
4344 if (rdev2 == rdev)
4345 clear_bit(R5_Insync, &dev->flags);
4346 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004347 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004348 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10004349 } else
4350 clear_bit(R5_WriteError, &dev->flags);
4351 }
NeilBrown1cc03eb2014-01-06 13:19:42 +11004352 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004353 /* This flag does not apply to '.replacement'
4354 * only to .rdev, so make sure to check that*/
4355 struct md_rdev *rdev2 = rcu_dereference(
4356 conf->disks[i].rdev);
4357 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10004358 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004359 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10004360 } else
4361 clear_bit(R5_MadeGood, &dev->flags);
4362 }
NeilBrown977df362011-12-23 10:17:53 +11004363 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4364 struct md_rdev *rdev2 = rcu_dereference(
4365 conf->disks[i].replacement);
4366 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4367 s->handle_bad_blocks = 1;
4368 atomic_inc(&rdev2->nr_pending);
4369 } else
4370 clear_bit(R5_MadeGoodRepl, &dev->flags);
4371 }
NeilBrown415e72d2010-06-17 17:25:21 +10004372 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004373 /* The ReadError flag will just be confusing now */
4374 clear_bit(R5_ReadError, &dev->flags);
4375 clear_bit(R5_ReWrite, &dev->flags);
4376 }
NeilBrown415e72d2010-06-17 17:25:21 +10004377 if (test_bit(R5_ReadError, &dev->flags))
4378 clear_bit(R5_Insync, &dev->flags);
4379 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004380 if (s->failed < 2)
4381 s->failed_num[s->failed] = i;
4382 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11004383 if (rdev && !test_bit(Faulty, &rdev->flags))
4384 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10004385 }
Song Liu2ded3702016-11-17 15:24:38 -08004386
4387 if (test_bit(R5_InJournal, &dev->flags))
4388 s->injournal++;
Song Liu1e6d6902016-11-17 15:24:39 -08004389 if (test_bit(R5_InJournal, &dev->flags) && dev->written)
4390 s->just_cached++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004391 }
NeilBrown9a3e1102011-12-23 10:17:53 +11004392 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4393 /* If there is a failed device being replaced,
4394 * we must be recovering.
4395 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10004396 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11004397 * else we can only be replacing
4398 * sync and recovery both need to read all devices, and so
4399 * use the same flag.
4400 */
4401 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10004402 sh->sector >= conf->mddev->recovery_cp ||
4403 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11004404 s->syncing = 1;
4405 else
4406 s->replacing = 1;
4407 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004408 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10004409}
NeilBrownf4168852007-02-28 20:11:53 -08004410
shli@kernel.org59fc6302014-12-15 12:57:03 +11004411static int clear_batch_ready(struct stripe_head *sh)
4412{
NeilBrownb15a9db2015-05-22 15:20:04 +10004413 /* Return '1' if this is a member of batch, or
4414 * '0' if it is a lone stripe or a head which can now be
4415 * handled.
4416 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11004417 struct stripe_head *tmp;
4418 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
NeilBrownb15a9db2015-05-22 15:20:04 +10004419 return (sh->batch_head && sh->batch_head != sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11004420 spin_lock(&sh->stripe_lock);
4421 if (!sh->batch_head) {
4422 spin_unlock(&sh->stripe_lock);
4423 return 0;
4424 }
4425
4426 /*
4427 * this stripe could be added to a batch list before we check
4428 * BATCH_READY, skips it
4429 */
4430 if (sh->batch_head != sh) {
4431 spin_unlock(&sh->stripe_lock);
4432 return 1;
4433 }
4434 spin_lock(&sh->batch_lock);
4435 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4436 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4437 spin_unlock(&sh->batch_lock);
4438 spin_unlock(&sh->stripe_lock);
4439
4440 /*
4441 * BATCH_READY is cleared, no new stripes can be added.
4442 * batch_list can be accessed without lock
4443 */
4444 return 0;
4445}
4446
NeilBrown3960ce72015-05-21 12:20:36 +10004447static void break_stripe_batch_list(struct stripe_head *head_sh,
4448 unsigned long handle_flags)
shli@kernel.org72ac7332014-12-15 12:57:03 +11004449{
NeilBrown4e3d62f2015-05-21 11:50:16 +10004450 struct stripe_head *sh, *next;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004451 int i;
NeilBrownfb642b92015-05-21 12:00:47 +10004452 int do_wakeup = 0;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004453
NeilBrownbb270512015-05-08 18:19:40 +10004454 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4455
shli@kernel.org72ac7332014-12-15 12:57:03 +11004456 list_del_init(&sh->batch_list);
4457
Shaohua Lifb3229d2016-03-09 10:08:38 -08004458 WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
NeilBrown1b956f72015-05-21 12:40:26 +10004459 (1 << STRIPE_SYNCING) |
4460 (1 << STRIPE_REPLACED) |
NeilBrown1b956f72015-05-21 12:40:26 +10004461 (1 << STRIPE_DELAYED) |
4462 (1 << STRIPE_BIT_DELAY) |
4463 (1 << STRIPE_FULL_WRITE) |
4464 (1 << STRIPE_BIOFILL_RUN) |
4465 (1 << STRIPE_COMPUTE_RUN) |
4466 (1 << STRIPE_OPS_REQ_PENDING) |
4467 (1 << STRIPE_DISCARD) |
4468 (1 << STRIPE_BATCH_READY) |
4469 (1 << STRIPE_BATCH_ERR) |
Shaohua Lifb3229d2016-03-09 10:08:38 -08004470 (1 << STRIPE_BITMAP_PENDING)),
4471 "stripe state: %lx\n", sh->state);
4472 WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4473 (1 << STRIPE_REPLACED)),
4474 "head stripe state: %lx\n", head_sh->state);
NeilBrown1b956f72015-05-21 12:40:26 +10004475
4476 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
NeilBrown550da242016-03-09 12:58:25 +11004477 (1 << STRIPE_PREREAD_ACTIVE) |
NeilBrown1b956f72015-05-21 12:40:26 +10004478 (1 << STRIPE_DEGRADED)),
4479 head_sh->state & (1 << STRIPE_INSYNC));
4480
shli@kernel.org72ac7332014-12-15 12:57:03 +11004481 sh->check_state = head_sh->check_state;
4482 sh->reconstruct_state = head_sh->reconstruct_state;
NeilBrownfb642b92015-05-21 12:00:47 +10004483 for (i = 0; i < sh->disks; i++) {
4484 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4485 do_wakeup = 1;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004486 sh->dev[i].flags = head_sh->dev[i].flags &
4487 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
NeilBrownfb642b92015-05-21 12:00:47 +10004488 }
shli@kernel.org72ac7332014-12-15 12:57:03 +11004489 spin_lock_irq(&sh->stripe_lock);
4490 sh->batch_head = NULL;
4491 spin_unlock_irq(&sh->stripe_lock);
NeilBrown3960ce72015-05-21 12:20:36 +10004492 if (handle_flags == 0 ||
4493 sh->state & handle_flags)
4494 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07004495 raid5_release_stripe(sh);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004496 }
NeilBrownfb642b92015-05-21 12:00:47 +10004497 spin_lock_irq(&head_sh->stripe_lock);
4498 head_sh->batch_head = NULL;
4499 spin_unlock_irq(&head_sh->stripe_lock);
4500 for (i = 0; i < head_sh->disks; i++)
4501 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4502 do_wakeup = 1;
NeilBrown3960ce72015-05-21 12:20:36 +10004503 if (head_sh->state & handle_flags)
4504 set_bit(STRIPE_HANDLE, &head_sh->state);
NeilBrownfb642b92015-05-21 12:00:47 +10004505
4506 if (do_wakeup)
4507 wake_up(&head_sh->raid_conf->wait_for_overlap);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004508}
4509
NeilBrowncc940152011-07-26 11:35:35 +10004510static void handle_stripe(struct stripe_head *sh)
4511{
4512 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11004513 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10004514 int i;
NeilBrown84789552011-07-27 11:00:36 +10004515 int prexor;
4516 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004517 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10004518
4519 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11004520 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10004521 /* already being handled, ensure it gets handled
4522 * again when current action finishes */
4523 set_bit(STRIPE_HANDLE, &sh->state);
4524 return;
4525 }
4526
shli@kernel.org59fc6302014-12-15 12:57:03 +11004527 if (clear_batch_ready(sh) ) {
4528 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
4529 return;
4530 }
4531
NeilBrown4e3d62f2015-05-21 11:50:16 +10004532 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
NeilBrown3960ce72015-05-21 12:20:36 +10004533 break_stripe_batch_list(sh, 0);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004534
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004535 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
NeilBrownf8dfcff2013-03-12 12:18:06 +11004536 spin_lock(&sh->stripe_lock);
4537 /* Cannot process 'sync' concurrently with 'discard' */
4538 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
4539 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4540 set_bit(STRIPE_SYNCING, &sh->state);
4541 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10004542 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004543 }
4544 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10004545 }
4546 clear_bit(STRIPE_DELAYED, &sh->state);
4547
4548 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4549 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4550 (unsigned long long)sh->sector, sh->state,
4551 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4552 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10004553
NeilBrownacfe7262011-07-27 11:00:36 +10004554 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10004555
Shaohua Lib70abcb2015-08-13 14:31:58 -07004556 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
4557 goto finish;
4558
NeilBrownbc2607f2011-07-28 11:39:22 +10004559 if (s.handle_bad_blocks) {
4560 set_bit(STRIPE_HANDLE, &sh->state);
4561 goto finish;
4562 }
4563
NeilBrown474af965fe2011-07-27 11:00:36 +10004564 if (unlikely(s.blocked_rdev)) {
4565 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11004566 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10004567 set_bit(STRIPE_HANDLE, &sh->state);
4568 goto finish;
4569 }
4570 /* There is nothing for the blocked_rdev to block */
4571 rdev_dec_pending(s.blocked_rdev, conf->mddev);
4572 s.blocked_rdev = NULL;
4573 }
4574
4575 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4576 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4577 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4578 }
4579
4580 pr_debug("locked=%d uptodate=%d to_read=%d"
4581 " to_write=%d failed=%d failed_num=%d,%d\n",
4582 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4583 s.failed_num[0], s.failed_num[1]);
4584 /* check if the array has lost more than max_degraded devices and,
4585 * if so, some requests might need to be failed.
4586 */
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004587 if (s.failed > conf->max_degraded || s.log_failed) {
NeilBrown9a3f5302011-11-08 16:22:01 +11004588 sh->check_state = 0;
4589 sh->reconstruct_state = 0;
NeilBrown626f2092015-05-22 14:03:10 +10004590 break_stripe_batch_list(sh, 0);
NeilBrown9a3f5302011-11-08 16:22:01 +11004591 if (s.to_read+s.to_write+s.written)
4592 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11004593 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11004594 handle_failed_sync(conf, sh, &s);
4595 }
NeilBrown474af965fe2011-07-27 11:00:36 +10004596
NeilBrown84789552011-07-27 11:00:36 +10004597 /* Now we check to see if any write operations have recently
4598 * completed
4599 */
4600 prexor = 0;
4601 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4602 prexor = 1;
4603 if (sh->reconstruct_state == reconstruct_state_drain_result ||
4604 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
4605 sh->reconstruct_state = reconstruct_state_idle;
4606
4607 /* All the 'written' buffers and the parity block are ready to
4608 * be written back to disk
4609 */
Shaohua Li9e4447682012-10-11 13:49:49 +11004610 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
4611 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004612 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11004613 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
4614 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004615 for (i = disks; i--; ) {
4616 struct r5dev *dev = &sh->dev[i];
4617 if (test_bit(R5_LOCKED, &dev->flags) &&
4618 (i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08004619 dev->written || test_bit(R5_InJournal,
4620 &dev->flags))) {
NeilBrown84789552011-07-27 11:00:36 +10004621 pr_debug("Writing block %d\n", i);
4622 set_bit(R5_Wantwrite, &dev->flags);
4623 if (prexor)
4624 continue;
NeilBrown9c4bdf62014-08-13 09:57:07 +10004625 if (s.failed > 1)
4626 continue;
NeilBrown84789552011-07-27 11:00:36 +10004627 if (!test_bit(R5_Insync, &dev->flags) ||
4628 ((i == sh->pd_idx || i == sh->qd_idx) &&
4629 s.failed == 0))
4630 set_bit(STRIPE_INSYNC, &sh->state);
4631 }
4632 }
4633 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4634 s.dec_preread_active = 1;
4635 }
4636
NeilBrownef5b7c62012-11-22 09:13:36 +11004637 /*
4638 * might be able to return some write requests if the parity blocks
4639 * are safe, or on a failed drive
4640 */
4641 pdev = &sh->dev[sh->pd_idx];
4642 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
4643 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
4644 qdev = &sh->dev[sh->qd_idx];
4645 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
4646 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
4647 || conf->level < 6;
4648
4649 if (s.written &&
4650 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
4651 && !test_bit(R5_LOCKED, &pdev->flags)
4652 && (test_bit(R5_UPTODATE, &pdev->flags) ||
4653 test_bit(R5_Discard, &pdev->flags))))) &&
4654 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
4655 && !test_bit(R5_LOCKED, &qdev->flags)
4656 && (test_bit(R5_UPTODATE, &qdev->flags) ||
4657 test_bit(R5_Discard, &qdev->flags))))))
4658 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
4659
Song Liu1e6d6902016-11-17 15:24:39 -08004660 if (s.just_cached)
4661 r5c_handle_cached_data_endio(conf, sh, disks, &s.return_bi);
4662 r5l_stripe_write_finished(sh);
4663
NeilBrownef5b7c62012-11-22 09:13:36 +11004664 /* Now we might consider reading some blocks, either to check/generate
4665 * parity, or to satisfy requests
4666 * or to load a block that is being partially written.
4667 */
4668 if (s.to_read || s.non_overwrite
4669 || (conf->level == 6 && s.to_write && s.failed)
4670 || (s.syncing && (s.uptodate + s.compute < disks))
4671 || s.replacing
4672 || s.expanding)
4673 handle_stripe_fill(sh, &s, disks);
4674
Song Liu2ded3702016-11-17 15:24:38 -08004675 /*
4676 * When the stripe finishes full journal write cycle (write to journal
4677 * and raid disk), this is the clean up procedure so it is ready for
4678 * next operation.
4679 */
4680 r5c_finish_stripe_write_out(conf, sh, &s);
4681
4682 /*
4683 * Now to consider new write requests, cache write back and what else,
4684 * if anything should be read. We do not handle new writes when:
NeilBrown84789552011-07-27 11:00:36 +10004685 * 1/ A 'write' operation (copy+xor) is already in flight.
4686 * 2/ A 'check' operation is in flight, as it may clobber the parity
4687 * block.
Song Liu2ded3702016-11-17 15:24:38 -08004688 * 3/ A r5c cache log write is in flight.
NeilBrown84789552011-07-27 11:00:36 +10004689 */
Song Liu2ded3702016-11-17 15:24:38 -08004690
4691 if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
4692 if (!r5c_is_writeback(conf->log)) {
4693 if (s.to_write)
4694 handle_stripe_dirtying(conf, sh, &s, disks);
4695 } else { /* write back cache */
4696 int ret = 0;
4697
4698 /* First, try handle writes in caching phase */
4699 if (s.to_write)
4700 ret = r5c_try_caching_write(conf, sh, &s,
4701 disks);
4702 /*
4703 * If caching phase failed: ret == -EAGAIN
4704 * OR
4705 * stripe under reclaim: !caching && injournal
4706 *
4707 * fall back to handle_stripe_dirtying()
4708 */
4709 if (ret == -EAGAIN ||
4710 /* stripe under reclaim: !caching && injournal */
4711 (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
Song Liud7bd3982016-11-23 22:50:39 -08004712 s.injournal > 0)) {
4713 ret = handle_stripe_dirtying(conf, sh, &s,
4714 disks);
4715 if (ret == -EAGAIN)
4716 goto finish;
4717 }
Song Liu2ded3702016-11-17 15:24:38 -08004718 }
4719 }
NeilBrown84789552011-07-27 11:00:36 +10004720
4721 /* maybe we need to check and possibly fix the parity for this stripe
4722 * Any reads will already have been scheduled, so we just see if enough
4723 * data is available. The parity check is held off while parity
4724 * dependent operations are in flight.
4725 */
4726 if (sh->check_state ||
4727 (s.syncing && s.locked == 0 &&
4728 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
4729 !test_bit(STRIPE_INSYNC, &sh->state))) {
4730 if (conf->level == 6)
4731 handle_parity_checks6(conf, sh, &s, disks);
4732 else
4733 handle_parity_checks5(conf, sh, &s, disks);
4734 }
NeilBrownc5a31002011-07-27 11:00:36 +10004735
NeilBrownf94c0b62013-07-22 12:57:21 +10004736 if ((s.replacing || s.syncing) && s.locked == 0
4737 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
4738 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11004739 /* Write out to replacement devices where possible */
4740 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10004741 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
4742 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11004743 set_bit(R5_WantReplace, &sh->dev[i].flags);
4744 set_bit(R5_LOCKED, &sh->dev[i].flags);
4745 s.locked++;
4746 }
NeilBrownf94c0b62013-07-22 12:57:21 +10004747 if (s.replacing)
4748 set_bit(STRIPE_INSYNC, &sh->state);
4749 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11004750 }
4751 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10004752 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11004753 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10004754 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4755 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004756 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
4757 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10004758 }
4759
4760 /* If the failed drives are just a ReadError, then we might need
4761 * to progress the repair/check process
4762 */
4763 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
4764 for (i = 0; i < s.failed; i++) {
4765 struct r5dev *dev = &sh->dev[s.failed_num[i]];
4766 if (test_bit(R5_ReadError, &dev->flags)
4767 && !test_bit(R5_LOCKED, &dev->flags)
4768 && test_bit(R5_UPTODATE, &dev->flags)
4769 ) {
4770 if (!test_bit(R5_ReWrite, &dev->flags)) {
4771 set_bit(R5_Wantwrite, &dev->flags);
4772 set_bit(R5_ReWrite, &dev->flags);
4773 set_bit(R5_LOCKED, &dev->flags);
4774 s.locked++;
4775 } else {
4776 /* let's read it back */
4777 set_bit(R5_Wantread, &dev->flags);
4778 set_bit(R5_LOCKED, &dev->flags);
4779 s.locked++;
4780 }
4781 }
4782 }
4783
NeilBrown3687c062011-07-27 11:00:36 +10004784 /* Finish reconstruct operations initiated by the expansion process */
4785 if (sh->reconstruct_state == reconstruct_state_result) {
4786 struct stripe_head *sh_src
Shaohua Li6d036f72015-08-13 14:31:57 -07004787 = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrown3687c062011-07-27 11:00:36 +10004788 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
4789 /* sh cannot be written until sh_src has been read.
4790 * so arrange for sh to be delayed a little
4791 */
4792 set_bit(STRIPE_DELAYED, &sh->state);
4793 set_bit(STRIPE_HANDLE, &sh->state);
4794 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
4795 &sh_src->state))
4796 atomic_inc(&conf->preread_active_stripes);
Shaohua Li6d036f72015-08-13 14:31:57 -07004797 raid5_release_stripe(sh_src);
NeilBrown3687c062011-07-27 11:00:36 +10004798 goto finish;
4799 }
4800 if (sh_src)
Shaohua Li6d036f72015-08-13 14:31:57 -07004801 raid5_release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07004802
NeilBrown3687c062011-07-27 11:00:36 +10004803 sh->reconstruct_state = reconstruct_state_idle;
4804 clear_bit(STRIPE_EXPANDING, &sh->state);
4805 for (i = conf->raid_disks; i--; ) {
4806 set_bit(R5_Wantwrite, &sh->dev[i].flags);
4807 set_bit(R5_LOCKED, &sh->dev[i].flags);
4808 s.locked++;
4809 }
4810 }
4811
4812 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
4813 !sh->reconstruct_state) {
4814 /* Need to write out all blocks after computing parity */
4815 sh->disks = conf->raid_disks;
4816 stripe_set_idx(sh->sector, conf, 0, sh);
4817 schedule_reconstruction(sh, &s, 1, 1);
4818 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
4819 clear_bit(STRIPE_EXPAND_READY, &sh->state);
4820 atomic_dec(&conf->reshape_stripes);
4821 wake_up(&conf->wait_for_overlap);
4822 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4823 }
4824
4825 if (s.expanding && s.locked == 0 &&
4826 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
4827 handle_stripe_expansion(conf, sh);
4828
4829finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07004830 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10004831 if (unlikely(s.blocked_rdev)) {
4832 if (conf->mddev->external)
4833 md_wait_for_blocked_rdev(s.blocked_rdev,
4834 conf->mddev);
4835 else
4836 /* Internal metadata will immediately
4837 * be written by raid5d, so we don't
4838 * need to wait here.
4839 */
4840 rdev_dec_pending(s.blocked_rdev,
4841 conf->mddev);
4842 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004843
NeilBrownbc2607f2011-07-28 11:39:22 +10004844 if (s.handle_bad_blocks)
4845 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004846 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10004847 struct r5dev *dev = &sh->dev[i];
4848 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
4849 /* We own a safe reference to the rdev */
4850 rdev = conf->disks[i].rdev;
4851 if (!rdev_set_badblocks(rdev, sh->sector,
4852 STRIPE_SECTORS, 0))
4853 md_error(conf->mddev, rdev);
4854 rdev_dec_pending(rdev, conf->mddev);
4855 }
NeilBrownb84db562011-07-28 11:39:23 +10004856 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
4857 rdev = conf->disks[i].rdev;
4858 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004859 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10004860 rdev_dec_pending(rdev, conf->mddev);
4861 }
NeilBrown977df362011-12-23 10:17:53 +11004862 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
4863 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11004864 if (!rdev)
4865 /* rdev have been moved down */
4866 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11004867 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004868 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11004869 rdev_dec_pending(rdev, conf->mddev);
4870 }
NeilBrownbc2607f2011-07-28 11:39:22 +10004871 }
4872
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004873 if (s.ops_request)
4874 raid_run_ops(sh, s.ops_request);
4875
Dan Williamsf0e43bc2008-06-28 08:31:55 +10004876 ops_run_io(sh, &s);
4877
NeilBrownc5709ef2011-07-26 11:35:20 +10004878 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11004879 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02004880 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11004881 * have actually been submitted.
4882 */
4883 atomic_dec(&conf->preread_active_stripes);
4884 if (atomic_read(&conf->preread_active_stripes) <
4885 IO_THRESHOLD)
4886 md_wakeup_thread(conf->mddev->thread);
4887 }
4888
NeilBrownc3cce6c2015-08-14 12:47:33 +10004889 if (!bio_list_empty(&s.return_bi)) {
Shaohua Li29530792016-12-08 15:48:19 -08004890 if (test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) {
NeilBrownc3cce6c2015-08-14 12:47:33 +10004891 spin_lock_irq(&conf->device_lock);
4892 bio_list_merge(&conf->return_bi, &s.return_bi);
4893 spin_unlock_irq(&conf->device_lock);
4894 md_wakeup_thread(conf->mddev->thread);
4895 } else
4896 return_io(&s.return_bi);
4897 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004898
Dan Williams257a4b42011-11-08 16:22:06 +11004899 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07004900}
4901
NeilBrownd1688a62011-10-11 16:49:52 +11004902static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903{
4904 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
4905 while (!list_empty(&conf->delayed_list)) {
4906 struct list_head *l = conf->delayed_list.next;
4907 struct stripe_head *sh;
4908 sh = list_entry(l, struct stripe_head, lru);
4909 list_del_init(l);
4910 clear_bit(STRIPE_DELAYED, &sh->state);
4911 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4912 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004913 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08004914 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 }
NeilBrown482c0832011-04-18 18:25:42 +10004916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917}
4918
Shaohua Li566c09c2013-11-14 15:16:17 +11004919static void activate_bit_delay(struct r5conf *conf,
4920 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07004921{
4922 /* device_lock is held */
4923 struct list_head head;
4924 list_add(&head, &conf->bitmap_list);
4925 list_del_init(&conf->bitmap_list);
4926 while (!list_empty(&head)) {
4927 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11004928 int hash;
NeilBrown72626682005-09-09 16:23:54 -07004929 list_del_init(&sh->lru);
4930 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11004931 hash = sh->hash_lock_index;
4932 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07004933 }
4934}
4935
NeilBrown5c675f82014-12-15 12:56:56 +11004936static int raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07004937{
NeilBrownd1688a62011-10-11 16:49:52 +11004938 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07004939
4940 /* No difference between reads and writes. Just check
4941 * how busy the stripe_cache is
4942 */
NeilBrown3fa841d2009-09-23 18:10:29 +10004943
NeilBrown54233992015-02-26 12:21:04 +11004944 if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state))
NeilBrownf022b2f2006-10-03 01:15:56 -07004945 return 1;
Song Liua39f7af2016-11-17 15:24:40 -08004946
4947 /* Also checks whether there is pressure on r5cache log space */
4948 if (test_bit(R5C_LOG_TIGHT, &conf->cache_state))
4949 return 1;
NeilBrownf022b2f2006-10-03 01:15:56 -07004950 if (conf->quiesce)
4951 return 1;
Shaohua Li4bda5562013-11-14 15:16:17 +11004952 if (atomic_read(&conf->empty_inactive_list_nr))
NeilBrownf022b2f2006-10-03 01:15:56 -07004953 return 1;
4954
4955 return 0;
4956}
4957
NeilBrownfd01b882011-10-11 16:47:53 +11004958static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004959{
NeilBrown3cb5edf2015-07-15 17:24:17 +10004960 struct r5conf *conf = mddev->private;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004961 sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev);
NeilBrown3cb5edf2015-07-15 17:24:17 +10004962 unsigned int chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004963 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004964
NeilBrown3cb5edf2015-07-15 17:24:17 +10004965 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004966 return chunk_sectors >=
4967 ((sector & (chunk_sectors - 1)) + bio_sectors);
4968}
4969
4970/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004971 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4972 * later sampled by raid5d.
4973 */
NeilBrownd1688a62011-10-11 16:49:52 +11004974static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004975{
4976 unsigned long flags;
4977
4978 spin_lock_irqsave(&conf->device_lock, flags);
4979
4980 bi->bi_next = conf->retry_read_aligned_list;
4981 conf->retry_read_aligned_list = bi;
4982
4983 spin_unlock_irqrestore(&conf->device_lock, flags);
4984 md_wakeup_thread(conf->mddev->thread);
4985}
4986
NeilBrownd1688a62011-10-11 16:49:52 +11004987static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004988{
4989 struct bio *bi;
4990
4991 bi = conf->retry_read_aligned;
4992 if (bi) {
4993 conf->retry_read_aligned = NULL;
4994 return bi;
4995 }
4996 bi = conf->retry_read_aligned_list;
4997 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08004998 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004999 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02005000 /*
5001 * this sets the active strip count to 1 and the processed
5002 * strip count to zero (upper 8 bits)
5003 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10005004 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005005 }
5006
5007 return bi;
5008}
5009
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005010/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005011 * The "raid5_align_endio" should check if the read succeeded and if it
5012 * did, call bio_endio on the original bio (having bio_put the new bio
5013 * first).
5014 * If the read failed..
5015 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005016static void raid5_align_endio(struct bio *bi)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005017{
5018 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11005019 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005020 struct r5conf *conf;
NeilBrown3cb03002011-10-11 16:45:26 +11005021 struct md_rdev *rdev;
Sasha Levin9b81c842015-08-10 19:05:18 -04005022 int error = bi->bi_error;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005023
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005024 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005025
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005026 rdev = (void*)raid_bi->bi_next;
5027 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11005028 mddev = rdev->mddev;
5029 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005030
5031 rdev_dec_pending(rdev, conf->mddev);
5032
Sasha Levin9b81c842015-08-10 19:05:18 -04005033 if (!error) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005034 bio_endio(raid_bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005035 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10005036 wake_up(&conf->wait_for_quiescent);
NeilBrown6712ecf2007-09-27 12:47:43 +02005037 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005038 }
5039
Dan Williams45b42332007-07-09 11:56:43 -07005040 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005041
5042 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005043}
5044
Ming Lin7ef6b122015-05-06 22:51:24 -07005045static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005046{
NeilBrownd1688a62011-10-11 16:49:52 +11005047 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11005048 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005049 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11005050 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11005051 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005052
5053 if (!in_chunk_boundary(mddev, raid_bio)) {
Ming Lin7ef6b122015-05-06 22:51:24 -07005054 pr_debug("%s: non aligned\n", __func__);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005055 return 0;
5056 }
5057 /*
Ming Leid7a10302017-02-14 23:29:03 +08005058 * use bio_clone_fast to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005059 */
Ming Leid7a10302017-02-14 23:29:03 +08005060 align_bi = bio_clone_fast(raid_bio, GFP_NOIO, mddev->bio_set);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005061 if (!align_bi)
5062 return 0;
5063 /*
5064 * set bi_end_io to a new function, and set bi_private to the
5065 * original bio.
5066 */
5067 align_bi->bi_end_io = raid5_align_endio;
5068 align_bi->bi_private = raid_bio;
5069 /*
5070 * compute position
5071 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005072 align_bi->bi_iter.bi_sector =
5073 raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
5074 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005075
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005076 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005077 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11005078 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
5079 if (!rdev || test_bit(Faulty, &rdev->flags) ||
5080 rdev->recovery_offset < end_sector) {
5081 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
5082 if (rdev &&
5083 (test_bit(Faulty, &rdev->flags) ||
5084 !(test_bit(In_sync, &rdev->flags) ||
5085 rdev->recovery_offset >= end_sector)))
5086 rdev = NULL;
5087 }
Song Liu03b047f2017-01-11 13:39:14 -08005088
5089 if (r5c_big_stripe_cached(conf, align_bi->bi_iter.bi_sector)) {
5090 rcu_read_unlock();
5091 bio_put(align_bi);
5092 return 0;
5093 }
5094
NeilBrown671488c2011-12-23 10:17:52 +11005095 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10005096 sector_t first_bad;
5097 int bad_sectors;
5098
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005099 atomic_inc(&rdev->nr_pending);
5100 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005101 raid_bio->bi_next = (void*)rdev;
5102 align_bi->bi_bdev = rdev->bdev;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06005103 bio_clear_flag(align_bi, BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005104
Kent Overstreet7140aaf2013-09-25 13:37:01 -07005105 if (is_badblock(rdev, align_bi->bi_iter.bi_sector,
Kent Overstreet4f024f32013-10-11 15:44:27 -07005106 bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10005107 &first_bad, &bad_sectors)) {
Neil Brown387bb172007-02-08 14:20:29 -08005108 bio_put(align_bi);
5109 rdev_dec_pending(rdev, mddev);
5110 return 0;
5111 }
5112
majianpeng6c0544e2012-06-12 08:31:10 +08005113 /* No reshape active, so we can trust rdev->data_offset */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005114 align_bi->bi_iter.bi_sector += rdev->data_offset;
majianpeng6c0544e2012-06-12 08:31:10 +08005115
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005116 spin_lock_irq(&conf->device_lock);
Yuanhan Liub1b46482015-05-08 18:19:06 +10005117 wait_event_lock_irq(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005118 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01005119 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005120 atomic_inc(&conf->active_aligned_reads);
5121 spin_unlock_irq(&conf->device_lock);
5122
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005123 if (mddev->gendisk)
5124 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
5125 align_bi, disk_devt(mddev->gendisk),
Kent Overstreet4f024f32013-10-11 15:44:27 -07005126 raid_bio->bi_iter.bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005127 generic_make_request(align_bi);
5128 return 1;
5129 } else {
5130 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005131 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005132 return 0;
5133 }
5134}
5135
Ming Lin7ef6b122015-05-06 22:51:24 -07005136static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
5137{
5138 struct bio *split;
5139
5140 do {
5141 sector_t sector = raid_bio->bi_iter.bi_sector;
5142 unsigned chunk_sects = mddev->chunk_sectors;
5143 unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
5144
5145 if (sectors < bio_sectors(raid_bio)) {
5146 split = bio_split(raid_bio, sectors, GFP_NOIO, fs_bio_set);
5147 bio_chain(split, raid_bio);
5148 } else
5149 split = raid_bio;
5150
5151 if (!raid5_read_one_chunk(mddev, split)) {
5152 if (split != raid_bio)
5153 generic_make_request(raid_bio);
5154 return split;
5155 }
5156 } while (split != raid_bio);
5157
5158 return NULL;
5159}
5160
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005161/* __get_priority_stripe - get the next stripe to process
5162 *
5163 * Full stripe writes are allowed to pass preread active stripes up until
5164 * the bypass_threshold is exceeded. In general the bypass_count
5165 * increments when the handle_list is handled before the hold_list; however, it
5166 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
5167 * stripe with in flight i/o. The bypass_count will be reset when the
5168 * head of the hold_list has changed, i.e. the head was promoted to the
5169 * handle_list.
5170 */
Shaohua Li851c30c2013-08-28 14:30:16 +08005171static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005172{
Shaohua Li851c30c2013-08-28 14:30:16 +08005173 struct stripe_head *sh = NULL, *tmp;
5174 struct list_head *handle_list = NULL;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005175 struct r5worker_group *wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005176
5177 if (conf->worker_cnt_per_group == 0) {
5178 handle_list = &conf->handle_list;
5179 } else if (group != ANY_GROUP) {
5180 handle_list = &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005181 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08005182 } else {
5183 int i;
5184 for (i = 0; i < conf->group_cnt; i++) {
5185 handle_list = &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005186 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08005187 if (!list_empty(handle_list))
5188 break;
5189 }
5190 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005191
5192 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5193 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08005194 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005195 list_empty(&conf->hold_list) ? "empty" : "busy",
5196 atomic_read(&conf->pending_full_writes), conf->bypass_count);
5197
Shaohua Li851c30c2013-08-28 14:30:16 +08005198 if (!list_empty(handle_list)) {
5199 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005200
5201 if (list_empty(&conf->hold_list))
5202 conf->bypass_count = 0;
5203 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
5204 if (conf->hold_list.next == conf->last_hold)
5205 conf->bypass_count++;
5206 else {
5207 conf->last_hold = conf->hold_list.next;
5208 conf->bypass_count -= conf->bypass_threshold;
5209 if (conf->bypass_count < 0)
5210 conf->bypass_count = 0;
5211 }
5212 }
5213 } else if (!list_empty(&conf->hold_list) &&
5214 ((conf->bypass_threshold &&
5215 conf->bypass_count > conf->bypass_threshold) ||
5216 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08005217
5218 list_for_each_entry(tmp, &conf->hold_list, lru) {
5219 if (conf->worker_cnt_per_group == 0 ||
5220 group == ANY_GROUP ||
5221 !cpu_online(tmp->cpu) ||
5222 cpu_to_group(tmp->cpu) == group) {
5223 sh = tmp;
5224 break;
5225 }
5226 }
5227
5228 if (sh) {
5229 conf->bypass_count -= conf->bypass_threshold;
5230 if (conf->bypass_count < 0)
5231 conf->bypass_count = 0;
5232 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08005233 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005234 }
5235
5236 if (!sh)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005237 return NULL;
5238
Shaohua Libfc90cb2013-08-29 15:40:32 +08005239 if (wg) {
5240 wg->stripes_cnt--;
5241 sh->group = NULL;
5242 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005243 list_del_init(&sh->lru);
Shaohua Lic7a6d352014-04-15 09:12:54 +08005244 BUG_ON(atomic_inc_return(&sh->count) != 1);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005245 return sh;
5246}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005247
Shaohua Li8811b592012-08-02 08:33:00 +10005248struct raid5_plug_cb {
5249 struct blk_plug_cb cb;
5250 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11005251 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10005252};
5253
5254static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
5255{
5256 struct raid5_plug_cb *cb = container_of(
5257 blk_cb, struct raid5_plug_cb, cb);
5258 struct stripe_head *sh;
5259 struct mddev *mddev = cb->cb.data;
5260 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11005261 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11005262 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10005263
5264 if (cb->list.next && !list_empty(&cb->list)) {
5265 spin_lock_irq(&conf->device_lock);
5266 while (!list_empty(&cb->list)) {
5267 sh = list_first_entry(&cb->list, struct stripe_head, lru);
5268 list_del_init(&sh->lru);
5269 /*
5270 * avoid race release_stripe_plug() sees
5271 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5272 * is still in our list
5273 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005274 smp_mb__before_atomic();
Shaohua Li8811b592012-08-02 08:33:00 +10005275 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08005276 /*
5277 * STRIPE_ON_RELEASE_LIST could be set here. In that
5278 * case, the count is always > 1 here
5279 */
Shaohua Li566c09c2013-11-14 15:16:17 +11005280 hash = sh->hash_lock_index;
5281 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11005282 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10005283 }
5284 spin_unlock_irq(&conf->device_lock);
5285 }
Shaohua Li566c09c2013-11-14 15:16:17 +11005286 release_inactive_stripe_list(conf, cb->temp_inactive_list,
5287 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005288 if (mddev->queue)
5289 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10005290 kfree(cb);
5291}
5292
5293static void release_stripe_plug(struct mddev *mddev,
5294 struct stripe_head *sh)
5295{
5296 struct blk_plug_cb *blk_cb = blk_check_plugged(
5297 raid5_unplug, mddev,
5298 sizeof(struct raid5_plug_cb));
5299 struct raid5_plug_cb *cb;
5300
5301 if (!blk_cb) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005302 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005303 return;
5304 }
5305
5306 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
5307
Shaohua Li566c09c2013-11-14 15:16:17 +11005308 if (cb->list.next == NULL) {
5309 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10005310 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11005311 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5312 INIT_LIST_HEAD(cb->temp_inactive_list + i);
5313 }
Shaohua Li8811b592012-08-02 08:33:00 +10005314
5315 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
5316 list_add_tail(&sh->lru, &cb->list);
5317 else
Shaohua Li6d036f72015-08-13 14:31:57 -07005318 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005319}
5320
Shaohua Li620125f2012-10-11 13:49:05 +11005321static void make_discard_request(struct mddev *mddev, struct bio *bi)
5322{
5323 struct r5conf *conf = mddev->private;
5324 sector_t logical_sector, last_sector;
5325 struct stripe_head *sh;
5326 int remaining;
5327 int stripe_sectors;
5328
5329 if (mddev->reshape_position != MaxSector)
5330 /* Skip discard while reshape is happening */
5331 return;
5332
Kent Overstreet4f024f32013-10-11 15:44:27 -07005333 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
5334 last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9);
Shaohua Li620125f2012-10-11 13:49:05 +11005335
5336 bi->bi_next = NULL;
5337 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
5338
5339 stripe_sectors = conf->chunk_sectors *
5340 (conf->raid_disks - conf->max_degraded);
5341 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5342 stripe_sectors);
5343 sector_div(last_sector, stripe_sectors);
5344
5345 logical_sector *= conf->chunk_sectors;
5346 last_sector *= conf->chunk_sectors;
5347
5348 for (; logical_sector < last_sector;
5349 logical_sector += STRIPE_SECTORS) {
5350 DEFINE_WAIT(w);
5351 int d;
5352 again:
Shaohua Li6d036f72015-08-13 14:31:57 -07005353 sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
Shaohua Li620125f2012-10-11 13:49:05 +11005354 prepare_to_wait(&conf->wait_for_overlap, &w,
5355 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005356 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5357 if (test_bit(STRIPE_SYNCING, &sh->state)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005358 raid5_release_stripe(sh);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005359 schedule();
5360 goto again;
5361 }
5362 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11005363 spin_lock_irq(&sh->stripe_lock);
5364 for (d = 0; d < conf->raid_disks; d++) {
5365 if (d == sh->pd_idx || d == sh->qd_idx)
5366 continue;
5367 if (sh->dev[d].towrite || sh->dev[d].toread) {
5368 set_bit(R5_Overlap, &sh->dev[d].flags);
5369 spin_unlock_irq(&sh->stripe_lock);
Shaohua Li6d036f72015-08-13 14:31:57 -07005370 raid5_release_stripe(sh);
Shaohua Li620125f2012-10-11 13:49:05 +11005371 schedule();
5372 goto again;
5373 }
5374 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11005375 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11005376 finish_wait(&conf->wait_for_overlap, &w);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005377 sh->overwrite_disks = 0;
Shaohua Li620125f2012-10-11 13:49:05 +11005378 for (d = 0; d < conf->raid_disks; d++) {
5379 if (d == sh->pd_idx || d == sh->qd_idx)
5380 continue;
5381 sh->dev[d].towrite = bi;
5382 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
5383 raid5_inc_bi_active_stripes(bi);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005384 sh->overwrite_disks++;
Shaohua Li620125f2012-10-11 13:49:05 +11005385 }
5386 spin_unlock_irq(&sh->stripe_lock);
5387 if (conf->mddev->bitmap) {
5388 for (d = 0;
5389 d < conf->raid_disks - conf->max_degraded;
5390 d++)
5391 bitmap_startwrite(mddev->bitmap,
5392 sh->sector,
5393 STRIPE_SECTORS,
5394 0);
5395 sh->bm_seq = conf->seq_flush + 1;
5396 set_bit(STRIPE_BIT_DELAY, &sh->state);
5397 }
5398
5399 set_bit(STRIPE_HANDLE, &sh->state);
5400 clear_bit(STRIPE_DELAYED, &sh->state);
5401 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5402 atomic_inc(&conf->preread_active_stripes);
5403 release_stripe_plug(mddev, sh);
5404 }
5405
5406 remaining = raid5_dec_bi_active_stripes(bi);
5407 if (remaining == 0) {
5408 md_write_end(mddev);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005409 bio_endio(bi);
Shaohua Li620125f2012-10-11 13:49:05 +11005410 }
5411}
5412
Shaohua Li849674e2016-01-20 13:52:20 -08005413static void raid5_make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414{
NeilBrownd1688a62011-10-11 16:49:52 +11005415 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11005416 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 sector_t new_sector;
5418 sector_t logical_sector, last_sector;
5419 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01005420 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11005421 int remaining;
Shaohua Li27c0f682014-04-09 11:25:47 +08005422 DEFINE_WAIT(w);
5423 bool do_prepare;
Song Liu3bddb7f2016-11-18 16:46:50 -08005424 bool do_flush = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
Jens Axboe1eff9d32016-08-05 15:35:16 -06005426 if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
Shaohua Li828cbe92015-09-02 13:49:49 -07005427 int ret = r5l_handle_flush_request(conf->log, bi);
5428
5429 if (ret == 0)
5430 return;
5431 if (ret == -ENODEV) {
5432 md_flush_request(mddev, bi);
5433 return;
5434 }
5435 /* ret == -EAGAIN, fallback */
Song Liu3bddb7f2016-11-18 16:46:50 -08005436 /*
5437 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
5438 * we need to flush journal device
5439 */
5440 do_flush = bi->bi_opf & REQ_PREFLUSH;
NeilBrowne5dcdd82005-09-09 16:23:41 -07005441 }
5442
NeilBrown3d310eb2005-06-21 17:17:26 -07005443 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07005444
Eric Mei9ffc8f72015-03-18 23:39:11 -06005445 /*
5446 * If array is degraded, better not do chunk aligned read because
5447 * later we might have to read it again in order to reconstruct
5448 * data on failed drives.
5449 */
5450 if (rw == READ && mddev->degraded == 0 &&
Ming Lin7ef6b122015-05-06 22:51:24 -07005451 mddev->reshape_position == MaxSector) {
5452 bi = chunk_aligned_read(mddev, bi);
5453 if (!bi)
5454 return;
5455 }
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08005456
Mike Christie796a5cf2016-06-05 14:32:07 -05005457 if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
Shaohua Li620125f2012-10-11 13:49:05 +11005458 make_discard_request(mddev, bi);
5459 return;
5460 }
5461
Kent Overstreet4f024f32013-10-11 15:44:27 -07005462 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005463 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 bi->bi_next = NULL;
5465 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07005466
Shaohua Li27c0f682014-04-09 11:25:47 +08005467 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005469 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10005470 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08005471
Shaohua Li27c0f682014-04-09 11:25:47 +08005472 do_prepare = false;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005473 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10005474 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11005475 previous = 0;
Shaohua Li27c0f682014-04-09 11:25:47 +08005476 if (do_prepare)
5477 prepare_to_wait(&conf->wait_for_overlap, &w,
5478 TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005479 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11005480 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08005481 * 64bit on a 32bit platform, and so it might be
5482 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02005483 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08005484 * the lock is dropped, so once we get a reference
5485 * to the stripe that we think it is, we will have
5486 * to check again.
5487 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005488 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005489 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005490 ? logical_sector < conf->reshape_progress
5491 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005492 previous = 1;
5493 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10005494 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005495 ? logical_sector < conf->reshape_safe
5496 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08005497 spin_unlock_irq(&conf->device_lock);
5498 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005499 do_prepare = true;
NeilBrownb578d552006-03-27 01:18:12 -08005500 goto retry;
5501 }
5502 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005503 spin_unlock_irq(&conf->device_lock);
5504 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005505
NeilBrown112bf892009-03-31 14:39:38 +11005506 new_sector = raid5_compute_sector(conf, logical_sector,
5507 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11005508 &dd_idx, NULL);
Shaohua Li849674e2016-01-20 13:52:20 -08005509 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10005510 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 (unsigned long long)logical_sector);
5512
Shaohua Li6d036f72015-08-13 14:31:57 -07005513 sh = raid5_get_active_stripe(conf, new_sector, previous,
Jens Axboe1eff9d32016-08-05 15:35:16 -06005514 (bi->bi_opf & REQ_RAHEAD), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11005516 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005517 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08005518 * stripe, so we must do the range check again.
5519 * Expansion could still move past after this
5520 * test, but as we are holding a reference to
5521 * 'sh', we know that if that happens,
5522 * STRIPE_EXPANDING will get set and the expansion
5523 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005524 */
5525 int must_retry = 0;
5526 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005527 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11005528 ? logical_sector >= conf->reshape_progress
5529 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005530 /* mismatch, need to try again */
5531 must_retry = 1;
5532 spin_unlock_irq(&conf->device_lock);
5533 if (must_retry) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005534 raid5_release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07005535 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005536 do_prepare = true;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005537 goto retry;
5538 }
5539 }
NeilBrownc46501b2013-08-27 15:52:13 +10005540 if (read_seqcount_retry(&conf->gen_lock, seq)) {
5541 /* Might have got the wrong stripe_head
5542 * by accident
5543 */
Shaohua Li6d036f72015-08-13 14:31:57 -07005544 raid5_release_stripe(sh);
NeilBrownc46501b2013-08-27 15:52:13 +10005545 goto retry;
5546 }
NeilBrowne62e58a2009-07-01 13:15:35 +10005547
Namhyung Kimffd96e32011-07-18 17:38:51 +10005548 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10005549 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08005550 logical_sector < mddev->suspend_hi) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005551 raid5_release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10005552 /* As the suspend_* range is controlled by
5553 * userspace, we want an interruptible
5554 * wait.
5555 */
5556 flush_signals(current);
5557 prepare_to_wait(&conf->wait_for_overlap,
5558 &w, TASK_INTERRUPTIBLE);
5559 if (logical_sector >= mddev->suspend_lo &&
Shaohua Li27c0f682014-04-09 11:25:47 +08005560 logical_sector < mddev->suspend_hi) {
NeilBrowne62e58a2009-07-01 13:15:35 +10005561 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005562 do_prepare = true;
5563 }
NeilBrowne464eaf2006-03-27 01:18:14 -08005564 goto retry;
5565 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005566
5567 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
shli@kernel.orgda41ba62014-12-15 12:57:03 +11005568 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005569 /* Stripe is busy expanding or
5570 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 * and wait a while
5572 */
NeilBrown482c0832011-04-18 18:25:42 +10005573 md_wakeup_thread(mddev->thread);
Shaohua Li6d036f72015-08-13 14:31:57 -07005574 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005576 do_prepare = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 goto retry;
5578 }
Song Liu3bddb7f2016-11-18 16:46:50 -08005579 if (do_flush) {
5580 set_bit(STRIPE_R5C_PREFLUSH, &sh->state);
5581 /* we only need flush for one stripe */
5582 do_flush = false;
5583 }
5584
NeilBrown6ed30032008-02-06 01:40:00 -08005585 set_bit(STRIPE_HANDLE, &sh->state);
5586 clear_bit(STRIPE_DELAYED, &sh->state);
shli@kernel.org59fc6302014-12-15 12:57:03 +11005587 if ((!sh->batch_head || sh == sh->batch_head) &&
Jens Axboe1eff9d32016-08-05 15:35:16 -06005588 (bi->bi_opf & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11005589 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5590 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10005591 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 } else {
5593 /* cannot get stripe for read-ahead, just give-up */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005594 bi->bi_error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 break;
5596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 }
Shaohua Li27c0f682014-04-09 11:25:47 +08005598 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown7c13edc2011-04-18 18:25:43 +10005599
Shaohua Lie7836bd62012-07-19 16:01:31 +10005600 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08005601 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
NeilBrown16a53ec2006-06-26 00:27:38 -07005603 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02005605
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005606 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
5607 bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005608 bio_endio(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610}
5611
NeilBrownfd01b882011-10-11 16:47:53 +11005612static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11005613
NeilBrownfd01b882011-10-11 16:47:53 +11005614static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615{
NeilBrown52c03292006-06-26 00:27:43 -07005616 /* reshaping is quite different to recovery/resync so it is
5617 * handled quite separately ... here.
5618 *
5619 * On each call to sync_request, we gather one chunk worth of
5620 * destination stripes and flag them as expanding.
5621 * Then we find all the source stripes and request reads.
5622 * As the reads complete, handle_stripe will copy the data
5623 * into the destination stripe and release that stripe.
5624 */
NeilBrownd1688a62011-10-11 16:49:52 +11005625 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005627 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08005628 int raid_disks = conf->previous_raid_disks;
5629 int data_disks = raid_disks - conf->max_degraded;
5630 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07005631 int i;
5632 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11005633 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11005634 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11005635 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11005636 struct list_head stripes;
NeilBrown92140482015-07-06 12:28:45 +10005637 sector_t retn;
NeilBrown52c03292006-06-26 00:27:43 -07005638
NeilBrownfef9c612009-03-31 15:16:46 +11005639 if (sector_nr == 0) {
5640 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10005641 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005642 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
5643 sector_nr = raid5_size(mddev, 0, 0)
5644 - conf->reshape_progress;
NeilBrown6cbd8142015-07-24 13:30:32 +10005645 } else if (mddev->reshape_backwards &&
5646 conf->reshape_progress == MaxSector) {
5647 /* shouldn't happen, but just in case, finish up.*/
5648 sector_nr = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +10005649 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005650 conf->reshape_progress > 0)
5651 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005652 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005653 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11005654 mddev->curr_resync_completed = sector_nr;
5655 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11005656 *skipped = 1;
NeilBrown92140482015-07-06 12:28:45 +10005657 retn = sector_nr;
5658 goto finish;
NeilBrownfef9c612009-03-31 15:16:46 +11005659 }
NeilBrown52c03292006-06-26 00:27:43 -07005660 }
5661
NeilBrown7a661382009-03-31 15:21:40 +11005662 /* We need to process a full chunk at a time.
5663 * If old and new chunk sizes differ, we need to process the
5664 * largest of these
5665 */
NeilBrown3cb5edf2015-07-15 17:24:17 +10005666
5667 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
NeilBrown7a661382009-03-31 15:21:40 +11005668
NeilBrownb5254dd2012-05-21 09:27:01 +10005669 /* We update the metadata at least every 10 seconds, or when
5670 * the data about to be copied would over-write the source of
5671 * the data at the front of the range. i.e. one new_stripe
5672 * along from reshape_progress new_maps to after where
5673 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07005674 */
NeilBrownfef9c612009-03-31 15:16:46 +11005675 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005676 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11005677 readpos = conf->reshape_progress;
5678 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005679 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08005680 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10005681 if (mddev->reshape_backwards) {
NeilBrownc74c0d72015-07-15 17:54:15 +10005682 BUG_ON(writepos < reshape_sectors);
5683 writepos -= reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11005684 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11005685 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11005686 } else {
NeilBrown7a661382009-03-31 15:21:40 +11005687 writepos += reshape_sectors;
NeilBrownc74c0d72015-07-15 17:54:15 +10005688 /* readpos and safepos are worst-case calculations.
5689 * A negative number is overly pessimistic, and causes
5690 * obvious problems for unsigned storage. So clip to 0.
5691 */
NeilBrowned37d832009-05-27 21:39:05 +10005692 readpos -= min_t(sector_t, reshape_sectors, readpos);
5693 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11005694 }
NeilBrown52c03292006-06-26 00:27:43 -07005695
NeilBrownb5254dd2012-05-21 09:27:01 +10005696 /* Having calculated the 'writepos' possibly use it
5697 * to set 'stripe_addr' which is where we will write to.
5698 */
5699 if (mddev->reshape_backwards) {
5700 BUG_ON(conf->reshape_progress == 0);
5701 stripe_addr = writepos;
5702 BUG_ON((mddev->dev_sectors &
5703 ~((sector_t)reshape_sectors - 1))
5704 - reshape_sectors - stripe_addr
5705 != sector_nr);
5706 } else {
5707 BUG_ON(writepos != sector_nr + reshape_sectors);
5708 stripe_addr = sector_nr;
5709 }
5710
NeilBrownc8f517c2009-03-31 15:28:40 +11005711 /* 'writepos' is the most advanced device address we might write.
5712 * 'readpos' is the least advanced device address we might read.
5713 * 'safepos' is the least address recorded in the metadata as having
5714 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10005715 * If there is a min_offset_diff, these are adjusted either by
5716 * increasing the safepos/readpos if diff is negative, or
5717 * increasing writepos if diff is positive.
5718 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11005719 * ensure safety in the face of a crash - that must be done by userspace
5720 * making a backup of the data. So in that case there is no particular
5721 * rush to update metadata.
5722 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5723 * update the metadata to advance 'safepos' to match 'readpos' so that
5724 * we can be safe in the event of a crash.
5725 * So we insist on updating metadata if safepos is behind writepos and
5726 * readpos is beyond writepos.
5727 * In any case, update the metadata every 10 seconds.
5728 * Maybe that number should be configurable, but I'm not sure it is
5729 * worth it.... maybe it could be a multiple of safemode_delay???
5730 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005731 if (conf->min_offset_diff < 0) {
5732 safepos += -conf->min_offset_diff;
5733 readpos += -conf->min_offset_diff;
5734 } else
5735 writepos += conf->min_offset_diff;
5736
NeilBrown2c810cd2012-05-21 09:27:00 +10005737 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11005738 ? (safepos > writepos && readpos < writepos)
5739 : (safepos < writepos && readpos > writepos)) ||
5740 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07005741 /* Cannot proceed until we've updated the superblock... */
5742 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005743 atomic_read(&conf->reshape_stripes)==0
5744 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5745 if (atomic_read(&conf->reshape_stripes) != 0)
5746 return 0;
NeilBrownfef9c612009-03-31 15:16:46 +11005747 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005748 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005749 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08005750 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown52c03292006-06-26 00:27:43 -07005751 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08005752 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11005753 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5754 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5755 return 0;
NeilBrown52c03292006-06-26 00:27:43 -07005756 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005757 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07005758 spin_unlock_irq(&conf->device_lock);
5759 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005760 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07005761 }
5762
NeilBrownab69ae12009-03-31 15:26:47 +11005763 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11005764 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07005765 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10005766 int skipped_disk = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07005767 sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005768 set_bit(STRIPE_EXPANDING, &sh->state);
5769 atomic_inc(&conf->reshape_stripes);
5770 /* If any of this stripe is beyond the end of the old
5771 * array, then we need to zero those blocks
5772 */
5773 for (j=sh->disks; j--;) {
5774 sector_t s;
5775 if (j == sh->pd_idx)
5776 continue;
NeilBrownf4168852007-02-28 20:11:53 -08005777 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11005778 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08005779 continue;
Shaohua Li6d036f72015-08-13 14:31:57 -07005780 s = raid5_compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11005781 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10005782 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07005783 continue;
5784 }
5785 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
5786 set_bit(R5_Expanded, &sh->dev[j].flags);
5787 set_bit(R5_UPTODATE, &sh->dev[j].flags);
5788 }
NeilBrowna9f326e2009-09-23 18:06:41 +10005789 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07005790 set_bit(STRIPE_EXPAND_READY, &sh->state);
5791 set_bit(STRIPE_HANDLE, &sh->state);
5792 }
NeilBrownab69ae12009-03-31 15:26:47 +11005793 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07005794 }
5795 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005796 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11005797 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005798 else
NeilBrown7a661382009-03-31 15:21:40 +11005799 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07005800 spin_unlock_irq(&conf->device_lock);
5801 /* Ok, those stripe are ready. We can start scheduling
5802 * reads on the source stripes.
5803 * The source stripes are determined by mapping the first and last
5804 * block on the destination stripes.
5805 */
NeilBrown52c03292006-06-26 00:27:43 -07005806 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11005807 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11005808 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07005809 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10005810 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10005811 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11005812 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11005813 if (last_sector >= mddev->dev_sectors)
5814 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07005815 while (first_sector <= last_sector) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005816 sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005817 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
5818 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07005819 raid5_release_stripe(sh);
NeilBrown52c03292006-06-26 00:27:43 -07005820 first_sector += STRIPE_SECTORS;
5821 }
NeilBrownab69ae12009-03-31 15:26:47 +11005822 /* Now that the sources are clearly marked, we can release
5823 * the destination stripes
5824 */
5825 while (!list_empty(&stripes)) {
5826 sh = list_entry(stripes.next, struct stripe_head, lru);
5827 list_del_init(&sh->lru);
Shaohua Li6d036f72015-08-13 14:31:57 -07005828 raid5_release_stripe(sh);
NeilBrownab69ae12009-03-31 15:26:47 +11005829 }
NeilBrownc6207272008-02-06 01:39:52 -08005830 /* If this takes us to the resync_max point where we have to pause,
5831 * then we need to write out the superblock.
5832 */
NeilBrown7a661382009-03-31 15:21:40 +11005833 sector_nr += reshape_sectors;
NeilBrown92140482015-07-06 12:28:45 +10005834 retn = reshape_sectors;
5835finish:
NeilBrownc5e19d92015-07-17 12:06:02 +10005836 if (mddev->curr_resync_completed > mddev->resync_max ||
5837 (sector_nr - mddev->curr_resync_completed) * 2
NeilBrownc03f6a12009-04-17 11:06:30 +10005838 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08005839 /* Cannot proceed until we've updated the superblock... */
5840 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005841 atomic_read(&conf->reshape_stripes) == 0
5842 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5843 if (atomic_read(&conf->reshape_stripes) != 0)
5844 goto ret;
NeilBrownfef9c612009-03-31 15:16:46 +11005845 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005846 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005847 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08005848 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownc6207272008-02-06 01:39:52 -08005849 md_wakeup_thread(mddev->thread);
5850 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005851 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)
NeilBrownc91abf52013-11-19 12:02:01 +11005852 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5853 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5854 goto ret;
NeilBrownc6207272008-02-06 01:39:52 -08005855 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005856 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08005857 spin_unlock_irq(&conf->device_lock);
5858 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005859 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08005860 }
NeilBrownc91abf52013-11-19 12:02:01 +11005861ret:
NeilBrown92140482015-07-06 12:28:45 +10005862 return retn;
NeilBrown52c03292006-06-26 00:27:43 -07005863}
5864
Shaohua Li849674e2016-01-20 13:52:20 -08005865static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
5866 int *skipped)
NeilBrown52c03292006-06-26 00:27:43 -07005867{
NeilBrownd1688a62011-10-11 16:49:52 +11005868 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07005869 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11005870 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11005871 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07005872 int still_degraded = 0;
5873 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874
NeilBrown72626682005-09-09 16:23:54 -07005875 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11005877
NeilBrown29269552006-03-27 01:18:10 -08005878 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
5879 end_reshape(conf);
5880 return 0;
5881 }
NeilBrown72626682005-09-09 16:23:54 -07005882
5883 if (mddev->curr_resync < max_sector) /* aborted */
5884 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
5885 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07005886 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07005887 conf->fullsync = 0;
5888 bitmap_close_sync(mddev->bitmap);
5889
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 return 0;
5891 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08005892
NeilBrown64bd6602009-08-03 10:59:58 +10005893 /* Allow raid5_quiesce to complete */
5894 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
5895
NeilBrown52c03292006-06-26 00:27:43 -07005896 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5897 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08005898
NeilBrownc6207272008-02-06 01:39:52 -08005899 /* No need to check resync_max as we never do more than one
5900 * stripe, and as resync_max will always be on a chunk boundary,
5901 * if the check in md_do_sync didn't fire, there is no chance
5902 * of overstepping resync_max here
5903 */
5904
NeilBrown16a53ec2006-06-26 00:27:38 -07005905 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 * to resync, then assert that we are finished, because there is
5907 * nothing we can do.
5908 */
NeilBrown3285edf2006-06-26 00:27:55 -07005909 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005910 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005911 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07005912 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 return rv;
5914 }
majianpeng6f608042013-04-24 11:42:41 +10005915 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
5916 !conf->fullsync &&
5917 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
5918 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07005919 /* we can skip this block, and probably more */
5920 sync_blocks /= STRIPE_SECTORS;
5921 *skipped = 1;
5922 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
5923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10005925 bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
NeilBrownb47490c2008-02-06 01:39:50 -08005926
Shaohua Li6d036f72015-08-13 14:31:57 -07005927 sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 if (sh == NULL) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005929 sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07005931 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08005933 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005935 /* Need to check if array will still be degraded after recovery/resync
Eric Mei16d9cfa2015-01-06 09:35:02 -08005936 * Note in case of > 1 drive failures it's possible we're rebuilding
5937 * one drive while leaving another faulty drive in array.
NeilBrown16a53ec2006-06-26 00:27:38 -07005938 */
Eric Mei16d9cfa2015-01-06 09:35:02 -08005939 rcu_read_lock();
5940 for (i = 0; i < conf->raid_disks; i++) {
5941 struct md_rdev *rdev = ACCESS_ONCE(conf->disks[i].rdev);
5942
5943 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
NeilBrown16a53ec2006-06-26 00:27:38 -07005944 still_degraded = 1;
Eric Mei16d9cfa2015-01-06 09:35:02 -08005945 }
5946 rcu_read_unlock();
NeilBrown16a53ec2006-06-26 00:27:38 -07005947
5948 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
5949
NeilBrown83206d62011-07-26 11:19:49 +10005950 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Eivind Sarto053f5b62014-06-09 17:06:19 -07005951 set_bit(STRIPE_HANDLE, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
Shaohua Li6d036f72015-08-13 14:31:57 -07005953 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954
5955 return STRIPE_SECTORS;
5956}
5957
NeilBrownd1688a62011-10-11 16:49:52 +11005958static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005959{
5960 /* We may not be able to submit a whole bio at once as there
5961 * may not be enough stripe_heads available.
5962 * We cannot pre-allocate enough stripe_heads as we may need
5963 * more than exist in the cache (if we allow ever large chunks).
5964 * So we do one stripe head at a time and record in
5965 * ->bi_hw_segments how many have been done.
5966 *
5967 * We *know* that this entire raid_bio is in one chunk, so
5968 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5969 */
5970 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11005971 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005972 sector_t sector, logical_sector, last_sector;
5973 int scnt = 0;
5974 int remaining;
5975 int handled = 0;
5976
Kent Overstreet4f024f32013-10-11 15:44:27 -07005977 logical_sector = raid_bio->bi_iter.bi_sector &
5978 ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11005979 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11005980 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005981 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005982
5983 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08005984 logical_sector += STRIPE_SECTORS,
5985 sector += STRIPE_SECTORS,
5986 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005987
Shaohua Lie7836bd62012-07-19 16:01:31 +10005988 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005989 /* already done this stripe */
5990 continue;
5991
Shaohua Li6d036f72015-08-13 14:31:57 -07005992 sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005993
5994 if (!sh) {
5995 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10005996 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005997 conf->retry_read_aligned = raid_bio;
5998 return handled;
5999 }
6000
shli@kernel.orgda41ba62014-12-15 12:57:03 +11006001 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07006002 raid5_release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10006003 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08006004 conf->retry_read_aligned = raid_bio;
6005 return handled;
6006 }
6007
majianpeng3f9e7c12012-07-31 10:04:21 +10006008 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07006009 handle_stripe(sh);
Shaohua Li6d036f72015-08-13 14:31:57 -07006010 raid5_release_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006011 handled++;
6012 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10006013 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07006014 if (remaining == 0) {
6015 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
6016 raid_bio, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006017 bio_endio(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07006018 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006019 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10006020 wake_up(&conf->wait_for_quiescent);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006021 return handled;
6022}
6023
Shaohua Libfc90cb2013-08-29 15:40:32 +08006024static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11006025 struct r5worker *worker,
6026 struct list_head *temp_inactive_list)
Shaohua Li46a06402012-08-02 08:33:15 +10006027{
6028 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11006029 int i, batch_size = 0, hash;
6030 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10006031
6032 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08006033 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10006034 batch[batch_size++] = sh;
6035
Shaohua Li566c09c2013-11-14 15:16:17 +11006036 if (batch_size == 0) {
6037 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6038 if (!list_empty(temp_inactive_list + i))
6039 break;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006040 if (i == NR_STRIPE_HASH_LOCKS) {
6041 spin_unlock_irq(&conf->device_lock);
6042 r5l_flush_stripe_to_raid(conf->log);
6043 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006044 return batch_size;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006045 }
Shaohua Li566c09c2013-11-14 15:16:17 +11006046 release_inactive = true;
6047 }
Shaohua Li46a06402012-08-02 08:33:15 +10006048 spin_unlock_irq(&conf->device_lock);
6049
Shaohua Li566c09c2013-11-14 15:16:17 +11006050 release_inactive_stripe_list(conf, temp_inactive_list,
6051 NR_STRIPE_HASH_LOCKS);
6052
Shaohua Lia8c34f92015-09-02 13:49:46 -07006053 r5l_flush_stripe_to_raid(conf->log);
Shaohua Li566c09c2013-11-14 15:16:17 +11006054 if (release_inactive) {
6055 spin_lock_irq(&conf->device_lock);
6056 return 0;
6057 }
6058
Shaohua Li46a06402012-08-02 08:33:15 +10006059 for (i = 0; i < batch_size; i++)
6060 handle_stripe(batch[i]);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07006061 r5l_write_stripe_run(conf->log);
Shaohua Li46a06402012-08-02 08:33:15 +10006062
6063 cond_resched();
6064
6065 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006066 for (i = 0; i < batch_size; i++) {
6067 hash = batch[i]->hash_lock_index;
6068 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
6069 }
Shaohua Li46a06402012-08-02 08:33:15 +10006070 return batch_size;
6071}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006072
Shaohua Li851c30c2013-08-28 14:30:16 +08006073static void raid5_do_work(struct work_struct *work)
6074{
6075 struct r5worker *worker = container_of(work, struct r5worker, work);
6076 struct r5worker_group *group = worker->group;
6077 struct r5conf *conf = group->conf;
6078 int group_id = group - conf->worker_groups;
6079 int handled;
6080 struct blk_plug plug;
6081
6082 pr_debug("+++ raid5worker active\n");
6083
6084 blk_start_plug(&plug);
6085 handled = 0;
6086 spin_lock_irq(&conf->device_lock);
6087 while (1) {
6088 int batch_size, released;
6089
Shaohua Li566c09c2013-11-14 15:16:17 +11006090 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006091
Shaohua Li566c09c2013-11-14 15:16:17 +11006092 batch_size = handle_active_stripes(conf, group_id, worker,
6093 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08006094 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08006095 if (!batch_size && !released)
6096 break;
6097 handled += batch_size;
6098 }
6099 pr_debug("%d stripes handled\n", handled);
6100
6101 spin_unlock_irq(&conf->device_lock);
6102 blk_finish_plug(&plug);
6103
6104 pr_debug("--- raid5worker inactive\n");
6105}
6106
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107/*
6108 * This is our raid5 kernel thread.
6109 *
6110 * We scan the hash table for stripes which can be handled now.
6111 * During the scan, completed stripes are saved for us by the interrupt
6112 * handler, so that they will not have to wait for our next wakeup.
6113 */
Shaohua Li4ed87312012-10-11 13:34:00 +11006114static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115{
Shaohua Li4ed87312012-10-11 13:34:00 +11006116 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11006117 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006119 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
Dan Williams45b42332007-07-09 11:56:43 -07006121 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122
6123 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124
NeilBrownc3cce6c2015-08-14 12:47:33 +10006125 if (!bio_list_empty(&conf->return_bi) &&
Shaohua Li29530792016-12-08 15:48:19 -08006126 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrownc3cce6c2015-08-14 12:47:33 +10006127 struct bio_list tmp = BIO_EMPTY_LIST;
6128 spin_lock_irq(&conf->device_lock);
Shaohua Li29530792016-12-08 15:48:19 -08006129 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrownc3cce6c2015-08-14 12:47:33 +10006130 bio_list_merge(&tmp, &conf->return_bi);
6131 bio_list_init(&conf->return_bi);
6132 }
6133 spin_unlock_irq(&conf->device_lock);
6134 return_io(&tmp);
6135 }
6136
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006137 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 handled = 0;
6139 spin_lock_irq(&conf->device_lock);
6140 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006141 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08006142 int batch_size, released;
6143
Shaohua Li566c09c2013-11-14 15:16:17 +11006144 released = release_stripe_list(conf, conf->temp_inactive_list);
NeilBrownedbe83a2015-02-26 12:47:56 +11006145 if (released)
6146 clear_bit(R5_DID_ALLOC, &conf->cache_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147
NeilBrown0021b7b2012-07-31 09:08:14 +02006148 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10006149 !list_empty(&conf->bitmap_list)) {
6150 /* Now is a good time to flush some bitmap updates */
6151 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08006152 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07006153 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08006154 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10006155 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11006156 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07006157 }
NeilBrown0021b7b2012-07-31 09:08:14 +02006158 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07006159
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006160 while ((bio = remove_bio_from_retry(conf))) {
6161 int ok;
6162 spin_unlock_irq(&conf->device_lock);
6163 ok = retry_aligned_read(conf, bio);
6164 spin_lock_irq(&conf->device_lock);
6165 if (!ok)
6166 break;
6167 handled++;
6168 }
6169
Shaohua Li566c09c2013-11-14 15:16:17 +11006170 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
6171 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08006172 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 break;
Shaohua Li46a06402012-08-02 08:33:15 +10006174 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175
Shaohua Li29530792016-12-08 15:48:19 -08006176 if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) {
Shaohua Li46a06402012-08-02 08:33:15 +10006177 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10006178 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10006179 spin_lock_irq(&conf->device_lock);
6180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 }
Dan Williams45b42332007-07-09 11:56:43 -07006182 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183
6184 spin_unlock_irq(&conf->device_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006185 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
6186 mutex_trylock(&conf->cache_size_mutex)) {
NeilBrownedbe83a2015-02-26 12:47:56 +11006187 grow_one_stripe(conf, __GFP_NOWARN);
6188 /* Set flag even if allocation failed. This helps
6189 * slow down allocation requests when mem is short
6190 */
6191 set_bit(R5_DID_ALLOC, &conf->cache_state);
NeilBrown2d5b5692015-07-06 12:49:23 +10006192 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194
Shaohua Li765d7042017-01-04 09:33:23 -08006195 flush_deferred_bios(conf);
6196
Shaohua Li0576b1c2015-08-13 14:32:00 -07006197 r5l_flush_stripe_to_raid(conf->log);
6198
Dan Williamsc9f21aa2008-07-23 12:05:51 -07006199 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006200 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201
Dan Williams45b42332007-07-09 11:56:43 -07006202 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203}
6204
NeilBrown3f294f42005-11-08 21:39:25 -08006205static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006206raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006207{
NeilBrown7b1485b2014-12-15 12:56:59 +11006208 struct r5conf *conf;
6209 int ret = 0;
6210 spin_lock(&mddev->lock);
6211 conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006212 if (conf)
NeilBrownedbe83a2015-02-26 12:47:56 +11006213 ret = sprintf(page, "%d\n", conf->min_nr_stripes);
NeilBrown7b1485b2014-12-15 12:56:59 +11006214 spin_unlock(&mddev->lock);
6215 return ret;
NeilBrown3f294f42005-11-08 21:39:25 -08006216}
6217
NeilBrownc41d4ac2010-06-01 19:37:24 +10006218int
NeilBrownfd01b882011-10-11 16:47:53 +11006219raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10006220{
NeilBrownd1688a62011-10-11 16:49:52 +11006221 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10006222 int err;
6223
6224 if (size <= 16 || size > 32768)
6225 return -EINVAL;
NeilBrown486f0642015-02-25 12:10:35 +11006226
NeilBrownedbe83a2015-02-26 12:47:56 +11006227 conf->min_nr_stripes = size;
NeilBrown2d5b5692015-07-06 12:49:23 +10006228 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006229 while (size < conf->max_nr_stripes &&
6230 drop_one_stripe(conf))
6231 ;
NeilBrown2d5b5692015-07-06 12:49:23 +10006232 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006233
NeilBrownedbe83a2015-02-26 12:47:56 +11006234
NeilBrownc41d4ac2010-06-01 19:37:24 +10006235 err = md_allow_write(mddev);
6236 if (err)
6237 return err;
NeilBrown486f0642015-02-25 12:10:35 +11006238
NeilBrown2d5b5692015-07-06 12:49:23 +10006239 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006240 while (size > conf->max_nr_stripes)
6241 if (!grow_one_stripe(conf, GFP_KERNEL))
6242 break;
NeilBrown2d5b5692015-07-06 12:49:23 +10006243 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006244
NeilBrownc41d4ac2010-06-01 19:37:24 +10006245 return 0;
6246}
6247EXPORT_SYMBOL(raid5_set_cache_size);
6248
NeilBrown3f294f42005-11-08 21:39:25 -08006249static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006250raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08006251{
NeilBrown67918752014-12-15 12:57:01 +11006252 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006253 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07006254 int err;
6255
NeilBrown3f294f42005-11-08 21:39:25 -08006256 if (len >= PAGE_SIZE)
6257 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006258 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08006259 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006260 err = mddev_lock(mddev);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006261 if (err)
6262 return err;
NeilBrown67918752014-12-15 12:57:01 +11006263 conf = mddev->private;
6264 if (!conf)
6265 err = -ENODEV;
6266 else
6267 err = raid5_set_cache_size(mddev, new);
6268 mddev_unlock(mddev);
6269
6270 return err ?: len;
NeilBrown3f294f42005-11-08 21:39:25 -08006271}
NeilBrown007583c2005-11-08 21:39:30 -08006272
NeilBrown96de1e62005-11-08 21:39:39 -08006273static struct md_sysfs_entry
6274raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
6275 raid5_show_stripe_cache_size,
6276 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08006277
6278static ssize_t
Markus Stockhausend06f1912014-12-15 12:57:05 +11006279raid5_show_rmw_level(struct mddev *mddev, char *page)
6280{
6281 struct r5conf *conf = mddev->private;
6282 if (conf)
6283 return sprintf(page, "%d\n", conf->rmw_level);
6284 else
6285 return 0;
6286}
6287
6288static ssize_t
6289raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
6290{
6291 struct r5conf *conf = mddev->private;
6292 unsigned long new;
6293
6294 if (!conf)
6295 return -ENODEV;
6296
6297 if (len >= PAGE_SIZE)
6298 return -EINVAL;
6299
6300 if (kstrtoul(page, 10, &new))
6301 return -EINVAL;
6302
6303 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
6304 return -EINVAL;
6305
6306 if (new != PARITY_DISABLE_RMW &&
6307 new != PARITY_ENABLE_RMW &&
6308 new != PARITY_PREFER_RMW)
6309 return -EINVAL;
6310
6311 conf->rmw_level = new;
6312 return len;
6313}
6314
6315static struct md_sysfs_entry
6316raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
6317 raid5_show_rmw_level,
6318 raid5_store_rmw_level);
6319
6320
6321static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006322raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006323{
NeilBrown7b1485b2014-12-15 12:56:59 +11006324 struct r5conf *conf;
6325 int ret = 0;
6326 spin_lock(&mddev->lock);
6327 conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006328 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006329 ret = sprintf(page, "%d\n", conf->bypass_threshold);
6330 spin_unlock(&mddev->lock);
6331 return ret;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006332}
6333
6334static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006335raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006336{
NeilBrown67918752014-12-15 12:57:01 +11006337 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006338 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006339 int err;
6340
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006341 if (len >= PAGE_SIZE)
6342 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006343 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006344 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006345
6346 err = mddev_lock(mddev);
6347 if (err)
6348 return err;
6349 conf = mddev->private;
6350 if (!conf)
6351 err = -ENODEV;
NeilBrownedbe83a2015-02-26 12:47:56 +11006352 else if (new > conf->min_nr_stripes)
NeilBrown67918752014-12-15 12:57:01 +11006353 err = -EINVAL;
6354 else
6355 conf->bypass_threshold = new;
6356 mddev_unlock(mddev);
6357 return err ?: len;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006358}
6359
6360static struct md_sysfs_entry
6361raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
6362 S_IRUGO | S_IWUSR,
6363 raid5_show_preread_threshold,
6364 raid5_store_preread_threshold);
6365
6366static ssize_t
Shaohua Lid592a992014-05-21 17:57:44 +08006367raid5_show_skip_copy(struct mddev *mddev, char *page)
6368{
NeilBrown7b1485b2014-12-15 12:56:59 +11006369 struct r5conf *conf;
6370 int ret = 0;
6371 spin_lock(&mddev->lock);
6372 conf = mddev->private;
Shaohua Lid592a992014-05-21 17:57:44 +08006373 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006374 ret = sprintf(page, "%d\n", conf->skip_copy);
6375 spin_unlock(&mddev->lock);
6376 return ret;
Shaohua Lid592a992014-05-21 17:57:44 +08006377}
6378
6379static ssize_t
6380raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
6381{
NeilBrown67918752014-12-15 12:57:01 +11006382 struct r5conf *conf;
Shaohua Lid592a992014-05-21 17:57:44 +08006383 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006384 int err;
6385
Shaohua Lid592a992014-05-21 17:57:44 +08006386 if (len >= PAGE_SIZE)
6387 return -EINVAL;
Shaohua Lid592a992014-05-21 17:57:44 +08006388 if (kstrtoul(page, 10, &new))
6389 return -EINVAL;
6390 new = !!new;
Shaohua Lid592a992014-05-21 17:57:44 +08006391
NeilBrown67918752014-12-15 12:57:01 +11006392 err = mddev_lock(mddev);
6393 if (err)
6394 return err;
6395 conf = mddev->private;
6396 if (!conf)
6397 err = -ENODEV;
6398 else if (new != conf->skip_copy) {
6399 mddev_suspend(mddev);
6400 conf->skip_copy = new;
6401 if (new)
Jan Karadc3b17c2017-02-02 15:56:50 +01006402 mddev->queue->backing_dev_info->capabilities |=
NeilBrown67918752014-12-15 12:57:01 +11006403 BDI_CAP_STABLE_WRITES;
6404 else
Jan Karadc3b17c2017-02-02 15:56:50 +01006405 mddev->queue->backing_dev_info->capabilities &=
NeilBrown67918752014-12-15 12:57:01 +11006406 ~BDI_CAP_STABLE_WRITES;
6407 mddev_resume(mddev);
6408 }
6409 mddev_unlock(mddev);
6410 return err ?: len;
Shaohua Lid592a992014-05-21 17:57:44 +08006411}
6412
6413static struct md_sysfs_entry
6414raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
6415 raid5_show_skip_copy,
6416 raid5_store_skip_copy);
6417
Shaohua Lid592a992014-05-21 17:57:44 +08006418static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006419stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006420{
NeilBrownd1688a62011-10-11 16:49:52 +11006421 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006422 if (conf)
6423 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
6424 else
6425 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08006426}
6427
NeilBrown96de1e62005-11-08 21:39:39 -08006428static struct md_sysfs_entry
6429raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08006430
Shaohua Lib7214202013-08-27 17:50:42 +08006431static ssize_t
6432raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
6433{
NeilBrown7b1485b2014-12-15 12:56:59 +11006434 struct r5conf *conf;
6435 int ret = 0;
6436 spin_lock(&mddev->lock);
6437 conf = mddev->private;
Shaohua Lib7214202013-08-27 17:50:42 +08006438 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006439 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
6440 spin_unlock(&mddev->lock);
6441 return ret;
Shaohua Lib7214202013-08-27 17:50:42 +08006442}
6443
majianpeng60aaf932013-11-14 15:16:20 +11006444static int alloc_thread_groups(struct r5conf *conf, int cnt,
6445 int *group_cnt,
6446 int *worker_cnt_per_group,
6447 struct r5worker_group **worker_groups);
Shaohua Lib7214202013-08-27 17:50:42 +08006448static ssize_t
6449raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
6450{
NeilBrown67918752014-12-15 12:57:01 +11006451 struct r5conf *conf;
Shaohua Lib7214202013-08-27 17:50:42 +08006452 unsigned long new;
6453 int err;
majianpeng60aaf932013-11-14 15:16:20 +11006454 struct r5worker_group *new_groups, *old_groups;
6455 int group_cnt, worker_cnt_per_group;
Shaohua Lib7214202013-08-27 17:50:42 +08006456
6457 if (len >= PAGE_SIZE)
6458 return -EINVAL;
Shaohua Lib7214202013-08-27 17:50:42 +08006459 if (kstrtoul(page, 10, &new))
6460 return -EINVAL;
6461
NeilBrown67918752014-12-15 12:57:01 +11006462 err = mddev_lock(mddev);
Shaohua Lib7214202013-08-27 17:50:42 +08006463 if (err)
6464 return err;
NeilBrown67918752014-12-15 12:57:01 +11006465 conf = mddev->private;
6466 if (!conf)
6467 err = -ENODEV;
6468 else if (new != conf->worker_cnt_per_group) {
6469 mddev_suspend(mddev);
6470
6471 old_groups = conf->worker_groups;
6472 if (old_groups)
6473 flush_workqueue(raid5_wq);
6474
6475 err = alloc_thread_groups(conf, new,
6476 &group_cnt, &worker_cnt_per_group,
6477 &new_groups);
6478 if (!err) {
6479 spin_lock_irq(&conf->device_lock);
6480 conf->group_cnt = group_cnt;
6481 conf->worker_cnt_per_group = worker_cnt_per_group;
6482 conf->worker_groups = new_groups;
6483 spin_unlock_irq(&conf->device_lock);
6484
6485 if (old_groups)
6486 kfree(old_groups[0].workers);
6487 kfree(old_groups);
6488 }
6489 mddev_resume(mddev);
6490 }
6491 mddev_unlock(mddev);
6492
6493 return err ?: len;
Shaohua Lib7214202013-08-27 17:50:42 +08006494}
6495
6496static struct md_sysfs_entry
6497raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
6498 raid5_show_group_thread_cnt,
6499 raid5_store_group_thread_cnt);
6500
NeilBrown007583c2005-11-08 21:39:30 -08006501static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08006502 &raid5_stripecache_size.attr,
6503 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006504 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08006505 &raid5_group_thread_cnt.attr,
Shaohua Lid592a992014-05-21 17:57:44 +08006506 &raid5_skip_copy.attr,
Markus Stockhausend06f1912014-12-15 12:57:05 +11006507 &raid5_rmw_level.attr,
Song Liu2c7da142016-11-17 15:24:41 -08006508 &r5c_journal_mode.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08006509 NULL,
6510};
NeilBrown007583c2005-11-08 21:39:30 -08006511static struct attribute_group raid5_attrs_group = {
6512 .name = NULL,
6513 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08006514};
6515
majianpeng60aaf932013-11-14 15:16:20 +11006516static int alloc_thread_groups(struct r5conf *conf, int cnt,
6517 int *group_cnt,
6518 int *worker_cnt_per_group,
6519 struct r5worker_group **worker_groups)
Shaohua Li851c30c2013-08-28 14:30:16 +08006520{
Shaohua Li566c09c2013-11-14 15:16:17 +11006521 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08006522 ssize_t size;
6523 struct r5worker *workers;
6524
majianpeng60aaf932013-11-14 15:16:20 +11006525 *worker_cnt_per_group = cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +08006526 if (cnt == 0) {
majianpeng60aaf932013-11-14 15:16:20 +11006527 *group_cnt = 0;
6528 *worker_groups = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08006529 return 0;
6530 }
majianpeng60aaf932013-11-14 15:16:20 +11006531 *group_cnt = num_possible_nodes();
Shaohua Li851c30c2013-08-28 14:30:16 +08006532 size = sizeof(struct r5worker) * cnt;
majianpeng60aaf932013-11-14 15:16:20 +11006533 workers = kzalloc(size * *group_cnt, GFP_NOIO);
6534 *worker_groups = kzalloc(sizeof(struct r5worker_group) *
6535 *group_cnt, GFP_NOIO);
6536 if (!*worker_groups || !workers) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006537 kfree(workers);
majianpeng60aaf932013-11-14 15:16:20 +11006538 kfree(*worker_groups);
Shaohua Li851c30c2013-08-28 14:30:16 +08006539 return -ENOMEM;
6540 }
6541
majianpeng60aaf932013-11-14 15:16:20 +11006542 for (i = 0; i < *group_cnt; i++) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006543 struct r5worker_group *group;
6544
NeilBrown0c775d52013-11-25 11:12:43 +11006545 group = &(*worker_groups)[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08006546 INIT_LIST_HEAD(&group->handle_list);
6547 group->conf = conf;
6548 group->workers = workers + i * cnt;
6549
6550 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11006551 struct r5worker *worker = group->workers + j;
6552 worker->group = group;
6553 INIT_WORK(&worker->work, raid5_do_work);
6554
6555 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
6556 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08006557 }
6558 }
6559
6560 return 0;
6561}
6562
6563static void free_thread_groups(struct r5conf *conf)
6564{
6565 if (conf->worker_groups)
6566 kfree(conf->worker_groups[0].workers);
6567 kfree(conf->worker_groups);
6568 conf->worker_groups = NULL;
6569}
6570
Dan Williams80c3a6c2009-03-17 18:10:40 -07006571static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11006572raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07006573{
NeilBrownd1688a62011-10-11 16:49:52 +11006574 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006575
6576 if (!sectors)
6577 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006578 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11006579 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11006580 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006581
NeilBrown3cb5edf2015-07-15 17:24:17 +10006582 sectors &= ~((sector_t)conf->chunk_sectors - 1);
6583 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006584 return sectors * (raid_disks - conf->max_degraded);
6585}
6586
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306587static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6588{
6589 safe_put_page(percpu->spare_page);
shli@kernel.org46d5b782014-12-15 12:57:02 +11006590 if (percpu->scribble)
6591 flex_array_free(percpu->scribble);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306592 percpu->spare_page = NULL;
6593 percpu->scribble = NULL;
6594}
6595
6596static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6597{
6598 if (conf->level == 6 && !percpu->spare_page)
6599 percpu->spare_page = alloc_page(GFP_KERNEL);
6600 if (!percpu->scribble)
shli@kernel.org46d5b782014-12-15 12:57:02 +11006601 percpu->scribble = scribble_alloc(max(conf->raid_disks,
NeilBrown738a2732015-05-08 18:19:39 +10006602 conf->previous_raid_disks),
6603 max(conf->chunk_sectors,
6604 conf->prev_chunk_sectors)
6605 / STRIPE_SECTORS,
6606 GFP_KERNEL);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306607
6608 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
6609 free_scratch_buffer(conf, percpu);
6610 return -ENOMEM;
6611 }
6612
6613 return 0;
6614}
6615
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006616static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
6617{
6618 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
6619
6620 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
6621 return 0;
6622}
6623
NeilBrownd1688a62011-10-11 16:49:52 +11006624static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006625{
Dan Williams36d1c642009-07-14 11:48:22 -07006626 if (!conf->percpu)
6627 return;
6628
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006629 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Dan Williams36d1c642009-07-14 11:48:22 -07006630 free_percpu(conf->percpu);
6631}
6632
NeilBrownd1688a62011-10-11 16:49:52 +11006633static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10006634{
Song Liud7bd3982016-11-23 22:50:39 -08006635 int i;
6636
Shaohua Li5c7e81c2015-08-13 14:32:04 -07006637 if (conf->log)
6638 r5l_exit_log(conf->log);
Shaohua Li30c89462016-09-21 09:07:13 -07006639 if (conf->shrinker.nr_deferred)
NeilBrownedbe83a2015-02-26 12:47:56 +11006640 unregister_shrinker(&conf->shrinker);
Shaohua Li5c7e81c2015-08-13 14:32:04 -07006641
Shaohua Li851c30c2013-08-28 14:30:16 +08006642 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10006643 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07006644 raid5_free_percpu(conf);
Song Liud7bd3982016-11-23 22:50:39 -08006645 for (i = 0; i < conf->pool_size; i++)
6646 if (conf->disks[i].extra_page)
6647 put_page(conf->disks[i].extra_page);
Dan Williams95fc17a2009-07-31 12:39:15 +10006648 kfree(conf->disks);
6649 kfree(conf->stripe_hashtbl);
6650 kfree(conf);
6651}
6652
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006653static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
Dan Williams36d1c642009-07-14 11:48:22 -07006654{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006655 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
Dan Williams36d1c642009-07-14 11:48:22 -07006656 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
6657
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006658 if (alloc_scratch_buffer(conf, percpu)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006659 pr_warn("%s: failed memory allocation for cpu%u\n",
6660 __func__, cpu);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006661 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006662 }
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006663 return 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006664}
Dan Williams36d1c642009-07-14 11:48:22 -07006665
NeilBrownd1688a62011-10-11 16:49:52 +11006666static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006667{
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306668 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006669
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306670 conf->percpu = alloc_percpu(struct raid5_percpu);
6671 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07006672 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006673
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006674 err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Shaohua Li27a353c2016-02-24 17:38:28 -08006675 if (!err) {
6676 conf->scribble_disks = max(conf->raid_disks,
6677 conf->previous_raid_disks);
6678 conf->scribble_sectors = max(conf->chunk_sectors,
6679 conf->prev_chunk_sectors);
6680 }
Dan Williams36d1c642009-07-14 11:48:22 -07006681 return err;
6682}
6683
NeilBrownedbe83a2015-02-26 12:47:56 +11006684static unsigned long raid5_cache_scan(struct shrinker *shrink,
6685 struct shrink_control *sc)
6686{
6687 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
NeilBrown2d5b5692015-07-06 12:49:23 +10006688 unsigned long ret = SHRINK_STOP;
6689
6690 if (mutex_trylock(&conf->cache_size_mutex)) {
6691 ret= 0;
NeilBrown49895bc2015-08-03 17:09:57 +10006692 while (ret < sc->nr_to_scan &&
6693 conf->max_nr_stripes > conf->min_nr_stripes) {
NeilBrown2d5b5692015-07-06 12:49:23 +10006694 if (drop_one_stripe(conf) == 0) {
6695 ret = SHRINK_STOP;
6696 break;
6697 }
6698 ret++;
6699 }
6700 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006701 }
6702 return ret;
6703}
6704
6705static unsigned long raid5_cache_count(struct shrinker *shrink,
6706 struct shrink_control *sc)
6707{
6708 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
6709
6710 if (conf->max_nr_stripes < conf->min_nr_stripes)
6711 /* unlikely, but not impossible */
6712 return 0;
6713 return conf->max_nr_stripes - conf->min_nr_stripes;
6714}
6715
NeilBrownd1688a62011-10-11 16:49:52 +11006716static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717{
NeilBrownd1688a62011-10-11 16:49:52 +11006718 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006719 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11006720 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10006722 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11006723 int i;
majianpeng60aaf932013-11-14 15:16:20 +11006724 int group_cnt, worker_cnt_per_group;
6725 struct r5worker_group *new_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726
NeilBrown91adb562009-03-31 14:39:39 +11006727 if (mddev->new_level != 5
6728 && mddev->new_level != 4
6729 && mddev->new_level != 6) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006730 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
6731 mdname(mddev), mddev->new_level);
NeilBrown91adb562009-03-31 14:39:39 +11006732 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733 }
NeilBrown91adb562009-03-31 14:39:39 +11006734 if ((mddev->new_level == 5
6735 && !algorithm_valid_raid5(mddev->new_layout)) ||
6736 (mddev->new_level == 6
6737 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006738 pr_warn("md/raid:%s: layout %d not supported\n",
6739 mdname(mddev), mddev->new_layout);
NeilBrown91adb562009-03-31 14:39:39 +11006740 return ERR_PTR(-EIO);
6741 }
6742 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006743 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
6744 mdname(mddev), mddev->raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006745 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11006746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747
Andre Noll664e7c42009-06-18 08:45:27 +10006748 if (!mddev->new_chunk_sectors ||
6749 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
6750 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006751 pr_warn("md/raid:%s: invalid chunk size %d\n",
6752 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11006753 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11006754 }
6755
NeilBrownd1688a62011-10-11 16:49:52 +11006756 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11006757 if (conf == NULL)
6758 goto abort;
Shaohua Li851c30c2013-08-28 14:30:16 +08006759 /* Don't enable multi-threading by default*/
majianpeng60aaf932013-11-14 15:16:20 +11006760 if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group,
6761 &new_group)) {
6762 conf->group_cnt = group_cnt;
6763 conf->worker_cnt_per_group = worker_cnt_per_group;
6764 conf->worker_groups = new_group;
6765 } else
Shaohua Li851c30c2013-08-28 14:30:16 +08006766 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11006767 spin_lock_init(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10006768 seqcount_init(&conf->gen_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006769 mutex_init(&conf->cache_size_mutex);
Yuanhan Liub1b46482015-05-08 18:19:06 +10006770 init_waitqueue_head(&conf->wait_for_quiescent);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08006771 init_waitqueue_head(&conf->wait_for_stripe);
Dan Williamsf5efd452009-10-16 15:55:38 +11006772 init_waitqueue_head(&conf->wait_for_overlap);
6773 INIT_LIST_HEAD(&conf->handle_list);
6774 INIT_LIST_HEAD(&conf->hold_list);
6775 INIT_LIST_HEAD(&conf->delayed_list);
6776 INIT_LIST_HEAD(&conf->bitmap_list);
NeilBrownc3cce6c2015-08-14 12:47:33 +10006777 bio_list_init(&conf->return_bi);
Shaohua Li773ca822013-08-27 17:50:39 +08006778 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11006779 atomic_set(&conf->active_stripes, 0);
6780 atomic_set(&conf->preread_active_stripes, 0);
6781 atomic_set(&conf->active_aligned_reads, 0);
Shaohua Li765d7042017-01-04 09:33:23 -08006782 bio_list_init(&conf->pending_bios);
6783 spin_lock_init(&conf->pending_bios_lock);
6784 conf->batch_bio_dispatch = true;
6785 rdev_for_each(rdev, mddev) {
6786 if (test_bit(Journal, &rdev->flags))
6787 continue;
6788 if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
6789 conf->batch_bio_dispatch = false;
6790 break;
6791 }
6792 }
6793
Dan Williamsf5efd452009-10-16 15:55:38 +11006794 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11006795 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11006796
6797 conf->raid_disks = mddev->raid_disks;
6798 if (mddev->reshape_position == MaxSector)
6799 conf->previous_raid_disks = mddev->raid_disks;
6800 else
6801 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006802 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006803
NeilBrown5e5e3e72009-10-16 16:35:30 +11006804 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11006805 GFP_KERNEL);
Song Liud7bd3982016-11-23 22:50:39 -08006806
NeilBrown91adb562009-03-31 14:39:39 +11006807 if (!conf->disks)
6808 goto abort;
6809
Song Liud7bd3982016-11-23 22:50:39 -08006810 for (i = 0; i < max_disks; i++) {
6811 conf->disks[i].extra_page = alloc_page(GFP_KERNEL);
6812 if (!conf->disks[i].extra_page)
6813 goto abort;
6814 }
6815
NeilBrown91adb562009-03-31 14:39:39 +11006816 conf->mddev = mddev;
6817
6818 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
6819 goto abort;
6820
Shaohua Li566c09c2013-11-14 15:16:17 +11006821 /* We init hash_locks[0] separately to that it can be used
6822 * as the reference lock in the spin_lock_nest_lock() call
6823 * in lock_all_device_hash_locks_irq in order to convince
6824 * lockdep that we know what we are doing.
6825 */
6826 spin_lock_init(conf->hash_locks);
6827 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
6828 spin_lock_init(conf->hash_locks + i);
6829
6830 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6831 INIT_LIST_HEAD(conf->inactive_list + i);
6832
6833 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6834 INIT_LIST_HEAD(conf->temp_inactive_list + i);
6835
Song Liu1e6d6902016-11-17 15:24:39 -08006836 atomic_set(&conf->r5c_cached_full_stripes, 0);
6837 INIT_LIST_HEAD(&conf->r5c_full_stripe_list);
6838 atomic_set(&conf->r5c_cached_partial_stripes, 0);
6839 INIT_LIST_HEAD(&conf->r5c_partial_stripe_list);
Shaohua Lie33fbb92017-02-10 16:18:09 -08006840 atomic_set(&conf->r5c_flushing_full_stripes, 0);
6841 atomic_set(&conf->r5c_flushing_partial_stripes, 0);
Song Liu1e6d6902016-11-17 15:24:39 -08006842
Dan Williams36d1c642009-07-14 11:48:22 -07006843 conf->level = mddev->new_level;
shli@kernel.org46d5b782014-12-15 12:57:02 +11006844 conf->chunk_sectors = mddev->new_chunk_sectors;
Dan Williams36d1c642009-07-14 11:48:22 -07006845 if (raid5_alloc_percpu(conf) != 0)
6846 goto abort;
6847
NeilBrown0c55e022010-05-03 14:09:02 +10006848 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11006849
NeilBrowndafb20f2012-03-19 12:46:39 +11006850 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11006851 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006852 if (raid_disk >= max_disks
Shaohua Lif2076e72015-10-08 21:54:12 -07006853 || raid_disk < 0 || test_bit(Journal, &rdev->flags))
NeilBrown91adb562009-03-31 14:39:39 +11006854 continue;
6855 disk = conf->disks + raid_disk;
6856
NeilBrown17045f52011-12-23 10:17:53 +11006857 if (test_bit(Replacement, &rdev->flags)) {
6858 if (disk->replacement)
6859 goto abort;
6860 disk->replacement = rdev;
6861 } else {
6862 if (disk->rdev)
6863 goto abort;
6864 disk->rdev = rdev;
6865 }
NeilBrown91adb562009-03-31 14:39:39 +11006866
6867 if (test_bit(In_sync, &rdev->flags)) {
6868 char b[BDEVNAME_SIZE];
NeilBrowncc6167b2016-11-02 14:16:50 +11006869 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
6870 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05006871 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11006872 /* Cannot rely on bitmap to complete recovery */
6873 conf->fullsync = 1;
6874 }
6875
NeilBrown91adb562009-03-31 14:39:39 +11006876 conf->level = mddev->new_level;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006877 if (conf->level == 6) {
NeilBrown91adb562009-03-31 14:39:39 +11006878 conf->max_degraded = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006879 if (raid6_call.xor_syndrome)
6880 conf->rmw_level = PARITY_ENABLE_RMW;
6881 else
6882 conf->rmw_level = PARITY_DISABLE_RMW;
6883 } else {
NeilBrown91adb562009-03-31 14:39:39 +11006884 conf->max_degraded = 1;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006885 conf->rmw_level = PARITY_ENABLE_RMW;
6886 }
NeilBrown91adb562009-03-31 14:39:39 +11006887 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11006888 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11006889 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10006890 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11006891 conf->prev_algo = mddev->layout;
NeilBrown5cac6bc2015-07-17 12:17:50 +10006892 } else {
6893 conf->prev_chunk_sectors = conf->chunk_sectors;
6894 conf->prev_algo = conf->algorithm;
NeilBrowne183eae2009-03-31 15:20:22 +11006895 }
NeilBrown91adb562009-03-31 14:39:39 +11006896
NeilBrownedbe83a2015-02-26 12:47:56 +11006897 conf->min_nr_stripes = NR_STRIPES;
Shaohua Liad5b0f72016-08-30 10:29:33 -07006898 if (mddev->reshape_position != MaxSector) {
6899 int stripes = max_t(int,
6900 ((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4,
6901 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4);
6902 conf->min_nr_stripes = max(NR_STRIPES, stripes);
6903 if (conf->min_nr_stripes != NR_STRIPES)
NeilBrowncc6167b2016-11-02 14:16:50 +11006904 pr_info("md/raid:%s: force stripe size %d for reshape\n",
Shaohua Liad5b0f72016-08-30 10:29:33 -07006905 mdname(mddev), conf->min_nr_stripes);
6906 }
NeilBrownedbe83a2015-02-26 12:47:56 +11006907 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11006908 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li4bda5562013-11-14 15:16:17 +11006909 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
NeilBrownedbe83a2015-02-26 12:47:56 +11006910 if (grow_stripes(conf, conf->min_nr_stripes)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006911 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
6912 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11006913 goto abort;
6914 } else
NeilBrowncc6167b2016-11-02 14:16:50 +11006915 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory);
NeilBrownedbe83a2015-02-26 12:47:56 +11006916 /*
6917 * Losing a stripe head costs more than the time to refill it,
6918 * it reduces the queue depth and so can hurt throughput.
6919 * So set it rather large, scaled by number of devices.
6920 */
6921 conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
6922 conf->shrinker.scan_objects = raid5_cache_scan;
6923 conf->shrinker.count_objects = raid5_cache_count;
6924 conf->shrinker.batch = 128;
6925 conf->shrinker.flags = 0;
Chao Yu6a0f53f2016-09-20 10:33:57 +08006926 if (register_shrinker(&conf->shrinker)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006927 pr_warn("md/raid:%s: couldn't register shrinker.\n",
6928 mdname(mddev));
Chao Yu6a0f53f2016-09-20 10:33:57 +08006929 goto abort;
6930 }
NeilBrown91adb562009-03-31 14:39:39 +11006931
NeilBrown02326052012-07-03 15:56:52 +10006932 sprintf(pers_name, "raid%d", mddev->new_level);
6933 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11006934 if (!conf->thread) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006935 pr_warn("md/raid:%s: couldn't allocate thread.\n",
6936 mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11006937 goto abort;
6938 }
6939
6940 return conf;
6941
6942 abort:
6943 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10006944 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11006945 return ERR_PTR(-EIO);
6946 } else
6947 return ERR_PTR(-ENOMEM);
6948}
6949
NeilBrownc148ffd2009-11-13 17:47:00 +11006950static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
6951{
6952 switch (algo) {
6953 case ALGORITHM_PARITY_0:
6954 if (raid_disk < max_degraded)
6955 return 1;
6956 break;
6957 case ALGORITHM_PARITY_N:
6958 if (raid_disk >= raid_disks - max_degraded)
6959 return 1;
6960 break;
6961 case ALGORITHM_PARITY_0_6:
NeilBrownf72ffdd2014-09-30 14:23:59 +10006962 if (raid_disk == 0 ||
NeilBrownc148ffd2009-11-13 17:47:00 +11006963 raid_disk == raid_disks - 1)
6964 return 1;
6965 break;
6966 case ALGORITHM_LEFT_ASYMMETRIC_6:
6967 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6968 case ALGORITHM_LEFT_SYMMETRIC_6:
6969 case ALGORITHM_RIGHT_SYMMETRIC_6:
6970 if (raid_disk == raid_disks - 1)
6971 return 1;
6972 }
6973 return 0;
6974}
6975
Shaohua Li849674e2016-01-20 13:52:20 -08006976static int raid5_run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11006977{
NeilBrownd1688a62011-10-11 16:49:52 +11006978 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10006979 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11006980 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006981 struct md_rdev *rdev;
Shaohua Li713cf5a2015-08-13 14:32:03 -07006982 struct md_rdev *journal_dev = NULL;
NeilBrownc148ffd2009-11-13 17:47:00 +11006983 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11006984 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10006985 long long min_offset_diff = 0;
6986 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11006987
Andre Noll8c6ac862009-06-18 08:48:06 +10006988 if (mddev->recovery_cp != MaxSector)
NeilBrowncc6167b2016-11-02 14:16:50 +11006989 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
6990 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10006991
6992 rdev_for_each(rdev, mddev) {
6993 long long diff;
Shaohua Li713cf5a2015-08-13 14:32:03 -07006994
Shaohua Lif2076e72015-10-08 21:54:12 -07006995 if (test_bit(Journal, &rdev->flags)) {
Shaohua Li713cf5a2015-08-13 14:32:03 -07006996 journal_dev = rdev;
Shaohua Lif2076e72015-10-08 21:54:12 -07006997 continue;
6998 }
NeilBrownb5254dd2012-05-21 09:27:01 +10006999 if (rdev->raid_disk < 0)
7000 continue;
7001 diff = (rdev->new_data_offset - rdev->data_offset);
7002 if (first) {
7003 min_offset_diff = diff;
7004 first = 0;
7005 } else if (mddev->reshape_backwards &&
7006 diff < min_offset_diff)
7007 min_offset_diff = diff;
7008 else if (!mddev->reshape_backwards &&
7009 diff > min_offset_diff)
7010 min_offset_diff = diff;
7011 }
7012
NeilBrownf6705572006-03-27 01:18:11 -08007013 if (mddev->reshape_position != MaxSector) {
7014 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10007015 * Difficulties arise if the stripe we would write to
7016 * next is at or after the stripe we would read from next.
7017 * For a reshape that changes the number of devices, this
7018 * is only possible for a very short time, and mdadm makes
7019 * sure that time appears to have past before assembling
7020 * the array. So we fail if that time hasn't passed.
7021 * For a reshape that keeps the number of devices the same
7022 * mdadm must be monitoring the reshape can keeping the
7023 * critical areas read-only and backed up. It will start
7024 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08007025 */
7026 sector_t here_new, here_old;
7027 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11007028 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrown05256d92015-07-15 17:36:21 +10007029 int chunk_sectors;
7030 int new_data_disks;
NeilBrownf6705572006-03-27 01:18:11 -08007031
Shaohua Li713cf5a2015-08-13 14:32:03 -07007032 if (journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007033 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
7034 mdname(mddev));
Shaohua Li713cf5a2015-08-13 14:32:03 -07007035 return -EINVAL;
7036 }
7037
NeilBrown88ce4932009-03-31 15:24:23 +11007038 if (mddev->new_level != mddev->level) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007039 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
7040 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007041 return -EINVAL;
7042 }
NeilBrownf6705572006-03-27 01:18:11 -08007043 old_disks = mddev->raid_disks - mddev->delta_disks;
7044 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08007045 * further up in new geometry must map after here in old
7046 * geometry.
NeilBrown05256d92015-07-15 17:36:21 +10007047 * If the chunk sizes are different, then as we perform reshape
7048 * in units of the largest of the two, reshape_position needs
7049 * be a multiple of the largest chunk size times new data disks.
NeilBrownf6705572006-03-27 01:18:11 -08007050 */
7051 here_new = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007052 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
7053 new_data_disks = mddev->raid_disks - max_degraded;
7054 if (sector_div(here_new, chunk_sectors * new_data_disks)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007055 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
7056 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007057 return -EINVAL;
7058 }
NeilBrown05256d92015-07-15 17:36:21 +10007059 reshape_offset = here_new * chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08007060 /* here_new is the stripe we will write to */
7061 here_old = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007062 sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
NeilBrownf4168852007-02-28 20:11:53 -08007063 /* here_old is the first stripe that we might need to read
7064 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10007065 if (mddev->delta_disks == 0) {
7066 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10007067 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10007068 * and taking constant backups.
7069 * mdadm always starts a situation like this in
7070 * readonly mode so it can take control before
7071 * allowing any writes. So just check for that.
7072 */
NeilBrownb5254dd2012-05-21 09:27:01 +10007073 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
7074 abs(min_offset_diff) >= mddev->new_chunk_sectors)
7075 /* not really in-place - so OK */;
7076 else if (mddev->ro == 0) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007077 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
7078 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10007079 return -EINVAL;
7080 }
NeilBrown2c810cd2012-05-21 09:27:00 +10007081 } else if (mddev->reshape_backwards
NeilBrown05256d92015-07-15 17:36:21 +10007082 ? (here_new * chunk_sectors + min_offset_diff <=
7083 here_old * chunk_sectors)
7084 : (here_new * chunk_sectors >=
7085 here_old * chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08007086 /* Reading from the same stripe as writing to - bad */
NeilBrowncc6167b2016-11-02 14:16:50 +11007087 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
7088 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007089 return -EINVAL;
7090 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007091 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007092 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08007093 } else {
NeilBrown91adb562009-03-31 14:39:39 +11007094 BUG_ON(mddev->level != mddev->new_level);
7095 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10007096 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11007097 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08007098 }
7099
NeilBrown245f46c2009-03-31 14:39:39 +11007100 if (mddev->private == NULL)
7101 conf = setup_conf(mddev);
7102 else
7103 conf = mddev->private;
7104
NeilBrown91adb562009-03-31 14:39:39 +11007105 if (IS_ERR(conf))
7106 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08007107
Song Liu486b0f72016-08-19 15:34:01 -07007108 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
7109 if (!journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007110 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
7111 mdname(mddev));
Song Liu486b0f72016-08-19 15:34:01 -07007112 mddev->ro = 1;
7113 set_disk_ro(mddev->gendisk, 1);
7114 } else if (mddev->recovery_cp == MaxSector)
7115 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
Shaohua Li7dde2ad2015-10-08 21:54:10 -07007116 }
7117
NeilBrownb5254dd2012-05-21 09:27:01 +10007118 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11007119 mddev->thread = conf->thread;
7120 conf->thread = NULL;
7121 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122
NeilBrown17045f52011-12-23 10:17:53 +11007123 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
7124 i++) {
7125 rdev = conf->disks[i].rdev;
7126 if (!rdev && conf->disks[i].replacement) {
7127 /* The replacement is all we have yet */
7128 rdev = conf->disks[i].replacement;
7129 conf->disks[i].replacement = NULL;
7130 clear_bit(Replacement, &rdev->flags);
7131 conf->disks[i].rdev = rdev;
7132 }
7133 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11007134 continue;
NeilBrown17045f52011-12-23 10:17:53 +11007135 if (conf->disks[i].replacement &&
7136 conf->reshape_progress != MaxSector) {
7137 /* replacements and reshape simply do not mix. */
NeilBrowncc6167b2016-11-02 14:16:50 +11007138 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
NeilBrown17045f52011-12-23 10:17:53 +11007139 goto abort;
7140 }
NeilBrown2f115882010-06-17 17:41:03 +10007141 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11007142 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10007143 continue;
7144 }
NeilBrownc148ffd2009-11-13 17:47:00 +11007145 /* This disc is not fully in-sync. However if it
7146 * just stored parity (beyond the recovery_offset),
7147 * when we don't need to be concerned about the
7148 * array being dirty.
7149 * When reshape goes 'backwards', we never have
7150 * partially completed devices, so we only need
7151 * to worry about reshape going forwards.
7152 */
7153 /* Hack because v0.91 doesn't store recovery_offset properly. */
7154 if (mddev->major_version == 0 &&
7155 mddev->minor_version > 90)
7156 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007157
NeilBrownc148ffd2009-11-13 17:47:00 +11007158 if (rdev->recovery_offset < reshape_offset) {
7159 /* We need to check old and new layout */
7160 if (!only_parity(rdev->raid_disk,
7161 conf->algorithm,
7162 conf->raid_disks,
7163 conf->max_degraded))
7164 continue;
7165 }
7166 if (!only_parity(rdev->raid_disk,
7167 conf->prev_algo,
7168 conf->previous_raid_disks,
7169 conf->max_degraded))
7170 continue;
7171 dirty_parity_disks++;
7172 }
NeilBrown91adb562009-03-31 14:39:39 +11007173
NeilBrown17045f52011-12-23 10:17:53 +11007174 /*
7175 * 0 for a fully functional array, 1 or 2 for a degraded array.
7176 */
Song Liu2e38a372017-01-24 10:45:30 -08007177 mddev->degraded = raid5_calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178
NeilBrown674806d2010-06-16 17:17:53 +10007179 if (has_failed(conf)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007180 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07007181 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182 goto abort;
7183 }
7184
NeilBrown91adb562009-03-31 14:39:39 +11007185 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10007186 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11007187 mddev->resync_max_sectors = mddev->dev_sectors;
7188
NeilBrownc148ffd2009-11-13 17:47:00 +11007189 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007191 if (mddev->ok_start_degraded)
NeilBrowncc6167b2016-11-02 14:16:50 +11007192 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
7193 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007194 else {
NeilBrowncc6167b2016-11-02 14:16:50 +11007195 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
7196 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007197 goto abort;
7198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 }
7200
NeilBrowncc6167b2016-11-02 14:16:50 +11007201 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
7202 mdname(mddev), conf->level,
7203 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
7204 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205
7206 print_raid5_conf(conf);
7207
NeilBrownfef9c612009-03-31 15:16:46 +11007208 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11007209 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08007210 atomic_set(&conf->reshape_stripes, 0);
7211 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7212 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7213 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7214 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7215 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007216 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08007217 }
7218
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10007220 if (mddev->to_remove == &raid5_attrs_group)
7221 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007222 else if (mddev->kobj.sd &&
7223 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrowncc6167b2016-11-02 14:16:50 +11007224 pr_warn("raid5: failed to create sysfs attributes for %s\n",
7225 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10007226 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
7227
7228 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007229 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11007230 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10007231 /* read-ahead size must cover two whole stripes, which
7232 * is 2 * (datadisks) * chunksize where 'n' is the
7233 * number of raid devices
7234 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235 int data_disks = conf->previous_raid_disks - conf->max_degraded;
7236 int stripe = data_disks *
7237 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Jan Karadc3b17c2017-02-02 15:56:50 +01007238 if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
7239 mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10007240
NeilBrown9f7c2222010-07-26 12:04:13 +10007241 chunk_size = mddev->chunk_sectors << 9;
7242 blk_queue_io_min(mddev->queue, chunk_size);
7243 blk_queue_io_opt(mddev->queue, chunk_size *
7244 (conf->raid_disks - conf->max_degraded));
Kent Overstreetc78afc62013-07-11 22:39:53 -07007245 mddev->queue->limits.raid_partial_stripes_expensive = 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007246 /*
7247 * We can only discard a whole stripe. It doesn't make sense to
7248 * discard data disk but write parity disk
7249 */
7250 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11007251 /* Round up to power of 2, as discard handling
7252 * currently assumes that */
7253 while ((stripe-1) & stripe)
7254 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007255 mddev->queue->limits.discard_alignment = stripe;
7256 mddev->queue->limits.discard_granularity = stripe;
Konstantin Khlebnikove8d7c332016-11-27 19:32:32 +03007257
7258 /*
7259 * We use 16-bit counter of active stripes in bi_phys_segments
7260 * (minus one for over-loaded initialization)
7261 */
7262 blk_queue_max_hw_sectors(mddev->queue, 0xfffe * STRIPE_SECTORS);
7263 blk_queue_max_discard_sectors(mddev->queue,
7264 0xfffe * STRIPE_SECTORS);
7265
Shaohua Li620125f2012-10-11 13:49:05 +11007266 /*
7267 * unaligned part of discard request will be ignored, so can't
NeilBrown8e0e99b2014-10-02 13:45:00 +10007268 * guarantee discard_zeroes_data
Shaohua Li620125f2012-10-11 13:49:05 +11007269 */
7270 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10007271
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007272 blk_queue_max_write_same_sectors(mddev->queue, 0);
7273
NeilBrown05616be2012-05-21 09:27:00 +10007274 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007275 disk_stack_limits(mddev->gendisk, rdev->bdev,
7276 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10007277 disk_stack_limits(mddev->gendisk, rdev->bdev,
7278 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11007279 /*
7280 * discard_zeroes_data is required, otherwise data
7281 * could be lost. Consider a scenario: discard a stripe
7282 * (the stripe could be inconsistent if
7283 * discard_zeroes_data is 0); write one disk of the
7284 * stripe (the stripe could be inconsistent again
7285 * depending on which disks are used to calculate
7286 * parity); the disk is broken; The stripe data of this
7287 * disk is lost.
7288 */
7289 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
7290 !bdev_get_queue(rdev->bdev)->
7291 limits.discard_zeroes_data)
7292 discard_supported = false;
NeilBrown8e0e99b2014-10-02 13:45:00 +10007293 /* Unfortunately, discard_zeroes_data is not currently
7294 * a guarantee - just a hint. So we only allow DISCARD
7295 * if the sysadmin has confirmed that only safe devices
7296 * are in use by setting a module parameter.
7297 */
7298 if (!devices_handle_discard_safely) {
7299 if (discard_supported) {
7300 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
7301 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
7302 }
7303 discard_supported = false;
7304 }
NeilBrown05616be2012-05-21 09:27:00 +10007305 }
Shaohua Li620125f2012-10-11 13:49:05 +11007306
7307 if (discard_supported &&
Jes Sorensene7597e62016-02-16 16:44:24 -05007308 mddev->queue->limits.max_discard_sectors >= (stripe >> 9) &&
7309 mddev->queue->limits.discard_granularity >= stripe)
Shaohua Li620125f2012-10-11 13:49:05 +11007310 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
7311 mddev->queue);
7312 else
7313 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
7314 mddev->queue);
Shaohua Li1dffddd2016-09-08 10:49:06 -07007315
7316 blk_queue_max_hw_sectors(mddev->queue, UINT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317 }
7318
Shaohua Li5c7e81c2015-08-13 14:32:04 -07007319 if (journal_dev) {
7320 char b[BDEVNAME_SIZE];
7321
NeilBrowncc6167b2016-11-02 14:16:50 +11007322 pr_debug("md/raid:%s: using device %s as journal\n",
7323 mdname(mddev), bdevname(journal_dev->bdev, b));
Song Liu5aabf7c2016-11-17 15:24:44 -08007324 if (r5l_init_log(conf, journal_dev))
7325 goto abort;
Shaohua Li5c7e81c2015-08-13 14:32:04 -07007326 }
7327
Linus Torvalds1da177e2005-04-16 15:20:36 -07007328 return 0;
7329abort:
NeilBrown01f96c02011-09-21 15:30:20 +10007330 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11007331 print_raid5_conf(conf);
7332 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333 mddev->private = NULL;
NeilBrowncc6167b2016-11-02 14:16:50 +11007334 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335 return -EIO;
7336}
7337
NeilBrownafa0f552014-12-15 12:56:58 +11007338static void raid5_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339{
NeilBrownafa0f552014-12-15 12:56:58 +11007340 struct r5conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007341
Dan Williams95fc17a2009-07-31 12:39:15 +10007342 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10007343 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007344}
7345
Shaohua Li849674e2016-01-20 13:52:20 -08007346static void raid5_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007347{
NeilBrownd1688a62011-10-11 16:49:52 +11007348 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 int i;
7350
Andre Noll9d8f0362009-06-18 08:45:01 +10007351 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
NeilBrown3cb5edf2015-07-15 17:24:17 +10007352 conf->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07007353 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
NeilBrown5fd13352016-06-02 16:19:52 +10007354 rcu_read_lock();
7355 for (i = 0; i < conf->raid_disks; i++) {
7356 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
7357 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
7358 }
7359 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361}
7362
NeilBrownd1688a62011-10-11 16:49:52 +11007363static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364{
7365 int i;
7366 struct disk_info *tmp;
7367
NeilBrowncc6167b2016-11-02 14:16:50 +11007368 pr_debug("RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007369 if (!conf) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007370 pr_debug("(conf==NULL)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371 return;
7372 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007373 pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level,
NeilBrown0c55e022010-05-03 14:09:02 +10007374 conf->raid_disks,
7375 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376
7377 for (i = 0; i < conf->raid_disks; i++) {
7378 char b[BDEVNAME_SIZE];
7379 tmp = conf->disks + i;
7380 if (tmp->rdev)
NeilBrowncc6167b2016-11-02 14:16:50 +11007381 pr_debug(" disk %d, o:%d, dev:%s\n",
NeilBrown0c55e022010-05-03 14:09:02 +10007382 i, !test_bit(Faulty, &tmp->rdev->flags),
7383 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007384 }
7385}
7386
NeilBrownfd01b882011-10-11 16:47:53 +11007387static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007388{
7389 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11007390 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10007392 int count = 0;
7393 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007394
7395 for (i = 0; i < conf->raid_disks; i++) {
7396 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11007397 if (tmp->replacement
7398 && tmp->replacement->recovery_offset == MaxSector
7399 && !test_bit(Faulty, &tmp->replacement->flags)
7400 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
7401 /* Replacement has just become active. */
7402 if (!tmp->rdev
7403 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
7404 count++;
7405 if (tmp->rdev) {
7406 /* Replaced device not technically faulty,
7407 * but we need to be sure it gets removed
7408 * and never re-added.
7409 */
7410 set_bit(Faulty, &tmp->rdev->flags);
7411 sysfs_notify_dirent_safe(
7412 tmp->rdev->sysfs_state);
7413 }
7414 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
7415 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10007416 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08007417 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07007418 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10007419 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11007420 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007421 }
7422 }
NeilBrown6b965622010-08-18 11:56:59 +10007423 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08007424 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007425 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007427 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007428}
7429
NeilBrownb8321b62011-12-23 10:17:51 +11007430static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431{
NeilBrownd1688a62011-10-11 16:49:52 +11007432 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11007434 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11007435 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007436 struct disk_info *p = conf->disks + number;
7437
7438 print_raid5_conf(conf);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007439 if (test_bit(Journal, &rdev->flags) && conf->log) {
7440 struct r5l_log *log;
Shaohua Lic2bb6242015-10-08 21:54:07 -07007441 /*
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007442 * we can't wait pending write here, as this is called in
7443 * raid5d, wait will deadlock.
Shaohua Lic2bb6242015-10-08 21:54:07 -07007444 */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007445 if (atomic_read(&mddev->writes_pending))
7446 return -EBUSY;
7447 log = conf->log;
7448 conf->log = NULL;
7449 synchronize_rcu();
7450 r5l_exit_log(log);
7451 return 0;
Shaohua Lic2bb6242015-10-08 21:54:07 -07007452 }
NeilBrown657e3e42011-12-23 10:17:52 +11007453 if (rdev == p->rdev)
7454 rdevp = &p->rdev;
7455 else if (rdev == p->replacement)
7456 rdevp = &p->replacement;
7457 else
7458 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11007459
NeilBrown657e3e42011-12-23 10:17:52 +11007460 if (number >= conf->raid_disks &&
7461 conf->reshape_progress == MaxSector)
7462 clear_bit(In_sync, &rdev->flags);
7463
7464 if (test_bit(In_sync, &rdev->flags) ||
7465 atomic_read(&rdev->nr_pending)) {
7466 err = -EBUSY;
7467 goto abort;
7468 }
7469 /* Only remove non-faulty devices if recovery
7470 * isn't possible.
7471 */
7472 if (!test_bit(Faulty, &rdev->flags) &&
7473 mddev->recovery_disabled != conf->recovery_disabled &&
7474 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11007475 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11007476 number < conf->raid_disks) {
7477 err = -EBUSY;
7478 goto abort;
7479 }
7480 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10007481 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
7482 synchronize_rcu();
7483 if (atomic_read(&rdev->nr_pending)) {
7484 /* lost the race, try later */
7485 err = -EBUSY;
7486 *rdevp = rdev;
7487 }
7488 }
7489 if (p->replacement) {
NeilBrowndd054fc2011-12-23 10:17:53 +11007490 /* We must have just cleared 'rdev' */
7491 p->rdev = p->replacement;
7492 clear_bit(Replacement, &p->replacement->flags);
7493 smp_mb(); /* Make sure other CPUs may see both as identical
7494 * but will never see neither - if they are careful
7495 */
7496 p->replacement = NULL;
7497 clear_bit(WantReplacement, &rdev->flags);
7498 } else
7499 /* We might have just removed the Replacement as faulty-
7500 * clear the bit just in case
7501 */
7502 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503abort:
7504
7505 print_raid5_conf(conf);
7506 return err;
7507}
7508
NeilBrownfd01b882011-10-11 16:47:53 +11007509static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007510{
NeilBrownd1688a62011-10-11 16:49:52 +11007511 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10007512 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007513 int disk;
7514 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10007515 int first = 0;
7516 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007517
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007518 if (test_bit(Journal, &rdev->flags)) {
7519 char b[BDEVNAME_SIZE];
7520 if (conf->log)
7521 return -EBUSY;
7522
7523 rdev->raid_disk = 0;
7524 /*
7525 * The array is in readonly mode if journal is missing, so no
7526 * write requests running. We should be safe
7527 */
7528 r5l_init_log(conf, rdev);
NeilBrowncc6167b2016-11-02 14:16:50 +11007529 pr_debug("md/raid:%s: using device %s as journal\n",
7530 mdname(mddev), bdevname(rdev->bdev, b));
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007531 return 0;
7532 }
NeilBrown7f0da592011-07-28 11:39:22 +10007533 if (mddev->recovery_disabled == conf->recovery_disabled)
7534 return -EBUSY;
7535
NeilBrowndc10c642012-03-19 12:46:37 +11007536 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10007538 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539
Neil Brown6c2fce22008-06-28 08:31:31 +10007540 if (rdev->raid_disk >= 0)
7541 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542
7543 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07007544 * find the disk ... but prefer rdev->saved_raid_disk
7545 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546 */
NeilBrown16a53ec2006-06-26 00:27:38 -07007547 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10007548 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07007549 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10007550 first = rdev->saved_raid_disk;
7551
7552 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007553 p = conf->disks + disk;
7554 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08007555 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10007557 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07007558 if (rdev->saved_raid_disk != disk)
7559 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08007560 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10007561 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007563 }
7564 for (disk = first; disk <= last; disk++) {
7565 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007566 if (test_bit(WantReplacement, &p->rdev->flags) &&
7567 p->replacement == NULL) {
7568 clear_bit(In_sync, &rdev->flags);
7569 set_bit(Replacement, &rdev->flags);
7570 rdev->raid_disk = disk;
7571 err = 0;
7572 conf->fullsync = 1;
7573 rcu_assign_pointer(p->replacement, rdev);
7574 break;
7575 }
7576 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007577out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10007579 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007580}
7581
NeilBrownfd01b882011-10-11 16:47:53 +11007582static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583{
7584 /* no resync is happening, and there is enough space
7585 * on all devices, so we can resize.
7586 * We need to make sure resync covers any new space.
7587 * If the array is shrinking we should possibly wait until
7588 * any io in the removed space completes, but it hardly seems
7589 * worth it.
7590 */
NeilBrowna4a61252012-05-22 13:55:27 +10007591 sector_t newsize;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007592 struct r5conf *conf = mddev->private;
7593
Shaohua Li713cf5a2015-08-13 14:32:03 -07007594 if (conf->log)
7595 return -EINVAL;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007596 sectors &= ~((sector_t)conf->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10007597 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
7598 if (mddev->external_size &&
7599 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11007600 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10007601 if (mddev->bitmap) {
7602 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
7603 if (ret)
7604 return ret;
7605 }
7606 md_set_array_sectors(mddev, newsize);
NeilBrownb0986362011-05-11 15:52:21 +10007607 if (sectors > mddev->dev_sectors &&
7608 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11007609 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7611 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007612 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07007613 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007614 return 0;
7615}
7616
NeilBrownfd01b882011-10-11 16:47:53 +11007617static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10007618{
7619 /* Can only proceed if there are plenty of stripe_heads.
7620 * We need a minimum of one full stripe,, and for sensible progress
7621 * it is best to have about 4 times that.
7622 * If we require 4 times, then the default 256 4K stripe_heads will
7623 * allow for chunk sizes up to 256K, which is probably OK.
7624 * If the chunk size is greater, user-space should request more
7625 * stripe_heads first.
7626 */
NeilBrownd1688a62011-10-11 16:49:52 +11007627 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10007628 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007629 > conf->min_nr_stripes ||
NeilBrown01ee22b2009-06-18 08:47:20 +10007630 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007631 > conf->min_nr_stripes) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007632 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7633 mdname(mddev),
7634 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
7635 / STRIPE_SIZE)*4);
NeilBrown01ee22b2009-06-18 08:47:20 +10007636 return 0;
7637 }
7638 return 1;
7639}
7640
NeilBrownfd01b882011-10-11 16:47:53 +11007641static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08007642{
NeilBrownd1688a62011-10-11 16:49:52 +11007643 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08007644
Shaohua Li713cf5a2015-08-13 14:32:03 -07007645 if (conf->log)
7646 return -EINVAL;
NeilBrown88ce4932009-03-31 15:24:23 +11007647 if (mddev->delta_disks == 0 &&
7648 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10007649 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10007650 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10007651 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11007652 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10007653 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11007654 /* We might be able to shrink, but the devices must
7655 * be made bigger first.
7656 * For raid6, 4 is the minimum size.
7657 * Otherwise 2 is the minimum
7658 */
7659 int min = 2;
7660 if (mddev->level == 6)
7661 min = 4;
7662 if (mddev->raid_disks + mddev->delta_disks < min)
7663 return -EINVAL;
7664 }
NeilBrown29269552006-03-27 01:18:10 -08007665
NeilBrown01ee22b2009-06-18 08:47:20 +10007666 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08007667 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08007668
NeilBrown738a2732015-05-08 18:19:39 +10007669 if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
7670 mddev->delta_disks > 0)
7671 if (resize_chunks(conf,
7672 conf->previous_raid_disks
7673 + max(0, mddev->delta_disks),
7674 max(mddev->new_chunk_sectors,
7675 mddev->chunk_sectors)
7676 ) < 0)
7677 return -ENOMEM;
NeilBrowne56108d62012-10-11 14:24:13 +11007678 return resize_stripes(conf, (conf->previous_raid_disks
7679 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08007680}
7681
NeilBrownfd01b882011-10-11 16:47:53 +11007682static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08007683{
NeilBrownd1688a62011-10-11 16:49:52 +11007684 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11007685 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08007686 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07007687 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08007688
NeilBrownf4168852007-02-28 20:11:53 -08007689 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08007690 return -EBUSY;
7691
NeilBrown01ee22b2009-06-18 08:47:20 +10007692 if (!check_stripe_cache(mddev))
7693 return -ENOSPC;
7694
NeilBrown30b67642012-05-22 13:55:28 +10007695 if (has_failed(conf))
7696 return -EINVAL;
7697
NeilBrownc6563a82012-05-21 09:27:00 +10007698 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11007699 if (!test_bit(In_sync, &rdev->flags)
7700 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08007701 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10007702 }
NeilBrown63c70c42006-03-27 01:18:13 -08007703
NeilBrownf4168852007-02-28 20:11:53 -08007704 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08007705 /* Not enough devices even to make a degraded array
7706 * of that size
7707 */
7708 return -EINVAL;
7709
NeilBrownec32a2b2009-03-31 15:17:38 +11007710 /* Refuse to reduce size of the array. Any reductions in
7711 * array size must be through explicit setting of array_size
7712 * attribute.
7713 */
7714 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
7715 < mddev->array_sectors) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007716 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
7717 mdname(mddev));
NeilBrownec32a2b2009-03-31 15:17:38 +11007718 return -EINVAL;
7719 }
7720
NeilBrownf6705572006-03-27 01:18:11 -08007721 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08007722 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10007723 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007724 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08007725 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007726 conf->prev_chunk_sectors = conf->chunk_sectors;
7727 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11007728 conf->prev_algo = conf->algorithm;
7729 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10007730 conf->generation++;
7731 /* Code that selects data_offset needs to see the generation update
7732 * if reshape_progress has been set - so a memory barrier needed.
7733 */
7734 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10007735 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11007736 conf->reshape_progress = raid5_size(mddev, 0, 0);
7737 else
7738 conf->reshape_progress = 0;
7739 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10007740 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007741 spin_unlock_irq(&conf->device_lock);
7742
NeilBrown4d77e3b2013-08-27 15:57:47 +10007743 /* Now make sure any requests that proceeded on the assumption
7744 * the reshape wasn't running - like Discard or Read - have
7745 * completed.
7746 */
7747 mddev_suspend(mddev);
7748 mddev_resume(mddev);
7749
NeilBrown29269552006-03-27 01:18:10 -08007750 /* Add some new drives, as many as will fit.
7751 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10007752 * Don't add devices if we are reducing the number of
7753 * devices in the array. This is because it is not possible
7754 * to correctly record the "partially reconstructed" state of
7755 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08007756 */
NeilBrown87a8dec2011-01-31 11:57:43 +11007757 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11007758 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11007759 if (rdev->raid_disk < 0 &&
7760 !test_bit(Faulty, &rdev->flags)) {
7761 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11007762 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11007763 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11007764 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11007765 else
NeilBrown87a8dec2011-01-31 11:57:43 +11007766 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10007767
7768 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11007769 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11007770 }
NeilBrown87a8dec2011-01-31 11:57:43 +11007771 } else if (rdev->raid_disk >= conf->previous_raid_disks
7772 && !test_bit(Faulty, &rdev->flags)) {
7773 /* This is a spare that was manually added */
7774 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11007775 }
NeilBrown29269552006-03-27 01:18:10 -08007776
NeilBrown87a8dec2011-01-31 11:57:43 +11007777 /* When a reshape changes the number of devices,
7778 * ->degraded is measured against the larger of the
7779 * pre and post number of devices.
7780 */
NeilBrownec32a2b2009-03-31 15:17:38 +11007781 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08007782 mddev->degraded = raid5_calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11007783 spin_unlock_irqrestore(&conf->device_lock, flags);
7784 }
NeilBrown63c70c42006-03-27 01:18:13 -08007785 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10007786 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08007787 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownf6705572006-03-27 01:18:11 -08007788
NeilBrown29269552006-03-27 01:18:10 -08007789 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7790 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10007791 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown29269552006-03-27 01:18:10 -08007792 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7793 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7794 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007795 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08007796 if (!mddev->sync_thread) {
7797 mddev->recovery = 0;
7798 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11007799 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007800 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11007801 mddev->new_chunk_sectors =
7802 conf->chunk_sectors = conf->prev_chunk_sectors;
7803 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10007804 rdev_for_each(rdev, mddev)
7805 rdev->new_data_offset = rdev->data_offset;
7806 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11007807 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11007808 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11007809 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11007810 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007811 spin_unlock_irq(&conf->device_lock);
7812 return -EAGAIN;
7813 }
NeilBrownc8f517c2009-03-31 15:28:40 +11007814 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08007815 md_wakeup_thread(mddev->sync_thread);
7816 md_new_event(mddev);
7817 return 0;
7818}
NeilBrown29269552006-03-27 01:18:10 -08007819
NeilBrownec32a2b2009-03-31 15:17:38 +11007820/* This is called from the reshape thread and should make any
7821 * changes needed in 'conf'
7822 */
NeilBrownd1688a62011-10-11 16:49:52 +11007823static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08007824{
NeilBrown29269552006-03-27 01:18:10 -08007825
NeilBrownf6705572006-03-27 01:18:11 -08007826 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10007827 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07007828
NeilBrownf6705572006-03-27 01:18:11 -08007829 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11007830 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10007831 rdev_for_each(rdev, conf->mddev)
7832 rdev->data_offset = rdev->new_data_offset;
7833 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11007834 conf->reshape_progress = MaxSector;
NeilBrown6cbd8142015-07-24 13:30:32 +10007835 conf->mddev->reshape_position = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08007836 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11007837 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07007838
7839 /* read-ahead size must cover two whole stripes, which is
7840 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
7841 */
NeilBrown4a5add42010-06-01 19:37:28 +10007842 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11007843 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007844 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11007845 / PAGE_SIZE);
Jan Karadc3b17c2017-02-02 15:56:50 +01007846 if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
7847 conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown16a53ec2006-06-26 00:27:38 -07007848 }
NeilBrown29269552006-03-27 01:18:10 -08007849 }
NeilBrown29269552006-03-27 01:18:10 -08007850}
7851
NeilBrownec32a2b2009-03-31 15:17:38 +11007852/* This is called from the raid5d thread with mddev_lock held.
7853 * It makes config changes to the device.
7854 */
NeilBrownfd01b882011-10-11 16:47:53 +11007855static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11007856{
NeilBrownd1688a62011-10-11 16:49:52 +11007857 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11007858
7859 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7860
NeilBrownec32a2b2009-03-31 15:17:38 +11007861 if (mddev->delta_disks > 0) {
7862 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
Heinz Mauelshagenfe67d192016-05-03 22:15:31 +02007863 if (mddev->queue) {
7864 set_capacity(mddev->gendisk, mddev->array_sectors);
7865 revalidate_disk(mddev->gendisk);
7866 }
NeilBrownec32a2b2009-03-31 15:17:38 +11007867 } else {
7868 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11007869 spin_lock_irq(&conf->device_lock);
Song Liu2e38a372017-01-24 10:45:30 -08007870 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11007871 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11007872 for (d = conf->raid_disks ;
7873 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10007874 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11007875 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10007876 if (rdev)
7877 clear_bit(In_sync, &rdev->flags);
7878 rdev = conf->disks[d].replacement;
7879 if (rdev)
7880 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10007881 }
NeilBrowncea9c222009-03-31 15:15:05 +11007882 }
NeilBrown88ce4932009-03-31 15:24:23 +11007883 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007884 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11007885 mddev->reshape_position = MaxSector;
7886 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007887 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11007888 }
7889}
7890
NeilBrownfd01b882011-10-11 16:47:53 +11007891static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07007892{
NeilBrownd1688a62011-10-11 16:49:52 +11007893 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07007894
7895 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08007896 case 2: /* resume for a suspend */
7897 wake_up(&conf->wait_for_overlap);
7898 break;
7899
NeilBrown72626682005-09-09 16:23:54 -07007900 case 1: /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11007901 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10007902 /* '2' tells resync/reshape to pause so that all
7903 * active stripes can drain
7904 */
Song Liua39f7af2016-11-17 15:24:40 -08007905 r5c_flush_cache(conf, INT_MAX);
NeilBrown64bd6602009-08-03 10:59:58 +10007906 conf->quiesce = 2;
Yuanhan Liub1b46482015-05-08 18:19:06 +10007907 wait_event_cmd(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08007908 atomic_read(&conf->active_stripes) == 0 &&
7909 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11007910 unlock_all_device_hash_locks_irq(conf),
7911 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10007912 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11007913 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10007914 /* allow reshape to continue */
7915 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07007916 break;
7917
7918 case 0: /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11007919 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07007920 conf->quiesce = 0;
Yuanhan Liub1b46482015-05-08 18:19:06 +10007921 wake_up(&conf->wait_for_quiescent);
NeilBrowne464eaf2006-03-27 01:18:14 -08007922 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11007923 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07007924 break;
7925 }
Shaohua Lie6c033f2015-10-04 09:20:12 -07007926 r5l_quiesce(conf->log, state);
NeilBrown72626682005-09-09 16:23:54 -07007927}
NeilBrownb15c2e52006-01-06 00:20:16 -08007928
NeilBrownfd01b882011-10-11 16:47:53 +11007929static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11007930{
NeilBrowne373ab12011-10-11 16:48:59 +11007931 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07007932 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11007933
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007934 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11007935 if (raid0_conf->nr_strip_zones > 1) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007936 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
7937 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007938 return ERR_PTR(-EINVAL);
7939 }
7940
NeilBrowne373ab12011-10-11 16:48:59 +11007941 sectors = raid0_conf->strip_zone[0].zone_end;
7942 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10007943 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007944 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11007945 mddev->new_layout = ALGORITHM_PARITY_N;
7946 mddev->new_chunk_sectors = mddev->chunk_sectors;
7947 mddev->raid_disks += 1;
7948 mddev->delta_disks = 1;
7949 /* make sure it will be not marked as dirty */
7950 mddev->recovery_cp = MaxSector;
7951
7952 return setup_conf(mddev);
7953}
7954
NeilBrownfd01b882011-10-11 16:47:53 +11007955static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11007956{
7957 int chunksect;
Shaohua Li6995f0b2016-12-08 15:48:17 -08007958 void *ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11007959
7960 if (mddev->raid_disks != 2 ||
7961 mddev->degraded > 1)
7962 return ERR_PTR(-EINVAL);
7963
7964 /* Should check if there are write-behind devices? */
7965
7966 chunksect = 64*2; /* 64K by default */
7967
7968 /* The array must be an exact multiple of chunksize */
7969 while (chunksect && (mddev->array_sectors & (chunksect-1)))
7970 chunksect >>= 1;
7971
7972 if ((chunksect<<9) < STRIPE_SIZE)
7973 /* array size does not allow a suitable chunk size */
7974 return ERR_PTR(-EINVAL);
7975
7976 mddev->new_level = 5;
7977 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10007978 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11007979
Shaohua Li6995f0b2016-12-08 15:48:17 -08007980 ret = setup_conf(mddev);
Jes Sorensen32cd7cb2017-01-06 19:31:35 -05007981 if (!IS_ERR(ret))
Shaohua Li394ed8e2017-01-04 16:10:19 -08007982 mddev_clear_unsupported_flags(mddev,
7983 UNSUPPORTED_MDDEV_FLAGS);
Shaohua Li6995f0b2016-12-08 15:48:17 -08007984 return ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11007985}
7986
NeilBrownfd01b882011-10-11 16:47:53 +11007987static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11007988{
7989 int new_layout;
7990
7991 switch (mddev->layout) {
7992 case ALGORITHM_LEFT_ASYMMETRIC_6:
7993 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
7994 break;
7995 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7996 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
7997 break;
7998 case ALGORITHM_LEFT_SYMMETRIC_6:
7999 new_layout = ALGORITHM_LEFT_SYMMETRIC;
8000 break;
8001 case ALGORITHM_RIGHT_SYMMETRIC_6:
8002 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
8003 break;
8004 case ALGORITHM_PARITY_0_6:
8005 new_layout = ALGORITHM_PARITY_0;
8006 break;
8007 case ALGORITHM_PARITY_N:
8008 new_layout = ALGORITHM_PARITY_N;
8009 break;
8010 default:
8011 return ERR_PTR(-EINVAL);
8012 }
8013 mddev->new_level = 5;
8014 mddev->new_layout = new_layout;
8015 mddev->delta_disks = -1;
8016 mddev->raid_disks -= 1;
8017 return setup_conf(mddev);
8018}
8019
NeilBrownfd01b882011-10-11 16:47:53 +11008020static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11008021{
NeilBrown88ce4932009-03-31 15:24:23 +11008022 /* For a 2-drive array, the layout and chunk size can be changed
8023 * immediately as not restriping is needed.
8024 * For larger arrays we record the new value - after validation
8025 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11008026 */
NeilBrownd1688a62011-10-11 16:49:52 +11008027 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10008028 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11008029
NeilBrown597a7112009-06-18 08:47:42 +10008030 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11008031 return -EINVAL;
8032 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008033 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11008034 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008035 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11008036 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008037 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11008038 /* not factor of array size */
8039 return -EINVAL;
8040 }
8041
8042 /* They look valid */
8043
NeilBrown88ce4932009-03-31 15:24:23 +11008044 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10008045 /* can make the change immediately */
8046 if (mddev->new_layout >= 0) {
8047 conf->algorithm = mddev->new_layout;
8048 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11008049 }
8050 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10008051 conf->chunk_sectors = new_chunk ;
8052 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11008053 }
Shaohua Li29530792016-12-08 15:48:19 -08008054 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown88ce4932009-03-31 15:24:23 +11008055 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11008056 }
NeilBrown50ac1682009-06-18 08:47:55 +10008057 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11008058}
8059
NeilBrownfd01b882011-10-11 16:47:53 +11008060static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11008061{
NeilBrown597a7112009-06-18 08:47:42 +10008062 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10008063
NeilBrown597a7112009-06-18 08:47:42 +10008064 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11008065 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008066 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008067 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11008068 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008069 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11008070 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008071 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11008072 /* not factor of array size */
8073 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008074 }
NeilBrown88ce4932009-03-31 15:24:23 +11008075
8076 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10008077 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11008078}
8079
NeilBrownfd01b882011-10-11 16:47:53 +11008080static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11008081{
8082 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008083 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008084 * raid1 - if there are two drives. We need to know the chunk size
8085 * raid4 - trivial - just use a raid4 layout.
8086 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008087 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008088 if (mddev->level == 0)
8089 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11008090 if (mddev->level == 1)
8091 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11008092 if (mddev->level == 4) {
8093 mddev->new_layout = ALGORITHM_PARITY_N;
8094 mddev->new_level = 5;
8095 return setup_conf(mddev);
8096 }
NeilBrownfc9739c2009-03-31 14:57:20 +11008097 if (mddev->level == 6)
8098 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11008099
8100 return ERR_PTR(-EINVAL);
8101}
8102
NeilBrownfd01b882011-10-11 16:47:53 +11008103static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11008104{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008105 /* raid4 can take over:
8106 * raid0 - if there is only one strip zone
8107 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11008108 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008109 if (mddev->level == 0)
8110 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11008111 if (mddev->level == 5 &&
8112 mddev->layout == ALGORITHM_PARITY_N) {
8113 mddev->new_layout = 0;
8114 mddev->new_level = 4;
8115 return setup_conf(mddev);
8116 }
8117 return ERR_PTR(-EINVAL);
8118}
NeilBrownd562b0c2009-03-31 14:39:39 +11008119
NeilBrown84fc4b52011-10-11 16:49:58 +11008120static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11008121
NeilBrownfd01b882011-10-11 16:47:53 +11008122static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11008123{
8124 /* Currently can only take over a raid5. We map the
8125 * personality to an equivalent raid6 personality
8126 * with the Q block at the end.
8127 */
8128 int new_layout;
8129
8130 if (mddev->pers != &raid5_personality)
8131 return ERR_PTR(-EINVAL);
8132 if (mddev->degraded > 1)
8133 return ERR_PTR(-EINVAL);
8134 if (mddev->raid_disks > 253)
8135 return ERR_PTR(-EINVAL);
8136 if (mddev->raid_disks < 3)
8137 return ERR_PTR(-EINVAL);
8138
8139 switch (mddev->layout) {
8140 case ALGORITHM_LEFT_ASYMMETRIC:
8141 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
8142 break;
8143 case ALGORITHM_RIGHT_ASYMMETRIC:
8144 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
8145 break;
8146 case ALGORITHM_LEFT_SYMMETRIC:
8147 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
8148 break;
8149 case ALGORITHM_RIGHT_SYMMETRIC:
8150 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
8151 break;
8152 case ALGORITHM_PARITY_0:
8153 new_layout = ALGORITHM_PARITY_0_6;
8154 break;
8155 case ALGORITHM_PARITY_N:
8156 new_layout = ALGORITHM_PARITY_N;
8157 break;
8158 default:
8159 return ERR_PTR(-EINVAL);
8160 }
8161 mddev->new_level = 6;
8162 mddev->new_layout = new_layout;
8163 mddev->delta_disks = 1;
8164 mddev->raid_disks += 1;
8165 return setup_conf(mddev);
8166}
8167
NeilBrown84fc4b52011-10-11 16:49:58 +11008168static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07008169{
8170 .name = "raid6",
8171 .level = 6,
8172 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008173 .make_request = raid5_make_request,
8174 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11008175 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008176 .status = raid5_status,
8177 .error_handler = raid5_error,
NeilBrown16a53ec2006-06-26 00:27:38 -07008178 .hot_add_disk = raid5_add_disk,
8179 .hot_remove_disk= raid5_remove_disk,
8180 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008181 .sync_request = raid5_sync_request,
NeilBrown16a53ec2006-06-26 00:27:38 -07008182 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008183 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10008184 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08008185 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008186 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07008187 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11008188 .takeover = raid6_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008189 .congested = raid5_congested,
NeilBrown16a53ec2006-06-26 00:27:38 -07008190};
NeilBrown84fc4b52011-10-11 16:49:58 +11008191static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008192{
8193 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08008194 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008195 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008196 .make_request = raid5_make_request,
8197 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11008198 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008199 .status = raid5_status,
8200 .error_handler = raid5_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008201 .hot_add_disk = raid5_add_disk,
8202 .hot_remove_disk= raid5_remove_disk,
8203 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008204 .sync_request = raid5_sync_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008205 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008206 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08008207 .check_reshape = raid5_check_reshape,
8208 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008209 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07008210 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11008211 .takeover = raid5_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008212 .congested = raid5_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008213};
8214
NeilBrown84fc4b52011-10-11 16:49:58 +11008215static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216{
NeilBrown2604b702006-01-06 00:20:36 -08008217 .name = "raid4",
8218 .level = 4,
8219 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008220 .make_request = raid5_make_request,
8221 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11008222 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008223 .status = raid5_status,
8224 .error_handler = raid5_error,
NeilBrown2604b702006-01-06 00:20:36 -08008225 .hot_add_disk = raid5_add_disk,
8226 .hot_remove_disk= raid5_remove_disk,
8227 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008228 .sync_request = raid5_sync_request,
NeilBrown2604b702006-01-06 00:20:36 -08008229 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008230 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08008231 .check_reshape = raid5_check_reshape,
8232 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008233 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08008234 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11008235 .takeover = raid4_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008236 .congested = raid5_congested,
NeilBrown2604b702006-01-06 00:20:36 -08008237};
8238
8239static int __init raid5_init(void)
8240{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008241 int ret;
8242
Shaohua Li851c30c2013-08-28 14:30:16 +08008243 raid5_wq = alloc_workqueue("raid5wq",
8244 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
8245 if (!raid5_wq)
8246 return -ENOMEM;
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008247
8248 ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
8249 "md/raid5:prepare",
8250 raid456_cpu_up_prepare,
8251 raid456_cpu_dead);
8252 if (ret) {
8253 destroy_workqueue(raid5_wq);
8254 return ret;
8255 }
NeilBrown16a53ec2006-06-26 00:27:38 -07008256 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008257 register_md_personality(&raid5_personality);
8258 register_md_personality(&raid4_personality);
8259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008260}
8261
NeilBrown2604b702006-01-06 00:20:36 -08008262static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008263{
NeilBrown16a53ec2006-06-26 00:27:38 -07008264 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008265 unregister_md_personality(&raid5_personality);
8266 unregister_md_personality(&raid4_personality);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008267 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
Shaohua Li851c30c2013-08-28 14:30:16 +08008268 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008269}
8270
8271module_init(raid5_init);
8272module_exit(raid5_exit);
8273MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008274MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008275MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08008276MODULE_ALIAS("md-raid5");
8277MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08008278MODULE_ALIAS("md-level-5");
8279MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07008280MODULE_ALIAS("md-personality-8"); /* RAID6 */
8281MODULE_ALIAS("md-raid6");
8282MODULE_ALIAS("md-level-6");
8283
8284/* This used to be two separate modules, they were: */
8285MODULE_ALIAS("raid5");
8286MODULE_ALIAS("raid6");