blob: b200c195160c995f2b34edb353bf037d636ab6ca [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;
113 local_irq_disable();
114 spin_lock(conf->hash_locks);
115 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
116 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
117 spin_lock(&conf->device_lock);
118}
119
120static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
121{
122 int i;
123 spin_unlock(&conf->device_lock);
124 for (i = NR_STRIPE_HASH_LOCKS; i; i--)
125 spin_unlock(conf->hash_locks + i - 1);
126 local_irq_enable();
127}
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/* bio's attached to a stripe+device for I/O are linked together in bi_sector
130 * order without overlap. There may be several bio's per stripe+device, and
131 * a bio could span several devices.
132 * When walking this list for a particular stripe+device, we must never proceed
133 * beyond a bio that extends past this device, as the next bio might no longer
134 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +1100135 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 * of the current stripe+device
137 */
NeilBrowndb298e12011-10-07 14:23:00 +1100138static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
139{
Kent Overstreetaa8b57a2013-02-05 15:19:29 -0800140 int sectors = bio_sectors(bio);
Kent Overstreet4f024f32013-10-11 15:44:27 -0700141 if (bio->bi_iter.bi_sector + sectors < sector + STRIPE_SECTORS)
NeilBrowndb298e12011-10-07 14:23:00 +1100142 return bio->bi_next;
143 else
144 return NULL;
145}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jens Axboe960e7392008-08-15 10:41:18 +0200147/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200148 * We maintain a biased count of active stripes in the bottom 16 bits of
149 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200150 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000151static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200152{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000153 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
154 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200155}
156
Shaohua Lie7836bd62012-07-19 16:01:31 +1000157static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200158{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000159 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
160 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200161}
162
Shaohua Lie7836bd62012-07-19 16:01:31 +1000163static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200164{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000165 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
166 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200167}
168
Shaohua Lie7836bd62012-07-19 16:01:31 +1000169static inline void raid5_set_bi_processed_stripes(struct bio *bio,
170 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200171{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000172 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
173 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200174
Shaohua Lie7836bd62012-07-19 16:01:31 +1000175 do {
176 old = atomic_read(segments);
177 new = (old & 0xffff) | (cnt << 16);
178 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200179}
180
Shaohua Lie7836bd62012-07-19 16:01:31 +1000181static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200182{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000183 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
184 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200185}
186
NeilBrownd0dabf72009-03-31 14:39:38 +1100187/* Find first data disk in a raid6 stripe */
188static inline int raid6_d0(struct stripe_head *sh)
189{
NeilBrown67cc2b82009-03-31 14:39:38 +1100190 if (sh->ddf_layout)
191 /* ddf always start from first device */
192 return 0;
193 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100194 if (sh->qd_idx == sh->disks - 1)
195 return 0;
196 else
197 return sh->qd_idx + 1;
198}
NeilBrown16a53ec2006-06-26 00:27:38 -0700199static inline int raid6_next_disk(int disk, int raid_disks)
200{
201 disk++;
202 return (disk < raid_disks) ? disk : 0;
203}
Dan Williamsa4456852007-07-09 11:56:43 -0700204
NeilBrownd0dabf72009-03-31 14:39:38 +1100205/* When walking through the disks in a raid5, starting at raid6_d0,
206 * We need to map each disk to a 'slot', where the data disks are slot
207 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
208 * is raid_disks-1. This help does that mapping.
209 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100210static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
211 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100212{
Dan Williams66295422009-10-19 18:09:32 -0700213 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100214
NeilBrowne4424fe2009-10-16 16:27:34 +1100215 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700216 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100217 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100218 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100219 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100220 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100221 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700222 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100223 return slot;
224}
225
NeilBrown34a6f802015-08-14 12:07:57 +1000226static void return_io(struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -0700227{
NeilBrown34a6f802015-08-14 12:07:57 +1000228 struct bio *bi;
229 while ((bi = bio_list_pop(return_bi)) != NULL) {
Kent Overstreet4f024f32013-10-11 15:44:27 -0700230 bi->bi_iter.bi_size = 0;
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700231 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
232 bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200233 bio_endio(bi);
Dan Williamsa4456852007-07-09 11:56:43 -0700234 }
235}
236
NeilBrownd1688a62011-10-11 16:49:52 +1100237static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Dan Williams600aa102008-06-28 08:32:05 +1000239static int stripe_operations_active(struct stripe_head *sh)
240{
241 return sh->check_state || sh->reconstruct_state ||
242 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
243 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
244}
245
Shaohua Li851c30c2013-08-28 14:30:16 +0800246static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
247{
248 struct r5conf *conf = sh->raid_conf;
249 struct r5worker_group *group;
Shaohua Libfc90cb2013-08-29 15:40:32 +0800250 int thread_cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +0800251 int i, cpu = sh->cpu;
252
253 if (!cpu_online(cpu)) {
254 cpu = cpumask_any(cpu_online_mask);
255 sh->cpu = cpu;
256 }
257
258 if (list_empty(&sh->lru)) {
259 struct r5worker_group *group;
260 group = conf->worker_groups + cpu_to_group(cpu);
261 list_add_tail(&sh->lru, &group->handle_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800262 group->stripes_cnt++;
263 sh->group = group;
Shaohua Li851c30c2013-08-28 14:30:16 +0800264 }
265
266 if (conf->worker_cnt_per_group == 0) {
267 md_wakeup_thread(conf->mddev->thread);
268 return;
269 }
270
271 group = conf->worker_groups + cpu_to_group(sh->cpu);
272
Shaohua Libfc90cb2013-08-29 15:40:32 +0800273 group->workers[0].working = true;
274 /* at least one worker should run to avoid race */
275 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
276
277 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
278 /* wakeup more workers */
279 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
280 if (group->workers[i].working == false) {
281 group->workers[i].working = true;
282 queue_work_on(sh->cpu, raid5_wq,
283 &group->workers[i].work);
284 thread_cnt--;
285 }
286 }
Shaohua Li851c30c2013-08-28 14:30:16 +0800287}
288
Shaohua Li566c09c2013-11-14 15:16:17 +1100289static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
290 struct list_head *temp_inactive_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000292 BUG_ON(!list_empty(&sh->lru));
293 BUG_ON(atomic_read(&conf->active_stripes)==0);
294 if (test_bit(STRIPE_HANDLE, &sh->state)) {
295 if (test_bit(STRIPE_DELAYED, &sh->state) &&
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500296 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
Shaohua Li4eb788d2012-07-19 16:01:31 +1000297 list_add_tail(&sh->lru, &conf->delayed_list);
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500298 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
Shaohua Li4eb788d2012-07-19 16:01:31 +1000299 sh->bm_seq - conf->seq_write > 0)
300 list_add_tail(&sh->lru, &conf->bitmap_list);
301 else {
302 clear_bit(STRIPE_DELAYED, &sh->state);
303 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Shaohua Li851c30c2013-08-28 14:30:16 +0800304 if (conf->worker_cnt_per_group == 0) {
305 list_add_tail(&sh->lru, &conf->handle_list);
306 } else {
307 raid5_wakeup_stripe_thread(sh);
308 return;
309 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000310 }
311 md_wakeup_thread(conf->mddev->thread);
312 } else {
313 BUG_ON(stripe_operations_active(sh));
314 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
315 if (atomic_dec_return(&conf->preread_active_stripes)
316 < IO_THRESHOLD)
317 md_wakeup_thread(conf->mddev->thread);
318 atomic_dec(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100319 if (!test_bit(STRIPE_EXPANDING, &sh->state))
320 list_add_tail(&sh->lru, temp_inactive_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322}
NeilBrownd0dabf72009-03-31 14:39:38 +1100323
Shaohua Li566c09c2013-11-14 15:16:17 +1100324static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
325 struct list_head *temp_inactive_list)
Shaohua Li4eb788d2012-07-19 16:01:31 +1000326{
327 if (atomic_dec_and_test(&sh->count))
Shaohua Li566c09c2013-11-14 15:16:17 +1100328 do_release_stripe(conf, sh, temp_inactive_list);
329}
330
331/*
332 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
333 *
334 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
335 * given time. Adding stripes only takes device lock, while deleting stripes
336 * only takes hash lock.
337 */
338static void release_inactive_stripe_list(struct r5conf *conf,
339 struct list_head *temp_inactive_list,
340 int hash)
341{
342 int size;
Yuanhan Liue9e4c372015-05-08 18:19:07 +1000343 unsigned long do_wakeup = 0;
344 int i = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +1100345 unsigned long flags;
346
347 if (hash == NR_STRIPE_HASH_LOCKS) {
348 size = NR_STRIPE_HASH_LOCKS;
349 hash = NR_STRIPE_HASH_LOCKS - 1;
350 } else
351 size = 1;
352 while (size) {
353 struct list_head *list = &temp_inactive_list[size - 1];
354
355 /*
Shaohua Li6d036f72015-08-13 14:31:57 -0700356 * We don't hold any lock here yet, raid5_get_active_stripe() might
Shaohua Li566c09c2013-11-14 15:16:17 +1100357 * remove stripes from the list
358 */
359 if (!list_empty_careful(list)) {
360 spin_lock_irqsave(conf->hash_locks + hash, flags);
Shaohua Li4bda5562013-11-14 15:16:17 +1100361 if (list_empty(conf->inactive_list + hash) &&
362 !list_empty(list))
363 atomic_dec(&conf->empty_inactive_list_nr);
Shaohua Li566c09c2013-11-14 15:16:17 +1100364 list_splice_tail_init(list, conf->inactive_list + hash);
Yuanhan Liue9e4c372015-05-08 18:19:07 +1000365 do_wakeup |= 1 << hash;
Shaohua Li566c09c2013-11-14 15:16:17 +1100366 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
367 }
368 size--;
369 hash--;
370 }
371
Yuanhan Liue9e4c372015-05-08 18:19:07 +1000372 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) {
373 if (do_wakeup & (1 << i))
374 wake_up(&conf->wait_for_stripe[i]);
375 }
376
Shaohua Li566c09c2013-11-14 15:16:17 +1100377 if (do_wakeup) {
Yuanhan Liub1b46482015-05-08 18:19:06 +1000378 if (atomic_read(&conf->active_stripes) == 0)
379 wake_up(&conf->wait_for_quiescent);
Shaohua Li566c09c2013-11-14 15:16:17 +1100380 if (conf->retry_read_aligned)
381 md_wakeup_thread(conf->mddev->thread);
382 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000383}
384
Shaohua Li773ca822013-08-27 17:50:39 +0800385/* should hold conf->device_lock already */
Shaohua Li566c09c2013-11-14 15:16:17 +1100386static int release_stripe_list(struct r5conf *conf,
387 struct list_head *temp_inactive_list)
Shaohua Li773ca822013-08-27 17:50:39 +0800388{
389 struct stripe_head *sh;
390 int count = 0;
391 struct llist_node *head;
392
393 head = llist_del_all(&conf->released_stripes);
Shaohua Lid265d9d2013-08-28 14:29:05 +0800394 head = llist_reverse_order(head);
Shaohua Li773ca822013-08-27 17:50:39 +0800395 while (head) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100396 int hash;
397
Shaohua Li773ca822013-08-27 17:50:39 +0800398 sh = llist_entry(head, struct stripe_head, release_list);
399 head = llist_next(head);
400 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
401 smp_mb();
402 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
403 /*
404 * Don't worry the bit is set here, because if the bit is set
405 * again, the count is always > 1. This is true for
406 * STRIPE_ON_UNPLUG_LIST bit too.
407 */
Shaohua Li566c09c2013-11-14 15:16:17 +1100408 hash = sh->hash_lock_index;
409 __release_stripe(conf, sh, &temp_inactive_list[hash]);
Shaohua Li773ca822013-08-27 17:50:39 +0800410 count++;
411 }
412
413 return count;
414}
415
Shaohua Li6d036f72015-08-13 14:31:57 -0700416void raid5_release_stripe(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
NeilBrownd1688a62011-10-11 16:49:52 +1100418 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 unsigned long flags;
Shaohua Li566c09c2013-11-14 15:16:17 +1100420 struct list_head list;
421 int hash;
Shaohua Li773ca822013-08-27 17:50:39 +0800422 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700423
Eivind Sartocf170f32014-05-28 13:39:23 +1000424 /* Avoid release_list until the last reference.
425 */
426 if (atomic_add_unless(&sh->count, -1, 1))
427 return;
428
majianpengad4068d2013-11-14 15:16:15 +1100429 if (unlikely(!conf->mddev->thread) ||
430 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
Shaohua Li773ca822013-08-27 17:50:39 +0800431 goto slow_path;
432 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
433 if (wakeup)
434 md_wakeup_thread(conf->mddev->thread);
435 return;
436slow_path:
Shaohua Li4eb788d2012-07-19 16:01:31 +1000437 local_irq_save(flags);
Shaohua Li773ca822013-08-27 17:50:39 +0800438 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Shaohua Li4eb788d2012-07-19 16:01:31 +1000439 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100440 INIT_LIST_HEAD(&list);
441 hash = sh->hash_lock_index;
442 do_release_stripe(conf, sh, &list);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000443 spin_unlock(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +1100444 release_inactive_stripe_list(conf, &list, hash);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000445 }
446 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
NeilBrownfccddba2006-01-06 00:20:33 -0800449static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Dan Williams45b42332007-07-09 11:56:43 -0700451 pr_debug("remove_hash(), stripe %llu\n",
452 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
NeilBrownfccddba2006-01-06 00:20:33 -0800454 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
NeilBrownd1688a62011-10-11 16:49:52 +1100457static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
NeilBrownfccddba2006-01-06 00:20:33 -0800459 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Dan Williams45b42332007-07-09 11:56:43 -0700461 pr_debug("insert_hash(), stripe %llu\n",
462 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
NeilBrownfccddba2006-01-06 00:20:33 -0800464 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467/* find an idle stripe, make sure it is unhashed, and return it. */
Shaohua Li566c09c2013-11-14 15:16:17 +1100468static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 struct stripe_head *sh = NULL;
471 struct list_head *first;
472
Shaohua Li566c09c2013-11-14 15:16:17 +1100473 if (list_empty(conf->inactive_list + hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 goto out;
Shaohua Li566c09c2013-11-14 15:16:17 +1100475 first = (conf->inactive_list + hash)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 sh = list_entry(first, struct stripe_head, lru);
477 list_del_init(first);
478 remove_hash(sh);
479 atomic_inc(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100480 BUG_ON(hash != sh->hash_lock_index);
Shaohua Li4bda5562013-11-14 15:16:17 +1100481 if (list_empty(conf->inactive_list + hash))
482 atomic_inc(&conf->empty_inactive_list_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483out:
484 return sh;
485}
486
NeilBrowne4e11e32010-06-16 16:45:16 +1000487static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct page *p;
490 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000491 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
NeilBrowne4e11e32010-06-16 16:45:16 +1000493 for (i = 0; i < num ; i++) {
Shaohua Lid592a992014-05-21 17:57:44 +0800494 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 p = sh->dev[i].page;
496 if (!p)
497 continue;
498 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800499 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501}
502
NeilBrowna9683a72015-02-25 12:02:51 +1100503static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000506 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
NeilBrowne4e11e32010-06-16 16:45:16 +1000508 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 struct page *page;
510
NeilBrowna9683a72015-02-25 12:02:51 +1100511 if (!(page = alloc_page(gfp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return 1;
513 }
514 sh->dev[i].page = page;
Shaohua Lid592a992014-05-21 17:57:44 +0800515 sh->dev[i].orig_page = page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517 return 0;
518}
519
NeilBrown784052e2009-03-31 15:19:07 +1100520static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100521static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100522 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
NeilBrownb5663ba2009-03-31 14:39:38 +1100524static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
NeilBrownd1688a62011-10-11 16:49:52 +1100526 struct r5conf *conf = sh->raid_conf;
Shaohua Li566c09c2013-11-14 15:16:17 +1100527 int i, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200529 BUG_ON(atomic_read(&sh->count) != 0);
530 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000531 BUG_ON(stripe_operations_active(sh));
shli@kernel.org59fc6302014-12-15 12:57:03 +1100532 BUG_ON(sh->batch_head);
Dan Williamsd84e0f12007-01-02 13:52:30 -0700533
Dan Williams45b42332007-07-09 11:56:43 -0700534 pr_debug("init_stripe called, stripe %llu\n",
Markus Stockhausenb8e6a152014-08-23 20:19:27 +1000535 (unsigned long long)sector);
Shaohua Li566c09c2013-11-14 15:16:17 +1100536retry:
537 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100538 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100539 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100541 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 sh->state = 0;
543
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800544 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 struct r5dev *dev = &sh->dev[i];
546
Dan Williamsd84e0f12007-01-02 13:52:30 -0700547 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700549 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700551 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000553 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100556 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100558 if (read_seqcount_retry(&conf->gen_lock, seq))
559 goto retry;
shli@kernel.org7a87f432014-12-15 12:57:03 +1100560 sh->overwrite_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800562 sh->cpu = smp_processor_id();
shli@kernel.orgda41ba62014-12-15 12:57:03 +1100563 set_bit(STRIPE_BATCH_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
NeilBrownd1688a62011-10-11 16:49:52 +1100566static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100567 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 struct stripe_head *sh;
570
Dan Williams45b42332007-07-09 11:56:43 -0700571 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800572 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100573 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700575 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return NULL;
577}
578
NeilBrown674806d2010-06-16 17:17:53 +1000579/*
580 * Need to check if array has failed when deciding whether to:
581 * - start an array
582 * - remove non-faulty devices
583 * - add a spare
584 * - allow a reshape
585 * This determination is simple when no reshape is happening.
586 * However if there is a reshape, we need to carefully check
587 * both the before and after sections.
588 * This is because some failed devices may only affect one
589 * of the two sections, and some non-in_sync devices may
590 * be insync in the section most affected by failed devices.
591 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100592static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000593{
NeilBrown908f4fb2011-12-23 10:17:50 +1100594 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000595 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000596
597 rcu_read_lock();
598 degraded = 0;
599 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100600 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000601 if (rdev && test_bit(Faulty, &rdev->flags))
602 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000603 if (!rdev || test_bit(Faulty, &rdev->flags))
604 degraded++;
605 else if (test_bit(In_sync, &rdev->flags))
606 ;
607 else
608 /* not in-sync or faulty.
609 * If the reshape increases the number of devices,
610 * this is being recovered by the reshape, so
611 * this 'previous' section is not in_sync.
612 * If the number of devices is being reduced however,
613 * the device can only be part of the array if
614 * we are reverting a reshape, so this section will
615 * be in-sync.
616 */
617 if (conf->raid_disks >= conf->previous_raid_disks)
618 degraded++;
619 }
620 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100621 if (conf->raid_disks == conf->previous_raid_disks)
622 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000623 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100624 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000625 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100626 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000627 if (rdev && test_bit(Faulty, &rdev->flags))
628 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000629 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100630 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000631 else if (test_bit(In_sync, &rdev->flags))
632 ;
633 else
634 /* not in-sync or faulty.
635 * If reshape increases the number of devices, this
636 * section has already been recovered, else it
637 * almost certainly hasn't.
638 */
639 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100640 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000641 }
642 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100643 if (degraded2 > degraded)
644 return degraded2;
645 return degraded;
646}
647
648static int has_failed(struct r5conf *conf)
649{
650 int degraded;
651
652 if (conf->mddev->reshape_position == MaxSector)
653 return conf->mddev->degraded > conf->max_degraded;
654
655 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000656 if (degraded > conf->max_degraded)
657 return 1;
658 return 0;
659}
660
Shaohua Li6d036f72015-08-13 14:31:57 -0700661struct stripe_head *
662raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
663 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct stripe_head *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +1100666 int hash = stripe_hash_locks_hash(sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Dan Williams45b42332007-07-09 11:56:43 -0700668 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Shaohua Li566c09c2013-11-14 15:16:17 +1100670 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 do {
Yuanhan Liub1b46482015-05-08 18:19:06 +1000673 wait_event_lock_irq(conf->wait_for_quiescent,
NeilBrowna8c906c2009-06-09 14:39:59 +1000674 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100675 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100676 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (!sh) {
NeilBrownedbe83a2015-02-26 12:47:56 +1100678 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100679 sh = get_free_stripe(conf, hash);
Shaohua Li713bc5c2015-05-28 17:33:47 -0700680 if (!sh && !test_bit(R5_DID_ALLOC,
681 &conf->cache_state))
NeilBrownedbe83a2015-02-26 12:47:56 +1100682 set_bit(R5_ALLOC_MORE,
683 &conf->cache_state);
684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (noblock && sh == NULL)
686 break;
687 if (!sh) {
NeilBrown54233992015-02-26 12:21:04 +1100688 set_bit(R5_INACTIVE_BLOCKED,
689 &conf->cache_state);
Yuanhan Liue9e4c372015-05-08 18:19:07 +1000690 wait_event_exclusive_cmd(
691 conf->wait_for_stripe[hash],
Shaohua Li566c09c2013-11-14 15:16:17 +1100692 !list_empty(conf->inactive_list + hash) &&
693 (atomic_read(&conf->active_stripes)
694 < (conf->max_nr_stripes * 3 / 4)
NeilBrown54233992015-02-26 12:21:04 +1100695 || !test_bit(R5_INACTIVE_BLOCKED,
696 &conf->cache_state)),
Yuanhan Liue9e4c372015-05-08 18:19:07 +1000697 spin_unlock_irq(conf->hash_locks + hash),
698 spin_lock_irq(conf->hash_locks + hash));
NeilBrown54233992015-02-26 12:21:04 +1100699 clear_bit(R5_INACTIVE_BLOCKED,
700 &conf->cache_state);
NeilBrown7da9d452014-01-22 11:45:03 +1100701 } else {
NeilBrownb5663ba2009-03-31 14:39:38 +1100702 init_stripe(sh, sector, previous);
NeilBrown7da9d452014-01-22 11:45:03 +1100703 atomic_inc(&sh->count);
704 }
Shaohua Lie240c182014-04-09 11:27:42 +0800705 } else if (!atomic_inc_not_zero(&sh->count)) {
NeilBrown6d183de2013-11-28 10:55:27 +1100706 spin_lock(&conf->device_lock);
Shaohua Lie240c182014-04-09 11:27:42 +0800707 if (!atomic_read(&sh->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (!test_bit(STRIPE_HANDLE, &sh->state))
709 atomic_inc(&conf->active_stripes);
NeilBrown5af9bef2014-01-14 15:16:10 +1100710 BUG_ON(list_empty(&sh->lru) &&
711 !test_bit(STRIPE_EXPANDING, &sh->state));
NeilBrown16a53ec2006-06-26 00:27:38 -0700712 list_del_init(&sh->lru);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800713 if (sh->group) {
714 sh->group->stripes_cnt--;
715 sh->group = NULL;
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
NeilBrown7da9d452014-01-22 11:45:03 +1100718 atomic_inc(&sh->count);
NeilBrown6d183de2013-11-28 10:55:27 +1100719 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721 } while (sh == NULL);
722
Yuanhan Liue9e4c372015-05-08 18:19:07 +1000723 if (!list_empty(conf->inactive_list + hash))
724 wake_up(&conf->wait_for_stripe[hash]);
725
Shaohua Li566c09c2013-11-14 15:16:17 +1100726 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return sh;
728}
729
shli@kernel.org7a87f432014-12-15 12:57:03 +1100730static bool is_full_stripe_write(struct stripe_head *sh)
731{
732 BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded));
733 return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded);
734}
735
shli@kernel.org59fc6302014-12-15 12:57:03 +1100736static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
737{
738 local_irq_disable();
739 if (sh1 > sh2) {
740 spin_lock(&sh2->stripe_lock);
741 spin_lock_nested(&sh1->stripe_lock, 1);
742 } else {
743 spin_lock(&sh1->stripe_lock);
744 spin_lock_nested(&sh2->stripe_lock, 1);
745 }
746}
747
748static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
749{
750 spin_unlock(&sh1->stripe_lock);
751 spin_unlock(&sh2->stripe_lock);
752 local_irq_enable();
753}
754
755/* Only freshly new full stripe normal write stripe can be added to a batch list */
756static bool stripe_can_batch(struct stripe_head *sh)
757{
758 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
NeilBrownd0852df52015-05-27 08:43:45 +1000759 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
shli@kernel.org59fc6302014-12-15 12:57:03 +1100760 is_full_stripe_write(sh);
761}
762
763/* we only do back search */
764static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
765{
766 struct stripe_head *head;
767 sector_t head_sector, tmp_sec;
768 int hash;
769 int dd_idx;
770
771 if (!stripe_can_batch(sh))
772 return;
773 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
774 tmp_sec = sh->sector;
775 if (!sector_div(tmp_sec, conf->chunk_sectors))
776 return;
777 head_sector = sh->sector - STRIPE_SECTORS;
778
779 hash = stripe_hash_locks_hash(head_sector);
780 spin_lock_irq(conf->hash_locks + hash);
781 head = __find_stripe(conf, head_sector, conf->generation);
782 if (head && !atomic_inc_not_zero(&head->count)) {
783 spin_lock(&conf->device_lock);
784 if (!atomic_read(&head->count)) {
785 if (!test_bit(STRIPE_HANDLE, &head->state))
786 atomic_inc(&conf->active_stripes);
787 BUG_ON(list_empty(&head->lru) &&
788 !test_bit(STRIPE_EXPANDING, &head->state));
789 list_del_init(&head->lru);
790 if (head->group) {
791 head->group->stripes_cnt--;
792 head->group = NULL;
793 }
794 }
795 atomic_inc(&head->count);
796 spin_unlock(&conf->device_lock);
797 }
798 spin_unlock_irq(conf->hash_locks + hash);
799
800 if (!head)
801 return;
802 if (!stripe_can_batch(head))
803 goto out;
804
805 lock_two_stripes(head, sh);
806 /* clear_batch_ready clear the flag */
807 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
808 goto unlock_out;
809
810 if (sh->batch_head)
811 goto unlock_out;
812
813 dd_idx = 0;
814 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
815 dd_idx++;
816 if (head->dev[dd_idx].towrite->bi_rw != sh->dev[dd_idx].towrite->bi_rw)
817 goto unlock_out;
818
819 if (head->batch_head) {
820 spin_lock(&head->batch_head->batch_lock);
821 /* This batch list is already running */
822 if (!stripe_can_batch(head)) {
823 spin_unlock(&head->batch_head->batch_lock);
824 goto unlock_out;
825 }
826
827 /*
828 * at this point, head's BATCH_READY could be cleared, but we
829 * can still add the stripe to batch list
830 */
831 list_add(&sh->batch_list, &head->batch_list);
832 spin_unlock(&head->batch_head->batch_lock);
833
834 sh->batch_head = head->batch_head;
835 } else {
836 head->batch_head = head;
837 sh->batch_head = head->batch_head;
838 spin_lock(&head->batch_lock);
839 list_add_tail(&sh->batch_list, &head->batch_list);
840 spin_unlock(&head->batch_lock);
841 }
842
843 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
844 if (atomic_dec_return(&conf->preread_active_stripes)
845 < IO_THRESHOLD)
846 md_wakeup_thread(conf->mddev->thread);
847
NeilBrown2b6b2452015-05-21 15:10:01 +1000848 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
849 int seq = sh->bm_seq;
850 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
851 sh->batch_head->bm_seq > seq)
852 seq = sh->batch_head->bm_seq;
853 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
854 sh->batch_head->bm_seq = seq;
855 }
856
shli@kernel.org59fc6302014-12-15 12:57:03 +1100857 atomic_inc(&sh->count);
858unlock_out:
859 unlock_two_stripes(head, sh);
860out:
Shaohua Li6d036f72015-08-13 14:31:57 -0700861 raid5_release_stripe(head);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100862}
863
NeilBrown05616be2012-05-21 09:27:00 +1000864/* Determine if 'data_offset' or 'new_data_offset' should be used
865 * in this stripe_head.
866 */
867static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
868{
869 sector_t progress = conf->reshape_progress;
870 /* Need a memory barrier to make sure we see the value
871 * of conf->generation, or ->data_offset that was set before
872 * reshape_progress was updated.
873 */
874 smp_rmb();
875 if (progress == MaxSector)
876 return 0;
877 if (sh->generation == conf->generation - 1)
878 return 0;
879 /* We are in a reshape, and this is a new-generation stripe,
880 * so use new_data_offset.
881 */
882 return 1;
883}
884
NeilBrown6712ecf2007-09-27 12:47:43 +0200885static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200886raid5_end_read_request(struct bio *bi);
NeilBrown6712ecf2007-09-27 12:47:43 +0200887static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200888raid5_end_write_request(struct bio *bi);
Dan Williams91c00922007-01-02 13:52:30 -0700889
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000890static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700891{
NeilBrownd1688a62011-10-11 16:49:52 +1100892 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700893 int i, disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100894 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -0700895
896 might_sleep();
897
898 for (i = disks; i--; ) {
899 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100900 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100901 struct bio *bi, *rbi;
902 struct md_rdev *rdev, *rrdev = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100903
904 sh = head_sh;
Tejun Heoe9c74692010-09-03 11:56:18 +0200905 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
906 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
907 rw = WRITE_FUA;
908 else
909 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100910 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100911 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200912 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700913 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100914 else if (test_and_clear_bit(R5_WantReplace,
915 &sh->dev[i].flags)) {
916 rw = WRITE;
917 replace_only = 1;
918 } else
Dan Williams91c00922007-01-02 13:52:30 -0700919 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000920 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
921 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700922
shli@kernel.org59fc6302014-12-15 12:57:03 +1100923again:
Dan Williams91c00922007-01-02 13:52:30 -0700924 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100925 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700926
Dan Williams91c00922007-01-02 13:52:30 -0700927 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100928 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100929 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
930 rdev = rcu_dereference(conf->disks[i].rdev);
931 if (!rdev) {
932 rdev = rrdev;
933 rrdev = NULL;
934 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100935 if (rw & WRITE) {
936 if (replace_only)
937 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100938 if (rdev == rrdev)
939 /* We raced and saw duplicates */
940 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100941 } else {
shli@kernel.org59fc6302014-12-15 12:57:03 +1100942 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100943 rdev = rrdev;
944 rrdev = NULL;
945 }
NeilBrown977df362011-12-23 10:17:53 +1100946
Dan Williams91c00922007-01-02 13:52:30 -0700947 if (rdev && test_bit(Faulty, &rdev->flags))
948 rdev = NULL;
949 if (rdev)
950 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100951 if (rrdev && test_bit(Faulty, &rrdev->flags))
952 rrdev = NULL;
953 if (rrdev)
954 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700955 rcu_read_unlock();
956
NeilBrown73e92e52011-07-28 11:39:22 +1000957 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100958 * need to check for writes. We never accept write errors
959 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000960 */
961 while ((rw & WRITE) && rdev &&
962 test_bit(WriteErrorSeen, &rdev->flags)) {
963 sector_t first_bad;
964 int bad_sectors;
965 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
966 &first_bad, &bad_sectors);
967 if (!bad)
968 break;
969
970 if (bad < 0) {
971 set_bit(BlockedBadBlocks, &rdev->flags);
972 if (!conf->mddev->external &&
973 conf->mddev->flags) {
974 /* It is very unlikely, but we might
975 * still need to write out the
976 * bad block log - better give it
977 * a chance*/
978 md_check_recovery(conf->mddev);
979 }
majianpeng18507532012-07-03 12:11:54 +1000980 /*
981 * Because md_wait_for_blocked_rdev
982 * will dec nr_pending, we must
983 * increment it first.
984 */
985 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000986 md_wait_for_blocked_rdev(rdev, conf->mddev);
987 } else {
988 /* Acknowledged bad block - skip the write */
989 rdev_dec_pending(rdev, conf->mddev);
990 rdev = NULL;
991 }
992 }
993
Dan Williams91c00922007-01-02 13:52:30 -0700994 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100995 if (s->syncing || s->expanding || s->expanded
996 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700997 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
998
Dan Williams2b7497f2008-06-28 08:31:52 +1000999 set_bit(STRIPE_IO_STARTED, &sh->state);
1000
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001001 bio_reset(bi);
Dan Williams91c00922007-01-02 13:52:30 -07001002 bi->bi_bdev = rdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001003 bi->bi_rw = rw;
1004 bi->bi_end_io = (rw & WRITE)
1005 ? raid5_end_write_request
1006 : raid5_end_read_request;
1007 bi->bi_private = sh;
1008
Dan Williams91c00922007-01-02 13:52:30 -07001009 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07001010 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -07001011 bi->bi_rw, i);
1012 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001013 if (sh != head_sh)
1014 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001015 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001016 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001017 + rdev->new_data_offset);
1018 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001019 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001020 + rdev->data_offset);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001021 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
majianpenge59aa232013-11-14 15:16:19 +11001022 bi->bi_rw |= REQ_NOMERGE;
majianpeng3f9e7c12012-07-31 10:04:21 +10001023
Shaohua Lid592a992014-05-21 17:57:44 +08001024 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1025 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1026 sh->dev[i].vec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001027 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001028 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1029 bi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001030 bi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001031 /*
1032 * If this is discard request, set bi_vcnt 0. We don't
1033 * want to confuse SCSI because SCSI will replace payload
1034 */
1035 if (rw & REQ_DISCARD)
1036 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +11001037 if (rrdev)
1038 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001039
1040 if (conf->mddev->gendisk)
1041 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
1042 bi, disk_devt(conf->mddev->gendisk),
1043 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -07001044 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +11001045 }
1046 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001047 if (s->syncing || s->expanding || s->expanded
1048 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +11001049 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
1050
1051 set_bit(STRIPE_IO_STARTED, &sh->state);
1052
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001053 bio_reset(rbi);
NeilBrown977df362011-12-23 10:17:53 +11001054 rbi->bi_bdev = rrdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001055 rbi->bi_rw = rw;
1056 BUG_ON(!(rw & WRITE));
1057 rbi->bi_end_io = raid5_end_write_request;
1058 rbi->bi_private = sh;
1059
NeilBrown977df362011-12-23 10:17:53 +11001060 pr_debug("%s: for %llu schedule op %ld on "
1061 "replacement disc %d\n",
1062 __func__, (unsigned long long)sh->sector,
1063 rbi->bi_rw, i);
1064 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001065 if (sh != head_sh)
1066 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001067 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001068 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001069 + rrdev->new_data_offset);
1070 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001071 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001072 + rrdev->data_offset);
Shaohua Lid592a992014-05-21 17:57:44 +08001073 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1074 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1075 sh->dev[i].rvec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001076 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +11001077 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1078 rbi->bi_io_vec[0].bv_offset = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001079 rbi->bi_iter.bi_size = STRIPE_SIZE;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001080 /*
1081 * If this is discard request, set bi_vcnt 0. We don't
1082 * want to confuse SCSI because SCSI will replace payload
1083 */
1084 if (rw & REQ_DISCARD)
1085 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001086 if (conf->mddev->gendisk)
1087 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
1088 rbi, disk_devt(conf->mddev->gendisk),
1089 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +11001090 generic_make_request(rbi);
1091 }
1092 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +10001093 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -07001094 set_bit(STRIPE_DEGRADED, &sh->state);
1095 pr_debug("skip op %ld on disc %d for sector %llu\n",
1096 bi->bi_rw, i, (unsigned long long)sh->sector);
1097 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1098 set_bit(STRIPE_HANDLE, &sh->state);
1099 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001100
1101 if (!head_sh->batch_head)
1102 continue;
1103 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1104 batch_list);
1105 if (sh != head_sh)
1106 goto again;
Dan Williams91c00922007-01-02 13:52:30 -07001107 }
1108}
1109
1110static struct dma_async_tx_descriptor *
Shaohua Lid592a992014-05-21 17:57:44 +08001111async_copy_data(int frombio, struct bio *bio, struct page **page,
1112 sector_t sector, struct dma_async_tx_descriptor *tx,
1113 struct stripe_head *sh)
Dan Williams91c00922007-01-02 13:52:30 -07001114{
Kent Overstreet79886132013-11-23 17:19:00 -08001115 struct bio_vec bvl;
1116 struct bvec_iter iter;
Dan Williams91c00922007-01-02 13:52:30 -07001117 struct page *bio_page;
Dan Williams91c00922007-01-02 13:52:30 -07001118 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -07001119 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -07001120 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001121
Kent Overstreet4f024f32013-10-11 15:44:27 -07001122 if (bio->bi_iter.bi_sector >= sector)
1123 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
Dan Williams91c00922007-01-02 13:52:30 -07001124 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001125 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -07001126
Dan Williams0403e382009-09-08 17:42:50 -07001127 if (frombio)
1128 flags |= ASYNC_TX_FENCE;
1129 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1130
Kent Overstreet79886132013-11-23 17:19:00 -08001131 bio_for_each_segment(bvl, bio, iter) {
1132 int len = bvl.bv_len;
Dan Williams91c00922007-01-02 13:52:30 -07001133 int clen;
1134 int b_offset = 0;
1135
1136 if (page_offset < 0) {
1137 b_offset = -page_offset;
1138 page_offset += b_offset;
1139 len -= b_offset;
1140 }
1141
1142 if (len > 0 && page_offset + len > STRIPE_SIZE)
1143 clen = STRIPE_SIZE - page_offset;
1144 else
1145 clen = len;
1146
1147 if (clen > 0) {
Kent Overstreet79886132013-11-23 17:19:00 -08001148 b_offset += bvl.bv_offset;
1149 bio_page = bvl.bv_page;
Shaohua Lid592a992014-05-21 17:57:44 +08001150 if (frombio) {
1151 if (sh->raid_conf->skip_copy &&
1152 b_offset == 0 && page_offset == 0 &&
1153 clen == STRIPE_SIZE)
1154 *page = bio_page;
1155 else
1156 tx = async_memcpy(*page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001157 b_offset, clen, &submit);
Shaohua Lid592a992014-05-21 17:57:44 +08001158 } else
1159 tx = async_memcpy(bio_page, *page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -07001160 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001161 }
Dan Williamsa08abd82009-06-03 11:43:59 -07001162 /* chain the operations */
1163 submit.depend_tx = tx;
1164
Dan Williams91c00922007-01-02 13:52:30 -07001165 if (clen < len) /* hit end of page */
1166 break;
1167 page_offset += len;
1168 }
1169
1170 return tx;
1171}
1172
1173static void ops_complete_biofill(void *stripe_head_ref)
1174{
1175 struct stripe_head *sh = stripe_head_ref;
NeilBrown34a6f802015-08-14 12:07:57 +10001176 struct bio_list return_bi = BIO_EMPTY_LIST;
Dan Williamse4d84902007-09-24 10:06:13 -07001177 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001178
Harvey Harrisone46b2722008-04-28 02:15:50 -07001179 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001180 (unsigned long long)sh->sector);
1181
1182 /* clear completed biofills */
1183 for (i = sh->disks; i--; ) {
1184 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001185
1186 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001187 /* and check if we need to reply to a read request,
1188 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001189 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001190 */
1191 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001192 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001193
Dan Williams91c00922007-01-02 13:52:30 -07001194 BUG_ON(!dev->read);
1195 rbi = dev->read;
1196 dev->read = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001197 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001198 dev->sector + STRIPE_SECTORS) {
1199 rbi2 = r5_next_bio(rbi, dev->sector);
NeilBrown34a6f802015-08-14 12:07:57 +10001200 if (!raid5_dec_bi_active_stripes(rbi))
1201 bio_list_add(&return_bi, rbi);
Dan Williams91c00922007-01-02 13:52:30 -07001202 rbi = rbi2;
1203 }
1204 }
1205 }
Dan Williams83de75c2008-06-28 08:31:58 +10001206 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001207
NeilBrown34a6f802015-08-14 12:07:57 +10001208 return_io(&return_bi);
Dan Williams91c00922007-01-02 13:52:30 -07001209
Dan Williamse4d84902007-09-24 10:06:13 -07001210 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001211 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001212}
1213
1214static void ops_run_biofill(struct stripe_head *sh)
1215{
1216 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001217 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001218 int i;
1219
shli@kernel.org59fc6302014-12-15 12:57:03 +11001220 BUG_ON(sh->batch_head);
Harvey Harrisone46b2722008-04-28 02:15:50 -07001221 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001222 (unsigned long long)sh->sector);
1223
1224 for (i = sh->disks; i--; ) {
1225 struct r5dev *dev = &sh->dev[i];
1226 if (test_bit(R5_Wantfill, &dev->flags)) {
1227 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001228 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001229 dev->read = rbi = dev->toread;
1230 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001231 spin_unlock_irq(&sh->stripe_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001232 while (rbi && rbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001233 dev->sector + STRIPE_SECTORS) {
Shaohua Lid592a992014-05-21 17:57:44 +08001234 tx = async_copy_data(0, rbi, &dev->page,
1235 dev->sector, tx, sh);
Dan Williams91c00922007-01-02 13:52:30 -07001236 rbi = r5_next_bio(rbi, dev->sector);
1237 }
1238 }
1239 }
1240
1241 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001242 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1243 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001244}
1245
Dan Williams4e7d2c02009-08-29 19:13:11 -07001246static void mark_target_uptodate(struct stripe_head *sh, int target)
1247{
1248 struct r5dev *tgt;
1249
1250 if (target < 0)
1251 return;
1252
1253 tgt = &sh->dev[target];
1254 set_bit(R5_UPTODATE, &tgt->flags);
1255 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1256 clear_bit(R5_Wantcompute, &tgt->flags);
1257}
1258
Dan Williamsac6b53b2009-07-14 13:40:19 -07001259static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001260{
1261 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001262
Harvey Harrisone46b2722008-04-28 02:15:50 -07001263 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001264 (unsigned long long)sh->sector);
1265
Dan Williamsac6b53b2009-07-14 13:40:19 -07001266 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001267 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001268 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001269
Dan Williamsecc65c92008-06-28 08:31:57 +10001270 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1271 if (sh->check_state == check_state_compute_run)
1272 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001273 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001274 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001275}
1276
Dan Williamsd6f38f32009-07-14 11:50:52 -07001277/* return a pointer to the address conversion region of the scribble buffer */
1278static addr_conv_t *to_addr_conv(struct stripe_head *sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001279 struct raid5_percpu *percpu, int i)
Dan Williams91c00922007-01-02 13:52:30 -07001280{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001281 void *addr;
1282
1283 addr = flex_array_get(percpu->scribble, i);
1284 return addr + sizeof(struct page *) * (sh->disks + 2);
1285}
1286
1287/* return a pointer to the address conversion region of the scribble buffer */
1288static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
1289{
1290 void *addr;
1291
1292 addr = flex_array_get(percpu->scribble, i);
1293 return addr;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001294}
1295
1296static struct dma_async_tx_descriptor *
1297ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1298{
Dan Williams91c00922007-01-02 13:52:30 -07001299 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001300 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001301 int target = sh->ops.target;
1302 struct r5dev *tgt = &sh->dev[target];
1303 struct page *xor_dest = tgt->page;
1304 int count = 0;
1305 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001306 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001307 int i;
1308
shli@kernel.org59fc6302014-12-15 12:57:03 +11001309 BUG_ON(sh->batch_head);
1310
Dan Williams91c00922007-01-02 13:52:30 -07001311 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07001312 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001313 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1314
1315 for (i = disks; i--; )
1316 if (i != target)
1317 xor_srcs[count++] = sh->dev[i].page;
1318
1319 atomic_inc(&sh->count);
1320
Dan Williams0403e382009-09-08 17:42:50 -07001321 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001322 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
Dan Williams91c00922007-01-02 13:52:30 -07001323 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -07001324 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001325 else
Dan Williamsa08abd82009-06-03 11:43:59 -07001326 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001327
Dan Williams91c00922007-01-02 13:52:30 -07001328 return tx;
1329}
1330
Dan Williamsac6b53b2009-07-14 13:40:19 -07001331/* set_syndrome_sources - populate source buffers for gen_syndrome
1332 * @srcs - (struct page *) array of size sh->disks
1333 * @sh - stripe_head to parse
1334 *
1335 * Populates srcs in proper layout order for the stripe and returns the
1336 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1337 * destination buffer is recorded in srcs[count] and the Q destination
1338 * is recorded in srcs[count+1]].
1339 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001340static int set_syndrome_sources(struct page **srcs,
1341 struct stripe_head *sh,
1342 int srctype)
Dan Williamsac6b53b2009-07-14 13:40:19 -07001343{
1344 int disks = sh->disks;
1345 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1346 int d0_idx = raid6_d0(sh);
1347 int count;
1348 int i;
1349
1350 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001351 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001352
1353 count = 0;
1354 i = d0_idx;
1355 do {
1356 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001357 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001358
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001359 if (i == sh->qd_idx || i == sh->pd_idx ||
1360 (srctype == SYNDROME_SRC_ALL) ||
1361 (srctype == SYNDROME_SRC_WANT_DRAIN &&
1362 test_bit(R5_Wantdrain, &dev->flags)) ||
1363 (srctype == SYNDROME_SRC_WRITTEN &&
1364 dev->written))
1365 srcs[slot] = sh->dev[i].page;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001366 i = raid6_next_disk(i, disks);
1367 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001368
NeilBrowne4424fe2009-10-16 16:27:34 +11001369 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001370}
1371
1372static struct dma_async_tx_descriptor *
1373ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1374{
1375 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001376 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001377 int target;
1378 int qd_idx = sh->qd_idx;
1379 struct dma_async_tx_descriptor *tx;
1380 struct async_submit_ctl submit;
1381 struct r5dev *tgt;
1382 struct page *dest;
1383 int i;
1384 int count;
1385
shli@kernel.org59fc6302014-12-15 12:57:03 +11001386 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001387 if (sh->ops.target < 0)
1388 target = sh->ops.target2;
1389 else if (sh->ops.target2 < 0)
1390 target = sh->ops.target;
1391 else
1392 /* we should only have one valid target */
1393 BUG();
1394 BUG_ON(target < 0);
1395 pr_debug("%s: stripe %llu block: %d\n",
1396 __func__, (unsigned long long)sh->sector, target);
1397
1398 tgt = &sh->dev[target];
1399 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1400 dest = tgt->page;
1401
1402 atomic_inc(&sh->count);
1403
1404 if (target == qd_idx) {
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001405 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001406 blocks[count] = NULL; /* regenerating p is not necessary */
1407 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001408 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1409 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001410 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001411 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1412 } else {
1413 /* Compute any data- or p-drive using XOR */
1414 count = 0;
1415 for (i = disks; i-- ; ) {
1416 if (i == target || i == qd_idx)
1417 continue;
1418 blocks[count++] = sh->dev[i].page;
1419 }
1420
Dan Williams0403e382009-09-08 17:42:50 -07001421 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1422 NULL, ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001423 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001424 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1425 }
1426
1427 return tx;
1428}
1429
1430static struct dma_async_tx_descriptor *
1431ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1432{
1433 int i, count, disks = sh->disks;
1434 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1435 int d0_idx = raid6_d0(sh);
1436 int faila = -1, failb = -1;
1437 int target = sh->ops.target;
1438 int target2 = sh->ops.target2;
1439 struct r5dev *tgt = &sh->dev[target];
1440 struct r5dev *tgt2 = &sh->dev[target2];
1441 struct dma_async_tx_descriptor *tx;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001442 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001443 struct async_submit_ctl submit;
1444
shli@kernel.org59fc6302014-12-15 12:57:03 +11001445 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001446 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1447 __func__, (unsigned long long)sh->sector, target, target2);
1448 BUG_ON(target < 0 || target2 < 0);
1449 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1450 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1451
Dan Williams6c910a72009-09-16 12:24:54 -07001452 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001453 * slot number conversion for 'faila' and 'failb'
1454 */
1455 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001456 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001457 count = 0;
1458 i = d0_idx;
1459 do {
1460 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1461
1462 blocks[slot] = sh->dev[i].page;
1463
1464 if (i == target)
1465 faila = slot;
1466 if (i == target2)
1467 failb = slot;
1468 i = raid6_next_disk(i, disks);
1469 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001470
1471 BUG_ON(faila == failb);
1472 if (failb < faila)
1473 swap(faila, failb);
1474 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1475 __func__, (unsigned long long)sh->sector, faila, failb);
1476
1477 atomic_inc(&sh->count);
1478
1479 if (failb == syndrome_disks+1) {
1480 /* Q disk is one of the missing disks */
1481 if (faila == syndrome_disks) {
1482 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001483 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1484 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001485 to_addr_conv(sh, percpu, 0));
NeilBrowne4424fe2009-10-16 16:27:34 +11001486 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001487 STRIPE_SIZE, &submit);
1488 } else {
1489 struct page *dest;
1490 int data_target;
1491 int qd_idx = sh->qd_idx;
1492
1493 /* Missing D+Q: recompute D from P, then recompute Q */
1494 if (target == qd_idx)
1495 data_target = target2;
1496 else
1497 data_target = target;
1498
1499 count = 0;
1500 for (i = disks; i-- ; ) {
1501 if (i == data_target || i == qd_idx)
1502 continue;
1503 blocks[count++] = sh->dev[i].page;
1504 }
1505 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001506 init_async_submit(&submit,
1507 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1508 NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001509 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001510 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1511 &submit);
1512
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001513 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williams0403e382009-09-08 17:42:50 -07001514 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1515 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001516 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001517 return async_gen_syndrome(blocks, 0, count+2,
1518 STRIPE_SIZE, &submit);
1519 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001520 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001521 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1522 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001523 to_addr_conv(sh, percpu, 0));
Dan Williams6c910a72009-09-16 12:24:54 -07001524 if (failb == syndrome_disks) {
1525 /* We're missing D+P. */
1526 return async_raid6_datap_recov(syndrome_disks+2,
1527 STRIPE_SIZE, faila,
1528 blocks, &submit);
1529 } else {
1530 /* We're missing D+D. */
1531 return async_raid6_2data_recov(syndrome_disks+2,
1532 STRIPE_SIZE, faila, failb,
1533 blocks, &submit);
1534 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001535 }
1536}
1537
Dan Williams91c00922007-01-02 13:52:30 -07001538static void ops_complete_prexor(void *stripe_head_ref)
1539{
1540 struct stripe_head *sh = stripe_head_ref;
1541
Harvey Harrisone46b2722008-04-28 02:15:50 -07001542 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001543 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001544}
1545
1546static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001547ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1548 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001549{
Dan Williams91c00922007-01-02 13:52:30 -07001550 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001551 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001552 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001553 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001554
1555 /* existing parity data subtracted */
1556 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1557
shli@kernel.org59fc6302014-12-15 12:57:03 +11001558 BUG_ON(sh->batch_head);
Harvey Harrisone46b2722008-04-28 02:15:50 -07001559 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001560 (unsigned long long)sh->sector);
1561
1562 for (i = disks; i--; ) {
1563 struct r5dev *dev = &sh->dev[i];
1564 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001565 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001566 xor_srcs[count++] = dev->page;
1567 }
1568
Dan Williams0403e382009-09-08 17:42:50 -07001569 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001570 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Dan Williamsa08abd82009-06-03 11:43:59 -07001571 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001572
1573 return tx;
1574}
1575
1576static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001577ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1578 struct dma_async_tx_descriptor *tx)
1579{
1580 struct page **blocks = to_addr_page(percpu, 0);
1581 int count;
1582 struct async_submit_ctl submit;
1583
1584 pr_debug("%s: stripe %llu\n", __func__,
1585 (unsigned long long)sh->sector);
1586
1587 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN);
1588
1589 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1590 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
1591 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1592
1593 return tx;
1594}
1595
1596static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001597ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001598{
1599 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001600 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001601 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -07001602
Harvey Harrisone46b2722008-04-28 02:15:50 -07001603 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001604 (unsigned long long)sh->sector);
1605
1606 for (i = disks; i--; ) {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001607 struct r5dev *dev;
Dan Williams91c00922007-01-02 13:52:30 -07001608 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001609
shli@kernel.org59fc6302014-12-15 12:57:03 +11001610 sh = head_sh;
1611 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001612 struct bio *wbi;
1613
shli@kernel.org59fc6302014-12-15 12:57:03 +11001614again:
1615 dev = &sh->dev[i];
Shaohua Lib17459c2012-07-19 16:01:31 +10001616 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001617 chosen = dev->towrite;
1618 dev->towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11001619 sh->overwrite_disks = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001620 BUG_ON(dev->written);
1621 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001622 spin_unlock_irq(&sh->stripe_lock);
Shaohua Lid592a992014-05-21 17:57:44 +08001623 WARN_ON(dev->page != dev->orig_page);
Dan Williams91c00922007-01-02 13:52:30 -07001624
Kent Overstreet4f024f32013-10-11 15:44:27 -07001625 while (wbi && wbi->bi_iter.bi_sector <
Dan Williams91c00922007-01-02 13:52:30 -07001626 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001627 if (wbi->bi_rw & REQ_FUA)
1628 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001629 if (wbi->bi_rw & REQ_SYNC)
1630 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001631 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001632 set_bit(R5_Discard, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08001633 else {
1634 tx = async_copy_data(1, wbi, &dev->page,
1635 dev->sector, tx, sh);
1636 if (dev->page != dev->orig_page) {
1637 set_bit(R5_SkipCopy, &dev->flags);
1638 clear_bit(R5_UPTODATE, &dev->flags);
1639 clear_bit(R5_OVERWRITE, &dev->flags);
1640 }
1641 }
Dan Williams91c00922007-01-02 13:52:30 -07001642 wbi = r5_next_bio(wbi, dev->sector);
1643 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001644
1645 if (head_sh->batch_head) {
1646 sh = list_first_entry(&sh->batch_list,
1647 struct stripe_head,
1648 batch_list);
1649 if (sh == head_sh)
1650 continue;
1651 goto again;
1652 }
Dan Williams91c00922007-01-02 13:52:30 -07001653 }
1654 }
1655
1656 return tx;
1657}
1658
Dan Williamsac6b53b2009-07-14 13:40:19 -07001659static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001660{
1661 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001662 int disks = sh->disks;
1663 int pd_idx = sh->pd_idx;
1664 int qd_idx = sh->qd_idx;
1665 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001666 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001667
Harvey Harrisone46b2722008-04-28 02:15:50 -07001668 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001669 (unsigned long long)sh->sector);
1670
Shaohua Libc0934f2012-05-22 13:55:05 +10001671 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001672 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001673 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001674 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001675 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001676
Dan Williams91c00922007-01-02 13:52:30 -07001677 for (i = disks; i--; ) {
1678 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001679
Tejun Heoe9c74692010-09-03 11:56:18 +02001680 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Lid592a992014-05-21 17:57:44 +08001681 if (!discard && !test_bit(R5_SkipCopy, &dev->flags))
Shaohua Li9e4447682012-10-11 13:49:49 +11001682 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001683 if (fua)
1684 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001685 if (sync)
1686 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001687 }
Dan Williams91c00922007-01-02 13:52:30 -07001688 }
1689
Dan Williamsd8ee0722008-06-28 08:32:06 +10001690 if (sh->reconstruct_state == reconstruct_state_drain_run)
1691 sh->reconstruct_state = reconstruct_state_drain_result;
1692 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1693 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1694 else {
1695 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1696 sh->reconstruct_state = reconstruct_state_result;
1697 }
Dan Williams91c00922007-01-02 13:52:30 -07001698
1699 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001700 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001701}
1702
1703static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001704ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1705 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001706{
Dan Williams91c00922007-01-02 13:52:30 -07001707 int disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001708 struct page **xor_srcs;
Dan Williamsa08abd82009-06-03 11:43:59 -07001709 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001710 int count, pd_idx = sh->pd_idx, i;
Dan Williams91c00922007-01-02 13:52:30 -07001711 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001712 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001713 unsigned long flags;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001714 int j = 0;
1715 struct stripe_head *head_sh = sh;
1716 int last_stripe;
Dan Williams91c00922007-01-02 13:52:30 -07001717
Harvey Harrisone46b2722008-04-28 02:15:50 -07001718 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001719 (unsigned long long)sh->sector);
1720
Shaohua Li620125f2012-10-11 13:49:05 +11001721 for (i = 0; i < sh->disks; i++) {
1722 if (pd_idx == i)
1723 continue;
1724 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1725 break;
1726 }
1727 if (i >= sh->disks) {
1728 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001729 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1730 ops_complete_reconstruct(sh);
1731 return;
1732 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001733again:
1734 count = 0;
1735 xor_srcs = to_addr_page(percpu, j);
Dan Williams91c00922007-01-02 13:52:30 -07001736 /* check if prexor is active which means only process blocks
1737 * that are part of a read-modify-write (written)
1738 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001739 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001740 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001741 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1742 for (i = disks; i--; ) {
1743 struct r5dev *dev = &sh->dev[i];
shli@kernel.org59fc6302014-12-15 12:57:03 +11001744 if (head_sh->dev[i].written)
Dan Williams91c00922007-01-02 13:52:30 -07001745 xor_srcs[count++] = dev->page;
1746 }
1747 } else {
1748 xor_dest = sh->dev[pd_idx].page;
1749 for (i = disks; i--; ) {
1750 struct r5dev *dev = &sh->dev[i];
1751 if (i != pd_idx)
1752 xor_srcs[count++] = dev->page;
1753 }
1754 }
1755
Dan Williams91c00922007-01-02 13:52:30 -07001756 /* 1/ if we prexor'd then the dest is reused as a source
1757 * 2/ if we did not prexor then we are redoing the parity
1758 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1759 * for the synchronous xor case
1760 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001761 last_stripe = !head_sh->batch_head ||
1762 list_first_entry(&sh->batch_list,
1763 struct stripe_head, batch_list) == head_sh;
1764 if (last_stripe) {
1765 flags = ASYNC_TX_ACK |
1766 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
Dan Williams91c00922007-01-02 13:52:30 -07001767
shli@kernel.org59fc6302014-12-15 12:57:03 +11001768 atomic_inc(&head_sh->count);
1769 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
1770 to_addr_conv(sh, percpu, j));
1771 } else {
1772 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
1773 init_async_submit(&submit, flags, tx, NULL, NULL,
1774 to_addr_conv(sh, percpu, j));
1775 }
Dan Williams91c00922007-01-02 13:52:30 -07001776
Dan Williamsa08abd82009-06-03 11:43:59 -07001777 if (unlikely(count == 1))
1778 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1779 else
1780 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001781 if (!last_stripe) {
1782 j++;
1783 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1784 batch_list);
1785 goto again;
1786 }
Dan Williams91c00922007-01-02 13:52:30 -07001787}
1788
Dan Williamsac6b53b2009-07-14 13:40:19 -07001789static void
1790ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1791 struct dma_async_tx_descriptor *tx)
1792{
1793 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001794 struct page **blocks;
1795 int count, i, j = 0;
1796 struct stripe_head *head_sh = sh;
1797 int last_stripe;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001798 int synflags;
1799 unsigned long txflags;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001800
1801 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1802
Shaohua Li620125f2012-10-11 13:49:05 +11001803 for (i = 0; i < sh->disks; i++) {
1804 if (sh->pd_idx == i || sh->qd_idx == i)
1805 continue;
1806 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1807 break;
1808 }
1809 if (i >= sh->disks) {
1810 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001811 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1812 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1813 ops_complete_reconstruct(sh);
1814 return;
1815 }
1816
shli@kernel.org59fc6302014-12-15 12:57:03 +11001817again:
1818 blocks = to_addr_page(percpu, j);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001819
1820 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1821 synflags = SYNDROME_SRC_WRITTEN;
1822 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
1823 } else {
1824 synflags = SYNDROME_SRC_ALL;
1825 txflags = ASYNC_TX_ACK;
1826 }
1827
1828 count = set_syndrome_sources(blocks, sh, synflags);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001829 last_stripe = !head_sh->batch_head ||
1830 list_first_entry(&sh->batch_list,
1831 struct stripe_head, batch_list) == head_sh;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001832
shli@kernel.org59fc6302014-12-15 12:57:03 +11001833 if (last_stripe) {
1834 atomic_inc(&head_sh->count);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001835 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
shli@kernel.org59fc6302014-12-15 12:57:03 +11001836 head_sh, to_addr_conv(sh, percpu, j));
1837 } else
1838 init_async_submit(&submit, 0, tx, NULL, NULL,
1839 to_addr_conv(sh, percpu, j));
Shaohua Li48769692015-05-13 09:30:08 -07001840 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001841 if (!last_stripe) {
1842 j++;
1843 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1844 batch_list);
1845 goto again;
1846 }
Dan Williams91c00922007-01-02 13:52:30 -07001847}
1848
1849static void ops_complete_check(void *stripe_head_ref)
1850{
1851 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001852
Harvey Harrisone46b2722008-04-28 02:15:50 -07001853 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001854 (unsigned long long)sh->sector);
1855
Dan Williamsecc65c92008-06-28 08:31:57 +10001856 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001857 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001858 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001859}
1860
Dan Williamsac6b53b2009-07-14 13:40:19 -07001861static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001862{
Dan Williams91c00922007-01-02 13:52:30 -07001863 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001864 int pd_idx = sh->pd_idx;
1865 int qd_idx = sh->qd_idx;
1866 struct page *xor_dest;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001867 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001868 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001869 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001870 int count;
1871 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001872
Harvey Harrisone46b2722008-04-28 02:15:50 -07001873 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001874 (unsigned long long)sh->sector);
1875
shli@kernel.org59fc6302014-12-15 12:57:03 +11001876 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001877 count = 0;
1878 xor_dest = sh->dev[pd_idx].page;
1879 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001880 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001881 if (i == pd_idx || i == qd_idx)
1882 continue;
1883 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001884 }
1885
Dan Williamsd6f38f32009-07-14 11:50:52 -07001886 init_async_submit(&submit, 0, NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001887 to_addr_conv(sh, percpu, 0));
Dan Williams099f53c2009-04-08 14:28:37 -07001888 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001889 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001890
Dan Williams91c00922007-01-02 13:52:30 -07001891 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001892 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1893 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001894}
1895
Dan Williamsac6b53b2009-07-14 13:40:19 -07001896static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1897{
shli@kernel.org46d5b782014-12-15 12:57:02 +11001898 struct page **srcs = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001899 struct async_submit_ctl submit;
1900 int count;
1901
1902 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1903 (unsigned long long)sh->sector, checkp);
1904
shli@kernel.org59fc6302014-12-15 12:57:03 +11001905 BUG_ON(sh->batch_head);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001906 count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001907 if (!checkp)
1908 srcs[count] = NULL;
1909
1910 atomic_inc(&sh->count);
1911 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001912 sh, to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001913 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1914 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1915}
1916
NeilBrown51acbce2013-02-28 09:08:34 +11001917static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001918{
1919 int overlap_clear = 0, i, disks = sh->disks;
1920 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001921 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001922 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001923 struct raid5_percpu *percpu;
1924 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001925
Dan Williamsd6f38f32009-07-14 11:50:52 -07001926 cpu = get_cpu();
1927 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001928 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001929 ops_run_biofill(sh);
1930 overlap_clear++;
1931 }
1932
Dan Williams7b3a8712008-06-28 08:32:09 +10001933 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001934 if (level < 6)
1935 tx = ops_run_compute5(sh, percpu);
1936 else {
1937 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1938 tx = ops_run_compute6_1(sh, percpu);
1939 else
1940 tx = ops_run_compute6_2(sh, percpu);
1941 }
1942 /* terminate the chain if reconstruct is not set to be run */
1943 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001944 async_tx_ack(tx);
1945 }
Dan Williams91c00922007-01-02 13:52:30 -07001946
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001947 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
1948 if (level < 6)
1949 tx = ops_run_prexor5(sh, percpu, tx);
1950 else
1951 tx = ops_run_prexor6(sh, percpu, tx);
1952 }
Dan Williams91c00922007-01-02 13:52:30 -07001953
Dan Williams600aa102008-06-28 08:32:05 +10001954 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001955 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001956 overlap_clear++;
1957 }
1958
Dan Williamsac6b53b2009-07-14 13:40:19 -07001959 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1960 if (level < 6)
1961 ops_run_reconstruct5(sh, percpu, tx);
1962 else
1963 ops_run_reconstruct6(sh, percpu, tx);
1964 }
Dan Williams91c00922007-01-02 13:52:30 -07001965
Dan Williamsac6b53b2009-07-14 13:40:19 -07001966 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1967 if (sh->check_state == check_state_run)
1968 ops_run_check_p(sh, percpu);
1969 else if (sh->check_state == check_state_run_q)
1970 ops_run_check_pq(sh, percpu, 0);
1971 else if (sh->check_state == check_state_run_pq)
1972 ops_run_check_pq(sh, percpu, 1);
1973 else
1974 BUG();
1975 }
Dan Williams91c00922007-01-02 13:52:30 -07001976
shli@kernel.org59fc6302014-12-15 12:57:03 +11001977 if (overlap_clear && !sh->batch_head)
Dan Williams91c00922007-01-02 13:52:30 -07001978 for (i = disks; i--; ) {
1979 struct r5dev *dev = &sh->dev[i];
1980 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1981 wake_up(&sh->raid_conf->wait_for_overlap);
1982 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001983 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001984}
1985
NeilBrownf18c1a32015-05-08 18:19:04 +10001986static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp)
1987{
1988 struct stripe_head *sh;
1989
1990 sh = kmem_cache_zalloc(sc, gfp);
1991 if (sh) {
1992 spin_lock_init(&sh->stripe_lock);
1993 spin_lock_init(&sh->batch_lock);
1994 INIT_LIST_HEAD(&sh->batch_list);
1995 INIT_LIST_HEAD(&sh->lru);
1996 atomic_set(&sh->count, 1);
1997 }
1998 return sh;
1999}
NeilBrown486f0642015-02-25 12:10:35 +11002000static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
2002 struct stripe_head *sh;
NeilBrownf18c1a32015-05-08 18:19:04 +10002003
2004 sh = alloc_stripe(conf->slab_cache, gfp);
NeilBrown3f294f42005-11-08 21:39:25 -08002005 if (!sh)
2006 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10002007
NeilBrown3f294f42005-11-08 21:39:25 -08002008 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08002009
NeilBrowna9683a72015-02-25 12:02:51 +11002010 if (grow_buffers(sh, gfp)) {
NeilBrowne4e11e32010-06-16 16:45:16 +10002011 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002012 kmem_cache_free(conf->slab_cache, sh);
2013 return 0;
2014 }
NeilBrown486f0642015-02-25 12:10:35 +11002015 sh->hash_lock_index =
2016 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrown3f294f42005-11-08 21:39:25 -08002017 /* we just created an active stripe so... */
NeilBrown3f294f42005-11-08 21:39:25 -08002018 atomic_inc(&conf->active_stripes);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002019
Shaohua Li6d036f72015-08-13 14:31:57 -07002020 raid5_release_stripe(sh);
NeilBrown486f0642015-02-25 12:10:35 +11002021 conf->max_nr_stripes++;
NeilBrown3f294f42005-11-08 21:39:25 -08002022 return 1;
2023}
2024
NeilBrownd1688a62011-10-11 16:49:52 +11002025static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08002026{
Christoph Lametere18b8902006-12-06 20:33:20 -08002027 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11002028 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
NeilBrownf4be6b42010-06-01 19:37:25 +10002030 if (conf->mddev->gendisk)
2031 sprintf(conf->cache_name[0],
2032 "raid%d-%s", conf->level, mdname(conf->mddev));
2033 else
2034 sprintf(conf->cache_name[0],
2035 "raid%d-%p", conf->level, conf->mddev);
2036 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
2037
NeilBrownad01c9e2006-03-27 01:18:07 -08002038 conf->active_name = 0;
2039 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002041 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (!sc)
2043 return 1;
2044 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002045 conf->pool_size = devs;
NeilBrown486f0642015-02-25 12:10:35 +11002046 while (num--)
2047 if (!grow_one_stripe(conf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 return 1;
NeilBrown486f0642015-02-25 12:10:35 +11002049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 return 0;
2051}
NeilBrown29269552006-03-27 01:18:10 -08002052
Dan Williamsd6f38f32009-07-14 11:50:52 -07002053/**
2054 * scribble_len - return the required size of the scribble region
2055 * @num - total number of disks in the array
2056 *
2057 * The size must be enough to contain:
2058 * 1/ a struct page pointer for each device in the array +2
2059 * 2/ room to convert each entry in (1) to its corresponding dma
2060 * (dma_map_page()) or page (page_address()) address.
2061 *
2062 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2063 * calculate over all devices (not just the data blocks), using zeros in place
2064 * of the P and Q blocks.
2065 */
shli@kernel.org46d5b782014-12-15 12:57:02 +11002066static struct flex_array *scribble_alloc(int num, int cnt, gfp_t flags)
Dan Williamsd6f38f32009-07-14 11:50:52 -07002067{
shli@kernel.org46d5b782014-12-15 12:57:02 +11002068 struct flex_array *ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002069 size_t len;
2070
2071 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
shli@kernel.org46d5b782014-12-15 12:57:02 +11002072 ret = flex_array_alloc(len, cnt, flags);
2073 if (!ret)
2074 return NULL;
2075 /* always prealloc all elements, so no locking is required */
2076 if (flex_array_prealloc(ret, 0, cnt, flags)) {
2077 flex_array_free(ret);
2078 return NULL;
2079 }
2080 return ret;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002081}
2082
NeilBrown738a2732015-05-08 18:19:39 +10002083static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2084{
2085 unsigned long cpu;
2086 int err = 0;
2087
2088 mddev_suspend(conf->mddev);
2089 get_online_cpus();
2090 for_each_present_cpu(cpu) {
2091 struct raid5_percpu *percpu;
2092 struct flex_array *scribble;
2093
2094 percpu = per_cpu_ptr(conf->percpu, cpu);
2095 scribble = scribble_alloc(new_disks,
2096 new_sectors / STRIPE_SECTORS,
2097 GFP_NOIO);
2098
2099 if (scribble) {
2100 flex_array_free(percpu->scribble);
2101 percpu->scribble = scribble;
2102 } else {
2103 err = -ENOMEM;
2104 break;
2105 }
2106 }
2107 put_online_cpus();
2108 mddev_resume(conf->mddev);
2109 return err;
2110}
2111
NeilBrownd1688a62011-10-11 16:49:52 +11002112static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08002113{
2114 /* Make all the stripes able to hold 'newsize' devices.
2115 * New slots in each stripe get 'page' set to a new page.
2116 *
2117 * This happens in stages:
2118 * 1/ create a new kmem_cache and allocate the required number of
2119 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09002120 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08002121 * to the new stripe_heads. This will have the side effect of
2122 * freezing the array as once all stripe_heads have been collected,
2123 * no IO will be possible. Old stripe heads are freed once their
2124 * pages have been transferred over, and the old kmem_cache is
2125 * freed when all stripes are done.
2126 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
2127 * we simple return a failre status - no need to clean anything up.
2128 * 4/ allocate new pages for the new slots in the new stripe_heads.
2129 * If this fails, we don't bother trying the shrink the
2130 * stripe_heads down again, we just leave them as they are.
2131 * As each stripe_head is processed the new one is released into
2132 * active service.
2133 *
2134 * Once step2 is started, we cannot afford to wait for a write,
2135 * so we use GFP_NOIO allocations.
2136 */
2137 struct stripe_head *osh, *nsh;
2138 LIST_HEAD(newstripes);
2139 struct disk_info *ndisks;
Dan Williamsb5470dc2008-06-27 21:44:04 -07002140 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08002141 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002142 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11002143 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08002144
2145 if (newsize <= conf->pool_size)
2146 return 0; /* never bother to shrink */
2147
Dan Williamsb5470dc2008-06-27 21:44:04 -07002148 err = md_allow_write(conf->mddev);
2149 if (err)
2150 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08002151
NeilBrownad01c9e2006-03-27 01:18:07 -08002152 /* Step 1 */
2153 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2154 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002155 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08002156 if (!sc)
2157 return -ENOMEM;
2158
NeilBrown2d5b5692015-07-06 12:49:23 +10002159 /* Need to ensure auto-resizing doesn't interfere */
2160 mutex_lock(&conf->cache_size_mutex);
2161
NeilBrownad01c9e2006-03-27 01:18:07 -08002162 for (i = conf->max_nr_stripes; i; i--) {
NeilBrownf18c1a32015-05-08 18:19:04 +10002163 nsh = alloc_stripe(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08002164 if (!nsh)
2165 break;
2166
NeilBrownad01c9e2006-03-27 01:18:07 -08002167 nsh->raid_conf = conf;
NeilBrownad01c9e2006-03-27 01:18:07 -08002168 list_add(&nsh->lru, &newstripes);
2169 }
2170 if (i) {
2171 /* didn't get enough, give up */
2172 while (!list_empty(&newstripes)) {
2173 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2174 list_del(&nsh->lru);
2175 kmem_cache_free(sc, nsh);
2176 }
2177 kmem_cache_destroy(sc);
NeilBrown2d5b5692015-07-06 12:49:23 +10002178 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002179 return -ENOMEM;
2180 }
2181 /* Step 2 - Must use GFP_NOIO now.
2182 * OK, we have enough stripes, start collecting inactive
2183 * stripes and copying them over
2184 */
Shaohua Li566c09c2013-11-14 15:16:17 +11002185 hash = 0;
2186 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08002187 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11002188 lock_device_hash_lock(conf, hash);
Yuanhan Liue9e4c372015-05-08 18:19:07 +10002189 wait_event_exclusive_cmd(conf->wait_for_stripe[hash],
Shaohua Li566c09c2013-11-14 15:16:17 +11002190 !list_empty(conf->inactive_list + hash),
2191 unlock_device_hash_lock(conf, hash),
2192 lock_device_hash_lock(conf, hash));
2193 osh = get_free_stripe(conf, hash);
2194 unlock_device_hash_lock(conf, hash);
NeilBrownf18c1a32015-05-08 18:19:04 +10002195
Shaohua Lid592a992014-05-21 17:57:44 +08002196 for(i=0; i<conf->pool_size; i++) {
NeilBrownad01c9e2006-03-27 01:18:07 -08002197 nsh->dev[i].page = osh->dev[i].page;
Shaohua Lid592a992014-05-21 17:57:44 +08002198 nsh->dev[i].orig_page = osh->dev[i].page;
2199 }
Shaohua Li566c09c2013-11-14 15:16:17 +11002200 nsh->hash_lock_index = hash;
NeilBrownad01c9e2006-03-27 01:18:07 -08002201 kmem_cache_free(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11002202 cnt++;
2203 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2204 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2205 hash++;
2206 cnt = 0;
2207 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002208 }
2209 kmem_cache_destroy(conf->slab_cache);
2210
2211 /* Step 3.
2212 * At this point, we are holding all the stripes so the array
2213 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07002214 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08002215 */
2216 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
2217 if (ndisks) {
2218 for (i=0; i<conf->raid_disks; i++)
2219 ndisks[i] = conf->disks[i];
2220 kfree(conf->disks);
2221 conf->disks = ndisks;
2222 } else
2223 err = -ENOMEM;
2224
NeilBrown2d5b5692015-07-06 12:49:23 +10002225 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002226 /* Step 4, return new stripes to service */
2227 while(!list_empty(&newstripes)) {
2228 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2229 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07002230
NeilBrownad01c9e2006-03-27 01:18:07 -08002231 for (i=conf->raid_disks; i < newsize; i++)
2232 if (nsh->dev[i].page == NULL) {
2233 struct page *p = alloc_page(GFP_NOIO);
2234 nsh->dev[i].page = p;
Shaohua Lid592a992014-05-21 17:57:44 +08002235 nsh->dev[i].orig_page = p;
NeilBrownad01c9e2006-03-27 01:18:07 -08002236 if (!p)
2237 err = -ENOMEM;
2238 }
Shaohua Li6d036f72015-08-13 14:31:57 -07002239 raid5_release_stripe(nsh);
NeilBrownad01c9e2006-03-27 01:18:07 -08002240 }
2241 /* critical section pass, GFP_NOIO no longer needed */
2242
2243 conf->slab_cache = sc;
2244 conf->active_name = 1-conf->active_name;
NeilBrown6e9eac22015-05-08 18:19:34 +10002245 if (!err)
2246 conf->pool_size = newsize;
NeilBrownad01c9e2006-03-27 01:18:07 -08002247 return err;
2248}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
NeilBrown486f0642015-02-25 12:10:35 +11002250static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
2252 struct stripe_head *sh;
NeilBrown49895bc2015-08-03 17:09:57 +10002253 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Shaohua Li566c09c2013-11-14 15:16:17 +11002255 spin_lock_irq(conf->hash_locks + hash);
2256 sh = get_free_stripe(conf, hash);
2257 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08002258 if (!sh)
2259 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002260 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10002261 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002262 kmem_cache_free(conf->slab_cache, sh);
2263 atomic_dec(&conf->active_stripes);
NeilBrown486f0642015-02-25 12:10:35 +11002264 conf->max_nr_stripes--;
NeilBrown3f294f42005-11-08 21:39:25 -08002265 return 1;
2266}
2267
NeilBrownd1688a62011-10-11 16:49:52 +11002268static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08002269{
NeilBrown486f0642015-02-25 12:10:35 +11002270 while (conf->max_nr_stripes &&
2271 drop_one_stripe(conf))
2272 ;
NeilBrown3f294f42005-11-08 21:39:25 -08002273
Julia Lawall644df1a2015-09-13 14:15:10 +02002274 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 conf->slab_cache = NULL;
2276}
2277
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002278static void raid5_end_read_request(struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
NeilBrown99c0fb52009-03-31 14:39:38 +11002280 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002281 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002282 int disks = sh->disks, i;
NeilBrownd6950432006-07-10 04:44:20 -07002283 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11002284 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10002285 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 for (i=0 ; i<disks; i++)
2288 if (bi == &sh->dev[i].req)
2289 break;
2290
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002291 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
Dan Williams45b42332007-07-09 11:56:43 -07002292 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002293 bi->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if (i == disks) {
2295 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002296 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 }
NeilBrown14a75d32011-12-23 10:17:52 +11002298 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11002299 /* If replacement finished while this request was outstanding,
2300 * 'replacement' might be NULL already.
2301 * In that case it moved down to 'rdev'.
2302 * rdev is not removed until all requests are finished.
2303 */
NeilBrown14a75d32011-12-23 10:17:52 +11002304 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002305 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11002306 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
NeilBrown05616be2012-05-21 09:27:00 +10002308 if (use_new_offset(conf, sh))
2309 s = sh->sector + rdev->new_data_offset;
2310 else
2311 s = sh->sector + rdev->data_offset;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002312 if (!bi->bi_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08002314 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11002315 /* Note that this cannot happen on a
2316 * replacement device. We just fail those on
2317 * any error
2318 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10002319 printk_ratelimited(
2320 KERN_INFO
2321 "md/raid:%s: read error corrected"
2322 " (%lu sectors at %llu on %s)\n",
2323 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10002324 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002325 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10002326 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08002327 clear_bit(R5_ReadError, &sh->dev[i].flags);
2328 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002329 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2330 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2331
NeilBrown14a75d32011-12-23 10:17:52 +11002332 if (atomic_read(&rdev->read_errors))
2333 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002335 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002336 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002337 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07002340 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002341 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
2342 printk_ratelimited(
2343 KERN_WARNING
2344 "md/raid:%s: read error on replacement device "
2345 "(sector %llu on %s).\n",
2346 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002347 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002348 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002349 else if (conf->mddev->degraded >= conf->max_degraded) {
2350 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10002351 printk_ratelimited(
2352 KERN_WARNING
2353 "md/raid:%s: read error not correctable "
2354 "(sector %llu on %s).\n",
2355 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002356 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002357 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002358 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002359 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002360 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10002361 printk_ratelimited(
2362 KERN_WARNING
2363 "md/raid:%s: read error NOT corrected!! "
2364 "(sector %llu on %s).\n",
2365 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002366 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002367 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002368 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08002369 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08002370 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10002371 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07002372 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08002373 else
2374 retry = 1;
Bian Yuedfa1f62013-11-14 15:16:17 +11002375 if (set_bad && test_bit(In_sync, &rdev->flags)
2376 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2377 retry = 1;
NeilBrownba22dcb2005-11-08 21:39:31 -08002378 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10002379 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
2380 set_bit(R5_ReadError, &sh->dev[i].flags);
2381 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2382 } else
2383 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002384 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002385 clear_bit(R5_ReadError, &sh->dev[i].flags);
2386 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002387 if (!(set_bad
2388 && test_bit(In_sync, &rdev->flags)
2389 && rdev_set_badblocks(
2390 rdev, sh->sector, STRIPE_SECTORS, 0)))
2391 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 }
NeilBrown14a75d32011-12-23 10:17:52 +11002394 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2396 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002397 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002400static void raid5_end_write_request(struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401{
NeilBrown99c0fb52009-03-31 14:39:38 +11002402 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002403 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002404 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11002405 struct md_rdev *uninitialized_var(rdev);
NeilBrownb84db562011-07-28 11:39:23 +10002406 sector_t first_bad;
2407 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002408 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
NeilBrown977df362011-12-23 10:17:53 +11002410 for (i = 0 ; i < disks; i++) {
2411 if (bi == &sh->dev[i].req) {
2412 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 break;
NeilBrown977df362011-12-23 10:17:53 +11002414 }
2415 if (bi == &sh->dev[i].rreq) {
2416 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002417 if (rdev)
2418 replacement = 1;
2419 else
2420 /* rdev was removed and 'replacement'
2421 * replaced it. rdev is not removed
2422 * until all requests are finished.
2423 */
2424 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002425 break;
2426 }
2427 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002428 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002430 bi->bi_error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (i == disks) {
2432 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002433 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 }
2435
NeilBrown977df362011-12-23 10:17:53 +11002436 if (replacement) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002437 if (bi->bi_error)
NeilBrown977df362011-12-23 10:17:53 +11002438 md_error(conf->mddev, rdev);
2439 else if (is_badblock(rdev, sh->sector,
2440 STRIPE_SECTORS,
2441 &first_bad, &bad_sectors))
2442 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2443 } else {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002444 if (bi->bi_error) {
NeilBrown9f97e4b2014-01-16 09:35:38 +11002445 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11002446 set_bit(WriteErrorSeen, &rdev->flags);
2447 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002448 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2449 set_bit(MD_RECOVERY_NEEDED,
2450 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002451 } else if (is_badblock(rdev, sh->sector,
2452 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10002453 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002454 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002455 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2456 /* That was a successful write so make
2457 * sure it looks like we already did
2458 * a re-write.
2459 */
2460 set_bit(R5_ReWrite, &sh->dev[i].flags);
2461 }
NeilBrown977df362011-12-23 10:17:53 +11002462 }
2463 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002465 if (sh->batch_head && bi->bi_error && !replacement)
shli@kernel.org72ac7332014-12-15 12:57:03 +11002466 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2467
NeilBrown977df362011-12-23 10:17:53 +11002468 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2469 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002471 raid5_release_stripe(sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002472
2473 if (sh->batch_head && sh != sh->batch_head)
Shaohua Li6d036f72015-08-13 14:31:57 -07002474 raid5_release_stripe(sh->batch_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
NeilBrown784052e2009-03-31 15:19:07 +11002477static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
2479 struct r5dev *dev = &sh->dev[i];
2480
2481 bio_init(&dev->req);
2482 dev->req.bi_io_vec = &dev->vec;
Shaohua Lid592a992014-05-21 17:57:44 +08002483 dev->req.bi_max_vecs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 dev->req.bi_private = sh;
2485
NeilBrown977df362011-12-23 10:17:53 +11002486 bio_init(&dev->rreq);
2487 dev->rreq.bi_io_vec = &dev->rvec;
Shaohua Lid592a992014-05-21 17:57:44 +08002488 dev->rreq.bi_max_vecs = 1;
NeilBrown977df362011-12-23 10:17:53 +11002489 dev->rreq.bi_private = sh;
NeilBrown977df362011-12-23 10:17:53 +11002490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 dev->flags = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07002492 dev->sector = raid5_compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493}
2494
NeilBrownfd01b882011-10-11 16:47:53 +11002495static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
2497 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002498 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002499 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002500 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
NeilBrown908f4fb2011-12-23 10:17:50 +11002502 spin_lock_irqsave(&conf->device_lock, flags);
2503 clear_bit(In_sync, &rdev->flags);
2504 mddev->degraded = calc_degraded(conf);
2505 spin_unlock_irqrestore(&conf->device_lock, flags);
2506 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2507
NeilBrownde393cd2011-07-28 11:31:48 +10002508 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10002509 set_bit(Faulty, &rdev->flags);
2510 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownc3cce6c2015-08-14 12:47:33 +10002511 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10002512 printk(KERN_ALERT
2513 "md/raid:%s: Disk failure on %s, disabling device.\n"
2514 "md/raid:%s: Operation continuing on %d devices.\n",
2515 mdname(mddev),
2516 bdevname(rdev->bdev, b),
2517 mdname(mddev),
2518 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07002519}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
2521/*
2522 * Input: a 'big' sector number,
2523 * Output: index of the data and parity disk, and the sector # in them.
2524 */
Shaohua Li6d036f72015-08-13 14:31:57 -07002525sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2526 int previous, int *dd_idx,
2527 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002529 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002530 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002532 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002533 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002535 int algorithm = previous ? conf->prev_algo
2536 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002537 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2538 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002539 int raid_disks = previous ? conf->previous_raid_disks
2540 : conf->raid_disks;
2541 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
2543 /* First compute the information on this sector */
2544
2545 /*
2546 * Compute the chunk number and the sector offset inside the chunk
2547 */
2548 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2549 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 /*
2552 * Compute the stripe number
2553 */
NeilBrown35f2a592010-04-20 14:13:34 +10002554 stripe = chunk_number;
2555 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002556 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 /*
2558 * Select the parity disk based on the user selected algorithm.
2559 */
NeilBrown84789552011-07-27 11:00:36 +10002560 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002561 switch(conf->level) {
2562 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002563 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002564 break;
2565 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002566 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002568 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002569 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 (*dd_idx)++;
2571 break;
2572 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002573 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002574 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 (*dd_idx)++;
2576 break;
2577 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002578 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002579 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 break;
2581 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002582 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002583 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002585 case ALGORITHM_PARITY_0:
2586 pd_idx = 0;
2587 (*dd_idx)++;
2588 break;
2589 case ALGORITHM_PARITY_N:
2590 pd_idx = data_disks;
2591 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002593 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002594 }
2595 break;
2596 case 6:
2597
NeilBrowne183eae2009-03-31 15:20:22 +11002598 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002599 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002600 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002601 qd_idx = pd_idx + 1;
2602 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002603 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002604 qd_idx = 0;
2605 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002606 (*dd_idx) += 2; /* D D P Q D */
2607 break;
2608 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002609 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002610 qd_idx = pd_idx + 1;
2611 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002612 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002613 qd_idx = 0;
2614 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002615 (*dd_idx) += 2; /* D D P Q D */
2616 break;
2617 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002618 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002619 qd_idx = (pd_idx + 1) % raid_disks;
2620 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002621 break;
2622 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002623 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002624 qd_idx = (pd_idx + 1) % raid_disks;
2625 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002626 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002627
2628 case ALGORITHM_PARITY_0:
2629 pd_idx = 0;
2630 qd_idx = 1;
2631 (*dd_idx) += 2;
2632 break;
2633 case ALGORITHM_PARITY_N:
2634 pd_idx = data_disks;
2635 qd_idx = data_disks + 1;
2636 break;
2637
2638 case ALGORITHM_ROTATING_ZERO_RESTART:
2639 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2640 * of blocks for computing Q is different.
2641 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002642 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002643 qd_idx = pd_idx + 1;
2644 if (pd_idx == raid_disks-1) {
2645 (*dd_idx)++; /* Q D D D P */
2646 qd_idx = 0;
2647 } else if (*dd_idx >= pd_idx)
2648 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002649 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002650 break;
2651
2652 case ALGORITHM_ROTATING_N_RESTART:
2653 /* Same a left_asymmetric, by first stripe is
2654 * D D D P Q rather than
2655 * Q D D D P
2656 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002657 stripe2 += 1;
2658 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002659 qd_idx = pd_idx + 1;
2660 if (pd_idx == raid_disks-1) {
2661 (*dd_idx)++; /* Q D D D P */
2662 qd_idx = 0;
2663 } else if (*dd_idx >= pd_idx)
2664 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002665 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002666 break;
2667
2668 case ALGORITHM_ROTATING_N_CONTINUE:
2669 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002670 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002671 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2672 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002673 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002674 break;
2675
2676 case ALGORITHM_LEFT_ASYMMETRIC_6:
2677 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002678 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002679 if (*dd_idx >= pd_idx)
2680 (*dd_idx)++;
2681 qd_idx = raid_disks - 1;
2682 break;
2683
2684 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002685 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002686 if (*dd_idx >= pd_idx)
2687 (*dd_idx)++;
2688 qd_idx = raid_disks - 1;
2689 break;
2690
2691 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002692 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002693 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2694 qd_idx = raid_disks - 1;
2695 break;
2696
2697 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002698 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002699 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2700 qd_idx = raid_disks - 1;
2701 break;
2702
2703 case ALGORITHM_PARITY_0_6:
2704 pd_idx = 0;
2705 (*dd_idx)++;
2706 qd_idx = raid_disks - 1;
2707 break;
2708
NeilBrown16a53ec2006-06-26 00:27:38 -07002709 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002710 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002711 }
2712 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
2714
NeilBrown911d4ee2009-03-31 14:39:38 +11002715 if (sh) {
2716 sh->pd_idx = pd_idx;
2717 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002718 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 /*
2721 * Finally, compute the new sector number
2722 */
2723 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2724 return new_sector;
2725}
2726
Shaohua Li6d036f72015-08-13 14:31:57 -07002727sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728{
NeilBrownd1688a62011-10-11 16:49:52 +11002729 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002730 int raid_disks = sh->disks;
2731 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002733 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2734 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002735 int algorithm = previous ? conf->prev_algo
2736 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 sector_t stripe;
2738 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002739 sector_t chunk_number;
2740 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002742 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
2744 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2745 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
NeilBrown16a53ec2006-06-26 00:27:38 -07002747 if (i == sh->pd_idx)
2748 return 0;
2749 switch(conf->level) {
2750 case 4: break;
2751 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002752 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 case ALGORITHM_LEFT_ASYMMETRIC:
2754 case ALGORITHM_RIGHT_ASYMMETRIC:
2755 if (i > sh->pd_idx)
2756 i--;
2757 break;
2758 case ALGORITHM_LEFT_SYMMETRIC:
2759 case ALGORITHM_RIGHT_SYMMETRIC:
2760 if (i < sh->pd_idx)
2761 i += raid_disks;
2762 i -= (sh->pd_idx + 1);
2763 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002764 case ALGORITHM_PARITY_0:
2765 i -= 1;
2766 break;
2767 case ALGORITHM_PARITY_N:
2768 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002770 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002771 }
2772 break;
2773 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002774 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002775 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002776 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002777 case ALGORITHM_LEFT_ASYMMETRIC:
2778 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002779 case ALGORITHM_ROTATING_ZERO_RESTART:
2780 case ALGORITHM_ROTATING_N_RESTART:
2781 if (sh->pd_idx == raid_disks-1)
2782 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002783 else if (i > sh->pd_idx)
2784 i -= 2; /* D D P Q D */
2785 break;
2786 case ALGORITHM_LEFT_SYMMETRIC:
2787 case ALGORITHM_RIGHT_SYMMETRIC:
2788 if (sh->pd_idx == raid_disks-1)
2789 i--; /* Q D D D P */
2790 else {
2791 /* D D P Q D */
2792 if (i < sh->pd_idx)
2793 i += raid_disks;
2794 i -= (sh->pd_idx + 2);
2795 }
2796 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002797 case ALGORITHM_PARITY_0:
2798 i -= 2;
2799 break;
2800 case ALGORITHM_PARITY_N:
2801 break;
2802 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002803 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002804 if (sh->pd_idx == 0)
2805 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002806 else {
2807 /* D D Q P D */
2808 if (i < sh->pd_idx)
2809 i += raid_disks;
2810 i -= (sh->pd_idx + 1);
2811 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002812 break;
2813 case ALGORITHM_LEFT_ASYMMETRIC_6:
2814 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2815 if (i > sh->pd_idx)
2816 i--;
2817 break;
2818 case ALGORITHM_LEFT_SYMMETRIC_6:
2819 case ALGORITHM_RIGHT_SYMMETRIC_6:
2820 if (i < sh->pd_idx)
2821 i += data_disks + 1;
2822 i -= (sh->pd_idx + 1);
2823 break;
2824 case ALGORITHM_PARITY_0_6:
2825 i -= 1;
2826 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002827 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002828 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002829 }
2830 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 }
2832
2833 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002834 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
NeilBrown112bf892009-03-31 14:39:38 +11002836 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002837 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002838 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2839 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002840 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2841 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return 0;
2843 }
2844 return r_sector;
2845}
2846
Dan Williams600aa102008-06-28 08:32:05 +10002847static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002848schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002849 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002850{
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002851 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002852 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002853 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002854
Dan Williamse33129d2007-01-02 13:52:30 -07002855 if (rcw) {
Dan Williamse33129d2007-01-02 13:52:30 -07002856
2857 for (i = disks; i--; ) {
2858 struct r5dev *dev = &sh->dev[i];
2859
2860 if (dev->towrite) {
2861 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002862 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002863 if (!expand)
2864 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002865 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002866 }
2867 }
NeilBrownce7d3632013-03-04 12:37:14 +11002868 /* if we are not expanding this is a proper write request, and
2869 * there will be bios with new data to be drained into the
2870 * stripe cache
2871 */
2872 if (!expand) {
2873 if (!s->locked)
2874 /* False alarm, nothing to do */
2875 return;
2876 sh->reconstruct_state = reconstruct_state_drain_run;
2877 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2878 } else
2879 sh->reconstruct_state = reconstruct_state_run;
2880
2881 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2882
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002883 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002884 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002885 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002886 } else {
2887 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2888 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002889 BUG_ON(level == 6 &&
2890 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
2891 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
Dan Williamse33129d2007-01-02 13:52:30 -07002892
Dan Williamse33129d2007-01-02 13:52:30 -07002893 for (i = disks; i--; ) {
2894 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002895 if (i == pd_idx || i == qd_idx)
Dan Williamse33129d2007-01-02 13:52:30 -07002896 continue;
2897
Dan Williamse33129d2007-01-02 13:52:30 -07002898 if (dev->towrite &&
2899 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002900 test_bit(R5_Wantcompute, &dev->flags))) {
2901 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002902 set_bit(R5_LOCKED, &dev->flags);
2903 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002904 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002905 }
2906 }
NeilBrownce7d3632013-03-04 12:37:14 +11002907 if (!s->locked)
2908 /* False alarm - nothing to do */
2909 return;
2910 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2911 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2912 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2913 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002914 }
2915
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002916 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002917 * are in flight
2918 */
2919 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2920 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002921 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002922
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002923 if (level == 6) {
2924 int qd_idx = sh->qd_idx;
2925 struct r5dev *dev = &sh->dev[qd_idx];
2926
2927 set_bit(R5_LOCKED, &dev->flags);
2928 clear_bit(R5_UPTODATE, &dev->flags);
2929 s->locked++;
2930 }
2931
Dan Williams600aa102008-06-28 08:32:05 +10002932 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002933 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002934 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002935}
NeilBrown16a53ec2006-06-26 00:27:38 -07002936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937/*
2938 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002939 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 * The bi_next chain must be in order.
2941 */
shli@kernel.orgda41ba62014-12-15 12:57:03 +11002942static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
2943 int forwrite, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944{
2945 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002946 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002947 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
NeilBrowncbe47ec2011-07-26 11:20:35 +10002949 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07002950 (unsigned long long)bi->bi_iter.bi_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 (unsigned long long)sh->sector);
2952
Shaohua Lib17459c2012-07-19 16:01:31 +10002953 /*
2954 * If several bio share a stripe. The bio bi_phys_segments acts as a
2955 * reference count to avoid race. The reference count should already be
2956 * increased before this function is called (for example, in
2957 * make_request()), so other bio sharing this stripe will not free the
2958 * stripe. If a stripe is owned by one stripe, the stripe lock will
2959 * protect it.
2960 */
2961 spin_lock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002962 /* Don't allow new IO added to stripes in batch list */
2963 if (sh->batch_head)
2964 goto overlap;
NeilBrown72626682005-09-09 16:23:54 -07002965 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002967 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002968 firstwrite = 1;
2969 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 bip = &sh->dev[dd_idx].toread;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002971 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
2972 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 goto overlap;
2974 bip = & (*bip)->bi_next;
2975 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07002976 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 goto overlap;
2978
shli@kernel.orgda41ba62014-12-15 12:57:03 +11002979 if (!forwrite || previous)
2980 clear_bit(STRIPE_BATCH_READY, &sh->state);
2981
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002982 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 if (*bip)
2984 bi->bi_next = *bip;
2985 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002986 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002987
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 if (forwrite) {
2989 /* check if page is covered */
2990 sector_t sector = sh->dev[dd_idx].sector;
2991 for (bi=sh->dev[dd_idx].towrite;
2992 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07002993 bi && bi->bi_iter.bi_sector <= sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002995 if (bio_end_sector(bi) >= sector)
2996 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 }
2998 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
shli@kernel.org7a87f432014-12-15 12:57:03 +11002999 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
3000 sh->overwrite_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003002
3003 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003004 (unsigned long long)(*bip)->bi_iter.bi_sector,
NeilBrowncbe47ec2011-07-26 11:20:35 +10003005 (unsigned long long)sh->sector, dd_idx);
3006
3007 if (conf->mddev->bitmap && firstwrite) {
NeilBrownd0852df52015-05-27 08:43:45 +10003008 /* Cannot hold spinlock over bitmap_startwrite,
3009 * but must ensure this isn't added to a batch until
3010 * we have added to the bitmap and set bm_seq.
3011 * So set STRIPE_BITMAP_PENDING to prevent
3012 * batching.
3013 * If multiple add_stripe_bio() calls race here they
3014 * much all set STRIPE_BITMAP_PENDING. So only the first one
3015 * to complete "bitmap_startwrite" gets to set
3016 * STRIPE_BIT_DELAY. This is important as once a stripe
3017 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3018 * any more.
3019 */
3020 set_bit(STRIPE_BITMAP_PENDING, &sh->state);
3021 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10003022 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
3023 STRIPE_SECTORS, 0);
NeilBrownd0852df52015-05-27 08:43:45 +10003024 spin_lock_irq(&sh->stripe_lock);
3025 clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
3026 if (!sh->batch_head) {
3027 sh->bm_seq = conf->seq_flush+1;
3028 set_bit(STRIPE_BIT_DELAY, &sh->state);
3029 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003030 }
NeilBrownd0852df52015-05-27 08:43:45 +10003031 spin_unlock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003032
3033 if (stripe_can_batch(sh))
3034 stripe_add_to_batch_list(conf, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 return 1;
3036
3037 overlap:
3038 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10003039 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 return 0;
3041}
3042
NeilBrownd1688a62011-10-11 16:49:52 +11003043static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08003044
NeilBrownd1688a62011-10-11 16:49:52 +11003045static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003046 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08003047{
NeilBrown784052e2009-03-31 15:19:07 +11003048 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10003049 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11003050 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003051 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11003052 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003053
NeilBrown112bf892009-03-31 14:39:38 +11003054 raid5_compute_sector(conf,
3055 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08003056 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11003057 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003058 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003059}
3060
Dan Williamsa4456852007-07-09 11:56:43 -07003061static void
NeilBrownd1688a62011-10-11 16:49:52 +11003062handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003063 struct stripe_head_state *s, int disks,
NeilBrown34a6f802015-08-14 12:07:57 +10003064 struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003065{
3066 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003067 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003068 for (i = disks; i--; ) {
3069 struct bio *bi;
3070 int bitmap_end = 0;
3071
3072 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003073 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07003074 rcu_read_lock();
3075 rdev = rcu_dereference(conf->disks[i].rdev);
3076 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10003077 atomic_inc(&rdev->nr_pending);
3078 else
3079 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003080 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10003081 if (rdev) {
3082 if (!rdev_set_badblocks(
3083 rdev,
3084 sh->sector,
3085 STRIPE_SECTORS, 0))
3086 md_error(conf->mddev, rdev);
3087 rdev_dec_pending(rdev, conf->mddev);
3088 }
Dan Williamsa4456852007-07-09 11:56:43 -07003089 }
Shaohua Lib17459c2012-07-19 16:01:31 +10003090 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003091 /* fail all writes first */
3092 bi = sh->dev[i].towrite;
3093 sh->dev[i].towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11003094 sh->overwrite_disks = 0;
Shaohua Lib17459c2012-07-19 16:01:31 +10003095 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11003096 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003097 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07003098
3099 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3100 wake_up(&conf->wait_for_overlap);
3101
Kent Overstreet4f024f32013-10-11 15:44:27 -07003102 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003103 sh->dev[i].sector + STRIPE_SECTORS) {
3104 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003105
3106 bi->bi_error = -EIO;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003107 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003108 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003109 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003110 }
3111 bi = nextbi;
3112 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003113 if (bitmap_end)
3114 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3115 STRIPE_SECTORS, 0, 0);
3116 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003117 /* and fail all 'written' */
3118 bi = sh->dev[i].written;
3119 sh->dev[i].written = NULL;
Shaohua Lid592a992014-05-21 17:57:44 +08003120 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3121 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3122 sh->dev[i].page = sh->dev[i].orig_page;
3123 }
3124
Dan Williamsa4456852007-07-09 11:56:43 -07003125 if (bi) bitmap_end = 1;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003126 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003127 sh->dev[i].sector + STRIPE_SECTORS) {
3128 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003129
3130 bi->bi_error = -EIO;
Shaohua Lie7836bd62012-07-19 16:01:31 +10003131 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003132 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003133 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003134 }
3135 bi = bi2;
3136 }
3137
Dan Williamsb5e98d62007-01-02 13:52:31 -07003138 /* fail any reads if this device is non-operational and
3139 * the data has not reached the cache yet.
3140 */
3141 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
3142 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3143 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11003144 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003145 bi = sh->dev[i].toread;
3146 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11003147 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003148 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3149 wake_up(&conf->wait_for_overlap);
Shaohua Liebda7802015-09-18 10:20:13 -07003150 if (bi)
3151 s->to_read--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003152 while (bi && bi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003153 sh->dev[i].sector + STRIPE_SECTORS) {
3154 struct bio *nextbi =
3155 r5_next_bio(bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003156
3157 bi->bi_error = -EIO;
NeilBrown34a6f802015-08-14 12:07:57 +10003158 if (!raid5_dec_bi_active_stripes(bi))
3159 bio_list_add(return_bi, bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003160 bi = nextbi;
3161 }
3162 }
Dan Williamsa4456852007-07-09 11:56:43 -07003163 if (bitmap_end)
3164 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3165 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10003166 /* If we were in the middle of a write the parity block might
3167 * still be locked - so just clear all R5_LOCKED flags
3168 */
3169 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003170 }
Shaohua Liebda7802015-09-18 10:20:13 -07003171 s->to_write = 0;
3172 s->written = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003173
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003174 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3175 if (atomic_dec_and_test(&conf->pending_full_writes))
3176 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003177}
3178
NeilBrown7f0da592011-07-28 11:39:22 +10003179static void
NeilBrownd1688a62011-10-11 16:49:52 +11003180handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10003181 struct stripe_head_state *s)
3182{
3183 int abort = 0;
3184 int i;
3185
shli@kernel.org59fc6302014-12-15 12:57:03 +11003186 BUG_ON(sh->batch_head);
NeilBrown7f0da592011-07-28 11:39:22 +10003187 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003188 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3189 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10003190 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11003191 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10003192 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10003193 * Don't even need to abort as that is handled elsewhere
3194 * if needed, and not always wanted e.g. if there is a known
3195 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11003196 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10003197 * non-sync devices, or abort the recovery
3198 */
NeilBrown18b98372012-04-01 23:48:38 +10003199 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3200 /* During recovery devices cannot be removed, so
3201 * locking and refcounting of rdevs is not needed
3202 */
3203 for (i = 0; i < conf->raid_disks; i++) {
3204 struct md_rdev *rdev = conf->disks[i].rdev;
3205 if (rdev
3206 && !test_bit(Faulty, &rdev->flags)
3207 && !test_bit(In_sync, &rdev->flags)
3208 && !rdev_set_badblocks(rdev, sh->sector,
3209 STRIPE_SECTORS, 0))
3210 abort = 1;
3211 rdev = conf->disks[i].replacement;
3212 if (rdev
3213 && !test_bit(Faulty, &rdev->flags)
3214 && !test_bit(In_sync, &rdev->flags)
3215 && !rdev_set_badblocks(rdev, sh->sector,
3216 STRIPE_SECTORS, 0))
3217 abort = 1;
3218 }
3219 if (abort)
3220 conf->recovery_disabled =
3221 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10003222 }
NeilBrown18b98372012-04-01 23:48:38 +10003223 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10003224}
3225
NeilBrown9a3e1102011-12-23 10:17:53 +11003226static int want_replace(struct stripe_head *sh, int disk_idx)
3227{
3228 struct md_rdev *rdev;
3229 int rv = 0;
3230 /* Doing recovery so rcu locking not required */
3231 rdev = sh->raid_conf->disks[disk_idx].replacement;
3232 if (rdev
3233 && !test_bit(Faulty, &rdev->flags)
3234 && !test_bit(In_sync, &rdev->flags)
3235 && (rdev->recovery_offset <= sh->sector
3236 || rdev->mddev->recovery_cp <= sh->sector))
3237 rv = 1;
3238
3239 return rv;
3240}
3241
NeilBrown93b3dbc2011-07-27 11:00:36 +10003242/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10003243 * to be read or computed to satisfy a request.
3244 *
3245 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10003246 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07003247 */
NeilBrown2c58f062015-02-02 11:32:23 +11003248
3249static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3250 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07003251{
3252 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10003253 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3254 &sh->dev[s->failed_num[1]] };
NeilBrownea664c82015-02-02 14:03:28 +11003255 int i;
Dan Williamsf38e1212007-01-02 13:52:30 -07003256
NeilBrowna79cfe12015-02-02 11:37:59 +11003257
3258 if (test_bit(R5_LOCKED, &dev->flags) ||
3259 test_bit(R5_UPTODATE, &dev->flags))
3260 /* No point reading this as we already have it or have
3261 * decided to get it.
3262 */
3263 return 0;
3264
3265 if (dev->toread ||
3266 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3267 /* We need this block to directly satisfy a request */
3268 return 1;
3269
3270 if (s->syncing || s->expanding ||
3271 (s->replacing && want_replace(sh, disk_idx)))
3272 /* When syncing, or expanding we read everything.
3273 * When replacing, we need the replaced block.
3274 */
3275 return 1;
3276
3277 if ((s->failed >= 1 && fdev[0]->toread) ||
3278 (s->failed >= 2 && fdev[1]->toread))
3279 /* If we want to read from a failed device, then
3280 * we need to actually read every other device.
3281 */
3282 return 1;
3283
NeilBrowna9d56952015-02-02 11:49:10 +11003284 /* Sometimes neither read-modify-write nor reconstruct-write
3285 * cycles can work. In those cases we read every block we
3286 * can. Then the parity-update is certain to have enough to
3287 * work with.
3288 * This can only be a problem when we need to write something,
3289 * and some device has failed. If either of those tests
3290 * fail we need look no further.
3291 */
3292 if (!s->failed || !s->to_write)
3293 return 0;
3294
3295 if (test_bit(R5_Insync, &dev->flags) &&
3296 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3297 /* Pre-reads at not permitted until after short delay
3298 * to gather multiple requests. However if this
3299 * device is no Insync, the block could only be be computed
3300 * and there is no need to delay that.
3301 */
3302 return 0;
NeilBrownea664c82015-02-02 14:03:28 +11003303
NeilBrown36707bb2015-09-24 15:25:36 +10003304 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrownea664c82015-02-02 14:03:28 +11003305 if (fdev[i]->towrite &&
3306 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3307 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3308 /* If we have a partial write to a failed
3309 * device, then we will need to reconstruct
3310 * the content of that device, so all other
3311 * devices must be read.
3312 */
3313 return 1;
3314 }
3315
3316 /* If we are forced to do a reconstruct-write, either because
3317 * the current RAID6 implementation only supports that, or
3318 * or because parity cannot be trusted and we are currently
3319 * recovering it, there is extra need to be careful.
3320 * If one of the devices that we would need to read, because
3321 * it is not being overwritten (and maybe not written at all)
3322 * is missing/faulty, then we need to read everything we can.
3323 */
3324 if (sh->raid_conf->level != 6 &&
3325 sh->sector < sh->raid_conf->mddev->recovery_cp)
3326 /* reconstruct-write isn't being forced */
3327 return 0;
NeilBrown36707bb2015-09-24 15:25:36 +10003328 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrown10d82c52015-05-08 18:19:33 +10003329 if (s->failed_num[i] != sh->pd_idx &&
3330 s->failed_num[i] != sh->qd_idx &&
3331 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
NeilBrownea664c82015-02-02 14:03:28 +11003332 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3333 return 1;
3334 }
3335
NeilBrown2c58f062015-02-02 11:32:23 +11003336 return 0;
3337}
3338
3339static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3340 int disk_idx, int disks)
3341{
3342 struct r5dev *dev = &sh->dev[disk_idx];
3343
3344 /* is the data in this block needed, and can we get it? */
3345 if (need_this_block(sh, s, disk_idx, disks)) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003346 /* we would like to get this block, possibly by computing it,
3347 * otherwise read it if the backing disk is insync
3348 */
3349 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3350 BUG_ON(test_bit(R5_Wantread, &dev->flags));
NeilBrownb0c783b2015-05-08 18:19:32 +10003351 BUG_ON(sh->batch_head);
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003352 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10003353 (s->failed && (disk_idx == s->failed_num[0] ||
3354 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003355 /* have disk failed, and we're requested to fetch it;
3356 * do compute it
3357 */
3358 pr_debug("Computing stripe %llu block %d\n",
3359 (unsigned long long)sh->sector, disk_idx);
3360 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3361 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3362 set_bit(R5_Wantcompute, &dev->flags);
3363 sh->ops.target = disk_idx;
3364 sh->ops.target2 = -1; /* no 2nd target */
3365 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10003366 /* Careful: from this point on 'uptodate' is in the eye
3367 * of raid_run_ops which services 'compute' operations
3368 * before writes. R5_Wantcompute flags a block that will
3369 * be R5_UPTODATE by the time it is needed for a
3370 * subsequent operation.
3371 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003372 s->uptodate++;
3373 return 1;
3374 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3375 /* Computing 2-failure is *very* expensive; only
3376 * do it if failed >= 2
3377 */
3378 int other;
3379 for (other = disks; other--; ) {
3380 if (other == disk_idx)
3381 continue;
3382 if (!test_bit(R5_UPTODATE,
3383 &sh->dev[other].flags))
3384 break;
3385 }
3386 BUG_ON(other < 0);
3387 pr_debug("Computing stripe %llu blocks %d,%d\n",
3388 (unsigned long long)sh->sector,
3389 disk_idx, other);
3390 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3391 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3392 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3393 set_bit(R5_Wantcompute, &sh->dev[other].flags);
3394 sh->ops.target = disk_idx;
3395 sh->ops.target2 = other;
3396 s->uptodate += 2;
3397 s->req_compute = 1;
3398 return 1;
3399 } else if (test_bit(R5_Insync, &dev->flags)) {
3400 set_bit(R5_LOCKED, &dev->flags);
3401 set_bit(R5_Wantread, &dev->flags);
3402 s->locked++;
3403 pr_debug("Reading block %d (sync=%d)\n",
3404 disk_idx, s->syncing);
3405 }
3406 }
3407
3408 return 0;
3409}
3410
3411/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10003412 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003413 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10003414static void handle_stripe_fill(struct stripe_head *sh,
3415 struct stripe_head_state *s,
3416 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003417{
3418 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003419
3420 /* look for blocks to read/compute, skip this if a compute
3421 * is already in flight, or if the stripe contents are in the
3422 * midst of changing due to a write
3423 */
3424 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
3425 !sh->reconstruct_state)
3426 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10003427 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003428 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003429 set_bit(STRIPE_HANDLE, &sh->state);
3430}
3431
NeilBrown787b76f2015-05-21 12:56:41 +10003432static void break_stripe_batch_list(struct stripe_head *head_sh,
3433 unsigned long handle_flags);
Dan Williams1fe797e2008-06-28 09:16:30 +10003434/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07003435 * any written block on an uptodate or failed drive can be returned.
3436 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3437 * never LOCKED, so we don't need to test 'failed' directly.
3438 */
NeilBrownd1688a62011-10-11 16:49:52 +11003439static void handle_stripe_clean_event(struct r5conf *conf,
NeilBrown34a6f802015-08-14 12:07:57 +10003440 struct stripe_head *sh, int disks, struct bio_list *return_bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003441{
3442 int i;
3443 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003444 int discard_pending = 0;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003445 struct stripe_head *head_sh = sh;
3446 bool do_endio = false;
Dan Williamsa4456852007-07-09 11:56:43 -07003447
3448 for (i = disks; i--; )
3449 if (sh->dev[i].written) {
3450 dev = &sh->dev[i];
3451 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003452 (test_bit(R5_UPTODATE, &dev->flags) ||
Shaohua Lid592a992014-05-21 17:57:44 +08003453 test_bit(R5_Discard, &dev->flags) ||
3454 test_bit(R5_SkipCopy, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003455 /* We can return any write requests */
3456 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003457 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003458 if (test_and_clear_bit(R5_Discard, &dev->flags))
3459 clear_bit(R5_UPTODATE, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08003460 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3461 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
Shaohua Lid592a992014-05-21 17:57:44 +08003462 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003463 do_endio = true;
3464
3465returnbi:
3466 dev->page = dev->orig_page;
Dan Williamsa4456852007-07-09 11:56:43 -07003467 wbi = dev->written;
3468 dev->written = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003469 while (wbi && wbi->bi_iter.bi_sector <
Dan Williamsa4456852007-07-09 11:56:43 -07003470 dev->sector + STRIPE_SECTORS) {
3471 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10003472 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003473 md_write_end(conf->mddev);
NeilBrown34a6f802015-08-14 12:07:57 +10003474 bio_list_add(return_bi, wbi);
Dan Williamsa4456852007-07-09 11:56:43 -07003475 }
3476 wbi = wbi2;
3477 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003478 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
3479 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07003480 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003481 0);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003482 if (head_sh->batch_head) {
3483 sh = list_first_entry(&sh->batch_list,
3484 struct stripe_head,
3485 batch_list);
3486 if (sh != head_sh) {
3487 dev = &sh->dev[i];
3488 goto returnbi;
3489 }
3490 }
3491 sh = head_sh;
3492 dev = &sh->dev[i];
NeilBrownf8dfcff2013-03-12 12:18:06 +11003493 } else if (test_bit(R5_Discard, &dev->flags))
3494 discard_pending = 1;
Shaohua Lid592a992014-05-21 17:57:44 +08003495 WARN_ON(test_bit(R5_SkipCopy, &dev->flags));
3496 WARN_ON(dev->page != dev->orig_page);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003497 }
3498 if (!discard_pending &&
3499 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
3500 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3501 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3502 if (sh->qd_idx >= 0) {
3503 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3504 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3505 }
3506 /* now that discard is done we can proceed with any sync */
3507 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08003508 /*
3509 * SCSI discard will change some bio fields and the stripe has
3510 * no updated data, so remove it from hash list and the stripe
3511 * will be reinitialized
3512 */
3513 spin_lock_irq(&conf->device_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003514unhash:
Shaohua Lid47648f2013-10-19 14:51:42 +08003515 remove_hash(sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003516 if (head_sh->batch_head) {
3517 sh = list_first_entry(&sh->batch_list,
3518 struct stripe_head, batch_list);
3519 if (sh != head_sh)
3520 goto unhash;
3521 }
Shaohua Lid47648f2013-10-19 14:51:42 +08003522 spin_unlock_irq(&conf->device_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003523 sh = head_sh;
3524
NeilBrownf8dfcff2013-03-12 12:18:06 +11003525 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3526 set_bit(STRIPE_HANDLE, &sh->state);
3527
3528 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003529
3530 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3531 if (atomic_dec_and_test(&conf->pending_full_writes))
3532 md_wakeup_thread(conf->mddev->thread);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003533
NeilBrown787b76f2015-05-21 12:56:41 +10003534 if (head_sh->batch_head && do_endio)
3535 break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
Dan Williamsa4456852007-07-09 11:56:43 -07003536}
3537
NeilBrownd1688a62011-10-11 16:49:52 +11003538static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10003539 struct stripe_head *sh,
3540 struct stripe_head_state *s,
3541 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003542{
3543 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003544 sector_t recovery_cp = conf->mddev->recovery_cp;
3545
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003546 /* Check whether resync is now happening or should start.
Alexander Lyakasa7854482012-10-11 13:50:12 +11003547 * If yes, then the array is dirty (after unclean shutdown or
3548 * initial creation), so parity in some stripes might be inconsistent.
3549 * In this case, we need to always do reconstruct-write, to ensure
3550 * that in case of drive failure or read-error correction, we
3551 * generate correct data from the parity.
3552 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003553 if (conf->rmw_level == PARITY_DISABLE_RMW ||
NeilBrown26ac1072015-02-18 11:35:14 +11003554 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
3555 s->failed == 0)) {
Alexander Lyakasa7854482012-10-11 13:50:12 +11003556 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10003557 * look like rcw is cheaper
3558 */
3559 rcw = 1; rmw = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003560 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
3561 conf->rmw_level, (unsigned long long)recovery_cp,
Alexander Lyakasa7854482012-10-11 13:50:12 +11003562 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10003563 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003564 /* would I have to read this buffer for read_modify_write */
3565 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003566 if ((dev->towrite || i == sh->pd_idx || i == sh->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003567 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003568 !(test_bit(R5_UPTODATE, &dev->flags) ||
3569 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003570 if (test_bit(R5_Insync, &dev->flags))
3571 rmw++;
3572 else
3573 rmw += 2*disks; /* cannot read it */
3574 }
3575 /* Would I have to read this buffer for reconstruct_write */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003576 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3577 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003578 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003579 !(test_bit(R5_UPTODATE, &dev->flags) ||
3580 test_bit(R5_Wantcompute, &dev->flags))) {
NeilBrown67f45542014-05-28 13:39:22 +10003581 if (test_bit(R5_Insync, &dev->flags))
3582 rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07003583 else
3584 rcw += 2*disks;
3585 }
3586 }
Dan Williams45b42332007-07-09 11:56:43 -07003587 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07003588 (unsigned long long)sh->sector, rmw, rcw);
3589 set_bit(STRIPE_HANDLE, &sh->state);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003590 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_ENABLE_RMW)) && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003591 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003592 if (conf->mddev->queue)
3593 blk_add_trace_msg(conf->mddev->queue,
3594 "raid5 rmw %llu %d",
3595 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07003596 for (i = disks; i--; ) {
3597 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003598 if ((dev->towrite || i == sh->pd_idx || i == sh->qd_idx) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003599 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003600 !(test_bit(R5_UPTODATE, &dev->flags) ||
3601 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003602 test_bit(R5_Insync, &dev->flags)) {
NeilBrown67f45542014-05-28 13:39:22 +10003603 if (test_bit(STRIPE_PREREAD_ACTIVE,
3604 &sh->state)) {
3605 pr_debug("Read_old block %d for r-m-w\n",
3606 i);
Dan Williamsa4456852007-07-09 11:56:43 -07003607 set_bit(R5_LOCKED, &dev->flags);
3608 set_bit(R5_Wantread, &dev->flags);
3609 s->locked++;
3610 } else {
3611 set_bit(STRIPE_DELAYED, &sh->state);
3612 set_bit(STRIPE_HANDLE, &sh->state);
3613 }
3614 }
3615 }
NeilBrowna9add5d2012-10-31 11:59:09 +11003616 }
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003617 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_ENABLE_RMW)) && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003618 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11003619 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10003620 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003621 for (i = disks; i--; ) {
3622 struct r5dev *dev = &sh->dev[i];
3623 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10003624 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003625 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003626 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10003627 test_bit(R5_Wantcompute, &dev->flags))) {
3628 rcw++;
NeilBrown67f45542014-05-28 13:39:22 +10003629 if (test_bit(R5_Insync, &dev->flags) &&
3630 test_bit(STRIPE_PREREAD_ACTIVE,
3631 &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07003632 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07003633 "%d for Reconstruct\n", i);
3634 set_bit(R5_LOCKED, &dev->flags);
3635 set_bit(R5_Wantread, &dev->flags);
3636 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11003637 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07003638 } else {
3639 set_bit(STRIPE_DELAYED, &sh->state);
3640 set_bit(STRIPE_HANDLE, &sh->state);
3641 }
3642 }
3643 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003644 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11003645 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
3646 (unsigned long long)sh->sector,
3647 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10003648 }
NeilBrownb1b02fe2015-02-02 10:44:29 +11003649
3650 if (rcw > disks && rmw > disks &&
3651 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3652 set_bit(STRIPE_DELAYED, &sh->state);
3653
Dan Williamsa4456852007-07-09 11:56:43 -07003654 /* now if nothing is locked, and if we have enough data,
3655 * we can start a write request
3656 */
Dan Williamsf38e1212007-01-02 13:52:30 -07003657 /* since handle_stripe can be called at any time we need to handle the
3658 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07003659 * subsequent call wants to start a write request. raid_run_ops only
3660 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07003661 * simultaneously. If this is not the case then new writes need to be
3662 * held off until the compute completes.
3663 */
Dan Williams976ea8d2008-06-28 08:32:03 +10003664 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
3665 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
3666 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003667 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07003668}
3669
NeilBrownd1688a62011-10-11 16:49:52 +11003670static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07003671 struct stripe_head_state *s, int disks)
3672{
Dan Williamsecc65c92008-06-28 08:31:57 +10003673 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07003674
shli@kernel.org59fc6302014-12-15 12:57:03 +11003675 BUG_ON(sh->batch_head);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003676 set_bit(STRIPE_HANDLE, &sh->state);
3677
Dan Williamsecc65c92008-06-28 08:31:57 +10003678 switch (sh->check_state) {
3679 case check_state_idle:
3680 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07003681 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07003682 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10003683 sh->check_state = check_state_run;
3684 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003685 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07003686 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10003687 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07003688 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003689 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10003690 /* fall through */
3691 case check_state_compute_result:
3692 sh->check_state = check_state_idle;
3693 if (!dev)
3694 dev = &sh->dev[sh->pd_idx];
3695
3696 /* check that a write has not made the stripe insync */
3697 if (test_bit(STRIPE_INSYNC, &sh->state))
3698 break;
3699
3700 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07003701 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
3702 BUG_ON(s->uptodate != disks);
3703
3704 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10003705 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003706 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003707
Dan Williamsa4456852007-07-09 11:56:43 -07003708 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003709 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003710 break;
3711 case check_state_run:
3712 break; /* we will be called again upon completion */
3713 case check_state_check_result:
3714 sh->check_state = check_state_idle;
3715
3716 /* if a failure occurred during the check operation, leave
3717 * STRIPE_INSYNC not set and let the stripe be handled again
3718 */
3719 if (s->failed)
3720 break;
3721
3722 /* handle a successful check operation, if parity is correct
3723 * we are done. Otherwise update the mismatch count and repair
3724 * parity if !MD_RECOVERY_CHECK
3725 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003726 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003727 /* parity is correct (on disc,
3728 * not in buffer any more)
3729 */
3730 set_bit(STRIPE_INSYNC, &sh->state);
3731 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003732 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003733 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3734 /* don't try to repair!! */
3735 set_bit(STRIPE_INSYNC, &sh->state);
3736 else {
3737 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003738 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003739 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3740 set_bit(R5_Wantcompute,
3741 &sh->dev[sh->pd_idx].flags);
3742 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003743 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003744 s->uptodate++;
3745 }
3746 }
3747 break;
3748 case check_state_compute_run:
3749 break;
3750 default:
3751 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3752 __func__, sh->check_state,
3753 (unsigned long long) sh->sector);
3754 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003755 }
3756}
3757
NeilBrownd1688a62011-10-11 16:49:52 +11003758static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003759 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003760 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003761{
Dan Williamsa4456852007-07-09 11:56:43 -07003762 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003763 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003764 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003765
shli@kernel.org59fc6302014-12-15 12:57:03 +11003766 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003767 set_bit(STRIPE_HANDLE, &sh->state);
3768
3769 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003770
Dan Williamsa4456852007-07-09 11:56:43 -07003771 /* Want to check and possibly repair P and Q.
3772 * However there could be one 'failed' device, in which
3773 * case we can only check one of them, possibly using the
3774 * other to generate missing data
3775 */
3776
Dan Williamsd82dfee2009-07-14 13:40:57 -07003777 switch (sh->check_state) {
3778 case check_state_idle:
3779 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003780 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003781 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003782 * makes sense to check P (If anything else were failed,
3783 * we would have used P to recreate it).
3784 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003785 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003786 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003787 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003788 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003789 * anything, so it makes sense to check it
3790 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003791 if (sh->check_state == check_state_run)
3792 sh->check_state = check_state_run_pq;
3793 else
3794 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003795 }
Dan Williams36d1c642009-07-14 11:48:22 -07003796
Dan Williamsd82dfee2009-07-14 13:40:57 -07003797 /* discard potentially stale zero_sum_result */
3798 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003799
Dan Williamsd82dfee2009-07-14 13:40:57 -07003800 if (sh->check_state == check_state_run) {
3801 /* async_xor_zero_sum destroys the contents of P */
3802 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3803 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003804 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003805 if (sh->check_state >= check_state_run &&
3806 sh->check_state <= check_state_run_pq) {
3807 /* async_syndrome_zero_sum preserves P and Q, so
3808 * no need to mark them !uptodate here
3809 */
3810 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3811 break;
3812 }
Dan Williams36d1c642009-07-14 11:48:22 -07003813
Dan Williamsd82dfee2009-07-14 13:40:57 -07003814 /* we have 2-disk failure */
3815 BUG_ON(s->failed != 2);
3816 /* fall through */
3817 case check_state_compute_result:
3818 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003819
Dan Williamsd82dfee2009-07-14 13:40:57 -07003820 /* check that a write has not made the stripe insync */
3821 if (test_bit(STRIPE_INSYNC, &sh->state))
3822 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003823
3824 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003825 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003826 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003827 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003828 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003829 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003830 s->locked++;
3831 set_bit(R5_LOCKED, &dev->flags);
3832 set_bit(R5_Wantwrite, &dev->flags);
3833 }
3834 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003835 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003836 s->locked++;
3837 set_bit(R5_LOCKED, &dev->flags);
3838 set_bit(R5_Wantwrite, &dev->flags);
3839 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003840 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003841 dev = &sh->dev[pd_idx];
3842 s->locked++;
3843 set_bit(R5_LOCKED, &dev->flags);
3844 set_bit(R5_Wantwrite, &dev->flags);
3845 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003846 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003847 dev = &sh->dev[qd_idx];
3848 s->locked++;
3849 set_bit(R5_LOCKED, &dev->flags);
3850 set_bit(R5_Wantwrite, &dev->flags);
3851 }
3852 clear_bit(STRIPE_DEGRADED, &sh->state);
3853
3854 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003855 break;
3856 case check_state_run:
3857 case check_state_run_q:
3858 case check_state_run_pq:
3859 break; /* we will be called again upon completion */
3860 case check_state_check_result:
3861 sh->check_state = check_state_idle;
3862
3863 /* handle a successful check operation, if parity is correct
3864 * we are done. Otherwise update the mismatch count and repair
3865 * parity if !MD_RECOVERY_CHECK
3866 */
3867 if (sh->ops.zero_sum_result == 0) {
3868 /* both parities are correct */
3869 if (!s->failed)
3870 set_bit(STRIPE_INSYNC, &sh->state);
3871 else {
3872 /* in contrast to the raid5 case we can validate
3873 * parity, but still have a failure to write
3874 * back
3875 */
3876 sh->check_state = check_state_compute_result;
3877 /* Returning at this point means that we may go
3878 * off and bring p and/or q uptodate again so
3879 * we make sure to check zero_sum_result again
3880 * to verify if p or q need writeback
3881 */
3882 }
3883 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003884 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003885 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3886 /* don't try to repair!! */
3887 set_bit(STRIPE_INSYNC, &sh->state);
3888 else {
3889 int *target = &sh->ops.target;
3890
3891 sh->ops.target = -1;
3892 sh->ops.target2 = -1;
3893 sh->check_state = check_state_compute_run;
3894 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3895 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3896 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3897 set_bit(R5_Wantcompute,
3898 &sh->dev[pd_idx].flags);
3899 *target = pd_idx;
3900 target = &sh->ops.target2;
3901 s->uptodate++;
3902 }
3903 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3904 set_bit(R5_Wantcompute,
3905 &sh->dev[qd_idx].flags);
3906 *target = qd_idx;
3907 s->uptodate++;
3908 }
3909 }
3910 }
3911 break;
3912 case check_state_compute_run:
3913 break;
3914 default:
3915 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3916 __func__, sh->check_state,
3917 (unsigned long long) sh->sector);
3918 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003919 }
3920}
3921
NeilBrownd1688a62011-10-11 16:49:52 +11003922static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003923{
3924 int i;
3925
3926 /* We have read all the blocks in this stripe and now we need to
3927 * copy some of them into a target stripe for expand.
3928 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003929 struct dma_async_tx_descriptor *tx = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003930 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003931 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3932 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003933 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003934 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003935 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003936 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003937
Shaohua Li6d036f72015-08-13 14:31:57 -07003938 sector_t bn = raid5_compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003939 sector_t s = raid5_compute_sector(conf, bn, 0,
3940 &dd_idx, NULL);
Shaohua Li6d036f72015-08-13 14:31:57 -07003941 sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003942 if (sh2 == NULL)
3943 /* so far only the early blocks of this stripe
3944 * have been requested. When later blocks
3945 * get requested, we will try again
3946 */
3947 continue;
3948 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3949 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3950 /* must have already done this block */
Shaohua Li6d036f72015-08-13 14:31:57 -07003951 raid5_release_stripe(sh2);
Dan Williamsa4456852007-07-09 11:56:43 -07003952 continue;
3953 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003954
3955 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003956 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003957 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003958 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003959 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003960
Dan Williamsa4456852007-07-09 11:56:43 -07003961 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3962 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3963 for (j = 0; j < conf->raid_disks; j++)
3964 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003965 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003966 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3967 break;
3968 if (j == conf->raid_disks) {
3969 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3970 set_bit(STRIPE_HANDLE, &sh2->state);
3971 }
Shaohua Li6d036f72015-08-13 14:31:57 -07003972 raid5_release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003973
Dan Williamsa4456852007-07-09 11:56:43 -07003974 }
NeilBrowna2e08552007-09-11 15:23:36 -07003975 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003976 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003977}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
3979/*
3980 * handle_stripe - do things to a stripe.
3981 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003982 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3983 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003985 * return some read requests which now have data
3986 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 * schedule a read on some buffers
3988 * schedule a write of some buffers
3989 * return confirmation of parity correctness
3990 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 */
Dan Williamsa4456852007-07-09 11:56:43 -07003992
NeilBrownacfe7262011-07-27 11:00:36 +10003993static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003994{
NeilBrownd1688a62011-10-11 16:49:52 +11003995 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003996 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003997 struct r5dev *dev;
3998 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003999 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07004000
NeilBrownacfe7262011-07-27 11:00:36 +10004001 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07004002
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004003 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
4004 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
NeilBrownacfe7262011-07-27 11:00:36 +10004005 s->failed_num[0] = -1;
4006 s->failed_num[1] = -1;
4007
4008 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07004009 rcu_read_lock();
4010 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004011 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10004012 sector_t first_bad;
4013 int bad_sectors;
4014 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10004015
NeilBrown16a53ec2006-06-26 00:27:38 -07004016 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07004017
Dan Williams45b42332007-07-09 11:56:43 -07004018 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11004019 i, dev->flags,
4020 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004021 /* maybe we can reply to a read
4022 *
4023 * new wantfill requests are only permitted while
4024 * ops_complete_biofill is guaranteed to be inactive
4025 */
4026 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4027 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4028 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07004029
4030 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10004031 if (test_bit(R5_LOCKED, &dev->flags))
4032 s->locked++;
4033 if (test_bit(R5_UPTODATE, &dev->flags))
4034 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004035 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004036 s->compute++;
4037 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004038 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004039
NeilBrownacfe7262011-07-27 11:00:36 +10004040 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004041 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10004042 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10004043 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004044 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10004045 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004046 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004047 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004048 }
Dan Williamsa4456852007-07-09 11:56:43 -07004049 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10004050 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11004051 /* Prefer to use the replacement for reads, but only
4052 * if it is recovered enough and has no bad blocks.
4053 */
4054 rdev = rcu_dereference(conf->disks[i].replacement);
4055 if (rdev && !test_bit(Faulty, &rdev->flags) &&
4056 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
4057 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4058 &first_bad, &bad_sectors))
4059 set_bit(R5_ReadRepl, &dev->flags);
4060 else {
NeilBrowne6030cb2015-07-17 13:26:23 +10004061 if (rdev && !test_bit(Faulty, &rdev->flags))
NeilBrown9a3e1102011-12-23 10:17:53 +11004062 set_bit(R5_NeedReplace, &dev->flags);
NeilBrowne6030cb2015-07-17 13:26:23 +10004063 else
4064 clear_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11004065 rdev = rcu_dereference(conf->disks[i].rdev);
4066 clear_bit(R5_ReadRepl, &dev->flags);
4067 }
NeilBrown9283d8c2011-12-08 16:27:57 +11004068 if (rdev && test_bit(Faulty, &rdev->flags))
4069 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10004070 if (rdev) {
4071 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
4072 &first_bad, &bad_sectors);
4073 if (s->blocked_rdev == NULL
4074 && (test_bit(Blocked, &rdev->flags)
4075 || is_bad < 0)) {
4076 if (is_bad < 0)
4077 set_bit(BlockedBadBlocks,
4078 &rdev->flags);
4079 s->blocked_rdev = rdev;
4080 atomic_inc(&rdev->nr_pending);
4081 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004082 }
NeilBrown415e72d2010-06-17 17:25:21 +10004083 clear_bit(R5_Insync, &dev->flags);
4084 if (!rdev)
4085 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10004086 else if (is_bad) {
4087 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10004088 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4089 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10004090 /* treat as in-sync, but with a read error
4091 * which we can now try to correct
4092 */
4093 set_bit(R5_Insync, &dev->flags);
4094 set_bit(R5_ReadError, &dev->flags);
4095 }
4096 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10004097 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11004098 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10004099 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11004100 set_bit(R5_Insync, &dev->flags);
4101 else if (test_bit(R5_UPTODATE, &dev->flags) &&
4102 test_bit(R5_Expanded, &dev->flags))
4103 /* If we've reshaped into here, we assume it is Insync.
4104 * We will shortly update recovery_offset to make
4105 * it official.
4106 */
4107 set_bit(R5_Insync, &dev->flags);
4108
NeilBrown1cc03eb2014-01-06 13:19:42 +11004109 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004110 /* This flag does not apply to '.replacement'
4111 * only to .rdev, so make sure to check that*/
4112 struct md_rdev *rdev2 = rcu_dereference(
4113 conf->disks[i].rdev);
4114 if (rdev2 == rdev)
4115 clear_bit(R5_Insync, &dev->flags);
4116 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004117 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004118 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10004119 } else
4120 clear_bit(R5_WriteError, &dev->flags);
4121 }
NeilBrown1cc03eb2014-01-06 13:19:42 +11004122 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004123 /* This flag does not apply to '.replacement'
4124 * only to .rdev, so make sure to check that*/
4125 struct md_rdev *rdev2 = rcu_dereference(
4126 conf->disks[i].rdev);
4127 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10004128 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004129 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10004130 } else
4131 clear_bit(R5_MadeGood, &dev->flags);
4132 }
NeilBrown977df362011-12-23 10:17:53 +11004133 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4134 struct md_rdev *rdev2 = rcu_dereference(
4135 conf->disks[i].replacement);
4136 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4137 s->handle_bad_blocks = 1;
4138 atomic_inc(&rdev2->nr_pending);
4139 } else
4140 clear_bit(R5_MadeGoodRepl, &dev->flags);
4141 }
NeilBrown415e72d2010-06-17 17:25:21 +10004142 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004143 /* The ReadError flag will just be confusing now */
4144 clear_bit(R5_ReadError, &dev->flags);
4145 clear_bit(R5_ReWrite, &dev->flags);
4146 }
NeilBrown415e72d2010-06-17 17:25:21 +10004147 if (test_bit(R5_ReadError, &dev->flags))
4148 clear_bit(R5_Insync, &dev->flags);
4149 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004150 if (s->failed < 2)
4151 s->failed_num[s->failed] = i;
4152 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11004153 if (rdev && !test_bit(Faulty, &rdev->flags))
4154 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10004155 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004156 }
NeilBrown9a3e1102011-12-23 10:17:53 +11004157 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4158 /* If there is a failed device being replaced,
4159 * we must be recovering.
4160 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10004161 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11004162 * else we can only be replacing
4163 * sync and recovery both need to read all devices, and so
4164 * use the same flag.
4165 */
4166 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10004167 sh->sector >= conf->mddev->recovery_cp ||
4168 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11004169 s->syncing = 1;
4170 else
4171 s->replacing = 1;
4172 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004173 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10004174}
NeilBrownf4168852007-02-28 20:11:53 -08004175
shli@kernel.org59fc6302014-12-15 12:57:03 +11004176static int clear_batch_ready(struct stripe_head *sh)
4177{
NeilBrownb15a9db2015-05-22 15:20:04 +10004178 /* Return '1' if this is a member of batch, or
4179 * '0' if it is a lone stripe or a head which can now be
4180 * handled.
4181 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11004182 struct stripe_head *tmp;
4183 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
NeilBrownb15a9db2015-05-22 15:20:04 +10004184 return (sh->batch_head && sh->batch_head != sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11004185 spin_lock(&sh->stripe_lock);
4186 if (!sh->batch_head) {
4187 spin_unlock(&sh->stripe_lock);
4188 return 0;
4189 }
4190
4191 /*
4192 * this stripe could be added to a batch list before we check
4193 * BATCH_READY, skips it
4194 */
4195 if (sh->batch_head != sh) {
4196 spin_unlock(&sh->stripe_lock);
4197 return 1;
4198 }
4199 spin_lock(&sh->batch_lock);
4200 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4201 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4202 spin_unlock(&sh->batch_lock);
4203 spin_unlock(&sh->stripe_lock);
4204
4205 /*
4206 * BATCH_READY is cleared, no new stripes can be added.
4207 * batch_list can be accessed without lock
4208 */
4209 return 0;
4210}
4211
NeilBrown3960ce72015-05-21 12:20:36 +10004212static void break_stripe_batch_list(struct stripe_head *head_sh,
4213 unsigned long handle_flags)
shli@kernel.org72ac7332014-12-15 12:57:03 +11004214{
NeilBrown4e3d62f2015-05-21 11:50:16 +10004215 struct stripe_head *sh, *next;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004216 int i;
NeilBrownfb642b92015-05-21 12:00:47 +10004217 int do_wakeup = 0;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004218
NeilBrownbb270512015-05-08 18:19:40 +10004219 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4220
shli@kernel.org72ac7332014-12-15 12:57:03 +11004221 list_del_init(&sh->batch_list);
4222
NeilBrown1b956f72015-05-21 12:40:26 +10004223 WARN_ON_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
4224 (1 << STRIPE_SYNCING) |
4225 (1 << STRIPE_REPLACED) |
4226 (1 << STRIPE_PREREAD_ACTIVE) |
4227 (1 << STRIPE_DELAYED) |
4228 (1 << STRIPE_BIT_DELAY) |
4229 (1 << STRIPE_FULL_WRITE) |
4230 (1 << STRIPE_BIOFILL_RUN) |
4231 (1 << STRIPE_COMPUTE_RUN) |
4232 (1 << STRIPE_OPS_REQ_PENDING) |
4233 (1 << STRIPE_DISCARD) |
4234 (1 << STRIPE_BATCH_READY) |
4235 (1 << STRIPE_BATCH_ERR) |
4236 (1 << STRIPE_BITMAP_PENDING)));
4237 WARN_ON_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4238 (1 << STRIPE_REPLACED)));
4239
4240 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
4241 (1 << STRIPE_DEGRADED)),
4242 head_sh->state & (1 << STRIPE_INSYNC));
4243
shli@kernel.org72ac7332014-12-15 12:57:03 +11004244 sh->check_state = head_sh->check_state;
4245 sh->reconstruct_state = head_sh->reconstruct_state;
NeilBrownfb642b92015-05-21 12:00:47 +10004246 for (i = 0; i < sh->disks; i++) {
4247 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4248 do_wakeup = 1;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004249 sh->dev[i].flags = head_sh->dev[i].flags &
4250 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
NeilBrownfb642b92015-05-21 12:00:47 +10004251 }
shli@kernel.org72ac7332014-12-15 12:57:03 +11004252 spin_lock_irq(&sh->stripe_lock);
4253 sh->batch_head = NULL;
4254 spin_unlock_irq(&sh->stripe_lock);
NeilBrown3960ce72015-05-21 12:20:36 +10004255 if (handle_flags == 0 ||
4256 sh->state & handle_flags)
4257 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07004258 raid5_release_stripe(sh);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004259 }
NeilBrownfb642b92015-05-21 12:00:47 +10004260 spin_lock_irq(&head_sh->stripe_lock);
4261 head_sh->batch_head = NULL;
4262 spin_unlock_irq(&head_sh->stripe_lock);
4263 for (i = 0; i < head_sh->disks; i++)
4264 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4265 do_wakeup = 1;
NeilBrown3960ce72015-05-21 12:20:36 +10004266 if (head_sh->state & handle_flags)
4267 set_bit(STRIPE_HANDLE, &head_sh->state);
NeilBrownfb642b92015-05-21 12:00:47 +10004268
4269 if (do_wakeup)
4270 wake_up(&head_sh->raid_conf->wait_for_overlap);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004271}
4272
NeilBrowncc940152011-07-26 11:35:35 +10004273static void handle_stripe(struct stripe_head *sh)
4274{
4275 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11004276 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10004277 int i;
NeilBrown84789552011-07-27 11:00:36 +10004278 int prexor;
4279 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004280 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10004281
4282 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11004283 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10004284 /* already being handled, ensure it gets handled
4285 * again when current action finishes */
4286 set_bit(STRIPE_HANDLE, &sh->state);
4287 return;
4288 }
4289
shli@kernel.org59fc6302014-12-15 12:57:03 +11004290 if (clear_batch_ready(sh) ) {
4291 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
4292 return;
4293 }
4294
NeilBrown4e3d62f2015-05-21 11:50:16 +10004295 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
NeilBrown3960ce72015-05-21 12:20:36 +10004296 break_stripe_batch_list(sh, 0);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004297
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004298 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
NeilBrownf8dfcff2013-03-12 12:18:06 +11004299 spin_lock(&sh->stripe_lock);
4300 /* Cannot process 'sync' concurrently with 'discard' */
4301 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
4302 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4303 set_bit(STRIPE_SYNCING, &sh->state);
4304 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10004305 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004306 }
4307 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10004308 }
4309 clear_bit(STRIPE_DELAYED, &sh->state);
4310
4311 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4312 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4313 (unsigned long long)sh->sector, sh->state,
4314 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4315 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10004316
NeilBrownacfe7262011-07-27 11:00:36 +10004317 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10004318
NeilBrownbc2607f2011-07-28 11:39:22 +10004319 if (s.handle_bad_blocks) {
4320 set_bit(STRIPE_HANDLE, &sh->state);
4321 goto finish;
4322 }
4323
NeilBrown474af965fe2011-07-27 11:00:36 +10004324 if (unlikely(s.blocked_rdev)) {
4325 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11004326 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10004327 set_bit(STRIPE_HANDLE, &sh->state);
4328 goto finish;
4329 }
4330 /* There is nothing for the blocked_rdev to block */
4331 rdev_dec_pending(s.blocked_rdev, conf->mddev);
4332 s.blocked_rdev = NULL;
4333 }
4334
4335 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4336 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4337 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4338 }
4339
4340 pr_debug("locked=%d uptodate=%d to_read=%d"
4341 " to_write=%d failed=%d failed_num=%d,%d\n",
4342 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4343 s.failed_num[0], s.failed_num[1]);
4344 /* check if the array has lost more than max_degraded devices and,
4345 * if so, some requests might need to be failed.
4346 */
NeilBrown9a3f5302011-11-08 16:22:01 +11004347 if (s.failed > conf->max_degraded) {
4348 sh->check_state = 0;
4349 sh->reconstruct_state = 0;
NeilBrown626f2092015-05-22 14:03:10 +10004350 break_stripe_batch_list(sh, 0);
NeilBrown9a3f5302011-11-08 16:22:01 +11004351 if (s.to_read+s.to_write+s.written)
4352 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11004353 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11004354 handle_failed_sync(conf, sh, &s);
4355 }
NeilBrown474af965fe2011-07-27 11:00:36 +10004356
NeilBrown84789552011-07-27 11:00:36 +10004357 /* Now we check to see if any write operations have recently
4358 * completed
4359 */
4360 prexor = 0;
4361 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4362 prexor = 1;
4363 if (sh->reconstruct_state == reconstruct_state_drain_result ||
4364 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
4365 sh->reconstruct_state = reconstruct_state_idle;
4366
4367 /* All the 'written' buffers and the parity block are ready to
4368 * be written back to disk
4369 */
Shaohua Li9e4447682012-10-11 13:49:49 +11004370 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
4371 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004372 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11004373 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
4374 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004375 for (i = disks; i--; ) {
4376 struct r5dev *dev = &sh->dev[i];
4377 if (test_bit(R5_LOCKED, &dev->flags) &&
4378 (i == sh->pd_idx || i == sh->qd_idx ||
4379 dev->written)) {
4380 pr_debug("Writing block %d\n", i);
4381 set_bit(R5_Wantwrite, &dev->flags);
4382 if (prexor)
4383 continue;
NeilBrown9c4bdf62014-08-13 09:57:07 +10004384 if (s.failed > 1)
4385 continue;
NeilBrown84789552011-07-27 11:00:36 +10004386 if (!test_bit(R5_Insync, &dev->flags) ||
4387 ((i == sh->pd_idx || i == sh->qd_idx) &&
4388 s.failed == 0))
4389 set_bit(STRIPE_INSYNC, &sh->state);
4390 }
4391 }
4392 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4393 s.dec_preread_active = 1;
4394 }
4395
NeilBrownef5b7c62012-11-22 09:13:36 +11004396 /*
4397 * might be able to return some write requests if the parity blocks
4398 * are safe, or on a failed drive
4399 */
4400 pdev = &sh->dev[sh->pd_idx];
4401 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
4402 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
4403 qdev = &sh->dev[sh->qd_idx];
4404 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
4405 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
4406 || conf->level < 6;
4407
4408 if (s.written &&
4409 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
4410 && !test_bit(R5_LOCKED, &pdev->flags)
4411 && (test_bit(R5_UPTODATE, &pdev->flags) ||
4412 test_bit(R5_Discard, &pdev->flags))))) &&
4413 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
4414 && !test_bit(R5_LOCKED, &qdev->flags)
4415 && (test_bit(R5_UPTODATE, &qdev->flags) ||
4416 test_bit(R5_Discard, &qdev->flags))))))
4417 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
4418
4419 /* Now we might consider reading some blocks, either to check/generate
4420 * parity, or to satisfy requests
4421 * or to load a block that is being partially written.
4422 */
4423 if (s.to_read || s.non_overwrite
4424 || (conf->level == 6 && s.to_write && s.failed)
4425 || (s.syncing && (s.uptodate + s.compute < disks))
4426 || s.replacing
4427 || s.expanding)
4428 handle_stripe_fill(sh, &s, disks);
4429
NeilBrown84789552011-07-27 11:00:36 +10004430 /* Now to consider new write requests and what else, if anything
4431 * should be read. We do not handle new writes when:
4432 * 1/ A 'write' operation (copy+xor) is already in flight.
4433 * 2/ A 'check' operation is in flight, as it may clobber the parity
4434 * block.
4435 */
4436 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
4437 handle_stripe_dirtying(conf, sh, &s, disks);
4438
4439 /* maybe we need to check and possibly fix the parity for this stripe
4440 * Any reads will already have been scheduled, so we just see if enough
4441 * data is available. The parity check is held off while parity
4442 * dependent operations are in flight.
4443 */
4444 if (sh->check_state ||
4445 (s.syncing && s.locked == 0 &&
4446 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
4447 !test_bit(STRIPE_INSYNC, &sh->state))) {
4448 if (conf->level == 6)
4449 handle_parity_checks6(conf, sh, &s, disks);
4450 else
4451 handle_parity_checks5(conf, sh, &s, disks);
4452 }
NeilBrownc5a31002011-07-27 11:00:36 +10004453
NeilBrownf94c0b62013-07-22 12:57:21 +10004454 if ((s.replacing || s.syncing) && s.locked == 0
4455 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
4456 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11004457 /* Write out to replacement devices where possible */
4458 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10004459 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
4460 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11004461 set_bit(R5_WantReplace, &sh->dev[i].flags);
4462 set_bit(R5_LOCKED, &sh->dev[i].flags);
4463 s.locked++;
4464 }
NeilBrownf94c0b62013-07-22 12:57:21 +10004465 if (s.replacing)
4466 set_bit(STRIPE_INSYNC, &sh->state);
4467 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11004468 }
4469 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10004470 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11004471 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10004472 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4473 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004474 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
4475 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10004476 }
4477
4478 /* If the failed drives are just a ReadError, then we might need
4479 * to progress the repair/check process
4480 */
4481 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
4482 for (i = 0; i < s.failed; i++) {
4483 struct r5dev *dev = &sh->dev[s.failed_num[i]];
4484 if (test_bit(R5_ReadError, &dev->flags)
4485 && !test_bit(R5_LOCKED, &dev->flags)
4486 && test_bit(R5_UPTODATE, &dev->flags)
4487 ) {
4488 if (!test_bit(R5_ReWrite, &dev->flags)) {
4489 set_bit(R5_Wantwrite, &dev->flags);
4490 set_bit(R5_ReWrite, &dev->flags);
4491 set_bit(R5_LOCKED, &dev->flags);
4492 s.locked++;
4493 } else {
4494 /* let's read it back */
4495 set_bit(R5_Wantread, &dev->flags);
4496 set_bit(R5_LOCKED, &dev->flags);
4497 s.locked++;
4498 }
4499 }
4500 }
4501
NeilBrown3687c062011-07-27 11:00:36 +10004502 /* Finish reconstruct operations initiated by the expansion process */
4503 if (sh->reconstruct_state == reconstruct_state_result) {
4504 struct stripe_head *sh_src
Shaohua Li6d036f72015-08-13 14:31:57 -07004505 = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrown3687c062011-07-27 11:00:36 +10004506 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
4507 /* sh cannot be written until sh_src has been read.
4508 * so arrange for sh to be delayed a little
4509 */
4510 set_bit(STRIPE_DELAYED, &sh->state);
4511 set_bit(STRIPE_HANDLE, &sh->state);
4512 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
4513 &sh_src->state))
4514 atomic_inc(&conf->preread_active_stripes);
Shaohua Li6d036f72015-08-13 14:31:57 -07004515 raid5_release_stripe(sh_src);
NeilBrown3687c062011-07-27 11:00:36 +10004516 goto finish;
4517 }
4518 if (sh_src)
Shaohua Li6d036f72015-08-13 14:31:57 -07004519 raid5_release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07004520
NeilBrown3687c062011-07-27 11:00:36 +10004521 sh->reconstruct_state = reconstruct_state_idle;
4522 clear_bit(STRIPE_EXPANDING, &sh->state);
4523 for (i = conf->raid_disks; i--; ) {
4524 set_bit(R5_Wantwrite, &sh->dev[i].flags);
4525 set_bit(R5_LOCKED, &sh->dev[i].flags);
4526 s.locked++;
4527 }
4528 }
4529
4530 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
4531 !sh->reconstruct_state) {
4532 /* Need to write out all blocks after computing parity */
4533 sh->disks = conf->raid_disks;
4534 stripe_set_idx(sh->sector, conf, 0, sh);
4535 schedule_reconstruction(sh, &s, 1, 1);
4536 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
4537 clear_bit(STRIPE_EXPAND_READY, &sh->state);
4538 atomic_dec(&conf->reshape_stripes);
4539 wake_up(&conf->wait_for_overlap);
4540 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
4541 }
4542
4543 if (s.expanding && s.locked == 0 &&
4544 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
4545 handle_stripe_expansion(conf, sh);
4546
4547finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07004548 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10004549 if (unlikely(s.blocked_rdev)) {
4550 if (conf->mddev->external)
4551 md_wait_for_blocked_rdev(s.blocked_rdev,
4552 conf->mddev);
4553 else
4554 /* Internal metadata will immediately
4555 * be written by raid5d, so we don't
4556 * need to wait here.
4557 */
4558 rdev_dec_pending(s.blocked_rdev,
4559 conf->mddev);
4560 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004561
NeilBrownbc2607f2011-07-28 11:39:22 +10004562 if (s.handle_bad_blocks)
4563 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004564 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10004565 struct r5dev *dev = &sh->dev[i];
4566 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
4567 /* We own a safe reference to the rdev */
4568 rdev = conf->disks[i].rdev;
4569 if (!rdev_set_badblocks(rdev, sh->sector,
4570 STRIPE_SECTORS, 0))
4571 md_error(conf->mddev, rdev);
4572 rdev_dec_pending(rdev, conf->mddev);
4573 }
NeilBrownb84db562011-07-28 11:39:23 +10004574 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
4575 rdev = conf->disks[i].rdev;
4576 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004577 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10004578 rdev_dec_pending(rdev, conf->mddev);
4579 }
NeilBrown977df362011-12-23 10:17:53 +11004580 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
4581 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11004582 if (!rdev)
4583 /* rdev have been moved down */
4584 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11004585 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10004586 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11004587 rdev_dec_pending(rdev, conf->mddev);
4588 }
NeilBrownbc2607f2011-07-28 11:39:22 +10004589 }
4590
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004591 if (s.ops_request)
4592 raid_run_ops(sh, s.ops_request);
4593
Dan Williamsf0e43bc2008-06-28 08:31:55 +10004594 ops_run_io(sh, &s);
4595
NeilBrownc5709ef2011-07-26 11:35:20 +10004596 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11004597 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02004598 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11004599 * have actually been submitted.
4600 */
4601 atomic_dec(&conf->preread_active_stripes);
4602 if (atomic_read(&conf->preread_active_stripes) <
4603 IO_THRESHOLD)
4604 md_wakeup_thread(conf->mddev->thread);
4605 }
4606
NeilBrownc3cce6c2015-08-14 12:47:33 +10004607 if (!bio_list_empty(&s.return_bi)) {
4608 if (test_bit(MD_CHANGE_PENDING, &conf->mddev->flags)) {
4609 spin_lock_irq(&conf->device_lock);
4610 bio_list_merge(&conf->return_bi, &s.return_bi);
4611 spin_unlock_irq(&conf->device_lock);
4612 md_wakeup_thread(conf->mddev->thread);
4613 } else
4614 return_io(&s.return_bi);
4615 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004616
Dan Williams257a4b42011-11-08 16:22:06 +11004617 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07004618}
4619
NeilBrownd1688a62011-10-11 16:49:52 +11004620static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621{
4622 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
4623 while (!list_empty(&conf->delayed_list)) {
4624 struct list_head *l = conf->delayed_list.next;
4625 struct stripe_head *sh;
4626 sh = list_entry(l, struct stripe_head, lru);
4627 list_del_init(l);
4628 clear_bit(STRIPE_DELAYED, &sh->state);
4629 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4630 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004631 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08004632 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 }
NeilBrown482c0832011-04-18 18:25:42 +10004634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635}
4636
Shaohua Li566c09c2013-11-14 15:16:17 +11004637static void activate_bit_delay(struct r5conf *conf,
4638 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07004639{
4640 /* device_lock is held */
4641 struct list_head head;
4642 list_add(&head, &conf->bitmap_list);
4643 list_del_init(&conf->bitmap_list);
4644 while (!list_empty(&head)) {
4645 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11004646 int hash;
NeilBrown72626682005-09-09 16:23:54 -07004647 list_del_init(&sh->lru);
4648 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11004649 hash = sh->hash_lock_index;
4650 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07004651 }
4652}
4653
NeilBrown5c675f82014-12-15 12:56:56 +11004654static int raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07004655{
NeilBrownd1688a62011-10-11 16:49:52 +11004656 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07004657
4658 /* No difference between reads and writes. Just check
4659 * how busy the stripe_cache is
4660 */
NeilBrown3fa841d2009-09-23 18:10:29 +10004661
NeilBrown54233992015-02-26 12:21:04 +11004662 if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state))
NeilBrownf022b2f2006-10-03 01:15:56 -07004663 return 1;
4664 if (conf->quiesce)
4665 return 1;
Shaohua Li4bda5562013-11-14 15:16:17 +11004666 if (atomic_read(&conf->empty_inactive_list_nr))
NeilBrownf022b2f2006-10-03 01:15:56 -07004667 return 1;
4668
4669 return 0;
4670}
4671
NeilBrownfd01b882011-10-11 16:47:53 +11004672static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004673{
NeilBrown3cb5edf2015-07-15 17:24:17 +10004674 struct r5conf *conf = mddev->private;
Kent Overstreet4f024f32013-10-11 15:44:27 -07004675 sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev);
NeilBrown3cb5edf2015-07-15 17:24:17 +10004676 unsigned int chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004677 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004678
NeilBrown3cb5edf2015-07-15 17:24:17 +10004679 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004680 return chunk_sectors >=
4681 ((sector & (chunk_sectors - 1)) + bio_sectors);
4682}
4683
4684/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004685 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
4686 * later sampled by raid5d.
4687 */
NeilBrownd1688a62011-10-11 16:49:52 +11004688static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004689{
4690 unsigned long flags;
4691
4692 spin_lock_irqsave(&conf->device_lock, flags);
4693
4694 bi->bi_next = conf->retry_read_aligned_list;
4695 conf->retry_read_aligned_list = bi;
4696
4697 spin_unlock_irqrestore(&conf->device_lock, flags);
4698 md_wakeup_thread(conf->mddev->thread);
4699}
4700
NeilBrownd1688a62011-10-11 16:49:52 +11004701static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004702{
4703 struct bio *bi;
4704
4705 bi = conf->retry_read_aligned;
4706 if (bi) {
4707 conf->retry_read_aligned = NULL;
4708 return bi;
4709 }
4710 bi = conf->retry_read_aligned_list;
4711 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08004712 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004713 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02004714 /*
4715 * this sets the active strip count to 1 and the processed
4716 * strip count to zero (upper 8 bits)
4717 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004718 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004719 }
4720
4721 return bi;
4722}
4723
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004724/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004725 * The "raid5_align_endio" should check if the read succeeded and if it
4726 * did, call bio_endio on the original bio (having bio_put the new bio
4727 * first).
4728 * If the read failed..
4729 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004730static void raid5_align_endio(struct bio *bi)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004731{
4732 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11004733 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004734 struct r5conf *conf;
NeilBrown3cb03002011-10-11 16:45:26 +11004735 struct md_rdev *rdev;
Sasha Levin9b81c842015-08-10 19:05:18 -04004736 int error = bi->bi_error;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004737
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004738 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004739
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004740 rdev = (void*)raid_bi->bi_next;
4741 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11004742 mddev = rdev->mddev;
4743 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004744
4745 rdev_dec_pending(rdev, conf->mddev);
4746
Sasha Levin9b81c842015-08-10 19:05:18 -04004747 if (!error) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004748 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
4749 raid_bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02004750 bio_endio(raid_bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004751 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10004752 wake_up(&conf->wait_for_quiescent);
NeilBrown6712ecf2007-09-27 12:47:43 +02004753 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004754 }
4755
Dan Williams45b42332007-07-09 11:56:43 -07004756 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004757
4758 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004759}
4760
Ming Lin7ef6b122015-05-06 22:51:24 -07004761static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004762{
NeilBrownd1688a62011-10-11 16:49:52 +11004763 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11004764 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004765 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11004766 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11004767 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004768
4769 if (!in_chunk_boundary(mddev, raid_bio)) {
Ming Lin7ef6b122015-05-06 22:51:24 -07004770 pr_debug("%s: non aligned\n", __func__);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004771 return 0;
4772 }
4773 /*
NeilBrowna167f662010-10-26 18:31:13 +11004774 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004775 */
NeilBrowna167f662010-10-26 18:31:13 +11004776 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004777 if (!align_bi)
4778 return 0;
4779 /*
4780 * set bi_end_io to a new function, and set bi_private to the
4781 * original bio.
4782 */
4783 align_bi->bi_end_io = raid5_align_endio;
4784 align_bi->bi_private = raid_bio;
4785 /*
4786 * compute position
4787 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07004788 align_bi->bi_iter.bi_sector =
4789 raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
4790 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004791
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004792 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004793 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11004794 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4795 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4796 rdev->recovery_offset < end_sector) {
4797 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4798 if (rdev &&
4799 (test_bit(Faulty, &rdev->flags) ||
4800 !(test_bit(In_sync, &rdev->flags) ||
4801 rdev->recovery_offset >= end_sector)))
4802 rdev = NULL;
4803 }
4804 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10004805 sector_t first_bad;
4806 int bad_sectors;
4807
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004808 atomic_inc(&rdev->nr_pending);
4809 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004810 raid_bio->bi_next = (void*)rdev;
4811 align_bi->bi_bdev = rdev->bdev;
Jens Axboeb7c44ed2015-07-24 12:37:59 -06004812 bio_clear_flag(align_bi, BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004813
Kent Overstreet7140aaf2013-09-25 13:37:01 -07004814 if (is_badblock(rdev, align_bi->bi_iter.bi_sector,
Kent Overstreet4f024f32013-10-11 15:44:27 -07004815 bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10004816 &first_bad, &bad_sectors)) {
Neil Brown387bb172007-02-08 14:20:29 -08004817 bio_put(align_bi);
4818 rdev_dec_pending(rdev, mddev);
4819 return 0;
4820 }
4821
majianpeng6c0544e2012-06-12 08:31:10 +08004822 /* No reshape active, so we can trust rdev->data_offset */
Kent Overstreet4f024f32013-10-11 15:44:27 -07004823 align_bi->bi_iter.bi_sector += rdev->data_offset;
majianpeng6c0544e2012-06-12 08:31:10 +08004824
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004825 spin_lock_irq(&conf->device_lock);
Yuanhan Liub1b46482015-05-08 18:19:06 +10004826 wait_event_lock_irq(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004827 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004828 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004829 atomic_inc(&conf->active_aligned_reads);
4830 spin_unlock_irq(&conf->device_lock);
4831
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004832 if (mddev->gendisk)
4833 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4834 align_bi, disk_devt(mddev->gendisk),
Kent Overstreet4f024f32013-10-11 15:44:27 -07004835 raid_bio->bi_iter.bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004836 generic_make_request(align_bi);
4837 return 1;
4838 } else {
4839 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004840 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004841 return 0;
4842 }
4843}
4844
Ming Lin7ef6b122015-05-06 22:51:24 -07004845static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
4846{
4847 struct bio *split;
4848
4849 do {
4850 sector_t sector = raid_bio->bi_iter.bi_sector;
4851 unsigned chunk_sects = mddev->chunk_sectors;
4852 unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
4853
4854 if (sectors < bio_sectors(raid_bio)) {
4855 split = bio_split(raid_bio, sectors, GFP_NOIO, fs_bio_set);
4856 bio_chain(split, raid_bio);
4857 } else
4858 split = raid_bio;
4859
4860 if (!raid5_read_one_chunk(mddev, split)) {
4861 if (split != raid_bio)
4862 generic_make_request(raid_bio);
4863 return split;
4864 }
4865 } while (split != raid_bio);
4866
4867 return NULL;
4868}
4869
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004870/* __get_priority_stripe - get the next stripe to process
4871 *
4872 * Full stripe writes are allowed to pass preread active stripes up until
4873 * the bypass_threshold is exceeded. In general the bypass_count
4874 * increments when the handle_list is handled before the hold_list; however, it
4875 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4876 * stripe with in flight i/o. The bypass_count will be reset when the
4877 * head of the hold_list has changed, i.e. the head was promoted to the
4878 * handle_list.
4879 */
Shaohua Li851c30c2013-08-28 14:30:16 +08004880static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004881{
Shaohua Li851c30c2013-08-28 14:30:16 +08004882 struct stripe_head *sh = NULL, *tmp;
4883 struct list_head *handle_list = NULL;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004884 struct r5worker_group *wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08004885
4886 if (conf->worker_cnt_per_group == 0) {
4887 handle_list = &conf->handle_list;
4888 } else if (group != ANY_GROUP) {
4889 handle_list = &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004890 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08004891 } else {
4892 int i;
4893 for (i = 0; i < conf->group_cnt; i++) {
4894 handle_list = &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08004895 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08004896 if (!list_empty(handle_list))
4897 break;
4898 }
4899 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004900
4901 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4902 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08004903 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004904 list_empty(&conf->hold_list) ? "empty" : "busy",
4905 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4906
Shaohua Li851c30c2013-08-28 14:30:16 +08004907 if (!list_empty(handle_list)) {
4908 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004909
4910 if (list_empty(&conf->hold_list))
4911 conf->bypass_count = 0;
4912 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4913 if (conf->hold_list.next == conf->last_hold)
4914 conf->bypass_count++;
4915 else {
4916 conf->last_hold = conf->hold_list.next;
4917 conf->bypass_count -= conf->bypass_threshold;
4918 if (conf->bypass_count < 0)
4919 conf->bypass_count = 0;
4920 }
4921 }
4922 } else if (!list_empty(&conf->hold_list) &&
4923 ((conf->bypass_threshold &&
4924 conf->bypass_count > conf->bypass_threshold) ||
4925 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08004926
4927 list_for_each_entry(tmp, &conf->hold_list, lru) {
4928 if (conf->worker_cnt_per_group == 0 ||
4929 group == ANY_GROUP ||
4930 !cpu_online(tmp->cpu) ||
4931 cpu_to_group(tmp->cpu) == group) {
4932 sh = tmp;
4933 break;
4934 }
4935 }
4936
4937 if (sh) {
4938 conf->bypass_count -= conf->bypass_threshold;
4939 if (conf->bypass_count < 0)
4940 conf->bypass_count = 0;
4941 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08004942 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08004943 }
4944
4945 if (!sh)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004946 return NULL;
4947
Shaohua Libfc90cb2013-08-29 15:40:32 +08004948 if (wg) {
4949 wg->stripes_cnt--;
4950 sh->group = NULL;
4951 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004952 list_del_init(&sh->lru);
Shaohua Lic7a6d352014-04-15 09:12:54 +08004953 BUG_ON(atomic_inc_return(&sh->count) != 1);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004954 return sh;
4955}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004956
Shaohua Li8811b592012-08-02 08:33:00 +10004957struct raid5_plug_cb {
4958 struct blk_plug_cb cb;
4959 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11004960 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10004961};
4962
4963static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4964{
4965 struct raid5_plug_cb *cb = container_of(
4966 blk_cb, struct raid5_plug_cb, cb);
4967 struct stripe_head *sh;
4968 struct mddev *mddev = cb->cb.data;
4969 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004970 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11004971 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10004972
4973 if (cb->list.next && !list_empty(&cb->list)) {
4974 spin_lock_irq(&conf->device_lock);
4975 while (!list_empty(&cb->list)) {
4976 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4977 list_del_init(&sh->lru);
4978 /*
4979 * avoid race release_stripe_plug() sees
4980 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4981 * is still in our list
4982 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01004983 smp_mb__before_atomic();
Shaohua Li8811b592012-08-02 08:33:00 +10004984 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08004985 /*
4986 * STRIPE_ON_RELEASE_LIST could be set here. In that
4987 * case, the count is always > 1 here
4988 */
Shaohua Li566c09c2013-11-14 15:16:17 +11004989 hash = sh->hash_lock_index;
4990 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11004991 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004992 }
4993 spin_unlock_irq(&conf->device_lock);
4994 }
Shaohua Li566c09c2013-11-14 15:16:17 +11004995 release_inactive_stripe_list(conf, cb->temp_inactive_list,
4996 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004997 if (mddev->queue)
4998 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004999 kfree(cb);
5000}
5001
5002static void release_stripe_plug(struct mddev *mddev,
5003 struct stripe_head *sh)
5004{
5005 struct blk_plug_cb *blk_cb = blk_check_plugged(
5006 raid5_unplug, mddev,
5007 sizeof(struct raid5_plug_cb));
5008 struct raid5_plug_cb *cb;
5009
5010 if (!blk_cb) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005011 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005012 return;
5013 }
5014
5015 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
5016
Shaohua Li566c09c2013-11-14 15:16:17 +11005017 if (cb->list.next == NULL) {
5018 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10005019 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11005020 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5021 INIT_LIST_HEAD(cb->temp_inactive_list + i);
5022 }
Shaohua Li8811b592012-08-02 08:33:00 +10005023
5024 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
5025 list_add_tail(&sh->lru, &cb->list);
5026 else
Shaohua Li6d036f72015-08-13 14:31:57 -07005027 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005028}
5029
Shaohua Li620125f2012-10-11 13:49:05 +11005030static void make_discard_request(struct mddev *mddev, struct bio *bi)
5031{
5032 struct r5conf *conf = mddev->private;
5033 sector_t logical_sector, last_sector;
5034 struct stripe_head *sh;
5035 int remaining;
5036 int stripe_sectors;
5037
5038 if (mddev->reshape_position != MaxSector)
5039 /* Skip discard while reshape is happening */
5040 return;
5041
Kent Overstreet4f024f32013-10-11 15:44:27 -07005042 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
5043 last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9);
Shaohua Li620125f2012-10-11 13:49:05 +11005044
5045 bi->bi_next = NULL;
5046 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
5047
5048 stripe_sectors = conf->chunk_sectors *
5049 (conf->raid_disks - conf->max_degraded);
5050 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5051 stripe_sectors);
5052 sector_div(last_sector, stripe_sectors);
5053
5054 logical_sector *= conf->chunk_sectors;
5055 last_sector *= conf->chunk_sectors;
5056
5057 for (; logical_sector < last_sector;
5058 logical_sector += STRIPE_SECTORS) {
5059 DEFINE_WAIT(w);
5060 int d;
5061 again:
Shaohua Li6d036f72015-08-13 14:31:57 -07005062 sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
Shaohua Li620125f2012-10-11 13:49:05 +11005063 prepare_to_wait(&conf->wait_for_overlap, &w,
5064 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005065 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5066 if (test_bit(STRIPE_SYNCING, &sh->state)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005067 raid5_release_stripe(sh);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005068 schedule();
5069 goto again;
5070 }
5071 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11005072 spin_lock_irq(&sh->stripe_lock);
5073 for (d = 0; d < conf->raid_disks; d++) {
5074 if (d == sh->pd_idx || d == sh->qd_idx)
5075 continue;
5076 if (sh->dev[d].towrite || sh->dev[d].toread) {
5077 set_bit(R5_Overlap, &sh->dev[d].flags);
5078 spin_unlock_irq(&sh->stripe_lock);
Shaohua Li6d036f72015-08-13 14:31:57 -07005079 raid5_release_stripe(sh);
Shaohua Li620125f2012-10-11 13:49:05 +11005080 schedule();
5081 goto again;
5082 }
5083 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11005084 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11005085 finish_wait(&conf->wait_for_overlap, &w);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005086 sh->overwrite_disks = 0;
Shaohua Li620125f2012-10-11 13:49:05 +11005087 for (d = 0; d < conf->raid_disks; d++) {
5088 if (d == sh->pd_idx || d == sh->qd_idx)
5089 continue;
5090 sh->dev[d].towrite = bi;
5091 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
5092 raid5_inc_bi_active_stripes(bi);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005093 sh->overwrite_disks++;
Shaohua Li620125f2012-10-11 13:49:05 +11005094 }
5095 spin_unlock_irq(&sh->stripe_lock);
5096 if (conf->mddev->bitmap) {
5097 for (d = 0;
5098 d < conf->raid_disks - conf->max_degraded;
5099 d++)
5100 bitmap_startwrite(mddev->bitmap,
5101 sh->sector,
5102 STRIPE_SECTORS,
5103 0);
5104 sh->bm_seq = conf->seq_flush + 1;
5105 set_bit(STRIPE_BIT_DELAY, &sh->state);
5106 }
5107
5108 set_bit(STRIPE_HANDLE, &sh->state);
5109 clear_bit(STRIPE_DELAYED, &sh->state);
5110 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5111 atomic_inc(&conf->preread_active_stripes);
5112 release_stripe_plug(mddev, sh);
5113 }
5114
5115 remaining = raid5_dec_bi_active_stripes(bi);
5116 if (remaining == 0) {
5117 md_write_end(mddev);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005118 bio_endio(bi);
Shaohua Li620125f2012-10-11 13:49:05 +11005119 }
5120}
5121
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07005122static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123{
NeilBrownd1688a62011-10-11 16:49:52 +11005124 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11005125 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 sector_t new_sector;
5127 sector_t logical_sector, last_sector;
5128 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01005129 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11005130 int remaining;
Shaohua Li27c0f682014-04-09 11:25:47 +08005131 DEFINE_WAIT(w);
5132 bool do_prepare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133
Tejun Heoe9c74692010-09-03 11:56:18 +02005134 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
5135 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02005136 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07005137 }
5138
NeilBrown3d310eb2005-06-21 17:17:26 -07005139 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07005140
Eric Mei9ffc8f72015-03-18 23:39:11 -06005141 /*
5142 * If array is degraded, better not do chunk aligned read because
5143 * later we might have to read it again in order to reconstruct
5144 * data on failed drives.
5145 */
5146 if (rw == READ && mddev->degraded == 0 &&
Ming Lin7ef6b122015-05-06 22:51:24 -07005147 mddev->reshape_position == MaxSector) {
5148 bi = chunk_aligned_read(mddev, bi);
5149 if (!bi)
5150 return;
5151 }
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08005152
Shaohua Li620125f2012-10-11 13:49:05 +11005153 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
5154 make_discard_request(mddev, bi);
5155 return;
5156 }
5157
Kent Overstreet4f024f32013-10-11 15:44:27 -07005158 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005159 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 bi->bi_next = NULL;
5161 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07005162
Shaohua Li27c0f682014-04-09 11:25:47 +08005163 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005165 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10005166 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08005167
Shaohua Li27c0f682014-04-09 11:25:47 +08005168 do_prepare = false;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005169 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10005170 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11005171 previous = 0;
Shaohua Li27c0f682014-04-09 11:25:47 +08005172 if (do_prepare)
5173 prepare_to_wait(&conf->wait_for_overlap, &w,
5174 TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005175 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11005176 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08005177 * 64bit on a 32bit platform, and so it might be
5178 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02005179 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08005180 * the lock is dropped, so once we get a reference
5181 * to the stripe that we think it is, we will have
5182 * to check again.
5183 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005184 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005185 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005186 ? logical_sector < conf->reshape_progress
5187 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005188 previous = 1;
5189 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10005190 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005191 ? logical_sector < conf->reshape_safe
5192 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08005193 spin_unlock_irq(&conf->device_lock);
5194 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005195 do_prepare = true;
NeilBrownb578d552006-03-27 01:18:12 -08005196 goto retry;
5197 }
5198 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005199 spin_unlock_irq(&conf->device_lock);
5200 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005201
NeilBrown112bf892009-03-31 14:39:38 +11005202 new_sector = raid5_compute_sector(conf, logical_sector,
5203 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11005204 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10005205 pr_debug("raid456: make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10005206 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 (unsigned long long)logical_sector);
5208
Shaohua Li6d036f72015-08-13 14:31:57 -07005209 sh = raid5_get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10005210 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11005212 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005213 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08005214 * stripe, so we must do the range check again.
5215 * Expansion could still move past after this
5216 * test, but as we are holding a reference to
5217 * 'sh', we know that if that happens,
5218 * STRIPE_EXPANDING will get set and the expansion
5219 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005220 */
5221 int must_retry = 0;
5222 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005223 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11005224 ? logical_sector >= conf->reshape_progress
5225 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005226 /* mismatch, need to try again */
5227 must_retry = 1;
5228 spin_unlock_irq(&conf->device_lock);
5229 if (must_retry) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005230 raid5_release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07005231 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005232 do_prepare = true;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005233 goto retry;
5234 }
5235 }
NeilBrownc46501b2013-08-27 15:52:13 +10005236 if (read_seqcount_retry(&conf->gen_lock, seq)) {
5237 /* Might have got the wrong stripe_head
5238 * by accident
5239 */
Shaohua Li6d036f72015-08-13 14:31:57 -07005240 raid5_release_stripe(sh);
NeilBrownc46501b2013-08-27 15:52:13 +10005241 goto retry;
5242 }
NeilBrowne62e58a2009-07-01 13:15:35 +10005243
Namhyung Kimffd96e32011-07-18 17:38:51 +10005244 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10005245 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08005246 logical_sector < mddev->suspend_hi) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005247 raid5_release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10005248 /* As the suspend_* range is controlled by
5249 * userspace, we want an interruptible
5250 * wait.
5251 */
5252 flush_signals(current);
5253 prepare_to_wait(&conf->wait_for_overlap,
5254 &w, TASK_INTERRUPTIBLE);
5255 if (logical_sector >= mddev->suspend_lo &&
Shaohua Li27c0f682014-04-09 11:25:47 +08005256 logical_sector < mddev->suspend_hi) {
NeilBrowne62e58a2009-07-01 13:15:35 +10005257 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005258 do_prepare = true;
5259 }
NeilBrowne464eaf2006-03-27 01:18:14 -08005260 goto retry;
5261 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005262
5263 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
shli@kernel.orgda41ba62014-12-15 12:57:03 +11005264 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005265 /* Stripe is busy expanding or
5266 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 * and wait a while
5268 */
NeilBrown482c0832011-04-18 18:25:42 +10005269 md_wakeup_thread(mddev->thread);
Shaohua Li6d036f72015-08-13 14:31:57 -07005270 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005272 do_prepare = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 goto retry;
5274 }
NeilBrown6ed30032008-02-06 01:40:00 -08005275 set_bit(STRIPE_HANDLE, &sh->state);
5276 clear_bit(STRIPE_DELAYED, &sh->state);
shli@kernel.org59fc6302014-12-15 12:57:03 +11005277 if ((!sh->batch_head || sh == sh->batch_head) &&
5278 (bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11005279 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5280 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10005281 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 } else {
5283 /* cannot get stripe for read-ahead, just give-up */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005284 bi->bi_error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 break;
5286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 }
Shaohua Li27c0f682014-04-09 11:25:47 +08005288 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown7c13edc2011-04-18 18:25:43 +10005289
Shaohua Lie7836bd62012-07-19 16:01:31 +10005290 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08005291 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
NeilBrown16a53ec2006-06-26 00:27:38 -07005293 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02005295
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005296 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
5297 bi, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005298 bio_endio(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300}
5301
NeilBrownfd01b882011-10-11 16:47:53 +11005302static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11005303
NeilBrownfd01b882011-10-11 16:47:53 +11005304static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305{
NeilBrown52c03292006-06-26 00:27:43 -07005306 /* reshaping is quite different to recovery/resync so it is
5307 * handled quite separately ... here.
5308 *
5309 * On each call to sync_request, we gather one chunk worth of
5310 * destination stripes and flag them as expanding.
5311 * Then we find all the source stripes and request reads.
5312 * As the reads complete, handle_stripe will copy the data
5313 * into the destination stripe and release that stripe.
5314 */
NeilBrownd1688a62011-10-11 16:49:52 +11005315 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005317 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08005318 int raid_disks = conf->previous_raid_disks;
5319 int data_disks = raid_disks - conf->max_degraded;
5320 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07005321 int i;
5322 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11005323 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11005324 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11005325 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11005326 struct list_head stripes;
NeilBrown92140482015-07-06 12:28:45 +10005327 sector_t retn;
NeilBrown52c03292006-06-26 00:27:43 -07005328
NeilBrownfef9c612009-03-31 15:16:46 +11005329 if (sector_nr == 0) {
5330 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10005331 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005332 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
5333 sector_nr = raid5_size(mddev, 0, 0)
5334 - conf->reshape_progress;
NeilBrown6cbd8142015-07-24 13:30:32 +10005335 } else if (mddev->reshape_backwards &&
5336 conf->reshape_progress == MaxSector) {
5337 /* shouldn't happen, but just in case, finish up.*/
5338 sector_nr = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +10005339 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005340 conf->reshape_progress > 0)
5341 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005342 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005343 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11005344 mddev->curr_resync_completed = sector_nr;
5345 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11005346 *skipped = 1;
NeilBrown92140482015-07-06 12:28:45 +10005347 retn = sector_nr;
5348 goto finish;
NeilBrownfef9c612009-03-31 15:16:46 +11005349 }
NeilBrown52c03292006-06-26 00:27:43 -07005350 }
5351
NeilBrown7a661382009-03-31 15:21:40 +11005352 /* We need to process a full chunk at a time.
5353 * If old and new chunk sizes differ, we need to process the
5354 * largest of these
5355 */
NeilBrown3cb5edf2015-07-15 17:24:17 +10005356
5357 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
NeilBrown7a661382009-03-31 15:21:40 +11005358
NeilBrownb5254dd2012-05-21 09:27:01 +10005359 /* We update the metadata at least every 10 seconds, or when
5360 * the data about to be copied would over-write the source of
5361 * the data at the front of the range. i.e. one new_stripe
5362 * along from reshape_progress new_maps to after where
5363 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07005364 */
NeilBrownfef9c612009-03-31 15:16:46 +11005365 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005366 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11005367 readpos = conf->reshape_progress;
5368 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005369 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08005370 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10005371 if (mddev->reshape_backwards) {
NeilBrownc74c0d72015-07-15 17:54:15 +10005372 BUG_ON(writepos < reshape_sectors);
5373 writepos -= reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11005374 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11005375 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11005376 } else {
NeilBrown7a661382009-03-31 15:21:40 +11005377 writepos += reshape_sectors;
NeilBrownc74c0d72015-07-15 17:54:15 +10005378 /* readpos and safepos are worst-case calculations.
5379 * A negative number is overly pessimistic, and causes
5380 * obvious problems for unsigned storage. So clip to 0.
5381 */
NeilBrowned37d832009-05-27 21:39:05 +10005382 readpos -= min_t(sector_t, reshape_sectors, readpos);
5383 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11005384 }
NeilBrown52c03292006-06-26 00:27:43 -07005385
NeilBrownb5254dd2012-05-21 09:27:01 +10005386 /* Having calculated the 'writepos' possibly use it
5387 * to set 'stripe_addr' which is where we will write to.
5388 */
5389 if (mddev->reshape_backwards) {
5390 BUG_ON(conf->reshape_progress == 0);
5391 stripe_addr = writepos;
5392 BUG_ON((mddev->dev_sectors &
5393 ~((sector_t)reshape_sectors - 1))
5394 - reshape_sectors - stripe_addr
5395 != sector_nr);
5396 } else {
5397 BUG_ON(writepos != sector_nr + reshape_sectors);
5398 stripe_addr = sector_nr;
5399 }
5400
NeilBrownc8f517c2009-03-31 15:28:40 +11005401 /* 'writepos' is the most advanced device address we might write.
5402 * 'readpos' is the least advanced device address we might read.
5403 * 'safepos' is the least address recorded in the metadata as having
5404 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10005405 * If there is a min_offset_diff, these are adjusted either by
5406 * increasing the safepos/readpos if diff is negative, or
5407 * increasing writepos if diff is positive.
5408 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11005409 * ensure safety in the face of a crash - that must be done by userspace
5410 * making a backup of the data. So in that case there is no particular
5411 * rush to update metadata.
5412 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5413 * update the metadata to advance 'safepos' to match 'readpos' so that
5414 * we can be safe in the event of a crash.
5415 * So we insist on updating metadata if safepos is behind writepos and
5416 * readpos is beyond writepos.
5417 * In any case, update the metadata every 10 seconds.
5418 * Maybe that number should be configurable, but I'm not sure it is
5419 * worth it.... maybe it could be a multiple of safemode_delay???
5420 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005421 if (conf->min_offset_diff < 0) {
5422 safepos += -conf->min_offset_diff;
5423 readpos += -conf->min_offset_diff;
5424 } else
5425 writepos += conf->min_offset_diff;
5426
NeilBrown2c810cd2012-05-21 09:27:00 +10005427 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11005428 ? (safepos > writepos && readpos < writepos)
5429 : (safepos < writepos && readpos > writepos)) ||
5430 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07005431 /* Cannot proceed until we've updated the superblock... */
5432 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005433 atomic_read(&conf->reshape_stripes)==0
5434 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5435 if (atomic_read(&conf->reshape_stripes) != 0)
5436 return 0;
NeilBrownfef9c612009-03-31 15:16:46 +11005437 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005438 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005439 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07005440 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07005441 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07005442 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11005443 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5444 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5445 return 0;
NeilBrown52c03292006-06-26 00:27:43 -07005446 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005447 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07005448 spin_unlock_irq(&conf->device_lock);
5449 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005450 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07005451 }
5452
NeilBrownab69ae12009-03-31 15:26:47 +11005453 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11005454 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07005455 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10005456 int skipped_disk = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07005457 sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005458 set_bit(STRIPE_EXPANDING, &sh->state);
5459 atomic_inc(&conf->reshape_stripes);
5460 /* If any of this stripe is beyond the end of the old
5461 * array, then we need to zero those blocks
5462 */
5463 for (j=sh->disks; j--;) {
5464 sector_t s;
5465 if (j == sh->pd_idx)
5466 continue;
NeilBrownf4168852007-02-28 20:11:53 -08005467 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11005468 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08005469 continue;
Shaohua Li6d036f72015-08-13 14:31:57 -07005470 s = raid5_compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11005471 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10005472 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07005473 continue;
5474 }
5475 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
5476 set_bit(R5_Expanded, &sh->dev[j].flags);
5477 set_bit(R5_UPTODATE, &sh->dev[j].flags);
5478 }
NeilBrowna9f326e2009-09-23 18:06:41 +10005479 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07005480 set_bit(STRIPE_EXPAND_READY, &sh->state);
5481 set_bit(STRIPE_HANDLE, &sh->state);
5482 }
NeilBrownab69ae12009-03-31 15:26:47 +11005483 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07005484 }
5485 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005486 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11005487 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005488 else
NeilBrown7a661382009-03-31 15:21:40 +11005489 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07005490 spin_unlock_irq(&conf->device_lock);
5491 /* Ok, those stripe are ready. We can start scheduling
5492 * reads on the source stripes.
5493 * The source stripes are determined by mapping the first and last
5494 * block on the destination stripes.
5495 */
NeilBrown52c03292006-06-26 00:27:43 -07005496 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11005497 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11005498 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07005499 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10005500 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10005501 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11005502 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11005503 if (last_sector >= mddev->dev_sectors)
5504 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07005505 while (first_sector <= last_sector) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005506 sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005507 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
5508 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07005509 raid5_release_stripe(sh);
NeilBrown52c03292006-06-26 00:27:43 -07005510 first_sector += STRIPE_SECTORS;
5511 }
NeilBrownab69ae12009-03-31 15:26:47 +11005512 /* Now that the sources are clearly marked, we can release
5513 * the destination stripes
5514 */
5515 while (!list_empty(&stripes)) {
5516 sh = list_entry(stripes.next, struct stripe_head, lru);
5517 list_del_init(&sh->lru);
Shaohua Li6d036f72015-08-13 14:31:57 -07005518 raid5_release_stripe(sh);
NeilBrownab69ae12009-03-31 15:26:47 +11005519 }
NeilBrownc6207272008-02-06 01:39:52 -08005520 /* If this takes us to the resync_max point where we have to pause,
5521 * then we need to write out the superblock.
5522 */
NeilBrown7a661382009-03-31 15:21:40 +11005523 sector_nr += reshape_sectors;
NeilBrown92140482015-07-06 12:28:45 +10005524 retn = reshape_sectors;
5525finish:
NeilBrownc5e19d92015-07-17 12:06:02 +10005526 if (mddev->curr_resync_completed > mddev->resync_max ||
5527 (sector_nr - mddev->curr_resync_completed) * 2
NeilBrownc03f6a12009-04-17 11:06:30 +10005528 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08005529 /* Cannot proceed until we've updated the superblock... */
5530 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005531 atomic_read(&conf->reshape_stripes) == 0
5532 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5533 if (atomic_read(&conf->reshape_stripes) != 0)
5534 goto ret;
NeilBrownfef9c612009-03-31 15:16:46 +11005535 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005536 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11005537 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08005538 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5539 md_wakeup_thread(mddev->thread);
5540 wait_event(mddev->sb_wait,
5541 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
NeilBrownc91abf52013-11-19 12:02:01 +11005542 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5543 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5544 goto ret;
NeilBrownc6207272008-02-06 01:39:52 -08005545 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005546 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08005547 spin_unlock_irq(&conf->device_lock);
5548 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10005549 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08005550 }
NeilBrownc91abf52013-11-19 12:02:01 +11005551ret:
NeilBrown92140482015-07-06 12:28:45 +10005552 return retn;
NeilBrown52c03292006-06-26 00:27:43 -07005553}
5554
NeilBrown09314792015-02-19 16:04:40 +11005555static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
NeilBrown52c03292006-06-26 00:27:43 -07005556{
NeilBrownd1688a62011-10-11 16:49:52 +11005557 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07005558 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11005559 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11005560 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07005561 int still_degraded = 0;
5562 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563
NeilBrown72626682005-09-09 16:23:54 -07005564 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11005566
NeilBrown29269552006-03-27 01:18:10 -08005567 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
5568 end_reshape(conf);
5569 return 0;
5570 }
NeilBrown72626682005-09-09 16:23:54 -07005571
5572 if (mddev->curr_resync < max_sector) /* aborted */
5573 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
5574 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07005575 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07005576 conf->fullsync = 0;
5577 bitmap_close_sync(mddev->bitmap);
5578
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 return 0;
5580 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08005581
NeilBrown64bd6602009-08-03 10:59:58 +10005582 /* Allow raid5_quiesce to complete */
5583 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
5584
NeilBrown52c03292006-06-26 00:27:43 -07005585 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5586 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08005587
NeilBrownc6207272008-02-06 01:39:52 -08005588 /* No need to check resync_max as we never do more than one
5589 * stripe, and as resync_max will always be on a chunk boundary,
5590 * if the check in md_do_sync didn't fire, there is no chance
5591 * of overstepping resync_max here
5592 */
5593
NeilBrown16a53ec2006-06-26 00:27:38 -07005594 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 * to resync, then assert that we are finished, because there is
5596 * nothing we can do.
5597 */
NeilBrown3285edf2006-06-26 00:27:55 -07005598 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005599 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005600 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07005601 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 return rv;
5603 }
majianpeng6f608042013-04-24 11:42:41 +10005604 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
5605 !conf->fullsync &&
5606 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
5607 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07005608 /* we can skip this block, and probably more */
5609 sync_blocks /= STRIPE_SECTORS;
5610 *skipped = 1;
5611 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
5612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
Goldwyn Rodriguesc40f3412015-08-19 08:14:42 +10005614 bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
NeilBrownb47490c2008-02-06 01:39:50 -08005615
Shaohua Li6d036f72015-08-13 14:31:57 -07005616 sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 if (sh == NULL) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005618 sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07005620 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08005622 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005624 /* Need to check if array will still be degraded after recovery/resync
Eric Mei16d9cfa2015-01-06 09:35:02 -08005625 * Note in case of > 1 drive failures it's possible we're rebuilding
5626 * one drive while leaving another faulty drive in array.
NeilBrown16a53ec2006-06-26 00:27:38 -07005627 */
Eric Mei16d9cfa2015-01-06 09:35:02 -08005628 rcu_read_lock();
5629 for (i = 0; i < conf->raid_disks; i++) {
5630 struct md_rdev *rdev = ACCESS_ONCE(conf->disks[i].rdev);
5631
5632 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
NeilBrown16a53ec2006-06-26 00:27:38 -07005633 still_degraded = 1;
Eric Mei16d9cfa2015-01-06 09:35:02 -08005634 }
5635 rcu_read_unlock();
NeilBrown16a53ec2006-06-26 00:27:38 -07005636
5637 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
5638
NeilBrown83206d62011-07-26 11:19:49 +10005639 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Eivind Sarto053f5b62014-06-09 17:06:19 -07005640 set_bit(STRIPE_HANDLE, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
Shaohua Li6d036f72015-08-13 14:31:57 -07005642 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643
5644 return STRIPE_SECTORS;
5645}
5646
NeilBrownd1688a62011-10-11 16:49:52 +11005647static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005648{
5649 /* We may not be able to submit a whole bio at once as there
5650 * may not be enough stripe_heads available.
5651 * We cannot pre-allocate enough stripe_heads as we may need
5652 * more than exist in the cache (if we allow ever large chunks).
5653 * So we do one stripe head at a time and record in
5654 * ->bi_hw_segments how many have been done.
5655 *
5656 * We *know* that this entire raid_bio is in one chunk, so
5657 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
5658 */
5659 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11005660 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005661 sector_t sector, logical_sector, last_sector;
5662 int scnt = 0;
5663 int remaining;
5664 int handled = 0;
5665
Kent Overstreet4f024f32013-10-11 15:44:27 -07005666 logical_sector = raid_bio->bi_iter.bi_sector &
5667 ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11005668 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11005669 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005670 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005671
5672 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08005673 logical_sector += STRIPE_SECTORS,
5674 sector += STRIPE_SECTORS,
5675 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005676
Shaohua Lie7836bd62012-07-19 16:01:31 +10005677 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005678 /* already done this stripe */
5679 continue;
5680
Shaohua Li6d036f72015-08-13 14:31:57 -07005681 sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005682
5683 if (!sh) {
5684 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10005685 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005686 conf->retry_read_aligned = raid_bio;
5687 return handled;
5688 }
5689
shli@kernel.orgda41ba62014-12-15 12:57:03 +11005690 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005691 raid5_release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10005692 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08005693 conf->retry_read_aligned = raid_bio;
5694 return handled;
5695 }
5696
majianpeng3f9e7c12012-07-31 10:04:21 +10005697 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07005698 handle_stripe(sh);
Shaohua Li6d036f72015-08-13 14:31:57 -07005699 raid5_release_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005700 handled++;
5701 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10005702 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005703 if (remaining == 0) {
5704 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
5705 raid_bio, 0);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005706 bio_endio(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07005707 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005708 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10005709 wake_up(&conf->wait_for_quiescent);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005710 return handled;
5711}
5712
Shaohua Libfc90cb2013-08-29 15:40:32 +08005713static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11005714 struct r5worker *worker,
5715 struct list_head *temp_inactive_list)
Shaohua Li46a06402012-08-02 08:33:15 +10005716{
5717 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11005718 int i, batch_size = 0, hash;
5719 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10005720
5721 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08005722 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10005723 batch[batch_size++] = sh;
5724
Shaohua Li566c09c2013-11-14 15:16:17 +11005725 if (batch_size == 0) {
5726 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5727 if (!list_empty(temp_inactive_list + i))
5728 break;
5729 if (i == NR_STRIPE_HASH_LOCKS)
5730 return batch_size;
5731 release_inactive = true;
5732 }
Shaohua Li46a06402012-08-02 08:33:15 +10005733 spin_unlock_irq(&conf->device_lock);
5734
Shaohua Li566c09c2013-11-14 15:16:17 +11005735 release_inactive_stripe_list(conf, temp_inactive_list,
5736 NR_STRIPE_HASH_LOCKS);
5737
5738 if (release_inactive) {
5739 spin_lock_irq(&conf->device_lock);
5740 return 0;
5741 }
5742
Shaohua Li46a06402012-08-02 08:33:15 +10005743 for (i = 0; i < batch_size; i++)
5744 handle_stripe(batch[i]);
5745
5746 cond_resched();
5747
5748 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11005749 for (i = 0; i < batch_size; i++) {
5750 hash = batch[i]->hash_lock_index;
5751 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
5752 }
Shaohua Li46a06402012-08-02 08:33:15 +10005753 return batch_size;
5754}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005755
Shaohua Li851c30c2013-08-28 14:30:16 +08005756static void raid5_do_work(struct work_struct *work)
5757{
5758 struct r5worker *worker = container_of(work, struct r5worker, work);
5759 struct r5worker_group *group = worker->group;
5760 struct r5conf *conf = group->conf;
5761 int group_id = group - conf->worker_groups;
5762 int handled;
5763 struct blk_plug plug;
5764
5765 pr_debug("+++ raid5worker active\n");
5766
5767 blk_start_plug(&plug);
5768 handled = 0;
5769 spin_lock_irq(&conf->device_lock);
5770 while (1) {
5771 int batch_size, released;
5772
Shaohua Li566c09c2013-11-14 15:16:17 +11005773 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08005774
Shaohua Li566c09c2013-11-14 15:16:17 +11005775 batch_size = handle_active_stripes(conf, group_id, worker,
5776 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08005777 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08005778 if (!batch_size && !released)
5779 break;
5780 handled += batch_size;
5781 }
5782 pr_debug("%d stripes handled\n", handled);
5783
5784 spin_unlock_irq(&conf->device_lock);
5785 blk_finish_plug(&plug);
5786
5787 pr_debug("--- raid5worker inactive\n");
5788}
5789
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790/*
5791 * This is our raid5 kernel thread.
5792 *
5793 * We scan the hash table for stripes which can be handled now.
5794 * During the scan, completed stripes are saved for us by the interrupt
5795 * handler, so that they will not have to wait for our next wakeup.
5796 */
Shaohua Li4ed87312012-10-11 13:34:00 +11005797static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798{
Shaohua Li4ed87312012-10-11 13:34:00 +11005799 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005800 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005802 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803
Dan Williams45b42332007-07-09 11:56:43 -07005804 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
5806 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807
NeilBrownc3cce6c2015-08-14 12:47:33 +10005808 if (!bio_list_empty(&conf->return_bi) &&
5809 !test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
5810 struct bio_list tmp = BIO_EMPTY_LIST;
5811 spin_lock_irq(&conf->device_lock);
5812 if (!test_bit(MD_CHANGE_PENDING, &mddev->flags)) {
5813 bio_list_merge(&tmp, &conf->return_bi);
5814 bio_list_init(&conf->return_bi);
5815 }
5816 spin_unlock_irq(&conf->device_lock);
5817 return_io(&tmp);
5818 }
5819
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005820 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 handled = 0;
5822 spin_lock_irq(&conf->device_lock);
5823 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005824 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08005825 int batch_size, released;
5826
Shaohua Li566c09c2013-11-14 15:16:17 +11005827 released = release_stripe_list(conf, conf->temp_inactive_list);
NeilBrownedbe83a2015-02-26 12:47:56 +11005828 if (released)
5829 clear_bit(R5_DID_ALLOC, &conf->cache_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830
NeilBrown0021b7b2012-07-31 09:08:14 +02005831 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10005832 !list_empty(&conf->bitmap_list)) {
5833 /* Now is a good time to flush some bitmap updates */
5834 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08005835 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07005836 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08005837 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10005838 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11005839 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07005840 }
NeilBrown0021b7b2012-07-31 09:08:14 +02005841 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07005842
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005843 while ((bio = remove_bio_from_retry(conf))) {
5844 int ok;
5845 spin_unlock_irq(&conf->device_lock);
5846 ok = retry_aligned_read(conf, bio);
5847 spin_lock_irq(&conf->device_lock);
5848 if (!ok)
5849 break;
5850 handled++;
5851 }
5852
Shaohua Li566c09c2013-11-14 15:16:17 +11005853 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
5854 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08005855 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 break;
Shaohua Li46a06402012-08-02 08:33:15 +10005857 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858
Shaohua Li46a06402012-08-02 08:33:15 +10005859 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
5860 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10005861 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10005862 spin_lock_irq(&conf->device_lock);
5863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 }
Dan Williams45b42332007-07-09 11:56:43 -07005865 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866
5867 spin_unlock_irq(&conf->device_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10005868 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
5869 mutex_trylock(&conf->cache_size_mutex)) {
NeilBrownedbe83a2015-02-26 12:47:56 +11005870 grow_one_stripe(conf, __GFP_NOWARN);
5871 /* Set flag even if allocation failed. This helps
5872 * slow down allocation requests when mem is short
5873 */
5874 set_bit(R5_DID_ALLOC, &conf->cache_state);
NeilBrown2d5b5692015-07-06 12:49:23 +10005875 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11005876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877
Dan Williamsc9f21aa2008-07-23 12:05:51 -07005878 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10005879 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880
Dan Williams45b42332007-07-09 11:56:43 -07005881 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882}
5883
NeilBrown3f294f42005-11-08 21:39:25 -08005884static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005885raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005886{
NeilBrown7b1485b2014-12-15 12:56:59 +11005887 struct r5conf *conf;
5888 int ret = 0;
5889 spin_lock(&mddev->lock);
5890 conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005891 if (conf)
NeilBrownedbe83a2015-02-26 12:47:56 +11005892 ret = sprintf(page, "%d\n", conf->min_nr_stripes);
NeilBrown7b1485b2014-12-15 12:56:59 +11005893 spin_unlock(&mddev->lock);
5894 return ret;
NeilBrown3f294f42005-11-08 21:39:25 -08005895}
5896
NeilBrownc41d4ac2010-06-01 19:37:24 +10005897int
NeilBrownfd01b882011-10-11 16:47:53 +11005898raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10005899{
NeilBrownd1688a62011-10-11 16:49:52 +11005900 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10005901 int err;
5902
5903 if (size <= 16 || size > 32768)
5904 return -EINVAL;
NeilBrown486f0642015-02-25 12:10:35 +11005905
NeilBrownedbe83a2015-02-26 12:47:56 +11005906 conf->min_nr_stripes = size;
NeilBrown2d5b5692015-07-06 12:49:23 +10005907 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11005908 while (size < conf->max_nr_stripes &&
5909 drop_one_stripe(conf))
5910 ;
NeilBrown2d5b5692015-07-06 12:49:23 +10005911 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11005912
NeilBrownedbe83a2015-02-26 12:47:56 +11005913
NeilBrownc41d4ac2010-06-01 19:37:24 +10005914 err = md_allow_write(mddev);
5915 if (err)
5916 return err;
NeilBrown486f0642015-02-25 12:10:35 +11005917
NeilBrown2d5b5692015-07-06 12:49:23 +10005918 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11005919 while (size > conf->max_nr_stripes)
5920 if (!grow_one_stripe(conf, GFP_KERNEL))
5921 break;
NeilBrown2d5b5692015-07-06 12:49:23 +10005922 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11005923
NeilBrownc41d4ac2010-06-01 19:37:24 +10005924 return 0;
5925}
5926EXPORT_SYMBOL(raid5_set_cache_size);
5927
NeilBrown3f294f42005-11-08 21:39:25 -08005928static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005929raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08005930{
NeilBrown67918752014-12-15 12:57:01 +11005931 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07005932 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07005933 int err;
5934
NeilBrown3f294f42005-11-08 21:39:25 -08005935 if (len >= PAGE_SIZE)
5936 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09005937 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08005938 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11005939 err = mddev_lock(mddev);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005940 if (err)
5941 return err;
NeilBrown67918752014-12-15 12:57:01 +11005942 conf = mddev->private;
5943 if (!conf)
5944 err = -ENODEV;
5945 else
5946 err = raid5_set_cache_size(mddev, new);
5947 mddev_unlock(mddev);
5948
5949 return err ?: len;
NeilBrown3f294f42005-11-08 21:39:25 -08005950}
NeilBrown007583c2005-11-08 21:39:30 -08005951
NeilBrown96de1e62005-11-08 21:39:39 -08005952static struct md_sysfs_entry
5953raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
5954 raid5_show_stripe_cache_size,
5955 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08005956
5957static ssize_t
Markus Stockhausend06f1912014-12-15 12:57:05 +11005958raid5_show_rmw_level(struct mddev *mddev, char *page)
5959{
5960 struct r5conf *conf = mddev->private;
5961 if (conf)
5962 return sprintf(page, "%d\n", conf->rmw_level);
5963 else
5964 return 0;
5965}
5966
5967static ssize_t
5968raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
5969{
5970 struct r5conf *conf = mddev->private;
5971 unsigned long new;
5972
5973 if (!conf)
5974 return -ENODEV;
5975
5976 if (len >= PAGE_SIZE)
5977 return -EINVAL;
5978
5979 if (kstrtoul(page, 10, &new))
5980 return -EINVAL;
5981
5982 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
5983 return -EINVAL;
5984
5985 if (new != PARITY_DISABLE_RMW &&
5986 new != PARITY_ENABLE_RMW &&
5987 new != PARITY_PREFER_RMW)
5988 return -EINVAL;
5989
5990 conf->rmw_level = new;
5991 return len;
5992}
5993
5994static struct md_sysfs_entry
5995raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
5996 raid5_show_rmw_level,
5997 raid5_store_rmw_level);
5998
5999
6000static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006001raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006002{
NeilBrown7b1485b2014-12-15 12:56:59 +11006003 struct r5conf *conf;
6004 int ret = 0;
6005 spin_lock(&mddev->lock);
6006 conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006007 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006008 ret = sprintf(page, "%d\n", conf->bypass_threshold);
6009 spin_unlock(&mddev->lock);
6010 return ret;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006011}
6012
6013static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006014raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006015{
NeilBrown67918752014-12-15 12:57:01 +11006016 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006017 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006018 int err;
6019
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006020 if (len >= PAGE_SIZE)
6021 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006022 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006023 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006024
6025 err = mddev_lock(mddev);
6026 if (err)
6027 return err;
6028 conf = mddev->private;
6029 if (!conf)
6030 err = -ENODEV;
NeilBrownedbe83a2015-02-26 12:47:56 +11006031 else if (new > conf->min_nr_stripes)
NeilBrown67918752014-12-15 12:57:01 +11006032 err = -EINVAL;
6033 else
6034 conf->bypass_threshold = new;
6035 mddev_unlock(mddev);
6036 return err ?: len;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006037}
6038
6039static struct md_sysfs_entry
6040raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
6041 S_IRUGO | S_IWUSR,
6042 raid5_show_preread_threshold,
6043 raid5_store_preread_threshold);
6044
6045static ssize_t
Shaohua Lid592a992014-05-21 17:57:44 +08006046raid5_show_skip_copy(struct mddev *mddev, char *page)
6047{
NeilBrown7b1485b2014-12-15 12:56:59 +11006048 struct r5conf *conf;
6049 int ret = 0;
6050 spin_lock(&mddev->lock);
6051 conf = mddev->private;
Shaohua Lid592a992014-05-21 17:57:44 +08006052 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006053 ret = sprintf(page, "%d\n", conf->skip_copy);
6054 spin_unlock(&mddev->lock);
6055 return ret;
Shaohua Lid592a992014-05-21 17:57:44 +08006056}
6057
6058static ssize_t
6059raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
6060{
NeilBrown67918752014-12-15 12:57:01 +11006061 struct r5conf *conf;
Shaohua Lid592a992014-05-21 17:57:44 +08006062 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006063 int err;
6064
Shaohua Lid592a992014-05-21 17:57:44 +08006065 if (len >= PAGE_SIZE)
6066 return -EINVAL;
Shaohua Lid592a992014-05-21 17:57:44 +08006067 if (kstrtoul(page, 10, &new))
6068 return -EINVAL;
6069 new = !!new;
Shaohua Lid592a992014-05-21 17:57:44 +08006070
NeilBrown67918752014-12-15 12:57:01 +11006071 err = mddev_lock(mddev);
6072 if (err)
6073 return err;
6074 conf = mddev->private;
6075 if (!conf)
6076 err = -ENODEV;
6077 else if (new != conf->skip_copy) {
6078 mddev_suspend(mddev);
6079 conf->skip_copy = new;
6080 if (new)
6081 mddev->queue->backing_dev_info.capabilities |=
6082 BDI_CAP_STABLE_WRITES;
6083 else
6084 mddev->queue->backing_dev_info.capabilities &=
6085 ~BDI_CAP_STABLE_WRITES;
6086 mddev_resume(mddev);
6087 }
6088 mddev_unlock(mddev);
6089 return err ?: len;
Shaohua Lid592a992014-05-21 17:57:44 +08006090}
6091
6092static struct md_sysfs_entry
6093raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
6094 raid5_show_skip_copy,
6095 raid5_store_skip_copy);
6096
Shaohua Lid592a992014-05-21 17:57:44 +08006097static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006098stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006099{
NeilBrownd1688a62011-10-11 16:49:52 +11006100 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006101 if (conf)
6102 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
6103 else
6104 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08006105}
6106
NeilBrown96de1e62005-11-08 21:39:39 -08006107static struct md_sysfs_entry
6108raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08006109
Shaohua Lib7214202013-08-27 17:50:42 +08006110static ssize_t
6111raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
6112{
NeilBrown7b1485b2014-12-15 12:56:59 +11006113 struct r5conf *conf;
6114 int ret = 0;
6115 spin_lock(&mddev->lock);
6116 conf = mddev->private;
Shaohua Lib7214202013-08-27 17:50:42 +08006117 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006118 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
6119 spin_unlock(&mddev->lock);
6120 return ret;
Shaohua Lib7214202013-08-27 17:50:42 +08006121}
6122
majianpeng60aaf932013-11-14 15:16:20 +11006123static int alloc_thread_groups(struct r5conf *conf, int cnt,
6124 int *group_cnt,
6125 int *worker_cnt_per_group,
6126 struct r5worker_group **worker_groups);
Shaohua Lib7214202013-08-27 17:50:42 +08006127static ssize_t
6128raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
6129{
NeilBrown67918752014-12-15 12:57:01 +11006130 struct r5conf *conf;
Shaohua Lib7214202013-08-27 17:50:42 +08006131 unsigned long new;
6132 int err;
majianpeng60aaf932013-11-14 15:16:20 +11006133 struct r5worker_group *new_groups, *old_groups;
6134 int group_cnt, worker_cnt_per_group;
Shaohua Lib7214202013-08-27 17:50:42 +08006135
6136 if (len >= PAGE_SIZE)
6137 return -EINVAL;
Shaohua Lib7214202013-08-27 17:50:42 +08006138 if (kstrtoul(page, 10, &new))
6139 return -EINVAL;
6140
NeilBrown67918752014-12-15 12:57:01 +11006141 err = mddev_lock(mddev);
Shaohua Lib7214202013-08-27 17:50:42 +08006142 if (err)
6143 return err;
NeilBrown67918752014-12-15 12:57:01 +11006144 conf = mddev->private;
6145 if (!conf)
6146 err = -ENODEV;
6147 else if (new != conf->worker_cnt_per_group) {
6148 mddev_suspend(mddev);
6149
6150 old_groups = conf->worker_groups;
6151 if (old_groups)
6152 flush_workqueue(raid5_wq);
6153
6154 err = alloc_thread_groups(conf, new,
6155 &group_cnt, &worker_cnt_per_group,
6156 &new_groups);
6157 if (!err) {
6158 spin_lock_irq(&conf->device_lock);
6159 conf->group_cnt = group_cnt;
6160 conf->worker_cnt_per_group = worker_cnt_per_group;
6161 conf->worker_groups = new_groups;
6162 spin_unlock_irq(&conf->device_lock);
6163
6164 if (old_groups)
6165 kfree(old_groups[0].workers);
6166 kfree(old_groups);
6167 }
6168 mddev_resume(mddev);
6169 }
6170 mddev_unlock(mddev);
6171
6172 return err ?: len;
Shaohua Lib7214202013-08-27 17:50:42 +08006173}
6174
6175static struct md_sysfs_entry
6176raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
6177 raid5_show_group_thread_cnt,
6178 raid5_store_group_thread_cnt);
6179
NeilBrown007583c2005-11-08 21:39:30 -08006180static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08006181 &raid5_stripecache_size.attr,
6182 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006183 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08006184 &raid5_group_thread_cnt.attr,
Shaohua Lid592a992014-05-21 17:57:44 +08006185 &raid5_skip_copy.attr,
Markus Stockhausend06f1912014-12-15 12:57:05 +11006186 &raid5_rmw_level.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08006187 NULL,
6188};
NeilBrown007583c2005-11-08 21:39:30 -08006189static struct attribute_group raid5_attrs_group = {
6190 .name = NULL,
6191 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08006192};
6193
majianpeng60aaf932013-11-14 15:16:20 +11006194static int alloc_thread_groups(struct r5conf *conf, int cnt,
6195 int *group_cnt,
6196 int *worker_cnt_per_group,
6197 struct r5worker_group **worker_groups)
Shaohua Li851c30c2013-08-28 14:30:16 +08006198{
Shaohua Li566c09c2013-11-14 15:16:17 +11006199 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08006200 ssize_t size;
6201 struct r5worker *workers;
6202
majianpeng60aaf932013-11-14 15:16:20 +11006203 *worker_cnt_per_group = cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +08006204 if (cnt == 0) {
majianpeng60aaf932013-11-14 15:16:20 +11006205 *group_cnt = 0;
6206 *worker_groups = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08006207 return 0;
6208 }
majianpeng60aaf932013-11-14 15:16:20 +11006209 *group_cnt = num_possible_nodes();
Shaohua Li851c30c2013-08-28 14:30:16 +08006210 size = sizeof(struct r5worker) * cnt;
majianpeng60aaf932013-11-14 15:16:20 +11006211 workers = kzalloc(size * *group_cnt, GFP_NOIO);
6212 *worker_groups = kzalloc(sizeof(struct r5worker_group) *
6213 *group_cnt, GFP_NOIO);
6214 if (!*worker_groups || !workers) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006215 kfree(workers);
majianpeng60aaf932013-11-14 15:16:20 +11006216 kfree(*worker_groups);
Shaohua Li851c30c2013-08-28 14:30:16 +08006217 return -ENOMEM;
6218 }
6219
majianpeng60aaf932013-11-14 15:16:20 +11006220 for (i = 0; i < *group_cnt; i++) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006221 struct r5worker_group *group;
6222
NeilBrown0c775d52013-11-25 11:12:43 +11006223 group = &(*worker_groups)[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08006224 INIT_LIST_HEAD(&group->handle_list);
6225 group->conf = conf;
6226 group->workers = workers + i * cnt;
6227
6228 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11006229 struct r5worker *worker = group->workers + j;
6230 worker->group = group;
6231 INIT_WORK(&worker->work, raid5_do_work);
6232
6233 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
6234 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08006235 }
6236 }
6237
6238 return 0;
6239}
6240
6241static void free_thread_groups(struct r5conf *conf)
6242{
6243 if (conf->worker_groups)
6244 kfree(conf->worker_groups[0].workers);
6245 kfree(conf->worker_groups);
6246 conf->worker_groups = NULL;
6247}
6248
Dan Williams80c3a6c2009-03-17 18:10:40 -07006249static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11006250raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07006251{
NeilBrownd1688a62011-10-11 16:49:52 +11006252 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006253
6254 if (!sectors)
6255 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006256 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11006257 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11006258 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006259
NeilBrown3cb5edf2015-07-15 17:24:17 +10006260 sectors &= ~((sector_t)conf->chunk_sectors - 1);
6261 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006262 return sectors * (raid_disks - conf->max_degraded);
6263}
6264
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306265static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6266{
6267 safe_put_page(percpu->spare_page);
shli@kernel.org46d5b782014-12-15 12:57:02 +11006268 if (percpu->scribble)
6269 flex_array_free(percpu->scribble);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306270 percpu->spare_page = NULL;
6271 percpu->scribble = NULL;
6272}
6273
6274static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6275{
6276 if (conf->level == 6 && !percpu->spare_page)
6277 percpu->spare_page = alloc_page(GFP_KERNEL);
6278 if (!percpu->scribble)
shli@kernel.org46d5b782014-12-15 12:57:02 +11006279 percpu->scribble = scribble_alloc(max(conf->raid_disks,
NeilBrown738a2732015-05-08 18:19:39 +10006280 conf->previous_raid_disks),
6281 max(conf->chunk_sectors,
6282 conf->prev_chunk_sectors)
6283 / STRIPE_SECTORS,
6284 GFP_KERNEL);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306285
6286 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
6287 free_scratch_buffer(conf, percpu);
6288 return -ENOMEM;
6289 }
6290
6291 return 0;
6292}
6293
NeilBrownd1688a62011-10-11 16:49:52 +11006294static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006295{
Dan Williams36d1c642009-07-14 11:48:22 -07006296 unsigned long cpu;
6297
6298 if (!conf->percpu)
6299 return;
6300
Dan Williams36d1c642009-07-14 11:48:22 -07006301#ifdef CONFIG_HOTPLUG_CPU
6302 unregister_cpu_notifier(&conf->cpu_notify);
6303#endif
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306304
6305 get_online_cpus();
6306 for_each_possible_cpu(cpu)
6307 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07006308 put_online_cpus();
6309
6310 free_percpu(conf->percpu);
6311}
6312
NeilBrownd1688a62011-10-11 16:49:52 +11006313static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10006314{
NeilBrownedbe83a2015-02-26 12:47:56 +11006315 if (conf->shrinker.seeks)
6316 unregister_shrinker(&conf->shrinker);
Shaohua Li851c30c2013-08-28 14:30:16 +08006317 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10006318 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07006319 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10006320 kfree(conf->disks);
6321 kfree(conf->stripe_hashtbl);
6322 kfree(conf);
6323}
6324
Dan Williams36d1c642009-07-14 11:48:22 -07006325#ifdef CONFIG_HOTPLUG_CPU
6326static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
6327 void *hcpu)
6328{
NeilBrownd1688a62011-10-11 16:49:52 +11006329 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07006330 long cpu = (long)hcpu;
6331 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
6332
6333 switch (action) {
6334 case CPU_UP_PREPARE:
6335 case CPU_UP_PREPARE_FROZEN:
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306336 if (alloc_scratch_buffer(conf, percpu)) {
Dan Williams36d1c642009-07-14 11:48:22 -07006337 pr_err("%s: failed memory allocation for cpu%ld\n",
6338 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07006339 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07006340 }
6341 break;
6342 case CPU_DEAD:
6343 case CPU_DEAD_FROZEN:
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306344 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07006345 break;
6346 default:
6347 break;
6348 }
6349 return NOTIFY_OK;
6350}
6351#endif
6352
NeilBrownd1688a62011-10-11 16:49:52 +11006353static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006354{
6355 unsigned long cpu;
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306356 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006357
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306358 conf->percpu = alloc_percpu(struct raid5_percpu);
6359 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07006360 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006361
Dan Williams36d1c642009-07-14 11:48:22 -07006362#ifdef CONFIG_HOTPLUG_CPU
6363 conf->cpu_notify.notifier_call = raid456_cpu_notify;
6364 conf->cpu_notify.priority = 0;
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306365 err = register_cpu_notifier(&conf->cpu_notify);
6366 if (err)
6367 return err;
Dan Williams36d1c642009-07-14 11:48:22 -07006368#endif
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306369
6370 get_online_cpus();
6371 for_each_present_cpu(cpu) {
6372 err = alloc_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
6373 if (err) {
6374 pr_err("%s: failed memory allocation for cpu%ld\n",
6375 __func__, cpu);
6376 break;
6377 }
6378 }
Dan Williams36d1c642009-07-14 11:48:22 -07006379 put_online_cpus();
6380
6381 return err;
6382}
6383
NeilBrownedbe83a2015-02-26 12:47:56 +11006384static unsigned long raid5_cache_scan(struct shrinker *shrink,
6385 struct shrink_control *sc)
6386{
6387 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
NeilBrown2d5b5692015-07-06 12:49:23 +10006388 unsigned long ret = SHRINK_STOP;
6389
6390 if (mutex_trylock(&conf->cache_size_mutex)) {
6391 ret= 0;
NeilBrown49895bc2015-08-03 17:09:57 +10006392 while (ret < sc->nr_to_scan &&
6393 conf->max_nr_stripes > conf->min_nr_stripes) {
NeilBrown2d5b5692015-07-06 12:49:23 +10006394 if (drop_one_stripe(conf) == 0) {
6395 ret = SHRINK_STOP;
6396 break;
6397 }
6398 ret++;
6399 }
6400 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006401 }
6402 return ret;
6403}
6404
6405static unsigned long raid5_cache_count(struct shrinker *shrink,
6406 struct shrink_control *sc)
6407{
6408 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
6409
6410 if (conf->max_nr_stripes < conf->min_nr_stripes)
6411 /* unlikely, but not impossible */
6412 return 0;
6413 return conf->max_nr_stripes - conf->min_nr_stripes;
6414}
6415
NeilBrownd1688a62011-10-11 16:49:52 +11006416static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417{
NeilBrownd1688a62011-10-11 16:49:52 +11006418 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006419 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11006420 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10006422 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11006423 int i;
majianpeng60aaf932013-11-14 15:16:20 +11006424 int group_cnt, worker_cnt_per_group;
6425 struct r5worker_group *new_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426
NeilBrown91adb562009-03-31 14:39:39 +11006427 if (mddev->new_level != 5
6428 && mddev->new_level != 4
6429 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10006430 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11006431 mdname(mddev), mddev->new_level);
6432 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 }
NeilBrown91adb562009-03-31 14:39:39 +11006434 if ((mddev->new_level == 5
6435 && !algorithm_valid_raid5(mddev->new_layout)) ||
6436 (mddev->new_level == 6
6437 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10006438 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11006439 mdname(mddev), mddev->new_layout);
6440 return ERR_PTR(-EIO);
6441 }
6442 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10006443 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11006444 mdname(mddev), mddev->raid_disks);
6445 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11006446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447
Andre Noll664e7c42009-06-18 08:45:27 +10006448 if (!mddev->new_chunk_sectors ||
6449 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
6450 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10006451 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
6452 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11006453 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11006454 }
6455
NeilBrownd1688a62011-10-11 16:49:52 +11006456 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11006457 if (conf == NULL)
6458 goto abort;
Shaohua Li851c30c2013-08-28 14:30:16 +08006459 /* Don't enable multi-threading by default*/
majianpeng60aaf932013-11-14 15:16:20 +11006460 if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group,
6461 &new_group)) {
6462 conf->group_cnt = group_cnt;
6463 conf->worker_cnt_per_group = worker_cnt_per_group;
6464 conf->worker_groups = new_group;
6465 } else
Shaohua Li851c30c2013-08-28 14:30:16 +08006466 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11006467 spin_lock_init(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10006468 seqcount_init(&conf->gen_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006469 mutex_init(&conf->cache_size_mutex);
Yuanhan Liub1b46482015-05-08 18:19:06 +10006470 init_waitqueue_head(&conf->wait_for_quiescent);
Yuanhan Liue9e4c372015-05-08 18:19:07 +10006471 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) {
6472 init_waitqueue_head(&conf->wait_for_stripe[i]);
6473 }
Dan Williamsf5efd452009-10-16 15:55:38 +11006474 init_waitqueue_head(&conf->wait_for_overlap);
6475 INIT_LIST_HEAD(&conf->handle_list);
6476 INIT_LIST_HEAD(&conf->hold_list);
6477 INIT_LIST_HEAD(&conf->delayed_list);
6478 INIT_LIST_HEAD(&conf->bitmap_list);
NeilBrownc3cce6c2015-08-14 12:47:33 +10006479 bio_list_init(&conf->return_bi);
Shaohua Li773ca822013-08-27 17:50:39 +08006480 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11006481 atomic_set(&conf->active_stripes, 0);
6482 atomic_set(&conf->preread_active_stripes, 0);
6483 atomic_set(&conf->active_aligned_reads, 0);
6484 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11006485 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11006486
6487 conf->raid_disks = mddev->raid_disks;
6488 if (mddev->reshape_position == MaxSector)
6489 conf->previous_raid_disks = mddev->raid_disks;
6490 else
6491 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006492 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11006493
NeilBrown5e5e3e72009-10-16 16:35:30 +11006494 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11006495 GFP_KERNEL);
6496 if (!conf->disks)
6497 goto abort;
6498
6499 conf->mddev = mddev;
6500
6501 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
6502 goto abort;
6503
Shaohua Li566c09c2013-11-14 15:16:17 +11006504 /* We init hash_locks[0] separately to that it can be used
6505 * as the reference lock in the spin_lock_nest_lock() call
6506 * in lock_all_device_hash_locks_irq in order to convince
6507 * lockdep that we know what we are doing.
6508 */
6509 spin_lock_init(conf->hash_locks);
6510 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
6511 spin_lock_init(conf->hash_locks + i);
6512
6513 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6514 INIT_LIST_HEAD(conf->inactive_list + i);
6515
6516 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6517 INIT_LIST_HEAD(conf->temp_inactive_list + i);
6518
Dan Williams36d1c642009-07-14 11:48:22 -07006519 conf->level = mddev->new_level;
shli@kernel.org46d5b782014-12-15 12:57:02 +11006520 conf->chunk_sectors = mddev->new_chunk_sectors;
Dan Williams36d1c642009-07-14 11:48:22 -07006521 if (raid5_alloc_percpu(conf) != 0)
6522 goto abort;
6523
NeilBrown0c55e022010-05-03 14:09:02 +10006524 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11006525
NeilBrowndafb20f2012-03-19 12:46:39 +11006526 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11006527 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006528 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11006529 || raid_disk < 0)
6530 continue;
6531 disk = conf->disks + raid_disk;
6532
NeilBrown17045f52011-12-23 10:17:53 +11006533 if (test_bit(Replacement, &rdev->flags)) {
6534 if (disk->replacement)
6535 goto abort;
6536 disk->replacement = rdev;
6537 } else {
6538 if (disk->rdev)
6539 goto abort;
6540 disk->rdev = rdev;
6541 }
NeilBrown91adb562009-03-31 14:39:39 +11006542
6543 if (test_bit(In_sync, &rdev->flags)) {
6544 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10006545 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
6546 " disk %d\n",
6547 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05006548 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11006549 /* Cannot rely on bitmap to complete recovery */
6550 conf->fullsync = 1;
6551 }
6552
NeilBrown91adb562009-03-31 14:39:39 +11006553 conf->level = mddev->new_level;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006554 if (conf->level == 6) {
NeilBrown91adb562009-03-31 14:39:39 +11006555 conf->max_degraded = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006556 if (raid6_call.xor_syndrome)
6557 conf->rmw_level = PARITY_ENABLE_RMW;
6558 else
6559 conf->rmw_level = PARITY_DISABLE_RMW;
6560 } else {
NeilBrown91adb562009-03-31 14:39:39 +11006561 conf->max_degraded = 1;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11006562 conf->rmw_level = PARITY_ENABLE_RMW;
6563 }
NeilBrown91adb562009-03-31 14:39:39 +11006564 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11006565 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11006566 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10006567 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11006568 conf->prev_algo = mddev->layout;
NeilBrown5cac6bc2015-07-17 12:17:50 +10006569 } else {
6570 conf->prev_chunk_sectors = conf->chunk_sectors;
6571 conf->prev_algo = conf->algorithm;
NeilBrowne183eae2009-03-31 15:20:22 +11006572 }
NeilBrown91adb562009-03-31 14:39:39 +11006573
NeilBrownedbe83a2015-02-26 12:47:56 +11006574 conf->min_nr_stripes = NR_STRIPES;
6575 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11006576 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li4bda5562013-11-14 15:16:17 +11006577 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
NeilBrownedbe83a2015-02-26 12:47:56 +11006578 if (grow_stripes(conf, conf->min_nr_stripes)) {
NeilBrown91adb562009-03-31 14:39:39 +11006579 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10006580 "md/raid:%s: couldn't allocate %dkB for buffers\n",
6581 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11006582 goto abort;
6583 } else
NeilBrown0c55e022010-05-03 14:09:02 +10006584 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
6585 mdname(mddev), memory);
NeilBrownedbe83a2015-02-26 12:47:56 +11006586 /*
6587 * Losing a stripe head costs more than the time to refill it,
6588 * it reduces the queue depth and so can hurt throughput.
6589 * So set it rather large, scaled by number of devices.
6590 */
6591 conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
6592 conf->shrinker.scan_objects = raid5_cache_scan;
6593 conf->shrinker.count_objects = raid5_cache_count;
6594 conf->shrinker.batch = 128;
6595 conf->shrinker.flags = 0;
6596 register_shrinker(&conf->shrinker);
NeilBrown91adb562009-03-31 14:39:39 +11006597
NeilBrown02326052012-07-03 15:56:52 +10006598 sprintf(pers_name, "raid%d", mddev->new_level);
6599 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11006600 if (!conf->thread) {
6601 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10006602 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11006603 mdname(mddev));
6604 goto abort;
6605 }
6606
6607 return conf;
6608
6609 abort:
6610 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10006611 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11006612 return ERR_PTR(-EIO);
6613 } else
6614 return ERR_PTR(-ENOMEM);
6615}
6616
NeilBrownc148ffd2009-11-13 17:47:00 +11006617static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
6618{
6619 switch (algo) {
6620 case ALGORITHM_PARITY_0:
6621 if (raid_disk < max_degraded)
6622 return 1;
6623 break;
6624 case ALGORITHM_PARITY_N:
6625 if (raid_disk >= raid_disks - max_degraded)
6626 return 1;
6627 break;
6628 case ALGORITHM_PARITY_0_6:
NeilBrownf72ffdd2014-09-30 14:23:59 +10006629 if (raid_disk == 0 ||
NeilBrownc148ffd2009-11-13 17:47:00 +11006630 raid_disk == raid_disks - 1)
6631 return 1;
6632 break;
6633 case ALGORITHM_LEFT_ASYMMETRIC_6:
6634 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6635 case ALGORITHM_LEFT_SYMMETRIC_6:
6636 case ALGORITHM_RIGHT_SYMMETRIC_6:
6637 if (raid_disk == raid_disks - 1)
6638 return 1;
6639 }
6640 return 0;
6641}
6642
NeilBrownfd01b882011-10-11 16:47:53 +11006643static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11006644{
NeilBrownd1688a62011-10-11 16:49:52 +11006645 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10006646 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11006647 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006648 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11006649 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11006650 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10006651 long long min_offset_diff = 0;
6652 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11006653
Andre Noll8c6ac862009-06-18 08:48:06 +10006654 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10006655 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10006656 " -- starting background reconstruction\n",
6657 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10006658
6659 rdev_for_each(rdev, mddev) {
6660 long long diff;
6661 if (rdev->raid_disk < 0)
6662 continue;
6663 diff = (rdev->new_data_offset - rdev->data_offset);
6664 if (first) {
6665 min_offset_diff = diff;
6666 first = 0;
6667 } else if (mddev->reshape_backwards &&
6668 diff < min_offset_diff)
6669 min_offset_diff = diff;
6670 else if (!mddev->reshape_backwards &&
6671 diff > min_offset_diff)
6672 min_offset_diff = diff;
6673 }
6674
NeilBrownf6705572006-03-27 01:18:11 -08006675 if (mddev->reshape_position != MaxSector) {
6676 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10006677 * Difficulties arise if the stripe we would write to
6678 * next is at or after the stripe we would read from next.
6679 * For a reshape that changes the number of devices, this
6680 * is only possible for a very short time, and mdadm makes
6681 * sure that time appears to have past before assembling
6682 * the array. So we fail if that time hasn't passed.
6683 * For a reshape that keeps the number of devices the same
6684 * mdadm must be monitoring the reshape can keeping the
6685 * critical areas read-only and backed up. It will start
6686 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08006687 */
6688 sector_t here_new, here_old;
6689 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11006690 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrown05256d92015-07-15 17:36:21 +10006691 int chunk_sectors;
6692 int new_data_disks;
NeilBrownf6705572006-03-27 01:18:11 -08006693
NeilBrown88ce4932009-03-31 15:24:23 +11006694 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10006695 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08006696 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08006697 mdname(mddev));
6698 return -EINVAL;
6699 }
NeilBrownf6705572006-03-27 01:18:11 -08006700 old_disks = mddev->raid_disks - mddev->delta_disks;
6701 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08006702 * further up in new geometry must map after here in old
6703 * geometry.
NeilBrown05256d92015-07-15 17:36:21 +10006704 * If the chunk sizes are different, then as we perform reshape
6705 * in units of the largest of the two, reshape_position needs
6706 * be a multiple of the largest chunk size times new data disks.
NeilBrownf6705572006-03-27 01:18:11 -08006707 */
6708 here_new = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10006709 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
6710 new_data_disks = mddev->raid_disks - max_degraded;
6711 if (sector_div(here_new, chunk_sectors * new_data_disks)) {
NeilBrown0c55e022010-05-03 14:09:02 +10006712 printk(KERN_ERR "md/raid:%s: reshape_position not "
6713 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08006714 return -EINVAL;
6715 }
NeilBrown05256d92015-07-15 17:36:21 +10006716 reshape_offset = here_new * chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08006717 /* here_new is the stripe we will write to */
6718 here_old = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10006719 sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
NeilBrownf4168852007-02-28 20:11:53 -08006720 /* here_old is the first stripe that we might need to read
6721 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10006722 if (mddev->delta_disks == 0) {
6723 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10006724 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10006725 * and taking constant backups.
6726 * mdadm always starts a situation like this in
6727 * readonly mode so it can take control before
6728 * allowing any writes. So just check for that.
6729 */
NeilBrownb5254dd2012-05-21 09:27:01 +10006730 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
6731 abs(min_offset_diff) >= mddev->new_chunk_sectors)
6732 /* not really in-place - so OK */;
6733 else if (mddev->ro == 0) {
6734 printk(KERN_ERR "md/raid:%s: in-place reshape "
6735 "must be started in read-only mode "
6736 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10006737 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10006738 return -EINVAL;
6739 }
NeilBrown2c810cd2012-05-21 09:27:00 +10006740 } else if (mddev->reshape_backwards
NeilBrown05256d92015-07-15 17:36:21 +10006741 ? (here_new * chunk_sectors + min_offset_diff <=
6742 here_old * chunk_sectors)
6743 : (here_new * chunk_sectors >=
6744 here_old * chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08006745 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10006746 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
6747 "auto-recovery - aborting.\n",
6748 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08006749 return -EINVAL;
6750 }
NeilBrown0c55e022010-05-03 14:09:02 +10006751 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
6752 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08006753 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08006754 } else {
NeilBrown91adb562009-03-31 14:39:39 +11006755 BUG_ON(mddev->level != mddev->new_level);
6756 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10006757 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11006758 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08006759 }
6760
NeilBrown245f46c2009-03-31 14:39:39 +11006761 if (mddev->private == NULL)
6762 conf = setup_conf(mddev);
6763 else
6764 conf = mddev->private;
6765
NeilBrown91adb562009-03-31 14:39:39 +11006766 if (IS_ERR(conf))
6767 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08006768
NeilBrownb5254dd2012-05-21 09:27:01 +10006769 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11006770 mddev->thread = conf->thread;
6771 conf->thread = NULL;
6772 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773
NeilBrown17045f52011-12-23 10:17:53 +11006774 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
6775 i++) {
6776 rdev = conf->disks[i].rdev;
6777 if (!rdev && conf->disks[i].replacement) {
6778 /* The replacement is all we have yet */
6779 rdev = conf->disks[i].replacement;
6780 conf->disks[i].replacement = NULL;
6781 clear_bit(Replacement, &rdev->flags);
6782 conf->disks[i].rdev = rdev;
6783 }
6784 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11006785 continue;
NeilBrown17045f52011-12-23 10:17:53 +11006786 if (conf->disks[i].replacement &&
6787 conf->reshape_progress != MaxSector) {
6788 /* replacements and reshape simply do not mix. */
6789 printk(KERN_ERR "md: cannot handle concurrent "
6790 "replacement and reshape.\n");
6791 goto abort;
6792 }
NeilBrown2f115882010-06-17 17:41:03 +10006793 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11006794 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10006795 continue;
6796 }
NeilBrownc148ffd2009-11-13 17:47:00 +11006797 /* This disc is not fully in-sync. However if it
6798 * just stored parity (beyond the recovery_offset),
6799 * when we don't need to be concerned about the
6800 * array being dirty.
6801 * When reshape goes 'backwards', we never have
6802 * partially completed devices, so we only need
6803 * to worry about reshape going forwards.
6804 */
6805 /* Hack because v0.91 doesn't store recovery_offset properly. */
6806 if (mddev->major_version == 0 &&
6807 mddev->minor_version > 90)
6808 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07006809
NeilBrownc148ffd2009-11-13 17:47:00 +11006810 if (rdev->recovery_offset < reshape_offset) {
6811 /* We need to check old and new layout */
6812 if (!only_parity(rdev->raid_disk,
6813 conf->algorithm,
6814 conf->raid_disks,
6815 conf->max_degraded))
6816 continue;
6817 }
6818 if (!only_parity(rdev->raid_disk,
6819 conf->prev_algo,
6820 conf->previous_raid_disks,
6821 conf->max_degraded))
6822 continue;
6823 dirty_parity_disks++;
6824 }
NeilBrown91adb562009-03-31 14:39:39 +11006825
NeilBrown17045f52011-12-23 10:17:53 +11006826 /*
6827 * 0 for a fully functional array, 1 or 2 for a degraded array.
6828 */
NeilBrown908f4fb2011-12-23 10:17:50 +11006829 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830
NeilBrown674806d2010-06-16 17:17:53 +10006831 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10006832 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07006834 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835 goto abort;
6836 }
6837
NeilBrown91adb562009-03-31 14:39:39 +11006838 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10006839 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11006840 mddev->resync_max_sectors = mddev->dev_sectors;
6841
NeilBrownc148ffd2009-11-13 17:47:00 +11006842 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006844 if (mddev->ok_start_degraded)
6845 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10006846 "md/raid:%s: starting dirty degraded array"
6847 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006848 mdname(mddev));
6849 else {
6850 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10006851 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006852 mdname(mddev));
6853 goto abort;
6854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 }
6856
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10006858 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
6859 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11006860 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
6861 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 else
NeilBrown0c55e022010-05-03 14:09:02 +10006863 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
6864 " out of %d devices, algorithm %d\n",
6865 mdname(mddev), conf->level,
6866 mddev->raid_disks - mddev->degraded,
6867 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868
6869 print_raid5_conf(conf);
6870
NeilBrownfef9c612009-03-31 15:16:46 +11006871 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11006872 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08006873 atomic_set(&conf->reshape_stripes, 0);
6874 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6875 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6876 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6877 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6878 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006879 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08006880 }
6881
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10006883 if (mddev->to_remove == &raid5_attrs_group)
6884 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006885 else if (mddev->kobj.sd &&
6886 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08006887 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10006888 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08006889 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10006890 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6891
6892 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10006893 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11006894 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10006895 /* read-ahead size must cover two whole stripes, which
6896 * is 2 * (datadisks) * chunksize where 'n' is the
6897 * number of raid devices
6898 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 int data_disks = conf->previous_raid_disks - conf->max_degraded;
6900 int stripe = data_disks *
6901 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
6902 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6903 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10006904
NeilBrown9f7c2222010-07-26 12:04:13 +10006905 chunk_size = mddev->chunk_sectors << 9;
6906 blk_queue_io_min(mddev->queue, chunk_size);
6907 blk_queue_io_opt(mddev->queue, chunk_size *
6908 (conf->raid_disks - conf->max_degraded));
Kent Overstreetc78afc62013-07-11 22:39:53 -07006909 mddev->queue->limits.raid_partial_stripes_expensive = 1;
Shaohua Li620125f2012-10-11 13:49:05 +11006910 /*
6911 * We can only discard a whole stripe. It doesn't make sense to
6912 * discard data disk but write parity disk
6913 */
6914 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11006915 /* Round up to power of 2, as discard handling
6916 * currently assumes that */
6917 while ((stripe-1) & stripe)
6918 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11006919 mddev->queue->limits.discard_alignment = stripe;
6920 mddev->queue->limits.discard_granularity = stripe;
6921 /*
6922 * unaligned part of discard request will be ignored, so can't
NeilBrown8e0e99b2014-10-02 13:45:00 +10006923 * guarantee discard_zeroes_data
Shaohua Li620125f2012-10-11 13:49:05 +11006924 */
6925 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10006926
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07006927 blk_queue_max_write_same_sectors(mddev->queue, 0);
6928
NeilBrown05616be2012-05-21 09:27:00 +10006929 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10006930 disk_stack_limits(mddev->gendisk, rdev->bdev,
6931 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10006932 disk_stack_limits(mddev->gendisk, rdev->bdev,
6933 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11006934 /*
6935 * discard_zeroes_data is required, otherwise data
6936 * could be lost. Consider a scenario: discard a stripe
6937 * (the stripe could be inconsistent if
6938 * discard_zeroes_data is 0); write one disk of the
6939 * stripe (the stripe could be inconsistent again
6940 * depending on which disks are used to calculate
6941 * parity); the disk is broken; The stripe data of this
6942 * disk is lost.
6943 */
6944 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
6945 !bdev_get_queue(rdev->bdev)->
6946 limits.discard_zeroes_data)
6947 discard_supported = false;
NeilBrown8e0e99b2014-10-02 13:45:00 +10006948 /* Unfortunately, discard_zeroes_data is not currently
6949 * a guarantee - just a hint. So we only allow DISCARD
6950 * if the sysadmin has confirmed that only safe devices
6951 * are in use by setting a module parameter.
6952 */
6953 if (!devices_handle_discard_safely) {
6954 if (discard_supported) {
6955 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
6956 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
6957 }
6958 discard_supported = false;
6959 }
NeilBrown05616be2012-05-21 09:27:00 +10006960 }
Shaohua Li620125f2012-10-11 13:49:05 +11006961
6962 if (discard_supported &&
6963 mddev->queue->limits.max_discard_sectors >= stripe &&
6964 mddev->queue->limits.discard_granularity >= stripe)
6965 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
6966 mddev->queue);
6967 else
6968 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
6969 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 }
6971
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 return 0;
6973abort:
NeilBrown01f96c02011-09-21 15:30:20 +10006974 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11006975 print_raid5_conf(conf);
6976 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10006978 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979 return -EIO;
6980}
6981
NeilBrownafa0f552014-12-15 12:56:58 +11006982static void raid5_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983{
NeilBrownafa0f552014-12-15 12:56:58 +11006984 struct r5conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985
Dan Williams95fc17a2009-07-31 12:39:15 +10006986 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10006987 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988}
6989
NeilBrownfd01b882011-10-11 16:47:53 +11006990static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991{
NeilBrownd1688a62011-10-11 16:49:52 +11006992 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993 int i;
6994
Andre Noll9d8f0362009-06-18 08:45:01 +10006995 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
NeilBrown3cb5edf2015-07-15 17:24:17 +10006996 conf->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07006997 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 for (i = 0; i < conf->raid_disks; i++)
6999 seq_printf (seq, "%s",
7000 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08007001 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003}
7004
NeilBrownd1688a62011-10-11 16:49:52 +11007005static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006{
7007 int i;
7008 struct disk_info *tmp;
7009
NeilBrown0c55e022010-05-03 14:09:02 +10007010 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011 if (!conf) {
7012 printk("(conf==NULL)\n");
7013 return;
7014 }
NeilBrown0c55e022010-05-03 14:09:02 +10007015 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
7016 conf->raid_disks,
7017 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018
7019 for (i = 0; i < conf->raid_disks; i++) {
7020 char b[BDEVNAME_SIZE];
7021 tmp = conf->disks + i;
7022 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10007023 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
7024 i, !test_bit(Faulty, &tmp->rdev->flags),
7025 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 }
7027}
7028
NeilBrownfd01b882011-10-11 16:47:53 +11007029static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030{
7031 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11007032 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10007034 int count = 0;
7035 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036
7037 for (i = 0; i < conf->raid_disks; i++) {
7038 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11007039 if (tmp->replacement
7040 && tmp->replacement->recovery_offset == MaxSector
7041 && !test_bit(Faulty, &tmp->replacement->flags)
7042 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
7043 /* Replacement has just become active. */
7044 if (!tmp->rdev
7045 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
7046 count++;
7047 if (tmp->rdev) {
7048 /* Replaced device not technically faulty,
7049 * but we need to be sure it gets removed
7050 * and never re-added.
7051 */
7052 set_bit(Faulty, &tmp->rdev->flags);
7053 sysfs_notify_dirent_safe(
7054 tmp->rdev->sysfs_state);
7055 }
7056 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
7057 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10007058 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08007059 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07007060 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10007061 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11007062 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063 }
7064 }
NeilBrown6b965622010-08-18 11:56:59 +10007065 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11007066 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007067 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007069 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070}
7071
NeilBrownb8321b62011-12-23 10:17:51 +11007072static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073{
NeilBrownd1688a62011-10-11 16:49:52 +11007074 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11007076 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11007077 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078 struct disk_info *p = conf->disks + number;
7079
7080 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11007081 if (rdev == p->rdev)
7082 rdevp = &p->rdev;
7083 else if (rdev == p->replacement)
7084 rdevp = &p->replacement;
7085 else
7086 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11007087
NeilBrown657e3e42011-12-23 10:17:52 +11007088 if (number >= conf->raid_disks &&
7089 conf->reshape_progress == MaxSector)
7090 clear_bit(In_sync, &rdev->flags);
7091
7092 if (test_bit(In_sync, &rdev->flags) ||
7093 atomic_read(&rdev->nr_pending)) {
7094 err = -EBUSY;
7095 goto abort;
7096 }
7097 /* Only remove non-faulty devices if recovery
7098 * isn't possible.
7099 */
7100 if (!test_bit(Faulty, &rdev->flags) &&
7101 mddev->recovery_disabled != conf->recovery_disabled &&
7102 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11007103 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11007104 number < conf->raid_disks) {
7105 err = -EBUSY;
7106 goto abort;
7107 }
7108 *rdevp = NULL;
7109 synchronize_rcu();
7110 if (atomic_read(&rdev->nr_pending)) {
7111 /* lost the race, try later */
7112 err = -EBUSY;
7113 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11007114 } else if (p->replacement) {
7115 /* We must have just cleared 'rdev' */
7116 p->rdev = p->replacement;
7117 clear_bit(Replacement, &p->replacement->flags);
7118 smp_mb(); /* Make sure other CPUs may see both as identical
7119 * but will never see neither - if they are careful
7120 */
7121 p->replacement = NULL;
7122 clear_bit(WantReplacement, &rdev->flags);
7123 } else
7124 /* We might have just removed the Replacement as faulty-
7125 * clear the bit just in case
7126 */
7127 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128abort:
7129
7130 print_raid5_conf(conf);
7131 return err;
7132}
7133
NeilBrownfd01b882011-10-11 16:47:53 +11007134static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135{
NeilBrownd1688a62011-10-11 16:49:52 +11007136 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10007137 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138 int disk;
7139 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10007140 int first = 0;
7141 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142
NeilBrown7f0da592011-07-28 11:39:22 +10007143 if (mddev->recovery_disabled == conf->recovery_disabled)
7144 return -EBUSY;
7145
NeilBrowndc10c642012-03-19 12:46:37 +11007146 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10007148 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
Neil Brown6c2fce22008-06-28 08:31:31 +10007150 if (rdev->raid_disk >= 0)
7151 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152
7153 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07007154 * find the disk ... but prefer rdev->saved_raid_disk
7155 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 */
NeilBrown16a53ec2006-06-26 00:27:38 -07007157 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10007158 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07007159 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10007160 first = rdev->saved_raid_disk;
7161
7162 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007163 p = conf->disks + disk;
7164 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08007165 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10007167 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07007168 if (rdev->saved_raid_disk != disk)
7169 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08007170 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10007171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007173 }
7174 for (disk = first; disk <= last; disk++) {
7175 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007176 if (test_bit(WantReplacement, &p->rdev->flags) &&
7177 p->replacement == NULL) {
7178 clear_bit(In_sync, &rdev->flags);
7179 set_bit(Replacement, &rdev->flags);
7180 rdev->raid_disk = disk;
7181 err = 0;
7182 conf->fullsync = 1;
7183 rcu_assign_pointer(p->replacement, rdev);
7184 break;
7185 }
7186 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007187out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10007189 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190}
7191
NeilBrownfd01b882011-10-11 16:47:53 +11007192static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193{
7194 /* no resync is happening, and there is enough space
7195 * on all devices, so we can resize.
7196 * We need to make sure resync covers any new space.
7197 * If the array is shrinking we should possibly wait until
7198 * any io in the removed space completes, but it hardly seems
7199 * worth it.
7200 */
NeilBrowna4a61252012-05-22 13:55:27 +10007201 sector_t newsize;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007202 struct r5conf *conf = mddev->private;
7203
7204 sectors &= ~((sector_t)conf->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10007205 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
7206 if (mddev->external_size &&
7207 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11007208 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10007209 if (mddev->bitmap) {
7210 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
7211 if (ret)
7212 return ret;
7213 }
7214 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10007215 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10007216 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10007217 if (sectors > mddev->dev_sectors &&
7218 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11007219 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7221 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007222 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07007223 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 return 0;
7225}
7226
NeilBrownfd01b882011-10-11 16:47:53 +11007227static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10007228{
7229 /* Can only proceed if there are plenty of stripe_heads.
7230 * We need a minimum of one full stripe,, and for sensible progress
7231 * it is best to have about 4 times that.
7232 * If we require 4 times, then the default 256 4K stripe_heads will
7233 * allow for chunk sizes up to 256K, which is probably OK.
7234 * If the chunk size is greater, user-space should request more
7235 * stripe_heads first.
7236 */
NeilBrownd1688a62011-10-11 16:49:52 +11007237 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10007238 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007239 > conf->min_nr_stripes ||
NeilBrown01ee22b2009-06-18 08:47:20 +10007240 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007241 > conf->min_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10007242 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7243 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10007244 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
7245 / STRIPE_SIZE)*4);
7246 return 0;
7247 }
7248 return 1;
7249}
7250
NeilBrownfd01b882011-10-11 16:47:53 +11007251static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08007252{
NeilBrownd1688a62011-10-11 16:49:52 +11007253 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08007254
NeilBrown88ce4932009-03-31 15:24:23 +11007255 if (mddev->delta_disks == 0 &&
7256 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10007257 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10007258 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10007259 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11007260 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10007261 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11007262 /* We might be able to shrink, but the devices must
7263 * be made bigger first.
7264 * For raid6, 4 is the minimum size.
7265 * Otherwise 2 is the minimum
7266 */
7267 int min = 2;
7268 if (mddev->level == 6)
7269 min = 4;
7270 if (mddev->raid_disks + mddev->delta_disks < min)
7271 return -EINVAL;
7272 }
NeilBrown29269552006-03-27 01:18:10 -08007273
NeilBrown01ee22b2009-06-18 08:47:20 +10007274 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08007275 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08007276
NeilBrown738a2732015-05-08 18:19:39 +10007277 if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
7278 mddev->delta_disks > 0)
7279 if (resize_chunks(conf,
7280 conf->previous_raid_disks
7281 + max(0, mddev->delta_disks),
7282 max(mddev->new_chunk_sectors,
7283 mddev->chunk_sectors)
7284 ) < 0)
7285 return -ENOMEM;
NeilBrowne56108d62012-10-11 14:24:13 +11007286 return resize_stripes(conf, (conf->previous_raid_disks
7287 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08007288}
7289
NeilBrownfd01b882011-10-11 16:47:53 +11007290static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08007291{
NeilBrownd1688a62011-10-11 16:49:52 +11007292 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11007293 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08007294 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07007295 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08007296
NeilBrownf4168852007-02-28 20:11:53 -08007297 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08007298 return -EBUSY;
7299
NeilBrown01ee22b2009-06-18 08:47:20 +10007300 if (!check_stripe_cache(mddev))
7301 return -ENOSPC;
7302
NeilBrown30b67642012-05-22 13:55:28 +10007303 if (has_failed(conf))
7304 return -EINVAL;
7305
NeilBrownc6563a82012-05-21 09:27:00 +10007306 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11007307 if (!test_bit(In_sync, &rdev->flags)
7308 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08007309 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10007310 }
NeilBrown63c70c42006-03-27 01:18:13 -08007311
NeilBrownf4168852007-02-28 20:11:53 -08007312 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08007313 /* Not enough devices even to make a degraded array
7314 * of that size
7315 */
7316 return -EINVAL;
7317
NeilBrownec32a2b2009-03-31 15:17:38 +11007318 /* Refuse to reduce size of the array. Any reductions in
7319 * array size must be through explicit setting of array_size
7320 * attribute.
7321 */
7322 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
7323 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10007324 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11007325 "before number of disks\n", mdname(mddev));
7326 return -EINVAL;
7327 }
7328
NeilBrownf6705572006-03-27 01:18:11 -08007329 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08007330 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10007331 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007332 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08007333 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007334 conf->prev_chunk_sectors = conf->chunk_sectors;
7335 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11007336 conf->prev_algo = conf->algorithm;
7337 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10007338 conf->generation++;
7339 /* Code that selects data_offset needs to see the generation update
7340 * if reshape_progress has been set - so a memory barrier needed.
7341 */
7342 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10007343 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11007344 conf->reshape_progress = raid5_size(mddev, 0, 0);
7345 else
7346 conf->reshape_progress = 0;
7347 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10007348 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007349 spin_unlock_irq(&conf->device_lock);
7350
NeilBrown4d77e3b2013-08-27 15:57:47 +10007351 /* Now make sure any requests that proceeded on the assumption
7352 * the reshape wasn't running - like Discard or Read - have
7353 * completed.
7354 */
7355 mddev_suspend(mddev);
7356 mddev_resume(mddev);
7357
NeilBrown29269552006-03-27 01:18:10 -08007358 /* Add some new drives, as many as will fit.
7359 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10007360 * Don't add devices if we are reducing the number of
7361 * devices in the array. This is because it is not possible
7362 * to correctly record the "partially reconstructed" state of
7363 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08007364 */
NeilBrown87a8dec2011-01-31 11:57:43 +11007365 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11007366 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11007367 if (rdev->raid_disk < 0 &&
7368 !test_bit(Faulty, &rdev->flags)) {
7369 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11007370 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11007371 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11007372 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11007373 else
NeilBrown87a8dec2011-01-31 11:57:43 +11007374 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10007375
7376 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11007377 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11007378 }
NeilBrown87a8dec2011-01-31 11:57:43 +11007379 } else if (rdev->raid_disk >= conf->previous_raid_disks
7380 && !test_bit(Faulty, &rdev->flags)) {
7381 /* This is a spare that was manually added */
7382 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11007383 }
NeilBrown29269552006-03-27 01:18:10 -08007384
NeilBrown87a8dec2011-01-31 11:57:43 +11007385 /* When a reshape changes the number of devices,
7386 * ->degraded is measured against the larger of the
7387 * pre and post number of devices.
7388 */
NeilBrownec32a2b2009-03-31 15:17:38 +11007389 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11007390 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11007391 spin_unlock_irqrestore(&conf->device_lock, flags);
7392 }
NeilBrown63c70c42006-03-27 01:18:13 -08007393 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10007394 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07007395 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08007396
NeilBrown29269552006-03-27 01:18:10 -08007397 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7398 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10007399 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown29269552006-03-27 01:18:10 -08007400 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7401 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7402 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007403 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08007404 if (!mddev->sync_thread) {
7405 mddev->recovery = 0;
7406 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11007407 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007408 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11007409 mddev->new_chunk_sectors =
7410 conf->chunk_sectors = conf->prev_chunk_sectors;
7411 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10007412 rdev_for_each(rdev, mddev)
7413 rdev->new_data_offset = rdev->data_offset;
7414 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11007415 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11007416 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11007417 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11007418 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08007419 spin_unlock_irq(&conf->device_lock);
7420 return -EAGAIN;
7421 }
NeilBrownc8f517c2009-03-31 15:28:40 +11007422 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08007423 md_wakeup_thread(mddev->sync_thread);
7424 md_new_event(mddev);
7425 return 0;
7426}
NeilBrown29269552006-03-27 01:18:10 -08007427
NeilBrownec32a2b2009-03-31 15:17:38 +11007428/* This is called from the reshape thread and should make any
7429 * changes needed in 'conf'
7430 */
NeilBrownd1688a62011-10-11 16:49:52 +11007431static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08007432{
NeilBrown29269552006-03-27 01:18:10 -08007433
NeilBrownf6705572006-03-27 01:18:11 -08007434 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10007435 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07007436
NeilBrownf6705572006-03-27 01:18:11 -08007437 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11007438 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10007439 rdev_for_each(rdev, conf->mddev)
7440 rdev->data_offset = rdev->new_data_offset;
7441 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11007442 conf->reshape_progress = MaxSector;
NeilBrown6cbd8142015-07-24 13:30:32 +10007443 conf->mddev->reshape_position = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08007444 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11007445 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07007446
7447 /* read-ahead size must cover two whole stripes, which is
7448 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
7449 */
NeilBrown4a5add42010-06-01 19:37:28 +10007450 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11007451 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007452 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11007453 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07007454 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
7455 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
7456 }
NeilBrown29269552006-03-27 01:18:10 -08007457 }
NeilBrown29269552006-03-27 01:18:10 -08007458}
7459
NeilBrownec32a2b2009-03-31 15:17:38 +11007460/* This is called from the raid5d thread with mddev_lock held.
7461 * It makes config changes to the device.
7462 */
NeilBrownfd01b882011-10-11 16:47:53 +11007463static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11007464{
NeilBrownd1688a62011-10-11 16:49:52 +11007465 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11007466
7467 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7468
NeilBrownec32a2b2009-03-31 15:17:38 +11007469 if (mddev->delta_disks > 0) {
7470 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
7471 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10007472 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11007473 } else {
7474 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11007475 spin_lock_irq(&conf->device_lock);
7476 mddev->degraded = calc_degraded(conf);
7477 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11007478 for (d = conf->raid_disks ;
7479 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10007480 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11007481 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10007482 if (rdev)
7483 clear_bit(In_sync, &rdev->flags);
7484 rdev = conf->disks[d].replacement;
7485 if (rdev)
7486 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10007487 }
NeilBrowncea9c222009-03-31 15:15:05 +11007488 }
NeilBrown88ce4932009-03-31 15:24:23 +11007489 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10007490 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11007491 mddev->reshape_position = MaxSector;
7492 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10007493 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11007494 }
7495}
7496
NeilBrownfd01b882011-10-11 16:47:53 +11007497static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07007498{
NeilBrownd1688a62011-10-11 16:49:52 +11007499 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07007500
7501 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08007502 case 2: /* resume for a suspend */
7503 wake_up(&conf->wait_for_overlap);
7504 break;
7505
NeilBrown72626682005-09-09 16:23:54 -07007506 case 1: /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11007507 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10007508 /* '2' tells resync/reshape to pause so that all
7509 * active stripes can drain
7510 */
7511 conf->quiesce = 2;
Yuanhan Liub1b46482015-05-08 18:19:06 +10007512 wait_event_cmd(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08007513 atomic_read(&conf->active_stripes) == 0 &&
7514 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11007515 unlock_all_device_hash_locks_irq(conf),
7516 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10007517 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11007518 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10007519 /* allow reshape to continue */
7520 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07007521 break;
7522
7523 case 0: /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11007524 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07007525 conf->quiesce = 0;
Yuanhan Liub1b46482015-05-08 18:19:06 +10007526 wake_up(&conf->wait_for_quiescent);
NeilBrowne464eaf2006-03-27 01:18:14 -08007527 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11007528 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07007529 break;
7530 }
NeilBrown72626682005-09-09 16:23:54 -07007531}
NeilBrownb15c2e52006-01-06 00:20:16 -08007532
NeilBrownfd01b882011-10-11 16:47:53 +11007533static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11007534{
NeilBrowne373ab12011-10-11 16:48:59 +11007535 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07007536 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11007537
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007538 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11007539 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10007540 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
7541 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007542 return ERR_PTR(-EINVAL);
7543 }
7544
NeilBrowne373ab12011-10-11 16:48:59 +11007545 sectors = raid0_conf->strip_zone[0].zone_end;
7546 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10007547 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007548 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11007549 mddev->new_layout = ALGORITHM_PARITY_N;
7550 mddev->new_chunk_sectors = mddev->chunk_sectors;
7551 mddev->raid_disks += 1;
7552 mddev->delta_disks = 1;
7553 /* make sure it will be not marked as dirty */
7554 mddev->recovery_cp = MaxSector;
7555
7556 return setup_conf(mddev);
7557}
7558
NeilBrownfd01b882011-10-11 16:47:53 +11007559static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11007560{
7561 int chunksect;
7562
7563 if (mddev->raid_disks != 2 ||
7564 mddev->degraded > 1)
7565 return ERR_PTR(-EINVAL);
7566
7567 /* Should check if there are write-behind devices? */
7568
7569 chunksect = 64*2; /* 64K by default */
7570
7571 /* The array must be an exact multiple of chunksize */
7572 while (chunksect && (mddev->array_sectors & (chunksect-1)))
7573 chunksect >>= 1;
7574
7575 if ((chunksect<<9) < STRIPE_SIZE)
7576 /* array size does not allow a suitable chunk size */
7577 return ERR_PTR(-EINVAL);
7578
7579 mddev->new_level = 5;
7580 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10007581 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11007582
7583 return setup_conf(mddev);
7584}
7585
NeilBrownfd01b882011-10-11 16:47:53 +11007586static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11007587{
7588 int new_layout;
7589
7590 switch (mddev->layout) {
7591 case ALGORITHM_LEFT_ASYMMETRIC_6:
7592 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
7593 break;
7594 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7595 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
7596 break;
7597 case ALGORITHM_LEFT_SYMMETRIC_6:
7598 new_layout = ALGORITHM_LEFT_SYMMETRIC;
7599 break;
7600 case ALGORITHM_RIGHT_SYMMETRIC_6:
7601 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
7602 break;
7603 case ALGORITHM_PARITY_0_6:
7604 new_layout = ALGORITHM_PARITY_0;
7605 break;
7606 case ALGORITHM_PARITY_N:
7607 new_layout = ALGORITHM_PARITY_N;
7608 break;
7609 default:
7610 return ERR_PTR(-EINVAL);
7611 }
7612 mddev->new_level = 5;
7613 mddev->new_layout = new_layout;
7614 mddev->delta_disks = -1;
7615 mddev->raid_disks -= 1;
7616 return setup_conf(mddev);
7617}
7618
NeilBrownfd01b882011-10-11 16:47:53 +11007619static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11007620{
NeilBrown88ce4932009-03-31 15:24:23 +11007621 /* For a 2-drive array, the layout and chunk size can be changed
7622 * immediately as not restriping is needed.
7623 * For larger arrays we record the new value - after validation
7624 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11007625 */
NeilBrownd1688a62011-10-11 16:49:52 +11007626 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10007627 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11007628
NeilBrown597a7112009-06-18 08:47:42 +10007629 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11007630 return -EINVAL;
7631 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10007632 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11007633 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007634 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11007635 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007636 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11007637 /* not factor of array size */
7638 return -EINVAL;
7639 }
7640
7641 /* They look valid */
7642
NeilBrown88ce4932009-03-31 15:24:23 +11007643 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10007644 /* can make the change immediately */
7645 if (mddev->new_layout >= 0) {
7646 conf->algorithm = mddev->new_layout;
7647 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11007648 }
7649 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10007650 conf->chunk_sectors = new_chunk ;
7651 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11007652 }
7653 set_bit(MD_CHANGE_DEVS, &mddev->flags);
7654 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11007655 }
NeilBrown50ac1682009-06-18 08:47:55 +10007656 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11007657}
7658
NeilBrownfd01b882011-10-11 16:47:53 +11007659static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11007660{
NeilBrown597a7112009-06-18 08:47:42 +10007661 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10007662
NeilBrown597a7112009-06-18 08:47:42 +10007663 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11007664 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11007665 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10007666 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11007667 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007668 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11007669 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10007670 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11007671 /* not factor of array size */
7672 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11007673 }
NeilBrown88ce4932009-03-31 15:24:23 +11007674
7675 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10007676 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11007677}
7678
NeilBrownfd01b882011-10-11 16:47:53 +11007679static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11007680{
7681 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007682 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11007683 * raid1 - if there are two drives. We need to know the chunk size
7684 * raid4 - trivial - just use a raid4 layout.
7685 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11007686 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007687 if (mddev->level == 0)
7688 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11007689 if (mddev->level == 1)
7690 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11007691 if (mddev->level == 4) {
7692 mddev->new_layout = ALGORITHM_PARITY_N;
7693 mddev->new_level = 5;
7694 return setup_conf(mddev);
7695 }
NeilBrownfc9739c2009-03-31 14:57:20 +11007696 if (mddev->level == 6)
7697 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11007698
7699 return ERR_PTR(-EINVAL);
7700}
7701
NeilBrownfd01b882011-10-11 16:47:53 +11007702static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11007703{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007704 /* raid4 can take over:
7705 * raid0 - if there is only one strip zone
7706 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11007707 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07007708 if (mddev->level == 0)
7709 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11007710 if (mddev->level == 5 &&
7711 mddev->layout == ALGORITHM_PARITY_N) {
7712 mddev->new_layout = 0;
7713 mddev->new_level = 4;
7714 return setup_conf(mddev);
7715 }
7716 return ERR_PTR(-EINVAL);
7717}
NeilBrownd562b0c2009-03-31 14:39:39 +11007718
NeilBrown84fc4b52011-10-11 16:49:58 +11007719static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11007720
NeilBrownfd01b882011-10-11 16:47:53 +11007721static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11007722{
7723 /* Currently can only take over a raid5. We map the
7724 * personality to an equivalent raid6 personality
7725 * with the Q block at the end.
7726 */
7727 int new_layout;
7728
7729 if (mddev->pers != &raid5_personality)
7730 return ERR_PTR(-EINVAL);
7731 if (mddev->degraded > 1)
7732 return ERR_PTR(-EINVAL);
7733 if (mddev->raid_disks > 253)
7734 return ERR_PTR(-EINVAL);
7735 if (mddev->raid_disks < 3)
7736 return ERR_PTR(-EINVAL);
7737
7738 switch (mddev->layout) {
7739 case ALGORITHM_LEFT_ASYMMETRIC:
7740 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
7741 break;
7742 case ALGORITHM_RIGHT_ASYMMETRIC:
7743 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
7744 break;
7745 case ALGORITHM_LEFT_SYMMETRIC:
7746 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
7747 break;
7748 case ALGORITHM_RIGHT_SYMMETRIC:
7749 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
7750 break;
7751 case ALGORITHM_PARITY_0:
7752 new_layout = ALGORITHM_PARITY_0_6;
7753 break;
7754 case ALGORITHM_PARITY_N:
7755 new_layout = ALGORITHM_PARITY_N;
7756 break;
7757 default:
7758 return ERR_PTR(-EINVAL);
7759 }
7760 mddev->new_level = 6;
7761 mddev->new_layout = new_layout;
7762 mddev->delta_disks = 1;
7763 mddev->raid_disks += 1;
7764 return setup_conf(mddev);
7765}
7766
NeilBrown84fc4b52011-10-11 16:49:58 +11007767static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07007768{
7769 .name = "raid6",
7770 .level = 6,
7771 .owner = THIS_MODULE,
7772 .make_request = make_request,
7773 .run = run,
NeilBrownafa0f552014-12-15 12:56:58 +11007774 .free = raid5_free,
NeilBrown16a53ec2006-06-26 00:27:38 -07007775 .status = status,
7776 .error_handler = error,
7777 .hot_add_disk = raid5_add_disk,
7778 .hot_remove_disk= raid5_remove_disk,
7779 .spare_active = raid5_spare_active,
7780 .sync_request = sync_request,
7781 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07007782 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10007783 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08007784 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007785 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07007786 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11007787 .takeover = raid6_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11007788 .congested = raid5_congested,
NeilBrown16a53ec2006-06-26 00:27:38 -07007789};
NeilBrown84fc4b52011-10-11 16:49:58 +11007790static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007791{
7792 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08007793 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007794 .owner = THIS_MODULE,
7795 .make_request = make_request,
7796 .run = run,
NeilBrownafa0f552014-12-15 12:56:58 +11007797 .free = raid5_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007798 .status = status,
7799 .error_handler = error,
7800 .hot_add_disk = raid5_add_disk,
7801 .hot_remove_disk= raid5_remove_disk,
7802 .spare_active = raid5_spare_active,
7803 .sync_request = sync_request,
7804 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07007805 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08007806 .check_reshape = raid5_check_reshape,
7807 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007808 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07007809 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11007810 .takeover = raid5_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11007811 .congested = raid5_congested,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007812};
7813
NeilBrown84fc4b52011-10-11 16:49:58 +11007814static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07007815{
NeilBrown2604b702006-01-06 00:20:36 -08007816 .name = "raid4",
7817 .level = 4,
7818 .owner = THIS_MODULE,
7819 .make_request = make_request,
7820 .run = run,
NeilBrownafa0f552014-12-15 12:56:58 +11007821 .free = raid5_free,
NeilBrown2604b702006-01-06 00:20:36 -08007822 .status = status,
7823 .error_handler = error,
7824 .hot_add_disk = raid5_add_disk,
7825 .hot_remove_disk= raid5_remove_disk,
7826 .spare_active = raid5_spare_active,
7827 .sync_request = sync_request,
7828 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07007829 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08007830 .check_reshape = raid5_check_reshape,
7831 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11007832 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08007833 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11007834 .takeover = raid4_takeover,
NeilBrown5c675f82014-12-15 12:56:56 +11007835 .congested = raid5_congested,
NeilBrown2604b702006-01-06 00:20:36 -08007836};
7837
7838static int __init raid5_init(void)
7839{
Shaohua Li851c30c2013-08-28 14:30:16 +08007840 raid5_wq = alloc_workqueue("raid5wq",
7841 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
7842 if (!raid5_wq)
7843 return -ENOMEM;
NeilBrown16a53ec2006-06-26 00:27:38 -07007844 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08007845 register_md_personality(&raid5_personality);
7846 register_md_personality(&raid4_personality);
7847 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007848}
7849
NeilBrown2604b702006-01-06 00:20:36 -08007850static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007851{
NeilBrown16a53ec2006-06-26 00:27:38 -07007852 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08007853 unregister_md_personality(&raid5_personality);
7854 unregister_md_personality(&raid4_personality);
Shaohua Li851c30c2013-08-28 14:30:16 +08007855 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007856}
7857
7858module_init(raid5_init);
7859module_exit(raid5_exit);
7860MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007861MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007862MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08007863MODULE_ALIAS("md-raid5");
7864MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08007865MODULE_ALIAS("md-level-5");
7866MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07007867MODULE_ALIAS("md-personality-8"); /* RAID6 */
7868MODULE_ALIAS("md-raid6");
7869MODULE_ALIAS("md-level-6");
7870
7871/* This used to be two separate modules, they were: */
7872MODULE_ALIAS("raid5");
7873MODULE_ALIAS("raid6");