blob: 5a28bd9b5b5f502c8385e8df54a25e5fc3e45452 [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 Li535ae4e2017-02-15 19:37:32 -0800179static bool stripe_is_lowprio(struct stripe_head *sh)
180{
181 return (test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) ||
182 test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) &&
183 !test_bit(STRIPE_R5C_CACHING, &sh->state);
184}
185
Shaohua Li851c30c2013-08-28 14:30:16 +0800186static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
187{
188 struct r5conf *conf = sh->raid_conf;
189 struct r5worker_group *group;
Shaohua Libfc90cb2013-08-29 15:40:32 +0800190 int thread_cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +0800191 int i, cpu = sh->cpu;
192
193 if (!cpu_online(cpu)) {
194 cpu = cpumask_any(cpu_online_mask);
195 sh->cpu = cpu;
196 }
197
198 if (list_empty(&sh->lru)) {
199 struct r5worker_group *group;
200 group = conf->worker_groups + cpu_to_group(cpu);
Shaohua Li535ae4e2017-02-15 19:37:32 -0800201 if (stripe_is_lowprio(sh))
202 list_add_tail(&sh->lru, &group->loprio_list);
203 else
204 list_add_tail(&sh->lru, &group->handle_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800205 group->stripes_cnt++;
206 sh->group = group;
Shaohua Li851c30c2013-08-28 14:30:16 +0800207 }
208
209 if (conf->worker_cnt_per_group == 0) {
210 md_wakeup_thread(conf->mddev->thread);
211 return;
212 }
213
214 group = conf->worker_groups + cpu_to_group(sh->cpu);
215
Shaohua Libfc90cb2013-08-29 15:40:32 +0800216 group->workers[0].working = true;
217 /* at least one worker should run to avoid race */
218 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
219
220 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
221 /* wakeup more workers */
222 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
223 if (group->workers[i].working == false) {
224 group->workers[i].working = true;
225 queue_work_on(sh->cpu, raid5_wq,
226 &group->workers[i].work);
227 thread_cnt--;
228 }
229 }
Shaohua Li851c30c2013-08-28 14:30:16 +0800230}
231
Shaohua Li566c09c2013-11-14 15:16:17 +1100232static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
233 struct list_head *temp_inactive_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Song Liu1e6d6902016-11-17 15:24:39 -0800235 int i;
236 int injournal = 0; /* number of date pages with R5_InJournal */
237
Shaohua Li4eb788d2012-07-19 16:01:31 +1000238 BUG_ON(!list_empty(&sh->lru));
239 BUG_ON(atomic_read(&conf->active_stripes)==0);
Song Liu1e6d6902016-11-17 15:24:39 -0800240
241 if (r5c_is_writeback(conf->log))
242 for (i = sh->disks; i--; )
243 if (test_bit(R5_InJournal, &sh->dev[i].flags))
244 injournal++;
Song Liua39f7af2016-11-17 15:24:40 -0800245 /*
246 * When quiesce in r5c write back, set STRIPE_HANDLE for stripes with
247 * data in journal, so they are not released to cached lists
248 */
249 if (conf->quiesce && r5c_is_writeback(conf->log) &&
250 !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0) {
251 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
252 r5c_make_stripe_write_out(sh);
253 set_bit(STRIPE_HANDLE, &sh->state);
254 }
Song Liu1e6d6902016-11-17 15:24:39 -0800255
Shaohua Li4eb788d2012-07-19 16:01:31 +1000256 if (test_bit(STRIPE_HANDLE, &sh->state)) {
257 if (test_bit(STRIPE_DELAYED, &sh->state) &&
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500258 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
Shaohua Li4eb788d2012-07-19 16:01:31 +1000259 list_add_tail(&sh->lru, &conf->delayed_list);
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500260 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
Shaohua Li4eb788d2012-07-19 16:01:31 +1000261 sh->bm_seq - conf->seq_write > 0)
262 list_add_tail(&sh->lru, &conf->bitmap_list);
263 else {
264 clear_bit(STRIPE_DELAYED, &sh->state);
265 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Shaohua Li851c30c2013-08-28 14:30:16 +0800266 if (conf->worker_cnt_per_group == 0) {
Shaohua Li535ae4e2017-02-15 19:37:32 -0800267 if (stripe_is_lowprio(sh))
268 list_add_tail(&sh->lru,
269 &conf->loprio_list);
270 else
271 list_add_tail(&sh->lru,
272 &conf->handle_list);
Shaohua Li851c30c2013-08-28 14:30:16 +0800273 } else {
274 raid5_wakeup_stripe_thread(sh);
275 return;
276 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000277 }
278 md_wakeup_thread(conf->mddev->thread);
279 } else {
280 BUG_ON(stripe_operations_active(sh));
281 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
282 if (atomic_dec_return(&conf->preread_active_stripes)
283 < IO_THRESHOLD)
284 md_wakeup_thread(conf->mddev->thread);
285 atomic_dec(&conf->active_stripes);
Song Liu1e6d6902016-11-17 15:24:39 -0800286 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
287 if (!r5c_is_writeback(conf->log))
288 list_add_tail(&sh->lru, temp_inactive_list);
289 else {
290 WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags));
291 if (injournal == 0)
292 list_add_tail(&sh->lru, temp_inactive_list);
293 else if (injournal == conf->raid_disks - conf->max_degraded) {
294 /* full stripe */
295 if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state))
296 atomic_inc(&conf->r5c_cached_full_stripes);
297 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state))
298 atomic_dec(&conf->r5c_cached_partial_stripes);
299 list_add_tail(&sh->lru, &conf->r5c_full_stripe_list);
Song Liua39f7af2016-11-17 15:24:40 -0800300 r5c_check_cached_full_stripe(conf);
Song Liu03b047f2017-01-11 13:39:14 -0800301 } else
302 /*
303 * STRIPE_R5C_PARTIAL_STRIPE is set in
304 * r5c_try_caching_write(). No need to
305 * set it again.
306 */
Song Liu1e6d6902016-11-17 15:24:39 -0800307 list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list);
Song Liu1e6d6902016-11-17 15:24:39 -0800308 }
309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311}
NeilBrownd0dabf72009-03-31 14:39:38 +1100312
Shaohua Li566c09c2013-11-14 15:16:17 +1100313static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
314 struct list_head *temp_inactive_list)
Shaohua Li4eb788d2012-07-19 16:01:31 +1000315{
316 if (atomic_dec_and_test(&sh->count))
Shaohua Li566c09c2013-11-14 15:16:17 +1100317 do_release_stripe(conf, sh, temp_inactive_list);
318}
319
320/*
321 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
322 *
323 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
324 * given time. Adding stripes only takes device lock, while deleting stripes
325 * only takes hash lock.
326 */
327static void release_inactive_stripe_list(struct r5conf *conf,
328 struct list_head *temp_inactive_list,
329 int hash)
330{
331 int size;
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800332 bool do_wakeup = false;
Shaohua Li566c09c2013-11-14 15:16:17 +1100333 unsigned long flags;
334
335 if (hash == NR_STRIPE_HASH_LOCKS) {
336 size = NR_STRIPE_HASH_LOCKS;
337 hash = NR_STRIPE_HASH_LOCKS - 1;
338 } else
339 size = 1;
340 while (size) {
341 struct list_head *list = &temp_inactive_list[size - 1];
342
343 /*
Shaohua Li6d036f72015-08-13 14:31:57 -0700344 * We don't hold any lock here yet, raid5_get_active_stripe() might
Shaohua Li566c09c2013-11-14 15:16:17 +1100345 * remove stripes from the list
346 */
347 if (!list_empty_careful(list)) {
348 spin_lock_irqsave(conf->hash_locks + hash, flags);
Shaohua Li4bda5562013-11-14 15:16:17 +1100349 if (list_empty(conf->inactive_list + hash) &&
350 !list_empty(list))
351 atomic_dec(&conf->empty_inactive_list_nr);
Shaohua Li566c09c2013-11-14 15:16:17 +1100352 list_splice_tail_init(list, conf->inactive_list + hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800353 do_wakeup = true;
Shaohua Li566c09c2013-11-14 15:16:17 +1100354 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
355 }
356 size--;
357 hash--;
358 }
359
360 if (do_wakeup) {
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800361 wake_up(&conf->wait_for_stripe);
Yuanhan Liub1b46482015-05-08 18:19:06 +1000362 if (atomic_read(&conf->active_stripes) == 0)
363 wake_up(&conf->wait_for_quiescent);
Shaohua Li566c09c2013-11-14 15:16:17 +1100364 if (conf->retry_read_aligned)
365 md_wakeup_thread(conf->mddev->thread);
366 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000367}
368
Shaohua Li773ca822013-08-27 17:50:39 +0800369/* should hold conf->device_lock already */
Shaohua Li566c09c2013-11-14 15:16:17 +1100370static int release_stripe_list(struct r5conf *conf,
371 struct list_head *temp_inactive_list)
Shaohua Li773ca822013-08-27 17:50:39 +0800372{
Byungchul Parkeae82632017-02-14 16:26:24 +0900373 struct stripe_head *sh, *t;
Shaohua Li773ca822013-08-27 17:50:39 +0800374 int count = 0;
375 struct llist_node *head;
376
377 head = llist_del_all(&conf->released_stripes);
Shaohua Lid265d9d2013-08-28 14:29:05 +0800378 head = llist_reverse_order(head);
Byungchul Parkeae82632017-02-14 16:26:24 +0900379 llist_for_each_entry_safe(sh, t, head, release_list) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100380 int hash;
381
Shaohua Li773ca822013-08-27 17:50:39 +0800382 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
383 smp_mb();
384 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
385 /*
386 * Don't worry the bit is set here, because if the bit is set
387 * again, the count is always > 1. This is true for
388 * STRIPE_ON_UNPLUG_LIST bit too.
389 */
Shaohua Li566c09c2013-11-14 15:16:17 +1100390 hash = sh->hash_lock_index;
391 __release_stripe(conf, sh, &temp_inactive_list[hash]);
Shaohua Li773ca822013-08-27 17:50:39 +0800392 count++;
393 }
394
395 return count;
396}
397
Shaohua Li6d036f72015-08-13 14:31:57 -0700398void raid5_release_stripe(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
NeilBrownd1688a62011-10-11 16:49:52 +1100400 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 unsigned long flags;
Shaohua Li566c09c2013-11-14 15:16:17 +1100402 struct list_head list;
403 int hash;
Shaohua Li773ca822013-08-27 17:50:39 +0800404 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700405
Eivind Sartocf170f32014-05-28 13:39:23 +1000406 /* Avoid release_list until the last reference.
407 */
408 if (atomic_add_unless(&sh->count, -1, 1))
409 return;
410
majianpengad4068d2013-11-14 15:16:15 +1100411 if (unlikely(!conf->mddev->thread) ||
412 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
Shaohua Li773ca822013-08-27 17:50:39 +0800413 goto slow_path;
414 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
415 if (wakeup)
416 md_wakeup_thread(conf->mddev->thread);
417 return;
418slow_path:
Shaohua Li4eb788d2012-07-19 16:01:31 +1000419 local_irq_save(flags);
Shaohua Li773ca822013-08-27 17:50:39 +0800420 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Shaohua Li4eb788d2012-07-19 16:01:31 +1000421 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100422 INIT_LIST_HEAD(&list);
423 hash = sh->hash_lock_index;
424 do_release_stripe(conf, sh, &list);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000425 spin_unlock(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +1100426 release_inactive_stripe_list(conf, &list, hash);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000427 }
428 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
NeilBrownfccddba2006-01-06 00:20:33 -0800431static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Dan Williams45b42332007-07-09 11:56:43 -0700433 pr_debug("remove_hash(), stripe %llu\n",
434 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
NeilBrownfccddba2006-01-06 00:20:33 -0800436 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
NeilBrownd1688a62011-10-11 16:49:52 +1100439static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
NeilBrownfccddba2006-01-06 00:20:33 -0800441 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Dan Williams45b42332007-07-09 11:56:43 -0700443 pr_debug("insert_hash(), stripe %llu\n",
444 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
NeilBrownfccddba2006-01-06 00:20:33 -0800446 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/* find an idle stripe, make sure it is unhashed, and return it. */
Shaohua Li566c09c2013-11-14 15:16:17 +1100450static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 struct stripe_head *sh = NULL;
453 struct list_head *first;
454
Shaohua Li566c09c2013-11-14 15:16:17 +1100455 if (list_empty(conf->inactive_list + hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 goto out;
Shaohua Li566c09c2013-11-14 15:16:17 +1100457 first = (conf->inactive_list + hash)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 sh = list_entry(first, struct stripe_head, lru);
459 list_del_init(first);
460 remove_hash(sh);
461 atomic_inc(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100462 BUG_ON(hash != sh->hash_lock_index);
Shaohua Li4bda5562013-11-14 15:16:17 +1100463 if (list_empty(conf->inactive_list + hash))
464 atomic_inc(&conf->empty_inactive_list_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465out:
466 return sh;
467}
468
NeilBrowne4e11e32010-06-16 16:45:16 +1000469static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 struct page *p;
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++) {
Shaohua Lid592a992014-05-21 17:57:44 +0800476 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 p = sh->dev[i].page;
478 if (!p)
479 continue;
480 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800481 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483}
484
NeilBrowna9683a72015-02-25 12:02:51 +1100485static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000488 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
NeilBrowne4e11e32010-06-16 16:45:16 +1000490 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 struct page *page;
492
NeilBrowna9683a72015-02-25 12:02:51 +1100493 if (!(page = alloc_page(gfp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return 1;
495 }
496 sh->dev[i].page = page;
Shaohua Lid592a992014-05-21 17:57:44 +0800497 sh->dev[i].orig_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499 return 0;
500}
501
NeilBrown784052e2009-03-31 15:19:07 +1100502static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100503static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100504 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
NeilBrownb5663ba2009-03-31 14:39:38 +1100506static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
NeilBrownd1688a62011-10-11 16:49:52 +1100508 struct r5conf *conf = sh->raid_conf;
Shaohua Li566c09c2013-11-14 15:16:17 +1100509 int i, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200511 BUG_ON(atomic_read(&sh->count) != 0);
512 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000513 BUG_ON(stripe_operations_active(sh));
shli@kernel.org59fc6302014-12-15 12:57:03 +1100514 BUG_ON(sh->batch_head);
Dan Williamsd84e0f12007-01-02 13:52:30 -0700515
Dan Williams45b42332007-07-09 11:56:43 -0700516 pr_debug("init_stripe called, stripe %llu\n",
Markus Stockhausenb8e6a152014-08-23 20:19:27 +1000517 (unsigned long long)sector);
Shaohua Li566c09c2013-11-14 15:16:17 +1100518retry:
519 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100520 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100521 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100523 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 sh->state = 0;
525
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800526 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 struct r5dev *dev = &sh->dev[i];
528
Dan Williamsd84e0f12007-01-02 13:52:30 -0700529 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 test_bit(R5_LOCKED, &dev->flags)) {
NeilBrowncc6167b2016-11-02 14:16:50 +1100531 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700533 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000535 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100538 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100540 if (read_seqcount_retry(&conf->gen_lock, seq))
541 goto retry;
shli@kernel.org7a87f432014-12-15 12:57:03 +1100542 sh->overwrite_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800544 sh->cpu = smp_processor_id();
shli@kernel.orgda41ba62014-12-15 12:57:03 +1100545 set_bit(STRIPE_BATCH_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
NeilBrownd1688a62011-10-11 16:49:52 +1100548static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100549 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 struct stripe_head *sh;
552
Dan Williams45b42332007-07-09 11:56:43 -0700553 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800554 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100555 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700557 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return NULL;
559}
560
NeilBrown674806d2010-06-16 17:17:53 +1000561/*
562 * Need to check if array has failed when deciding whether to:
563 * - start an array
564 * - remove non-faulty devices
565 * - add a spare
566 * - allow a reshape
567 * This determination is simple when no reshape is happening.
568 * However if there is a reshape, we need to carefully check
569 * both the before and after sections.
570 * This is because some failed devices may only affect one
571 * of the two sections, and some non-in_sync devices may
572 * be insync in the section most affected by failed devices.
573 */
Song Liu2e38a372017-01-24 10:45:30 -0800574int raid5_calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000575{
NeilBrown908f4fb2011-12-23 10:17:50 +1100576 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000577 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000578
579 rcu_read_lock();
580 degraded = 0;
581 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100582 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000583 if (rdev && test_bit(Faulty, &rdev->flags))
584 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000585 if (!rdev || test_bit(Faulty, &rdev->flags))
586 degraded++;
587 else if (test_bit(In_sync, &rdev->flags))
588 ;
589 else
590 /* not in-sync or faulty.
591 * If the reshape increases the number of devices,
592 * this is being recovered by the reshape, so
593 * this 'previous' section is not in_sync.
594 * If the number of devices is being reduced however,
595 * the device can only be part of the array if
596 * we are reverting a reshape, so this section will
597 * be in-sync.
598 */
599 if (conf->raid_disks >= conf->previous_raid_disks)
600 degraded++;
601 }
602 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100603 if (conf->raid_disks == conf->previous_raid_disks)
604 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000605 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100606 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000607 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100608 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000609 if (rdev && test_bit(Faulty, &rdev->flags))
610 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000611 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100612 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000613 else if (test_bit(In_sync, &rdev->flags))
614 ;
615 else
616 /* not in-sync or faulty.
617 * If reshape increases the number of devices, this
618 * section has already been recovered, else it
619 * almost certainly hasn't.
620 */
621 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100622 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000623 }
624 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100625 if (degraded2 > degraded)
626 return degraded2;
627 return degraded;
628}
629
630static int has_failed(struct r5conf *conf)
631{
632 int degraded;
633
634 if (conf->mddev->reshape_position == MaxSector)
635 return conf->mddev->degraded > conf->max_degraded;
636
Song Liu2e38a372017-01-24 10:45:30 -0800637 degraded = raid5_calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000638 if (degraded > conf->max_degraded)
639 return 1;
640 return 0;
641}
642
Shaohua Li6d036f72015-08-13 14:31:57 -0700643struct stripe_head *
644raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
645 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 struct stripe_head *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +1100648 int hash = stripe_hash_locks_hash(sector);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800649 int inc_empty_inactive_list_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Dan Williams45b42332007-07-09 11:56:43 -0700651 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Shaohua Li566c09c2013-11-14 15:16:17 +1100653 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 do {
Yuanhan Liub1b46482015-05-08 18:19:06 +1000656 wait_event_lock_irq(conf->wait_for_quiescent,
NeilBrowna8c906c2009-06-09 14:39:59 +1000657 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100658 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100659 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (!sh) {
NeilBrownedbe83a2015-02-26 12:47:56 +1100661 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100662 sh = get_free_stripe(conf, hash);
Shaohua Li713bc5c2015-05-28 17:33:47 -0700663 if (!sh && !test_bit(R5_DID_ALLOC,
664 &conf->cache_state))
NeilBrownedbe83a2015-02-26 12:47:56 +1100665 set_bit(R5_ALLOC_MORE,
666 &conf->cache_state);
667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (noblock && sh == NULL)
669 break;
Song Liua39f7af2016-11-17 15:24:40 -0800670
671 r5c_check_stripe_cache_usage(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (!sh) {
NeilBrown54233992015-02-26 12:21:04 +1100673 set_bit(R5_INACTIVE_BLOCKED,
674 &conf->cache_state);
Song Liua39f7af2016-11-17 15:24:40 -0800675 r5l_wake_reclaim(conf->log, 0);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800676 wait_event_lock_irq(
677 conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +1100678 !list_empty(conf->inactive_list + hash) &&
679 (atomic_read(&conf->active_stripes)
680 < (conf->max_nr_stripes * 3 / 4)
NeilBrown54233992015-02-26 12:21:04 +1100681 || !test_bit(R5_INACTIVE_BLOCKED,
682 &conf->cache_state)),
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800683 *(conf->hash_locks + hash));
NeilBrown54233992015-02-26 12:21:04 +1100684 clear_bit(R5_INACTIVE_BLOCKED,
685 &conf->cache_state);
NeilBrown7da9d452014-01-22 11:45:03 +1100686 } else {
NeilBrownb5663ba2009-03-31 14:39:38 +1100687 init_stripe(sh, sector, previous);
NeilBrown7da9d452014-01-22 11:45:03 +1100688 atomic_inc(&sh->count);
689 }
Shaohua Lie240c182014-04-09 11:27:42 +0800690 } else if (!atomic_inc_not_zero(&sh->count)) {
NeilBrown6d183de2013-11-28 10:55:27 +1100691 spin_lock(&conf->device_lock);
Shaohua Lie240c182014-04-09 11:27:42 +0800692 if (!atomic_read(&sh->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (!test_bit(STRIPE_HANDLE, &sh->state))
694 atomic_inc(&conf->active_stripes);
NeilBrown5af9bef2014-01-14 15:16:10 +1100695 BUG_ON(list_empty(&sh->lru) &&
696 !test_bit(STRIPE_EXPANDING, &sh->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800697 inc_empty_inactive_list_flag = 0;
698 if (!list_empty(conf->inactive_list + hash))
699 inc_empty_inactive_list_flag = 1;
NeilBrown16a53ec2006-06-26 00:27:38 -0700700 list_del_init(&sh->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800701 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
702 atomic_inc(&conf->empty_inactive_list_nr);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800703 if (sh->group) {
704 sh->group->stripes_cnt--;
705 sh->group = NULL;
706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
NeilBrown7da9d452014-01-22 11:45:03 +1100708 atomic_inc(&sh->count);
NeilBrown6d183de2013-11-28 10:55:27 +1100709 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711 } while (sh == NULL);
712
Shaohua Li566c09c2013-11-14 15:16:17 +1100713 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return sh;
715}
716
shli@kernel.org7a87f432014-12-15 12:57:03 +1100717static bool is_full_stripe_write(struct stripe_head *sh)
718{
719 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
720 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
721}
722
shli@kernel.org59fc6302014-12-15 12:57:03 +1100723static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
724{
725 local_irq_disable();
726 if (sh1 > sh2) {
727 spin_lock(&sh2->stripe_lock);
728 spin_lock_nested(&sh1->stripe_lock, 1);
729 } else {
730 spin_lock(&sh1->stripe_lock);
731 spin_lock_nested(&sh2->stripe_lock, 1);
732 }
733}
734
735static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
736{
737 spin_unlock(&sh1->stripe_lock);
738 spin_unlock(&sh2->stripe_lock);
739 local_irq_enable();
740}
741
742/* Only freshly new full stripe normal write stripe can be added to a batch list */
743static bool stripe_can_batch(struct stripe_head *sh)
744{
Shaohua Li9c3e3332015-08-13 14:32:02 -0700745 struct r5conf *conf = sh->raid_conf;
746
747 if (conf->log)
748 return false;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100749 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
NeilBrownd0852df52015-05-27 08:43:45 +1000750 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
shli@kernel.org59fc6302014-12-15 12:57:03 +1100751 is_full_stripe_write(sh);
752}
753
754/* we only do back search */
755static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
756{
757 struct stripe_head *head;
758 sector_t head_sector, tmp_sec;
759 int hash;
760 int dd_idx;
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800761 int inc_empty_inactive_list_flag;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100762
shli@kernel.org59fc6302014-12-15 12:57:03 +1100763 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
764 tmp_sec = sh->sector;
765 if (!sector_div(tmp_sec, conf->chunk_sectors))
766 return;
767 head_sector = sh->sector - STRIPE_SECTORS;
768
769 hash = stripe_hash_locks_hash(head_sector);
770 spin_lock_irq(conf->hash_locks + hash);
771 head = __find_stripe(conf, head_sector, conf->generation);
772 if (head && !atomic_inc_not_zero(&head->count)) {
773 spin_lock(&conf->device_lock);
774 if (!atomic_read(&head->count)) {
775 if (!test_bit(STRIPE_HANDLE, &head->state))
776 atomic_inc(&conf->active_stripes);
777 BUG_ON(list_empty(&head->lru) &&
778 !test_bit(STRIPE_EXPANDING, &head->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800779 inc_empty_inactive_list_flag = 0;
780 if (!list_empty(conf->inactive_list + hash))
781 inc_empty_inactive_list_flag = 1;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100782 list_del_init(&head->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800783 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
784 atomic_inc(&conf->empty_inactive_list_nr);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100785 if (head->group) {
786 head->group->stripes_cnt--;
787 head->group = NULL;
788 }
789 }
790 atomic_inc(&head->count);
791 spin_unlock(&conf->device_lock);
792 }
793 spin_unlock_irq(conf->hash_locks + hash);
794
795 if (!head)
796 return;
797 if (!stripe_can_batch(head))
798 goto out;
799
800 lock_two_stripes(head, sh);
801 /* clear_batch_ready clear the flag */
802 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
803 goto unlock_out;
804
805 if (sh->batch_head)
806 goto unlock_out;
807
808 dd_idx = 0;
809 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
810 dd_idx++;
Jens Axboe1eff9d32016-08-05 15:35:16 -0600811 if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
Mike Christie796a5cf2016-06-05 14:32:07 -0500812 bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
shli@kernel.org59fc6302014-12-15 12:57:03 +1100813 goto unlock_out;
814
815 if (head->batch_head) {
816 spin_lock(&head->batch_head->batch_lock);
817 /* This batch list is already running */
818 if (!stripe_can_batch(head)) {
819 spin_unlock(&head->batch_head->batch_lock);
820 goto unlock_out;
821 }
822
823 /*
824 * at this point, head's BATCH_READY could be cleared, but we
825 * can still add the stripe to batch list
826 */
827 list_add(&sh->batch_list, &head->batch_list);
828 spin_unlock(&head->batch_head->batch_lock);
829
830 sh->batch_head = head->batch_head;
831 } else {
832 head->batch_head = head;
833 sh->batch_head = head->batch_head;
834 spin_lock(&head->batch_lock);
835 list_add_tail(&sh->batch_list, &head->batch_list);
836 spin_unlock(&head->batch_lock);
837 }
838
839 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
840 if (atomic_dec_return(&conf->preread_active_stripes)
841 < IO_THRESHOLD)
842 md_wakeup_thread(conf->mddev->thread);
843
NeilBrown2b6b2452015-05-21 15:10:01 +1000844 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
845 int seq = sh->bm_seq;
846 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
847 sh->batch_head->bm_seq > seq)
848 seq = sh->batch_head->bm_seq;
849 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
850 sh->batch_head->bm_seq = seq;
851 }
852
shli@kernel.org59fc6302014-12-15 12:57:03 +1100853 atomic_inc(&sh->count);
854unlock_out:
855 unlock_two_stripes(head, sh);
856out:
Shaohua Li6d036f72015-08-13 14:31:57 -0700857 raid5_release_stripe(head);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100858}
859
NeilBrown05616be2012-05-21 09:27:00 +1000860/* Determine if 'data_offset' or 'new_data_offset' should be used
861 * in this stripe_head.
862 */
863static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
864{
865 sector_t progress = conf->reshape_progress;
866 /* Need a memory barrier to make sure we see the value
867 * of conf->generation, or ->data_offset that was set before
868 * reshape_progress was updated.
869 */
870 smp_rmb();
871 if (progress == MaxSector)
872 return 0;
873 if (sh->generation == conf->generation - 1)
874 return 0;
875 /* We are in a reshape, and this is a new-generation stripe,
876 * so use new_data_offset.
877 */
878 return 1;
879}
880
Shaohua Li765d7042017-01-04 09:33:23 -0800881static void flush_deferred_bios(struct r5conf *conf)
882{
883 struct bio_list tmp;
884 struct bio *bio;
885
886 if (!conf->batch_bio_dispatch || !conf->group_cnt)
887 return;
888
889 bio_list_init(&tmp);
890 spin_lock(&conf->pending_bios_lock);
891 bio_list_merge(&tmp, &conf->pending_bios);
892 bio_list_init(&conf->pending_bios);
893 spin_unlock(&conf->pending_bios_lock);
894
895 while ((bio = bio_list_pop(&tmp)))
896 generic_make_request(bio);
897}
898
899static void defer_bio_issue(struct r5conf *conf, struct bio *bio)
900{
901 /*
902 * change group_cnt will drain all bios, so this is safe
903 *
904 * A read generally means a read-modify-write, which usually means a
905 * randwrite, so we don't delay it
906 */
907 if (!conf->batch_bio_dispatch || !conf->group_cnt ||
908 bio_op(bio) == REQ_OP_READ) {
909 generic_make_request(bio);
910 return;
911 }
912 spin_lock(&conf->pending_bios_lock);
913 bio_list_add(&conf->pending_bios, bio);
914 spin_unlock(&conf->pending_bios_lock);
915 md_wakeup_thread(conf->mddev->thread);
916}
917
NeilBrown6712ecf2007-09-27 12:47:43 +0200918static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200919raid5_end_read_request(struct bio *bi);
NeilBrown6712ecf2007-09-27 12:47:43 +0200920static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200921raid5_end_write_request(struct bio *bi);
Dan Williams91c00922007-01-02 13:52:30 -0700922
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000923static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700924{
NeilBrownd1688a62011-10-11 16:49:52 +1100925 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700926 int i, disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100927 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -0700928
929 might_sleep();
930
Song Liu1e6d6902016-11-17 15:24:39 -0800931 if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
932 /* writing out phase */
Song Liud7bd3982016-11-23 22:50:39 -0800933 if (s->waiting_extra_page)
934 return;
Song Liu1e6d6902016-11-17 15:24:39 -0800935 if (r5l_write_stripe(conf->log, sh) == 0)
936 return;
937 } else { /* caching phase */
938 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) {
939 r5c_cache_data(conf->log, sh, s);
940 return;
941 }
942 }
943
Dan Williams91c00922007-01-02 13:52:30 -0700944 for (i = disks; i--; ) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500945 int op, op_flags = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +1100946 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100947 struct bio *bi, *rbi;
948 struct md_rdev *rdev, *rrdev = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100949
950 sh = head_sh;
Tejun Heoe9c74692010-09-03 11:56:18 +0200951 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500952 op = REQ_OP_WRITE;
Tejun Heoe9c74692010-09-03 11:56:18 +0200953 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
Christoph Hellwig70fd7612016-11-01 07:40:10 -0600954 op_flags = REQ_FUA;
Shaohua Li9e4447682012-10-11 13:49:49 +1100955 if (test_bit(R5_Discard, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -0500956 op = REQ_OP_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200957 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -0500958 op = REQ_OP_READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100959 else if (test_and_clear_bit(R5_WantReplace,
960 &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500961 op = REQ_OP_WRITE;
NeilBrown9a3e1102011-12-23 10:17:53 +1100962 replace_only = 1;
963 } else
Dan Williams91c00922007-01-02 13:52:30 -0700964 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000965 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -0500966 op_flags |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700967
shli@kernel.org59fc6302014-12-15 12:57:03 +1100968again:
Dan Williams91c00922007-01-02 13:52:30 -0700969 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100970 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700971
Dan Williams91c00922007-01-02 13:52:30 -0700972 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100973 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100974 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
975 rdev = rcu_dereference(conf->disks[i].rdev);
976 if (!rdev) {
977 rdev = rrdev;
978 rrdev = NULL;
979 }
Mike Christie796a5cf2016-06-05 14:32:07 -0500980 if (op_is_write(op)) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100981 if (replace_only)
982 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100983 if (rdev == rrdev)
984 /* We raced and saw duplicates */
985 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100986 } else {
shli@kernel.org59fc6302014-12-15 12:57:03 +1100987 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100988 rdev = rrdev;
989 rrdev = NULL;
990 }
NeilBrown977df362011-12-23 10:17:53 +1100991
Dan Williams91c00922007-01-02 13:52:30 -0700992 if (rdev && test_bit(Faulty, &rdev->flags))
993 rdev = NULL;
994 if (rdev)
995 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100996 if (rrdev && test_bit(Faulty, &rrdev->flags))
997 rrdev = NULL;
998 if (rrdev)
999 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -07001000 rcu_read_unlock();
1001
NeilBrown73e92e52011-07-28 11:39:22 +10001002 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +11001003 * need to check for writes. We never accept write errors
1004 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +10001005 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001006 while (op_is_write(op) && rdev &&
NeilBrown73e92e52011-07-28 11:39:22 +10001007 test_bit(WriteErrorSeen, &rdev->flags)) {
1008 sector_t first_bad;
1009 int bad_sectors;
1010 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
1011 &first_bad, &bad_sectors);
1012 if (!bad)
1013 break;
1014
1015 if (bad < 0) {
1016 set_bit(BlockedBadBlocks, &rdev->flags);
1017 if (!conf->mddev->external &&
Shaohua Li29530792016-12-08 15:48:19 -08001018 conf->mddev->sb_flags) {
NeilBrown73e92e52011-07-28 11:39:22 +10001019 /* It is very unlikely, but we might
1020 * still need to write out the
1021 * bad block log - better give it
1022 * a chance*/
1023 md_check_recovery(conf->mddev);
1024 }
majianpeng18507532012-07-03 12:11:54 +10001025 /*
1026 * Because md_wait_for_blocked_rdev
1027 * will dec nr_pending, we must
1028 * increment it first.
1029 */
1030 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +10001031 md_wait_for_blocked_rdev(rdev, conf->mddev);
1032 } else {
1033 /* Acknowledged bad block - skip the write */
1034 rdev_dec_pending(rdev, conf->mddev);
1035 rdev = NULL;
1036 }
1037 }
1038
Dan Williams91c00922007-01-02 13:52:30 -07001039 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001040 if (s->syncing || s->expanding || s->expanded
1041 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -07001042 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1043
Dan Williams2b7497f2008-06-28 08:31:52 +10001044 set_bit(STRIPE_IO_STARTED, &sh->state);
1045
Dan Williams91c00922007-01-02 13:52:30 -07001046 bi->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05001047 bio_set_op_attrs(bi, op, op_flags);
1048 bi->bi_end_io = op_is_write(op)
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001049 ? raid5_end_write_request
1050 : raid5_end_read_request;
1051 bi->bi_private = sh;
1052
Mike Christie6296b962016-06-05 14:32:21 -05001053 pr_debug("%s: for %llu schedule op %d on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001054 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001055 bi->bi_opf, i);
Dan Williams91c00922007-01-02 13:52:30 -07001056 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001057 if (sh != head_sh)
1058 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001059 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001060 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001061 + rdev->new_data_offset);
1062 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001063 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001064 + rdev->data_offset);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001065 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
Jens Axboe1eff9d32016-08-05 15:35:16 -06001066 bi->bi_opf |= REQ_NOMERGE;
majianpeng3f9e7c12012-07-31 10:04:21 +10001067
Shaohua Lid592a992014-05-21 17:57:44 +08001068 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1069 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
Song Liu86aa1392017-01-12 17:22:41 -08001070
1071 if (!op_is_write(op) &&
1072 test_bit(R5_InJournal, &sh->dev[i].flags))
1073 /*
1074 * issuing read for a page in journal, this
1075 * must be preparing for prexor in rmw; read
1076 * the data into orig_page
1077 */
1078 sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
1079 else
1080 sh->dev[i].vec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001081 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001082 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1083 bi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001084 bi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001085 /*
1086 * If this is discard request, set bi_vcnt 0. We don't
1087 * want to confuse SCSI because SCSI will replace payload
1088 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001089 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001090 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +11001091 if (rrdev)
1092 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001093
1094 if (conf->mddev->gendisk)
1095 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
1096 bi, disk_devt(conf->mddev->gendisk),
1097 sh->dev[i].sector);
Shaohua Li765d7042017-01-04 09:33:23 -08001098 defer_bio_issue(conf, bi);
NeilBrown977df362011-12-23 10:17:53 +11001099 }
1100 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001101 if (s->syncing || s->expanding || s->expanded
1102 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +11001103 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
1104
1105 set_bit(STRIPE_IO_STARTED, &sh->state);
1106
1107 rbi->bi_bdev = rrdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05001108 bio_set_op_attrs(rbi, op, op_flags);
1109 BUG_ON(!op_is_write(op));
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001110 rbi->bi_end_io = raid5_end_write_request;
1111 rbi->bi_private = sh;
1112
Mike Christie6296b962016-06-05 14:32:21 -05001113 pr_debug("%s: for %llu schedule op %d on "
NeilBrown977df362011-12-23 10:17:53 +11001114 "replacement disc %d\n",
1115 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001116 rbi->bi_opf, i);
NeilBrown977df362011-12-23 10:17:53 +11001117 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001118 if (sh != head_sh)
1119 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001120 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001121 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001122 + rrdev->new_data_offset);
1123 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001124 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001125 + rrdev->data_offset);
Shaohua Lid592a992014-05-21 17:57:44 +08001126 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1127 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1128 sh->dev[i].rvec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001129 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +11001130 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1131 rbi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001132 rbi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001133 /*
1134 * If this is discard request, set bi_vcnt 0. We don't
1135 * want to confuse SCSI because SCSI will replace payload
1136 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001137 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001138 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001139 if (conf->mddev->gendisk)
1140 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
1141 rbi, disk_devt(conf->mddev->gendisk),
1142 sh->dev[i].sector);
Shaohua Li765d7042017-01-04 09:33:23 -08001143 defer_bio_issue(conf, rbi);
NeilBrown977df362011-12-23 10:17:53 +11001144 }
1145 if (!rdev && !rrdev) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001146 if (op_is_write(op))
Dan Williams91c00922007-01-02 13:52:30 -07001147 set_bit(STRIPE_DEGRADED, &sh->state);
Mike Christie6296b962016-06-05 14:32:21 -05001148 pr_debug("skip op %d on disc %d for sector %llu\n",
Jens Axboe1eff9d32016-08-05 15:35:16 -06001149 bi->bi_opf, i, (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001150 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1151 set_bit(STRIPE_HANDLE, &sh->state);
1152 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001153
1154 if (!head_sh->batch_head)
1155 continue;
1156 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1157 batch_list);
1158 if (sh != head_sh)
1159 goto again;
Dan Williams91c00922007-01-02 13:52:30 -07001160 }
1161}
1162
1163static struct dma_async_tx_descriptor *
Shaohua Lid592a992014-05-21 17:57:44 +08001164async_copy_data(int frombio, struct bio *bio, struct page **page,
1165 sector_t sector, struct dma_async_tx_descriptor *tx,
Song Liu1e6d6902016-11-17 15:24:39 -08001166 struct stripe_head *sh, int no_skipcopy)
Dan Williams91c00922007-01-02 13:52:30 -07001167{
Kent Overstreet79886132013-11-23 17:19:00 -08001168 struct bio_vec bvl;
1169 struct bvec_iter iter;
Dan Williams91c00922007-01-02 13:52:30 -07001170 struct page *bio_page;
Dan Williams91c00922007-01-02 13:52:30 -07001171 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -07001172 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -07001173 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001174
Kent Overstreet4f024f32013-10-11 15:44:27 -07001175 if (bio->bi_iter.bi_sector >= sector)
1176 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
Dan Williams91c00922007-01-02 13:52:30 -07001177 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001178 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -07001179
Dan Williams0403e382009-09-08 17:42:50 -07001180 if (frombio)
1181 flags |= ASYNC_TX_FENCE;
1182 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1183
Kent Overstreet79886132013-11-23 17:19:00 -08001184 bio_for_each_segment(bvl, bio, iter) {
1185 int len = bvl.bv_len;
Dan Williams91c00922007-01-02 13:52:30 -07001186 int clen;
1187 int b_offset = 0;
1188
1189 if (page_offset < 0) {
1190 b_offset = -page_offset;
1191 page_offset += b_offset;
1192 len -= b_offset;
1193 }
1194
1195 if (len > 0 && page_offset + len > STRIPE_SIZE)
1196 clen = STRIPE_SIZE - page_offset;
1197 else
1198 clen = len;
1199
1200 if (clen > 0) {
Kent Overstreet79886132013-11-23 17:19:00 -08001201 b_offset += bvl.bv_offset;
1202 bio_page = bvl.bv_page;
Shaohua Lid592a992014-05-21 17:57:44 +08001203 if (frombio) {
1204 if (sh->raid_conf->skip_copy &&
1205 b_offset == 0 && page_offset == 0 &&
Song Liu1e6d6902016-11-17 15:24:39 -08001206 clen == STRIPE_SIZE &&
1207 !no_skipcopy)
Shaohua Lid592a992014-05-21 17:57:44 +08001208 *page = bio_page;
1209 else
1210 tx = async_memcpy(*page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001211 b_offset, clen, &submit);
Shaohua Lid592a992014-05-21 17:57:44 +08001212 } else
1213 tx = async_memcpy(bio_page, *page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001214 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001215 }
Dan Williamsa08abd82009-06-03 11:43:59 -07001216 /* chain the operations */
1217 submit.depend_tx = tx;
1218
Dan Williams91c00922007-01-02 13:52:30 -07001219 if (clen < len) /* hit end of page */
1220 break;
1221 page_offset += len;
1222 }
1223
1224 return tx;
1225}
1226
1227static void ops_complete_biofill(void *stripe_head_ref)
1228{
1229 struct stripe_head *sh = stripe_head_ref;
NeilBrown34a6f802015-08-14 12:07:57 +10001230 struct bio_list return_bi = BIO_EMPTY_LIST;
Dan Williamse4d84902007-09-24 10:06:13 -07001231 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001232
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001233 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001234 (unsigned long long)sh->sector);
1235
1236 /* clear completed biofills */
1237 for (i = sh->disks; i--; ) {
1238 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001239
1240 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001241 /* and check if we need to reply to a read request,
1242 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001243 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001244 */
1245 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001246 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001247
Dan Williams91c00922007-01-02 13:52:30 -07001248 BUG_ON(!dev->read);
1249 rbi = dev->read;
1250 dev->read = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001251 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001252 dev->sector + STRIPE_SECTORS) {
1253 rbi2 = r5_next_bio(rbi, dev->sector);
NeilBrown34a6f802015-08-14 12:07:57 +10001254 if (!raid5_dec_bi_active_stripes(rbi))
1255 bio_list_add(&return_bi, rbi);
Dan Williams91c00922007-01-02 13:52:30 -07001256 rbi = rbi2;
1257 }
1258 }
1259 }
Dan Williams83de75c2008-06-28 08:31:58 +10001260 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001261
NeilBrown34a6f802015-08-14 12:07:57 +10001262 return_io(&return_bi);
Dan Williams91c00922007-01-02 13:52:30 -07001263
Dan Williamse4d84902007-09-24 10:06:13 -07001264 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001265 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001266}
1267
1268static void ops_run_biofill(struct stripe_head *sh)
1269{
1270 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001271 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001272 int i;
1273
shli@kernel.org59fc6302014-12-15 12:57:03 +11001274 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001275 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001276 (unsigned long long)sh->sector);
1277
1278 for (i = sh->disks; i--; ) {
1279 struct r5dev *dev = &sh->dev[i];
1280 if (test_bit(R5_Wantfill, &dev->flags)) {
1281 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001282 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001283 dev->read = rbi = dev->toread;
1284 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001285 spin_unlock_irq(&sh->stripe_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001286 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001287 dev->sector + STRIPE_SECTORS) {
Shaohua Lid592a992014-05-21 17:57:44 +08001288 tx = async_copy_data(0, rbi, &dev->page,
Song Liu1e6d6902016-11-17 15:24:39 -08001289 dev->sector, tx, sh, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001290 rbi = r5_next_bio(rbi, dev->sector);
1291 }
1292 }
1293 }
1294
1295 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001296 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1297 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001298}
1299
Dan Williams4e7d2c02009-08-29 19:13:11 -07001300static void mark_target_uptodate(struct stripe_head *sh, int target)
1301{
1302 struct r5dev *tgt;
1303
1304 if (target < 0)
1305 return;
1306
1307 tgt = &sh->dev[target];
1308 set_bit(R5_UPTODATE, &tgt->flags);
1309 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1310 clear_bit(R5_Wantcompute, &tgt->flags);
1311}
1312
Dan Williamsac6b53b2009-07-14 13:40:19 -07001313static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001314{
1315 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001316
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001317 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001318 (unsigned long long)sh->sector);
1319
Dan Williamsac6b53b2009-07-14 13:40:19 -07001320 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001321 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001322 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001323
Dan Williamsecc65c92008-06-28 08:31:57 +10001324 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1325 if (sh->check_state == check_state_compute_run)
1326 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001327 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001328 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001329}
1330
Dan Williamsd6f38f32009-07-14 11:50:52 -07001331/* return a pointer to the address conversion region of the scribble buffer */
1332static addr_conv_t *to_addr_conv(struct stripe_head *sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001333 struct raid5_percpu *percpu, int i)
Dan Williams91c00922007-01-02 13:52:30 -07001334{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001335 void *addr;
1336
1337 addr = flex_array_get(percpu->scribble, i);
1338 return addr + sizeof(struct page *) * (sh->disks + 2);
1339}
1340
1341/* return a pointer to the address conversion region of the scribble buffer */
1342static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
1343{
1344 void *addr;
1345
1346 addr = flex_array_get(percpu->scribble, i);
1347 return addr;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001348}
1349
1350static struct dma_async_tx_descriptor *
1351ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1352{
Dan Williams91c00922007-01-02 13:52:30 -07001353 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001354 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001355 int target = sh->ops.target;
1356 struct r5dev *tgt = &sh->dev[target];
1357 struct page *xor_dest = tgt->page;
1358 int count = 0;
1359 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001360 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001361 int i;
1362
shli@kernel.org59fc6302014-12-15 12:57:03 +11001363 BUG_ON(sh->batch_head);
1364
Dan Williams91c00922007-01-02 13:52:30 -07001365 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001366 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001367 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1368
1369 for (i = disks; i--; )
1370 if (i != target)
1371 xor_srcs[count++] = sh->dev[i].page;
1372
1373 atomic_inc(&sh->count);
1374
Dan Williams0403e382009-09-08 17:42:50 -07001375 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001376 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
Dan Williams91c00922007-01-02 13:52:30 -07001377 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -07001378 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001379 else
Dan Williamsa08abd82009-06-03 11:43:59 -07001380 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001381
Dan Williams91c00922007-01-02 13:52:30 -07001382 return tx;
1383}
1384
Dan Williamsac6b53b2009-07-14 13:40:19 -07001385/* set_syndrome_sources - populate source buffers for gen_syndrome
1386 * @srcs - (struct page *) array of size sh->disks
1387 * @sh - stripe_head to parse
1388 *
1389 * Populates srcs in proper layout order for the stripe and returns the
1390 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1391 * destination buffer is recorded in srcs[count] and the Q destination
1392 * is recorded in srcs[count+1]].
1393 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001394static int set_syndrome_sources(struct page **srcs,
1395 struct stripe_head *sh,
1396 int srctype)
Dan Williamsac6b53b2009-07-14 13:40:19 -07001397{
1398 int disks = sh->disks;
1399 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1400 int d0_idx = raid6_d0(sh);
1401 int count;
1402 int i;
1403
1404 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001405 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001406
1407 count = 0;
1408 i = d0_idx;
1409 do {
1410 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001411 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001412
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001413 if (i == sh->qd_idx || i == sh->pd_idx ||
1414 (srctype == SYNDROME_SRC_ALL) ||
1415 (srctype == SYNDROME_SRC_WANT_DRAIN &&
Song Liu1e6d6902016-11-17 15:24:39 -08001416 (test_bit(R5_Wantdrain, &dev->flags) ||
1417 test_bit(R5_InJournal, &dev->flags))) ||
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001418 (srctype == SYNDROME_SRC_WRITTEN &&
Song Liu09777622017-03-13 13:44:35 -07001419 (dev->written ||
1420 test_bit(R5_InJournal, &dev->flags)))) {
Song Liu1e6d6902016-11-17 15:24:39 -08001421 if (test_bit(R5_InJournal, &dev->flags))
1422 srcs[slot] = sh->dev[i].orig_page;
1423 else
1424 srcs[slot] = sh->dev[i].page;
1425 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001426 i = raid6_next_disk(i, disks);
1427 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001428
NeilBrowne4424fe2009-10-16 16:27:34 +11001429 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001430}
1431
1432static struct dma_async_tx_descriptor *
1433ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1434{
1435 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001436 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001437 int target;
1438 int qd_idx = sh->qd_idx;
1439 struct dma_async_tx_descriptor *tx;
1440 struct async_submit_ctl submit;
1441 struct r5dev *tgt;
1442 struct page *dest;
1443 int i;
1444 int count;
1445
shli@kernel.org59fc6302014-12-15 12:57:03 +11001446 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001447 if (sh->ops.target < 0)
1448 target = sh->ops.target2;
1449 else if (sh->ops.target2 < 0)
1450 target = sh->ops.target;
1451 else
1452 /* we should only have one valid target */
1453 BUG();
1454 BUG_ON(target < 0);
1455 pr_debug("%s: stripe %llu block: %d\n",
1456 __func__, (unsigned long long)sh->sector, target);
1457
1458 tgt = &sh->dev[target];
1459 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1460 dest = tgt->page;
1461
1462 atomic_inc(&sh->count);
1463
1464 if (target == qd_idx) {
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001465 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001466 blocks[count] = NULL; /* regenerating p is not necessary */
1467 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001468 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1469 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001470 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001471 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1472 } else {
1473 /* Compute any data- or p-drive using XOR */
1474 count = 0;
1475 for (i = disks; i-- ; ) {
1476 if (i == target || i == qd_idx)
1477 continue;
1478 blocks[count++] = sh->dev[i].page;
1479 }
1480
Dan Williams0403e382009-09-08 17:42:50 -07001481 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1482 NULL, ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001483 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001484 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1485 }
1486
1487 return tx;
1488}
1489
1490static struct dma_async_tx_descriptor *
1491ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1492{
1493 int i, count, disks = sh->disks;
1494 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1495 int d0_idx = raid6_d0(sh);
1496 int faila = -1, failb = -1;
1497 int target = sh->ops.target;
1498 int target2 = sh->ops.target2;
1499 struct r5dev *tgt = &sh->dev[target];
1500 struct r5dev *tgt2 = &sh->dev[target2];
1501 struct dma_async_tx_descriptor *tx;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001502 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001503 struct async_submit_ctl submit;
1504
shli@kernel.org59fc6302014-12-15 12:57:03 +11001505 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001506 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1507 __func__, (unsigned long long)sh->sector, target, target2);
1508 BUG_ON(target < 0 || target2 < 0);
1509 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1510 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1511
Dan Williams6c910a72009-09-16 12:24:54 -07001512 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001513 * slot number conversion for 'faila' and 'failb'
1514 */
1515 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001516 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001517 count = 0;
1518 i = d0_idx;
1519 do {
1520 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1521
1522 blocks[slot] = sh->dev[i].page;
1523
1524 if (i == target)
1525 faila = slot;
1526 if (i == target2)
1527 failb = slot;
1528 i = raid6_next_disk(i, disks);
1529 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001530
1531 BUG_ON(faila == failb);
1532 if (failb < faila)
1533 swap(faila, failb);
1534 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1535 __func__, (unsigned long long)sh->sector, faila, failb);
1536
1537 atomic_inc(&sh->count);
1538
1539 if (failb == syndrome_disks+1) {
1540 /* Q disk is one of the missing disks */
1541 if (faila == syndrome_disks) {
1542 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001543 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1544 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001545 to_addr_conv(sh, percpu, 0));
NeilBrowne4424fe2009-10-16 16:27:34 +11001546 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001547 STRIPE_SIZE, &submit);
1548 } else {
1549 struct page *dest;
1550 int data_target;
1551 int qd_idx = sh->qd_idx;
1552
1553 /* Missing D+Q: recompute D from P, then recompute Q */
1554 if (target == qd_idx)
1555 data_target = target2;
1556 else
1557 data_target = target;
1558
1559 count = 0;
1560 for (i = disks; i-- ; ) {
1561 if (i == data_target || i == qd_idx)
1562 continue;
1563 blocks[count++] = sh->dev[i].page;
1564 }
1565 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001566 init_async_submit(&submit,
1567 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1568 NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001569 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001570 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1571 &submit);
1572
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001573 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williams0403e382009-09-08 17:42:50 -07001574 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1575 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001576 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001577 return async_gen_syndrome(blocks, 0, count+2,
1578 STRIPE_SIZE, &submit);
1579 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001580 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001581 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1582 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001583 to_addr_conv(sh, percpu, 0));
Dan Williams6c910a72009-09-16 12:24:54 -07001584 if (failb == syndrome_disks) {
1585 /* We're missing D+P. */
1586 return async_raid6_datap_recov(syndrome_disks+2,
1587 STRIPE_SIZE, faila,
1588 blocks, &submit);
1589 } else {
1590 /* We're missing D+D. */
1591 return async_raid6_2data_recov(syndrome_disks+2,
1592 STRIPE_SIZE, faila, failb,
1593 blocks, &submit);
1594 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001595 }
1596}
1597
Dan Williams91c00922007-01-02 13:52:30 -07001598static void ops_complete_prexor(void *stripe_head_ref)
1599{
1600 struct stripe_head *sh = stripe_head_ref;
1601
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001602 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001603 (unsigned long long)sh->sector);
Song Liu1e6d6902016-11-17 15:24:39 -08001604
1605 if (r5c_is_writeback(sh->raid_conf->log))
1606 /*
1607 * raid5-cache write back uses orig_page during prexor.
1608 * After prexor, it is time to free orig_page
1609 */
1610 r5c_release_extra_page(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001611}
1612
1613static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001614ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1615 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001616{
Dan Williams91c00922007-01-02 13:52:30 -07001617 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001618 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001619 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001620 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001621
1622 /* existing parity data subtracted */
1623 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1624
shli@kernel.org59fc6302014-12-15 12:57:03 +11001625 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001626 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001627 (unsigned long long)sh->sector);
1628
1629 for (i = disks; i--; ) {
1630 struct r5dev *dev = &sh->dev[i];
1631 /* Only process blocks that are known to be uptodate */
Song Liu1e6d6902016-11-17 15:24:39 -08001632 if (test_bit(R5_InJournal, &dev->flags))
1633 xor_srcs[count++] = dev->orig_page;
1634 else if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001635 xor_srcs[count++] = dev->page;
1636 }
1637
Dan Williams0403e382009-09-08 17:42:50 -07001638 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001639 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Dan Williamsa08abd82009-06-03 11:43:59 -07001640 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001641
1642 return tx;
1643}
1644
1645static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001646ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1647 struct dma_async_tx_descriptor *tx)
1648{
1649 struct page **blocks = to_addr_page(percpu, 0);
1650 int count;
1651 struct async_submit_ctl submit;
1652
1653 pr_debug("%s: stripe %llu\n", __func__,
1654 (unsigned long long)sh->sector);
1655
1656 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN);
1657
1658 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1659 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1660 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1661
1662 return tx;
1663}
1664
1665static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001666ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001667{
Song Liu1e6d6902016-11-17 15:24:39 -08001668 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001669 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001670 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001671 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -07001672
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001673 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001674 (unsigned long long)sh->sector);
1675
1676 for (i = disks; i--; ) {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001677 struct r5dev *dev;
Dan Williams91c00922007-01-02 13:52:30 -07001678 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001679
shli@kernel.org59fc6302014-12-15 12:57:03 +11001680 sh = head_sh;
1681 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001682 struct bio *wbi;
1683
shli@kernel.org59fc6302014-12-15 12:57:03 +11001684again:
1685 dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001686 /*
1687 * clear R5_InJournal, so when rewriting a page in
1688 * journal, it is not skipped by r5l_log_stripe()
1689 */
1690 clear_bit(R5_InJournal, &dev->flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10001691 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001692 chosen = dev->towrite;
1693 dev->towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11001694 sh->overwrite_disks = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001695 BUG_ON(dev->written);
1696 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001697 spin_unlock_irq(&sh->stripe_lock);
Shaohua Lid592a992014-05-21 17:57:44 +08001698 WARN_ON(dev->page != dev->orig_page);
Dan Williams91c00922007-01-02 13:52:30 -07001699
Kent Overstreet4f024f32013-10-11 15:44:27 -07001700 while (wbi && wbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001701 dev->sector + STRIPE_SECTORS) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001702 if (wbi->bi_opf & REQ_FUA)
Tejun Heoe9c74692010-09-03 11:56:18 +02001703 set_bit(R5_WantFUA, &dev->flags);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001704 if (wbi->bi_opf & REQ_SYNC)
Shaohua Libc0934f2012-05-22 13:55:05 +10001705 set_bit(R5_SyncIO, &dev->flags);
Mike Christie796a5cf2016-06-05 14:32:07 -05001706 if (bio_op(wbi) == REQ_OP_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001707 set_bit(R5_Discard, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08001708 else {
1709 tx = async_copy_data(1, wbi, &dev->page,
Song Liu1e6d6902016-11-17 15:24:39 -08001710 dev->sector, tx, sh,
1711 r5c_is_writeback(conf->log));
1712 if (dev->page != dev->orig_page &&
1713 !r5c_is_writeback(conf->log)) {
Shaohua Lid592a992014-05-21 17:57:44 +08001714 set_bit(R5_SkipCopy, &dev->flags);
1715 clear_bit(R5_UPTODATE, &dev->flags);
1716 clear_bit(R5_OVERWRITE, &dev->flags);
1717 }
1718 }
Dan Williams91c00922007-01-02 13:52:30 -07001719 wbi = r5_next_bio(wbi, dev->sector);
1720 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001721
1722 if (head_sh->batch_head) {
1723 sh = list_first_entry(&sh->batch_list,
1724 struct stripe_head,
1725 batch_list);
1726 if (sh == head_sh)
1727 continue;
1728 goto again;
1729 }
Dan Williams91c00922007-01-02 13:52:30 -07001730 }
1731 }
1732
1733 return tx;
1734}
1735
Dan Williamsac6b53b2009-07-14 13:40:19 -07001736static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001737{
1738 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001739 int disks = sh->disks;
1740 int pd_idx = sh->pd_idx;
1741 int qd_idx = sh->qd_idx;
1742 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001743 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001744
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001745 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001746 (unsigned long long)sh->sector);
1747
Shaohua Libc0934f2012-05-22 13:55:05 +10001748 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001749 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001750 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001751 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001752 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001753
Dan Williams91c00922007-01-02 13:52:30 -07001754 for (i = disks; i--; ) {
1755 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001756
Tejun Heoe9c74692010-09-03 11:56:18 +02001757 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Lid592a992014-05-21 17:57:44 +08001758 if (!discard && !test_bit(R5_SkipCopy, &dev->flags))
Shaohua Li9e4447682012-10-11 13:49:49 +11001759 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001760 if (fua)
1761 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001762 if (sync)
1763 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001764 }
Dan Williams91c00922007-01-02 13:52:30 -07001765 }
1766
Dan Williamsd8ee0722008-06-28 08:32:06 +10001767 if (sh->reconstruct_state == reconstruct_state_drain_run)
1768 sh->reconstruct_state = reconstruct_state_drain_result;
1769 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1770 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1771 else {
1772 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1773 sh->reconstruct_state = reconstruct_state_result;
1774 }
Dan Williams91c00922007-01-02 13:52:30 -07001775
1776 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001777 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001778}
1779
1780static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001781ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1782 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001783{
Dan Williams91c00922007-01-02 13:52:30 -07001784 int disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001785 struct page **xor_srcs;
Dan Williamsa08abd82009-06-03 11:43:59 -07001786 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001787 int count, pd_idx = sh->pd_idx, i;
Dan Williams91c00922007-01-02 13:52:30 -07001788 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001789 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001790 unsigned long flags;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001791 int j = 0;
1792 struct stripe_head *head_sh = sh;
1793 int last_stripe;
Dan Williams91c00922007-01-02 13:52:30 -07001794
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001795 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001796 (unsigned long long)sh->sector);
1797
Shaohua Li620125f2012-10-11 13:49:05 +11001798 for (i = 0; i < sh->disks; i++) {
1799 if (pd_idx == i)
1800 continue;
1801 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1802 break;
1803 }
1804 if (i >= sh->disks) {
1805 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001806 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1807 ops_complete_reconstruct(sh);
1808 return;
1809 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001810again:
1811 count = 0;
1812 xor_srcs = to_addr_page(percpu, j);
Dan Williams91c00922007-01-02 13:52:30 -07001813 /* check if prexor is active which means only process blocks
1814 * that are part of a read-modify-write (written)
1815 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001816 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001817 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001818 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1819 for (i = disks; i--; ) {
1820 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001821 if (head_sh->dev[i].written ||
1822 test_bit(R5_InJournal, &head_sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -07001823 xor_srcs[count++] = dev->page;
1824 }
1825 } else {
1826 xor_dest = sh->dev[pd_idx].page;
1827 for (i = disks; i--; ) {
1828 struct r5dev *dev = &sh->dev[i];
1829 if (i != pd_idx)
1830 xor_srcs[count++] = dev->page;
1831 }
1832 }
1833
Dan Williams91c00922007-01-02 13:52:30 -07001834 /* 1/ if we prexor'd then the dest is reused as a source
1835 * 2/ if we did not prexor then we are redoing the parity
1836 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1837 * for the synchronous xor case
1838 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001839 last_stripe = !head_sh->batch_head ||
1840 list_first_entry(&sh->batch_list,
1841 struct stripe_head, batch_list) == head_sh;
1842 if (last_stripe) {
1843 flags = ASYNC_TX_ACK |
1844 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
Dan Williams91c00922007-01-02 13:52:30 -07001845
shli@kernel.org59fc6302014-12-15 12:57:03 +11001846 atomic_inc(&head_sh->count);
1847 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
1848 to_addr_conv(sh, percpu, j));
1849 } else {
1850 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
1851 init_async_submit(&submit, flags, tx, NULL, NULL,
1852 to_addr_conv(sh, percpu, j));
1853 }
Dan Williams91c00922007-01-02 13:52:30 -07001854
Dan Williamsa08abd82009-06-03 11:43:59 -07001855 if (unlikely(count == 1))
1856 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1857 else
1858 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001859 if (!last_stripe) {
1860 j++;
1861 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1862 batch_list);
1863 goto again;
1864 }
Dan Williams91c00922007-01-02 13:52:30 -07001865}
1866
Dan Williamsac6b53b2009-07-14 13:40:19 -07001867static void
1868ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1869 struct dma_async_tx_descriptor *tx)
1870{
1871 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001872 struct page **blocks;
1873 int count, i, j = 0;
1874 struct stripe_head *head_sh = sh;
1875 int last_stripe;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001876 int synflags;
1877 unsigned long txflags;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001878
1879 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1880
Shaohua Li620125f2012-10-11 13:49:05 +11001881 for (i = 0; i < sh->disks; i++) {
1882 if (sh->pd_idx == i || sh->qd_idx == i)
1883 continue;
1884 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1885 break;
1886 }
1887 if (i >= sh->disks) {
1888 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001889 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1890 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1891 ops_complete_reconstruct(sh);
1892 return;
1893 }
1894
shli@kernel.org59fc6302014-12-15 12:57:03 +11001895again:
1896 blocks = to_addr_page(percpu, j);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001897
1898 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1899 synflags = SYNDROME_SRC_WRITTEN;
1900 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
1901 } else {
1902 synflags = SYNDROME_SRC_ALL;
1903 txflags = ASYNC_TX_ACK;
1904 }
1905
1906 count = set_syndrome_sources(blocks, sh, synflags);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001907 last_stripe = !head_sh->batch_head ||
1908 list_first_entry(&sh->batch_list,
1909 struct stripe_head, batch_list) == head_sh;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001910
shli@kernel.org59fc6302014-12-15 12:57:03 +11001911 if (last_stripe) {
1912 atomic_inc(&head_sh->count);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001913 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
shli@kernel.org59fc6302014-12-15 12:57:03 +11001914 head_sh, to_addr_conv(sh, percpu, j));
1915 } else
1916 init_async_submit(&submit, 0, tx, NULL, NULL,
1917 to_addr_conv(sh, percpu, j));
Shaohua Li48769692015-05-13 09:30:08 -07001918 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001919 if (!last_stripe) {
1920 j++;
1921 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1922 batch_list);
1923 goto again;
1924 }
Dan Williams91c00922007-01-02 13:52:30 -07001925}
1926
1927static void ops_complete_check(void *stripe_head_ref)
1928{
1929 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001930
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001931 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001932 (unsigned long long)sh->sector);
1933
Dan Williamsecc65c92008-06-28 08:31:57 +10001934 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001935 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001936 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001937}
1938
Dan Williamsac6b53b2009-07-14 13:40:19 -07001939static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001940{
Dan Williams91c00922007-01-02 13:52:30 -07001941 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001942 int pd_idx = sh->pd_idx;
1943 int qd_idx = sh->qd_idx;
1944 struct page *xor_dest;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001945 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001946 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001947 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001948 int count;
1949 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001950
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001951 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001952 (unsigned long long)sh->sector);
1953
shli@kernel.org59fc6302014-12-15 12:57:03 +11001954 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001955 count = 0;
1956 xor_dest = sh->dev[pd_idx].page;
1957 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001958 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001959 if (i == pd_idx || i == qd_idx)
1960 continue;
1961 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001962 }
1963
Dan Williamsd6f38f32009-07-14 11:50:52 -07001964 init_async_submit(&submit, 0, NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001965 to_addr_conv(sh, percpu, 0));
Dan Williams099f53c2009-04-08 14:28:37 -07001966 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001967 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001968
Dan Williams91c00922007-01-02 13:52:30 -07001969 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001970 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1971 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001972}
1973
Dan Williamsac6b53b2009-07-14 13:40:19 -07001974static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1975{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001976 struct page **srcs = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001977 struct async_submit_ctl submit;
1978 int count;
1979
1980 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1981 (unsigned long long)sh->sector, checkp);
1982
shli@kernel.org59fc6302014-12-15 12:57:03 +11001983 BUG_ON(sh->batch_head);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001984 count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001985 if (!checkp)
1986 srcs[count] = NULL;
1987
1988 atomic_inc(&sh->count);
1989 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001990 sh, to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001991 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1992 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1993}
1994
NeilBrown51acbce2013-02-28 09:08:34 +11001995static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001996{
1997 int overlap_clear = 0, i, disks = sh->disks;
1998 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001999 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002000 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002001 struct raid5_percpu *percpu;
2002 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07002003
Dan Williamsd6f38f32009-07-14 11:50:52 -07002004 cpu = get_cpu();
2005 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10002006 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07002007 ops_run_biofill(sh);
2008 overlap_clear++;
2009 }
2010
Dan Williams7b3a8712008-06-28 08:32:09 +10002011 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07002012 if (level < 6)
2013 tx = ops_run_compute5(sh, percpu);
2014 else {
2015 if (sh->ops.target2 < 0 || sh->ops.target < 0)
2016 tx = ops_run_compute6_1(sh, percpu);
2017 else
2018 tx = ops_run_compute6_2(sh, percpu);
2019 }
2020 /* terminate the chain if reconstruct is not set to be run */
2021 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10002022 async_tx_ack(tx);
2023 }
Dan Williams91c00922007-01-02 13:52:30 -07002024
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002025 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
2026 if (level < 6)
2027 tx = ops_run_prexor5(sh, percpu, tx);
2028 else
2029 tx = ops_run_prexor6(sh, percpu, tx);
2030 }
Dan Williams91c00922007-01-02 13:52:30 -07002031
Dan Williams600aa102008-06-28 08:32:05 +10002032 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10002033 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07002034 overlap_clear++;
2035 }
2036
Dan Williamsac6b53b2009-07-14 13:40:19 -07002037 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
2038 if (level < 6)
2039 ops_run_reconstruct5(sh, percpu, tx);
2040 else
2041 ops_run_reconstruct6(sh, percpu, tx);
2042 }
Dan Williams91c00922007-01-02 13:52:30 -07002043
Dan Williamsac6b53b2009-07-14 13:40:19 -07002044 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
2045 if (sh->check_state == check_state_run)
2046 ops_run_check_p(sh, percpu);
2047 else if (sh->check_state == check_state_run_q)
2048 ops_run_check_pq(sh, percpu, 0);
2049 else if (sh->check_state == check_state_run_pq)
2050 ops_run_check_pq(sh, percpu, 1);
2051 else
2052 BUG();
2053 }
Dan Williams91c00922007-01-02 13:52:30 -07002054
shli@kernel.org59fc6302014-12-15 12:57:03 +11002055 if (overlap_clear && !sh->batch_head)
Dan Williams91c00922007-01-02 13:52:30 -07002056 for (i = disks; i--; ) {
2057 struct r5dev *dev = &sh->dev[i];
2058 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2059 wake_up(&sh->raid_conf->wait_for_overlap);
2060 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07002061 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07002062}
2063
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002064static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
2065 int disks)
NeilBrownf18c1a32015-05-08 18:19:04 +10002066{
2067 struct stripe_head *sh;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002068 int i;
NeilBrownf18c1a32015-05-08 18:19:04 +10002069
2070 sh = kmem_cache_zalloc(sc, gfp);
2071 if (sh) {
2072 spin_lock_init(&sh->stripe_lock);
2073 spin_lock_init(&sh->batch_lock);
2074 INIT_LIST_HEAD(&sh->batch_list);
2075 INIT_LIST_HEAD(&sh->lru);
Song Liua39f7af2016-11-17 15:24:40 -08002076 INIT_LIST_HEAD(&sh->r5c);
Song Liud7bd3982016-11-23 22:50:39 -08002077 INIT_LIST_HEAD(&sh->log_list);
NeilBrownf18c1a32015-05-08 18:19:04 +10002078 atomic_set(&sh->count, 1);
Song Liua39f7af2016-11-17 15:24:40 -08002079 sh->log_start = MaxSector;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002080 for (i = 0; i < disks; i++) {
2081 struct r5dev *dev = &sh->dev[i];
2082
Ming Lei3a83f462016-11-22 08:57:21 -07002083 bio_init(&dev->req, &dev->vec, 1);
2084 bio_init(&dev->rreq, &dev->rvec, 1);
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002085 }
NeilBrownf18c1a32015-05-08 18:19:04 +10002086 }
2087 return sh;
2088}
NeilBrown486f0642015-02-25 12:10:35 +11002089static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
2091 struct stripe_head *sh;
NeilBrownf18c1a32015-05-08 18:19:04 +10002092
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002093 sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08002094 if (!sh)
2095 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10002096
NeilBrown3f294f42005-11-08 21:39:25 -08002097 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08002098
NeilBrowna9683a72015-02-25 12:02:51 +11002099 if (grow_buffers(sh, gfp)) {
NeilBrowne4e11e32010-06-16 16:45:16 +10002100 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002101 kmem_cache_free(conf->slab_cache, sh);
2102 return 0;
2103 }
NeilBrown486f0642015-02-25 12:10:35 +11002104 sh->hash_lock_index =
2105 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrown3f294f42005-11-08 21:39:25 -08002106 /* we just created an active stripe so... */
NeilBrown3f294f42005-11-08 21:39:25 -08002107 atomic_inc(&conf->active_stripes);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002108
Shaohua Li6d036f72015-08-13 14:31:57 -07002109 raid5_release_stripe(sh);
NeilBrown486f0642015-02-25 12:10:35 +11002110 conf->max_nr_stripes++;
NeilBrown3f294f42005-11-08 21:39:25 -08002111 return 1;
2112}
2113
NeilBrownd1688a62011-10-11 16:49:52 +11002114static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08002115{
Christoph Lametere18b8902006-12-06 20:33:20 -08002116 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11002117 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
NeilBrownf4be6b42010-06-01 19:37:25 +10002119 if (conf->mddev->gendisk)
2120 sprintf(conf->cache_name[0],
2121 "raid%d-%s", conf->level, mdname(conf->mddev));
2122 else
2123 sprintf(conf->cache_name[0],
2124 "raid%d-%p", conf->level, conf->mddev);
2125 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
2126
NeilBrownad01c9e2006-03-27 01:18:07 -08002127 conf->active_name = 0;
2128 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002130 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (!sc)
2132 return 1;
2133 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002134 conf->pool_size = devs;
NeilBrown486f0642015-02-25 12:10:35 +11002135 while (num--)
2136 if (!grow_one_stripe(conf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 return 1;
NeilBrown486f0642015-02-25 12:10:35 +11002138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 return 0;
2140}
NeilBrown29269552006-03-27 01:18:10 -08002141
Dan Williamsd6f38f32009-07-14 11:50:52 -07002142/**
2143 * scribble_len - return the required size of the scribble region
2144 * @num - total number of disks in the array
2145 *
2146 * The size must be enough to contain:
2147 * 1/ a struct page pointer for each device in the array +2
2148 * 2/ room to convert each entry in (1) to its corresponding dma
2149 * (dma_map_page()) or page (page_address()) address.
2150 *
2151 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2152 * calculate over all devices (not just the data blocks), using zeros in place
2153 * of the P and Q blocks.
2154 */
shli@kernel.org46d5b782014-12-15 12:57:02 +11002155static struct flex_array *scribble_alloc(int num, int cnt, gfp_t flags)
Dan Williamsd6f38f32009-07-14 11:50:52 -07002156{
shli@kernel.org46d5b782014-12-15 12:57:02 +11002157 struct flex_array *ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002158 size_t len;
2159
2160 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
shli@kernel.org46d5b782014-12-15 12:57:02 +11002161 ret = flex_array_alloc(len, cnt, flags);
2162 if (!ret)
2163 return NULL;
2164 /* always prealloc all elements, so no locking is required */
2165 if (flex_array_prealloc(ret, 0, cnt, flags)) {
2166 flex_array_free(ret);
2167 return NULL;
2168 }
2169 return ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002170}
2171
NeilBrown738a2732015-05-08 18:19:39 +10002172static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2173{
2174 unsigned long cpu;
2175 int err = 0;
2176
Shaohua Li27a353c2016-02-24 17:38:28 -08002177 /*
2178 * Never shrink. And mddev_suspend() could deadlock if this is called
2179 * from raid5d. In that case, scribble_disks and scribble_sectors
2180 * should equal to new_disks and new_sectors
2181 */
2182 if (conf->scribble_disks >= new_disks &&
2183 conf->scribble_sectors >= new_sectors)
2184 return 0;
NeilBrown738a2732015-05-08 18:19:39 +10002185 mddev_suspend(conf->mddev);
2186 get_online_cpus();
2187 for_each_present_cpu(cpu) {
2188 struct raid5_percpu *percpu;
2189 struct flex_array *scribble;
2190
2191 percpu = per_cpu_ptr(conf->percpu, cpu);
2192 scribble = scribble_alloc(new_disks,
2193 new_sectors / STRIPE_SECTORS,
2194 GFP_NOIO);
2195
2196 if (scribble) {
2197 flex_array_free(percpu->scribble);
2198 percpu->scribble = scribble;
2199 } else {
2200 err = -ENOMEM;
2201 break;
2202 }
2203 }
2204 put_online_cpus();
2205 mddev_resume(conf->mddev);
Shaohua Li27a353c2016-02-24 17:38:28 -08002206 if (!err) {
2207 conf->scribble_disks = new_disks;
2208 conf->scribble_sectors = new_sectors;
2209 }
NeilBrown738a2732015-05-08 18:19:39 +10002210 return err;
2211}
2212
NeilBrownd1688a62011-10-11 16:49:52 +11002213static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08002214{
2215 /* Make all the stripes able to hold 'newsize' devices.
2216 * New slots in each stripe get 'page' set to a new page.
2217 *
2218 * This happens in stages:
2219 * 1/ create a new kmem_cache and allocate the required number of
2220 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09002221 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08002222 * to the new stripe_heads. This will have the side effect of
2223 * freezing the array as once all stripe_heads have been collected,
2224 * no IO will be possible. Old stripe heads are freed once their
2225 * pages have been transferred over, and the old kmem_cache is
2226 * freed when all stripes are done.
2227 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
2228 * we simple return a failre status - no need to clean anything up.
2229 * 4/ allocate new pages for the new slots in the new stripe_heads.
2230 * If this fails, we don't bother trying the shrink the
2231 * stripe_heads down again, we just leave them as they are.
2232 * As each stripe_head is processed the new one is released into
2233 * active service.
2234 *
2235 * Once step2 is started, we cannot afford to wait for a write,
2236 * so we use GFP_NOIO allocations.
2237 */
2238 struct stripe_head *osh, *nsh;
2239 LIST_HEAD(newstripes);
2240 struct disk_info *ndisks;
Dan Williamsb5470dc2008-06-27 21:44:04 -07002241 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08002242 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002243 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11002244 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08002245
2246 if (newsize <= conf->pool_size)
2247 return 0; /* never bother to shrink */
2248
Dan Williamsb5470dc2008-06-27 21:44:04 -07002249 err = md_allow_write(conf->mddev);
2250 if (err)
2251 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08002252
NeilBrownad01c9e2006-03-27 01:18:07 -08002253 /* Step 1 */
2254 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2255 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002256 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08002257 if (!sc)
2258 return -ENOMEM;
2259
NeilBrown2d5b5692015-07-06 12:49:23 +10002260 /* Need to ensure auto-resizing doesn't interfere */
2261 mutex_lock(&conf->cache_size_mutex);
2262
NeilBrownad01c9e2006-03-27 01:18:07 -08002263 for (i = conf->max_nr_stripes; i; i--) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002264 nsh = alloc_stripe(sc, GFP_KERNEL, newsize);
NeilBrownad01c9e2006-03-27 01:18:07 -08002265 if (!nsh)
2266 break;
2267
NeilBrownad01c9e2006-03-27 01:18:07 -08002268 nsh->raid_conf = conf;
NeilBrownad01c9e2006-03-27 01:18:07 -08002269 list_add(&nsh->lru, &newstripes);
2270 }
2271 if (i) {
2272 /* didn't get enough, give up */
2273 while (!list_empty(&newstripes)) {
2274 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2275 list_del(&nsh->lru);
2276 kmem_cache_free(sc, nsh);
2277 }
2278 kmem_cache_destroy(sc);
NeilBrown2d5b5692015-07-06 12:49:23 +10002279 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002280 return -ENOMEM;
2281 }
2282 /* Step 2 - Must use GFP_NOIO now.
2283 * OK, we have enough stripes, start collecting inactive
2284 * stripes and copying them over
2285 */
Shaohua Li566c09c2013-11-14 15:16:17 +11002286 hash = 0;
2287 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08002288 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11002289 lock_device_hash_lock(conf, hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08002290 wait_event_cmd(conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +11002291 !list_empty(conf->inactive_list + hash),
2292 unlock_device_hash_lock(conf, hash),
2293 lock_device_hash_lock(conf, hash));
2294 osh = get_free_stripe(conf, hash);
2295 unlock_device_hash_lock(conf, hash);
NeilBrownf18c1a32015-05-08 18:19:04 +10002296
Shaohua Lid592a992014-05-21 17:57:44 +08002297 for(i=0; i<conf->pool_size; i++) {
NeilBrownad01c9e2006-03-27 01:18:07 -08002298 nsh->dev[i].page = osh->dev[i].page;
Shaohua Lid592a992014-05-21 17:57:44 +08002299 nsh->dev[i].orig_page = osh->dev[i].page;
2300 }
Shaohua Li566c09c2013-11-14 15:16:17 +11002301 nsh->hash_lock_index = hash;
NeilBrownad01c9e2006-03-27 01:18:07 -08002302 kmem_cache_free(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11002303 cnt++;
2304 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2305 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2306 hash++;
2307 cnt = 0;
2308 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002309 }
2310 kmem_cache_destroy(conf->slab_cache);
2311
2312 /* Step 3.
2313 * At this point, we are holding all the stripes so the array
2314 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07002315 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08002316 */
2317 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
2318 if (ndisks) {
Song Liud7bd3982016-11-23 22:50:39 -08002319 for (i = 0; i < conf->pool_size; i++)
NeilBrownad01c9e2006-03-27 01:18:07 -08002320 ndisks[i] = conf->disks[i];
Song Liud7bd3982016-11-23 22:50:39 -08002321
2322 for (i = conf->pool_size; i < newsize; i++) {
2323 ndisks[i].extra_page = alloc_page(GFP_NOIO);
2324 if (!ndisks[i].extra_page)
2325 err = -ENOMEM;
2326 }
2327
2328 if (err) {
2329 for (i = conf->pool_size; i < newsize; i++)
2330 if (ndisks[i].extra_page)
2331 put_page(ndisks[i].extra_page);
2332 kfree(ndisks);
2333 } else {
2334 kfree(conf->disks);
2335 conf->disks = ndisks;
2336 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002337 } else
2338 err = -ENOMEM;
2339
NeilBrown2d5b5692015-07-06 12:49:23 +10002340 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002341 /* Step 4, return new stripes to service */
2342 while(!list_empty(&newstripes)) {
2343 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2344 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07002345
NeilBrownad01c9e2006-03-27 01:18:07 -08002346 for (i=conf->raid_disks; i < newsize; i++)
2347 if (nsh->dev[i].page == NULL) {
2348 struct page *p = alloc_page(GFP_NOIO);
2349 nsh->dev[i].page = p;
Shaohua Lid592a992014-05-21 17:57:44 +08002350 nsh->dev[i].orig_page = p;
NeilBrownad01c9e2006-03-27 01:18:07 -08002351 if (!p)
2352 err = -ENOMEM;
2353 }
Shaohua Li6d036f72015-08-13 14:31:57 -07002354 raid5_release_stripe(nsh);
NeilBrownad01c9e2006-03-27 01:18:07 -08002355 }
2356 /* critical section pass, GFP_NOIO no longer needed */
2357
2358 conf->slab_cache = sc;
2359 conf->active_name = 1-conf->active_name;
NeilBrown6e9eac22015-05-08 18:19:34 +10002360 if (!err)
2361 conf->pool_size = newsize;
NeilBrownad01c9e2006-03-27 01:18:07 -08002362 return err;
2363}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
NeilBrown486f0642015-02-25 12:10:35 +11002365static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
2367 struct stripe_head *sh;
NeilBrown49895bc2015-08-03 17:09:57 +10002368 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Shaohua Li566c09c2013-11-14 15:16:17 +11002370 spin_lock_irq(conf->hash_locks + hash);
2371 sh = get_free_stripe(conf, hash);
2372 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08002373 if (!sh)
2374 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002375 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10002376 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002377 kmem_cache_free(conf->slab_cache, sh);
2378 atomic_dec(&conf->active_stripes);
NeilBrown486f0642015-02-25 12:10:35 +11002379 conf->max_nr_stripes--;
NeilBrown3f294f42005-11-08 21:39:25 -08002380 return 1;
2381}
2382
NeilBrownd1688a62011-10-11 16:49:52 +11002383static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08002384{
NeilBrown486f0642015-02-25 12:10:35 +11002385 while (conf->max_nr_stripes &&
2386 drop_one_stripe(conf))
2387 ;
NeilBrown3f294f42005-11-08 21:39:25 -08002388
Julia Lawall644df1a2015-09-13 14:15:10 +02002389 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 conf->slab_cache = NULL;
2391}
2392
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002393static void raid5_end_read_request(struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
NeilBrown99c0fb52009-03-31 14:39:38 +11002395 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002396 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002397 int disks = sh->disks, i;
NeilBrownd6950432006-07-10 04:44:20 -07002398 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11002399 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10002400 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402 for (i=0 ; i<disks; i++)
2403 if (bi == &sh->dev[i].req)
2404 break;
2405
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002406 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
Dan Williams45b42332007-07-09 11:56:43 -07002407 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002408 bi->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002410 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002412 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 }
NeilBrown14a75d32011-12-23 10:17:52 +11002414 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11002415 /* If replacement finished while this request was outstanding,
2416 * 'replacement' might be NULL already.
2417 * In that case it moved down to 'rdev'.
2418 * rdev is not removed until all requests are finished.
2419 */
NeilBrown14a75d32011-12-23 10:17:52 +11002420 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002421 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11002422 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
NeilBrown05616be2012-05-21 09:27:00 +10002424 if (use_new_offset(conf, sh))
2425 s = sh->sector + rdev->new_data_offset;
2426 else
2427 s = sh->sector + rdev->data_offset;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002428 if (!bi->bi_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08002430 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11002431 /* Note that this cannot happen on a
2432 * replacement device. We just fail those on
2433 * any error
2434 */
NeilBrowncc6167b2016-11-02 14:16:50 +11002435 pr_info_ratelimited(
2436 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002437 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10002438 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002439 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10002440 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08002441 clear_bit(R5_ReadError, &sh->dev[i].flags);
2442 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002443 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2444 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2445
Song Liu86aa1392017-01-12 17:22:41 -08002446 if (test_bit(R5_InJournal, &sh->dev[i].flags))
2447 /*
2448 * end read for a page in journal, this
2449 * must be preparing for prexor in rmw
2450 */
2451 set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
2452
NeilBrown14a75d32011-12-23 10:17:52 +11002453 if (atomic_read(&rdev->read_errors))
2454 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002456 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002457 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002458 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07002461 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002462 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowncc6167b2016-11-02 14:16:50 +11002463 pr_warn_ratelimited(
2464 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
NeilBrown14a75d32011-12-23 10:17:52 +11002465 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002466 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002467 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002468 else if (conf->mddev->degraded >= conf->max_degraded) {
2469 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002470 pr_warn_ratelimited(
2471 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002472 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002473 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002474 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002475 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002476 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002477 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002478 pr_warn_ratelimited(
2479 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002480 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002481 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002482 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002483 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08002484 > conf->max_nr_stripes)
NeilBrowncc6167b2016-11-02 14:16:50 +11002485 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07002486 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08002487 else
2488 retry = 1;
Bian Yuedfa1f62013-11-14 15:16:17 +11002489 if (set_bad && test_bit(In_sync, &rdev->flags)
2490 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2491 retry = 1;
NeilBrownba22dcb2005-11-08 21:39:31 -08002492 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10002493 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2494 set_bit(R5_ReadError, &sh->dev[i].flags);
2495 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2496 } else
2497 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002498 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002499 clear_bit(R5_ReadError, &sh->dev[i].flags);
2500 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002501 if (!(set_bad
2502 && test_bit(In_sync, &rdev->flags)
2503 && rdev_set_badblocks(
2504 rdev, sh->sector, STRIPE_SECTORS, 0)))
2505 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 }
NeilBrown14a75d32011-12-23 10:17:52 +11002508 rdev_dec_pending(rdev, conf->mddev);
Shaohua Lic9445552016-09-08 10:43:58 -07002509 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2511 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002512 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002515static void raid5_end_write_request(struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516{
NeilBrown99c0fb52009-03-31 14:39:38 +11002517 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002518 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002519 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11002520 struct md_rdev *uninitialized_var(rdev);
NeilBrownb84db562011-07-28 11:39:23 +10002521 sector_t first_bad;
2522 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002523 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
NeilBrown977df362011-12-23 10:17:53 +11002525 for (i = 0 ; i < disks; i++) {
2526 if (bi == &sh->dev[i].req) {
2527 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 break;
NeilBrown977df362011-12-23 10:17:53 +11002529 }
2530 if (bi == &sh->dev[i].rreq) {
2531 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002532 if (rdev)
2533 replacement = 1;
2534 else
2535 /* rdev was removed and 'replacement'
2536 * replaced it. rdev is not removed
2537 * until all requests are finished.
2538 */
2539 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002540 break;
2541 }
2542 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002543 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002545 bi->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002547 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002549 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 }
2551
NeilBrown977df362011-12-23 10:17:53 +11002552 if (replacement) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002553 if (bi->bi_error)
NeilBrown977df362011-12-23 10:17:53 +11002554 md_error(conf->mddev, rdev);
2555 else if (is_badblock(rdev, sh->sector,
2556 STRIPE_SECTORS,
2557 &first_bad, &bad_sectors))
2558 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2559 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002560 if (bi->bi_error) {
NeilBrown9f97e4b2014-01-16 09:35:38 +11002561 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11002562 set_bit(WriteErrorSeen, &rdev->flags);
2563 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002564 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2565 set_bit(MD_RECOVERY_NEEDED,
2566 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002567 } else if (is_badblock(rdev, sh->sector,
2568 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10002569 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002570 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002571 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2572 /* That was a successful write so make
2573 * sure it looks like we already did
2574 * a re-write.
2575 */
2576 set_bit(R5_ReWrite, &sh->dev[i].flags);
2577 }
NeilBrown977df362011-12-23 10:17:53 +11002578 }
2579 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002581 if (sh->batch_head && bi->bi_error && !replacement)
shli@kernel.org72ac7332014-12-15 12:57:03 +11002582 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2583
Shaohua Lic9445552016-09-08 10:43:58 -07002584 bio_reset(bi);
NeilBrown977df362011-12-23 10:17:53 +11002585 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2586 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002588 raid5_release_stripe(sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002589
2590 if (sh->batch_head && sh != sh->batch_head)
Shaohua Li6d036f72015-08-13 14:31:57 -07002591 raid5_release_stripe(sh->batch_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
NeilBrown784052e2009-03-31 15:19:07 +11002594static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
2596 struct r5dev *dev = &sh->dev[i];
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 dev->flags = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07002599 dev->sector = raid5_compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600}
2601
Shaohua Li849674e2016-01-20 13:52:20 -08002602static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
2604 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002605 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002606 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002607 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
NeilBrown908f4fb2011-12-23 10:17:50 +11002609 spin_lock_irqsave(&conf->device_lock, flags);
2610 clear_bit(In_sync, &rdev->flags);
Song Liu2e38a372017-01-24 10:45:30 -08002611 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11002612 spin_unlock_irqrestore(&conf->device_lock, flags);
2613 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2614
NeilBrownde393cd2011-07-28 11:31:48 +10002615 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10002616 set_bit(Faulty, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08002617 set_mask_bits(&mddev->sb_flags, 0,
2618 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrowncc6167b2016-11-02 14:16:50 +11002619 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
2620 "md/raid:%s: Operation continuing on %d devices.\n",
2621 mdname(mddev),
2622 bdevname(rdev->bdev, b),
2623 mdname(mddev),
2624 conf->raid_disks - mddev->degraded);
Song Liu2e38a372017-01-24 10:45:30 -08002625 r5c_update_on_rdev_error(mddev);
NeilBrown16a53ec2006-06-26 00:27:38 -07002626}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
2628/*
2629 * Input: a 'big' sector number,
2630 * Output: index of the data and parity disk, and the sector # in them.
2631 */
Shaohua Li6d036f72015-08-13 14:31:57 -07002632sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2633 int previous, int *dd_idx,
2634 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002636 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002637 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002639 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002640 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002642 int algorithm = previous ? conf->prev_algo
2643 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002644 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2645 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002646 int raid_disks = previous ? conf->previous_raid_disks
2647 : conf->raid_disks;
2648 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
2650 /* First compute the information on this sector */
2651
2652 /*
2653 * Compute the chunk number and the sector offset inside the chunk
2654 */
2655 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2656 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
2658 /*
2659 * Compute the stripe number
2660 */
NeilBrown35f2a592010-04-20 14:13:34 +10002661 stripe = chunk_number;
2662 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002663 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 /*
2665 * Select the parity disk based on the user selected algorithm.
2666 */
NeilBrown84789552011-07-27 11:00:36 +10002667 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002668 switch(conf->level) {
2669 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002670 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002671 break;
2672 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002673 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002675 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002676 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 (*dd_idx)++;
2678 break;
2679 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002680 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002681 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 (*dd_idx)++;
2683 break;
2684 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002685 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002686 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 break;
2688 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002689 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002690 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002692 case ALGORITHM_PARITY_0:
2693 pd_idx = 0;
2694 (*dd_idx)++;
2695 break;
2696 case ALGORITHM_PARITY_N:
2697 pd_idx = data_disks;
2698 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002700 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002701 }
2702 break;
2703 case 6:
2704
NeilBrowne183eae2009-03-31 15:20:22 +11002705 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002706 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002707 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002708 qd_idx = pd_idx + 1;
2709 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002710 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002711 qd_idx = 0;
2712 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002713 (*dd_idx) += 2; /* D D P Q D */
2714 break;
2715 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002716 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002717 qd_idx = pd_idx + 1;
2718 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002719 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002720 qd_idx = 0;
2721 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002722 (*dd_idx) += 2; /* D D P Q D */
2723 break;
2724 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002725 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002726 qd_idx = (pd_idx + 1) % raid_disks;
2727 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002728 break;
2729 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002730 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002731 qd_idx = (pd_idx + 1) % raid_disks;
2732 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002733 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002734
2735 case ALGORITHM_PARITY_0:
2736 pd_idx = 0;
2737 qd_idx = 1;
2738 (*dd_idx) += 2;
2739 break;
2740 case ALGORITHM_PARITY_N:
2741 pd_idx = data_disks;
2742 qd_idx = data_disks + 1;
2743 break;
2744
2745 case ALGORITHM_ROTATING_ZERO_RESTART:
2746 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2747 * of blocks for computing Q is different.
2748 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002749 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002750 qd_idx = pd_idx + 1;
2751 if (pd_idx == raid_disks-1) {
2752 (*dd_idx)++; /* Q D D D P */
2753 qd_idx = 0;
2754 } else if (*dd_idx >= pd_idx)
2755 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002756 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002757 break;
2758
2759 case ALGORITHM_ROTATING_N_RESTART:
2760 /* Same a left_asymmetric, by first stripe is
2761 * D D D P Q rather than
2762 * Q D D D P
2763 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002764 stripe2 += 1;
2765 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002766 qd_idx = pd_idx + 1;
2767 if (pd_idx == raid_disks-1) {
2768 (*dd_idx)++; /* Q D D D P */
2769 qd_idx = 0;
2770 } else if (*dd_idx >= pd_idx)
2771 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002772 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002773 break;
2774
2775 case ALGORITHM_ROTATING_N_CONTINUE:
2776 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002777 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002778 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2779 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002780 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002781 break;
2782
2783 case ALGORITHM_LEFT_ASYMMETRIC_6:
2784 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002785 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002786 if (*dd_idx >= pd_idx)
2787 (*dd_idx)++;
2788 qd_idx = raid_disks - 1;
2789 break;
2790
2791 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002792 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002793 if (*dd_idx >= pd_idx)
2794 (*dd_idx)++;
2795 qd_idx = raid_disks - 1;
2796 break;
2797
2798 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002799 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002800 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2801 qd_idx = raid_disks - 1;
2802 break;
2803
2804 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002805 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002806 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2807 qd_idx = raid_disks - 1;
2808 break;
2809
2810 case ALGORITHM_PARITY_0_6:
2811 pd_idx = 0;
2812 (*dd_idx)++;
2813 qd_idx = raid_disks - 1;
2814 break;
2815
NeilBrown16a53ec2006-06-26 00:27:38 -07002816 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002817 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002818 }
2819 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 }
2821
NeilBrown911d4ee2009-03-31 14:39:38 +11002822 if (sh) {
2823 sh->pd_idx = pd_idx;
2824 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002825 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 /*
2828 * Finally, compute the new sector number
2829 */
2830 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2831 return new_sector;
2832}
2833
Shaohua Li6d036f72015-08-13 14:31:57 -07002834sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
NeilBrownd1688a62011-10-11 16:49:52 +11002836 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002837 int raid_disks = sh->disks;
2838 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002840 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2841 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002842 int algorithm = previous ? conf->prev_algo
2843 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 sector_t stripe;
2845 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002846 sector_t chunk_number;
2847 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002849 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2852 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
NeilBrown16a53ec2006-06-26 00:27:38 -07002854 if (i == sh->pd_idx)
2855 return 0;
2856 switch(conf->level) {
2857 case 4: break;
2858 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002859 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 case ALGORITHM_LEFT_ASYMMETRIC:
2861 case ALGORITHM_RIGHT_ASYMMETRIC:
2862 if (i > sh->pd_idx)
2863 i--;
2864 break;
2865 case ALGORITHM_LEFT_SYMMETRIC:
2866 case ALGORITHM_RIGHT_SYMMETRIC:
2867 if (i < sh->pd_idx)
2868 i += raid_disks;
2869 i -= (sh->pd_idx + 1);
2870 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002871 case ALGORITHM_PARITY_0:
2872 i -= 1;
2873 break;
2874 case ALGORITHM_PARITY_N:
2875 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002877 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002878 }
2879 break;
2880 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002881 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002882 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002883 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002884 case ALGORITHM_LEFT_ASYMMETRIC:
2885 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002886 case ALGORITHM_ROTATING_ZERO_RESTART:
2887 case ALGORITHM_ROTATING_N_RESTART:
2888 if (sh->pd_idx == raid_disks-1)
2889 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002890 else if (i > sh->pd_idx)
2891 i -= 2; /* D D P Q D */
2892 break;
2893 case ALGORITHM_LEFT_SYMMETRIC:
2894 case ALGORITHM_RIGHT_SYMMETRIC:
2895 if (sh->pd_idx == raid_disks-1)
2896 i--; /* Q D D D P */
2897 else {
2898 /* D D P Q D */
2899 if (i < sh->pd_idx)
2900 i += raid_disks;
2901 i -= (sh->pd_idx + 2);
2902 }
2903 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002904 case ALGORITHM_PARITY_0:
2905 i -= 2;
2906 break;
2907 case ALGORITHM_PARITY_N:
2908 break;
2909 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002910 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002911 if (sh->pd_idx == 0)
2912 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002913 else {
2914 /* D D Q P D */
2915 if (i < sh->pd_idx)
2916 i += raid_disks;
2917 i -= (sh->pd_idx + 1);
2918 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002919 break;
2920 case ALGORITHM_LEFT_ASYMMETRIC_6:
2921 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2922 if (i > sh->pd_idx)
2923 i--;
2924 break;
2925 case ALGORITHM_LEFT_SYMMETRIC_6:
2926 case ALGORITHM_RIGHT_SYMMETRIC_6:
2927 if (i < sh->pd_idx)
2928 i += data_disks + 1;
2929 i -= (sh->pd_idx + 1);
2930 break;
2931 case ALGORITHM_PARITY_0_6:
2932 i -= 1;
2933 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002934 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002935 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002936 }
2937 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 }
2939
2940 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002941 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
NeilBrown112bf892009-03-31 14:39:38 +11002943 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002944 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002945 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2946 || sh2.qd_idx != sh->qd_idx) {
NeilBrowncc6167b2016-11-02 14:16:50 +11002947 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
2948 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return 0;
2950 }
2951 return r_sector;
2952}
2953
Song Liu07e83362017-01-23 17:12:58 -08002954/*
2955 * There are cases where we want handle_stripe_dirtying() and
2956 * schedule_reconstruction() to delay towrite to some dev of a stripe.
2957 *
2958 * This function checks whether we want to delay the towrite. Specifically,
2959 * we delay the towrite when:
2960 *
2961 * 1. degraded stripe has a non-overwrite to the missing dev, AND this
2962 * stripe has data in journal (for other devices).
2963 *
2964 * In this case, when reading data for the non-overwrite dev, it is
2965 * necessary to handle complex rmw of write back cache (prexor with
2966 * orig_page, and xor with page). To keep read path simple, we would
2967 * like to flush data in journal to RAID disks first, so complex rmw
2968 * is handled in the write patch (handle_stripe_dirtying).
2969 *
Song Liu39b99582017-01-24 14:08:23 -08002970 * 2. when journal space is critical (R5C_LOG_CRITICAL=1)
2971 *
2972 * It is important to be able to flush all stripes in raid5-cache.
2973 * Therefore, we need reserve some space on the journal device for
2974 * these flushes. If flush operation includes pending writes to the
2975 * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe
2976 * for the flush out. If we exclude these pending writes from flush
2977 * operation, we only need (conf->max_degraded + 1) pages per stripe.
2978 * Therefore, excluding pending writes in these cases enables more
2979 * efficient use of the journal device.
2980 *
2981 * Note: To make sure the stripe makes progress, we only delay
2982 * towrite for stripes with data already in journal (injournal > 0).
2983 * When LOG_CRITICAL, stripes with injournal == 0 will be sent to
2984 * no_space_stripes list.
2985 *
Song Liu07e83362017-01-23 17:12:58 -08002986 */
Song Liu39b99582017-01-24 14:08:23 -08002987static inline bool delay_towrite(struct r5conf *conf,
2988 struct r5dev *dev,
2989 struct stripe_head_state *s)
Song Liu07e83362017-01-23 17:12:58 -08002990{
Song Liu39b99582017-01-24 14:08:23 -08002991 /* case 1 above */
2992 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2993 !test_bit(R5_Insync, &dev->flags) && s->injournal)
2994 return true;
2995 /* case 2 above */
2996 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
2997 s->injournal > 0)
2998 return true;
2999 return false;
Song Liu07e83362017-01-23 17:12:58 -08003000}
3001
Dan Williams600aa102008-06-28 08:32:05 +10003002static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003003schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10003004 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07003005{
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003006 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11003007 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003008 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07003009
Dan Williamse33129d2007-01-02 13:52:30 -07003010 if (rcw) {
Song Liu1e6d6902016-11-17 15:24:39 -08003011 /*
3012 * In some cases, handle_stripe_dirtying initially decided to
3013 * run rmw and allocates extra page for prexor. However, rcw is
3014 * cheaper later on. We need to free the extra page now,
3015 * because we won't be able to do that in ops_complete_prexor().
3016 */
3017 r5c_release_extra_page(sh);
Dan Williamse33129d2007-01-02 13:52:30 -07003018
3019 for (i = disks; i--; ) {
3020 struct r5dev *dev = &sh->dev[i];
3021
Song Liu39b99582017-01-24 14:08:23 -08003022 if (dev->towrite && !delay_towrite(conf, dev, s)) {
Dan Williamse33129d2007-01-02 13:52:30 -07003023 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10003024 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003025 if (!expand)
3026 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003027 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003028 } else if (test_bit(R5_InJournal, &dev->flags)) {
3029 set_bit(R5_LOCKED, &dev->flags);
3030 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003031 }
3032 }
NeilBrownce7d3632013-03-04 12:37:14 +11003033 /* if we are not expanding this is a proper write request, and
3034 * there will be bios with new data to be drained into the
3035 * stripe cache
3036 */
3037 if (!expand) {
3038 if (!s->locked)
3039 /* False alarm, nothing to do */
3040 return;
3041 sh->reconstruct_state = reconstruct_state_drain_run;
3042 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3043 } else
3044 sh->reconstruct_state = reconstruct_state_run;
3045
3046 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3047
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003048 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003049 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003050 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07003051 } else {
3052 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
3053 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003054 BUG_ON(level == 6 &&
3055 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
3056 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
Dan Williamse33129d2007-01-02 13:52:30 -07003057
Dan Williamse33129d2007-01-02 13:52:30 -07003058 for (i = disks; i--; ) {
3059 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003060 if (i == pd_idx || i == qd_idx)
Dan Williamse33129d2007-01-02 13:52:30 -07003061 continue;
3062
Dan Williamse33129d2007-01-02 13:52:30 -07003063 if (dev->towrite &&
3064 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10003065 test_bit(R5_Wantcompute, &dev->flags))) {
3066 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003067 set_bit(R5_LOCKED, &dev->flags);
3068 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003069 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003070 } else if (test_bit(R5_InJournal, &dev->flags)) {
3071 set_bit(R5_LOCKED, &dev->flags);
3072 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003073 }
3074 }
NeilBrownce7d3632013-03-04 12:37:14 +11003075 if (!s->locked)
3076 /* False alarm - nothing to do */
3077 return;
3078 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
3079 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
3080 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3081 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003082 }
3083
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003084 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07003085 * are in flight
3086 */
3087 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
3088 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10003089 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003090
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003091 if (level == 6) {
3092 int qd_idx = sh->qd_idx;
3093 struct r5dev *dev = &sh->dev[qd_idx];
3094
3095 set_bit(R5_LOCKED, &dev->flags);
3096 clear_bit(R5_UPTODATE, &dev->flags);
3097 s->locked++;
3098 }
3099
Dan Williams600aa102008-06-28 08:32:05 +10003100 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07003101 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10003102 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003103}
NeilBrown16a53ec2006-06-26 00:27:38 -07003104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105/*
3106 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07003107 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 * The bi_next chain must be in order.
3109 */
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003110static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
3111 int forwrite, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
3113 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11003114 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07003115 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
NeilBrowncbe47ec2011-07-26 11:20:35 +10003117 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003118 (unsigned long long)bi->bi_iter.bi_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 (unsigned long long)sh->sector);
3120
Shaohua Lib17459c2012-07-19 16:01:31 +10003121 /*
3122 * If several bio share a stripe. The bio bi_phys_segments acts as a
3123 * reference count to avoid race. The reference count should already be
3124 * increased before this function is called (for example, in
Shaohua Li849674e2016-01-20 13:52:20 -08003125 * raid5_make_request()), so other bio sharing this stripe will not free the
Shaohua Lib17459c2012-07-19 16:01:31 +10003126 * stripe. If a stripe is owned by one stripe, the stripe lock will
3127 * protect it.
3128 */
3129 spin_lock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003130 /* Don't allow new IO added to stripes in batch list */
3131 if (sh->batch_head)
3132 goto overlap;
NeilBrown72626682005-09-09 16:23:54 -07003133 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003135 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07003136 firstwrite = 1;
3137 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 bip = &sh->dev[dd_idx].toread;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003139 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
3140 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 goto overlap;
3142 bip = & (*bip)->bi_next;
3143 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07003144 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 goto overlap;
3146
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003147 if (!forwrite || previous)
3148 clear_bit(STRIPE_BATCH_READY, &sh->state);
3149
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02003150 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 if (*bip)
3152 bi->bi_next = *bip;
3153 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003154 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07003155
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 if (forwrite) {
3157 /* check if page is covered */
3158 sector_t sector = sh->dev[dd_idx].sector;
3159 for (bi=sh->dev[dd_idx].towrite;
3160 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07003161 bi && bi->bi_iter.bi_sector <= sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07003163 if (bio_end_sector(bi) >= sector)
3164 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 }
3166 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
shli@kernel.org7a87f432014-12-15 12:57:03 +11003167 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
3168 sh->overwrite_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003170
3171 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003172 (unsigned long long)(*bip)->bi_iter.bi_sector,
NeilBrowncbe47ec2011-07-26 11:20:35 +10003173 (unsigned long long)sh->sector, dd_idx);
3174
3175 if (conf->mddev->bitmap && firstwrite) {
NeilBrownd0852df52015-05-27 08:43:45 +10003176 /* Cannot hold spinlock over bitmap_startwrite,
3177 * but must ensure this isn't added to a batch until
3178 * we have added to the bitmap and set bm_seq.
3179 * So set STRIPE_BITMAP_PENDING to prevent
3180 * batching.
3181 * If multiple add_stripe_bio() calls race here they
3182 * much all set STRIPE_BITMAP_PENDING. So only the first one
3183 * to complete "bitmap_startwrite" gets to set
3184 * STRIPE_BIT_DELAY. This is important as once a stripe
3185 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3186 * any more.
3187 */
3188 set_bit(STRIPE_BITMAP_PENDING, &sh->state);
3189 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10003190 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
3191 STRIPE_SECTORS, 0);
NeilBrownd0852df52015-05-27 08:43:45 +10003192 spin_lock_irq(&sh->stripe_lock);
3193 clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
3194 if (!sh->batch_head) {
3195 sh->bm_seq = conf->seq_flush+1;
3196 set_bit(STRIPE_BIT_DELAY, &sh->state);
3197 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003198 }
NeilBrownd0852df52015-05-27 08:43:45 +10003199 spin_unlock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003200
3201 if (stripe_can_batch(sh))
3202 stripe_add_to_batch_list(conf, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 return 1;
3204
3205 overlap:
3206 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10003207 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 return 0;
3209}
3210
NeilBrownd1688a62011-10-11 16:49:52 +11003211static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08003212
NeilBrownd1688a62011-10-11 16:49:52 +11003213static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003214 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08003215{
NeilBrown784052e2009-03-31 15:19:07 +11003216 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10003217 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11003218 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003219 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11003220 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003221
NeilBrown112bf892009-03-31 14:39:38 +11003222 raid5_compute_sector(conf,
3223 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08003224 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11003225 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003226 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003227}
3228
Dan Williamsa4456852007-07-09 11:56:43 -07003229static void
NeilBrownd1688a62011-10-11 16:49:52 +11003230handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003231 struct stripe_head_state *s, int disks,
NeilBrown34a6f802015-08-14 12:07:57 +10003232 struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003233{
3234 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003235 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003236 for (i = disks; i--; ) {
3237 struct bio *bi;
3238 int bitmap_end = 0;
3239
3240 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003241 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07003242 rcu_read_lock();
3243 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownf5b67ae2016-06-02 16:19:53 +10003244 if (rdev && test_bit(In_sync, &rdev->flags) &&
3245 !test_bit(Faulty, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10003246 atomic_inc(&rdev->nr_pending);
3247 else
3248 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003249 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10003250 if (rdev) {
3251 if (!rdev_set_badblocks(
3252 rdev,
3253 sh->sector,
3254 STRIPE_SECTORS, 0))
3255 md_error(conf->mddev, rdev);
3256 rdev_dec_pending(rdev, conf->mddev);
3257 }
Dan Williamsa4456852007-07-09 11:56:43 -07003258 }
Shaohua Lib17459c2012-07-19 16:01:31 +10003259 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003260 /* fail all writes first */
3261 bi = sh->dev[i].towrite;
3262 sh->dev[i].towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11003263 sh->overwrite_disks = 0;
Shaohua Lib17459c2012-07-19 16:01:31 +10003264 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11003265 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003266 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07003267
Shaohua Li0576b1c2015-08-13 14:32:00 -07003268 r5l_stripe_write_finished(sh);
3269
Dan Williamsa4456852007-07-09 11:56:43 -07003270 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3271 wake_up(&conf->wait_for_overlap);
3272
Kent Overstreet4f024f32013-10-11 15:44:27 -07003273 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003274 sh->dev[i].sector + STRIPE_SECTORS) {
3275 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003276
3277 bi->bi_error = -EIO;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003278 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003279 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003280 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003281 }
3282 bi = nextbi;
3283 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003284 if (bitmap_end)
3285 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3286 STRIPE_SECTORS, 0, 0);
3287 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003288 /* and fail all 'written' */
3289 bi = sh->dev[i].written;
3290 sh->dev[i].written = NULL;
Shaohua Lid592a992014-05-21 17:57:44 +08003291 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3292 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3293 sh->dev[i].page = sh->dev[i].orig_page;
3294 }
3295
Dan Williamsa4456852007-07-09 11:56:43 -07003296 if (bi) bitmap_end = 1;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003297 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003298 sh->dev[i].sector + STRIPE_SECTORS) {
3299 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003300
3301 bi->bi_error = -EIO;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003302 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003303 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003304 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003305 }
3306 bi = bi2;
3307 }
3308
Dan Williamsb5e98d62007-01-02 13:52:31 -07003309 /* fail any reads if this device is non-operational and
3310 * the data has not reached the cache yet.
3311 */
3312 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
Shaohua Li6e74a9c2015-10-08 21:54:08 -07003313 s->failed > conf->max_degraded &&
Dan Williamsb5e98d62007-01-02 13:52:31 -07003314 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3315 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11003316 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003317 bi = sh->dev[i].toread;
3318 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11003319 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003320 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3321 wake_up(&conf->wait_for_overlap);
Shaohua Liebda7802015-09-18 10:20:13 -07003322 if (bi)
3323 s->to_read--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003324 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003325 sh->dev[i].sector + STRIPE_SECTORS) {
3326 struct bio *nextbi =
3327 r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003328
3329 bi->bi_error = -EIO;
NeilBrown34a6f802015-08-14 12:07:57 +10003330 if (!raid5_dec_bi_active_stripes(bi))
3331 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003332 bi = nextbi;
3333 }
3334 }
Dan Williamsa4456852007-07-09 11:56:43 -07003335 if (bitmap_end)
3336 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3337 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10003338 /* If we were in the middle of a write the parity block might
3339 * still be locked - so just clear all R5_LOCKED flags
3340 */
3341 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003342 }
Shaohua Liebda7802015-09-18 10:20:13 -07003343 s->to_write = 0;
3344 s->written = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003345
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003346 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3347 if (atomic_dec_and_test(&conf->pending_full_writes))
3348 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003349}
3350
NeilBrown7f0da592011-07-28 11:39:22 +10003351static void
NeilBrownd1688a62011-10-11 16:49:52 +11003352handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10003353 struct stripe_head_state *s)
3354{
3355 int abort = 0;
3356 int i;
3357
shli@kernel.org59fc6302014-12-15 12:57:03 +11003358 BUG_ON(sh->batch_head);
NeilBrown7f0da592011-07-28 11:39:22 +10003359 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003360 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3361 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10003362 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11003363 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10003364 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10003365 * Don't even need to abort as that is handled elsewhere
3366 * if needed, and not always wanted e.g. if there is a known
3367 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11003368 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10003369 * non-sync devices, or abort the recovery
3370 */
NeilBrown18b98372012-04-01 23:48:38 +10003371 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3372 /* During recovery devices cannot be removed, so
3373 * locking and refcounting of rdevs is not needed
3374 */
NeilBrowne50d3992016-06-02 16:19:52 +10003375 rcu_read_lock();
NeilBrown18b98372012-04-01 23:48:38 +10003376 for (i = 0; i < conf->raid_disks; i++) {
NeilBrowne50d3992016-06-02 16:19:52 +10003377 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown18b98372012-04-01 23:48:38 +10003378 if (rdev
3379 && !test_bit(Faulty, &rdev->flags)
3380 && !test_bit(In_sync, &rdev->flags)
3381 && !rdev_set_badblocks(rdev, sh->sector,
3382 STRIPE_SECTORS, 0))
3383 abort = 1;
NeilBrowne50d3992016-06-02 16:19:52 +10003384 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown18b98372012-04-01 23:48:38 +10003385 if (rdev
3386 && !test_bit(Faulty, &rdev->flags)
3387 && !test_bit(In_sync, &rdev->flags)
3388 && !rdev_set_badblocks(rdev, sh->sector,
3389 STRIPE_SECTORS, 0))
3390 abort = 1;
3391 }
NeilBrowne50d3992016-06-02 16:19:52 +10003392 rcu_read_unlock();
NeilBrown18b98372012-04-01 23:48:38 +10003393 if (abort)
3394 conf->recovery_disabled =
3395 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10003396 }
NeilBrown18b98372012-04-01 23:48:38 +10003397 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10003398}
3399
NeilBrown9a3e1102011-12-23 10:17:53 +11003400static int want_replace(struct stripe_head *sh, int disk_idx)
3401{
3402 struct md_rdev *rdev;
3403 int rv = 0;
NeilBrown3f232d62016-06-02 16:19:52 +10003404
3405 rcu_read_lock();
3406 rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
NeilBrown9a3e1102011-12-23 10:17:53 +11003407 if (rdev
3408 && !test_bit(Faulty, &rdev->flags)
3409 && !test_bit(In_sync, &rdev->flags)
3410 && (rdev->recovery_offset <= sh->sector
3411 || rdev->mddev->recovery_cp <= sh->sector))
3412 rv = 1;
NeilBrown3f232d62016-06-02 16:19:52 +10003413 rcu_read_unlock();
NeilBrown9a3e1102011-12-23 10:17:53 +11003414 return rv;
3415}
3416
NeilBrown2c58f062015-02-02 11:32:23 +11003417static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3418 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07003419{
3420 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10003421 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3422 &sh->dev[s->failed_num[1]] };
NeilBrownea664c82015-02-02 14:03:28 +11003423 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07003424
NeilBrowna79cfe12015-02-02 11:37:59 +11003425
3426 if (test_bit(R5_LOCKED, &dev->flags) ||
3427 test_bit(R5_UPTODATE, &dev->flags))
3428 /* No point reading this as we already have it or have
3429 * decided to get it.
3430 */
3431 return 0;
3432
3433 if (dev->toread ||
3434 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3435 /* We need this block to directly satisfy a request */
3436 return 1;
3437
3438 if (s->syncing || s->expanding ||
3439 (s->replacing && want_replace(sh, disk_idx)))
3440 /* When syncing, or expanding we read everything.
3441 * When replacing, we need the replaced block.
3442 */
3443 return 1;
3444
3445 if ((s->failed >= 1 && fdev[0]->toread) ||
3446 (s->failed >= 2 && fdev[1]->toread))
3447 /* If we want to read from a failed device, then
3448 * we need to actually read every other device.
3449 */
3450 return 1;
3451
NeilBrowna9d56952015-02-02 11:49:10 +11003452 /* Sometimes neither read-modify-write nor reconstruct-write
3453 * cycles can work. In those cases we read every block we
3454 * can. Then the parity-update is certain to have enough to
3455 * work with.
3456 * This can only be a problem when we need to write something,
3457 * and some device has failed. If either of those tests
3458 * fail we need look no further.
3459 */
3460 if (!s->failed || !s->to_write)
3461 return 0;
3462
3463 if (test_bit(R5_Insync, &dev->flags) &&
3464 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3465 /* Pre-reads at not permitted until after short delay
3466 * to gather multiple requests. However if this
3467 * device is no Insync, the block could only be be computed
3468 * and there is no need to delay that.
3469 */
3470 return 0;
NeilBrownea664c82015-02-02 14:03:28 +11003471
NeilBrown36707bb2015-09-24 15:25:36 +10003472 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrownea664c82015-02-02 14:03:28 +11003473 if (fdev[i]->towrite &&
3474 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3475 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3476 /* If we have a partial write to a failed
3477 * device, then we will need to reconstruct
3478 * the content of that device, so all other
3479 * devices must be read.
3480 */
3481 return 1;
3482 }
3483
3484 /* If we are forced to do a reconstruct-write, either because
3485 * the current RAID6 implementation only supports that, or
3486 * or because parity cannot be trusted and we are currently
3487 * recovering it, there is extra need to be careful.
3488 * If one of the devices that we would need to read, because
3489 * it is not being overwritten (and maybe not written at all)
3490 * is missing/faulty, then we need to read everything we can.
3491 */
3492 if (sh->raid_conf->level != 6 &&
3493 sh->sector < sh->raid_conf->mddev->recovery_cp)
3494 /* reconstruct-write isn't being forced */
3495 return 0;
NeilBrown36707bb2015-09-24 15:25:36 +10003496 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrown10d82c52015-05-08 18:19:33 +10003497 if (s->failed_num[i] != sh->pd_idx &&
3498 s->failed_num[i] != sh->qd_idx &&
3499 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
NeilBrownea664c82015-02-02 14:03:28 +11003500 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3501 return 1;
3502 }
3503
NeilBrown2c58f062015-02-02 11:32:23 +11003504 return 0;
3505}
3506
Song Liuba026842017-01-12 17:22:42 -08003507/* fetch_block - checks the given member device to see if its data needs
3508 * to be read or computed to satisfy a request.
3509 *
3510 * Returns 1 when no more member devices need to be checked, otherwise returns
3511 * 0 to tell the loop in handle_stripe_fill to continue
3512 */
NeilBrown2c58f062015-02-02 11:32:23 +11003513static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3514 int disk_idx, int disks)
3515{
3516 struct r5dev *dev = &sh->dev[disk_idx];
3517
3518 /* is the data in this block needed, and can we get it? */
3519 if (need_this_block(sh, s, disk_idx, disks)) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003520 /* we would like to get this block, possibly by computing it,
3521 * otherwise read it if the backing disk is insync
3522 */
3523 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3524 BUG_ON(test_bit(R5_Wantread, &dev->flags));
NeilBrownb0c783b2015-05-08 18:19:32 +10003525 BUG_ON(sh->batch_head);
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003526 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10003527 (s->failed && (disk_idx == s->failed_num[0] ||
3528 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003529 /* have disk failed, and we're requested to fetch it;
3530 * do compute it
3531 */
3532 pr_debug("Computing stripe %llu block %d\n",
3533 (unsigned long long)sh->sector, disk_idx);
3534 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3535 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3536 set_bit(R5_Wantcompute, &dev->flags);
3537 sh->ops.target = disk_idx;
3538 sh->ops.target2 = -1; /* no 2nd target */
3539 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10003540 /* Careful: from this point on 'uptodate' is in the eye
3541 * of raid_run_ops which services 'compute' operations
3542 * before writes. R5_Wantcompute flags a block that will
3543 * be R5_UPTODATE by the time it is needed for a
3544 * subsequent operation.
3545 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003546 s->uptodate++;
3547 return 1;
3548 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3549 /* Computing 2-failure is *very* expensive; only
3550 * do it if failed >= 2
3551 */
3552 int other;
3553 for (other = disks; other--; ) {
3554 if (other == disk_idx)
3555 continue;
3556 if (!test_bit(R5_UPTODATE,
3557 &sh->dev[other].flags))
3558 break;
3559 }
3560 BUG_ON(other < 0);
3561 pr_debug("Computing stripe %llu blocks %d,%d\n",
3562 (unsigned long long)sh->sector,
3563 disk_idx, other);
3564 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3565 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3566 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3567 set_bit(R5_Wantcompute, &sh->dev[other].flags);
3568 sh->ops.target = disk_idx;
3569 sh->ops.target2 = other;
3570 s->uptodate += 2;
3571 s->req_compute = 1;
3572 return 1;
3573 } else if (test_bit(R5_Insync, &dev->flags)) {
3574 set_bit(R5_LOCKED, &dev->flags);
3575 set_bit(R5_Wantread, &dev->flags);
3576 s->locked++;
3577 pr_debug("Reading block %d (sync=%d)\n",
3578 disk_idx, s->syncing);
3579 }
3580 }
3581
3582 return 0;
3583}
3584
3585/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10003586 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003587 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10003588static void handle_stripe_fill(struct stripe_head *sh,
3589 struct stripe_head_state *s,
3590 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003591{
3592 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003593
3594 /* look for blocks to read/compute, skip this if a compute
3595 * is already in flight, or if the stripe contents are in the
3596 * midst of changing due to a write
3597 */
3598 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Song Liu07e83362017-01-23 17:12:58 -08003599 !sh->reconstruct_state) {
3600
3601 /*
3602 * For degraded stripe with data in journal, do not handle
3603 * read requests yet, instead, flush the stripe to raid
3604 * disks first, this avoids handling complex rmw of write
3605 * back cache (prexor with orig_page, and then xor with
3606 * page) in the read path
3607 */
3608 if (s->injournal && s->failed) {
3609 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
3610 r5c_make_stripe_write_out(sh);
3611 goto out;
3612 }
3613
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003614 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10003615 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003616 break;
Song Liu07e83362017-01-23 17:12:58 -08003617 }
3618out:
Dan Williamsa4456852007-07-09 11:56:43 -07003619 set_bit(STRIPE_HANDLE, &sh->state);
3620}
3621
NeilBrown787b76f2015-05-21 12:56:41 +10003622static void break_stripe_batch_list(struct stripe_head *head_sh,
3623 unsigned long handle_flags);
Dan Williams1fe797e2008-06-28 09:16:30 +10003624/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07003625 * any written block on an uptodate or failed drive can be returned.
3626 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3627 * never LOCKED, so we don't need to test 'failed' directly.
3628 */
NeilBrownd1688a62011-10-11 16:49:52 +11003629static void handle_stripe_clean_event(struct r5conf *conf,
NeilBrown34a6f802015-08-14 12:07:57 +10003630 struct stripe_head *sh, int disks, struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003631{
3632 int i;
3633 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003634 int discard_pending = 0;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003635 struct stripe_head *head_sh = sh;
3636 bool do_endio = false;
Dan Williamsa4456852007-07-09 11:56:43 -07003637
3638 for (i = disks; i--; )
3639 if (sh->dev[i].written) {
3640 dev = &sh->dev[i];
3641 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003642 (test_bit(R5_UPTODATE, &dev->flags) ||
Shaohua Lid592a992014-05-21 17:57:44 +08003643 test_bit(R5_Discard, &dev->flags) ||
3644 test_bit(R5_SkipCopy, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003645 /* We can return any write requests */
3646 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003647 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003648 if (test_and_clear_bit(R5_Discard, &dev->flags))
3649 clear_bit(R5_UPTODATE, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08003650 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3651 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
Shaohua Lid592a992014-05-21 17:57:44 +08003652 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003653 do_endio = true;
3654
3655returnbi:
3656 dev->page = dev->orig_page;
Dan Williamsa4456852007-07-09 11:56:43 -07003657 wbi = dev->written;
3658 dev->written = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003659 while (wbi && wbi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003660 dev->sector + STRIPE_SECTORS) {
3661 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10003662 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003663 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003664 bio_list_add(return_bi, wbi);
Dan Williamsa4456852007-07-09 11:56:43 -07003665 }
3666 wbi = wbi2;
3667 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003668 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3669 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07003670 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003671 0);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003672 if (head_sh->batch_head) {
3673 sh = list_first_entry(&sh->batch_list,
3674 struct stripe_head,
3675 batch_list);
3676 if (sh != head_sh) {
3677 dev = &sh->dev[i];
3678 goto returnbi;
3679 }
3680 }
3681 sh = head_sh;
3682 dev = &sh->dev[i];
NeilBrownf8dfcff2013-03-12 12:18:06 +11003683 } else if (test_bit(R5_Discard, &dev->flags))
3684 discard_pending = 1;
3685 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -07003686
Shaohua Li0576b1c2015-08-13 14:32:00 -07003687 r5l_stripe_write_finished(sh);
3688
NeilBrownf8dfcff2013-03-12 12:18:06 +11003689 if (!discard_pending &&
3690 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003691 int hash;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003692 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3693 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3694 if (sh->qd_idx >= 0) {
3695 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3696 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3697 }
3698 /* now that discard is done we can proceed with any sync */
3699 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08003700 /*
3701 * SCSI discard will change some bio fields and the stripe has
3702 * no updated data, so remove it from hash list and the stripe
3703 * will be reinitialized
3704 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11003705unhash:
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003706 hash = sh->hash_lock_index;
3707 spin_lock_irq(conf->hash_locks + hash);
Shaohua Lid47648f2013-10-19 14:51:42 +08003708 remove_hash(sh);
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003709 spin_unlock_irq(conf->hash_locks + hash);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003710 if (head_sh->batch_head) {
3711 sh = list_first_entry(&sh->batch_list,
3712 struct stripe_head, batch_list);
3713 if (sh != head_sh)
3714 goto unhash;
3715 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003716 sh = head_sh;
3717
NeilBrownf8dfcff2013-03-12 12:18:06 +11003718 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3719 set_bit(STRIPE_HANDLE, &sh->state);
3720
3721 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003722
3723 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3724 if (atomic_dec_and_test(&conf->pending_full_writes))
3725 md_wakeup_thread(conf->mddev->thread);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003726
NeilBrown787b76f2015-05-21 12:56:41 +10003727 if (head_sh->batch_head && do_endio)
3728 break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
Dan Williamsa4456852007-07-09 11:56:43 -07003729}
3730
Song Liu86aa1392017-01-12 17:22:41 -08003731/*
3732 * For RMW in write back cache, we need extra page in prexor to store the
3733 * old data. This page is stored in dev->orig_page.
3734 *
3735 * This function checks whether we have data for prexor. The exact logic
3736 * is:
3737 * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
3738 */
3739static inline bool uptodate_for_rmw(struct r5dev *dev)
3740{
3741 return (test_bit(R5_UPTODATE, &dev->flags)) &&
3742 (!test_bit(R5_InJournal, &dev->flags) ||
3743 test_bit(R5_OrigPageUPTDODATE, &dev->flags));
3744}
3745
Song Liud7bd3982016-11-23 22:50:39 -08003746static int handle_stripe_dirtying(struct r5conf *conf,
3747 struct stripe_head *sh,
3748 struct stripe_head_state *s,
3749 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003750{
3751 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003752 sector_t recovery_cp = conf->mddev->recovery_cp;
3753
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003754 /* Check whether resync is now happening or should start.
Alexander Lyakasa7854482012-10-11 13:50:12 +11003755 * If yes, then the array is dirty (after unclean shutdown or
3756 * initial creation), so parity in some stripes might be inconsistent.
3757 * In this case, we need to always do reconstruct-write, to ensure
3758 * that in case of drive failure or read-error correction, we
3759 * generate correct data from the parity.
3760 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003761 if (conf->rmw_level == PARITY_DISABLE_RMW ||
NeilBrown26ac1072015-02-18 11:35:14 +11003762 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
3763 s->failed == 0)) {
Alexander Lyakasa7854482012-10-11 13:50:12 +11003764 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10003765 * look like rcw is cheaper
3766 */
3767 rcw = 1; rmw = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003768 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
3769 conf->rmw_level, (unsigned long long)recovery_cp,
Alexander Lyakasa7854482012-10-11 13:50:12 +11003770 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10003771 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003772 /* would I have to read this buffer for read_modify_write */
3773 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08003774 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu07e83362017-01-23 17:12:58 -08003775 i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08003776 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003777 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08003778 !(uptodate_for_rmw(dev) ||
Dan Williamsf38e1212007-01-02 13:52:30 -07003779 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003780 if (test_bit(R5_Insync, &dev->flags))
3781 rmw++;
3782 else
3783 rmw += 2*disks; /* cannot read it */
3784 }
3785 /* Would I have to read this buffer for reconstruct_write */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003786 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3787 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003788 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003789 !(test_bit(R5_UPTODATE, &dev->flags) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003790 test_bit(R5_Wantcompute, &dev->flags))) {
NeilBrown67f45542014-05-28 13:39:22 +10003791 if (test_bit(R5_Insync, &dev->flags))
3792 rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07003793 else
3794 rcw += 2*disks;
3795 }
3796 }
Song Liu1e6d6902016-11-17 15:24:39 -08003797
Song Liu39b99582017-01-24 14:08:23 -08003798 pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n",
3799 (unsigned long long)sh->sector, sh->state, rmw, rcw);
Dan Williamsa4456852007-07-09 11:56:43 -07003800 set_bit(STRIPE_HANDLE, &sh->state);
Song Liu41257582016-05-23 17:25:06 -07003801 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003802 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003803 if (conf->mddev->queue)
3804 blk_add_trace_msg(conf->mddev->queue,
3805 "raid5 rmw %llu %d",
3806 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07003807 for (i = disks; i--; ) {
3808 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08003809 if (test_bit(R5_InJournal, &dev->flags) &&
3810 dev->page == dev->orig_page &&
3811 !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) {
3812 /* alloc page for prexor */
Song Liud7bd3982016-11-23 22:50:39 -08003813 struct page *p = alloc_page(GFP_NOIO);
Song Liu1e6d6902016-11-17 15:24:39 -08003814
Song Liud7bd3982016-11-23 22:50:39 -08003815 if (p) {
3816 dev->orig_page = p;
3817 continue;
3818 }
3819
3820 /*
3821 * alloc_page() failed, try use
3822 * disk_info->extra_page
3823 */
3824 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE,
3825 &conf->cache_state)) {
3826 r5c_use_extra_page(sh);
3827 break;
3828 }
3829
3830 /* extra_page in use, add to delayed_list */
3831 set_bit(STRIPE_DELAYED, &sh->state);
3832 s->waiting_extra_page = 1;
3833 return -EAGAIN;
Song Liu1e6d6902016-11-17 15:24:39 -08003834 }
Song Liud7bd3982016-11-23 22:50:39 -08003835 }
Song Liu1e6d6902016-11-17 15:24:39 -08003836
Song Liud7bd3982016-11-23 22:50:39 -08003837 for (i = disks; i--; ) {
3838 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08003839 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003840 i == sh->pd_idx || i == sh->qd_idx ||
3841 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003842 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08003843 !(uptodate_for_rmw(dev) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003844 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003845 test_bit(R5_Insync, &dev->flags)) {
NeilBrown67f45542014-05-28 13:39:22 +10003846 if (test_bit(STRIPE_PREREAD_ACTIVE,
3847 &sh->state)) {
3848 pr_debug("Read_old block %d for r-m-w\n",
3849 i);
Dan Williamsa4456852007-07-09 11:56:43 -07003850 set_bit(R5_LOCKED, &dev->flags);
3851 set_bit(R5_Wantread, &dev->flags);
3852 s->locked++;
3853 } else {
3854 set_bit(STRIPE_DELAYED, &sh->state);
3855 set_bit(STRIPE_HANDLE, &sh->state);
3856 }
3857 }
3858 }
NeilBrowna9add5d2012-10-31 11:59:09 +11003859 }
Song Liu41257582016-05-23 17:25:06 -07003860 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003861 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11003862 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10003863 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003864 for (i = disks; i--; ) {
3865 struct r5dev *dev = &sh->dev[i];
3866 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10003867 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003868 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003869 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10003870 test_bit(R5_Wantcompute, &dev->flags))) {
3871 rcw++;
NeilBrown67f45542014-05-28 13:39:22 +10003872 if (test_bit(R5_Insync, &dev->flags) &&
3873 test_bit(STRIPE_PREREAD_ACTIVE,
3874 &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07003875 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07003876 "%d for Reconstruct\n", i);
3877 set_bit(R5_LOCKED, &dev->flags);
3878 set_bit(R5_Wantread, &dev->flags);
3879 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11003880 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07003881 } else {
3882 set_bit(STRIPE_DELAYED, &sh->state);
3883 set_bit(STRIPE_HANDLE, &sh->state);
3884 }
3885 }
3886 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003887 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11003888 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3889 (unsigned long long)sh->sector,
3890 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10003891 }
NeilBrownb1b02fe2015-02-02 10:44:29 +11003892
3893 if (rcw > disks && rmw > disks &&
3894 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3895 set_bit(STRIPE_DELAYED, &sh->state);
3896
Dan Williamsa4456852007-07-09 11:56:43 -07003897 /* now if nothing is locked, and if we have enough data,
3898 * we can start a write request
3899 */
Dan Williamsf38e1212007-01-02 13:52:30 -07003900 /* since handle_stripe can be called at any time we need to handle the
3901 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07003902 * subsequent call wants to start a write request. raid_run_ops only
3903 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07003904 * simultaneously. If this is not the case then new writes need to be
3905 * held off until the compute completes.
3906 */
Dan Williams976ea8d2008-06-28 08:32:03 +10003907 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3908 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
Song Liu1e6d6902016-11-17 15:24:39 -08003909 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003910 schedule_reconstruction(sh, s, rcw == 0, 0);
Song Liud7bd3982016-11-23 22:50:39 -08003911 return 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003912}
3913
NeilBrownd1688a62011-10-11 16:49:52 +11003914static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003915 struct stripe_head_state *s, int disks)
3916{
Dan Williamsecc65c92008-06-28 08:31:57 +10003917 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07003918
shli@kernel.org59fc6302014-12-15 12:57:03 +11003919 BUG_ON(sh->batch_head);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003920 set_bit(STRIPE_HANDLE, &sh->state);
3921
Dan Williamsecc65c92008-06-28 08:31:57 +10003922 switch (sh->check_state) {
3923 case check_state_idle:
3924 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07003925 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07003926 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10003927 sh->check_state = check_state_run;
3928 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003929 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003930 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10003931 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07003932 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003933 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10003934 /* fall through */
3935 case check_state_compute_result:
3936 sh->check_state = check_state_idle;
3937 if (!dev)
3938 dev = &sh->dev[sh->pd_idx];
3939
3940 /* check that a write has not made the stripe insync */
3941 if (test_bit(STRIPE_INSYNC, &sh->state))
3942 break;
3943
3944 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07003945 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3946 BUG_ON(s->uptodate != disks);
3947
3948 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10003949 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003950 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003951
Dan Williamsa4456852007-07-09 11:56:43 -07003952 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003953 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003954 break;
3955 case check_state_run:
3956 break; /* we will be called again upon completion */
3957 case check_state_check_result:
3958 sh->check_state = check_state_idle;
3959
3960 /* if a failure occurred during the check operation, leave
3961 * STRIPE_INSYNC not set and let the stripe be handled again
3962 */
3963 if (s->failed)
3964 break;
3965
3966 /* handle a successful check operation, if parity is correct
3967 * we are done. Otherwise update the mismatch count and repair
3968 * parity if !MD_RECOVERY_CHECK
3969 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003970 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003971 /* parity is correct (on disc,
3972 * not in buffer any more)
3973 */
3974 set_bit(STRIPE_INSYNC, &sh->state);
3975 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003976 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003977 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3978 /* don't try to repair!! */
3979 set_bit(STRIPE_INSYNC, &sh->state);
3980 else {
3981 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003982 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003983 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3984 set_bit(R5_Wantcompute,
3985 &sh->dev[sh->pd_idx].flags);
3986 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003987 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003988 s->uptodate++;
3989 }
3990 }
3991 break;
3992 case check_state_compute_run:
3993 break;
3994 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11003995 pr_err("%s: unknown check_state: %d sector: %llu\n",
Dan Williamsecc65c92008-06-28 08:31:57 +10003996 __func__, sh->check_state,
3997 (unsigned long long) sh->sector);
3998 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003999 }
4000}
4001
NeilBrownd1688a62011-10-11 16:49:52 +11004002static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07004003 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10004004 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07004005{
Dan Williamsa4456852007-07-09 11:56:43 -07004006 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11004007 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07004008 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07004009
shli@kernel.org59fc6302014-12-15 12:57:03 +11004010 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07004011 set_bit(STRIPE_HANDLE, &sh->state);
4012
4013 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004014
Dan Williamsa4456852007-07-09 11:56:43 -07004015 /* Want to check and possibly repair P and Q.
4016 * However there could be one 'failed' device, in which
4017 * case we can only check one of them, possibly using the
4018 * other to generate missing data
4019 */
4020
Dan Williamsd82dfee2009-07-14 13:40:57 -07004021 switch (sh->check_state) {
4022 case check_state_idle:
4023 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10004024 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004025 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07004026 * makes sense to check P (If anything else were failed,
4027 * we would have used P to recreate it).
4028 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004029 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07004030 }
NeilBrownf2b3b442011-07-26 11:35:19 +10004031 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004032 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07004033 * anything, so it makes sense to check it
4034 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004035 if (sh->check_state == check_state_run)
4036 sh->check_state = check_state_run_pq;
4037 else
4038 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07004039 }
Dan Williams36d1c642009-07-14 11:48:22 -07004040
Dan Williamsd82dfee2009-07-14 13:40:57 -07004041 /* discard potentially stale zero_sum_result */
4042 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07004043
Dan Williamsd82dfee2009-07-14 13:40:57 -07004044 if (sh->check_state == check_state_run) {
4045 /* async_xor_zero_sum destroys the contents of P */
4046 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
4047 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07004048 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004049 if (sh->check_state >= check_state_run &&
4050 sh->check_state <= check_state_run_pq) {
4051 /* async_syndrome_zero_sum preserves P and Q, so
4052 * no need to mark them !uptodate here
4053 */
4054 set_bit(STRIPE_OP_CHECK, &s->ops_request);
4055 break;
4056 }
Dan Williams36d1c642009-07-14 11:48:22 -07004057
Dan Williamsd82dfee2009-07-14 13:40:57 -07004058 /* we have 2-disk failure */
4059 BUG_ON(s->failed != 2);
4060 /* fall through */
4061 case check_state_compute_result:
4062 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07004063
Dan Williamsd82dfee2009-07-14 13:40:57 -07004064 /* check that a write has not made the stripe insync */
4065 if (test_bit(STRIPE_INSYNC, &sh->state))
4066 break;
Dan Williamsa4456852007-07-09 11:56:43 -07004067
4068 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07004069 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07004070 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004071 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07004072 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004073 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07004074 s->locked++;
4075 set_bit(R5_LOCKED, &dev->flags);
4076 set_bit(R5_Wantwrite, &dev->flags);
4077 }
4078 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004079 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07004080 s->locked++;
4081 set_bit(R5_LOCKED, &dev->flags);
4082 set_bit(R5_Wantwrite, &dev->flags);
4083 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004084 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004085 dev = &sh->dev[pd_idx];
4086 s->locked++;
4087 set_bit(R5_LOCKED, &dev->flags);
4088 set_bit(R5_Wantwrite, &dev->flags);
4089 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004090 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004091 dev = &sh->dev[qd_idx];
4092 s->locked++;
4093 set_bit(R5_LOCKED, &dev->flags);
4094 set_bit(R5_Wantwrite, &dev->flags);
4095 }
4096 clear_bit(STRIPE_DEGRADED, &sh->state);
4097
4098 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004099 break;
4100 case check_state_run:
4101 case check_state_run_q:
4102 case check_state_run_pq:
4103 break; /* we will be called again upon completion */
4104 case check_state_check_result:
4105 sh->check_state = check_state_idle;
4106
4107 /* handle a successful check operation, if parity is correct
4108 * we are done. Otherwise update the mismatch count and repair
4109 * parity if !MD_RECOVERY_CHECK
4110 */
4111 if (sh->ops.zero_sum_result == 0) {
4112 /* both parities are correct */
4113 if (!s->failed)
4114 set_bit(STRIPE_INSYNC, &sh->state);
4115 else {
4116 /* in contrast to the raid5 case we can validate
4117 * parity, but still have a failure to write
4118 * back
4119 */
4120 sh->check_state = check_state_compute_result;
4121 /* Returning at this point means that we may go
4122 * off and bring p and/or q uptodate again so
4123 * we make sure to check zero_sum_result again
4124 * to verify if p or q need writeback
4125 */
4126 }
4127 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004128 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004129 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
4130 /* don't try to repair!! */
4131 set_bit(STRIPE_INSYNC, &sh->state);
4132 else {
4133 int *target = &sh->ops.target;
4134
4135 sh->ops.target = -1;
4136 sh->ops.target2 = -1;
4137 sh->check_state = check_state_compute_run;
4138 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4139 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4140 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4141 set_bit(R5_Wantcompute,
4142 &sh->dev[pd_idx].flags);
4143 *target = pd_idx;
4144 target = &sh->ops.target2;
4145 s->uptodate++;
4146 }
4147 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4148 set_bit(R5_Wantcompute,
4149 &sh->dev[qd_idx].flags);
4150 *target = qd_idx;
4151 s->uptodate++;
4152 }
4153 }
4154 }
4155 break;
4156 case check_state_compute_run:
4157 break;
4158 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11004159 pr_warn("%s: unknown check_state: %d sector: %llu\n",
4160 __func__, sh->check_state,
4161 (unsigned long long) sh->sector);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004162 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07004163 }
4164}
4165
NeilBrownd1688a62011-10-11 16:49:52 +11004166static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07004167{
4168 int i;
4169
4170 /* We have read all the blocks in this stripe and now we need to
4171 * copy some of them into a target stripe for expand.
4172 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07004173 struct dma_async_tx_descriptor *tx = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +11004174 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07004175 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4176 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11004177 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11004178 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07004179 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07004180 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07004181
Shaohua Li6d036f72015-08-13 14:31:57 -07004182 sector_t bn = raid5_compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11004183 sector_t s = raid5_compute_sector(conf, bn, 0,
4184 &dd_idx, NULL);
Shaohua Li6d036f72015-08-13 14:31:57 -07004185 sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07004186 if (sh2 == NULL)
4187 /* so far only the early blocks of this stripe
4188 * have been requested. When later blocks
4189 * get requested, we will try again
4190 */
4191 continue;
4192 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
4193 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
4194 /* must have already done this block */
Shaohua Li6d036f72015-08-13 14:31:57 -07004195 raid5_release_stripe(sh2);
Dan Williamsa4456852007-07-09 11:56:43 -07004196 continue;
4197 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07004198
4199 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07004200 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004201 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07004202 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07004203 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004204
Dan Williamsa4456852007-07-09 11:56:43 -07004205 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
4206 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
4207 for (j = 0; j < conf->raid_disks; j++)
4208 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10004209 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07004210 !test_bit(R5_Expanded, &sh2->dev[j].flags))
4211 break;
4212 if (j == conf->raid_disks) {
4213 set_bit(STRIPE_EXPAND_READY, &sh2->state);
4214 set_bit(STRIPE_HANDLE, &sh2->state);
4215 }
Shaohua Li6d036f72015-08-13 14:31:57 -07004216 raid5_release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004217
Dan Williamsa4456852007-07-09 11:56:43 -07004218 }
NeilBrowna2e08552007-09-11 15:23:36 -07004219 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11004220 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07004221}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
4223/*
4224 * handle_stripe - do things to a stripe.
4225 *
NeilBrown9a3e1102011-12-23 10:17:53 +11004226 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4227 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11004229 * return some read requests which now have data
4230 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 * schedule a read on some buffers
4232 * schedule a write of some buffers
4233 * return confirmation of parity correctness
4234 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 */
Dan Williamsa4456852007-07-09 11:56:43 -07004236
NeilBrownacfe7262011-07-27 11:00:36 +10004237static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07004238{
NeilBrownd1688a62011-10-11 16:49:52 +11004239 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08004240 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004241 struct r5dev *dev;
4242 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11004243 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07004244
NeilBrownacfe7262011-07-27 11:00:36 +10004245 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07004246
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004247 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
4248 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
NeilBrownacfe7262011-07-27 11:00:36 +10004249 s->failed_num[0] = -1;
4250 s->failed_num[1] = -1;
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004251 s->log_failed = r5l_log_disk_error(conf);
NeilBrownacfe7262011-07-27 11:00:36 +10004252
4253 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07004254 rcu_read_lock();
4255 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004256 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10004257 sector_t first_bad;
4258 int bad_sectors;
4259 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10004260
NeilBrown16a53ec2006-06-26 00:27:38 -07004261 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07004262
Dan Williams45b42332007-07-09 11:56:43 -07004263 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11004264 i, dev->flags,
4265 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004266 /* maybe we can reply to a read
4267 *
4268 * new wantfill requests are only permitted while
4269 * ops_complete_biofill is guaranteed to be inactive
4270 */
4271 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4272 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4273 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07004274
4275 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10004276 if (test_bit(R5_LOCKED, &dev->flags))
4277 s->locked++;
4278 if (test_bit(R5_UPTODATE, &dev->flags))
4279 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004280 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004281 s->compute++;
4282 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004283 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004284
NeilBrownacfe7262011-07-27 11:00:36 +10004285 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004286 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10004287 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10004288 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004289 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10004290 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004291 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004292 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004293 }
Dan Williamsa4456852007-07-09 11:56:43 -07004294 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10004295 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11004296 /* Prefer to use the replacement for reads, but only
4297 * if it is recovered enough and has no bad blocks.
4298 */
4299 rdev = rcu_dereference(conf->disks[i].replacement);
4300 if (rdev && !test_bit(Faulty, &rdev->flags) &&
4301 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
4302 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4303 &first_bad, &bad_sectors))
4304 set_bit(R5_ReadRepl, &dev->flags);
4305 else {
NeilBrowne6030cb2015-07-17 13:26:23 +10004306 if (rdev && !test_bit(Faulty, &rdev->flags))
NeilBrown9a3e1102011-12-23 10:17:53 +11004307 set_bit(R5_NeedReplace, &dev->flags);
NeilBrowne6030cb2015-07-17 13:26:23 +10004308 else
4309 clear_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11004310 rdev = rcu_dereference(conf->disks[i].rdev);
4311 clear_bit(R5_ReadRepl, &dev->flags);
4312 }
NeilBrown9283d8c2011-12-08 16:27:57 +11004313 if (rdev && test_bit(Faulty, &rdev->flags))
4314 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10004315 if (rdev) {
4316 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4317 &first_bad, &bad_sectors);
4318 if (s->blocked_rdev == NULL
4319 && (test_bit(Blocked, &rdev->flags)
4320 || is_bad < 0)) {
4321 if (is_bad < 0)
4322 set_bit(BlockedBadBlocks,
4323 &rdev->flags);
4324 s->blocked_rdev = rdev;
4325 atomic_inc(&rdev->nr_pending);
4326 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004327 }
NeilBrown415e72d2010-06-17 17:25:21 +10004328 clear_bit(R5_Insync, &dev->flags);
4329 if (!rdev)
4330 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10004331 else if (is_bad) {
4332 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10004333 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4334 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10004335 /* treat as in-sync, but with a read error
4336 * which we can now try to correct
4337 */
4338 set_bit(R5_Insync, &dev->flags);
4339 set_bit(R5_ReadError, &dev->flags);
4340 }
4341 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10004342 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11004343 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10004344 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11004345 set_bit(R5_Insync, &dev->flags);
4346 else if (test_bit(R5_UPTODATE, &dev->flags) &&
4347 test_bit(R5_Expanded, &dev->flags))
4348 /* If we've reshaped into here, we assume it is Insync.
4349 * We will shortly update recovery_offset to make
4350 * it official.
4351 */
4352 set_bit(R5_Insync, &dev->flags);
4353
NeilBrown1cc03eb2014-01-06 13:19:42 +11004354 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004355 /* This flag does not apply to '.replacement'
4356 * only to .rdev, so make sure to check that*/
4357 struct md_rdev *rdev2 = rcu_dereference(
4358 conf->disks[i].rdev);
4359 if (rdev2 == rdev)
4360 clear_bit(R5_Insync, &dev->flags);
4361 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004362 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004363 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10004364 } else
4365 clear_bit(R5_WriteError, &dev->flags);
4366 }
NeilBrown1cc03eb2014-01-06 13:19:42 +11004367 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004368 /* This flag does not apply to '.replacement'
4369 * only to .rdev, so make sure to check that*/
4370 struct md_rdev *rdev2 = rcu_dereference(
4371 conf->disks[i].rdev);
4372 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10004373 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004374 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10004375 } else
4376 clear_bit(R5_MadeGood, &dev->flags);
4377 }
NeilBrown977df362011-12-23 10:17:53 +11004378 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4379 struct md_rdev *rdev2 = rcu_dereference(
4380 conf->disks[i].replacement);
4381 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4382 s->handle_bad_blocks = 1;
4383 atomic_inc(&rdev2->nr_pending);
4384 } else
4385 clear_bit(R5_MadeGoodRepl, &dev->flags);
4386 }
NeilBrown415e72d2010-06-17 17:25:21 +10004387 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004388 /* The ReadError flag will just be confusing now */
4389 clear_bit(R5_ReadError, &dev->flags);
4390 clear_bit(R5_ReWrite, &dev->flags);
4391 }
NeilBrown415e72d2010-06-17 17:25:21 +10004392 if (test_bit(R5_ReadError, &dev->flags))
4393 clear_bit(R5_Insync, &dev->flags);
4394 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004395 if (s->failed < 2)
4396 s->failed_num[s->failed] = i;
4397 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11004398 if (rdev && !test_bit(Faulty, &rdev->flags))
4399 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10004400 }
Song Liu2ded3702016-11-17 15:24:38 -08004401
4402 if (test_bit(R5_InJournal, &dev->flags))
4403 s->injournal++;
Song Liu1e6d6902016-11-17 15:24:39 -08004404 if (test_bit(R5_InJournal, &dev->flags) && dev->written)
4405 s->just_cached++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004406 }
NeilBrown9a3e1102011-12-23 10:17:53 +11004407 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4408 /* If there is a failed device being replaced,
4409 * we must be recovering.
4410 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10004411 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11004412 * else we can only be replacing
4413 * sync and recovery both need to read all devices, and so
4414 * use the same flag.
4415 */
4416 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10004417 sh->sector >= conf->mddev->recovery_cp ||
4418 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11004419 s->syncing = 1;
4420 else
4421 s->replacing = 1;
4422 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004423 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10004424}
NeilBrownf4168852007-02-28 20:11:53 -08004425
shli@kernel.org59fc6302014-12-15 12:57:03 +11004426static int clear_batch_ready(struct stripe_head *sh)
4427{
NeilBrownb15a9db2015-05-22 15:20:04 +10004428 /* Return '1' if this is a member of batch, or
4429 * '0' if it is a lone stripe or a head which can now be
4430 * handled.
4431 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11004432 struct stripe_head *tmp;
4433 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
NeilBrownb15a9db2015-05-22 15:20:04 +10004434 return (sh->batch_head && sh->batch_head != sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11004435 spin_lock(&sh->stripe_lock);
4436 if (!sh->batch_head) {
4437 spin_unlock(&sh->stripe_lock);
4438 return 0;
4439 }
4440
4441 /*
4442 * this stripe could be added to a batch list before we check
4443 * BATCH_READY, skips it
4444 */
4445 if (sh->batch_head != sh) {
4446 spin_unlock(&sh->stripe_lock);
4447 return 1;
4448 }
4449 spin_lock(&sh->batch_lock);
4450 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4451 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4452 spin_unlock(&sh->batch_lock);
4453 spin_unlock(&sh->stripe_lock);
4454
4455 /*
4456 * BATCH_READY is cleared, no new stripes can be added.
4457 * batch_list can be accessed without lock
4458 */
4459 return 0;
4460}
4461
NeilBrown3960ce72015-05-21 12:20:36 +10004462static void break_stripe_batch_list(struct stripe_head *head_sh,
4463 unsigned long handle_flags)
shli@kernel.org72ac7332014-12-15 12:57:03 +11004464{
NeilBrown4e3d62f2015-05-21 11:50:16 +10004465 struct stripe_head *sh, *next;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004466 int i;
NeilBrownfb642b92015-05-21 12:00:47 +10004467 int do_wakeup = 0;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004468
NeilBrownbb270512015-05-08 18:19:40 +10004469 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4470
shli@kernel.org72ac7332014-12-15 12:57:03 +11004471 list_del_init(&sh->batch_list);
4472
Shaohua Lifb3229d2016-03-09 10:08:38 -08004473 WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
NeilBrown1b956f72015-05-21 12:40:26 +10004474 (1 << STRIPE_SYNCING) |
4475 (1 << STRIPE_REPLACED) |
NeilBrown1b956f72015-05-21 12:40:26 +10004476 (1 << STRIPE_DELAYED) |
4477 (1 << STRIPE_BIT_DELAY) |
4478 (1 << STRIPE_FULL_WRITE) |
4479 (1 << STRIPE_BIOFILL_RUN) |
4480 (1 << STRIPE_COMPUTE_RUN) |
4481 (1 << STRIPE_OPS_REQ_PENDING) |
4482 (1 << STRIPE_DISCARD) |
4483 (1 << STRIPE_BATCH_READY) |
4484 (1 << STRIPE_BATCH_ERR) |
Shaohua Lifb3229d2016-03-09 10:08:38 -08004485 (1 << STRIPE_BITMAP_PENDING)),
4486 "stripe state: %lx\n", sh->state);
4487 WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4488 (1 << STRIPE_REPLACED)),
4489 "head stripe state: %lx\n", head_sh->state);
NeilBrown1b956f72015-05-21 12:40:26 +10004490
4491 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
NeilBrown550da242016-03-09 12:58:25 +11004492 (1 << STRIPE_PREREAD_ACTIVE) |
NeilBrown1b956f72015-05-21 12:40:26 +10004493 (1 << STRIPE_DEGRADED)),
4494 head_sh->state & (1 << STRIPE_INSYNC));
4495
shli@kernel.org72ac7332014-12-15 12:57:03 +11004496 sh->check_state = head_sh->check_state;
4497 sh->reconstruct_state = head_sh->reconstruct_state;
NeilBrownfb642b92015-05-21 12:00:47 +10004498 for (i = 0; i < sh->disks; i++) {
4499 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4500 do_wakeup = 1;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004501 sh->dev[i].flags = head_sh->dev[i].flags &
4502 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
NeilBrownfb642b92015-05-21 12:00:47 +10004503 }
shli@kernel.org72ac7332014-12-15 12:57:03 +11004504 spin_lock_irq(&sh->stripe_lock);
4505 sh->batch_head = NULL;
4506 spin_unlock_irq(&sh->stripe_lock);
NeilBrown3960ce72015-05-21 12:20:36 +10004507 if (handle_flags == 0 ||
4508 sh->state & handle_flags)
4509 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07004510 raid5_release_stripe(sh);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004511 }
NeilBrownfb642b92015-05-21 12:00:47 +10004512 spin_lock_irq(&head_sh->stripe_lock);
4513 head_sh->batch_head = NULL;
4514 spin_unlock_irq(&head_sh->stripe_lock);
4515 for (i = 0; i < head_sh->disks; i++)
4516 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4517 do_wakeup = 1;
NeilBrown3960ce72015-05-21 12:20:36 +10004518 if (head_sh->state & handle_flags)
4519 set_bit(STRIPE_HANDLE, &head_sh->state);
NeilBrownfb642b92015-05-21 12:00:47 +10004520
4521 if (do_wakeup)
4522 wake_up(&head_sh->raid_conf->wait_for_overlap);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004523}
4524
NeilBrowncc940152011-07-26 11:35:35 +10004525static void handle_stripe(struct stripe_head *sh)
4526{
4527 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11004528 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10004529 int i;
NeilBrown84789552011-07-27 11:00:36 +10004530 int prexor;
4531 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004532 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10004533
4534 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11004535 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10004536 /* already being handled, ensure it gets handled
4537 * again when current action finishes */
4538 set_bit(STRIPE_HANDLE, &sh->state);
4539 return;
4540 }
4541
shli@kernel.org59fc6302014-12-15 12:57:03 +11004542 if (clear_batch_ready(sh) ) {
4543 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
4544 return;
4545 }
4546
NeilBrown4e3d62f2015-05-21 11:50:16 +10004547 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
NeilBrown3960ce72015-05-21 12:20:36 +10004548 break_stripe_batch_list(sh, 0);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004549
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004550 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
NeilBrownf8dfcff2013-03-12 12:18:06 +11004551 spin_lock(&sh->stripe_lock);
4552 /* Cannot process 'sync' concurrently with 'discard' */
4553 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
4554 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4555 set_bit(STRIPE_SYNCING, &sh->state);
4556 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10004557 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004558 }
4559 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10004560 }
4561 clear_bit(STRIPE_DELAYED, &sh->state);
4562
4563 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4564 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4565 (unsigned long long)sh->sector, sh->state,
4566 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4567 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10004568
NeilBrownacfe7262011-07-27 11:00:36 +10004569 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10004570
Shaohua Lib70abcb2015-08-13 14:31:58 -07004571 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
4572 goto finish;
4573
NeilBrownbc2607f2011-07-28 11:39:22 +10004574 if (s.handle_bad_blocks) {
4575 set_bit(STRIPE_HANDLE, &sh->state);
4576 goto finish;
4577 }
4578
NeilBrown474af965fe2011-07-27 11:00:36 +10004579 if (unlikely(s.blocked_rdev)) {
4580 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11004581 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10004582 set_bit(STRIPE_HANDLE, &sh->state);
4583 goto finish;
4584 }
4585 /* There is nothing for the blocked_rdev to block */
4586 rdev_dec_pending(s.blocked_rdev, conf->mddev);
4587 s.blocked_rdev = NULL;
4588 }
4589
4590 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4591 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4592 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4593 }
4594
4595 pr_debug("locked=%d uptodate=%d to_read=%d"
4596 " to_write=%d failed=%d failed_num=%d,%d\n",
4597 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4598 s.failed_num[0], s.failed_num[1]);
4599 /* check if the array has lost more than max_degraded devices and,
4600 * if so, some requests might need to be failed.
4601 */
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004602 if (s.failed > conf->max_degraded || s.log_failed) {
NeilBrown9a3f5302011-11-08 16:22:01 +11004603 sh->check_state = 0;
4604 sh->reconstruct_state = 0;
NeilBrown626f2092015-05-22 14:03:10 +10004605 break_stripe_batch_list(sh, 0);
NeilBrown9a3f5302011-11-08 16:22:01 +11004606 if (s.to_read+s.to_write+s.written)
4607 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11004608 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11004609 handle_failed_sync(conf, sh, &s);
4610 }
NeilBrown474af965fe2011-07-27 11:00:36 +10004611
NeilBrown84789552011-07-27 11:00:36 +10004612 /* Now we check to see if any write operations have recently
4613 * completed
4614 */
4615 prexor = 0;
4616 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4617 prexor = 1;
4618 if (sh->reconstruct_state == reconstruct_state_drain_result ||
4619 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
4620 sh->reconstruct_state = reconstruct_state_idle;
4621
4622 /* All the 'written' buffers and the parity block are ready to
4623 * be written back to disk
4624 */
Shaohua Li9e4447682012-10-11 13:49:49 +11004625 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
4626 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004627 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11004628 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
4629 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004630 for (i = disks; i--; ) {
4631 struct r5dev *dev = &sh->dev[i];
4632 if (test_bit(R5_LOCKED, &dev->flags) &&
4633 (i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08004634 dev->written || test_bit(R5_InJournal,
4635 &dev->flags))) {
NeilBrown84789552011-07-27 11:00:36 +10004636 pr_debug("Writing block %d\n", i);
4637 set_bit(R5_Wantwrite, &dev->flags);
4638 if (prexor)
4639 continue;
NeilBrown9c4bdf62014-08-13 09:57:07 +10004640 if (s.failed > 1)
4641 continue;
NeilBrown84789552011-07-27 11:00:36 +10004642 if (!test_bit(R5_Insync, &dev->flags) ||
4643 ((i == sh->pd_idx || i == sh->qd_idx) &&
4644 s.failed == 0))
4645 set_bit(STRIPE_INSYNC, &sh->state);
4646 }
4647 }
4648 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4649 s.dec_preread_active = 1;
4650 }
4651
NeilBrownef5b7c62012-11-22 09:13:36 +11004652 /*
4653 * might be able to return some write requests if the parity blocks
4654 * are safe, or on a failed drive
4655 */
4656 pdev = &sh->dev[sh->pd_idx];
4657 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
4658 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
4659 qdev = &sh->dev[sh->qd_idx];
4660 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
4661 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
4662 || conf->level < 6;
4663
4664 if (s.written &&
4665 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
4666 && !test_bit(R5_LOCKED, &pdev->flags)
4667 && (test_bit(R5_UPTODATE, &pdev->flags) ||
4668 test_bit(R5_Discard, &pdev->flags))))) &&
4669 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
4670 && !test_bit(R5_LOCKED, &qdev->flags)
4671 && (test_bit(R5_UPTODATE, &qdev->flags) ||
4672 test_bit(R5_Discard, &qdev->flags))))))
4673 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
4674
Song Liu1e6d6902016-11-17 15:24:39 -08004675 if (s.just_cached)
4676 r5c_handle_cached_data_endio(conf, sh, disks, &s.return_bi);
4677 r5l_stripe_write_finished(sh);
4678
NeilBrownef5b7c62012-11-22 09:13:36 +11004679 /* Now we might consider reading some blocks, either to check/generate
4680 * parity, or to satisfy requests
4681 * or to load a block that is being partially written.
4682 */
4683 if (s.to_read || s.non_overwrite
4684 || (conf->level == 6 && s.to_write && s.failed)
4685 || (s.syncing && (s.uptodate + s.compute < disks))
4686 || s.replacing
4687 || s.expanding)
4688 handle_stripe_fill(sh, &s, disks);
4689
Song Liu2ded3702016-11-17 15:24:38 -08004690 /*
4691 * When the stripe finishes full journal write cycle (write to journal
4692 * and raid disk), this is the clean up procedure so it is ready for
4693 * next operation.
4694 */
4695 r5c_finish_stripe_write_out(conf, sh, &s);
4696
4697 /*
4698 * Now to consider new write requests, cache write back and what else,
4699 * if anything should be read. We do not handle new writes when:
NeilBrown84789552011-07-27 11:00:36 +10004700 * 1/ A 'write' operation (copy+xor) is already in flight.
4701 * 2/ A 'check' operation is in flight, as it may clobber the parity
4702 * block.
Song Liu2ded3702016-11-17 15:24:38 -08004703 * 3/ A r5c cache log write is in flight.
NeilBrown84789552011-07-27 11:00:36 +10004704 */
Song Liu2ded3702016-11-17 15:24:38 -08004705
4706 if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
4707 if (!r5c_is_writeback(conf->log)) {
4708 if (s.to_write)
4709 handle_stripe_dirtying(conf, sh, &s, disks);
4710 } else { /* write back cache */
4711 int ret = 0;
4712
4713 /* First, try handle writes in caching phase */
4714 if (s.to_write)
4715 ret = r5c_try_caching_write(conf, sh, &s,
4716 disks);
4717 /*
4718 * If caching phase failed: ret == -EAGAIN
4719 * OR
4720 * stripe under reclaim: !caching && injournal
4721 *
4722 * fall back to handle_stripe_dirtying()
4723 */
4724 if (ret == -EAGAIN ||
4725 /* stripe under reclaim: !caching && injournal */
4726 (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
Song Liud7bd3982016-11-23 22:50:39 -08004727 s.injournal > 0)) {
4728 ret = handle_stripe_dirtying(conf, sh, &s,
4729 disks);
4730 if (ret == -EAGAIN)
4731 goto finish;
4732 }
Song Liu2ded3702016-11-17 15:24:38 -08004733 }
4734 }
NeilBrown84789552011-07-27 11:00:36 +10004735
4736 /* maybe we need to check and possibly fix the parity for this stripe
4737 * Any reads will already have been scheduled, so we just see if enough
4738 * data is available. The parity check is held off while parity
4739 * dependent operations are in flight.
4740 */
4741 if (sh->check_state ||
4742 (s.syncing && s.locked == 0 &&
4743 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
4744 !test_bit(STRIPE_INSYNC, &sh->state))) {
4745 if (conf->level == 6)
4746 handle_parity_checks6(conf, sh, &s, disks);
4747 else
4748 handle_parity_checks5(conf, sh, &s, disks);
4749 }
NeilBrownc5a31002011-07-27 11:00:36 +10004750
NeilBrownf94c0b62013-07-22 12:57:21 +10004751 if ((s.replacing || s.syncing) && s.locked == 0
4752 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
4753 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11004754 /* Write out to replacement devices where possible */
4755 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10004756 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
4757 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11004758 set_bit(R5_WantReplace, &sh->dev[i].flags);
4759 set_bit(R5_LOCKED, &sh->dev[i].flags);
4760 s.locked++;
4761 }
NeilBrownf94c0b62013-07-22 12:57:21 +10004762 if (s.replacing)
4763 set_bit(STRIPE_INSYNC, &sh->state);
4764 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11004765 }
4766 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10004767 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11004768 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10004769 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4770 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004771 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
4772 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10004773 }
4774
4775 /* If the failed drives are just a ReadError, then we might need
4776 * to progress the repair/check process
4777 */
4778 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
4779 for (i = 0; i < s.failed; i++) {
4780 struct r5dev *dev = &sh->dev[s.failed_num[i]];
4781 if (test_bit(R5_ReadError, &dev->flags)
4782 && !test_bit(R5_LOCKED, &dev->flags)
4783 && test_bit(R5_UPTODATE, &dev->flags)
4784 ) {
4785 if (!test_bit(R5_ReWrite, &dev->flags)) {
4786 set_bit(R5_Wantwrite, &dev->flags);
4787 set_bit(R5_ReWrite, &dev->flags);
4788 set_bit(R5_LOCKED, &dev->flags);
4789 s.locked++;
4790 } else {
4791 /* let's read it back */
4792 set_bit(R5_Wantread, &dev->flags);
4793 set_bit(R5_LOCKED, &dev->flags);
4794 s.locked++;
4795 }
4796 }
4797 }
4798
NeilBrown3687c062011-07-27 11:00:36 +10004799 /* Finish reconstruct operations initiated by the expansion process */
4800 if (sh->reconstruct_state == reconstruct_state_result) {
4801 struct stripe_head *sh_src
Shaohua Li6d036f72015-08-13 14:31:57 -07004802 = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrown3687c062011-07-27 11:00:36 +10004803 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
4804 /* sh cannot be written until sh_src has been read.
4805 * so arrange for sh to be delayed a little
4806 */
4807 set_bit(STRIPE_DELAYED, &sh->state);
4808 set_bit(STRIPE_HANDLE, &sh->state);
4809 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
4810 &sh_src->state))
4811 atomic_inc(&conf->preread_active_stripes);
Shaohua Li6d036f72015-08-13 14:31:57 -07004812 raid5_release_stripe(sh_src);
NeilBrown3687c062011-07-27 11:00:36 +10004813 goto finish;
4814 }
4815 if (sh_src)
Shaohua Li6d036f72015-08-13 14:31:57 -07004816 raid5_release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07004817
NeilBrown3687c062011-07-27 11:00:36 +10004818 sh->reconstruct_state = reconstruct_state_idle;
4819 clear_bit(STRIPE_EXPANDING, &sh->state);
4820 for (i = conf->raid_disks; i--; ) {
4821 set_bit(R5_Wantwrite, &sh->dev[i].flags);
4822 set_bit(R5_LOCKED, &sh->dev[i].flags);
4823 s.locked++;
4824 }
4825 }
4826
4827 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
4828 !sh->reconstruct_state) {
4829 /* Need to write out all blocks after computing parity */
4830 sh->disks = conf->raid_disks;
4831 stripe_set_idx(sh->sector, conf, 0, sh);
4832 schedule_reconstruction(sh, &s, 1, 1);
4833 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
4834 clear_bit(STRIPE_EXPAND_READY, &sh->state);
4835 atomic_dec(&conf->reshape_stripes);
4836 wake_up(&conf->wait_for_overlap);
4837 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4838 }
4839
4840 if (s.expanding && s.locked == 0 &&
4841 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
4842 handle_stripe_expansion(conf, sh);
4843
4844finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07004845 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10004846 if (unlikely(s.blocked_rdev)) {
4847 if (conf->mddev->external)
4848 md_wait_for_blocked_rdev(s.blocked_rdev,
4849 conf->mddev);
4850 else
4851 /* Internal metadata will immediately
4852 * be written by raid5d, so we don't
4853 * need to wait here.
4854 */
4855 rdev_dec_pending(s.blocked_rdev,
4856 conf->mddev);
4857 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004858
NeilBrownbc2607f2011-07-28 11:39:22 +10004859 if (s.handle_bad_blocks)
4860 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004861 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10004862 struct r5dev *dev = &sh->dev[i];
4863 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
4864 /* We own a safe reference to the rdev */
4865 rdev = conf->disks[i].rdev;
4866 if (!rdev_set_badblocks(rdev, sh->sector,
4867 STRIPE_SECTORS, 0))
4868 md_error(conf->mddev, rdev);
4869 rdev_dec_pending(rdev, conf->mddev);
4870 }
NeilBrownb84db562011-07-28 11:39:23 +10004871 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
4872 rdev = conf->disks[i].rdev;
4873 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004874 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10004875 rdev_dec_pending(rdev, conf->mddev);
4876 }
NeilBrown977df362011-12-23 10:17:53 +11004877 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
4878 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11004879 if (!rdev)
4880 /* rdev have been moved down */
4881 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11004882 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004883 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11004884 rdev_dec_pending(rdev, conf->mddev);
4885 }
NeilBrownbc2607f2011-07-28 11:39:22 +10004886 }
4887
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004888 if (s.ops_request)
4889 raid_run_ops(sh, s.ops_request);
4890
Dan Williamsf0e43bc2008-06-28 08:31:55 +10004891 ops_run_io(sh, &s);
4892
NeilBrownc5709ef2011-07-26 11:35:20 +10004893 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11004894 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02004895 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11004896 * have actually been submitted.
4897 */
4898 atomic_dec(&conf->preread_active_stripes);
4899 if (atomic_read(&conf->preread_active_stripes) <
4900 IO_THRESHOLD)
4901 md_wakeup_thread(conf->mddev->thread);
4902 }
4903
NeilBrownc3cce6c2015-08-14 12:47:33 +10004904 if (!bio_list_empty(&s.return_bi)) {
Shaohua Li29530792016-12-08 15:48:19 -08004905 if (test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) {
NeilBrownc3cce6c2015-08-14 12:47:33 +10004906 spin_lock_irq(&conf->device_lock);
4907 bio_list_merge(&conf->return_bi, &s.return_bi);
4908 spin_unlock_irq(&conf->device_lock);
4909 md_wakeup_thread(conf->mddev->thread);
4910 } else
4911 return_io(&s.return_bi);
4912 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004913
Dan Williams257a4b42011-11-08 16:22:06 +11004914 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07004915}
4916
NeilBrownd1688a62011-10-11 16:49:52 +11004917static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918{
4919 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
4920 while (!list_empty(&conf->delayed_list)) {
4921 struct list_head *l = conf->delayed_list.next;
4922 struct stripe_head *sh;
4923 sh = list_entry(l, struct stripe_head, lru);
4924 list_del_init(l);
4925 clear_bit(STRIPE_DELAYED, &sh->state);
4926 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4927 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004928 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08004929 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 }
NeilBrown482c0832011-04-18 18:25:42 +10004931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932}
4933
Shaohua Li566c09c2013-11-14 15:16:17 +11004934static void activate_bit_delay(struct r5conf *conf,
4935 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07004936{
4937 /* device_lock is held */
4938 struct list_head head;
4939 list_add(&head, &conf->bitmap_list);
4940 list_del_init(&conf->bitmap_list);
4941 while (!list_empty(&head)) {
4942 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11004943 int hash;
NeilBrown72626682005-09-09 16:23:54 -07004944 list_del_init(&sh->lru);
4945 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11004946 hash = sh->hash_lock_index;
4947 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07004948 }
4949}
4950
NeilBrown5c675f82014-12-15 12:56:56 +11004951static int raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07004952{
NeilBrownd1688a62011-10-11 16:49:52 +11004953 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07004954
4955 /* No difference between reads and writes. Just check
4956 * how busy the stripe_cache is
4957 */
NeilBrown3fa841d2009-09-23 18:10:29 +10004958
NeilBrown54233992015-02-26 12:21:04 +11004959 if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state))
NeilBrownf022b2f2006-10-03 01:15:56 -07004960 return 1;
Song Liua39f7af2016-11-17 15:24:40 -08004961
4962 /* Also checks whether there is pressure on r5cache log space */
4963 if (test_bit(R5C_LOG_TIGHT, &conf->cache_state))
4964 return 1;
NeilBrownf022b2f2006-10-03 01:15:56 -07004965 if (conf->quiesce)
4966 return 1;
Shaohua Li4bda5562013-11-14 15:16:17 +11004967 if (atomic_read(&conf->empty_inactive_list_nr))
NeilBrownf022b2f2006-10-03 01:15:56 -07004968 return 1;
4969
4970 return 0;
4971}
4972
NeilBrownfd01b882011-10-11 16:47:53 +11004973static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004974{
NeilBrown3cb5edf2015-07-15 17:24:17 +10004975 struct r5conf *conf = mddev->private;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004976 sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev);
NeilBrown3cb5edf2015-07-15 17:24:17 +10004977 unsigned int chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004978 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004979
NeilBrown3cb5edf2015-07-15 17:24:17 +10004980 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004981 return chunk_sectors >=
4982 ((sector & (chunk_sectors - 1)) + bio_sectors);
4983}
4984
4985/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004986 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4987 * later sampled by raid5d.
4988 */
NeilBrownd1688a62011-10-11 16:49:52 +11004989static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004990{
4991 unsigned long flags;
4992
4993 spin_lock_irqsave(&conf->device_lock, flags);
4994
4995 bi->bi_next = conf->retry_read_aligned_list;
4996 conf->retry_read_aligned_list = bi;
4997
4998 spin_unlock_irqrestore(&conf->device_lock, flags);
4999 md_wakeup_thread(conf->mddev->thread);
5000}
5001
NeilBrownd1688a62011-10-11 16:49:52 +11005002static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005003{
5004 struct bio *bi;
5005
5006 bi = conf->retry_read_aligned;
5007 if (bi) {
5008 conf->retry_read_aligned = NULL;
5009 return bi;
5010 }
5011 bi = conf->retry_read_aligned_list;
5012 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08005013 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005014 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02005015 /*
5016 * this sets the active strip count to 1 and the processed
5017 * strip count to zero (upper 8 bits)
5018 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10005019 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005020 }
5021
5022 return bi;
5023}
5024
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005025/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005026 * The "raid5_align_endio" should check if the read succeeded and if it
5027 * did, call bio_endio on the original bio (having bio_put the new bio
5028 * first).
5029 * If the read failed..
5030 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005031static void raid5_align_endio(struct bio *bi)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005032{
5033 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11005034 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005035 struct r5conf *conf;
NeilBrown3cb03002011-10-11 16:45:26 +11005036 struct md_rdev *rdev;
Sasha Levin9b81c842015-08-10 19:05:18 -04005037 int error = bi->bi_error;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005038
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005039 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005040
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005041 rdev = (void*)raid_bi->bi_next;
5042 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11005043 mddev = rdev->mddev;
5044 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005045
5046 rdev_dec_pending(rdev, conf->mddev);
5047
Sasha Levin9b81c842015-08-10 19:05:18 -04005048 if (!error) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005049 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
5050 raid_bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005051 bio_endio(raid_bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005052 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10005053 wake_up(&conf->wait_for_quiescent);
NeilBrown6712ecf2007-09-27 12:47:43 +02005054 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005055 }
5056
Dan Williams45b42332007-07-09 11:56:43 -07005057 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005058
5059 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005060}
5061
Ming Lin7ef6b122015-05-06 22:51:24 -07005062static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005063{
NeilBrownd1688a62011-10-11 16:49:52 +11005064 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11005065 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005066 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11005067 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11005068 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005069
5070 if (!in_chunk_boundary(mddev, raid_bio)) {
Ming Lin7ef6b122015-05-06 22:51:24 -07005071 pr_debug("%s: non aligned\n", __func__);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005072 return 0;
5073 }
5074 /*
Ming Leid7a10302017-02-14 23:29:03 +08005075 * use bio_clone_fast to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005076 */
Ming Leid7a10302017-02-14 23:29:03 +08005077 align_bi = bio_clone_fast(raid_bio, GFP_NOIO, mddev->bio_set);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005078 if (!align_bi)
5079 return 0;
5080 /*
5081 * set bi_end_io to a new function, and set bi_private to the
5082 * original bio.
5083 */
5084 align_bi->bi_end_io = raid5_align_endio;
5085 align_bi->bi_private = raid_bio;
5086 /*
5087 * compute position
5088 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005089 align_bi->bi_iter.bi_sector =
5090 raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
5091 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005092
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005093 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005094 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11005095 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
5096 if (!rdev || test_bit(Faulty, &rdev->flags) ||
5097 rdev->recovery_offset < end_sector) {
5098 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
5099 if (rdev &&
5100 (test_bit(Faulty, &rdev->flags) ||
5101 !(test_bit(In_sync, &rdev->flags) ||
5102 rdev->recovery_offset >= end_sector)))
5103 rdev = NULL;
5104 }
Song Liu03b047f2017-01-11 13:39:14 -08005105
5106 if (r5c_big_stripe_cached(conf, align_bi->bi_iter.bi_sector)) {
5107 rcu_read_unlock();
5108 bio_put(align_bi);
5109 return 0;
5110 }
5111
NeilBrown671488c2011-12-23 10:17:52 +11005112 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10005113 sector_t first_bad;
5114 int bad_sectors;
5115
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005116 atomic_inc(&rdev->nr_pending);
5117 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005118 raid_bio->bi_next = (void*)rdev;
5119 align_bi->bi_bdev = rdev->bdev;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06005120 bio_clear_flag(align_bi, BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005121
Kent Overstreet7140aaf2013-09-25 13:37:01 -07005122 if (is_badblock(rdev, align_bi->bi_iter.bi_sector,
Kent Overstreet4f024f32013-10-11 15:44:27 -07005123 bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10005124 &first_bad, &bad_sectors)) {
Neil Brown387bb172007-02-08 14:20:29 -08005125 bio_put(align_bi);
5126 rdev_dec_pending(rdev, mddev);
5127 return 0;
5128 }
5129
majianpeng6c0544e2012-06-12 08:31:10 +08005130 /* No reshape active, so we can trust rdev->data_offset */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005131 align_bi->bi_iter.bi_sector += rdev->data_offset;
majianpeng6c0544e2012-06-12 08:31:10 +08005132
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005133 spin_lock_irq(&conf->device_lock);
Yuanhan Liub1b46482015-05-08 18:19:06 +10005134 wait_event_lock_irq(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005135 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01005136 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005137 atomic_inc(&conf->active_aligned_reads);
5138 spin_unlock_irq(&conf->device_lock);
5139
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005140 if (mddev->gendisk)
5141 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
5142 align_bi, disk_devt(mddev->gendisk),
Kent Overstreet4f024f32013-10-11 15:44:27 -07005143 raid_bio->bi_iter.bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005144 generic_make_request(align_bi);
5145 return 1;
5146 } else {
5147 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005148 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005149 return 0;
5150 }
5151}
5152
Ming Lin7ef6b122015-05-06 22:51:24 -07005153static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
5154{
5155 struct bio *split;
5156
5157 do {
5158 sector_t sector = raid_bio->bi_iter.bi_sector;
5159 unsigned chunk_sects = mddev->chunk_sectors;
5160 unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
5161
5162 if (sectors < bio_sectors(raid_bio)) {
5163 split = bio_split(raid_bio, sectors, GFP_NOIO, fs_bio_set);
5164 bio_chain(split, raid_bio);
5165 } else
5166 split = raid_bio;
5167
5168 if (!raid5_read_one_chunk(mddev, split)) {
5169 if (split != raid_bio)
5170 generic_make_request(raid_bio);
5171 return split;
5172 }
5173 } while (split != raid_bio);
5174
5175 return NULL;
5176}
5177
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005178/* __get_priority_stripe - get the next stripe to process
5179 *
5180 * Full stripe writes are allowed to pass preread active stripes up until
5181 * the bypass_threshold is exceeded. In general the bypass_count
5182 * increments when the handle_list is handled before the hold_list; however, it
5183 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
5184 * stripe with in flight i/o. The bypass_count will be reset when the
5185 * head of the hold_list has changed, i.e. the head was promoted to the
5186 * handle_list.
5187 */
Shaohua Li851c30c2013-08-28 14:30:16 +08005188static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005189{
Shaohua Li535ae4e2017-02-15 19:37:32 -08005190 struct stripe_head *sh, *tmp;
Shaohua Li851c30c2013-08-28 14:30:16 +08005191 struct list_head *handle_list = NULL;
Shaohua Li535ae4e2017-02-15 19:37:32 -08005192 struct r5worker_group *wg;
5193 bool second_try = !r5c_is_writeback(conf->log);
5194 bool try_loprio = test_bit(R5C_LOG_TIGHT, &conf->cache_state);
Shaohua Li851c30c2013-08-28 14:30:16 +08005195
Shaohua Li535ae4e2017-02-15 19:37:32 -08005196again:
5197 wg = NULL;
5198 sh = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005199 if (conf->worker_cnt_per_group == 0) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005200 handle_list = try_loprio ? &conf->loprio_list :
5201 &conf->handle_list;
Shaohua Li851c30c2013-08-28 14:30:16 +08005202 } else if (group != ANY_GROUP) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005203 handle_list = try_loprio ? &conf->worker_groups[group].loprio_list :
5204 &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005205 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08005206 } else {
5207 int i;
5208 for (i = 0; i < conf->group_cnt; i++) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005209 handle_list = try_loprio ? &conf->worker_groups[i].loprio_list :
5210 &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005211 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08005212 if (!list_empty(handle_list))
5213 break;
5214 }
5215 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005216
5217 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5218 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08005219 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005220 list_empty(&conf->hold_list) ? "empty" : "busy",
5221 atomic_read(&conf->pending_full_writes), conf->bypass_count);
5222
Shaohua Li851c30c2013-08-28 14:30:16 +08005223 if (!list_empty(handle_list)) {
5224 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005225
5226 if (list_empty(&conf->hold_list))
5227 conf->bypass_count = 0;
5228 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
5229 if (conf->hold_list.next == conf->last_hold)
5230 conf->bypass_count++;
5231 else {
5232 conf->last_hold = conf->hold_list.next;
5233 conf->bypass_count -= conf->bypass_threshold;
5234 if (conf->bypass_count < 0)
5235 conf->bypass_count = 0;
5236 }
5237 }
5238 } else if (!list_empty(&conf->hold_list) &&
5239 ((conf->bypass_threshold &&
5240 conf->bypass_count > conf->bypass_threshold) ||
5241 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08005242
5243 list_for_each_entry(tmp, &conf->hold_list, lru) {
5244 if (conf->worker_cnt_per_group == 0 ||
5245 group == ANY_GROUP ||
5246 !cpu_online(tmp->cpu) ||
5247 cpu_to_group(tmp->cpu) == group) {
5248 sh = tmp;
5249 break;
5250 }
5251 }
5252
5253 if (sh) {
5254 conf->bypass_count -= conf->bypass_threshold;
5255 if (conf->bypass_count < 0)
5256 conf->bypass_count = 0;
5257 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08005258 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005259 }
5260
Shaohua Li535ae4e2017-02-15 19:37:32 -08005261 if (!sh) {
5262 if (second_try)
5263 return NULL;
5264 second_try = true;
5265 try_loprio = !try_loprio;
5266 goto again;
5267 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005268
Shaohua Libfc90cb2013-08-29 15:40:32 +08005269 if (wg) {
5270 wg->stripes_cnt--;
5271 sh->group = NULL;
5272 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005273 list_del_init(&sh->lru);
Shaohua Lic7a6d352014-04-15 09:12:54 +08005274 BUG_ON(atomic_inc_return(&sh->count) != 1);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005275 return sh;
5276}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005277
Shaohua Li8811b592012-08-02 08:33:00 +10005278struct raid5_plug_cb {
5279 struct blk_plug_cb cb;
5280 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11005281 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10005282};
5283
5284static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
5285{
5286 struct raid5_plug_cb *cb = container_of(
5287 blk_cb, struct raid5_plug_cb, cb);
5288 struct stripe_head *sh;
5289 struct mddev *mddev = cb->cb.data;
5290 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11005291 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11005292 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10005293
5294 if (cb->list.next && !list_empty(&cb->list)) {
5295 spin_lock_irq(&conf->device_lock);
5296 while (!list_empty(&cb->list)) {
5297 sh = list_first_entry(&cb->list, struct stripe_head, lru);
5298 list_del_init(&sh->lru);
5299 /*
5300 * avoid race release_stripe_plug() sees
5301 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5302 * is still in our list
5303 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005304 smp_mb__before_atomic();
Shaohua Li8811b592012-08-02 08:33:00 +10005305 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08005306 /*
5307 * STRIPE_ON_RELEASE_LIST could be set here. In that
5308 * case, the count is always > 1 here
5309 */
Shaohua Li566c09c2013-11-14 15:16:17 +11005310 hash = sh->hash_lock_index;
5311 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11005312 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10005313 }
5314 spin_unlock_irq(&conf->device_lock);
5315 }
Shaohua Li566c09c2013-11-14 15:16:17 +11005316 release_inactive_stripe_list(conf, cb->temp_inactive_list,
5317 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005318 if (mddev->queue)
5319 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10005320 kfree(cb);
5321}
5322
5323static void release_stripe_plug(struct mddev *mddev,
5324 struct stripe_head *sh)
5325{
5326 struct blk_plug_cb *blk_cb = blk_check_plugged(
5327 raid5_unplug, mddev,
5328 sizeof(struct raid5_plug_cb));
5329 struct raid5_plug_cb *cb;
5330
5331 if (!blk_cb) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005332 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005333 return;
5334 }
5335
5336 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
5337
Shaohua Li566c09c2013-11-14 15:16:17 +11005338 if (cb->list.next == NULL) {
5339 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10005340 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11005341 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5342 INIT_LIST_HEAD(cb->temp_inactive_list + i);
5343 }
Shaohua Li8811b592012-08-02 08:33:00 +10005344
5345 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
5346 list_add_tail(&sh->lru, &cb->list);
5347 else
Shaohua Li6d036f72015-08-13 14:31:57 -07005348 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005349}
5350
Shaohua Li620125f2012-10-11 13:49:05 +11005351static void make_discard_request(struct mddev *mddev, struct bio *bi)
5352{
5353 struct r5conf *conf = mddev->private;
5354 sector_t logical_sector, last_sector;
5355 struct stripe_head *sh;
5356 int remaining;
5357 int stripe_sectors;
5358
5359 if (mddev->reshape_position != MaxSector)
5360 /* Skip discard while reshape is happening */
5361 return;
5362
Kent Overstreet4f024f32013-10-11 15:44:27 -07005363 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
5364 last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9);
Shaohua Li620125f2012-10-11 13:49:05 +11005365
5366 bi->bi_next = NULL;
5367 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
5368
5369 stripe_sectors = conf->chunk_sectors *
5370 (conf->raid_disks - conf->max_degraded);
5371 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5372 stripe_sectors);
5373 sector_div(last_sector, stripe_sectors);
5374
5375 logical_sector *= conf->chunk_sectors;
5376 last_sector *= conf->chunk_sectors;
5377
5378 for (; logical_sector < last_sector;
5379 logical_sector += STRIPE_SECTORS) {
5380 DEFINE_WAIT(w);
5381 int d;
5382 again:
Shaohua Li6d036f72015-08-13 14:31:57 -07005383 sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
Shaohua Li620125f2012-10-11 13:49:05 +11005384 prepare_to_wait(&conf->wait_for_overlap, &w,
5385 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005386 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5387 if (test_bit(STRIPE_SYNCING, &sh->state)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005388 raid5_release_stripe(sh);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005389 schedule();
5390 goto again;
5391 }
5392 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11005393 spin_lock_irq(&sh->stripe_lock);
5394 for (d = 0; d < conf->raid_disks; d++) {
5395 if (d == sh->pd_idx || d == sh->qd_idx)
5396 continue;
5397 if (sh->dev[d].towrite || sh->dev[d].toread) {
5398 set_bit(R5_Overlap, &sh->dev[d].flags);
5399 spin_unlock_irq(&sh->stripe_lock);
Shaohua Li6d036f72015-08-13 14:31:57 -07005400 raid5_release_stripe(sh);
Shaohua Li620125f2012-10-11 13:49:05 +11005401 schedule();
5402 goto again;
5403 }
5404 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11005405 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11005406 finish_wait(&conf->wait_for_overlap, &w);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005407 sh->overwrite_disks = 0;
Shaohua Li620125f2012-10-11 13:49:05 +11005408 for (d = 0; d < conf->raid_disks; d++) {
5409 if (d == sh->pd_idx || d == sh->qd_idx)
5410 continue;
5411 sh->dev[d].towrite = bi;
5412 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
5413 raid5_inc_bi_active_stripes(bi);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005414 sh->overwrite_disks++;
Shaohua Li620125f2012-10-11 13:49:05 +11005415 }
5416 spin_unlock_irq(&sh->stripe_lock);
5417 if (conf->mddev->bitmap) {
5418 for (d = 0;
5419 d < conf->raid_disks - conf->max_degraded;
5420 d++)
5421 bitmap_startwrite(mddev->bitmap,
5422 sh->sector,
5423 STRIPE_SECTORS,
5424 0);
5425 sh->bm_seq = conf->seq_flush + 1;
5426 set_bit(STRIPE_BIT_DELAY, &sh->state);
5427 }
5428
5429 set_bit(STRIPE_HANDLE, &sh->state);
5430 clear_bit(STRIPE_DELAYED, &sh->state);
5431 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5432 atomic_inc(&conf->preread_active_stripes);
5433 release_stripe_plug(mddev, sh);
5434 }
5435
5436 remaining = raid5_dec_bi_active_stripes(bi);
5437 if (remaining == 0) {
5438 md_write_end(mddev);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005439 bio_endio(bi);
Shaohua Li620125f2012-10-11 13:49:05 +11005440 }
5441}
5442
Shaohua Li849674e2016-01-20 13:52:20 -08005443static void raid5_make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444{
NeilBrownd1688a62011-10-11 16:49:52 +11005445 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11005446 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 sector_t new_sector;
5448 sector_t logical_sector, last_sector;
5449 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01005450 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11005451 int remaining;
Shaohua Li27c0f682014-04-09 11:25:47 +08005452 DEFINE_WAIT(w);
5453 bool do_prepare;
Song Liu3bddb7f2016-11-18 16:46:50 -08005454 bool do_flush = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
Jens Axboe1eff9d32016-08-05 15:35:16 -06005456 if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
Shaohua Li828cbe92015-09-02 13:49:49 -07005457 int ret = r5l_handle_flush_request(conf->log, bi);
5458
5459 if (ret == 0)
5460 return;
5461 if (ret == -ENODEV) {
5462 md_flush_request(mddev, bi);
5463 return;
5464 }
5465 /* ret == -EAGAIN, fallback */
Song Liu3bddb7f2016-11-18 16:46:50 -08005466 /*
5467 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
5468 * we need to flush journal device
5469 */
5470 do_flush = bi->bi_opf & REQ_PREFLUSH;
NeilBrowne5dcdd82005-09-09 16:23:41 -07005471 }
5472
NeilBrown3d310eb2005-06-21 17:17:26 -07005473 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07005474
Eric Mei9ffc8f72015-03-18 23:39:11 -06005475 /*
5476 * If array is degraded, better not do chunk aligned read because
5477 * later we might have to read it again in order to reconstruct
5478 * data on failed drives.
5479 */
5480 if (rw == READ && mddev->degraded == 0 &&
Ming Lin7ef6b122015-05-06 22:51:24 -07005481 mddev->reshape_position == MaxSector) {
5482 bi = chunk_aligned_read(mddev, bi);
5483 if (!bi)
5484 return;
5485 }
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08005486
Mike Christie796a5cf2016-06-05 14:32:07 -05005487 if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
Shaohua Li620125f2012-10-11 13:49:05 +11005488 make_discard_request(mddev, bi);
5489 return;
5490 }
5491
Kent Overstreet4f024f32013-10-11 15:44:27 -07005492 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005493 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 bi->bi_next = NULL;
5495 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07005496
Shaohua Li27c0f682014-04-09 11:25:47 +08005497 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005499 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10005500 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08005501
Shaohua Li27c0f682014-04-09 11:25:47 +08005502 do_prepare = false;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005503 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10005504 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11005505 previous = 0;
Shaohua Li27c0f682014-04-09 11:25:47 +08005506 if (do_prepare)
5507 prepare_to_wait(&conf->wait_for_overlap, &w,
5508 TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005509 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11005510 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08005511 * 64bit on a 32bit platform, and so it might be
5512 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02005513 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08005514 * the lock is dropped, so once we get a reference
5515 * to the stripe that we think it is, we will have
5516 * to check again.
5517 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005518 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005519 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005520 ? logical_sector < conf->reshape_progress
5521 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005522 previous = 1;
5523 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10005524 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005525 ? logical_sector < conf->reshape_safe
5526 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08005527 spin_unlock_irq(&conf->device_lock);
5528 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005529 do_prepare = true;
NeilBrownb578d552006-03-27 01:18:12 -08005530 goto retry;
5531 }
5532 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005533 spin_unlock_irq(&conf->device_lock);
5534 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005535
NeilBrown112bf892009-03-31 14:39:38 +11005536 new_sector = raid5_compute_sector(conf, logical_sector,
5537 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11005538 &dd_idx, NULL);
Shaohua Li849674e2016-01-20 13:52:20 -08005539 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10005540 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 (unsigned long long)logical_sector);
5542
Shaohua Li6d036f72015-08-13 14:31:57 -07005543 sh = raid5_get_active_stripe(conf, new_sector, previous,
Jens Axboe1eff9d32016-08-05 15:35:16 -06005544 (bi->bi_opf & REQ_RAHEAD), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11005546 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005547 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08005548 * stripe, so we must do the range check again.
5549 * Expansion could still move past after this
5550 * test, but as we are holding a reference to
5551 * 'sh', we know that if that happens,
5552 * STRIPE_EXPANDING will get set and the expansion
5553 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005554 */
5555 int must_retry = 0;
5556 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005557 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11005558 ? logical_sector >= conf->reshape_progress
5559 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005560 /* mismatch, need to try again */
5561 must_retry = 1;
5562 spin_unlock_irq(&conf->device_lock);
5563 if (must_retry) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005564 raid5_release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07005565 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005566 do_prepare = true;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005567 goto retry;
5568 }
5569 }
NeilBrownc46501b2013-08-27 15:52:13 +10005570 if (read_seqcount_retry(&conf->gen_lock, seq)) {
5571 /* Might have got the wrong stripe_head
5572 * by accident
5573 */
Shaohua Li6d036f72015-08-13 14:31:57 -07005574 raid5_release_stripe(sh);
NeilBrownc46501b2013-08-27 15:52:13 +10005575 goto retry;
5576 }
NeilBrowne62e58a2009-07-01 13:15:35 +10005577
Namhyung Kimffd96e32011-07-18 17:38:51 +10005578 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10005579 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08005580 logical_sector < mddev->suspend_hi) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005581 raid5_release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10005582 /* As the suspend_* range is controlled by
5583 * userspace, we want an interruptible
5584 * wait.
5585 */
5586 flush_signals(current);
5587 prepare_to_wait(&conf->wait_for_overlap,
5588 &w, TASK_INTERRUPTIBLE);
5589 if (logical_sector >= mddev->suspend_lo &&
Shaohua Li27c0f682014-04-09 11:25:47 +08005590 logical_sector < mddev->suspend_hi) {
NeilBrowne62e58a2009-07-01 13:15:35 +10005591 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005592 do_prepare = true;
5593 }
NeilBrowne464eaf2006-03-27 01:18:14 -08005594 goto retry;
5595 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005596
5597 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
shli@kernel.orgda41ba62014-12-15 12:57:03 +11005598 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005599 /* Stripe is busy expanding or
5600 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 * and wait a while
5602 */
NeilBrown482c0832011-04-18 18:25:42 +10005603 md_wakeup_thread(mddev->thread);
Shaohua Li6d036f72015-08-13 14:31:57 -07005604 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005606 do_prepare = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 goto retry;
5608 }
Song Liu3bddb7f2016-11-18 16:46:50 -08005609 if (do_flush) {
5610 set_bit(STRIPE_R5C_PREFLUSH, &sh->state);
5611 /* we only need flush for one stripe */
5612 do_flush = false;
5613 }
5614
NeilBrown6ed30032008-02-06 01:40:00 -08005615 set_bit(STRIPE_HANDLE, &sh->state);
5616 clear_bit(STRIPE_DELAYED, &sh->state);
shli@kernel.org59fc6302014-12-15 12:57:03 +11005617 if ((!sh->batch_head || sh == sh->batch_head) &&
Jens Axboe1eff9d32016-08-05 15:35:16 -06005618 (bi->bi_opf & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11005619 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5620 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10005621 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 } else {
5623 /* cannot get stripe for read-ahead, just give-up */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005624 bi->bi_error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 break;
5626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 }
Shaohua Li27c0f682014-04-09 11:25:47 +08005628 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown7c13edc2011-04-18 18:25:43 +10005629
Shaohua Lie7836bd62012-07-19 16:01:31 +10005630 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08005631 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632
NeilBrown16a53ec2006-06-26 00:27:38 -07005633 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02005635
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005636 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
5637 bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005638 bio_endio(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640}
5641
NeilBrownfd01b882011-10-11 16:47:53 +11005642static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11005643
NeilBrownfd01b882011-10-11 16:47:53 +11005644static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645{
NeilBrown52c03292006-06-26 00:27:43 -07005646 /* reshaping is quite different to recovery/resync so it is
5647 * handled quite separately ... here.
5648 *
5649 * On each call to sync_request, we gather one chunk worth of
5650 * destination stripes and flag them as expanding.
5651 * Then we find all the source stripes and request reads.
5652 * As the reads complete, handle_stripe will copy the data
5653 * into the destination stripe and release that stripe.
5654 */
NeilBrownd1688a62011-10-11 16:49:52 +11005655 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005657 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08005658 int raid_disks = conf->previous_raid_disks;
5659 int data_disks = raid_disks - conf->max_degraded;
5660 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07005661 int i;
5662 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11005663 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11005664 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11005665 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11005666 struct list_head stripes;
NeilBrown92140482015-07-06 12:28:45 +10005667 sector_t retn;
NeilBrown52c03292006-06-26 00:27:43 -07005668
NeilBrownfef9c612009-03-31 15:16:46 +11005669 if (sector_nr == 0) {
5670 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10005671 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005672 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
5673 sector_nr = raid5_size(mddev, 0, 0)
5674 - conf->reshape_progress;
NeilBrown6cbd8142015-07-24 13:30:32 +10005675 } else if (mddev->reshape_backwards &&
5676 conf->reshape_progress == MaxSector) {
5677 /* shouldn't happen, but just in case, finish up.*/
5678 sector_nr = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +10005679 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005680 conf->reshape_progress > 0)
5681 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005682 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005683 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11005684 mddev->curr_resync_completed = sector_nr;
5685 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11005686 *skipped = 1;
NeilBrown92140482015-07-06 12:28:45 +10005687 retn = sector_nr;
5688 goto finish;
NeilBrownfef9c612009-03-31 15:16:46 +11005689 }
NeilBrown52c03292006-06-26 00:27:43 -07005690 }
5691
NeilBrown7a661382009-03-31 15:21:40 +11005692 /* We need to process a full chunk at a time.
5693 * If old and new chunk sizes differ, we need to process the
5694 * largest of these
5695 */
NeilBrown3cb5edf2015-07-15 17:24:17 +10005696
5697 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
NeilBrown7a661382009-03-31 15:21:40 +11005698
NeilBrownb5254dd2012-05-21 09:27:01 +10005699 /* We update the metadata at least every 10 seconds, or when
5700 * the data about to be copied would over-write the source of
5701 * the data at the front of the range. i.e. one new_stripe
5702 * along from reshape_progress new_maps to after where
5703 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07005704 */
NeilBrownfef9c612009-03-31 15:16:46 +11005705 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005706 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11005707 readpos = conf->reshape_progress;
5708 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005709 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08005710 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10005711 if (mddev->reshape_backwards) {
NeilBrownc74c0d72015-07-15 17:54:15 +10005712 BUG_ON(writepos < reshape_sectors);
5713 writepos -= reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11005714 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11005715 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11005716 } else {
NeilBrown7a661382009-03-31 15:21:40 +11005717 writepos += reshape_sectors;
NeilBrownc74c0d72015-07-15 17:54:15 +10005718 /* readpos and safepos are worst-case calculations.
5719 * A negative number is overly pessimistic, and causes
5720 * obvious problems for unsigned storage. So clip to 0.
5721 */
NeilBrowned37d832009-05-27 21:39:05 +10005722 readpos -= min_t(sector_t, reshape_sectors, readpos);
5723 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11005724 }
NeilBrown52c03292006-06-26 00:27:43 -07005725
NeilBrownb5254dd2012-05-21 09:27:01 +10005726 /* Having calculated the 'writepos' possibly use it
5727 * to set 'stripe_addr' which is where we will write to.
5728 */
5729 if (mddev->reshape_backwards) {
5730 BUG_ON(conf->reshape_progress == 0);
5731 stripe_addr = writepos;
5732 BUG_ON((mddev->dev_sectors &
5733 ~((sector_t)reshape_sectors - 1))
5734 - reshape_sectors - stripe_addr
5735 != sector_nr);
5736 } else {
5737 BUG_ON(writepos != sector_nr + reshape_sectors);
5738 stripe_addr = sector_nr;
5739 }
5740
NeilBrownc8f517c2009-03-31 15:28:40 +11005741 /* 'writepos' is the most advanced device address we might write.
5742 * 'readpos' is the least advanced device address we might read.
5743 * 'safepos' is the least address recorded in the metadata as having
5744 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10005745 * If there is a min_offset_diff, these are adjusted either by
5746 * increasing the safepos/readpos if diff is negative, or
5747 * increasing writepos if diff is positive.
5748 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11005749 * ensure safety in the face of a crash - that must be done by userspace
5750 * making a backup of the data. So in that case there is no particular
5751 * rush to update metadata.
5752 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5753 * update the metadata to advance 'safepos' to match 'readpos' so that
5754 * we can be safe in the event of a crash.
5755 * So we insist on updating metadata if safepos is behind writepos and
5756 * readpos is beyond writepos.
5757 * In any case, update the metadata every 10 seconds.
5758 * Maybe that number should be configurable, but I'm not sure it is
5759 * worth it.... maybe it could be a multiple of safemode_delay???
5760 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005761 if (conf->min_offset_diff < 0) {
5762 safepos += -conf->min_offset_diff;
5763 readpos += -conf->min_offset_diff;
5764 } else
5765 writepos += conf->min_offset_diff;
5766
NeilBrown2c810cd2012-05-21 09:27:00 +10005767 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11005768 ? (safepos > writepos && readpos < writepos)
5769 : (safepos < writepos && readpos > writepos)) ||
5770 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07005771 /* Cannot proceed until we've updated the superblock... */
5772 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005773 atomic_read(&conf->reshape_stripes)==0
5774 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5775 if (atomic_read(&conf->reshape_stripes) != 0)
5776 return 0;
NeilBrownfef9c612009-03-31 15:16:46 +11005777 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005778 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005779 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08005780 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown52c03292006-06-26 00:27:43 -07005781 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08005782 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11005783 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5784 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5785 return 0;
NeilBrown52c03292006-06-26 00:27:43 -07005786 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005787 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07005788 spin_unlock_irq(&conf->device_lock);
5789 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005790 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07005791 }
5792
NeilBrownab69ae12009-03-31 15:26:47 +11005793 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11005794 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07005795 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10005796 int skipped_disk = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07005797 sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005798 set_bit(STRIPE_EXPANDING, &sh->state);
5799 atomic_inc(&conf->reshape_stripes);
5800 /* If any of this stripe is beyond the end of the old
5801 * array, then we need to zero those blocks
5802 */
5803 for (j=sh->disks; j--;) {
5804 sector_t s;
5805 if (j == sh->pd_idx)
5806 continue;
NeilBrownf4168852007-02-28 20:11:53 -08005807 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11005808 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08005809 continue;
Shaohua Li6d036f72015-08-13 14:31:57 -07005810 s = raid5_compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11005811 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10005812 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07005813 continue;
5814 }
5815 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
5816 set_bit(R5_Expanded, &sh->dev[j].flags);
5817 set_bit(R5_UPTODATE, &sh->dev[j].flags);
5818 }
NeilBrowna9f326e2009-09-23 18:06:41 +10005819 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07005820 set_bit(STRIPE_EXPAND_READY, &sh->state);
5821 set_bit(STRIPE_HANDLE, &sh->state);
5822 }
NeilBrownab69ae12009-03-31 15:26:47 +11005823 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07005824 }
5825 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005826 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11005827 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005828 else
NeilBrown7a661382009-03-31 15:21:40 +11005829 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07005830 spin_unlock_irq(&conf->device_lock);
5831 /* Ok, those stripe are ready. We can start scheduling
5832 * reads on the source stripes.
5833 * The source stripes are determined by mapping the first and last
5834 * block on the destination stripes.
5835 */
NeilBrown52c03292006-06-26 00:27:43 -07005836 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11005837 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11005838 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07005839 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10005840 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10005841 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11005842 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11005843 if (last_sector >= mddev->dev_sectors)
5844 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07005845 while (first_sector <= last_sector) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005846 sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005847 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
5848 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07005849 raid5_release_stripe(sh);
NeilBrown52c03292006-06-26 00:27:43 -07005850 first_sector += STRIPE_SECTORS;
5851 }
NeilBrownab69ae12009-03-31 15:26:47 +11005852 /* Now that the sources are clearly marked, we can release
5853 * the destination stripes
5854 */
5855 while (!list_empty(&stripes)) {
5856 sh = list_entry(stripes.next, struct stripe_head, lru);
5857 list_del_init(&sh->lru);
Shaohua Li6d036f72015-08-13 14:31:57 -07005858 raid5_release_stripe(sh);
NeilBrownab69ae12009-03-31 15:26:47 +11005859 }
NeilBrownc6207272008-02-06 01:39:52 -08005860 /* If this takes us to the resync_max point where we have to pause,
5861 * then we need to write out the superblock.
5862 */
NeilBrown7a661382009-03-31 15:21:40 +11005863 sector_nr += reshape_sectors;
NeilBrown92140482015-07-06 12:28:45 +10005864 retn = reshape_sectors;
5865finish:
NeilBrownc5e19d92015-07-17 12:06:02 +10005866 if (mddev->curr_resync_completed > mddev->resync_max ||
5867 (sector_nr - mddev->curr_resync_completed) * 2
NeilBrownc03f6a12009-04-17 11:06:30 +10005868 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08005869 /* Cannot proceed until we've updated the superblock... */
5870 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005871 atomic_read(&conf->reshape_stripes) == 0
5872 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5873 if (atomic_read(&conf->reshape_stripes) != 0)
5874 goto ret;
NeilBrownfef9c612009-03-31 15:16:46 +11005875 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005876 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005877 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08005878 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownc6207272008-02-06 01:39:52 -08005879 md_wakeup_thread(mddev->thread);
5880 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005881 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)
NeilBrownc91abf52013-11-19 12:02:01 +11005882 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5883 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5884 goto ret;
NeilBrownc6207272008-02-06 01:39:52 -08005885 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005886 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08005887 spin_unlock_irq(&conf->device_lock);
5888 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005889 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08005890 }
NeilBrownc91abf52013-11-19 12:02:01 +11005891ret:
NeilBrown92140482015-07-06 12:28:45 +10005892 return retn;
NeilBrown52c03292006-06-26 00:27:43 -07005893}
5894
Shaohua Li849674e2016-01-20 13:52:20 -08005895static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
5896 int *skipped)
NeilBrown52c03292006-06-26 00:27:43 -07005897{
NeilBrownd1688a62011-10-11 16:49:52 +11005898 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07005899 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11005900 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11005901 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07005902 int still_degraded = 0;
5903 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904
NeilBrown72626682005-09-09 16:23:54 -07005905 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11005907
NeilBrown29269552006-03-27 01:18:10 -08005908 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
5909 end_reshape(conf);
5910 return 0;
5911 }
NeilBrown72626682005-09-09 16:23:54 -07005912
5913 if (mddev->curr_resync < max_sector) /* aborted */
5914 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
5915 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07005916 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07005917 conf->fullsync = 0;
5918 bitmap_close_sync(mddev->bitmap);
5919
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 return 0;
5921 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08005922
NeilBrown64bd6602009-08-03 10:59:58 +10005923 /* Allow raid5_quiesce to complete */
5924 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
5925
NeilBrown52c03292006-06-26 00:27:43 -07005926 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5927 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08005928
NeilBrownc6207272008-02-06 01:39:52 -08005929 /* No need to check resync_max as we never do more than one
5930 * stripe, and as resync_max will always be on a chunk boundary,
5931 * if the check in md_do_sync didn't fire, there is no chance
5932 * of overstepping resync_max here
5933 */
5934
NeilBrown16a53ec2006-06-26 00:27:38 -07005935 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 * to resync, then assert that we are finished, because there is
5937 * nothing we can do.
5938 */
NeilBrown3285edf2006-06-26 00:27:55 -07005939 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005940 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005941 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07005942 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 return rv;
5944 }
majianpeng6f608042013-04-24 11:42:41 +10005945 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
5946 !conf->fullsync &&
5947 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
5948 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07005949 /* we can skip this block, and probably more */
5950 sync_blocks /= STRIPE_SECTORS;
5951 *skipped = 1;
5952 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
5953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10005955 bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
NeilBrownb47490c2008-02-06 01:39:50 -08005956
Shaohua Li6d036f72015-08-13 14:31:57 -07005957 sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 if (sh == NULL) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005959 sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07005961 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08005963 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005965 /* Need to check if array will still be degraded after recovery/resync
Eric Mei16d9cfa2015-01-06 09:35:02 -08005966 * Note in case of > 1 drive failures it's possible we're rebuilding
5967 * one drive while leaving another faulty drive in array.
NeilBrown16a53ec2006-06-26 00:27:38 -07005968 */
Eric Mei16d9cfa2015-01-06 09:35:02 -08005969 rcu_read_lock();
5970 for (i = 0; i < conf->raid_disks; i++) {
5971 struct md_rdev *rdev = ACCESS_ONCE(conf->disks[i].rdev);
5972
5973 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
NeilBrown16a53ec2006-06-26 00:27:38 -07005974 still_degraded = 1;
Eric Mei16d9cfa2015-01-06 09:35:02 -08005975 }
5976 rcu_read_unlock();
NeilBrown16a53ec2006-06-26 00:27:38 -07005977
5978 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
5979
NeilBrown83206d62011-07-26 11:19:49 +10005980 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Eivind Sarto053f5b62014-06-09 17:06:19 -07005981 set_bit(STRIPE_HANDLE, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
Shaohua Li6d036f72015-08-13 14:31:57 -07005983 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
5985 return STRIPE_SECTORS;
5986}
5987
NeilBrownd1688a62011-10-11 16:49:52 +11005988static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005989{
5990 /* We may not be able to submit a whole bio at once as there
5991 * may not be enough stripe_heads available.
5992 * We cannot pre-allocate enough stripe_heads as we may need
5993 * more than exist in the cache (if we allow ever large chunks).
5994 * So we do one stripe head at a time and record in
5995 * ->bi_hw_segments how many have been done.
5996 *
5997 * We *know* that this entire raid_bio is in one chunk, so
5998 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5999 */
6000 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11006001 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006002 sector_t sector, logical_sector, last_sector;
6003 int scnt = 0;
6004 int remaining;
6005 int handled = 0;
6006
Kent Overstreet4f024f32013-10-11 15:44:27 -07006007 logical_sector = raid_bio->bi_iter.bi_sector &
6008 ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11006009 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11006010 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07006011 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006012
6013 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08006014 logical_sector += STRIPE_SECTORS,
6015 sector += STRIPE_SECTORS,
6016 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006017
Shaohua Lie7836bd62012-07-19 16:01:31 +10006018 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006019 /* already done this stripe */
6020 continue;
6021
Shaohua Li6d036f72015-08-13 14:31:57 -07006022 sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006023
6024 if (!sh) {
6025 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10006026 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006027 conf->retry_read_aligned = raid_bio;
6028 return handled;
6029 }
6030
shli@kernel.orgda41ba62014-12-15 12:57:03 +11006031 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07006032 raid5_release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10006033 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08006034 conf->retry_read_aligned = raid_bio;
6035 return handled;
6036 }
6037
majianpeng3f9e7c12012-07-31 10:04:21 +10006038 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07006039 handle_stripe(sh);
Shaohua Li6d036f72015-08-13 14:31:57 -07006040 raid5_release_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006041 handled++;
6042 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10006043 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07006044 if (remaining == 0) {
6045 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
6046 raid_bio, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006047 bio_endio(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07006048 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006049 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10006050 wake_up(&conf->wait_for_quiescent);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006051 return handled;
6052}
6053
Shaohua Libfc90cb2013-08-29 15:40:32 +08006054static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11006055 struct r5worker *worker,
6056 struct list_head *temp_inactive_list)
Shaohua Li46a06402012-08-02 08:33:15 +10006057{
6058 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11006059 int i, batch_size = 0, hash;
6060 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10006061
6062 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08006063 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10006064 batch[batch_size++] = sh;
6065
Shaohua Li566c09c2013-11-14 15:16:17 +11006066 if (batch_size == 0) {
6067 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6068 if (!list_empty(temp_inactive_list + i))
6069 break;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006070 if (i == NR_STRIPE_HASH_LOCKS) {
6071 spin_unlock_irq(&conf->device_lock);
6072 r5l_flush_stripe_to_raid(conf->log);
6073 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006074 return batch_size;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006075 }
Shaohua Li566c09c2013-11-14 15:16:17 +11006076 release_inactive = true;
6077 }
Shaohua Li46a06402012-08-02 08:33:15 +10006078 spin_unlock_irq(&conf->device_lock);
6079
Shaohua Li566c09c2013-11-14 15:16:17 +11006080 release_inactive_stripe_list(conf, temp_inactive_list,
6081 NR_STRIPE_HASH_LOCKS);
6082
Shaohua Lia8c34f92015-09-02 13:49:46 -07006083 r5l_flush_stripe_to_raid(conf->log);
Shaohua Li566c09c2013-11-14 15:16:17 +11006084 if (release_inactive) {
6085 spin_lock_irq(&conf->device_lock);
6086 return 0;
6087 }
6088
Shaohua Li46a06402012-08-02 08:33:15 +10006089 for (i = 0; i < batch_size; i++)
6090 handle_stripe(batch[i]);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07006091 r5l_write_stripe_run(conf->log);
Shaohua Li46a06402012-08-02 08:33:15 +10006092
6093 cond_resched();
6094
6095 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006096 for (i = 0; i < batch_size; i++) {
6097 hash = batch[i]->hash_lock_index;
6098 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
6099 }
Shaohua Li46a06402012-08-02 08:33:15 +10006100 return batch_size;
6101}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006102
Shaohua Li851c30c2013-08-28 14:30:16 +08006103static void raid5_do_work(struct work_struct *work)
6104{
6105 struct r5worker *worker = container_of(work, struct r5worker, work);
6106 struct r5worker_group *group = worker->group;
6107 struct r5conf *conf = group->conf;
6108 int group_id = group - conf->worker_groups;
6109 int handled;
6110 struct blk_plug plug;
6111
6112 pr_debug("+++ raid5worker active\n");
6113
6114 blk_start_plug(&plug);
6115 handled = 0;
6116 spin_lock_irq(&conf->device_lock);
6117 while (1) {
6118 int batch_size, released;
6119
Shaohua Li566c09c2013-11-14 15:16:17 +11006120 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006121
Shaohua Li566c09c2013-11-14 15:16:17 +11006122 batch_size = handle_active_stripes(conf, group_id, worker,
6123 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08006124 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08006125 if (!batch_size && !released)
6126 break;
6127 handled += batch_size;
6128 }
6129 pr_debug("%d stripes handled\n", handled);
6130
6131 spin_unlock_irq(&conf->device_lock);
6132 blk_finish_plug(&plug);
6133
6134 pr_debug("--- raid5worker inactive\n");
6135}
6136
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137/*
6138 * This is our raid5 kernel thread.
6139 *
6140 * We scan the hash table for stripes which can be handled now.
6141 * During the scan, completed stripes are saved for us by the interrupt
6142 * handler, so that they will not have to wait for our next wakeup.
6143 */
Shaohua Li4ed87312012-10-11 13:34:00 +11006144static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145{
Shaohua Li4ed87312012-10-11 13:34:00 +11006146 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11006147 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006149 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150
Dan Williams45b42332007-07-09 11:56:43 -07006151 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152
6153 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154
NeilBrownc3cce6c2015-08-14 12:47:33 +10006155 if (!bio_list_empty(&conf->return_bi) &&
Shaohua Li29530792016-12-08 15:48:19 -08006156 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrownc3cce6c2015-08-14 12:47:33 +10006157 struct bio_list tmp = BIO_EMPTY_LIST;
6158 spin_lock_irq(&conf->device_lock);
Shaohua Li29530792016-12-08 15:48:19 -08006159 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrownc3cce6c2015-08-14 12:47:33 +10006160 bio_list_merge(&tmp, &conf->return_bi);
6161 bio_list_init(&conf->return_bi);
6162 }
6163 spin_unlock_irq(&conf->device_lock);
6164 return_io(&tmp);
6165 }
6166
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006167 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 handled = 0;
6169 spin_lock_irq(&conf->device_lock);
6170 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006171 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08006172 int batch_size, released;
6173
Shaohua Li566c09c2013-11-14 15:16:17 +11006174 released = release_stripe_list(conf, conf->temp_inactive_list);
NeilBrownedbe83a2015-02-26 12:47:56 +11006175 if (released)
6176 clear_bit(R5_DID_ALLOC, &conf->cache_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177
NeilBrown0021b7b2012-07-31 09:08:14 +02006178 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10006179 !list_empty(&conf->bitmap_list)) {
6180 /* Now is a good time to flush some bitmap updates */
6181 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08006182 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07006183 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08006184 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10006185 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11006186 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07006187 }
NeilBrown0021b7b2012-07-31 09:08:14 +02006188 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07006189
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006190 while ((bio = remove_bio_from_retry(conf))) {
6191 int ok;
6192 spin_unlock_irq(&conf->device_lock);
6193 ok = retry_aligned_read(conf, bio);
6194 spin_lock_irq(&conf->device_lock);
6195 if (!ok)
6196 break;
6197 handled++;
6198 }
6199
Shaohua Li566c09c2013-11-14 15:16:17 +11006200 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
6201 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08006202 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 break;
Shaohua Li46a06402012-08-02 08:33:15 +10006204 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205
Shaohua Li29530792016-12-08 15:48:19 -08006206 if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) {
Shaohua Li46a06402012-08-02 08:33:15 +10006207 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10006208 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10006209 spin_lock_irq(&conf->device_lock);
6210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 }
Dan Williams45b42332007-07-09 11:56:43 -07006212 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213
6214 spin_unlock_irq(&conf->device_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006215 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
6216 mutex_trylock(&conf->cache_size_mutex)) {
NeilBrownedbe83a2015-02-26 12:47:56 +11006217 grow_one_stripe(conf, __GFP_NOWARN);
6218 /* Set flag even if allocation failed. This helps
6219 * slow down allocation requests when mem is short
6220 */
6221 set_bit(R5_DID_ALLOC, &conf->cache_state);
NeilBrown2d5b5692015-07-06 12:49:23 +10006222 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224
Shaohua Li765d7042017-01-04 09:33:23 -08006225 flush_deferred_bios(conf);
6226
Shaohua Li0576b1c2015-08-13 14:32:00 -07006227 r5l_flush_stripe_to_raid(conf->log);
6228
Dan Williamsc9f21aa2008-07-23 12:05:51 -07006229 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006230 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231
Dan Williams45b42332007-07-09 11:56:43 -07006232 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233}
6234
NeilBrown3f294f42005-11-08 21:39:25 -08006235static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006236raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006237{
NeilBrown7b1485b2014-12-15 12:56:59 +11006238 struct r5conf *conf;
6239 int ret = 0;
6240 spin_lock(&mddev->lock);
6241 conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006242 if (conf)
NeilBrownedbe83a2015-02-26 12:47:56 +11006243 ret = sprintf(page, "%d\n", conf->min_nr_stripes);
NeilBrown7b1485b2014-12-15 12:56:59 +11006244 spin_unlock(&mddev->lock);
6245 return ret;
NeilBrown3f294f42005-11-08 21:39:25 -08006246}
6247
NeilBrownc41d4ac2010-06-01 19:37:24 +10006248int
NeilBrownfd01b882011-10-11 16:47:53 +11006249raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10006250{
NeilBrownd1688a62011-10-11 16:49:52 +11006251 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10006252 int err;
6253
6254 if (size <= 16 || size > 32768)
6255 return -EINVAL;
NeilBrown486f0642015-02-25 12:10:35 +11006256
NeilBrownedbe83a2015-02-26 12:47:56 +11006257 conf->min_nr_stripes = size;
NeilBrown2d5b5692015-07-06 12:49:23 +10006258 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006259 while (size < conf->max_nr_stripes &&
6260 drop_one_stripe(conf))
6261 ;
NeilBrown2d5b5692015-07-06 12:49:23 +10006262 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006263
NeilBrownedbe83a2015-02-26 12:47:56 +11006264
NeilBrownc41d4ac2010-06-01 19:37:24 +10006265 err = md_allow_write(mddev);
6266 if (err)
6267 return err;
NeilBrown486f0642015-02-25 12:10:35 +11006268
NeilBrown2d5b5692015-07-06 12:49:23 +10006269 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006270 while (size > conf->max_nr_stripes)
6271 if (!grow_one_stripe(conf, GFP_KERNEL))
6272 break;
NeilBrown2d5b5692015-07-06 12:49:23 +10006273 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006274
NeilBrownc41d4ac2010-06-01 19:37:24 +10006275 return 0;
6276}
6277EXPORT_SYMBOL(raid5_set_cache_size);
6278
NeilBrown3f294f42005-11-08 21:39:25 -08006279static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006280raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08006281{
NeilBrown67918752014-12-15 12:57:01 +11006282 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006283 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07006284 int err;
6285
NeilBrown3f294f42005-11-08 21:39:25 -08006286 if (len >= PAGE_SIZE)
6287 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006288 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08006289 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006290 err = mddev_lock(mddev);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006291 if (err)
6292 return err;
NeilBrown67918752014-12-15 12:57:01 +11006293 conf = mddev->private;
6294 if (!conf)
6295 err = -ENODEV;
6296 else
6297 err = raid5_set_cache_size(mddev, new);
6298 mddev_unlock(mddev);
6299
6300 return err ?: len;
NeilBrown3f294f42005-11-08 21:39:25 -08006301}
NeilBrown007583c2005-11-08 21:39:30 -08006302
NeilBrown96de1e62005-11-08 21:39:39 -08006303static struct md_sysfs_entry
6304raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
6305 raid5_show_stripe_cache_size,
6306 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08006307
6308static ssize_t
Markus Stockhausend06f1912014-12-15 12:57:05 +11006309raid5_show_rmw_level(struct mddev *mddev, char *page)
6310{
6311 struct r5conf *conf = mddev->private;
6312 if (conf)
6313 return sprintf(page, "%d\n", conf->rmw_level);
6314 else
6315 return 0;
6316}
6317
6318static ssize_t
6319raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
6320{
6321 struct r5conf *conf = mddev->private;
6322 unsigned long new;
6323
6324 if (!conf)
6325 return -ENODEV;
6326
6327 if (len >= PAGE_SIZE)
6328 return -EINVAL;
6329
6330 if (kstrtoul(page, 10, &new))
6331 return -EINVAL;
6332
6333 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
6334 return -EINVAL;
6335
6336 if (new != PARITY_DISABLE_RMW &&
6337 new != PARITY_ENABLE_RMW &&
6338 new != PARITY_PREFER_RMW)
6339 return -EINVAL;
6340
6341 conf->rmw_level = new;
6342 return len;
6343}
6344
6345static struct md_sysfs_entry
6346raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
6347 raid5_show_rmw_level,
6348 raid5_store_rmw_level);
6349
6350
6351static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006352raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006353{
NeilBrown7b1485b2014-12-15 12:56:59 +11006354 struct r5conf *conf;
6355 int ret = 0;
6356 spin_lock(&mddev->lock);
6357 conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006358 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006359 ret = sprintf(page, "%d\n", conf->bypass_threshold);
6360 spin_unlock(&mddev->lock);
6361 return ret;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006362}
6363
6364static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006365raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006366{
NeilBrown67918752014-12-15 12:57:01 +11006367 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006368 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006369 int err;
6370
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006371 if (len >= PAGE_SIZE)
6372 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006373 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006374 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006375
6376 err = mddev_lock(mddev);
6377 if (err)
6378 return err;
6379 conf = mddev->private;
6380 if (!conf)
6381 err = -ENODEV;
NeilBrownedbe83a2015-02-26 12:47:56 +11006382 else if (new > conf->min_nr_stripes)
NeilBrown67918752014-12-15 12:57:01 +11006383 err = -EINVAL;
6384 else
6385 conf->bypass_threshold = new;
6386 mddev_unlock(mddev);
6387 return err ?: len;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006388}
6389
6390static struct md_sysfs_entry
6391raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
6392 S_IRUGO | S_IWUSR,
6393 raid5_show_preread_threshold,
6394 raid5_store_preread_threshold);
6395
6396static ssize_t
Shaohua Lid592a992014-05-21 17:57:44 +08006397raid5_show_skip_copy(struct mddev *mddev, char *page)
6398{
NeilBrown7b1485b2014-12-15 12:56:59 +11006399 struct r5conf *conf;
6400 int ret = 0;
6401 spin_lock(&mddev->lock);
6402 conf = mddev->private;
Shaohua Lid592a992014-05-21 17:57:44 +08006403 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006404 ret = sprintf(page, "%d\n", conf->skip_copy);
6405 spin_unlock(&mddev->lock);
6406 return ret;
Shaohua Lid592a992014-05-21 17:57:44 +08006407}
6408
6409static ssize_t
6410raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
6411{
NeilBrown67918752014-12-15 12:57:01 +11006412 struct r5conf *conf;
Shaohua Lid592a992014-05-21 17:57:44 +08006413 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006414 int err;
6415
Shaohua Lid592a992014-05-21 17:57:44 +08006416 if (len >= PAGE_SIZE)
6417 return -EINVAL;
Shaohua Lid592a992014-05-21 17:57:44 +08006418 if (kstrtoul(page, 10, &new))
6419 return -EINVAL;
6420 new = !!new;
Shaohua Lid592a992014-05-21 17:57:44 +08006421
NeilBrown67918752014-12-15 12:57:01 +11006422 err = mddev_lock(mddev);
6423 if (err)
6424 return err;
6425 conf = mddev->private;
6426 if (!conf)
6427 err = -ENODEV;
6428 else if (new != conf->skip_copy) {
6429 mddev_suspend(mddev);
6430 conf->skip_copy = new;
6431 if (new)
Jan Karadc3b17c2017-02-02 15:56:50 +01006432 mddev->queue->backing_dev_info->capabilities |=
NeilBrown67918752014-12-15 12:57:01 +11006433 BDI_CAP_STABLE_WRITES;
6434 else
Jan Karadc3b17c2017-02-02 15:56:50 +01006435 mddev->queue->backing_dev_info->capabilities &=
NeilBrown67918752014-12-15 12:57:01 +11006436 ~BDI_CAP_STABLE_WRITES;
6437 mddev_resume(mddev);
6438 }
6439 mddev_unlock(mddev);
6440 return err ?: len;
Shaohua Lid592a992014-05-21 17:57:44 +08006441}
6442
6443static struct md_sysfs_entry
6444raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
6445 raid5_show_skip_copy,
6446 raid5_store_skip_copy);
6447
Shaohua Lid592a992014-05-21 17:57:44 +08006448static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006449stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006450{
NeilBrownd1688a62011-10-11 16:49:52 +11006451 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006452 if (conf)
6453 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
6454 else
6455 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08006456}
6457
NeilBrown96de1e62005-11-08 21:39:39 -08006458static struct md_sysfs_entry
6459raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08006460
Shaohua Lib7214202013-08-27 17:50:42 +08006461static ssize_t
6462raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
6463{
NeilBrown7b1485b2014-12-15 12:56:59 +11006464 struct r5conf *conf;
6465 int ret = 0;
6466 spin_lock(&mddev->lock);
6467 conf = mddev->private;
Shaohua Lib7214202013-08-27 17:50:42 +08006468 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006469 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
6470 spin_unlock(&mddev->lock);
6471 return ret;
Shaohua Lib7214202013-08-27 17:50:42 +08006472}
6473
majianpeng60aaf932013-11-14 15:16:20 +11006474static int alloc_thread_groups(struct r5conf *conf, int cnt,
6475 int *group_cnt,
6476 int *worker_cnt_per_group,
6477 struct r5worker_group **worker_groups);
Shaohua Lib7214202013-08-27 17:50:42 +08006478static ssize_t
6479raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
6480{
NeilBrown67918752014-12-15 12:57:01 +11006481 struct r5conf *conf;
Shaohua Lib7214202013-08-27 17:50:42 +08006482 unsigned long new;
6483 int err;
majianpeng60aaf932013-11-14 15:16:20 +11006484 struct r5worker_group *new_groups, *old_groups;
6485 int group_cnt, worker_cnt_per_group;
Shaohua Lib7214202013-08-27 17:50:42 +08006486
6487 if (len >= PAGE_SIZE)
6488 return -EINVAL;
Shaohua Lib7214202013-08-27 17:50:42 +08006489 if (kstrtoul(page, 10, &new))
6490 return -EINVAL;
6491
NeilBrown67918752014-12-15 12:57:01 +11006492 err = mddev_lock(mddev);
Shaohua Lib7214202013-08-27 17:50:42 +08006493 if (err)
6494 return err;
NeilBrown67918752014-12-15 12:57:01 +11006495 conf = mddev->private;
6496 if (!conf)
6497 err = -ENODEV;
6498 else if (new != conf->worker_cnt_per_group) {
6499 mddev_suspend(mddev);
6500
6501 old_groups = conf->worker_groups;
6502 if (old_groups)
6503 flush_workqueue(raid5_wq);
6504
6505 err = alloc_thread_groups(conf, new,
6506 &group_cnt, &worker_cnt_per_group,
6507 &new_groups);
6508 if (!err) {
6509 spin_lock_irq(&conf->device_lock);
6510 conf->group_cnt = group_cnt;
6511 conf->worker_cnt_per_group = worker_cnt_per_group;
6512 conf->worker_groups = new_groups;
6513 spin_unlock_irq(&conf->device_lock);
6514
6515 if (old_groups)
6516 kfree(old_groups[0].workers);
6517 kfree(old_groups);
6518 }
6519 mddev_resume(mddev);
6520 }
6521 mddev_unlock(mddev);
6522
6523 return err ?: len;
Shaohua Lib7214202013-08-27 17:50:42 +08006524}
6525
6526static struct md_sysfs_entry
6527raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
6528 raid5_show_group_thread_cnt,
6529 raid5_store_group_thread_cnt);
6530
NeilBrown007583c2005-11-08 21:39:30 -08006531static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08006532 &raid5_stripecache_size.attr,
6533 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006534 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08006535 &raid5_group_thread_cnt.attr,
Shaohua Lid592a992014-05-21 17:57:44 +08006536 &raid5_skip_copy.attr,
Markus Stockhausend06f1912014-12-15 12:57:05 +11006537 &raid5_rmw_level.attr,
Song Liu2c7da142016-11-17 15:24:41 -08006538 &r5c_journal_mode.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08006539 NULL,
6540};
NeilBrown007583c2005-11-08 21:39:30 -08006541static struct attribute_group raid5_attrs_group = {
6542 .name = NULL,
6543 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08006544};
6545
majianpeng60aaf932013-11-14 15:16:20 +11006546static int alloc_thread_groups(struct r5conf *conf, int cnt,
6547 int *group_cnt,
6548 int *worker_cnt_per_group,
6549 struct r5worker_group **worker_groups)
Shaohua Li851c30c2013-08-28 14:30:16 +08006550{
Shaohua Li566c09c2013-11-14 15:16:17 +11006551 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08006552 ssize_t size;
6553 struct r5worker *workers;
6554
majianpeng60aaf932013-11-14 15:16:20 +11006555 *worker_cnt_per_group = cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +08006556 if (cnt == 0) {
majianpeng60aaf932013-11-14 15:16:20 +11006557 *group_cnt = 0;
6558 *worker_groups = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08006559 return 0;
6560 }
majianpeng60aaf932013-11-14 15:16:20 +11006561 *group_cnt = num_possible_nodes();
Shaohua Li851c30c2013-08-28 14:30:16 +08006562 size = sizeof(struct r5worker) * cnt;
majianpeng60aaf932013-11-14 15:16:20 +11006563 workers = kzalloc(size * *group_cnt, GFP_NOIO);
6564 *worker_groups = kzalloc(sizeof(struct r5worker_group) *
6565 *group_cnt, GFP_NOIO);
6566 if (!*worker_groups || !workers) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006567 kfree(workers);
majianpeng60aaf932013-11-14 15:16:20 +11006568 kfree(*worker_groups);
Shaohua Li851c30c2013-08-28 14:30:16 +08006569 return -ENOMEM;
6570 }
6571
majianpeng60aaf932013-11-14 15:16:20 +11006572 for (i = 0; i < *group_cnt; i++) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006573 struct r5worker_group *group;
6574
NeilBrown0c775d52013-11-25 11:12:43 +11006575 group = &(*worker_groups)[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08006576 INIT_LIST_HEAD(&group->handle_list);
Shaohua Li535ae4e2017-02-15 19:37:32 -08006577 INIT_LIST_HEAD(&group->loprio_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006578 group->conf = conf;
6579 group->workers = workers + i * cnt;
6580
6581 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11006582 struct r5worker *worker = group->workers + j;
6583 worker->group = group;
6584 INIT_WORK(&worker->work, raid5_do_work);
6585
6586 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
6587 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08006588 }
6589 }
6590
6591 return 0;
6592}
6593
6594static void free_thread_groups(struct r5conf *conf)
6595{
6596 if (conf->worker_groups)
6597 kfree(conf->worker_groups[0].workers);
6598 kfree(conf->worker_groups);
6599 conf->worker_groups = NULL;
6600}
6601
Dan Williams80c3a6c2009-03-17 18:10:40 -07006602static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11006603raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07006604{
NeilBrownd1688a62011-10-11 16:49:52 +11006605 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006606
6607 if (!sectors)
6608 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006609 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11006610 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11006611 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006612
NeilBrown3cb5edf2015-07-15 17:24:17 +10006613 sectors &= ~((sector_t)conf->chunk_sectors - 1);
6614 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006615 return sectors * (raid_disks - conf->max_degraded);
6616}
6617
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306618static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6619{
6620 safe_put_page(percpu->spare_page);
shli@kernel.org46d5b782014-12-15 12:57:02 +11006621 if (percpu->scribble)
6622 flex_array_free(percpu->scribble);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306623 percpu->spare_page = NULL;
6624 percpu->scribble = NULL;
6625}
6626
6627static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6628{
6629 if (conf->level == 6 && !percpu->spare_page)
6630 percpu->spare_page = alloc_page(GFP_KERNEL);
6631 if (!percpu->scribble)
shli@kernel.org46d5b782014-12-15 12:57:02 +11006632 percpu->scribble = scribble_alloc(max(conf->raid_disks,
NeilBrown738a2732015-05-08 18:19:39 +10006633 conf->previous_raid_disks),
6634 max(conf->chunk_sectors,
6635 conf->prev_chunk_sectors)
6636 / STRIPE_SECTORS,
6637 GFP_KERNEL);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306638
6639 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
6640 free_scratch_buffer(conf, percpu);
6641 return -ENOMEM;
6642 }
6643
6644 return 0;
6645}
6646
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006647static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
6648{
6649 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
6650
6651 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
6652 return 0;
6653}
6654
NeilBrownd1688a62011-10-11 16:49:52 +11006655static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006656{
Dan Williams36d1c642009-07-14 11:48:22 -07006657 if (!conf->percpu)
6658 return;
6659
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006660 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Dan Williams36d1c642009-07-14 11:48:22 -07006661 free_percpu(conf->percpu);
6662}
6663
NeilBrownd1688a62011-10-11 16:49:52 +11006664static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10006665{
Song Liud7bd3982016-11-23 22:50:39 -08006666 int i;
6667
Shaohua Li5c7e81c2015-08-13 14:32:04 -07006668 if (conf->log)
6669 r5l_exit_log(conf->log);
Shaohua Li30c89462016-09-21 09:07:13 -07006670 if (conf->shrinker.nr_deferred)
NeilBrownedbe83a2015-02-26 12:47:56 +11006671 unregister_shrinker(&conf->shrinker);
Shaohua Li5c7e81c2015-08-13 14:32:04 -07006672
Shaohua Li851c30c2013-08-28 14:30:16 +08006673 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10006674 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07006675 raid5_free_percpu(conf);
Song Liud7bd3982016-11-23 22:50:39 -08006676 for (i = 0; i < conf->pool_size; i++)
6677 if (conf->disks[i].extra_page)
6678 put_page(conf->disks[i].extra_page);
Dan Williams95fc17a2009-07-31 12:39:15 +10006679 kfree(conf->disks);
6680 kfree(conf->stripe_hashtbl);
6681 kfree(conf);
6682}
6683
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006684static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
Dan Williams36d1c642009-07-14 11:48:22 -07006685{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006686 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
Dan Williams36d1c642009-07-14 11:48:22 -07006687 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
6688
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006689 if (alloc_scratch_buffer(conf, percpu)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006690 pr_warn("%s: failed memory allocation for cpu%u\n",
6691 __func__, cpu);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006692 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006693 }
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006694 return 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006695}
Dan Williams36d1c642009-07-14 11:48:22 -07006696
NeilBrownd1688a62011-10-11 16:49:52 +11006697static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006698{
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306699 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006700
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306701 conf->percpu = alloc_percpu(struct raid5_percpu);
6702 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07006703 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006704
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006705 err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Shaohua Li27a353c2016-02-24 17:38:28 -08006706 if (!err) {
6707 conf->scribble_disks = max(conf->raid_disks,
6708 conf->previous_raid_disks);
6709 conf->scribble_sectors = max(conf->chunk_sectors,
6710 conf->prev_chunk_sectors);
6711 }
Dan Williams36d1c642009-07-14 11:48:22 -07006712 return err;
6713}
6714
NeilBrownedbe83a2015-02-26 12:47:56 +11006715static unsigned long raid5_cache_scan(struct shrinker *shrink,
6716 struct shrink_control *sc)
6717{
6718 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
NeilBrown2d5b5692015-07-06 12:49:23 +10006719 unsigned long ret = SHRINK_STOP;
6720
6721 if (mutex_trylock(&conf->cache_size_mutex)) {
6722 ret= 0;
NeilBrown49895bc2015-08-03 17:09:57 +10006723 while (ret < sc->nr_to_scan &&
6724 conf->max_nr_stripes > conf->min_nr_stripes) {
NeilBrown2d5b5692015-07-06 12:49:23 +10006725 if (drop_one_stripe(conf) == 0) {
6726 ret = SHRINK_STOP;
6727 break;
6728 }
6729 ret++;
6730 }
6731 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006732 }
6733 return ret;
6734}
6735
6736static unsigned long raid5_cache_count(struct shrinker *shrink,
6737 struct shrink_control *sc)
6738{
6739 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
6740
6741 if (conf->max_nr_stripes < conf->min_nr_stripes)
6742 /* unlikely, but not impossible */
6743 return 0;
6744 return conf->max_nr_stripes - conf->min_nr_stripes;
6745}
6746
NeilBrownd1688a62011-10-11 16:49:52 +11006747static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748{
NeilBrownd1688a62011-10-11 16:49:52 +11006749 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006750 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11006751 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10006753 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11006754 int i;
majianpeng60aaf932013-11-14 15:16:20 +11006755 int group_cnt, worker_cnt_per_group;
6756 struct r5worker_group *new_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757
NeilBrown91adb562009-03-31 14:39:39 +11006758 if (mddev->new_level != 5
6759 && mddev->new_level != 4
6760 && mddev->new_level != 6) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006761 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
6762 mdname(mddev), mddev->new_level);
NeilBrown91adb562009-03-31 14:39:39 +11006763 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 }
NeilBrown91adb562009-03-31 14:39:39 +11006765 if ((mddev->new_level == 5
6766 && !algorithm_valid_raid5(mddev->new_layout)) ||
6767 (mddev->new_level == 6
6768 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006769 pr_warn("md/raid:%s: layout %d not supported\n",
6770 mdname(mddev), mddev->new_layout);
NeilBrown91adb562009-03-31 14:39:39 +11006771 return ERR_PTR(-EIO);
6772 }
6773 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006774 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
6775 mdname(mddev), mddev->raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006776 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11006777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778
Andre Noll664e7c42009-06-18 08:45:27 +10006779 if (!mddev->new_chunk_sectors ||
6780 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
6781 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006782 pr_warn("md/raid:%s: invalid chunk size %d\n",
6783 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11006784 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11006785 }
6786
NeilBrownd1688a62011-10-11 16:49:52 +11006787 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11006788 if (conf == NULL)
6789 goto abort;
Shaohua Li851c30c2013-08-28 14:30:16 +08006790 /* Don't enable multi-threading by default*/
majianpeng60aaf932013-11-14 15:16:20 +11006791 if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group,
6792 &new_group)) {
6793 conf->group_cnt = group_cnt;
6794 conf->worker_cnt_per_group = worker_cnt_per_group;
6795 conf->worker_groups = new_group;
6796 } else
Shaohua Li851c30c2013-08-28 14:30:16 +08006797 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11006798 spin_lock_init(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10006799 seqcount_init(&conf->gen_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006800 mutex_init(&conf->cache_size_mutex);
Yuanhan Liub1b46482015-05-08 18:19:06 +10006801 init_waitqueue_head(&conf->wait_for_quiescent);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08006802 init_waitqueue_head(&conf->wait_for_stripe);
Dan Williamsf5efd452009-10-16 15:55:38 +11006803 init_waitqueue_head(&conf->wait_for_overlap);
6804 INIT_LIST_HEAD(&conf->handle_list);
Shaohua Li535ae4e2017-02-15 19:37:32 -08006805 INIT_LIST_HEAD(&conf->loprio_list);
Dan Williamsf5efd452009-10-16 15:55:38 +11006806 INIT_LIST_HEAD(&conf->hold_list);
6807 INIT_LIST_HEAD(&conf->delayed_list);
6808 INIT_LIST_HEAD(&conf->bitmap_list);
NeilBrownc3cce6c2015-08-14 12:47:33 +10006809 bio_list_init(&conf->return_bi);
Shaohua Li773ca822013-08-27 17:50:39 +08006810 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11006811 atomic_set(&conf->active_stripes, 0);
6812 atomic_set(&conf->preread_active_stripes, 0);
6813 atomic_set(&conf->active_aligned_reads, 0);
Shaohua Li765d7042017-01-04 09:33:23 -08006814 bio_list_init(&conf->pending_bios);
6815 spin_lock_init(&conf->pending_bios_lock);
6816 conf->batch_bio_dispatch = true;
6817 rdev_for_each(rdev, mddev) {
6818 if (test_bit(Journal, &rdev->flags))
6819 continue;
6820 if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
6821 conf->batch_bio_dispatch = false;
6822 break;
6823 }
6824 }
6825
Dan Williamsf5efd452009-10-16 15:55:38 +11006826 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11006827 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11006828
6829 conf->raid_disks = mddev->raid_disks;
6830 if (mddev->reshape_position == MaxSector)
6831 conf->previous_raid_disks = mddev->raid_disks;
6832 else
6833 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006834 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006835
NeilBrown5e5e3e72009-10-16 16:35:30 +11006836 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11006837 GFP_KERNEL);
Song Liud7bd3982016-11-23 22:50:39 -08006838
NeilBrown91adb562009-03-31 14:39:39 +11006839 if (!conf->disks)
6840 goto abort;
6841
Song Liud7bd3982016-11-23 22:50:39 -08006842 for (i = 0; i < max_disks; i++) {
6843 conf->disks[i].extra_page = alloc_page(GFP_KERNEL);
6844 if (!conf->disks[i].extra_page)
6845 goto abort;
6846 }
6847
NeilBrown91adb562009-03-31 14:39:39 +11006848 conf->mddev = mddev;
6849
6850 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
6851 goto abort;
6852
Shaohua Li566c09c2013-11-14 15:16:17 +11006853 /* We init hash_locks[0] separately to that it can be used
6854 * as the reference lock in the spin_lock_nest_lock() call
6855 * in lock_all_device_hash_locks_irq in order to convince
6856 * lockdep that we know what we are doing.
6857 */
6858 spin_lock_init(conf->hash_locks);
6859 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
6860 spin_lock_init(conf->hash_locks + i);
6861
6862 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6863 INIT_LIST_HEAD(conf->inactive_list + i);
6864
6865 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6866 INIT_LIST_HEAD(conf->temp_inactive_list + i);
6867
Song Liu1e6d6902016-11-17 15:24:39 -08006868 atomic_set(&conf->r5c_cached_full_stripes, 0);
6869 INIT_LIST_HEAD(&conf->r5c_full_stripe_list);
6870 atomic_set(&conf->r5c_cached_partial_stripes, 0);
6871 INIT_LIST_HEAD(&conf->r5c_partial_stripe_list);
Shaohua Lie33fbb92017-02-10 16:18:09 -08006872 atomic_set(&conf->r5c_flushing_full_stripes, 0);
6873 atomic_set(&conf->r5c_flushing_partial_stripes, 0);
Song Liu1e6d6902016-11-17 15:24:39 -08006874
Dan Williams36d1c642009-07-14 11:48:22 -07006875 conf->level = mddev->new_level;
shli@kernel.org46d5b782014-12-15 12:57:02 +11006876 conf->chunk_sectors = mddev->new_chunk_sectors;
Dan Williams36d1c642009-07-14 11:48:22 -07006877 if (raid5_alloc_percpu(conf) != 0)
6878 goto abort;
6879
NeilBrown0c55e022010-05-03 14:09:02 +10006880 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11006881
NeilBrowndafb20f2012-03-19 12:46:39 +11006882 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11006883 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006884 if (raid_disk >= max_disks
Shaohua Lif2076e72015-10-08 21:54:12 -07006885 || raid_disk < 0 || test_bit(Journal, &rdev->flags))
NeilBrown91adb562009-03-31 14:39:39 +11006886 continue;
6887 disk = conf->disks + raid_disk;
6888
NeilBrown17045f52011-12-23 10:17:53 +11006889 if (test_bit(Replacement, &rdev->flags)) {
6890 if (disk->replacement)
6891 goto abort;
6892 disk->replacement = rdev;
6893 } else {
6894 if (disk->rdev)
6895 goto abort;
6896 disk->rdev = rdev;
6897 }
NeilBrown91adb562009-03-31 14:39:39 +11006898
6899 if (test_bit(In_sync, &rdev->flags)) {
6900 char b[BDEVNAME_SIZE];
NeilBrowncc6167b2016-11-02 14:16:50 +11006901 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
6902 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05006903 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11006904 /* Cannot rely on bitmap to complete recovery */
6905 conf->fullsync = 1;
6906 }
6907
NeilBrown91adb562009-03-31 14:39:39 +11006908 conf->level = mddev->new_level;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006909 if (conf->level == 6) {
NeilBrown91adb562009-03-31 14:39:39 +11006910 conf->max_degraded = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006911 if (raid6_call.xor_syndrome)
6912 conf->rmw_level = PARITY_ENABLE_RMW;
6913 else
6914 conf->rmw_level = PARITY_DISABLE_RMW;
6915 } else {
NeilBrown91adb562009-03-31 14:39:39 +11006916 conf->max_degraded = 1;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006917 conf->rmw_level = PARITY_ENABLE_RMW;
6918 }
NeilBrown91adb562009-03-31 14:39:39 +11006919 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11006920 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11006921 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10006922 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11006923 conf->prev_algo = mddev->layout;
NeilBrown5cac6bc2015-07-17 12:17:50 +10006924 } else {
6925 conf->prev_chunk_sectors = conf->chunk_sectors;
6926 conf->prev_algo = conf->algorithm;
NeilBrowne183eae2009-03-31 15:20:22 +11006927 }
NeilBrown91adb562009-03-31 14:39:39 +11006928
NeilBrownedbe83a2015-02-26 12:47:56 +11006929 conf->min_nr_stripes = NR_STRIPES;
Shaohua Liad5b0f72016-08-30 10:29:33 -07006930 if (mddev->reshape_position != MaxSector) {
6931 int stripes = max_t(int,
6932 ((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4,
6933 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4);
6934 conf->min_nr_stripes = max(NR_STRIPES, stripes);
6935 if (conf->min_nr_stripes != NR_STRIPES)
NeilBrowncc6167b2016-11-02 14:16:50 +11006936 pr_info("md/raid:%s: force stripe size %d for reshape\n",
Shaohua Liad5b0f72016-08-30 10:29:33 -07006937 mdname(mddev), conf->min_nr_stripes);
6938 }
NeilBrownedbe83a2015-02-26 12:47:56 +11006939 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11006940 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li4bda5562013-11-14 15:16:17 +11006941 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
NeilBrownedbe83a2015-02-26 12:47:56 +11006942 if (grow_stripes(conf, conf->min_nr_stripes)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006943 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
6944 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11006945 goto abort;
6946 } else
NeilBrowncc6167b2016-11-02 14:16:50 +11006947 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory);
NeilBrownedbe83a2015-02-26 12:47:56 +11006948 /*
6949 * Losing a stripe head costs more than the time to refill it,
6950 * it reduces the queue depth and so can hurt throughput.
6951 * So set it rather large, scaled by number of devices.
6952 */
6953 conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
6954 conf->shrinker.scan_objects = raid5_cache_scan;
6955 conf->shrinker.count_objects = raid5_cache_count;
6956 conf->shrinker.batch = 128;
6957 conf->shrinker.flags = 0;
Chao Yu6a0f53f2016-09-20 10:33:57 +08006958 if (register_shrinker(&conf->shrinker)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006959 pr_warn("md/raid:%s: couldn't register shrinker.\n",
6960 mdname(mddev));
Chao Yu6a0f53f2016-09-20 10:33:57 +08006961 goto abort;
6962 }
NeilBrown91adb562009-03-31 14:39:39 +11006963
NeilBrown02326052012-07-03 15:56:52 +10006964 sprintf(pers_name, "raid%d", mddev->new_level);
6965 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11006966 if (!conf->thread) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006967 pr_warn("md/raid:%s: couldn't allocate thread.\n",
6968 mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11006969 goto abort;
6970 }
6971
6972 return conf;
6973
6974 abort:
6975 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10006976 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11006977 return ERR_PTR(-EIO);
6978 } else
6979 return ERR_PTR(-ENOMEM);
6980}
6981
NeilBrownc148ffd2009-11-13 17:47:00 +11006982static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
6983{
6984 switch (algo) {
6985 case ALGORITHM_PARITY_0:
6986 if (raid_disk < max_degraded)
6987 return 1;
6988 break;
6989 case ALGORITHM_PARITY_N:
6990 if (raid_disk >= raid_disks - max_degraded)
6991 return 1;
6992 break;
6993 case ALGORITHM_PARITY_0_6:
NeilBrownf72ffdd2014-09-30 14:23:59 +10006994 if (raid_disk == 0 ||
NeilBrownc148ffd2009-11-13 17:47:00 +11006995 raid_disk == raid_disks - 1)
6996 return 1;
6997 break;
6998 case ALGORITHM_LEFT_ASYMMETRIC_6:
6999 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7000 case ALGORITHM_LEFT_SYMMETRIC_6:
7001 case ALGORITHM_RIGHT_SYMMETRIC_6:
7002 if (raid_disk == raid_disks - 1)
7003 return 1;
7004 }
7005 return 0;
7006}
7007
Shaohua Li849674e2016-01-20 13:52:20 -08007008static int raid5_run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11007009{
NeilBrownd1688a62011-10-11 16:49:52 +11007010 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10007011 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11007012 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007013 struct md_rdev *rdev;
Shaohua Li713cf5a2015-08-13 14:32:03 -07007014 struct md_rdev *journal_dev = NULL;
NeilBrownc148ffd2009-11-13 17:47:00 +11007015 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11007016 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10007017 long long min_offset_diff = 0;
7018 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11007019
Andre Noll8c6ac8682009-06-18 08:48:06 +10007020 if (mddev->recovery_cp != MaxSector)
NeilBrowncc6167b2016-11-02 14:16:50 +11007021 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
7022 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10007023
7024 rdev_for_each(rdev, mddev) {
7025 long long diff;
Shaohua Li713cf5a2015-08-13 14:32:03 -07007026
Shaohua Lif2076e72015-10-08 21:54:12 -07007027 if (test_bit(Journal, &rdev->flags)) {
Shaohua Li713cf5a2015-08-13 14:32:03 -07007028 journal_dev = rdev;
Shaohua Lif2076e72015-10-08 21:54:12 -07007029 continue;
7030 }
NeilBrownb5254dd2012-05-21 09:27:01 +10007031 if (rdev->raid_disk < 0)
7032 continue;
7033 diff = (rdev->new_data_offset - rdev->data_offset);
7034 if (first) {
7035 min_offset_diff = diff;
7036 first = 0;
7037 } else if (mddev->reshape_backwards &&
7038 diff < min_offset_diff)
7039 min_offset_diff = diff;
7040 else if (!mddev->reshape_backwards &&
7041 diff > min_offset_diff)
7042 min_offset_diff = diff;
7043 }
7044
NeilBrownf6705572006-03-27 01:18:11 -08007045 if (mddev->reshape_position != MaxSector) {
7046 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10007047 * Difficulties arise if the stripe we would write to
7048 * next is at or after the stripe we would read from next.
7049 * For a reshape that changes the number of devices, this
7050 * is only possible for a very short time, and mdadm makes
7051 * sure that time appears to have past before assembling
7052 * the array. So we fail if that time hasn't passed.
7053 * For a reshape that keeps the number of devices the same
7054 * mdadm must be monitoring the reshape can keeping the
7055 * critical areas read-only and backed up. It will start
7056 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08007057 */
7058 sector_t here_new, here_old;
7059 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11007060 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrown05256d92015-07-15 17:36:21 +10007061 int chunk_sectors;
7062 int new_data_disks;
NeilBrownf6705572006-03-27 01:18:11 -08007063
Shaohua Li713cf5a2015-08-13 14:32:03 -07007064 if (journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007065 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
7066 mdname(mddev));
Shaohua Li713cf5a2015-08-13 14:32:03 -07007067 return -EINVAL;
7068 }
7069
NeilBrown88ce4932009-03-31 15:24:23 +11007070 if (mddev->new_level != mddev->level) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007071 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
7072 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007073 return -EINVAL;
7074 }
NeilBrownf6705572006-03-27 01:18:11 -08007075 old_disks = mddev->raid_disks - mddev->delta_disks;
7076 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08007077 * further up in new geometry must map after here in old
7078 * geometry.
NeilBrown05256d92015-07-15 17:36:21 +10007079 * If the chunk sizes are different, then as we perform reshape
7080 * in units of the largest of the two, reshape_position needs
7081 * be a multiple of the largest chunk size times new data disks.
NeilBrownf6705572006-03-27 01:18:11 -08007082 */
7083 here_new = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007084 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
7085 new_data_disks = mddev->raid_disks - max_degraded;
7086 if (sector_div(here_new, chunk_sectors * new_data_disks)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007087 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
7088 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007089 return -EINVAL;
7090 }
NeilBrown05256d92015-07-15 17:36:21 +10007091 reshape_offset = here_new * chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08007092 /* here_new is the stripe we will write to */
7093 here_old = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007094 sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
NeilBrownf4168852007-02-28 20:11:53 -08007095 /* here_old is the first stripe that we might need to read
7096 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10007097 if (mddev->delta_disks == 0) {
7098 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10007099 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10007100 * and taking constant backups.
7101 * mdadm always starts a situation like this in
7102 * readonly mode so it can take control before
7103 * allowing any writes. So just check for that.
7104 */
NeilBrownb5254dd2012-05-21 09:27:01 +10007105 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
7106 abs(min_offset_diff) >= mddev->new_chunk_sectors)
7107 /* not really in-place - so OK */;
7108 else if (mddev->ro == 0) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007109 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
7110 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10007111 return -EINVAL;
7112 }
NeilBrown2c810cd2012-05-21 09:27:00 +10007113 } else if (mddev->reshape_backwards
NeilBrown05256d92015-07-15 17:36:21 +10007114 ? (here_new * chunk_sectors + min_offset_diff <=
7115 here_old * chunk_sectors)
7116 : (here_new * chunk_sectors >=
7117 here_old * chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08007118 /* Reading from the same stripe as writing to - bad */
NeilBrowncc6167b2016-11-02 14:16:50 +11007119 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
7120 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007121 return -EINVAL;
7122 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007123 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007124 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08007125 } else {
NeilBrown91adb562009-03-31 14:39:39 +11007126 BUG_ON(mddev->level != mddev->new_level);
7127 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10007128 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11007129 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08007130 }
7131
NeilBrown245f46c2009-03-31 14:39:39 +11007132 if (mddev->private == NULL)
7133 conf = setup_conf(mddev);
7134 else
7135 conf = mddev->private;
7136
NeilBrown91adb562009-03-31 14:39:39 +11007137 if (IS_ERR(conf))
7138 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08007139
Song Liu486b0f72016-08-19 15:34:01 -07007140 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
7141 if (!journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007142 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
7143 mdname(mddev));
Song Liu486b0f72016-08-19 15:34:01 -07007144 mddev->ro = 1;
7145 set_disk_ro(mddev->gendisk, 1);
7146 } else if (mddev->recovery_cp == MaxSector)
7147 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
Shaohua Li7dde2ad2015-10-08 21:54:10 -07007148 }
7149
NeilBrownb5254dd2012-05-21 09:27:01 +10007150 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11007151 mddev->thread = conf->thread;
7152 conf->thread = NULL;
7153 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154
NeilBrown17045f52011-12-23 10:17:53 +11007155 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
7156 i++) {
7157 rdev = conf->disks[i].rdev;
7158 if (!rdev && conf->disks[i].replacement) {
7159 /* The replacement is all we have yet */
7160 rdev = conf->disks[i].replacement;
7161 conf->disks[i].replacement = NULL;
7162 clear_bit(Replacement, &rdev->flags);
7163 conf->disks[i].rdev = rdev;
7164 }
7165 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11007166 continue;
NeilBrown17045f52011-12-23 10:17:53 +11007167 if (conf->disks[i].replacement &&
7168 conf->reshape_progress != MaxSector) {
7169 /* replacements and reshape simply do not mix. */
NeilBrowncc6167b2016-11-02 14:16:50 +11007170 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
NeilBrown17045f52011-12-23 10:17:53 +11007171 goto abort;
7172 }
NeilBrown2f115882010-06-17 17:41:03 +10007173 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11007174 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10007175 continue;
7176 }
NeilBrownc148ffd2009-11-13 17:47:00 +11007177 /* This disc is not fully in-sync. However if it
7178 * just stored parity (beyond the recovery_offset),
7179 * when we don't need to be concerned about the
7180 * array being dirty.
7181 * When reshape goes 'backwards', we never have
7182 * partially completed devices, so we only need
7183 * to worry about reshape going forwards.
7184 */
7185 /* Hack because v0.91 doesn't store recovery_offset properly. */
7186 if (mddev->major_version == 0 &&
7187 mddev->minor_version > 90)
7188 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007189
NeilBrownc148ffd2009-11-13 17:47:00 +11007190 if (rdev->recovery_offset < reshape_offset) {
7191 /* We need to check old and new layout */
7192 if (!only_parity(rdev->raid_disk,
7193 conf->algorithm,
7194 conf->raid_disks,
7195 conf->max_degraded))
7196 continue;
7197 }
7198 if (!only_parity(rdev->raid_disk,
7199 conf->prev_algo,
7200 conf->previous_raid_disks,
7201 conf->max_degraded))
7202 continue;
7203 dirty_parity_disks++;
7204 }
NeilBrown91adb562009-03-31 14:39:39 +11007205
NeilBrown17045f52011-12-23 10:17:53 +11007206 /*
7207 * 0 for a fully functional array, 1 or 2 for a degraded array.
7208 */
Song Liu2e38a372017-01-24 10:45:30 -08007209 mddev->degraded = raid5_calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210
NeilBrown674806d2010-06-16 17:17:53 +10007211 if (has_failed(conf)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007212 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07007213 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214 goto abort;
7215 }
7216
NeilBrown91adb562009-03-31 14:39:39 +11007217 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10007218 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11007219 mddev->resync_max_sectors = mddev->dev_sectors;
7220
NeilBrownc148ffd2009-11-13 17:47:00 +11007221 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007223 if (mddev->ok_start_degraded)
NeilBrowncc6167b2016-11-02 14:16:50 +11007224 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
7225 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007226 else {
NeilBrowncc6167b2016-11-02 14:16:50 +11007227 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
7228 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007229 goto abort;
7230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231 }
7232
NeilBrowncc6167b2016-11-02 14:16:50 +11007233 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
7234 mdname(mddev), conf->level,
7235 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
7236 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237
7238 print_raid5_conf(conf);
7239
NeilBrownfef9c612009-03-31 15:16:46 +11007240 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11007241 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08007242 atomic_set(&conf->reshape_stripes, 0);
7243 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7244 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7245 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7246 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7247 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007248 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08007249 }
7250
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10007252 if (mddev->to_remove == &raid5_attrs_group)
7253 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007254 else if (mddev->kobj.sd &&
7255 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrowncc6167b2016-11-02 14:16:50 +11007256 pr_warn("raid5: failed to create sysfs attributes for %s\n",
7257 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10007258 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
7259
7260 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007261 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11007262 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10007263 /* read-ahead size must cover two whole stripes, which
7264 * is 2 * (datadisks) * chunksize where 'n' is the
7265 * number of raid devices
7266 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007267 int data_disks = conf->previous_raid_disks - conf->max_degraded;
7268 int stripe = data_disks *
7269 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Jan Karadc3b17c2017-02-02 15:56:50 +01007270 if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
7271 mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10007272
NeilBrown9f7c2222010-07-26 12:04:13 +10007273 chunk_size = mddev->chunk_sectors << 9;
7274 blk_queue_io_min(mddev->queue, chunk_size);
7275 blk_queue_io_opt(mddev->queue, chunk_size *
7276 (conf->raid_disks - conf->max_degraded));
Kent Overstreetc78afc62013-07-11 22:39:53 -07007277 mddev->queue->limits.raid_partial_stripes_expensive = 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007278 /*
7279 * We can only discard a whole stripe. It doesn't make sense to
7280 * discard data disk but write parity disk
7281 */
7282 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11007283 /* Round up to power of 2, as discard handling
7284 * currently assumes that */
7285 while ((stripe-1) & stripe)
7286 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007287 mddev->queue->limits.discard_alignment = stripe;
7288 mddev->queue->limits.discard_granularity = stripe;
Konstantin Khlebnikove8d7c332016-11-27 19:32:32 +03007289
7290 /*
7291 * We use 16-bit counter of active stripes in bi_phys_segments
7292 * (minus one for over-loaded initialization)
7293 */
7294 blk_queue_max_hw_sectors(mddev->queue, 0xfffe * STRIPE_SECTORS);
7295 blk_queue_max_discard_sectors(mddev->queue,
7296 0xfffe * STRIPE_SECTORS);
7297
Shaohua Li620125f2012-10-11 13:49:05 +11007298 /*
7299 * unaligned part of discard request will be ignored, so can't
NeilBrown8e0e99b2014-10-02 13:45:00 +10007300 * guarantee discard_zeroes_data
Shaohua Li620125f2012-10-11 13:49:05 +11007301 */
7302 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10007303
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007304 blk_queue_max_write_same_sectors(mddev->queue, 0);
7305
NeilBrown05616be2012-05-21 09:27:00 +10007306 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007307 disk_stack_limits(mddev->gendisk, rdev->bdev,
7308 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10007309 disk_stack_limits(mddev->gendisk, rdev->bdev,
7310 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11007311 /*
7312 * discard_zeroes_data is required, otherwise data
7313 * could be lost. Consider a scenario: discard a stripe
7314 * (the stripe could be inconsistent if
7315 * discard_zeroes_data is 0); write one disk of the
7316 * stripe (the stripe could be inconsistent again
7317 * depending on which disks are used to calculate
7318 * parity); the disk is broken; The stripe data of this
7319 * disk is lost.
7320 */
7321 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
7322 !bdev_get_queue(rdev->bdev)->
7323 limits.discard_zeroes_data)
7324 discard_supported = false;
NeilBrown8e0e99b2014-10-02 13:45:00 +10007325 /* Unfortunately, discard_zeroes_data is not currently
7326 * a guarantee - just a hint. So we only allow DISCARD
7327 * if the sysadmin has confirmed that only safe devices
7328 * are in use by setting a module parameter.
7329 */
7330 if (!devices_handle_discard_safely) {
7331 if (discard_supported) {
7332 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
7333 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
7334 }
7335 discard_supported = false;
7336 }
NeilBrown05616be2012-05-21 09:27:00 +10007337 }
Shaohua Li620125f2012-10-11 13:49:05 +11007338
7339 if (discard_supported &&
Jes Sorensene7597e62016-02-16 16:44:24 -05007340 mddev->queue->limits.max_discard_sectors >= (stripe >> 9) &&
7341 mddev->queue->limits.discard_granularity >= stripe)
Shaohua Li620125f2012-10-11 13:49:05 +11007342 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
7343 mddev->queue);
7344 else
7345 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
7346 mddev->queue);
Shaohua Li1dffddd2016-09-08 10:49:06 -07007347
7348 blk_queue_max_hw_sectors(mddev->queue, UINT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349 }
7350
Shaohua Li5c7e81c2015-08-13 14:32:04 -07007351 if (journal_dev) {
7352 char b[BDEVNAME_SIZE];
7353
NeilBrowncc6167b2016-11-02 14:16:50 +11007354 pr_debug("md/raid:%s: using device %s as journal\n",
7355 mdname(mddev), bdevname(journal_dev->bdev, b));
Song Liu5aabf7c2016-11-17 15:24:44 -08007356 if (r5l_init_log(conf, journal_dev))
7357 goto abort;
Shaohua Li5c7e81c2015-08-13 14:32:04 -07007358 }
7359
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360 return 0;
7361abort:
NeilBrown01f96c02011-09-21 15:30:20 +10007362 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11007363 print_raid5_conf(conf);
7364 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365 mddev->private = NULL;
NeilBrowncc6167b2016-11-02 14:16:50 +11007366 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367 return -EIO;
7368}
7369
NeilBrownafa0f552014-12-15 12:56:58 +11007370static void raid5_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371{
NeilBrownafa0f552014-12-15 12:56:58 +11007372 struct r5conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373
Dan Williams95fc17a2009-07-31 12:39:15 +10007374 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10007375 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007376}
7377
Shaohua Li849674e2016-01-20 13:52:20 -08007378static void raid5_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379{
NeilBrownd1688a62011-10-11 16:49:52 +11007380 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381 int i;
7382
Andre Noll9d8f0362009-06-18 08:45:01 +10007383 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
NeilBrown3cb5edf2015-07-15 17:24:17 +10007384 conf->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07007385 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
NeilBrown5fd13352016-06-02 16:19:52 +10007386 rcu_read_lock();
7387 for (i = 0; i < conf->raid_disks; i++) {
7388 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
7389 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
7390 }
7391 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007392 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393}
7394
NeilBrownd1688a62011-10-11 16:49:52 +11007395static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007396{
7397 int i;
7398 struct disk_info *tmp;
7399
NeilBrowncc6167b2016-11-02 14:16:50 +11007400 pr_debug("RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401 if (!conf) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007402 pr_debug("(conf==NULL)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007403 return;
7404 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007405 pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level,
NeilBrown0c55e022010-05-03 14:09:02 +10007406 conf->raid_disks,
7407 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408
7409 for (i = 0; i < conf->raid_disks; i++) {
7410 char b[BDEVNAME_SIZE];
7411 tmp = conf->disks + i;
7412 if (tmp->rdev)
NeilBrowncc6167b2016-11-02 14:16:50 +11007413 pr_debug(" disk %d, o:%d, dev:%s\n",
NeilBrown0c55e022010-05-03 14:09:02 +10007414 i, !test_bit(Faulty, &tmp->rdev->flags),
7415 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007416 }
7417}
7418
NeilBrownfd01b882011-10-11 16:47:53 +11007419static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420{
7421 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11007422 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007423 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10007424 int count = 0;
7425 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007426
7427 for (i = 0; i < conf->raid_disks; i++) {
7428 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11007429 if (tmp->replacement
7430 && tmp->replacement->recovery_offset == MaxSector
7431 && !test_bit(Faulty, &tmp->replacement->flags)
7432 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
7433 /* Replacement has just become active. */
7434 if (!tmp->rdev
7435 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
7436 count++;
7437 if (tmp->rdev) {
7438 /* Replaced device not technically faulty,
7439 * but we need to be sure it gets removed
7440 * and never re-added.
7441 */
7442 set_bit(Faulty, &tmp->rdev->flags);
7443 sysfs_notify_dirent_safe(
7444 tmp->rdev->sysfs_state);
7445 }
7446 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
7447 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10007448 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08007449 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07007450 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10007451 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11007452 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 }
7454 }
NeilBrown6b965622010-08-18 11:56:59 +10007455 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08007456 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007457 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007459 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007460}
7461
NeilBrownb8321b62011-12-23 10:17:51 +11007462static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007463{
NeilBrownd1688a62011-10-11 16:49:52 +11007464 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11007466 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11007467 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 struct disk_info *p = conf->disks + number;
7469
7470 print_raid5_conf(conf);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007471 if (test_bit(Journal, &rdev->flags) && conf->log) {
7472 struct r5l_log *log;
Shaohua Lic2bb6242015-10-08 21:54:07 -07007473 /*
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007474 * we can't wait pending write here, as this is called in
7475 * raid5d, wait will deadlock.
Shaohua Lic2bb6242015-10-08 21:54:07 -07007476 */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007477 if (atomic_read(&mddev->writes_pending))
7478 return -EBUSY;
7479 log = conf->log;
7480 conf->log = NULL;
7481 synchronize_rcu();
7482 r5l_exit_log(log);
7483 return 0;
Shaohua Lic2bb6242015-10-08 21:54:07 -07007484 }
NeilBrown657e3e42011-12-23 10:17:52 +11007485 if (rdev == p->rdev)
7486 rdevp = &p->rdev;
7487 else if (rdev == p->replacement)
7488 rdevp = &p->replacement;
7489 else
7490 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11007491
NeilBrown657e3e42011-12-23 10:17:52 +11007492 if (number >= conf->raid_disks &&
7493 conf->reshape_progress == MaxSector)
7494 clear_bit(In_sync, &rdev->flags);
7495
7496 if (test_bit(In_sync, &rdev->flags) ||
7497 atomic_read(&rdev->nr_pending)) {
7498 err = -EBUSY;
7499 goto abort;
7500 }
7501 /* Only remove non-faulty devices if recovery
7502 * isn't possible.
7503 */
7504 if (!test_bit(Faulty, &rdev->flags) &&
7505 mddev->recovery_disabled != conf->recovery_disabled &&
7506 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11007507 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11007508 number < conf->raid_disks) {
7509 err = -EBUSY;
7510 goto abort;
7511 }
7512 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10007513 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
7514 synchronize_rcu();
7515 if (atomic_read(&rdev->nr_pending)) {
7516 /* lost the race, try later */
7517 err = -EBUSY;
7518 *rdevp = rdev;
7519 }
7520 }
7521 if (p->replacement) {
NeilBrowndd054fc2011-12-23 10:17:53 +11007522 /* We must have just cleared 'rdev' */
7523 p->rdev = p->replacement;
7524 clear_bit(Replacement, &p->replacement->flags);
7525 smp_mb(); /* Make sure other CPUs may see both as identical
7526 * but will never see neither - if they are careful
7527 */
7528 p->replacement = NULL;
7529 clear_bit(WantReplacement, &rdev->flags);
7530 } else
7531 /* We might have just removed the Replacement as faulty-
7532 * clear the bit just in case
7533 */
7534 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535abort:
7536
7537 print_raid5_conf(conf);
7538 return err;
7539}
7540
NeilBrownfd01b882011-10-11 16:47:53 +11007541static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542{
NeilBrownd1688a62011-10-11 16:49:52 +11007543 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10007544 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545 int disk;
7546 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10007547 int first = 0;
7548 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007549
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007550 if (test_bit(Journal, &rdev->flags)) {
7551 char b[BDEVNAME_SIZE];
7552 if (conf->log)
7553 return -EBUSY;
7554
7555 rdev->raid_disk = 0;
7556 /*
7557 * The array is in readonly mode if journal is missing, so no
7558 * write requests running. We should be safe
7559 */
7560 r5l_init_log(conf, rdev);
NeilBrowncc6167b2016-11-02 14:16:50 +11007561 pr_debug("md/raid:%s: using device %s as journal\n",
7562 mdname(mddev), bdevname(rdev->bdev, b));
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007563 return 0;
7564 }
NeilBrown7f0da592011-07-28 11:39:22 +10007565 if (mddev->recovery_disabled == conf->recovery_disabled)
7566 return -EBUSY;
7567
NeilBrowndc10c642012-03-19 12:46:37 +11007568 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10007570 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007571
Neil Brown6c2fce22008-06-28 08:31:31 +10007572 if (rdev->raid_disk >= 0)
7573 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007574
7575 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07007576 * find the disk ... but prefer rdev->saved_raid_disk
7577 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578 */
NeilBrown16a53ec2006-06-26 00:27:38 -07007579 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10007580 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07007581 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10007582 first = rdev->saved_raid_disk;
7583
7584 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007585 p = conf->disks + disk;
7586 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08007587 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007588 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10007589 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07007590 if (rdev->saved_raid_disk != disk)
7591 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08007592 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10007593 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007594 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007595 }
7596 for (disk = first; disk <= last; disk++) {
7597 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007598 if (test_bit(WantReplacement, &p->rdev->flags) &&
7599 p->replacement == NULL) {
7600 clear_bit(In_sync, &rdev->flags);
7601 set_bit(Replacement, &rdev->flags);
7602 rdev->raid_disk = disk;
7603 err = 0;
7604 conf->fullsync = 1;
7605 rcu_assign_pointer(p->replacement, rdev);
7606 break;
7607 }
7608 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007609out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10007611 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612}
7613
NeilBrownfd01b882011-10-11 16:47:53 +11007614static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615{
7616 /* no resync is happening, and there is enough space
7617 * on all devices, so we can resize.
7618 * We need to make sure resync covers any new space.
7619 * If the array is shrinking we should possibly wait until
7620 * any io in the removed space completes, but it hardly seems
7621 * worth it.
7622 */
NeilBrowna4a61252012-05-22 13:55:27 +10007623 sector_t newsize;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007624 struct r5conf *conf = mddev->private;
7625
Shaohua Li713cf5a2015-08-13 14:32:03 -07007626 if (conf->log)
7627 return -EINVAL;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007628 sectors &= ~((sector_t)conf->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10007629 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
7630 if (mddev->external_size &&
7631 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11007632 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10007633 if (mddev->bitmap) {
7634 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
7635 if (ret)
7636 return ret;
7637 }
7638 md_set_array_sectors(mddev, newsize);
NeilBrownb0986362011-05-11 15:52:21 +10007639 if (sectors > mddev->dev_sectors &&
7640 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11007641 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007642 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7643 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007644 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07007645 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007646 return 0;
7647}
7648
NeilBrownfd01b882011-10-11 16:47:53 +11007649static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10007650{
7651 /* Can only proceed if there are plenty of stripe_heads.
7652 * We need a minimum of one full stripe,, and for sensible progress
7653 * it is best to have about 4 times that.
7654 * If we require 4 times, then the default 256 4K stripe_heads will
7655 * allow for chunk sizes up to 256K, which is probably OK.
7656 * If the chunk size is greater, user-space should request more
7657 * stripe_heads first.
7658 */
NeilBrownd1688a62011-10-11 16:49:52 +11007659 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10007660 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007661 > conf->min_nr_stripes ||
NeilBrown01ee22b2009-06-18 08:47:20 +10007662 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007663 > conf->min_nr_stripes) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007664 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7665 mdname(mddev),
7666 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
7667 / STRIPE_SIZE)*4);
NeilBrown01ee22b2009-06-18 08:47:20 +10007668 return 0;
7669 }
7670 return 1;
7671}
7672
NeilBrownfd01b882011-10-11 16:47:53 +11007673static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08007674{
NeilBrownd1688a62011-10-11 16:49:52 +11007675 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08007676
Shaohua Li713cf5a2015-08-13 14:32:03 -07007677 if (conf->log)
7678 return -EINVAL;
NeilBrown88ce4932009-03-31 15:24:23 +11007679 if (mddev->delta_disks == 0 &&
7680 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10007681 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10007682 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10007683 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11007684 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10007685 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11007686 /* We might be able to shrink, but the devices must
7687 * be made bigger first.
7688 * For raid6, 4 is the minimum size.
7689 * Otherwise 2 is the minimum
7690 */
7691 int min = 2;
7692 if (mddev->level == 6)
7693 min = 4;
7694 if (mddev->raid_disks + mddev->delta_disks < min)
7695 return -EINVAL;
7696 }
NeilBrown29269552006-03-27 01:18:10 -08007697
NeilBrown01ee22b2009-06-18 08:47:20 +10007698 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08007699 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08007700
NeilBrown738a2732015-05-08 18:19:39 +10007701 if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
7702 mddev->delta_disks > 0)
7703 if (resize_chunks(conf,
7704 conf->previous_raid_disks
7705 + max(0, mddev->delta_disks),
7706 max(mddev->new_chunk_sectors,
7707 mddev->chunk_sectors)
7708 ) < 0)
7709 return -ENOMEM;
NeilBrowne56108d62012-10-11 14:24:13 +11007710 return resize_stripes(conf, (conf->previous_raid_disks
7711 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08007712}
7713
NeilBrownfd01b882011-10-11 16:47:53 +11007714static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08007715{
NeilBrownd1688a62011-10-11 16:49:52 +11007716 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11007717 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08007718 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07007719 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08007720
NeilBrownf4168852007-02-28 20:11:53 -08007721 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08007722 return -EBUSY;
7723
NeilBrown01ee22b2009-06-18 08:47:20 +10007724 if (!check_stripe_cache(mddev))
7725 return -ENOSPC;
7726
NeilBrown30b67642012-05-22 13:55:28 +10007727 if (has_failed(conf))
7728 return -EINVAL;
7729
NeilBrownc6563a82012-05-21 09:27:00 +10007730 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11007731 if (!test_bit(In_sync, &rdev->flags)
7732 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08007733 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10007734 }
NeilBrown63c70c42006-03-27 01:18:13 -08007735
NeilBrownf4168852007-02-28 20:11:53 -08007736 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08007737 /* Not enough devices even to make a degraded array
7738 * of that size
7739 */
7740 return -EINVAL;
7741
NeilBrownec32a2b2009-03-31 15:17:38 +11007742 /* Refuse to reduce size of the array. Any reductions in
7743 * array size must be through explicit setting of array_size
7744 * attribute.
7745 */
7746 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
7747 < mddev->array_sectors) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007748 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
7749 mdname(mddev));
NeilBrownec32a2b2009-03-31 15:17:38 +11007750 return -EINVAL;
7751 }
7752
NeilBrownf6705572006-03-27 01:18:11 -08007753 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08007754 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10007755 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007756 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08007757 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007758 conf->prev_chunk_sectors = conf->chunk_sectors;
7759 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11007760 conf->prev_algo = conf->algorithm;
7761 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10007762 conf->generation++;
7763 /* Code that selects data_offset needs to see the generation update
7764 * if reshape_progress has been set - so a memory barrier needed.
7765 */
7766 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10007767 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11007768 conf->reshape_progress = raid5_size(mddev, 0, 0);
7769 else
7770 conf->reshape_progress = 0;
7771 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10007772 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007773 spin_unlock_irq(&conf->device_lock);
7774
NeilBrown4d77e3b2013-08-27 15:57:47 +10007775 /* Now make sure any requests that proceeded on the assumption
7776 * the reshape wasn't running - like Discard or Read - have
7777 * completed.
7778 */
7779 mddev_suspend(mddev);
7780 mddev_resume(mddev);
7781
NeilBrown29269552006-03-27 01:18:10 -08007782 /* Add some new drives, as many as will fit.
7783 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10007784 * Don't add devices if we are reducing the number of
7785 * devices in the array. This is because it is not possible
7786 * to correctly record the "partially reconstructed" state of
7787 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08007788 */
NeilBrown87a8dec2011-01-31 11:57:43 +11007789 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11007790 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11007791 if (rdev->raid_disk < 0 &&
7792 !test_bit(Faulty, &rdev->flags)) {
7793 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11007794 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11007795 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11007796 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11007797 else
NeilBrown87a8dec2011-01-31 11:57:43 +11007798 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10007799
7800 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11007801 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11007802 }
NeilBrown87a8dec2011-01-31 11:57:43 +11007803 } else if (rdev->raid_disk >= conf->previous_raid_disks
7804 && !test_bit(Faulty, &rdev->flags)) {
7805 /* This is a spare that was manually added */
7806 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11007807 }
NeilBrown29269552006-03-27 01:18:10 -08007808
NeilBrown87a8dec2011-01-31 11:57:43 +11007809 /* When a reshape changes the number of devices,
7810 * ->degraded is measured against the larger of the
7811 * pre and post number of devices.
7812 */
NeilBrownec32a2b2009-03-31 15:17:38 +11007813 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08007814 mddev->degraded = raid5_calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11007815 spin_unlock_irqrestore(&conf->device_lock, flags);
7816 }
NeilBrown63c70c42006-03-27 01:18:13 -08007817 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10007818 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08007819 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownf6705572006-03-27 01:18:11 -08007820
NeilBrown29269552006-03-27 01:18:10 -08007821 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7822 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10007823 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown29269552006-03-27 01:18:10 -08007824 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7825 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7826 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007827 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08007828 if (!mddev->sync_thread) {
7829 mddev->recovery = 0;
7830 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11007831 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007832 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11007833 mddev->new_chunk_sectors =
7834 conf->chunk_sectors = conf->prev_chunk_sectors;
7835 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10007836 rdev_for_each(rdev, mddev)
7837 rdev->new_data_offset = rdev->data_offset;
7838 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11007839 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11007840 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11007841 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11007842 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007843 spin_unlock_irq(&conf->device_lock);
7844 return -EAGAIN;
7845 }
NeilBrownc8f517c2009-03-31 15:28:40 +11007846 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08007847 md_wakeup_thread(mddev->sync_thread);
7848 md_new_event(mddev);
7849 return 0;
7850}
NeilBrown29269552006-03-27 01:18:10 -08007851
NeilBrownec32a2b2009-03-31 15:17:38 +11007852/* This is called from the reshape thread and should make any
7853 * changes needed in 'conf'
7854 */
NeilBrownd1688a62011-10-11 16:49:52 +11007855static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08007856{
NeilBrown29269552006-03-27 01:18:10 -08007857
NeilBrownf6705572006-03-27 01:18:11 -08007858 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10007859 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07007860
NeilBrownf6705572006-03-27 01:18:11 -08007861 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11007862 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10007863 rdev_for_each(rdev, conf->mddev)
7864 rdev->data_offset = rdev->new_data_offset;
7865 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11007866 conf->reshape_progress = MaxSector;
NeilBrown6cbd8142015-07-24 13:30:32 +10007867 conf->mddev->reshape_position = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08007868 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11007869 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07007870
7871 /* read-ahead size must cover two whole stripes, which is
7872 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
7873 */
NeilBrown4a5add42010-06-01 19:37:28 +10007874 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11007875 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007876 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11007877 / PAGE_SIZE);
Jan Karadc3b17c2017-02-02 15:56:50 +01007878 if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
7879 conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown16a53ec2006-06-26 00:27:38 -07007880 }
NeilBrown29269552006-03-27 01:18:10 -08007881 }
NeilBrown29269552006-03-27 01:18:10 -08007882}
7883
NeilBrownec32a2b2009-03-31 15:17:38 +11007884/* This is called from the raid5d thread with mddev_lock held.
7885 * It makes config changes to the device.
7886 */
NeilBrownfd01b882011-10-11 16:47:53 +11007887static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11007888{
NeilBrownd1688a62011-10-11 16:49:52 +11007889 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11007890
7891 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7892
NeilBrownec32a2b2009-03-31 15:17:38 +11007893 if (mddev->delta_disks > 0) {
7894 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
Heinz Mauelshagenfe67d192016-05-03 22:15:31 +02007895 if (mddev->queue) {
7896 set_capacity(mddev->gendisk, mddev->array_sectors);
7897 revalidate_disk(mddev->gendisk);
7898 }
NeilBrownec32a2b2009-03-31 15:17:38 +11007899 } else {
7900 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11007901 spin_lock_irq(&conf->device_lock);
Song Liu2e38a372017-01-24 10:45:30 -08007902 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11007903 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11007904 for (d = conf->raid_disks ;
7905 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10007906 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11007907 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10007908 if (rdev)
7909 clear_bit(In_sync, &rdev->flags);
7910 rdev = conf->disks[d].replacement;
7911 if (rdev)
7912 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10007913 }
NeilBrowncea9c222009-03-31 15:15:05 +11007914 }
NeilBrown88ce4932009-03-31 15:24:23 +11007915 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007916 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11007917 mddev->reshape_position = MaxSector;
7918 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007919 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11007920 }
7921}
7922
NeilBrownfd01b882011-10-11 16:47:53 +11007923static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07007924{
NeilBrownd1688a62011-10-11 16:49:52 +11007925 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07007926
7927 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08007928 case 2: /* resume for a suspend */
7929 wake_up(&conf->wait_for_overlap);
7930 break;
7931
NeilBrown72626682005-09-09 16:23:54 -07007932 case 1: /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11007933 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10007934 /* '2' tells resync/reshape to pause so that all
7935 * active stripes can drain
7936 */
Song Liua39f7af2016-11-17 15:24:40 -08007937 r5c_flush_cache(conf, INT_MAX);
NeilBrown64bd6602009-08-03 10:59:58 +10007938 conf->quiesce = 2;
Yuanhan Liub1b46482015-05-08 18:19:06 +10007939 wait_event_cmd(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08007940 atomic_read(&conf->active_stripes) == 0 &&
7941 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11007942 unlock_all_device_hash_locks_irq(conf),
7943 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10007944 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11007945 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10007946 /* allow reshape to continue */
7947 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07007948 break;
7949
7950 case 0: /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11007951 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07007952 conf->quiesce = 0;
Yuanhan Liub1b46482015-05-08 18:19:06 +10007953 wake_up(&conf->wait_for_quiescent);
NeilBrowne464eaf2006-03-27 01:18:14 -08007954 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11007955 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07007956 break;
7957 }
Shaohua Lie6c033f2015-10-04 09:20:12 -07007958 r5l_quiesce(conf->log, state);
NeilBrown72626682005-09-09 16:23:54 -07007959}
NeilBrownb15c2e52006-01-06 00:20:16 -08007960
NeilBrownfd01b882011-10-11 16:47:53 +11007961static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11007962{
NeilBrowne373ab12011-10-11 16:48:59 +11007963 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07007964 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11007965
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007966 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11007967 if (raid0_conf->nr_strip_zones > 1) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007968 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
7969 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007970 return ERR_PTR(-EINVAL);
7971 }
7972
NeilBrowne373ab12011-10-11 16:48:59 +11007973 sectors = raid0_conf->strip_zone[0].zone_end;
7974 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10007975 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007976 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11007977 mddev->new_layout = ALGORITHM_PARITY_N;
7978 mddev->new_chunk_sectors = mddev->chunk_sectors;
7979 mddev->raid_disks += 1;
7980 mddev->delta_disks = 1;
7981 /* make sure it will be not marked as dirty */
7982 mddev->recovery_cp = MaxSector;
7983
7984 return setup_conf(mddev);
7985}
7986
NeilBrownfd01b882011-10-11 16:47:53 +11007987static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11007988{
7989 int chunksect;
Shaohua Li6995f0b2016-12-08 15:48:17 -08007990 void *ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11007991
7992 if (mddev->raid_disks != 2 ||
7993 mddev->degraded > 1)
7994 return ERR_PTR(-EINVAL);
7995
7996 /* Should check if there are write-behind devices? */
7997
7998 chunksect = 64*2; /* 64K by default */
7999
8000 /* The array must be an exact multiple of chunksize */
8001 while (chunksect && (mddev->array_sectors & (chunksect-1)))
8002 chunksect >>= 1;
8003
8004 if ((chunksect<<9) < STRIPE_SIZE)
8005 /* array size does not allow a suitable chunk size */
8006 return ERR_PTR(-EINVAL);
8007
8008 mddev->new_level = 5;
8009 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10008010 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11008011
Shaohua Li6995f0b2016-12-08 15:48:17 -08008012 ret = setup_conf(mddev);
Jes Sorensen32cd7cb2017-01-06 19:31:35 -05008013 if (!IS_ERR(ret))
Shaohua Li394ed8e2017-01-04 16:10:19 -08008014 mddev_clear_unsupported_flags(mddev,
8015 UNSUPPORTED_MDDEV_FLAGS);
Shaohua Li6995f0b2016-12-08 15:48:17 -08008016 return ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11008017}
8018
NeilBrownfd01b882011-10-11 16:47:53 +11008019static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11008020{
8021 int new_layout;
8022
8023 switch (mddev->layout) {
8024 case ALGORITHM_LEFT_ASYMMETRIC_6:
8025 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
8026 break;
8027 case ALGORITHM_RIGHT_ASYMMETRIC_6:
8028 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
8029 break;
8030 case ALGORITHM_LEFT_SYMMETRIC_6:
8031 new_layout = ALGORITHM_LEFT_SYMMETRIC;
8032 break;
8033 case ALGORITHM_RIGHT_SYMMETRIC_6:
8034 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
8035 break;
8036 case ALGORITHM_PARITY_0_6:
8037 new_layout = ALGORITHM_PARITY_0;
8038 break;
8039 case ALGORITHM_PARITY_N:
8040 new_layout = ALGORITHM_PARITY_N;
8041 break;
8042 default:
8043 return ERR_PTR(-EINVAL);
8044 }
8045 mddev->new_level = 5;
8046 mddev->new_layout = new_layout;
8047 mddev->delta_disks = -1;
8048 mddev->raid_disks -= 1;
8049 return setup_conf(mddev);
8050}
8051
NeilBrownfd01b882011-10-11 16:47:53 +11008052static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11008053{
NeilBrown88ce4932009-03-31 15:24:23 +11008054 /* For a 2-drive array, the layout and chunk size can be changed
8055 * immediately as not restriping is needed.
8056 * For larger arrays we record the new value - after validation
8057 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11008058 */
NeilBrownd1688a62011-10-11 16:49:52 +11008059 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10008060 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11008061
NeilBrown597a7112009-06-18 08:47:42 +10008062 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11008063 return -EINVAL;
8064 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008065 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11008066 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008067 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11008068 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008069 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11008070 /* not factor of array size */
8071 return -EINVAL;
8072 }
8073
8074 /* They look valid */
8075
NeilBrown88ce4932009-03-31 15:24:23 +11008076 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10008077 /* can make the change immediately */
8078 if (mddev->new_layout >= 0) {
8079 conf->algorithm = mddev->new_layout;
8080 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11008081 }
8082 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10008083 conf->chunk_sectors = new_chunk ;
8084 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11008085 }
Shaohua Li29530792016-12-08 15:48:19 -08008086 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown88ce4932009-03-31 15:24:23 +11008087 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11008088 }
NeilBrown50ac1682009-06-18 08:47:55 +10008089 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11008090}
8091
NeilBrownfd01b882011-10-11 16:47:53 +11008092static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11008093{
NeilBrown597a7112009-06-18 08:47:42 +10008094 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10008095
NeilBrown597a7112009-06-18 08:47:42 +10008096 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11008097 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008098 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008099 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11008100 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008101 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11008102 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008103 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11008104 /* not factor of array size */
8105 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008106 }
NeilBrown88ce4932009-03-31 15:24:23 +11008107
8108 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10008109 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11008110}
8111
NeilBrownfd01b882011-10-11 16:47:53 +11008112static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11008113{
8114 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008115 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008116 * raid1 - if there are two drives. We need to know the chunk size
8117 * raid4 - trivial - just use a raid4 layout.
8118 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008119 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008120 if (mddev->level == 0)
8121 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11008122 if (mddev->level == 1)
8123 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11008124 if (mddev->level == 4) {
8125 mddev->new_layout = ALGORITHM_PARITY_N;
8126 mddev->new_level = 5;
8127 return setup_conf(mddev);
8128 }
NeilBrownfc9739c2009-03-31 14:57:20 +11008129 if (mddev->level == 6)
8130 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11008131
8132 return ERR_PTR(-EINVAL);
8133}
8134
NeilBrownfd01b882011-10-11 16:47:53 +11008135static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11008136{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008137 /* raid4 can take over:
8138 * raid0 - if there is only one strip zone
8139 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11008140 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008141 if (mddev->level == 0)
8142 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11008143 if (mddev->level == 5 &&
8144 mddev->layout == ALGORITHM_PARITY_N) {
8145 mddev->new_layout = 0;
8146 mddev->new_level = 4;
8147 return setup_conf(mddev);
8148 }
8149 return ERR_PTR(-EINVAL);
8150}
NeilBrownd562b0c2009-03-31 14:39:39 +11008151
NeilBrown84fc4b52011-10-11 16:49:58 +11008152static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11008153
NeilBrownfd01b882011-10-11 16:47:53 +11008154static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11008155{
8156 /* Currently can only take over a raid5. We map the
8157 * personality to an equivalent raid6 personality
8158 * with the Q block at the end.
8159 */
8160 int new_layout;
8161
8162 if (mddev->pers != &raid5_personality)
8163 return ERR_PTR(-EINVAL);
8164 if (mddev->degraded > 1)
8165 return ERR_PTR(-EINVAL);
8166 if (mddev->raid_disks > 253)
8167 return ERR_PTR(-EINVAL);
8168 if (mddev->raid_disks < 3)
8169 return ERR_PTR(-EINVAL);
8170
8171 switch (mddev->layout) {
8172 case ALGORITHM_LEFT_ASYMMETRIC:
8173 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
8174 break;
8175 case ALGORITHM_RIGHT_ASYMMETRIC:
8176 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
8177 break;
8178 case ALGORITHM_LEFT_SYMMETRIC:
8179 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
8180 break;
8181 case ALGORITHM_RIGHT_SYMMETRIC:
8182 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
8183 break;
8184 case ALGORITHM_PARITY_0:
8185 new_layout = ALGORITHM_PARITY_0_6;
8186 break;
8187 case ALGORITHM_PARITY_N:
8188 new_layout = ALGORITHM_PARITY_N;
8189 break;
8190 default:
8191 return ERR_PTR(-EINVAL);
8192 }
8193 mddev->new_level = 6;
8194 mddev->new_layout = new_layout;
8195 mddev->delta_disks = 1;
8196 mddev->raid_disks += 1;
8197 return setup_conf(mddev);
8198}
8199
NeilBrown84fc4b52011-10-11 16:49:58 +11008200static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07008201{
8202 .name = "raid6",
8203 .level = 6,
8204 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008205 .make_request = raid5_make_request,
8206 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11008207 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008208 .status = raid5_status,
8209 .error_handler = raid5_error,
NeilBrown16a53ec2006-06-26 00:27:38 -07008210 .hot_add_disk = raid5_add_disk,
8211 .hot_remove_disk= raid5_remove_disk,
8212 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008213 .sync_request = raid5_sync_request,
NeilBrown16a53ec2006-06-26 00:27:38 -07008214 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008215 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10008216 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08008217 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008218 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07008219 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11008220 .takeover = raid6_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008221 .congested = raid5_congested,
NeilBrown16a53ec2006-06-26 00:27:38 -07008222};
NeilBrown84fc4b52011-10-11 16:49:58 +11008223static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224{
8225 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08008226 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008228 .make_request = raid5_make_request,
8229 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11008230 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008231 .status = raid5_status,
8232 .error_handler = raid5_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008233 .hot_add_disk = raid5_add_disk,
8234 .hot_remove_disk= raid5_remove_disk,
8235 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008236 .sync_request = raid5_sync_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008237 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008238 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08008239 .check_reshape = raid5_check_reshape,
8240 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008241 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07008242 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11008243 .takeover = raid5_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008244 .congested = raid5_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008245};
8246
NeilBrown84fc4b52011-10-11 16:49:58 +11008247static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008248{
NeilBrown2604b702006-01-06 00:20:36 -08008249 .name = "raid4",
8250 .level = 4,
8251 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008252 .make_request = raid5_make_request,
8253 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11008254 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008255 .status = raid5_status,
8256 .error_handler = raid5_error,
NeilBrown2604b702006-01-06 00:20:36 -08008257 .hot_add_disk = raid5_add_disk,
8258 .hot_remove_disk= raid5_remove_disk,
8259 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008260 .sync_request = raid5_sync_request,
NeilBrown2604b702006-01-06 00:20:36 -08008261 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008262 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08008263 .check_reshape = raid5_check_reshape,
8264 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008265 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08008266 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11008267 .takeover = raid4_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008268 .congested = raid5_congested,
NeilBrown2604b702006-01-06 00:20:36 -08008269};
8270
8271static int __init raid5_init(void)
8272{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008273 int ret;
8274
Shaohua Li851c30c2013-08-28 14:30:16 +08008275 raid5_wq = alloc_workqueue("raid5wq",
8276 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
8277 if (!raid5_wq)
8278 return -ENOMEM;
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008279
8280 ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
8281 "md/raid5:prepare",
8282 raid456_cpu_up_prepare,
8283 raid456_cpu_dead);
8284 if (ret) {
8285 destroy_workqueue(raid5_wq);
8286 return ret;
8287 }
NeilBrown16a53ec2006-06-26 00:27:38 -07008288 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008289 register_md_personality(&raid5_personality);
8290 register_md_personality(&raid4_personality);
8291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008292}
8293
NeilBrown2604b702006-01-06 00:20:36 -08008294static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008295{
NeilBrown16a53ec2006-06-26 00:27:38 -07008296 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008297 unregister_md_personality(&raid5_personality);
8298 unregister_md_personality(&raid4_personality);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008299 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
Shaohua Li851c30c2013-08-28 14:30:16 +08008300 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008301}
8302
8303module_init(raid5_init);
8304module_exit(raid5_exit);
8305MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008306MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008307MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08008308MODULE_ALIAS("md-raid5");
8309MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08008310MODULE_ALIAS("md-level-5");
8311MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07008312MODULE_ALIAS("md-personality-8"); /* RAID6 */
8313MODULE_ALIAS("md-raid6");
8314MODULE_ALIAS("md-level-6");
8315
8316/* This used to be two separate modules, they were: */
8317MODULE_ALIAS("raid5");
8318MODULE_ALIAS("raid6");