blob: 013398ce2080bf10c038552729e9dc9cff895675 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
Shaohua Li851c30c2013-08-28 14:30:16 +080056#include <linux/nodemask.h>
shli@kernel.org46d5b782014-12-15 12:57:02 +110057#include <linux/flex_array.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010058#include <linux/sched/signal.h>
59
NeilBrowna9add5d2012-10-31 11:59:09 +110060#include <trace/events/block.h>
Shaohua Liaaf9f122017-03-03 22:06:12 -080061#include <linux/list_sort.h>
NeilBrowna9add5d2012-10-31 11:59:09 +110062
NeilBrown43b2e5d2009-03-31 14:33:13 +110063#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110064#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110065#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110066#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070067
Shaohua Li394ed8e2017-01-04 16:10:19 -080068#define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED)
69
Shaohua Li851c30c2013-08-28 14:30:16 +080070#define cpu_to_group(cpu) cpu_to_node(cpu)
71#define ANY_GROUP NUMA_NO_NODE
72
NeilBrown8e0e99b2014-10-02 13:45:00 +100073static bool devices_handle_discard_safely = false;
74module_param(devices_handle_discard_safely, bool, 0644);
75MODULE_PARM_DESC(devices_handle_discard_safely,
76 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
Shaohua Li851c30c2013-08-28 14:30:16 +080077static struct workqueue_struct *raid5_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
NeilBrownd1688a62011-10-11 16:49:52 +110079static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110080{
81 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
82 return &conf->stripe_hashtbl[hash];
83}
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Shaohua Li566c09c2013-11-14 15:16:17 +110085static inline int stripe_hash_locks_hash(sector_t sect)
86{
87 return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK;
88}
89
90static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
91{
92 spin_lock_irq(conf->hash_locks + hash);
93 spin_lock(&conf->device_lock);
94}
95
96static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
97{
98 spin_unlock(&conf->device_lock);
99 spin_unlock_irq(conf->hash_locks + hash);
100}
101
102static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
103{
104 int i;
105 local_irq_disable();
106 spin_lock(conf->hash_locks);
107 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
108 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
109 spin_lock(&conf->device_lock);
110}
111
112static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
113{
114 int i;
115 spin_unlock(&conf->device_lock);
116 for (i = NR_STRIPE_HASH_LOCKS; i; i--)
117 spin_unlock(conf->hash_locks + i - 1);
118 local_irq_enable();
119}
120
NeilBrownd0dabf72009-03-31 14:39:38 +1100121/* Find first data disk in a raid6 stripe */
122static inline int raid6_d0(struct stripe_head *sh)
123{
NeilBrown67cc2b82009-03-31 14:39:38 +1100124 if (sh->ddf_layout)
125 /* ddf always start from first device */
126 return 0;
127 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100128 if (sh->qd_idx == sh->disks - 1)
129 return 0;
130 else
131 return sh->qd_idx + 1;
132}
NeilBrown16a53ec2006-06-26 00:27:38 -0700133static inline int raid6_next_disk(int disk, int raid_disks)
134{
135 disk++;
136 return (disk < raid_disks) ? disk : 0;
137}
Dan Williamsa4456852007-07-09 11:56:43 -0700138
NeilBrownd0dabf72009-03-31 14:39:38 +1100139/* When walking through the disks in a raid5, starting at raid6_d0,
140 * We need to map each disk to a 'slot', where the data disks are slot
141 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
142 * is raid_disks-1. This help does that mapping.
143 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100144static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
145 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100146{
Dan Williams66295422009-10-19 18:09:32 -0700147 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100148
NeilBrowne4424fe2009-10-16 16:27:34 +1100149 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700150 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100151 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100152 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100153 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100154 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100155 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700156 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100157 return slot;
158}
159
NeilBrown34a6f802015-08-14 12:07:57 +1000160static void return_io(struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -0700161{
NeilBrown34a6f802015-08-14 12:07:57 +1000162 struct bio *bi;
163 while ((bi = bio_list_pop(return_bi)) != NULL) {
Kent Overstreet4f024f32013-10-11 15:44:27 -0700164 bi->bi_iter.bi_size = 0;
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700165 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
166 bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200167 bio_endio(bi);
Dan Williamsa4456852007-07-09 11:56:43 -0700168 }
169}
170
NeilBrownd1688a62011-10-11 16:49:52 +1100171static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Dan Williams600aa102008-06-28 08:32:05 +1000173static int stripe_operations_active(struct stripe_head *sh)
174{
175 return sh->check_state || sh->reconstruct_state ||
176 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
177 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
178}
179
Shaohua Li535ae4e2017-02-15 19:37:32 -0800180static bool stripe_is_lowprio(struct stripe_head *sh)
181{
182 return (test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) ||
183 test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) &&
184 !test_bit(STRIPE_R5C_CACHING, &sh->state);
185}
186
Shaohua Li851c30c2013-08-28 14:30:16 +0800187static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
188{
189 struct r5conf *conf = sh->raid_conf;
190 struct r5worker_group *group;
Shaohua Libfc90cb2013-08-29 15:40:32 +0800191 int thread_cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +0800192 int i, cpu = sh->cpu;
193
194 if (!cpu_online(cpu)) {
195 cpu = cpumask_any(cpu_online_mask);
196 sh->cpu = cpu;
197 }
198
199 if (list_empty(&sh->lru)) {
200 struct r5worker_group *group;
201 group = conf->worker_groups + cpu_to_group(cpu);
Shaohua Li535ae4e2017-02-15 19:37:32 -0800202 if (stripe_is_lowprio(sh))
203 list_add_tail(&sh->lru, &group->loprio_list);
204 else
205 list_add_tail(&sh->lru, &group->handle_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800206 group->stripes_cnt++;
207 sh->group = group;
Shaohua Li851c30c2013-08-28 14:30:16 +0800208 }
209
210 if (conf->worker_cnt_per_group == 0) {
211 md_wakeup_thread(conf->mddev->thread);
212 return;
213 }
214
215 group = conf->worker_groups + cpu_to_group(sh->cpu);
216
Shaohua Libfc90cb2013-08-29 15:40:32 +0800217 group->workers[0].working = true;
218 /* at least one worker should run to avoid race */
219 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
220
221 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
222 /* wakeup more workers */
223 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
224 if (group->workers[i].working == false) {
225 group->workers[i].working = true;
226 queue_work_on(sh->cpu, raid5_wq,
227 &group->workers[i].work);
228 thread_cnt--;
229 }
230 }
Shaohua Li851c30c2013-08-28 14:30:16 +0800231}
232
Shaohua Li566c09c2013-11-14 15:16:17 +1100233static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
234 struct list_head *temp_inactive_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Song Liu1e6d6902016-11-17 15:24:39 -0800236 int i;
237 int injournal = 0; /* number of date pages with R5_InJournal */
238
Shaohua Li4eb788d2012-07-19 16:01:31 +1000239 BUG_ON(!list_empty(&sh->lru));
240 BUG_ON(atomic_read(&conf->active_stripes)==0);
Song Liu1e6d6902016-11-17 15:24:39 -0800241
242 if (r5c_is_writeback(conf->log))
243 for (i = sh->disks; i--; )
244 if (test_bit(R5_InJournal, &sh->dev[i].flags))
245 injournal++;
Song Liua39f7af2016-11-17 15:24:40 -0800246 /*
247 * When quiesce in r5c write back, set STRIPE_HANDLE for stripes with
248 * data in journal, so they are not released to cached lists
249 */
250 if (conf->quiesce && r5c_is_writeback(conf->log) &&
251 !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0) {
252 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
253 r5c_make_stripe_write_out(sh);
254 set_bit(STRIPE_HANDLE, &sh->state);
255 }
Song Liu1e6d6902016-11-17 15:24:39 -0800256
Shaohua Li4eb788d2012-07-19 16:01:31 +1000257 if (test_bit(STRIPE_HANDLE, &sh->state)) {
258 if (test_bit(STRIPE_DELAYED, &sh->state) &&
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500259 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
Shaohua Li4eb788d2012-07-19 16:01:31 +1000260 list_add_tail(&sh->lru, &conf->delayed_list);
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500261 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
Shaohua Li4eb788d2012-07-19 16:01:31 +1000262 sh->bm_seq - conf->seq_write > 0)
263 list_add_tail(&sh->lru, &conf->bitmap_list);
264 else {
265 clear_bit(STRIPE_DELAYED, &sh->state);
266 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Shaohua Li851c30c2013-08-28 14:30:16 +0800267 if (conf->worker_cnt_per_group == 0) {
Shaohua Li535ae4e2017-02-15 19:37:32 -0800268 if (stripe_is_lowprio(sh))
269 list_add_tail(&sh->lru,
270 &conf->loprio_list);
271 else
272 list_add_tail(&sh->lru,
273 &conf->handle_list);
Shaohua Li851c30c2013-08-28 14:30:16 +0800274 } else {
275 raid5_wakeup_stripe_thread(sh);
276 return;
277 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000278 }
279 md_wakeup_thread(conf->mddev->thread);
280 } else {
281 BUG_ON(stripe_operations_active(sh));
282 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
283 if (atomic_dec_return(&conf->preread_active_stripes)
284 < IO_THRESHOLD)
285 md_wakeup_thread(conf->mddev->thread);
286 atomic_dec(&conf->active_stripes);
Song Liu1e6d6902016-11-17 15:24:39 -0800287 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
288 if (!r5c_is_writeback(conf->log))
289 list_add_tail(&sh->lru, temp_inactive_list);
290 else {
291 WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags));
292 if (injournal == 0)
293 list_add_tail(&sh->lru, temp_inactive_list);
294 else if (injournal == conf->raid_disks - conf->max_degraded) {
295 /* full stripe */
296 if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state))
297 atomic_inc(&conf->r5c_cached_full_stripes);
298 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state))
299 atomic_dec(&conf->r5c_cached_partial_stripes);
300 list_add_tail(&sh->lru, &conf->r5c_full_stripe_list);
Song Liua39f7af2016-11-17 15:24:40 -0800301 r5c_check_cached_full_stripe(conf);
Song Liu03b047f2017-01-11 13:39:14 -0800302 } else
303 /*
304 * STRIPE_R5C_PARTIAL_STRIPE is set in
305 * r5c_try_caching_write(). No need to
306 * set it again.
307 */
Song Liu1e6d6902016-11-17 15:24:39 -0800308 list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list);
Song Liu1e6d6902016-11-17 15:24:39 -0800309 }
310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312}
NeilBrownd0dabf72009-03-31 14:39:38 +1100313
Shaohua Li566c09c2013-11-14 15:16:17 +1100314static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
315 struct list_head *temp_inactive_list)
Shaohua Li4eb788d2012-07-19 16:01:31 +1000316{
317 if (atomic_dec_and_test(&sh->count))
Shaohua Li566c09c2013-11-14 15:16:17 +1100318 do_release_stripe(conf, sh, temp_inactive_list);
319}
320
321/*
322 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
323 *
324 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
325 * given time. Adding stripes only takes device lock, while deleting stripes
326 * only takes hash lock.
327 */
328static void release_inactive_stripe_list(struct r5conf *conf,
329 struct list_head *temp_inactive_list,
330 int hash)
331{
332 int size;
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800333 bool do_wakeup = false;
Shaohua Li566c09c2013-11-14 15:16:17 +1100334 unsigned long flags;
335
336 if (hash == NR_STRIPE_HASH_LOCKS) {
337 size = NR_STRIPE_HASH_LOCKS;
338 hash = NR_STRIPE_HASH_LOCKS - 1;
339 } else
340 size = 1;
341 while (size) {
342 struct list_head *list = &temp_inactive_list[size - 1];
343
344 /*
Shaohua Li6d036f72015-08-13 14:31:57 -0700345 * We don't hold any lock here yet, raid5_get_active_stripe() might
Shaohua Li566c09c2013-11-14 15:16:17 +1100346 * remove stripes from the list
347 */
348 if (!list_empty_careful(list)) {
349 spin_lock_irqsave(conf->hash_locks + hash, flags);
Shaohua Li4bda5562013-11-14 15:16:17 +1100350 if (list_empty(conf->inactive_list + hash) &&
351 !list_empty(list))
352 atomic_dec(&conf->empty_inactive_list_nr);
Shaohua Li566c09c2013-11-14 15:16:17 +1100353 list_splice_tail_init(list, conf->inactive_list + hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800354 do_wakeup = true;
Shaohua Li566c09c2013-11-14 15:16:17 +1100355 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
356 }
357 size--;
358 hash--;
359 }
360
361 if (do_wakeup) {
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800362 wake_up(&conf->wait_for_stripe);
Yuanhan Liub1b46482015-05-08 18:19:06 +1000363 if (atomic_read(&conf->active_stripes) == 0)
364 wake_up(&conf->wait_for_quiescent);
Shaohua Li566c09c2013-11-14 15:16:17 +1100365 if (conf->retry_read_aligned)
366 md_wakeup_thread(conf->mddev->thread);
367 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000368}
369
Shaohua Li773ca822013-08-27 17:50:39 +0800370/* should hold conf->device_lock already */
Shaohua Li566c09c2013-11-14 15:16:17 +1100371static int release_stripe_list(struct r5conf *conf,
372 struct list_head *temp_inactive_list)
Shaohua Li773ca822013-08-27 17:50:39 +0800373{
Byungchul Parkeae82632017-02-14 16:26:24 +0900374 struct stripe_head *sh, *t;
Shaohua Li773ca822013-08-27 17:50:39 +0800375 int count = 0;
376 struct llist_node *head;
377
378 head = llist_del_all(&conf->released_stripes);
Shaohua Lid265d9d2013-08-28 14:29:05 +0800379 head = llist_reverse_order(head);
Byungchul Parkeae82632017-02-14 16:26:24 +0900380 llist_for_each_entry_safe(sh, t, head, release_list) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100381 int hash;
382
Shaohua Li773ca822013-08-27 17:50:39 +0800383 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
384 smp_mb();
385 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
386 /*
387 * Don't worry the bit is set here, because if the bit is set
388 * again, the count is always > 1. This is true for
389 * STRIPE_ON_UNPLUG_LIST bit too.
390 */
Shaohua Li566c09c2013-11-14 15:16:17 +1100391 hash = sh->hash_lock_index;
392 __release_stripe(conf, sh, &temp_inactive_list[hash]);
Shaohua Li773ca822013-08-27 17:50:39 +0800393 count++;
394 }
395
396 return count;
397}
398
Shaohua Li6d036f72015-08-13 14:31:57 -0700399void raid5_release_stripe(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
NeilBrownd1688a62011-10-11 16:49:52 +1100401 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 unsigned long flags;
Shaohua Li566c09c2013-11-14 15:16:17 +1100403 struct list_head list;
404 int hash;
Shaohua Li773ca822013-08-27 17:50:39 +0800405 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700406
Eivind Sartocf170f32014-05-28 13:39:23 +1000407 /* Avoid release_list until the last reference.
408 */
409 if (atomic_add_unless(&sh->count, -1, 1))
410 return;
411
majianpengad4068d2013-11-14 15:16:15 +1100412 if (unlikely(!conf->mddev->thread) ||
413 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
Shaohua Li773ca822013-08-27 17:50:39 +0800414 goto slow_path;
415 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
416 if (wakeup)
417 md_wakeup_thread(conf->mddev->thread);
418 return;
419slow_path:
Shaohua Li4eb788d2012-07-19 16:01:31 +1000420 local_irq_save(flags);
Shaohua Li773ca822013-08-27 17:50:39 +0800421 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Shaohua Li4eb788d2012-07-19 16:01:31 +1000422 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100423 INIT_LIST_HEAD(&list);
424 hash = sh->hash_lock_index;
425 do_release_stripe(conf, sh, &list);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000426 spin_unlock(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +1100427 release_inactive_stripe_list(conf, &list, hash);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000428 }
429 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
NeilBrownfccddba2006-01-06 00:20:33 -0800432static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Dan Williams45b42332007-07-09 11:56:43 -0700434 pr_debug("remove_hash(), stripe %llu\n",
435 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
NeilBrownfccddba2006-01-06 00:20:33 -0800437 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
NeilBrownd1688a62011-10-11 16:49:52 +1100440static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
NeilBrownfccddba2006-01-06 00:20:33 -0800442 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Dan Williams45b42332007-07-09 11:56:43 -0700444 pr_debug("insert_hash(), stripe %llu\n",
445 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
NeilBrownfccddba2006-01-06 00:20:33 -0800447 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450/* find an idle stripe, make sure it is unhashed, and return it. */
Shaohua Li566c09c2013-11-14 15:16:17 +1100451static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 struct stripe_head *sh = NULL;
454 struct list_head *first;
455
Shaohua Li566c09c2013-11-14 15:16:17 +1100456 if (list_empty(conf->inactive_list + hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 goto out;
Shaohua Li566c09c2013-11-14 15:16:17 +1100458 first = (conf->inactive_list + hash)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 sh = list_entry(first, struct stripe_head, lru);
460 list_del_init(first);
461 remove_hash(sh);
462 atomic_inc(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100463 BUG_ON(hash != sh->hash_lock_index);
Shaohua Li4bda5562013-11-14 15:16:17 +1100464 if (list_empty(conf->inactive_list + hash))
465 atomic_inc(&conf->empty_inactive_list_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466out:
467 return sh;
468}
469
NeilBrowne4e11e32010-06-16 16:45:16 +1000470static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 struct page *p;
473 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000474 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
NeilBrowne4e11e32010-06-16 16:45:16 +1000476 for (i = 0; i < num ; i++) {
Shaohua Lid592a992014-05-21 17:57:44 +0800477 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 p = sh->dev[i].page;
479 if (!p)
480 continue;
481 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800482 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484}
485
NeilBrowna9683a72015-02-25 12:02:51 +1100486static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000489 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
NeilBrowne4e11e32010-06-16 16:45:16 +1000491 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 struct page *page;
493
NeilBrowna9683a72015-02-25 12:02:51 +1100494 if (!(page = alloc_page(gfp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 return 1;
496 }
497 sh->dev[i].page = page;
Shaohua Lid592a992014-05-21 17:57:44 +0800498 sh->dev[i].orig_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500 return 0;
501}
502
NeilBrown784052e2009-03-31 15:19:07 +1100503static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100504static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100505 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
NeilBrownb5663ba2009-03-31 14:39:38 +1100507static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
NeilBrownd1688a62011-10-11 16:49:52 +1100509 struct r5conf *conf = sh->raid_conf;
Shaohua Li566c09c2013-11-14 15:16:17 +1100510 int i, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200512 BUG_ON(atomic_read(&sh->count) != 0);
513 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000514 BUG_ON(stripe_operations_active(sh));
shli@kernel.org59fc6302014-12-15 12:57:03 +1100515 BUG_ON(sh->batch_head);
Dan Williamsd84e0f12007-01-02 13:52:30 -0700516
Dan Williams45b42332007-07-09 11:56:43 -0700517 pr_debug("init_stripe called, stripe %llu\n",
Markus Stockhausenb8e6a152014-08-23 20:19:27 +1000518 (unsigned long long)sector);
Shaohua Li566c09c2013-11-14 15:16:17 +1100519retry:
520 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100521 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100522 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100524 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 sh->state = 0;
526
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800527 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct r5dev *dev = &sh->dev[i];
529
Dan Williamsd84e0f12007-01-02 13:52:30 -0700530 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 test_bit(R5_LOCKED, &dev->flags)) {
NeilBrowncc6167b2016-11-02 14:16:50 +1100532 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700534 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000536 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100539 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100541 if (read_seqcount_retry(&conf->gen_lock, seq))
542 goto retry;
shli@kernel.org7a87f432014-12-15 12:57:03 +1100543 sh->overwrite_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800545 sh->cpu = smp_processor_id();
shli@kernel.orgda41ba62014-12-15 12:57:03 +1100546 set_bit(STRIPE_BATCH_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
NeilBrownd1688a62011-10-11 16:49:52 +1100549static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100550 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct stripe_head *sh;
553
Dan Williams45b42332007-07-09 11:56:43 -0700554 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800555 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100556 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700558 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return NULL;
560}
561
NeilBrown674806d2010-06-16 17:17:53 +1000562/*
563 * Need to check if array has failed when deciding whether to:
564 * - start an array
565 * - remove non-faulty devices
566 * - add a spare
567 * - allow a reshape
568 * This determination is simple when no reshape is happening.
569 * However if there is a reshape, we need to carefully check
570 * both the before and after sections.
571 * This is because some failed devices may only affect one
572 * of the two sections, and some non-in_sync devices may
573 * be insync in the section most affected by failed devices.
574 */
Song Liu2e38a372017-01-24 10:45:30 -0800575int raid5_calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000576{
NeilBrown908f4fb2011-12-23 10:17:50 +1100577 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000578 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000579
580 rcu_read_lock();
581 degraded = 0;
582 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100583 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000584 if (rdev && test_bit(Faulty, &rdev->flags))
585 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000586 if (!rdev || test_bit(Faulty, &rdev->flags))
587 degraded++;
588 else if (test_bit(In_sync, &rdev->flags))
589 ;
590 else
591 /* not in-sync or faulty.
592 * If the reshape increases the number of devices,
593 * this is being recovered by the reshape, so
594 * this 'previous' section is not in_sync.
595 * If the number of devices is being reduced however,
596 * the device can only be part of the array if
597 * we are reverting a reshape, so this section will
598 * be in-sync.
599 */
600 if (conf->raid_disks >= conf->previous_raid_disks)
601 degraded++;
602 }
603 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100604 if (conf->raid_disks == conf->previous_raid_disks)
605 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000606 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100607 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000608 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100609 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000610 if (rdev && test_bit(Faulty, &rdev->flags))
611 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000612 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100613 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000614 else if (test_bit(In_sync, &rdev->flags))
615 ;
616 else
617 /* not in-sync or faulty.
618 * If reshape increases the number of devices, this
619 * section has already been recovered, else it
620 * almost certainly hasn't.
621 */
622 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100623 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000624 }
625 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100626 if (degraded2 > degraded)
627 return degraded2;
628 return degraded;
629}
630
631static int has_failed(struct r5conf *conf)
632{
633 int degraded;
634
635 if (conf->mddev->reshape_position == MaxSector)
636 return conf->mddev->degraded > conf->max_degraded;
637
Song Liu2e38a372017-01-24 10:45:30 -0800638 degraded = raid5_calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000639 if (degraded > conf->max_degraded)
640 return 1;
641 return 0;
642}
643
Shaohua Li6d036f72015-08-13 14:31:57 -0700644struct stripe_head *
645raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
646 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 struct stripe_head *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +1100649 int hash = stripe_hash_locks_hash(sector);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800650 int inc_empty_inactive_list_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Dan Williams45b42332007-07-09 11:56:43 -0700652 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Shaohua Li566c09c2013-11-14 15:16:17 +1100654 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 do {
Yuanhan Liub1b46482015-05-08 18:19:06 +1000657 wait_event_lock_irq(conf->wait_for_quiescent,
NeilBrowna8c906c2009-06-09 14:39:59 +1000658 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100659 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100660 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (!sh) {
NeilBrownedbe83a2015-02-26 12:47:56 +1100662 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100663 sh = get_free_stripe(conf, hash);
Shaohua Li713bc5c2015-05-28 17:33:47 -0700664 if (!sh && !test_bit(R5_DID_ALLOC,
665 &conf->cache_state))
NeilBrownedbe83a2015-02-26 12:47:56 +1100666 set_bit(R5_ALLOC_MORE,
667 &conf->cache_state);
668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (noblock && sh == NULL)
670 break;
Song Liua39f7af2016-11-17 15:24:40 -0800671
672 r5c_check_stripe_cache_usage(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (!sh) {
NeilBrown54233992015-02-26 12:21:04 +1100674 set_bit(R5_INACTIVE_BLOCKED,
675 &conf->cache_state);
Song Liua39f7af2016-11-17 15:24:40 -0800676 r5l_wake_reclaim(conf->log, 0);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800677 wait_event_lock_irq(
678 conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +1100679 !list_empty(conf->inactive_list + hash) &&
680 (atomic_read(&conf->active_stripes)
681 < (conf->max_nr_stripes * 3 / 4)
NeilBrown54233992015-02-26 12:21:04 +1100682 || !test_bit(R5_INACTIVE_BLOCKED,
683 &conf->cache_state)),
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800684 *(conf->hash_locks + hash));
NeilBrown54233992015-02-26 12:21:04 +1100685 clear_bit(R5_INACTIVE_BLOCKED,
686 &conf->cache_state);
NeilBrown7da9d452014-01-22 11:45:03 +1100687 } else {
NeilBrownb5663ba2009-03-31 14:39:38 +1100688 init_stripe(sh, sector, previous);
NeilBrown7da9d452014-01-22 11:45:03 +1100689 atomic_inc(&sh->count);
690 }
Shaohua Lie240c182014-04-09 11:27:42 +0800691 } else if (!atomic_inc_not_zero(&sh->count)) {
NeilBrown6d183de2013-11-28 10:55:27 +1100692 spin_lock(&conf->device_lock);
Shaohua Lie240c182014-04-09 11:27:42 +0800693 if (!atomic_read(&sh->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (!test_bit(STRIPE_HANDLE, &sh->state))
695 atomic_inc(&conf->active_stripes);
NeilBrown5af9bef2014-01-14 15:16:10 +1100696 BUG_ON(list_empty(&sh->lru) &&
697 !test_bit(STRIPE_EXPANDING, &sh->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800698 inc_empty_inactive_list_flag = 0;
699 if (!list_empty(conf->inactive_list + hash))
700 inc_empty_inactive_list_flag = 1;
NeilBrown16a53ec2006-06-26 00:27:38 -0700701 list_del_init(&sh->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800702 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
703 atomic_inc(&conf->empty_inactive_list_nr);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800704 if (sh->group) {
705 sh->group->stripes_cnt--;
706 sh->group = NULL;
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
NeilBrown7da9d452014-01-22 11:45:03 +1100709 atomic_inc(&sh->count);
NeilBrown6d183de2013-11-28 10:55:27 +1100710 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712 } while (sh == NULL);
713
Shaohua Li566c09c2013-11-14 15:16:17 +1100714 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return sh;
716}
717
shli@kernel.org7a87f432014-12-15 12:57:03 +1100718static bool is_full_stripe_write(struct stripe_head *sh)
719{
720 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
721 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
722}
723
shli@kernel.org59fc6302014-12-15 12:57:03 +1100724static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
725{
726 local_irq_disable();
727 if (sh1 > sh2) {
728 spin_lock(&sh2->stripe_lock);
729 spin_lock_nested(&sh1->stripe_lock, 1);
730 } else {
731 spin_lock(&sh1->stripe_lock);
732 spin_lock_nested(&sh2->stripe_lock, 1);
733 }
734}
735
736static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
737{
738 spin_unlock(&sh1->stripe_lock);
739 spin_unlock(&sh2->stripe_lock);
740 local_irq_enable();
741}
742
743/* Only freshly new full stripe normal write stripe can be added to a batch list */
744static bool stripe_can_batch(struct stripe_head *sh)
745{
Shaohua Li9c3e3332015-08-13 14:32:02 -0700746 struct r5conf *conf = sh->raid_conf;
747
748 if (conf->log)
749 return false;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100750 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
NeilBrownd0852df52015-05-27 08:43:45 +1000751 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
shli@kernel.org59fc6302014-12-15 12:57:03 +1100752 is_full_stripe_write(sh);
753}
754
755/* we only do back search */
756static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
757{
758 struct stripe_head *head;
759 sector_t head_sector, tmp_sec;
760 int hash;
761 int dd_idx;
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800762 int inc_empty_inactive_list_flag;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100763
shli@kernel.org59fc6302014-12-15 12:57:03 +1100764 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
765 tmp_sec = sh->sector;
766 if (!sector_div(tmp_sec, conf->chunk_sectors))
767 return;
768 head_sector = sh->sector - STRIPE_SECTORS;
769
770 hash = stripe_hash_locks_hash(head_sector);
771 spin_lock_irq(conf->hash_locks + hash);
772 head = __find_stripe(conf, head_sector, conf->generation);
773 if (head && !atomic_inc_not_zero(&head->count)) {
774 spin_lock(&conf->device_lock);
775 if (!atomic_read(&head->count)) {
776 if (!test_bit(STRIPE_HANDLE, &head->state))
777 atomic_inc(&conf->active_stripes);
778 BUG_ON(list_empty(&head->lru) &&
779 !test_bit(STRIPE_EXPANDING, &head->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800780 inc_empty_inactive_list_flag = 0;
781 if (!list_empty(conf->inactive_list + hash))
782 inc_empty_inactive_list_flag = 1;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100783 list_del_init(&head->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800784 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
785 atomic_inc(&conf->empty_inactive_list_nr);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100786 if (head->group) {
787 head->group->stripes_cnt--;
788 head->group = NULL;
789 }
790 }
791 atomic_inc(&head->count);
792 spin_unlock(&conf->device_lock);
793 }
794 spin_unlock_irq(conf->hash_locks + hash);
795
796 if (!head)
797 return;
798 if (!stripe_can_batch(head))
799 goto out;
800
801 lock_two_stripes(head, sh);
802 /* clear_batch_ready clear the flag */
803 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
804 goto unlock_out;
805
806 if (sh->batch_head)
807 goto unlock_out;
808
809 dd_idx = 0;
810 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
811 dd_idx++;
Jens Axboe1eff9d32016-08-05 15:35:16 -0600812 if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
Mike Christie796a5cf2016-06-05 14:32:07 -0500813 bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
shli@kernel.org59fc6302014-12-15 12:57:03 +1100814 goto unlock_out;
815
816 if (head->batch_head) {
817 spin_lock(&head->batch_head->batch_lock);
818 /* This batch list is already running */
819 if (!stripe_can_batch(head)) {
820 spin_unlock(&head->batch_head->batch_lock);
821 goto unlock_out;
822 }
823
824 /*
825 * at this point, head's BATCH_READY could be cleared, but we
826 * can still add the stripe to batch list
827 */
828 list_add(&sh->batch_list, &head->batch_list);
829 spin_unlock(&head->batch_head->batch_lock);
830
831 sh->batch_head = head->batch_head;
832 } else {
833 head->batch_head = head;
834 sh->batch_head = head->batch_head;
835 spin_lock(&head->batch_lock);
836 list_add_tail(&sh->batch_list, &head->batch_list);
837 spin_unlock(&head->batch_lock);
838 }
839
840 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
841 if (atomic_dec_return(&conf->preread_active_stripes)
842 < IO_THRESHOLD)
843 md_wakeup_thread(conf->mddev->thread);
844
NeilBrown2b6b2452015-05-21 15:10:01 +1000845 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
846 int seq = sh->bm_seq;
847 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
848 sh->batch_head->bm_seq > seq)
849 seq = sh->batch_head->bm_seq;
850 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
851 sh->batch_head->bm_seq = seq;
852 }
853
shli@kernel.org59fc6302014-12-15 12:57:03 +1100854 atomic_inc(&sh->count);
855unlock_out:
856 unlock_two_stripes(head, sh);
857out:
Shaohua Li6d036f72015-08-13 14:31:57 -0700858 raid5_release_stripe(head);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100859}
860
NeilBrown05616be2012-05-21 09:27:00 +1000861/* Determine if 'data_offset' or 'new_data_offset' should be used
862 * in this stripe_head.
863 */
864static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
865{
866 sector_t progress = conf->reshape_progress;
867 /* Need a memory barrier to make sure we see the value
868 * of conf->generation, or ->data_offset that was set before
869 * reshape_progress was updated.
870 */
871 smp_rmb();
872 if (progress == MaxSector)
873 return 0;
874 if (sh->generation == conf->generation - 1)
875 return 0;
876 /* We are in a reshape, and this is a new-generation stripe,
877 * so use new_data_offset.
878 */
879 return 1;
880}
881
Shaohua Liaaf9f122017-03-03 22:06:12 -0800882static void dispatch_bio_list(struct bio_list *tmp)
Shaohua Li765d7042017-01-04 09:33:23 -0800883{
Shaohua Li765d7042017-01-04 09:33:23 -0800884 struct bio *bio;
885
Shaohua Liaaf9f122017-03-03 22:06:12 -0800886 while ((bio = bio_list_pop(tmp)))
Shaohua Li765d7042017-01-04 09:33:23 -0800887 generic_make_request(bio);
888}
889
Shaohua Liaaf9f122017-03-03 22:06:12 -0800890static int cmp_stripe(void *priv, struct list_head *a, struct list_head *b)
Shaohua Li765d7042017-01-04 09:33:23 -0800891{
Shaohua Liaaf9f122017-03-03 22:06:12 -0800892 const struct r5pending_data *da = list_entry(a,
893 struct r5pending_data, sibling);
894 const struct r5pending_data *db = list_entry(b,
895 struct r5pending_data, sibling);
896 if (da->sector > db->sector)
897 return 1;
898 if (da->sector < db->sector)
899 return -1;
900 return 0;
901}
902
903static void dispatch_defer_bios(struct r5conf *conf, int target,
904 struct bio_list *list)
905{
906 struct r5pending_data *data;
907 struct list_head *first, *next = NULL;
908 int cnt = 0;
909
910 if (conf->pending_data_cnt == 0)
Shaohua Li765d7042017-01-04 09:33:23 -0800911 return;
Shaohua Liaaf9f122017-03-03 22:06:12 -0800912
913 list_sort(NULL, &conf->pending_list, cmp_stripe);
914
915 first = conf->pending_list.next;
916
917 /* temporarily move the head */
918 if (conf->next_pending_data)
919 list_move_tail(&conf->pending_list,
920 &conf->next_pending_data->sibling);
921
922 while (!list_empty(&conf->pending_list)) {
923 data = list_first_entry(&conf->pending_list,
924 struct r5pending_data, sibling);
925 if (&data->sibling == first)
926 first = data->sibling.next;
927 next = data->sibling.next;
928
929 bio_list_merge(list, &data->bios);
930 list_move(&data->sibling, &conf->free_list);
931 cnt++;
932 if (cnt >= target)
933 break;
Shaohua Li765d7042017-01-04 09:33:23 -0800934 }
Shaohua Liaaf9f122017-03-03 22:06:12 -0800935 conf->pending_data_cnt -= cnt;
936 BUG_ON(conf->pending_data_cnt < 0 || cnt < target);
937
938 if (next != &conf->pending_list)
939 conf->next_pending_data = list_entry(next,
940 struct r5pending_data, sibling);
941 else
942 conf->next_pending_data = NULL;
943 /* list isn't empty */
944 if (first != &conf->pending_list)
945 list_move_tail(&conf->pending_list, first);
946}
947
948static void flush_deferred_bios(struct r5conf *conf)
949{
950 struct bio_list tmp = BIO_EMPTY_LIST;
951
952 if (conf->pending_data_cnt == 0)
953 return;
954
Shaohua Li765d7042017-01-04 09:33:23 -0800955 spin_lock(&conf->pending_bios_lock);
Shaohua Liaaf9f122017-03-03 22:06:12 -0800956 dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp);
957 BUG_ON(conf->pending_data_cnt != 0);
Shaohua Li765d7042017-01-04 09:33:23 -0800958 spin_unlock(&conf->pending_bios_lock);
Shaohua Liaaf9f122017-03-03 22:06:12 -0800959
960 dispatch_bio_list(&tmp);
961}
962
963static void defer_issue_bios(struct r5conf *conf, sector_t sector,
964 struct bio_list *bios)
965{
966 struct bio_list tmp = BIO_EMPTY_LIST;
967 struct r5pending_data *ent;
968
969 spin_lock(&conf->pending_bios_lock);
970 ent = list_first_entry(&conf->free_list, struct r5pending_data,
971 sibling);
972 list_move_tail(&ent->sibling, &conf->pending_list);
973 ent->sector = sector;
974 bio_list_init(&ent->bios);
975 bio_list_merge(&ent->bios, bios);
976 conf->pending_data_cnt++;
977 if (conf->pending_data_cnt >= PENDING_IO_MAX)
978 dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp);
979
980 spin_unlock(&conf->pending_bios_lock);
981
982 dispatch_bio_list(&tmp);
Shaohua Li765d7042017-01-04 09:33:23 -0800983}
984
NeilBrown6712ecf2007-09-27 12:47:43 +0200985static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200986raid5_end_read_request(struct bio *bi);
NeilBrown6712ecf2007-09-27 12:47:43 +0200987static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200988raid5_end_write_request(struct bio *bi);
Dan Williams91c00922007-01-02 13:52:30 -0700989
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000990static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700991{
NeilBrownd1688a62011-10-11 16:49:52 +1100992 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700993 int i, disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100994 struct stripe_head *head_sh = sh;
Shaohua Liaaf9f122017-03-03 22:06:12 -0800995 struct bio_list pending_bios = BIO_EMPTY_LIST;
996 bool should_defer;
Dan Williams91c00922007-01-02 13:52:30 -0700997
998 might_sleep();
999
Song Liu1e6d6902016-11-17 15:24:39 -08001000 if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) {
1001 /* writing out phase */
Song Liud7bd3982016-11-23 22:50:39 -08001002 if (s->waiting_extra_page)
1003 return;
Song Liu1e6d6902016-11-17 15:24:39 -08001004 if (r5l_write_stripe(conf->log, sh) == 0)
1005 return;
1006 } else { /* caching phase */
1007 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) {
1008 r5c_cache_data(conf->log, sh, s);
1009 return;
1010 }
1011 }
1012
Shaohua Liaaf9f122017-03-03 22:06:12 -08001013 should_defer = conf->batch_bio_dispatch && conf->group_cnt;
1014
Dan Williams91c00922007-01-02 13:52:30 -07001015 for (i = disks; i--; ) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001016 int op, op_flags = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11001017 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +11001018 struct bio *bi, *rbi;
1019 struct md_rdev *rdev, *rrdev = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001020
1021 sh = head_sh;
Tejun Heoe9c74692010-09-03 11:56:18 +02001022 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001023 op = REQ_OP_WRITE;
Tejun Heoe9c74692010-09-03 11:56:18 +02001024 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001025 op_flags = REQ_FUA;
Shaohua Li9e4447682012-10-11 13:49:49 +11001026 if (test_bit(R5_Discard, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001027 op = REQ_OP_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +02001028 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001029 op = REQ_OP_READ;
NeilBrown9a3e1102011-12-23 10:17:53 +11001030 else if (test_and_clear_bit(R5_WantReplace,
1031 &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001032 op = REQ_OP_WRITE;
NeilBrown9a3e1102011-12-23 10:17:53 +11001033 replace_only = 1;
1034 } else
Dan Williams91c00922007-01-02 13:52:30 -07001035 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +10001036 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001037 op_flags |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -07001038
shli@kernel.org59fc6302014-12-15 12:57:03 +11001039again:
Dan Williams91c00922007-01-02 13:52:30 -07001040 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +11001041 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -07001042
Dan Williams91c00922007-01-02 13:52:30 -07001043 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +11001044 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +11001045 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
1046 rdev = rcu_dereference(conf->disks[i].rdev);
1047 if (!rdev) {
1048 rdev = rrdev;
1049 rrdev = NULL;
1050 }
Mike Christie796a5cf2016-06-05 14:32:07 -05001051 if (op_is_write(op)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001052 if (replace_only)
1053 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +11001054 if (rdev == rrdev)
1055 /* We raced and saw duplicates */
1056 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +11001057 } else {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001058 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +11001059 rdev = rrdev;
1060 rrdev = NULL;
1061 }
NeilBrown977df362011-12-23 10:17:53 +11001062
Dan Williams91c00922007-01-02 13:52:30 -07001063 if (rdev && test_bit(Faulty, &rdev->flags))
1064 rdev = NULL;
1065 if (rdev)
1066 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +11001067 if (rrdev && test_bit(Faulty, &rrdev->flags))
1068 rrdev = NULL;
1069 if (rrdev)
1070 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -07001071 rcu_read_unlock();
1072
NeilBrown73e92e52011-07-28 11:39:22 +10001073 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +11001074 * need to check for writes. We never accept write errors
1075 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +10001076 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001077 while (op_is_write(op) && rdev &&
NeilBrown73e92e52011-07-28 11:39:22 +10001078 test_bit(WriteErrorSeen, &rdev->flags)) {
1079 sector_t first_bad;
1080 int bad_sectors;
1081 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
1082 &first_bad, &bad_sectors);
1083 if (!bad)
1084 break;
1085
1086 if (bad < 0) {
1087 set_bit(BlockedBadBlocks, &rdev->flags);
1088 if (!conf->mddev->external &&
Shaohua Li29530792016-12-08 15:48:19 -08001089 conf->mddev->sb_flags) {
NeilBrown73e92e52011-07-28 11:39:22 +10001090 /* It is very unlikely, but we might
1091 * still need to write out the
1092 * bad block log - better give it
1093 * a chance*/
1094 md_check_recovery(conf->mddev);
1095 }
majianpeng18507532012-07-03 12:11:54 +10001096 /*
1097 * Because md_wait_for_blocked_rdev
1098 * will dec nr_pending, we must
1099 * increment it first.
1100 */
1101 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +10001102 md_wait_for_blocked_rdev(rdev, conf->mddev);
1103 } else {
1104 /* Acknowledged bad block - skip the write */
1105 rdev_dec_pending(rdev, conf->mddev);
1106 rdev = NULL;
1107 }
1108 }
1109
Dan Williams91c00922007-01-02 13:52:30 -07001110 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001111 if (s->syncing || s->expanding || s->expanded
1112 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -07001113 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1114
Dan Williams2b7497f2008-06-28 08:31:52 +10001115 set_bit(STRIPE_IO_STARTED, &sh->state);
1116
Dan Williams91c00922007-01-02 13:52:30 -07001117 bi->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05001118 bio_set_op_attrs(bi, op, op_flags);
1119 bi->bi_end_io = op_is_write(op)
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001120 ? raid5_end_write_request
1121 : raid5_end_read_request;
1122 bi->bi_private = sh;
1123
Mike Christie6296b962016-06-05 14:32:21 -05001124 pr_debug("%s: for %llu schedule op %d on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001125 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001126 bi->bi_opf, i);
Dan Williams91c00922007-01-02 13:52:30 -07001127 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001128 if (sh != head_sh)
1129 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001130 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001131 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001132 + rdev->new_data_offset);
1133 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001134 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001135 + rdev->data_offset);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001136 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
Jens Axboe1eff9d32016-08-05 15:35:16 -06001137 bi->bi_opf |= REQ_NOMERGE;
majianpeng3f9e7c12012-07-31 10:04:21 +10001138
Shaohua Lid592a992014-05-21 17:57:44 +08001139 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1140 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
Song Liu86aa1392017-01-12 17:22:41 -08001141
1142 if (!op_is_write(op) &&
1143 test_bit(R5_InJournal, &sh->dev[i].flags))
1144 /*
1145 * issuing read for a page in journal, this
1146 * must be preparing for prexor in rmw; read
1147 * the data into orig_page
1148 */
1149 sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
1150 else
1151 sh->dev[i].vec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001152 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001153 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1154 bi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001155 bi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001156 /*
1157 * If this is discard request, set bi_vcnt 0. We don't
1158 * want to confuse SCSI because SCSI will replace payload
1159 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001160 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001161 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +11001162 if (rrdev)
1163 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001164
1165 if (conf->mddev->gendisk)
1166 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
1167 bi, disk_devt(conf->mddev->gendisk),
1168 sh->dev[i].sector);
Shaohua Liaaf9f122017-03-03 22:06:12 -08001169 if (should_defer && op_is_write(op))
1170 bio_list_add(&pending_bios, bi);
1171 else
1172 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +11001173 }
1174 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001175 if (s->syncing || s->expanding || s->expanded
1176 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +11001177 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
1178
1179 set_bit(STRIPE_IO_STARTED, &sh->state);
1180
1181 rbi->bi_bdev = rrdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05001182 bio_set_op_attrs(rbi, op, op_flags);
1183 BUG_ON(!op_is_write(op));
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001184 rbi->bi_end_io = raid5_end_write_request;
1185 rbi->bi_private = sh;
1186
Mike Christie6296b962016-06-05 14:32:21 -05001187 pr_debug("%s: for %llu schedule op %d on "
NeilBrown977df362011-12-23 10:17:53 +11001188 "replacement disc %d\n",
1189 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001190 rbi->bi_opf, i);
NeilBrown977df362011-12-23 10:17:53 +11001191 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001192 if (sh != head_sh)
1193 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001194 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001195 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001196 + rrdev->new_data_offset);
1197 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001198 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001199 + rrdev->data_offset);
Shaohua Lid592a992014-05-21 17:57:44 +08001200 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1201 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1202 sh->dev[i].rvec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001203 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +11001204 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1205 rbi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001206 rbi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001207 /*
1208 * If this is discard request, set bi_vcnt 0. We don't
1209 * want to confuse SCSI because SCSI will replace payload
1210 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001211 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001212 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001213 if (conf->mddev->gendisk)
1214 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
1215 rbi, disk_devt(conf->mddev->gendisk),
1216 sh->dev[i].sector);
Shaohua Liaaf9f122017-03-03 22:06:12 -08001217 if (should_defer && op_is_write(op))
1218 bio_list_add(&pending_bios, rbi);
1219 else
1220 generic_make_request(rbi);
NeilBrown977df362011-12-23 10:17:53 +11001221 }
1222 if (!rdev && !rrdev) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001223 if (op_is_write(op))
Dan Williams91c00922007-01-02 13:52:30 -07001224 set_bit(STRIPE_DEGRADED, &sh->state);
Mike Christie6296b962016-06-05 14:32:21 -05001225 pr_debug("skip op %d on disc %d for sector %llu\n",
Jens Axboe1eff9d32016-08-05 15:35:16 -06001226 bi->bi_opf, i, (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001227 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1228 set_bit(STRIPE_HANDLE, &sh->state);
1229 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001230
1231 if (!head_sh->batch_head)
1232 continue;
1233 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1234 batch_list);
1235 if (sh != head_sh)
1236 goto again;
Dan Williams91c00922007-01-02 13:52:30 -07001237 }
Shaohua Liaaf9f122017-03-03 22:06:12 -08001238
1239 if (should_defer && !bio_list_empty(&pending_bios))
1240 defer_issue_bios(conf, head_sh->sector, &pending_bios);
Dan Williams91c00922007-01-02 13:52:30 -07001241}
1242
1243static struct dma_async_tx_descriptor *
Shaohua Lid592a992014-05-21 17:57:44 +08001244async_copy_data(int frombio, struct bio *bio, struct page **page,
1245 sector_t sector, struct dma_async_tx_descriptor *tx,
Song Liu1e6d6902016-11-17 15:24:39 -08001246 struct stripe_head *sh, int no_skipcopy)
Dan Williams91c00922007-01-02 13:52:30 -07001247{
Kent Overstreet79886132013-11-23 17:19:00 -08001248 struct bio_vec bvl;
1249 struct bvec_iter iter;
Dan Williams91c00922007-01-02 13:52:30 -07001250 struct page *bio_page;
Dan Williams91c00922007-01-02 13:52:30 -07001251 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -07001252 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -07001253 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001254
Kent Overstreet4f024f32013-10-11 15:44:27 -07001255 if (bio->bi_iter.bi_sector >= sector)
1256 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
Dan Williams91c00922007-01-02 13:52:30 -07001257 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001258 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -07001259
Dan Williams0403e382009-09-08 17:42:50 -07001260 if (frombio)
1261 flags |= ASYNC_TX_FENCE;
1262 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1263
Kent Overstreet79886132013-11-23 17:19:00 -08001264 bio_for_each_segment(bvl, bio, iter) {
1265 int len = bvl.bv_len;
Dan Williams91c00922007-01-02 13:52:30 -07001266 int clen;
1267 int b_offset = 0;
1268
1269 if (page_offset < 0) {
1270 b_offset = -page_offset;
1271 page_offset += b_offset;
1272 len -= b_offset;
1273 }
1274
1275 if (len > 0 && page_offset + len > STRIPE_SIZE)
1276 clen = STRIPE_SIZE - page_offset;
1277 else
1278 clen = len;
1279
1280 if (clen > 0) {
Kent Overstreet79886132013-11-23 17:19:00 -08001281 b_offset += bvl.bv_offset;
1282 bio_page = bvl.bv_page;
Shaohua Lid592a992014-05-21 17:57:44 +08001283 if (frombio) {
1284 if (sh->raid_conf->skip_copy &&
1285 b_offset == 0 && page_offset == 0 &&
Song Liu1e6d6902016-11-17 15:24:39 -08001286 clen == STRIPE_SIZE &&
1287 !no_skipcopy)
Shaohua Lid592a992014-05-21 17:57:44 +08001288 *page = bio_page;
1289 else
1290 tx = async_memcpy(*page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001291 b_offset, clen, &submit);
Shaohua Lid592a992014-05-21 17:57:44 +08001292 } else
1293 tx = async_memcpy(bio_page, *page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001294 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001295 }
Dan Williamsa08abd82009-06-03 11:43:59 -07001296 /* chain the operations */
1297 submit.depend_tx = tx;
1298
Dan Williams91c00922007-01-02 13:52:30 -07001299 if (clen < len) /* hit end of page */
1300 break;
1301 page_offset += len;
1302 }
1303
1304 return tx;
1305}
1306
1307static void ops_complete_biofill(void *stripe_head_ref)
1308{
1309 struct stripe_head *sh = stripe_head_ref;
NeilBrown34a6f802015-08-14 12:07:57 +10001310 struct bio_list return_bi = BIO_EMPTY_LIST;
Dan Williamse4d84902007-09-24 10:06:13 -07001311 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001312
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001313 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001314 (unsigned long long)sh->sector);
1315
1316 /* clear completed biofills */
1317 for (i = sh->disks; i--; ) {
1318 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001319
1320 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001321 /* and check if we need to reply to a read request,
1322 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001323 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001324 */
1325 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001326 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001327
Dan Williams91c00922007-01-02 13:52:30 -07001328 BUG_ON(!dev->read);
1329 rbi = dev->read;
1330 dev->read = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001331 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001332 dev->sector + STRIPE_SECTORS) {
1333 rbi2 = r5_next_bio(rbi, dev->sector);
NeilBrown34a6f802015-08-14 12:07:57 +10001334 if (!raid5_dec_bi_active_stripes(rbi))
1335 bio_list_add(&return_bi, rbi);
Dan Williams91c00922007-01-02 13:52:30 -07001336 rbi = rbi2;
1337 }
1338 }
1339 }
Dan Williams83de75c2008-06-28 08:31:58 +10001340 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001341
NeilBrown34a6f802015-08-14 12:07:57 +10001342 return_io(&return_bi);
Dan Williams91c00922007-01-02 13:52:30 -07001343
Dan Williamse4d84902007-09-24 10:06:13 -07001344 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001345 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001346}
1347
1348static void ops_run_biofill(struct stripe_head *sh)
1349{
1350 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001351 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001352 int i;
1353
shli@kernel.org59fc6302014-12-15 12:57:03 +11001354 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001355 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001356 (unsigned long long)sh->sector);
1357
1358 for (i = sh->disks; i--; ) {
1359 struct r5dev *dev = &sh->dev[i];
1360 if (test_bit(R5_Wantfill, &dev->flags)) {
1361 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001362 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001363 dev->read = rbi = dev->toread;
1364 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001365 spin_unlock_irq(&sh->stripe_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001366 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001367 dev->sector + STRIPE_SECTORS) {
Shaohua Lid592a992014-05-21 17:57:44 +08001368 tx = async_copy_data(0, rbi, &dev->page,
Song Liu1e6d6902016-11-17 15:24:39 -08001369 dev->sector, tx, sh, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001370 rbi = r5_next_bio(rbi, dev->sector);
1371 }
1372 }
1373 }
1374
1375 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001376 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1377 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001378}
1379
Dan Williams4e7d2c02009-08-29 19:13:11 -07001380static void mark_target_uptodate(struct stripe_head *sh, int target)
1381{
1382 struct r5dev *tgt;
1383
1384 if (target < 0)
1385 return;
1386
1387 tgt = &sh->dev[target];
1388 set_bit(R5_UPTODATE, &tgt->flags);
1389 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1390 clear_bit(R5_Wantcompute, &tgt->flags);
1391}
1392
Dan Williamsac6b53b2009-07-14 13:40:19 -07001393static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001394{
1395 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001396
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001397 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001398 (unsigned long long)sh->sector);
1399
Dan Williamsac6b53b2009-07-14 13:40:19 -07001400 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001401 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001402 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001403
Dan Williamsecc65c92008-06-28 08:31:57 +10001404 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1405 if (sh->check_state == check_state_compute_run)
1406 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001407 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001408 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001409}
1410
Dan Williamsd6f38f32009-07-14 11:50:52 -07001411/* return a pointer to the address conversion region of the scribble buffer */
1412static addr_conv_t *to_addr_conv(struct stripe_head *sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001413 struct raid5_percpu *percpu, int i)
Dan Williams91c00922007-01-02 13:52:30 -07001414{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001415 void *addr;
1416
1417 addr = flex_array_get(percpu->scribble, i);
1418 return addr + sizeof(struct page *) * (sh->disks + 2);
1419}
1420
1421/* return a pointer to the address conversion region of the scribble buffer */
1422static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
1423{
1424 void *addr;
1425
1426 addr = flex_array_get(percpu->scribble, i);
1427 return addr;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001428}
1429
1430static struct dma_async_tx_descriptor *
1431ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1432{
Dan Williams91c00922007-01-02 13:52:30 -07001433 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001434 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001435 int target = sh->ops.target;
1436 struct r5dev *tgt = &sh->dev[target];
1437 struct page *xor_dest = tgt->page;
1438 int count = 0;
1439 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001440 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001441 int i;
1442
shli@kernel.org59fc6302014-12-15 12:57:03 +11001443 BUG_ON(sh->batch_head);
1444
Dan Williams91c00922007-01-02 13:52:30 -07001445 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001446 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001447 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1448
1449 for (i = disks; i--; )
1450 if (i != target)
1451 xor_srcs[count++] = sh->dev[i].page;
1452
1453 atomic_inc(&sh->count);
1454
Dan Williams0403e382009-09-08 17:42:50 -07001455 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001456 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
Dan Williams91c00922007-01-02 13:52:30 -07001457 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -07001458 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001459 else
Dan Williamsa08abd82009-06-03 11:43:59 -07001460 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001461
Dan Williams91c00922007-01-02 13:52:30 -07001462 return tx;
1463}
1464
Dan Williamsac6b53b2009-07-14 13:40:19 -07001465/* set_syndrome_sources - populate source buffers for gen_syndrome
1466 * @srcs - (struct page *) array of size sh->disks
1467 * @sh - stripe_head to parse
1468 *
1469 * Populates srcs in proper layout order for the stripe and returns the
1470 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1471 * destination buffer is recorded in srcs[count] and the Q destination
1472 * is recorded in srcs[count+1]].
1473 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001474static int set_syndrome_sources(struct page **srcs,
1475 struct stripe_head *sh,
1476 int srctype)
Dan Williamsac6b53b2009-07-14 13:40:19 -07001477{
1478 int disks = sh->disks;
1479 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1480 int d0_idx = raid6_d0(sh);
1481 int count;
1482 int i;
1483
1484 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001485 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001486
1487 count = 0;
1488 i = d0_idx;
1489 do {
1490 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001491 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001492
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001493 if (i == sh->qd_idx || i == sh->pd_idx ||
1494 (srctype == SYNDROME_SRC_ALL) ||
1495 (srctype == SYNDROME_SRC_WANT_DRAIN &&
Song Liu1e6d6902016-11-17 15:24:39 -08001496 (test_bit(R5_Wantdrain, &dev->flags) ||
1497 test_bit(R5_InJournal, &dev->flags))) ||
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001498 (srctype == SYNDROME_SRC_WRITTEN &&
Song Liu09777622017-03-13 13:44:35 -07001499 (dev->written ||
1500 test_bit(R5_InJournal, &dev->flags)))) {
Song Liu1e6d6902016-11-17 15:24:39 -08001501 if (test_bit(R5_InJournal, &dev->flags))
1502 srcs[slot] = sh->dev[i].orig_page;
1503 else
1504 srcs[slot] = sh->dev[i].page;
1505 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001506 i = raid6_next_disk(i, disks);
1507 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001508
NeilBrowne4424fe2009-10-16 16:27:34 +11001509 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001510}
1511
1512static struct dma_async_tx_descriptor *
1513ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1514{
1515 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001516 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001517 int target;
1518 int qd_idx = sh->qd_idx;
1519 struct dma_async_tx_descriptor *tx;
1520 struct async_submit_ctl submit;
1521 struct r5dev *tgt;
1522 struct page *dest;
1523 int i;
1524 int count;
1525
shli@kernel.org59fc6302014-12-15 12:57:03 +11001526 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001527 if (sh->ops.target < 0)
1528 target = sh->ops.target2;
1529 else if (sh->ops.target2 < 0)
1530 target = sh->ops.target;
1531 else
1532 /* we should only have one valid target */
1533 BUG();
1534 BUG_ON(target < 0);
1535 pr_debug("%s: stripe %llu block: %d\n",
1536 __func__, (unsigned long long)sh->sector, target);
1537
1538 tgt = &sh->dev[target];
1539 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1540 dest = tgt->page;
1541
1542 atomic_inc(&sh->count);
1543
1544 if (target == qd_idx) {
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001545 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001546 blocks[count] = NULL; /* regenerating p is not necessary */
1547 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001548 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1549 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001550 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001551 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1552 } else {
1553 /* Compute any data- or p-drive using XOR */
1554 count = 0;
1555 for (i = disks; i-- ; ) {
1556 if (i == target || i == qd_idx)
1557 continue;
1558 blocks[count++] = sh->dev[i].page;
1559 }
1560
Dan Williams0403e382009-09-08 17:42:50 -07001561 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1562 NULL, ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001563 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001564 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1565 }
1566
1567 return tx;
1568}
1569
1570static struct dma_async_tx_descriptor *
1571ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1572{
1573 int i, count, disks = sh->disks;
1574 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1575 int d0_idx = raid6_d0(sh);
1576 int faila = -1, failb = -1;
1577 int target = sh->ops.target;
1578 int target2 = sh->ops.target2;
1579 struct r5dev *tgt = &sh->dev[target];
1580 struct r5dev *tgt2 = &sh->dev[target2];
1581 struct dma_async_tx_descriptor *tx;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001582 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001583 struct async_submit_ctl submit;
1584
shli@kernel.org59fc6302014-12-15 12:57:03 +11001585 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001586 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1587 __func__, (unsigned long long)sh->sector, target, target2);
1588 BUG_ON(target < 0 || target2 < 0);
1589 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1590 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1591
Dan Williams6c910a72009-09-16 12:24:54 -07001592 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001593 * slot number conversion for 'faila' and 'failb'
1594 */
1595 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001596 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001597 count = 0;
1598 i = d0_idx;
1599 do {
1600 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1601
1602 blocks[slot] = sh->dev[i].page;
1603
1604 if (i == target)
1605 faila = slot;
1606 if (i == target2)
1607 failb = slot;
1608 i = raid6_next_disk(i, disks);
1609 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001610
1611 BUG_ON(faila == failb);
1612 if (failb < faila)
1613 swap(faila, failb);
1614 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1615 __func__, (unsigned long long)sh->sector, faila, failb);
1616
1617 atomic_inc(&sh->count);
1618
1619 if (failb == syndrome_disks+1) {
1620 /* Q disk is one of the missing disks */
1621 if (faila == syndrome_disks) {
1622 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001623 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1624 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001625 to_addr_conv(sh, percpu, 0));
NeilBrowne4424fe2009-10-16 16:27:34 +11001626 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001627 STRIPE_SIZE, &submit);
1628 } else {
1629 struct page *dest;
1630 int data_target;
1631 int qd_idx = sh->qd_idx;
1632
1633 /* Missing D+Q: recompute D from P, then recompute Q */
1634 if (target == qd_idx)
1635 data_target = target2;
1636 else
1637 data_target = target;
1638
1639 count = 0;
1640 for (i = disks; i-- ; ) {
1641 if (i == data_target || i == qd_idx)
1642 continue;
1643 blocks[count++] = sh->dev[i].page;
1644 }
1645 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001646 init_async_submit(&submit,
1647 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1648 NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001649 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001650 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1651 &submit);
1652
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001653 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williams0403e382009-09-08 17:42:50 -07001654 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1655 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001656 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001657 return async_gen_syndrome(blocks, 0, count+2,
1658 STRIPE_SIZE, &submit);
1659 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001660 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001661 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1662 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001663 to_addr_conv(sh, percpu, 0));
Dan Williams6c910a72009-09-16 12:24:54 -07001664 if (failb == syndrome_disks) {
1665 /* We're missing D+P. */
1666 return async_raid6_datap_recov(syndrome_disks+2,
1667 STRIPE_SIZE, faila,
1668 blocks, &submit);
1669 } else {
1670 /* We're missing D+D. */
1671 return async_raid6_2data_recov(syndrome_disks+2,
1672 STRIPE_SIZE, faila, failb,
1673 blocks, &submit);
1674 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001675 }
1676}
1677
Dan Williams91c00922007-01-02 13:52:30 -07001678static void ops_complete_prexor(void *stripe_head_ref)
1679{
1680 struct stripe_head *sh = stripe_head_ref;
1681
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001682 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001683 (unsigned long long)sh->sector);
Song Liu1e6d6902016-11-17 15:24:39 -08001684
1685 if (r5c_is_writeback(sh->raid_conf->log))
1686 /*
1687 * raid5-cache write back uses orig_page during prexor.
1688 * After prexor, it is time to free orig_page
1689 */
1690 r5c_release_extra_page(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001691}
1692
1693static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001694ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1695 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001696{
Dan Williams91c00922007-01-02 13:52:30 -07001697 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001698 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001699 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001700 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001701
1702 /* existing parity data subtracted */
1703 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1704
shli@kernel.org59fc6302014-12-15 12:57:03 +11001705 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001706 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001707 (unsigned long long)sh->sector);
1708
1709 for (i = disks; i--; ) {
1710 struct r5dev *dev = &sh->dev[i];
1711 /* Only process blocks that are known to be uptodate */
Song Liu1e6d6902016-11-17 15:24:39 -08001712 if (test_bit(R5_InJournal, &dev->flags))
1713 xor_srcs[count++] = dev->orig_page;
1714 else if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001715 xor_srcs[count++] = dev->page;
1716 }
1717
Dan Williams0403e382009-09-08 17:42:50 -07001718 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001719 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Dan Williamsa08abd82009-06-03 11:43:59 -07001720 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001721
1722 return tx;
1723}
1724
1725static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001726ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1727 struct dma_async_tx_descriptor *tx)
1728{
1729 struct page **blocks = to_addr_page(percpu, 0);
1730 int count;
1731 struct async_submit_ctl submit;
1732
1733 pr_debug("%s: stripe %llu\n", __func__,
1734 (unsigned long long)sh->sector);
1735
1736 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN);
1737
1738 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1739 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1740 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1741
1742 return tx;
1743}
1744
1745static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001746ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001747{
Song Liu1e6d6902016-11-17 15:24:39 -08001748 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001749 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001750 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001751 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -07001752
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001753 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001754 (unsigned long long)sh->sector);
1755
1756 for (i = disks; i--; ) {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001757 struct r5dev *dev;
Dan Williams91c00922007-01-02 13:52:30 -07001758 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001759
shli@kernel.org59fc6302014-12-15 12:57:03 +11001760 sh = head_sh;
1761 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001762 struct bio *wbi;
1763
shli@kernel.org59fc6302014-12-15 12:57:03 +11001764again:
1765 dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001766 /*
1767 * clear R5_InJournal, so when rewriting a page in
1768 * journal, it is not skipped by r5l_log_stripe()
1769 */
1770 clear_bit(R5_InJournal, &dev->flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10001771 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001772 chosen = dev->towrite;
1773 dev->towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11001774 sh->overwrite_disks = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001775 BUG_ON(dev->written);
1776 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001777 spin_unlock_irq(&sh->stripe_lock);
Shaohua Lid592a992014-05-21 17:57:44 +08001778 WARN_ON(dev->page != dev->orig_page);
Dan Williams91c00922007-01-02 13:52:30 -07001779
Kent Overstreet4f024f32013-10-11 15:44:27 -07001780 while (wbi && wbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001781 dev->sector + STRIPE_SECTORS) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001782 if (wbi->bi_opf & REQ_FUA)
Tejun Heoe9c74692010-09-03 11:56:18 +02001783 set_bit(R5_WantFUA, &dev->flags);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001784 if (wbi->bi_opf & REQ_SYNC)
Shaohua Libc0934f2012-05-22 13:55:05 +10001785 set_bit(R5_SyncIO, &dev->flags);
Mike Christie796a5cf2016-06-05 14:32:07 -05001786 if (bio_op(wbi) == REQ_OP_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001787 set_bit(R5_Discard, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08001788 else {
1789 tx = async_copy_data(1, wbi, &dev->page,
Song Liu1e6d6902016-11-17 15:24:39 -08001790 dev->sector, tx, sh,
1791 r5c_is_writeback(conf->log));
1792 if (dev->page != dev->orig_page &&
1793 !r5c_is_writeback(conf->log)) {
Shaohua Lid592a992014-05-21 17:57:44 +08001794 set_bit(R5_SkipCopy, &dev->flags);
1795 clear_bit(R5_UPTODATE, &dev->flags);
1796 clear_bit(R5_OVERWRITE, &dev->flags);
1797 }
1798 }
Dan Williams91c00922007-01-02 13:52:30 -07001799 wbi = r5_next_bio(wbi, dev->sector);
1800 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001801
1802 if (head_sh->batch_head) {
1803 sh = list_first_entry(&sh->batch_list,
1804 struct stripe_head,
1805 batch_list);
1806 if (sh == head_sh)
1807 continue;
1808 goto again;
1809 }
Dan Williams91c00922007-01-02 13:52:30 -07001810 }
1811 }
1812
1813 return tx;
1814}
1815
Dan Williamsac6b53b2009-07-14 13:40:19 -07001816static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001817{
1818 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001819 int disks = sh->disks;
1820 int pd_idx = sh->pd_idx;
1821 int qd_idx = sh->qd_idx;
1822 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001823 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001824
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001825 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001826 (unsigned long long)sh->sector);
1827
Shaohua Libc0934f2012-05-22 13:55:05 +10001828 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001829 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001830 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001831 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001832 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001833
Dan Williams91c00922007-01-02 13:52:30 -07001834 for (i = disks; i--; ) {
1835 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001836
Tejun Heoe9c74692010-09-03 11:56:18 +02001837 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Lid592a992014-05-21 17:57:44 +08001838 if (!discard && !test_bit(R5_SkipCopy, &dev->flags))
Shaohua Li9e4447682012-10-11 13:49:49 +11001839 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001840 if (fua)
1841 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001842 if (sync)
1843 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001844 }
Dan Williams91c00922007-01-02 13:52:30 -07001845 }
1846
Dan Williamsd8ee0722008-06-28 08:32:06 +10001847 if (sh->reconstruct_state == reconstruct_state_drain_run)
1848 sh->reconstruct_state = reconstruct_state_drain_result;
1849 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1850 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1851 else {
1852 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1853 sh->reconstruct_state = reconstruct_state_result;
1854 }
Dan Williams91c00922007-01-02 13:52:30 -07001855
1856 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001857 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001858}
1859
1860static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001861ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1862 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001863{
Dan Williams91c00922007-01-02 13:52:30 -07001864 int disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001865 struct page **xor_srcs;
Dan Williamsa08abd82009-06-03 11:43:59 -07001866 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001867 int count, pd_idx = sh->pd_idx, i;
Dan Williams91c00922007-01-02 13:52:30 -07001868 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001869 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001870 unsigned long flags;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001871 int j = 0;
1872 struct stripe_head *head_sh = sh;
1873 int last_stripe;
Dan Williams91c00922007-01-02 13:52:30 -07001874
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001875 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001876 (unsigned long long)sh->sector);
1877
Shaohua Li620125f2012-10-11 13:49:05 +11001878 for (i = 0; i < sh->disks; i++) {
1879 if (pd_idx == i)
1880 continue;
1881 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1882 break;
1883 }
1884 if (i >= sh->disks) {
1885 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001886 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1887 ops_complete_reconstruct(sh);
1888 return;
1889 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001890again:
1891 count = 0;
1892 xor_srcs = to_addr_page(percpu, j);
Dan Williams91c00922007-01-02 13:52:30 -07001893 /* check if prexor is active which means only process blocks
1894 * that are part of a read-modify-write (written)
1895 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001896 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001897 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001898 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1899 for (i = disks; i--; ) {
1900 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001901 if (head_sh->dev[i].written ||
1902 test_bit(R5_InJournal, &head_sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -07001903 xor_srcs[count++] = dev->page;
1904 }
1905 } else {
1906 xor_dest = sh->dev[pd_idx].page;
1907 for (i = disks; i--; ) {
1908 struct r5dev *dev = &sh->dev[i];
1909 if (i != pd_idx)
1910 xor_srcs[count++] = dev->page;
1911 }
1912 }
1913
Dan Williams91c00922007-01-02 13:52:30 -07001914 /* 1/ if we prexor'd then the dest is reused as a source
1915 * 2/ if we did not prexor then we are redoing the parity
1916 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1917 * for the synchronous xor case
1918 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001919 last_stripe = !head_sh->batch_head ||
1920 list_first_entry(&sh->batch_list,
1921 struct stripe_head, batch_list) == head_sh;
1922 if (last_stripe) {
1923 flags = ASYNC_TX_ACK |
1924 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
Dan Williams91c00922007-01-02 13:52:30 -07001925
shli@kernel.org59fc6302014-12-15 12:57:03 +11001926 atomic_inc(&head_sh->count);
1927 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
1928 to_addr_conv(sh, percpu, j));
1929 } else {
1930 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
1931 init_async_submit(&submit, flags, tx, NULL, NULL,
1932 to_addr_conv(sh, percpu, j));
1933 }
Dan Williams91c00922007-01-02 13:52:30 -07001934
Dan Williamsa08abd82009-06-03 11:43:59 -07001935 if (unlikely(count == 1))
1936 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1937 else
1938 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001939 if (!last_stripe) {
1940 j++;
1941 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1942 batch_list);
1943 goto again;
1944 }
Dan Williams91c00922007-01-02 13:52:30 -07001945}
1946
Dan Williamsac6b53b2009-07-14 13:40:19 -07001947static void
1948ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1949 struct dma_async_tx_descriptor *tx)
1950{
1951 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001952 struct page **blocks;
1953 int count, i, j = 0;
1954 struct stripe_head *head_sh = sh;
1955 int last_stripe;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001956 int synflags;
1957 unsigned long txflags;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001958
1959 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1960
Shaohua Li620125f2012-10-11 13:49:05 +11001961 for (i = 0; i < sh->disks; i++) {
1962 if (sh->pd_idx == i || sh->qd_idx == i)
1963 continue;
1964 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1965 break;
1966 }
1967 if (i >= sh->disks) {
1968 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001969 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1970 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1971 ops_complete_reconstruct(sh);
1972 return;
1973 }
1974
shli@kernel.org59fc6302014-12-15 12:57:03 +11001975again:
1976 blocks = to_addr_page(percpu, j);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001977
1978 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1979 synflags = SYNDROME_SRC_WRITTEN;
1980 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
1981 } else {
1982 synflags = SYNDROME_SRC_ALL;
1983 txflags = ASYNC_TX_ACK;
1984 }
1985
1986 count = set_syndrome_sources(blocks, sh, synflags);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001987 last_stripe = !head_sh->batch_head ||
1988 list_first_entry(&sh->batch_list,
1989 struct stripe_head, batch_list) == head_sh;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001990
shli@kernel.org59fc6302014-12-15 12:57:03 +11001991 if (last_stripe) {
1992 atomic_inc(&head_sh->count);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001993 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
shli@kernel.org59fc6302014-12-15 12:57:03 +11001994 head_sh, to_addr_conv(sh, percpu, j));
1995 } else
1996 init_async_submit(&submit, 0, tx, NULL, NULL,
1997 to_addr_conv(sh, percpu, j));
Shaohua Li48769692015-05-13 09:30:08 -07001998 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001999 if (!last_stripe) {
2000 j++;
2001 sh = list_first_entry(&sh->batch_list, struct stripe_head,
2002 batch_list);
2003 goto again;
2004 }
Dan Williams91c00922007-01-02 13:52:30 -07002005}
2006
2007static void ops_complete_check(void *stripe_head_ref)
2008{
2009 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07002010
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002011 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07002012 (unsigned long long)sh->sector);
2013
Dan Williamsecc65c92008-06-28 08:31:57 +10002014 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07002015 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002016 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07002017}
2018
Dan Williamsac6b53b2009-07-14 13:40:19 -07002019static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07002020{
Dan Williams91c00922007-01-02 13:52:30 -07002021 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002022 int pd_idx = sh->pd_idx;
2023 int qd_idx = sh->qd_idx;
2024 struct page *xor_dest;
shli@kernel.org46d5b782014-12-15 12:57:02 +11002025 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07002026 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07002027 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002028 int count;
2029 int i;
Dan Williams91c00922007-01-02 13:52:30 -07002030
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002031 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07002032 (unsigned long long)sh->sector);
2033
shli@kernel.org59fc6302014-12-15 12:57:03 +11002034 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002035 count = 0;
2036 xor_dest = sh->dev[pd_idx].page;
2037 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07002038 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07002039 if (i == pd_idx || i == qd_idx)
2040 continue;
2041 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07002042 }
2043
Dan Williamsd6f38f32009-07-14 11:50:52 -07002044 init_async_submit(&submit, 0, NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11002045 to_addr_conv(sh, percpu, 0));
Dan Williams099f53c2009-04-08 14:28:37 -07002046 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07002047 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07002048
Dan Williams91c00922007-01-02 13:52:30 -07002049 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07002050 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
2051 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07002052}
2053
Dan Williamsac6b53b2009-07-14 13:40:19 -07002054static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
2055{
shli@kernel.org46d5b782014-12-15 12:57:02 +11002056 struct page **srcs = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002057 struct async_submit_ctl submit;
2058 int count;
2059
2060 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
2061 (unsigned long long)sh->sector, checkp);
2062
shli@kernel.org59fc6302014-12-15 12:57:03 +11002063 BUG_ON(sh->batch_head);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002064 count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002065 if (!checkp)
2066 srcs[count] = NULL;
2067
2068 atomic_inc(&sh->count);
2069 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
shli@kernel.org46d5b782014-12-15 12:57:02 +11002070 sh, to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07002071 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
2072 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
2073}
2074
NeilBrown51acbce2013-02-28 09:08:34 +11002075static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07002076{
2077 int overlap_clear = 0, i, disks = sh->disks;
2078 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11002079 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002080 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002081 struct raid5_percpu *percpu;
2082 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07002083
Dan Williamsd6f38f32009-07-14 11:50:52 -07002084 cpu = get_cpu();
2085 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10002086 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07002087 ops_run_biofill(sh);
2088 overlap_clear++;
2089 }
2090
Dan Williams7b3a8712008-06-28 08:32:09 +10002091 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07002092 if (level < 6)
2093 tx = ops_run_compute5(sh, percpu);
2094 else {
2095 if (sh->ops.target2 < 0 || sh->ops.target < 0)
2096 tx = ops_run_compute6_1(sh, percpu);
2097 else
2098 tx = ops_run_compute6_2(sh, percpu);
2099 }
2100 /* terminate the chain if reconstruct is not set to be run */
2101 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10002102 async_tx_ack(tx);
2103 }
Dan Williams91c00922007-01-02 13:52:30 -07002104
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002105 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
2106 if (level < 6)
2107 tx = ops_run_prexor5(sh, percpu, tx);
2108 else
2109 tx = ops_run_prexor6(sh, percpu, tx);
2110 }
Dan Williams91c00922007-01-02 13:52:30 -07002111
Dan Williams600aa102008-06-28 08:32:05 +10002112 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10002113 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07002114 overlap_clear++;
2115 }
2116
Dan Williamsac6b53b2009-07-14 13:40:19 -07002117 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
2118 if (level < 6)
2119 ops_run_reconstruct5(sh, percpu, tx);
2120 else
2121 ops_run_reconstruct6(sh, percpu, tx);
2122 }
Dan Williams91c00922007-01-02 13:52:30 -07002123
Dan Williamsac6b53b2009-07-14 13:40:19 -07002124 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
2125 if (sh->check_state == check_state_run)
2126 ops_run_check_p(sh, percpu);
2127 else if (sh->check_state == check_state_run_q)
2128 ops_run_check_pq(sh, percpu, 0);
2129 else if (sh->check_state == check_state_run_pq)
2130 ops_run_check_pq(sh, percpu, 1);
2131 else
2132 BUG();
2133 }
Dan Williams91c00922007-01-02 13:52:30 -07002134
shli@kernel.org59fc6302014-12-15 12:57:03 +11002135 if (overlap_clear && !sh->batch_head)
Dan Williams91c00922007-01-02 13:52:30 -07002136 for (i = disks; i--; ) {
2137 struct r5dev *dev = &sh->dev[i];
2138 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2139 wake_up(&sh->raid_conf->wait_for_overlap);
2140 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07002141 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07002142}
2143
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002144static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
2145 int disks)
NeilBrownf18c1a32015-05-08 18:19:04 +10002146{
2147 struct stripe_head *sh;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002148 int i;
NeilBrownf18c1a32015-05-08 18:19:04 +10002149
2150 sh = kmem_cache_zalloc(sc, gfp);
2151 if (sh) {
2152 spin_lock_init(&sh->stripe_lock);
2153 spin_lock_init(&sh->batch_lock);
2154 INIT_LIST_HEAD(&sh->batch_list);
2155 INIT_LIST_HEAD(&sh->lru);
Song Liua39f7af2016-11-17 15:24:40 -08002156 INIT_LIST_HEAD(&sh->r5c);
Song Liud7bd3982016-11-23 22:50:39 -08002157 INIT_LIST_HEAD(&sh->log_list);
NeilBrownf18c1a32015-05-08 18:19:04 +10002158 atomic_set(&sh->count, 1);
Song Liua39f7af2016-11-17 15:24:40 -08002159 sh->log_start = MaxSector;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002160 for (i = 0; i < disks; i++) {
2161 struct r5dev *dev = &sh->dev[i];
2162
Ming Lei3a83f462016-11-22 08:57:21 -07002163 bio_init(&dev->req, &dev->vec, 1);
2164 bio_init(&dev->rreq, &dev->rvec, 1);
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002165 }
NeilBrownf18c1a32015-05-08 18:19:04 +10002166 }
2167 return sh;
2168}
NeilBrown486f0642015-02-25 12:10:35 +11002169static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
2171 struct stripe_head *sh;
NeilBrownf18c1a32015-05-08 18:19:04 +10002172
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002173 sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08002174 if (!sh)
2175 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10002176
NeilBrown3f294f42005-11-08 21:39:25 -08002177 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08002178
NeilBrowna9683a72015-02-25 12:02:51 +11002179 if (grow_buffers(sh, gfp)) {
NeilBrowne4e11e32010-06-16 16:45:16 +10002180 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002181 kmem_cache_free(conf->slab_cache, sh);
2182 return 0;
2183 }
NeilBrown486f0642015-02-25 12:10:35 +11002184 sh->hash_lock_index =
2185 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrown3f294f42005-11-08 21:39:25 -08002186 /* we just created an active stripe so... */
NeilBrown3f294f42005-11-08 21:39:25 -08002187 atomic_inc(&conf->active_stripes);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002188
Shaohua Li6d036f72015-08-13 14:31:57 -07002189 raid5_release_stripe(sh);
NeilBrown486f0642015-02-25 12:10:35 +11002190 conf->max_nr_stripes++;
NeilBrown3f294f42005-11-08 21:39:25 -08002191 return 1;
2192}
2193
NeilBrownd1688a62011-10-11 16:49:52 +11002194static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08002195{
Christoph Lametere18b8902006-12-06 20:33:20 -08002196 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11002197 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
NeilBrownf4be6b42010-06-01 19:37:25 +10002199 if (conf->mddev->gendisk)
2200 sprintf(conf->cache_name[0],
2201 "raid%d-%s", conf->level, mdname(conf->mddev));
2202 else
2203 sprintf(conf->cache_name[0],
2204 "raid%d-%p", conf->level, conf->mddev);
2205 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
2206
NeilBrownad01c9e2006-03-27 01:18:07 -08002207 conf->active_name = 0;
2208 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002210 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (!sc)
2212 return 1;
2213 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002214 conf->pool_size = devs;
NeilBrown486f0642015-02-25 12:10:35 +11002215 while (num--)
2216 if (!grow_one_stripe(conf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return 1;
NeilBrown486f0642015-02-25 12:10:35 +11002218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return 0;
2220}
NeilBrown29269552006-03-27 01:18:10 -08002221
Dan Williamsd6f38f32009-07-14 11:50:52 -07002222/**
2223 * scribble_len - return the required size of the scribble region
2224 * @num - total number of disks in the array
2225 *
2226 * The size must be enough to contain:
2227 * 1/ a struct page pointer for each device in the array +2
2228 * 2/ room to convert each entry in (1) to its corresponding dma
2229 * (dma_map_page()) or page (page_address()) address.
2230 *
2231 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2232 * calculate over all devices (not just the data blocks), using zeros in place
2233 * of the P and Q blocks.
2234 */
shli@kernel.org46d5b782014-12-15 12:57:02 +11002235static struct flex_array *scribble_alloc(int num, int cnt, gfp_t flags)
Dan Williamsd6f38f32009-07-14 11:50:52 -07002236{
shli@kernel.org46d5b782014-12-15 12:57:02 +11002237 struct flex_array *ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002238 size_t len;
2239
2240 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
shli@kernel.org46d5b782014-12-15 12:57:02 +11002241 ret = flex_array_alloc(len, cnt, flags);
2242 if (!ret)
2243 return NULL;
2244 /* always prealloc all elements, so no locking is required */
2245 if (flex_array_prealloc(ret, 0, cnt, flags)) {
2246 flex_array_free(ret);
2247 return NULL;
2248 }
2249 return ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002250}
2251
NeilBrown738a2732015-05-08 18:19:39 +10002252static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2253{
2254 unsigned long cpu;
2255 int err = 0;
2256
Shaohua Li27a353c2016-02-24 17:38:28 -08002257 /*
2258 * Never shrink. And mddev_suspend() could deadlock if this is called
2259 * from raid5d. In that case, scribble_disks and scribble_sectors
2260 * should equal to new_disks and new_sectors
2261 */
2262 if (conf->scribble_disks >= new_disks &&
2263 conf->scribble_sectors >= new_sectors)
2264 return 0;
NeilBrown738a2732015-05-08 18:19:39 +10002265 mddev_suspend(conf->mddev);
2266 get_online_cpus();
2267 for_each_present_cpu(cpu) {
2268 struct raid5_percpu *percpu;
2269 struct flex_array *scribble;
2270
2271 percpu = per_cpu_ptr(conf->percpu, cpu);
2272 scribble = scribble_alloc(new_disks,
2273 new_sectors / STRIPE_SECTORS,
2274 GFP_NOIO);
2275
2276 if (scribble) {
2277 flex_array_free(percpu->scribble);
2278 percpu->scribble = scribble;
2279 } else {
2280 err = -ENOMEM;
2281 break;
2282 }
2283 }
2284 put_online_cpus();
2285 mddev_resume(conf->mddev);
Shaohua Li27a353c2016-02-24 17:38:28 -08002286 if (!err) {
2287 conf->scribble_disks = new_disks;
2288 conf->scribble_sectors = new_sectors;
2289 }
NeilBrown738a2732015-05-08 18:19:39 +10002290 return err;
2291}
2292
NeilBrownd1688a62011-10-11 16:49:52 +11002293static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08002294{
2295 /* Make all the stripes able to hold 'newsize' devices.
2296 * New slots in each stripe get 'page' set to a new page.
2297 *
2298 * This happens in stages:
2299 * 1/ create a new kmem_cache and allocate the required number of
2300 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09002301 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08002302 * to the new stripe_heads. This will have the side effect of
2303 * freezing the array as once all stripe_heads have been collected,
2304 * no IO will be possible. Old stripe heads are freed once their
2305 * pages have been transferred over, and the old kmem_cache is
2306 * freed when all stripes are done.
2307 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
2308 * we simple return a failre status - no need to clean anything up.
2309 * 4/ allocate new pages for the new slots in the new stripe_heads.
2310 * If this fails, we don't bother trying the shrink the
2311 * stripe_heads down again, we just leave them as they are.
2312 * As each stripe_head is processed the new one is released into
2313 * active service.
2314 *
2315 * Once step2 is started, we cannot afford to wait for a write,
2316 * so we use GFP_NOIO allocations.
2317 */
2318 struct stripe_head *osh, *nsh;
2319 LIST_HEAD(newstripes);
2320 struct disk_info *ndisks;
Dan Williamsb5470dc2008-06-27 21:44:04 -07002321 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08002322 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002323 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11002324 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08002325
2326 if (newsize <= conf->pool_size)
2327 return 0; /* never bother to shrink */
2328
Dan Williamsb5470dc2008-06-27 21:44:04 -07002329 err = md_allow_write(conf->mddev);
2330 if (err)
2331 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08002332
NeilBrownad01c9e2006-03-27 01:18:07 -08002333 /* Step 1 */
2334 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2335 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002336 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08002337 if (!sc)
2338 return -ENOMEM;
2339
NeilBrown2d5b5692015-07-06 12:49:23 +10002340 /* Need to ensure auto-resizing doesn't interfere */
2341 mutex_lock(&conf->cache_size_mutex);
2342
NeilBrownad01c9e2006-03-27 01:18:07 -08002343 for (i = conf->max_nr_stripes; i; i--) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002344 nsh = alloc_stripe(sc, GFP_KERNEL, newsize);
NeilBrownad01c9e2006-03-27 01:18:07 -08002345 if (!nsh)
2346 break;
2347
NeilBrownad01c9e2006-03-27 01:18:07 -08002348 nsh->raid_conf = conf;
NeilBrownad01c9e2006-03-27 01:18:07 -08002349 list_add(&nsh->lru, &newstripes);
2350 }
2351 if (i) {
2352 /* didn't get enough, give up */
2353 while (!list_empty(&newstripes)) {
2354 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2355 list_del(&nsh->lru);
2356 kmem_cache_free(sc, nsh);
2357 }
2358 kmem_cache_destroy(sc);
NeilBrown2d5b5692015-07-06 12:49:23 +10002359 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002360 return -ENOMEM;
2361 }
2362 /* Step 2 - Must use GFP_NOIO now.
2363 * OK, we have enough stripes, start collecting inactive
2364 * stripes and copying them over
2365 */
Shaohua Li566c09c2013-11-14 15:16:17 +11002366 hash = 0;
2367 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08002368 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11002369 lock_device_hash_lock(conf, hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08002370 wait_event_cmd(conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +11002371 !list_empty(conf->inactive_list + hash),
2372 unlock_device_hash_lock(conf, hash),
2373 lock_device_hash_lock(conf, hash));
2374 osh = get_free_stripe(conf, hash);
2375 unlock_device_hash_lock(conf, hash);
NeilBrownf18c1a32015-05-08 18:19:04 +10002376
Shaohua Lid592a992014-05-21 17:57:44 +08002377 for(i=0; i<conf->pool_size; i++) {
NeilBrownad01c9e2006-03-27 01:18:07 -08002378 nsh->dev[i].page = osh->dev[i].page;
Shaohua Lid592a992014-05-21 17:57:44 +08002379 nsh->dev[i].orig_page = osh->dev[i].page;
2380 }
Shaohua Li566c09c2013-11-14 15:16:17 +11002381 nsh->hash_lock_index = hash;
NeilBrownad01c9e2006-03-27 01:18:07 -08002382 kmem_cache_free(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11002383 cnt++;
2384 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2385 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2386 hash++;
2387 cnt = 0;
2388 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002389 }
2390 kmem_cache_destroy(conf->slab_cache);
2391
2392 /* Step 3.
2393 * At this point, we are holding all the stripes so the array
2394 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07002395 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08002396 */
2397 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
2398 if (ndisks) {
Song Liud7bd3982016-11-23 22:50:39 -08002399 for (i = 0; i < conf->pool_size; i++)
NeilBrownad01c9e2006-03-27 01:18:07 -08002400 ndisks[i] = conf->disks[i];
Song Liud7bd3982016-11-23 22:50:39 -08002401
2402 for (i = conf->pool_size; i < newsize; i++) {
2403 ndisks[i].extra_page = alloc_page(GFP_NOIO);
2404 if (!ndisks[i].extra_page)
2405 err = -ENOMEM;
2406 }
2407
2408 if (err) {
2409 for (i = conf->pool_size; i < newsize; i++)
2410 if (ndisks[i].extra_page)
2411 put_page(ndisks[i].extra_page);
2412 kfree(ndisks);
2413 } else {
2414 kfree(conf->disks);
2415 conf->disks = ndisks;
2416 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002417 } else
2418 err = -ENOMEM;
2419
NeilBrown2d5b5692015-07-06 12:49:23 +10002420 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002421 /* Step 4, return new stripes to service */
2422 while(!list_empty(&newstripes)) {
2423 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2424 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07002425
NeilBrownad01c9e2006-03-27 01:18:07 -08002426 for (i=conf->raid_disks; i < newsize; i++)
2427 if (nsh->dev[i].page == NULL) {
2428 struct page *p = alloc_page(GFP_NOIO);
2429 nsh->dev[i].page = p;
Shaohua Lid592a992014-05-21 17:57:44 +08002430 nsh->dev[i].orig_page = p;
NeilBrownad01c9e2006-03-27 01:18:07 -08002431 if (!p)
2432 err = -ENOMEM;
2433 }
Shaohua Li6d036f72015-08-13 14:31:57 -07002434 raid5_release_stripe(nsh);
NeilBrownad01c9e2006-03-27 01:18:07 -08002435 }
2436 /* critical section pass, GFP_NOIO no longer needed */
2437
2438 conf->slab_cache = sc;
2439 conf->active_name = 1-conf->active_name;
NeilBrown6e9eac22015-05-08 18:19:34 +10002440 if (!err)
2441 conf->pool_size = newsize;
NeilBrownad01c9e2006-03-27 01:18:07 -08002442 return err;
2443}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
NeilBrown486f0642015-02-25 12:10:35 +11002445static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
2447 struct stripe_head *sh;
NeilBrown49895bc2015-08-03 17:09:57 +10002448 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Shaohua Li566c09c2013-11-14 15:16:17 +11002450 spin_lock_irq(conf->hash_locks + hash);
2451 sh = get_free_stripe(conf, hash);
2452 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08002453 if (!sh)
2454 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002455 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10002456 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002457 kmem_cache_free(conf->slab_cache, sh);
2458 atomic_dec(&conf->active_stripes);
NeilBrown486f0642015-02-25 12:10:35 +11002459 conf->max_nr_stripes--;
NeilBrown3f294f42005-11-08 21:39:25 -08002460 return 1;
2461}
2462
NeilBrownd1688a62011-10-11 16:49:52 +11002463static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08002464{
NeilBrown486f0642015-02-25 12:10:35 +11002465 while (conf->max_nr_stripes &&
2466 drop_one_stripe(conf))
2467 ;
NeilBrown3f294f42005-11-08 21:39:25 -08002468
Julia Lawall644df1a2015-09-13 14:15:10 +02002469 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 conf->slab_cache = NULL;
2471}
2472
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002473static void raid5_end_read_request(struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
NeilBrown99c0fb52009-03-31 14:39:38 +11002475 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002476 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002477 int disks = sh->disks, i;
NeilBrownd6950432006-07-10 04:44:20 -07002478 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11002479 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10002480 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
2482 for (i=0 ; i<disks; i++)
2483 if (bi == &sh->dev[i].req)
2484 break;
2485
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002486 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
Dan Williams45b42332007-07-09 11:56:43 -07002487 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002488 bi->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002490 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002492 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
NeilBrown14a75d32011-12-23 10:17:52 +11002494 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11002495 /* If replacement finished while this request was outstanding,
2496 * 'replacement' might be NULL already.
2497 * In that case it moved down to 'rdev'.
2498 * rdev is not removed until all requests are finished.
2499 */
NeilBrown14a75d32011-12-23 10:17:52 +11002500 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002501 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11002502 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
NeilBrown05616be2012-05-21 09:27:00 +10002504 if (use_new_offset(conf, sh))
2505 s = sh->sector + rdev->new_data_offset;
2506 else
2507 s = sh->sector + rdev->data_offset;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002508 if (!bi->bi_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08002510 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11002511 /* Note that this cannot happen on a
2512 * replacement device. We just fail those on
2513 * any error
2514 */
NeilBrowncc6167b2016-11-02 14:16:50 +11002515 pr_info_ratelimited(
2516 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002517 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10002518 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002519 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10002520 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08002521 clear_bit(R5_ReadError, &sh->dev[i].flags);
2522 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002523 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2524 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2525
Song Liu86aa1392017-01-12 17:22:41 -08002526 if (test_bit(R5_InJournal, &sh->dev[i].flags))
2527 /*
2528 * end read for a page in journal, this
2529 * must be preparing for prexor in rmw
2530 */
2531 set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
2532
NeilBrown14a75d32011-12-23 10:17:52 +11002533 if (atomic_read(&rdev->read_errors))
2534 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002536 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002537 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002538 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07002541 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002542 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowncc6167b2016-11-02 14:16:50 +11002543 pr_warn_ratelimited(
2544 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
NeilBrown14a75d32011-12-23 10:17:52 +11002545 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002546 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002547 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002548 else if (conf->mddev->degraded >= conf->max_degraded) {
2549 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002550 pr_warn_ratelimited(
2551 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002552 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002553 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002554 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002555 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002556 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002557 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002558 pr_warn_ratelimited(
2559 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002560 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002561 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002562 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002563 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08002564 > conf->max_nr_stripes)
NeilBrowncc6167b2016-11-02 14:16:50 +11002565 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07002566 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08002567 else
2568 retry = 1;
Bian Yuedfa1f62013-11-14 15:16:17 +11002569 if (set_bad && test_bit(In_sync, &rdev->flags)
2570 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2571 retry = 1;
NeilBrownba22dcb2005-11-08 21:39:31 -08002572 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10002573 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2574 set_bit(R5_ReadError, &sh->dev[i].flags);
2575 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2576 } else
2577 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002578 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002579 clear_bit(R5_ReadError, &sh->dev[i].flags);
2580 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002581 if (!(set_bad
2582 && test_bit(In_sync, &rdev->flags)
2583 && rdev_set_badblocks(
2584 rdev, sh->sector, STRIPE_SECTORS, 0)))
2585 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 }
NeilBrown14a75d32011-12-23 10:17:52 +11002588 rdev_dec_pending(rdev, conf->mddev);
Shaohua Lic9445552016-09-08 10:43:58 -07002589 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2591 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002592 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
2594
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002595static void raid5_end_write_request(struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
NeilBrown99c0fb52009-03-31 14:39:38 +11002597 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002598 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002599 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11002600 struct md_rdev *uninitialized_var(rdev);
NeilBrownb84db562011-07-28 11:39:23 +10002601 sector_t first_bad;
2602 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002603 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
NeilBrown977df362011-12-23 10:17:53 +11002605 for (i = 0 ; i < disks; i++) {
2606 if (bi == &sh->dev[i].req) {
2607 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 break;
NeilBrown977df362011-12-23 10:17:53 +11002609 }
2610 if (bi == &sh->dev[i].rreq) {
2611 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002612 if (rdev)
2613 replacement = 1;
2614 else
2615 /* rdev was removed and 'replacement'
2616 * replaced it. rdev is not removed
2617 * until all requests are finished.
2618 */
2619 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002620 break;
2621 }
2622 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002623 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002625 bi->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002627 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002629 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
2631
NeilBrown977df362011-12-23 10:17:53 +11002632 if (replacement) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002633 if (bi->bi_error)
NeilBrown977df362011-12-23 10:17:53 +11002634 md_error(conf->mddev, rdev);
2635 else if (is_badblock(rdev, sh->sector,
2636 STRIPE_SECTORS,
2637 &first_bad, &bad_sectors))
2638 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2639 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002640 if (bi->bi_error) {
NeilBrown9f97e4b2014-01-16 09:35:38 +11002641 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11002642 set_bit(WriteErrorSeen, &rdev->flags);
2643 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002644 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2645 set_bit(MD_RECOVERY_NEEDED,
2646 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002647 } else if (is_badblock(rdev, sh->sector,
2648 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10002649 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002650 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002651 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2652 /* That was a successful write so make
2653 * sure it looks like we already did
2654 * a re-write.
2655 */
2656 set_bit(R5_ReWrite, &sh->dev[i].flags);
2657 }
NeilBrown977df362011-12-23 10:17:53 +11002658 }
2659 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002661 if (sh->batch_head && bi->bi_error && !replacement)
shli@kernel.org72ac7332014-12-15 12:57:03 +11002662 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2663
Shaohua Lic9445552016-09-08 10:43:58 -07002664 bio_reset(bi);
NeilBrown977df362011-12-23 10:17:53 +11002665 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2666 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002668 raid5_release_stripe(sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002669
2670 if (sh->batch_head && sh != sh->batch_head)
Shaohua Li6d036f72015-08-13 14:31:57 -07002671 raid5_release_stripe(sh->batch_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
2673
NeilBrown784052e2009-03-31 15:19:07 +11002674static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
2676 struct r5dev *dev = &sh->dev[i];
2677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 dev->flags = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07002679 dev->sector = raid5_compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
2681
Shaohua Li849674e2016-01-20 13:52:20 -08002682static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
2684 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002685 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002686 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002687 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
NeilBrown908f4fb2011-12-23 10:17:50 +11002689 spin_lock_irqsave(&conf->device_lock, flags);
2690 clear_bit(In_sync, &rdev->flags);
Song Liu2e38a372017-01-24 10:45:30 -08002691 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11002692 spin_unlock_irqrestore(&conf->device_lock, flags);
2693 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2694
NeilBrownde393cd2011-07-28 11:31:48 +10002695 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10002696 set_bit(Faulty, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08002697 set_mask_bits(&mddev->sb_flags, 0,
2698 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrowncc6167b2016-11-02 14:16:50 +11002699 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
2700 "md/raid:%s: Operation continuing on %d devices.\n",
2701 mdname(mddev),
2702 bdevname(rdev->bdev, b),
2703 mdname(mddev),
2704 conf->raid_disks - mddev->degraded);
Song Liu2e38a372017-01-24 10:45:30 -08002705 r5c_update_on_rdev_error(mddev);
NeilBrown16a53ec2006-06-26 00:27:38 -07002706}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708/*
2709 * Input: a 'big' sector number,
2710 * Output: index of the data and parity disk, and the sector # in them.
2711 */
Shaohua Li6d036f72015-08-13 14:31:57 -07002712sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2713 int previous, int *dd_idx,
2714 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002716 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002717 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002719 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002720 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002722 int algorithm = previous ? conf->prev_algo
2723 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002724 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2725 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002726 int raid_disks = previous ? conf->previous_raid_disks
2727 : conf->raid_disks;
2728 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
2730 /* First compute the information on this sector */
2731
2732 /*
2733 * Compute the chunk number and the sector offset inside the chunk
2734 */
2735 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2736 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 /*
2739 * Compute the stripe number
2740 */
NeilBrown35f2a592010-04-20 14:13:34 +10002741 stripe = chunk_number;
2742 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002743 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 /*
2745 * Select the parity disk based on the user selected algorithm.
2746 */
NeilBrown84789552011-07-27 11:00:36 +10002747 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002748 switch(conf->level) {
2749 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002750 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002751 break;
2752 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002753 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002755 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002756 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 (*dd_idx)++;
2758 break;
2759 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002760 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002761 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 (*dd_idx)++;
2763 break;
2764 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002765 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002766 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 break;
2768 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002769 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002770 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002772 case ALGORITHM_PARITY_0:
2773 pd_idx = 0;
2774 (*dd_idx)++;
2775 break;
2776 case ALGORITHM_PARITY_N:
2777 pd_idx = data_disks;
2778 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002780 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002781 }
2782 break;
2783 case 6:
2784
NeilBrowne183eae2009-03-31 15:20:22 +11002785 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002786 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002787 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002788 qd_idx = pd_idx + 1;
2789 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002790 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002791 qd_idx = 0;
2792 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002793 (*dd_idx) += 2; /* D D P Q D */
2794 break;
2795 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002796 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002797 qd_idx = pd_idx + 1;
2798 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002799 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002800 qd_idx = 0;
2801 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002802 (*dd_idx) += 2; /* D D P Q D */
2803 break;
2804 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002805 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002806 qd_idx = (pd_idx + 1) % raid_disks;
2807 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002808 break;
2809 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002810 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002811 qd_idx = (pd_idx + 1) % raid_disks;
2812 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002813 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002814
2815 case ALGORITHM_PARITY_0:
2816 pd_idx = 0;
2817 qd_idx = 1;
2818 (*dd_idx) += 2;
2819 break;
2820 case ALGORITHM_PARITY_N:
2821 pd_idx = data_disks;
2822 qd_idx = data_disks + 1;
2823 break;
2824
2825 case ALGORITHM_ROTATING_ZERO_RESTART:
2826 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2827 * of blocks for computing Q is different.
2828 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002829 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002830 qd_idx = pd_idx + 1;
2831 if (pd_idx == raid_disks-1) {
2832 (*dd_idx)++; /* Q D D D P */
2833 qd_idx = 0;
2834 } else if (*dd_idx >= pd_idx)
2835 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002836 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002837 break;
2838
2839 case ALGORITHM_ROTATING_N_RESTART:
2840 /* Same a left_asymmetric, by first stripe is
2841 * D D D P Q rather than
2842 * Q D D D P
2843 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002844 stripe2 += 1;
2845 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002846 qd_idx = pd_idx + 1;
2847 if (pd_idx == raid_disks-1) {
2848 (*dd_idx)++; /* Q D D D P */
2849 qd_idx = 0;
2850 } else if (*dd_idx >= pd_idx)
2851 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002852 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002853 break;
2854
2855 case ALGORITHM_ROTATING_N_CONTINUE:
2856 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002857 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002858 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2859 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002860 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002861 break;
2862
2863 case ALGORITHM_LEFT_ASYMMETRIC_6:
2864 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002865 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002866 if (*dd_idx >= pd_idx)
2867 (*dd_idx)++;
2868 qd_idx = raid_disks - 1;
2869 break;
2870
2871 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002872 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002873 if (*dd_idx >= pd_idx)
2874 (*dd_idx)++;
2875 qd_idx = raid_disks - 1;
2876 break;
2877
2878 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002879 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002880 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2881 qd_idx = raid_disks - 1;
2882 break;
2883
2884 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002885 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002886 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2887 qd_idx = raid_disks - 1;
2888 break;
2889
2890 case ALGORITHM_PARITY_0_6:
2891 pd_idx = 0;
2892 (*dd_idx)++;
2893 qd_idx = raid_disks - 1;
2894 break;
2895
NeilBrown16a53ec2006-06-26 00:27:38 -07002896 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002897 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002898 }
2899 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 }
2901
NeilBrown911d4ee2009-03-31 14:39:38 +11002902 if (sh) {
2903 sh->pd_idx = pd_idx;
2904 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002905 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 /*
2908 * Finally, compute the new sector number
2909 */
2910 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2911 return new_sector;
2912}
2913
Shaohua Li6d036f72015-08-13 14:31:57 -07002914sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
NeilBrownd1688a62011-10-11 16:49:52 +11002916 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002917 int raid_disks = sh->disks;
2918 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002920 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2921 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002922 int algorithm = previous ? conf->prev_algo
2923 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 sector_t stripe;
2925 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002926 sector_t chunk_number;
2927 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002929 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
2931 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2932 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
NeilBrown16a53ec2006-06-26 00:27:38 -07002934 if (i == sh->pd_idx)
2935 return 0;
2936 switch(conf->level) {
2937 case 4: break;
2938 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002939 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 case ALGORITHM_LEFT_ASYMMETRIC:
2941 case ALGORITHM_RIGHT_ASYMMETRIC:
2942 if (i > sh->pd_idx)
2943 i--;
2944 break;
2945 case ALGORITHM_LEFT_SYMMETRIC:
2946 case ALGORITHM_RIGHT_SYMMETRIC:
2947 if (i < sh->pd_idx)
2948 i += raid_disks;
2949 i -= (sh->pd_idx + 1);
2950 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002951 case ALGORITHM_PARITY_0:
2952 i -= 1;
2953 break;
2954 case ALGORITHM_PARITY_N:
2955 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002957 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002958 }
2959 break;
2960 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002961 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002962 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002963 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002964 case ALGORITHM_LEFT_ASYMMETRIC:
2965 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002966 case ALGORITHM_ROTATING_ZERO_RESTART:
2967 case ALGORITHM_ROTATING_N_RESTART:
2968 if (sh->pd_idx == raid_disks-1)
2969 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002970 else if (i > sh->pd_idx)
2971 i -= 2; /* D D P Q D */
2972 break;
2973 case ALGORITHM_LEFT_SYMMETRIC:
2974 case ALGORITHM_RIGHT_SYMMETRIC:
2975 if (sh->pd_idx == raid_disks-1)
2976 i--; /* Q D D D P */
2977 else {
2978 /* D D P Q D */
2979 if (i < sh->pd_idx)
2980 i += raid_disks;
2981 i -= (sh->pd_idx + 2);
2982 }
2983 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002984 case ALGORITHM_PARITY_0:
2985 i -= 2;
2986 break;
2987 case ALGORITHM_PARITY_N:
2988 break;
2989 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002990 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002991 if (sh->pd_idx == 0)
2992 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002993 else {
2994 /* D D Q P D */
2995 if (i < sh->pd_idx)
2996 i += raid_disks;
2997 i -= (sh->pd_idx + 1);
2998 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002999 break;
3000 case ALGORITHM_LEFT_ASYMMETRIC_6:
3001 case ALGORITHM_RIGHT_ASYMMETRIC_6:
3002 if (i > sh->pd_idx)
3003 i--;
3004 break;
3005 case ALGORITHM_LEFT_SYMMETRIC_6:
3006 case ALGORITHM_RIGHT_SYMMETRIC_6:
3007 if (i < sh->pd_idx)
3008 i += data_disks + 1;
3009 i -= (sh->pd_idx + 1);
3010 break;
3011 case ALGORITHM_PARITY_0_6:
3012 i -= 1;
3013 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07003014 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11003015 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07003016 }
3017 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 }
3019
3020 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10003021 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
NeilBrown112bf892009-03-31 14:39:38 +11003023 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11003024 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11003025 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
3026 || sh2.qd_idx != sh->qd_idx) {
NeilBrowncc6167b2016-11-02 14:16:50 +11003027 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
3028 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 return 0;
3030 }
3031 return r_sector;
3032}
3033
Song Liu07e83362017-01-23 17:12:58 -08003034/*
3035 * There are cases where we want handle_stripe_dirtying() and
3036 * schedule_reconstruction() to delay towrite to some dev of a stripe.
3037 *
3038 * This function checks whether we want to delay the towrite. Specifically,
3039 * we delay the towrite when:
3040 *
3041 * 1. degraded stripe has a non-overwrite to the missing dev, AND this
3042 * stripe has data in journal (for other devices).
3043 *
3044 * In this case, when reading data for the non-overwrite dev, it is
3045 * necessary to handle complex rmw of write back cache (prexor with
3046 * orig_page, and xor with page). To keep read path simple, we would
3047 * like to flush data in journal to RAID disks first, so complex rmw
3048 * is handled in the write patch (handle_stripe_dirtying).
3049 *
Song Liu39b99582017-01-24 14:08:23 -08003050 * 2. when journal space is critical (R5C_LOG_CRITICAL=1)
3051 *
3052 * It is important to be able to flush all stripes in raid5-cache.
3053 * Therefore, we need reserve some space on the journal device for
3054 * these flushes. If flush operation includes pending writes to the
3055 * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe
3056 * for the flush out. If we exclude these pending writes from flush
3057 * operation, we only need (conf->max_degraded + 1) pages per stripe.
3058 * Therefore, excluding pending writes in these cases enables more
3059 * efficient use of the journal device.
3060 *
3061 * Note: To make sure the stripe makes progress, we only delay
3062 * towrite for stripes with data already in journal (injournal > 0).
3063 * When LOG_CRITICAL, stripes with injournal == 0 will be sent to
3064 * no_space_stripes list.
3065 *
Song Liu07e83362017-01-23 17:12:58 -08003066 */
Song Liu39b99582017-01-24 14:08:23 -08003067static inline bool delay_towrite(struct r5conf *conf,
3068 struct r5dev *dev,
3069 struct stripe_head_state *s)
Song Liu07e83362017-01-23 17:12:58 -08003070{
Song Liu39b99582017-01-24 14:08:23 -08003071 /* case 1 above */
3072 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3073 !test_bit(R5_Insync, &dev->flags) && s->injournal)
3074 return true;
3075 /* case 2 above */
3076 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
3077 s->injournal > 0)
3078 return true;
3079 return false;
Song Liu07e83362017-01-23 17:12:58 -08003080}
3081
Dan Williams600aa102008-06-28 08:32:05 +10003082static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003083schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10003084 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07003085{
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003086 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11003087 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003088 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07003089
Dan Williamse33129d2007-01-02 13:52:30 -07003090 if (rcw) {
Song Liu1e6d6902016-11-17 15:24:39 -08003091 /*
3092 * In some cases, handle_stripe_dirtying initially decided to
3093 * run rmw and allocates extra page for prexor. However, rcw is
3094 * cheaper later on. We need to free the extra page now,
3095 * because we won't be able to do that in ops_complete_prexor().
3096 */
3097 r5c_release_extra_page(sh);
Dan Williamse33129d2007-01-02 13:52:30 -07003098
3099 for (i = disks; i--; ) {
3100 struct r5dev *dev = &sh->dev[i];
3101
Song Liu39b99582017-01-24 14:08:23 -08003102 if (dev->towrite && !delay_towrite(conf, dev, s)) {
Dan Williamse33129d2007-01-02 13:52:30 -07003103 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10003104 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003105 if (!expand)
3106 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003107 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003108 } else if (test_bit(R5_InJournal, &dev->flags)) {
3109 set_bit(R5_LOCKED, &dev->flags);
3110 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003111 }
3112 }
NeilBrownce7d3632013-03-04 12:37:14 +11003113 /* if we are not expanding this is a proper write request, and
3114 * there will be bios with new data to be drained into the
3115 * stripe cache
3116 */
3117 if (!expand) {
3118 if (!s->locked)
3119 /* False alarm, nothing to do */
3120 return;
3121 sh->reconstruct_state = reconstruct_state_drain_run;
3122 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3123 } else
3124 sh->reconstruct_state = reconstruct_state_run;
3125
3126 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3127
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003128 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003129 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003130 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07003131 } else {
3132 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
3133 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003134 BUG_ON(level == 6 &&
3135 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
3136 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
Dan Williamse33129d2007-01-02 13:52:30 -07003137
Dan Williamse33129d2007-01-02 13:52:30 -07003138 for (i = disks; i--; ) {
3139 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003140 if (i == pd_idx || i == qd_idx)
Dan Williamse33129d2007-01-02 13:52:30 -07003141 continue;
3142
Dan Williamse33129d2007-01-02 13:52:30 -07003143 if (dev->towrite &&
3144 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10003145 test_bit(R5_Wantcompute, &dev->flags))) {
3146 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003147 set_bit(R5_LOCKED, &dev->flags);
3148 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003149 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003150 } else if (test_bit(R5_InJournal, &dev->flags)) {
3151 set_bit(R5_LOCKED, &dev->flags);
3152 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003153 }
3154 }
NeilBrownce7d3632013-03-04 12:37:14 +11003155 if (!s->locked)
3156 /* False alarm - nothing to do */
3157 return;
3158 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
3159 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
3160 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3161 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003162 }
3163
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003164 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07003165 * are in flight
3166 */
3167 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
3168 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10003169 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003170
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003171 if (level == 6) {
3172 int qd_idx = sh->qd_idx;
3173 struct r5dev *dev = &sh->dev[qd_idx];
3174
3175 set_bit(R5_LOCKED, &dev->flags);
3176 clear_bit(R5_UPTODATE, &dev->flags);
3177 s->locked++;
3178 }
3179
Dan Williams600aa102008-06-28 08:32:05 +10003180 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07003181 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10003182 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003183}
NeilBrown16a53ec2006-06-26 00:27:38 -07003184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185/*
3186 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07003187 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 * The bi_next chain must be in order.
3189 */
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003190static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
3191 int forwrite, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
3193 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11003194 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07003195 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
NeilBrowncbe47ec2011-07-26 11:20:35 +10003197 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003198 (unsigned long long)bi->bi_iter.bi_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 (unsigned long long)sh->sector);
3200
Shaohua Lib17459c2012-07-19 16:01:31 +10003201 /*
3202 * If several bio share a stripe. The bio bi_phys_segments acts as a
3203 * reference count to avoid race. The reference count should already be
3204 * increased before this function is called (for example, in
Shaohua Li849674e2016-01-20 13:52:20 -08003205 * raid5_make_request()), so other bio sharing this stripe will not free the
Shaohua Lib17459c2012-07-19 16:01:31 +10003206 * stripe. If a stripe is owned by one stripe, the stripe lock will
3207 * protect it.
3208 */
3209 spin_lock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003210 /* Don't allow new IO added to stripes in batch list */
3211 if (sh->batch_head)
3212 goto overlap;
NeilBrown72626682005-09-09 16:23:54 -07003213 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003215 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07003216 firstwrite = 1;
3217 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 bip = &sh->dev[dd_idx].toread;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003219 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
3220 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 goto overlap;
3222 bip = & (*bip)->bi_next;
3223 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07003224 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 goto overlap;
3226
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003227 if (!forwrite || previous)
3228 clear_bit(STRIPE_BATCH_READY, &sh->state);
3229
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02003230 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 if (*bip)
3232 bi->bi_next = *bip;
3233 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003234 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07003235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 if (forwrite) {
3237 /* check if page is covered */
3238 sector_t sector = sh->dev[dd_idx].sector;
3239 for (bi=sh->dev[dd_idx].towrite;
3240 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07003241 bi && bi->bi_iter.bi_sector <= sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07003243 if (bio_end_sector(bi) >= sector)
3244 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 }
3246 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
shli@kernel.org7a87f432014-12-15 12:57:03 +11003247 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
3248 sh->overwrite_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003250
3251 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003252 (unsigned long long)(*bip)->bi_iter.bi_sector,
NeilBrowncbe47ec2011-07-26 11:20:35 +10003253 (unsigned long long)sh->sector, dd_idx);
3254
3255 if (conf->mddev->bitmap && firstwrite) {
NeilBrownd0852df52015-05-27 08:43:45 +10003256 /* Cannot hold spinlock over bitmap_startwrite,
3257 * but must ensure this isn't added to a batch until
3258 * we have added to the bitmap and set bm_seq.
3259 * So set STRIPE_BITMAP_PENDING to prevent
3260 * batching.
3261 * If multiple add_stripe_bio() calls race here they
3262 * much all set STRIPE_BITMAP_PENDING. So only the first one
3263 * to complete "bitmap_startwrite" gets to set
3264 * STRIPE_BIT_DELAY. This is important as once a stripe
3265 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3266 * any more.
3267 */
3268 set_bit(STRIPE_BITMAP_PENDING, &sh->state);
3269 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10003270 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
3271 STRIPE_SECTORS, 0);
NeilBrownd0852df52015-05-27 08:43:45 +10003272 spin_lock_irq(&sh->stripe_lock);
3273 clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
3274 if (!sh->batch_head) {
3275 sh->bm_seq = conf->seq_flush+1;
3276 set_bit(STRIPE_BIT_DELAY, &sh->state);
3277 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003278 }
NeilBrownd0852df52015-05-27 08:43:45 +10003279 spin_unlock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003280
3281 if (stripe_can_batch(sh))
3282 stripe_add_to_batch_list(conf, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 return 1;
3284
3285 overlap:
3286 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10003287 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 return 0;
3289}
3290
NeilBrownd1688a62011-10-11 16:49:52 +11003291static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08003292
NeilBrownd1688a62011-10-11 16:49:52 +11003293static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003294 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08003295{
NeilBrown784052e2009-03-31 15:19:07 +11003296 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10003297 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11003298 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003299 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11003300 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003301
NeilBrown112bf892009-03-31 14:39:38 +11003302 raid5_compute_sector(conf,
3303 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08003304 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11003305 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003306 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003307}
3308
Dan Williamsa4456852007-07-09 11:56:43 -07003309static void
NeilBrownd1688a62011-10-11 16:49:52 +11003310handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003311 struct stripe_head_state *s, int disks,
NeilBrown34a6f802015-08-14 12:07:57 +10003312 struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003313{
3314 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003315 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003316 for (i = disks; i--; ) {
3317 struct bio *bi;
3318 int bitmap_end = 0;
3319
3320 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003321 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07003322 rcu_read_lock();
3323 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownf5b67ae2016-06-02 16:19:53 +10003324 if (rdev && test_bit(In_sync, &rdev->flags) &&
3325 !test_bit(Faulty, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10003326 atomic_inc(&rdev->nr_pending);
3327 else
3328 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003329 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10003330 if (rdev) {
3331 if (!rdev_set_badblocks(
3332 rdev,
3333 sh->sector,
3334 STRIPE_SECTORS, 0))
3335 md_error(conf->mddev, rdev);
3336 rdev_dec_pending(rdev, conf->mddev);
3337 }
Dan Williamsa4456852007-07-09 11:56:43 -07003338 }
Shaohua Lib17459c2012-07-19 16:01:31 +10003339 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003340 /* fail all writes first */
3341 bi = sh->dev[i].towrite;
3342 sh->dev[i].towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11003343 sh->overwrite_disks = 0;
Shaohua Lib17459c2012-07-19 16:01:31 +10003344 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11003345 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003346 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07003347
Shaohua Li0576b1c2015-08-13 14:32:00 -07003348 r5l_stripe_write_finished(sh);
3349
Dan Williamsa4456852007-07-09 11:56:43 -07003350 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3351 wake_up(&conf->wait_for_overlap);
3352
Kent Overstreet4f024f32013-10-11 15:44:27 -07003353 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003354 sh->dev[i].sector + STRIPE_SECTORS) {
3355 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003356
3357 bi->bi_error = -EIO;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003358 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003359 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003360 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003361 }
3362 bi = nextbi;
3363 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003364 if (bitmap_end)
3365 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3366 STRIPE_SECTORS, 0, 0);
3367 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003368 /* and fail all 'written' */
3369 bi = sh->dev[i].written;
3370 sh->dev[i].written = NULL;
Shaohua Lid592a992014-05-21 17:57:44 +08003371 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3372 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3373 sh->dev[i].page = sh->dev[i].orig_page;
3374 }
3375
Dan Williamsa4456852007-07-09 11:56:43 -07003376 if (bi) bitmap_end = 1;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003377 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003378 sh->dev[i].sector + STRIPE_SECTORS) {
3379 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003380
3381 bi->bi_error = -EIO;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003382 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003383 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003384 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003385 }
3386 bi = bi2;
3387 }
3388
Dan Williamsb5e98d62007-01-02 13:52:31 -07003389 /* fail any reads if this device is non-operational and
3390 * the data has not reached the cache yet.
3391 */
3392 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
Shaohua Li6e74a9c2015-10-08 21:54:08 -07003393 s->failed > conf->max_degraded &&
Dan Williamsb5e98d62007-01-02 13:52:31 -07003394 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3395 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11003396 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003397 bi = sh->dev[i].toread;
3398 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11003399 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003400 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3401 wake_up(&conf->wait_for_overlap);
Shaohua Liebda7802015-09-18 10:20:13 -07003402 if (bi)
3403 s->to_read--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003404 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003405 sh->dev[i].sector + STRIPE_SECTORS) {
3406 struct bio *nextbi =
3407 r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003408
3409 bi->bi_error = -EIO;
NeilBrown34a6f802015-08-14 12:07:57 +10003410 if (!raid5_dec_bi_active_stripes(bi))
3411 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003412 bi = nextbi;
3413 }
3414 }
Dan Williamsa4456852007-07-09 11:56:43 -07003415 if (bitmap_end)
3416 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3417 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10003418 /* If we were in the middle of a write the parity block might
3419 * still be locked - so just clear all R5_LOCKED flags
3420 */
3421 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003422 }
Shaohua Liebda7802015-09-18 10:20:13 -07003423 s->to_write = 0;
3424 s->written = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003425
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003426 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3427 if (atomic_dec_and_test(&conf->pending_full_writes))
3428 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003429}
3430
NeilBrown7f0da592011-07-28 11:39:22 +10003431static void
NeilBrownd1688a62011-10-11 16:49:52 +11003432handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10003433 struct stripe_head_state *s)
3434{
3435 int abort = 0;
3436 int i;
3437
shli@kernel.org59fc6302014-12-15 12:57:03 +11003438 BUG_ON(sh->batch_head);
NeilBrown7f0da592011-07-28 11:39:22 +10003439 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003440 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3441 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10003442 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11003443 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10003444 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10003445 * Don't even need to abort as that is handled elsewhere
3446 * if needed, and not always wanted e.g. if there is a known
3447 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11003448 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10003449 * non-sync devices, or abort the recovery
3450 */
NeilBrown18b98372012-04-01 23:48:38 +10003451 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3452 /* During recovery devices cannot be removed, so
3453 * locking and refcounting of rdevs is not needed
3454 */
NeilBrowne50d3992016-06-02 16:19:52 +10003455 rcu_read_lock();
NeilBrown18b98372012-04-01 23:48:38 +10003456 for (i = 0; i < conf->raid_disks; i++) {
NeilBrowne50d3992016-06-02 16:19:52 +10003457 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown18b98372012-04-01 23:48:38 +10003458 if (rdev
3459 && !test_bit(Faulty, &rdev->flags)
3460 && !test_bit(In_sync, &rdev->flags)
3461 && !rdev_set_badblocks(rdev, sh->sector,
3462 STRIPE_SECTORS, 0))
3463 abort = 1;
NeilBrowne50d3992016-06-02 16:19:52 +10003464 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown18b98372012-04-01 23:48:38 +10003465 if (rdev
3466 && !test_bit(Faulty, &rdev->flags)
3467 && !test_bit(In_sync, &rdev->flags)
3468 && !rdev_set_badblocks(rdev, sh->sector,
3469 STRIPE_SECTORS, 0))
3470 abort = 1;
3471 }
NeilBrowne50d3992016-06-02 16:19:52 +10003472 rcu_read_unlock();
NeilBrown18b98372012-04-01 23:48:38 +10003473 if (abort)
3474 conf->recovery_disabled =
3475 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10003476 }
NeilBrown18b98372012-04-01 23:48:38 +10003477 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10003478}
3479
NeilBrown9a3e1102011-12-23 10:17:53 +11003480static int want_replace(struct stripe_head *sh, int disk_idx)
3481{
3482 struct md_rdev *rdev;
3483 int rv = 0;
NeilBrown3f232d62016-06-02 16:19:52 +10003484
3485 rcu_read_lock();
3486 rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
NeilBrown9a3e1102011-12-23 10:17:53 +11003487 if (rdev
3488 && !test_bit(Faulty, &rdev->flags)
3489 && !test_bit(In_sync, &rdev->flags)
3490 && (rdev->recovery_offset <= sh->sector
3491 || rdev->mddev->recovery_cp <= sh->sector))
3492 rv = 1;
NeilBrown3f232d62016-06-02 16:19:52 +10003493 rcu_read_unlock();
NeilBrown9a3e1102011-12-23 10:17:53 +11003494 return rv;
3495}
3496
NeilBrown2c58f062015-02-02 11:32:23 +11003497static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3498 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07003499{
3500 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10003501 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3502 &sh->dev[s->failed_num[1]] };
NeilBrownea664c82015-02-02 14:03:28 +11003503 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07003504
NeilBrowna79cfe12015-02-02 11:37:59 +11003505
3506 if (test_bit(R5_LOCKED, &dev->flags) ||
3507 test_bit(R5_UPTODATE, &dev->flags))
3508 /* No point reading this as we already have it or have
3509 * decided to get it.
3510 */
3511 return 0;
3512
3513 if (dev->toread ||
3514 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3515 /* We need this block to directly satisfy a request */
3516 return 1;
3517
3518 if (s->syncing || s->expanding ||
3519 (s->replacing && want_replace(sh, disk_idx)))
3520 /* When syncing, or expanding we read everything.
3521 * When replacing, we need the replaced block.
3522 */
3523 return 1;
3524
3525 if ((s->failed >= 1 && fdev[0]->toread) ||
3526 (s->failed >= 2 && fdev[1]->toread))
3527 /* If we want to read from a failed device, then
3528 * we need to actually read every other device.
3529 */
3530 return 1;
3531
NeilBrowna9d56952015-02-02 11:49:10 +11003532 /* Sometimes neither read-modify-write nor reconstruct-write
3533 * cycles can work. In those cases we read every block we
3534 * can. Then the parity-update is certain to have enough to
3535 * work with.
3536 * This can only be a problem when we need to write something,
3537 * and some device has failed. If either of those tests
3538 * fail we need look no further.
3539 */
3540 if (!s->failed || !s->to_write)
3541 return 0;
3542
3543 if (test_bit(R5_Insync, &dev->flags) &&
3544 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3545 /* Pre-reads at not permitted until after short delay
3546 * to gather multiple requests. However if this
3547 * device is no Insync, the block could only be be computed
3548 * and there is no need to delay that.
3549 */
3550 return 0;
NeilBrownea664c82015-02-02 14:03:28 +11003551
NeilBrown36707bb2015-09-24 15:25:36 +10003552 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrownea664c82015-02-02 14:03:28 +11003553 if (fdev[i]->towrite &&
3554 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3555 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3556 /* If we have a partial write to a failed
3557 * device, then we will need to reconstruct
3558 * the content of that device, so all other
3559 * devices must be read.
3560 */
3561 return 1;
3562 }
3563
3564 /* If we are forced to do a reconstruct-write, either because
3565 * the current RAID6 implementation only supports that, or
3566 * or because parity cannot be trusted and we are currently
3567 * recovering it, there is extra need to be careful.
3568 * If one of the devices that we would need to read, because
3569 * it is not being overwritten (and maybe not written at all)
3570 * is missing/faulty, then we need to read everything we can.
3571 */
3572 if (sh->raid_conf->level != 6 &&
3573 sh->sector < sh->raid_conf->mddev->recovery_cp)
3574 /* reconstruct-write isn't being forced */
3575 return 0;
NeilBrown36707bb2015-09-24 15:25:36 +10003576 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrown10d82c52015-05-08 18:19:33 +10003577 if (s->failed_num[i] != sh->pd_idx &&
3578 s->failed_num[i] != sh->qd_idx &&
3579 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
NeilBrownea664c82015-02-02 14:03:28 +11003580 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3581 return 1;
3582 }
3583
NeilBrown2c58f062015-02-02 11:32:23 +11003584 return 0;
3585}
3586
Song Liuba026842017-01-12 17:22:42 -08003587/* fetch_block - checks the given member device to see if its data needs
3588 * to be read or computed to satisfy a request.
3589 *
3590 * Returns 1 when no more member devices need to be checked, otherwise returns
3591 * 0 to tell the loop in handle_stripe_fill to continue
3592 */
NeilBrown2c58f062015-02-02 11:32:23 +11003593static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3594 int disk_idx, int disks)
3595{
3596 struct r5dev *dev = &sh->dev[disk_idx];
3597
3598 /* is the data in this block needed, and can we get it? */
3599 if (need_this_block(sh, s, disk_idx, disks)) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003600 /* we would like to get this block, possibly by computing it,
3601 * otherwise read it if the backing disk is insync
3602 */
3603 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3604 BUG_ON(test_bit(R5_Wantread, &dev->flags));
NeilBrownb0c783b2015-05-08 18:19:32 +10003605 BUG_ON(sh->batch_head);
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003606 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10003607 (s->failed && (disk_idx == s->failed_num[0] ||
3608 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003609 /* have disk failed, and we're requested to fetch it;
3610 * do compute it
3611 */
3612 pr_debug("Computing stripe %llu block %d\n",
3613 (unsigned long long)sh->sector, disk_idx);
3614 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3615 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3616 set_bit(R5_Wantcompute, &dev->flags);
3617 sh->ops.target = disk_idx;
3618 sh->ops.target2 = -1; /* no 2nd target */
3619 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10003620 /* Careful: from this point on 'uptodate' is in the eye
3621 * of raid_run_ops which services 'compute' operations
3622 * before writes. R5_Wantcompute flags a block that will
3623 * be R5_UPTODATE by the time it is needed for a
3624 * subsequent operation.
3625 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003626 s->uptodate++;
3627 return 1;
3628 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3629 /* Computing 2-failure is *very* expensive; only
3630 * do it if failed >= 2
3631 */
3632 int other;
3633 for (other = disks; other--; ) {
3634 if (other == disk_idx)
3635 continue;
3636 if (!test_bit(R5_UPTODATE,
3637 &sh->dev[other].flags))
3638 break;
3639 }
3640 BUG_ON(other < 0);
3641 pr_debug("Computing stripe %llu blocks %d,%d\n",
3642 (unsigned long long)sh->sector,
3643 disk_idx, other);
3644 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3645 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3646 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3647 set_bit(R5_Wantcompute, &sh->dev[other].flags);
3648 sh->ops.target = disk_idx;
3649 sh->ops.target2 = other;
3650 s->uptodate += 2;
3651 s->req_compute = 1;
3652 return 1;
3653 } else if (test_bit(R5_Insync, &dev->flags)) {
3654 set_bit(R5_LOCKED, &dev->flags);
3655 set_bit(R5_Wantread, &dev->flags);
3656 s->locked++;
3657 pr_debug("Reading block %d (sync=%d)\n",
3658 disk_idx, s->syncing);
3659 }
3660 }
3661
3662 return 0;
3663}
3664
3665/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10003666 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003667 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10003668static void handle_stripe_fill(struct stripe_head *sh,
3669 struct stripe_head_state *s,
3670 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003671{
3672 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003673
3674 /* look for blocks to read/compute, skip this if a compute
3675 * is already in flight, or if the stripe contents are in the
3676 * midst of changing due to a write
3677 */
3678 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Song Liu07e83362017-01-23 17:12:58 -08003679 !sh->reconstruct_state) {
3680
3681 /*
3682 * For degraded stripe with data in journal, do not handle
3683 * read requests yet, instead, flush the stripe to raid
3684 * disks first, this avoids handling complex rmw of write
3685 * back cache (prexor with orig_page, and then xor with
3686 * page) in the read path
3687 */
3688 if (s->injournal && s->failed) {
3689 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
3690 r5c_make_stripe_write_out(sh);
3691 goto out;
3692 }
3693
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003694 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10003695 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003696 break;
Song Liu07e83362017-01-23 17:12:58 -08003697 }
3698out:
Dan Williamsa4456852007-07-09 11:56:43 -07003699 set_bit(STRIPE_HANDLE, &sh->state);
3700}
3701
NeilBrown787b76f2015-05-21 12:56:41 +10003702static void break_stripe_batch_list(struct stripe_head *head_sh,
3703 unsigned long handle_flags);
Dan Williams1fe797e2008-06-28 09:16:30 +10003704/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07003705 * any written block on an uptodate or failed drive can be returned.
3706 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3707 * never LOCKED, so we don't need to test 'failed' directly.
3708 */
NeilBrownd1688a62011-10-11 16:49:52 +11003709static void handle_stripe_clean_event(struct r5conf *conf,
NeilBrown34a6f802015-08-14 12:07:57 +10003710 struct stripe_head *sh, int disks, struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003711{
3712 int i;
3713 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003714 int discard_pending = 0;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003715 struct stripe_head *head_sh = sh;
3716 bool do_endio = false;
Dan Williamsa4456852007-07-09 11:56:43 -07003717
3718 for (i = disks; i--; )
3719 if (sh->dev[i].written) {
3720 dev = &sh->dev[i];
3721 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003722 (test_bit(R5_UPTODATE, &dev->flags) ||
Shaohua Lid592a992014-05-21 17:57:44 +08003723 test_bit(R5_Discard, &dev->flags) ||
3724 test_bit(R5_SkipCopy, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003725 /* We can return any write requests */
3726 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003727 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003728 if (test_and_clear_bit(R5_Discard, &dev->flags))
3729 clear_bit(R5_UPTODATE, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08003730 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3731 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
Shaohua Lid592a992014-05-21 17:57:44 +08003732 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003733 do_endio = true;
3734
3735returnbi:
3736 dev->page = dev->orig_page;
Dan Williamsa4456852007-07-09 11:56:43 -07003737 wbi = dev->written;
3738 dev->written = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003739 while (wbi && wbi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003740 dev->sector + STRIPE_SECTORS) {
3741 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10003742 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003743 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003744 bio_list_add(return_bi, wbi);
Dan Williamsa4456852007-07-09 11:56:43 -07003745 }
3746 wbi = wbi2;
3747 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003748 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3749 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07003750 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003751 0);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003752 if (head_sh->batch_head) {
3753 sh = list_first_entry(&sh->batch_list,
3754 struct stripe_head,
3755 batch_list);
3756 if (sh != head_sh) {
3757 dev = &sh->dev[i];
3758 goto returnbi;
3759 }
3760 }
3761 sh = head_sh;
3762 dev = &sh->dev[i];
NeilBrownf8dfcff2013-03-12 12:18:06 +11003763 } else if (test_bit(R5_Discard, &dev->flags))
3764 discard_pending = 1;
3765 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -07003766
Shaohua Li0576b1c2015-08-13 14:32:00 -07003767 r5l_stripe_write_finished(sh);
3768
NeilBrownf8dfcff2013-03-12 12:18:06 +11003769 if (!discard_pending &&
3770 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003771 int hash;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003772 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3773 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3774 if (sh->qd_idx >= 0) {
3775 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3776 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3777 }
3778 /* now that discard is done we can proceed with any sync */
3779 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08003780 /*
3781 * SCSI discard will change some bio fields and the stripe has
3782 * no updated data, so remove it from hash list and the stripe
3783 * will be reinitialized
3784 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11003785unhash:
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003786 hash = sh->hash_lock_index;
3787 spin_lock_irq(conf->hash_locks + hash);
Shaohua Lid47648f2013-10-19 14:51:42 +08003788 remove_hash(sh);
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003789 spin_unlock_irq(conf->hash_locks + hash);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003790 if (head_sh->batch_head) {
3791 sh = list_first_entry(&sh->batch_list,
3792 struct stripe_head, batch_list);
3793 if (sh != head_sh)
3794 goto unhash;
3795 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003796 sh = head_sh;
3797
NeilBrownf8dfcff2013-03-12 12:18:06 +11003798 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3799 set_bit(STRIPE_HANDLE, &sh->state);
3800
3801 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003802
3803 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3804 if (atomic_dec_and_test(&conf->pending_full_writes))
3805 md_wakeup_thread(conf->mddev->thread);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003806
NeilBrown787b76f2015-05-21 12:56:41 +10003807 if (head_sh->batch_head && do_endio)
3808 break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
Dan Williamsa4456852007-07-09 11:56:43 -07003809}
3810
Song Liu86aa1392017-01-12 17:22:41 -08003811/*
3812 * For RMW in write back cache, we need extra page in prexor to store the
3813 * old data. This page is stored in dev->orig_page.
3814 *
3815 * This function checks whether we have data for prexor. The exact logic
3816 * is:
3817 * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
3818 */
3819static inline bool uptodate_for_rmw(struct r5dev *dev)
3820{
3821 return (test_bit(R5_UPTODATE, &dev->flags)) &&
3822 (!test_bit(R5_InJournal, &dev->flags) ||
3823 test_bit(R5_OrigPageUPTDODATE, &dev->flags));
3824}
3825
Song Liud7bd3982016-11-23 22:50:39 -08003826static int handle_stripe_dirtying(struct r5conf *conf,
3827 struct stripe_head *sh,
3828 struct stripe_head_state *s,
3829 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003830{
3831 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003832 sector_t recovery_cp = conf->mddev->recovery_cp;
3833
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003834 /* Check whether resync is now happening or should start.
Alexander Lyakasa7854482012-10-11 13:50:12 +11003835 * If yes, then the array is dirty (after unclean shutdown or
3836 * initial creation), so parity in some stripes might be inconsistent.
3837 * In this case, we need to always do reconstruct-write, to ensure
3838 * that in case of drive failure or read-error correction, we
3839 * generate correct data from the parity.
3840 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003841 if (conf->rmw_level == PARITY_DISABLE_RMW ||
NeilBrown26ac1072015-02-18 11:35:14 +11003842 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
3843 s->failed == 0)) {
Alexander Lyakasa7854482012-10-11 13:50:12 +11003844 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10003845 * look like rcw is cheaper
3846 */
3847 rcw = 1; rmw = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003848 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
3849 conf->rmw_level, (unsigned long long)recovery_cp,
Alexander Lyakasa7854482012-10-11 13:50:12 +11003850 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10003851 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003852 /* would I have to read this buffer for read_modify_write */
3853 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08003854 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu07e83362017-01-23 17:12:58 -08003855 i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08003856 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003857 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08003858 !(uptodate_for_rmw(dev) ||
Dan Williamsf38e1212007-01-02 13:52:30 -07003859 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003860 if (test_bit(R5_Insync, &dev->flags))
3861 rmw++;
3862 else
3863 rmw += 2*disks; /* cannot read it */
3864 }
3865 /* Would I have to read this buffer for reconstruct_write */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003866 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3867 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) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003870 test_bit(R5_Wantcompute, &dev->flags))) {
NeilBrown67f45542014-05-28 13:39:22 +10003871 if (test_bit(R5_Insync, &dev->flags))
3872 rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07003873 else
3874 rcw += 2*disks;
3875 }
3876 }
Song Liu1e6d6902016-11-17 15:24:39 -08003877
Song Liu39b99582017-01-24 14:08:23 -08003878 pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n",
3879 (unsigned long long)sh->sector, sh->state, rmw, rcw);
Dan Williamsa4456852007-07-09 11:56:43 -07003880 set_bit(STRIPE_HANDLE, &sh->state);
Song Liu41257582016-05-23 17:25:06 -07003881 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003882 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003883 if (conf->mddev->queue)
3884 blk_add_trace_msg(conf->mddev->queue,
3885 "raid5 rmw %llu %d",
3886 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07003887 for (i = disks; i--; ) {
3888 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08003889 if (test_bit(R5_InJournal, &dev->flags) &&
3890 dev->page == dev->orig_page &&
3891 !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) {
3892 /* alloc page for prexor */
Song Liud7bd3982016-11-23 22:50:39 -08003893 struct page *p = alloc_page(GFP_NOIO);
Song Liu1e6d6902016-11-17 15:24:39 -08003894
Song Liud7bd3982016-11-23 22:50:39 -08003895 if (p) {
3896 dev->orig_page = p;
3897 continue;
3898 }
3899
3900 /*
3901 * alloc_page() failed, try use
3902 * disk_info->extra_page
3903 */
3904 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE,
3905 &conf->cache_state)) {
3906 r5c_use_extra_page(sh);
3907 break;
3908 }
3909
3910 /* extra_page in use, add to delayed_list */
3911 set_bit(STRIPE_DELAYED, &sh->state);
3912 s->waiting_extra_page = 1;
3913 return -EAGAIN;
Song Liu1e6d6902016-11-17 15:24:39 -08003914 }
Song Liud7bd3982016-11-23 22:50:39 -08003915 }
Song Liu1e6d6902016-11-17 15:24:39 -08003916
Song Liud7bd3982016-11-23 22:50:39 -08003917 for (i = disks; i--; ) {
3918 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08003919 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003920 i == sh->pd_idx || i == sh->qd_idx ||
3921 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003922 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08003923 !(uptodate_for_rmw(dev) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003924 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003925 test_bit(R5_Insync, &dev->flags)) {
NeilBrown67f45542014-05-28 13:39:22 +10003926 if (test_bit(STRIPE_PREREAD_ACTIVE,
3927 &sh->state)) {
3928 pr_debug("Read_old block %d for r-m-w\n",
3929 i);
Dan Williamsa4456852007-07-09 11:56:43 -07003930 set_bit(R5_LOCKED, &dev->flags);
3931 set_bit(R5_Wantread, &dev->flags);
3932 s->locked++;
3933 } else {
3934 set_bit(STRIPE_DELAYED, &sh->state);
3935 set_bit(STRIPE_HANDLE, &sh->state);
3936 }
3937 }
3938 }
NeilBrowna9add5d2012-10-31 11:59:09 +11003939 }
Song Liu41257582016-05-23 17:25:06 -07003940 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003941 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11003942 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10003943 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003944 for (i = disks; i--; ) {
3945 struct r5dev *dev = &sh->dev[i];
3946 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10003947 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003948 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003949 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10003950 test_bit(R5_Wantcompute, &dev->flags))) {
3951 rcw++;
NeilBrown67f45542014-05-28 13:39:22 +10003952 if (test_bit(R5_Insync, &dev->flags) &&
3953 test_bit(STRIPE_PREREAD_ACTIVE,
3954 &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07003955 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07003956 "%d for Reconstruct\n", i);
3957 set_bit(R5_LOCKED, &dev->flags);
3958 set_bit(R5_Wantread, &dev->flags);
3959 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11003960 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07003961 } else {
3962 set_bit(STRIPE_DELAYED, &sh->state);
3963 set_bit(STRIPE_HANDLE, &sh->state);
3964 }
3965 }
3966 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003967 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11003968 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3969 (unsigned long long)sh->sector,
3970 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10003971 }
NeilBrownb1b02fe2015-02-02 10:44:29 +11003972
3973 if (rcw > disks && rmw > disks &&
3974 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3975 set_bit(STRIPE_DELAYED, &sh->state);
3976
Dan Williamsa4456852007-07-09 11:56:43 -07003977 /* now if nothing is locked, and if we have enough data,
3978 * we can start a write request
3979 */
Dan Williamsf38e1212007-01-02 13:52:30 -07003980 /* since handle_stripe can be called at any time we need to handle the
3981 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07003982 * subsequent call wants to start a write request. raid_run_ops only
3983 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07003984 * simultaneously. If this is not the case then new writes need to be
3985 * held off until the compute completes.
3986 */
Dan Williams976ea8d2008-06-28 08:32:03 +10003987 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3988 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
Song Liu1e6d6902016-11-17 15:24:39 -08003989 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003990 schedule_reconstruction(sh, s, rcw == 0, 0);
Song Liud7bd3982016-11-23 22:50:39 -08003991 return 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003992}
3993
NeilBrownd1688a62011-10-11 16:49:52 +11003994static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003995 struct stripe_head_state *s, int disks)
3996{
Dan Williamsecc65c92008-06-28 08:31:57 +10003997 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07003998
shli@kernel.org59fc6302014-12-15 12:57:03 +11003999 BUG_ON(sh->batch_head);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004000 set_bit(STRIPE_HANDLE, &sh->state);
4001
Dan Williamsecc65c92008-06-28 08:31:57 +10004002 switch (sh->check_state) {
4003 case check_state_idle:
4004 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07004005 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07004006 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10004007 sh->check_state = check_state_run;
4008 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004009 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004010 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10004011 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07004012 }
NeilBrownf2b3b442011-07-26 11:35:19 +10004013 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10004014 /* fall through */
4015 case check_state_compute_result:
4016 sh->check_state = check_state_idle;
4017 if (!dev)
4018 dev = &sh->dev[sh->pd_idx];
4019
4020 /* check that a write has not made the stripe insync */
4021 if (test_bit(STRIPE_INSYNC, &sh->state))
4022 break;
4023
4024 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07004025 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
4026 BUG_ON(s->uptodate != disks);
4027
4028 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10004029 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07004030 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07004031
Dan Williamsa4456852007-07-09 11:56:43 -07004032 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07004033 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10004034 break;
4035 case check_state_run:
4036 break; /* we will be called again upon completion */
4037 case check_state_check_result:
4038 sh->check_state = check_state_idle;
4039
4040 /* if a failure occurred during the check operation, leave
4041 * STRIPE_INSYNC not set and let the stripe be handled again
4042 */
4043 if (s->failed)
4044 break;
4045
4046 /* handle a successful check operation, if parity is correct
4047 * we are done. Otherwise update the mismatch count and repair
4048 * parity if !MD_RECOVERY_CHECK
4049 */
Dan Williamsad283ea2009-08-29 19:09:26 -07004050 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10004051 /* parity is correct (on disc,
4052 * not in buffer any more)
4053 */
4054 set_bit(STRIPE_INSYNC, &sh->state);
4055 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004056 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10004057 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
4058 /* don't try to repair!! */
4059 set_bit(STRIPE_INSYNC, &sh->state);
4060 else {
4061 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10004062 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10004063 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4064 set_bit(R5_Wantcompute,
4065 &sh->dev[sh->pd_idx].flags);
4066 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07004067 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10004068 s->uptodate++;
4069 }
4070 }
4071 break;
4072 case check_state_compute_run:
4073 break;
4074 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11004075 pr_err("%s: unknown check_state: %d sector: %llu\n",
Dan Williamsecc65c92008-06-28 08:31:57 +10004076 __func__, sh->check_state,
4077 (unsigned long long) sh->sector);
4078 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07004079 }
4080}
4081
NeilBrownd1688a62011-10-11 16:49:52 +11004082static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07004083 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10004084 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07004085{
Dan Williamsa4456852007-07-09 11:56:43 -07004086 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11004087 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07004088 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07004089
shli@kernel.org59fc6302014-12-15 12:57:03 +11004090 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07004091 set_bit(STRIPE_HANDLE, &sh->state);
4092
4093 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004094
Dan Williamsa4456852007-07-09 11:56:43 -07004095 /* Want to check and possibly repair P and Q.
4096 * However there could be one 'failed' device, in which
4097 * case we can only check one of them, possibly using the
4098 * other to generate missing data
4099 */
4100
Dan Williamsd82dfee2009-07-14 13:40:57 -07004101 switch (sh->check_state) {
4102 case check_state_idle:
4103 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10004104 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004105 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07004106 * makes sense to check P (If anything else were failed,
4107 * we would have used P to recreate it).
4108 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004109 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07004110 }
NeilBrownf2b3b442011-07-26 11:35:19 +10004111 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004112 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07004113 * anything, so it makes sense to check it
4114 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004115 if (sh->check_state == check_state_run)
4116 sh->check_state = check_state_run_pq;
4117 else
4118 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07004119 }
Dan Williams36d1c642009-07-14 11:48:22 -07004120
Dan Williamsd82dfee2009-07-14 13:40:57 -07004121 /* discard potentially stale zero_sum_result */
4122 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07004123
Dan Williamsd82dfee2009-07-14 13:40:57 -07004124 if (sh->check_state == check_state_run) {
4125 /* async_xor_zero_sum destroys the contents of P */
4126 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
4127 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07004128 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004129 if (sh->check_state >= check_state_run &&
4130 sh->check_state <= check_state_run_pq) {
4131 /* async_syndrome_zero_sum preserves P and Q, so
4132 * no need to mark them !uptodate here
4133 */
4134 set_bit(STRIPE_OP_CHECK, &s->ops_request);
4135 break;
4136 }
Dan Williams36d1c642009-07-14 11:48:22 -07004137
Dan Williamsd82dfee2009-07-14 13:40:57 -07004138 /* we have 2-disk failure */
4139 BUG_ON(s->failed != 2);
4140 /* fall through */
4141 case check_state_compute_result:
4142 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07004143
Dan Williamsd82dfee2009-07-14 13:40:57 -07004144 /* check that a write has not made the stripe insync */
4145 if (test_bit(STRIPE_INSYNC, &sh->state))
4146 break;
Dan Williamsa4456852007-07-09 11:56:43 -07004147
4148 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07004149 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07004150 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004151 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07004152 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004153 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07004154 s->locked++;
4155 set_bit(R5_LOCKED, &dev->flags);
4156 set_bit(R5_Wantwrite, &dev->flags);
4157 }
4158 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004159 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07004160 s->locked++;
4161 set_bit(R5_LOCKED, &dev->flags);
4162 set_bit(R5_Wantwrite, &dev->flags);
4163 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004164 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004165 dev = &sh->dev[pd_idx];
4166 s->locked++;
4167 set_bit(R5_LOCKED, &dev->flags);
4168 set_bit(R5_Wantwrite, &dev->flags);
4169 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004170 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004171 dev = &sh->dev[qd_idx];
4172 s->locked++;
4173 set_bit(R5_LOCKED, &dev->flags);
4174 set_bit(R5_Wantwrite, &dev->flags);
4175 }
4176 clear_bit(STRIPE_DEGRADED, &sh->state);
4177
4178 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004179 break;
4180 case check_state_run:
4181 case check_state_run_q:
4182 case check_state_run_pq:
4183 break; /* we will be called again upon completion */
4184 case check_state_check_result:
4185 sh->check_state = check_state_idle;
4186
4187 /* handle a successful check operation, if parity is correct
4188 * we are done. Otherwise update the mismatch count and repair
4189 * parity if !MD_RECOVERY_CHECK
4190 */
4191 if (sh->ops.zero_sum_result == 0) {
4192 /* both parities are correct */
4193 if (!s->failed)
4194 set_bit(STRIPE_INSYNC, &sh->state);
4195 else {
4196 /* in contrast to the raid5 case we can validate
4197 * parity, but still have a failure to write
4198 * back
4199 */
4200 sh->check_state = check_state_compute_result;
4201 /* Returning at this point means that we may go
4202 * off and bring p and/or q uptodate again so
4203 * we make sure to check zero_sum_result again
4204 * to verify if p or q need writeback
4205 */
4206 }
4207 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11004208 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004209 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
4210 /* don't try to repair!! */
4211 set_bit(STRIPE_INSYNC, &sh->state);
4212 else {
4213 int *target = &sh->ops.target;
4214
4215 sh->ops.target = -1;
4216 sh->ops.target2 = -1;
4217 sh->check_state = check_state_compute_run;
4218 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4219 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4220 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4221 set_bit(R5_Wantcompute,
4222 &sh->dev[pd_idx].flags);
4223 *target = pd_idx;
4224 target = &sh->ops.target2;
4225 s->uptodate++;
4226 }
4227 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4228 set_bit(R5_Wantcompute,
4229 &sh->dev[qd_idx].flags);
4230 *target = qd_idx;
4231 s->uptodate++;
4232 }
4233 }
4234 }
4235 break;
4236 case check_state_compute_run:
4237 break;
4238 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11004239 pr_warn("%s: unknown check_state: %d sector: %llu\n",
4240 __func__, sh->check_state,
4241 (unsigned long long) sh->sector);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004242 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07004243 }
4244}
4245
NeilBrownd1688a62011-10-11 16:49:52 +11004246static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07004247{
4248 int i;
4249
4250 /* We have read all the blocks in this stripe and now we need to
4251 * copy some of them into a target stripe for expand.
4252 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07004253 struct dma_async_tx_descriptor *tx = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +11004254 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07004255 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4256 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11004257 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11004258 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07004259 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07004260 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07004261
Shaohua Li6d036f72015-08-13 14:31:57 -07004262 sector_t bn = raid5_compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11004263 sector_t s = raid5_compute_sector(conf, bn, 0,
4264 &dd_idx, NULL);
Shaohua Li6d036f72015-08-13 14:31:57 -07004265 sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07004266 if (sh2 == NULL)
4267 /* so far only the early blocks of this stripe
4268 * have been requested. When later blocks
4269 * get requested, we will try again
4270 */
4271 continue;
4272 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
4273 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
4274 /* must have already done this block */
Shaohua Li6d036f72015-08-13 14:31:57 -07004275 raid5_release_stripe(sh2);
Dan Williamsa4456852007-07-09 11:56:43 -07004276 continue;
4277 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07004278
4279 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07004280 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004281 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07004282 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07004283 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004284
Dan Williamsa4456852007-07-09 11:56:43 -07004285 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
4286 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
4287 for (j = 0; j < conf->raid_disks; j++)
4288 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10004289 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07004290 !test_bit(R5_Expanded, &sh2->dev[j].flags))
4291 break;
4292 if (j == conf->raid_disks) {
4293 set_bit(STRIPE_EXPAND_READY, &sh2->state);
4294 set_bit(STRIPE_HANDLE, &sh2->state);
4295 }
Shaohua Li6d036f72015-08-13 14:31:57 -07004296 raid5_release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004297
Dan Williamsa4456852007-07-09 11:56:43 -07004298 }
NeilBrowna2e08552007-09-11 15:23:36 -07004299 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11004300 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07004301}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
4303/*
4304 * handle_stripe - do things to a stripe.
4305 *
NeilBrown9a3e1102011-12-23 10:17:53 +11004306 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4307 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11004309 * return some read requests which now have data
4310 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 * schedule a read on some buffers
4312 * schedule a write of some buffers
4313 * return confirmation of parity correctness
4314 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 */
Dan Williamsa4456852007-07-09 11:56:43 -07004316
NeilBrownacfe7262011-07-27 11:00:36 +10004317static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07004318{
NeilBrownd1688a62011-10-11 16:49:52 +11004319 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08004320 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004321 struct r5dev *dev;
4322 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11004323 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07004324
NeilBrownacfe7262011-07-27 11:00:36 +10004325 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07004326
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004327 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
4328 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
NeilBrownacfe7262011-07-27 11:00:36 +10004329 s->failed_num[0] = -1;
4330 s->failed_num[1] = -1;
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004331 s->log_failed = r5l_log_disk_error(conf);
NeilBrownacfe7262011-07-27 11:00:36 +10004332
4333 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07004334 rcu_read_lock();
4335 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004336 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10004337 sector_t first_bad;
4338 int bad_sectors;
4339 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10004340
NeilBrown16a53ec2006-06-26 00:27:38 -07004341 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07004342
Dan Williams45b42332007-07-09 11:56:43 -07004343 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11004344 i, dev->flags,
4345 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004346 /* maybe we can reply to a read
4347 *
4348 * new wantfill requests are only permitted while
4349 * ops_complete_biofill is guaranteed to be inactive
4350 */
4351 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4352 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4353 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07004354
4355 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10004356 if (test_bit(R5_LOCKED, &dev->flags))
4357 s->locked++;
4358 if (test_bit(R5_UPTODATE, &dev->flags))
4359 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004360 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004361 s->compute++;
4362 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004363 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004364
NeilBrownacfe7262011-07-27 11:00:36 +10004365 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004366 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10004367 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10004368 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004369 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10004370 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004371 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004372 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004373 }
Dan Williamsa4456852007-07-09 11:56:43 -07004374 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10004375 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11004376 /* Prefer to use the replacement for reads, but only
4377 * if it is recovered enough and has no bad blocks.
4378 */
4379 rdev = rcu_dereference(conf->disks[i].replacement);
4380 if (rdev && !test_bit(Faulty, &rdev->flags) &&
4381 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
4382 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4383 &first_bad, &bad_sectors))
4384 set_bit(R5_ReadRepl, &dev->flags);
4385 else {
NeilBrowne6030cb2015-07-17 13:26:23 +10004386 if (rdev && !test_bit(Faulty, &rdev->flags))
NeilBrown9a3e1102011-12-23 10:17:53 +11004387 set_bit(R5_NeedReplace, &dev->flags);
NeilBrowne6030cb2015-07-17 13:26:23 +10004388 else
4389 clear_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11004390 rdev = rcu_dereference(conf->disks[i].rdev);
4391 clear_bit(R5_ReadRepl, &dev->flags);
4392 }
NeilBrown9283d8c2011-12-08 16:27:57 +11004393 if (rdev && test_bit(Faulty, &rdev->flags))
4394 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10004395 if (rdev) {
4396 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4397 &first_bad, &bad_sectors);
4398 if (s->blocked_rdev == NULL
4399 && (test_bit(Blocked, &rdev->flags)
4400 || is_bad < 0)) {
4401 if (is_bad < 0)
4402 set_bit(BlockedBadBlocks,
4403 &rdev->flags);
4404 s->blocked_rdev = rdev;
4405 atomic_inc(&rdev->nr_pending);
4406 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004407 }
NeilBrown415e72d2010-06-17 17:25:21 +10004408 clear_bit(R5_Insync, &dev->flags);
4409 if (!rdev)
4410 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10004411 else if (is_bad) {
4412 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10004413 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4414 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10004415 /* treat as in-sync, but with a read error
4416 * which we can now try to correct
4417 */
4418 set_bit(R5_Insync, &dev->flags);
4419 set_bit(R5_ReadError, &dev->flags);
4420 }
4421 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10004422 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11004423 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10004424 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11004425 set_bit(R5_Insync, &dev->flags);
4426 else if (test_bit(R5_UPTODATE, &dev->flags) &&
4427 test_bit(R5_Expanded, &dev->flags))
4428 /* If we've reshaped into here, we assume it is Insync.
4429 * We will shortly update recovery_offset to make
4430 * it official.
4431 */
4432 set_bit(R5_Insync, &dev->flags);
4433
NeilBrown1cc03eb2014-01-06 13:19:42 +11004434 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004435 /* This flag does not apply to '.replacement'
4436 * only to .rdev, so make sure to check that*/
4437 struct md_rdev *rdev2 = rcu_dereference(
4438 conf->disks[i].rdev);
4439 if (rdev2 == rdev)
4440 clear_bit(R5_Insync, &dev->flags);
4441 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004442 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004443 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10004444 } else
4445 clear_bit(R5_WriteError, &dev->flags);
4446 }
NeilBrown1cc03eb2014-01-06 13:19:42 +11004447 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004448 /* This flag does not apply to '.replacement'
4449 * only to .rdev, so make sure to check that*/
4450 struct md_rdev *rdev2 = rcu_dereference(
4451 conf->disks[i].rdev);
4452 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10004453 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004454 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10004455 } else
4456 clear_bit(R5_MadeGood, &dev->flags);
4457 }
NeilBrown977df362011-12-23 10:17:53 +11004458 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4459 struct md_rdev *rdev2 = rcu_dereference(
4460 conf->disks[i].replacement);
4461 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4462 s->handle_bad_blocks = 1;
4463 atomic_inc(&rdev2->nr_pending);
4464 } else
4465 clear_bit(R5_MadeGoodRepl, &dev->flags);
4466 }
NeilBrown415e72d2010-06-17 17:25:21 +10004467 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004468 /* The ReadError flag will just be confusing now */
4469 clear_bit(R5_ReadError, &dev->flags);
4470 clear_bit(R5_ReWrite, &dev->flags);
4471 }
NeilBrown415e72d2010-06-17 17:25:21 +10004472 if (test_bit(R5_ReadError, &dev->flags))
4473 clear_bit(R5_Insync, &dev->flags);
4474 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004475 if (s->failed < 2)
4476 s->failed_num[s->failed] = i;
4477 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11004478 if (rdev && !test_bit(Faulty, &rdev->flags))
4479 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10004480 }
Song Liu2ded3702016-11-17 15:24:38 -08004481
4482 if (test_bit(R5_InJournal, &dev->flags))
4483 s->injournal++;
Song Liu1e6d6902016-11-17 15:24:39 -08004484 if (test_bit(R5_InJournal, &dev->flags) && dev->written)
4485 s->just_cached++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004486 }
NeilBrown9a3e1102011-12-23 10:17:53 +11004487 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4488 /* If there is a failed device being replaced,
4489 * we must be recovering.
4490 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10004491 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11004492 * else we can only be replacing
4493 * sync and recovery both need to read all devices, and so
4494 * use the same flag.
4495 */
4496 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10004497 sh->sector >= conf->mddev->recovery_cp ||
4498 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11004499 s->syncing = 1;
4500 else
4501 s->replacing = 1;
4502 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004503 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10004504}
NeilBrownf4168852007-02-28 20:11:53 -08004505
shli@kernel.org59fc6302014-12-15 12:57:03 +11004506static int clear_batch_ready(struct stripe_head *sh)
4507{
NeilBrownb15a9db2015-05-22 15:20:04 +10004508 /* Return '1' if this is a member of batch, or
4509 * '0' if it is a lone stripe or a head which can now be
4510 * handled.
4511 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11004512 struct stripe_head *tmp;
4513 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
NeilBrownb15a9db2015-05-22 15:20:04 +10004514 return (sh->batch_head && sh->batch_head != sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11004515 spin_lock(&sh->stripe_lock);
4516 if (!sh->batch_head) {
4517 spin_unlock(&sh->stripe_lock);
4518 return 0;
4519 }
4520
4521 /*
4522 * this stripe could be added to a batch list before we check
4523 * BATCH_READY, skips it
4524 */
4525 if (sh->batch_head != sh) {
4526 spin_unlock(&sh->stripe_lock);
4527 return 1;
4528 }
4529 spin_lock(&sh->batch_lock);
4530 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4531 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4532 spin_unlock(&sh->batch_lock);
4533 spin_unlock(&sh->stripe_lock);
4534
4535 /*
4536 * BATCH_READY is cleared, no new stripes can be added.
4537 * batch_list can be accessed without lock
4538 */
4539 return 0;
4540}
4541
NeilBrown3960ce72015-05-21 12:20:36 +10004542static void break_stripe_batch_list(struct stripe_head *head_sh,
4543 unsigned long handle_flags)
shli@kernel.org72ac7332014-12-15 12:57:03 +11004544{
NeilBrown4e3d62f2015-05-21 11:50:16 +10004545 struct stripe_head *sh, *next;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004546 int i;
NeilBrownfb642b92015-05-21 12:00:47 +10004547 int do_wakeup = 0;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004548
NeilBrownbb270512015-05-08 18:19:40 +10004549 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4550
shli@kernel.org72ac7332014-12-15 12:57:03 +11004551 list_del_init(&sh->batch_list);
4552
Shaohua Lifb3229d2016-03-09 10:08:38 -08004553 WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
NeilBrown1b956f72015-05-21 12:40:26 +10004554 (1 << STRIPE_SYNCING) |
4555 (1 << STRIPE_REPLACED) |
NeilBrown1b956f72015-05-21 12:40:26 +10004556 (1 << STRIPE_DELAYED) |
4557 (1 << STRIPE_BIT_DELAY) |
4558 (1 << STRIPE_FULL_WRITE) |
4559 (1 << STRIPE_BIOFILL_RUN) |
4560 (1 << STRIPE_COMPUTE_RUN) |
4561 (1 << STRIPE_OPS_REQ_PENDING) |
4562 (1 << STRIPE_DISCARD) |
4563 (1 << STRIPE_BATCH_READY) |
4564 (1 << STRIPE_BATCH_ERR) |
Shaohua Lifb3229d2016-03-09 10:08:38 -08004565 (1 << STRIPE_BITMAP_PENDING)),
4566 "stripe state: %lx\n", sh->state);
4567 WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4568 (1 << STRIPE_REPLACED)),
4569 "head stripe state: %lx\n", head_sh->state);
NeilBrown1b956f72015-05-21 12:40:26 +10004570
4571 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
NeilBrown550da242016-03-09 12:58:25 +11004572 (1 << STRIPE_PREREAD_ACTIVE) |
NeilBrown1b956f72015-05-21 12:40:26 +10004573 (1 << STRIPE_DEGRADED)),
4574 head_sh->state & (1 << STRIPE_INSYNC));
4575
shli@kernel.org72ac7332014-12-15 12:57:03 +11004576 sh->check_state = head_sh->check_state;
4577 sh->reconstruct_state = head_sh->reconstruct_state;
NeilBrownfb642b92015-05-21 12:00:47 +10004578 for (i = 0; i < sh->disks; i++) {
4579 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4580 do_wakeup = 1;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004581 sh->dev[i].flags = head_sh->dev[i].flags &
4582 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
NeilBrownfb642b92015-05-21 12:00:47 +10004583 }
shli@kernel.org72ac7332014-12-15 12:57:03 +11004584 spin_lock_irq(&sh->stripe_lock);
4585 sh->batch_head = NULL;
4586 spin_unlock_irq(&sh->stripe_lock);
NeilBrown3960ce72015-05-21 12:20:36 +10004587 if (handle_flags == 0 ||
4588 sh->state & handle_flags)
4589 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07004590 raid5_release_stripe(sh);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004591 }
NeilBrownfb642b92015-05-21 12:00:47 +10004592 spin_lock_irq(&head_sh->stripe_lock);
4593 head_sh->batch_head = NULL;
4594 spin_unlock_irq(&head_sh->stripe_lock);
4595 for (i = 0; i < head_sh->disks; i++)
4596 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4597 do_wakeup = 1;
NeilBrown3960ce72015-05-21 12:20:36 +10004598 if (head_sh->state & handle_flags)
4599 set_bit(STRIPE_HANDLE, &head_sh->state);
NeilBrownfb642b92015-05-21 12:00:47 +10004600
4601 if (do_wakeup)
4602 wake_up(&head_sh->raid_conf->wait_for_overlap);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004603}
4604
NeilBrowncc940152011-07-26 11:35:35 +10004605static void handle_stripe(struct stripe_head *sh)
4606{
4607 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11004608 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10004609 int i;
NeilBrown84789552011-07-27 11:00:36 +10004610 int prexor;
4611 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004612 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10004613
4614 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11004615 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10004616 /* already being handled, ensure it gets handled
4617 * again when current action finishes */
4618 set_bit(STRIPE_HANDLE, &sh->state);
4619 return;
4620 }
4621
shli@kernel.org59fc6302014-12-15 12:57:03 +11004622 if (clear_batch_ready(sh) ) {
4623 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
4624 return;
4625 }
4626
NeilBrown4e3d62f2015-05-21 11:50:16 +10004627 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
NeilBrown3960ce72015-05-21 12:20:36 +10004628 break_stripe_batch_list(sh, 0);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004629
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004630 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
NeilBrownf8dfcff2013-03-12 12:18:06 +11004631 spin_lock(&sh->stripe_lock);
4632 /* Cannot process 'sync' concurrently with 'discard' */
4633 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
4634 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4635 set_bit(STRIPE_SYNCING, &sh->state);
4636 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10004637 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004638 }
4639 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10004640 }
4641 clear_bit(STRIPE_DELAYED, &sh->state);
4642
4643 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4644 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4645 (unsigned long long)sh->sector, sh->state,
4646 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4647 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10004648
NeilBrownacfe7262011-07-27 11:00:36 +10004649 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10004650
Shaohua Lib70abcb2015-08-13 14:31:58 -07004651 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
4652 goto finish;
4653
NeilBrownbc2607f2011-07-28 11:39:22 +10004654 if (s.handle_bad_blocks) {
4655 set_bit(STRIPE_HANDLE, &sh->state);
4656 goto finish;
4657 }
4658
NeilBrown474af965fe2011-07-27 11:00:36 +10004659 if (unlikely(s.blocked_rdev)) {
4660 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11004661 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10004662 set_bit(STRIPE_HANDLE, &sh->state);
4663 goto finish;
4664 }
4665 /* There is nothing for the blocked_rdev to block */
4666 rdev_dec_pending(s.blocked_rdev, conf->mddev);
4667 s.blocked_rdev = NULL;
4668 }
4669
4670 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4671 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4672 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4673 }
4674
4675 pr_debug("locked=%d uptodate=%d to_read=%d"
4676 " to_write=%d failed=%d failed_num=%d,%d\n",
4677 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4678 s.failed_num[0], s.failed_num[1]);
4679 /* check if the array has lost more than max_degraded devices and,
4680 * if so, some requests might need to be failed.
4681 */
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004682 if (s.failed > conf->max_degraded || s.log_failed) {
NeilBrown9a3f5302011-11-08 16:22:01 +11004683 sh->check_state = 0;
4684 sh->reconstruct_state = 0;
NeilBrown626f2092015-05-22 14:03:10 +10004685 break_stripe_batch_list(sh, 0);
NeilBrown9a3f5302011-11-08 16:22:01 +11004686 if (s.to_read+s.to_write+s.written)
4687 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11004688 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11004689 handle_failed_sync(conf, sh, &s);
4690 }
NeilBrown474af965fe2011-07-27 11:00:36 +10004691
NeilBrown84789552011-07-27 11:00:36 +10004692 /* Now we check to see if any write operations have recently
4693 * completed
4694 */
4695 prexor = 0;
4696 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4697 prexor = 1;
4698 if (sh->reconstruct_state == reconstruct_state_drain_result ||
4699 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
4700 sh->reconstruct_state = reconstruct_state_idle;
4701
4702 /* All the 'written' buffers and the parity block are ready to
4703 * be written back to disk
4704 */
Shaohua Li9e4447682012-10-11 13:49:49 +11004705 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
4706 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004707 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11004708 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
4709 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004710 for (i = disks; i--; ) {
4711 struct r5dev *dev = &sh->dev[i];
4712 if (test_bit(R5_LOCKED, &dev->flags) &&
4713 (i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08004714 dev->written || test_bit(R5_InJournal,
4715 &dev->flags))) {
NeilBrown84789552011-07-27 11:00:36 +10004716 pr_debug("Writing block %d\n", i);
4717 set_bit(R5_Wantwrite, &dev->flags);
4718 if (prexor)
4719 continue;
NeilBrown9c4bdf62014-08-13 09:57:07 +10004720 if (s.failed > 1)
4721 continue;
NeilBrown84789552011-07-27 11:00:36 +10004722 if (!test_bit(R5_Insync, &dev->flags) ||
4723 ((i == sh->pd_idx || i == sh->qd_idx) &&
4724 s.failed == 0))
4725 set_bit(STRIPE_INSYNC, &sh->state);
4726 }
4727 }
4728 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4729 s.dec_preread_active = 1;
4730 }
4731
NeilBrownef5b7c62012-11-22 09:13:36 +11004732 /*
4733 * might be able to return some write requests if the parity blocks
4734 * are safe, or on a failed drive
4735 */
4736 pdev = &sh->dev[sh->pd_idx];
4737 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
4738 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
4739 qdev = &sh->dev[sh->qd_idx];
4740 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
4741 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
4742 || conf->level < 6;
4743
4744 if (s.written &&
4745 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
4746 && !test_bit(R5_LOCKED, &pdev->flags)
4747 && (test_bit(R5_UPTODATE, &pdev->flags) ||
4748 test_bit(R5_Discard, &pdev->flags))))) &&
4749 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
4750 && !test_bit(R5_LOCKED, &qdev->flags)
4751 && (test_bit(R5_UPTODATE, &qdev->flags) ||
4752 test_bit(R5_Discard, &qdev->flags))))))
4753 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
4754
Song Liu1e6d6902016-11-17 15:24:39 -08004755 if (s.just_cached)
4756 r5c_handle_cached_data_endio(conf, sh, disks, &s.return_bi);
4757 r5l_stripe_write_finished(sh);
4758
NeilBrownef5b7c62012-11-22 09:13:36 +11004759 /* Now we might consider reading some blocks, either to check/generate
4760 * parity, or to satisfy requests
4761 * or to load a block that is being partially written.
4762 */
4763 if (s.to_read || s.non_overwrite
4764 || (conf->level == 6 && s.to_write && s.failed)
4765 || (s.syncing && (s.uptodate + s.compute < disks))
4766 || s.replacing
4767 || s.expanding)
4768 handle_stripe_fill(sh, &s, disks);
4769
Song Liu2ded3702016-11-17 15:24:38 -08004770 /*
4771 * When the stripe finishes full journal write cycle (write to journal
4772 * and raid disk), this is the clean up procedure so it is ready for
4773 * next operation.
4774 */
4775 r5c_finish_stripe_write_out(conf, sh, &s);
4776
4777 /*
4778 * Now to consider new write requests, cache write back and what else,
4779 * if anything should be read. We do not handle new writes when:
NeilBrown84789552011-07-27 11:00:36 +10004780 * 1/ A 'write' operation (copy+xor) is already in flight.
4781 * 2/ A 'check' operation is in flight, as it may clobber the parity
4782 * block.
Song Liu2ded3702016-11-17 15:24:38 -08004783 * 3/ A r5c cache log write is in flight.
NeilBrown84789552011-07-27 11:00:36 +10004784 */
Song Liu2ded3702016-11-17 15:24:38 -08004785
4786 if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
4787 if (!r5c_is_writeback(conf->log)) {
4788 if (s.to_write)
4789 handle_stripe_dirtying(conf, sh, &s, disks);
4790 } else { /* write back cache */
4791 int ret = 0;
4792
4793 /* First, try handle writes in caching phase */
4794 if (s.to_write)
4795 ret = r5c_try_caching_write(conf, sh, &s,
4796 disks);
4797 /*
4798 * If caching phase failed: ret == -EAGAIN
4799 * OR
4800 * stripe under reclaim: !caching && injournal
4801 *
4802 * fall back to handle_stripe_dirtying()
4803 */
4804 if (ret == -EAGAIN ||
4805 /* stripe under reclaim: !caching && injournal */
4806 (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
Song Liud7bd3982016-11-23 22:50:39 -08004807 s.injournal > 0)) {
4808 ret = handle_stripe_dirtying(conf, sh, &s,
4809 disks);
4810 if (ret == -EAGAIN)
4811 goto finish;
4812 }
Song Liu2ded3702016-11-17 15:24:38 -08004813 }
4814 }
NeilBrown84789552011-07-27 11:00:36 +10004815
4816 /* maybe we need to check and possibly fix the parity for this stripe
4817 * Any reads will already have been scheduled, so we just see if enough
4818 * data is available. The parity check is held off while parity
4819 * dependent operations are in flight.
4820 */
4821 if (sh->check_state ||
4822 (s.syncing && s.locked == 0 &&
4823 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
4824 !test_bit(STRIPE_INSYNC, &sh->state))) {
4825 if (conf->level == 6)
4826 handle_parity_checks6(conf, sh, &s, disks);
4827 else
4828 handle_parity_checks5(conf, sh, &s, disks);
4829 }
NeilBrownc5a31002011-07-27 11:00:36 +10004830
NeilBrownf94c0b62013-07-22 12:57:21 +10004831 if ((s.replacing || s.syncing) && s.locked == 0
4832 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
4833 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11004834 /* Write out to replacement devices where possible */
4835 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10004836 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
4837 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11004838 set_bit(R5_WantReplace, &sh->dev[i].flags);
4839 set_bit(R5_LOCKED, &sh->dev[i].flags);
4840 s.locked++;
4841 }
NeilBrownf94c0b62013-07-22 12:57:21 +10004842 if (s.replacing)
4843 set_bit(STRIPE_INSYNC, &sh->state);
4844 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11004845 }
4846 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10004847 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11004848 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10004849 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4850 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004851 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
4852 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10004853 }
4854
4855 /* If the failed drives are just a ReadError, then we might need
4856 * to progress the repair/check process
4857 */
4858 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
4859 for (i = 0; i < s.failed; i++) {
4860 struct r5dev *dev = &sh->dev[s.failed_num[i]];
4861 if (test_bit(R5_ReadError, &dev->flags)
4862 && !test_bit(R5_LOCKED, &dev->flags)
4863 && test_bit(R5_UPTODATE, &dev->flags)
4864 ) {
4865 if (!test_bit(R5_ReWrite, &dev->flags)) {
4866 set_bit(R5_Wantwrite, &dev->flags);
4867 set_bit(R5_ReWrite, &dev->flags);
4868 set_bit(R5_LOCKED, &dev->flags);
4869 s.locked++;
4870 } else {
4871 /* let's read it back */
4872 set_bit(R5_Wantread, &dev->flags);
4873 set_bit(R5_LOCKED, &dev->flags);
4874 s.locked++;
4875 }
4876 }
4877 }
4878
NeilBrown3687c062011-07-27 11:00:36 +10004879 /* Finish reconstruct operations initiated by the expansion process */
4880 if (sh->reconstruct_state == reconstruct_state_result) {
4881 struct stripe_head *sh_src
Shaohua Li6d036f72015-08-13 14:31:57 -07004882 = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrown3687c062011-07-27 11:00:36 +10004883 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
4884 /* sh cannot be written until sh_src has been read.
4885 * so arrange for sh to be delayed a little
4886 */
4887 set_bit(STRIPE_DELAYED, &sh->state);
4888 set_bit(STRIPE_HANDLE, &sh->state);
4889 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
4890 &sh_src->state))
4891 atomic_inc(&conf->preread_active_stripes);
Shaohua Li6d036f72015-08-13 14:31:57 -07004892 raid5_release_stripe(sh_src);
NeilBrown3687c062011-07-27 11:00:36 +10004893 goto finish;
4894 }
4895 if (sh_src)
Shaohua Li6d036f72015-08-13 14:31:57 -07004896 raid5_release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07004897
NeilBrown3687c062011-07-27 11:00:36 +10004898 sh->reconstruct_state = reconstruct_state_idle;
4899 clear_bit(STRIPE_EXPANDING, &sh->state);
4900 for (i = conf->raid_disks; i--; ) {
4901 set_bit(R5_Wantwrite, &sh->dev[i].flags);
4902 set_bit(R5_LOCKED, &sh->dev[i].flags);
4903 s.locked++;
4904 }
4905 }
4906
4907 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
4908 !sh->reconstruct_state) {
4909 /* Need to write out all blocks after computing parity */
4910 sh->disks = conf->raid_disks;
4911 stripe_set_idx(sh->sector, conf, 0, sh);
4912 schedule_reconstruction(sh, &s, 1, 1);
4913 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
4914 clear_bit(STRIPE_EXPAND_READY, &sh->state);
4915 atomic_dec(&conf->reshape_stripes);
4916 wake_up(&conf->wait_for_overlap);
4917 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4918 }
4919
4920 if (s.expanding && s.locked == 0 &&
4921 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
4922 handle_stripe_expansion(conf, sh);
4923
4924finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07004925 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10004926 if (unlikely(s.blocked_rdev)) {
4927 if (conf->mddev->external)
4928 md_wait_for_blocked_rdev(s.blocked_rdev,
4929 conf->mddev);
4930 else
4931 /* Internal metadata will immediately
4932 * be written by raid5d, so we don't
4933 * need to wait here.
4934 */
4935 rdev_dec_pending(s.blocked_rdev,
4936 conf->mddev);
4937 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004938
NeilBrownbc2607f2011-07-28 11:39:22 +10004939 if (s.handle_bad_blocks)
4940 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004941 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10004942 struct r5dev *dev = &sh->dev[i];
4943 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
4944 /* We own a safe reference to the rdev */
4945 rdev = conf->disks[i].rdev;
4946 if (!rdev_set_badblocks(rdev, sh->sector,
4947 STRIPE_SECTORS, 0))
4948 md_error(conf->mddev, rdev);
4949 rdev_dec_pending(rdev, conf->mddev);
4950 }
NeilBrownb84db562011-07-28 11:39:23 +10004951 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
4952 rdev = conf->disks[i].rdev;
4953 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004954 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10004955 rdev_dec_pending(rdev, conf->mddev);
4956 }
NeilBrown977df362011-12-23 10:17:53 +11004957 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
4958 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11004959 if (!rdev)
4960 /* rdev have been moved down */
4961 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11004962 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004963 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11004964 rdev_dec_pending(rdev, conf->mddev);
4965 }
NeilBrownbc2607f2011-07-28 11:39:22 +10004966 }
4967
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004968 if (s.ops_request)
4969 raid_run_ops(sh, s.ops_request);
4970
Dan Williamsf0e43bc2008-06-28 08:31:55 +10004971 ops_run_io(sh, &s);
4972
NeilBrownc5709ef2011-07-26 11:35:20 +10004973 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11004974 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02004975 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11004976 * have actually been submitted.
4977 */
4978 atomic_dec(&conf->preread_active_stripes);
4979 if (atomic_read(&conf->preread_active_stripes) <
4980 IO_THRESHOLD)
4981 md_wakeup_thread(conf->mddev->thread);
4982 }
4983
NeilBrownc3cce6c2015-08-14 12:47:33 +10004984 if (!bio_list_empty(&s.return_bi)) {
Shaohua Li29530792016-12-08 15:48:19 -08004985 if (test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) {
NeilBrownc3cce6c2015-08-14 12:47:33 +10004986 spin_lock_irq(&conf->device_lock);
4987 bio_list_merge(&conf->return_bi, &s.return_bi);
4988 spin_unlock_irq(&conf->device_lock);
4989 md_wakeup_thread(conf->mddev->thread);
4990 } else
4991 return_io(&s.return_bi);
4992 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004993
Dan Williams257a4b42011-11-08 16:22:06 +11004994 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07004995}
4996
NeilBrownd1688a62011-10-11 16:49:52 +11004997static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998{
4999 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
5000 while (!list_empty(&conf->delayed_list)) {
5001 struct list_head *l = conf->delayed_list.next;
5002 struct stripe_head *sh;
5003 sh = list_entry(l, struct stripe_head, lru);
5004 list_del_init(l);
5005 clear_bit(STRIPE_DELAYED, &sh->state);
5006 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5007 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005008 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08005009 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 }
NeilBrown482c0832011-04-18 18:25:42 +10005011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012}
5013
Shaohua Li566c09c2013-11-14 15:16:17 +11005014static void activate_bit_delay(struct r5conf *conf,
5015 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07005016{
5017 /* device_lock is held */
5018 struct list_head head;
5019 list_add(&head, &conf->bitmap_list);
5020 list_del_init(&conf->bitmap_list);
5021 while (!list_empty(&head)) {
5022 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11005023 int hash;
NeilBrown72626682005-09-09 16:23:54 -07005024 list_del_init(&sh->lru);
5025 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11005026 hash = sh->hash_lock_index;
5027 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07005028 }
5029}
5030
NeilBrown5c675f82014-12-15 12:56:56 +11005031static int raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07005032{
NeilBrownd1688a62011-10-11 16:49:52 +11005033 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07005034
5035 /* No difference between reads and writes. Just check
5036 * how busy the stripe_cache is
5037 */
NeilBrown3fa841d2009-09-23 18:10:29 +10005038
NeilBrown54233992015-02-26 12:21:04 +11005039 if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state))
NeilBrownf022b2f2006-10-03 01:15:56 -07005040 return 1;
Song Liua39f7af2016-11-17 15:24:40 -08005041
5042 /* Also checks whether there is pressure on r5cache log space */
5043 if (test_bit(R5C_LOG_TIGHT, &conf->cache_state))
5044 return 1;
NeilBrownf022b2f2006-10-03 01:15:56 -07005045 if (conf->quiesce)
5046 return 1;
Shaohua Li4bda5562013-11-14 15:16:17 +11005047 if (atomic_read(&conf->empty_inactive_list_nr))
NeilBrownf022b2f2006-10-03 01:15:56 -07005048 return 1;
5049
5050 return 0;
5051}
5052
NeilBrownfd01b882011-10-11 16:47:53 +11005053static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005054{
NeilBrown3cb5edf2015-07-15 17:24:17 +10005055 struct r5conf *conf = mddev->private;
Kent Overstreet4f024f32013-10-11 15:44:27 -07005056 sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev);
NeilBrown3cb5edf2015-07-15 17:24:17 +10005057 unsigned int chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005058 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005059
NeilBrown3cb5edf2015-07-15 17:24:17 +10005060 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005061 return chunk_sectors >=
5062 ((sector & (chunk_sectors - 1)) + bio_sectors);
5063}
5064
5065/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005066 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
5067 * later sampled by raid5d.
5068 */
NeilBrownd1688a62011-10-11 16:49:52 +11005069static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005070{
5071 unsigned long flags;
5072
5073 spin_lock_irqsave(&conf->device_lock, flags);
5074
5075 bi->bi_next = conf->retry_read_aligned_list;
5076 conf->retry_read_aligned_list = bi;
5077
5078 spin_unlock_irqrestore(&conf->device_lock, flags);
5079 md_wakeup_thread(conf->mddev->thread);
5080}
5081
NeilBrownd1688a62011-10-11 16:49:52 +11005082static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005083{
5084 struct bio *bi;
5085
5086 bi = conf->retry_read_aligned;
5087 if (bi) {
5088 conf->retry_read_aligned = NULL;
5089 return bi;
5090 }
5091 bi = conf->retry_read_aligned_list;
5092 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08005093 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005094 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02005095 /*
5096 * this sets the active strip count to 1 and the processed
5097 * strip count to zero (upper 8 bits)
5098 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10005099 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005100 }
5101
5102 return bi;
5103}
5104
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005105/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005106 * The "raid5_align_endio" should check if the read succeeded and if it
5107 * did, call bio_endio on the original bio (having bio_put the new bio
5108 * first).
5109 * If the read failed..
5110 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005111static void raid5_align_endio(struct bio *bi)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005112{
5113 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11005114 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005115 struct r5conf *conf;
NeilBrown3cb03002011-10-11 16:45:26 +11005116 struct md_rdev *rdev;
Sasha Levin9b81c842015-08-10 19:05:18 -04005117 int error = bi->bi_error;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005118
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005119 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005120
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005121 rdev = (void*)raid_bi->bi_next;
5122 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11005123 mddev = rdev->mddev;
5124 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005125
5126 rdev_dec_pending(rdev, conf->mddev);
5127
Sasha Levin9b81c842015-08-10 19:05:18 -04005128 if (!error) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005129 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
5130 raid_bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005131 bio_endio(raid_bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005132 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10005133 wake_up(&conf->wait_for_quiescent);
NeilBrown6712ecf2007-09-27 12:47:43 +02005134 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005135 }
5136
Dan Williams45b42332007-07-09 11:56:43 -07005137 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005138
5139 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005140}
5141
Ming Lin7ef6b122015-05-06 22:51:24 -07005142static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005143{
NeilBrownd1688a62011-10-11 16:49:52 +11005144 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11005145 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005146 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11005147 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11005148 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005149
5150 if (!in_chunk_boundary(mddev, raid_bio)) {
Ming Lin7ef6b122015-05-06 22:51:24 -07005151 pr_debug("%s: non aligned\n", __func__);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005152 return 0;
5153 }
5154 /*
Ming Leid7a10302017-02-14 23:29:03 +08005155 * use bio_clone_fast to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005156 */
Ming Leid7a10302017-02-14 23:29:03 +08005157 align_bi = bio_clone_fast(raid_bio, GFP_NOIO, mddev->bio_set);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005158 if (!align_bi)
5159 return 0;
5160 /*
5161 * set bi_end_io to a new function, and set bi_private to the
5162 * original bio.
5163 */
5164 align_bi->bi_end_io = raid5_align_endio;
5165 align_bi->bi_private = raid_bio;
5166 /*
5167 * compute position
5168 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005169 align_bi->bi_iter.bi_sector =
5170 raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
5171 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005172
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005173 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005174 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11005175 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
5176 if (!rdev || test_bit(Faulty, &rdev->flags) ||
5177 rdev->recovery_offset < end_sector) {
5178 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
5179 if (rdev &&
5180 (test_bit(Faulty, &rdev->flags) ||
5181 !(test_bit(In_sync, &rdev->flags) ||
5182 rdev->recovery_offset >= end_sector)))
5183 rdev = NULL;
5184 }
Song Liu03b047f2017-01-11 13:39:14 -08005185
5186 if (r5c_big_stripe_cached(conf, align_bi->bi_iter.bi_sector)) {
5187 rcu_read_unlock();
5188 bio_put(align_bi);
5189 return 0;
5190 }
5191
NeilBrown671488c2011-12-23 10:17:52 +11005192 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10005193 sector_t first_bad;
5194 int bad_sectors;
5195
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005196 atomic_inc(&rdev->nr_pending);
5197 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005198 raid_bio->bi_next = (void*)rdev;
5199 align_bi->bi_bdev = rdev->bdev;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06005200 bio_clear_flag(align_bi, BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005201
Kent Overstreet7140aaf2013-09-25 13:37:01 -07005202 if (is_badblock(rdev, align_bi->bi_iter.bi_sector,
Kent Overstreet4f024f32013-10-11 15:44:27 -07005203 bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10005204 &first_bad, &bad_sectors)) {
Neil Brown387bb172007-02-08 14:20:29 -08005205 bio_put(align_bi);
5206 rdev_dec_pending(rdev, mddev);
5207 return 0;
5208 }
5209
majianpeng6c0544e2012-06-12 08:31:10 +08005210 /* No reshape active, so we can trust rdev->data_offset */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005211 align_bi->bi_iter.bi_sector += rdev->data_offset;
majianpeng6c0544e2012-06-12 08:31:10 +08005212
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005213 spin_lock_irq(&conf->device_lock);
Yuanhan Liub1b46482015-05-08 18:19:06 +10005214 wait_event_lock_irq(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005215 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01005216 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005217 atomic_inc(&conf->active_aligned_reads);
5218 spin_unlock_irq(&conf->device_lock);
5219
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005220 if (mddev->gendisk)
5221 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
5222 align_bi, disk_devt(mddev->gendisk),
Kent Overstreet4f024f32013-10-11 15:44:27 -07005223 raid_bio->bi_iter.bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005224 generic_make_request(align_bi);
5225 return 1;
5226 } else {
5227 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005228 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005229 return 0;
5230 }
5231}
5232
Ming Lin7ef6b122015-05-06 22:51:24 -07005233static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
5234{
5235 struct bio *split;
5236
5237 do {
5238 sector_t sector = raid_bio->bi_iter.bi_sector;
5239 unsigned chunk_sects = mddev->chunk_sectors;
5240 unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
5241
5242 if (sectors < bio_sectors(raid_bio)) {
5243 split = bio_split(raid_bio, sectors, GFP_NOIO, fs_bio_set);
5244 bio_chain(split, raid_bio);
5245 } else
5246 split = raid_bio;
5247
5248 if (!raid5_read_one_chunk(mddev, split)) {
5249 if (split != raid_bio)
5250 generic_make_request(raid_bio);
5251 return split;
5252 }
5253 } while (split != raid_bio);
5254
5255 return NULL;
5256}
5257
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005258/* __get_priority_stripe - get the next stripe to process
5259 *
5260 * Full stripe writes are allowed to pass preread active stripes up until
5261 * the bypass_threshold is exceeded. In general the bypass_count
5262 * increments when the handle_list is handled before the hold_list; however, it
5263 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
5264 * stripe with in flight i/o. The bypass_count will be reset when the
5265 * head of the hold_list has changed, i.e. the head was promoted to the
5266 * handle_list.
5267 */
Shaohua Li851c30c2013-08-28 14:30:16 +08005268static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005269{
Shaohua Li535ae4e2017-02-15 19:37:32 -08005270 struct stripe_head *sh, *tmp;
Shaohua Li851c30c2013-08-28 14:30:16 +08005271 struct list_head *handle_list = NULL;
Shaohua Li535ae4e2017-02-15 19:37:32 -08005272 struct r5worker_group *wg;
5273 bool second_try = !r5c_is_writeback(conf->log);
5274 bool try_loprio = test_bit(R5C_LOG_TIGHT, &conf->cache_state);
Shaohua Li851c30c2013-08-28 14:30:16 +08005275
Shaohua Li535ae4e2017-02-15 19:37:32 -08005276again:
5277 wg = NULL;
5278 sh = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005279 if (conf->worker_cnt_per_group == 0) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005280 handle_list = try_loprio ? &conf->loprio_list :
5281 &conf->handle_list;
Shaohua Li851c30c2013-08-28 14:30:16 +08005282 } else if (group != ANY_GROUP) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005283 handle_list = try_loprio ? &conf->worker_groups[group].loprio_list :
5284 &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005285 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08005286 } else {
5287 int i;
5288 for (i = 0; i < conf->group_cnt; i++) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005289 handle_list = try_loprio ? &conf->worker_groups[i].loprio_list :
5290 &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005291 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08005292 if (!list_empty(handle_list))
5293 break;
5294 }
5295 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005296
5297 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5298 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08005299 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005300 list_empty(&conf->hold_list) ? "empty" : "busy",
5301 atomic_read(&conf->pending_full_writes), conf->bypass_count);
5302
Shaohua Li851c30c2013-08-28 14:30:16 +08005303 if (!list_empty(handle_list)) {
5304 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005305
5306 if (list_empty(&conf->hold_list))
5307 conf->bypass_count = 0;
5308 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
5309 if (conf->hold_list.next == conf->last_hold)
5310 conf->bypass_count++;
5311 else {
5312 conf->last_hold = conf->hold_list.next;
5313 conf->bypass_count -= conf->bypass_threshold;
5314 if (conf->bypass_count < 0)
5315 conf->bypass_count = 0;
5316 }
5317 }
5318 } else if (!list_empty(&conf->hold_list) &&
5319 ((conf->bypass_threshold &&
5320 conf->bypass_count > conf->bypass_threshold) ||
5321 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08005322
5323 list_for_each_entry(tmp, &conf->hold_list, lru) {
5324 if (conf->worker_cnt_per_group == 0 ||
5325 group == ANY_GROUP ||
5326 !cpu_online(tmp->cpu) ||
5327 cpu_to_group(tmp->cpu) == group) {
5328 sh = tmp;
5329 break;
5330 }
5331 }
5332
5333 if (sh) {
5334 conf->bypass_count -= conf->bypass_threshold;
5335 if (conf->bypass_count < 0)
5336 conf->bypass_count = 0;
5337 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08005338 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005339 }
5340
Shaohua Li535ae4e2017-02-15 19:37:32 -08005341 if (!sh) {
5342 if (second_try)
5343 return NULL;
5344 second_try = true;
5345 try_loprio = !try_loprio;
5346 goto again;
5347 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005348
Shaohua Libfc90cb2013-08-29 15:40:32 +08005349 if (wg) {
5350 wg->stripes_cnt--;
5351 sh->group = NULL;
5352 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005353 list_del_init(&sh->lru);
Shaohua Lic7a6d352014-04-15 09:12:54 +08005354 BUG_ON(atomic_inc_return(&sh->count) != 1);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005355 return sh;
5356}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005357
Shaohua Li8811b592012-08-02 08:33:00 +10005358struct raid5_plug_cb {
5359 struct blk_plug_cb cb;
5360 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11005361 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10005362};
5363
5364static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
5365{
5366 struct raid5_plug_cb *cb = container_of(
5367 blk_cb, struct raid5_plug_cb, cb);
5368 struct stripe_head *sh;
5369 struct mddev *mddev = cb->cb.data;
5370 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11005371 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11005372 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10005373
5374 if (cb->list.next && !list_empty(&cb->list)) {
5375 spin_lock_irq(&conf->device_lock);
5376 while (!list_empty(&cb->list)) {
5377 sh = list_first_entry(&cb->list, struct stripe_head, lru);
5378 list_del_init(&sh->lru);
5379 /*
5380 * avoid race release_stripe_plug() sees
5381 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5382 * is still in our list
5383 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005384 smp_mb__before_atomic();
Shaohua Li8811b592012-08-02 08:33:00 +10005385 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08005386 /*
5387 * STRIPE_ON_RELEASE_LIST could be set here. In that
5388 * case, the count is always > 1 here
5389 */
Shaohua Li566c09c2013-11-14 15:16:17 +11005390 hash = sh->hash_lock_index;
5391 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11005392 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10005393 }
5394 spin_unlock_irq(&conf->device_lock);
5395 }
Shaohua Li566c09c2013-11-14 15:16:17 +11005396 release_inactive_stripe_list(conf, cb->temp_inactive_list,
5397 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005398 if (mddev->queue)
5399 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10005400 kfree(cb);
5401}
5402
5403static void release_stripe_plug(struct mddev *mddev,
5404 struct stripe_head *sh)
5405{
5406 struct blk_plug_cb *blk_cb = blk_check_plugged(
5407 raid5_unplug, mddev,
5408 sizeof(struct raid5_plug_cb));
5409 struct raid5_plug_cb *cb;
5410
5411 if (!blk_cb) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005412 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005413 return;
5414 }
5415
5416 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
5417
Shaohua Li566c09c2013-11-14 15:16:17 +11005418 if (cb->list.next == NULL) {
5419 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10005420 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11005421 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5422 INIT_LIST_HEAD(cb->temp_inactive_list + i);
5423 }
Shaohua Li8811b592012-08-02 08:33:00 +10005424
5425 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
5426 list_add_tail(&sh->lru, &cb->list);
5427 else
Shaohua Li6d036f72015-08-13 14:31:57 -07005428 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005429}
5430
Shaohua Li620125f2012-10-11 13:49:05 +11005431static void make_discard_request(struct mddev *mddev, struct bio *bi)
5432{
5433 struct r5conf *conf = mddev->private;
5434 sector_t logical_sector, last_sector;
5435 struct stripe_head *sh;
5436 int remaining;
5437 int stripe_sectors;
5438
5439 if (mddev->reshape_position != MaxSector)
5440 /* Skip discard while reshape is happening */
5441 return;
5442
Kent Overstreet4f024f32013-10-11 15:44:27 -07005443 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
5444 last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9);
Shaohua Li620125f2012-10-11 13:49:05 +11005445
5446 bi->bi_next = NULL;
5447 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
5448
5449 stripe_sectors = conf->chunk_sectors *
5450 (conf->raid_disks - conf->max_degraded);
5451 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5452 stripe_sectors);
5453 sector_div(last_sector, stripe_sectors);
5454
5455 logical_sector *= conf->chunk_sectors;
5456 last_sector *= conf->chunk_sectors;
5457
5458 for (; logical_sector < last_sector;
5459 logical_sector += STRIPE_SECTORS) {
5460 DEFINE_WAIT(w);
5461 int d;
5462 again:
Shaohua Li6d036f72015-08-13 14:31:57 -07005463 sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
Shaohua Li620125f2012-10-11 13:49:05 +11005464 prepare_to_wait(&conf->wait_for_overlap, &w,
5465 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005466 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5467 if (test_bit(STRIPE_SYNCING, &sh->state)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005468 raid5_release_stripe(sh);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005469 schedule();
5470 goto again;
5471 }
5472 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11005473 spin_lock_irq(&sh->stripe_lock);
5474 for (d = 0; d < conf->raid_disks; d++) {
5475 if (d == sh->pd_idx || d == sh->qd_idx)
5476 continue;
5477 if (sh->dev[d].towrite || sh->dev[d].toread) {
5478 set_bit(R5_Overlap, &sh->dev[d].flags);
5479 spin_unlock_irq(&sh->stripe_lock);
Shaohua Li6d036f72015-08-13 14:31:57 -07005480 raid5_release_stripe(sh);
Shaohua Li620125f2012-10-11 13:49:05 +11005481 schedule();
5482 goto again;
5483 }
5484 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11005485 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11005486 finish_wait(&conf->wait_for_overlap, &w);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005487 sh->overwrite_disks = 0;
Shaohua Li620125f2012-10-11 13:49:05 +11005488 for (d = 0; d < conf->raid_disks; d++) {
5489 if (d == sh->pd_idx || d == sh->qd_idx)
5490 continue;
5491 sh->dev[d].towrite = bi;
5492 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
5493 raid5_inc_bi_active_stripes(bi);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005494 sh->overwrite_disks++;
Shaohua Li620125f2012-10-11 13:49:05 +11005495 }
5496 spin_unlock_irq(&sh->stripe_lock);
5497 if (conf->mddev->bitmap) {
5498 for (d = 0;
5499 d < conf->raid_disks - conf->max_degraded;
5500 d++)
5501 bitmap_startwrite(mddev->bitmap,
5502 sh->sector,
5503 STRIPE_SECTORS,
5504 0);
5505 sh->bm_seq = conf->seq_flush + 1;
5506 set_bit(STRIPE_BIT_DELAY, &sh->state);
5507 }
5508
5509 set_bit(STRIPE_HANDLE, &sh->state);
5510 clear_bit(STRIPE_DELAYED, &sh->state);
5511 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5512 atomic_inc(&conf->preread_active_stripes);
5513 release_stripe_plug(mddev, sh);
5514 }
5515
5516 remaining = raid5_dec_bi_active_stripes(bi);
5517 if (remaining == 0) {
5518 md_write_end(mddev);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005519 bio_endio(bi);
Shaohua Li620125f2012-10-11 13:49:05 +11005520 }
5521}
5522
Shaohua Li849674e2016-01-20 13:52:20 -08005523static void raid5_make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524{
NeilBrownd1688a62011-10-11 16:49:52 +11005525 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11005526 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 sector_t new_sector;
5528 sector_t logical_sector, last_sector;
5529 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01005530 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11005531 int remaining;
Shaohua Li27c0f682014-04-09 11:25:47 +08005532 DEFINE_WAIT(w);
5533 bool do_prepare;
Song Liu3bddb7f2016-11-18 16:46:50 -08005534 bool do_flush = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
Jens Axboe1eff9d32016-08-05 15:35:16 -06005536 if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
Shaohua Li828cbe92015-09-02 13:49:49 -07005537 int ret = r5l_handle_flush_request(conf->log, bi);
5538
5539 if (ret == 0)
5540 return;
5541 if (ret == -ENODEV) {
5542 md_flush_request(mddev, bi);
5543 return;
5544 }
5545 /* ret == -EAGAIN, fallback */
Song Liu3bddb7f2016-11-18 16:46:50 -08005546 /*
5547 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
5548 * we need to flush journal device
5549 */
5550 do_flush = bi->bi_opf & REQ_PREFLUSH;
NeilBrowne5dcdd82005-09-09 16:23:41 -07005551 }
5552
NeilBrown3d310eb2005-06-21 17:17:26 -07005553 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07005554
Eric Mei9ffc8f72015-03-18 23:39:11 -06005555 /*
5556 * If array is degraded, better not do chunk aligned read because
5557 * later we might have to read it again in order to reconstruct
5558 * data on failed drives.
5559 */
5560 if (rw == READ && mddev->degraded == 0 &&
Ming Lin7ef6b122015-05-06 22:51:24 -07005561 mddev->reshape_position == MaxSector) {
5562 bi = chunk_aligned_read(mddev, bi);
5563 if (!bi)
5564 return;
5565 }
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08005566
Mike Christie796a5cf2016-06-05 14:32:07 -05005567 if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
Shaohua Li620125f2012-10-11 13:49:05 +11005568 make_discard_request(mddev, bi);
5569 return;
5570 }
5571
Kent Overstreet4f024f32013-10-11 15:44:27 -07005572 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005573 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 bi->bi_next = NULL;
5575 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07005576
Shaohua Li27c0f682014-04-09 11:25:47 +08005577 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005579 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10005580 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08005581
Shaohua Li27c0f682014-04-09 11:25:47 +08005582 do_prepare = false;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005583 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10005584 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11005585 previous = 0;
Shaohua Li27c0f682014-04-09 11:25:47 +08005586 if (do_prepare)
5587 prepare_to_wait(&conf->wait_for_overlap, &w,
5588 TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005589 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11005590 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08005591 * 64bit on a 32bit platform, and so it might be
5592 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02005593 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08005594 * the lock is dropped, so once we get a reference
5595 * to the stripe that we think it is, we will have
5596 * to check again.
5597 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005598 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005599 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005600 ? logical_sector < conf->reshape_progress
5601 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005602 previous = 1;
5603 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10005604 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005605 ? logical_sector < conf->reshape_safe
5606 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08005607 spin_unlock_irq(&conf->device_lock);
5608 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005609 do_prepare = true;
NeilBrownb578d552006-03-27 01:18:12 -08005610 goto retry;
5611 }
5612 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005613 spin_unlock_irq(&conf->device_lock);
5614 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005615
NeilBrown112bf892009-03-31 14:39:38 +11005616 new_sector = raid5_compute_sector(conf, logical_sector,
5617 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11005618 &dd_idx, NULL);
Shaohua Li849674e2016-01-20 13:52:20 -08005619 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10005620 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 (unsigned long long)logical_sector);
5622
Shaohua Li6d036f72015-08-13 14:31:57 -07005623 sh = raid5_get_active_stripe(conf, new_sector, previous,
Jens Axboe1eff9d32016-08-05 15:35:16 -06005624 (bi->bi_opf & REQ_RAHEAD), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11005626 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005627 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08005628 * stripe, so we must do the range check again.
5629 * Expansion could still move past after this
5630 * test, but as we are holding a reference to
5631 * 'sh', we know that if that happens,
5632 * STRIPE_EXPANDING will get set and the expansion
5633 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005634 */
5635 int must_retry = 0;
5636 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005637 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11005638 ? logical_sector >= conf->reshape_progress
5639 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005640 /* mismatch, need to try again */
5641 must_retry = 1;
5642 spin_unlock_irq(&conf->device_lock);
5643 if (must_retry) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005644 raid5_release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07005645 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005646 do_prepare = true;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005647 goto retry;
5648 }
5649 }
NeilBrownc46501b2013-08-27 15:52:13 +10005650 if (read_seqcount_retry(&conf->gen_lock, seq)) {
5651 /* Might have got the wrong stripe_head
5652 * by accident
5653 */
Shaohua Li6d036f72015-08-13 14:31:57 -07005654 raid5_release_stripe(sh);
NeilBrownc46501b2013-08-27 15:52:13 +10005655 goto retry;
5656 }
NeilBrowne62e58a2009-07-01 13:15:35 +10005657
Namhyung Kimffd96e32011-07-18 17:38:51 +10005658 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10005659 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08005660 logical_sector < mddev->suspend_hi) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005661 raid5_release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10005662 /* As the suspend_* range is controlled by
5663 * userspace, we want an interruptible
5664 * wait.
5665 */
5666 flush_signals(current);
5667 prepare_to_wait(&conf->wait_for_overlap,
5668 &w, TASK_INTERRUPTIBLE);
5669 if (logical_sector >= mddev->suspend_lo &&
Shaohua Li27c0f682014-04-09 11:25:47 +08005670 logical_sector < mddev->suspend_hi) {
NeilBrowne62e58a2009-07-01 13:15:35 +10005671 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005672 do_prepare = true;
5673 }
NeilBrowne464eaf2006-03-27 01:18:14 -08005674 goto retry;
5675 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005676
5677 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
shli@kernel.orgda41ba62014-12-15 12:57:03 +11005678 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005679 /* Stripe is busy expanding or
5680 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 * and wait a while
5682 */
NeilBrown482c0832011-04-18 18:25:42 +10005683 md_wakeup_thread(mddev->thread);
Shaohua Li6d036f72015-08-13 14:31:57 -07005684 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005686 do_prepare = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 goto retry;
5688 }
Song Liu3bddb7f2016-11-18 16:46:50 -08005689 if (do_flush) {
5690 set_bit(STRIPE_R5C_PREFLUSH, &sh->state);
5691 /* we only need flush for one stripe */
5692 do_flush = false;
5693 }
5694
NeilBrown6ed30032008-02-06 01:40:00 -08005695 set_bit(STRIPE_HANDLE, &sh->state);
5696 clear_bit(STRIPE_DELAYED, &sh->state);
shli@kernel.org59fc6302014-12-15 12:57:03 +11005697 if ((!sh->batch_head || sh == sh->batch_head) &&
Jens Axboe1eff9d32016-08-05 15:35:16 -06005698 (bi->bi_opf & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11005699 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5700 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10005701 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 } else {
5703 /* cannot get stripe for read-ahead, just give-up */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005704 bi->bi_error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 break;
5706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 }
Shaohua Li27c0f682014-04-09 11:25:47 +08005708 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown7c13edc2011-04-18 18:25:43 +10005709
Shaohua Lie7836bd62012-07-19 16:01:31 +10005710 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08005711 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712
NeilBrown16a53ec2006-06-26 00:27:38 -07005713 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02005715
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005716 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
5717 bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005718 bio_endio(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720}
5721
NeilBrownfd01b882011-10-11 16:47:53 +11005722static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11005723
NeilBrownfd01b882011-10-11 16:47:53 +11005724static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725{
NeilBrown52c03292006-06-26 00:27:43 -07005726 /* reshaping is quite different to recovery/resync so it is
5727 * handled quite separately ... here.
5728 *
5729 * On each call to sync_request, we gather one chunk worth of
5730 * destination stripes and flag them as expanding.
5731 * Then we find all the source stripes and request reads.
5732 * As the reads complete, handle_stripe will copy the data
5733 * into the destination stripe and release that stripe.
5734 */
NeilBrownd1688a62011-10-11 16:49:52 +11005735 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005737 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08005738 int raid_disks = conf->previous_raid_disks;
5739 int data_disks = raid_disks - conf->max_degraded;
5740 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07005741 int i;
5742 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11005743 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11005744 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11005745 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11005746 struct list_head stripes;
NeilBrown92140482015-07-06 12:28:45 +10005747 sector_t retn;
NeilBrown52c03292006-06-26 00:27:43 -07005748
NeilBrownfef9c612009-03-31 15:16:46 +11005749 if (sector_nr == 0) {
5750 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10005751 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005752 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
5753 sector_nr = raid5_size(mddev, 0, 0)
5754 - conf->reshape_progress;
NeilBrown6cbd8142015-07-24 13:30:32 +10005755 } else if (mddev->reshape_backwards &&
5756 conf->reshape_progress == MaxSector) {
5757 /* shouldn't happen, but just in case, finish up.*/
5758 sector_nr = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +10005759 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005760 conf->reshape_progress > 0)
5761 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005762 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005763 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11005764 mddev->curr_resync_completed = sector_nr;
5765 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11005766 *skipped = 1;
NeilBrown92140482015-07-06 12:28:45 +10005767 retn = sector_nr;
5768 goto finish;
NeilBrownfef9c612009-03-31 15:16:46 +11005769 }
NeilBrown52c03292006-06-26 00:27:43 -07005770 }
5771
NeilBrown7a661382009-03-31 15:21:40 +11005772 /* We need to process a full chunk at a time.
5773 * If old and new chunk sizes differ, we need to process the
5774 * largest of these
5775 */
NeilBrown3cb5edf2015-07-15 17:24:17 +10005776
5777 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
NeilBrown7a661382009-03-31 15:21:40 +11005778
NeilBrownb5254dd2012-05-21 09:27:01 +10005779 /* We update the metadata at least every 10 seconds, or when
5780 * the data about to be copied would over-write the source of
5781 * the data at the front of the range. i.e. one new_stripe
5782 * along from reshape_progress new_maps to after where
5783 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07005784 */
NeilBrownfef9c612009-03-31 15:16:46 +11005785 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005786 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11005787 readpos = conf->reshape_progress;
5788 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005789 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08005790 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10005791 if (mddev->reshape_backwards) {
NeilBrownc74c0d72015-07-15 17:54:15 +10005792 BUG_ON(writepos < reshape_sectors);
5793 writepos -= reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11005794 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11005795 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11005796 } else {
NeilBrown7a661382009-03-31 15:21:40 +11005797 writepos += reshape_sectors;
NeilBrownc74c0d72015-07-15 17:54:15 +10005798 /* readpos and safepos are worst-case calculations.
5799 * A negative number is overly pessimistic, and causes
5800 * obvious problems for unsigned storage. So clip to 0.
5801 */
NeilBrowned37d832009-05-27 21:39:05 +10005802 readpos -= min_t(sector_t, reshape_sectors, readpos);
5803 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11005804 }
NeilBrown52c03292006-06-26 00:27:43 -07005805
NeilBrownb5254dd2012-05-21 09:27:01 +10005806 /* Having calculated the 'writepos' possibly use it
5807 * to set 'stripe_addr' which is where we will write to.
5808 */
5809 if (mddev->reshape_backwards) {
5810 BUG_ON(conf->reshape_progress == 0);
5811 stripe_addr = writepos;
5812 BUG_ON((mddev->dev_sectors &
5813 ~((sector_t)reshape_sectors - 1))
5814 - reshape_sectors - stripe_addr
5815 != sector_nr);
5816 } else {
5817 BUG_ON(writepos != sector_nr + reshape_sectors);
5818 stripe_addr = sector_nr;
5819 }
5820
NeilBrownc8f517c2009-03-31 15:28:40 +11005821 /* 'writepos' is the most advanced device address we might write.
5822 * 'readpos' is the least advanced device address we might read.
5823 * 'safepos' is the least address recorded in the metadata as having
5824 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10005825 * If there is a min_offset_diff, these are adjusted either by
5826 * increasing the safepos/readpos if diff is negative, or
5827 * increasing writepos if diff is positive.
5828 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11005829 * ensure safety in the face of a crash - that must be done by userspace
5830 * making a backup of the data. So in that case there is no particular
5831 * rush to update metadata.
5832 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5833 * update the metadata to advance 'safepos' to match 'readpos' so that
5834 * we can be safe in the event of a crash.
5835 * So we insist on updating metadata if safepos is behind writepos and
5836 * readpos is beyond writepos.
5837 * In any case, update the metadata every 10 seconds.
5838 * Maybe that number should be configurable, but I'm not sure it is
5839 * worth it.... maybe it could be a multiple of safemode_delay???
5840 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005841 if (conf->min_offset_diff < 0) {
5842 safepos += -conf->min_offset_diff;
5843 readpos += -conf->min_offset_diff;
5844 } else
5845 writepos += conf->min_offset_diff;
5846
NeilBrown2c810cd2012-05-21 09:27:00 +10005847 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11005848 ? (safepos > writepos && readpos < writepos)
5849 : (safepos < writepos && readpos > writepos)) ||
5850 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07005851 /* Cannot proceed until we've updated the superblock... */
5852 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005853 atomic_read(&conf->reshape_stripes)==0
5854 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5855 if (atomic_read(&conf->reshape_stripes) != 0)
5856 return 0;
NeilBrownfef9c612009-03-31 15:16:46 +11005857 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005858 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005859 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08005860 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown52c03292006-06-26 00:27:43 -07005861 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08005862 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11005863 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5864 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5865 return 0;
NeilBrown52c03292006-06-26 00:27:43 -07005866 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005867 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07005868 spin_unlock_irq(&conf->device_lock);
5869 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005870 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07005871 }
5872
NeilBrownab69ae12009-03-31 15:26:47 +11005873 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11005874 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07005875 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10005876 int skipped_disk = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07005877 sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005878 set_bit(STRIPE_EXPANDING, &sh->state);
5879 atomic_inc(&conf->reshape_stripes);
5880 /* If any of this stripe is beyond the end of the old
5881 * array, then we need to zero those blocks
5882 */
5883 for (j=sh->disks; j--;) {
5884 sector_t s;
5885 if (j == sh->pd_idx)
5886 continue;
NeilBrownf4168852007-02-28 20:11:53 -08005887 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11005888 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08005889 continue;
Shaohua Li6d036f72015-08-13 14:31:57 -07005890 s = raid5_compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11005891 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10005892 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07005893 continue;
5894 }
5895 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
5896 set_bit(R5_Expanded, &sh->dev[j].flags);
5897 set_bit(R5_UPTODATE, &sh->dev[j].flags);
5898 }
NeilBrowna9f326e2009-09-23 18:06:41 +10005899 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07005900 set_bit(STRIPE_EXPAND_READY, &sh->state);
5901 set_bit(STRIPE_HANDLE, &sh->state);
5902 }
NeilBrownab69ae12009-03-31 15:26:47 +11005903 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07005904 }
5905 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005906 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11005907 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005908 else
NeilBrown7a661382009-03-31 15:21:40 +11005909 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07005910 spin_unlock_irq(&conf->device_lock);
5911 /* Ok, those stripe are ready. We can start scheduling
5912 * reads on the source stripes.
5913 * The source stripes are determined by mapping the first and last
5914 * block on the destination stripes.
5915 */
NeilBrown52c03292006-06-26 00:27:43 -07005916 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11005917 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11005918 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07005919 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10005920 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10005921 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11005922 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11005923 if (last_sector >= mddev->dev_sectors)
5924 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07005925 while (first_sector <= last_sector) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005926 sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005927 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
5928 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07005929 raid5_release_stripe(sh);
NeilBrown52c03292006-06-26 00:27:43 -07005930 first_sector += STRIPE_SECTORS;
5931 }
NeilBrownab69ae12009-03-31 15:26:47 +11005932 /* Now that the sources are clearly marked, we can release
5933 * the destination stripes
5934 */
5935 while (!list_empty(&stripes)) {
5936 sh = list_entry(stripes.next, struct stripe_head, lru);
5937 list_del_init(&sh->lru);
Shaohua Li6d036f72015-08-13 14:31:57 -07005938 raid5_release_stripe(sh);
NeilBrownab69ae12009-03-31 15:26:47 +11005939 }
NeilBrownc6207272008-02-06 01:39:52 -08005940 /* If this takes us to the resync_max point where we have to pause,
5941 * then we need to write out the superblock.
5942 */
NeilBrown7a661382009-03-31 15:21:40 +11005943 sector_nr += reshape_sectors;
NeilBrown92140482015-07-06 12:28:45 +10005944 retn = reshape_sectors;
5945finish:
NeilBrownc5e19d92015-07-17 12:06:02 +10005946 if (mddev->curr_resync_completed > mddev->resync_max ||
5947 (sector_nr - mddev->curr_resync_completed) * 2
NeilBrownc03f6a12009-04-17 11:06:30 +10005948 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08005949 /* Cannot proceed until we've updated the superblock... */
5950 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005951 atomic_read(&conf->reshape_stripes) == 0
5952 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5953 if (atomic_read(&conf->reshape_stripes) != 0)
5954 goto ret;
NeilBrownfef9c612009-03-31 15:16:46 +11005955 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005956 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005957 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08005958 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownc6207272008-02-06 01:39:52 -08005959 md_wakeup_thread(mddev->thread);
5960 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08005961 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)
NeilBrownc91abf52013-11-19 12:02:01 +11005962 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5963 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5964 goto ret;
NeilBrownc6207272008-02-06 01:39:52 -08005965 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005966 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08005967 spin_unlock_irq(&conf->device_lock);
5968 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005969 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08005970 }
NeilBrownc91abf52013-11-19 12:02:01 +11005971ret:
NeilBrown92140482015-07-06 12:28:45 +10005972 return retn;
NeilBrown52c03292006-06-26 00:27:43 -07005973}
5974
Shaohua Li849674e2016-01-20 13:52:20 -08005975static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
5976 int *skipped)
NeilBrown52c03292006-06-26 00:27:43 -07005977{
NeilBrownd1688a62011-10-11 16:49:52 +11005978 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07005979 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11005980 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11005981 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07005982 int still_degraded = 0;
5983 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984
NeilBrown72626682005-09-09 16:23:54 -07005985 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11005987
NeilBrown29269552006-03-27 01:18:10 -08005988 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
5989 end_reshape(conf);
5990 return 0;
5991 }
NeilBrown72626682005-09-09 16:23:54 -07005992
5993 if (mddev->curr_resync < max_sector) /* aborted */
5994 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
5995 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07005996 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07005997 conf->fullsync = 0;
5998 bitmap_close_sync(mddev->bitmap);
5999
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 return 0;
6001 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08006002
NeilBrown64bd6602009-08-03 10:59:58 +10006003 /* Allow raid5_quiesce to complete */
6004 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
6005
NeilBrown52c03292006-06-26 00:27:43 -07006006 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6007 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08006008
NeilBrownc6207272008-02-06 01:39:52 -08006009 /* No need to check resync_max as we never do more than one
6010 * stripe, and as resync_max will always be on a chunk boundary,
6011 * if the check in md_do_sync didn't fire, there is no chance
6012 * of overstepping resync_max here
6013 */
6014
NeilBrown16a53ec2006-06-26 00:27:38 -07006015 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 * to resync, then assert that we are finished, because there is
6017 * nothing we can do.
6018 */
NeilBrown3285edf2006-06-26 00:27:55 -07006019 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07006020 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11006021 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07006022 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 return rv;
6024 }
majianpeng6f608042013-04-24 11:42:41 +10006025 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
6026 !conf->fullsync &&
6027 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
6028 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07006029 /* we can skip this block, and probably more */
6030 sync_blocks /= STRIPE_SECTORS;
6031 *skipped = 1;
6032 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
6033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10006035 bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
NeilBrownb47490c2008-02-06 01:39:50 -08006036
Shaohua Li6d036f72015-08-13 14:31:57 -07006037 sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 if (sh == NULL) {
Shaohua Li6d036f72015-08-13 14:31:57 -07006039 sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07006041 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08006043 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 }
NeilBrown16a53ec2006-06-26 00:27:38 -07006045 /* Need to check if array will still be degraded after recovery/resync
Eric Mei16d9cfa2015-01-06 09:35:02 -08006046 * Note in case of > 1 drive failures it's possible we're rebuilding
6047 * one drive while leaving another faulty drive in array.
NeilBrown16a53ec2006-06-26 00:27:38 -07006048 */
Eric Mei16d9cfa2015-01-06 09:35:02 -08006049 rcu_read_lock();
6050 for (i = 0; i < conf->raid_disks; i++) {
6051 struct md_rdev *rdev = ACCESS_ONCE(conf->disks[i].rdev);
6052
6053 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
NeilBrown16a53ec2006-06-26 00:27:38 -07006054 still_degraded = 1;
Eric Mei16d9cfa2015-01-06 09:35:02 -08006055 }
6056 rcu_read_unlock();
NeilBrown16a53ec2006-06-26 00:27:38 -07006057
6058 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
6059
NeilBrown83206d62011-07-26 11:19:49 +10006060 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Eivind Sarto053f5b62014-06-09 17:06:19 -07006061 set_bit(STRIPE_HANDLE, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062
Shaohua Li6d036f72015-08-13 14:31:57 -07006063 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064
6065 return STRIPE_SECTORS;
6066}
6067
NeilBrownd1688a62011-10-11 16:49:52 +11006068static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006069{
6070 /* We may not be able to submit a whole bio at once as there
6071 * may not be enough stripe_heads available.
6072 * We cannot pre-allocate enough stripe_heads as we may need
6073 * more than exist in the cache (if we allow ever large chunks).
6074 * So we do one stripe head at a time and record in
6075 * ->bi_hw_segments how many have been done.
6076 *
6077 * We *know* that this entire raid_bio is in one chunk, so
6078 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
6079 */
6080 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11006081 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006082 sector_t sector, logical_sector, last_sector;
6083 int scnt = 0;
6084 int remaining;
6085 int handled = 0;
6086
Kent Overstreet4f024f32013-10-11 15:44:27 -07006087 logical_sector = raid_bio->bi_iter.bi_sector &
6088 ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11006089 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11006090 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07006091 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006092
6093 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08006094 logical_sector += STRIPE_SECTORS,
6095 sector += STRIPE_SECTORS,
6096 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006097
Shaohua Lie7836bd62012-07-19 16:01:31 +10006098 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006099 /* already done this stripe */
6100 continue;
6101
Shaohua Li6d036f72015-08-13 14:31:57 -07006102 sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006103
6104 if (!sh) {
6105 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10006106 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006107 conf->retry_read_aligned = raid_bio;
6108 return handled;
6109 }
6110
shli@kernel.orgda41ba62014-12-15 12:57:03 +11006111 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07006112 raid5_release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10006113 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08006114 conf->retry_read_aligned = raid_bio;
6115 return handled;
6116 }
6117
majianpeng3f9e7c12012-07-31 10:04:21 +10006118 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07006119 handle_stripe(sh);
Shaohua Li6d036f72015-08-13 14:31:57 -07006120 raid5_release_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006121 handled++;
6122 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10006123 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07006124 if (remaining == 0) {
6125 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
6126 raid_bio, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02006127 bio_endio(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07006128 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006129 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10006130 wake_up(&conf->wait_for_quiescent);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006131 return handled;
6132}
6133
Shaohua Libfc90cb2013-08-29 15:40:32 +08006134static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11006135 struct r5worker *worker,
6136 struct list_head *temp_inactive_list)
Shaohua Li46a06402012-08-02 08:33:15 +10006137{
6138 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11006139 int i, batch_size = 0, hash;
6140 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10006141
6142 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08006143 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10006144 batch[batch_size++] = sh;
6145
Shaohua Li566c09c2013-11-14 15:16:17 +11006146 if (batch_size == 0) {
6147 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6148 if (!list_empty(temp_inactive_list + i))
6149 break;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006150 if (i == NR_STRIPE_HASH_LOCKS) {
6151 spin_unlock_irq(&conf->device_lock);
6152 r5l_flush_stripe_to_raid(conf->log);
6153 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006154 return batch_size;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006155 }
Shaohua Li566c09c2013-11-14 15:16:17 +11006156 release_inactive = true;
6157 }
Shaohua Li46a06402012-08-02 08:33:15 +10006158 spin_unlock_irq(&conf->device_lock);
6159
Shaohua Li566c09c2013-11-14 15:16:17 +11006160 release_inactive_stripe_list(conf, temp_inactive_list,
6161 NR_STRIPE_HASH_LOCKS);
6162
Shaohua Lia8c34f92015-09-02 13:49:46 -07006163 r5l_flush_stripe_to_raid(conf->log);
Shaohua Li566c09c2013-11-14 15:16:17 +11006164 if (release_inactive) {
6165 spin_lock_irq(&conf->device_lock);
6166 return 0;
6167 }
6168
Shaohua Li46a06402012-08-02 08:33:15 +10006169 for (i = 0; i < batch_size; i++)
6170 handle_stripe(batch[i]);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07006171 r5l_write_stripe_run(conf->log);
Shaohua Li46a06402012-08-02 08:33:15 +10006172
6173 cond_resched();
6174
6175 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006176 for (i = 0; i < batch_size; i++) {
6177 hash = batch[i]->hash_lock_index;
6178 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
6179 }
Shaohua Li46a06402012-08-02 08:33:15 +10006180 return batch_size;
6181}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006182
Shaohua Li851c30c2013-08-28 14:30:16 +08006183static void raid5_do_work(struct work_struct *work)
6184{
6185 struct r5worker *worker = container_of(work, struct r5worker, work);
6186 struct r5worker_group *group = worker->group;
6187 struct r5conf *conf = group->conf;
6188 int group_id = group - conf->worker_groups;
6189 int handled;
6190 struct blk_plug plug;
6191
6192 pr_debug("+++ raid5worker active\n");
6193
6194 blk_start_plug(&plug);
6195 handled = 0;
6196 spin_lock_irq(&conf->device_lock);
6197 while (1) {
6198 int batch_size, released;
6199
Shaohua Li566c09c2013-11-14 15:16:17 +11006200 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006201
Shaohua Li566c09c2013-11-14 15:16:17 +11006202 batch_size = handle_active_stripes(conf, group_id, worker,
6203 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08006204 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08006205 if (!batch_size && !released)
6206 break;
6207 handled += batch_size;
6208 }
6209 pr_debug("%d stripes handled\n", handled);
6210
6211 spin_unlock_irq(&conf->device_lock);
6212 blk_finish_plug(&plug);
6213
6214 pr_debug("--- raid5worker inactive\n");
6215}
6216
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217/*
6218 * This is our raid5 kernel thread.
6219 *
6220 * We scan the hash table for stripes which can be handled now.
6221 * During the scan, completed stripes are saved for us by the interrupt
6222 * handler, so that they will not have to wait for our next wakeup.
6223 */
Shaohua Li4ed87312012-10-11 13:34:00 +11006224static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225{
Shaohua Li4ed87312012-10-11 13:34:00 +11006226 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11006227 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006229 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230
Dan Williams45b42332007-07-09 11:56:43 -07006231 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232
6233 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234
NeilBrownc3cce6c2015-08-14 12:47:33 +10006235 if (!bio_list_empty(&conf->return_bi) &&
Shaohua Li29530792016-12-08 15:48:19 -08006236 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrownc3cce6c2015-08-14 12:47:33 +10006237 struct bio_list tmp = BIO_EMPTY_LIST;
6238 spin_lock_irq(&conf->device_lock);
Shaohua Li29530792016-12-08 15:48:19 -08006239 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
NeilBrownc3cce6c2015-08-14 12:47:33 +10006240 bio_list_merge(&tmp, &conf->return_bi);
6241 bio_list_init(&conf->return_bi);
6242 }
6243 spin_unlock_irq(&conf->device_lock);
6244 return_io(&tmp);
6245 }
6246
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006247 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 handled = 0;
6249 spin_lock_irq(&conf->device_lock);
6250 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006251 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08006252 int batch_size, released;
6253
Shaohua Li566c09c2013-11-14 15:16:17 +11006254 released = release_stripe_list(conf, conf->temp_inactive_list);
NeilBrownedbe83a2015-02-26 12:47:56 +11006255 if (released)
6256 clear_bit(R5_DID_ALLOC, &conf->cache_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257
NeilBrown0021b7b2012-07-31 09:08:14 +02006258 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10006259 !list_empty(&conf->bitmap_list)) {
6260 /* Now is a good time to flush some bitmap updates */
6261 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08006262 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07006263 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08006264 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10006265 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11006266 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07006267 }
NeilBrown0021b7b2012-07-31 09:08:14 +02006268 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07006269
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006270 while ((bio = remove_bio_from_retry(conf))) {
6271 int ok;
6272 spin_unlock_irq(&conf->device_lock);
6273 ok = retry_aligned_read(conf, bio);
6274 spin_lock_irq(&conf->device_lock);
6275 if (!ok)
6276 break;
6277 handled++;
6278 }
6279
Shaohua Li566c09c2013-11-14 15:16:17 +11006280 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
6281 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08006282 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 break;
Shaohua Li46a06402012-08-02 08:33:15 +10006284 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285
Shaohua Li29530792016-12-08 15:48:19 -08006286 if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) {
Shaohua Li46a06402012-08-02 08:33:15 +10006287 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10006288 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10006289 spin_lock_irq(&conf->device_lock);
6290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 }
Dan Williams45b42332007-07-09 11:56:43 -07006292 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293
6294 spin_unlock_irq(&conf->device_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006295 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
6296 mutex_trylock(&conf->cache_size_mutex)) {
NeilBrownedbe83a2015-02-26 12:47:56 +11006297 grow_one_stripe(conf, __GFP_NOWARN);
6298 /* Set flag even if allocation failed. This helps
6299 * slow down allocation requests when mem is short
6300 */
6301 set_bit(R5_DID_ALLOC, &conf->cache_state);
NeilBrown2d5b5692015-07-06 12:49:23 +10006302 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304
Shaohua Li765d7042017-01-04 09:33:23 -08006305 flush_deferred_bios(conf);
6306
Shaohua Li0576b1c2015-08-13 14:32:00 -07006307 r5l_flush_stripe_to_raid(conf->log);
6308
Dan Williamsc9f21aa2008-07-23 12:05:51 -07006309 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006310 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311
Dan Williams45b42332007-07-09 11:56:43 -07006312 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313}
6314
NeilBrown3f294f42005-11-08 21:39:25 -08006315static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006316raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006317{
NeilBrown7b1485b2014-12-15 12:56:59 +11006318 struct r5conf *conf;
6319 int ret = 0;
6320 spin_lock(&mddev->lock);
6321 conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006322 if (conf)
NeilBrownedbe83a2015-02-26 12:47:56 +11006323 ret = sprintf(page, "%d\n", conf->min_nr_stripes);
NeilBrown7b1485b2014-12-15 12:56:59 +11006324 spin_unlock(&mddev->lock);
6325 return ret;
NeilBrown3f294f42005-11-08 21:39:25 -08006326}
6327
NeilBrownc41d4ac2010-06-01 19:37:24 +10006328int
NeilBrownfd01b882011-10-11 16:47:53 +11006329raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10006330{
NeilBrownd1688a62011-10-11 16:49:52 +11006331 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10006332 int err;
6333
6334 if (size <= 16 || size > 32768)
6335 return -EINVAL;
NeilBrown486f0642015-02-25 12:10:35 +11006336
NeilBrownedbe83a2015-02-26 12:47:56 +11006337 conf->min_nr_stripes = size;
NeilBrown2d5b5692015-07-06 12:49:23 +10006338 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006339 while (size < conf->max_nr_stripes &&
6340 drop_one_stripe(conf))
6341 ;
NeilBrown2d5b5692015-07-06 12:49:23 +10006342 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006343
NeilBrownedbe83a2015-02-26 12:47:56 +11006344
NeilBrownc41d4ac2010-06-01 19:37:24 +10006345 err = md_allow_write(mddev);
6346 if (err)
6347 return err;
NeilBrown486f0642015-02-25 12:10:35 +11006348
NeilBrown2d5b5692015-07-06 12:49:23 +10006349 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006350 while (size > conf->max_nr_stripes)
6351 if (!grow_one_stripe(conf, GFP_KERNEL))
6352 break;
NeilBrown2d5b5692015-07-06 12:49:23 +10006353 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006354
NeilBrownc41d4ac2010-06-01 19:37:24 +10006355 return 0;
6356}
6357EXPORT_SYMBOL(raid5_set_cache_size);
6358
NeilBrown3f294f42005-11-08 21:39:25 -08006359static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006360raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08006361{
NeilBrown67918752014-12-15 12:57:01 +11006362 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006363 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07006364 int err;
6365
NeilBrown3f294f42005-11-08 21:39:25 -08006366 if (len >= PAGE_SIZE)
6367 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006368 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08006369 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006370 err = mddev_lock(mddev);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006371 if (err)
6372 return err;
NeilBrown67918752014-12-15 12:57:01 +11006373 conf = mddev->private;
6374 if (!conf)
6375 err = -ENODEV;
6376 else
6377 err = raid5_set_cache_size(mddev, new);
6378 mddev_unlock(mddev);
6379
6380 return err ?: len;
NeilBrown3f294f42005-11-08 21:39:25 -08006381}
NeilBrown007583c2005-11-08 21:39:30 -08006382
NeilBrown96de1e62005-11-08 21:39:39 -08006383static struct md_sysfs_entry
6384raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
6385 raid5_show_stripe_cache_size,
6386 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08006387
6388static ssize_t
Markus Stockhausend06f1912014-12-15 12:57:05 +11006389raid5_show_rmw_level(struct mddev *mddev, char *page)
6390{
6391 struct r5conf *conf = mddev->private;
6392 if (conf)
6393 return sprintf(page, "%d\n", conf->rmw_level);
6394 else
6395 return 0;
6396}
6397
6398static ssize_t
6399raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
6400{
6401 struct r5conf *conf = mddev->private;
6402 unsigned long new;
6403
6404 if (!conf)
6405 return -ENODEV;
6406
6407 if (len >= PAGE_SIZE)
6408 return -EINVAL;
6409
6410 if (kstrtoul(page, 10, &new))
6411 return -EINVAL;
6412
6413 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
6414 return -EINVAL;
6415
6416 if (new != PARITY_DISABLE_RMW &&
6417 new != PARITY_ENABLE_RMW &&
6418 new != PARITY_PREFER_RMW)
6419 return -EINVAL;
6420
6421 conf->rmw_level = new;
6422 return len;
6423}
6424
6425static struct md_sysfs_entry
6426raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
6427 raid5_show_rmw_level,
6428 raid5_store_rmw_level);
6429
6430
6431static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006432raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006433{
NeilBrown7b1485b2014-12-15 12:56:59 +11006434 struct r5conf *conf;
6435 int ret = 0;
6436 spin_lock(&mddev->lock);
6437 conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006438 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006439 ret = sprintf(page, "%d\n", conf->bypass_threshold);
6440 spin_unlock(&mddev->lock);
6441 return ret;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006442}
6443
6444static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006445raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006446{
NeilBrown67918752014-12-15 12:57:01 +11006447 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006448 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006449 int err;
6450
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006451 if (len >= PAGE_SIZE)
6452 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006453 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006454 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006455
6456 err = mddev_lock(mddev);
6457 if (err)
6458 return err;
6459 conf = mddev->private;
6460 if (!conf)
6461 err = -ENODEV;
NeilBrownedbe83a2015-02-26 12:47:56 +11006462 else if (new > conf->min_nr_stripes)
NeilBrown67918752014-12-15 12:57:01 +11006463 err = -EINVAL;
6464 else
6465 conf->bypass_threshold = new;
6466 mddev_unlock(mddev);
6467 return err ?: len;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006468}
6469
6470static struct md_sysfs_entry
6471raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
6472 S_IRUGO | S_IWUSR,
6473 raid5_show_preread_threshold,
6474 raid5_store_preread_threshold);
6475
6476static ssize_t
Shaohua Lid592a992014-05-21 17:57:44 +08006477raid5_show_skip_copy(struct mddev *mddev, char *page)
6478{
NeilBrown7b1485b2014-12-15 12:56:59 +11006479 struct r5conf *conf;
6480 int ret = 0;
6481 spin_lock(&mddev->lock);
6482 conf = mddev->private;
Shaohua Lid592a992014-05-21 17:57:44 +08006483 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006484 ret = sprintf(page, "%d\n", conf->skip_copy);
6485 spin_unlock(&mddev->lock);
6486 return ret;
Shaohua Lid592a992014-05-21 17:57:44 +08006487}
6488
6489static ssize_t
6490raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
6491{
NeilBrown67918752014-12-15 12:57:01 +11006492 struct r5conf *conf;
Shaohua Lid592a992014-05-21 17:57:44 +08006493 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006494 int err;
6495
Shaohua Lid592a992014-05-21 17:57:44 +08006496 if (len >= PAGE_SIZE)
6497 return -EINVAL;
Shaohua Lid592a992014-05-21 17:57:44 +08006498 if (kstrtoul(page, 10, &new))
6499 return -EINVAL;
6500 new = !!new;
Shaohua Lid592a992014-05-21 17:57:44 +08006501
NeilBrown67918752014-12-15 12:57:01 +11006502 err = mddev_lock(mddev);
6503 if (err)
6504 return err;
6505 conf = mddev->private;
6506 if (!conf)
6507 err = -ENODEV;
6508 else if (new != conf->skip_copy) {
6509 mddev_suspend(mddev);
6510 conf->skip_copy = new;
6511 if (new)
Jan Karadc3b17c2017-02-02 15:56:50 +01006512 mddev->queue->backing_dev_info->capabilities |=
NeilBrown67918752014-12-15 12:57:01 +11006513 BDI_CAP_STABLE_WRITES;
6514 else
Jan Karadc3b17c2017-02-02 15:56:50 +01006515 mddev->queue->backing_dev_info->capabilities &=
NeilBrown67918752014-12-15 12:57:01 +11006516 ~BDI_CAP_STABLE_WRITES;
6517 mddev_resume(mddev);
6518 }
6519 mddev_unlock(mddev);
6520 return err ?: len;
Shaohua Lid592a992014-05-21 17:57:44 +08006521}
6522
6523static struct md_sysfs_entry
6524raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
6525 raid5_show_skip_copy,
6526 raid5_store_skip_copy);
6527
Shaohua Lid592a992014-05-21 17:57:44 +08006528static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006529stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006530{
NeilBrownd1688a62011-10-11 16:49:52 +11006531 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006532 if (conf)
6533 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
6534 else
6535 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08006536}
6537
NeilBrown96de1e62005-11-08 21:39:39 -08006538static struct md_sysfs_entry
6539raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08006540
Shaohua Lib7214202013-08-27 17:50:42 +08006541static ssize_t
6542raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
6543{
NeilBrown7b1485b2014-12-15 12:56:59 +11006544 struct r5conf *conf;
6545 int ret = 0;
6546 spin_lock(&mddev->lock);
6547 conf = mddev->private;
Shaohua Lib7214202013-08-27 17:50:42 +08006548 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006549 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
6550 spin_unlock(&mddev->lock);
6551 return ret;
Shaohua Lib7214202013-08-27 17:50:42 +08006552}
6553
majianpeng60aaf932013-11-14 15:16:20 +11006554static int alloc_thread_groups(struct r5conf *conf, int cnt,
6555 int *group_cnt,
6556 int *worker_cnt_per_group,
6557 struct r5worker_group **worker_groups);
Shaohua Lib7214202013-08-27 17:50:42 +08006558static ssize_t
6559raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
6560{
NeilBrown67918752014-12-15 12:57:01 +11006561 struct r5conf *conf;
Shaohua Lib7214202013-08-27 17:50:42 +08006562 unsigned long new;
6563 int err;
majianpeng60aaf932013-11-14 15:16:20 +11006564 struct r5worker_group *new_groups, *old_groups;
6565 int group_cnt, worker_cnt_per_group;
Shaohua Lib7214202013-08-27 17:50:42 +08006566
6567 if (len >= PAGE_SIZE)
6568 return -EINVAL;
Shaohua Lib7214202013-08-27 17:50:42 +08006569 if (kstrtoul(page, 10, &new))
6570 return -EINVAL;
6571
NeilBrown67918752014-12-15 12:57:01 +11006572 err = mddev_lock(mddev);
Shaohua Lib7214202013-08-27 17:50:42 +08006573 if (err)
6574 return err;
NeilBrown67918752014-12-15 12:57:01 +11006575 conf = mddev->private;
6576 if (!conf)
6577 err = -ENODEV;
6578 else if (new != conf->worker_cnt_per_group) {
6579 mddev_suspend(mddev);
6580
6581 old_groups = conf->worker_groups;
6582 if (old_groups)
6583 flush_workqueue(raid5_wq);
6584
6585 err = alloc_thread_groups(conf, new,
6586 &group_cnt, &worker_cnt_per_group,
6587 &new_groups);
6588 if (!err) {
6589 spin_lock_irq(&conf->device_lock);
6590 conf->group_cnt = group_cnt;
6591 conf->worker_cnt_per_group = worker_cnt_per_group;
6592 conf->worker_groups = new_groups;
6593 spin_unlock_irq(&conf->device_lock);
6594
6595 if (old_groups)
6596 kfree(old_groups[0].workers);
6597 kfree(old_groups);
6598 }
6599 mddev_resume(mddev);
6600 }
6601 mddev_unlock(mddev);
6602
6603 return err ?: len;
Shaohua Lib7214202013-08-27 17:50:42 +08006604}
6605
6606static struct md_sysfs_entry
6607raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
6608 raid5_show_group_thread_cnt,
6609 raid5_store_group_thread_cnt);
6610
NeilBrown007583c2005-11-08 21:39:30 -08006611static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08006612 &raid5_stripecache_size.attr,
6613 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006614 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08006615 &raid5_group_thread_cnt.attr,
Shaohua Lid592a992014-05-21 17:57:44 +08006616 &raid5_skip_copy.attr,
Markus Stockhausend06f1912014-12-15 12:57:05 +11006617 &raid5_rmw_level.attr,
Song Liu2c7da142016-11-17 15:24:41 -08006618 &r5c_journal_mode.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08006619 NULL,
6620};
NeilBrown007583c2005-11-08 21:39:30 -08006621static struct attribute_group raid5_attrs_group = {
6622 .name = NULL,
6623 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08006624};
6625
majianpeng60aaf932013-11-14 15:16:20 +11006626static int alloc_thread_groups(struct r5conf *conf, int cnt,
6627 int *group_cnt,
6628 int *worker_cnt_per_group,
6629 struct r5worker_group **worker_groups)
Shaohua Li851c30c2013-08-28 14:30:16 +08006630{
Shaohua Li566c09c2013-11-14 15:16:17 +11006631 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08006632 ssize_t size;
6633 struct r5worker *workers;
6634
majianpeng60aaf932013-11-14 15:16:20 +11006635 *worker_cnt_per_group = cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +08006636 if (cnt == 0) {
majianpeng60aaf932013-11-14 15:16:20 +11006637 *group_cnt = 0;
6638 *worker_groups = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08006639 return 0;
6640 }
majianpeng60aaf932013-11-14 15:16:20 +11006641 *group_cnt = num_possible_nodes();
Shaohua Li851c30c2013-08-28 14:30:16 +08006642 size = sizeof(struct r5worker) * cnt;
majianpeng60aaf932013-11-14 15:16:20 +11006643 workers = kzalloc(size * *group_cnt, GFP_NOIO);
6644 *worker_groups = kzalloc(sizeof(struct r5worker_group) *
6645 *group_cnt, GFP_NOIO);
6646 if (!*worker_groups || !workers) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006647 kfree(workers);
majianpeng60aaf932013-11-14 15:16:20 +11006648 kfree(*worker_groups);
Shaohua Li851c30c2013-08-28 14:30:16 +08006649 return -ENOMEM;
6650 }
6651
majianpeng60aaf932013-11-14 15:16:20 +11006652 for (i = 0; i < *group_cnt; i++) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006653 struct r5worker_group *group;
6654
NeilBrown0c775d52013-11-25 11:12:43 +11006655 group = &(*worker_groups)[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08006656 INIT_LIST_HEAD(&group->handle_list);
Shaohua Li535ae4e2017-02-15 19:37:32 -08006657 INIT_LIST_HEAD(&group->loprio_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006658 group->conf = conf;
6659 group->workers = workers + i * cnt;
6660
6661 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11006662 struct r5worker *worker = group->workers + j;
6663 worker->group = group;
6664 INIT_WORK(&worker->work, raid5_do_work);
6665
6666 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
6667 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08006668 }
6669 }
6670
6671 return 0;
6672}
6673
6674static void free_thread_groups(struct r5conf *conf)
6675{
6676 if (conf->worker_groups)
6677 kfree(conf->worker_groups[0].workers);
6678 kfree(conf->worker_groups);
6679 conf->worker_groups = NULL;
6680}
6681
Dan Williams80c3a6c2009-03-17 18:10:40 -07006682static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11006683raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07006684{
NeilBrownd1688a62011-10-11 16:49:52 +11006685 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006686
6687 if (!sectors)
6688 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006689 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11006690 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11006691 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006692
NeilBrown3cb5edf2015-07-15 17:24:17 +10006693 sectors &= ~((sector_t)conf->chunk_sectors - 1);
6694 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006695 return sectors * (raid_disks - conf->max_degraded);
6696}
6697
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306698static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6699{
6700 safe_put_page(percpu->spare_page);
shli@kernel.org46d5b782014-12-15 12:57:02 +11006701 if (percpu->scribble)
6702 flex_array_free(percpu->scribble);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306703 percpu->spare_page = NULL;
6704 percpu->scribble = NULL;
6705}
6706
6707static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6708{
6709 if (conf->level == 6 && !percpu->spare_page)
6710 percpu->spare_page = alloc_page(GFP_KERNEL);
6711 if (!percpu->scribble)
shli@kernel.org46d5b782014-12-15 12:57:02 +11006712 percpu->scribble = scribble_alloc(max(conf->raid_disks,
NeilBrown738a2732015-05-08 18:19:39 +10006713 conf->previous_raid_disks),
6714 max(conf->chunk_sectors,
6715 conf->prev_chunk_sectors)
6716 / STRIPE_SECTORS,
6717 GFP_KERNEL);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306718
6719 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
6720 free_scratch_buffer(conf, percpu);
6721 return -ENOMEM;
6722 }
6723
6724 return 0;
6725}
6726
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006727static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
6728{
6729 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
6730
6731 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
6732 return 0;
6733}
6734
NeilBrownd1688a62011-10-11 16:49:52 +11006735static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006736{
Dan Williams36d1c642009-07-14 11:48:22 -07006737 if (!conf->percpu)
6738 return;
6739
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006740 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Dan Williams36d1c642009-07-14 11:48:22 -07006741 free_percpu(conf->percpu);
6742}
6743
NeilBrownd1688a62011-10-11 16:49:52 +11006744static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10006745{
Song Liud7bd3982016-11-23 22:50:39 -08006746 int i;
6747
Shaohua Li5c7e81c2015-08-13 14:32:04 -07006748 if (conf->log)
6749 r5l_exit_log(conf->log);
Shaohua Li30c89462016-09-21 09:07:13 -07006750 if (conf->shrinker.nr_deferred)
NeilBrownedbe83a2015-02-26 12:47:56 +11006751 unregister_shrinker(&conf->shrinker);
Shaohua Li5c7e81c2015-08-13 14:32:04 -07006752
Shaohua Li851c30c2013-08-28 14:30:16 +08006753 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10006754 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07006755 raid5_free_percpu(conf);
Song Liud7bd3982016-11-23 22:50:39 -08006756 for (i = 0; i < conf->pool_size; i++)
6757 if (conf->disks[i].extra_page)
6758 put_page(conf->disks[i].extra_page);
Dan Williams95fc17a2009-07-31 12:39:15 +10006759 kfree(conf->disks);
6760 kfree(conf->stripe_hashtbl);
Shaohua Liaaf9f122017-03-03 22:06:12 -08006761 kfree(conf->pending_data);
Dan Williams95fc17a2009-07-31 12:39:15 +10006762 kfree(conf);
6763}
6764
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006765static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
Dan Williams36d1c642009-07-14 11:48:22 -07006766{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006767 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
Dan Williams36d1c642009-07-14 11:48:22 -07006768 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
6769
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006770 if (alloc_scratch_buffer(conf, percpu)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006771 pr_warn("%s: failed memory allocation for cpu%u\n",
6772 __func__, cpu);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006773 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006774 }
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006775 return 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006776}
Dan Williams36d1c642009-07-14 11:48:22 -07006777
NeilBrownd1688a62011-10-11 16:49:52 +11006778static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006779{
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306780 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006781
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306782 conf->percpu = alloc_percpu(struct raid5_percpu);
6783 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07006784 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006785
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006786 err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Shaohua Li27a353c2016-02-24 17:38:28 -08006787 if (!err) {
6788 conf->scribble_disks = max(conf->raid_disks,
6789 conf->previous_raid_disks);
6790 conf->scribble_sectors = max(conf->chunk_sectors,
6791 conf->prev_chunk_sectors);
6792 }
Dan Williams36d1c642009-07-14 11:48:22 -07006793 return err;
6794}
6795
NeilBrownedbe83a2015-02-26 12:47:56 +11006796static unsigned long raid5_cache_scan(struct shrinker *shrink,
6797 struct shrink_control *sc)
6798{
6799 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
NeilBrown2d5b5692015-07-06 12:49:23 +10006800 unsigned long ret = SHRINK_STOP;
6801
6802 if (mutex_trylock(&conf->cache_size_mutex)) {
6803 ret= 0;
NeilBrown49895bc2015-08-03 17:09:57 +10006804 while (ret < sc->nr_to_scan &&
6805 conf->max_nr_stripes > conf->min_nr_stripes) {
NeilBrown2d5b5692015-07-06 12:49:23 +10006806 if (drop_one_stripe(conf) == 0) {
6807 ret = SHRINK_STOP;
6808 break;
6809 }
6810 ret++;
6811 }
6812 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006813 }
6814 return ret;
6815}
6816
6817static unsigned long raid5_cache_count(struct shrinker *shrink,
6818 struct shrink_control *sc)
6819{
6820 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
6821
6822 if (conf->max_nr_stripes < conf->min_nr_stripes)
6823 /* unlikely, but not impossible */
6824 return 0;
6825 return conf->max_nr_stripes - conf->min_nr_stripes;
6826}
6827
NeilBrownd1688a62011-10-11 16:49:52 +11006828static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829{
NeilBrownd1688a62011-10-11 16:49:52 +11006830 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006831 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11006832 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10006834 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11006835 int i;
majianpeng60aaf932013-11-14 15:16:20 +11006836 int group_cnt, worker_cnt_per_group;
6837 struct r5worker_group *new_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
NeilBrown91adb562009-03-31 14:39:39 +11006839 if (mddev->new_level != 5
6840 && mddev->new_level != 4
6841 && mddev->new_level != 6) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006842 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
6843 mdname(mddev), mddev->new_level);
NeilBrown91adb562009-03-31 14:39:39 +11006844 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 }
NeilBrown91adb562009-03-31 14:39:39 +11006846 if ((mddev->new_level == 5
6847 && !algorithm_valid_raid5(mddev->new_layout)) ||
6848 (mddev->new_level == 6
6849 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006850 pr_warn("md/raid:%s: layout %d not supported\n",
6851 mdname(mddev), mddev->new_layout);
NeilBrown91adb562009-03-31 14:39:39 +11006852 return ERR_PTR(-EIO);
6853 }
6854 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006855 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
6856 mdname(mddev), mddev->raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006857 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11006858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859
Andre Noll664e7c42009-06-18 08:45:27 +10006860 if (!mddev->new_chunk_sectors ||
6861 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
6862 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006863 pr_warn("md/raid:%s: invalid chunk size %d\n",
6864 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11006865 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11006866 }
6867
NeilBrownd1688a62011-10-11 16:49:52 +11006868 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11006869 if (conf == NULL)
6870 goto abort;
Shaohua Liaaf9f122017-03-03 22:06:12 -08006871 INIT_LIST_HEAD(&conf->free_list);
6872 INIT_LIST_HEAD(&conf->pending_list);
6873 conf->pending_data = kzalloc(sizeof(struct r5pending_data) *
6874 PENDING_IO_MAX, GFP_KERNEL);
6875 if (!conf->pending_data)
6876 goto abort;
6877 for (i = 0; i < PENDING_IO_MAX; i++)
6878 list_add(&conf->pending_data[i].sibling, &conf->free_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006879 /* Don't enable multi-threading by default*/
majianpeng60aaf932013-11-14 15:16:20 +11006880 if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group,
6881 &new_group)) {
6882 conf->group_cnt = group_cnt;
6883 conf->worker_cnt_per_group = worker_cnt_per_group;
6884 conf->worker_groups = new_group;
6885 } else
Shaohua Li851c30c2013-08-28 14:30:16 +08006886 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11006887 spin_lock_init(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10006888 seqcount_init(&conf->gen_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006889 mutex_init(&conf->cache_size_mutex);
Yuanhan Liub1b46482015-05-08 18:19:06 +10006890 init_waitqueue_head(&conf->wait_for_quiescent);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08006891 init_waitqueue_head(&conf->wait_for_stripe);
Dan Williamsf5efd452009-10-16 15:55:38 +11006892 init_waitqueue_head(&conf->wait_for_overlap);
6893 INIT_LIST_HEAD(&conf->handle_list);
Shaohua Li535ae4e2017-02-15 19:37:32 -08006894 INIT_LIST_HEAD(&conf->loprio_list);
Dan Williamsf5efd452009-10-16 15:55:38 +11006895 INIT_LIST_HEAD(&conf->hold_list);
6896 INIT_LIST_HEAD(&conf->delayed_list);
6897 INIT_LIST_HEAD(&conf->bitmap_list);
NeilBrownc3cce6c2015-08-14 12:47:33 +10006898 bio_list_init(&conf->return_bi);
Shaohua Li773ca822013-08-27 17:50:39 +08006899 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11006900 atomic_set(&conf->active_stripes, 0);
6901 atomic_set(&conf->preread_active_stripes, 0);
6902 atomic_set(&conf->active_aligned_reads, 0);
Shaohua Li765d7042017-01-04 09:33:23 -08006903 spin_lock_init(&conf->pending_bios_lock);
6904 conf->batch_bio_dispatch = true;
6905 rdev_for_each(rdev, mddev) {
6906 if (test_bit(Journal, &rdev->flags))
6907 continue;
6908 if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
6909 conf->batch_bio_dispatch = false;
6910 break;
6911 }
6912 }
6913
Dan Williamsf5efd452009-10-16 15:55:38 +11006914 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11006915 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11006916
6917 conf->raid_disks = mddev->raid_disks;
6918 if (mddev->reshape_position == MaxSector)
6919 conf->previous_raid_disks = mddev->raid_disks;
6920 else
6921 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006922 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006923
NeilBrown5e5e3e72009-10-16 16:35:30 +11006924 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11006925 GFP_KERNEL);
Song Liud7bd3982016-11-23 22:50:39 -08006926
NeilBrown91adb562009-03-31 14:39:39 +11006927 if (!conf->disks)
6928 goto abort;
6929
Song Liud7bd3982016-11-23 22:50:39 -08006930 for (i = 0; i < max_disks; i++) {
6931 conf->disks[i].extra_page = alloc_page(GFP_KERNEL);
6932 if (!conf->disks[i].extra_page)
6933 goto abort;
6934 }
6935
NeilBrown91adb562009-03-31 14:39:39 +11006936 conf->mddev = mddev;
6937
6938 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
6939 goto abort;
6940
Shaohua Li566c09c2013-11-14 15:16:17 +11006941 /* We init hash_locks[0] separately to that it can be used
6942 * as the reference lock in the spin_lock_nest_lock() call
6943 * in lock_all_device_hash_locks_irq in order to convince
6944 * lockdep that we know what we are doing.
6945 */
6946 spin_lock_init(conf->hash_locks);
6947 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
6948 spin_lock_init(conf->hash_locks + i);
6949
6950 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6951 INIT_LIST_HEAD(conf->inactive_list + i);
6952
6953 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6954 INIT_LIST_HEAD(conf->temp_inactive_list + i);
6955
Song Liu1e6d6902016-11-17 15:24:39 -08006956 atomic_set(&conf->r5c_cached_full_stripes, 0);
6957 INIT_LIST_HEAD(&conf->r5c_full_stripe_list);
6958 atomic_set(&conf->r5c_cached_partial_stripes, 0);
6959 INIT_LIST_HEAD(&conf->r5c_partial_stripe_list);
Shaohua Lie33fbb92017-02-10 16:18:09 -08006960 atomic_set(&conf->r5c_flushing_full_stripes, 0);
6961 atomic_set(&conf->r5c_flushing_partial_stripes, 0);
Song Liu1e6d6902016-11-17 15:24:39 -08006962
Dan Williams36d1c642009-07-14 11:48:22 -07006963 conf->level = mddev->new_level;
shli@kernel.org46d5b782014-12-15 12:57:02 +11006964 conf->chunk_sectors = mddev->new_chunk_sectors;
Dan Williams36d1c642009-07-14 11:48:22 -07006965 if (raid5_alloc_percpu(conf) != 0)
6966 goto abort;
6967
NeilBrown0c55e022010-05-03 14:09:02 +10006968 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11006969
NeilBrowndafb20f2012-03-19 12:46:39 +11006970 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11006971 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006972 if (raid_disk >= max_disks
Shaohua Lif2076e72015-10-08 21:54:12 -07006973 || raid_disk < 0 || test_bit(Journal, &rdev->flags))
NeilBrown91adb562009-03-31 14:39:39 +11006974 continue;
6975 disk = conf->disks + raid_disk;
6976
NeilBrown17045f52011-12-23 10:17:53 +11006977 if (test_bit(Replacement, &rdev->flags)) {
6978 if (disk->replacement)
6979 goto abort;
6980 disk->replacement = rdev;
6981 } else {
6982 if (disk->rdev)
6983 goto abort;
6984 disk->rdev = rdev;
6985 }
NeilBrown91adb562009-03-31 14:39:39 +11006986
6987 if (test_bit(In_sync, &rdev->flags)) {
6988 char b[BDEVNAME_SIZE];
NeilBrowncc6167b2016-11-02 14:16:50 +11006989 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
6990 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05006991 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11006992 /* Cannot rely on bitmap to complete recovery */
6993 conf->fullsync = 1;
6994 }
6995
NeilBrown91adb562009-03-31 14:39:39 +11006996 conf->level = mddev->new_level;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006997 if (conf->level == 6) {
NeilBrown91adb562009-03-31 14:39:39 +11006998 conf->max_degraded = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006999 if (raid6_call.xor_syndrome)
7000 conf->rmw_level = PARITY_ENABLE_RMW;
7001 else
7002 conf->rmw_level = PARITY_DISABLE_RMW;
7003 } else {
NeilBrown91adb562009-03-31 14:39:39 +11007004 conf->max_degraded = 1;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11007005 conf->rmw_level = PARITY_ENABLE_RMW;
7006 }
NeilBrown91adb562009-03-31 14:39:39 +11007007 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11007008 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11007009 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10007010 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11007011 conf->prev_algo = mddev->layout;
NeilBrown5cac6bc2015-07-17 12:17:50 +10007012 } else {
7013 conf->prev_chunk_sectors = conf->chunk_sectors;
7014 conf->prev_algo = conf->algorithm;
NeilBrowne183eae2009-03-31 15:20:22 +11007015 }
NeilBrown91adb562009-03-31 14:39:39 +11007016
NeilBrownedbe83a2015-02-26 12:47:56 +11007017 conf->min_nr_stripes = NR_STRIPES;
Shaohua Liad5b0f72016-08-30 10:29:33 -07007018 if (mddev->reshape_position != MaxSector) {
7019 int stripes = max_t(int,
7020 ((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4,
7021 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4);
7022 conf->min_nr_stripes = max(NR_STRIPES, stripes);
7023 if (conf->min_nr_stripes != NR_STRIPES)
NeilBrowncc6167b2016-11-02 14:16:50 +11007024 pr_info("md/raid:%s: force stripe size %d for reshape\n",
Shaohua Liad5b0f72016-08-30 10:29:33 -07007025 mdname(mddev), conf->min_nr_stripes);
7026 }
NeilBrownedbe83a2015-02-26 12:47:56 +11007027 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11007028 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li4bda5562013-11-14 15:16:17 +11007029 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
NeilBrownedbe83a2015-02-26 12:47:56 +11007030 if (grow_stripes(conf, conf->min_nr_stripes)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007031 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
7032 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11007033 goto abort;
7034 } else
NeilBrowncc6167b2016-11-02 14:16:50 +11007035 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory);
NeilBrownedbe83a2015-02-26 12:47:56 +11007036 /*
7037 * Losing a stripe head costs more than the time to refill it,
7038 * it reduces the queue depth and so can hurt throughput.
7039 * So set it rather large, scaled by number of devices.
7040 */
7041 conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
7042 conf->shrinker.scan_objects = raid5_cache_scan;
7043 conf->shrinker.count_objects = raid5_cache_count;
7044 conf->shrinker.batch = 128;
7045 conf->shrinker.flags = 0;
Chao Yu6a0f53f2016-09-20 10:33:57 +08007046 if (register_shrinker(&conf->shrinker)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007047 pr_warn("md/raid:%s: couldn't register shrinker.\n",
7048 mdname(mddev));
Chao Yu6a0f53f2016-09-20 10:33:57 +08007049 goto abort;
7050 }
NeilBrown91adb562009-03-31 14:39:39 +11007051
NeilBrown02326052012-07-03 15:56:52 +10007052 sprintf(pers_name, "raid%d", mddev->new_level);
7053 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11007054 if (!conf->thread) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007055 pr_warn("md/raid:%s: couldn't allocate thread.\n",
7056 mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11007057 goto abort;
7058 }
7059
7060 return conf;
7061
7062 abort:
7063 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10007064 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11007065 return ERR_PTR(-EIO);
7066 } else
7067 return ERR_PTR(-ENOMEM);
7068}
7069
NeilBrownc148ffd2009-11-13 17:47:00 +11007070static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
7071{
7072 switch (algo) {
7073 case ALGORITHM_PARITY_0:
7074 if (raid_disk < max_degraded)
7075 return 1;
7076 break;
7077 case ALGORITHM_PARITY_N:
7078 if (raid_disk >= raid_disks - max_degraded)
7079 return 1;
7080 break;
7081 case ALGORITHM_PARITY_0_6:
NeilBrownf72ffdd2014-09-30 14:23:59 +10007082 if (raid_disk == 0 ||
NeilBrownc148ffd2009-11-13 17:47:00 +11007083 raid_disk == raid_disks - 1)
7084 return 1;
7085 break;
7086 case ALGORITHM_LEFT_ASYMMETRIC_6:
7087 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7088 case ALGORITHM_LEFT_SYMMETRIC_6:
7089 case ALGORITHM_RIGHT_SYMMETRIC_6:
7090 if (raid_disk == raid_disks - 1)
7091 return 1;
7092 }
7093 return 0;
7094}
7095
Shaohua Li849674e2016-01-20 13:52:20 -08007096static int raid5_run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11007097{
NeilBrownd1688a62011-10-11 16:49:52 +11007098 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10007099 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11007100 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007101 struct md_rdev *rdev;
Shaohua Li713cf5a2015-08-13 14:32:03 -07007102 struct md_rdev *journal_dev = NULL;
NeilBrownc148ffd2009-11-13 17:47:00 +11007103 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11007104 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10007105 long long min_offset_diff = 0;
7106 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11007107
Andre Noll8c6ac8682009-06-18 08:48:06 +10007108 if (mddev->recovery_cp != MaxSector)
NeilBrowncc6167b2016-11-02 14:16:50 +11007109 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
7110 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10007111
7112 rdev_for_each(rdev, mddev) {
7113 long long diff;
Shaohua Li713cf5a2015-08-13 14:32:03 -07007114
Shaohua Lif2076e72015-10-08 21:54:12 -07007115 if (test_bit(Journal, &rdev->flags)) {
Shaohua Li713cf5a2015-08-13 14:32:03 -07007116 journal_dev = rdev;
Shaohua Lif2076e72015-10-08 21:54:12 -07007117 continue;
7118 }
NeilBrownb5254dd2012-05-21 09:27:01 +10007119 if (rdev->raid_disk < 0)
7120 continue;
7121 diff = (rdev->new_data_offset - rdev->data_offset);
7122 if (first) {
7123 min_offset_diff = diff;
7124 first = 0;
7125 } else if (mddev->reshape_backwards &&
7126 diff < min_offset_diff)
7127 min_offset_diff = diff;
7128 else if (!mddev->reshape_backwards &&
7129 diff > min_offset_diff)
7130 min_offset_diff = diff;
7131 }
7132
NeilBrownf6705572006-03-27 01:18:11 -08007133 if (mddev->reshape_position != MaxSector) {
7134 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10007135 * Difficulties arise if the stripe we would write to
7136 * next is at or after the stripe we would read from next.
7137 * For a reshape that changes the number of devices, this
7138 * is only possible for a very short time, and mdadm makes
7139 * sure that time appears to have past before assembling
7140 * the array. So we fail if that time hasn't passed.
7141 * For a reshape that keeps the number of devices the same
7142 * mdadm must be monitoring the reshape can keeping the
7143 * critical areas read-only and backed up. It will start
7144 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08007145 */
7146 sector_t here_new, here_old;
7147 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11007148 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrown05256d92015-07-15 17:36:21 +10007149 int chunk_sectors;
7150 int new_data_disks;
NeilBrownf6705572006-03-27 01:18:11 -08007151
Shaohua Li713cf5a2015-08-13 14:32:03 -07007152 if (journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007153 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
7154 mdname(mddev));
Shaohua Li713cf5a2015-08-13 14:32:03 -07007155 return -EINVAL;
7156 }
7157
NeilBrown88ce4932009-03-31 15:24:23 +11007158 if (mddev->new_level != mddev->level) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007159 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
7160 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007161 return -EINVAL;
7162 }
NeilBrownf6705572006-03-27 01:18:11 -08007163 old_disks = mddev->raid_disks - mddev->delta_disks;
7164 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08007165 * further up in new geometry must map after here in old
7166 * geometry.
NeilBrown05256d92015-07-15 17:36:21 +10007167 * If the chunk sizes are different, then as we perform reshape
7168 * in units of the largest of the two, reshape_position needs
7169 * be a multiple of the largest chunk size times new data disks.
NeilBrownf6705572006-03-27 01:18:11 -08007170 */
7171 here_new = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007172 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
7173 new_data_disks = mddev->raid_disks - max_degraded;
7174 if (sector_div(here_new, chunk_sectors * new_data_disks)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007175 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
7176 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007177 return -EINVAL;
7178 }
NeilBrown05256d92015-07-15 17:36:21 +10007179 reshape_offset = here_new * chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08007180 /* here_new is the stripe we will write to */
7181 here_old = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007182 sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
NeilBrownf4168852007-02-28 20:11:53 -08007183 /* here_old is the first stripe that we might need to read
7184 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10007185 if (mddev->delta_disks == 0) {
7186 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10007187 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10007188 * and taking constant backups.
7189 * mdadm always starts a situation like this in
7190 * readonly mode so it can take control before
7191 * allowing any writes. So just check for that.
7192 */
NeilBrownb5254dd2012-05-21 09:27:01 +10007193 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
7194 abs(min_offset_diff) >= mddev->new_chunk_sectors)
7195 /* not really in-place - so OK */;
7196 else if (mddev->ro == 0) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007197 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
7198 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10007199 return -EINVAL;
7200 }
NeilBrown2c810cd2012-05-21 09:27:00 +10007201 } else if (mddev->reshape_backwards
NeilBrown05256d92015-07-15 17:36:21 +10007202 ? (here_new * chunk_sectors + min_offset_diff <=
7203 here_old * chunk_sectors)
7204 : (here_new * chunk_sectors >=
7205 here_old * chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08007206 /* Reading from the same stripe as writing to - bad */
NeilBrowncc6167b2016-11-02 14:16:50 +11007207 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
7208 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007209 return -EINVAL;
7210 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007211 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007212 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08007213 } else {
NeilBrown91adb562009-03-31 14:39:39 +11007214 BUG_ON(mddev->level != mddev->new_level);
7215 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10007216 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11007217 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08007218 }
7219
NeilBrown245f46c2009-03-31 14:39:39 +11007220 if (mddev->private == NULL)
7221 conf = setup_conf(mddev);
7222 else
7223 conf = mddev->private;
7224
NeilBrown91adb562009-03-31 14:39:39 +11007225 if (IS_ERR(conf))
7226 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08007227
Song Liu486b0f72016-08-19 15:34:01 -07007228 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
7229 if (!journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007230 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
7231 mdname(mddev));
Song Liu486b0f72016-08-19 15:34:01 -07007232 mddev->ro = 1;
7233 set_disk_ro(mddev->gendisk, 1);
7234 } else if (mddev->recovery_cp == MaxSector)
7235 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
Shaohua Li7dde2ad2015-10-08 21:54:10 -07007236 }
7237
NeilBrownb5254dd2012-05-21 09:27:01 +10007238 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11007239 mddev->thread = conf->thread;
7240 conf->thread = NULL;
7241 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242
NeilBrown17045f52011-12-23 10:17:53 +11007243 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
7244 i++) {
7245 rdev = conf->disks[i].rdev;
7246 if (!rdev && conf->disks[i].replacement) {
7247 /* The replacement is all we have yet */
7248 rdev = conf->disks[i].replacement;
7249 conf->disks[i].replacement = NULL;
7250 clear_bit(Replacement, &rdev->flags);
7251 conf->disks[i].rdev = rdev;
7252 }
7253 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11007254 continue;
NeilBrown17045f52011-12-23 10:17:53 +11007255 if (conf->disks[i].replacement &&
7256 conf->reshape_progress != MaxSector) {
7257 /* replacements and reshape simply do not mix. */
NeilBrowncc6167b2016-11-02 14:16:50 +11007258 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
NeilBrown17045f52011-12-23 10:17:53 +11007259 goto abort;
7260 }
NeilBrown2f115882010-06-17 17:41:03 +10007261 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11007262 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10007263 continue;
7264 }
NeilBrownc148ffd2009-11-13 17:47:00 +11007265 /* This disc is not fully in-sync. However if it
7266 * just stored parity (beyond the recovery_offset),
7267 * when we don't need to be concerned about the
7268 * array being dirty.
7269 * When reshape goes 'backwards', we never have
7270 * partially completed devices, so we only need
7271 * to worry about reshape going forwards.
7272 */
7273 /* Hack because v0.91 doesn't store recovery_offset properly. */
7274 if (mddev->major_version == 0 &&
7275 mddev->minor_version > 90)
7276 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007277
NeilBrownc148ffd2009-11-13 17:47:00 +11007278 if (rdev->recovery_offset < reshape_offset) {
7279 /* We need to check old and new layout */
7280 if (!only_parity(rdev->raid_disk,
7281 conf->algorithm,
7282 conf->raid_disks,
7283 conf->max_degraded))
7284 continue;
7285 }
7286 if (!only_parity(rdev->raid_disk,
7287 conf->prev_algo,
7288 conf->previous_raid_disks,
7289 conf->max_degraded))
7290 continue;
7291 dirty_parity_disks++;
7292 }
NeilBrown91adb562009-03-31 14:39:39 +11007293
NeilBrown17045f52011-12-23 10:17:53 +11007294 /*
7295 * 0 for a fully functional array, 1 or 2 for a degraded array.
7296 */
Song Liu2e38a372017-01-24 10:45:30 -08007297 mddev->degraded = raid5_calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298
NeilBrown674806d2010-06-16 17:17:53 +10007299 if (has_failed(conf)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007300 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07007301 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302 goto abort;
7303 }
7304
NeilBrown91adb562009-03-31 14:39:39 +11007305 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10007306 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11007307 mddev->resync_max_sectors = mddev->dev_sectors;
7308
NeilBrownc148ffd2009-11-13 17:47:00 +11007309 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007311 if (mddev->ok_start_degraded)
NeilBrowncc6167b2016-11-02 14:16:50 +11007312 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
7313 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007314 else {
NeilBrowncc6167b2016-11-02 14:16:50 +11007315 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
7316 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007317 goto abort;
7318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319 }
7320
NeilBrowncc6167b2016-11-02 14:16:50 +11007321 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
7322 mdname(mddev), conf->level,
7323 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
7324 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325
7326 print_raid5_conf(conf);
7327
NeilBrownfef9c612009-03-31 15:16:46 +11007328 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11007329 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08007330 atomic_set(&conf->reshape_stripes, 0);
7331 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7332 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7333 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7334 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7335 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007336 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08007337 }
7338
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10007340 if (mddev->to_remove == &raid5_attrs_group)
7341 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007342 else if (mddev->kobj.sd &&
7343 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrowncc6167b2016-11-02 14:16:50 +11007344 pr_warn("raid5: failed to create sysfs attributes for %s\n",
7345 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10007346 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
7347
7348 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007349 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11007350 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10007351 /* read-ahead size must cover two whole stripes, which
7352 * is 2 * (datadisks) * chunksize where 'n' is the
7353 * number of raid devices
7354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355 int data_disks = conf->previous_raid_disks - conf->max_degraded;
7356 int stripe = data_disks *
7357 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
Jan Karadc3b17c2017-02-02 15:56:50 +01007358 if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
7359 mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10007360
NeilBrown9f7c2222010-07-26 12:04:13 +10007361 chunk_size = mddev->chunk_sectors << 9;
7362 blk_queue_io_min(mddev->queue, chunk_size);
7363 blk_queue_io_opt(mddev->queue, chunk_size *
7364 (conf->raid_disks - conf->max_degraded));
Kent Overstreetc78afc62013-07-11 22:39:53 -07007365 mddev->queue->limits.raid_partial_stripes_expensive = 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007366 /*
7367 * We can only discard a whole stripe. It doesn't make sense to
7368 * discard data disk but write parity disk
7369 */
7370 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11007371 /* Round up to power of 2, as discard handling
7372 * currently assumes that */
7373 while ((stripe-1) & stripe)
7374 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007375 mddev->queue->limits.discard_alignment = stripe;
7376 mddev->queue->limits.discard_granularity = stripe;
Konstantin Khlebnikove8d7c332016-11-27 19:32:32 +03007377
7378 /*
7379 * We use 16-bit counter of active stripes in bi_phys_segments
7380 * (minus one for over-loaded initialization)
7381 */
7382 blk_queue_max_hw_sectors(mddev->queue, 0xfffe * STRIPE_SECTORS);
7383 blk_queue_max_discard_sectors(mddev->queue,
7384 0xfffe * STRIPE_SECTORS);
7385
Shaohua Li620125f2012-10-11 13:49:05 +11007386 /*
7387 * unaligned part of discard request will be ignored, so can't
NeilBrown8e0e99b2014-10-02 13:45:00 +10007388 * guarantee discard_zeroes_data
Shaohua Li620125f2012-10-11 13:49:05 +11007389 */
7390 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10007391
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007392 blk_queue_max_write_same_sectors(mddev->queue, 0);
7393
NeilBrown05616be2012-05-21 09:27:00 +10007394 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007395 disk_stack_limits(mddev->gendisk, rdev->bdev,
7396 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10007397 disk_stack_limits(mddev->gendisk, rdev->bdev,
7398 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11007399 /*
7400 * discard_zeroes_data is required, otherwise data
7401 * could be lost. Consider a scenario: discard a stripe
7402 * (the stripe could be inconsistent if
7403 * discard_zeroes_data is 0); write one disk of the
7404 * stripe (the stripe could be inconsistent again
7405 * depending on which disks are used to calculate
7406 * parity); the disk is broken; The stripe data of this
7407 * disk is lost.
7408 */
7409 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
7410 !bdev_get_queue(rdev->bdev)->
7411 limits.discard_zeroes_data)
7412 discard_supported = false;
NeilBrown8e0e99b2014-10-02 13:45:00 +10007413 /* Unfortunately, discard_zeroes_data is not currently
7414 * a guarantee - just a hint. So we only allow DISCARD
7415 * if the sysadmin has confirmed that only safe devices
7416 * are in use by setting a module parameter.
7417 */
7418 if (!devices_handle_discard_safely) {
7419 if (discard_supported) {
7420 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
7421 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
7422 }
7423 discard_supported = false;
7424 }
NeilBrown05616be2012-05-21 09:27:00 +10007425 }
Shaohua Li620125f2012-10-11 13:49:05 +11007426
7427 if (discard_supported &&
Jes Sorensene7597e62016-02-16 16:44:24 -05007428 mddev->queue->limits.max_discard_sectors >= (stripe >> 9) &&
7429 mddev->queue->limits.discard_granularity >= stripe)
Shaohua Li620125f2012-10-11 13:49:05 +11007430 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
7431 mddev->queue);
7432 else
7433 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
7434 mddev->queue);
Shaohua Li1dffddd2016-09-08 10:49:06 -07007435
7436 blk_queue_max_hw_sectors(mddev->queue, UINT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007437 }
7438
Shaohua Li5c7e81c2015-08-13 14:32:04 -07007439 if (journal_dev) {
7440 char b[BDEVNAME_SIZE];
7441
NeilBrowncc6167b2016-11-02 14:16:50 +11007442 pr_debug("md/raid:%s: using device %s as journal\n",
7443 mdname(mddev), bdevname(journal_dev->bdev, b));
Song Liu5aabf7c2016-11-17 15:24:44 -08007444 if (r5l_init_log(conf, journal_dev))
7445 goto abort;
Shaohua Li5c7e81c2015-08-13 14:32:04 -07007446 }
7447
Linus Torvalds1da177e2005-04-16 15:20:36 -07007448 return 0;
7449abort:
NeilBrown01f96c02011-09-21 15:30:20 +10007450 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11007451 print_raid5_conf(conf);
7452 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 mddev->private = NULL;
NeilBrowncc6167b2016-11-02 14:16:50 +11007454 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455 return -EIO;
7456}
7457
NeilBrownafa0f552014-12-15 12:56:58 +11007458static void raid5_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007459{
NeilBrownafa0f552014-12-15 12:56:58 +11007460 struct r5conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007461
Dan Williams95fc17a2009-07-31 12:39:15 +10007462 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10007463 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007464}
7465
Shaohua Li849674e2016-01-20 13:52:20 -08007466static void raid5_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007467{
NeilBrownd1688a62011-10-11 16:49:52 +11007468 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007469 int i;
7470
Andre Noll9d8f0362009-06-18 08:45:01 +10007471 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
NeilBrown3cb5edf2015-07-15 17:24:17 +10007472 conf->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07007473 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
NeilBrown5fd13352016-06-02 16:19:52 +10007474 rcu_read_lock();
7475 for (i = 0; i < conf->raid_disks; i++) {
7476 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
7477 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
7478 }
7479 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007480 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007481}
7482
NeilBrownd1688a62011-10-11 16:49:52 +11007483static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007484{
7485 int i;
7486 struct disk_info *tmp;
7487
NeilBrowncc6167b2016-11-02 14:16:50 +11007488 pr_debug("RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007489 if (!conf) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007490 pr_debug("(conf==NULL)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491 return;
7492 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007493 pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level,
NeilBrown0c55e022010-05-03 14:09:02 +10007494 conf->raid_disks,
7495 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007496
7497 for (i = 0; i < conf->raid_disks; i++) {
7498 char b[BDEVNAME_SIZE];
7499 tmp = conf->disks + i;
7500 if (tmp->rdev)
NeilBrowncc6167b2016-11-02 14:16:50 +11007501 pr_debug(" disk %d, o:%d, dev:%s\n",
NeilBrown0c55e022010-05-03 14:09:02 +10007502 i, !test_bit(Faulty, &tmp->rdev->flags),
7503 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007504 }
7505}
7506
NeilBrownfd01b882011-10-11 16:47:53 +11007507static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508{
7509 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11007510 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007511 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10007512 int count = 0;
7513 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514
7515 for (i = 0; i < conf->raid_disks; i++) {
7516 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11007517 if (tmp->replacement
7518 && tmp->replacement->recovery_offset == MaxSector
7519 && !test_bit(Faulty, &tmp->replacement->flags)
7520 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
7521 /* Replacement has just become active. */
7522 if (!tmp->rdev
7523 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
7524 count++;
7525 if (tmp->rdev) {
7526 /* Replaced device not technically faulty,
7527 * but we need to be sure it gets removed
7528 * and never re-added.
7529 */
7530 set_bit(Faulty, &tmp->rdev->flags);
7531 sysfs_notify_dirent_safe(
7532 tmp->rdev->sysfs_state);
7533 }
7534 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
7535 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10007536 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08007537 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07007538 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10007539 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11007540 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007541 }
7542 }
NeilBrown6b965622010-08-18 11:56:59 +10007543 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08007544 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007545 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007547 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548}
7549
NeilBrownb8321b62011-12-23 10:17:51 +11007550static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551{
NeilBrownd1688a62011-10-11 16:49:52 +11007552 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007553 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11007554 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11007555 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556 struct disk_info *p = conf->disks + number;
7557
7558 print_raid5_conf(conf);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007559 if (test_bit(Journal, &rdev->flags) && conf->log) {
7560 struct r5l_log *log;
Shaohua Lic2bb6242015-10-08 21:54:07 -07007561 /*
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007562 * we can't wait pending write here, as this is called in
7563 * raid5d, wait will deadlock.
Shaohua Lic2bb6242015-10-08 21:54:07 -07007564 */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007565 if (atomic_read(&mddev->writes_pending))
7566 return -EBUSY;
7567 log = conf->log;
7568 conf->log = NULL;
7569 synchronize_rcu();
7570 r5l_exit_log(log);
7571 return 0;
Shaohua Lic2bb6242015-10-08 21:54:07 -07007572 }
NeilBrown657e3e42011-12-23 10:17:52 +11007573 if (rdev == p->rdev)
7574 rdevp = &p->rdev;
7575 else if (rdev == p->replacement)
7576 rdevp = &p->replacement;
7577 else
7578 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11007579
NeilBrown657e3e42011-12-23 10:17:52 +11007580 if (number >= conf->raid_disks &&
7581 conf->reshape_progress == MaxSector)
7582 clear_bit(In_sync, &rdev->flags);
7583
7584 if (test_bit(In_sync, &rdev->flags) ||
7585 atomic_read(&rdev->nr_pending)) {
7586 err = -EBUSY;
7587 goto abort;
7588 }
7589 /* Only remove non-faulty devices if recovery
7590 * isn't possible.
7591 */
7592 if (!test_bit(Faulty, &rdev->flags) &&
7593 mddev->recovery_disabled != conf->recovery_disabled &&
7594 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11007595 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11007596 number < conf->raid_disks) {
7597 err = -EBUSY;
7598 goto abort;
7599 }
7600 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10007601 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
7602 synchronize_rcu();
7603 if (atomic_read(&rdev->nr_pending)) {
7604 /* lost the race, try later */
7605 err = -EBUSY;
7606 *rdevp = rdev;
7607 }
7608 }
7609 if (p->replacement) {
NeilBrowndd054fc2011-12-23 10:17:53 +11007610 /* We must have just cleared 'rdev' */
7611 p->rdev = p->replacement;
7612 clear_bit(Replacement, &p->replacement->flags);
7613 smp_mb(); /* Make sure other CPUs may see both as identical
7614 * but will never see neither - if they are careful
7615 */
7616 p->replacement = NULL;
7617 clear_bit(WantReplacement, &rdev->flags);
7618 } else
7619 /* We might have just removed the Replacement as faulty-
7620 * clear the bit just in case
7621 */
7622 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623abort:
7624
7625 print_raid5_conf(conf);
7626 return err;
7627}
7628
NeilBrownfd01b882011-10-11 16:47:53 +11007629static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007630{
NeilBrownd1688a62011-10-11 16:49:52 +11007631 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10007632 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633 int disk;
7634 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10007635 int first = 0;
7636 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007637
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007638 if (test_bit(Journal, &rdev->flags)) {
7639 char b[BDEVNAME_SIZE];
7640 if (conf->log)
7641 return -EBUSY;
7642
7643 rdev->raid_disk = 0;
7644 /*
7645 * The array is in readonly mode if journal is missing, so no
7646 * write requests running. We should be safe
7647 */
7648 r5l_init_log(conf, rdev);
NeilBrowncc6167b2016-11-02 14:16:50 +11007649 pr_debug("md/raid:%s: using device %s as journal\n",
7650 mdname(mddev), bdevname(rdev->bdev, b));
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007651 return 0;
7652 }
NeilBrown7f0da592011-07-28 11:39:22 +10007653 if (mddev->recovery_disabled == conf->recovery_disabled)
7654 return -EBUSY;
7655
NeilBrowndc10c642012-03-19 12:46:37 +11007656 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007657 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10007658 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007659
Neil Brown6c2fce22008-06-28 08:31:31 +10007660 if (rdev->raid_disk >= 0)
7661 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007662
7663 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07007664 * find the disk ... but prefer rdev->saved_raid_disk
7665 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007666 */
NeilBrown16a53ec2006-06-26 00:27:38 -07007667 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10007668 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07007669 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10007670 first = rdev->saved_raid_disk;
7671
7672 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007673 p = conf->disks + disk;
7674 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08007675 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007676 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10007677 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07007678 if (rdev->saved_raid_disk != disk)
7679 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08007680 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10007681 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007682 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007683 }
7684 for (disk = first; disk <= last; disk++) {
7685 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007686 if (test_bit(WantReplacement, &p->rdev->flags) &&
7687 p->replacement == NULL) {
7688 clear_bit(In_sync, &rdev->flags);
7689 set_bit(Replacement, &rdev->flags);
7690 rdev->raid_disk = disk;
7691 err = 0;
7692 conf->fullsync = 1;
7693 rcu_assign_pointer(p->replacement, rdev);
7694 break;
7695 }
7696 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007697out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007698 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10007699 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007700}
7701
NeilBrownfd01b882011-10-11 16:47:53 +11007702static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007703{
7704 /* no resync is happening, and there is enough space
7705 * on all devices, so we can resize.
7706 * We need to make sure resync covers any new space.
7707 * If the array is shrinking we should possibly wait until
7708 * any io in the removed space completes, but it hardly seems
7709 * worth it.
7710 */
NeilBrowna4a61252012-05-22 13:55:27 +10007711 sector_t newsize;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007712 struct r5conf *conf = mddev->private;
7713
Shaohua Li713cf5a2015-08-13 14:32:03 -07007714 if (conf->log)
7715 return -EINVAL;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007716 sectors &= ~((sector_t)conf->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10007717 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
7718 if (mddev->external_size &&
7719 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11007720 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10007721 if (mddev->bitmap) {
7722 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
7723 if (ret)
7724 return ret;
7725 }
7726 md_set_array_sectors(mddev, newsize);
NeilBrownb0986362011-05-11 15:52:21 +10007727 if (sectors > mddev->dev_sectors &&
7728 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11007729 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007730 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7731 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007732 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07007733 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007734 return 0;
7735}
7736
NeilBrownfd01b882011-10-11 16:47:53 +11007737static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10007738{
7739 /* Can only proceed if there are plenty of stripe_heads.
7740 * We need a minimum of one full stripe,, and for sensible progress
7741 * it is best to have about 4 times that.
7742 * If we require 4 times, then the default 256 4K stripe_heads will
7743 * allow for chunk sizes up to 256K, which is probably OK.
7744 * If the chunk size is greater, user-space should request more
7745 * stripe_heads first.
7746 */
NeilBrownd1688a62011-10-11 16:49:52 +11007747 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10007748 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007749 > conf->min_nr_stripes ||
NeilBrown01ee22b2009-06-18 08:47:20 +10007750 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007751 > conf->min_nr_stripes) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007752 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7753 mdname(mddev),
7754 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
7755 / STRIPE_SIZE)*4);
NeilBrown01ee22b2009-06-18 08:47:20 +10007756 return 0;
7757 }
7758 return 1;
7759}
7760
NeilBrownfd01b882011-10-11 16:47:53 +11007761static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08007762{
NeilBrownd1688a62011-10-11 16:49:52 +11007763 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08007764
Shaohua Li713cf5a2015-08-13 14:32:03 -07007765 if (conf->log)
7766 return -EINVAL;
NeilBrown88ce4932009-03-31 15:24:23 +11007767 if (mddev->delta_disks == 0 &&
7768 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10007769 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10007770 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10007771 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11007772 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10007773 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11007774 /* We might be able to shrink, but the devices must
7775 * be made bigger first.
7776 * For raid6, 4 is the minimum size.
7777 * Otherwise 2 is the minimum
7778 */
7779 int min = 2;
7780 if (mddev->level == 6)
7781 min = 4;
7782 if (mddev->raid_disks + mddev->delta_disks < min)
7783 return -EINVAL;
7784 }
NeilBrown29269552006-03-27 01:18:10 -08007785
NeilBrown01ee22b2009-06-18 08:47:20 +10007786 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08007787 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08007788
NeilBrown738a2732015-05-08 18:19:39 +10007789 if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
7790 mddev->delta_disks > 0)
7791 if (resize_chunks(conf,
7792 conf->previous_raid_disks
7793 + max(0, mddev->delta_disks),
7794 max(mddev->new_chunk_sectors,
7795 mddev->chunk_sectors)
7796 ) < 0)
7797 return -ENOMEM;
NeilBrowne56108d62012-10-11 14:24:13 +11007798 return resize_stripes(conf, (conf->previous_raid_disks
7799 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08007800}
7801
NeilBrownfd01b882011-10-11 16:47:53 +11007802static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08007803{
NeilBrownd1688a62011-10-11 16:49:52 +11007804 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11007805 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08007806 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07007807 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08007808
NeilBrownf4168852007-02-28 20:11:53 -08007809 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08007810 return -EBUSY;
7811
NeilBrown01ee22b2009-06-18 08:47:20 +10007812 if (!check_stripe_cache(mddev))
7813 return -ENOSPC;
7814
NeilBrown30b67642012-05-22 13:55:28 +10007815 if (has_failed(conf))
7816 return -EINVAL;
7817
NeilBrownc6563a82012-05-21 09:27:00 +10007818 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11007819 if (!test_bit(In_sync, &rdev->flags)
7820 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08007821 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10007822 }
NeilBrown63c70c42006-03-27 01:18:13 -08007823
NeilBrownf4168852007-02-28 20:11:53 -08007824 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08007825 /* Not enough devices even to make a degraded array
7826 * of that size
7827 */
7828 return -EINVAL;
7829
NeilBrownec32a2b2009-03-31 15:17:38 +11007830 /* Refuse to reduce size of the array. Any reductions in
7831 * array size must be through explicit setting of array_size
7832 * attribute.
7833 */
7834 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
7835 < mddev->array_sectors) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007836 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
7837 mdname(mddev));
NeilBrownec32a2b2009-03-31 15:17:38 +11007838 return -EINVAL;
7839 }
7840
NeilBrownf6705572006-03-27 01:18:11 -08007841 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08007842 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10007843 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007844 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08007845 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007846 conf->prev_chunk_sectors = conf->chunk_sectors;
7847 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11007848 conf->prev_algo = conf->algorithm;
7849 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10007850 conf->generation++;
7851 /* Code that selects data_offset needs to see the generation update
7852 * if reshape_progress has been set - so a memory barrier needed.
7853 */
7854 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10007855 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11007856 conf->reshape_progress = raid5_size(mddev, 0, 0);
7857 else
7858 conf->reshape_progress = 0;
7859 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10007860 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007861 spin_unlock_irq(&conf->device_lock);
7862
NeilBrown4d77e3b2013-08-27 15:57:47 +10007863 /* Now make sure any requests that proceeded on the assumption
7864 * the reshape wasn't running - like Discard or Read - have
7865 * completed.
7866 */
7867 mddev_suspend(mddev);
7868 mddev_resume(mddev);
7869
NeilBrown29269552006-03-27 01:18:10 -08007870 /* Add some new drives, as many as will fit.
7871 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10007872 * Don't add devices if we are reducing the number of
7873 * devices in the array. This is because it is not possible
7874 * to correctly record the "partially reconstructed" state of
7875 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08007876 */
NeilBrown87a8dec2011-01-31 11:57:43 +11007877 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11007878 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11007879 if (rdev->raid_disk < 0 &&
7880 !test_bit(Faulty, &rdev->flags)) {
7881 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11007882 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11007883 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11007884 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11007885 else
NeilBrown87a8dec2011-01-31 11:57:43 +11007886 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10007887
7888 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11007889 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11007890 }
NeilBrown87a8dec2011-01-31 11:57:43 +11007891 } else if (rdev->raid_disk >= conf->previous_raid_disks
7892 && !test_bit(Faulty, &rdev->flags)) {
7893 /* This is a spare that was manually added */
7894 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11007895 }
NeilBrown29269552006-03-27 01:18:10 -08007896
NeilBrown87a8dec2011-01-31 11:57:43 +11007897 /* When a reshape changes the number of devices,
7898 * ->degraded is measured against the larger of the
7899 * pre and post number of devices.
7900 */
NeilBrownec32a2b2009-03-31 15:17:38 +11007901 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08007902 mddev->degraded = raid5_calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11007903 spin_unlock_irqrestore(&conf->device_lock, flags);
7904 }
NeilBrown63c70c42006-03-27 01:18:13 -08007905 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10007906 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08007907 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownf6705572006-03-27 01:18:11 -08007908
NeilBrown29269552006-03-27 01:18:10 -08007909 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7910 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10007911 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown29269552006-03-27 01:18:10 -08007912 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7913 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7914 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007915 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08007916 if (!mddev->sync_thread) {
7917 mddev->recovery = 0;
7918 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11007919 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007920 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11007921 mddev->new_chunk_sectors =
7922 conf->chunk_sectors = conf->prev_chunk_sectors;
7923 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10007924 rdev_for_each(rdev, mddev)
7925 rdev->new_data_offset = rdev->data_offset;
7926 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11007927 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11007928 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11007929 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11007930 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007931 spin_unlock_irq(&conf->device_lock);
7932 return -EAGAIN;
7933 }
NeilBrownc8f517c2009-03-31 15:28:40 +11007934 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08007935 md_wakeup_thread(mddev->sync_thread);
7936 md_new_event(mddev);
7937 return 0;
7938}
NeilBrown29269552006-03-27 01:18:10 -08007939
NeilBrownec32a2b2009-03-31 15:17:38 +11007940/* This is called from the reshape thread and should make any
7941 * changes needed in 'conf'
7942 */
NeilBrownd1688a62011-10-11 16:49:52 +11007943static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08007944{
NeilBrown29269552006-03-27 01:18:10 -08007945
NeilBrownf6705572006-03-27 01:18:11 -08007946 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10007947 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07007948
NeilBrownf6705572006-03-27 01:18:11 -08007949 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11007950 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10007951 rdev_for_each(rdev, conf->mddev)
7952 rdev->data_offset = rdev->new_data_offset;
7953 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11007954 conf->reshape_progress = MaxSector;
NeilBrown6cbd8142015-07-24 13:30:32 +10007955 conf->mddev->reshape_position = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08007956 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11007957 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07007958
7959 /* read-ahead size must cover two whole stripes, which is
7960 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
7961 */
NeilBrown4a5add42010-06-01 19:37:28 +10007962 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11007963 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007964 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11007965 / PAGE_SIZE);
Jan Karadc3b17c2017-02-02 15:56:50 +01007966 if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
7967 conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
NeilBrown16a53ec2006-06-26 00:27:38 -07007968 }
NeilBrown29269552006-03-27 01:18:10 -08007969 }
NeilBrown29269552006-03-27 01:18:10 -08007970}
7971
NeilBrownec32a2b2009-03-31 15:17:38 +11007972/* This is called from the raid5d thread with mddev_lock held.
7973 * It makes config changes to the device.
7974 */
NeilBrownfd01b882011-10-11 16:47:53 +11007975static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11007976{
NeilBrownd1688a62011-10-11 16:49:52 +11007977 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11007978
7979 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7980
NeilBrownec32a2b2009-03-31 15:17:38 +11007981 if (mddev->delta_disks > 0) {
7982 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
Heinz Mauelshagenfe67d192016-05-03 22:15:31 +02007983 if (mddev->queue) {
7984 set_capacity(mddev->gendisk, mddev->array_sectors);
7985 revalidate_disk(mddev->gendisk);
7986 }
NeilBrownec32a2b2009-03-31 15:17:38 +11007987 } else {
7988 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11007989 spin_lock_irq(&conf->device_lock);
Song Liu2e38a372017-01-24 10:45:30 -08007990 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11007991 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11007992 for (d = conf->raid_disks ;
7993 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10007994 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11007995 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10007996 if (rdev)
7997 clear_bit(In_sync, &rdev->flags);
7998 rdev = conf->disks[d].replacement;
7999 if (rdev)
8000 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10008001 }
NeilBrowncea9c222009-03-31 15:15:05 +11008002 }
NeilBrown88ce4932009-03-31 15:24:23 +11008003 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10008004 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11008005 mddev->reshape_position = MaxSector;
8006 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10008007 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11008008 }
8009}
8010
NeilBrownfd01b882011-10-11 16:47:53 +11008011static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07008012{
NeilBrownd1688a62011-10-11 16:49:52 +11008013 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07008014
8015 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08008016 case 2: /* resume for a suspend */
8017 wake_up(&conf->wait_for_overlap);
8018 break;
8019
NeilBrown72626682005-09-09 16:23:54 -07008020 case 1: /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11008021 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10008022 /* '2' tells resync/reshape to pause so that all
8023 * active stripes can drain
8024 */
Song Liua39f7af2016-11-17 15:24:40 -08008025 r5c_flush_cache(conf, INT_MAX);
NeilBrown64bd6602009-08-03 10:59:58 +10008026 conf->quiesce = 2;
Yuanhan Liub1b46482015-05-08 18:19:06 +10008027 wait_event_cmd(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08008028 atomic_read(&conf->active_stripes) == 0 &&
8029 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11008030 unlock_all_device_hash_locks_irq(conf),
8031 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10008032 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11008033 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10008034 /* allow reshape to continue */
8035 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07008036 break;
8037
8038 case 0: /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11008039 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07008040 conf->quiesce = 0;
Yuanhan Liub1b46482015-05-08 18:19:06 +10008041 wake_up(&conf->wait_for_quiescent);
NeilBrowne464eaf2006-03-27 01:18:14 -08008042 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11008043 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07008044 break;
8045 }
Shaohua Lie6c033f2015-10-04 09:20:12 -07008046 r5l_quiesce(conf->log, state);
NeilBrown72626682005-09-09 16:23:54 -07008047}
NeilBrownb15c2e52006-01-06 00:20:16 -08008048
NeilBrownfd01b882011-10-11 16:47:53 +11008049static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11008050{
NeilBrowne373ab12011-10-11 16:48:59 +11008051 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07008052 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11008053
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008054 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11008055 if (raid0_conf->nr_strip_zones > 1) {
NeilBrowncc6167b2016-11-02 14:16:50 +11008056 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
8057 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008058 return ERR_PTR(-EINVAL);
8059 }
8060
NeilBrowne373ab12011-10-11 16:48:59 +11008061 sectors = raid0_conf->strip_zone[0].zone_end;
8062 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10008063 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008064 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11008065 mddev->new_layout = ALGORITHM_PARITY_N;
8066 mddev->new_chunk_sectors = mddev->chunk_sectors;
8067 mddev->raid_disks += 1;
8068 mddev->delta_disks = 1;
8069 /* make sure it will be not marked as dirty */
8070 mddev->recovery_cp = MaxSector;
8071
8072 return setup_conf(mddev);
8073}
8074
NeilBrownfd01b882011-10-11 16:47:53 +11008075static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11008076{
8077 int chunksect;
Shaohua Li6995f0b2016-12-08 15:48:17 -08008078 void *ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11008079
8080 if (mddev->raid_disks != 2 ||
8081 mddev->degraded > 1)
8082 return ERR_PTR(-EINVAL);
8083
8084 /* Should check if there are write-behind devices? */
8085
8086 chunksect = 64*2; /* 64K by default */
8087
8088 /* The array must be an exact multiple of chunksize */
8089 while (chunksect && (mddev->array_sectors & (chunksect-1)))
8090 chunksect >>= 1;
8091
8092 if ((chunksect<<9) < STRIPE_SIZE)
8093 /* array size does not allow a suitable chunk size */
8094 return ERR_PTR(-EINVAL);
8095
8096 mddev->new_level = 5;
8097 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10008098 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11008099
Shaohua Li6995f0b2016-12-08 15:48:17 -08008100 ret = setup_conf(mddev);
Jes Sorensen32cd7cb2017-01-06 19:31:35 -05008101 if (!IS_ERR(ret))
Shaohua Li394ed8e2017-01-04 16:10:19 -08008102 mddev_clear_unsupported_flags(mddev,
8103 UNSUPPORTED_MDDEV_FLAGS);
Shaohua Li6995f0b2016-12-08 15:48:17 -08008104 return ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11008105}
8106
NeilBrownfd01b882011-10-11 16:47:53 +11008107static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11008108{
8109 int new_layout;
8110
8111 switch (mddev->layout) {
8112 case ALGORITHM_LEFT_ASYMMETRIC_6:
8113 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
8114 break;
8115 case ALGORITHM_RIGHT_ASYMMETRIC_6:
8116 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
8117 break;
8118 case ALGORITHM_LEFT_SYMMETRIC_6:
8119 new_layout = ALGORITHM_LEFT_SYMMETRIC;
8120 break;
8121 case ALGORITHM_RIGHT_SYMMETRIC_6:
8122 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
8123 break;
8124 case ALGORITHM_PARITY_0_6:
8125 new_layout = ALGORITHM_PARITY_0;
8126 break;
8127 case ALGORITHM_PARITY_N:
8128 new_layout = ALGORITHM_PARITY_N;
8129 break;
8130 default:
8131 return ERR_PTR(-EINVAL);
8132 }
8133 mddev->new_level = 5;
8134 mddev->new_layout = new_layout;
8135 mddev->delta_disks = -1;
8136 mddev->raid_disks -= 1;
8137 return setup_conf(mddev);
8138}
8139
NeilBrownfd01b882011-10-11 16:47:53 +11008140static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11008141{
NeilBrown88ce4932009-03-31 15:24:23 +11008142 /* For a 2-drive array, the layout and chunk size can be changed
8143 * immediately as not restriping is needed.
8144 * For larger arrays we record the new value - after validation
8145 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11008146 */
NeilBrownd1688a62011-10-11 16:49:52 +11008147 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10008148 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11008149
NeilBrown597a7112009-06-18 08:47:42 +10008150 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11008151 return -EINVAL;
8152 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008153 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11008154 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008155 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11008156 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008157 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11008158 /* not factor of array size */
8159 return -EINVAL;
8160 }
8161
8162 /* They look valid */
8163
NeilBrown88ce4932009-03-31 15:24:23 +11008164 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10008165 /* can make the change immediately */
8166 if (mddev->new_layout >= 0) {
8167 conf->algorithm = mddev->new_layout;
8168 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11008169 }
8170 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10008171 conf->chunk_sectors = new_chunk ;
8172 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11008173 }
Shaohua Li29530792016-12-08 15:48:19 -08008174 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown88ce4932009-03-31 15:24:23 +11008175 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11008176 }
NeilBrown50ac1682009-06-18 08:47:55 +10008177 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11008178}
8179
NeilBrownfd01b882011-10-11 16:47:53 +11008180static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11008181{
NeilBrown597a7112009-06-18 08:47:42 +10008182 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10008183
NeilBrown597a7112009-06-18 08:47:42 +10008184 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11008185 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008186 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008187 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11008188 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008189 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11008190 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008191 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11008192 /* not factor of array size */
8193 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008194 }
NeilBrown88ce4932009-03-31 15:24:23 +11008195
8196 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10008197 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11008198}
8199
NeilBrownfd01b882011-10-11 16:47:53 +11008200static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11008201{
8202 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008203 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008204 * raid1 - if there are two drives. We need to know the chunk size
8205 * raid4 - trivial - just use a raid4 layout.
8206 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008207 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008208 if (mddev->level == 0)
8209 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11008210 if (mddev->level == 1)
8211 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11008212 if (mddev->level == 4) {
8213 mddev->new_layout = ALGORITHM_PARITY_N;
8214 mddev->new_level = 5;
8215 return setup_conf(mddev);
8216 }
NeilBrownfc9739c2009-03-31 14:57:20 +11008217 if (mddev->level == 6)
8218 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11008219
8220 return ERR_PTR(-EINVAL);
8221}
8222
NeilBrownfd01b882011-10-11 16:47:53 +11008223static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11008224{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008225 /* raid4 can take over:
8226 * raid0 - if there is only one strip zone
8227 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11008228 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008229 if (mddev->level == 0)
8230 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11008231 if (mddev->level == 5 &&
8232 mddev->layout == ALGORITHM_PARITY_N) {
8233 mddev->new_layout = 0;
8234 mddev->new_level = 4;
8235 return setup_conf(mddev);
8236 }
8237 return ERR_PTR(-EINVAL);
8238}
NeilBrownd562b0c2009-03-31 14:39:39 +11008239
NeilBrown84fc4b52011-10-11 16:49:58 +11008240static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11008241
NeilBrownfd01b882011-10-11 16:47:53 +11008242static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11008243{
8244 /* Currently can only take over a raid5. We map the
8245 * personality to an equivalent raid6 personality
8246 * with the Q block at the end.
8247 */
8248 int new_layout;
8249
8250 if (mddev->pers != &raid5_personality)
8251 return ERR_PTR(-EINVAL);
8252 if (mddev->degraded > 1)
8253 return ERR_PTR(-EINVAL);
8254 if (mddev->raid_disks > 253)
8255 return ERR_PTR(-EINVAL);
8256 if (mddev->raid_disks < 3)
8257 return ERR_PTR(-EINVAL);
8258
8259 switch (mddev->layout) {
8260 case ALGORITHM_LEFT_ASYMMETRIC:
8261 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
8262 break;
8263 case ALGORITHM_RIGHT_ASYMMETRIC:
8264 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
8265 break;
8266 case ALGORITHM_LEFT_SYMMETRIC:
8267 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
8268 break;
8269 case ALGORITHM_RIGHT_SYMMETRIC:
8270 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
8271 break;
8272 case ALGORITHM_PARITY_0:
8273 new_layout = ALGORITHM_PARITY_0_6;
8274 break;
8275 case ALGORITHM_PARITY_N:
8276 new_layout = ALGORITHM_PARITY_N;
8277 break;
8278 default:
8279 return ERR_PTR(-EINVAL);
8280 }
8281 mddev->new_level = 6;
8282 mddev->new_layout = new_layout;
8283 mddev->delta_disks = 1;
8284 mddev->raid_disks += 1;
8285 return setup_conf(mddev);
8286}
8287
NeilBrown84fc4b52011-10-11 16:49:58 +11008288static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07008289{
8290 .name = "raid6",
8291 .level = 6,
8292 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008293 .make_request = raid5_make_request,
8294 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11008295 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008296 .status = raid5_status,
8297 .error_handler = raid5_error,
NeilBrown16a53ec2006-06-26 00:27:38 -07008298 .hot_add_disk = raid5_add_disk,
8299 .hot_remove_disk= raid5_remove_disk,
8300 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008301 .sync_request = raid5_sync_request,
NeilBrown16a53ec2006-06-26 00:27:38 -07008302 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008303 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10008304 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08008305 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008306 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07008307 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11008308 .takeover = raid6_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008309 .congested = raid5_congested,
NeilBrown16a53ec2006-06-26 00:27:38 -07008310};
NeilBrown84fc4b52011-10-11 16:49:58 +11008311static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008312{
8313 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08008314 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008315 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008316 .make_request = raid5_make_request,
8317 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11008318 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008319 .status = raid5_status,
8320 .error_handler = raid5_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008321 .hot_add_disk = raid5_add_disk,
8322 .hot_remove_disk= raid5_remove_disk,
8323 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008324 .sync_request = raid5_sync_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008325 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008326 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08008327 .check_reshape = raid5_check_reshape,
8328 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008329 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07008330 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11008331 .takeover = raid5_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008332 .congested = raid5_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008333};
8334
NeilBrown84fc4b52011-10-11 16:49:58 +11008335static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008336{
NeilBrown2604b702006-01-06 00:20:36 -08008337 .name = "raid4",
8338 .level = 4,
8339 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008340 .make_request = raid5_make_request,
8341 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11008342 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008343 .status = raid5_status,
8344 .error_handler = raid5_error,
NeilBrown2604b702006-01-06 00:20:36 -08008345 .hot_add_disk = raid5_add_disk,
8346 .hot_remove_disk= raid5_remove_disk,
8347 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008348 .sync_request = raid5_sync_request,
NeilBrown2604b702006-01-06 00:20:36 -08008349 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008350 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08008351 .check_reshape = raid5_check_reshape,
8352 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008353 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08008354 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11008355 .takeover = raid4_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11008356 .congested = raid5_congested,
NeilBrown2604b702006-01-06 00:20:36 -08008357};
8358
8359static int __init raid5_init(void)
8360{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008361 int ret;
8362
Shaohua Li851c30c2013-08-28 14:30:16 +08008363 raid5_wq = alloc_workqueue("raid5wq",
8364 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
8365 if (!raid5_wq)
8366 return -ENOMEM;
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008367
8368 ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
8369 "md/raid5:prepare",
8370 raid456_cpu_up_prepare,
8371 raid456_cpu_dead);
8372 if (ret) {
8373 destroy_workqueue(raid5_wq);
8374 return ret;
8375 }
NeilBrown16a53ec2006-06-26 00:27:38 -07008376 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008377 register_md_personality(&raid5_personality);
8378 register_md_personality(&raid4_personality);
8379 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008380}
8381
NeilBrown2604b702006-01-06 00:20:36 -08008382static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008383{
NeilBrown16a53ec2006-06-26 00:27:38 -07008384 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008385 unregister_md_personality(&raid5_personality);
8386 unregister_md_personality(&raid4_personality);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008387 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
Shaohua Li851c30c2013-08-28 14:30:16 +08008388 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008389}
8390
8391module_init(raid5_init);
8392module_exit(raid5_exit);
8393MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008394MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008395MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08008396MODULE_ALIAS("md-raid5");
8397MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08008398MODULE_ALIAS("md-level-5");
8399MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07008400MODULE_ALIAS("md-personality-8"); /* RAID6 */
8401MODULE_ALIAS("md-raid6");
8402MODULE_ALIAS("md-level-6");
8403
8404/* This used to be two separate modules, they were: */
8405MODULE_ALIAS("raid5");
8406MODULE_ALIAS("raid6");