blob: 86ba7851e881c5e51631d21bf26f635c131f5b6a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
Shaohua Li851c30c2013-08-28 14:30:16 +080056#include <linux/nodemask.h>
shli@kernel.org46d5b782014-12-15 12:57:02 +110057#include <linux/flex_array.h>
NeilBrowna9add5d2012-10-31 11:59:09 +110058#include <trace/events/block.h>
59
NeilBrown43b2e5d2009-03-31 14:33:13 +110060#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110061#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110062#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110063#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070064
Shaohua Li851c30c2013-08-28 14:30:16 +080065#define cpu_to_group(cpu) cpu_to_node(cpu)
66#define ANY_GROUP NUMA_NO_NODE
67
NeilBrown8e0e99b2014-10-02 13:45:00 +100068static bool devices_handle_discard_safely = false;
69module_param(devices_handle_discard_safely, bool, 0644);
70MODULE_PARM_DESC(devices_handle_discard_safely,
71 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
Shaohua Li851c30c2013-08-28 14:30:16 +080072static struct workqueue_struct *raid5_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
74 * Stripe cache
75 */
76
77#define NR_STRIPES 256
78#define STRIPE_SIZE PAGE_SIZE
79#define STRIPE_SHIFT (PAGE_SHIFT - 9)
80#define STRIPE_SECTORS (STRIPE_SIZE>>9)
81#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070082#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080083#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#define HASH_MASK (NR_HASH - 1)
Shaohua Libfc90cb2013-08-29 15:40:32 +080085#define MAX_STRIPE_BATCH 8
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
NeilBrownd1688a62011-10-11 16:49:52 +110087static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110088{
89 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
90 return &conf->stripe_hashtbl[hash];
91}
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Shaohua Li566c09c2013-11-14 15:16:17 +110093static inline int stripe_hash_locks_hash(sector_t sect)
94{
95 return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK;
96}
97
98static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
99{
100 spin_lock_irq(conf->hash_locks + hash);
101 spin_lock(&conf->device_lock);
102}
103
104static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
105{
106 spin_unlock(&conf->device_lock);
107 spin_unlock_irq(conf->hash_locks + hash);
108}
109
110static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
111{
112 int i;
Julia Cartwrightbeaf4452017-04-28 12:41:02 -0500113 spin_lock_irq(conf->hash_locks);
Shaohua Li566c09c2013-11-14 15:16:17 +1100114 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
115 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
116 spin_lock(&conf->device_lock);
117}
118
119static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
120{
121 int i;
122 spin_unlock(&conf->device_lock);
Julia Cartwrightbeaf4452017-04-28 12:41:02 -0500123 for (i = NR_STRIPE_HASH_LOCKS - 1; i; i--)
124 spin_unlock(conf->hash_locks + i);
125 spin_unlock_irq(conf->hash_locks);
Shaohua Li566c09c2013-11-14 15:16:17 +1100126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/* bio's attached to a stripe+device for I/O are linked together in bi_sector
129 * order without overlap. There may be several bio's per stripe+device, and
130 * a bio could span several devices.
131 * When walking this list for a particular stripe+device, we must never proceed
132 * beyond a bio that extends past this device, as the next bio might no longer
133 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +1100134 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * of the current stripe+device
136 */
NeilBrowndb298e12011-10-07 14:23:00 +1100137static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
138{
Kent Overstreetaa8b57a2013-02-05 15:19:29 -0800139 int sectors = bio_sectors(bio);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700140 if (bio->bi_iter.bi_sector + sectors < sector + STRIPE_SECTORS)
NeilBrowndb298e12011-10-07 14:23:00 +1100141 return bio->bi_next;
142 else
143 return NULL;
144}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Jens Axboe960e7392008-08-15 10:41:18 +0200146/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200147 * We maintain a biased count of active stripes in the bottom 16 bits of
148 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200149 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000150static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200151{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000152 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
153 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200154}
155
Shaohua Lie7836bd62012-07-19 16:01:31 +1000156static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200157{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000158 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
159 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200160}
161
Shaohua Lie7836bd62012-07-19 16:01:31 +1000162static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200163{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000164 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
165 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200166}
167
Shaohua Lie7836bd62012-07-19 16:01:31 +1000168static inline void raid5_set_bi_processed_stripes(struct bio *bio,
169 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200170{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000171 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
172 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200173
Shaohua Lie7836bd62012-07-19 16:01:31 +1000174 do {
175 old = atomic_read(segments);
176 new = (old & 0xffff) | (cnt << 16);
177 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200178}
179
Shaohua Lie7836bd62012-07-19 16:01:31 +1000180static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200181{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000182 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
183 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200184}
185
NeilBrownd0dabf72009-03-31 14:39:38 +1100186/* Find first data disk in a raid6 stripe */
187static inline int raid6_d0(struct stripe_head *sh)
188{
NeilBrown67cc2b82009-03-31 14:39:38 +1100189 if (sh->ddf_layout)
190 /* ddf always start from first device */
191 return 0;
192 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100193 if (sh->qd_idx == sh->disks - 1)
194 return 0;
195 else
196 return sh->qd_idx + 1;
197}
NeilBrown16a53ec2006-06-26 00:27:38 -0700198static inline int raid6_next_disk(int disk, int raid_disks)
199{
200 disk++;
201 return (disk < raid_disks) ? disk : 0;
202}
Dan Williamsa4456852007-07-09 11:56:43 -0700203
NeilBrownd0dabf72009-03-31 14:39:38 +1100204/* When walking through the disks in a raid5, starting at raid6_d0,
205 * We need to map each disk to a 'slot', where the data disks are slot
206 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
207 * is raid_disks-1. This help does that mapping.
208 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100209static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
210 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100211{
Dan Williams66295422009-10-19 18:09:32 -0700212 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100213
NeilBrowne4424fe2009-10-16 16:27:34 +1100214 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700215 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100216 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100217 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100218 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100219 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100220 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700221 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100222 return slot;
223}
224
NeilBrown34a6f802015-08-14 12:07:57 +1000225static void return_io(struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -0700226{
NeilBrown34a6f802015-08-14 12:07:57 +1000227 struct bio *bi;
228 while ((bi = bio_list_pop(return_bi)) != NULL) {
Kent Overstreet4f024f32013-10-11 15:44:27 -0700229 bi->bi_iter.bi_size = 0;
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700230 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
231 bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200232 bio_endio(bi);
Dan Williamsa4456852007-07-09 11:56:43 -0700233 }
234}
235
NeilBrownd1688a62011-10-11 16:49:52 +1100236static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Dan Williams600aa102008-06-28 08:32:05 +1000238static int stripe_operations_active(struct stripe_head *sh)
239{
240 return sh->check_state || sh->reconstruct_state ||
241 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
242 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
243}
244
Shaohua Li851c30c2013-08-28 14:30:16 +0800245static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
246{
247 struct r5conf *conf = sh->raid_conf;
248 struct r5worker_group *group;
Shaohua Libfc90cb2013-08-29 15:40:32 +0800249 int thread_cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +0800250 int i, cpu = sh->cpu;
251
252 if (!cpu_online(cpu)) {
253 cpu = cpumask_any(cpu_online_mask);
254 sh->cpu = cpu;
255 }
256
257 if (list_empty(&sh->lru)) {
258 struct r5worker_group *group;
259 group = conf->worker_groups + cpu_to_group(cpu);
260 list_add_tail(&sh->lru, &group->handle_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800261 group->stripes_cnt++;
262 sh->group = group;
Shaohua Li851c30c2013-08-28 14:30:16 +0800263 }
264
265 if (conf->worker_cnt_per_group == 0) {
266 md_wakeup_thread(conf->mddev->thread);
267 return;
268 }
269
270 group = conf->worker_groups + cpu_to_group(sh->cpu);
271
Shaohua Libfc90cb2013-08-29 15:40:32 +0800272 group->workers[0].working = true;
273 /* at least one worker should run to avoid race */
274 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
275
276 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
277 /* wakeup more workers */
278 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
279 if (group->workers[i].working == false) {
280 group->workers[i].working = true;
281 queue_work_on(sh->cpu, raid5_wq,
282 &group->workers[i].work);
283 thread_cnt--;
284 }
285 }
Shaohua Li851c30c2013-08-28 14:30:16 +0800286}
287
Shaohua Li566c09c2013-11-14 15:16:17 +1100288static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
289 struct list_head *temp_inactive_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000291 BUG_ON(!list_empty(&sh->lru));
292 BUG_ON(atomic_read(&conf->active_stripes)==0);
293 if (test_bit(STRIPE_HANDLE, &sh->state)) {
294 if (test_bit(STRIPE_DELAYED, &sh->state) &&
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500295 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
Shaohua Li4eb788d2012-07-19 16:01:31 +1000296 list_add_tail(&sh->lru, &conf->delayed_list);
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500297 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
Shaohua Li4eb788d2012-07-19 16:01:31 +1000298 sh->bm_seq - conf->seq_write > 0)
299 list_add_tail(&sh->lru, &conf->bitmap_list);
300 else {
301 clear_bit(STRIPE_DELAYED, &sh->state);
302 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Shaohua Li851c30c2013-08-28 14:30:16 +0800303 if (conf->worker_cnt_per_group == 0) {
304 list_add_tail(&sh->lru, &conf->handle_list);
305 } else {
306 raid5_wakeup_stripe_thread(sh);
307 return;
308 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000309 }
310 md_wakeup_thread(conf->mddev->thread);
311 } else {
312 BUG_ON(stripe_operations_active(sh));
313 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
314 if (atomic_dec_return(&conf->preread_active_stripes)
315 < IO_THRESHOLD)
316 md_wakeup_thread(conf->mddev->thread);
317 atomic_dec(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100318 if (!test_bit(STRIPE_EXPANDING, &sh->state))
319 list_add_tail(&sh->lru, temp_inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321}
NeilBrownd0dabf72009-03-31 14:39:38 +1100322
Shaohua Li566c09c2013-11-14 15:16:17 +1100323static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
324 struct list_head *temp_inactive_list)
Shaohua Li4eb788d2012-07-19 16:01:31 +1000325{
326 if (atomic_dec_and_test(&sh->count))
Shaohua Li566c09c2013-11-14 15:16:17 +1100327 do_release_stripe(conf, sh, temp_inactive_list);
328}
329
330/*
331 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
332 *
333 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
334 * given time. Adding stripes only takes device lock, while deleting stripes
335 * only takes hash lock.
336 */
337static void release_inactive_stripe_list(struct r5conf *conf,
338 struct list_head *temp_inactive_list,
339 int hash)
340{
341 int size;
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800342 bool do_wakeup = false;
Shaohua Li566c09c2013-11-14 15:16:17 +1100343 unsigned long flags;
344
345 if (hash == NR_STRIPE_HASH_LOCKS) {
346 size = NR_STRIPE_HASH_LOCKS;
347 hash = NR_STRIPE_HASH_LOCKS - 1;
348 } else
349 size = 1;
350 while (size) {
351 struct list_head *list = &temp_inactive_list[size - 1];
352
353 /*
Shaohua Li6d036f72015-08-13 14:31:57 -0700354 * We don't hold any lock here yet, raid5_get_active_stripe() might
Shaohua Li566c09c2013-11-14 15:16:17 +1100355 * remove stripes from the list
356 */
357 if (!list_empty_careful(list)) {
358 spin_lock_irqsave(conf->hash_locks + hash, flags);
Shaohua Li4bda5562013-11-14 15:16:17 +1100359 if (list_empty(conf->inactive_list + hash) &&
360 !list_empty(list))
361 atomic_dec(&conf->empty_inactive_list_nr);
Shaohua Li566c09c2013-11-14 15:16:17 +1100362 list_splice_tail_init(list, conf->inactive_list + hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800363 do_wakeup = true;
Shaohua Li566c09c2013-11-14 15:16:17 +1100364 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
365 }
366 size--;
367 hash--;
368 }
369
370 if (do_wakeup) {
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800371 wake_up(&conf->wait_for_stripe);
Yuanhan Liub1b46482015-05-08 18:19:06 +1000372 if (atomic_read(&conf->active_stripes) == 0)
373 wake_up(&conf->wait_for_quiescent);
Shaohua Li566c09c2013-11-14 15:16:17 +1100374 if (conf->retry_read_aligned)
375 md_wakeup_thread(conf->mddev->thread);
376 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000377}
378
Shaohua Li773ca822013-08-27 17:50:39 +0800379/* should hold conf->device_lock already */
Shaohua Li566c09c2013-11-14 15:16:17 +1100380static int release_stripe_list(struct r5conf *conf,
381 struct list_head *temp_inactive_list)
Shaohua Li773ca822013-08-27 17:50:39 +0800382{
383 struct stripe_head *sh;
384 int count = 0;
385 struct llist_node *head;
386
387 head = llist_del_all(&conf->released_stripes);
Shaohua Lid265d9d2013-08-28 14:29:05 +0800388 head = llist_reverse_order(head);
Shaohua Li773ca822013-08-27 17:50:39 +0800389 while (head) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100390 int hash;
391
Shaohua Li773ca822013-08-27 17:50:39 +0800392 sh = llist_entry(head, struct stripe_head, release_list);
393 head = llist_next(head);
394 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
395 smp_mb();
396 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
397 /*
398 * Don't worry the bit is set here, because if the bit is set
399 * again, the count is always > 1. This is true for
400 * STRIPE_ON_UNPLUG_LIST bit too.
401 */
Shaohua Li566c09c2013-11-14 15:16:17 +1100402 hash = sh->hash_lock_index;
403 __release_stripe(conf, sh, &temp_inactive_list[hash]);
Shaohua Li773ca822013-08-27 17:50:39 +0800404 count++;
405 }
406
407 return count;
408}
409
Shaohua Li6d036f72015-08-13 14:31:57 -0700410void raid5_release_stripe(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
NeilBrownd1688a62011-10-11 16:49:52 +1100412 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 unsigned long flags;
Shaohua Li566c09c2013-11-14 15:16:17 +1100414 struct list_head list;
415 int hash;
Shaohua Li773ca822013-08-27 17:50:39 +0800416 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700417
Eivind Sartocf170f32014-05-28 13:39:23 +1000418 /* Avoid release_list until the last reference.
419 */
420 if (atomic_add_unless(&sh->count, -1, 1))
421 return;
422
majianpengad4068d2013-11-14 15:16:15 +1100423 if (unlikely(!conf->mddev->thread) ||
424 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
Shaohua Li773ca822013-08-27 17:50:39 +0800425 goto slow_path;
426 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
427 if (wakeup)
428 md_wakeup_thread(conf->mddev->thread);
429 return;
430slow_path:
Shaohua Li4eb788d2012-07-19 16:01:31 +1000431 local_irq_save(flags);
Shaohua Li773ca822013-08-27 17:50:39 +0800432 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Shaohua Li4eb788d2012-07-19 16:01:31 +1000433 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100434 INIT_LIST_HEAD(&list);
435 hash = sh->hash_lock_index;
436 do_release_stripe(conf, sh, &list);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000437 spin_unlock(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +1100438 release_inactive_stripe_list(conf, &list, hash);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000439 }
440 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
NeilBrownfccddba2006-01-06 00:20:33 -0800443static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Dan Williams45b42332007-07-09 11:56:43 -0700445 pr_debug("remove_hash(), stripe %llu\n",
446 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
NeilBrownfccddba2006-01-06 00:20:33 -0800448 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
NeilBrownd1688a62011-10-11 16:49:52 +1100451static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
NeilBrownfccddba2006-01-06 00:20:33 -0800453 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Dan Williams45b42332007-07-09 11:56:43 -0700455 pr_debug("insert_hash(), stripe %llu\n",
456 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
NeilBrownfccddba2006-01-06 00:20:33 -0800458 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/* find an idle stripe, make sure it is unhashed, and return it. */
Shaohua Li566c09c2013-11-14 15:16:17 +1100462static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct stripe_head *sh = NULL;
465 struct list_head *first;
466
Shaohua Li566c09c2013-11-14 15:16:17 +1100467 if (list_empty(conf->inactive_list + hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 goto out;
Shaohua Li566c09c2013-11-14 15:16:17 +1100469 first = (conf->inactive_list + hash)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 sh = list_entry(first, struct stripe_head, lru);
471 list_del_init(first);
472 remove_hash(sh);
473 atomic_inc(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100474 BUG_ON(hash != sh->hash_lock_index);
Shaohua Li4bda5562013-11-14 15:16:17 +1100475 if (list_empty(conf->inactive_list + hash))
476 atomic_inc(&conf->empty_inactive_list_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477out:
478 return sh;
479}
480
NeilBrowne4e11e32010-06-16 16:45:16 +1000481static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 struct page *p;
484 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000485 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
NeilBrowne4e11e32010-06-16 16:45:16 +1000487 for (i = 0; i < num ; i++) {
Shaohua Lid592a992014-05-21 17:57:44 +0800488 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 p = sh->dev[i].page;
490 if (!p)
491 continue;
492 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800493 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495}
496
NeilBrowna9683a72015-02-25 12:02:51 +1100497static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
499 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000500 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
NeilBrowne4e11e32010-06-16 16:45:16 +1000502 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct page *page;
504
NeilBrowna9683a72015-02-25 12:02:51 +1100505 if (!(page = alloc_page(gfp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return 1;
507 }
508 sh->dev[i].page = page;
Shaohua Lid592a992014-05-21 17:57:44 +0800509 sh->dev[i].orig_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511 return 0;
512}
513
NeilBrown784052e2009-03-31 15:19:07 +1100514static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100515static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100516 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
NeilBrownb5663ba2009-03-31 14:39:38 +1100518static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
NeilBrownd1688a62011-10-11 16:49:52 +1100520 struct r5conf *conf = sh->raid_conf;
Shaohua Li566c09c2013-11-14 15:16:17 +1100521 int i, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200523 BUG_ON(atomic_read(&sh->count) != 0);
524 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000525 BUG_ON(stripe_operations_active(sh));
shli@kernel.org59fc6302014-12-15 12:57:03 +1100526 BUG_ON(sh->batch_head);
Dan Williamsd84e0f12007-01-02 13:52:30 -0700527
Dan Williams45b42332007-07-09 11:56:43 -0700528 pr_debug("init_stripe called, stripe %llu\n",
Markus Stockhausenb8e6a152014-08-23 20:19:27 +1000529 (unsigned long long)sector);
Shaohua Li566c09c2013-11-14 15:16:17 +1100530retry:
531 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100532 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100533 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100535 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 sh->state = 0;
537
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800538 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 struct r5dev *dev = &sh->dev[i];
540
Dan Williamsd84e0f12007-01-02 13:52:30 -0700541 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700543 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700545 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000547 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100550 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100552 if (read_seqcount_retry(&conf->gen_lock, seq))
553 goto retry;
shli@kernel.org7a87f432014-12-15 12:57:03 +1100554 sh->overwrite_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800556 sh->cpu = smp_processor_id();
shli@kernel.orgda41ba62014-12-15 12:57:03 +1100557 set_bit(STRIPE_BATCH_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
NeilBrownd1688a62011-10-11 16:49:52 +1100560static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100561 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct stripe_head *sh;
564
Dan Williams45b42332007-07-09 11:56:43 -0700565 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800566 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100567 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700569 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return NULL;
571}
572
NeilBrown674806d2010-06-16 17:17:53 +1000573/*
574 * Need to check if array has failed when deciding whether to:
575 * - start an array
576 * - remove non-faulty devices
577 * - add a spare
578 * - allow a reshape
579 * This determination is simple when no reshape is happening.
580 * However if there is a reshape, we need to carefully check
581 * both the before and after sections.
582 * This is because some failed devices may only affect one
583 * of the two sections, and some non-in_sync devices may
584 * be insync in the section most affected by failed devices.
585 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100586static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000587{
NeilBrown908f4fb2011-12-23 10:17:50 +1100588 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000589 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000590
591 rcu_read_lock();
592 degraded = 0;
593 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100594 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000595 if (rdev && test_bit(Faulty, &rdev->flags))
596 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000597 if (!rdev || test_bit(Faulty, &rdev->flags))
598 degraded++;
599 else if (test_bit(In_sync, &rdev->flags))
600 ;
601 else
602 /* not in-sync or faulty.
603 * If the reshape increases the number of devices,
604 * this is being recovered by the reshape, so
605 * this 'previous' section is not in_sync.
606 * If the number of devices is being reduced however,
607 * the device can only be part of the array if
608 * we are reverting a reshape, so this section will
609 * be in-sync.
610 */
611 if (conf->raid_disks >= conf->previous_raid_disks)
612 degraded++;
613 }
614 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100615 if (conf->raid_disks == conf->previous_raid_disks)
616 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000617 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100618 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000619 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100620 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000621 if (rdev && test_bit(Faulty, &rdev->flags))
622 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000623 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100624 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000625 else if (test_bit(In_sync, &rdev->flags))
626 ;
627 else
628 /* not in-sync or faulty.
629 * If reshape increases the number of devices, this
630 * section has already been recovered, else it
631 * almost certainly hasn't.
632 */
633 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100634 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000635 }
636 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100637 if (degraded2 > degraded)
638 return degraded2;
639 return degraded;
640}
641
642static int has_failed(struct r5conf *conf)
643{
644 int degraded;
645
646 if (conf->mddev->reshape_position == MaxSector)
647 return conf->mddev->degraded > conf->max_degraded;
648
649 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000650 if (degraded > conf->max_degraded)
651 return 1;
652 return 0;
653}
654
Shaohua Li6d036f72015-08-13 14:31:57 -0700655struct stripe_head *
656raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
657 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 struct stripe_head *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +1100660 int hash = stripe_hash_locks_hash(sector);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800661 int inc_empty_inactive_list_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Dan Williams45b42332007-07-09 11:56:43 -0700663 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Shaohua Li566c09c2013-11-14 15:16:17 +1100665 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 do {
Yuanhan Liub1b46482015-05-08 18:19:06 +1000668 wait_event_lock_irq(conf->wait_for_quiescent,
NeilBrowna8c906c2009-06-09 14:39:59 +1000669 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100670 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100671 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (!sh) {
NeilBrownedbe83a2015-02-26 12:47:56 +1100673 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100674 sh = get_free_stripe(conf, hash);
Shaohua Li713bc5c2015-05-28 17:33:47 -0700675 if (!sh && !test_bit(R5_DID_ALLOC,
676 &conf->cache_state))
NeilBrownedbe83a2015-02-26 12:47:56 +1100677 set_bit(R5_ALLOC_MORE,
678 &conf->cache_state);
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (noblock && sh == NULL)
681 break;
682 if (!sh) {
NeilBrown54233992015-02-26 12:21:04 +1100683 set_bit(R5_INACTIVE_BLOCKED,
684 &conf->cache_state);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800685 wait_event_lock_irq(
686 conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +1100687 !list_empty(conf->inactive_list + hash) &&
688 (atomic_read(&conf->active_stripes)
689 < (conf->max_nr_stripes * 3 / 4)
NeilBrown54233992015-02-26 12:21:04 +1100690 || !test_bit(R5_INACTIVE_BLOCKED,
691 &conf->cache_state)),
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800692 *(conf->hash_locks + hash));
NeilBrown54233992015-02-26 12:21:04 +1100693 clear_bit(R5_INACTIVE_BLOCKED,
694 &conf->cache_state);
NeilBrown7da9d452014-01-22 11:45:03 +1100695 } else {
NeilBrownb5663ba2009-03-31 14:39:38 +1100696 init_stripe(sh, sector, previous);
NeilBrown7da9d452014-01-22 11:45:03 +1100697 atomic_inc(&sh->count);
698 }
Shaohua Lie240c182014-04-09 11:27:42 +0800699 } else if (!atomic_inc_not_zero(&sh->count)) {
NeilBrown6d183de2013-11-28 10:55:27 +1100700 spin_lock(&conf->device_lock);
Shaohua Lie240c182014-04-09 11:27:42 +0800701 if (!atomic_read(&sh->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (!test_bit(STRIPE_HANDLE, &sh->state))
703 atomic_inc(&conf->active_stripes);
NeilBrown5af9bef2014-01-14 15:16:10 +1100704 BUG_ON(list_empty(&sh->lru) &&
705 !test_bit(STRIPE_EXPANDING, &sh->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800706 inc_empty_inactive_list_flag = 0;
707 if (!list_empty(conf->inactive_list + hash))
708 inc_empty_inactive_list_flag = 1;
NeilBrown16a53ec2006-06-26 00:27:38 -0700709 list_del_init(&sh->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800710 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
711 atomic_inc(&conf->empty_inactive_list_nr);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800712 if (sh->group) {
713 sh->group->stripes_cnt--;
714 sh->group = NULL;
715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
NeilBrown7da9d452014-01-22 11:45:03 +1100717 atomic_inc(&sh->count);
NeilBrown6d183de2013-11-28 10:55:27 +1100718 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720 } while (sh == NULL);
721
Shaohua Li566c09c2013-11-14 15:16:17 +1100722 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return sh;
724}
725
shli@kernel.org7a87f432014-12-15 12:57:03 +1100726static bool is_full_stripe_write(struct stripe_head *sh)
727{
728 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
729 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
730}
731
shli@kernel.org59fc6302014-12-15 12:57:03 +1100732static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
733{
shli@kernel.org59fc6302014-12-15 12:57:03 +1100734 if (sh1 > sh2) {
Julia Cartwrightbeaf4452017-04-28 12:41:02 -0500735 spin_lock_irq(&sh2->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100736 spin_lock_nested(&sh1->stripe_lock, 1);
737 } else {
Julia Cartwrightbeaf4452017-04-28 12:41:02 -0500738 spin_lock_irq(&sh1->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100739 spin_lock_nested(&sh2->stripe_lock, 1);
740 }
741}
742
743static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
744{
745 spin_unlock(&sh1->stripe_lock);
Julia Cartwrightbeaf4452017-04-28 12:41:02 -0500746 spin_unlock_irq(&sh2->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100747}
748
749/* Only freshly new full stripe normal write stripe can be added to a batch list */
750static bool stripe_can_batch(struct stripe_head *sh)
751{
Shaohua Li9c3e3332015-08-13 14:32:02 -0700752 struct r5conf *conf = sh->raid_conf;
753
754 if (conf->log)
755 return false;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100756 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
NeilBrownd0852df52015-05-27 08:43:45 +1000757 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
shli@kernel.org59fc6302014-12-15 12:57:03 +1100758 is_full_stripe_write(sh);
759}
760
761/* we only do back search */
762static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
763{
764 struct stripe_head *head;
765 sector_t head_sector, tmp_sec;
766 int hash;
767 int dd_idx;
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800768 int inc_empty_inactive_list_flag;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100769
shli@kernel.org59fc6302014-12-15 12:57:03 +1100770 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
771 tmp_sec = sh->sector;
772 if (!sector_div(tmp_sec, conf->chunk_sectors))
773 return;
774 head_sector = sh->sector - STRIPE_SECTORS;
775
776 hash = stripe_hash_locks_hash(head_sector);
777 spin_lock_irq(conf->hash_locks + hash);
778 head = __find_stripe(conf, head_sector, conf->generation);
779 if (head && !atomic_inc_not_zero(&head->count)) {
780 spin_lock(&conf->device_lock);
781 if (!atomic_read(&head->count)) {
782 if (!test_bit(STRIPE_HANDLE, &head->state))
783 atomic_inc(&conf->active_stripes);
784 BUG_ON(list_empty(&head->lru) &&
785 !test_bit(STRIPE_EXPANDING, &head->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800786 inc_empty_inactive_list_flag = 0;
787 if (!list_empty(conf->inactive_list + hash))
788 inc_empty_inactive_list_flag = 1;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100789 list_del_init(&head->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800790 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
791 atomic_inc(&conf->empty_inactive_list_nr);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100792 if (head->group) {
793 head->group->stripes_cnt--;
794 head->group = NULL;
795 }
796 }
797 atomic_inc(&head->count);
798 spin_unlock(&conf->device_lock);
799 }
800 spin_unlock_irq(conf->hash_locks + hash);
801
802 if (!head)
803 return;
804 if (!stripe_can_batch(head))
805 goto out;
806
807 lock_two_stripes(head, sh);
808 /* clear_batch_ready clear the flag */
809 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
810 goto unlock_out;
811
812 if (sh->batch_head)
813 goto unlock_out;
814
815 dd_idx = 0;
816 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
817 dd_idx++;
Jens Axboe1eff9d32016-08-05 15:35:16 -0600818 if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
Mike Christie796a5cf2016-06-05 14:32:07 -0500819 bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
shli@kernel.org59fc6302014-12-15 12:57:03 +1100820 goto unlock_out;
821
822 if (head->batch_head) {
823 spin_lock(&head->batch_head->batch_lock);
824 /* This batch list is already running */
825 if (!stripe_can_batch(head)) {
826 spin_unlock(&head->batch_head->batch_lock);
827 goto unlock_out;
828 }
Shaohua Li648798c2017-08-25 10:40:02 -0700829 /*
830 * We must assign batch_head of this stripe within the
831 * batch_lock, otherwise clear_batch_ready of batch head
832 * stripe could clear BATCH_READY bit of this stripe and
833 * this stripe->batch_head doesn't get assigned, which
834 * could confuse clear_batch_ready for this stripe
835 */
836 sh->batch_head = head->batch_head;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100837
838 /*
839 * at this point, head's BATCH_READY could be cleared, but we
840 * can still add the stripe to batch list
841 */
842 list_add(&sh->batch_list, &head->batch_list);
843 spin_unlock(&head->batch_head->batch_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100844 } else {
845 head->batch_head = head;
846 sh->batch_head = head->batch_head;
847 spin_lock(&head->batch_lock);
848 list_add_tail(&sh->batch_list, &head->batch_list);
849 spin_unlock(&head->batch_lock);
850 }
851
852 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
853 if (atomic_dec_return(&conf->preread_active_stripes)
854 < IO_THRESHOLD)
855 md_wakeup_thread(conf->mddev->thread);
856
NeilBrown2b6b2452015-05-21 15:10:01 +1000857 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
858 int seq = sh->bm_seq;
859 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
860 sh->batch_head->bm_seq > seq)
861 seq = sh->batch_head->bm_seq;
862 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
863 sh->batch_head->bm_seq = seq;
864 }
865
shli@kernel.org59fc6302014-12-15 12:57:03 +1100866 atomic_inc(&sh->count);
867unlock_out:
868 unlock_two_stripes(head, sh);
869out:
Shaohua Li6d036f72015-08-13 14:31:57 -0700870 raid5_release_stripe(head);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100871}
872
NeilBrown05616be2012-05-21 09:27:00 +1000873/* Determine if 'data_offset' or 'new_data_offset' should be used
874 * in this stripe_head.
875 */
876static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
877{
878 sector_t progress = conf->reshape_progress;
879 /* Need a memory barrier to make sure we see the value
880 * of conf->generation, or ->data_offset that was set before
881 * reshape_progress was updated.
882 */
883 smp_rmb();
884 if (progress == MaxSector)
885 return 0;
886 if (sh->generation == conf->generation - 1)
887 return 0;
888 /* We are in a reshape, and this is a new-generation stripe,
889 * so use new_data_offset.
890 */
891 return 1;
892}
893
NeilBrown6712ecf2007-09-27 12:47:43 +0200894static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200895raid5_end_read_request(struct bio *bi);
NeilBrown6712ecf2007-09-27 12:47:43 +0200896static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200897raid5_end_write_request(struct bio *bi);
Dan Williams91c00922007-01-02 13:52:30 -0700898
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000899static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700900{
NeilBrownd1688a62011-10-11 16:49:52 +1100901 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700902 int i, disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100903 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -0700904
905 might_sleep();
906
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700907 if (r5l_write_stripe(conf->log, sh) == 0)
908 return;
Dan Williams91c00922007-01-02 13:52:30 -0700909 for (i = disks; i--; ) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500910 int op, op_flags = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +1100911 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100912 struct bio *bi, *rbi;
913 struct md_rdev *rdev, *rrdev = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100914
915 sh = head_sh;
Tejun Heoe9c74692010-09-03 11:56:18 +0200916 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500917 op = REQ_OP_WRITE;
Tejun Heoe9c74692010-09-03 11:56:18 +0200918 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -0500919 op_flags = WRITE_FUA;
Shaohua Li9e4447682012-10-11 13:49:49 +1100920 if (test_bit(R5_Discard, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -0500921 op = REQ_OP_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200922 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -0500923 op = REQ_OP_READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100924 else if (test_and_clear_bit(R5_WantReplace,
925 &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500926 op = REQ_OP_WRITE;
NeilBrown9a3e1102011-12-23 10:17:53 +1100927 replace_only = 1;
928 } else
Dan Williams91c00922007-01-02 13:52:30 -0700929 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000930 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -0500931 op_flags |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700932
shli@kernel.org59fc6302014-12-15 12:57:03 +1100933again:
Dan Williams91c00922007-01-02 13:52:30 -0700934 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100935 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700936
Dan Williams91c00922007-01-02 13:52:30 -0700937 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100938 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100939 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
940 rdev = rcu_dereference(conf->disks[i].rdev);
941 if (!rdev) {
942 rdev = rrdev;
943 rrdev = NULL;
944 }
Mike Christie796a5cf2016-06-05 14:32:07 -0500945 if (op_is_write(op)) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100946 if (replace_only)
947 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100948 if (rdev == rrdev)
949 /* We raced and saw duplicates */
950 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100951 } else {
shli@kernel.org59fc6302014-12-15 12:57:03 +1100952 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100953 rdev = rrdev;
954 rrdev = NULL;
955 }
NeilBrown977df362011-12-23 10:17:53 +1100956
Dan Williams91c00922007-01-02 13:52:30 -0700957 if (rdev && test_bit(Faulty, &rdev->flags))
958 rdev = NULL;
959 if (rdev)
960 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100961 if (rrdev && test_bit(Faulty, &rrdev->flags))
962 rrdev = NULL;
963 if (rrdev)
964 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700965 rcu_read_unlock();
966
NeilBrown73e92e52011-07-28 11:39:22 +1000967 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100968 * need to check for writes. We never accept write errors
969 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000970 */
Mike Christie796a5cf2016-06-05 14:32:07 -0500971 while (op_is_write(op) && rdev &&
NeilBrown73e92e52011-07-28 11:39:22 +1000972 test_bit(WriteErrorSeen, &rdev->flags)) {
973 sector_t first_bad;
974 int bad_sectors;
975 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
976 &first_bad, &bad_sectors);
977 if (!bad)
978 break;
979
980 if (bad < 0) {
981 set_bit(BlockedBadBlocks, &rdev->flags);
982 if (!conf->mddev->external &&
983 conf->mddev->flags) {
984 /* It is very unlikely, but we might
985 * still need to write out the
986 * bad block log - better give it
987 * a chance*/
988 md_check_recovery(conf->mddev);
989 }
majianpeng18507532012-07-03 12:11:54 +1000990 /*
991 * Because md_wait_for_blocked_rdev
992 * will dec nr_pending, we must
993 * increment it first.
994 */
995 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000996 md_wait_for_blocked_rdev(rdev, conf->mddev);
997 } else {
998 /* Acknowledged bad block - skip the write */
999 rdev_dec_pending(rdev, conf->mddev);
1000 rdev = NULL;
1001 }
1002 }
1003
Dan Williams91c00922007-01-02 13:52:30 -07001004 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001005 if (s->syncing || s->expanding || s->expanded
1006 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -07001007 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1008
Dan Williams2b7497f2008-06-28 08:31:52 +10001009 set_bit(STRIPE_IO_STARTED, &sh->state);
1010
Dan Williams91c00922007-01-02 13:52:30 -07001011 bi->bi_bdev = rdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05001012 bio_set_op_attrs(bi, op, op_flags);
1013 bi->bi_end_io = op_is_write(op)
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001014 ? raid5_end_write_request
1015 : raid5_end_read_request;
1016 bi->bi_private = sh;
1017
Mike Christie6296b962016-06-05 14:32:21 -05001018 pr_debug("%s: for %llu schedule op %d on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07001019 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001020 bi->bi_opf, i);
Dan Williams91c00922007-01-02 13:52:30 -07001021 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001022 if (sh != head_sh)
1023 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001024 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001025 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001026 + rdev->new_data_offset);
1027 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001028 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001029 + rdev->data_offset);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001030 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
Jens Axboe1eff9d32016-08-05 15:35:16 -06001031 bi->bi_opf |= REQ_NOMERGE;
majianpeng3f9e7c12012-07-31 10:04:21 +10001032
Shaohua Lid592a992014-05-21 17:57:44 +08001033 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1034 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1035 sh->dev[i].vec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001036 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001037 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1038 bi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001039 bi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001040 /*
1041 * If this is discard request, set bi_vcnt 0. We don't
1042 * want to confuse SCSI because SCSI will replace payload
1043 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001044 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001045 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +11001046 if (rrdev)
1047 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001048
1049 if (conf->mddev->gendisk)
1050 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
1051 bi, disk_devt(conf->mddev->gendisk),
1052 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -07001053 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +11001054 }
1055 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001056 if (s->syncing || s->expanding || s->expanded
1057 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +11001058 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
1059
1060 set_bit(STRIPE_IO_STARTED, &sh->state);
1061
1062 rbi->bi_bdev = rrdev->bdev;
Mike Christie796a5cf2016-06-05 14:32:07 -05001063 bio_set_op_attrs(rbi, op, op_flags);
1064 BUG_ON(!op_is_write(op));
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001065 rbi->bi_end_io = raid5_end_write_request;
1066 rbi->bi_private = sh;
1067
Mike Christie6296b962016-06-05 14:32:21 -05001068 pr_debug("%s: for %llu schedule op %d on "
NeilBrown977df362011-12-23 10:17:53 +11001069 "replacement disc %d\n",
1070 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001071 rbi->bi_opf, i);
NeilBrown977df362011-12-23 10:17:53 +11001072 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001073 if (sh != head_sh)
1074 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001075 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001076 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001077 + rrdev->new_data_offset);
1078 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001079 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001080 + rrdev->data_offset);
Shaohua Lid592a992014-05-21 17:57:44 +08001081 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1082 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1083 sh->dev[i].rvec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001084 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +11001085 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1086 rbi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001087 rbi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001088 /*
1089 * If this is discard request, set bi_vcnt 0. We don't
1090 * want to confuse SCSI because SCSI will replace payload
1091 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001092 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001093 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001094 if (conf->mddev->gendisk)
1095 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
1096 rbi, disk_devt(conf->mddev->gendisk),
1097 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +11001098 generic_make_request(rbi);
1099 }
1100 if (!rdev && !rrdev) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001101 if (op_is_write(op))
Dan Williams91c00922007-01-02 13:52:30 -07001102 set_bit(STRIPE_DEGRADED, &sh->state);
Mike Christie6296b962016-06-05 14:32:21 -05001103 pr_debug("skip op %d on disc %d for sector %llu\n",
Jens Axboe1eff9d32016-08-05 15:35:16 -06001104 bi->bi_opf, i, (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001105 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1106 set_bit(STRIPE_HANDLE, &sh->state);
1107 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001108
1109 if (!head_sh->batch_head)
1110 continue;
1111 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1112 batch_list);
1113 if (sh != head_sh)
1114 goto again;
Dan Williams91c00922007-01-02 13:52:30 -07001115 }
1116}
1117
1118static struct dma_async_tx_descriptor *
Shaohua Lid592a992014-05-21 17:57:44 +08001119async_copy_data(int frombio, struct bio *bio, struct page **page,
1120 sector_t sector, struct dma_async_tx_descriptor *tx,
1121 struct stripe_head *sh)
Dan Williams91c00922007-01-02 13:52:30 -07001122{
Kent Overstreet79886132013-11-23 17:19:00 -08001123 struct bio_vec bvl;
1124 struct bvec_iter iter;
Dan Williams91c00922007-01-02 13:52:30 -07001125 struct page *bio_page;
Dan Williams91c00922007-01-02 13:52:30 -07001126 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -07001127 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -07001128 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001129
Kent Overstreet4f024f32013-10-11 15:44:27 -07001130 if (bio->bi_iter.bi_sector >= sector)
1131 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
Dan Williams91c00922007-01-02 13:52:30 -07001132 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001133 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -07001134
Dan Williams0403e382009-09-08 17:42:50 -07001135 if (frombio)
1136 flags |= ASYNC_TX_FENCE;
1137 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1138
Kent Overstreet79886132013-11-23 17:19:00 -08001139 bio_for_each_segment(bvl, bio, iter) {
1140 int len = bvl.bv_len;
Dan Williams91c00922007-01-02 13:52:30 -07001141 int clen;
1142 int b_offset = 0;
1143
1144 if (page_offset < 0) {
1145 b_offset = -page_offset;
1146 page_offset += b_offset;
1147 len -= b_offset;
1148 }
1149
1150 if (len > 0 && page_offset + len > STRIPE_SIZE)
1151 clen = STRIPE_SIZE - page_offset;
1152 else
1153 clen = len;
1154
1155 if (clen > 0) {
Kent Overstreet79886132013-11-23 17:19:00 -08001156 b_offset += bvl.bv_offset;
1157 bio_page = bvl.bv_page;
Shaohua Lid592a992014-05-21 17:57:44 +08001158 if (frombio) {
1159 if (sh->raid_conf->skip_copy &&
1160 b_offset == 0 && page_offset == 0 &&
1161 clen == STRIPE_SIZE)
1162 *page = bio_page;
1163 else
1164 tx = async_memcpy(*page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001165 b_offset, clen, &submit);
Shaohua Lid592a992014-05-21 17:57:44 +08001166 } else
1167 tx = async_memcpy(bio_page, *page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001168 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001169 }
Dan Williamsa08abd82009-06-03 11:43:59 -07001170 /* chain the operations */
1171 submit.depend_tx = tx;
1172
Dan Williams91c00922007-01-02 13:52:30 -07001173 if (clen < len) /* hit end of page */
1174 break;
1175 page_offset += len;
1176 }
1177
1178 return tx;
1179}
1180
1181static void ops_complete_biofill(void *stripe_head_ref)
1182{
1183 struct stripe_head *sh = stripe_head_ref;
NeilBrown34a6f802015-08-14 12:07:57 +10001184 struct bio_list return_bi = BIO_EMPTY_LIST;
Dan Williamse4d84902007-09-24 10:06:13 -07001185 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001186
Harvey Harrisone46b2722008-04-28 02:15:50 -07001187 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001188 (unsigned long long)sh->sector);
1189
1190 /* clear completed biofills */
1191 for (i = sh->disks; i--; ) {
1192 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001193
1194 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001195 /* and check if we need to reply to a read request,
1196 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001197 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001198 */
1199 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001200 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001201
Dan Williams91c00922007-01-02 13:52:30 -07001202 BUG_ON(!dev->read);
1203 rbi = dev->read;
1204 dev->read = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001205 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001206 dev->sector + STRIPE_SECTORS) {
1207 rbi2 = r5_next_bio(rbi, dev->sector);
NeilBrown34a6f802015-08-14 12:07:57 +10001208 if (!raid5_dec_bi_active_stripes(rbi))
1209 bio_list_add(&return_bi, rbi);
Dan Williams91c00922007-01-02 13:52:30 -07001210 rbi = rbi2;
1211 }
1212 }
1213 }
Dan Williams83de75c2008-06-28 08:31:58 +10001214 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001215
NeilBrown34a6f802015-08-14 12:07:57 +10001216 return_io(&return_bi);
Dan Williams91c00922007-01-02 13:52:30 -07001217
Dan Williamse4d84902007-09-24 10:06:13 -07001218 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001219 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001220}
1221
1222static void ops_run_biofill(struct stripe_head *sh)
1223{
1224 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001225 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001226 int i;
1227
shli@kernel.org59fc6302014-12-15 12:57:03 +11001228 BUG_ON(sh->batch_head);
Harvey Harrisone46b2722008-04-28 02:15:50 -07001229 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001230 (unsigned long long)sh->sector);
1231
1232 for (i = sh->disks; i--; ) {
1233 struct r5dev *dev = &sh->dev[i];
1234 if (test_bit(R5_Wantfill, &dev->flags)) {
1235 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001236 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001237 dev->read = rbi = dev->toread;
1238 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001239 spin_unlock_irq(&sh->stripe_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001240 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001241 dev->sector + STRIPE_SECTORS) {
Shaohua Lid592a992014-05-21 17:57:44 +08001242 tx = async_copy_data(0, rbi, &dev->page,
1243 dev->sector, tx, sh);
Dan Williams91c00922007-01-02 13:52:30 -07001244 rbi = r5_next_bio(rbi, dev->sector);
1245 }
1246 }
1247 }
1248
1249 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001250 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1251 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001252}
1253
Dan Williams4e7d2c02009-08-29 19:13:11 -07001254static void mark_target_uptodate(struct stripe_head *sh, int target)
1255{
1256 struct r5dev *tgt;
1257
1258 if (target < 0)
1259 return;
1260
1261 tgt = &sh->dev[target];
1262 set_bit(R5_UPTODATE, &tgt->flags);
1263 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1264 clear_bit(R5_Wantcompute, &tgt->flags);
1265}
1266
Dan Williamsac6b53b2009-07-14 13:40:19 -07001267static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001268{
1269 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001270
Harvey Harrisone46b2722008-04-28 02:15:50 -07001271 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001272 (unsigned long long)sh->sector);
1273
Dan Williamsac6b53b2009-07-14 13:40:19 -07001274 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001275 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001276 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001277
Dan Williamsecc65c92008-06-28 08:31:57 +10001278 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1279 if (sh->check_state == check_state_compute_run)
1280 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001281 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001282 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001283}
1284
Dan Williamsd6f38f32009-07-14 11:50:52 -07001285/* return a pointer to the address conversion region of the scribble buffer */
1286static addr_conv_t *to_addr_conv(struct stripe_head *sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001287 struct raid5_percpu *percpu, int i)
Dan Williams91c00922007-01-02 13:52:30 -07001288{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001289 void *addr;
1290
1291 addr = flex_array_get(percpu->scribble, i);
1292 return addr + sizeof(struct page *) * (sh->disks + 2);
1293}
1294
1295/* return a pointer to the address conversion region of the scribble buffer */
1296static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
1297{
1298 void *addr;
1299
1300 addr = flex_array_get(percpu->scribble, i);
1301 return addr;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001302}
1303
1304static struct dma_async_tx_descriptor *
1305ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1306{
Dan Williams91c00922007-01-02 13:52:30 -07001307 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001308 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001309 int target = sh->ops.target;
1310 struct r5dev *tgt = &sh->dev[target];
1311 struct page *xor_dest = tgt->page;
1312 int count = 0;
1313 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001314 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001315 int i;
1316
shli@kernel.org59fc6302014-12-15 12:57:03 +11001317 BUG_ON(sh->batch_head);
1318
Dan Williams91c00922007-01-02 13:52:30 -07001319 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07001320 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001321 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1322
1323 for (i = disks; i--; )
1324 if (i != target)
1325 xor_srcs[count++] = sh->dev[i].page;
1326
1327 atomic_inc(&sh->count);
1328
Dan Williams0403e382009-09-08 17:42:50 -07001329 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001330 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
Dan Williams91c00922007-01-02 13:52:30 -07001331 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -07001332 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001333 else
Dan Williamsa08abd82009-06-03 11:43:59 -07001334 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001335
Dan Williams91c00922007-01-02 13:52:30 -07001336 return tx;
1337}
1338
Dan Williamsac6b53b2009-07-14 13:40:19 -07001339/* set_syndrome_sources - populate source buffers for gen_syndrome
1340 * @srcs - (struct page *) array of size sh->disks
1341 * @sh - stripe_head to parse
1342 *
1343 * Populates srcs in proper layout order for the stripe and returns the
1344 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1345 * destination buffer is recorded in srcs[count] and the Q destination
1346 * is recorded in srcs[count+1]].
1347 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001348static int set_syndrome_sources(struct page **srcs,
1349 struct stripe_head *sh,
1350 int srctype)
Dan Williamsac6b53b2009-07-14 13:40:19 -07001351{
1352 int disks = sh->disks;
1353 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1354 int d0_idx = raid6_d0(sh);
1355 int count;
1356 int i;
1357
1358 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001359 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001360
1361 count = 0;
1362 i = d0_idx;
1363 do {
1364 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001365 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001366
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001367 if (i == sh->qd_idx || i == sh->pd_idx ||
1368 (srctype == SYNDROME_SRC_ALL) ||
1369 (srctype == SYNDROME_SRC_WANT_DRAIN &&
1370 test_bit(R5_Wantdrain, &dev->flags)) ||
1371 (srctype == SYNDROME_SRC_WRITTEN &&
1372 dev->written))
1373 srcs[slot] = sh->dev[i].page;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001374 i = raid6_next_disk(i, disks);
1375 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001376
NeilBrowne4424fe2009-10-16 16:27:34 +11001377 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001378}
1379
1380static struct dma_async_tx_descriptor *
1381ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1382{
1383 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001384 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001385 int target;
1386 int qd_idx = sh->qd_idx;
1387 struct dma_async_tx_descriptor *tx;
1388 struct async_submit_ctl submit;
1389 struct r5dev *tgt;
1390 struct page *dest;
1391 int i;
1392 int count;
1393
shli@kernel.org59fc6302014-12-15 12:57:03 +11001394 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001395 if (sh->ops.target < 0)
1396 target = sh->ops.target2;
1397 else if (sh->ops.target2 < 0)
1398 target = sh->ops.target;
1399 else
1400 /* we should only have one valid target */
1401 BUG();
1402 BUG_ON(target < 0);
1403 pr_debug("%s: stripe %llu block: %d\n",
1404 __func__, (unsigned long long)sh->sector, target);
1405
1406 tgt = &sh->dev[target];
1407 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1408 dest = tgt->page;
1409
1410 atomic_inc(&sh->count);
1411
1412 if (target == qd_idx) {
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001413 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001414 blocks[count] = NULL; /* regenerating p is not necessary */
1415 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001416 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1417 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001418 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001419 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1420 } else {
1421 /* Compute any data- or p-drive using XOR */
1422 count = 0;
1423 for (i = disks; i-- ; ) {
1424 if (i == target || i == qd_idx)
1425 continue;
1426 blocks[count++] = sh->dev[i].page;
1427 }
1428
Dan Williams0403e382009-09-08 17:42:50 -07001429 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1430 NULL, ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001431 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001432 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1433 }
1434
1435 return tx;
1436}
1437
1438static struct dma_async_tx_descriptor *
1439ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1440{
1441 int i, count, disks = sh->disks;
1442 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1443 int d0_idx = raid6_d0(sh);
1444 int faila = -1, failb = -1;
1445 int target = sh->ops.target;
1446 int target2 = sh->ops.target2;
1447 struct r5dev *tgt = &sh->dev[target];
1448 struct r5dev *tgt2 = &sh->dev[target2];
1449 struct dma_async_tx_descriptor *tx;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001450 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001451 struct async_submit_ctl submit;
1452
shli@kernel.org59fc6302014-12-15 12:57:03 +11001453 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001454 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1455 __func__, (unsigned long long)sh->sector, target, target2);
1456 BUG_ON(target < 0 || target2 < 0);
1457 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1458 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1459
Dan Williams6c910a72009-09-16 12:24:54 -07001460 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001461 * slot number conversion for 'faila' and 'failb'
1462 */
1463 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001464 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001465 count = 0;
1466 i = d0_idx;
1467 do {
1468 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1469
1470 blocks[slot] = sh->dev[i].page;
1471
1472 if (i == target)
1473 faila = slot;
1474 if (i == target2)
1475 failb = slot;
1476 i = raid6_next_disk(i, disks);
1477 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001478
1479 BUG_ON(faila == failb);
1480 if (failb < faila)
1481 swap(faila, failb);
1482 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1483 __func__, (unsigned long long)sh->sector, faila, failb);
1484
1485 atomic_inc(&sh->count);
1486
1487 if (failb == syndrome_disks+1) {
1488 /* Q disk is one of the missing disks */
1489 if (faila == syndrome_disks) {
1490 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001491 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1492 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001493 to_addr_conv(sh, percpu, 0));
NeilBrowne4424fe2009-10-16 16:27:34 +11001494 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001495 STRIPE_SIZE, &submit);
1496 } else {
1497 struct page *dest;
1498 int data_target;
1499 int qd_idx = sh->qd_idx;
1500
1501 /* Missing D+Q: recompute D from P, then recompute Q */
1502 if (target == qd_idx)
1503 data_target = target2;
1504 else
1505 data_target = target;
1506
1507 count = 0;
1508 for (i = disks; i-- ; ) {
1509 if (i == data_target || i == qd_idx)
1510 continue;
1511 blocks[count++] = sh->dev[i].page;
1512 }
1513 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001514 init_async_submit(&submit,
1515 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1516 NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001517 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001518 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1519 &submit);
1520
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001521 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williams0403e382009-09-08 17:42:50 -07001522 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1523 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001524 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001525 return async_gen_syndrome(blocks, 0, count+2,
1526 STRIPE_SIZE, &submit);
1527 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001528 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001529 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1530 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001531 to_addr_conv(sh, percpu, 0));
Dan Williams6c910a72009-09-16 12:24:54 -07001532 if (failb == syndrome_disks) {
1533 /* We're missing D+P. */
1534 return async_raid6_datap_recov(syndrome_disks+2,
1535 STRIPE_SIZE, faila,
1536 blocks, &submit);
1537 } else {
1538 /* We're missing D+D. */
1539 return async_raid6_2data_recov(syndrome_disks+2,
1540 STRIPE_SIZE, faila, failb,
1541 blocks, &submit);
1542 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001543 }
1544}
1545
Dan Williams91c00922007-01-02 13:52:30 -07001546static void ops_complete_prexor(void *stripe_head_ref)
1547{
1548 struct stripe_head *sh = stripe_head_ref;
1549
Harvey Harrisone46b2722008-04-28 02:15:50 -07001550 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001551 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001552}
1553
1554static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001555ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1556 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001557{
Dan Williams91c00922007-01-02 13:52:30 -07001558 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001559 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001560 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001561 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001562
1563 /* existing parity data subtracted */
1564 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1565
shli@kernel.org59fc6302014-12-15 12:57:03 +11001566 BUG_ON(sh->batch_head);
Harvey Harrisone46b2722008-04-28 02:15:50 -07001567 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001568 (unsigned long long)sh->sector);
1569
1570 for (i = disks; i--; ) {
1571 struct r5dev *dev = &sh->dev[i];
1572 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001573 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001574 xor_srcs[count++] = dev->page;
1575 }
1576
Dan Williams0403e382009-09-08 17:42:50 -07001577 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001578 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Dan Williamsa08abd82009-06-03 11:43:59 -07001579 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001580
1581 return tx;
1582}
1583
1584static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001585ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1586 struct dma_async_tx_descriptor *tx)
1587{
1588 struct page **blocks = to_addr_page(percpu, 0);
1589 int count;
1590 struct async_submit_ctl submit;
1591
1592 pr_debug("%s: stripe %llu\n", __func__,
1593 (unsigned long long)sh->sector);
1594
1595 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN);
1596
1597 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1598 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1599 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1600
1601 return tx;
1602}
1603
1604static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001605ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001606{
1607 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001608 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001609 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -07001610
Harvey Harrisone46b2722008-04-28 02:15:50 -07001611 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001612 (unsigned long long)sh->sector);
1613
1614 for (i = disks; i--; ) {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001615 struct r5dev *dev;
Dan Williams91c00922007-01-02 13:52:30 -07001616 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001617
shli@kernel.org59fc6302014-12-15 12:57:03 +11001618 sh = head_sh;
1619 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001620 struct bio *wbi;
1621
shli@kernel.org59fc6302014-12-15 12:57:03 +11001622again:
1623 dev = &sh->dev[i];
Shaohua Lib17459c2012-07-19 16:01:31 +10001624 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001625 chosen = dev->towrite;
1626 dev->towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11001627 sh->overwrite_disks = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001628 BUG_ON(dev->written);
1629 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001630 spin_unlock_irq(&sh->stripe_lock);
Shaohua Lid592a992014-05-21 17:57:44 +08001631 WARN_ON(dev->page != dev->orig_page);
Dan Williams91c00922007-01-02 13:52:30 -07001632
Kent Overstreet4f024f32013-10-11 15:44:27 -07001633 while (wbi && wbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001634 dev->sector + STRIPE_SECTORS) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001635 if (wbi->bi_opf & REQ_FUA)
Tejun Heoe9c74692010-09-03 11:56:18 +02001636 set_bit(R5_WantFUA, &dev->flags);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001637 if (wbi->bi_opf & REQ_SYNC)
Shaohua Libc0934f2012-05-22 13:55:05 +10001638 set_bit(R5_SyncIO, &dev->flags);
Mike Christie796a5cf2016-06-05 14:32:07 -05001639 if (bio_op(wbi) == REQ_OP_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001640 set_bit(R5_Discard, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08001641 else {
1642 tx = async_copy_data(1, wbi, &dev->page,
1643 dev->sector, tx, sh);
1644 if (dev->page != dev->orig_page) {
1645 set_bit(R5_SkipCopy, &dev->flags);
1646 clear_bit(R5_UPTODATE, &dev->flags);
1647 clear_bit(R5_OVERWRITE, &dev->flags);
1648 }
1649 }
Dan Williams91c00922007-01-02 13:52:30 -07001650 wbi = r5_next_bio(wbi, dev->sector);
1651 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001652
1653 if (head_sh->batch_head) {
1654 sh = list_first_entry(&sh->batch_list,
1655 struct stripe_head,
1656 batch_list);
1657 if (sh == head_sh)
1658 continue;
1659 goto again;
1660 }
Dan Williams91c00922007-01-02 13:52:30 -07001661 }
1662 }
1663
1664 return tx;
1665}
1666
Dan Williamsac6b53b2009-07-14 13:40:19 -07001667static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001668{
1669 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001670 int disks = sh->disks;
1671 int pd_idx = sh->pd_idx;
1672 int qd_idx = sh->qd_idx;
1673 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001674 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001675
Harvey Harrisone46b2722008-04-28 02:15:50 -07001676 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001677 (unsigned long long)sh->sector);
1678
Shaohua Libc0934f2012-05-22 13:55:05 +10001679 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001680 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001681 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001682 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001683 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001684
Dan Williams91c00922007-01-02 13:52:30 -07001685 for (i = disks; i--; ) {
1686 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001687
Tejun Heoe9c74692010-09-03 11:56:18 +02001688 if (dev->written || i == pd_idx || i == qd_idx) {
NeilBrownf39486b2017-10-17 16:18:36 +11001689 if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001690 set_bit(R5_UPTODATE, &dev->flags);
NeilBrownf39486b2017-10-17 16:18:36 +11001691 if (test_bit(STRIPE_EXPAND_READY, &sh->state))
1692 set_bit(R5_Expanded, &dev->flags);
1693 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001694 if (fua)
1695 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001696 if (sync)
1697 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001698 }
Dan Williams91c00922007-01-02 13:52:30 -07001699 }
1700
Dan Williamsd8ee0722008-06-28 08:32:06 +10001701 if (sh->reconstruct_state == reconstruct_state_drain_run)
1702 sh->reconstruct_state = reconstruct_state_drain_result;
1703 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1704 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1705 else {
1706 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1707 sh->reconstruct_state = reconstruct_state_result;
1708 }
Dan Williams91c00922007-01-02 13:52:30 -07001709
1710 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001711 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001712}
1713
1714static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001715ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1716 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001717{
Dan Williams91c00922007-01-02 13:52:30 -07001718 int disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001719 struct page **xor_srcs;
Dan Williamsa08abd82009-06-03 11:43:59 -07001720 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001721 int count, pd_idx = sh->pd_idx, i;
Dan Williams91c00922007-01-02 13:52:30 -07001722 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001723 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001724 unsigned long flags;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001725 int j = 0;
1726 struct stripe_head *head_sh = sh;
1727 int last_stripe;
Dan Williams91c00922007-01-02 13:52:30 -07001728
Harvey Harrisone46b2722008-04-28 02:15:50 -07001729 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001730 (unsigned long long)sh->sector);
1731
Shaohua Li620125f2012-10-11 13:49:05 +11001732 for (i = 0; i < sh->disks; i++) {
1733 if (pd_idx == i)
1734 continue;
1735 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1736 break;
1737 }
1738 if (i >= sh->disks) {
1739 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001740 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1741 ops_complete_reconstruct(sh);
1742 return;
1743 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001744again:
1745 count = 0;
1746 xor_srcs = to_addr_page(percpu, j);
Dan Williams91c00922007-01-02 13:52:30 -07001747 /* check if prexor is active which means only process blocks
1748 * that are part of a read-modify-write (written)
1749 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001750 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001751 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001752 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1753 for (i = disks; i--; ) {
1754 struct r5dev *dev = &sh->dev[i];
shli@kernel.org59fc6302014-12-15 12:57:03 +11001755 if (head_sh->dev[i].written)
Dan Williams91c00922007-01-02 13:52:30 -07001756 xor_srcs[count++] = dev->page;
1757 }
1758 } else {
1759 xor_dest = sh->dev[pd_idx].page;
1760 for (i = disks; i--; ) {
1761 struct r5dev *dev = &sh->dev[i];
1762 if (i != pd_idx)
1763 xor_srcs[count++] = dev->page;
1764 }
1765 }
1766
Dan Williams91c00922007-01-02 13:52:30 -07001767 /* 1/ if we prexor'd then the dest is reused as a source
1768 * 2/ if we did not prexor then we are redoing the parity
1769 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1770 * for the synchronous xor case
1771 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001772 last_stripe = !head_sh->batch_head ||
1773 list_first_entry(&sh->batch_list,
1774 struct stripe_head, batch_list) == head_sh;
1775 if (last_stripe) {
1776 flags = ASYNC_TX_ACK |
1777 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
Dan Williams91c00922007-01-02 13:52:30 -07001778
shli@kernel.org59fc6302014-12-15 12:57:03 +11001779 atomic_inc(&head_sh->count);
1780 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
1781 to_addr_conv(sh, percpu, j));
1782 } else {
1783 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
1784 init_async_submit(&submit, flags, tx, NULL, NULL,
1785 to_addr_conv(sh, percpu, j));
1786 }
Dan Williams91c00922007-01-02 13:52:30 -07001787
Dan Williamsa08abd82009-06-03 11:43:59 -07001788 if (unlikely(count == 1))
1789 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1790 else
1791 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001792 if (!last_stripe) {
1793 j++;
1794 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1795 batch_list);
1796 goto again;
1797 }
Dan Williams91c00922007-01-02 13:52:30 -07001798}
1799
Dan Williamsac6b53b2009-07-14 13:40:19 -07001800static void
1801ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1802 struct dma_async_tx_descriptor *tx)
1803{
1804 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001805 struct page **blocks;
1806 int count, i, j = 0;
1807 struct stripe_head *head_sh = sh;
1808 int last_stripe;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001809 int synflags;
1810 unsigned long txflags;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001811
1812 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1813
Shaohua Li620125f2012-10-11 13:49:05 +11001814 for (i = 0; i < sh->disks; i++) {
1815 if (sh->pd_idx == i || sh->qd_idx == i)
1816 continue;
1817 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1818 break;
1819 }
1820 if (i >= sh->disks) {
1821 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001822 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1823 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1824 ops_complete_reconstruct(sh);
1825 return;
1826 }
1827
shli@kernel.org59fc6302014-12-15 12:57:03 +11001828again:
1829 blocks = to_addr_page(percpu, j);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001830
1831 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1832 synflags = SYNDROME_SRC_WRITTEN;
1833 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
1834 } else {
1835 synflags = SYNDROME_SRC_ALL;
1836 txflags = ASYNC_TX_ACK;
1837 }
1838
1839 count = set_syndrome_sources(blocks, sh, synflags);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001840 last_stripe = !head_sh->batch_head ||
1841 list_first_entry(&sh->batch_list,
1842 struct stripe_head, batch_list) == head_sh;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001843
shli@kernel.org59fc6302014-12-15 12:57:03 +11001844 if (last_stripe) {
1845 atomic_inc(&head_sh->count);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001846 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
shli@kernel.org59fc6302014-12-15 12:57:03 +11001847 head_sh, to_addr_conv(sh, percpu, j));
1848 } else
1849 init_async_submit(&submit, 0, tx, NULL, NULL,
1850 to_addr_conv(sh, percpu, j));
Shaohua Li48769692015-05-13 09:30:08 -07001851 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001852 if (!last_stripe) {
1853 j++;
1854 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1855 batch_list);
1856 goto again;
1857 }
Dan Williams91c00922007-01-02 13:52:30 -07001858}
1859
1860static void ops_complete_check(void *stripe_head_ref)
1861{
1862 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001863
Harvey Harrisone46b2722008-04-28 02:15:50 -07001864 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001865 (unsigned long long)sh->sector);
1866
Dan Williamsecc65c92008-06-28 08:31:57 +10001867 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001868 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001869 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001870}
1871
Dan Williamsac6b53b2009-07-14 13:40:19 -07001872static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001873{
Dan Williams91c00922007-01-02 13:52:30 -07001874 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001875 int pd_idx = sh->pd_idx;
1876 int qd_idx = sh->qd_idx;
1877 struct page *xor_dest;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001878 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001879 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001880 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001881 int count;
1882 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001883
Harvey Harrisone46b2722008-04-28 02:15:50 -07001884 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001885 (unsigned long long)sh->sector);
1886
shli@kernel.org59fc6302014-12-15 12:57:03 +11001887 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001888 count = 0;
1889 xor_dest = sh->dev[pd_idx].page;
1890 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001891 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001892 if (i == pd_idx || i == qd_idx)
1893 continue;
1894 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001895 }
1896
Dan Williamsd6f38f32009-07-14 11:50:52 -07001897 init_async_submit(&submit, 0, NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001898 to_addr_conv(sh, percpu, 0));
Dan Williams099f53c2009-04-08 14:28:37 -07001899 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001900 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001901
Dan Williams91c00922007-01-02 13:52:30 -07001902 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001903 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1904 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001905}
1906
Dan Williamsac6b53b2009-07-14 13:40:19 -07001907static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1908{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001909 struct page **srcs = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001910 struct async_submit_ctl submit;
1911 int count;
1912
1913 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1914 (unsigned long long)sh->sector, checkp);
1915
shli@kernel.org59fc6302014-12-15 12:57:03 +11001916 BUG_ON(sh->batch_head);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001917 count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001918 if (!checkp)
1919 srcs[count] = NULL;
1920
1921 atomic_inc(&sh->count);
1922 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001923 sh, to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001924 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1925 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1926}
1927
NeilBrown51acbce2013-02-28 09:08:34 +11001928static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001929{
1930 int overlap_clear = 0, i, disks = sh->disks;
1931 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001932 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001933 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001934 struct raid5_percpu *percpu;
1935 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001936
Dan Williamsd6f38f32009-07-14 11:50:52 -07001937 cpu = get_cpu();
1938 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001939 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001940 ops_run_biofill(sh);
1941 overlap_clear++;
1942 }
1943
Dan Williams7b3a8712008-06-28 08:32:09 +10001944 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001945 if (level < 6)
1946 tx = ops_run_compute5(sh, percpu);
1947 else {
1948 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1949 tx = ops_run_compute6_1(sh, percpu);
1950 else
1951 tx = ops_run_compute6_2(sh, percpu);
1952 }
1953 /* terminate the chain if reconstruct is not set to be run */
1954 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001955 async_tx_ack(tx);
1956 }
Dan Williams91c00922007-01-02 13:52:30 -07001957
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001958 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
1959 if (level < 6)
1960 tx = ops_run_prexor5(sh, percpu, tx);
1961 else
1962 tx = ops_run_prexor6(sh, percpu, tx);
1963 }
Dan Williams91c00922007-01-02 13:52:30 -07001964
Dan Williams600aa102008-06-28 08:32:05 +10001965 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001966 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001967 overlap_clear++;
1968 }
1969
Dan Williamsac6b53b2009-07-14 13:40:19 -07001970 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1971 if (level < 6)
1972 ops_run_reconstruct5(sh, percpu, tx);
1973 else
1974 ops_run_reconstruct6(sh, percpu, tx);
1975 }
Dan Williams91c00922007-01-02 13:52:30 -07001976
Dan Williamsac6b53b2009-07-14 13:40:19 -07001977 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1978 if (sh->check_state == check_state_run)
1979 ops_run_check_p(sh, percpu);
1980 else if (sh->check_state == check_state_run_q)
1981 ops_run_check_pq(sh, percpu, 0);
1982 else if (sh->check_state == check_state_run_pq)
1983 ops_run_check_pq(sh, percpu, 1);
1984 else
1985 BUG();
1986 }
Dan Williams91c00922007-01-02 13:52:30 -07001987
shli@kernel.org59fc6302014-12-15 12:57:03 +11001988 if (overlap_clear && !sh->batch_head)
Dan Williams91c00922007-01-02 13:52:30 -07001989 for (i = disks; i--; ) {
1990 struct r5dev *dev = &sh->dev[i];
1991 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1992 wake_up(&sh->raid_conf->wait_for_overlap);
1993 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001994 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001995}
1996
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07001997static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
1998 int disks)
NeilBrownf18c1a32015-05-08 18:19:04 +10001999{
2000 struct stripe_head *sh;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002001 int i;
NeilBrownf18c1a32015-05-08 18:19:04 +10002002
2003 sh = kmem_cache_zalloc(sc, gfp);
2004 if (sh) {
2005 spin_lock_init(&sh->stripe_lock);
2006 spin_lock_init(&sh->batch_lock);
2007 INIT_LIST_HEAD(&sh->batch_list);
2008 INIT_LIST_HEAD(&sh->lru);
2009 atomic_set(&sh->count, 1);
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002010 for (i = 0; i < disks; i++) {
2011 struct r5dev *dev = &sh->dev[i];
2012
2013 bio_init(&dev->req);
Shaohua Li45c91d82016-08-22 21:14:02 -07002014 dev->req.bi_io_vec = &dev->vec;
2015 dev->req.bi_max_vecs = 1;
2016
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002017 bio_init(&dev->rreq);
Shaohua Li45c91d82016-08-22 21:14:02 -07002018 dev->rreq.bi_io_vec = &dev->rvec;
2019 dev->rreq.bi_max_vecs = 1;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002020 }
NeilBrownf18c1a32015-05-08 18:19:04 +10002021 }
2022 return sh;
2023}
NeilBrown486f0642015-02-25 12:10:35 +11002024static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
2026 struct stripe_head *sh;
NeilBrownf18c1a32015-05-08 18:19:04 +10002027
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002028 sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size);
NeilBrown3f294f42005-11-08 21:39:25 -08002029 if (!sh)
2030 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10002031
NeilBrown3f294f42005-11-08 21:39:25 -08002032 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08002033
NeilBrowna9683a72015-02-25 12:02:51 +11002034 if (grow_buffers(sh, gfp)) {
NeilBrowne4e11e32010-06-16 16:45:16 +10002035 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002036 kmem_cache_free(conf->slab_cache, sh);
2037 return 0;
2038 }
NeilBrown486f0642015-02-25 12:10:35 +11002039 sh->hash_lock_index =
2040 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrown3f294f42005-11-08 21:39:25 -08002041 /* we just created an active stripe so... */
NeilBrown3f294f42005-11-08 21:39:25 -08002042 atomic_inc(&conf->active_stripes);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002043
Shaohua Li6d036f72015-08-13 14:31:57 -07002044 raid5_release_stripe(sh);
NeilBrown486f0642015-02-25 12:10:35 +11002045 conf->max_nr_stripes++;
NeilBrown3f294f42005-11-08 21:39:25 -08002046 return 1;
2047}
2048
NeilBrownd1688a62011-10-11 16:49:52 +11002049static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08002050{
Christoph Lametere18b8902006-12-06 20:33:20 -08002051 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11002052 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
NeilBrownf4be6b42010-06-01 19:37:25 +10002054 if (conf->mddev->gendisk)
2055 sprintf(conf->cache_name[0],
2056 "raid%d-%s", conf->level, mdname(conf->mddev));
2057 else
2058 sprintf(conf->cache_name[0],
2059 "raid%d-%p", conf->level, conf->mddev);
2060 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
2061
NeilBrownad01c9e2006-03-27 01:18:07 -08002062 conf->active_name = 0;
2063 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002065 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (!sc)
2067 return 1;
2068 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002069 conf->pool_size = devs;
NeilBrown486f0642015-02-25 12:10:35 +11002070 while (num--)
2071 if (!grow_one_stripe(conf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return 1;
NeilBrown486f0642015-02-25 12:10:35 +11002073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return 0;
2075}
NeilBrown29269552006-03-27 01:18:10 -08002076
Dan Williamsd6f38f32009-07-14 11:50:52 -07002077/**
2078 * scribble_len - return the required size of the scribble region
2079 * @num - total number of disks in the array
2080 *
2081 * The size must be enough to contain:
2082 * 1/ a struct page pointer for each device in the array +2
2083 * 2/ room to convert each entry in (1) to its corresponding dma
2084 * (dma_map_page()) or page (page_address()) address.
2085 *
2086 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2087 * calculate over all devices (not just the data blocks), using zeros in place
2088 * of the P and Q blocks.
2089 */
shli@kernel.org46d5b782014-12-15 12:57:02 +11002090static struct flex_array *scribble_alloc(int num, int cnt, gfp_t flags)
Dan Williamsd6f38f32009-07-14 11:50:52 -07002091{
shli@kernel.org46d5b782014-12-15 12:57:02 +11002092 struct flex_array *ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002093 size_t len;
2094
2095 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
shli@kernel.org46d5b782014-12-15 12:57:02 +11002096 ret = flex_array_alloc(len, cnt, flags);
2097 if (!ret)
2098 return NULL;
2099 /* always prealloc all elements, so no locking is required */
2100 if (flex_array_prealloc(ret, 0, cnt, flags)) {
2101 flex_array_free(ret);
2102 return NULL;
2103 }
2104 return ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002105}
2106
NeilBrown738a2732015-05-08 18:19:39 +10002107static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2108{
2109 unsigned long cpu;
2110 int err = 0;
2111
Shaohua Li27a353c2016-02-24 17:38:28 -08002112 /*
2113 * Never shrink. And mddev_suspend() could deadlock if this is called
2114 * from raid5d. In that case, scribble_disks and scribble_sectors
2115 * should equal to new_disks and new_sectors
2116 */
2117 if (conf->scribble_disks >= new_disks &&
2118 conf->scribble_sectors >= new_sectors)
2119 return 0;
NeilBrown738a2732015-05-08 18:19:39 +10002120 mddev_suspend(conf->mddev);
2121 get_online_cpus();
2122 for_each_present_cpu(cpu) {
2123 struct raid5_percpu *percpu;
2124 struct flex_array *scribble;
2125
2126 percpu = per_cpu_ptr(conf->percpu, cpu);
2127 scribble = scribble_alloc(new_disks,
2128 new_sectors / STRIPE_SECTORS,
2129 GFP_NOIO);
2130
2131 if (scribble) {
2132 flex_array_free(percpu->scribble);
2133 percpu->scribble = scribble;
2134 } else {
2135 err = -ENOMEM;
2136 break;
2137 }
2138 }
2139 put_online_cpus();
2140 mddev_resume(conf->mddev);
Shaohua Li27a353c2016-02-24 17:38:28 -08002141 if (!err) {
2142 conf->scribble_disks = new_disks;
2143 conf->scribble_sectors = new_sectors;
2144 }
NeilBrown738a2732015-05-08 18:19:39 +10002145 return err;
2146}
2147
NeilBrownd1688a62011-10-11 16:49:52 +11002148static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08002149{
2150 /* Make all the stripes able to hold 'newsize' devices.
2151 * New slots in each stripe get 'page' set to a new page.
2152 *
2153 * This happens in stages:
2154 * 1/ create a new kmem_cache and allocate the required number of
2155 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09002156 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08002157 * to the new stripe_heads. This will have the side effect of
2158 * freezing the array as once all stripe_heads have been collected,
2159 * no IO will be possible. Old stripe heads are freed once their
2160 * pages have been transferred over, and the old kmem_cache is
2161 * freed when all stripes are done.
2162 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
2163 * we simple return a failre status - no need to clean anything up.
2164 * 4/ allocate new pages for the new slots in the new stripe_heads.
2165 * If this fails, we don't bother trying the shrink the
2166 * stripe_heads down again, we just leave them as they are.
2167 * As each stripe_head is processed the new one is released into
2168 * active service.
2169 *
2170 * Once step2 is started, we cannot afford to wait for a write,
2171 * so we use GFP_NOIO allocations.
2172 */
2173 struct stripe_head *osh, *nsh;
2174 LIST_HEAD(newstripes);
2175 struct disk_info *ndisks;
Dan Williamsb5470dc2008-06-27 21:44:04 -07002176 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08002177 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002178 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11002179 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08002180
2181 if (newsize <= conf->pool_size)
2182 return 0; /* never bother to shrink */
2183
Dan Williamsb5470dc2008-06-27 21:44:04 -07002184 err = md_allow_write(conf->mddev);
2185 if (err)
2186 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08002187
NeilBrownad01c9e2006-03-27 01:18:07 -08002188 /* Step 1 */
2189 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2190 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002191 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08002192 if (!sc)
2193 return -ENOMEM;
2194
NeilBrown2d5b5692015-07-06 12:49:23 +10002195 /* Need to ensure auto-resizing doesn't interfere */
2196 mutex_lock(&conf->cache_size_mutex);
2197
NeilBrownad01c9e2006-03-27 01:18:07 -08002198 for (i = conf->max_nr_stripes; i; i--) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002199 nsh = alloc_stripe(sc, GFP_KERNEL, newsize);
NeilBrownad01c9e2006-03-27 01:18:07 -08002200 if (!nsh)
2201 break;
2202
NeilBrownad01c9e2006-03-27 01:18:07 -08002203 nsh->raid_conf = conf;
NeilBrownad01c9e2006-03-27 01:18:07 -08002204 list_add(&nsh->lru, &newstripes);
2205 }
2206 if (i) {
2207 /* didn't get enough, give up */
2208 while (!list_empty(&newstripes)) {
2209 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2210 list_del(&nsh->lru);
2211 kmem_cache_free(sc, nsh);
2212 }
2213 kmem_cache_destroy(sc);
NeilBrown2d5b5692015-07-06 12:49:23 +10002214 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002215 return -ENOMEM;
2216 }
2217 /* Step 2 - Must use GFP_NOIO now.
2218 * OK, we have enough stripes, start collecting inactive
2219 * stripes and copying them over
2220 */
Shaohua Li566c09c2013-11-14 15:16:17 +11002221 hash = 0;
2222 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08002223 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11002224 lock_device_hash_lock(conf, hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08002225 wait_event_cmd(conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +11002226 !list_empty(conf->inactive_list + hash),
2227 unlock_device_hash_lock(conf, hash),
2228 lock_device_hash_lock(conf, hash));
2229 osh = get_free_stripe(conf, hash);
2230 unlock_device_hash_lock(conf, hash);
NeilBrownf18c1a32015-05-08 18:19:04 +10002231
Shaohua Lid592a992014-05-21 17:57:44 +08002232 for(i=0; i<conf->pool_size; i++) {
NeilBrownad01c9e2006-03-27 01:18:07 -08002233 nsh->dev[i].page = osh->dev[i].page;
Shaohua Lid592a992014-05-21 17:57:44 +08002234 nsh->dev[i].orig_page = osh->dev[i].page;
2235 }
Shaohua Li566c09c2013-11-14 15:16:17 +11002236 nsh->hash_lock_index = hash;
NeilBrownad01c9e2006-03-27 01:18:07 -08002237 kmem_cache_free(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11002238 cnt++;
2239 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2240 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2241 hash++;
2242 cnt = 0;
2243 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002244 }
2245 kmem_cache_destroy(conf->slab_cache);
2246
2247 /* Step 3.
2248 * At this point, we are holding all the stripes so the array
2249 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07002250 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08002251 */
2252 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
2253 if (ndisks) {
2254 for (i=0; i<conf->raid_disks; i++)
2255 ndisks[i] = conf->disks[i];
2256 kfree(conf->disks);
2257 conf->disks = ndisks;
2258 } else
2259 err = -ENOMEM;
2260
NeilBrown2d5b5692015-07-06 12:49:23 +10002261 mutex_unlock(&conf->cache_size_mutex);
Dennis Yangfa9a4a92017-03-29 15:46:13 +08002262
2263 conf->slab_cache = sc;
2264 conf->active_name = 1-conf->active_name;
2265
NeilBrownad01c9e2006-03-27 01:18:07 -08002266 /* Step 4, return new stripes to service */
2267 while(!list_empty(&newstripes)) {
2268 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2269 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07002270
NeilBrownad01c9e2006-03-27 01:18:07 -08002271 for (i=conf->raid_disks; i < newsize; i++)
2272 if (nsh->dev[i].page == NULL) {
2273 struct page *p = alloc_page(GFP_NOIO);
2274 nsh->dev[i].page = p;
Shaohua Lid592a992014-05-21 17:57:44 +08002275 nsh->dev[i].orig_page = p;
NeilBrownad01c9e2006-03-27 01:18:07 -08002276 if (!p)
2277 err = -ENOMEM;
2278 }
Shaohua Li6d036f72015-08-13 14:31:57 -07002279 raid5_release_stripe(nsh);
NeilBrownad01c9e2006-03-27 01:18:07 -08002280 }
2281 /* critical section pass, GFP_NOIO no longer needed */
2282
NeilBrown6e9eac22015-05-08 18:19:34 +10002283 if (!err)
2284 conf->pool_size = newsize;
NeilBrownad01c9e2006-03-27 01:18:07 -08002285 return err;
2286}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
NeilBrown486f0642015-02-25 12:10:35 +11002288static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
2290 struct stripe_head *sh;
NeilBrown49895bc2015-08-03 17:09:57 +10002291 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Shaohua Li566c09c2013-11-14 15:16:17 +11002293 spin_lock_irq(conf->hash_locks + hash);
2294 sh = get_free_stripe(conf, hash);
2295 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08002296 if (!sh)
2297 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002298 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10002299 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002300 kmem_cache_free(conf->slab_cache, sh);
2301 atomic_dec(&conf->active_stripes);
NeilBrown486f0642015-02-25 12:10:35 +11002302 conf->max_nr_stripes--;
NeilBrown3f294f42005-11-08 21:39:25 -08002303 return 1;
2304}
2305
NeilBrownd1688a62011-10-11 16:49:52 +11002306static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08002307{
NeilBrown486f0642015-02-25 12:10:35 +11002308 while (conf->max_nr_stripes &&
2309 drop_one_stripe(conf))
2310 ;
NeilBrown3f294f42005-11-08 21:39:25 -08002311
Julia Lawall644df1a2015-09-13 14:15:10 +02002312 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 conf->slab_cache = NULL;
2314}
2315
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002316static void raid5_end_read_request(struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
NeilBrown99c0fb52009-03-31 14:39:38 +11002318 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002319 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002320 int disks = sh->disks, i;
NeilBrownd6950432006-07-10 04:44:20 -07002321 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11002322 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10002323 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 for (i=0 ; i<disks; i++)
2326 if (bi == &sh->dev[i].req)
2327 break;
2328
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002329 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
Dan Williams45b42332007-07-09 11:56:43 -07002330 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002331 bi->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002333 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002335 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
NeilBrown14a75d32011-12-23 10:17:52 +11002337 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11002338 /* If replacement finished while this request was outstanding,
2339 * 'replacement' might be NULL already.
2340 * In that case it moved down to 'rdev'.
2341 * rdev is not removed until all requests are finished.
2342 */
NeilBrown14a75d32011-12-23 10:17:52 +11002343 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002344 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11002345 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
NeilBrown05616be2012-05-21 09:27:00 +10002347 if (use_new_offset(conf, sh))
2348 s = sh->sector + rdev->new_data_offset;
2349 else
2350 s = sh->sector + rdev->data_offset;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002351 if (!bi->bi_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08002353 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11002354 /* Note that this cannot happen on a
2355 * replacement device. We just fail those on
2356 * any error
2357 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10002358 printk_ratelimited(
2359 KERN_INFO
2360 "md/raid:%s: read error corrected"
2361 " (%lu sectors at %llu on %s)\n",
2362 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10002363 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002364 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10002365 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08002366 clear_bit(R5_ReadError, &sh->dev[i].flags);
2367 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002368 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2369 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2370
NeilBrown14a75d32011-12-23 10:17:52 +11002371 if (atomic_read(&rdev->read_errors))
2372 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002374 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002375 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002376 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07002379 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002380 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
2381 printk_ratelimited(
2382 KERN_WARNING
2383 "md/raid:%s: read error on replacement device "
2384 "(sector %llu on %s).\n",
2385 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002386 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002387 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002388 else if (conf->mddev->degraded >= conf->max_degraded) {
2389 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10002390 printk_ratelimited(
2391 KERN_WARNING
2392 "md/raid:%s: read error not correctable "
2393 "(sector %llu on %s).\n",
2394 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002395 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002396 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002397 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002398 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002399 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10002400 printk_ratelimited(
2401 KERN_WARNING
2402 "md/raid:%s: read error NOT corrected!! "
2403 "(sector %llu on %s).\n",
2404 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002405 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002406 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002407 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08002408 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08002409 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10002410 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07002411 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08002412 else
2413 retry = 1;
Bian Yuedfa1f62013-11-14 15:16:17 +11002414 if (set_bad && test_bit(In_sync, &rdev->flags)
2415 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2416 retry = 1;
NeilBrownba22dcb2005-11-08 21:39:31 -08002417 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10002418 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2419 set_bit(R5_ReadError, &sh->dev[i].flags);
2420 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2421 } else
2422 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002423 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002424 clear_bit(R5_ReadError, &sh->dev[i].flags);
2425 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002426 if (!(set_bad
2427 && test_bit(In_sync, &rdev->flags)
2428 && rdev_set_badblocks(
2429 rdev, sh->sector, STRIPE_SECTORS, 0)))
2430 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 }
NeilBrown14a75d32011-12-23 10:17:52 +11002433 rdev_dec_pending(rdev, conf->mddev);
Shaohua Lic9445552016-09-08 10:43:58 -07002434 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2436 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002437 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438}
2439
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002440static void raid5_end_write_request(struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
NeilBrown99c0fb52009-03-31 14:39:38 +11002442 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002443 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002444 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11002445 struct md_rdev *uninitialized_var(rdev);
NeilBrownb84db562011-07-28 11:39:23 +10002446 sector_t first_bad;
2447 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002448 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
NeilBrown977df362011-12-23 10:17:53 +11002450 for (i = 0 ; i < disks; i++) {
2451 if (bi == &sh->dev[i].req) {
2452 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 break;
NeilBrown977df362011-12-23 10:17:53 +11002454 }
2455 if (bi == &sh->dev[i].rreq) {
2456 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002457 if (rdev)
2458 replacement = 1;
2459 else
2460 /* rdev was removed and 'replacement'
2461 * replaced it. rdev is not removed
2462 * until all requests are finished.
2463 */
2464 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002465 break;
2466 }
2467 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002468 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002470 bi->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002472 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002474 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 }
2476
NeilBrown977df362011-12-23 10:17:53 +11002477 if (replacement) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002478 if (bi->bi_error)
NeilBrown977df362011-12-23 10:17:53 +11002479 md_error(conf->mddev, rdev);
2480 else if (is_badblock(rdev, sh->sector,
2481 STRIPE_SECTORS,
2482 &first_bad, &bad_sectors))
2483 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2484 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002485 if (bi->bi_error) {
NeilBrown9f97e4b2014-01-16 09:35:38 +11002486 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11002487 set_bit(WriteErrorSeen, &rdev->flags);
2488 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002489 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2490 set_bit(MD_RECOVERY_NEEDED,
2491 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002492 } else if (is_badblock(rdev, sh->sector,
2493 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10002494 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002495 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002496 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2497 /* That was a successful write so make
2498 * sure it looks like we already did
2499 * a re-write.
2500 */
2501 set_bit(R5_ReWrite, &sh->dev[i].flags);
2502 }
NeilBrown977df362011-12-23 10:17:53 +11002503 }
2504 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002506 if (sh->batch_head && bi->bi_error && !replacement)
shli@kernel.org72ac7332014-12-15 12:57:03 +11002507 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2508
Shaohua Lic9445552016-09-08 10:43:58 -07002509 bio_reset(bi);
NeilBrown977df362011-12-23 10:17:53 +11002510 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2511 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002513 raid5_release_stripe(sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002514
2515 if (sh->batch_head && sh != sh->batch_head)
Shaohua Li6d036f72015-08-13 14:31:57 -07002516 raid5_release_stripe(sh->batch_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
NeilBrown784052e2009-03-31 15:19:07 +11002519static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
2521 struct r5dev *dev = &sh->dev[i];
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 dev->flags = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07002524 dev->sector = raid5_compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525}
2526
Shaohua Li849674e2016-01-20 13:52:20 -08002527static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
2529 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002530 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002531 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002532 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
NeilBrown908f4fb2011-12-23 10:17:50 +11002534 spin_lock_irqsave(&conf->device_lock, flags);
2535 clear_bit(In_sync, &rdev->flags);
2536 mddev->degraded = calc_degraded(conf);
2537 spin_unlock_irqrestore(&conf->device_lock, flags);
2538 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2539
NeilBrownde393cd2011-07-28 11:31:48 +10002540 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10002541 set_bit(Faulty, &rdev->flags);
Guoqing Jiang85ad1d12016-05-03 22:22:13 -04002542 set_mask_bits(&mddev->flags, 0,
2543 BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING));
NeilBrown6f8d0c72011-05-11 14:38:44 +10002544 printk(KERN_ALERT
2545 "md/raid:%s: Disk failure on %s, disabling device.\n"
2546 "md/raid:%s: Operation continuing on %d devices.\n",
2547 mdname(mddev),
2548 bdevname(rdev->bdev, b),
2549 mdname(mddev),
2550 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07002551}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
2553/*
2554 * Input: a 'big' sector number,
2555 * Output: index of the data and parity disk, and the sector # in them.
2556 */
Shaohua Li6d036f72015-08-13 14:31:57 -07002557sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2558 int previous, int *dd_idx,
2559 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002561 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002562 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002564 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002565 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002567 int algorithm = previous ? conf->prev_algo
2568 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002569 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2570 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002571 int raid_disks = previous ? conf->previous_raid_disks
2572 : conf->raid_disks;
2573 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
2575 /* First compute the information on this sector */
2576
2577 /*
2578 * Compute the chunk number and the sector offset inside the chunk
2579 */
2580 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2581 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583 /*
2584 * Compute the stripe number
2585 */
NeilBrown35f2a592010-04-20 14:13:34 +10002586 stripe = chunk_number;
2587 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002588 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 /*
2590 * Select the parity disk based on the user selected algorithm.
2591 */
NeilBrown84789552011-07-27 11:00:36 +10002592 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002593 switch(conf->level) {
2594 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002595 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002596 break;
2597 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002598 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002600 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002601 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 (*dd_idx)++;
2603 break;
2604 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002605 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002606 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 (*dd_idx)++;
2608 break;
2609 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002610 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002611 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 break;
2613 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002614 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002615 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002617 case ALGORITHM_PARITY_0:
2618 pd_idx = 0;
2619 (*dd_idx)++;
2620 break;
2621 case ALGORITHM_PARITY_N:
2622 pd_idx = data_disks;
2623 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002625 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002626 }
2627 break;
2628 case 6:
2629
NeilBrowne183eae2009-03-31 15:20:22 +11002630 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002631 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002632 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002633 qd_idx = pd_idx + 1;
2634 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002635 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002636 qd_idx = 0;
2637 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002638 (*dd_idx) += 2; /* D D P Q D */
2639 break;
2640 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002641 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002642 qd_idx = pd_idx + 1;
2643 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002644 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002645 qd_idx = 0;
2646 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002647 (*dd_idx) += 2; /* D D P Q D */
2648 break;
2649 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002650 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002651 qd_idx = (pd_idx + 1) % raid_disks;
2652 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002653 break;
2654 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002655 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002656 qd_idx = (pd_idx + 1) % raid_disks;
2657 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002658 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002659
2660 case ALGORITHM_PARITY_0:
2661 pd_idx = 0;
2662 qd_idx = 1;
2663 (*dd_idx) += 2;
2664 break;
2665 case ALGORITHM_PARITY_N:
2666 pd_idx = data_disks;
2667 qd_idx = data_disks + 1;
2668 break;
2669
2670 case ALGORITHM_ROTATING_ZERO_RESTART:
2671 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2672 * of blocks for computing Q is different.
2673 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002674 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002675 qd_idx = pd_idx + 1;
2676 if (pd_idx == raid_disks-1) {
2677 (*dd_idx)++; /* Q D D D P */
2678 qd_idx = 0;
2679 } else if (*dd_idx >= pd_idx)
2680 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002681 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002682 break;
2683
2684 case ALGORITHM_ROTATING_N_RESTART:
2685 /* Same a left_asymmetric, by first stripe is
2686 * D D D P Q rather than
2687 * Q D D D P
2688 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002689 stripe2 += 1;
2690 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002691 qd_idx = pd_idx + 1;
2692 if (pd_idx == raid_disks-1) {
2693 (*dd_idx)++; /* Q D D D P */
2694 qd_idx = 0;
2695 } else if (*dd_idx >= pd_idx)
2696 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002697 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002698 break;
2699
2700 case ALGORITHM_ROTATING_N_CONTINUE:
2701 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002702 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002703 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2704 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002705 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002706 break;
2707
2708 case ALGORITHM_LEFT_ASYMMETRIC_6:
2709 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002710 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002711 if (*dd_idx >= pd_idx)
2712 (*dd_idx)++;
2713 qd_idx = raid_disks - 1;
2714 break;
2715
2716 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002717 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002718 if (*dd_idx >= pd_idx)
2719 (*dd_idx)++;
2720 qd_idx = raid_disks - 1;
2721 break;
2722
2723 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002724 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002725 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2726 qd_idx = raid_disks - 1;
2727 break;
2728
2729 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002730 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002731 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2732 qd_idx = raid_disks - 1;
2733 break;
2734
2735 case ALGORITHM_PARITY_0_6:
2736 pd_idx = 0;
2737 (*dd_idx)++;
2738 qd_idx = raid_disks - 1;
2739 break;
2740
NeilBrown16a53ec2006-06-26 00:27:38 -07002741 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002742 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002743 }
2744 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
2746
NeilBrown911d4ee2009-03-31 14:39:38 +11002747 if (sh) {
2748 sh->pd_idx = pd_idx;
2749 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002750 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 /*
2753 * Finally, compute the new sector number
2754 */
2755 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2756 return new_sector;
2757}
2758
Shaohua Li6d036f72015-08-13 14:31:57 -07002759sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
NeilBrownd1688a62011-10-11 16:49:52 +11002761 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002762 int raid_disks = sh->disks;
2763 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002765 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2766 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002767 int algorithm = previous ? conf->prev_algo
2768 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 sector_t stripe;
2770 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002771 sector_t chunk_number;
2772 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002774 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
2776 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2777 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
NeilBrown16a53ec2006-06-26 00:27:38 -07002779 if (i == sh->pd_idx)
2780 return 0;
2781 switch(conf->level) {
2782 case 4: break;
2783 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002784 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 case ALGORITHM_LEFT_ASYMMETRIC:
2786 case ALGORITHM_RIGHT_ASYMMETRIC:
2787 if (i > sh->pd_idx)
2788 i--;
2789 break;
2790 case ALGORITHM_LEFT_SYMMETRIC:
2791 case ALGORITHM_RIGHT_SYMMETRIC:
2792 if (i < sh->pd_idx)
2793 i += raid_disks;
2794 i -= (sh->pd_idx + 1);
2795 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002796 case ALGORITHM_PARITY_0:
2797 i -= 1;
2798 break;
2799 case ALGORITHM_PARITY_N:
2800 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002802 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002803 }
2804 break;
2805 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002806 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002807 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002808 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002809 case ALGORITHM_LEFT_ASYMMETRIC:
2810 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002811 case ALGORITHM_ROTATING_ZERO_RESTART:
2812 case ALGORITHM_ROTATING_N_RESTART:
2813 if (sh->pd_idx == raid_disks-1)
2814 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002815 else if (i > sh->pd_idx)
2816 i -= 2; /* D D P Q D */
2817 break;
2818 case ALGORITHM_LEFT_SYMMETRIC:
2819 case ALGORITHM_RIGHT_SYMMETRIC:
2820 if (sh->pd_idx == raid_disks-1)
2821 i--; /* Q D D D P */
2822 else {
2823 /* D D P Q D */
2824 if (i < sh->pd_idx)
2825 i += raid_disks;
2826 i -= (sh->pd_idx + 2);
2827 }
2828 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002829 case ALGORITHM_PARITY_0:
2830 i -= 2;
2831 break;
2832 case ALGORITHM_PARITY_N:
2833 break;
2834 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002835 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002836 if (sh->pd_idx == 0)
2837 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002838 else {
2839 /* D D Q P D */
2840 if (i < sh->pd_idx)
2841 i += raid_disks;
2842 i -= (sh->pd_idx + 1);
2843 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002844 break;
2845 case ALGORITHM_LEFT_ASYMMETRIC_6:
2846 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2847 if (i > sh->pd_idx)
2848 i--;
2849 break;
2850 case ALGORITHM_LEFT_SYMMETRIC_6:
2851 case ALGORITHM_RIGHT_SYMMETRIC_6:
2852 if (i < sh->pd_idx)
2853 i += data_disks + 1;
2854 i -= (sh->pd_idx + 1);
2855 break;
2856 case ALGORITHM_PARITY_0_6:
2857 i -= 1;
2858 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002859 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002860 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002861 }
2862 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
2864
2865 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002866 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
NeilBrown112bf892009-03-31 14:39:38 +11002868 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002869 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002870 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2871 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002872 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2873 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 return 0;
2875 }
2876 return r_sector;
2877}
2878
Dan Williams600aa102008-06-28 08:32:05 +10002879static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002880schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002881 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002882{
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002883 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002884 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002885 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002886
Dan Williamse33129d2007-01-02 13:52:30 -07002887 if (rcw) {
Dan Williamse33129d2007-01-02 13:52:30 -07002888
2889 for (i = disks; i--; ) {
2890 struct r5dev *dev = &sh->dev[i];
2891
2892 if (dev->towrite) {
2893 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002894 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002895 if (!expand)
2896 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002897 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002898 }
2899 }
NeilBrownce7d3632013-03-04 12:37:14 +11002900 /* if we are not expanding this is a proper write request, and
2901 * there will be bios with new data to be drained into the
2902 * stripe cache
2903 */
2904 if (!expand) {
2905 if (!s->locked)
2906 /* False alarm, nothing to do */
2907 return;
2908 sh->reconstruct_state = reconstruct_state_drain_run;
2909 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2910 } else
2911 sh->reconstruct_state = reconstruct_state_run;
2912
2913 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2914
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002915 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002916 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002917 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002918 } else {
2919 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2920 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002921 BUG_ON(level == 6 &&
2922 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
2923 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
Dan Williamse33129d2007-01-02 13:52:30 -07002924
Dan Williamse33129d2007-01-02 13:52:30 -07002925 for (i = disks; i--; ) {
2926 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002927 if (i == pd_idx || i == qd_idx)
Dan Williamse33129d2007-01-02 13:52:30 -07002928 continue;
2929
Dan Williamse33129d2007-01-02 13:52:30 -07002930 if (dev->towrite &&
2931 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002932 test_bit(R5_Wantcompute, &dev->flags))) {
2933 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002934 set_bit(R5_LOCKED, &dev->flags);
2935 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002936 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002937 }
2938 }
NeilBrownce7d3632013-03-04 12:37:14 +11002939 if (!s->locked)
2940 /* False alarm - nothing to do */
2941 return;
2942 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2943 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2944 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2945 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002946 }
2947
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002948 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002949 * are in flight
2950 */
2951 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2952 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002953 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002954
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002955 if (level == 6) {
2956 int qd_idx = sh->qd_idx;
2957 struct r5dev *dev = &sh->dev[qd_idx];
2958
2959 set_bit(R5_LOCKED, &dev->flags);
2960 clear_bit(R5_UPTODATE, &dev->flags);
2961 s->locked++;
2962 }
2963
Dan Williams600aa102008-06-28 08:32:05 +10002964 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002965 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002966 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002967}
NeilBrown16a53ec2006-06-26 00:27:38 -07002968
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969/*
2970 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002971 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 * The bi_next chain must be in order.
2973 */
shli@kernel.orgda41ba62014-12-15 12:57:03 +11002974static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
2975 int forwrite, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976{
2977 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002978 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002979 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
NeilBrowncbe47ec2011-07-26 11:20:35 +10002981 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07002982 (unsigned long long)bi->bi_iter.bi_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 (unsigned long long)sh->sector);
2984
Shaohua Lib17459c2012-07-19 16:01:31 +10002985 /*
2986 * If several bio share a stripe. The bio bi_phys_segments acts as a
2987 * reference count to avoid race. The reference count should already be
2988 * increased before this function is called (for example, in
Shaohua Li849674e2016-01-20 13:52:20 -08002989 * raid5_make_request()), so other bio sharing this stripe will not free the
Shaohua Lib17459c2012-07-19 16:01:31 +10002990 * stripe. If a stripe is owned by one stripe, the stripe lock will
2991 * protect it.
2992 */
2993 spin_lock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002994 /* Don't allow new IO added to stripes in batch list */
2995 if (sh->batch_head)
2996 goto overlap;
NeilBrown72626682005-09-09 16:23:54 -07002997 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002999 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07003000 firstwrite = 1;
3001 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 bip = &sh->dev[dd_idx].toread;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003003 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
3004 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 goto overlap;
3006 bip = & (*bip)->bi_next;
3007 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07003008 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 goto overlap;
3010
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003011 if (!forwrite || previous)
3012 clear_bit(STRIPE_BATCH_READY, &sh->state);
3013
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02003014 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 if (*bip)
3016 bi->bi_next = *bip;
3017 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003018 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07003019
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 if (forwrite) {
3021 /* check if page is covered */
3022 sector_t sector = sh->dev[dd_idx].sector;
3023 for (bi=sh->dev[dd_idx].towrite;
3024 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07003025 bi && bi->bi_iter.bi_sector <= sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07003027 if (bio_end_sector(bi) >= sector)
3028 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 }
3030 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
shli@kernel.org7a87f432014-12-15 12:57:03 +11003031 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
3032 sh->overwrite_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003034
3035 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003036 (unsigned long long)(*bip)->bi_iter.bi_sector,
NeilBrowncbe47ec2011-07-26 11:20:35 +10003037 (unsigned long long)sh->sector, dd_idx);
3038
3039 if (conf->mddev->bitmap && firstwrite) {
NeilBrownd0852df52015-05-27 08:43:45 +10003040 /* Cannot hold spinlock over bitmap_startwrite,
3041 * but must ensure this isn't added to a batch until
3042 * we have added to the bitmap and set bm_seq.
3043 * So set STRIPE_BITMAP_PENDING to prevent
3044 * batching.
3045 * If multiple add_stripe_bio() calls race here they
3046 * much all set STRIPE_BITMAP_PENDING. So only the first one
3047 * to complete "bitmap_startwrite" gets to set
3048 * STRIPE_BIT_DELAY. This is important as once a stripe
3049 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3050 * any more.
3051 */
3052 set_bit(STRIPE_BITMAP_PENDING, &sh->state);
3053 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10003054 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
3055 STRIPE_SECTORS, 0);
NeilBrownd0852df52015-05-27 08:43:45 +10003056 spin_lock_irq(&sh->stripe_lock);
3057 clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
3058 if (!sh->batch_head) {
3059 sh->bm_seq = conf->seq_flush+1;
3060 set_bit(STRIPE_BIT_DELAY, &sh->state);
3061 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003062 }
NeilBrownd0852df52015-05-27 08:43:45 +10003063 spin_unlock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003064
3065 if (stripe_can_batch(sh))
3066 stripe_add_to_batch_list(conf, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 return 1;
3068
3069 overlap:
3070 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10003071 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 return 0;
3073}
3074
NeilBrownd1688a62011-10-11 16:49:52 +11003075static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08003076
NeilBrownd1688a62011-10-11 16:49:52 +11003077static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003078 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08003079{
NeilBrown784052e2009-03-31 15:19:07 +11003080 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10003081 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11003082 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003083 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11003084 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003085
NeilBrown112bf892009-03-31 14:39:38 +11003086 raid5_compute_sector(conf,
3087 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08003088 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11003089 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003090 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003091}
3092
Dan Williamsa4456852007-07-09 11:56:43 -07003093static void
NeilBrownd1688a62011-10-11 16:49:52 +11003094handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003095 struct stripe_head_state *s, int disks,
NeilBrown34a6f802015-08-14 12:07:57 +10003096 struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003097{
3098 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003099 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003100 for (i = disks; i--; ) {
3101 struct bio *bi;
3102 int bitmap_end = 0;
3103
3104 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003105 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07003106 rcu_read_lock();
3107 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownf5b67ae2016-06-02 16:19:53 +10003108 if (rdev && test_bit(In_sync, &rdev->flags) &&
3109 !test_bit(Faulty, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10003110 atomic_inc(&rdev->nr_pending);
3111 else
3112 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003113 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10003114 if (rdev) {
3115 if (!rdev_set_badblocks(
3116 rdev,
3117 sh->sector,
3118 STRIPE_SECTORS, 0))
3119 md_error(conf->mddev, rdev);
3120 rdev_dec_pending(rdev, conf->mddev);
3121 }
Dan Williamsa4456852007-07-09 11:56:43 -07003122 }
Shaohua Lib17459c2012-07-19 16:01:31 +10003123 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003124 /* fail all writes first */
3125 bi = sh->dev[i].towrite;
3126 sh->dev[i].towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11003127 sh->overwrite_disks = 0;
Shaohua Lib17459c2012-07-19 16:01:31 +10003128 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11003129 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003130 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07003131
Shaohua Li0576b1c2015-08-13 14:32:00 -07003132 r5l_stripe_write_finished(sh);
3133
Dan Williamsa4456852007-07-09 11:56:43 -07003134 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3135 wake_up(&conf->wait_for_overlap);
3136
Kent Overstreet4f024f32013-10-11 15:44:27 -07003137 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003138 sh->dev[i].sector + STRIPE_SECTORS) {
3139 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003140
3141 bi->bi_error = -EIO;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003142 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003143 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003144 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003145 }
3146 bi = nextbi;
3147 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003148 if (bitmap_end)
3149 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3150 STRIPE_SECTORS, 0, 0);
3151 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003152 /* and fail all 'written' */
3153 bi = sh->dev[i].written;
3154 sh->dev[i].written = NULL;
Shaohua Lid592a992014-05-21 17:57:44 +08003155 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3156 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3157 sh->dev[i].page = sh->dev[i].orig_page;
3158 }
3159
Dan Williamsa4456852007-07-09 11:56:43 -07003160 if (bi) bitmap_end = 1;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003161 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003162 sh->dev[i].sector + STRIPE_SECTORS) {
3163 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003164
3165 bi->bi_error = -EIO;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003166 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003167 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003168 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003169 }
3170 bi = bi2;
3171 }
3172
Dan Williamsb5e98d62007-01-02 13:52:31 -07003173 /* fail any reads if this device is non-operational and
3174 * the data has not reached the cache yet.
3175 */
3176 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
Shaohua Li6e74a9c2015-10-08 21:54:08 -07003177 s->failed > conf->max_degraded &&
Dan Williamsb5e98d62007-01-02 13:52:31 -07003178 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3179 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11003180 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003181 bi = sh->dev[i].toread;
3182 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11003183 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003184 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3185 wake_up(&conf->wait_for_overlap);
Shaohua Liebda7802015-09-18 10:20:13 -07003186 if (bi)
3187 s->to_read--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003188 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003189 sh->dev[i].sector + STRIPE_SECTORS) {
3190 struct bio *nextbi =
3191 r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003192
3193 bi->bi_error = -EIO;
NeilBrown34a6f802015-08-14 12:07:57 +10003194 if (!raid5_dec_bi_active_stripes(bi))
3195 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003196 bi = nextbi;
3197 }
3198 }
Dan Williamsa4456852007-07-09 11:56:43 -07003199 if (bitmap_end)
3200 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3201 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10003202 /* If we were in the middle of a write the parity block might
3203 * still be locked - so just clear all R5_LOCKED flags
3204 */
3205 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003206 }
Shaohua Liebda7802015-09-18 10:20:13 -07003207 s->to_write = 0;
3208 s->written = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003209
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003210 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3211 if (atomic_dec_and_test(&conf->pending_full_writes))
3212 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003213}
3214
NeilBrown7f0da592011-07-28 11:39:22 +10003215static void
NeilBrownd1688a62011-10-11 16:49:52 +11003216handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10003217 struct stripe_head_state *s)
3218{
3219 int abort = 0;
3220 int i;
3221
shli@kernel.org59fc6302014-12-15 12:57:03 +11003222 BUG_ON(sh->batch_head);
NeilBrown7f0da592011-07-28 11:39:22 +10003223 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003224 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3225 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10003226 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11003227 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10003228 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10003229 * Don't even need to abort as that is handled elsewhere
3230 * if needed, and not always wanted e.g. if there is a known
3231 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11003232 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10003233 * non-sync devices, or abort the recovery
3234 */
NeilBrown18b98372012-04-01 23:48:38 +10003235 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3236 /* During recovery devices cannot be removed, so
3237 * locking and refcounting of rdevs is not needed
3238 */
NeilBrowne50d3992016-06-02 16:19:52 +10003239 rcu_read_lock();
NeilBrown18b98372012-04-01 23:48:38 +10003240 for (i = 0; i < conf->raid_disks; i++) {
NeilBrowne50d3992016-06-02 16:19:52 +10003241 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown18b98372012-04-01 23:48:38 +10003242 if (rdev
3243 && !test_bit(Faulty, &rdev->flags)
3244 && !test_bit(In_sync, &rdev->flags)
3245 && !rdev_set_badblocks(rdev, sh->sector,
3246 STRIPE_SECTORS, 0))
3247 abort = 1;
NeilBrowne50d3992016-06-02 16:19:52 +10003248 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown18b98372012-04-01 23:48:38 +10003249 if (rdev
3250 && !test_bit(Faulty, &rdev->flags)
3251 && !test_bit(In_sync, &rdev->flags)
3252 && !rdev_set_badblocks(rdev, sh->sector,
3253 STRIPE_SECTORS, 0))
3254 abort = 1;
3255 }
NeilBrowne50d3992016-06-02 16:19:52 +10003256 rcu_read_unlock();
NeilBrown18b98372012-04-01 23:48:38 +10003257 if (abort)
3258 conf->recovery_disabled =
3259 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10003260 }
NeilBrown18b98372012-04-01 23:48:38 +10003261 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10003262}
3263
NeilBrown9a3e1102011-12-23 10:17:53 +11003264static int want_replace(struct stripe_head *sh, int disk_idx)
3265{
3266 struct md_rdev *rdev;
3267 int rv = 0;
NeilBrown3f232d62016-06-02 16:19:52 +10003268
3269 rcu_read_lock();
3270 rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
NeilBrown9a3e1102011-12-23 10:17:53 +11003271 if (rdev
3272 && !test_bit(Faulty, &rdev->flags)
3273 && !test_bit(In_sync, &rdev->flags)
3274 && (rdev->recovery_offset <= sh->sector
3275 || rdev->mddev->recovery_cp <= sh->sector))
3276 rv = 1;
NeilBrown3f232d62016-06-02 16:19:52 +10003277 rcu_read_unlock();
NeilBrown9a3e1102011-12-23 10:17:53 +11003278 return rv;
3279}
3280
NeilBrown93b3dbc2011-07-27 11:00:36 +10003281/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10003282 * to be read or computed to satisfy a request.
3283 *
3284 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10003285 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07003286 */
NeilBrown2c58f062015-02-02 11:32:23 +11003287
3288static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3289 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07003290{
3291 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10003292 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3293 &sh->dev[s->failed_num[1]] };
NeilBrownea664c82015-02-02 14:03:28 +11003294 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07003295
NeilBrowna79cfe12015-02-02 11:37:59 +11003296
3297 if (test_bit(R5_LOCKED, &dev->flags) ||
3298 test_bit(R5_UPTODATE, &dev->flags))
3299 /* No point reading this as we already have it or have
3300 * decided to get it.
3301 */
3302 return 0;
3303
3304 if (dev->toread ||
3305 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3306 /* We need this block to directly satisfy a request */
3307 return 1;
3308
3309 if (s->syncing || s->expanding ||
3310 (s->replacing && want_replace(sh, disk_idx)))
3311 /* When syncing, or expanding we read everything.
3312 * When replacing, we need the replaced block.
3313 */
3314 return 1;
3315
3316 if ((s->failed >= 1 && fdev[0]->toread) ||
3317 (s->failed >= 2 && fdev[1]->toread))
3318 /* If we want to read from a failed device, then
3319 * we need to actually read every other device.
3320 */
3321 return 1;
3322
NeilBrowna9d56952015-02-02 11:49:10 +11003323 /* Sometimes neither read-modify-write nor reconstruct-write
3324 * cycles can work. In those cases we read every block we
3325 * can. Then the parity-update is certain to have enough to
3326 * work with.
3327 * This can only be a problem when we need to write something,
3328 * and some device has failed. If either of those tests
3329 * fail we need look no further.
3330 */
3331 if (!s->failed || !s->to_write)
3332 return 0;
3333
3334 if (test_bit(R5_Insync, &dev->flags) &&
3335 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3336 /* Pre-reads at not permitted until after short delay
3337 * to gather multiple requests. However if this
3338 * device is no Insync, the block could only be be computed
3339 * and there is no need to delay that.
3340 */
3341 return 0;
NeilBrownea664c82015-02-02 14:03:28 +11003342
NeilBrown36707bb2015-09-24 15:25:36 +10003343 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrownea664c82015-02-02 14:03:28 +11003344 if (fdev[i]->towrite &&
3345 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3346 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3347 /* If we have a partial write to a failed
3348 * device, then we will need to reconstruct
3349 * the content of that device, so all other
3350 * devices must be read.
3351 */
3352 return 1;
3353 }
3354
3355 /* If we are forced to do a reconstruct-write, either because
3356 * the current RAID6 implementation only supports that, or
3357 * or because parity cannot be trusted and we are currently
3358 * recovering it, there is extra need to be careful.
3359 * If one of the devices that we would need to read, because
3360 * it is not being overwritten (and maybe not written at all)
3361 * is missing/faulty, then we need to read everything we can.
3362 */
3363 if (sh->raid_conf->level != 6 &&
3364 sh->sector < sh->raid_conf->mddev->recovery_cp)
3365 /* reconstruct-write isn't being forced */
3366 return 0;
NeilBrown36707bb2015-09-24 15:25:36 +10003367 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrown10d82c52015-05-08 18:19:33 +10003368 if (s->failed_num[i] != sh->pd_idx &&
3369 s->failed_num[i] != sh->qd_idx &&
3370 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
NeilBrownea664c82015-02-02 14:03:28 +11003371 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3372 return 1;
3373 }
3374
NeilBrown2c58f062015-02-02 11:32:23 +11003375 return 0;
3376}
3377
3378static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3379 int disk_idx, int disks)
3380{
3381 struct r5dev *dev = &sh->dev[disk_idx];
3382
3383 /* is the data in this block needed, and can we get it? */
3384 if (need_this_block(sh, s, disk_idx, disks)) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003385 /* we would like to get this block, possibly by computing it,
3386 * otherwise read it if the backing disk is insync
3387 */
3388 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3389 BUG_ON(test_bit(R5_Wantread, &dev->flags));
NeilBrownb0c783b2015-05-08 18:19:32 +10003390 BUG_ON(sh->batch_head);
NeilBrownb42b97a2017-04-03 12:11:32 +10003391
3392 /*
3393 * In the raid6 case if the only non-uptodate disk is P
3394 * then we already trusted P to compute the other failed
3395 * drives. It is safe to compute rather than re-read P.
3396 * In other cases we only compute blocks from failed
3397 * devices, otherwise check/repair might fail to detect
3398 * a real inconsistency.
3399 */
3400
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003401 if ((s->uptodate == disks - 1) &&
NeilBrownb42b97a2017-04-03 12:11:32 +10003402 ((sh->qd_idx >= 0 && sh->pd_idx == disk_idx) ||
NeilBrownf2b3b442011-07-26 11:35:19 +10003403 (s->failed && (disk_idx == s->failed_num[0] ||
NeilBrownb42b97a2017-04-03 12:11:32 +10003404 disk_idx == s->failed_num[1])))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003405 /* have disk failed, and we're requested to fetch it;
3406 * do compute it
3407 */
3408 pr_debug("Computing stripe %llu block %d\n",
3409 (unsigned long long)sh->sector, disk_idx);
3410 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3411 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3412 set_bit(R5_Wantcompute, &dev->flags);
3413 sh->ops.target = disk_idx;
3414 sh->ops.target2 = -1; /* no 2nd target */
3415 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10003416 /* Careful: from this point on 'uptodate' is in the eye
3417 * of raid_run_ops which services 'compute' operations
3418 * before writes. R5_Wantcompute flags a block that will
3419 * be R5_UPTODATE by the time it is needed for a
3420 * subsequent operation.
3421 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003422 s->uptodate++;
3423 return 1;
3424 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3425 /* Computing 2-failure is *very* expensive; only
3426 * do it if failed >= 2
3427 */
3428 int other;
3429 for (other = disks; other--; ) {
3430 if (other == disk_idx)
3431 continue;
3432 if (!test_bit(R5_UPTODATE,
3433 &sh->dev[other].flags))
3434 break;
3435 }
3436 BUG_ON(other < 0);
3437 pr_debug("Computing stripe %llu blocks %d,%d\n",
3438 (unsigned long long)sh->sector,
3439 disk_idx, other);
3440 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3441 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3442 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3443 set_bit(R5_Wantcompute, &sh->dev[other].flags);
3444 sh->ops.target = disk_idx;
3445 sh->ops.target2 = other;
3446 s->uptodate += 2;
3447 s->req_compute = 1;
3448 return 1;
3449 } else if (test_bit(R5_Insync, &dev->flags)) {
3450 set_bit(R5_LOCKED, &dev->flags);
3451 set_bit(R5_Wantread, &dev->flags);
3452 s->locked++;
3453 pr_debug("Reading block %d (sync=%d)\n",
3454 disk_idx, s->syncing);
3455 }
3456 }
3457
3458 return 0;
3459}
3460
3461/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10003462 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003463 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10003464static void handle_stripe_fill(struct stripe_head *sh,
3465 struct stripe_head_state *s,
3466 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003467{
3468 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003469
3470 /* look for blocks to read/compute, skip this if a compute
3471 * is already in flight, or if the stripe contents are in the
3472 * midst of changing due to a write
3473 */
3474 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
3475 !sh->reconstruct_state)
3476 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10003477 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003478 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003479 set_bit(STRIPE_HANDLE, &sh->state);
3480}
3481
NeilBrown787b76f2015-05-21 12:56:41 +10003482static void break_stripe_batch_list(struct stripe_head *head_sh,
3483 unsigned long handle_flags);
Dan Williams1fe797e2008-06-28 09:16:30 +10003484/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07003485 * any written block on an uptodate or failed drive can be returned.
3486 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3487 * never LOCKED, so we don't need to test 'failed' directly.
3488 */
NeilBrownd1688a62011-10-11 16:49:52 +11003489static void handle_stripe_clean_event(struct r5conf *conf,
NeilBrown34a6f802015-08-14 12:07:57 +10003490 struct stripe_head *sh, int disks, struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003491{
3492 int i;
3493 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003494 int discard_pending = 0;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003495 struct stripe_head *head_sh = sh;
3496 bool do_endio = false;
Dan Williamsa4456852007-07-09 11:56:43 -07003497
3498 for (i = disks; i--; )
3499 if (sh->dev[i].written) {
3500 dev = &sh->dev[i];
3501 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003502 (test_bit(R5_UPTODATE, &dev->flags) ||
Shaohua Lid592a992014-05-21 17:57:44 +08003503 test_bit(R5_Discard, &dev->flags) ||
3504 test_bit(R5_SkipCopy, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003505 /* We can return any write requests */
3506 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003507 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003508 if (test_and_clear_bit(R5_Discard, &dev->flags))
3509 clear_bit(R5_UPTODATE, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08003510 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3511 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
Shaohua Lid592a992014-05-21 17:57:44 +08003512 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003513 do_endio = true;
3514
3515returnbi:
3516 dev->page = dev->orig_page;
Dan Williamsa4456852007-07-09 11:56:43 -07003517 wbi = dev->written;
3518 dev->written = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003519 while (wbi && wbi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003520 dev->sector + STRIPE_SECTORS) {
3521 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10003522 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003523 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003524 bio_list_add(return_bi, wbi);
Dan Williamsa4456852007-07-09 11:56:43 -07003525 }
3526 wbi = wbi2;
3527 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003528 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3529 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07003530 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003531 0);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003532 if (head_sh->batch_head) {
3533 sh = list_first_entry(&sh->batch_list,
3534 struct stripe_head,
3535 batch_list);
3536 if (sh != head_sh) {
3537 dev = &sh->dev[i];
3538 goto returnbi;
3539 }
3540 }
3541 sh = head_sh;
3542 dev = &sh->dev[i];
NeilBrownf8dfcff2013-03-12 12:18:06 +11003543 } else if (test_bit(R5_Discard, &dev->flags))
3544 discard_pending = 1;
3545 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -07003546
Shaohua Li0576b1c2015-08-13 14:32:00 -07003547 r5l_stripe_write_finished(sh);
3548
NeilBrownf8dfcff2013-03-12 12:18:06 +11003549 if (!discard_pending &&
3550 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003551 int hash;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003552 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3553 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3554 if (sh->qd_idx >= 0) {
3555 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3556 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3557 }
3558 /* now that discard is done we can proceed with any sync */
3559 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08003560 /*
3561 * SCSI discard will change some bio fields and the stripe has
3562 * no updated data, so remove it from hash list and the stripe
3563 * will be reinitialized
3564 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11003565unhash:
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003566 hash = sh->hash_lock_index;
3567 spin_lock_irq(conf->hash_locks + hash);
Shaohua Lid47648f2013-10-19 14:51:42 +08003568 remove_hash(sh);
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003569 spin_unlock_irq(conf->hash_locks + hash);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003570 if (head_sh->batch_head) {
3571 sh = list_first_entry(&sh->batch_list,
3572 struct stripe_head, batch_list);
3573 if (sh != head_sh)
3574 goto unhash;
3575 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003576 sh = head_sh;
3577
NeilBrownf8dfcff2013-03-12 12:18:06 +11003578 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3579 set_bit(STRIPE_HANDLE, &sh->state);
3580
3581 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003582
3583 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3584 if (atomic_dec_and_test(&conf->pending_full_writes))
3585 md_wakeup_thread(conf->mddev->thread);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003586
NeilBrown787b76f2015-05-21 12:56:41 +10003587 if (head_sh->batch_head && do_endio)
3588 break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
Dan Williamsa4456852007-07-09 11:56:43 -07003589}
3590
NeilBrownd1688a62011-10-11 16:49:52 +11003591static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10003592 struct stripe_head *sh,
3593 struct stripe_head_state *s,
3594 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003595{
3596 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003597 sector_t recovery_cp = conf->mddev->recovery_cp;
3598
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003599 /* Check whether resync is now happening or should start.
Alexander Lyakasa7854482012-10-11 13:50:12 +11003600 * If yes, then the array is dirty (after unclean shutdown or
3601 * initial creation), so parity in some stripes might be inconsistent.
3602 * In this case, we need to always do reconstruct-write, to ensure
3603 * that in case of drive failure or read-error correction, we
3604 * generate correct data from the parity.
3605 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003606 if (conf->rmw_level == PARITY_DISABLE_RMW ||
NeilBrown26ac1072015-02-18 11:35:14 +11003607 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
3608 s->failed == 0)) {
Alexander Lyakasa7854482012-10-11 13:50:12 +11003609 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10003610 * look like rcw is cheaper
3611 */
3612 rcw = 1; rmw = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003613 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
3614 conf->rmw_level, (unsigned long long)recovery_cp,
Alexander Lyakasa7854482012-10-11 13:50:12 +11003615 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10003616 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003617 /* would I have to read this buffer for read_modify_write */
3618 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003619 if ((dev->towrite || i == sh->pd_idx || i == sh->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003620 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003621 !(test_bit(R5_UPTODATE, &dev->flags) ||
3622 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003623 if (test_bit(R5_Insync, &dev->flags))
3624 rmw++;
3625 else
3626 rmw += 2*disks; /* cannot read it */
3627 }
3628 /* Would I have to read this buffer for reconstruct_write */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003629 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3630 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003631 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003632 !(test_bit(R5_UPTODATE, &dev->flags) ||
3633 test_bit(R5_Wantcompute, &dev->flags))) {
NeilBrown67f45542014-05-28 13:39:22 +10003634 if (test_bit(R5_Insync, &dev->flags))
3635 rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07003636 else
3637 rcw += 2*disks;
3638 }
3639 }
Dan Williams45b42332007-07-09 11:56:43 -07003640 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003641 (unsigned long long)sh->sector, rmw, rcw);
3642 set_bit(STRIPE_HANDLE, &sh->state);
Song Liu41257582016-05-23 17:25:06 -07003643 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003644 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003645 if (conf->mddev->queue)
3646 blk_add_trace_msg(conf->mddev->queue,
3647 "raid5 rmw %llu %d",
3648 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07003649 for (i = disks; i--; ) {
3650 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003651 if ((dev->towrite || i == sh->pd_idx || i == sh->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003652 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003653 !(test_bit(R5_UPTODATE, &dev->flags) ||
3654 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003655 test_bit(R5_Insync, &dev->flags)) {
NeilBrown67f45542014-05-28 13:39:22 +10003656 if (test_bit(STRIPE_PREREAD_ACTIVE,
3657 &sh->state)) {
3658 pr_debug("Read_old block %d for r-m-w\n",
3659 i);
Dan Williamsa4456852007-07-09 11:56:43 -07003660 set_bit(R5_LOCKED, &dev->flags);
3661 set_bit(R5_Wantread, &dev->flags);
3662 s->locked++;
3663 } else {
3664 set_bit(STRIPE_DELAYED, &sh->state);
3665 set_bit(STRIPE_HANDLE, &sh->state);
3666 }
3667 }
3668 }
NeilBrowna9add5d2012-10-31 11:59:09 +11003669 }
Song Liu41257582016-05-23 17:25:06 -07003670 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003671 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11003672 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10003673 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003674 for (i = disks; i--; ) {
3675 struct r5dev *dev = &sh->dev[i];
3676 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10003677 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003678 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003679 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10003680 test_bit(R5_Wantcompute, &dev->flags))) {
3681 rcw++;
NeilBrown67f45542014-05-28 13:39:22 +10003682 if (test_bit(R5_Insync, &dev->flags) &&
3683 test_bit(STRIPE_PREREAD_ACTIVE,
3684 &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07003685 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07003686 "%d for Reconstruct\n", i);
3687 set_bit(R5_LOCKED, &dev->flags);
3688 set_bit(R5_Wantread, &dev->flags);
3689 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11003690 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07003691 } else {
3692 set_bit(STRIPE_DELAYED, &sh->state);
3693 set_bit(STRIPE_HANDLE, &sh->state);
3694 }
3695 }
3696 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003697 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11003698 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3699 (unsigned long long)sh->sector,
3700 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10003701 }
NeilBrownb1b02fe2015-02-02 10:44:29 +11003702
3703 if (rcw > disks && rmw > disks &&
3704 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3705 set_bit(STRIPE_DELAYED, &sh->state);
3706
Dan Williamsa4456852007-07-09 11:56:43 -07003707 /* now if nothing is locked, and if we have enough data,
3708 * we can start a write request
3709 */
Dan Williamsf38e1212007-01-02 13:52:30 -07003710 /* since handle_stripe can be called at any time we need to handle the
3711 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07003712 * subsequent call wants to start a write request. raid_run_ops only
3713 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07003714 * simultaneously. If this is not the case then new writes need to be
3715 * held off until the compute completes.
3716 */
Dan Williams976ea8d2008-06-28 08:32:03 +10003717 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3718 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
3719 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003720 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07003721}
3722
NeilBrownd1688a62011-10-11 16:49:52 +11003723static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003724 struct stripe_head_state *s, int disks)
3725{
Dan Williamsecc65c92008-06-28 08:31:57 +10003726 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07003727
shli@kernel.org59fc6302014-12-15 12:57:03 +11003728 BUG_ON(sh->batch_head);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003729 set_bit(STRIPE_HANDLE, &sh->state);
3730
Dan Williamsecc65c92008-06-28 08:31:57 +10003731 switch (sh->check_state) {
3732 case check_state_idle:
3733 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07003734 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07003735 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10003736 sh->check_state = check_state_run;
3737 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003738 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003739 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10003740 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07003741 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003742 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10003743 /* fall through */
3744 case check_state_compute_result:
3745 sh->check_state = check_state_idle;
3746 if (!dev)
3747 dev = &sh->dev[sh->pd_idx];
3748
3749 /* check that a write has not made the stripe insync */
3750 if (test_bit(STRIPE_INSYNC, &sh->state))
3751 break;
3752
3753 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07003754 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3755 BUG_ON(s->uptodate != disks);
3756
3757 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10003758 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003759 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003760
Dan Williamsa4456852007-07-09 11:56:43 -07003761 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003762 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003763 break;
3764 case check_state_run:
3765 break; /* we will be called again upon completion */
3766 case check_state_check_result:
3767 sh->check_state = check_state_idle;
3768
3769 /* if a failure occurred during the check operation, leave
3770 * STRIPE_INSYNC not set and let the stripe be handled again
3771 */
3772 if (s->failed)
3773 break;
3774
3775 /* handle a successful check operation, if parity is correct
3776 * we are done. Otherwise update the mismatch count and repair
3777 * parity if !MD_RECOVERY_CHECK
3778 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003779 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003780 /* parity is correct (on disc,
3781 * not in buffer any more)
3782 */
3783 set_bit(STRIPE_INSYNC, &sh->state);
3784 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003785 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003786 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3787 /* don't try to repair!! */
3788 set_bit(STRIPE_INSYNC, &sh->state);
3789 else {
3790 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003791 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003792 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3793 set_bit(R5_Wantcompute,
3794 &sh->dev[sh->pd_idx].flags);
3795 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003796 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003797 s->uptodate++;
3798 }
3799 }
3800 break;
3801 case check_state_compute_run:
3802 break;
3803 default:
3804 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3805 __func__, sh->check_state,
3806 (unsigned long long) sh->sector);
3807 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003808 }
3809}
3810
NeilBrownd1688a62011-10-11 16:49:52 +11003811static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003812 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003813 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003814{
Dan Williamsa4456852007-07-09 11:56:43 -07003815 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003816 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003817 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003818
shli@kernel.org59fc6302014-12-15 12:57:03 +11003819 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003820 set_bit(STRIPE_HANDLE, &sh->state);
3821
3822 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003823
Dan Williamsa4456852007-07-09 11:56:43 -07003824 /* Want to check and possibly repair P and Q.
3825 * However there could be one 'failed' device, in which
3826 * case we can only check one of them, possibly using the
3827 * other to generate missing data
3828 */
3829
Dan Williamsd82dfee2009-07-14 13:40:57 -07003830 switch (sh->check_state) {
3831 case check_state_idle:
3832 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003833 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003834 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003835 * makes sense to check P (If anything else were failed,
3836 * we would have used P to recreate it).
3837 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003838 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003839 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003840 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003841 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003842 * anything, so it makes sense to check it
3843 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003844 if (sh->check_state == check_state_run)
3845 sh->check_state = check_state_run_pq;
3846 else
3847 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003848 }
Dan Williams36d1c642009-07-14 11:48:22 -07003849
Dan Williamsd82dfee2009-07-14 13:40:57 -07003850 /* discard potentially stale zero_sum_result */
3851 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003852
Dan Williamsd82dfee2009-07-14 13:40:57 -07003853 if (sh->check_state == check_state_run) {
3854 /* async_xor_zero_sum destroys the contents of P */
3855 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3856 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003857 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003858 if (sh->check_state >= check_state_run &&
3859 sh->check_state <= check_state_run_pq) {
3860 /* async_syndrome_zero_sum preserves P and Q, so
3861 * no need to mark them !uptodate here
3862 */
3863 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3864 break;
3865 }
Dan Williams36d1c642009-07-14 11:48:22 -07003866
Dan Williamsd82dfee2009-07-14 13:40:57 -07003867 /* we have 2-disk failure */
3868 BUG_ON(s->failed != 2);
3869 /* fall through */
3870 case check_state_compute_result:
3871 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003872
Dan Williamsd82dfee2009-07-14 13:40:57 -07003873 /* check that a write has not made the stripe insync */
3874 if (test_bit(STRIPE_INSYNC, &sh->state))
3875 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003876
3877 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003878 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003879 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003880 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003881 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003882 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003883 s->locked++;
3884 set_bit(R5_LOCKED, &dev->flags);
3885 set_bit(R5_Wantwrite, &dev->flags);
3886 }
3887 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003888 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003889 s->locked++;
3890 set_bit(R5_LOCKED, &dev->flags);
3891 set_bit(R5_Wantwrite, &dev->flags);
3892 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003893 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003894 dev = &sh->dev[pd_idx];
3895 s->locked++;
3896 set_bit(R5_LOCKED, &dev->flags);
3897 set_bit(R5_Wantwrite, &dev->flags);
3898 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003899 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003900 dev = &sh->dev[qd_idx];
3901 s->locked++;
3902 set_bit(R5_LOCKED, &dev->flags);
3903 set_bit(R5_Wantwrite, &dev->flags);
3904 }
3905 clear_bit(STRIPE_DEGRADED, &sh->state);
3906
3907 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003908 break;
3909 case check_state_run:
3910 case check_state_run_q:
3911 case check_state_run_pq:
3912 break; /* we will be called again upon completion */
3913 case check_state_check_result:
3914 sh->check_state = check_state_idle;
3915
3916 /* handle a successful check operation, if parity is correct
3917 * we are done. Otherwise update the mismatch count and repair
3918 * parity if !MD_RECOVERY_CHECK
3919 */
3920 if (sh->ops.zero_sum_result == 0) {
3921 /* both parities are correct */
3922 if (!s->failed)
3923 set_bit(STRIPE_INSYNC, &sh->state);
3924 else {
3925 /* in contrast to the raid5 case we can validate
3926 * parity, but still have a failure to write
3927 * back
3928 */
3929 sh->check_state = check_state_compute_result;
3930 /* Returning at this point means that we may go
3931 * off and bring p and/or q uptodate again so
3932 * we make sure to check zero_sum_result again
3933 * to verify if p or q need writeback
3934 */
3935 }
3936 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003937 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003938 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3939 /* don't try to repair!! */
3940 set_bit(STRIPE_INSYNC, &sh->state);
3941 else {
3942 int *target = &sh->ops.target;
3943
3944 sh->ops.target = -1;
3945 sh->ops.target2 = -1;
3946 sh->check_state = check_state_compute_run;
3947 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3948 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3949 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3950 set_bit(R5_Wantcompute,
3951 &sh->dev[pd_idx].flags);
3952 *target = pd_idx;
3953 target = &sh->ops.target2;
3954 s->uptodate++;
3955 }
3956 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3957 set_bit(R5_Wantcompute,
3958 &sh->dev[qd_idx].flags);
3959 *target = qd_idx;
3960 s->uptodate++;
3961 }
3962 }
3963 }
3964 break;
3965 case check_state_compute_run:
3966 break;
3967 default:
3968 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3969 __func__, sh->check_state,
3970 (unsigned long long) sh->sector);
3971 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003972 }
3973}
3974
NeilBrownd1688a62011-10-11 16:49:52 +11003975static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003976{
3977 int i;
3978
3979 /* We have read all the blocks in this stripe and now we need to
3980 * copy some of them into a target stripe for expand.
3981 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003982 struct dma_async_tx_descriptor *tx = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003983 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003984 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3985 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003986 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003987 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003988 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003989 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003990
Shaohua Li6d036f72015-08-13 14:31:57 -07003991 sector_t bn = raid5_compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003992 sector_t s = raid5_compute_sector(conf, bn, 0,
3993 &dd_idx, NULL);
Shaohua Li6d036f72015-08-13 14:31:57 -07003994 sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003995 if (sh2 == NULL)
3996 /* so far only the early blocks of this stripe
3997 * have been requested. When later blocks
3998 * get requested, we will try again
3999 */
4000 continue;
4001 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
4002 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
4003 /* must have already done this block */
Shaohua Li6d036f72015-08-13 14:31:57 -07004004 raid5_release_stripe(sh2);
Dan Williamsa4456852007-07-09 11:56:43 -07004005 continue;
4006 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07004007
4008 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07004009 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004010 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07004011 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07004012 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004013
Dan Williamsa4456852007-07-09 11:56:43 -07004014 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
4015 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
4016 for (j = 0; j < conf->raid_disks; j++)
4017 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10004018 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07004019 !test_bit(R5_Expanded, &sh2->dev[j].flags))
4020 break;
4021 if (j == conf->raid_disks) {
4022 set_bit(STRIPE_EXPAND_READY, &sh2->state);
4023 set_bit(STRIPE_HANDLE, &sh2->state);
4024 }
Shaohua Li6d036f72015-08-13 14:31:57 -07004025 raid5_release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004026
Dan Williamsa4456852007-07-09 11:56:43 -07004027 }
NeilBrowna2e08552007-09-11 15:23:36 -07004028 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11004029 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07004030}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
4032/*
4033 * handle_stripe - do things to a stripe.
4034 *
NeilBrown9a3e1102011-12-23 10:17:53 +11004035 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4036 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11004038 * return some read requests which now have data
4039 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 * schedule a read on some buffers
4041 * schedule a write of some buffers
4042 * return confirmation of parity correctness
4043 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 */
Dan Williamsa4456852007-07-09 11:56:43 -07004045
NeilBrownacfe7262011-07-27 11:00:36 +10004046static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07004047{
NeilBrownd1688a62011-10-11 16:49:52 +11004048 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08004049 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004050 struct r5dev *dev;
4051 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11004052 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07004053
NeilBrownacfe7262011-07-27 11:00:36 +10004054 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07004055
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004056 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
4057 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
NeilBrownacfe7262011-07-27 11:00:36 +10004058 s->failed_num[0] = -1;
4059 s->failed_num[1] = -1;
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004060 s->log_failed = r5l_log_disk_error(conf);
NeilBrownacfe7262011-07-27 11:00:36 +10004061
4062 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07004063 rcu_read_lock();
4064 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004065 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10004066 sector_t first_bad;
4067 int bad_sectors;
4068 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10004069
NeilBrown16a53ec2006-06-26 00:27:38 -07004070 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07004071
Dan Williams45b42332007-07-09 11:56:43 -07004072 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11004073 i, dev->flags,
4074 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004075 /* maybe we can reply to a read
4076 *
4077 * new wantfill requests are only permitted while
4078 * ops_complete_biofill is guaranteed to be inactive
4079 */
4080 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4081 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4082 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07004083
4084 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10004085 if (test_bit(R5_LOCKED, &dev->flags))
4086 s->locked++;
4087 if (test_bit(R5_UPTODATE, &dev->flags))
4088 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004089 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004090 s->compute++;
4091 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004092 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004093
NeilBrownacfe7262011-07-27 11:00:36 +10004094 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004095 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10004096 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10004097 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004098 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10004099 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004100 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004101 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004102 }
Dan Williamsa4456852007-07-09 11:56:43 -07004103 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10004104 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11004105 /* Prefer to use the replacement for reads, but only
4106 * if it is recovered enough and has no bad blocks.
4107 */
4108 rdev = rcu_dereference(conf->disks[i].replacement);
4109 if (rdev && !test_bit(Faulty, &rdev->flags) &&
4110 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
4111 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4112 &first_bad, &bad_sectors))
4113 set_bit(R5_ReadRepl, &dev->flags);
4114 else {
NeilBrowne6030cb2015-07-17 13:26:23 +10004115 if (rdev && !test_bit(Faulty, &rdev->flags))
NeilBrown9a3e1102011-12-23 10:17:53 +11004116 set_bit(R5_NeedReplace, &dev->flags);
NeilBrowne6030cb2015-07-17 13:26:23 +10004117 else
4118 clear_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11004119 rdev = rcu_dereference(conf->disks[i].rdev);
4120 clear_bit(R5_ReadRepl, &dev->flags);
4121 }
NeilBrown9283d8c2011-12-08 16:27:57 +11004122 if (rdev && test_bit(Faulty, &rdev->flags))
4123 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10004124 if (rdev) {
4125 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4126 &first_bad, &bad_sectors);
4127 if (s->blocked_rdev == NULL
4128 && (test_bit(Blocked, &rdev->flags)
4129 || is_bad < 0)) {
4130 if (is_bad < 0)
4131 set_bit(BlockedBadBlocks,
4132 &rdev->flags);
4133 s->blocked_rdev = rdev;
4134 atomic_inc(&rdev->nr_pending);
4135 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004136 }
NeilBrown415e72d2010-06-17 17:25:21 +10004137 clear_bit(R5_Insync, &dev->flags);
4138 if (!rdev)
4139 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10004140 else if (is_bad) {
4141 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10004142 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4143 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10004144 /* treat as in-sync, but with a read error
4145 * which we can now try to correct
4146 */
4147 set_bit(R5_Insync, &dev->flags);
4148 set_bit(R5_ReadError, &dev->flags);
4149 }
4150 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10004151 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11004152 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10004153 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11004154 set_bit(R5_Insync, &dev->flags);
4155 else if (test_bit(R5_UPTODATE, &dev->flags) &&
4156 test_bit(R5_Expanded, &dev->flags))
4157 /* If we've reshaped into here, we assume it is Insync.
4158 * We will shortly update recovery_offset to make
4159 * it official.
4160 */
4161 set_bit(R5_Insync, &dev->flags);
4162
NeilBrown1cc03eb2014-01-06 13:19:42 +11004163 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004164 /* This flag does not apply to '.replacement'
4165 * only to .rdev, so make sure to check that*/
4166 struct md_rdev *rdev2 = rcu_dereference(
4167 conf->disks[i].rdev);
4168 if (rdev2 == rdev)
4169 clear_bit(R5_Insync, &dev->flags);
4170 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004171 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004172 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10004173 } else
4174 clear_bit(R5_WriteError, &dev->flags);
4175 }
NeilBrown1cc03eb2014-01-06 13:19:42 +11004176 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004177 /* This flag does not apply to '.replacement'
4178 * only to .rdev, so make sure to check that*/
4179 struct md_rdev *rdev2 = rcu_dereference(
4180 conf->disks[i].rdev);
4181 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10004182 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004183 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10004184 } else
4185 clear_bit(R5_MadeGood, &dev->flags);
4186 }
NeilBrown977df362011-12-23 10:17:53 +11004187 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4188 struct md_rdev *rdev2 = rcu_dereference(
4189 conf->disks[i].replacement);
4190 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4191 s->handle_bad_blocks = 1;
4192 atomic_inc(&rdev2->nr_pending);
4193 } else
4194 clear_bit(R5_MadeGoodRepl, &dev->flags);
4195 }
NeilBrown415e72d2010-06-17 17:25:21 +10004196 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004197 /* The ReadError flag will just be confusing now */
4198 clear_bit(R5_ReadError, &dev->flags);
4199 clear_bit(R5_ReWrite, &dev->flags);
4200 }
NeilBrown415e72d2010-06-17 17:25:21 +10004201 if (test_bit(R5_ReadError, &dev->flags))
4202 clear_bit(R5_Insync, &dev->flags);
4203 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004204 if (s->failed < 2)
4205 s->failed_num[s->failed] = i;
4206 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11004207 if (rdev && !test_bit(Faulty, &rdev->flags))
4208 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10004209 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004210 }
NeilBrown9a3e1102011-12-23 10:17:53 +11004211 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4212 /* If there is a failed device being replaced,
4213 * we must be recovering.
4214 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10004215 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11004216 * else we can only be replacing
4217 * sync and recovery both need to read all devices, and so
4218 * use the same flag.
4219 */
4220 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10004221 sh->sector >= conf->mddev->recovery_cp ||
4222 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11004223 s->syncing = 1;
4224 else
4225 s->replacing = 1;
4226 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004227 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10004228}
NeilBrownf4168852007-02-28 20:11:53 -08004229
shli@kernel.org59fc6302014-12-15 12:57:03 +11004230static int clear_batch_ready(struct stripe_head *sh)
4231{
NeilBrownb15a9db2015-05-22 15:20:04 +10004232 /* Return '1' if this is a member of batch, or
4233 * '0' if it is a lone stripe or a head which can now be
4234 * handled.
4235 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11004236 struct stripe_head *tmp;
4237 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
NeilBrownb15a9db2015-05-22 15:20:04 +10004238 return (sh->batch_head && sh->batch_head != sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11004239 spin_lock(&sh->stripe_lock);
4240 if (!sh->batch_head) {
4241 spin_unlock(&sh->stripe_lock);
4242 return 0;
4243 }
4244
4245 /*
4246 * this stripe could be added to a batch list before we check
4247 * BATCH_READY, skips it
4248 */
4249 if (sh->batch_head != sh) {
4250 spin_unlock(&sh->stripe_lock);
4251 return 1;
4252 }
4253 spin_lock(&sh->batch_lock);
4254 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4255 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4256 spin_unlock(&sh->batch_lock);
4257 spin_unlock(&sh->stripe_lock);
4258
4259 /*
4260 * BATCH_READY is cleared, no new stripes can be added.
4261 * batch_list can be accessed without lock
4262 */
4263 return 0;
4264}
4265
NeilBrown3960ce72015-05-21 12:20:36 +10004266static void break_stripe_batch_list(struct stripe_head *head_sh,
4267 unsigned long handle_flags)
shli@kernel.org72ac7332014-12-15 12:57:03 +11004268{
NeilBrown4e3d62f2015-05-21 11:50:16 +10004269 struct stripe_head *sh, *next;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004270 int i;
NeilBrownfb642b92015-05-21 12:00:47 +10004271 int do_wakeup = 0;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004272
NeilBrownbb270512015-05-08 18:19:40 +10004273 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4274
shli@kernel.org72ac7332014-12-15 12:57:03 +11004275 list_del_init(&sh->batch_list);
4276
Shaohua Lifb3229d2016-03-09 10:08:38 -08004277 WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
NeilBrown1b956f72015-05-21 12:40:26 +10004278 (1 << STRIPE_SYNCING) |
4279 (1 << STRIPE_REPLACED) |
NeilBrown1b956f72015-05-21 12:40:26 +10004280 (1 << STRIPE_DELAYED) |
4281 (1 << STRIPE_BIT_DELAY) |
4282 (1 << STRIPE_FULL_WRITE) |
4283 (1 << STRIPE_BIOFILL_RUN) |
4284 (1 << STRIPE_COMPUTE_RUN) |
4285 (1 << STRIPE_OPS_REQ_PENDING) |
4286 (1 << STRIPE_DISCARD) |
4287 (1 << STRIPE_BATCH_READY) |
4288 (1 << STRIPE_BATCH_ERR) |
Shaohua Lifb3229d2016-03-09 10:08:38 -08004289 (1 << STRIPE_BITMAP_PENDING)),
4290 "stripe state: %lx\n", sh->state);
4291 WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4292 (1 << STRIPE_REPLACED)),
4293 "head stripe state: %lx\n", head_sh->state);
NeilBrown1b956f72015-05-21 12:40:26 +10004294
4295 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
NeilBrown550da242016-03-09 12:58:25 +11004296 (1 << STRIPE_PREREAD_ACTIVE) |
Dennis Yang49c2b832017-09-06 11:02:35 +08004297 (1 << STRIPE_DEGRADED) |
4298 (1 << STRIPE_ON_UNPLUG_LIST)),
NeilBrown1b956f72015-05-21 12:40:26 +10004299 head_sh->state & (1 << STRIPE_INSYNC));
4300
shli@kernel.org72ac7332014-12-15 12:57:03 +11004301 sh->check_state = head_sh->check_state;
4302 sh->reconstruct_state = head_sh->reconstruct_state;
NeilBrownfb642b92015-05-21 12:00:47 +10004303 for (i = 0; i < sh->disks; i++) {
4304 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4305 do_wakeup = 1;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004306 sh->dev[i].flags = head_sh->dev[i].flags &
4307 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
NeilBrownfb642b92015-05-21 12:00:47 +10004308 }
shli@kernel.org72ac7332014-12-15 12:57:03 +11004309 spin_lock_irq(&sh->stripe_lock);
4310 sh->batch_head = NULL;
4311 spin_unlock_irq(&sh->stripe_lock);
NeilBrown3960ce72015-05-21 12:20:36 +10004312 if (handle_flags == 0 ||
4313 sh->state & handle_flags)
4314 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07004315 raid5_release_stripe(sh);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004316 }
NeilBrownfb642b92015-05-21 12:00:47 +10004317 spin_lock_irq(&head_sh->stripe_lock);
4318 head_sh->batch_head = NULL;
4319 spin_unlock_irq(&head_sh->stripe_lock);
4320 for (i = 0; i < head_sh->disks; i++)
4321 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4322 do_wakeup = 1;
NeilBrown3960ce72015-05-21 12:20:36 +10004323 if (head_sh->state & handle_flags)
4324 set_bit(STRIPE_HANDLE, &head_sh->state);
NeilBrownfb642b92015-05-21 12:00:47 +10004325
4326 if (do_wakeup)
4327 wake_up(&head_sh->raid_conf->wait_for_overlap);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004328}
4329
NeilBrowncc940152011-07-26 11:35:35 +10004330static void handle_stripe(struct stripe_head *sh)
4331{
4332 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11004333 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10004334 int i;
NeilBrown84789552011-07-27 11:00:36 +10004335 int prexor;
4336 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004337 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10004338
4339 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11004340 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10004341 /* already being handled, ensure it gets handled
4342 * again when current action finishes */
4343 set_bit(STRIPE_HANDLE, &sh->state);
4344 return;
4345 }
4346
shli@kernel.org59fc6302014-12-15 12:57:03 +11004347 if (clear_batch_ready(sh) ) {
4348 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
4349 return;
4350 }
4351
NeilBrown4e3d62f2015-05-21 11:50:16 +10004352 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
NeilBrown3960ce72015-05-21 12:20:36 +10004353 break_stripe_batch_list(sh, 0);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004354
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004355 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
NeilBrownf8dfcff2013-03-12 12:18:06 +11004356 spin_lock(&sh->stripe_lock);
4357 /* Cannot process 'sync' concurrently with 'discard' */
4358 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
4359 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4360 set_bit(STRIPE_SYNCING, &sh->state);
4361 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10004362 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004363 }
4364 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10004365 }
4366 clear_bit(STRIPE_DELAYED, &sh->state);
4367
4368 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4369 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4370 (unsigned long long)sh->sector, sh->state,
4371 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4372 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10004373
NeilBrownacfe7262011-07-27 11:00:36 +10004374 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10004375
Shaohua Lib70abcb2015-08-13 14:31:58 -07004376 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
4377 goto finish;
4378
NeilBrownbc2607f2011-07-28 11:39:22 +10004379 if (s.handle_bad_blocks) {
4380 set_bit(STRIPE_HANDLE, &sh->state);
4381 goto finish;
4382 }
4383
NeilBrown474af965fe2011-07-27 11:00:36 +10004384 if (unlikely(s.blocked_rdev)) {
4385 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11004386 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10004387 set_bit(STRIPE_HANDLE, &sh->state);
4388 goto finish;
4389 }
4390 /* There is nothing for the blocked_rdev to block */
4391 rdev_dec_pending(s.blocked_rdev, conf->mddev);
4392 s.blocked_rdev = NULL;
4393 }
4394
4395 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4396 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4397 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4398 }
4399
4400 pr_debug("locked=%d uptodate=%d to_read=%d"
4401 " to_write=%d failed=%d failed_num=%d,%d\n",
4402 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4403 s.failed_num[0], s.failed_num[1]);
4404 /* check if the array has lost more than max_degraded devices and,
4405 * if so, some requests might need to be failed.
4406 */
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004407 if (s.failed > conf->max_degraded || s.log_failed) {
NeilBrown9a3f5302011-11-08 16:22:01 +11004408 sh->check_state = 0;
4409 sh->reconstruct_state = 0;
NeilBrown626f2092015-05-22 14:03:10 +10004410 break_stripe_batch_list(sh, 0);
NeilBrown9a3f5302011-11-08 16:22:01 +11004411 if (s.to_read+s.to_write+s.written)
4412 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11004413 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11004414 handle_failed_sync(conf, sh, &s);
4415 }
NeilBrown474af965fe2011-07-27 11:00:36 +10004416
NeilBrown84789552011-07-27 11:00:36 +10004417 /* Now we check to see if any write operations have recently
4418 * completed
4419 */
4420 prexor = 0;
4421 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4422 prexor = 1;
4423 if (sh->reconstruct_state == reconstruct_state_drain_result ||
4424 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
4425 sh->reconstruct_state = reconstruct_state_idle;
4426
4427 /* All the 'written' buffers and the parity block are ready to
4428 * be written back to disk
4429 */
Shaohua Li9e4447682012-10-11 13:49:49 +11004430 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
4431 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004432 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11004433 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
4434 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004435 for (i = disks; i--; ) {
4436 struct r5dev *dev = &sh->dev[i];
4437 if (test_bit(R5_LOCKED, &dev->flags) &&
4438 (i == sh->pd_idx || i == sh->qd_idx ||
4439 dev->written)) {
4440 pr_debug("Writing block %d\n", i);
4441 set_bit(R5_Wantwrite, &dev->flags);
4442 if (prexor)
4443 continue;
NeilBrown9c4bdf62014-08-13 09:57:07 +10004444 if (s.failed > 1)
4445 continue;
NeilBrown84789552011-07-27 11:00:36 +10004446 if (!test_bit(R5_Insync, &dev->flags) ||
4447 ((i == sh->pd_idx || i == sh->qd_idx) &&
4448 s.failed == 0))
4449 set_bit(STRIPE_INSYNC, &sh->state);
4450 }
4451 }
4452 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4453 s.dec_preread_active = 1;
4454 }
4455
NeilBrownef5b7c62012-11-22 09:13:36 +11004456 /*
4457 * might be able to return some write requests if the parity blocks
4458 * are safe, or on a failed drive
4459 */
4460 pdev = &sh->dev[sh->pd_idx];
4461 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
4462 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
4463 qdev = &sh->dev[sh->qd_idx];
4464 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
4465 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
4466 || conf->level < 6;
4467
4468 if (s.written &&
4469 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
4470 && !test_bit(R5_LOCKED, &pdev->flags)
4471 && (test_bit(R5_UPTODATE, &pdev->flags) ||
4472 test_bit(R5_Discard, &pdev->flags))))) &&
4473 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
4474 && !test_bit(R5_LOCKED, &qdev->flags)
4475 && (test_bit(R5_UPTODATE, &qdev->flags) ||
4476 test_bit(R5_Discard, &qdev->flags))))))
4477 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
4478
4479 /* Now we might consider reading some blocks, either to check/generate
4480 * parity, or to satisfy requests
4481 * or to load a block that is being partially written.
4482 */
4483 if (s.to_read || s.non_overwrite
4484 || (conf->level == 6 && s.to_write && s.failed)
4485 || (s.syncing && (s.uptodate + s.compute < disks))
4486 || s.replacing
4487 || s.expanding)
4488 handle_stripe_fill(sh, &s, disks);
4489
NeilBrown84789552011-07-27 11:00:36 +10004490 /* Now to consider new write requests and what else, if anything
4491 * should be read. We do not handle new writes when:
4492 * 1/ A 'write' operation (copy+xor) is already in flight.
4493 * 2/ A 'check' operation is in flight, as it may clobber the parity
4494 * block.
4495 */
4496 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
4497 handle_stripe_dirtying(conf, sh, &s, disks);
4498
4499 /* maybe we need to check and possibly fix the parity for this stripe
4500 * Any reads will already have been scheduled, so we just see if enough
4501 * data is available. The parity check is held off while parity
4502 * dependent operations are in flight.
4503 */
4504 if (sh->check_state ||
4505 (s.syncing && s.locked == 0 &&
4506 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
4507 !test_bit(STRIPE_INSYNC, &sh->state))) {
4508 if (conf->level == 6)
4509 handle_parity_checks6(conf, sh, &s, disks);
4510 else
4511 handle_parity_checks5(conf, sh, &s, disks);
4512 }
NeilBrownc5a31002011-07-27 11:00:36 +10004513
NeilBrownf94c0b62013-07-22 12:57:21 +10004514 if ((s.replacing || s.syncing) && s.locked == 0
4515 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
4516 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11004517 /* Write out to replacement devices where possible */
4518 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10004519 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
4520 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11004521 set_bit(R5_WantReplace, &sh->dev[i].flags);
4522 set_bit(R5_LOCKED, &sh->dev[i].flags);
4523 s.locked++;
4524 }
NeilBrownf94c0b62013-07-22 12:57:21 +10004525 if (s.replacing)
4526 set_bit(STRIPE_INSYNC, &sh->state);
4527 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11004528 }
4529 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10004530 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11004531 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10004532 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4533 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004534 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
4535 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10004536 }
4537
4538 /* If the failed drives are just a ReadError, then we might need
4539 * to progress the repair/check process
4540 */
4541 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
4542 for (i = 0; i < s.failed; i++) {
4543 struct r5dev *dev = &sh->dev[s.failed_num[i]];
4544 if (test_bit(R5_ReadError, &dev->flags)
4545 && !test_bit(R5_LOCKED, &dev->flags)
4546 && test_bit(R5_UPTODATE, &dev->flags)
4547 ) {
4548 if (!test_bit(R5_ReWrite, &dev->flags)) {
4549 set_bit(R5_Wantwrite, &dev->flags);
4550 set_bit(R5_ReWrite, &dev->flags);
4551 set_bit(R5_LOCKED, &dev->flags);
4552 s.locked++;
4553 } else {
4554 /* let's read it back */
4555 set_bit(R5_Wantread, &dev->flags);
4556 set_bit(R5_LOCKED, &dev->flags);
4557 s.locked++;
4558 }
4559 }
4560 }
4561
NeilBrown3687c062011-07-27 11:00:36 +10004562 /* Finish reconstruct operations initiated by the expansion process */
4563 if (sh->reconstruct_state == reconstruct_state_result) {
4564 struct stripe_head *sh_src
Shaohua Li6d036f72015-08-13 14:31:57 -07004565 = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrown3687c062011-07-27 11:00:36 +10004566 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
4567 /* sh cannot be written until sh_src has been read.
4568 * so arrange for sh to be delayed a little
4569 */
4570 set_bit(STRIPE_DELAYED, &sh->state);
4571 set_bit(STRIPE_HANDLE, &sh->state);
4572 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
4573 &sh_src->state))
4574 atomic_inc(&conf->preread_active_stripes);
Shaohua Li6d036f72015-08-13 14:31:57 -07004575 raid5_release_stripe(sh_src);
NeilBrown3687c062011-07-27 11:00:36 +10004576 goto finish;
4577 }
4578 if (sh_src)
Shaohua Li6d036f72015-08-13 14:31:57 -07004579 raid5_release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07004580
NeilBrown3687c062011-07-27 11:00:36 +10004581 sh->reconstruct_state = reconstruct_state_idle;
4582 clear_bit(STRIPE_EXPANDING, &sh->state);
4583 for (i = conf->raid_disks; i--; ) {
4584 set_bit(R5_Wantwrite, &sh->dev[i].flags);
4585 set_bit(R5_LOCKED, &sh->dev[i].flags);
4586 s.locked++;
4587 }
4588 }
4589
4590 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
4591 !sh->reconstruct_state) {
4592 /* Need to write out all blocks after computing parity */
4593 sh->disks = conf->raid_disks;
4594 stripe_set_idx(sh->sector, conf, 0, sh);
4595 schedule_reconstruction(sh, &s, 1, 1);
4596 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
4597 clear_bit(STRIPE_EXPAND_READY, &sh->state);
4598 atomic_dec(&conf->reshape_stripes);
4599 wake_up(&conf->wait_for_overlap);
4600 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4601 }
4602
4603 if (s.expanding && s.locked == 0 &&
4604 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
4605 handle_stripe_expansion(conf, sh);
4606
4607finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07004608 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10004609 if (unlikely(s.blocked_rdev)) {
4610 if (conf->mddev->external)
4611 md_wait_for_blocked_rdev(s.blocked_rdev,
4612 conf->mddev);
4613 else
4614 /* Internal metadata will immediately
4615 * be written by raid5d, so we don't
4616 * need to wait here.
4617 */
4618 rdev_dec_pending(s.blocked_rdev,
4619 conf->mddev);
4620 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004621
NeilBrownbc2607f2011-07-28 11:39:22 +10004622 if (s.handle_bad_blocks)
4623 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004624 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10004625 struct r5dev *dev = &sh->dev[i];
4626 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
4627 /* We own a safe reference to the rdev */
4628 rdev = conf->disks[i].rdev;
4629 if (!rdev_set_badblocks(rdev, sh->sector,
4630 STRIPE_SECTORS, 0))
4631 md_error(conf->mddev, rdev);
4632 rdev_dec_pending(rdev, conf->mddev);
4633 }
NeilBrownb84db562011-07-28 11:39:23 +10004634 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
4635 rdev = conf->disks[i].rdev;
4636 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004637 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10004638 rdev_dec_pending(rdev, conf->mddev);
4639 }
NeilBrown977df362011-12-23 10:17:53 +11004640 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
4641 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11004642 if (!rdev)
4643 /* rdev have been moved down */
4644 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11004645 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004646 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11004647 rdev_dec_pending(rdev, conf->mddev);
4648 }
NeilBrownbc2607f2011-07-28 11:39:22 +10004649 }
4650
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004651 if (s.ops_request)
4652 raid_run_ops(sh, s.ops_request);
4653
Dan Williamsf0e43bc2008-06-28 08:31:55 +10004654 ops_run_io(sh, &s);
4655
NeilBrownc5709ef2011-07-26 11:35:20 +10004656 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11004657 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02004658 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11004659 * have actually been submitted.
4660 */
4661 atomic_dec(&conf->preread_active_stripes);
4662 if (atomic_read(&conf->preread_active_stripes) <
4663 IO_THRESHOLD)
4664 md_wakeup_thread(conf->mddev->thread);
4665 }
4666
NeilBrownc3cce6c2015-08-14 12:47:33 +10004667 if (!bio_list_empty(&s.return_bi)) {
Alexey Obitotskiy11367792016-08-03 10:02:56 +02004668 if (test_bit(MD_CHANGE_PENDING, &conf->mddev->flags) &&
4669 (s.failed <= conf->max_degraded ||
4670 conf->mddev->external == 0)) {
NeilBrownc3cce6c2015-08-14 12:47:33 +10004671 spin_lock_irq(&conf->device_lock);
4672 bio_list_merge(&conf->return_bi, &s.return_bi);
4673 spin_unlock_irq(&conf->device_lock);
4674 md_wakeup_thread(conf->mddev->thread);
4675 } else
4676 return_io(&s.return_bi);
4677 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004678
Dan Williams257a4b42011-11-08 16:22:06 +11004679 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07004680}
4681
NeilBrownd1688a62011-10-11 16:49:52 +11004682static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683{
4684 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
4685 while (!list_empty(&conf->delayed_list)) {
4686 struct list_head *l = conf->delayed_list.next;
4687 struct stripe_head *sh;
4688 sh = list_entry(l, struct stripe_head, lru);
4689 list_del_init(l);
4690 clear_bit(STRIPE_DELAYED, &sh->state);
4691 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4692 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004693 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08004694 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 }
NeilBrown482c0832011-04-18 18:25:42 +10004696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697}
4698
Shaohua Li566c09c2013-11-14 15:16:17 +11004699static void activate_bit_delay(struct r5conf *conf,
4700 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07004701{
4702 /* device_lock is held */
4703 struct list_head head;
4704 list_add(&head, &conf->bitmap_list);
4705 list_del_init(&conf->bitmap_list);
4706 while (!list_empty(&head)) {
4707 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11004708 int hash;
NeilBrown72626682005-09-09 16:23:54 -07004709 list_del_init(&sh->lru);
4710 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11004711 hash = sh->hash_lock_index;
4712 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07004713 }
4714}
4715
NeilBrown5c675f82014-12-15 12:56:56 +11004716static int raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07004717{
NeilBrownd1688a62011-10-11 16:49:52 +11004718 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07004719
4720 /* No difference between reads and writes. Just check
4721 * how busy the stripe_cache is
4722 */
NeilBrown3fa841d2009-09-23 18:10:29 +10004723
NeilBrown54233992015-02-26 12:21:04 +11004724 if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state))
NeilBrownf022b2f2006-10-03 01:15:56 -07004725 return 1;
4726 if (conf->quiesce)
4727 return 1;
Shaohua Li4bda5562013-11-14 15:16:17 +11004728 if (atomic_read(&conf->empty_inactive_list_nr))
NeilBrownf022b2f2006-10-03 01:15:56 -07004729 return 1;
4730
4731 return 0;
4732}
4733
NeilBrownfd01b882011-10-11 16:47:53 +11004734static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004735{
NeilBrown3cb5edf2015-07-15 17:24:17 +10004736 struct r5conf *conf = mddev->private;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004737 sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev);
NeilBrown3cb5edf2015-07-15 17:24:17 +10004738 unsigned int chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004739 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004740
NeilBrown3cb5edf2015-07-15 17:24:17 +10004741 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004742 return chunk_sectors >=
4743 ((sector & (chunk_sectors - 1)) + bio_sectors);
4744}
4745
4746/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004747 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4748 * later sampled by raid5d.
4749 */
NeilBrownd1688a62011-10-11 16:49:52 +11004750static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004751{
4752 unsigned long flags;
4753
4754 spin_lock_irqsave(&conf->device_lock, flags);
4755
4756 bi->bi_next = conf->retry_read_aligned_list;
4757 conf->retry_read_aligned_list = bi;
4758
4759 spin_unlock_irqrestore(&conf->device_lock, flags);
4760 md_wakeup_thread(conf->mddev->thread);
4761}
4762
NeilBrownd1688a62011-10-11 16:49:52 +11004763static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004764{
4765 struct bio *bi;
4766
4767 bi = conf->retry_read_aligned;
4768 if (bi) {
4769 conf->retry_read_aligned = NULL;
4770 return bi;
4771 }
4772 bi = conf->retry_read_aligned_list;
4773 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08004774 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004775 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02004776 /*
4777 * this sets the active strip count to 1 and the processed
4778 * strip count to zero (upper 8 bits)
4779 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004780 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004781 }
4782
4783 return bi;
4784}
4785
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004786/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004787 * The "raid5_align_endio" should check if the read succeeded and if it
4788 * did, call bio_endio on the original bio (having bio_put the new bio
4789 * first).
4790 * If the read failed..
4791 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004792static void raid5_align_endio(struct bio *bi)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004793{
4794 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11004795 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004796 struct r5conf *conf;
NeilBrown3cb03002011-10-11 16:45:26 +11004797 struct md_rdev *rdev;
Sasha Levin9b81c842015-08-10 19:05:18 -04004798 int error = bi->bi_error;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004799
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004800 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004801
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004802 rdev = (void*)raid_bi->bi_next;
4803 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11004804 mddev = rdev->mddev;
4805 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004806
4807 rdev_dec_pending(rdev, conf->mddev);
4808
Sasha Levin9b81c842015-08-10 19:05:18 -04004809 if (!error) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004810 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
4811 raid_bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004812 bio_endio(raid_bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004813 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10004814 wake_up(&conf->wait_for_quiescent);
NeilBrown6712ecf2007-09-27 12:47:43 +02004815 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004816 }
4817
Dan Williams45b42332007-07-09 11:56:43 -07004818 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004819
4820 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004821}
4822
Ming Lin7ef6b122015-05-06 22:51:24 -07004823static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004824{
NeilBrownd1688a62011-10-11 16:49:52 +11004825 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11004826 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004827 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11004828 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11004829 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004830
4831 if (!in_chunk_boundary(mddev, raid_bio)) {
Ming Lin7ef6b122015-05-06 22:51:24 -07004832 pr_debug("%s: non aligned\n", __func__);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004833 return 0;
4834 }
4835 /*
NeilBrowna167f662010-10-26 18:31:13 +11004836 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004837 */
NeilBrowna167f662010-10-26 18:31:13 +11004838 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004839 if (!align_bi)
4840 return 0;
4841 /*
4842 * set bi_end_io to a new function, and set bi_private to the
4843 * original bio.
4844 */
4845 align_bi->bi_end_io = raid5_align_endio;
4846 align_bi->bi_private = raid_bio;
4847 /*
4848 * compute position
4849 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07004850 align_bi->bi_iter.bi_sector =
4851 raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
4852 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004853
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004854 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004855 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11004856 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4857 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4858 rdev->recovery_offset < end_sector) {
4859 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4860 if (rdev &&
4861 (test_bit(Faulty, &rdev->flags) ||
4862 !(test_bit(In_sync, &rdev->flags) ||
4863 rdev->recovery_offset >= end_sector)))
4864 rdev = NULL;
4865 }
4866 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10004867 sector_t first_bad;
4868 int bad_sectors;
4869
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004870 atomic_inc(&rdev->nr_pending);
4871 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004872 raid_bio->bi_next = (void*)rdev;
4873 align_bi->bi_bdev = rdev->bdev;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06004874 bio_clear_flag(align_bi, BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004875
Kent Overstreet7140aaf2013-09-25 13:37:01 -07004876 if (is_badblock(rdev, align_bi->bi_iter.bi_sector,
Kent Overstreet4f024f32013-10-11 15:44:27 -07004877 bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10004878 &first_bad, &bad_sectors)) {
Neil Brown387bb172007-02-08 14:20:29 -08004879 bio_put(align_bi);
4880 rdev_dec_pending(rdev, mddev);
4881 return 0;
4882 }
4883
majianpeng6c0544e2012-06-12 08:31:10 +08004884 /* No reshape active, so we can trust rdev->data_offset */
Kent Overstreet4f024f32013-10-11 15:44:27 -07004885 align_bi->bi_iter.bi_sector += rdev->data_offset;
majianpeng6c0544e2012-06-12 08:31:10 +08004886
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004887 spin_lock_irq(&conf->device_lock);
Yuanhan Liub1b46482015-05-08 18:19:06 +10004888 wait_event_lock_irq(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004889 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004890 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004891 atomic_inc(&conf->active_aligned_reads);
4892 spin_unlock_irq(&conf->device_lock);
4893
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004894 if (mddev->gendisk)
4895 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4896 align_bi, disk_devt(mddev->gendisk),
Kent Overstreet4f024f32013-10-11 15:44:27 -07004897 raid_bio->bi_iter.bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004898 generic_make_request(align_bi);
4899 return 1;
4900 } else {
4901 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004902 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004903 return 0;
4904 }
4905}
4906
Ming Lin7ef6b122015-05-06 22:51:24 -07004907static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
4908{
4909 struct bio *split;
4910
4911 do {
4912 sector_t sector = raid_bio->bi_iter.bi_sector;
4913 unsigned chunk_sects = mddev->chunk_sectors;
4914 unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
4915
4916 if (sectors < bio_sectors(raid_bio)) {
4917 split = bio_split(raid_bio, sectors, GFP_NOIO, fs_bio_set);
4918 bio_chain(split, raid_bio);
4919 } else
4920 split = raid_bio;
4921
4922 if (!raid5_read_one_chunk(mddev, split)) {
4923 if (split != raid_bio)
4924 generic_make_request(raid_bio);
4925 return split;
4926 }
4927 } while (split != raid_bio);
4928
4929 return NULL;
4930}
4931
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004932/* __get_priority_stripe - get the next stripe to process
4933 *
4934 * Full stripe writes are allowed to pass preread active stripes up until
4935 * the bypass_threshold is exceeded. In general the bypass_count
4936 * increments when the handle_list is handled before the hold_list; however, it
4937 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4938 * stripe with in flight i/o. The bypass_count will be reset when the
4939 * head of the hold_list has changed, i.e. the head was promoted to the
4940 * handle_list.
4941 */
Shaohua Li851c30c2013-08-28 14:30:16 +08004942static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004943{
Shaohua Li851c30c2013-08-28 14:30:16 +08004944 struct stripe_head *sh = NULL, *tmp;
4945 struct list_head *handle_list = NULL;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004946 struct r5worker_group *wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08004947
4948 if (conf->worker_cnt_per_group == 0) {
4949 handle_list = &conf->handle_list;
4950 } else if (group != ANY_GROUP) {
4951 handle_list = &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004952 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08004953 } else {
4954 int i;
4955 for (i = 0; i < conf->group_cnt; i++) {
4956 handle_list = &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004957 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08004958 if (!list_empty(handle_list))
4959 break;
4960 }
4961 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004962
4963 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4964 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08004965 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004966 list_empty(&conf->hold_list) ? "empty" : "busy",
4967 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4968
Shaohua Li851c30c2013-08-28 14:30:16 +08004969 if (!list_empty(handle_list)) {
4970 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004971
4972 if (list_empty(&conf->hold_list))
4973 conf->bypass_count = 0;
4974 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4975 if (conf->hold_list.next == conf->last_hold)
4976 conf->bypass_count++;
4977 else {
4978 conf->last_hold = conf->hold_list.next;
4979 conf->bypass_count -= conf->bypass_threshold;
4980 if (conf->bypass_count < 0)
4981 conf->bypass_count = 0;
4982 }
4983 }
4984 } else if (!list_empty(&conf->hold_list) &&
4985 ((conf->bypass_threshold &&
4986 conf->bypass_count > conf->bypass_threshold) ||
4987 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08004988
4989 list_for_each_entry(tmp, &conf->hold_list, lru) {
4990 if (conf->worker_cnt_per_group == 0 ||
4991 group == ANY_GROUP ||
4992 !cpu_online(tmp->cpu) ||
4993 cpu_to_group(tmp->cpu) == group) {
4994 sh = tmp;
4995 break;
4996 }
4997 }
4998
4999 if (sh) {
5000 conf->bypass_count -= conf->bypass_threshold;
5001 if (conf->bypass_count < 0)
5002 conf->bypass_count = 0;
5003 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08005004 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005005 }
5006
5007 if (!sh)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005008 return NULL;
5009
Shaohua Libfc90cb2013-08-29 15:40:32 +08005010 if (wg) {
5011 wg->stripes_cnt--;
5012 sh->group = NULL;
5013 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005014 list_del_init(&sh->lru);
Shaohua Lic7a6d352014-04-15 09:12:54 +08005015 BUG_ON(atomic_inc_return(&sh->count) != 1);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005016 return sh;
5017}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005018
Shaohua Li8811b592012-08-02 08:33:00 +10005019struct raid5_plug_cb {
5020 struct blk_plug_cb cb;
5021 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11005022 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10005023};
5024
5025static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
5026{
5027 struct raid5_plug_cb *cb = container_of(
5028 blk_cb, struct raid5_plug_cb, cb);
5029 struct stripe_head *sh;
5030 struct mddev *mddev = cb->cb.data;
5031 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11005032 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11005033 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10005034
5035 if (cb->list.next && !list_empty(&cb->list)) {
5036 spin_lock_irq(&conf->device_lock);
5037 while (!list_empty(&cb->list)) {
5038 sh = list_first_entry(&cb->list, struct stripe_head, lru);
5039 list_del_init(&sh->lru);
5040 /*
5041 * avoid race release_stripe_plug() sees
5042 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5043 * is still in our list
5044 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005045 smp_mb__before_atomic();
Shaohua Li8811b592012-08-02 08:33:00 +10005046 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08005047 /*
5048 * STRIPE_ON_RELEASE_LIST could be set here. In that
5049 * case, the count is always > 1 here
5050 */
Shaohua Li566c09c2013-11-14 15:16:17 +11005051 hash = sh->hash_lock_index;
5052 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11005053 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10005054 }
5055 spin_unlock_irq(&conf->device_lock);
5056 }
Shaohua Li566c09c2013-11-14 15:16:17 +11005057 release_inactive_stripe_list(conf, cb->temp_inactive_list,
5058 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005059 if (mddev->queue)
5060 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10005061 kfree(cb);
5062}
5063
5064static void release_stripe_plug(struct mddev *mddev,
5065 struct stripe_head *sh)
5066{
5067 struct blk_plug_cb *blk_cb = blk_check_plugged(
5068 raid5_unplug, mddev,
5069 sizeof(struct raid5_plug_cb));
5070 struct raid5_plug_cb *cb;
5071
5072 if (!blk_cb) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005073 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005074 return;
5075 }
5076
5077 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
5078
Shaohua Li566c09c2013-11-14 15:16:17 +11005079 if (cb->list.next == NULL) {
5080 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10005081 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11005082 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5083 INIT_LIST_HEAD(cb->temp_inactive_list + i);
5084 }
Shaohua Li8811b592012-08-02 08:33:00 +10005085
5086 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
5087 list_add_tail(&sh->lru, &cb->list);
5088 else
Shaohua Li6d036f72015-08-13 14:31:57 -07005089 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005090}
5091
Shaohua Li620125f2012-10-11 13:49:05 +11005092static void make_discard_request(struct mddev *mddev, struct bio *bi)
5093{
5094 struct r5conf *conf = mddev->private;
5095 sector_t logical_sector, last_sector;
5096 struct stripe_head *sh;
5097 int remaining;
5098 int stripe_sectors;
5099
5100 if (mddev->reshape_position != MaxSector)
5101 /* Skip discard while reshape is happening */
5102 return;
5103
Kent Overstreet4f024f32013-10-11 15:44:27 -07005104 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
5105 last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9);
Shaohua Li620125f2012-10-11 13:49:05 +11005106
5107 bi->bi_next = NULL;
5108 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
5109
5110 stripe_sectors = conf->chunk_sectors *
5111 (conf->raid_disks - conf->max_degraded);
5112 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5113 stripe_sectors);
5114 sector_div(last_sector, stripe_sectors);
5115
5116 logical_sector *= conf->chunk_sectors;
5117 last_sector *= conf->chunk_sectors;
5118
5119 for (; logical_sector < last_sector;
5120 logical_sector += STRIPE_SECTORS) {
5121 DEFINE_WAIT(w);
5122 int d;
5123 again:
Shaohua Li6d036f72015-08-13 14:31:57 -07005124 sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
Shaohua Li620125f2012-10-11 13:49:05 +11005125 prepare_to_wait(&conf->wait_for_overlap, &w,
5126 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005127 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5128 if (test_bit(STRIPE_SYNCING, &sh->state)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005129 raid5_release_stripe(sh);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005130 schedule();
5131 goto again;
5132 }
5133 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11005134 spin_lock_irq(&sh->stripe_lock);
5135 for (d = 0; d < conf->raid_disks; d++) {
5136 if (d == sh->pd_idx || d == sh->qd_idx)
5137 continue;
5138 if (sh->dev[d].towrite || sh->dev[d].toread) {
5139 set_bit(R5_Overlap, &sh->dev[d].flags);
5140 spin_unlock_irq(&sh->stripe_lock);
Shaohua Li6d036f72015-08-13 14:31:57 -07005141 raid5_release_stripe(sh);
Shaohua Li620125f2012-10-11 13:49:05 +11005142 schedule();
5143 goto again;
5144 }
5145 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11005146 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11005147 finish_wait(&conf->wait_for_overlap, &w);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005148 sh->overwrite_disks = 0;
Shaohua Li620125f2012-10-11 13:49:05 +11005149 for (d = 0; d < conf->raid_disks; d++) {
5150 if (d == sh->pd_idx || d == sh->qd_idx)
5151 continue;
5152 sh->dev[d].towrite = bi;
5153 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
5154 raid5_inc_bi_active_stripes(bi);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005155 sh->overwrite_disks++;
Shaohua Li620125f2012-10-11 13:49:05 +11005156 }
5157 spin_unlock_irq(&sh->stripe_lock);
5158 if (conf->mddev->bitmap) {
5159 for (d = 0;
5160 d < conf->raid_disks - conf->max_degraded;
5161 d++)
5162 bitmap_startwrite(mddev->bitmap,
5163 sh->sector,
5164 STRIPE_SECTORS,
5165 0);
5166 sh->bm_seq = conf->seq_flush + 1;
5167 set_bit(STRIPE_BIT_DELAY, &sh->state);
5168 }
5169
5170 set_bit(STRIPE_HANDLE, &sh->state);
5171 clear_bit(STRIPE_DELAYED, &sh->state);
5172 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5173 atomic_inc(&conf->preread_active_stripes);
5174 release_stripe_plug(mddev, sh);
5175 }
5176
5177 remaining = raid5_dec_bi_active_stripes(bi);
5178 if (remaining == 0) {
5179 md_write_end(mddev);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005180 bio_endio(bi);
Shaohua Li620125f2012-10-11 13:49:05 +11005181 }
5182}
5183
Shaohua Li849674e2016-01-20 13:52:20 -08005184static void raid5_make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185{
NeilBrownd1688a62011-10-11 16:49:52 +11005186 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11005187 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 sector_t new_sector;
5189 sector_t logical_sector, last_sector;
5190 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01005191 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11005192 int remaining;
Shaohua Li27c0f682014-04-09 11:25:47 +08005193 DEFINE_WAIT(w);
5194 bool do_prepare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195
Jens Axboe1eff9d32016-08-05 15:35:16 -06005196 if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
Shaohua Li828cbe92015-09-02 13:49:49 -07005197 int ret = r5l_handle_flush_request(conf->log, bi);
5198
5199 if (ret == 0)
5200 return;
5201 if (ret == -ENODEV) {
5202 md_flush_request(mddev, bi);
5203 return;
5204 }
5205 /* ret == -EAGAIN, fallback */
NeilBrowne5dcdd82005-09-09 16:23:41 -07005206 }
5207
NeilBrown3d310eb2005-06-21 17:17:26 -07005208 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07005209
Eric Mei9ffc8f72015-03-18 23:39:11 -06005210 /*
5211 * If array is degraded, better not do chunk aligned read because
5212 * later we might have to read it again in order to reconstruct
5213 * data on failed drives.
5214 */
5215 if (rw == READ && mddev->degraded == 0 &&
Ming Lin7ef6b122015-05-06 22:51:24 -07005216 mddev->reshape_position == MaxSector) {
5217 bi = chunk_aligned_read(mddev, bi);
5218 if (!bi)
5219 return;
5220 }
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08005221
Mike Christie796a5cf2016-06-05 14:32:07 -05005222 if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
Shaohua Li620125f2012-10-11 13:49:05 +11005223 make_discard_request(mddev, bi);
5224 return;
5225 }
5226
Kent Overstreet4f024f32013-10-11 15:44:27 -07005227 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005228 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 bi->bi_next = NULL;
5230 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07005231
Shaohua Li27c0f682014-04-09 11:25:47 +08005232 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005234 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10005235 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08005236
Shaohua Li27c0f682014-04-09 11:25:47 +08005237 do_prepare = false;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005238 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10005239 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11005240 previous = 0;
Shaohua Li27c0f682014-04-09 11:25:47 +08005241 if (do_prepare)
5242 prepare_to_wait(&conf->wait_for_overlap, &w,
5243 TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005244 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11005245 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08005246 * 64bit on a 32bit platform, and so it might be
5247 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02005248 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08005249 * the lock is dropped, so once we get a reference
5250 * to the stripe that we think it is, we will have
5251 * to check again.
5252 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005253 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005254 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005255 ? logical_sector < conf->reshape_progress
5256 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005257 previous = 1;
5258 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10005259 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005260 ? logical_sector < conf->reshape_safe
5261 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08005262 spin_unlock_irq(&conf->device_lock);
5263 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005264 do_prepare = true;
NeilBrownb578d552006-03-27 01:18:12 -08005265 goto retry;
5266 }
5267 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005268 spin_unlock_irq(&conf->device_lock);
5269 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005270
NeilBrown112bf892009-03-31 14:39:38 +11005271 new_sector = raid5_compute_sector(conf, logical_sector,
5272 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11005273 &dd_idx, NULL);
Shaohua Li849674e2016-01-20 13:52:20 -08005274 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10005275 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 (unsigned long long)logical_sector);
5277
Shaohua Li6d036f72015-08-13 14:31:57 -07005278 sh = raid5_get_active_stripe(conf, new_sector, previous,
Jens Axboe1eff9d32016-08-05 15:35:16 -06005279 (bi->bi_opf & REQ_RAHEAD), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11005281 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005282 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08005283 * stripe, so we must do the range check again.
5284 * Expansion could still move past after this
5285 * test, but as we are holding a reference to
5286 * 'sh', we know that if that happens,
5287 * STRIPE_EXPANDING will get set and the expansion
5288 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005289 */
5290 int must_retry = 0;
5291 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005292 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11005293 ? logical_sector >= conf->reshape_progress
5294 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005295 /* mismatch, need to try again */
5296 must_retry = 1;
5297 spin_unlock_irq(&conf->device_lock);
5298 if (must_retry) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005299 raid5_release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07005300 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005301 do_prepare = true;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005302 goto retry;
5303 }
5304 }
NeilBrownc46501b2013-08-27 15:52:13 +10005305 if (read_seqcount_retry(&conf->gen_lock, seq)) {
5306 /* Might have got the wrong stripe_head
5307 * by accident
5308 */
Shaohua Li6d036f72015-08-13 14:31:57 -07005309 raid5_release_stripe(sh);
NeilBrownc46501b2013-08-27 15:52:13 +10005310 goto retry;
5311 }
NeilBrowne62e58a2009-07-01 13:15:35 +10005312
Namhyung Kimffd96e32011-07-18 17:38:51 +10005313 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10005314 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08005315 logical_sector < mddev->suspend_hi) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005316 raid5_release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10005317 /* As the suspend_* range is controlled by
5318 * userspace, we want an interruptible
5319 * wait.
5320 */
NeilBrowne62e58a2009-07-01 13:15:35 +10005321 prepare_to_wait(&conf->wait_for_overlap,
5322 &w, TASK_INTERRUPTIBLE);
5323 if (logical_sector >= mddev->suspend_lo &&
Shaohua Li27c0f682014-04-09 11:25:47 +08005324 logical_sector < mddev->suspend_hi) {
Mikulas Patocka03c1d9d2017-06-07 19:05:31 -04005325 sigset_t full, old;
5326 sigfillset(&full);
5327 sigprocmask(SIG_BLOCK, &full, &old);
NeilBrowne62e58a2009-07-01 13:15:35 +10005328 schedule();
Mikulas Patocka03c1d9d2017-06-07 19:05:31 -04005329 sigprocmask(SIG_SETMASK, &old, NULL);
Shaohua Li27c0f682014-04-09 11:25:47 +08005330 do_prepare = true;
5331 }
NeilBrowne464eaf2006-03-27 01:18:14 -08005332 goto retry;
5333 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005334
5335 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
shli@kernel.orgda41ba62014-12-15 12:57:03 +11005336 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005337 /* Stripe is busy expanding or
5338 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 * and wait a while
5340 */
NeilBrown482c0832011-04-18 18:25:42 +10005341 md_wakeup_thread(mddev->thread);
Shaohua Li6d036f72015-08-13 14:31:57 -07005342 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005344 do_prepare = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 goto retry;
5346 }
NeilBrown6ed30032008-02-06 01:40:00 -08005347 set_bit(STRIPE_HANDLE, &sh->state);
5348 clear_bit(STRIPE_DELAYED, &sh->state);
shli@kernel.org59fc6302014-12-15 12:57:03 +11005349 if ((!sh->batch_head || sh == sh->batch_head) &&
Jens Axboe1eff9d32016-08-05 15:35:16 -06005350 (bi->bi_opf & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11005351 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5352 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10005353 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 } else {
5355 /* cannot get stripe for read-ahead, just give-up */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005356 bi->bi_error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 break;
5358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 }
Shaohua Li27c0f682014-04-09 11:25:47 +08005360 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown7c13edc2011-04-18 18:25:43 +10005361
Shaohua Lie7836bd62012-07-19 16:01:31 +10005362 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08005363 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364
NeilBrown16a53ec2006-06-26 00:27:38 -07005365 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02005367
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005368 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
5369 bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005370 bio_endio(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372}
5373
NeilBrownfd01b882011-10-11 16:47:53 +11005374static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11005375
NeilBrownfd01b882011-10-11 16:47:53 +11005376static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377{
NeilBrown52c03292006-06-26 00:27:43 -07005378 /* reshaping is quite different to recovery/resync so it is
5379 * handled quite separately ... here.
5380 *
5381 * On each call to sync_request, we gather one chunk worth of
5382 * destination stripes and flag them as expanding.
5383 * Then we find all the source stripes and request reads.
5384 * As the reads complete, handle_stripe will copy the data
5385 * into the destination stripe and release that stripe.
5386 */
NeilBrownd1688a62011-10-11 16:49:52 +11005387 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005389 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08005390 int raid_disks = conf->previous_raid_disks;
5391 int data_disks = raid_disks - conf->max_degraded;
5392 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07005393 int i;
5394 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11005395 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11005396 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11005397 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11005398 struct list_head stripes;
NeilBrown92140482015-07-06 12:28:45 +10005399 sector_t retn;
NeilBrown52c03292006-06-26 00:27:43 -07005400
NeilBrownfef9c612009-03-31 15:16:46 +11005401 if (sector_nr == 0) {
5402 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10005403 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005404 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
5405 sector_nr = raid5_size(mddev, 0, 0)
5406 - conf->reshape_progress;
NeilBrown6cbd8142015-07-24 13:30:32 +10005407 } else if (mddev->reshape_backwards &&
5408 conf->reshape_progress == MaxSector) {
5409 /* shouldn't happen, but just in case, finish up.*/
5410 sector_nr = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +10005411 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005412 conf->reshape_progress > 0)
5413 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005414 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005415 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11005416 mddev->curr_resync_completed = sector_nr;
5417 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11005418 *skipped = 1;
NeilBrown92140482015-07-06 12:28:45 +10005419 retn = sector_nr;
5420 goto finish;
NeilBrownfef9c612009-03-31 15:16:46 +11005421 }
NeilBrown52c03292006-06-26 00:27:43 -07005422 }
5423
NeilBrown7a661382009-03-31 15:21:40 +11005424 /* We need to process a full chunk at a time.
5425 * If old and new chunk sizes differ, we need to process the
5426 * largest of these
5427 */
NeilBrown3cb5edf2015-07-15 17:24:17 +10005428
5429 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
NeilBrown7a661382009-03-31 15:21:40 +11005430
NeilBrownb5254dd2012-05-21 09:27:01 +10005431 /* We update the metadata at least every 10 seconds, or when
5432 * the data about to be copied would over-write the source of
5433 * the data at the front of the range. i.e. one new_stripe
5434 * along from reshape_progress new_maps to after where
5435 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07005436 */
NeilBrownfef9c612009-03-31 15:16:46 +11005437 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005438 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11005439 readpos = conf->reshape_progress;
5440 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005441 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08005442 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10005443 if (mddev->reshape_backwards) {
NeilBrownc74c0d72015-07-15 17:54:15 +10005444 BUG_ON(writepos < reshape_sectors);
5445 writepos -= reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11005446 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11005447 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11005448 } else {
NeilBrown7a661382009-03-31 15:21:40 +11005449 writepos += reshape_sectors;
NeilBrownc74c0d72015-07-15 17:54:15 +10005450 /* readpos and safepos are worst-case calculations.
5451 * A negative number is overly pessimistic, and causes
5452 * obvious problems for unsigned storage. So clip to 0.
5453 */
NeilBrowned37d832009-05-27 21:39:05 +10005454 readpos -= min_t(sector_t, reshape_sectors, readpos);
5455 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11005456 }
NeilBrown52c03292006-06-26 00:27:43 -07005457
NeilBrownb5254dd2012-05-21 09:27:01 +10005458 /* Having calculated the 'writepos' possibly use it
5459 * to set 'stripe_addr' which is where we will write to.
5460 */
5461 if (mddev->reshape_backwards) {
5462 BUG_ON(conf->reshape_progress == 0);
5463 stripe_addr = writepos;
5464 BUG_ON((mddev->dev_sectors &
5465 ~((sector_t)reshape_sectors - 1))
5466 - reshape_sectors - stripe_addr
5467 != sector_nr);
5468 } else {
5469 BUG_ON(writepos != sector_nr + reshape_sectors);
5470 stripe_addr = sector_nr;
5471 }
5472
NeilBrownc8f517c2009-03-31 15:28:40 +11005473 /* 'writepos' is the most advanced device address we might write.
5474 * 'readpos' is the least advanced device address we might read.
5475 * 'safepos' is the least address recorded in the metadata as having
5476 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10005477 * If there is a min_offset_diff, these are adjusted either by
5478 * increasing the safepos/readpos if diff is negative, or
5479 * increasing writepos if diff is positive.
5480 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11005481 * ensure safety in the face of a crash - that must be done by userspace
5482 * making a backup of the data. So in that case there is no particular
5483 * rush to update metadata.
5484 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5485 * update the metadata to advance 'safepos' to match 'readpos' so that
5486 * we can be safe in the event of a crash.
5487 * So we insist on updating metadata if safepos is behind writepos and
5488 * readpos is beyond writepos.
5489 * In any case, update the metadata every 10 seconds.
5490 * Maybe that number should be configurable, but I'm not sure it is
5491 * worth it.... maybe it could be a multiple of safemode_delay???
5492 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005493 if (conf->min_offset_diff < 0) {
5494 safepos += -conf->min_offset_diff;
5495 readpos += -conf->min_offset_diff;
5496 } else
5497 writepos += conf->min_offset_diff;
5498
NeilBrown2c810cd2012-05-21 09:27:00 +10005499 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11005500 ? (safepos > writepos && readpos < writepos)
5501 : (safepos < writepos && readpos > writepos)) ||
5502 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07005503 /* Cannot proceed until we've updated the superblock... */
5504 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005505 atomic_read(&conf->reshape_stripes)==0
5506 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5507 if (atomic_read(&conf->reshape_stripes) != 0)
5508 return 0;
NeilBrownfef9c612009-03-31 15:16:46 +11005509 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005510 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005511 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07005512 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07005513 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07005514 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11005515 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5516 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5517 return 0;
NeilBrown52c03292006-06-26 00:27:43 -07005518 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005519 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07005520 spin_unlock_irq(&conf->device_lock);
5521 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005522 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07005523 }
5524
NeilBrownab69ae12009-03-31 15:26:47 +11005525 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11005526 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07005527 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10005528 int skipped_disk = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07005529 sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005530 set_bit(STRIPE_EXPANDING, &sh->state);
5531 atomic_inc(&conf->reshape_stripes);
5532 /* If any of this stripe is beyond the end of the old
5533 * array, then we need to zero those blocks
5534 */
5535 for (j=sh->disks; j--;) {
5536 sector_t s;
5537 if (j == sh->pd_idx)
5538 continue;
NeilBrownf4168852007-02-28 20:11:53 -08005539 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11005540 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08005541 continue;
Shaohua Li6d036f72015-08-13 14:31:57 -07005542 s = raid5_compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11005543 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10005544 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07005545 continue;
5546 }
5547 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
5548 set_bit(R5_Expanded, &sh->dev[j].flags);
5549 set_bit(R5_UPTODATE, &sh->dev[j].flags);
5550 }
NeilBrowna9f326e2009-09-23 18:06:41 +10005551 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07005552 set_bit(STRIPE_EXPAND_READY, &sh->state);
5553 set_bit(STRIPE_HANDLE, &sh->state);
5554 }
NeilBrownab69ae12009-03-31 15:26:47 +11005555 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07005556 }
5557 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005558 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11005559 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005560 else
NeilBrown7a661382009-03-31 15:21:40 +11005561 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07005562 spin_unlock_irq(&conf->device_lock);
5563 /* Ok, those stripe are ready. We can start scheduling
5564 * reads on the source stripes.
5565 * The source stripes are determined by mapping the first and last
5566 * block on the destination stripes.
5567 */
NeilBrown52c03292006-06-26 00:27:43 -07005568 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11005569 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11005570 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07005571 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10005572 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10005573 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11005574 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11005575 if (last_sector >= mddev->dev_sectors)
5576 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07005577 while (first_sector <= last_sector) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005578 sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005579 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
5580 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07005581 raid5_release_stripe(sh);
NeilBrown52c03292006-06-26 00:27:43 -07005582 first_sector += STRIPE_SECTORS;
5583 }
NeilBrownab69ae12009-03-31 15:26:47 +11005584 /* Now that the sources are clearly marked, we can release
5585 * the destination stripes
5586 */
5587 while (!list_empty(&stripes)) {
5588 sh = list_entry(stripes.next, struct stripe_head, lru);
5589 list_del_init(&sh->lru);
Shaohua Li6d036f72015-08-13 14:31:57 -07005590 raid5_release_stripe(sh);
NeilBrownab69ae12009-03-31 15:26:47 +11005591 }
NeilBrownc6207272008-02-06 01:39:52 -08005592 /* If this takes us to the resync_max point where we have to pause,
5593 * then we need to write out the superblock.
5594 */
NeilBrown7a661382009-03-31 15:21:40 +11005595 sector_nr += reshape_sectors;
NeilBrown92140482015-07-06 12:28:45 +10005596 retn = reshape_sectors;
5597finish:
NeilBrownc5e19d92015-07-17 12:06:02 +10005598 if (mddev->curr_resync_completed > mddev->resync_max ||
5599 (sector_nr - mddev->curr_resync_completed) * 2
NeilBrownc03f6a12009-04-17 11:06:30 +10005600 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08005601 /* Cannot proceed until we've updated the superblock... */
5602 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005603 atomic_read(&conf->reshape_stripes) == 0
5604 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5605 if (atomic_read(&conf->reshape_stripes) != 0)
5606 goto ret;
NeilBrownfef9c612009-03-31 15:16:46 +11005607 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005608 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005609 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08005610 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5611 md_wakeup_thread(mddev->thread);
5612 wait_event(mddev->sb_wait,
5613 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
NeilBrownc91abf52013-11-19 12:02:01 +11005614 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5615 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5616 goto ret;
NeilBrownc6207272008-02-06 01:39:52 -08005617 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005618 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08005619 spin_unlock_irq(&conf->device_lock);
5620 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005621 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08005622 }
NeilBrownc91abf52013-11-19 12:02:01 +11005623ret:
NeilBrown92140482015-07-06 12:28:45 +10005624 return retn;
NeilBrown52c03292006-06-26 00:27:43 -07005625}
5626
Shaohua Li849674e2016-01-20 13:52:20 -08005627static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
5628 int *skipped)
NeilBrown52c03292006-06-26 00:27:43 -07005629{
NeilBrownd1688a62011-10-11 16:49:52 +11005630 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07005631 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11005632 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11005633 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07005634 int still_degraded = 0;
5635 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636
NeilBrown72626682005-09-09 16:23:54 -07005637 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11005639
NeilBrown29269552006-03-27 01:18:10 -08005640 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
5641 end_reshape(conf);
5642 return 0;
5643 }
NeilBrown72626682005-09-09 16:23:54 -07005644
5645 if (mddev->curr_resync < max_sector) /* aborted */
5646 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
5647 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07005648 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07005649 conf->fullsync = 0;
5650 bitmap_close_sync(mddev->bitmap);
5651
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 return 0;
5653 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08005654
NeilBrown64bd6602009-08-03 10:59:58 +10005655 /* Allow raid5_quiesce to complete */
5656 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
5657
NeilBrown52c03292006-06-26 00:27:43 -07005658 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5659 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08005660
NeilBrownc6207272008-02-06 01:39:52 -08005661 /* No need to check resync_max as we never do more than one
5662 * stripe, and as resync_max will always be on a chunk boundary,
5663 * if the check in md_do_sync didn't fire, there is no chance
5664 * of overstepping resync_max here
5665 */
5666
NeilBrown16a53ec2006-06-26 00:27:38 -07005667 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 * to resync, then assert that we are finished, because there is
5669 * nothing we can do.
5670 */
NeilBrown3285edf2006-06-26 00:27:55 -07005671 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005672 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005673 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07005674 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 return rv;
5676 }
majianpeng6f608042013-04-24 11:42:41 +10005677 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
5678 !conf->fullsync &&
5679 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
5680 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07005681 /* we can skip this block, and probably more */
5682 sync_blocks /= STRIPE_SECTORS;
5683 *skipped = 1;
5684 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
5685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10005687 bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
NeilBrownb47490c2008-02-06 01:39:50 -08005688
Shaohua Li6d036f72015-08-13 14:31:57 -07005689 sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 if (sh == NULL) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005691 sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07005693 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08005695 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005697 /* Need to check if array will still be degraded after recovery/resync
Eric Mei16d9cfa2015-01-06 09:35:02 -08005698 * Note in case of > 1 drive failures it's possible we're rebuilding
5699 * one drive while leaving another faulty drive in array.
NeilBrown16a53ec2006-06-26 00:27:38 -07005700 */
Eric Mei16d9cfa2015-01-06 09:35:02 -08005701 rcu_read_lock();
5702 for (i = 0; i < conf->raid_disks; i++) {
5703 struct md_rdev *rdev = ACCESS_ONCE(conf->disks[i].rdev);
5704
5705 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
NeilBrown16a53ec2006-06-26 00:27:38 -07005706 still_degraded = 1;
Eric Mei16d9cfa2015-01-06 09:35:02 -08005707 }
5708 rcu_read_unlock();
NeilBrown16a53ec2006-06-26 00:27:38 -07005709
5710 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
5711
NeilBrown83206d62011-07-26 11:19:49 +10005712 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Eivind Sarto053f5b62014-06-09 17:06:19 -07005713 set_bit(STRIPE_HANDLE, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714
Shaohua Li6d036f72015-08-13 14:31:57 -07005715 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716
5717 return STRIPE_SECTORS;
5718}
5719
NeilBrownd1688a62011-10-11 16:49:52 +11005720static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005721{
5722 /* We may not be able to submit a whole bio at once as there
5723 * may not be enough stripe_heads available.
5724 * We cannot pre-allocate enough stripe_heads as we may need
5725 * more than exist in the cache (if we allow ever large chunks).
5726 * So we do one stripe head at a time and record in
5727 * ->bi_hw_segments how many have been done.
5728 *
5729 * We *know* that this entire raid_bio is in one chunk, so
5730 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5731 */
5732 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11005733 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005734 sector_t sector, logical_sector, last_sector;
5735 int scnt = 0;
5736 int remaining;
5737 int handled = 0;
5738
Kent Overstreet4f024f32013-10-11 15:44:27 -07005739 logical_sector = raid_bio->bi_iter.bi_sector &
5740 ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11005741 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11005742 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005743 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005744
5745 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08005746 logical_sector += STRIPE_SECTORS,
5747 sector += STRIPE_SECTORS,
5748 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005749
Shaohua Lie7836bd62012-07-19 16:01:31 +10005750 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005751 /* already done this stripe */
5752 continue;
5753
Shaohua Li6d036f72015-08-13 14:31:57 -07005754 sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005755
5756 if (!sh) {
5757 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10005758 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005759 conf->retry_read_aligned = raid_bio;
5760 return handled;
5761 }
5762
shli@kernel.orgda41ba62014-12-15 12:57:03 +11005763 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005764 raid5_release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10005765 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08005766 conf->retry_read_aligned = raid_bio;
5767 return handled;
5768 }
5769
majianpeng3f9e7c12012-07-31 10:04:21 +10005770 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07005771 handle_stripe(sh);
Shaohua Li6d036f72015-08-13 14:31:57 -07005772 raid5_release_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005773 handled++;
5774 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10005775 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005776 if (remaining == 0) {
5777 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
5778 raid_bio, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005779 bio_endio(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005780 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005781 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10005782 wake_up(&conf->wait_for_quiescent);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005783 return handled;
5784}
5785
Shaohua Libfc90cb2013-08-29 15:40:32 +08005786static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11005787 struct r5worker *worker,
5788 struct list_head *temp_inactive_list)
Shaohua Li46a06402012-08-02 08:33:15 +10005789{
5790 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11005791 int i, batch_size = 0, hash;
5792 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10005793
5794 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08005795 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10005796 batch[batch_size++] = sh;
5797
Shaohua Li566c09c2013-11-14 15:16:17 +11005798 if (batch_size == 0) {
5799 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5800 if (!list_empty(temp_inactive_list + i))
5801 break;
Shaohua Lia8c34f92015-09-02 13:49:46 -07005802 if (i == NR_STRIPE_HASH_LOCKS) {
5803 spin_unlock_irq(&conf->device_lock);
5804 r5l_flush_stripe_to_raid(conf->log);
5805 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11005806 return batch_size;
Shaohua Lia8c34f92015-09-02 13:49:46 -07005807 }
Shaohua Li566c09c2013-11-14 15:16:17 +11005808 release_inactive = true;
5809 }
Shaohua Li46a06402012-08-02 08:33:15 +10005810 spin_unlock_irq(&conf->device_lock);
5811
Shaohua Li566c09c2013-11-14 15:16:17 +11005812 release_inactive_stripe_list(conf, temp_inactive_list,
5813 NR_STRIPE_HASH_LOCKS);
5814
Shaohua Lia8c34f92015-09-02 13:49:46 -07005815 r5l_flush_stripe_to_raid(conf->log);
Shaohua Li566c09c2013-11-14 15:16:17 +11005816 if (release_inactive) {
5817 spin_lock_irq(&conf->device_lock);
5818 return 0;
5819 }
5820
Shaohua Li46a06402012-08-02 08:33:15 +10005821 for (i = 0; i < batch_size; i++)
5822 handle_stripe(batch[i]);
Shaohua Lif6bed0e2015-08-13 14:31:59 -07005823 r5l_write_stripe_run(conf->log);
Shaohua Li46a06402012-08-02 08:33:15 +10005824
5825 cond_resched();
5826
5827 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11005828 for (i = 0; i < batch_size; i++) {
5829 hash = batch[i]->hash_lock_index;
5830 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
5831 }
Shaohua Li46a06402012-08-02 08:33:15 +10005832 return batch_size;
5833}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005834
Shaohua Li851c30c2013-08-28 14:30:16 +08005835static void raid5_do_work(struct work_struct *work)
5836{
5837 struct r5worker *worker = container_of(work, struct r5worker, work);
5838 struct r5worker_group *group = worker->group;
5839 struct r5conf *conf = group->conf;
5840 int group_id = group - conf->worker_groups;
5841 int handled;
5842 struct blk_plug plug;
5843
5844 pr_debug("+++ raid5worker active\n");
5845
5846 blk_start_plug(&plug);
5847 handled = 0;
5848 spin_lock_irq(&conf->device_lock);
5849 while (1) {
5850 int batch_size, released;
5851
Shaohua Li566c09c2013-11-14 15:16:17 +11005852 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08005853
Shaohua Li566c09c2013-11-14 15:16:17 +11005854 batch_size = handle_active_stripes(conf, group_id, worker,
5855 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08005856 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08005857 if (!batch_size && !released)
5858 break;
5859 handled += batch_size;
5860 }
5861 pr_debug("%d stripes handled\n", handled);
5862
5863 spin_unlock_irq(&conf->device_lock);
Ofer Heifetzfabc7df2017-07-24 09:17:40 +03005864
Song Liu7b5fcb72017-08-24 09:53:59 -07005865 r5l_flush_stripe_to_raid(conf->log);
5866
Ofer Heifetzfabc7df2017-07-24 09:17:40 +03005867 async_tx_issue_pending_all();
Shaohua Li851c30c2013-08-28 14:30:16 +08005868 blk_finish_plug(&plug);
5869
5870 pr_debug("--- raid5worker inactive\n");
5871}
5872
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873/*
5874 * This is our raid5 kernel thread.
5875 *
5876 * We scan the hash table for stripes which can be handled now.
5877 * During the scan, completed stripes are saved for us by the interrupt
5878 * handler, so that they will not have to wait for our next wakeup.
5879 */
Shaohua Li4ed87312012-10-11 13:34:00 +11005880static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881{
Shaohua Li4ed87312012-10-11 13:34:00 +11005882 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005883 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005885 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
Dan Williams45b42332007-07-09 11:56:43 -07005887 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888
5889 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890
NeilBrownc3cce6c2015-08-14 12:47:33 +10005891 if (!bio_list_empty(&conf->return_bi) &&
5892 !test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
5893 struct bio_list tmp = BIO_EMPTY_LIST;
5894 spin_lock_irq(&conf->device_lock);
5895 if (!test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
5896 bio_list_merge(&tmp, &conf->return_bi);
5897 bio_list_init(&conf->return_bi);
5898 }
5899 spin_unlock_irq(&conf->device_lock);
5900 return_io(&tmp);
5901 }
5902
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005903 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 handled = 0;
5905 spin_lock_irq(&conf->device_lock);
5906 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005907 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08005908 int batch_size, released;
5909
Shaohua Li566c09c2013-11-14 15:16:17 +11005910 released = release_stripe_list(conf, conf->temp_inactive_list);
NeilBrownedbe83a2015-02-26 12:47:56 +11005911 if (released)
5912 clear_bit(R5_DID_ALLOC, &conf->cache_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913
NeilBrown0021b7b2012-07-31 09:08:14 +02005914 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10005915 !list_empty(&conf->bitmap_list)) {
5916 /* Now is a good time to flush some bitmap updates */
5917 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08005918 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07005919 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08005920 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10005921 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11005922 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07005923 }
NeilBrown0021b7b2012-07-31 09:08:14 +02005924 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07005925
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005926 while ((bio = remove_bio_from_retry(conf))) {
5927 int ok;
5928 spin_unlock_irq(&conf->device_lock);
5929 ok = retry_aligned_read(conf, bio);
5930 spin_lock_irq(&conf->device_lock);
5931 if (!ok)
5932 break;
5933 handled++;
5934 }
5935
Shaohua Li566c09c2013-11-14 15:16:17 +11005936 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
5937 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08005938 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 break;
Shaohua Li46a06402012-08-02 08:33:15 +10005940 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941
Shaohua Li46a06402012-08-02 08:33:15 +10005942 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
5943 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10005944 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10005945 spin_lock_irq(&conf->device_lock);
5946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 }
Dan Williams45b42332007-07-09 11:56:43 -07005948 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949
5950 spin_unlock_irq(&conf->device_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10005951 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
5952 mutex_trylock(&conf->cache_size_mutex)) {
NeilBrownedbe83a2015-02-26 12:47:56 +11005953 grow_one_stripe(conf, __GFP_NOWARN);
5954 /* Set flag even if allocation failed. This helps
5955 * slow down allocation requests when mem is short
5956 */
5957 set_bit(R5_DID_ALLOC, &conf->cache_state);
NeilBrown2d5b5692015-07-06 12:49:23 +10005958 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11005959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960
Shaohua Li0576b1c2015-08-13 14:32:00 -07005961 r5l_flush_stripe_to_raid(conf->log);
5962
Dan Williamsc9f21aa2008-07-23 12:05:51 -07005963 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005964 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965
Dan Williams45b42332007-07-09 11:56:43 -07005966 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967}
5968
NeilBrown3f294f42005-11-08 21:39:25 -08005969static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005970raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005971{
NeilBrown7b1485b2014-12-15 12:56:59 +11005972 struct r5conf *conf;
5973 int ret = 0;
5974 spin_lock(&mddev->lock);
5975 conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005976 if (conf)
NeilBrownedbe83a2015-02-26 12:47:56 +11005977 ret = sprintf(page, "%d\n", conf->min_nr_stripes);
NeilBrown7b1485b2014-12-15 12:56:59 +11005978 spin_unlock(&mddev->lock);
5979 return ret;
NeilBrown3f294f42005-11-08 21:39:25 -08005980}
5981
NeilBrownc41d4ac2010-06-01 19:37:24 +10005982int
NeilBrownfd01b882011-10-11 16:47:53 +11005983raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10005984{
NeilBrownd1688a62011-10-11 16:49:52 +11005985 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005986 int err;
5987
5988 if (size <= 16 || size > 32768)
5989 return -EINVAL;
NeilBrown486f0642015-02-25 12:10:35 +11005990
NeilBrownedbe83a2015-02-26 12:47:56 +11005991 conf->min_nr_stripes = size;
NeilBrown2d5b5692015-07-06 12:49:23 +10005992 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11005993 while (size < conf->max_nr_stripes &&
5994 drop_one_stripe(conf))
5995 ;
NeilBrown2d5b5692015-07-06 12:49:23 +10005996 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11005997
NeilBrownedbe83a2015-02-26 12:47:56 +11005998
NeilBrownc41d4ac2010-06-01 19:37:24 +10005999 err = md_allow_write(mddev);
6000 if (err)
6001 return err;
NeilBrown486f0642015-02-25 12:10:35 +11006002
NeilBrown2d5b5692015-07-06 12:49:23 +10006003 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006004 while (size > conf->max_nr_stripes)
6005 if (!grow_one_stripe(conf, GFP_KERNEL))
6006 break;
NeilBrown2d5b5692015-07-06 12:49:23 +10006007 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006008
NeilBrownc41d4ac2010-06-01 19:37:24 +10006009 return 0;
6010}
6011EXPORT_SYMBOL(raid5_set_cache_size);
6012
NeilBrown3f294f42005-11-08 21:39:25 -08006013static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006014raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08006015{
NeilBrown67918752014-12-15 12:57:01 +11006016 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006017 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07006018 int err;
6019
NeilBrown3f294f42005-11-08 21:39:25 -08006020 if (len >= PAGE_SIZE)
6021 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006022 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08006023 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006024 err = mddev_lock(mddev);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006025 if (err)
6026 return err;
NeilBrown67918752014-12-15 12:57:01 +11006027 conf = mddev->private;
6028 if (!conf)
6029 err = -ENODEV;
6030 else
6031 err = raid5_set_cache_size(mddev, new);
6032 mddev_unlock(mddev);
6033
6034 return err ?: len;
NeilBrown3f294f42005-11-08 21:39:25 -08006035}
NeilBrown007583c2005-11-08 21:39:30 -08006036
NeilBrown96de1e62005-11-08 21:39:39 -08006037static struct md_sysfs_entry
6038raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
6039 raid5_show_stripe_cache_size,
6040 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08006041
6042static ssize_t
Markus Stockhausend06f1912014-12-15 12:57:05 +11006043raid5_show_rmw_level(struct mddev *mddev, char *page)
6044{
6045 struct r5conf *conf = mddev->private;
6046 if (conf)
6047 return sprintf(page, "%d\n", conf->rmw_level);
6048 else
6049 return 0;
6050}
6051
6052static ssize_t
6053raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
6054{
6055 struct r5conf *conf = mddev->private;
6056 unsigned long new;
6057
6058 if (!conf)
6059 return -ENODEV;
6060
6061 if (len >= PAGE_SIZE)
6062 return -EINVAL;
6063
6064 if (kstrtoul(page, 10, &new))
6065 return -EINVAL;
6066
6067 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
6068 return -EINVAL;
6069
6070 if (new != PARITY_DISABLE_RMW &&
6071 new != PARITY_ENABLE_RMW &&
6072 new != PARITY_PREFER_RMW)
6073 return -EINVAL;
6074
6075 conf->rmw_level = new;
6076 return len;
6077}
6078
6079static struct md_sysfs_entry
6080raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
6081 raid5_show_rmw_level,
6082 raid5_store_rmw_level);
6083
6084
6085static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006086raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006087{
NeilBrown7b1485b2014-12-15 12:56:59 +11006088 struct r5conf *conf;
6089 int ret = 0;
6090 spin_lock(&mddev->lock);
6091 conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006092 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006093 ret = sprintf(page, "%d\n", conf->bypass_threshold);
6094 spin_unlock(&mddev->lock);
6095 return ret;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006096}
6097
6098static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006099raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006100{
NeilBrown67918752014-12-15 12:57:01 +11006101 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006102 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006103 int err;
6104
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006105 if (len >= PAGE_SIZE)
6106 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006107 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006108 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006109
6110 err = mddev_lock(mddev);
6111 if (err)
6112 return err;
6113 conf = mddev->private;
6114 if (!conf)
6115 err = -ENODEV;
NeilBrownedbe83a2015-02-26 12:47:56 +11006116 else if (new > conf->min_nr_stripes)
NeilBrown67918752014-12-15 12:57:01 +11006117 err = -EINVAL;
6118 else
6119 conf->bypass_threshold = new;
6120 mddev_unlock(mddev);
6121 return err ?: len;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006122}
6123
6124static struct md_sysfs_entry
6125raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
6126 S_IRUGO | S_IWUSR,
6127 raid5_show_preread_threshold,
6128 raid5_store_preread_threshold);
6129
6130static ssize_t
Shaohua Lid592a992014-05-21 17:57:44 +08006131raid5_show_skip_copy(struct mddev *mddev, char *page)
6132{
NeilBrown7b1485b2014-12-15 12:56:59 +11006133 struct r5conf *conf;
6134 int ret = 0;
6135 spin_lock(&mddev->lock);
6136 conf = mddev->private;
Shaohua Lid592a992014-05-21 17:57:44 +08006137 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006138 ret = sprintf(page, "%d\n", conf->skip_copy);
6139 spin_unlock(&mddev->lock);
6140 return ret;
Shaohua Lid592a992014-05-21 17:57:44 +08006141}
6142
6143static ssize_t
6144raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
6145{
NeilBrown67918752014-12-15 12:57:01 +11006146 struct r5conf *conf;
Shaohua Lid592a992014-05-21 17:57:44 +08006147 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006148 int err;
6149
Shaohua Lid592a992014-05-21 17:57:44 +08006150 if (len >= PAGE_SIZE)
6151 return -EINVAL;
Shaohua Lid592a992014-05-21 17:57:44 +08006152 if (kstrtoul(page, 10, &new))
6153 return -EINVAL;
6154 new = !!new;
Shaohua Lid592a992014-05-21 17:57:44 +08006155
NeilBrown67918752014-12-15 12:57:01 +11006156 err = mddev_lock(mddev);
6157 if (err)
6158 return err;
6159 conf = mddev->private;
6160 if (!conf)
6161 err = -ENODEV;
6162 else if (new != conf->skip_copy) {
6163 mddev_suspend(mddev);
6164 conf->skip_copy = new;
6165 if (new)
6166 mddev->queue->backing_dev_info.capabilities |=
6167 BDI_CAP_STABLE_WRITES;
6168 else
6169 mddev->queue->backing_dev_info.capabilities &=
6170 ~BDI_CAP_STABLE_WRITES;
6171 mddev_resume(mddev);
6172 }
6173 mddev_unlock(mddev);
6174 return err ?: len;
Shaohua Lid592a992014-05-21 17:57:44 +08006175}
6176
6177static struct md_sysfs_entry
6178raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
6179 raid5_show_skip_copy,
6180 raid5_store_skip_copy);
6181
Shaohua Lid592a992014-05-21 17:57:44 +08006182static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006183stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006184{
NeilBrownd1688a62011-10-11 16:49:52 +11006185 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006186 if (conf)
6187 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
6188 else
6189 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08006190}
6191
NeilBrown96de1e62005-11-08 21:39:39 -08006192static struct md_sysfs_entry
6193raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08006194
Shaohua Lib7214202013-08-27 17:50:42 +08006195static ssize_t
6196raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
6197{
NeilBrown7b1485b2014-12-15 12:56:59 +11006198 struct r5conf *conf;
6199 int ret = 0;
6200 spin_lock(&mddev->lock);
6201 conf = mddev->private;
Shaohua Lib7214202013-08-27 17:50:42 +08006202 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006203 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
6204 spin_unlock(&mddev->lock);
6205 return ret;
Shaohua Lib7214202013-08-27 17:50:42 +08006206}
6207
majianpeng60aaf932013-11-14 15:16:20 +11006208static int alloc_thread_groups(struct r5conf *conf, int cnt,
6209 int *group_cnt,
6210 int *worker_cnt_per_group,
6211 struct r5worker_group **worker_groups);
Shaohua Lib7214202013-08-27 17:50:42 +08006212static ssize_t
6213raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
6214{
NeilBrown67918752014-12-15 12:57:01 +11006215 struct r5conf *conf;
Shaohua Lib7214202013-08-27 17:50:42 +08006216 unsigned long new;
6217 int err;
majianpeng60aaf932013-11-14 15:16:20 +11006218 struct r5worker_group *new_groups, *old_groups;
6219 int group_cnt, worker_cnt_per_group;
Shaohua Lib7214202013-08-27 17:50:42 +08006220
6221 if (len >= PAGE_SIZE)
6222 return -EINVAL;
Shaohua Lib7214202013-08-27 17:50:42 +08006223 if (kstrtoul(page, 10, &new))
6224 return -EINVAL;
6225
NeilBrown67918752014-12-15 12:57:01 +11006226 err = mddev_lock(mddev);
Shaohua Lib7214202013-08-27 17:50:42 +08006227 if (err)
6228 return err;
NeilBrown67918752014-12-15 12:57:01 +11006229 conf = mddev->private;
6230 if (!conf)
6231 err = -ENODEV;
6232 else if (new != conf->worker_cnt_per_group) {
6233 mddev_suspend(mddev);
6234
6235 old_groups = conf->worker_groups;
6236 if (old_groups)
6237 flush_workqueue(raid5_wq);
6238
6239 err = alloc_thread_groups(conf, new,
6240 &group_cnt, &worker_cnt_per_group,
6241 &new_groups);
6242 if (!err) {
6243 spin_lock_irq(&conf->device_lock);
6244 conf->group_cnt = group_cnt;
6245 conf->worker_cnt_per_group = worker_cnt_per_group;
6246 conf->worker_groups = new_groups;
6247 spin_unlock_irq(&conf->device_lock);
6248
6249 if (old_groups)
6250 kfree(old_groups[0].workers);
6251 kfree(old_groups);
6252 }
6253 mddev_resume(mddev);
6254 }
6255 mddev_unlock(mddev);
6256
6257 return err ?: len;
Shaohua Lib7214202013-08-27 17:50:42 +08006258}
6259
6260static struct md_sysfs_entry
6261raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
6262 raid5_show_group_thread_cnt,
6263 raid5_store_group_thread_cnt);
6264
NeilBrown007583c2005-11-08 21:39:30 -08006265static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08006266 &raid5_stripecache_size.attr,
6267 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006268 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08006269 &raid5_group_thread_cnt.attr,
Shaohua Lid592a992014-05-21 17:57:44 +08006270 &raid5_skip_copy.attr,
Markus Stockhausend06f1912014-12-15 12:57:05 +11006271 &raid5_rmw_level.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08006272 NULL,
6273};
NeilBrown007583c2005-11-08 21:39:30 -08006274static struct attribute_group raid5_attrs_group = {
6275 .name = NULL,
6276 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08006277};
6278
majianpeng60aaf932013-11-14 15:16:20 +11006279static int alloc_thread_groups(struct r5conf *conf, int cnt,
6280 int *group_cnt,
6281 int *worker_cnt_per_group,
6282 struct r5worker_group **worker_groups)
Shaohua Li851c30c2013-08-28 14:30:16 +08006283{
Shaohua Li566c09c2013-11-14 15:16:17 +11006284 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08006285 ssize_t size;
6286 struct r5worker *workers;
6287
majianpeng60aaf932013-11-14 15:16:20 +11006288 *worker_cnt_per_group = cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +08006289 if (cnt == 0) {
majianpeng60aaf932013-11-14 15:16:20 +11006290 *group_cnt = 0;
6291 *worker_groups = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08006292 return 0;
6293 }
majianpeng60aaf932013-11-14 15:16:20 +11006294 *group_cnt = num_possible_nodes();
Shaohua Li851c30c2013-08-28 14:30:16 +08006295 size = sizeof(struct r5worker) * cnt;
majianpeng60aaf932013-11-14 15:16:20 +11006296 workers = kzalloc(size * *group_cnt, GFP_NOIO);
6297 *worker_groups = kzalloc(sizeof(struct r5worker_group) *
6298 *group_cnt, GFP_NOIO);
6299 if (!*worker_groups || !workers) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006300 kfree(workers);
majianpeng60aaf932013-11-14 15:16:20 +11006301 kfree(*worker_groups);
Shaohua Li851c30c2013-08-28 14:30:16 +08006302 return -ENOMEM;
6303 }
6304
majianpeng60aaf932013-11-14 15:16:20 +11006305 for (i = 0; i < *group_cnt; i++) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006306 struct r5worker_group *group;
6307
NeilBrown0c775d52013-11-25 11:12:43 +11006308 group = &(*worker_groups)[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08006309 INIT_LIST_HEAD(&group->handle_list);
6310 group->conf = conf;
6311 group->workers = workers + i * cnt;
6312
6313 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11006314 struct r5worker *worker = group->workers + j;
6315 worker->group = group;
6316 INIT_WORK(&worker->work, raid5_do_work);
6317
6318 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
6319 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08006320 }
6321 }
6322
6323 return 0;
6324}
6325
6326static void free_thread_groups(struct r5conf *conf)
6327{
6328 if (conf->worker_groups)
6329 kfree(conf->worker_groups[0].workers);
6330 kfree(conf->worker_groups);
6331 conf->worker_groups = NULL;
6332}
6333
Dan Williams80c3a6c2009-03-17 18:10:40 -07006334static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11006335raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07006336{
NeilBrownd1688a62011-10-11 16:49:52 +11006337 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006338
6339 if (!sectors)
6340 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006341 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11006342 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11006343 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006344
NeilBrown3cb5edf2015-07-15 17:24:17 +10006345 sectors &= ~((sector_t)conf->chunk_sectors - 1);
6346 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006347 return sectors * (raid_disks - conf->max_degraded);
6348}
6349
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306350static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6351{
6352 safe_put_page(percpu->spare_page);
shli@kernel.org46d5b782014-12-15 12:57:02 +11006353 if (percpu->scribble)
6354 flex_array_free(percpu->scribble);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306355 percpu->spare_page = NULL;
6356 percpu->scribble = NULL;
6357}
6358
6359static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6360{
6361 if (conf->level == 6 && !percpu->spare_page)
6362 percpu->spare_page = alloc_page(GFP_KERNEL);
6363 if (!percpu->scribble)
shli@kernel.org46d5b782014-12-15 12:57:02 +11006364 percpu->scribble = scribble_alloc(max(conf->raid_disks,
NeilBrown738a2732015-05-08 18:19:39 +10006365 conf->previous_raid_disks),
6366 max(conf->chunk_sectors,
6367 conf->prev_chunk_sectors)
6368 / STRIPE_SECTORS,
6369 GFP_KERNEL);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306370
6371 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
6372 free_scratch_buffer(conf, percpu);
6373 return -ENOMEM;
6374 }
6375
6376 return 0;
6377}
6378
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006379static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
6380{
6381 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
6382
6383 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
6384 return 0;
6385}
6386
NeilBrownd1688a62011-10-11 16:49:52 +11006387static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006388{
Dan Williams36d1c642009-07-14 11:48:22 -07006389 if (!conf->percpu)
6390 return;
6391
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006392 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Dan Williams36d1c642009-07-14 11:48:22 -07006393 free_percpu(conf->percpu);
6394}
6395
NeilBrownd1688a62011-10-11 16:49:52 +11006396static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10006397{
Shaohua Li5c7e81c2015-08-13 14:32:04 -07006398 if (conf->log)
6399 r5l_exit_log(conf->log);
Shaohua Li30c89462016-09-21 09:07:13 -07006400 if (conf->shrinker.nr_deferred)
NeilBrownedbe83a2015-02-26 12:47:56 +11006401 unregister_shrinker(&conf->shrinker);
Shaohua Li5c7e81c2015-08-13 14:32:04 -07006402
Shaohua Li851c30c2013-08-28 14:30:16 +08006403 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10006404 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07006405 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10006406 kfree(conf->disks);
6407 kfree(conf->stripe_hashtbl);
6408 kfree(conf);
6409}
6410
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006411static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
Dan Williams36d1c642009-07-14 11:48:22 -07006412{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006413 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
Dan Williams36d1c642009-07-14 11:48:22 -07006414 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
6415
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006416 if (alloc_scratch_buffer(conf, percpu)) {
6417 pr_err("%s: failed memory allocation for cpu%u\n",
6418 __func__, cpu);
6419 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006420 }
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006421 return 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006422}
Dan Williams36d1c642009-07-14 11:48:22 -07006423
NeilBrownd1688a62011-10-11 16:49:52 +11006424static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006425{
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306426 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006427
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306428 conf->percpu = alloc_percpu(struct raid5_percpu);
6429 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07006430 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006431
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006432 err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Shaohua Li27a353c2016-02-24 17:38:28 -08006433 if (!err) {
6434 conf->scribble_disks = max(conf->raid_disks,
6435 conf->previous_raid_disks);
6436 conf->scribble_sectors = max(conf->chunk_sectors,
6437 conf->prev_chunk_sectors);
6438 }
Dan Williams36d1c642009-07-14 11:48:22 -07006439 return err;
6440}
6441
NeilBrownedbe83a2015-02-26 12:47:56 +11006442static unsigned long raid5_cache_scan(struct shrinker *shrink,
6443 struct shrink_control *sc)
6444{
6445 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
NeilBrown2d5b5692015-07-06 12:49:23 +10006446 unsigned long ret = SHRINK_STOP;
6447
6448 if (mutex_trylock(&conf->cache_size_mutex)) {
6449 ret= 0;
NeilBrown49895bc2015-08-03 17:09:57 +10006450 while (ret < sc->nr_to_scan &&
6451 conf->max_nr_stripes > conf->min_nr_stripes) {
NeilBrown2d5b5692015-07-06 12:49:23 +10006452 if (drop_one_stripe(conf) == 0) {
6453 ret = SHRINK_STOP;
6454 break;
6455 }
6456 ret++;
6457 }
6458 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006459 }
6460 return ret;
6461}
6462
6463static unsigned long raid5_cache_count(struct shrinker *shrink,
6464 struct shrink_control *sc)
6465{
6466 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
6467
6468 if (conf->max_nr_stripes < conf->min_nr_stripes)
6469 /* unlikely, but not impossible */
6470 return 0;
6471 return conf->max_nr_stripes - conf->min_nr_stripes;
6472}
6473
NeilBrownd1688a62011-10-11 16:49:52 +11006474static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475{
NeilBrownd1688a62011-10-11 16:49:52 +11006476 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006477 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11006478 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10006480 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11006481 int i;
majianpeng60aaf932013-11-14 15:16:20 +11006482 int group_cnt, worker_cnt_per_group;
6483 struct r5worker_group *new_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484
NeilBrown91adb562009-03-31 14:39:39 +11006485 if (mddev->new_level != 5
6486 && mddev->new_level != 4
6487 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10006488 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11006489 mdname(mddev), mddev->new_level);
6490 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 }
NeilBrown91adb562009-03-31 14:39:39 +11006492 if ((mddev->new_level == 5
6493 && !algorithm_valid_raid5(mddev->new_layout)) ||
6494 (mddev->new_level == 6
6495 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10006496 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11006497 mdname(mddev), mddev->new_layout);
6498 return ERR_PTR(-EIO);
6499 }
6500 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10006501 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11006502 mdname(mddev), mddev->raid_disks);
6503 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11006504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505
Andre Noll664e7c42009-06-18 08:45:27 +10006506 if (!mddev->new_chunk_sectors ||
6507 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
6508 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10006509 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
6510 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11006511 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11006512 }
6513
NeilBrownd1688a62011-10-11 16:49:52 +11006514 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11006515 if (conf == NULL)
6516 goto abort;
Shaohua Li851c30c2013-08-28 14:30:16 +08006517 /* Don't enable multi-threading by default*/
majianpeng60aaf932013-11-14 15:16:20 +11006518 if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group,
6519 &new_group)) {
6520 conf->group_cnt = group_cnt;
6521 conf->worker_cnt_per_group = worker_cnt_per_group;
6522 conf->worker_groups = new_group;
6523 } else
Shaohua Li851c30c2013-08-28 14:30:16 +08006524 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11006525 spin_lock_init(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10006526 seqcount_init(&conf->gen_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006527 mutex_init(&conf->cache_size_mutex);
Yuanhan Liub1b46482015-05-08 18:19:06 +10006528 init_waitqueue_head(&conf->wait_for_quiescent);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08006529 init_waitqueue_head(&conf->wait_for_stripe);
Dan Williamsf5efd452009-10-16 15:55:38 +11006530 init_waitqueue_head(&conf->wait_for_overlap);
6531 INIT_LIST_HEAD(&conf->handle_list);
6532 INIT_LIST_HEAD(&conf->hold_list);
6533 INIT_LIST_HEAD(&conf->delayed_list);
6534 INIT_LIST_HEAD(&conf->bitmap_list);
NeilBrownc3cce6c2015-08-14 12:47:33 +10006535 bio_list_init(&conf->return_bi);
Shaohua Li773ca822013-08-27 17:50:39 +08006536 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11006537 atomic_set(&conf->active_stripes, 0);
6538 atomic_set(&conf->preread_active_stripes, 0);
6539 atomic_set(&conf->active_aligned_reads, 0);
6540 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11006541 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11006542
6543 conf->raid_disks = mddev->raid_disks;
6544 if (mddev->reshape_position == MaxSector)
6545 conf->previous_raid_disks = mddev->raid_disks;
6546 else
6547 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006548 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006549
NeilBrown5e5e3e72009-10-16 16:35:30 +11006550 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11006551 GFP_KERNEL);
6552 if (!conf->disks)
6553 goto abort;
6554
6555 conf->mddev = mddev;
6556
6557 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
6558 goto abort;
6559
Shaohua Li566c09c2013-11-14 15:16:17 +11006560 /* We init hash_locks[0] separately to that it can be used
6561 * as the reference lock in the spin_lock_nest_lock() call
6562 * in lock_all_device_hash_locks_irq in order to convince
6563 * lockdep that we know what we are doing.
6564 */
6565 spin_lock_init(conf->hash_locks);
6566 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
6567 spin_lock_init(conf->hash_locks + i);
6568
6569 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6570 INIT_LIST_HEAD(conf->inactive_list + i);
6571
6572 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6573 INIT_LIST_HEAD(conf->temp_inactive_list + i);
6574
Dan Williams36d1c642009-07-14 11:48:22 -07006575 conf->level = mddev->new_level;
shli@kernel.org46d5b782014-12-15 12:57:02 +11006576 conf->chunk_sectors = mddev->new_chunk_sectors;
Dan Williams36d1c642009-07-14 11:48:22 -07006577 if (raid5_alloc_percpu(conf) != 0)
6578 goto abort;
6579
NeilBrown0c55e022010-05-03 14:09:02 +10006580 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11006581
NeilBrowndafb20f2012-03-19 12:46:39 +11006582 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11006583 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006584 if (raid_disk >= max_disks
Shaohua Lif2076e72015-10-08 21:54:12 -07006585 || raid_disk < 0 || test_bit(Journal, &rdev->flags))
NeilBrown91adb562009-03-31 14:39:39 +11006586 continue;
6587 disk = conf->disks + raid_disk;
6588
NeilBrown17045f52011-12-23 10:17:53 +11006589 if (test_bit(Replacement, &rdev->flags)) {
6590 if (disk->replacement)
6591 goto abort;
6592 disk->replacement = rdev;
6593 } else {
6594 if (disk->rdev)
6595 goto abort;
6596 disk->rdev = rdev;
6597 }
NeilBrown91adb562009-03-31 14:39:39 +11006598
6599 if (test_bit(In_sync, &rdev->flags)) {
6600 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10006601 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
6602 " disk %d\n",
6603 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05006604 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11006605 /* Cannot rely on bitmap to complete recovery */
6606 conf->fullsync = 1;
6607 }
6608
NeilBrown91adb562009-03-31 14:39:39 +11006609 conf->level = mddev->new_level;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006610 if (conf->level == 6) {
NeilBrown91adb562009-03-31 14:39:39 +11006611 conf->max_degraded = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006612 if (raid6_call.xor_syndrome)
6613 conf->rmw_level = PARITY_ENABLE_RMW;
6614 else
6615 conf->rmw_level = PARITY_DISABLE_RMW;
6616 } else {
NeilBrown91adb562009-03-31 14:39:39 +11006617 conf->max_degraded = 1;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006618 conf->rmw_level = PARITY_ENABLE_RMW;
6619 }
NeilBrown91adb562009-03-31 14:39:39 +11006620 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11006621 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11006622 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10006623 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11006624 conf->prev_algo = mddev->layout;
NeilBrown5cac6bc2015-07-17 12:17:50 +10006625 } else {
6626 conf->prev_chunk_sectors = conf->chunk_sectors;
6627 conf->prev_algo = conf->algorithm;
NeilBrowne183eae2009-03-31 15:20:22 +11006628 }
NeilBrown91adb562009-03-31 14:39:39 +11006629
NeilBrownedbe83a2015-02-26 12:47:56 +11006630 conf->min_nr_stripes = NR_STRIPES;
Shaohua Liad5b0f72016-08-30 10:29:33 -07006631 if (mddev->reshape_position != MaxSector) {
6632 int stripes = max_t(int,
6633 ((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4,
6634 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4);
6635 conf->min_nr_stripes = max(NR_STRIPES, stripes);
6636 if (conf->min_nr_stripes != NR_STRIPES)
6637 printk(KERN_INFO
6638 "md/raid:%s: force stripe size %d for reshape\n",
6639 mdname(mddev), conf->min_nr_stripes);
6640 }
NeilBrownedbe83a2015-02-26 12:47:56 +11006641 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11006642 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li4bda5562013-11-14 15:16:17 +11006643 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
NeilBrownedbe83a2015-02-26 12:47:56 +11006644 if (grow_stripes(conf, conf->min_nr_stripes)) {
NeilBrown91adb562009-03-31 14:39:39 +11006645 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10006646 "md/raid:%s: couldn't allocate %dkB for buffers\n",
6647 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11006648 goto abort;
6649 } else
NeilBrown0c55e022010-05-03 14:09:02 +10006650 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
6651 mdname(mddev), memory);
NeilBrownedbe83a2015-02-26 12:47:56 +11006652 /*
6653 * Losing a stripe head costs more than the time to refill it,
6654 * it reduces the queue depth and so can hurt throughput.
6655 * So set it rather large, scaled by number of devices.
6656 */
6657 conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
6658 conf->shrinker.scan_objects = raid5_cache_scan;
6659 conf->shrinker.count_objects = raid5_cache_count;
6660 conf->shrinker.batch = 128;
6661 conf->shrinker.flags = 0;
Chao Yu6a0f53f2016-09-20 10:33:57 +08006662 if (register_shrinker(&conf->shrinker)) {
6663 printk(KERN_ERR
6664 "md/raid:%s: couldn't register shrinker.\n",
6665 mdname(mddev));
6666 goto abort;
6667 }
NeilBrown91adb562009-03-31 14:39:39 +11006668
NeilBrown02326052012-07-03 15:56:52 +10006669 sprintf(pers_name, "raid%d", mddev->new_level);
6670 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11006671 if (!conf->thread) {
6672 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10006673 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11006674 mdname(mddev));
6675 goto abort;
6676 }
6677
6678 return conf;
6679
6680 abort:
6681 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10006682 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11006683 return ERR_PTR(-EIO);
6684 } else
6685 return ERR_PTR(-ENOMEM);
6686}
6687
NeilBrownc148ffd2009-11-13 17:47:00 +11006688static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
6689{
6690 switch (algo) {
6691 case ALGORITHM_PARITY_0:
6692 if (raid_disk < max_degraded)
6693 return 1;
6694 break;
6695 case ALGORITHM_PARITY_N:
6696 if (raid_disk >= raid_disks - max_degraded)
6697 return 1;
6698 break;
6699 case ALGORITHM_PARITY_0_6:
NeilBrownf72ffdd2014-09-30 14:23:59 +10006700 if (raid_disk == 0 ||
NeilBrownc148ffd2009-11-13 17:47:00 +11006701 raid_disk == raid_disks - 1)
6702 return 1;
6703 break;
6704 case ALGORITHM_LEFT_ASYMMETRIC_6:
6705 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6706 case ALGORITHM_LEFT_SYMMETRIC_6:
6707 case ALGORITHM_RIGHT_SYMMETRIC_6:
6708 if (raid_disk == raid_disks - 1)
6709 return 1;
6710 }
6711 return 0;
6712}
6713
Shaohua Li849674e2016-01-20 13:52:20 -08006714static int raid5_run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11006715{
NeilBrownd1688a62011-10-11 16:49:52 +11006716 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10006717 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11006718 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006719 struct md_rdev *rdev;
Shaohua Li713cf5a2015-08-13 14:32:03 -07006720 struct md_rdev *journal_dev = NULL;
NeilBrownc148ffd2009-11-13 17:47:00 +11006721 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11006722 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10006723 long long min_offset_diff = 0;
6724 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11006725
Andre Noll8c6ac862009-06-18 08:48:06 +10006726 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10006727 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10006728 " -- starting background reconstruction\n",
6729 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10006730
6731 rdev_for_each(rdev, mddev) {
6732 long long diff;
Shaohua Li713cf5a2015-08-13 14:32:03 -07006733
Shaohua Lif2076e72015-10-08 21:54:12 -07006734 if (test_bit(Journal, &rdev->flags)) {
Shaohua Li713cf5a2015-08-13 14:32:03 -07006735 journal_dev = rdev;
Shaohua Lif2076e72015-10-08 21:54:12 -07006736 continue;
6737 }
NeilBrownb5254dd2012-05-21 09:27:01 +10006738 if (rdev->raid_disk < 0)
6739 continue;
6740 diff = (rdev->new_data_offset - rdev->data_offset);
6741 if (first) {
6742 min_offset_diff = diff;
6743 first = 0;
6744 } else if (mddev->reshape_backwards &&
6745 diff < min_offset_diff)
6746 min_offset_diff = diff;
6747 else if (!mddev->reshape_backwards &&
6748 diff > min_offset_diff)
6749 min_offset_diff = diff;
6750 }
6751
NeilBrownf6705572006-03-27 01:18:11 -08006752 if (mddev->reshape_position != MaxSector) {
6753 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10006754 * Difficulties arise if the stripe we would write to
6755 * next is at or after the stripe we would read from next.
6756 * For a reshape that changes the number of devices, this
6757 * is only possible for a very short time, and mdadm makes
6758 * sure that time appears to have past before assembling
6759 * the array. So we fail if that time hasn't passed.
6760 * For a reshape that keeps the number of devices the same
6761 * mdadm must be monitoring the reshape can keeping the
6762 * critical areas read-only and backed up. It will start
6763 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08006764 */
6765 sector_t here_new, here_old;
6766 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11006767 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrown05256d92015-07-15 17:36:21 +10006768 int chunk_sectors;
6769 int new_data_disks;
NeilBrownf6705572006-03-27 01:18:11 -08006770
Shaohua Li713cf5a2015-08-13 14:32:03 -07006771 if (journal_dev) {
6772 printk(KERN_ERR "md/raid:%s: don't support reshape with journal - aborting.\n",
6773 mdname(mddev));
6774 return -EINVAL;
6775 }
6776
NeilBrown88ce4932009-03-31 15:24:23 +11006777 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10006778 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08006779 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08006780 mdname(mddev));
6781 return -EINVAL;
6782 }
NeilBrownf6705572006-03-27 01:18:11 -08006783 old_disks = mddev->raid_disks - mddev->delta_disks;
6784 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08006785 * further up in new geometry must map after here in old
6786 * geometry.
NeilBrown05256d92015-07-15 17:36:21 +10006787 * If the chunk sizes are different, then as we perform reshape
6788 * in units of the largest of the two, reshape_position needs
6789 * be a multiple of the largest chunk size times new data disks.
NeilBrownf6705572006-03-27 01:18:11 -08006790 */
6791 here_new = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10006792 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
6793 new_data_disks = mddev->raid_disks - max_degraded;
6794 if (sector_div(here_new, chunk_sectors * new_data_disks)) {
NeilBrown0c55e022010-05-03 14:09:02 +10006795 printk(KERN_ERR "md/raid:%s: reshape_position not "
6796 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08006797 return -EINVAL;
6798 }
NeilBrown05256d92015-07-15 17:36:21 +10006799 reshape_offset = here_new * chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006800 /* here_new is the stripe we will write to */
6801 here_old = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10006802 sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
NeilBrownf4168852007-02-28 20:11:53 -08006803 /* here_old is the first stripe that we might need to read
6804 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10006805 if (mddev->delta_disks == 0) {
6806 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10006807 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10006808 * and taking constant backups.
6809 * mdadm always starts a situation like this in
6810 * readonly mode so it can take control before
6811 * allowing any writes. So just check for that.
6812 */
NeilBrownb5254dd2012-05-21 09:27:01 +10006813 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
6814 abs(min_offset_diff) >= mddev->new_chunk_sectors)
6815 /* not really in-place - so OK */;
6816 else if (mddev->ro == 0) {
6817 printk(KERN_ERR "md/raid:%s: in-place reshape "
6818 "must be started in read-only mode "
6819 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10006820 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10006821 return -EINVAL;
6822 }
NeilBrown2c810cd2012-05-21 09:27:00 +10006823 } else if (mddev->reshape_backwards
NeilBrown05256d92015-07-15 17:36:21 +10006824 ? (here_new * chunk_sectors + min_offset_diff <=
6825 here_old * chunk_sectors)
6826 : (here_new * chunk_sectors >=
6827 here_old * chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08006828 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10006829 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
6830 "auto-recovery - aborting.\n",
6831 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08006832 return -EINVAL;
6833 }
NeilBrown0c55e022010-05-03 14:09:02 +10006834 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
6835 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08006836 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08006837 } else {
NeilBrown91adb562009-03-31 14:39:39 +11006838 BUG_ON(mddev->level != mddev->new_level);
6839 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10006840 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11006841 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08006842 }
6843
NeilBrown245f46c2009-03-31 14:39:39 +11006844 if (mddev->private == NULL)
6845 conf = setup_conf(mddev);
6846 else
6847 conf = mddev->private;
6848
NeilBrown91adb562009-03-31 14:39:39 +11006849 if (IS_ERR(conf))
6850 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08006851
Song Liu486b0f72016-08-19 15:34:01 -07006852 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
6853 if (!journal_dev) {
6854 pr_err("md/raid:%s: journal disk is missing, force array readonly\n",
6855 mdname(mddev));
6856 mddev->ro = 1;
6857 set_disk_ro(mddev->gendisk, 1);
6858 } else if (mddev->recovery_cp == MaxSector)
6859 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
Shaohua Li7dde2ad2015-10-08 21:54:10 -07006860 }
6861
NeilBrownb5254dd2012-05-21 09:27:01 +10006862 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11006863 mddev->thread = conf->thread;
6864 conf->thread = NULL;
6865 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866
NeilBrown17045f52011-12-23 10:17:53 +11006867 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
6868 i++) {
6869 rdev = conf->disks[i].rdev;
6870 if (!rdev && conf->disks[i].replacement) {
6871 /* The replacement is all we have yet */
6872 rdev = conf->disks[i].replacement;
6873 conf->disks[i].replacement = NULL;
6874 clear_bit(Replacement, &rdev->flags);
6875 conf->disks[i].rdev = rdev;
6876 }
6877 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11006878 continue;
NeilBrown17045f52011-12-23 10:17:53 +11006879 if (conf->disks[i].replacement &&
6880 conf->reshape_progress != MaxSector) {
6881 /* replacements and reshape simply do not mix. */
6882 printk(KERN_ERR "md: cannot handle concurrent "
6883 "replacement and reshape.\n");
6884 goto abort;
6885 }
NeilBrown2f115882010-06-17 17:41:03 +10006886 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11006887 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10006888 continue;
6889 }
NeilBrownc148ffd2009-11-13 17:47:00 +11006890 /* This disc is not fully in-sync. However if it
6891 * just stored parity (beyond the recovery_offset),
6892 * when we don't need to be concerned about the
6893 * array being dirty.
6894 * When reshape goes 'backwards', we never have
6895 * partially completed devices, so we only need
6896 * to worry about reshape going forwards.
6897 */
6898 /* Hack because v0.91 doesn't store recovery_offset properly. */
6899 if (mddev->major_version == 0 &&
6900 mddev->minor_version > 90)
6901 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07006902
NeilBrownc148ffd2009-11-13 17:47:00 +11006903 if (rdev->recovery_offset < reshape_offset) {
6904 /* We need to check old and new layout */
6905 if (!only_parity(rdev->raid_disk,
6906 conf->algorithm,
6907 conf->raid_disks,
6908 conf->max_degraded))
6909 continue;
6910 }
6911 if (!only_parity(rdev->raid_disk,
6912 conf->prev_algo,
6913 conf->previous_raid_disks,
6914 conf->max_degraded))
6915 continue;
6916 dirty_parity_disks++;
6917 }
NeilBrown91adb562009-03-31 14:39:39 +11006918
NeilBrown17045f52011-12-23 10:17:53 +11006919 /*
6920 * 0 for a fully functional array, 1 or 2 for a degraded array.
6921 */
NeilBrown908f4fb2011-12-23 10:17:50 +11006922 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923
NeilBrown674806d2010-06-16 17:17:53 +10006924 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10006925 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07006927 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 goto abort;
6929 }
6930
NeilBrown91adb562009-03-31 14:39:39 +11006931 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10006932 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11006933 mddev->resync_max_sectors = mddev->dev_sectors;
6934
NeilBrownc148ffd2009-11-13 17:47:00 +11006935 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006937 if (mddev->ok_start_degraded)
6938 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10006939 "md/raid:%s: starting dirty degraded array"
6940 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006941 mdname(mddev));
6942 else {
6943 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10006944 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006945 mdname(mddev));
6946 goto abort;
6947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 }
6949
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10006951 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
6952 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11006953 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
6954 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955 else
NeilBrown0c55e022010-05-03 14:09:02 +10006956 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
6957 " out of %d devices, algorithm %d\n",
6958 mdname(mddev), conf->level,
6959 mddev->raid_disks - mddev->degraded,
6960 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961
6962 print_raid5_conf(conf);
6963
NeilBrownfef9c612009-03-31 15:16:46 +11006964 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11006965 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08006966 atomic_set(&conf->reshape_stripes, 0);
6967 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6968 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6969 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6970 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6971 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006972 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08006973 }
6974
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10006976 if (mddev->to_remove == &raid5_attrs_group)
6977 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006978 else if (mddev->kobj.sd &&
6979 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08006980 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10006981 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08006982 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10006983 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6984
6985 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10006986 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11006987 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10006988 /* read-ahead size must cover two whole stripes, which
6989 * is 2 * (datadisks) * chunksize where 'n' is the
6990 * number of raid devices
6991 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992 int data_disks = conf->previous_raid_disks - conf->max_degraded;
6993 int stripe = data_disks *
6994 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
6995 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6996 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10006997
NeilBrown9f7c2222010-07-26 12:04:13 +10006998 chunk_size = mddev->chunk_sectors << 9;
6999 blk_queue_io_min(mddev->queue, chunk_size);
7000 blk_queue_io_opt(mddev->queue, chunk_size *
7001 (conf->raid_disks - conf->max_degraded));
Kent Overstreetc78afc62013-07-11 22:39:53 -07007002 mddev->queue->limits.raid_partial_stripes_expensive = 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007003 /*
7004 * We can only discard a whole stripe. It doesn't make sense to
7005 * discard data disk but write parity disk
7006 */
7007 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11007008 /* Round up to power of 2, as discard handling
7009 * currently assumes that */
7010 while ((stripe-1) & stripe)
7011 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007012 mddev->queue->limits.discard_alignment = stripe;
7013 mddev->queue->limits.discard_granularity = stripe;
Konstantin Khlebnikov8beb2522016-11-27 19:32:32 +03007014
7015 /*
7016 * We use 16-bit counter of active stripes in bi_phys_segments
7017 * (minus one for over-loaded initialization)
7018 */
7019 blk_queue_max_hw_sectors(mddev->queue, 0xfffe * STRIPE_SECTORS);
7020 blk_queue_max_discard_sectors(mddev->queue,
7021 0xfffe * STRIPE_SECTORS);
7022
Shaohua Li620125f2012-10-11 13:49:05 +11007023 /*
7024 * unaligned part of discard request will be ignored, so can't
NeilBrown8e0e99b2014-10-02 13:45:00 +10007025 * guarantee discard_zeroes_data
Shaohua Li620125f2012-10-11 13:49:05 +11007026 */
7027 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10007028
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007029 blk_queue_max_write_same_sectors(mddev->queue, 0);
7030
NeilBrown05616be2012-05-21 09:27:00 +10007031 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007032 disk_stack_limits(mddev->gendisk, rdev->bdev,
7033 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10007034 disk_stack_limits(mddev->gendisk, rdev->bdev,
7035 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11007036 /*
7037 * discard_zeroes_data is required, otherwise data
7038 * could be lost. Consider a scenario: discard a stripe
7039 * (the stripe could be inconsistent if
7040 * discard_zeroes_data is 0); write one disk of the
7041 * stripe (the stripe could be inconsistent again
7042 * depending on which disks are used to calculate
7043 * parity); the disk is broken; The stripe data of this
7044 * disk is lost.
7045 */
7046 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
7047 !bdev_get_queue(rdev->bdev)->
7048 limits.discard_zeroes_data)
7049 discard_supported = false;
NeilBrown8e0e99b2014-10-02 13:45:00 +10007050 /* Unfortunately, discard_zeroes_data is not currently
7051 * a guarantee - just a hint. So we only allow DISCARD
7052 * if the sysadmin has confirmed that only safe devices
7053 * are in use by setting a module parameter.
7054 */
7055 if (!devices_handle_discard_safely) {
7056 if (discard_supported) {
7057 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
7058 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
7059 }
7060 discard_supported = false;
7061 }
NeilBrown05616be2012-05-21 09:27:00 +10007062 }
Shaohua Li620125f2012-10-11 13:49:05 +11007063
7064 if (discard_supported &&
Jes Sorensene7597e62016-02-16 16:44:24 -05007065 mddev->queue->limits.max_discard_sectors >= (stripe >> 9) &&
7066 mddev->queue->limits.discard_granularity >= stripe)
Shaohua Li620125f2012-10-11 13:49:05 +11007067 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
7068 mddev->queue);
7069 else
7070 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
7071 mddev->queue);
Shaohua Li1dffddd2016-09-08 10:49:06 -07007072
7073 blk_queue_max_hw_sectors(mddev->queue, UINT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074 }
7075
Shaohua Li5c7e81c2015-08-13 14:32:04 -07007076 if (journal_dev) {
7077 char b[BDEVNAME_SIZE];
7078
7079 printk(KERN_INFO"md/raid:%s: using device %s as journal\n",
7080 mdname(mddev), bdevname(journal_dev->bdev, b));
7081 r5l_init_log(conf, journal_dev);
7082 }
7083
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084 return 0;
7085abort:
NeilBrown01f96c02011-09-21 15:30:20 +10007086 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11007087 print_raid5_conf(conf);
7088 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10007090 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091 return -EIO;
7092}
7093
NeilBrownafa0f552014-12-15 12:56:58 +11007094static void raid5_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095{
NeilBrownafa0f552014-12-15 12:56:58 +11007096 struct r5conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097
Dan Williams95fc17a2009-07-31 12:39:15 +10007098 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10007099 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100}
7101
Shaohua Li849674e2016-01-20 13:52:20 -08007102static void raid5_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103{
NeilBrownd1688a62011-10-11 16:49:52 +11007104 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 int i;
7106
Andre Noll9d8f0362009-06-18 08:45:01 +10007107 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
NeilBrown3cb5edf2015-07-15 17:24:17 +10007108 conf->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07007109 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
NeilBrown5fd13352016-06-02 16:19:52 +10007110 rcu_read_lock();
7111 for (i = 0; i < conf->raid_disks; i++) {
7112 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
7113 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
7114 }
7115 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117}
7118
NeilBrownd1688a62011-10-11 16:49:52 +11007119static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120{
7121 int i;
7122 struct disk_info *tmp;
7123
NeilBrown0c55e022010-05-03 14:09:02 +10007124 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125 if (!conf) {
7126 printk("(conf==NULL)\n");
7127 return;
7128 }
NeilBrown0c55e022010-05-03 14:09:02 +10007129 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
7130 conf->raid_disks,
7131 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132
7133 for (i = 0; i < conf->raid_disks; i++) {
7134 char b[BDEVNAME_SIZE];
7135 tmp = conf->disks + i;
7136 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10007137 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
7138 i, !test_bit(Faulty, &tmp->rdev->flags),
7139 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 }
7141}
7142
NeilBrownfd01b882011-10-11 16:47:53 +11007143static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144{
7145 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11007146 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10007148 int count = 0;
7149 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150
7151 for (i = 0; i < conf->raid_disks; i++) {
7152 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11007153 if (tmp->replacement
7154 && tmp->replacement->recovery_offset == MaxSector
7155 && !test_bit(Faulty, &tmp->replacement->flags)
7156 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
7157 /* Replacement has just become active. */
7158 if (!tmp->rdev
7159 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
7160 count++;
7161 if (tmp->rdev) {
7162 /* Replaced device not technically faulty,
7163 * but we need to be sure it gets removed
7164 * and never re-added.
7165 */
7166 set_bit(Faulty, &tmp->rdev->flags);
7167 sysfs_notify_dirent_safe(
7168 tmp->rdev->sysfs_state);
7169 }
7170 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
7171 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10007172 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08007173 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07007174 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10007175 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11007176 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177 }
7178 }
NeilBrown6b965622010-08-18 11:56:59 +10007179 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11007180 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007181 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007183 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184}
7185
NeilBrownb8321b62011-12-23 10:17:51 +11007186static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187{
NeilBrownd1688a62011-10-11 16:49:52 +11007188 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11007190 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11007191 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192 struct disk_info *p = conf->disks + number;
7193
7194 print_raid5_conf(conf);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007195 if (test_bit(Journal, &rdev->flags) && conf->log) {
7196 struct r5l_log *log;
Shaohua Lic2bb6242015-10-08 21:54:07 -07007197 /*
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007198 * we can't wait pending write here, as this is called in
7199 * raid5d, wait will deadlock.
Shaohua Lic2bb6242015-10-08 21:54:07 -07007200 */
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007201 if (atomic_read(&mddev->writes_pending))
7202 return -EBUSY;
7203 log = conf->log;
7204 conf->log = NULL;
7205 synchronize_rcu();
7206 r5l_exit_log(log);
7207 return 0;
Shaohua Lic2bb6242015-10-08 21:54:07 -07007208 }
NeilBrown657e3e42011-12-23 10:17:52 +11007209 if (rdev == p->rdev)
7210 rdevp = &p->rdev;
7211 else if (rdev == p->replacement)
7212 rdevp = &p->replacement;
7213 else
7214 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11007215
NeilBrown657e3e42011-12-23 10:17:52 +11007216 if (number >= conf->raid_disks &&
7217 conf->reshape_progress == MaxSector)
7218 clear_bit(In_sync, &rdev->flags);
7219
7220 if (test_bit(In_sync, &rdev->flags) ||
7221 atomic_read(&rdev->nr_pending)) {
7222 err = -EBUSY;
7223 goto abort;
7224 }
7225 /* Only remove non-faulty devices if recovery
7226 * isn't possible.
7227 */
7228 if (!test_bit(Faulty, &rdev->flags) &&
7229 mddev->recovery_disabled != conf->recovery_disabled &&
7230 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11007231 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11007232 number < conf->raid_disks) {
7233 err = -EBUSY;
7234 goto abort;
7235 }
7236 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10007237 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
7238 synchronize_rcu();
7239 if (atomic_read(&rdev->nr_pending)) {
7240 /* lost the race, try later */
7241 err = -EBUSY;
7242 *rdevp = rdev;
7243 }
7244 }
7245 if (p->replacement) {
NeilBrowndd054fc2011-12-23 10:17:53 +11007246 /* We must have just cleared 'rdev' */
7247 p->rdev = p->replacement;
7248 clear_bit(Replacement, &p->replacement->flags);
7249 smp_mb(); /* Make sure other CPUs may see both as identical
7250 * but will never see neither - if they are careful
7251 */
7252 p->replacement = NULL;
7253 clear_bit(WantReplacement, &rdev->flags);
7254 } else
7255 /* We might have just removed the Replacement as faulty-
7256 * clear the bit just in case
7257 */
7258 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259abort:
7260
7261 print_raid5_conf(conf);
7262 return err;
7263}
7264
NeilBrownfd01b882011-10-11 16:47:53 +11007265static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266{
NeilBrownd1688a62011-10-11 16:49:52 +11007267 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10007268 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269 int disk;
7270 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10007271 int first = 0;
7272 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007274 if (test_bit(Journal, &rdev->flags)) {
7275 char b[BDEVNAME_SIZE];
7276 if (conf->log)
7277 return -EBUSY;
7278
7279 rdev->raid_disk = 0;
7280 /*
7281 * The array is in readonly mode if journal is missing, so no
7282 * write requests running. We should be safe
7283 */
7284 r5l_init_log(conf, rdev);
7285 printk(KERN_INFO"md/raid:%s: using device %s as journal\n",
7286 mdname(mddev), bdevname(rdev->bdev, b));
7287 return 0;
7288 }
NeilBrown7f0da592011-07-28 11:39:22 +10007289 if (mddev->recovery_disabled == conf->recovery_disabled)
7290 return -EBUSY;
7291
NeilBrowndc10c642012-03-19 12:46:37 +11007292 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007293 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10007294 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295
Neil Brown6c2fce22008-06-28 08:31:31 +10007296 if (rdev->raid_disk >= 0)
7297 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298
7299 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07007300 * find the disk ... but prefer rdev->saved_raid_disk
7301 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007302 */
NeilBrown16a53ec2006-06-26 00:27:38 -07007303 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10007304 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07007305 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10007306 first = rdev->saved_raid_disk;
7307
7308 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007309 p = conf->disks + disk;
7310 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08007311 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007312 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10007313 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07007314 if (rdev->saved_raid_disk != disk)
7315 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08007316 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10007317 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007319 }
7320 for (disk = first; disk <= last; disk++) {
7321 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007322 if (test_bit(WantReplacement, &p->rdev->flags) &&
7323 p->replacement == NULL) {
7324 clear_bit(In_sync, &rdev->flags);
7325 set_bit(Replacement, &rdev->flags);
7326 rdev->raid_disk = disk;
7327 err = 0;
7328 conf->fullsync = 1;
7329 rcu_assign_pointer(p->replacement, rdev);
7330 break;
7331 }
7332 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007333out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007334 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10007335 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336}
7337
NeilBrownfd01b882011-10-11 16:47:53 +11007338static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339{
7340 /* no resync is happening, and there is enough space
7341 * on all devices, so we can resize.
7342 * We need to make sure resync covers any new space.
7343 * If the array is shrinking we should possibly wait until
7344 * any io in the removed space completes, but it hardly seems
7345 * worth it.
7346 */
NeilBrowna4a61252012-05-22 13:55:27 +10007347 sector_t newsize;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007348 struct r5conf *conf = mddev->private;
7349
Shaohua Li713cf5a2015-08-13 14:32:03 -07007350 if (conf->log)
7351 return -EINVAL;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007352 sectors &= ~((sector_t)conf->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10007353 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
7354 if (mddev->external_size &&
7355 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11007356 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10007357 if (mddev->bitmap) {
7358 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
7359 if (ret)
7360 return ret;
7361 }
7362 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10007363 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10007364 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10007365 if (sectors > mddev->dev_sectors &&
7366 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11007367 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007368 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7369 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007370 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07007371 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007372 return 0;
7373}
7374
NeilBrownfd01b882011-10-11 16:47:53 +11007375static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10007376{
7377 /* Can only proceed if there are plenty of stripe_heads.
7378 * We need a minimum of one full stripe,, and for sensible progress
7379 * it is best to have about 4 times that.
7380 * If we require 4 times, then the default 256 4K stripe_heads will
7381 * allow for chunk sizes up to 256K, which is probably OK.
7382 * If the chunk size is greater, user-space should request more
7383 * stripe_heads first.
7384 */
NeilBrownd1688a62011-10-11 16:49:52 +11007385 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10007386 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007387 > conf->min_nr_stripes ||
NeilBrown01ee22b2009-06-18 08:47:20 +10007388 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007389 > conf->min_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10007390 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7391 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10007392 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
7393 / STRIPE_SIZE)*4);
7394 return 0;
7395 }
7396 return 1;
7397}
7398
NeilBrownfd01b882011-10-11 16:47:53 +11007399static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08007400{
NeilBrownd1688a62011-10-11 16:49:52 +11007401 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08007402
Shaohua Li713cf5a2015-08-13 14:32:03 -07007403 if (conf->log)
7404 return -EINVAL;
NeilBrown88ce4932009-03-31 15:24:23 +11007405 if (mddev->delta_disks == 0 &&
7406 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10007407 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10007408 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10007409 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11007410 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10007411 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11007412 /* We might be able to shrink, but the devices must
7413 * be made bigger first.
7414 * For raid6, 4 is the minimum size.
7415 * Otherwise 2 is the minimum
7416 */
7417 int min = 2;
7418 if (mddev->level == 6)
7419 min = 4;
7420 if (mddev->raid_disks + mddev->delta_disks < min)
7421 return -EINVAL;
7422 }
NeilBrown29269552006-03-27 01:18:10 -08007423
NeilBrown01ee22b2009-06-18 08:47:20 +10007424 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08007425 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08007426
NeilBrown738a2732015-05-08 18:19:39 +10007427 if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
7428 mddev->delta_disks > 0)
7429 if (resize_chunks(conf,
7430 conf->previous_raid_disks
7431 + max(0, mddev->delta_disks),
7432 max(mddev->new_chunk_sectors,
7433 mddev->chunk_sectors)
7434 ) < 0)
7435 return -ENOMEM;
NeilBrowne56108d62012-10-11 14:24:13 +11007436 return resize_stripes(conf, (conf->previous_raid_disks
7437 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08007438}
7439
NeilBrownfd01b882011-10-11 16:47:53 +11007440static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08007441{
NeilBrownd1688a62011-10-11 16:49:52 +11007442 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11007443 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08007444 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07007445 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08007446
NeilBrownf4168852007-02-28 20:11:53 -08007447 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08007448 return -EBUSY;
7449
NeilBrown01ee22b2009-06-18 08:47:20 +10007450 if (!check_stripe_cache(mddev))
7451 return -ENOSPC;
7452
NeilBrown30b67642012-05-22 13:55:28 +10007453 if (has_failed(conf))
7454 return -EINVAL;
7455
NeilBrownc6563a82012-05-21 09:27:00 +10007456 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11007457 if (!test_bit(In_sync, &rdev->flags)
7458 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08007459 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10007460 }
NeilBrown63c70c42006-03-27 01:18:13 -08007461
NeilBrownf4168852007-02-28 20:11:53 -08007462 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08007463 /* Not enough devices even to make a degraded array
7464 * of that size
7465 */
7466 return -EINVAL;
7467
NeilBrownec32a2b2009-03-31 15:17:38 +11007468 /* Refuse to reduce size of the array. Any reductions in
7469 * array size must be through explicit setting of array_size
7470 * attribute.
7471 */
7472 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
7473 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10007474 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11007475 "before number of disks\n", mdname(mddev));
7476 return -EINVAL;
7477 }
7478
NeilBrownf6705572006-03-27 01:18:11 -08007479 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08007480 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10007481 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007482 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08007483 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007484 conf->prev_chunk_sectors = conf->chunk_sectors;
7485 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11007486 conf->prev_algo = conf->algorithm;
7487 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10007488 conf->generation++;
7489 /* Code that selects data_offset needs to see the generation update
7490 * if reshape_progress has been set - so a memory barrier needed.
7491 */
7492 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10007493 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11007494 conf->reshape_progress = raid5_size(mddev, 0, 0);
7495 else
7496 conf->reshape_progress = 0;
7497 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10007498 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007499 spin_unlock_irq(&conf->device_lock);
7500
NeilBrown4d77e3b2013-08-27 15:57:47 +10007501 /* Now make sure any requests that proceeded on the assumption
7502 * the reshape wasn't running - like Discard or Read - have
7503 * completed.
7504 */
7505 mddev_suspend(mddev);
7506 mddev_resume(mddev);
7507
NeilBrown29269552006-03-27 01:18:10 -08007508 /* Add some new drives, as many as will fit.
7509 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10007510 * Don't add devices if we are reducing the number of
7511 * devices in the array. This is because it is not possible
7512 * to correctly record the "partially reconstructed" state of
7513 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08007514 */
NeilBrown87a8dec2011-01-31 11:57:43 +11007515 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11007516 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11007517 if (rdev->raid_disk < 0 &&
7518 !test_bit(Faulty, &rdev->flags)) {
7519 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11007520 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11007521 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11007522 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11007523 else
NeilBrown87a8dec2011-01-31 11:57:43 +11007524 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10007525
7526 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11007527 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11007528 }
NeilBrown87a8dec2011-01-31 11:57:43 +11007529 } else if (rdev->raid_disk >= conf->previous_raid_disks
7530 && !test_bit(Faulty, &rdev->flags)) {
7531 /* This is a spare that was manually added */
7532 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11007533 }
NeilBrown29269552006-03-27 01:18:10 -08007534
NeilBrown87a8dec2011-01-31 11:57:43 +11007535 /* When a reshape changes the number of devices,
7536 * ->degraded is measured against the larger of the
7537 * pre and post number of devices.
7538 */
NeilBrownec32a2b2009-03-31 15:17:38 +11007539 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11007540 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11007541 spin_unlock_irqrestore(&conf->device_lock, flags);
7542 }
NeilBrown63c70c42006-03-27 01:18:13 -08007543 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10007544 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07007545 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08007546
NeilBrown29269552006-03-27 01:18:10 -08007547 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7548 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10007549 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown29269552006-03-27 01:18:10 -08007550 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7551 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7552 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007553 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08007554 if (!mddev->sync_thread) {
7555 mddev->recovery = 0;
7556 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11007557 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007558 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11007559 mddev->new_chunk_sectors =
7560 conf->chunk_sectors = conf->prev_chunk_sectors;
7561 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10007562 rdev_for_each(rdev, mddev)
7563 rdev->new_data_offset = rdev->data_offset;
7564 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11007565 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11007566 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11007567 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11007568 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007569 spin_unlock_irq(&conf->device_lock);
7570 return -EAGAIN;
7571 }
NeilBrownc8f517c2009-03-31 15:28:40 +11007572 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08007573 md_wakeup_thread(mddev->sync_thread);
7574 md_new_event(mddev);
7575 return 0;
7576}
NeilBrown29269552006-03-27 01:18:10 -08007577
NeilBrownec32a2b2009-03-31 15:17:38 +11007578/* This is called from the reshape thread and should make any
7579 * changes needed in 'conf'
7580 */
NeilBrownd1688a62011-10-11 16:49:52 +11007581static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08007582{
NeilBrown29269552006-03-27 01:18:10 -08007583
NeilBrownf6705572006-03-27 01:18:11 -08007584 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
Dan Williams80c3a6c2009-03-17 18:10:40 -07007585
NeilBrownf6705572006-03-27 01:18:11 -08007586 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11007587 conf->previous_raid_disks = conf->raid_disks;
Xiao Ni1e951482017-07-05 17:34:04 +08007588 md_finish_reshape(conf->mddev);
NeilBrown05616be2012-05-21 09:27:00 +10007589 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11007590 conf->reshape_progress = MaxSector;
NeilBrown6cbd8142015-07-24 13:30:32 +10007591 conf->mddev->reshape_position = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08007592 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11007593 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07007594
7595 /* read-ahead size must cover two whole stripes, which is
7596 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
7597 */
NeilBrown4a5add42010-06-01 19:37:28 +10007598 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11007599 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007600 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11007601 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07007602 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
7603 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
7604 }
NeilBrown29269552006-03-27 01:18:10 -08007605 }
NeilBrown29269552006-03-27 01:18:10 -08007606}
7607
NeilBrownec32a2b2009-03-31 15:17:38 +11007608/* This is called from the raid5d thread with mddev_lock held.
7609 * It makes config changes to the device.
7610 */
NeilBrownfd01b882011-10-11 16:47:53 +11007611static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11007612{
NeilBrownd1688a62011-10-11 16:49:52 +11007613 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11007614
7615 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7616
NeilBrownec32a2b2009-03-31 15:17:38 +11007617 if (mddev->delta_disks > 0) {
7618 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
Heinz Mauelshagenfe67d192016-05-03 22:15:31 +02007619 if (mddev->queue) {
7620 set_capacity(mddev->gendisk, mddev->array_sectors);
7621 revalidate_disk(mddev->gendisk);
7622 }
NeilBrownec32a2b2009-03-31 15:17:38 +11007623 } else {
7624 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11007625 spin_lock_irq(&conf->device_lock);
7626 mddev->degraded = calc_degraded(conf);
7627 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11007628 for (d = conf->raid_disks ;
7629 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10007630 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11007631 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10007632 if (rdev)
7633 clear_bit(In_sync, &rdev->flags);
7634 rdev = conf->disks[d].replacement;
7635 if (rdev)
7636 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10007637 }
NeilBrowncea9c222009-03-31 15:15:05 +11007638 }
NeilBrown88ce4932009-03-31 15:24:23 +11007639 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007640 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11007641 mddev->reshape_position = MaxSector;
7642 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007643 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11007644 }
7645}
7646
NeilBrownfd01b882011-10-11 16:47:53 +11007647static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07007648{
NeilBrownd1688a62011-10-11 16:49:52 +11007649 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07007650
7651 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08007652 case 2: /* resume for a suspend */
7653 wake_up(&conf->wait_for_overlap);
7654 break;
7655
NeilBrown72626682005-09-09 16:23:54 -07007656 case 1: /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11007657 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10007658 /* '2' tells resync/reshape to pause so that all
7659 * active stripes can drain
7660 */
7661 conf->quiesce = 2;
Yuanhan Liub1b46482015-05-08 18:19:06 +10007662 wait_event_cmd(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08007663 atomic_read(&conf->active_stripes) == 0 &&
7664 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11007665 unlock_all_device_hash_locks_irq(conf),
7666 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10007667 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11007668 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10007669 /* allow reshape to continue */
7670 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07007671 break;
7672
7673 case 0: /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11007674 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07007675 conf->quiesce = 0;
Yuanhan Liub1b46482015-05-08 18:19:06 +10007676 wake_up(&conf->wait_for_quiescent);
NeilBrowne464eaf2006-03-27 01:18:14 -08007677 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11007678 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07007679 break;
7680 }
Shaohua Lie6c033f2015-10-04 09:20:12 -07007681 r5l_quiesce(conf->log, state);
NeilBrown72626682005-09-09 16:23:54 -07007682}
NeilBrownb15c2e52006-01-06 00:20:16 -08007683
NeilBrownfd01b882011-10-11 16:47:53 +11007684static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11007685{
NeilBrowne373ab12011-10-11 16:48:59 +11007686 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07007687 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11007688
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007689 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11007690 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10007691 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
7692 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007693 return ERR_PTR(-EINVAL);
7694 }
7695
NeilBrowne373ab12011-10-11 16:48:59 +11007696 sectors = raid0_conf->strip_zone[0].zone_end;
7697 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10007698 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007699 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11007700 mddev->new_layout = ALGORITHM_PARITY_N;
7701 mddev->new_chunk_sectors = mddev->chunk_sectors;
7702 mddev->raid_disks += 1;
7703 mddev->delta_disks = 1;
7704 /* make sure it will be not marked as dirty */
7705 mddev->recovery_cp = MaxSector;
7706
7707 return setup_conf(mddev);
7708}
7709
NeilBrownfd01b882011-10-11 16:47:53 +11007710static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11007711{
7712 int chunksect;
7713
7714 if (mddev->raid_disks != 2 ||
7715 mddev->degraded > 1)
7716 return ERR_PTR(-EINVAL);
7717
7718 /* Should check if there are write-behind devices? */
7719
7720 chunksect = 64*2; /* 64K by default */
7721
7722 /* The array must be an exact multiple of chunksize */
7723 while (chunksect && (mddev->array_sectors & (chunksect-1)))
7724 chunksect >>= 1;
7725
7726 if ((chunksect<<9) < STRIPE_SIZE)
7727 /* array size does not allow a suitable chunk size */
7728 return ERR_PTR(-EINVAL);
7729
7730 mddev->new_level = 5;
7731 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10007732 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11007733
7734 return setup_conf(mddev);
7735}
7736
NeilBrownfd01b882011-10-11 16:47:53 +11007737static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11007738{
7739 int new_layout;
7740
7741 switch (mddev->layout) {
7742 case ALGORITHM_LEFT_ASYMMETRIC_6:
7743 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
7744 break;
7745 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7746 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
7747 break;
7748 case ALGORITHM_LEFT_SYMMETRIC_6:
7749 new_layout = ALGORITHM_LEFT_SYMMETRIC;
7750 break;
7751 case ALGORITHM_RIGHT_SYMMETRIC_6:
7752 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
7753 break;
7754 case ALGORITHM_PARITY_0_6:
7755 new_layout = ALGORITHM_PARITY_0;
7756 break;
7757 case ALGORITHM_PARITY_N:
7758 new_layout = ALGORITHM_PARITY_N;
7759 break;
7760 default:
7761 return ERR_PTR(-EINVAL);
7762 }
7763 mddev->new_level = 5;
7764 mddev->new_layout = new_layout;
7765 mddev->delta_disks = -1;
7766 mddev->raid_disks -= 1;
7767 return setup_conf(mddev);
7768}
7769
NeilBrownfd01b882011-10-11 16:47:53 +11007770static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11007771{
NeilBrown88ce4932009-03-31 15:24:23 +11007772 /* For a 2-drive array, the layout and chunk size can be changed
7773 * immediately as not restriping is needed.
7774 * For larger arrays we record the new value - after validation
7775 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11007776 */
NeilBrownd1688a62011-10-11 16:49:52 +11007777 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10007778 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11007779
NeilBrown597a7112009-06-18 08:47:42 +10007780 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11007781 return -EINVAL;
7782 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10007783 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11007784 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007785 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11007786 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007787 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11007788 /* not factor of array size */
7789 return -EINVAL;
7790 }
7791
7792 /* They look valid */
7793
NeilBrown88ce4932009-03-31 15:24:23 +11007794 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10007795 /* can make the change immediately */
7796 if (mddev->new_layout >= 0) {
7797 conf->algorithm = mddev->new_layout;
7798 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11007799 }
7800 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10007801 conf->chunk_sectors = new_chunk ;
7802 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11007803 }
7804 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7805 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11007806 }
NeilBrown50ac1682009-06-18 08:47:55 +10007807 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11007808}
7809
NeilBrownfd01b882011-10-11 16:47:53 +11007810static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11007811{
NeilBrown597a7112009-06-18 08:47:42 +10007812 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10007813
NeilBrown597a7112009-06-18 08:47:42 +10007814 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11007815 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11007816 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10007817 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11007818 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007819 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11007820 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007821 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11007822 /* not factor of array size */
7823 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11007824 }
NeilBrown88ce4932009-03-31 15:24:23 +11007825
7826 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10007827 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11007828}
7829
NeilBrownfd01b882011-10-11 16:47:53 +11007830static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11007831{
7832 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007833 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11007834 * raid1 - if there are two drives. We need to know the chunk size
7835 * raid4 - trivial - just use a raid4 layout.
7836 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11007837 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007838 if (mddev->level == 0)
7839 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11007840 if (mddev->level == 1)
7841 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11007842 if (mddev->level == 4) {
7843 mddev->new_layout = ALGORITHM_PARITY_N;
7844 mddev->new_level = 5;
7845 return setup_conf(mddev);
7846 }
NeilBrownfc9739c2009-03-31 14:57:20 +11007847 if (mddev->level == 6)
7848 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11007849
7850 return ERR_PTR(-EINVAL);
7851}
7852
NeilBrownfd01b882011-10-11 16:47:53 +11007853static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11007854{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007855 /* raid4 can take over:
7856 * raid0 - if there is only one strip zone
7857 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11007858 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007859 if (mddev->level == 0)
7860 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11007861 if (mddev->level == 5 &&
7862 mddev->layout == ALGORITHM_PARITY_N) {
7863 mddev->new_layout = 0;
7864 mddev->new_level = 4;
7865 return setup_conf(mddev);
7866 }
7867 return ERR_PTR(-EINVAL);
7868}
NeilBrownd562b0c2009-03-31 14:39:39 +11007869
NeilBrown84fc4b52011-10-11 16:49:58 +11007870static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11007871
NeilBrownfd01b882011-10-11 16:47:53 +11007872static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11007873{
7874 /* Currently can only take over a raid5. We map the
7875 * personality to an equivalent raid6 personality
7876 * with the Q block at the end.
7877 */
7878 int new_layout;
7879
7880 if (mddev->pers != &raid5_personality)
7881 return ERR_PTR(-EINVAL);
7882 if (mddev->degraded > 1)
7883 return ERR_PTR(-EINVAL);
7884 if (mddev->raid_disks > 253)
7885 return ERR_PTR(-EINVAL);
7886 if (mddev->raid_disks < 3)
7887 return ERR_PTR(-EINVAL);
7888
7889 switch (mddev->layout) {
7890 case ALGORITHM_LEFT_ASYMMETRIC:
7891 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
7892 break;
7893 case ALGORITHM_RIGHT_ASYMMETRIC:
7894 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
7895 break;
7896 case ALGORITHM_LEFT_SYMMETRIC:
7897 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
7898 break;
7899 case ALGORITHM_RIGHT_SYMMETRIC:
7900 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
7901 break;
7902 case ALGORITHM_PARITY_0:
7903 new_layout = ALGORITHM_PARITY_0_6;
7904 break;
7905 case ALGORITHM_PARITY_N:
7906 new_layout = ALGORITHM_PARITY_N;
7907 break;
7908 default:
7909 return ERR_PTR(-EINVAL);
7910 }
7911 mddev->new_level = 6;
7912 mddev->new_layout = new_layout;
7913 mddev->delta_disks = 1;
7914 mddev->raid_disks += 1;
7915 return setup_conf(mddev);
7916}
7917
NeilBrown84fc4b52011-10-11 16:49:58 +11007918static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07007919{
7920 .name = "raid6",
7921 .level = 6,
7922 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08007923 .make_request = raid5_make_request,
7924 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11007925 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08007926 .status = raid5_status,
7927 .error_handler = raid5_error,
NeilBrown16a53ec2006-06-26 00:27:38 -07007928 .hot_add_disk = raid5_add_disk,
7929 .hot_remove_disk= raid5_remove_disk,
7930 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08007931 .sync_request = raid5_sync_request,
NeilBrown16a53ec2006-06-26 00:27:38 -07007932 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07007933 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10007934 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08007935 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007936 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07007937 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11007938 .takeover = raid6_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11007939 .congested = raid5_congested,
NeilBrown16a53ec2006-06-26 00:27:38 -07007940};
NeilBrown84fc4b52011-10-11 16:49:58 +11007941static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007942{
7943 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08007944 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007945 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08007946 .make_request = raid5_make_request,
7947 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11007948 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08007949 .status = raid5_status,
7950 .error_handler = raid5_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007951 .hot_add_disk = raid5_add_disk,
7952 .hot_remove_disk= raid5_remove_disk,
7953 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08007954 .sync_request = raid5_sync_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007955 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07007956 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08007957 .check_reshape = raid5_check_reshape,
7958 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007959 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07007960 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11007961 .takeover = raid5_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11007962 .congested = raid5_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007963};
7964
NeilBrown84fc4b52011-10-11 16:49:58 +11007965static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007966{
NeilBrown2604b702006-01-06 00:20:36 -08007967 .name = "raid4",
7968 .level = 4,
7969 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08007970 .make_request = raid5_make_request,
7971 .run = raid5_run,
NeilBrownafa0f552014-12-15 12:56:58 +11007972 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08007973 .status = raid5_status,
7974 .error_handler = raid5_error,
NeilBrown2604b702006-01-06 00:20:36 -08007975 .hot_add_disk = raid5_add_disk,
7976 .hot_remove_disk= raid5_remove_disk,
7977 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08007978 .sync_request = raid5_sync_request,
NeilBrown2604b702006-01-06 00:20:36 -08007979 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07007980 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08007981 .check_reshape = raid5_check_reshape,
7982 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007983 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08007984 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11007985 .takeover = raid4_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11007986 .congested = raid5_congested,
NeilBrown2604b702006-01-06 00:20:36 -08007987};
7988
7989static int __init raid5_init(void)
7990{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02007991 int ret;
7992
Shaohua Li851c30c2013-08-28 14:30:16 +08007993 raid5_wq = alloc_workqueue("raid5wq",
7994 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
7995 if (!raid5_wq)
7996 return -ENOMEM;
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02007997
7998 ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
7999 "md/raid5:prepare",
8000 raid456_cpu_up_prepare,
8001 raid456_cpu_dead);
8002 if (ret) {
8003 destroy_workqueue(raid5_wq);
8004 return ret;
8005 }
NeilBrown16a53ec2006-06-26 00:27:38 -07008006 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008007 register_md_personality(&raid5_personality);
8008 register_md_personality(&raid4_personality);
8009 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008010}
8011
NeilBrown2604b702006-01-06 00:20:36 -08008012static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008013{
NeilBrown16a53ec2006-06-26 00:27:38 -07008014 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008015 unregister_md_personality(&raid5_personality);
8016 unregister_md_personality(&raid4_personality);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008017 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
Shaohua Li851c30c2013-08-28 14:30:16 +08008018 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008019}
8020
8021module_init(raid5_init);
8022module_exit(raid5_exit);
8023MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008024MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008025MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08008026MODULE_ALIAS("md-raid5");
8027MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08008028MODULE_ALIAS("md-level-5");
8029MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07008030MODULE_ALIAS("md-personality-8"); /* RAID6 */
8031MODULE_ALIAS("md-raid6");
8032MODULE_ALIAS("md-level-6");
8033
8034/* This used to be two separate modules, they were: */
8035MODULE_ALIAS("raid5");
8036MODULE_ALIAS("raid6");