blob: 4daf5c03b33bfdc626d53e6015ccb7c565060117 [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>
NeilBrowna9add5d2012-10-31 11:59:09 +110056#include <trace/events/block.h>
57
NeilBrown43b2e5d2009-03-31 14:33:13 +110058#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110059#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110060#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110061#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070062
NeilBrown06905ff2014-10-02 13:45:00 +100063static bool devices_handle_discard_safely = false;
64module_param(devices_handle_discard_safely, bool, 0644);
65MODULE_PARM_DESC(devices_handle_discard_safely,
66 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
68 * Stripe cache
69 */
70
71#define NR_STRIPES 256
72#define STRIPE_SIZE PAGE_SIZE
73#define STRIPE_SHIFT (PAGE_SHIFT - 9)
74#define STRIPE_SECTORS (STRIPE_SIZE>>9)
75#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070076#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080077#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define HASH_MASK (NR_HASH - 1)
79
NeilBrownd1688a62011-10-11 16:49:52 +110080static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110081{
82 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
83 return &conf->stripe_hashtbl[hash];
84}
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/* bio's attached to a stripe+device for I/O are linked together in bi_sector
87 * order without overlap. There may be several bio's per stripe+device, and
88 * a bio could span several devices.
89 * When walking this list for a particular stripe+device, we must never proceed
90 * beyond a bio that extends past this device, as the next bio might no longer
91 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110092 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * of the current stripe+device
94 */
NeilBrowndb298e12011-10-07 14:23:00 +110095static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
96{
Kent Overstreetaa8b57a2013-02-05 15:19:29 -080097 int sectors = bio_sectors(bio);
NeilBrowndb298e12011-10-07 14:23:00 +110098 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
99 return bio->bi_next;
100 else
101 return NULL;
102}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Jens Axboe960e7392008-08-15 10:41:18 +0200104/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200105 * We maintain a biased count of active stripes in the bottom 16 bits of
106 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200107 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000108static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200109{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000110 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
111 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200112}
113
Shaohua Lie7836bd62012-07-19 16:01:31 +1000114static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200115{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000116 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
117 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200118}
119
Shaohua Lie7836bd62012-07-19 16:01:31 +1000120static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200121{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000122 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
123 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200124}
125
Shaohua Lie7836bd62012-07-19 16:01:31 +1000126static inline void raid5_set_bi_processed_stripes(struct bio *bio,
127 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200128{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000129 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
130 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200131
Shaohua Lie7836bd62012-07-19 16:01:31 +1000132 do {
133 old = atomic_read(segments);
134 new = (old & 0xffff) | (cnt << 16);
135 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200136}
137
Shaohua Lie7836bd62012-07-19 16:01:31 +1000138static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200139{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000140 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
141 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200142}
143
NeilBrownd0dabf72009-03-31 14:39:38 +1100144/* Find first data disk in a raid6 stripe */
145static inline int raid6_d0(struct stripe_head *sh)
146{
NeilBrown67cc2b82009-03-31 14:39:38 +1100147 if (sh->ddf_layout)
148 /* ddf always start from first device */
149 return 0;
150 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100151 if (sh->qd_idx == sh->disks - 1)
152 return 0;
153 else
154 return sh->qd_idx + 1;
155}
NeilBrown16a53ec2006-06-26 00:27:38 -0700156static inline int raid6_next_disk(int disk, int raid_disks)
157{
158 disk++;
159 return (disk < raid_disks) ? disk : 0;
160}
Dan Williamsa4456852007-07-09 11:56:43 -0700161
NeilBrownd0dabf72009-03-31 14:39:38 +1100162/* When walking through the disks in a raid5, starting at raid6_d0,
163 * We need to map each disk to a 'slot', where the data disks are slot
164 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
165 * is raid_disks-1. This help does that mapping.
166 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100167static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
168 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100169{
Dan Williams66295422009-10-19 18:09:32 -0700170 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100171
NeilBrowne4424fe2009-10-16 16:27:34 +1100172 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700173 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100174 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100175 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100176 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100177 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100178 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700179 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100180 return slot;
181}
182
Dan Williamsa4456852007-07-09 11:56:43 -0700183static void return_io(struct bio *return_bi)
184{
185 struct bio *bi = return_bi;
186 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700187
188 return_bi = bi->bi_next;
189 bi->bi_next = NULL;
190 bi->bi_size = 0;
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700191 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
192 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +1000193 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700194 bi = return_bi;
195 }
196}
197
NeilBrownd1688a62011-10-11 16:49:52 +1100198static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Dan Williams600aa102008-06-28 08:32:05 +1000200static int stripe_operations_active(struct stripe_head *sh)
201{
202 return sh->check_state || sh->reconstruct_state ||
203 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
204 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
205}
206
Shaohua Li4eb788d2012-07-19 16:01:31 +1000207static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000209 BUG_ON(!list_empty(&sh->lru));
210 BUG_ON(atomic_read(&conf->active_stripes)==0);
211 if (test_bit(STRIPE_HANDLE, &sh->state)) {
212 if (test_bit(STRIPE_DELAYED, &sh->state) &&
213 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
214 list_add_tail(&sh->lru, &conf->delayed_list);
215 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
216 sh->bm_seq - conf->seq_write > 0)
217 list_add_tail(&sh->lru, &conf->bitmap_list);
218 else {
219 clear_bit(STRIPE_DELAYED, &sh->state);
220 clear_bit(STRIPE_BIT_DELAY, &sh->state);
221 list_add_tail(&sh->lru, &conf->handle_list);
222 }
223 md_wakeup_thread(conf->mddev->thread);
224 } else {
225 BUG_ON(stripe_operations_active(sh));
226 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
227 if (atomic_dec_return(&conf->preread_active_stripes)
228 < IO_THRESHOLD)
229 md_wakeup_thread(conf->mddev->thread);
230 atomic_dec(&conf->active_stripes);
231 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
232 list_add_tail(&sh->lru, &conf->inactive_list);
233 wake_up(&conf->wait_for_stripe);
234 if (conf->retry_read_aligned)
235 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237 }
238}
NeilBrownd0dabf72009-03-31 14:39:38 +1100239
Shaohua Li4eb788d2012-07-19 16:01:31 +1000240static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
241{
242 if (atomic_dec_and_test(&sh->count))
243 do_release_stripe(conf, sh);
244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246static void release_stripe(struct stripe_head *sh)
247{
NeilBrownd1688a62011-10-11 16:49:52 +1100248 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700250
Shaohua Li4eb788d2012-07-19 16:01:31 +1000251 local_irq_save(flags);
252 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
253 do_release_stripe(conf, sh);
254 spin_unlock(&conf->device_lock);
255 }
256 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
NeilBrownfccddba2006-01-06 00:20:33 -0800259static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Dan Williams45b42332007-07-09 11:56:43 -0700261 pr_debug("remove_hash(), stripe %llu\n",
262 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
NeilBrownfccddba2006-01-06 00:20:33 -0800264 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
NeilBrownd1688a62011-10-11 16:49:52 +1100267static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
NeilBrownfccddba2006-01-06 00:20:33 -0800269 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Dan Williams45b42332007-07-09 11:56:43 -0700271 pr_debug("insert_hash(), stripe %llu\n",
272 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
NeilBrownfccddba2006-01-06 00:20:33 -0800274 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
277
278/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100279static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct stripe_head *sh = NULL;
282 struct list_head *first;
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (list_empty(&conf->inactive_list))
285 goto out;
286 first = conf->inactive_list.next;
287 sh = list_entry(first, struct stripe_head, lru);
288 list_del_init(first);
289 remove_hash(sh);
290 atomic_inc(&conf->active_stripes);
291out:
292 return sh;
293}
294
NeilBrowne4e11e32010-06-16 16:45:16 +1000295static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 struct page *p;
298 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000299 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
NeilBrowne4e11e32010-06-16 16:45:16 +1000301 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 p = sh->dev[i].page;
303 if (!p)
304 continue;
305 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800306 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308}
309
NeilBrowne4e11e32010-06-16 16:45:16 +1000310static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000313 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
NeilBrowne4e11e32010-06-16 16:45:16 +1000315 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 struct page *page;
317
318 if (!(page = alloc_page(GFP_KERNEL))) {
319 return 1;
320 }
321 sh->dev[i].page = page;
322 }
323 return 0;
324}
325
NeilBrown784052e2009-03-31 15:19:07 +1100326static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100327static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100328 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
NeilBrownb5663ba2009-03-31 14:39:38 +1100330static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
NeilBrownd1688a62011-10-11 16:49:52 +1100332 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800333 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200335 BUG_ON(atomic_read(&sh->count) != 0);
336 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000337 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700338
Dan Williams45b42332007-07-09 11:56:43 -0700339 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 (unsigned long long)sh->sector);
341
342 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700343
NeilBrown86b42c72009-03-31 15:19:03 +1100344 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100345 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100347 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 sh->state = 0;
349
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800350
351 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct r5dev *dev = &sh->dev[i];
353
Dan Williamsd84e0f12007-01-02 13:52:30 -0700354 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700356 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700358 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000360 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100363 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365 insert_hash(conf, sh);
366}
367
NeilBrownd1688a62011-10-11 16:49:52 +1100368static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100369 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 struct stripe_head *sh;
372
Dan Williams45b42332007-07-09 11:56:43 -0700373 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800374 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100375 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700377 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return NULL;
379}
380
NeilBrown674806d2010-06-16 17:17:53 +1000381/*
382 * Need to check if array has failed when deciding whether to:
383 * - start an array
384 * - remove non-faulty devices
385 * - add a spare
386 * - allow a reshape
387 * This determination is simple when no reshape is happening.
388 * However if there is a reshape, we need to carefully check
389 * both the before and after sections.
390 * This is because some failed devices may only affect one
391 * of the two sections, and some non-in_sync devices may
392 * be insync in the section most affected by failed devices.
393 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100394static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000395{
NeilBrown908f4fb2011-12-23 10:17:50 +1100396 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000397 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000398
399 rcu_read_lock();
400 degraded = 0;
401 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100402 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000403 if (rdev && test_bit(Faulty, &rdev->flags))
404 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000405 if (!rdev || test_bit(Faulty, &rdev->flags))
406 degraded++;
407 else if (test_bit(In_sync, &rdev->flags))
408 ;
409 else
410 /* not in-sync or faulty.
411 * If the reshape increases the number of devices,
412 * this is being recovered by the reshape, so
413 * this 'previous' section is not in_sync.
414 * If the number of devices is being reduced however,
415 * the device can only be part of the array if
416 * we are reverting a reshape, so this section will
417 * be in-sync.
418 */
419 if (conf->raid_disks >= conf->previous_raid_disks)
420 degraded++;
421 }
422 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100423 if (conf->raid_disks == conf->previous_raid_disks)
424 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000425 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100426 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000427 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100428 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000429 if (rdev && test_bit(Faulty, &rdev->flags))
430 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000431 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100432 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000433 else if (test_bit(In_sync, &rdev->flags))
434 ;
435 else
436 /* not in-sync or faulty.
437 * If reshape increases the number of devices, this
438 * section has already been recovered, else it
439 * almost certainly hasn't.
440 */
441 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100442 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000443 }
444 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100445 if (degraded2 > degraded)
446 return degraded2;
447 return degraded;
448}
449
450static int has_failed(struct r5conf *conf)
451{
452 int degraded;
453
454 if (conf->mddev->reshape_position == MaxSector)
455 return conf->mddev->degraded > conf->max_degraded;
456
457 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000458 if (degraded > conf->max_degraded)
459 return 1;
460 return 0;
461}
462
NeilBrownb5663ba2009-03-31 14:39:38 +1100463static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100464get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000465 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct stripe_head *sh;
468
Dan Williams45b42332007-07-09 11:56:43 -0700469 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 spin_lock_irq(&conf->device_lock);
472
473 do {
NeilBrown72626682005-09-09 16:23:54 -0700474 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000475 conf->quiesce == 0 || noquiesce,
Lukas Czernereed8c022012-11-30 11:42:40 +0100476 conf->device_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100477 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (!sh) {
479 if (!conf->inactive_blocked)
480 sh = get_free_stripe(conf);
481 if (noblock && sh == NULL)
482 break;
483 if (!sh) {
484 conf->inactive_blocked = 1;
485 wait_event_lock_irq(conf->wait_for_stripe,
486 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800487 (atomic_read(&conf->active_stripes)
488 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 || !conf->inactive_blocked),
Lukas Czernereed8c022012-11-30 11:42:40 +0100490 conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 conf->inactive_blocked = 0;
492 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100493 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 } else {
495 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100496 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000497 && !test_bit(STRIPE_EXPANDING, &sh->state)
498 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 } else {
500 if (!test_bit(STRIPE_HANDLE, &sh->state))
501 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700502 if (list_empty(&sh->lru) &&
503 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700504 BUG();
505 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507 }
508 } while (sh == NULL);
509
510 if (sh)
511 atomic_inc(&sh->count);
512
513 spin_unlock_irq(&conf->device_lock);
514 return sh;
515}
516
NeilBrown05616be2012-05-21 09:27:00 +1000517/* Determine if 'data_offset' or 'new_data_offset' should be used
518 * in this stripe_head.
519 */
520static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
521{
522 sector_t progress = conf->reshape_progress;
523 /* Need a memory barrier to make sure we see the value
524 * of conf->generation, or ->data_offset that was set before
525 * reshape_progress was updated.
526 */
527 smp_rmb();
528 if (progress == MaxSector)
529 return 0;
530 if (sh->generation == conf->generation - 1)
531 return 0;
532 /* We are in a reshape, and this is a new-generation stripe,
533 * so use new_data_offset.
534 */
535 return 1;
536}
537
NeilBrown6712ecf2007-09-27 12:47:43 +0200538static void
539raid5_end_read_request(struct bio *bi, int error);
540static void
541raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700542
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000543static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700544{
NeilBrownd1688a62011-10-11 16:49:52 +1100545 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700546 int i, disks = sh->disks;
547
548 might_sleep();
549
550 for (i = disks; i--; ) {
551 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100552 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100553 struct bio *bi, *rbi;
554 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200555 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
556 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
557 rw = WRITE_FUA;
558 else
559 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100560 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100561 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200562 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700563 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100564 else if (test_and_clear_bit(R5_WantReplace,
565 &sh->dev[i].flags)) {
566 rw = WRITE;
567 replace_only = 1;
568 } else
Dan Williams91c00922007-01-02 13:52:30 -0700569 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000570 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
571 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700572
573 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100574 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700575
Dan Williams91c00922007-01-02 13:52:30 -0700576 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100577 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100578 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
579 rdev = rcu_dereference(conf->disks[i].rdev);
580 if (!rdev) {
581 rdev = rrdev;
582 rrdev = NULL;
583 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100584 if (rw & WRITE) {
585 if (replace_only)
586 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100587 if (rdev == rrdev)
588 /* We raced and saw duplicates */
589 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100590 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100591 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100592 rdev = rrdev;
593 rrdev = NULL;
594 }
NeilBrown977df362011-12-23 10:17:53 +1100595
Dan Williams91c00922007-01-02 13:52:30 -0700596 if (rdev && test_bit(Faulty, &rdev->flags))
597 rdev = NULL;
598 if (rdev)
599 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100600 if (rrdev && test_bit(Faulty, &rrdev->flags))
601 rrdev = NULL;
602 if (rrdev)
603 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700604 rcu_read_unlock();
605
NeilBrown73e92e52011-07-28 11:39:22 +1000606 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100607 * need to check for writes. We never accept write errors
608 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000609 */
610 while ((rw & WRITE) && rdev &&
611 test_bit(WriteErrorSeen, &rdev->flags)) {
612 sector_t first_bad;
613 int bad_sectors;
614 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
615 &first_bad, &bad_sectors);
616 if (!bad)
617 break;
618
619 if (bad < 0) {
620 set_bit(BlockedBadBlocks, &rdev->flags);
621 if (!conf->mddev->external &&
622 conf->mddev->flags) {
623 /* It is very unlikely, but we might
624 * still need to write out the
625 * bad block log - better give it
626 * a chance*/
627 md_check_recovery(conf->mddev);
628 }
majianpeng18507532012-07-03 12:11:54 +1000629 /*
630 * Because md_wait_for_blocked_rdev
631 * will dec nr_pending, we must
632 * increment it first.
633 */
634 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000635 md_wait_for_blocked_rdev(rdev, conf->mddev);
636 } else {
637 /* Acknowledged bad block - skip the write */
638 rdev_dec_pending(rdev, conf->mddev);
639 rdev = NULL;
640 }
641 }
642
Dan Williams91c00922007-01-02 13:52:30 -0700643 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100644 if (s->syncing || s->expanding || s->expanded
645 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700646 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
647
Dan Williams2b7497f2008-06-28 08:31:52 +1000648 set_bit(STRIPE_IO_STARTED, &sh->state);
649
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700650 bio_reset(bi);
Dan Williams91c00922007-01-02 13:52:30 -0700651 bi->bi_bdev = rdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700652 bi->bi_rw = rw;
653 bi->bi_end_io = (rw & WRITE)
654 ? raid5_end_write_request
655 : raid5_end_read_request;
656 bi->bi_private = sh;
657
Dan Williams91c00922007-01-02 13:52:30 -0700658 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700659 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700660 bi->bi_rw, i);
661 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000662 if (use_new_offset(conf, sh))
663 bi->bi_sector = (sh->sector
664 + rdev->new_data_offset);
665 else
666 bi->bi_sector = (sh->sector
667 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000668 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
669 bi->bi_rw |= REQ_FLUSH;
670
Kent Overstreet4997b722013-05-30 08:44:39 +0200671 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -0700672 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
673 bi->bi_io_vec[0].bv_offset = 0;
674 bi->bi_size = STRIPE_SIZE;
Shaohua Li7e44a922013-10-19 14:50:28 +0800675 /*
676 * If this is discard request, set bi_vcnt 0. We don't
677 * want to confuse SCSI because SCSI will replace payload
678 */
679 if (rw & REQ_DISCARD)
680 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +1100681 if (rrdev)
682 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600683
684 if (conf->mddev->gendisk)
685 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
686 bi, disk_devt(conf->mddev->gendisk),
687 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700688 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100689 }
690 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100691 if (s->syncing || s->expanding || s->expanded
692 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100693 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
694
695 set_bit(STRIPE_IO_STARTED, &sh->state);
696
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700697 bio_reset(rbi);
NeilBrown977df362011-12-23 10:17:53 +1100698 rbi->bi_bdev = rrdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700699 rbi->bi_rw = rw;
700 BUG_ON(!(rw & WRITE));
701 rbi->bi_end_io = raid5_end_write_request;
702 rbi->bi_private = sh;
703
NeilBrown977df362011-12-23 10:17:53 +1100704 pr_debug("%s: for %llu schedule op %ld on "
705 "replacement disc %d\n",
706 __func__, (unsigned long long)sh->sector,
707 rbi->bi_rw, i);
708 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000709 if (use_new_offset(conf, sh))
710 rbi->bi_sector = (sh->sector
711 + rrdev->new_data_offset);
712 else
713 rbi->bi_sector = (sh->sector
714 + rrdev->data_offset);
Kent Overstreet4997b722013-05-30 08:44:39 +0200715 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +1100716 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
717 rbi->bi_io_vec[0].bv_offset = 0;
718 rbi->bi_size = STRIPE_SIZE;
Shaohua Li7e44a922013-10-19 14:50:28 +0800719 /*
720 * If this is discard request, set bi_vcnt 0. We don't
721 * want to confuse SCSI because SCSI will replace payload
722 */
723 if (rw & REQ_DISCARD)
724 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600725 if (conf->mddev->gendisk)
726 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
727 rbi, disk_devt(conf->mddev->gendisk),
728 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100729 generic_make_request(rbi);
730 }
731 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000732 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700733 set_bit(STRIPE_DEGRADED, &sh->state);
734 pr_debug("skip op %ld on disc %d for sector %llu\n",
735 bi->bi_rw, i, (unsigned long long)sh->sector);
736 clear_bit(R5_LOCKED, &sh->dev[i].flags);
737 set_bit(STRIPE_HANDLE, &sh->state);
738 }
739 }
740}
741
742static struct dma_async_tx_descriptor *
743async_copy_data(int frombio, struct bio *bio, struct page *page,
744 sector_t sector, struct dma_async_tx_descriptor *tx)
745{
746 struct bio_vec *bvl;
747 struct page *bio_page;
748 int i;
749 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700750 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700751 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700752
753 if (bio->bi_sector >= sector)
754 page_offset = (signed)(bio->bi_sector - sector) * 512;
755 else
756 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700757
Dan Williams0403e382009-09-08 17:42:50 -0700758 if (frombio)
759 flags |= ASYNC_TX_FENCE;
760 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
761
Dan Williams91c00922007-01-02 13:52:30 -0700762 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000763 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700764 int clen;
765 int b_offset = 0;
766
767 if (page_offset < 0) {
768 b_offset = -page_offset;
769 page_offset += b_offset;
770 len -= b_offset;
771 }
772
773 if (len > 0 && page_offset + len > STRIPE_SIZE)
774 clen = STRIPE_SIZE - page_offset;
775 else
776 clen = len;
777
778 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000779 b_offset += bvl->bv_offset;
780 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700781 if (frombio)
782 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700783 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700784 else
785 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700786 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700787 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700788 /* chain the operations */
789 submit.depend_tx = tx;
790
Dan Williams91c00922007-01-02 13:52:30 -0700791 if (clen < len) /* hit end of page */
792 break;
793 page_offset += len;
794 }
795
796 return tx;
797}
798
799static void ops_complete_biofill(void *stripe_head_ref)
800{
801 struct stripe_head *sh = stripe_head_ref;
802 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700803 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700804
Harvey Harrisone46b2722008-04-28 02:15:50 -0700805 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700806 (unsigned long long)sh->sector);
807
808 /* clear completed biofills */
809 for (i = sh->disks; i--; ) {
810 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700811
812 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700813 /* and check if we need to reply to a read request,
814 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000815 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700816 */
817 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700818 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700819
Dan Williams91c00922007-01-02 13:52:30 -0700820 BUG_ON(!dev->read);
821 rbi = dev->read;
822 dev->read = NULL;
823 while (rbi && rbi->bi_sector <
824 dev->sector + STRIPE_SECTORS) {
825 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000826 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700827 rbi->bi_next = return_bi;
828 return_bi = rbi;
829 }
Dan Williams91c00922007-01-02 13:52:30 -0700830 rbi = rbi2;
831 }
832 }
833 }
Dan Williams83de75c2008-06-28 08:31:58 +1000834 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700835
836 return_io(return_bi);
837
Dan Williamse4d84902007-09-24 10:06:13 -0700838 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700839 release_stripe(sh);
840}
841
842static void ops_run_biofill(struct stripe_head *sh)
843{
844 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700845 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700846 int i;
847
Harvey Harrisone46b2722008-04-28 02:15:50 -0700848 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700849 (unsigned long long)sh->sector);
850
851 for (i = sh->disks; i--; ) {
852 struct r5dev *dev = &sh->dev[i];
853 if (test_bit(R5_Wantfill, &dev->flags)) {
854 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000855 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700856 dev->read = rbi = dev->toread;
857 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000858 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700859 while (rbi && rbi->bi_sector <
860 dev->sector + STRIPE_SECTORS) {
861 tx = async_copy_data(0, rbi, dev->page,
862 dev->sector, tx);
863 rbi = r5_next_bio(rbi, dev->sector);
864 }
865 }
866 }
867
868 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700869 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
870 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700871}
872
Dan Williams4e7d2c02009-08-29 19:13:11 -0700873static void mark_target_uptodate(struct stripe_head *sh, int target)
874{
875 struct r5dev *tgt;
876
877 if (target < 0)
878 return;
879
880 tgt = &sh->dev[target];
881 set_bit(R5_UPTODATE, &tgt->flags);
882 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
883 clear_bit(R5_Wantcompute, &tgt->flags);
884}
885
Dan Williamsac6b53b2009-07-14 13:40:19 -0700886static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700887{
888 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700889
Harvey Harrisone46b2722008-04-28 02:15:50 -0700890 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700891 (unsigned long long)sh->sector);
892
Dan Williamsac6b53b2009-07-14 13:40:19 -0700893 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700894 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700895 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700896
Dan Williamsecc65c92008-06-28 08:31:57 +1000897 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
898 if (sh->check_state == check_state_compute_run)
899 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700900 set_bit(STRIPE_HANDLE, &sh->state);
901 release_stripe(sh);
902}
903
Dan Williamsd6f38f32009-07-14 11:50:52 -0700904/* return a pointer to the address conversion region of the scribble buffer */
905static addr_conv_t *to_addr_conv(struct stripe_head *sh,
906 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700907{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700908 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
909}
910
911static struct dma_async_tx_descriptor *
912ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
913{
Dan Williams91c00922007-01-02 13:52:30 -0700914 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700915 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700916 int target = sh->ops.target;
917 struct r5dev *tgt = &sh->dev[target];
918 struct page *xor_dest = tgt->page;
919 int count = 0;
920 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700921 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700922 int i;
923
924 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700925 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700926 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
927
928 for (i = disks; i--; )
929 if (i != target)
930 xor_srcs[count++] = sh->dev[i].page;
931
932 atomic_inc(&sh->count);
933
Dan Williams0403e382009-09-08 17:42:50 -0700934 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700935 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700936 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700937 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700938 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700939 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700940
Dan Williams91c00922007-01-02 13:52:30 -0700941 return tx;
942}
943
Dan Williamsac6b53b2009-07-14 13:40:19 -0700944/* set_syndrome_sources - populate source buffers for gen_syndrome
945 * @srcs - (struct page *) array of size sh->disks
946 * @sh - stripe_head to parse
947 *
948 * Populates srcs in proper layout order for the stripe and returns the
949 * 'count' of sources to be used in a call to async_gen_syndrome. The P
950 * destination buffer is recorded in srcs[count] and the Q destination
951 * is recorded in srcs[count+1]].
952 */
953static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
954{
955 int disks = sh->disks;
956 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
957 int d0_idx = raid6_d0(sh);
958 int count;
959 int i;
960
961 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100962 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700963
964 count = 0;
965 i = d0_idx;
966 do {
967 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
968
969 srcs[slot] = sh->dev[i].page;
970 i = raid6_next_disk(i, disks);
971 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700972
NeilBrowne4424fe2009-10-16 16:27:34 +1100973 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700974}
975
976static struct dma_async_tx_descriptor *
977ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
978{
979 int disks = sh->disks;
980 struct page **blocks = percpu->scribble;
981 int target;
982 int qd_idx = sh->qd_idx;
983 struct dma_async_tx_descriptor *tx;
984 struct async_submit_ctl submit;
985 struct r5dev *tgt;
986 struct page *dest;
987 int i;
988 int count;
989
990 if (sh->ops.target < 0)
991 target = sh->ops.target2;
992 else if (sh->ops.target2 < 0)
993 target = sh->ops.target;
994 else
995 /* we should only have one valid target */
996 BUG();
997 BUG_ON(target < 0);
998 pr_debug("%s: stripe %llu block: %d\n",
999 __func__, (unsigned long long)sh->sector, target);
1000
1001 tgt = &sh->dev[target];
1002 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1003 dest = tgt->page;
1004
1005 atomic_inc(&sh->count);
1006
1007 if (target == qd_idx) {
1008 count = set_syndrome_sources(blocks, sh);
1009 blocks[count] = NULL; /* regenerating p is not necessary */
1010 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001011 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1012 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001013 to_addr_conv(sh, percpu));
1014 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1015 } else {
1016 /* Compute any data- or p-drive using XOR */
1017 count = 0;
1018 for (i = disks; i-- ; ) {
1019 if (i == target || i == qd_idx)
1020 continue;
1021 blocks[count++] = sh->dev[i].page;
1022 }
1023
Dan Williams0403e382009-09-08 17:42:50 -07001024 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1025 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001026 to_addr_conv(sh, percpu));
1027 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1028 }
1029
1030 return tx;
1031}
1032
1033static struct dma_async_tx_descriptor *
1034ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1035{
1036 int i, count, disks = sh->disks;
1037 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1038 int d0_idx = raid6_d0(sh);
1039 int faila = -1, failb = -1;
1040 int target = sh->ops.target;
1041 int target2 = sh->ops.target2;
1042 struct r5dev *tgt = &sh->dev[target];
1043 struct r5dev *tgt2 = &sh->dev[target2];
1044 struct dma_async_tx_descriptor *tx;
1045 struct page **blocks = percpu->scribble;
1046 struct async_submit_ctl submit;
1047
1048 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1049 __func__, (unsigned long long)sh->sector, target, target2);
1050 BUG_ON(target < 0 || target2 < 0);
1051 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1052 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1053
Dan Williams6c910a72009-09-16 12:24:54 -07001054 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001055 * slot number conversion for 'faila' and 'failb'
1056 */
1057 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001058 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001059 count = 0;
1060 i = d0_idx;
1061 do {
1062 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1063
1064 blocks[slot] = sh->dev[i].page;
1065
1066 if (i == target)
1067 faila = slot;
1068 if (i == target2)
1069 failb = slot;
1070 i = raid6_next_disk(i, disks);
1071 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001072
1073 BUG_ON(faila == failb);
1074 if (failb < faila)
1075 swap(faila, failb);
1076 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1077 __func__, (unsigned long long)sh->sector, faila, failb);
1078
1079 atomic_inc(&sh->count);
1080
1081 if (failb == syndrome_disks+1) {
1082 /* Q disk is one of the missing disks */
1083 if (faila == syndrome_disks) {
1084 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001085 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1086 ops_complete_compute, sh,
1087 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001088 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001089 STRIPE_SIZE, &submit);
1090 } else {
1091 struct page *dest;
1092 int data_target;
1093 int qd_idx = sh->qd_idx;
1094
1095 /* Missing D+Q: recompute D from P, then recompute Q */
1096 if (target == qd_idx)
1097 data_target = target2;
1098 else
1099 data_target = target;
1100
1101 count = 0;
1102 for (i = disks; i-- ; ) {
1103 if (i == data_target || i == qd_idx)
1104 continue;
1105 blocks[count++] = sh->dev[i].page;
1106 }
1107 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001108 init_async_submit(&submit,
1109 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1110 NULL, NULL, NULL,
1111 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001112 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1113 &submit);
1114
1115 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001116 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1117 ops_complete_compute, sh,
1118 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001119 return async_gen_syndrome(blocks, 0, count+2,
1120 STRIPE_SIZE, &submit);
1121 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001122 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001123 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1124 ops_complete_compute, sh,
1125 to_addr_conv(sh, percpu));
1126 if (failb == syndrome_disks) {
1127 /* We're missing D+P. */
1128 return async_raid6_datap_recov(syndrome_disks+2,
1129 STRIPE_SIZE, faila,
1130 blocks, &submit);
1131 } else {
1132 /* We're missing D+D. */
1133 return async_raid6_2data_recov(syndrome_disks+2,
1134 STRIPE_SIZE, faila, failb,
1135 blocks, &submit);
1136 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001137 }
1138}
1139
1140
Dan Williams91c00922007-01-02 13:52:30 -07001141static void ops_complete_prexor(void *stripe_head_ref)
1142{
1143 struct stripe_head *sh = stripe_head_ref;
1144
Harvey Harrisone46b2722008-04-28 02:15:50 -07001145 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001146 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001147}
1148
1149static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001150ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1151 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001152{
Dan Williams91c00922007-01-02 13:52:30 -07001153 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001154 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001155 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001156 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001157
1158 /* existing parity data subtracted */
1159 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1160
Harvey Harrisone46b2722008-04-28 02:15:50 -07001161 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001162 (unsigned long long)sh->sector);
1163
1164 for (i = disks; i--; ) {
1165 struct r5dev *dev = &sh->dev[i];
1166 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001167 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001168 xor_srcs[count++] = dev->page;
1169 }
1170
Dan Williams0403e382009-09-08 17:42:50 -07001171 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001172 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001173 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001174
1175 return tx;
1176}
1177
1178static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001179ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001180{
1181 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001182 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001183
Harvey Harrisone46b2722008-04-28 02:15:50 -07001184 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001185 (unsigned long long)sh->sector);
1186
1187 for (i = disks; i--; ) {
1188 struct r5dev *dev = &sh->dev[i];
1189 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001190
Dan Williamsd8ee0722008-06-28 08:32:06 +10001191 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001192 struct bio *wbi;
1193
Shaohua Lib17459c2012-07-19 16:01:31 +10001194 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001195 chosen = dev->towrite;
1196 dev->towrite = NULL;
1197 BUG_ON(dev->written);
1198 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001199 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001200
1201 while (wbi && wbi->bi_sector <
1202 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001203 if (wbi->bi_rw & REQ_FUA)
1204 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001205 if (wbi->bi_rw & REQ_SYNC)
1206 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001207 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001208 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001209 else
Shaohua Li620125f2012-10-11 13:49:05 +11001210 tx = async_copy_data(1, wbi, dev->page,
1211 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001212 wbi = r5_next_bio(wbi, dev->sector);
1213 }
1214 }
1215 }
1216
1217 return tx;
1218}
1219
Dan Williamsac6b53b2009-07-14 13:40:19 -07001220static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001221{
1222 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001223 int disks = sh->disks;
1224 int pd_idx = sh->pd_idx;
1225 int qd_idx = sh->qd_idx;
1226 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001227 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001228
Harvey Harrisone46b2722008-04-28 02:15:50 -07001229 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001230 (unsigned long long)sh->sector);
1231
Shaohua Libc0934f2012-05-22 13:55:05 +10001232 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001233 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001234 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001235 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001236 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001237
Dan Williams91c00922007-01-02 13:52:30 -07001238 for (i = disks; i--; ) {
1239 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001240
Tejun Heoe9c74692010-09-03 11:56:18 +02001241 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001242 if (!discard)
1243 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001244 if (fua)
1245 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001246 if (sync)
1247 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001248 }
Dan Williams91c00922007-01-02 13:52:30 -07001249 }
1250
Dan Williamsd8ee0722008-06-28 08:32:06 +10001251 if (sh->reconstruct_state == reconstruct_state_drain_run)
1252 sh->reconstruct_state = reconstruct_state_drain_result;
1253 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1254 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1255 else {
1256 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1257 sh->reconstruct_state = reconstruct_state_result;
1258 }
Dan Williams91c00922007-01-02 13:52:30 -07001259
1260 set_bit(STRIPE_HANDLE, &sh->state);
1261 release_stripe(sh);
1262}
1263
1264static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001265ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1266 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001267{
Dan Williams91c00922007-01-02 13:52:30 -07001268 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001269 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001270 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001271 int count = 0, pd_idx = sh->pd_idx, i;
1272 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001273 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001274 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001275
Harvey Harrisone46b2722008-04-28 02:15:50 -07001276 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001277 (unsigned long long)sh->sector);
1278
Shaohua Li620125f2012-10-11 13:49:05 +11001279 for (i = 0; i < sh->disks; i++) {
1280 if (pd_idx == i)
1281 continue;
1282 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1283 break;
1284 }
1285 if (i >= sh->disks) {
1286 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001287 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1288 ops_complete_reconstruct(sh);
1289 return;
1290 }
Dan Williams91c00922007-01-02 13:52:30 -07001291 /* check if prexor is active which means only process blocks
1292 * that are part of a read-modify-write (written)
1293 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001294 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1295 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001296 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1297 for (i = disks; i--; ) {
1298 struct r5dev *dev = &sh->dev[i];
1299 if (dev->written)
1300 xor_srcs[count++] = dev->page;
1301 }
1302 } else {
1303 xor_dest = sh->dev[pd_idx].page;
1304 for (i = disks; i--; ) {
1305 struct r5dev *dev = &sh->dev[i];
1306 if (i != pd_idx)
1307 xor_srcs[count++] = dev->page;
1308 }
1309 }
1310
Dan Williams91c00922007-01-02 13:52:30 -07001311 /* 1/ if we prexor'd then the dest is reused as a source
1312 * 2/ if we did not prexor then we are redoing the parity
1313 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1314 * for the synchronous xor case
1315 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001316 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001317 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1318
1319 atomic_inc(&sh->count);
1320
Dan Williamsac6b53b2009-07-14 13:40:19 -07001321 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001322 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001323 if (unlikely(count == 1))
1324 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1325 else
1326 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001327}
1328
Dan Williamsac6b53b2009-07-14 13:40:19 -07001329static void
1330ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1331 struct dma_async_tx_descriptor *tx)
1332{
1333 struct async_submit_ctl submit;
1334 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001335 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001336
1337 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1338
Shaohua Li620125f2012-10-11 13:49:05 +11001339 for (i = 0; i < sh->disks; i++) {
1340 if (sh->pd_idx == i || sh->qd_idx == i)
1341 continue;
1342 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1343 break;
1344 }
1345 if (i >= sh->disks) {
1346 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001347 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1348 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1349 ops_complete_reconstruct(sh);
1350 return;
1351 }
1352
Dan Williamsac6b53b2009-07-14 13:40:19 -07001353 count = set_syndrome_sources(blocks, sh);
1354
1355 atomic_inc(&sh->count);
1356
1357 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1358 sh, to_addr_conv(sh, percpu));
1359 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001360}
1361
1362static void ops_complete_check(void *stripe_head_ref)
1363{
1364 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001365
Harvey Harrisone46b2722008-04-28 02:15:50 -07001366 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001367 (unsigned long long)sh->sector);
1368
Dan Williamsecc65c92008-06-28 08:31:57 +10001369 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001370 set_bit(STRIPE_HANDLE, &sh->state);
1371 release_stripe(sh);
1372}
1373
Dan Williamsac6b53b2009-07-14 13:40:19 -07001374static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001375{
Dan Williams91c00922007-01-02 13:52:30 -07001376 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001377 int pd_idx = sh->pd_idx;
1378 int qd_idx = sh->qd_idx;
1379 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001380 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001381 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001382 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001383 int count;
1384 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001385
Harvey Harrisone46b2722008-04-28 02:15:50 -07001386 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001387 (unsigned long long)sh->sector);
1388
Dan Williamsac6b53b2009-07-14 13:40:19 -07001389 count = 0;
1390 xor_dest = sh->dev[pd_idx].page;
1391 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001392 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001393 if (i == pd_idx || i == qd_idx)
1394 continue;
1395 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001396 }
1397
Dan Williamsd6f38f32009-07-14 11:50:52 -07001398 init_async_submit(&submit, 0, NULL, NULL, NULL,
1399 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001400 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001401 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001402
Dan Williams91c00922007-01-02 13:52:30 -07001403 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001404 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1405 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001406}
1407
Dan Williamsac6b53b2009-07-14 13:40:19 -07001408static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1409{
1410 struct page **srcs = percpu->scribble;
1411 struct async_submit_ctl submit;
1412 int count;
1413
1414 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1415 (unsigned long long)sh->sector, checkp);
1416
1417 count = set_syndrome_sources(srcs, sh);
1418 if (!checkp)
1419 srcs[count] = NULL;
1420
1421 atomic_inc(&sh->count);
1422 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1423 sh, to_addr_conv(sh, percpu));
1424 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1425 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1426}
1427
NeilBrown51acbce2013-02-28 09:08:34 +11001428static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001429{
1430 int overlap_clear = 0, i, disks = sh->disks;
1431 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001432 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001433 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001434 struct raid5_percpu *percpu;
1435 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001436
Dan Williamsd6f38f32009-07-14 11:50:52 -07001437 cpu = get_cpu();
1438 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001439 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001440 ops_run_biofill(sh);
1441 overlap_clear++;
1442 }
1443
Dan Williams7b3a8712008-06-28 08:32:09 +10001444 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001445 if (level < 6)
1446 tx = ops_run_compute5(sh, percpu);
1447 else {
1448 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1449 tx = ops_run_compute6_1(sh, percpu);
1450 else
1451 tx = ops_run_compute6_2(sh, percpu);
1452 }
1453 /* terminate the chain if reconstruct is not set to be run */
1454 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001455 async_tx_ack(tx);
1456 }
Dan Williams91c00922007-01-02 13:52:30 -07001457
Dan Williams600aa102008-06-28 08:32:05 +10001458 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001459 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001460
Dan Williams600aa102008-06-28 08:32:05 +10001461 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001462 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001463 overlap_clear++;
1464 }
1465
Dan Williamsac6b53b2009-07-14 13:40:19 -07001466 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1467 if (level < 6)
1468 ops_run_reconstruct5(sh, percpu, tx);
1469 else
1470 ops_run_reconstruct6(sh, percpu, tx);
1471 }
Dan Williams91c00922007-01-02 13:52:30 -07001472
Dan Williamsac6b53b2009-07-14 13:40:19 -07001473 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1474 if (sh->check_state == check_state_run)
1475 ops_run_check_p(sh, percpu);
1476 else if (sh->check_state == check_state_run_q)
1477 ops_run_check_pq(sh, percpu, 0);
1478 else if (sh->check_state == check_state_run_pq)
1479 ops_run_check_pq(sh, percpu, 1);
1480 else
1481 BUG();
1482 }
Dan Williams91c00922007-01-02 13:52:30 -07001483
Dan Williams91c00922007-01-02 13:52:30 -07001484 if (overlap_clear)
1485 for (i = disks; i--; ) {
1486 struct r5dev *dev = &sh->dev[i];
1487 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1488 wake_up(&sh->raid_conf->wait_for_overlap);
1489 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001490 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001491}
1492
NeilBrownd1688a62011-10-11 16:49:52 +11001493static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
1495 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001496 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001497 if (!sh)
1498 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001499
NeilBrown3f294f42005-11-08 21:39:25 -08001500 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08001501
Shaohua Lib17459c2012-07-19 16:01:31 +10001502 spin_lock_init(&sh->stripe_lock);
1503
NeilBrowne4e11e32010-06-16 16:45:16 +10001504 if (grow_buffers(sh)) {
1505 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001506 kmem_cache_free(conf->slab_cache, sh);
1507 return 0;
1508 }
1509 /* we just created an active stripe so... */
1510 atomic_set(&sh->count, 1);
1511 atomic_inc(&conf->active_stripes);
1512 INIT_LIST_HEAD(&sh->lru);
1513 release_stripe(sh);
1514 return 1;
1515}
1516
NeilBrownd1688a62011-10-11 16:49:52 +11001517static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001518{
Christoph Lametere18b8902006-12-06 20:33:20 -08001519 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001520 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
NeilBrownf4be6b42010-06-01 19:37:25 +10001522 if (conf->mddev->gendisk)
1523 sprintf(conf->cache_name[0],
1524 "raid%d-%s", conf->level, mdname(conf->mddev));
1525 else
1526 sprintf(conf->cache_name[0],
1527 "raid%d-%p", conf->level, conf->mddev);
1528 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1529
NeilBrownad01c9e2006-03-27 01:18:07 -08001530 conf->active_name = 0;
1531 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001533 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (!sc)
1535 return 1;
1536 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001537 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001538 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001539 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return 0;
1542}
NeilBrown29269552006-03-27 01:18:10 -08001543
Dan Williamsd6f38f32009-07-14 11:50:52 -07001544/**
1545 * scribble_len - return the required size of the scribble region
1546 * @num - total number of disks in the array
1547 *
1548 * The size must be enough to contain:
1549 * 1/ a struct page pointer for each device in the array +2
1550 * 2/ room to convert each entry in (1) to its corresponding dma
1551 * (dma_map_page()) or page (page_address()) address.
1552 *
1553 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1554 * calculate over all devices (not just the data blocks), using zeros in place
1555 * of the P and Q blocks.
1556 */
1557static size_t scribble_len(int num)
1558{
1559 size_t len;
1560
1561 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1562
1563 return len;
1564}
1565
NeilBrownd1688a62011-10-11 16:49:52 +11001566static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001567{
1568 /* Make all the stripes able to hold 'newsize' devices.
1569 * New slots in each stripe get 'page' set to a new page.
1570 *
1571 * This happens in stages:
1572 * 1/ create a new kmem_cache and allocate the required number of
1573 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09001574 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08001575 * to the new stripe_heads. This will have the side effect of
1576 * freezing the array as once all stripe_heads have been collected,
1577 * no IO will be possible. Old stripe heads are freed once their
1578 * pages have been transferred over, and the old kmem_cache is
1579 * freed when all stripes are done.
1580 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1581 * we simple return a failre status - no need to clean anything up.
1582 * 4/ allocate new pages for the new slots in the new stripe_heads.
1583 * If this fails, we don't bother trying the shrink the
1584 * stripe_heads down again, we just leave them as they are.
1585 * As each stripe_head is processed the new one is released into
1586 * active service.
1587 *
1588 * Once step2 is started, we cannot afford to wait for a write,
1589 * so we use GFP_NOIO allocations.
1590 */
1591 struct stripe_head *osh, *nsh;
1592 LIST_HEAD(newstripes);
1593 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001594 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001595 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001596 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001597 int i;
1598
1599 if (newsize <= conf->pool_size)
1600 return 0; /* never bother to shrink */
1601
Dan Williamsb5470dc2008-06-27 21:44:04 -07001602 err = md_allow_write(conf->mddev);
1603 if (err)
1604 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001605
NeilBrownad01c9e2006-03-27 01:18:07 -08001606 /* Step 1 */
1607 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1608 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001609 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001610 if (!sc)
1611 return -ENOMEM;
1612
1613 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001614 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001615 if (!nsh)
1616 break;
1617
NeilBrownad01c9e2006-03-27 01:18:07 -08001618 nsh->raid_conf = conf;
NeilBrowncb13ff62012-09-24 16:27:20 +10001619 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001620
1621 list_add(&nsh->lru, &newstripes);
1622 }
1623 if (i) {
1624 /* didn't get enough, give up */
1625 while (!list_empty(&newstripes)) {
1626 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1627 list_del(&nsh->lru);
1628 kmem_cache_free(sc, nsh);
1629 }
1630 kmem_cache_destroy(sc);
1631 return -ENOMEM;
1632 }
1633 /* Step 2 - Must use GFP_NOIO now.
1634 * OK, we have enough stripes, start collecting inactive
1635 * stripes and copying them over
1636 */
1637 list_for_each_entry(nsh, &newstripes, lru) {
1638 spin_lock_irq(&conf->device_lock);
1639 wait_event_lock_irq(conf->wait_for_stripe,
1640 !list_empty(&conf->inactive_list),
Lukas Czernereed8c022012-11-30 11:42:40 +01001641 conf->device_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001642 osh = get_free_stripe(conf);
1643 spin_unlock_irq(&conf->device_lock);
1644 atomic_set(&nsh->count, 1);
1645 for(i=0; i<conf->pool_size; i++)
1646 nsh->dev[i].page = osh->dev[i].page;
1647 for( ; i<newsize; i++)
1648 nsh->dev[i].page = NULL;
1649 kmem_cache_free(conf->slab_cache, osh);
1650 }
1651 kmem_cache_destroy(conf->slab_cache);
1652
1653 /* Step 3.
1654 * At this point, we are holding all the stripes so the array
1655 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001656 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001657 */
1658 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1659 if (ndisks) {
1660 for (i=0; i<conf->raid_disks; i++)
1661 ndisks[i] = conf->disks[i];
1662 kfree(conf->disks);
1663 conf->disks = ndisks;
1664 } else
1665 err = -ENOMEM;
1666
Dan Williamsd6f38f32009-07-14 11:50:52 -07001667 get_online_cpus();
1668 conf->scribble_len = scribble_len(newsize);
1669 for_each_present_cpu(cpu) {
1670 struct raid5_percpu *percpu;
1671 void *scribble;
1672
1673 percpu = per_cpu_ptr(conf->percpu, cpu);
1674 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1675
1676 if (scribble) {
1677 kfree(percpu->scribble);
1678 percpu->scribble = scribble;
1679 } else {
1680 err = -ENOMEM;
1681 break;
1682 }
1683 }
1684 put_online_cpus();
1685
NeilBrownad01c9e2006-03-27 01:18:07 -08001686 /* Step 4, return new stripes to service */
1687 while(!list_empty(&newstripes)) {
1688 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1689 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001690
NeilBrownad01c9e2006-03-27 01:18:07 -08001691 for (i=conf->raid_disks; i < newsize; i++)
1692 if (nsh->dev[i].page == NULL) {
1693 struct page *p = alloc_page(GFP_NOIO);
1694 nsh->dev[i].page = p;
1695 if (!p)
1696 err = -ENOMEM;
1697 }
1698 release_stripe(nsh);
1699 }
1700 /* critical section pass, GFP_NOIO no longer needed */
1701
1702 conf->slab_cache = sc;
1703 conf->active_name = 1-conf->active_name;
1704 conf->pool_size = newsize;
1705 return err;
1706}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
NeilBrownd1688a62011-10-11 16:49:52 +11001708static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 struct stripe_head *sh;
1711
NeilBrown3f294f42005-11-08 21:39:25 -08001712 spin_lock_irq(&conf->device_lock);
1713 sh = get_free_stripe(conf);
1714 spin_unlock_irq(&conf->device_lock);
1715 if (!sh)
1716 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001717 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001718 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001719 kmem_cache_free(conf->slab_cache, sh);
1720 atomic_dec(&conf->active_stripes);
1721 return 1;
1722}
1723
NeilBrownd1688a62011-10-11 16:49:52 +11001724static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001725{
1726 while (drop_one_stripe(conf))
1727 ;
1728
NeilBrown29fc7e32006-02-03 03:03:41 -08001729 if (conf->slab_cache)
1730 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 conf->slab_cache = NULL;
1732}
1733
NeilBrown6712ecf2007-09-27 12:47:43 +02001734static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
NeilBrown99c0fb52009-03-31 14:39:38 +11001736 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001737 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001738 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001740 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001741 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001742 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 for (i=0 ; i<disks; i++)
1745 if (bi == &sh->dev[i].req)
1746 break;
1747
Dan Williams45b42332007-07-09 11:56:43 -07001748 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1749 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 uptodate);
1751 if (i == disks) {
1752 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001753 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
NeilBrown14a75d32011-12-23 10:17:52 +11001755 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001756 /* If replacement finished while this request was outstanding,
1757 * 'replacement' might be NULL already.
1758 * In that case it moved down to 'rdev'.
1759 * rdev is not removed until all requests are finished.
1760 */
NeilBrown14a75d32011-12-23 10:17:52 +11001761 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001762 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001763 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
NeilBrown05616be2012-05-21 09:27:00 +10001765 if (use_new_offset(conf, sh))
1766 s = sh->sector + rdev->new_data_offset;
1767 else
1768 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001771 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001772 /* Note that this cannot happen on a
1773 * replacement device. We just fail those on
1774 * any error
1775 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001776 printk_ratelimited(
1777 KERN_INFO
1778 "md/raid:%s: read error corrected"
1779 " (%lu sectors at %llu on %s)\n",
1780 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001781 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001782 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001783 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001784 clear_bit(R5_ReadError, &sh->dev[i].flags);
1785 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001786 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1787 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1788
NeilBrown14a75d32011-12-23 10:17:52 +11001789 if (atomic_read(&rdev->read_errors))
1790 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001792 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001793 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001794 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001797 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001798 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1799 printk_ratelimited(
1800 KERN_WARNING
1801 "md/raid:%s: read error on replacement device "
1802 "(sector %llu on %s).\n",
1803 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001804 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001805 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001806 else if (conf->mddev->degraded >= conf->max_degraded) {
1807 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001808 printk_ratelimited(
1809 KERN_WARNING
1810 "md/raid:%s: read error not correctable "
1811 "(sector %llu on %s).\n",
1812 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001813 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001814 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001815 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001816 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001817 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001818 printk_ratelimited(
1819 KERN_WARNING
1820 "md/raid:%s: read error NOT corrected!! "
1821 "(sector %llu on %s).\n",
1822 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001823 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001824 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001825 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001826 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001827 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001828 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001829 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001830 else
1831 retry = 1;
1832 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001833 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1834 set_bit(R5_ReadError, &sh->dev[i].flags);
1835 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1836 } else
1837 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001838 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001839 clear_bit(R5_ReadError, &sh->dev[i].flags);
1840 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001841 if (!(set_bad
1842 && test_bit(In_sync, &rdev->flags)
1843 && rdev_set_badblocks(
1844 rdev, sh->sector, STRIPE_SECTORS, 0)))
1845 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
NeilBrown14a75d32011-12-23 10:17:52 +11001848 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1850 set_bit(STRIPE_HANDLE, &sh->state);
1851 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
1853
NeilBrownd710e132008-10-13 11:55:12 +11001854static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
NeilBrown99c0fb52009-03-31 14:39:38 +11001856 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001857 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001858 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001859 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001861 sector_t first_bad;
1862 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001863 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
NeilBrown977df362011-12-23 10:17:53 +11001865 for (i = 0 ; i < disks; i++) {
1866 if (bi == &sh->dev[i].req) {
1867 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 break;
NeilBrown977df362011-12-23 10:17:53 +11001869 }
1870 if (bi == &sh->dev[i].rreq) {
1871 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001872 if (rdev)
1873 replacement = 1;
1874 else
1875 /* rdev was removed and 'replacement'
1876 * replaced it. rdev is not removed
1877 * until all requests are finished.
1878 */
1879 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001880 break;
1881 }
1882 }
Dan Williams45b42332007-07-09 11:56:43 -07001883 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1885 uptodate);
1886 if (i == disks) {
1887 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001888 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
1890
NeilBrown977df362011-12-23 10:17:53 +11001891 if (replacement) {
1892 if (!uptodate)
1893 md_error(conf->mddev, rdev);
1894 else if (is_badblock(rdev, sh->sector,
1895 STRIPE_SECTORS,
1896 &first_bad, &bad_sectors))
1897 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1898 } else {
1899 if (!uptodate) {
NeilBrown6ba854e2014-01-16 09:35:38 +11001900 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11001901 set_bit(WriteErrorSeen, &rdev->flags);
1902 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001903 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1904 set_bit(MD_RECOVERY_NEEDED,
1905 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001906 } else if (is_badblock(rdev, sh->sector,
1907 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10001908 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11001909 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10001910 if (test_bit(R5_ReadError, &sh->dev[i].flags))
1911 /* That was a successful write so make
1912 * sure it looks like we already did
1913 * a re-write.
1914 */
1915 set_bit(R5_ReWrite, &sh->dev[i].flags);
1916 }
NeilBrown977df362011-12-23 10:17:53 +11001917 }
1918 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
NeilBrown977df362011-12-23 10:17:53 +11001920 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1921 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001923 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
NeilBrown784052e2009-03-31 15:19:07 +11001926static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
NeilBrown784052e2009-03-31 15:19:07 +11001928static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 struct r5dev *dev = &sh->dev[i];
1931
1932 bio_init(&dev->req);
1933 dev->req.bi_io_vec = &dev->vec;
1934 dev->req.bi_vcnt++;
1935 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001937 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
NeilBrown977df362011-12-23 10:17:53 +11001939 bio_init(&dev->rreq);
1940 dev->rreq.bi_io_vec = &dev->rvec;
1941 dev->rreq.bi_vcnt++;
1942 dev->rreq.bi_max_vecs++;
1943 dev->rreq.bi_private = sh;
1944 dev->rvec.bv_page = dev->page;
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001947 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
NeilBrownfd01b882011-10-11 16:47:53 +11001950static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
1952 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001953 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001954 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001955 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
NeilBrown908f4fb2011-12-23 10:17:50 +11001957 spin_lock_irqsave(&conf->device_lock, flags);
1958 clear_bit(In_sync, &rdev->flags);
1959 mddev->degraded = calc_degraded(conf);
1960 spin_unlock_irqrestore(&conf->device_lock, flags);
1961 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1962
NeilBrownde393cd2011-07-28 11:31:48 +10001963 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001964 set_bit(Faulty, &rdev->flags);
1965 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1966 printk(KERN_ALERT
1967 "md/raid:%s: Disk failure on %s, disabling device.\n"
1968 "md/raid:%s: Operation continuing on %d devices.\n",
1969 mdname(mddev),
1970 bdevname(rdev->bdev, b),
1971 mdname(mddev),
1972 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001973}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975/*
1976 * Input: a 'big' sector number,
1977 * Output: index of the data and parity disk, and the sector # in them.
1978 */
NeilBrownd1688a62011-10-11 16:49:52 +11001979static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001980 int previous, int *dd_idx,
1981 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001983 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001984 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001986 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001987 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001989 int algorithm = previous ? conf->prev_algo
1990 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001991 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1992 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001993 int raid_disks = previous ? conf->previous_raid_disks
1994 : conf->raid_disks;
1995 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 /* First compute the information on this sector */
1998
1999 /*
2000 * Compute the chunk number and the sector offset inside the chunk
2001 */
2002 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2003 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 /*
2006 * Compute the stripe number
2007 */
NeilBrown35f2a592010-04-20 14:13:34 +10002008 stripe = chunk_number;
2009 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002010 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 /*
2012 * Select the parity disk based on the user selected algorithm.
2013 */
NeilBrown84789552011-07-27 11:00:36 +10002014 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002015 switch(conf->level) {
2016 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002017 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002018 break;
2019 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002020 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002022 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002023 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 (*dd_idx)++;
2025 break;
2026 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002027 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002028 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 (*dd_idx)++;
2030 break;
2031 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002032 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002033 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 break;
2035 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002036 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002037 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002039 case ALGORITHM_PARITY_0:
2040 pd_idx = 0;
2041 (*dd_idx)++;
2042 break;
2043 case ALGORITHM_PARITY_N:
2044 pd_idx = data_disks;
2045 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002047 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002048 }
2049 break;
2050 case 6:
2051
NeilBrowne183eae2009-03-31 15:20:22 +11002052 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002053 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002054 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002055 qd_idx = pd_idx + 1;
2056 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002057 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002058 qd_idx = 0;
2059 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002060 (*dd_idx) += 2; /* D D P Q D */
2061 break;
2062 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002063 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002064 qd_idx = pd_idx + 1;
2065 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002066 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002067 qd_idx = 0;
2068 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002069 (*dd_idx) += 2; /* D D P Q D */
2070 break;
2071 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002072 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002073 qd_idx = (pd_idx + 1) % raid_disks;
2074 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002075 break;
2076 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002077 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002078 qd_idx = (pd_idx + 1) % raid_disks;
2079 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002080 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002081
2082 case ALGORITHM_PARITY_0:
2083 pd_idx = 0;
2084 qd_idx = 1;
2085 (*dd_idx) += 2;
2086 break;
2087 case ALGORITHM_PARITY_N:
2088 pd_idx = data_disks;
2089 qd_idx = data_disks + 1;
2090 break;
2091
2092 case ALGORITHM_ROTATING_ZERO_RESTART:
2093 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2094 * of blocks for computing Q is different.
2095 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002096 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002097 qd_idx = pd_idx + 1;
2098 if (pd_idx == raid_disks-1) {
2099 (*dd_idx)++; /* Q D D D P */
2100 qd_idx = 0;
2101 } else if (*dd_idx >= pd_idx)
2102 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002103 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002104 break;
2105
2106 case ALGORITHM_ROTATING_N_RESTART:
2107 /* Same a left_asymmetric, by first stripe is
2108 * D D D P Q rather than
2109 * Q D D D P
2110 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002111 stripe2 += 1;
2112 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002113 qd_idx = pd_idx + 1;
2114 if (pd_idx == raid_disks-1) {
2115 (*dd_idx)++; /* Q D D D P */
2116 qd_idx = 0;
2117 } else if (*dd_idx >= pd_idx)
2118 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002119 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002120 break;
2121
2122 case ALGORITHM_ROTATING_N_CONTINUE:
2123 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002124 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002125 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2126 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002127 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002128 break;
2129
2130 case ALGORITHM_LEFT_ASYMMETRIC_6:
2131 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002132 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002133 if (*dd_idx >= pd_idx)
2134 (*dd_idx)++;
2135 qd_idx = raid_disks - 1;
2136 break;
2137
2138 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002139 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002140 if (*dd_idx >= pd_idx)
2141 (*dd_idx)++;
2142 qd_idx = raid_disks - 1;
2143 break;
2144
2145 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002146 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002147 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2148 qd_idx = raid_disks - 1;
2149 break;
2150
2151 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002152 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002153 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2154 qd_idx = raid_disks - 1;
2155 break;
2156
2157 case ALGORITHM_PARITY_0_6:
2158 pd_idx = 0;
2159 (*dd_idx)++;
2160 qd_idx = raid_disks - 1;
2161 break;
2162
NeilBrown16a53ec2006-06-26 00:27:38 -07002163 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002164 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002165 }
2166 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
2168
NeilBrown911d4ee2009-03-31 14:39:38 +11002169 if (sh) {
2170 sh->pd_idx = pd_idx;
2171 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002172 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 /*
2175 * Finally, compute the new sector number
2176 */
2177 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2178 return new_sector;
2179}
2180
2181
NeilBrown784052e2009-03-31 15:19:07 +11002182static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
NeilBrownd1688a62011-10-11 16:49:52 +11002184 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002185 int raid_disks = sh->disks;
2186 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002188 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2189 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002190 int algorithm = previous ? conf->prev_algo
2191 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 sector_t stripe;
2193 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002194 sector_t chunk_number;
2195 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002197 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
NeilBrown16a53ec2006-06-26 00:27:38 -07002199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2201 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
NeilBrown16a53ec2006-06-26 00:27:38 -07002203 if (i == sh->pd_idx)
2204 return 0;
2205 switch(conf->level) {
2206 case 4: break;
2207 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002208 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 case ALGORITHM_LEFT_ASYMMETRIC:
2210 case ALGORITHM_RIGHT_ASYMMETRIC:
2211 if (i > sh->pd_idx)
2212 i--;
2213 break;
2214 case ALGORITHM_LEFT_SYMMETRIC:
2215 case ALGORITHM_RIGHT_SYMMETRIC:
2216 if (i < sh->pd_idx)
2217 i += raid_disks;
2218 i -= (sh->pd_idx + 1);
2219 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002220 case ALGORITHM_PARITY_0:
2221 i -= 1;
2222 break;
2223 case ALGORITHM_PARITY_N:
2224 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002226 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002227 }
2228 break;
2229 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002230 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002231 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002232 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002233 case ALGORITHM_LEFT_ASYMMETRIC:
2234 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002235 case ALGORITHM_ROTATING_ZERO_RESTART:
2236 case ALGORITHM_ROTATING_N_RESTART:
2237 if (sh->pd_idx == raid_disks-1)
2238 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002239 else if (i > sh->pd_idx)
2240 i -= 2; /* D D P Q D */
2241 break;
2242 case ALGORITHM_LEFT_SYMMETRIC:
2243 case ALGORITHM_RIGHT_SYMMETRIC:
2244 if (sh->pd_idx == raid_disks-1)
2245 i--; /* Q D D D P */
2246 else {
2247 /* D D P Q D */
2248 if (i < sh->pd_idx)
2249 i += raid_disks;
2250 i -= (sh->pd_idx + 2);
2251 }
2252 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002253 case ALGORITHM_PARITY_0:
2254 i -= 2;
2255 break;
2256 case ALGORITHM_PARITY_N:
2257 break;
2258 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002259 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002260 if (sh->pd_idx == 0)
2261 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002262 else {
2263 /* D D Q P D */
2264 if (i < sh->pd_idx)
2265 i += raid_disks;
2266 i -= (sh->pd_idx + 1);
2267 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002268 break;
2269 case ALGORITHM_LEFT_ASYMMETRIC_6:
2270 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2271 if (i > sh->pd_idx)
2272 i--;
2273 break;
2274 case ALGORITHM_LEFT_SYMMETRIC_6:
2275 case ALGORITHM_RIGHT_SYMMETRIC_6:
2276 if (i < sh->pd_idx)
2277 i += data_disks + 1;
2278 i -= (sh->pd_idx + 1);
2279 break;
2280 case ALGORITHM_PARITY_0_6:
2281 i -= 1;
2282 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002283 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002284 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002285 }
2286 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 }
2288
2289 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002290 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
NeilBrown112bf892009-03-31 14:39:38 +11002292 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002293 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002294 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2295 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002296 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2297 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return 0;
2299 }
2300 return r_sector;
2301}
2302
2303
Dan Williams600aa102008-06-28 08:32:05 +10002304static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002305schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002306 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002307{
2308 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002309 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002310 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002311
Dan Williamse33129d2007-01-02 13:52:30 -07002312 if (rcw) {
Dan Williamse33129d2007-01-02 13:52:30 -07002313
2314 for (i = disks; i--; ) {
2315 struct r5dev *dev = &sh->dev[i];
2316
2317 if (dev->towrite) {
2318 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002319 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002320 if (!expand)
2321 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002322 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002323 }
2324 }
NeilBrownce7d3632013-03-04 12:37:14 +11002325 /* if we are not expanding this is a proper write request, and
2326 * there will be bios with new data to be drained into the
2327 * stripe cache
2328 */
2329 if (!expand) {
2330 if (!s->locked)
2331 /* False alarm, nothing to do */
2332 return;
2333 sh->reconstruct_state = reconstruct_state_drain_run;
2334 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2335 } else
2336 sh->reconstruct_state = reconstruct_state_run;
2337
2338 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2339
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002340 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002341 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002342 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002343 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002344 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002345 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2346 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2347
Dan Williamse33129d2007-01-02 13:52:30 -07002348 for (i = disks; i--; ) {
2349 struct r5dev *dev = &sh->dev[i];
2350 if (i == pd_idx)
2351 continue;
2352
Dan Williamse33129d2007-01-02 13:52:30 -07002353 if (dev->towrite &&
2354 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002355 test_bit(R5_Wantcompute, &dev->flags))) {
2356 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002357 set_bit(R5_LOCKED, &dev->flags);
2358 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002359 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002360 }
2361 }
NeilBrownce7d3632013-03-04 12:37:14 +11002362 if (!s->locked)
2363 /* False alarm - nothing to do */
2364 return;
2365 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2366 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2367 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2368 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002369 }
2370
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002371 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002372 * are in flight
2373 */
2374 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2375 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002376 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002377
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002378 if (level == 6) {
2379 int qd_idx = sh->qd_idx;
2380 struct r5dev *dev = &sh->dev[qd_idx];
2381
2382 set_bit(R5_LOCKED, &dev->flags);
2383 clear_bit(R5_UPTODATE, &dev->flags);
2384 s->locked++;
2385 }
2386
Dan Williams600aa102008-06-28 08:32:05 +10002387 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002388 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002389 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002390}
NeilBrown16a53ec2006-06-26 00:27:38 -07002391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392/*
2393 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002394 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 * The bi_next chain must be in order.
2396 */
2397static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2398{
2399 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002400 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002401 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
NeilBrowncbe47ec2011-07-26 11:20:35 +10002403 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 (unsigned long long)bi->bi_sector,
2405 (unsigned long long)sh->sector);
2406
Shaohua Lib17459c2012-07-19 16:01:31 +10002407 /*
2408 * If several bio share a stripe. The bio bi_phys_segments acts as a
2409 * reference count to avoid race. The reference count should already be
2410 * increased before this function is called (for example, in
2411 * make_request()), so other bio sharing this stripe will not free the
2412 * stripe. If a stripe is owned by one stripe, the stripe lock will
2413 * protect it.
2414 */
2415 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002416 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002418 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002419 firstwrite = 1;
2420 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 bip = &sh->dev[dd_idx].toread;
2422 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002423 if (bio_end_sector(*bip) > bi->bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 goto overlap;
2425 bip = & (*bip)->bi_next;
2426 }
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002427 if (*bip && (*bip)->bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 goto overlap;
2429
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002430 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (*bip)
2432 bi->bi_next = *bip;
2433 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002434 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (forwrite) {
2437 /* check if page is covered */
2438 sector_t sector = sh->dev[dd_idx].sector;
2439 for (bi=sh->dev[dd_idx].towrite;
2440 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2441 bi && bi->bi_sector <= sector;
2442 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002443 if (bio_end_sector(bi) >= sector)
2444 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
2446 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2447 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2448 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002449
2450 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2451 (unsigned long long)(*bip)->bi_sector,
2452 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002453 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002454
2455 if (conf->mddev->bitmap && firstwrite) {
2456 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2457 STRIPE_SECTORS, 0);
2458 sh->bm_seq = conf->seq_flush+1;
2459 set_bit(STRIPE_BIT_DELAY, &sh->state);
2460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 return 1;
2462
2463 overlap:
2464 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002465 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return 0;
2467}
2468
NeilBrownd1688a62011-10-11 16:49:52 +11002469static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002470
NeilBrownd1688a62011-10-11 16:49:52 +11002471static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002472 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002473{
NeilBrown784052e2009-03-31 15:19:07 +11002474 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002475 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002476 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002477 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002478 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002479
NeilBrown112bf892009-03-31 14:39:38 +11002480 raid5_compute_sector(conf,
2481 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002482 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002483 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002484 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002485}
2486
Dan Williamsa4456852007-07-09 11:56:43 -07002487static void
NeilBrownd1688a62011-10-11 16:49:52 +11002488handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002489 struct stripe_head_state *s, int disks,
2490 struct bio **return_bi)
2491{
2492 int i;
2493 for (i = disks; i--; ) {
2494 struct bio *bi;
2495 int bitmap_end = 0;
2496
2497 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002498 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002499 rcu_read_lock();
2500 rdev = rcu_dereference(conf->disks[i].rdev);
2501 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002502 atomic_inc(&rdev->nr_pending);
2503 else
2504 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002505 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002506 if (rdev) {
2507 if (!rdev_set_badblocks(
2508 rdev,
2509 sh->sector,
2510 STRIPE_SECTORS, 0))
2511 md_error(conf->mddev, rdev);
2512 rdev_dec_pending(rdev, conf->mddev);
2513 }
Dan Williamsa4456852007-07-09 11:56:43 -07002514 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002515 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002516 /* fail all writes first */
2517 bi = sh->dev[i].towrite;
2518 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002519 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002520 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002521 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002522
2523 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2524 wake_up(&conf->wait_for_overlap);
2525
2526 while (bi && bi->bi_sector <
2527 sh->dev[i].sector + STRIPE_SECTORS) {
2528 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2529 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002530 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002531 md_write_end(conf->mddev);
2532 bi->bi_next = *return_bi;
2533 *return_bi = bi;
2534 }
2535 bi = nextbi;
2536 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002537 if (bitmap_end)
2538 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2539 STRIPE_SECTORS, 0, 0);
2540 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002541 /* and fail all 'written' */
2542 bi = sh->dev[i].written;
2543 sh->dev[i].written = NULL;
2544 if (bi) bitmap_end = 1;
2545 while (bi && bi->bi_sector <
2546 sh->dev[i].sector + STRIPE_SECTORS) {
2547 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2548 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002549 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002550 md_write_end(conf->mddev);
2551 bi->bi_next = *return_bi;
2552 *return_bi = bi;
2553 }
2554 bi = bi2;
2555 }
2556
Dan Williamsb5e98d62007-01-02 13:52:31 -07002557 /* fail any reads if this device is non-operational and
2558 * the data has not reached the cache yet.
2559 */
2560 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2561 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2562 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002563 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002564 bi = sh->dev[i].toread;
2565 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002566 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002567 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2568 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002569 while (bi && bi->bi_sector <
2570 sh->dev[i].sector + STRIPE_SECTORS) {
2571 struct bio *nextbi =
2572 r5_next_bio(bi, sh->dev[i].sector);
2573 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002574 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002575 bi->bi_next = *return_bi;
2576 *return_bi = bi;
2577 }
2578 bi = nextbi;
2579 }
2580 }
Dan Williamsa4456852007-07-09 11:56:43 -07002581 if (bitmap_end)
2582 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2583 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002584 /* If we were in the middle of a write the parity block might
2585 * still be locked - so just clear all R5_LOCKED flags
2586 */
2587 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002588 }
2589
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002590 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2591 if (atomic_dec_and_test(&conf->pending_full_writes))
2592 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002593}
2594
NeilBrown7f0da592011-07-28 11:39:22 +10002595static void
NeilBrownd1688a62011-10-11 16:49:52 +11002596handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002597 struct stripe_head_state *s)
2598{
2599 int abort = 0;
2600 int i;
2601
NeilBrown7f0da592011-07-28 11:39:22 +10002602 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002603 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
2604 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10002605 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002606 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002607 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002608 * Don't even need to abort as that is handled elsewhere
2609 * if needed, and not always wanted e.g. if there is a known
2610 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002611 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002612 * non-sync devices, or abort the recovery
2613 */
NeilBrown18b98372012-04-01 23:48:38 +10002614 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2615 /* During recovery devices cannot be removed, so
2616 * locking and refcounting of rdevs is not needed
2617 */
2618 for (i = 0; i < conf->raid_disks; i++) {
2619 struct md_rdev *rdev = conf->disks[i].rdev;
2620 if (rdev
2621 && !test_bit(Faulty, &rdev->flags)
2622 && !test_bit(In_sync, &rdev->flags)
2623 && !rdev_set_badblocks(rdev, sh->sector,
2624 STRIPE_SECTORS, 0))
2625 abort = 1;
2626 rdev = conf->disks[i].replacement;
2627 if (rdev
2628 && !test_bit(Faulty, &rdev->flags)
2629 && !test_bit(In_sync, &rdev->flags)
2630 && !rdev_set_badblocks(rdev, sh->sector,
2631 STRIPE_SECTORS, 0))
2632 abort = 1;
2633 }
2634 if (abort)
2635 conf->recovery_disabled =
2636 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002637 }
NeilBrown18b98372012-04-01 23:48:38 +10002638 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002639}
2640
NeilBrown9a3e1102011-12-23 10:17:53 +11002641static int want_replace(struct stripe_head *sh, int disk_idx)
2642{
2643 struct md_rdev *rdev;
2644 int rv = 0;
2645 /* Doing recovery so rcu locking not required */
2646 rdev = sh->raid_conf->disks[disk_idx].replacement;
2647 if (rdev
2648 && !test_bit(Faulty, &rdev->flags)
2649 && !test_bit(In_sync, &rdev->flags)
2650 && (rdev->recovery_offset <= sh->sector
2651 || rdev->mddev->recovery_cp <= sh->sector))
2652 rv = 1;
2653
2654 return rv;
2655}
2656
NeilBrown93b3dbc2011-07-27 11:00:36 +10002657/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002658 * to be read or computed to satisfy a request.
2659 *
2660 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002661 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002662 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002663static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2664 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002665{
2666 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002667 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2668 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002669
Dan Williamsf38e1212007-01-02 13:52:30 -07002670 /* is the data in this block needed, and can we get it? */
2671 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002672 !test_bit(R5_UPTODATE, &dev->flags) &&
2673 (dev->toread ||
2674 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2675 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002676 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002677 (s->failed >= 1 && fdev[0]->toread) ||
2678 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002679 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2680 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
NeilBrowna4f2a8d2014-12-03 16:07:58 +11002681 ((sh->raid_conf->level == 6 || sh->sector >= sh->raid_conf->mddev->recovery_cp)
2682 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002683 /* we would like to get this block, possibly by computing it,
2684 * otherwise read it if the backing disk is insync
2685 */
2686 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2687 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2688 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002689 (s->failed && (disk_idx == s->failed_num[0] ||
2690 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002691 /* have disk failed, and we're requested to fetch it;
2692 * do compute it
2693 */
2694 pr_debug("Computing stripe %llu block %d\n",
2695 (unsigned long long)sh->sector, disk_idx);
2696 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2697 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2698 set_bit(R5_Wantcompute, &dev->flags);
2699 sh->ops.target = disk_idx;
2700 sh->ops.target2 = -1; /* no 2nd target */
2701 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002702 /* Careful: from this point on 'uptodate' is in the eye
2703 * of raid_run_ops which services 'compute' operations
2704 * before writes. R5_Wantcompute flags a block that will
2705 * be R5_UPTODATE by the time it is needed for a
2706 * subsequent operation.
2707 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002708 s->uptodate++;
2709 return 1;
2710 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2711 /* Computing 2-failure is *very* expensive; only
2712 * do it if failed >= 2
2713 */
2714 int other;
2715 for (other = disks; other--; ) {
2716 if (other == disk_idx)
2717 continue;
2718 if (!test_bit(R5_UPTODATE,
2719 &sh->dev[other].flags))
2720 break;
2721 }
2722 BUG_ON(other < 0);
2723 pr_debug("Computing stripe %llu blocks %d,%d\n",
2724 (unsigned long long)sh->sector,
2725 disk_idx, other);
2726 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2727 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2728 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2729 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2730 sh->ops.target = disk_idx;
2731 sh->ops.target2 = other;
2732 s->uptodate += 2;
2733 s->req_compute = 1;
2734 return 1;
2735 } else if (test_bit(R5_Insync, &dev->flags)) {
2736 set_bit(R5_LOCKED, &dev->flags);
2737 set_bit(R5_Wantread, &dev->flags);
2738 s->locked++;
2739 pr_debug("Reading block %d (sync=%d)\n",
2740 disk_idx, s->syncing);
2741 }
2742 }
2743
2744 return 0;
2745}
2746
2747/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002748 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002749 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002750static void handle_stripe_fill(struct stripe_head *sh,
2751 struct stripe_head_state *s,
2752 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002753{
2754 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002755
2756 /* look for blocks to read/compute, skip this if a compute
2757 * is already in flight, or if the stripe contents are in the
2758 * midst of changing due to a write
2759 */
2760 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2761 !sh->reconstruct_state)
2762 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002763 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002764 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002765 set_bit(STRIPE_HANDLE, &sh->state);
2766}
2767
2768
Dan Williams1fe797e2008-06-28 09:16:30 +10002769/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002770 * any written block on an uptodate or failed drive can be returned.
2771 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2772 * never LOCKED, so we don't need to test 'failed' directly.
2773 */
NeilBrownd1688a62011-10-11 16:49:52 +11002774static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002775 struct stripe_head *sh, int disks, struct bio **return_bi)
2776{
2777 int i;
2778 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11002779 int discard_pending = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002780
2781 for (i = disks; i--; )
2782 if (sh->dev[i].written) {
2783 dev = &sh->dev[i];
2784 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11002785 (test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownca64cae2012-11-21 16:33:40 +11002786 test_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002787 /* We can return any write requests */
2788 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002789 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11002790 if (test_and_clear_bit(R5_Discard, &dev->flags))
2791 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002792 wbi = dev->written;
2793 dev->written = NULL;
2794 while (wbi && wbi->bi_sector <
2795 dev->sector + STRIPE_SECTORS) {
2796 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002797 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002798 md_write_end(conf->mddev);
2799 wbi->bi_next = *return_bi;
2800 *return_bi = wbi;
2801 }
2802 wbi = wbi2;
2803 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002804 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2805 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002806 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002807 0);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002808 } else if (test_bit(R5_Discard, &dev->flags))
2809 discard_pending = 1;
2810 }
2811 if (!discard_pending &&
2812 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
2813 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
2814 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
2815 if (sh->qd_idx >= 0) {
2816 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
2817 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
2818 }
2819 /* now that discard is done we can proceed with any sync */
2820 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li01e608d2013-10-19 14:51:42 +08002821 /*
2822 * SCSI discard will change some bio fields and the stripe has
2823 * no updated data, so remove it from hash list and the stripe
2824 * will be reinitialized
2825 */
2826 spin_lock_irq(&conf->device_lock);
2827 remove_hash(sh);
2828 spin_unlock_irq(&conf->device_lock);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002829 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
2830 set_bit(STRIPE_HANDLE, &sh->state);
2831
2832 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002833
2834 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2835 if (atomic_dec_and_test(&conf->pending_full_writes))
2836 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002837}
2838
NeilBrownd1688a62011-10-11 16:49:52 +11002839static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002840 struct stripe_head *sh,
2841 struct stripe_head_state *s,
2842 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002843{
2844 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002845 sector_t recovery_cp = conf->mddev->recovery_cp;
2846
2847 /* RAID6 requires 'rcw' in current implementation.
2848 * Otherwise, check whether resync is now happening or should start.
2849 * If yes, then the array is dirty (after unclean shutdown or
2850 * initial creation), so parity in some stripes might be inconsistent.
2851 * In this case, we need to always do reconstruct-write, to ensure
2852 * that in case of drive failure or read-error correction, we
2853 * generate correct data from the parity.
2854 */
2855 if (conf->max_degraded == 2 ||
2856 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2857 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002858 * look like rcw is cheaper
2859 */
2860 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002861 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2862 conf->max_degraded, (unsigned long long)recovery_cp,
2863 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002864 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002865 /* would I have to read this buffer for read_modify_write */
2866 struct r5dev *dev = &sh->dev[i];
2867 if ((dev->towrite || i == sh->pd_idx) &&
2868 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002869 !(test_bit(R5_UPTODATE, &dev->flags) ||
2870 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002871 if (test_bit(R5_Insync, &dev->flags))
2872 rmw++;
2873 else
2874 rmw += 2*disks; /* cannot read it */
2875 }
2876 /* Would I have to read this buffer for reconstruct_write */
2877 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2878 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002879 !(test_bit(R5_UPTODATE, &dev->flags) ||
2880 test_bit(R5_Wantcompute, &dev->flags))) {
2881 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002882 else
2883 rcw += 2*disks;
2884 }
2885 }
Dan Williams45b42332007-07-09 11:56:43 -07002886 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002887 (unsigned long long)sh->sector, rmw, rcw);
2888 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11002889 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002890 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06002891 if (conf->mddev->queue)
2892 blk_add_trace_msg(conf->mddev->queue,
2893 "raid5 rmw %llu %d",
2894 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07002895 for (i = disks; i--; ) {
2896 struct r5dev *dev = &sh->dev[i];
2897 if ((dev->towrite || i == sh->pd_idx) &&
2898 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002899 !(test_bit(R5_UPTODATE, &dev->flags) ||
2900 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002901 test_bit(R5_Insync, &dev->flags)) {
2902 if (
2903 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002904 pr_debug("Read_old block "
NeilBrowna9add5d2012-10-31 11:59:09 +11002905 "%d for r-m-w\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002906 set_bit(R5_LOCKED, &dev->flags);
2907 set_bit(R5_Wantread, &dev->flags);
2908 s->locked++;
2909 } else {
2910 set_bit(STRIPE_DELAYED, &sh->state);
2911 set_bit(STRIPE_HANDLE, &sh->state);
2912 }
2913 }
2914 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002915 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002916 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002917 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002918 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10002919 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002920 for (i = disks; i--; ) {
2921 struct r5dev *dev = &sh->dev[i];
2922 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002923 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002924 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002925 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002926 test_bit(R5_Wantcompute, &dev->flags))) {
2927 rcw++;
2928 if (!test_bit(R5_Insync, &dev->flags))
2929 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002930 if (
2931 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002932 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002933 "%d for Reconstruct\n", i);
2934 set_bit(R5_LOCKED, &dev->flags);
2935 set_bit(R5_Wantread, &dev->flags);
2936 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11002937 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07002938 } else {
2939 set_bit(STRIPE_DELAYED, &sh->state);
2940 set_bit(STRIPE_HANDLE, &sh->state);
2941 }
2942 }
2943 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06002944 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11002945 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
2946 (unsigned long long)sh->sector,
2947 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10002948 }
Dan Williamsa4456852007-07-09 11:56:43 -07002949 /* now if nothing is locked, and if we have enough data,
2950 * we can start a write request
2951 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002952 /* since handle_stripe can be called at any time we need to handle the
2953 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002954 * subsequent call wants to start a write request. raid_run_ops only
2955 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002956 * simultaneously. If this is not the case then new writes need to be
2957 * held off until the compute completes.
2958 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002959 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2960 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2961 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002962 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002963}
2964
NeilBrownd1688a62011-10-11 16:49:52 +11002965static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002966 struct stripe_head_state *s, int disks)
2967{
Dan Williamsecc65c92008-06-28 08:31:57 +10002968 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002969
Dan Williamsbd2ab672008-04-10 21:29:27 -07002970 set_bit(STRIPE_HANDLE, &sh->state);
2971
Dan Williamsecc65c92008-06-28 08:31:57 +10002972 switch (sh->check_state) {
2973 case check_state_idle:
2974 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002975 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002976 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002977 sh->check_state = check_state_run;
2978 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002979 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002980 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002981 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002982 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002983 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002984 /* fall through */
2985 case check_state_compute_result:
2986 sh->check_state = check_state_idle;
2987 if (!dev)
2988 dev = &sh->dev[sh->pd_idx];
2989
2990 /* check that a write has not made the stripe insync */
2991 if (test_bit(STRIPE_INSYNC, &sh->state))
2992 break;
2993
2994 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002995 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2996 BUG_ON(s->uptodate != disks);
2997
2998 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002999 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003000 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003001
Dan Williamsa4456852007-07-09 11:56:43 -07003002 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003003 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003004 break;
3005 case check_state_run:
3006 break; /* we will be called again upon completion */
3007 case check_state_check_result:
3008 sh->check_state = check_state_idle;
3009
3010 /* if a failure occurred during the check operation, leave
3011 * STRIPE_INSYNC not set and let the stripe be handled again
3012 */
3013 if (s->failed)
3014 break;
3015
3016 /* handle a successful check operation, if parity is correct
3017 * we are done. Otherwise update the mismatch count and repair
3018 * parity if !MD_RECOVERY_CHECK
3019 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003020 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003021 /* parity is correct (on disc,
3022 * not in buffer any more)
3023 */
3024 set_bit(STRIPE_INSYNC, &sh->state);
3025 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003026 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003027 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3028 /* don't try to repair!! */
3029 set_bit(STRIPE_INSYNC, &sh->state);
3030 else {
3031 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003032 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003033 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3034 set_bit(R5_Wantcompute,
3035 &sh->dev[sh->pd_idx].flags);
3036 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003037 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003038 s->uptodate++;
3039 }
3040 }
3041 break;
3042 case check_state_compute_run:
3043 break;
3044 default:
3045 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3046 __func__, sh->check_state,
3047 (unsigned long long) sh->sector);
3048 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003049 }
3050}
3051
3052
NeilBrownd1688a62011-10-11 16:49:52 +11003053static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003054 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003055 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003056{
Dan Williamsa4456852007-07-09 11:56:43 -07003057 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003058 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003059 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003060
3061 set_bit(STRIPE_HANDLE, &sh->state);
3062
3063 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003064
Dan Williamsa4456852007-07-09 11:56:43 -07003065 /* Want to check and possibly repair P and Q.
3066 * However there could be one 'failed' device, in which
3067 * case we can only check one of them, possibly using the
3068 * other to generate missing data
3069 */
3070
Dan Williamsd82dfee2009-07-14 13:40:57 -07003071 switch (sh->check_state) {
3072 case check_state_idle:
3073 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003074 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003075 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003076 * makes sense to check P (If anything else were failed,
3077 * we would have used P to recreate it).
3078 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003079 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003080 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003081 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003082 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003083 * anything, so it makes sense to check it
3084 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003085 if (sh->check_state == check_state_run)
3086 sh->check_state = check_state_run_pq;
3087 else
3088 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003089 }
Dan Williams36d1c642009-07-14 11:48:22 -07003090
Dan Williamsd82dfee2009-07-14 13:40:57 -07003091 /* discard potentially stale zero_sum_result */
3092 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003093
Dan Williamsd82dfee2009-07-14 13:40:57 -07003094 if (sh->check_state == check_state_run) {
3095 /* async_xor_zero_sum destroys the contents of P */
3096 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3097 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003098 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003099 if (sh->check_state >= check_state_run &&
3100 sh->check_state <= check_state_run_pq) {
3101 /* async_syndrome_zero_sum preserves P and Q, so
3102 * no need to mark them !uptodate here
3103 */
3104 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3105 break;
3106 }
Dan Williams36d1c642009-07-14 11:48:22 -07003107
Dan Williamsd82dfee2009-07-14 13:40:57 -07003108 /* we have 2-disk failure */
3109 BUG_ON(s->failed != 2);
3110 /* fall through */
3111 case check_state_compute_result:
3112 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003113
Dan Williamsd82dfee2009-07-14 13:40:57 -07003114 /* check that a write has not made the stripe insync */
3115 if (test_bit(STRIPE_INSYNC, &sh->state))
3116 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003117
3118 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003119 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003120 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003121 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003122 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003123 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003124 s->locked++;
3125 set_bit(R5_LOCKED, &dev->flags);
3126 set_bit(R5_Wantwrite, &dev->flags);
3127 }
3128 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003129 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003130 s->locked++;
3131 set_bit(R5_LOCKED, &dev->flags);
3132 set_bit(R5_Wantwrite, &dev->flags);
3133 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003134 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003135 dev = &sh->dev[pd_idx];
3136 s->locked++;
3137 set_bit(R5_LOCKED, &dev->flags);
3138 set_bit(R5_Wantwrite, &dev->flags);
3139 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003140 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003141 dev = &sh->dev[qd_idx];
3142 s->locked++;
3143 set_bit(R5_LOCKED, &dev->flags);
3144 set_bit(R5_Wantwrite, &dev->flags);
3145 }
3146 clear_bit(STRIPE_DEGRADED, &sh->state);
3147
3148 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003149 break;
3150 case check_state_run:
3151 case check_state_run_q:
3152 case check_state_run_pq:
3153 break; /* we will be called again upon completion */
3154 case check_state_check_result:
3155 sh->check_state = check_state_idle;
3156
3157 /* handle a successful check operation, if parity is correct
3158 * we are done. Otherwise update the mismatch count and repair
3159 * parity if !MD_RECOVERY_CHECK
3160 */
3161 if (sh->ops.zero_sum_result == 0) {
3162 /* both parities are correct */
3163 if (!s->failed)
3164 set_bit(STRIPE_INSYNC, &sh->state);
3165 else {
3166 /* in contrast to the raid5 case we can validate
3167 * parity, but still have a failure to write
3168 * back
3169 */
3170 sh->check_state = check_state_compute_result;
3171 /* Returning at this point means that we may go
3172 * off and bring p and/or q uptodate again so
3173 * we make sure to check zero_sum_result again
3174 * to verify if p or q need writeback
3175 */
3176 }
3177 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003178 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003179 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3180 /* don't try to repair!! */
3181 set_bit(STRIPE_INSYNC, &sh->state);
3182 else {
3183 int *target = &sh->ops.target;
3184
3185 sh->ops.target = -1;
3186 sh->ops.target2 = -1;
3187 sh->check_state = check_state_compute_run;
3188 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3189 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3190 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3191 set_bit(R5_Wantcompute,
3192 &sh->dev[pd_idx].flags);
3193 *target = pd_idx;
3194 target = &sh->ops.target2;
3195 s->uptodate++;
3196 }
3197 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3198 set_bit(R5_Wantcompute,
3199 &sh->dev[qd_idx].flags);
3200 *target = qd_idx;
3201 s->uptodate++;
3202 }
3203 }
3204 }
3205 break;
3206 case check_state_compute_run:
3207 break;
3208 default:
3209 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3210 __func__, sh->check_state,
3211 (unsigned long long) sh->sector);
3212 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003213 }
3214}
3215
NeilBrownd1688a62011-10-11 16:49:52 +11003216static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003217{
3218 int i;
3219
3220 /* We have read all the blocks in this stripe and now we need to
3221 * copy some of them into a target stripe for expand.
3222 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003223 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003224 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3225 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003226 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003227 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003228 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003229 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003230
NeilBrown784052e2009-03-31 15:19:07 +11003231 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003232 sector_t s = raid5_compute_sector(conf, bn, 0,
3233 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003234 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003235 if (sh2 == NULL)
3236 /* so far only the early blocks of this stripe
3237 * have been requested. When later blocks
3238 * get requested, we will try again
3239 */
3240 continue;
3241 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3242 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3243 /* must have already done this block */
3244 release_stripe(sh2);
3245 continue;
3246 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003247
3248 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003249 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003250 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003251 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003252 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003253
Dan Williamsa4456852007-07-09 11:56:43 -07003254 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3255 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3256 for (j = 0; j < conf->raid_disks; j++)
3257 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003258 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003259 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3260 break;
3261 if (j == conf->raid_disks) {
3262 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3263 set_bit(STRIPE_HANDLE, &sh2->state);
3264 }
3265 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003266
Dan Williamsa4456852007-07-09 11:56:43 -07003267 }
NeilBrowna2e08552007-09-11 15:23:36 -07003268 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003269 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003270}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
3272/*
3273 * handle_stripe - do things to a stripe.
3274 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003275 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3276 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003278 * return some read requests which now have data
3279 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 * schedule a read on some buffers
3281 * schedule a write of some buffers
3282 * return confirmation of parity correctness
3283 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 */
Dan Williamsa4456852007-07-09 11:56:43 -07003285
NeilBrownacfe7262011-07-27 11:00:36 +10003286static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003287{
NeilBrownd1688a62011-10-11 16:49:52 +11003288 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003289 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003290 struct r5dev *dev;
3291 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003292 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003293
NeilBrownacfe7262011-07-27 11:00:36 +10003294 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003295
NeilBrownacfe7262011-07-27 11:00:36 +10003296 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3297 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3298 s->failed_num[0] = -1;
3299 s->failed_num[1] = -1;
3300
3301 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003302 rcu_read_lock();
3303 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003304 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003305 sector_t first_bad;
3306 int bad_sectors;
3307 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003308
NeilBrown16a53ec2006-06-26 00:27:38 -07003309 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003310
Dan Williams45b42332007-07-09 11:56:43 -07003311 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003312 i, dev->flags,
3313 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003314 /* maybe we can reply to a read
3315 *
3316 * new wantfill requests are only permitted while
3317 * ops_complete_biofill is guaranteed to be inactive
3318 */
3319 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3320 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3321 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003322
3323 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003324 if (test_bit(R5_LOCKED, &dev->flags))
3325 s->locked++;
3326 if (test_bit(R5_UPTODATE, &dev->flags))
3327 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003328 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003329 s->compute++;
3330 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003331 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003332
NeilBrownacfe7262011-07-27 11:00:36 +10003333 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003334 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003335 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003336 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003337 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003338 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003339 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003340 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003341 }
Dan Williamsa4456852007-07-09 11:56:43 -07003342 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003343 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003344 /* Prefer to use the replacement for reads, but only
3345 * if it is recovered enough and has no bad blocks.
3346 */
3347 rdev = rcu_dereference(conf->disks[i].replacement);
3348 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3349 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3350 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3351 &first_bad, &bad_sectors))
3352 set_bit(R5_ReadRepl, &dev->flags);
3353 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003354 if (rdev)
3355 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003356 rdev = rcu_dereference(conf->disks[i].rdev);
3357 clear_bit(R5_ReadRepl, &dev->flags);
3358 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003359 if (rdev && test_bit(Faulty, &rdev->flags))
3360 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003361 if (rdev) {
3362 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3363 &first_bad, &bad_sectors);
3364 if (s->blocked_rdev == NULL
3365 && (test_bit(Blocked, &rdev->flags)
3366 || is_bad < 0)) {
3367 if (is_bad < 0)
3368 set_bit(BlockedBadBlocks,
3369 &rdev->flags);
3370 s->blocked_rdev = rdev;
3371 atomic_inc(&rdev->nr_pending);
3372 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003373 }
NeilBrown415e72d2010-06-17 17:25:21 +10003374 clear_bit(R5_Insync, &dev->flags);
3375 if (!rdev)
3376 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003377 else if (is_bad) {
3378 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003379 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3380 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003381 /* treat as in-sync, but with a read error
3382 * which we can now try to correct
3383 */
3384 set_bit(R5_Insync, &dev->flags);
3385 set_bit(R5_ReadError, &dev->flags);
3386 }
3387 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003388 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003389 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003390 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003391 set_bit(R5_Insync, &dev->flags);
3392 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3393 test_bit(R5_Expanded, &dev->flags))
3394 /* If we've reshaped into here, we assume it is Insync.
3395 * We will shortly update recovery_offset to make
3396 * it official.
3397 */
3398 set_bit(R5_Insync, &dev->flags);
3399
NeilBrownc44bc442014-01-06 13:19:42 +11003400 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003401 /* This flag does not apply to '.replacement'
3402 * only to .rdev, so make sure to check that*/
3403 struct md_rdev *rdev2 = rcu_dereference(
3404 conf->disks[i].rdev);
3405 if (rdev2 == rdev)
3406 clear_bit(R5_Insync, &dev->flags);
3407 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003408 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003409 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003410 } else
3411 clear_bit(R5_WriteError, &dev->flags);
3412 }
NeilBrownc44bc442014-01-06 13:19:42 +11003413 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003414 /* This flag does not apply to '.replacement'
3415 * only to .rdev, so make sure to check that*/
3416 struct md_rdev *rdev2 = rcu_dereference(
3417 conf->disks[i].rdev);
3418 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003419 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003420 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003421 } else
3422 clear_bit(R5_MadeGood, &dev->flags);
3423 }
NeilBrown977df362011-12-23 10:17:53 +11003424 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3425 struct md_rdev *rdev2 = rcu_dereference(
3426 conf->disks[i].replacement);
3427 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3428 s->handle_bad_blocks = 1;
3429 atomic_inc(&rdev2->nr_pending);
3430 } else
3431 clear_bit(R5_MadeGoodRepl, &dev->flags);
3432 }
NeilBrown415e72d2010-06-17 17:25:21 +10003433 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003434 /* The ReadError flag will just be confusing now */
3435 clear_bit(R5_ReadError, &dev->flags);
3436 clear_bit(R5_ReWrite, &dev->flags);
3437 }
NeilBrown415e72d2010-06-17 17:25:21 +10003438 if (test_bit(R5_ReadError, &dev->flags))
3439 clear_bit(R5_Insync, &dev->flags);
3440 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003441 if (s->failed < 2)
3442 s->failed_num[s->failed] = i;
3443 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003444 if (rdev && !test_bit(Faulty, &rdev->flags))
3445 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003446 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003447 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003448 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3449 /* If there is a failed device being replaced,
3450 * we must be recovering.
3451 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003452 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003453 * else we can only be replacing
3454 * sync and recovery both need to read all devices, and so
3455 * use the same flag.
3456 */
3457 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003458 sh->sector >= conf->mddev->recovery_cp ||
3459 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003460 s->syncing = 1;
3461 else
3462 s->replacing = 1;
3463 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003464 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003465}
NeilBrownf4168852007-02-28 20:11:53 -08003466
NeilBrowncc940152011-07-26 11:35:35 +10003467static void handle_stripe(struct stripe_head *sh)
3468{
3469 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003470 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003471 int i;
NeilBrown84789552011-07-27 11:00:36 +10003472 int prexor;
3473 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003474 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003475
3476 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003477 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003478 /* already being handled, ensure it gets handled
3479 * again when current action finishes */
3480 set_bit(STRIPE_HANDLE, &sh->state);
3481 return;
3482 }
3483
NeilBrownf8dfcff2013-03-12 12:18:06 +11003484 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3485 spin_lock(&sh->stripe_lock);
3486 /* Cannot process 'sync' concurrently with 'discard' */
3487 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
3488 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3489 set_bit(STRIPE_SYNCING, &sh->state);
3490 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownc1dadcc2013-07-22 12:57:21 +10003491 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003492 }
3493 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10003494 }
3495 clear_bit(STRIPE_DELAYED, &sh->state);
3496
3497 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3498 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3499 (unsigned long long)sh->sector, sh->state,
3500 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3501 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003502
NeilBrownacfe7262011-07-27 11:00:36 +10003503 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003504
NeilBrownbc2607f2011-07-28 11:39:22 +10003505 if (s.handle_bad_blocks) {
3506 set_bit(STRIPE_HANDLE, &sh->state);
3507 goto finish;
3508 }
3509
NeilBrown474af965fe2011-07-27 11:00:36 +10003510 if (unlikely(s.blocked_rdev)) {
3511 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003512 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003513 set_bit(STRIPE_HANDLE, &sh->state);
3514 goto finish;
3515 }
3516 /* There is nothing for the blocked_rdev to block */
3517 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3518 s.blocked_rdev = NULL;
3519 }
3520
3521 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3522 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3523 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3524 }
3525
3526 pr_debug("locked=%d uptodate=%d to_read=%d"
3527 " to_write=%d failed=%d failed_num=%d,%d\n",
3528 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3529 s.failed_num[0], s.failed_num[1]);
3530 /* check if the array has lost more than max_degraded devices and,
3531 * if so, some requests might need to be failed.
3532 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003533 if (s.failed > conf->max_degraded) {
3534 sh->check_state = 0;
3535 sh->reconstruct_state = 0;
3536 if (s.to_read+s.to_write+s.written)
3537 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003538 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003539 handle_failed_sync(conf, sh, &s);
3540 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003541
NeilBrown84789552011-07-27 11:00:36 +10003542 /* Now we check to see if any write operations have recently
3543 * completed
3544 */
3545 prexor = 0;
3546 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3547 prexor = 1;
3548 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3549 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3550 sh->reconstruct_state = reconstruct_state_idle;
3551
3552 /* All the 'written' buffers and the parity block are ready to
3553 * be written back to disk
3554 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003555 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3556 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003557 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003558 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3559 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003560 for (i = disks; i--; ) {
3561 struct r5dev *dev = &sh->dev[i];
3562 if (test_bit(R5_LOCKED, &dev->flags) &&
3563 (i == sh->pd_idx || i == sh->qd_idx ||
3564 dev->written)) {
3565 pr_debug("Writing block %d\n", i);
3566 set_bit(R5_Wantwrite, &dev->flags);
3567 if (prexor)
3568 continue;
NeilBrown318a3d52014-08-13 09:57:07 +10003569 if (s.failed > 1)
3570 continue;
NeilBrown84789552011-07-27 11:00:36 +10003571 if (!test_bit(R5_Insync, &dev->flags) ||
3572 ((i == sh->pd_idx || i == sh->qd_idx) &&
3573 s.failed == 0))
3574 set_bit(STRIPE_INSYNC, &sh->state);
3575 }
3576 }
3577 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3578 s.dec_preread_active = 1;
3579 }
3580
NeilBrownef5b7c62012-11-22 09:13:36 +11003581 /*
3582 * might be able to return some write requests if the parity blocks
3583 * are safe, or on a failed drive
3584 */
3585 pdev = &sh->dev[sh->pd_idx];
3586 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3587 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3588 qdev = &sh->dev[sh->qd_idx];
3589 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3590 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3591 || conf->level < 6;
3592
3593 if (s.written &&
3594 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3595 && !test_bit(R5_LOCKED, &pdev->flags)
3596 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3597 test_bit(R5_Discard, &pdev->flags))))) &&
3598 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3599 && !test_bit(R5_LOCKED, &qdev->flags)
3600 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3601 test_bit(R5_Discard, &qdev->flags))))))
3602 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3603
3604 /* Now we might consider reading some blocks, either to check/generate
3605 * parity, or to satisfy requests
3606 * or to load a block that is being partially written.
3607 */
3608 if (s.to_read || s.non_overwrite
3609 || (conf->level == 6 && s.to_write && s.failed)
3610 || (s.syncing && (s.uptodate + s.compute < disks))
3611 || s.replacing
3612 || s.expanding)
3613 handle_stripe_fill(sh, &s, disks);
3614
NeilBrown84789552011-07-27 11:00:36 +10003615 /* Now to consider new write requests and what else, if anything
3616 * should be read. We do not handle new writes when:
3617 * 1/ A 'write' operation (copy+xor) is already in flight.
3618 * 2/ A 'check' operation is in flight, as it may clobber the parity
3619 * block.
3620 */
3621 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3622 handle_stripe_dirtying(conf, sh, &s, disks);
3623
3624 /* maybe we need to check and possibly fix the parity for this stripe
3625 * Any reads will already have been scheduled, so we just see if enough
3626 * data is available. The parity check is held off while parity
3627 * dependent operations are in flight.
3628 */
3629 if (sh->check_state ||
3630 (s.syncing && s.locked == 0 &&
3631 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3632 !test_bit(STRIPE_INSYNC, &sh->state))) {
3633 if (conf->level == 6)
3634 handle_parity_checks6(conf, sh, &s, disks);
3635 else
3636 handle_parity_checks5(conf, sh, &s, disks);
3637 }
NeilBrownc5a31002011-07-27 11:00:36 +10003638
NeilBrownc1dadcc2013-07-22 12:57:21 +10003639 if ((s.replacing || s.syncing) && s.locked == 0
3640 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
3641 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11003642 /* Write out to replacement devices where possible */
3643 for (i = 0; i < conf->raid_disks; i++)
NeilBrownc1dadcc2013-07-22 12:57:21 +10003644 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3645 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11003646 set_bit(R5_WantReplace, &sh->dev[i].flags);
3647 set_bit(R5_LOCKED, &sh->dev[i].flags);
3648 s.locked++;
3649 }
NeilBrownc1dadcc2013-07-22 12:57:21 +10003650 if (s.replacing)
3651 set_bit(STRIPE_INSYNC, &sh->state);
3652 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11003653 }
3654 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownc1dadcc2013-07-22 12:57:21 +10003655 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11003656 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003657 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3658 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003659 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3660 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10003661 }
3662
3663 /* If the failed drives are just a ReadError, then we might need
3664 * to progress the repair/check process
3665 */
3666 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3667 for (i = 0; i < s.failed; i++) {
3668 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3669 if (test_bit(R5_ReadError, &dev->flags)
3670 && !test_bit(R5_LOCKED, &dev->flags)
3671 && test_bit(R5_UPTODATE, &dev->flags)
3672 ) {
3673 if (!test_bit(R5_ReWrite, &dev->flags)) {
3674 set_bit(R5_Wantwrite, &dev->flags);
3675 set_bit(R5_ReWrite, &dev->flags);
3676 set_bit(R5_LOCKED, &dev->flags);
3677 s.locked++;
3678 } else {
3679 /* let's read it back */
3680 set_bit(R5_Wantread, &dev->flags);
3681 set_bit(R5_LOCKED, &dev->flags);
3682 s.locked++;
3683 }
3684 }
3685 }
3686
3687
NeilBrown3687c062011-07-27 11:00:36 +10003688 /* Finish reconstruct operations initiated by the expansion process */
3689 if (sh->reconstruct_state == reconstruct_state_result) {
3690 struct stripe_head *sh_src
3691 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3692 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3693 /* sh cannot be written until sh_src has been read.
3694 * so arrange for sh to be delayed a little
3695 */
3696 set_bit(STRIPE_DELAYED, &sh->state);
3697 set_bit(STRIPE_HANDLE, &sh->state);
3698 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3699 &sh_src->state))
3700 atomic_inc(&conf->preread_active_stripes);
3701 release_stripe(sh_src);
3702 goto finish;
3703 }
3704 if (sh_src)
3705 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003706
NeilBrown3687c062011-07-27 11:00:36 +10003707 sh->reconstruct_state = reconstruct_state_idle;
3708 clear_bit(STRIPE_EXPANDING, &sh->state);
3709 for (i = conf->raid_disks; i--; ) {
3710 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3711 set_bit(R5_LOCKED, &sh->dev[i].flags);
3712 s.locked++;
3713 }
3714 }
3715
3716 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3717 !sh->reconstruct_state) {
3718 /* Need to write out all blocks after computing parity */
3719 sh->disks = conf->raid_disks;
3720 stripe_set_idx(sh->sector, conf, 0, sh);
3721 schedule_reconstruction(sh, &s, 1, 1);
3722 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3723 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3724 atomic_dec(&conf->reshape_stripes);
3725 wake_up(&conf->wait_for_overlap);
3726 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3727 }
3728
3729 if (s.expanding && s.locked == 0 &&
3730 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3731 handle_stripe_expansion(conf, sh);
3732
3733finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003734 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003735 if (unlikely(s.blocked_rdev)) {
3736 if (conf->mddev->external)
3737 md_wait_for_blocked_rdev(s.blocked_rdev,
3738 conf->mddev);
3739 else
3740 /* Internal metadata will immediately
3741 * be written by raid5d, so we don't
3742 * need to wait here.
3743 */
3744 rdev_dec_pending(s.blocked_rdev,
3745 conf->mddev);
3746 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003747
NeilBrownbc2607f2011-07-28 11:39:22 +10003748 if (s.handle_bad_blocks)
3749 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003750 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003751 struct r5dev *dev = &sh->dev[i];
3752 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3753 /* We own a safe reference to the rdev */
3754 rdev = conf->disks[i].rdev;
3755 if (!rdev_set_badblocks(rdev, sh->sector,
3756 STRIPE_SECTORS, 0))
3757 md_error(conf->mddev, rdev);
3758 rdev_dec_pending(rdev, conf->mddev);
3759 }
NeilBrownb84db562011-07-28 11:39:23 +10003760 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3761 rdev = conf->disks[i].rdev;
3762 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003763 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003764 rdev_dec_pending(rdev, conf->mddev);
3765 }
NeilBrown977df362011-12-23 10:17:53 +11003766 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3767 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003768 if (!rdev)
3769 /* rdev have been moved down */
3770 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003771 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003772 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003773 rdev_dec_pending(rdev, conf->mddev);
3774 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003775 }
3776
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003777 if (s.ops_request)
3778 raid_run_ops(sh, s.ops_request);
3779
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003780 ops_run_io(sh, &s);
3781
NeilBrownc5709ef2011-07-26 11:35:20 +10003782 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003783 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003784 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003785 * have actually been submitted.
3786 */
3787 atomic_dec(&conf->preread_active_stripes);
3788 if (atomic_read(&conf->preread_active_stripes) <
3789 IO_THRESHOLD)
3790 md_wakeup_thread(conf->mddev->thread);
3791 }
3792
NeilBrownc5709ef2011-07-26 11:35:20 +10003793 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003794
Dan Williams257a4b42011-11-08 16:22:06 +11003795 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003796}
3797
NeilBrownd1688a62011-10-11 16:49:52 +11003798static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799{
3800 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3801 while (!list_empty(&conf->delayed_list)) {
3802 struct list_head *l = conf->delayed_list.next;
3803 struct stripe_head *sh;
3804 sh = list_entry(l, struct stripe_head, lru);
3805 list_del_init(l);
3806 clear_bit(STRIPE_DELAYED, &sh->state);
3807 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3808 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003809 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 }
NeilBrown482c0832011-04-18 18:25:42 +10003811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812}
3813
NeilBrownd1688a62011-10-11 16:49:52 +11003814static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003815{
3816 /* device_lock is held */
3817 struct list_head head;
3818 list_add(&head, &conf->bitmap_list);
3819 list_del_init(&conf->bitmap_list);
3820 while (!list_empty(&head)) {
3821 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3822 list_del_init(&sh->lru);
3823 atomic_inc(&sh->count);
3824 __release_stripe(conf, sh);
3825 }
3826}
3827
NeilBrownfd01b882011-10-11 16:47:53 +11003828int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003829{
NeilBrownd1688a62011-10-11 16:49:52 +11003830 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003831
3832 /* No difference between reads and writes. Just check
3833 * how busy the stripe_cache is
3834 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003835
NeilBrownf022b2f2006-10-03 01:15:56 -07003836 if (conf->inactive_blocked)
3837 return 1;
3838 if (conf->quiesce)
3839 return 1;
3840 if (list_empty_careful(&conf->inactive_list))
3841 return 1;
3842
3843 return 0;
3844}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003845EXPORT_SYMBOL_GPL(md_raid5_congested);
3846
3847static int raid5_congested(void *data, int bits)
3848{
NeilBrownfd01b882011-10-11 16:47:53 +11003849 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003850
3851 return mddev_congested(mddev, bits) ||
3852 md_raid5_congested(mddev, bits);
3853}
NeilBrownf022b2f2006-10-03 01:15:56 -07003854
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003855/* We want read requests to align with chunks where possible,
3856 * but write requests don't need to.
3857 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003858static int raid5_mergeable_bvec(struct request_queue *q,
3859 struct bvec_merge_data *bvm,
3860 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003861{
NeilBrownfd01b882011-10-11 16:47:53 +11003862 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003863 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003864 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003865 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003866 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003867
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003868 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003869 return biovec->bv_len; /* always allow writes to be mergeable */
3870
Andre Noll664e7c42009-06-18 08:45:27 +10003871 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3872 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003873 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3874 if (max < 0) max = 0;
3875 if (max <= biovec->bv_len && bio_sectors == 0)
3876 return biovec->bv_len;
3877 else
3878 return max;
3879}
3880
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003881
NeilBrownfd01b882011-10-11 16:47:53 +11003882static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003883{
3884 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003885 unsigned int chunk_sectors = mddev->chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08003886 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003887
Andre Noll664e7c42009-06-18 08:45:27 +10003888 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3889 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003890 return chunk_sectors >=
3891 ((sector & (chunk_sectors - 1)) + bio_sectors);
3892}
3893
3894/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003895 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3896 * later sampled by raid5d.
3897 */
NeilBrownd1688a62011-10-11 16:49:52 +11003898static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003899{
3900 unsigned long flags;
3901
3902 spin_lock_irqsave(&conf->device_lock, flags);
3903
3904 bi->bi_next = conf->retry_read_aligned_list;
3905 conf->retry_read_aligned_list = bi;
3906
3907 spin_unlock_irqrestore(&conf->device_lock, flags);
3908 md_wakeup_thread(conf->mddev->thread);
3909}
3910
3911
NeilBrownd1688a62011-10-11 16:49:52 +11003912static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003913{
3914 struct bio *bi;
3915
3916 bi = conf->retry_read_aligned;
3917 if (bi) {
3918 conf->retry_read_aligned = NULL;
3919 return bi;
3920 }
3921 bi = conf->retry_read_aligned_list;
3922 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003923 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003924 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003925 /*
3926 * this sets the active strip count to 1 and the processed
3927 * strip count to zero (upper 8 bits)
3928 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003929 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003930 }
3931
3932 return bi;
3933}
3934
3935
3936/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003937 * The "raid5_align_endio" should check if the read succeeded and if it
3938 * did, call bio_endio on the original bio (having bio_put the new bio
3939 * first).
3940 * If the read failed..
3941 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003942static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003943{
3944 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003945 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003946 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003947 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003948 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003949
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003950 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003951
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003952 rdev = (void*)raid_bi->bi_next;
3953 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003954 mddev = rdev->mddev;
3955 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003956
3957 rdev_dec_pending(rdev, conf->mddev);
3958
3959 if (!error && uptodate) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07003960 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
3961 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02003962 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003963 if (atomic_dec_and_test(&conf->active_aligned_reads))
3964 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003965 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003966 }
3967
3968
Dan Williams45b42332007-07-09 11:56:43 -07003969 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003970
3971 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003972}
3973
Neil Brown387bb172007-02-08 14:20:29 -08003974static int bio_fits_rdev(struct bio *bi)
3975{
Jens Axboe165125e2007-07-24 09:28:11 +02003976 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003977
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08003978 if (bio_sectors(bi) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003979 return 0;
3980 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003981 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003982 return 0;
3983
3984 if (q->merge_bvec_fn)
3985 /* it's too hard to apply the merge_bvec_fn at this stage,
3986 * just just give up
3987 */
3988 return 0;
3989
3990 return 1;
3991}
3992
3993
NeilBrownfd01b882011-10-11 16:47:53 +11003994static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003995{
NeilBrownd1688a62011-10-11 16:49:52 +11003996 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003997 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003998 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003999 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11004000 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004001
4002 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07004003 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004004 return 0;
4005 }
4006 /*
NeilBrowna167f662010-10-26 18:31:13 +11004007 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004008 */
NeilBrowna167f662010-10-26 18:31:13 +11004009 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004010 if (!align_bi)
4011 return 0;
4012 /*
4013 * set bi_end_io to a new function, and set bi_private to the
4014 * original bio.
4015 */
4016 align_bi->bi_end_io = raid5_align_endio;
4017 align_bi->bi_private = raid_bio;
4018 /*
4019 * compute position
4020 */
NeilBrown112bf892009-03-31 14:39:38 +11004021 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
4022 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11004023 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004024
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004025 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004026 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11004027 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4028 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4029 rdev->recovery_offset < end_sector) {
4030 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4031 if (rdev &&
4032 (test_bit(Faulty, &rdev->flags) ||
4033 !(test_bit(In_sync, &rdev->flags) ||
4034 rdev->recovery_offset >= end_sector)))
4035 rdev = NULL;
4036 }
4037 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10004038 sector_t first_bad;
4039 int bad_sectors;
4040
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004041 atomic_inc(&rdev->nr_pending);
4042 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004043 raid_bio->bi_next = (void*)rdev;
4044 align_bi->bi_bdev = rdev->bdev;
4045 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004046
NeilBrown31c176e2011-07-28 11:39:22 +10004047 if (!bio_fits_rdev(align_bi) ||
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004048 is_badblock(rdev, align_bi->bi_sector, bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10004049 &first_bad, &bad_sectors)) {
4050 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004051 bio_put(align_bi);
4052 rdev_dec_pending(rdev, mddev);
4053 return 0;
4054 }
4055
majianpeng6c0544e2012-06-12 08:31:10 +08004056 /* No reshape active, so we can trust rdev->data_offset */
4057 align_bi->bi_sector += rdev->data_offset;
4058
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004059 spin_lock_irq(&conf->device_lock);
4060 wait_event_lock_irq(conf->wait_for_stripe,
4061 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004062 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004063 atomic_inc(&conf->active_aligned_reads);
4064 spin_unlock_irq(&conf->device_lock);
4065
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004066 if (mddev->gendisk)
4067 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4068 align_bi, disk_devt(mddev->gendisk),
4069 raid_bio->bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004070 generic_make_request(align_bi);
4071 return 1;
4072 } else {
4073 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004074 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004075 return 0;
4076 }
4077}
4078
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004079/* __get_priority_stripe - get the next stripe to process
4080 *
4081 * Full stripe writes are allowed to pass preread active stripes up until
4082 * the bypass_threshold is exceeded. In general the bypass_count
4083 * increments when the handle_list is handled before the hold_list; however, it
4084 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4085 * stripe with in flight i/o. The bypass_count will be reset when the
4086 * head of the hold_list has changed, i.e. the head was promoted to the
4087 * handle_list.
4088 */
NeilBrownd1688a62011-10-11 16:49:52 +11004089static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004090{
4091 struct stripe_head *sh;
4092
4093 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4094 __func__,
4095 list_empty(&conf->handle_list) ? "empty" : "busy",
4096 list_empty(&conf->hold_list) ? "empty" : "busy",
4097 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4098
4099 if (!list_empty(&conf->handle_list)) {
4100 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4101
4102 if (list_empty(&conf->hold_list))
4103 conf->bypass_count = 0;
4104 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4105 if (conf->hold_list.next == conf->last_hold)
4106 conf->bypass_count++;
4107 else {
4108 conf->last_hold = conf->hold_list.next;
4109 conf->bypass_count -= conf->bypass_threshold;
4110 if (conf->bypass_count < 0)
4111 conf->bypass_count = 0;
4112 }
4113 }
4114 } else if (!list_empty(&conf->hold_list) &&
4115 ((conf->bypass_threshold &&
4116 conf->bypass_count > conf->bypass_threshold) ||
4117 atomic_read(&conf->pending_full_writes) == 0)) {
4118 sh = list_entry(conf->hold_list.next,
4119 typeof(*sh), lru);
4120 conf->bypass_count -= conf->bypass_threshold;
4121 if (conf->bypass_count < 0)
4122 conf->bypass_count = 0;
4123 } else
4124 return NULL;
4125
4126 list_del_init(&sh->lru);
4127 atomic_inc(&sh->count);
4128 BUG_ON(atomic_read(&sh->count) != 1);
4129 return sh;
4130}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004131
Shaohua Li8811b592012-08-02 08:33:00 +10004132struct raid5_plug_cb {
4133 struct blk_plug_cb cb;
4134 struct list_head list;
4135};
4136
4137static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4138{
4139 struct raid5_plug_cb *cb = container_of(
4140 blk_cb, struct raid5_plug_cb, cb);
4141 struct stripe_head *sh;
4142 struct mddev *mddev = cb->cb.data;
4143 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004144 int cnt = 0;
Shaohua Li8811b592012-08-02 08:33:00 +10004145
4146 if (cb->list.next && !list_empty(&cb->list)) {
4147 spin_lock_irq(&conf->device_lock);
4148 while (!list_empty(&cb->list)) {
4149 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4150 list_del_init(&sh->lru);
4151 /*
4152 * avoid race release_stripe_plug() sees
4153 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4154 * is still in our list
4155 */
4156 smp_mb__before_clear_bit();
4157 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4158 __release_stripe(conf, sh);
NeilBrowna9add5d2012-10-31 11:59:09 +11004159 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004160 }
4161 spin_unlock_irq(&conf->device_lock);
4162 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004163 if (mddev->queue)
4164 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004165 kfree(cb);
4166}
4167
4168static void release_stripe_plug(struct mddev *mddev,
4169 struct stripe_head *sh)
4170{
4171 struct blk_plug_cb *blk_cb = blk_check_plugged(
4172 raid5_unplug, mddev,
4173 sizeof(struct raid5_plug_cb));
4174 struct raid5_plug_cb *cb;
4175
4176 if (!blk_cb) {
4177 release_stripe(sh);
4178 return;
4179 }
4180
4181 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4182
4183 if (cb->list.next == NULL)
4184 INIT_LIST_HEAD(&cb->list);
4185
4186 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4187 list_add_tail(&sh->lru, &cb->list);
4188 else
4189 release_stripe(sh);
4190}
4191
Shaohua Li620125f2012-10-11 13:49:05 +11004192static void make_discard_request(struct mddev *mddev, struct bio *bi)
4193{
4194 struct r5conf *conf = mddev->private;
4195 sector_t logical_sector, last_sector;
4196 struct stripe_head *sh;
4197 int remaining;
4198 int stripe_sectors;
4199
4200 if (mddev->reshape_position != MaxSector)
4201 /* Skip discard while reshape is happening */
4202 return;
4203
4204 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4205 last_sector = bi->bi_sector + (bi->bi_size>>9);
4206
4207 bi->bi_next = NULL;
4208 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4209
4210 stripe_sectors = conf->chunk_sectors *
4211 (conf->raid_disks - conf->max_degraded);
4212 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4213 stripe_sectors);
4214 sector_div(last_sector, stripe_sectors);
4215
4216 logical_sector *= conf->chunk_sectors;
4217 last_sector *= conf->chunk_sectors;
4218
4219 for (; logical_sector < last_sector;
4220 logical_sector += STRIPE_SECTORS) {
4221 DEFINE_WAIT(w);
4222 int d;
4223 again:
4224 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4225 prepare_to_wait(&conf->wait_for_overlap, &w,
4226 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004227 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4228 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4229 release_stripe(sh);
4230 schedule();
4231 goto again;
4232 }
4233 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11004234 spin_lock_irq(&sh->stripe_lock);
4235 for (d = 0; d < conf->raid_disks; d++) {
4236 if (d == sh->pd_idx || d == sh->qd_idx)
4237 continue;
4238 if (sh->dev[d].towrite || sh->dev[d].toread) {
4239 set_bit(R5_Overlap, &sh->dev[d].flags);
4240 spin_unlock_irq(&sh->stripe_lock);
4241 release_stripe(sh);
4242 schedule();
4243 goto again;
4244 }
4245 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11004246 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11004247 finish_wait(&conf->wait_for_overlap, &w);
4248 for (d = 0; d < conf->raid_disks; d++) {
4249 if (d == sh->pd_idx || d == sh->qd_idx)
4250 continue;
4251 sh->dev[d].towrite = bi;
4252 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4253 raid5_inc_bi_active_stripes(bi);
4254 }
4255 spin_unlock_irq(&sh->stripe_lock);
4256 if (conf->mddev->bitmap) {
4257 for (d = 0;
4258 d < conf->raid_disks - conf->max_degraded;
4259 d++)
4260 bitmap_startwrite(mddev->bitmap,
4261 sh->sector,
4262 STRIPE_SECTORS,
4263 0);
4264 sh->bm_seq = conf->seq_flush + 1;
4265 set_bit(STRIPE_BIT_DELAY, &sh->state);
4266 }
4267
4268 set_bit(STRIPE_HANDLE, &sh->state);
4269 clear_bit(STRIPE_DELAYED, &sh->state);
4270 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4271 atomic_inc(&conf->preread_active_stripes);
4272 release_stripe_plug(mddev, sh);
4273 }
4274
4275 remaining = raid5_dec_bi_active_stripes(bi);
4276 if (remaining == 0) {
4277 md_write_end(mddev);
4278 bio_endio(bi, 0);
4279 }
4280}
4281
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004282static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283{
NeilBrownd1688a62011-10-11 16:49:52 +11004284 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004285 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 sector_t new_sector;
4287 sector_t logical_sector, last_sector;
4288 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004289 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004290 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
Tejun Heoe9c74692010-09-03 11:56:18 +02004292 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4293 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004294 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004295 }
4296
NeilBrown3d310eb2005-06-21 17:17:26 -07004297 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004298
NeilBrown802ba062006-12-13 00:34:13 -08004299 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004300 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004301 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004302 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004303
Shaohua Li620125f2012-10-11 13:49:05 +11004304 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4305 make_discard_request(mddev, bi);
4306 return;
4307 }
4308
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004310 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 bi->bi_next = NULL;
4312 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004313
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4315 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004316 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004317
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004318 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004319 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004320 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004321 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004322 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004323 * 64bit on a 32bit platform, and so it might be
4324 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004325 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004326 * the lock is dropped, so once we get a reference
4327 * to the stripe that we think it is, we will have
4328 * to check again.
4329 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004330 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004331 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004332 ? logical_sector < conf->reshape_progress
4333 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004334 previous = 1;
4335 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004336 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004337 ? logical_sector < conf->reshape_safe
4338 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004339 spin_unlock_irq(&conf->device_lock);
4340 schedule();
4341 goto retry;
4342 }
4343 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004344 spin_unlock_irq(&conf->device_lock);
4345 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004346
NeilBrown112bf892009-03-31 14:39:38 +11004347 new_sector = raid5_compute_sector(conf, logical_sector,
4348 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004349 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004350 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 (unsigned long long)new_sector,
4352 (unsigned long long)logical_sector);
4353
NeilBrownb5663ba2009-03-31 14:39:38 +11004354 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004355 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004357 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004358 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004359 * stripe, so we must do the range check again.
4360 * Expansion could still move past after this
4361 * test, but as we are holding a reference to
4362 * 'sh', we know that if that happens,
4363 * STRIPE_EXPANDING will get set and the expansion
4364 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004365 */
4366 int must_retry = 0;
4367 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004368 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004369 ? logical_sector >= conf->reshape_progress
4370 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004371 /* mismatch, need to try again */
4372 must_retry = 1;
4373 spin_unlock_irq(&conf->device_lock);
4374 if (must_retry) {
4375 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004376 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004377 goto retry;
4378 }
4379 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004380
Namhyung Kimffd96e32011-07-18 17:38:51 +10004381 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004382 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004383 logical_sector < mddev->suspend_hi) {
4384 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004385 /* As the suspend_* range is controlled by
4386 * userspace, we want an interruptible
4387 * wait.
4388 */
4389 flush_signals(current);
4390 prepare_to_wait(&conf->wait_for_overlap,
4391 &w, TASK_INTERRUPTIBLE);
4392 if (logical_sector >= mddev->suspend_lo &&
4393 logical_sector < mddev->suspend_hi)
4394 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004395 goto retry;
4396 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004397
4398 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004399 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004400 /* Stripe is busy expanding or
4401 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 * and wait a while
4403 */
NeilBrown482c0832011-04-18 18:25:42 +10004404 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 release_stripe(sh);
4406 schedule();
4407 goto retry;
4408 }
4409 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004410 set_bit(STRIPE_HANDLE, &sh->state);
4411 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004412 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004413 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4414 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004415 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 } else {
4417 /* cannot get stripe for read-ahead, just give-up */
4418 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4419 finish_wait(&conf->wait_for_overlap, &w);
4420 break;
4421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004423
Shaohua Lie7836bd62012-07-19 16:01:31 +10004424 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004425 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
NeilBrown16a53ec2006-06-26 00:27:38 -07004427 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004429
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004430 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4431 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004432 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434}
4435
NeilBrownfd01b882011-10-11 16:47:53 +11004436static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004437
NeilBrownfd01b882011-10-11 16:47:53 +11004438static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439{
NeilBrown52c03292006-06-26 00:27:43 -07004440 /* reshaping is quite different to recovery/resync so it is
4441 * handled quite separately ... here.
4442 *
4443 * On each call to sync_request, we gather one chunk worth of
4444 * destination stripes and flag them as expanding.
4445 * Then we find all the source stripes and request reads.
4446 * As the reads complete, handle_stripe will copy the data
4447 * into the destination stripe and release that stripe.
4448 */
NeilBrownd1688a62011-10-11 16:49:52 +11004449 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004451 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004452 int raid_disks = conf->previous_raid_disks;
4453 int data_disks = raid_disks - conf->max_degraded;
4454 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004455 int i;
4456 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004457 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004458 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004459 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004460 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004461
NeilBrownfef9c612009-03-31 15:16:46 +11004462 if (sector_nr == 0) {
4463 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004464 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004465 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4466 sector_nr = raid5_size(mddev, 0, 0)
4467 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004468 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004469 conf->reshape_progress > 0)
4470 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004471 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004472 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004473 mddev->curr_resync_completed = sector_nr;
4474 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004475 *skipped = 1;
4476 return sector_nr;
4477 }
NeilBrown52c03292006-06-26 00:27:43 -07004478 }
4479
NeilBrown7a661382009-03-31 15:21:40 +11004480 /* We need to process a full chunk at a time.
4481 * If old and new chunk sizes differ, we need to process the
4482 * largest of these
4483 */
Andre Noll664e7c42009-06-18 08:45:27 +10004484 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4485 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004486 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004487 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004488
NeilBrownb5254dd2012-05-21 09:27:01 +10004489 /* We update the metadata at least every 10 seconds, or when
4490 * the data about to be copied would over-write the source of
4491 * the data at the front of the range. i.e. one new_stripe
4492 * along from reshape_progress new_maps to after where
4493 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004494 */
NeilBrownfef9c612009-03-31 15:16:46 +11004495 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004496 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004497 readpos = conf->reshape_progress;
4498 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004499 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004500 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004501 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004502 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004503 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004504 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004505 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004506 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004507 readpos -= min_t(sector_t, reshape_sectors, readpos);
4508 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004509 }
NeilBrown52c03292006-06-26 00:27:43 -07004510
NeilBrownb5254dd2012-05-21 09:27:01 +10004511 /* Having calculated the 'writepos' possibly use it
4512 * to set 'stripe_addr' which is where we will write to.
4513 */
4514 if (mddev->reshape_backwards) {
4515 BUG_ON(conf->reshape_progress == 0);
4516 stripe_addr = writepos;
4517 BUG_ON((mddev->dev_sectors &
4518 ~((sector_t)reshape_sectors - 1))
4519 - reshape_sectors - stripe_addr
4520 != sector_nr);
4521 } else {
4522 BUG_ON(writepos != sector_nr + reshape_sectors);
4523 stripe_addr = sector_nr;
4524 }
4525
NeilBrownc8f517c2009-03-31 15:28:40 +11004526 /* 'writepos' is the most advanced device address we might write.
4527 * 'readpos' is the least advanced device address we might read.
4528 * 'safepos' is the least address recorded in the metadata as having
4529 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004530 * If there is a min_offset_diff, these are adjusted either by
4531 * increasing the safepos/readpos if diff is negative, or
4532 * increasing writepos if diff is positive.
4533 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004534 * ensure safety in the face of a crash - that must be done by userspace
4535 * making a backup of the data. So in that case there is no particular
4536 * rush to update metadata.
4537 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4538 * update the metadata to advance 'safepos' to match 'readpos' so that
4539 * we can be safe in the event of a crash.
4540 * So we insist on updating metadata if safepos is behind writepos and
4541 * readpos is beyond writepos.
4542 * In any case, update the metadata every 10 seconds.
4543 * Maybe that number should be configurable, but I'm not sure it is
4544 * worth it.... maybe it could be a multiple of safemode_delay???
4545 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004546 if (conf->min_offset_diff < 0) {
4547 safepos += -conf->min_offset_diff;
4548 readpos += -conf->min_offset_diff;
4549 } else
4550 writepos += conf->min_offset_diff;
4551
NeilBrown2c810cd2012-05-21 09:27:00 +10004552 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004553 ? (safepos > writepos && readpos < writepos)
4554 : (safepos < writepos && readpos > writepos)) ||
4555 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004556 /* Cannot proceed until we've updated the superblock... */
4557 wait_event(conf->wait_for_overlap,
4558 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004559 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004560 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004561 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004562 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004563 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004564 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004565 kthread_should_stop());
4566 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004567 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004568 spin_unlock_irq(&conf->device_lock);
4569 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004570 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004571 }
4572
NeilBrownab69ae12009-03-31 15:26:47 +11004573 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004574 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004575 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004576 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004577 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004578 set_bit(STRIPE_EXPANDING, &sh->state);
4579 atomic_inc(&conf->reshape_stripes);
4580 /* If any of this stripe is beyond the end of the old
4581 * array, then we need to zero those blocks
4582 */
4583 for (j=sh->disks; j--;) {
4584 sector_t s;
4585 if (j == sh->pd_idx)
4586 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004587 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004588 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004589 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004590 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004591 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004592 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004593 continue;
4594 }
4595 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4596 set_bit(R5_Expanded, &sh->dev[j].flags);
4597 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4598 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004599 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004600 set_bit(STRIPE_EXPAND_READY, &sh->state);
4601 set_bit(STRIPE_HANDLE, &sh->state);
4602 }
NeilBrownab69ae12009-03-31 15:26:47 +11004603 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004604 }
4605 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004606 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004607 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004608 else
NeilBrown7a661382009-03-31 15:21:40 +11004609 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004610 spin_unlock_irq(&conf->device_lock);
4611 /* Ok, those stripe are ready. We can start scheduling
4612 * reads on the source stripes.
4613 * The source stripes are determined by mapping the first and last
4614 * block on the destination stripes.
4615 */
NeilBrown52c03292006-06-26 00:27:43 -07004616 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004617 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004618 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004619 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004620 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004621 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004622 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004623 if (last_sector >= mddev->dev_sectors)
4624 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004625 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004626 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004627 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4628 set_bit(STRIPE_HANDLE, &sh->state);
4629 release_stripe(sh);
4630 first_sector += STRIPE_SECTORS;
4631 }
NeilBrownab69ae12009-03-31 15:26:47 +11004632 /* Now that the sources are clearly marked, we can release
4633 * the destination stripes
4634 */
4635 while (!list_empty(&stripes)) {
4636 sh = list_entry(stripes.next, struct stripe_head, lru);
4637 list_del_init(&sh->lru);
4638 release_stripe(sh);
4639 }
NeilBrownc6207272008-02-06 01:39:52 -08004640 /* If this takes us to the resync_max point where we have to pause,
4641 * then we need to write out the superblock.
4642 */
NeilBrown7a661382009-03-31 15:21:40 +11004643 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004644 if ((sector_nr - mddev->curr_resync_completed) * 2
4645 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004646 /* Cannot proceed until we've updated the superblock... */
4647 wait_event(conf->wait_for_overlap,
4648 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004649 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004650 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004651 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004652 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4653 md_wakeup_thread(mddev->thread);
4654 wait_event(mddev->sb_wait,
4655 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4656 || kthread_should_stop());
4657 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004658 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004659 spin_unlock_irq(&conf->device_lock);
4660 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004661 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004662 }
NeilBrown7a661382009-03-31 15:21:40 +11004663 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004664}
4665
4666/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004667static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004668{
NeilBrownd1688a62011-10-11 16:49:52 +11004669 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004670 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004671 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004672 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004673 int still_degraded = 0;
4674 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
NeilBrown72626682005-09-09 16:23:54 -07004676 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004678
NeilBrown29269552006-03-27 01:18:10 -08004679 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4680 end_reshape(conf);
4681 return 0;
4682 }
NeilBrown72626682005-09-09 16:23:54 -07004683
4684 if (mddev->curr_resync < max_sector) /* aborted */
4685 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4686 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004687 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004688 conf->fullsync = 0;
4689 bitmap_close_sync(mddev->bitmap);
4690
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 return 0;
4692 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004693
NeilBrown64bd6602009-08-03 10:59:58 +10004694 /* Allow raid5_quiesce to complete */
4695 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4696
NeilBrown52c03292006-06-26 00:27:43 -07004697 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4698 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004699
NeilBrownc6207272008-02-06 01:39:52 -08004700 /* No need to check resync_max as we never do more than one
4701 * stripe, and as resync_max will always be on a chunk boundary,
4702 * if the check in md_do_sync didn't fire, there is no chance
4703 * of overstepping resync_max here
4704 */
4705
NeilBrown16a53ec2006-06-26 00:27:38 -07004706 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 * to resync, then assert that we are finished, because there is
4708 * nothing we can do.
4709 */
NeilBrown3285edf2006-06-26 00:27:55 -07004710 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004711 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004712 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004713 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 return rv;
4715 }
majianpeng6f608042013-04-24 11:42:41 +10004716 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
4717 !conf->fullsync &&
4718 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
4719 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07004720 /* we can skip this block, and probably more */
4721 sync_blocks /= STRIPE_SECTORS;
4722 *skipped = 1;
4723 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725
NeilBrownb47490c2008-02-06 01:39:50 -08004726 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4727
NeilBrowna8c906c2009-06-09 14:39:59 +10004728 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004730 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004732 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004734 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004736 /* Need to check if array will still be degraded after recovery/resync
4737 * We don't need to check the 'failed' flag as when that gets set,
4738 * recovery aborts.
4739 */
NeilBrownf001a702009-06-09 14:30:31 +10004740 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004741 if (conf->disks[i].rdev == NULL)
4742 still_degraded = 1;
4743
4744 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4745
NeilBrown83206d62011-07-26 11:19:49 +10004746 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
NeilBrown14425772009-10-16 15:55:25 +11004748 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 release_stripe(sh);
4750
4751 return STRIPE_SECTORS;
4752}
4753
NeilBrownd1688a62011-10-11 16:49:52 +11004754static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004755{
4756 /* We may not be able to submit a whole bio at once as there
4757 * may not be enough stripe_heads available.
4758 * We cannot pre-allocate enough stripe_heads as we may need
4759 * more than exist in the cache (if we allow ever large chunks).
4760 * So we do one stripe head at a time and record in
4761 * ->bi_hw_segments how many have been done.
4762 *
4763 * We *know* that this entire raid_bio is in one chunk, so
4764 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4765 */
4766 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004767 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004768 sector_t sector, logical_sector, last_sector;
4769 int scnt = 0;
4770 int remaining;
4771 int handled = 0;
4772
4773 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004774 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004775 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004776 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004777
4778 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004779 logical_sector += STRIPE_SECTORS,
4780 sector += STRIPE_SECTORS,
4781 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004782
Shaohua Lie7836bd62012-07-19 16:01:31 +10004783 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004784 /* already done this stripe */
4785 continue;
4786
NeilBrowna8c906c2009-06-09 14:39:59 +10004787 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004788
4789 if (!sh) {
4790 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004791 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004792 conf->retry_read_aligned = raid_bio;
4793 return handled;
4794 }
4795
Neil Brown387bb172007-02-08 14:20:29 -08004796 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4797 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004798 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004799 conf->retry_read_aligned = raid_bio;
4800 return handled;
4801 }
4802
majianpeng3f9e7c12012-07-31 10:04:21 +10004803 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004804 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004805 release_stripe(sh);
4806 handled++;
4807 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004808 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004809 if (remaining == 0) {
4810 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
4811 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004812 bio_endio(raid_bio, 0);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004813 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004814 if (atomic_dec_and_test(&conf->active_aligned_reads))
4815 wake_up(&conf->wait_for_stripe);
4816 return handled;
4817}
4818
Shaohua Li46a06402012-08-02 08:33:15 +10004819#define MAX_STRIPE_BATCH 8
4820static int handle_active_stripes(struct r5conf *conf)
4821{
4822 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4823 int i, batch_size = 0;
4824
4825 while (batch_size < MAX_STRIPE_BATCH &&
4826 (sh = __get_priority_stripe(conf)) != NULL)
4827 batch[batch_size++] = sh;
4828
4829 if (batch_size == 0)
4830 return batch_size;
4831 spin_unlock_irq(&conf->device_lock);
4832
4833 for (i = 0; i < batch_size; i++)
4834 handle_stripe(batch[i]);
4835
4836 cond_resched();
4837
4838 spin_lock_irq(&conf->device_lock);
4839 for (i = 0; i < batch_size; i++)
4840 __release_stripe(conf, batch[i]);
4841 return batch_size;
4842}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844/*
4845 * This is our raid5 kernel thread.
4846 *
4847 * We scan the hash table for stripes which can be handled now.
4848 * During the scan, completed stripes are saved for us by the interrupt
4849 * handler, so that they will not have to wait for our next wakeup.
4850 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004851static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852{
Shaohua Li4ed87312012-10-11 13:34:00 +11004853 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004854 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004856 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
Dan Williams45b42332007-07-09 11:56:43 -07004858 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
4860 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004862 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 handled = 0;
4864 spin_lock_irq(&conf->device_lock);
4865 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004866 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004867 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
NeilBrown0021b7b2012-07-31 09:08:14 +02004869 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004870 !list_empty(&conf->bitmap_list)) {
4871 /* Now is a good time to flush some bitmap updates */
4872 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004873 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004874 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004875 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004876 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004877 activate_bit_delay(conf);
4878 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004879 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004880
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004881 while ((bio = remove_bio_from_retry(conf))) {
4882 int ok;
4883 spin_unlock_irq(&conf->device_lock);
4884 ok = retry_aligned_read(conf, bio);
4885 spin_lock_irq(&conf->device_lock);
4886 if (!ok)
4887 break;
4888 handled++;
4889 }
4890
Shaohua Li46a06402012-08-02 08:33:15 +10004891 batch_size = handle_active_stripes(conf);
4892 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004894 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
Shaohua Li46a06402012-08-02 08:33:15 +10004896 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4897 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004898 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004899 spin_lock_irq(&conf->device_lock);
4900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 }
Dan Williams45b42332007-07-09 11:56:43 -07004902 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903
4904 spin_unlock_irq(&conf->device_lock);
4905
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004906 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004907 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
Dan Williams45b42332007-07-09 11:56:43 -07004909 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910}
4911
NeilBrown3f294f42005-11-08 21:39:25 -08004912static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004913raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004914{
NeilBrownd1688a62011-10-11 16:49:52 +11004915 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004916 if (conf)
4917 return sprintf(page, "%d\n", conf->max_nr_stripes);
4918 else
4919 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004920}
4921
NeilBrownc41d4ac2010-06-01 19:37:24 +10004922int
NeilBrownfd01b882011-10-11 16:47:53 +11004923raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004924{
NeilBrownd1688a62011-10-11 16:49:52 +11004925 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004926 int err;
4927
4928 if (size <= 16 || size > 32768)
4929 return -EINVAL;
4930 while (size < conf->max_nr_stripes) {
4931 if (drop_one_stripe(conf))
4932 conf->max_nr_stripes--;
4933 else
4934 break;
4935 }
4936 err = md_allow_write(mddev);
4937 if (err)
4938 return err;
4939 while (size > conf->max_nr_stripes) {
4940 if (grow_one_stripe(conf))
4941 conf->max_nr_stripes++;
4942 else break;
4943 }
4944 return 0;
4945}
4946EXPORT_SYMBOL(raid5_set_cache_size);
4947
NeilBrown3f294f42005-11-08 21:39:25 -08004948static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004949raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004950{
NeilBrownd1688a62011-10-11 16:49:52 +11004951 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004952 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004953 int err;
4954
NeilBrown3f294f42005-11-08 21:39:25 -08004955 if (len >= PAGE_SIZE)
4956 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004957 if (!conf)
4958 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004959
Dan Williams4ef197d82008-04-28 02:15:54 -07004960 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004961 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004962 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004963 if (err)
4964 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004965 return len;
4966}
NeilBrown007583c2005-11-08 21:39:30 -08004967
NeilBrown96de1e62005-11-08 21:39:39 -08004968static struct md_sysfs_entry
4969raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4970 raid5_show_stripe_cache_size,
4971 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004972
4973static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004974raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004975{
NeilBrownd1688a62011-10-11 16:49:52 +11004976 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004977 if (conf)
4978 return sprintf(page, "%d\n", conf->bypass_threshold);
4979 else
4980 return 0;
4981}
4982
4983static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004984raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004985{
NeilBrownd1688a62011-10-11 16:49:52 +11004986 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004987 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004988 if (len >= PAGE_SIZE)
4989 return -EINVAL;
4990 if (!conf)
4991 return -ENODEV;
4992
Dan Williams4ef197d82008-04-28 02:15:54 -07004993 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004994 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004995 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004996 return -EINVAL;
4997 conf->bypass_threshold = new;
4998 return len;
4999}
5000
5001static struct md_sysfs_entry
5002raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
5003 S_IRUGO | S_IWUSR,
5004 raid5_show_preread_threshold,
5005 raid5_store_preread_threshold);
5006
5007static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005008stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005009{
NeilBrownd1688a62011-10-11 16:49:52 +11005010 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005011 if (conf)
5012 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
5013 else
5014 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005015}
5016
NeilBrown96de1e62005-11-08 21:39:39 -08005017static struct md_sysfs_entry
5018raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08005019
NeilBrown007583c2005-11-08 21:39:30 -08005020static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08005021 &raid5_stripecache_size.attr,
5022 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005023 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08005024 NULL,
5025};
NeilBrown007583c2005-11-08 21:39:30 -08005026static struct attribute_group raid5_attrs_group = {
5027 .name = NULL,
5028 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08005029};
5030
Dan Williams80c3a6c2009-03-17 18:10:40 -07005031static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11005032raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07005033{
NeilBrownd1688a62011-10-11 16:49:52 +11005034 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005035
5036 if (!sectors)
5037 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005038 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11005039 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11005040 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005041
Andre Noll9d8f0362009-06-18 08:45:01 +10005042 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10005043 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005044 return sectors * (raid_disks - conf->max_degraded);
5045}
5046
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305047static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
5048{
5049 safe_put_page(percpu->spare_page);
5050 kfree(percpu->scribble);
5051 percpu->spare_page = NULL;
5052 percpu->scribble = NULL;
5053}
5054
5055static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
5056{
5057 if (conf->level == 6 && !percpu->spare_page)
5058 percpu->spare_page = alloc_page(GFP_KERNEL);
5059 if (!percpu->scribble)
5060 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5061
5062 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
5063 free_scratch_buffer(conf, percpu);
5064 return -ENOMEM;
5065 }
5066
5067 return 0;
5068}
5069
NeilBrownd1688a62011-10-11 16:49:52 +11005070static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005071{
Dan Williams36d1c642009-07-14 11:48:22 -07005072 unsigned long cpu;
5073
5074 if (!conf->percpu)
5075 return;
5076
Dan Williams36d1c642009-07-14 11:48:22 -07005077#ifdef CONFIG_HOTPLUG_CPU
5078 unregister_cpu_notifier(&conf->cpu_notify);
5079#endif
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305080
5081 get_online_cpus();
5082 for_each_possible_cpu(cpu)
5083 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07005084 put_online_cpus();
5085
5086 free_percpu(conf->percpu);
5087}
5088
NeilBrownd1688a62011-10-11 16:49:52 +11005089static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10005090{
5091 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07005092 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005093 kfree(conf->disks);
5094 kfree(conf->stripe_hashtbl);
5095 kfree(conf);
5096}
5097
Dan Williams36d1c642009-07-14 11:48:22 -07005098#ifdef CONFIG_HOTPLUG_CPU
5099static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5100 void *hcpu)
5101{
NeilBrownd1688a62011-10-11 16:49:52 +11005102 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005103 long cpu = (long)hcpu;
5104 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5105
5106 switch (action) {
5107 case CPU_UP_PREPARE:
5108 case CPU_UP_PREPARE_FROZEN:
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305109 if (alloc_scratch_buffer(conf, percpu)) {
Dan Williams36d1c642009-07-14 11:48:22 -07005110 pr_err("%s: failed memory allocation for cpu%ld\n",
5111 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005112 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005113 }
5114 break;
5115 case CPU_DEAD:
5116 case CPU_DEAD_FROZEN:
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305117 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07005118 break;
5119 default:
5120 break;
5121 }
5122 return NOTIFY_OK;
5123}
5124#endif
5125
NeilBrownd1688a62011-10-11 16:49:52 +11005126static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005127{
5128 unsigned long cpu;
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305129 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07005130
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305131 conf->percpu = alloc_percpu(struct raid5_percpu);
5132 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07005133 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07005134
Dan Williams36d1c642009-07-14 11:48:22 -07005135#ifdef CONFIG_HOTPLUG_CPU
5136 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5137 conf->cpu_notify.priority = 0;
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305138 err = register_cpu_notifier(&conf->cpu_notify);
5139 if (err)
5140 return err;
Dan Williams36d1c642009-07-14 11:48:22 -07005141#endif
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305142
5143 get_online_cpus();
5144 for_each_present_cpu(cpu) {
5145 err = alloc_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
5146 if (err) {
5147 pr_err("%s: failed memory allocation for cpu%ld\n",
5148 __func__, cpu);
5149 break;
5150 }
5151 }
Dan Williams36d1c642009-07-14 11:48:22 -07005152 put_online_cpus();
5153
5154 return err;
5155}
5156
NeilBrownd1688a62011-10-11 16:49:52 +11005157static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158{
NeilBrownd1688a62011-10-11 16:49:52 +11005159 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005160 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005161 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005163 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164
NeilBrown91adb562009-03-31 14:39:39 +11005165 if (mddev->new_level != 5
5166 && mddev->new_level != 4
5167 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005168 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005169 mdname(mddev), mddev->new_level);
5170 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 }
NeilBrown91adb562009-03-31 14:39:39 +11005172 if ((mddev->new_level == 5
5173 && !algorithm_valid_raid5(mddev->new_layout)) ||
5174 (mddev->new_level == 6
5175 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005176 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005177 mdname(mddev), mddev->new_layout);
5178 return ERR_PTR(-EIO);
5179 }
5180 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005181 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005182 mdname(mddev), mddev->raid_disks);
5183 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185
Andre Noll664e7c42009-06-18 08:45:27 +10005186 if (!mddev->new_chunk_sectors ||
5187 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5188 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005189 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5190 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005191 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005192 }
5193
NeilBrownd1688a62011-10-11 16:49:52 +11005194 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005195 if (conf == NULL)
5196 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005197 spin_lock_init(&conf->device_lock);
5198 init_waitqueue_head(&conf->wait_for_stripe);
5199 init_waitqueue_head(&conf->wait_for_overlap);
5200 INIT_LIST_HEAD(&conf->handle_list);
5201 INIT_LIST_HEAD(&conf->hold_list);
5202 INIT_LIST_HEAD(&conf->delayed_list);
5203 INIT_LIST_HEAD(&conf->bitmap_list);
5204 INIT_LIST_HEAD(&conf->inactive_list);
5205 atomic_set(&conf->active_stripes, 0);
5206 atomic_set(&conf->preread_active_stripes, 0);
5207 atomic_set(&conf->active_aligned_reads, 0);
5208 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005209 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005210
5211 conf->raid_disks = mddev->raid_disks;
5212 if (mddev->reshape_position == MaxSector)
5213 conf->previous_raid_disks = mddev->raid_disks;
5214 else
5215 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005216 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5217 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005218
NeilBrown5e5e3e72009-10-16 16:35:30 +11005219 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005220 GFP_KERNEL);
5221 if (!conf->disks)
5222 goto abort;
5223
5224 conf->mddev = mddev;
5225
5226 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5227 goto abort;
5228
Dan Williams36d1c642009-07-14 11:48:22 -07005229 conf->level = mddev->new_level;
5230 if (raid5_alloc_percpu(conf) != 0)
5231 goto abort;
5232
NeilBrown0c55e022010-05-03 14:09:02 +10005233 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005234
NeilBrowndafb20f2012-03-19 12:46:39 +11005235 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005236 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005237 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005238 || raid_disk < 0)
5239 continue;
5240 disk = conf->disks + raid_disk;
5241
NeilBrown17045f52011-12-23 10:17:53 +11005242 if (test_bit(Replacement, &rdev->flags)) {
5243 if (disk->replacement)
5244 goto abort;
5245 disk->replacement = rdev;
5246 } else {
5247 if (disk->rdev)
5248 goto abort;
5249 disk->rdev = rdev;
5250 }
NeilBrown91adb562009-03-31 14:39:39 +11005251
5252 if (test_bit(In_sync, &rdev->flags)) {
5253 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005254 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5255 " disk %d\n",
5256 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005257 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005258 /* Cannot rely on bitmap to complete recovery */
5259 conf->fullsync = 1;
5260 }
5261
Andre Noll09c9e5f2009-06-18 08:45:55 +10005262 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005263 conf->level = mddev->new_level;
5264 if (conf->level == 6)
5265 conf->max_degraded = 2;
5266 else
5267 conf->max_degraded = 1;
5268 conf->algorithm = mddev->new_layout;
5269 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005270 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005271 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005272 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005273 conf->prev_algo = mddev->layout;
5274 }
NeilBrown91adb562009-03-31 14:39:39 +11005275
5276 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005277 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005278 if (grow_stripes(conf, conf->max_nr_stripes)) {
5279 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005280 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5281 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005282 goto abort;
5283 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005284 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5285 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005286
NeilBrown02326052012-07-03 15:56:52 +10005287 sprintf(pers_name, "raid%d", mddev->new_level);
5288 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005289 if (!conf->thread) {
5290 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005291 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005292 mdname(mddev));
5293 goto abort;
5294 }
5295
5296 return conf;
5297
5298 abort:
5299 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005300 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005301 return ERR_PTR(-EIO);
5302 } else
5303 return ERR_PTR(-ENOMEM);
5304}
5305
NeilBrownc148ffd2009-11-13 17:47:00 +11005306
5307static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5308{
5309 switch (algo) {
5310 case ALGORITHM_PARITY_0:
5311 if (raid_disk < max_degraded)
5312 return 1;
5313 break;
5314 case ALGORITHM_PARITY_N:
5315 if (raid_disk >= raid_disks - max_degraded)
5316 return 1;
5317 break;
5318 case ALGORITHM_PARITY_0_6:
5319 if (raid_disk == 0 ||
5320 raid_disk == raid_disks - 1)
5321 return 1;
5322 break;
5323 case ALGORITHM_LEFT_ASYMMETRIC_6:
5324 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5325 case ALGORITHM_LEFT_SYMMETRIC_6:
5326 case ALGORITHM_RIGHT_SYMMETRIC_6:
5327 if (raid_disk == raid_disks - 1)
5328 return 1;
5329 }
5330 return 0;
5331}
5332
NeilBrownfd01b882011-10-11 16:47:53 +11005333static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005334{
NeilBrownd1688a62011-10-11 16:49:52 +11005335 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005336 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005337 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005338 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005339 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005340 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005341 long long min_offset_diff = 0;
5342 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005343
Andre Noll8c6ac862009-06-18 08:48:06 +10005344 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005345 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005346 " -- starting background reconstruction\n",
5347 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005348
5349 rdev_for_each(rdev, mddev) {
5350 long long diff;
5351 if (rdev->raid_disk < 0)
5352 continue;
5353 diff = (rdev->new_data_offset - rdev->data_offset);
5354 if (first) {
5355 min_offset_diff = diff;
5356 first = 0;
5357 } else if (mddev->reshape_backwards &&
5358 diff < min_offset_diff)
5359 min_offset_diff = diff;
5360 else if (!mddev->reshape_backwards &&
5361 diff > min_offset_diff)
5362 min_offset_diff = diff;
5363 }
5364
NeilBrownf6705572006-03-27 01:18:11 -08005365 if (mddev->reshape_position != MaxSector) {
5366 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005367 * Difficulties arise if the stripe we would write to
5368 * next is at or after the stripe we would read from next.
5369 * For a reshape that changes the number of devices, this
5370 * is only possible for a very short time, and mdadm makes
5371 * sure that time appears to have past before assembling
5372 * the array. So we fail if that time hasn't passed.
5373 * For a reshape that keeps the number of devices the same
5374 * mdadm must be monitoring the reshape can keeping the
5375 * critical areas read-only and backed up. It will start
5376 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005377 */
5378 sector_t here_new, here_old;
5379 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005380 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005381
NeilBrown88ce4932009-03-31 15:24:23 +11005382 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005383 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005384 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005385 mdname(mddev));
5386 return -EINVAL;
5387 }
NeilBrownf6705572006-03-27 01:18:11 -08005388 old_disks = mddev->raid_disks - mddev->delta_disks;
5389 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005390 * further up in new geometry must map after here in old
5391 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005392 */
5393 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005394 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005395 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005396 printk(KERN_ERR "md/raid:%s: reshape_position not "
5397 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005398 return -EINVAL;
5399 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005400 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005401 /* here_new is the stripe we will write to */
5402 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005403 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005404 (old_disks-max_degraded));
5405 /* here_old is the first stripe that we might need to read
5406 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005407 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005408 if ((here_new * mddev->new_chunk_sectors !=
5409 here_old * mddev->chunk_sectors)) {
5410 printk(KERN_ERR "md/raid:%s: reshape position is"
5411 " confused - aborting\n", mdname(mddev));
5412 return -EINVAL;
5413 }
NeilBrown67ac6012009-08-13 10:06:24 +10005414 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005415 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005416 * and taking constant backups.
5417 * mdadm always starts a situation like this in
5418 * readonly mode so it can take control before
5419 * allowing any writes. So just check for that.
5420 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005421 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5422 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5423 /* not really in-place - so OK */;
5424 else if (mddev->ro == 0) {
5425 printk(KERN_ERR "md/raid:%s: in-place reshape "
5426 "must be started in read-only mode "
5427 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005428 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005429 return -EINVAL;
5430 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005431 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005432 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005433 here_old * mddev->chunk_sectors)
5434 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005435 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005436 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005437 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5438 "auto-recovery - aborting.\n",
5439 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005440 return -EINVAL;
5441 }
NeilBrown0c55e022010-05-03 14:09:02 +10005442 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5443 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005444 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005445 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005446 BUG_ON(mddev->level != mddev->new_level);
5447 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005448 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005449 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005450 }
5451
NeilBrown245f46c2009-03-31 14:39:39 +11005452 if (mddev->private == NULL)
5453 conf = setup_conf(mddev);
5454 else
5455 conf = mddev->private;
5456
NeilBrown91adb562009-03-31 14:39:39 +11005457 if (IS_ERR(conf))
5458 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005459
NeilBrownb5254dd2012-05-21 09:27:01 +10005460 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005461 mddev->thread = conf->thread;
5462 conf->thread = NULL;
5463 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464
NeilBrown17045f52011-12-23 10:17:53 +11005465 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5466 i++) {
5467 rdev = conf->disks[i].rdev;
5468 if (!rdev && conf->disks[i].replacement) {
5469 /* The replacement is all we have yet */
5470 rdev = conf->disks[i].replacement;
5471 conf->disks[i].replacement = NULL;
5472 clear_bit(Replacement, &rdev->flags);
5473 conf->disks[i].rdev = rdev;
5474 }
5475 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005476 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005477 if (conf->disks[i].replacement &&
5478 conf->reshape_progress != MaxSector) {
5479 /* replacements and reshape simply do not mix. */
5480 printk(KERN_ERR "md: cannot handle concurrent "
5481 "replacement and reshape.\n");
5482 goto abort;
5483 }
NeilBrown2f115882010-06-17 17:41:03 +10005484 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005485 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005486 continue;
5487 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005488 /* This disc is not fully in-sync. However if it
5489 * just stored parity (beyond the recovery_offset),
5490 * when we don't need to be concerned about the
5491 * array being dirty.
5492 * When reshape goes 'backwards', we never have
5493 * partially completed devices, so we only need
5494 * to worry about reshape going forwards.
5495 */
5496 /* Hack because v0.91 doesn't store recovery_offset properly. */
5497 if (mddev->major_version == 0 &&
5498 mddev->minor_version > 90)
5499 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005500
NeilBrownc148ffd2009-11-13 17:47:00 +11005501 if (rdev->recovery_offset < reshape_offset) {
5502 /* We need to check old and new layout */
5503 if (!only_parity(rdev->raid_disk,
5504 conf->algorithm,
5505 conf->raid_disks,
5506 conf->max_degraded))
5507 continue;
5508 }
5509 if (!only_parity(rdev->raid_disk,
5510 conf->prev_algo,
5511 conf->previous_raid_disks,
5512 conf->max_degraded))
5513 continue;
5514 dirty_parity_disks++;
5515 }
NeilBrown91adb562009-03-31 14:39:39 +11005516
NeilBrown17045f52011-12-23 10:17:53 +11005517 /*
5518 * 0 for a fully functional array, 1 or 2 for a degraded array.
5519 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005520 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521
NeilBrown674806d2010-06-16 17:17:53 +10005522 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005523 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005525 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 goto abort;
5527 }
5528
NeilBrown91adb562009-03-31 14:39:39 +11005529 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005530 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005531 mddev->resync_max_sectors = mddev->dev_sectors;
5532
NeilBrownc148ffd2009-11-13 17:47:00 +11005533 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005535 if (mddev->ok_start_degraded)
5536 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005537 "md/raid:%s: starting dirty degraded array"
5538 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005539 mdname(mddev));
5540 else {
5541 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005542 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005543 mdname(mddev));
5544 goto abort;
5545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 }
5547
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005549 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5550 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005551 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5552 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 else
NeilBrown0c55e022010-05-03 14:09:02 +10005554 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5555 " out of %d devices, algorithm %d\n",
5556 mdname(mddev), conf->level,
5557 mddev->raid_disks - mddev->degraded,
5558 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
5560 print_raid5_conf(conf);
5561
NeilBrownfef9c612009-03-31 15:16:46 +11005562 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005563 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005564 atomic_set(&conf->reshape_stripes, 0);
5565 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5566 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5567 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5568 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5569 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005570 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005571 }
5572
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573
5574 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005575 if (mddev->to_remove == &raid5_attrs_group)
5576 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005577 else if (mddev->kobj.sd &&
5578 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005579 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005580 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005581 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005582 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5583
5584 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005585 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005586 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005587 /* read-ahead size must cover two whole stripes, which
5588 * is 2 * (datadisks) * chunksize where 'n' is the
5589 * number of raid devices
5590 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5592 int stripe = data_disks *
5593 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5594 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5595 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005596
5597 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005598
5599 mddev->queue->backing_dev_info.congested_data = mddev;
5600 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005601
5602 chunk_size = mddev->chunk_sectors << 9;
5603 blk_queue_io_min(mddev->queue, chunk_size);
5604 blk_queue_io_opt(mddev->queue, chunk_size *
5605 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005606 /*
5607 * We can only discard a whole stripe. It doesn't make sense to
5608 * discard data disk but write parity disk
5609 */
5610 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11005611 /* Round up to power of 2, as discard handling
5612 * currently assumes that */
5613 while ((stripe-1) & stripe)
5614 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11005615 mddev->queue->limits.discard_alignment = stripe;
5616 mddev->queue->limits.discard_granularity = stripe;
5617 /*
5618 * unaligned part of discard request will be ignored, so can't
NeilBrown06905ff2014-10-02 13:45:00 +10005619 * guarantee discard_zeroes_data
Shaohua Li620125f2012-10-11 13:49:05 +11005620 */
5621 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005622
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005623 blk_queue_max_write_same_sectors(mddev->queue, 0);
5624
NeilBrown05616be2012-05-21 09:27:00 +10005625 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005626 disk_stack_limits(mddev->gendisk, rdev->bdev,
5627 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005628 disk_stack_limits(mddev->gendisk, rdev->bdev,
5629 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005630 /*
5631 * discard_zeroes_data is required, otherwise data
5632 * could be lost. Consider a scenario: discard a stripe
5633 * (the stripe could be inconsistent if
5634 * discard_zeroes_data is 0); write one disk of the
5635 * stripe (the stripe could be inconsistent again
5636 * depending on which disks are used to calculate
5637 * parity); the disk is broken; The stripe data of this
5638 * disk is lost.
5639 */
5640 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5641 !bdev_get_queue(rdev->bdev)->
5642 limits.discard_zeroes_data)
5643 discard_supported = false;
NeilBrown06905ff2014-10-02 13:45:00 +10005644 /* Unfortunately, discard_zeroes_data is not currently
5645 * a guarantee - just a hint. So we only allow DISCARD
5646 * if the sysadmin has confirmed that only safe devices
5647 * are in use by setting a module parameter.
5648 */
5649 if (!devices_handle_discard_safely) {
5650 if (discard_supported) {
5651 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
5652 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
5653 }
5654 discard_supported = false;
5655 }
NeilBrown05616be2012-05-21 09:27:00 +10005656 }
Shaohua Li620125f2012-10-11 13:49:05 +11005657
5658 if (discard_supported &&
5659 mddev->queue->limits.max_discard_sectors >= stripe &&
5660 mddev->queue->limits.discard_granularity >= stripe)
5661 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5662 mddev->queue);
5663 else
5664 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5665 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 }
5667
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 return 0;
5669abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005670 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005671 print_raid5_conf(conf);
5672 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005674 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 return -EIO;
5676}
5677
NeilBrownfd01b882011-10-11 16:47:53 +11005678static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679{
NeilBrownd1688a62011-10-11 16:49:52 +11005680 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
NeilBrown01f96c02011-09-21 15:30:20 +10005682 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005683 if (mddev->queue)
5684 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005685 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005686 mddev->private = NULL;
5687 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 return 0;
5689}
5690
NeilBrownfd01b882011-10-11 16:47:53 +11005691static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692{
NeilBrownd1688a62011-10-11 16:49:52 +11005693 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 int i;
5695
Andre Noll9d8f0362009-06-18 08:45:01 +10005696 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5697 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005698 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 for (i = 0; i < conf->raid_disks; i++)
5700 seq_printf (seq, "%s",
5701 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005702 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704}
5705
NeilBrownd1688a62011-10-11 16:49:52 +11005706static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707{
5708 int i;
5709 struct disk_info *tmp;
5710
NeilBrown0c55e022010-05-03 14:09:02 +10005711 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 if (!conf) {
5713 printk("(conf==NULL)\n");
5714 return;
5715 }
NeilBrown0c55e022010-05-03 14:09:02 +10005716 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5717 conf->raid_disks,
5718 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719
5720 for (i = 0; i < conf->raid_disks; i++) {
5721 char b[BDEVNAME_SIZE];
5722 tmp = conf->disks + i;
5723 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005724 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5725 i, !test_bit(Faulty, &tmp->rdev->flags),
5726 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 }
5728}
5729
NeilBrownfd01b882011-10-11 16:47:53 +11005730static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731{
5732 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005733 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005735 int count = 0;
5736 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737
5738 for (i = 0; i < conf->raid_disks; i++) {
5739 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005740 if (tmp->replacement
5741 && tmp->replacement->recovery_offset == MaxSector
5742 && !test_bit(Faulty, &tmp->replacement->flags)
5743 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5744 /* Replacement has just become active. */
5745 if (!tmp->rdev
5746 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5747 count++;
5748 if (tmp->rdev) {
5749 /* Replaced device not technically faulty,
5750 * but we need to be sure it gets removed
5751 * and never re-added.
5752 */
5753 set_bit(Faulty, &tmp->rdev->flags);
5754 sysfs_notify_dirent_safe(
5755 tmp->rdev->sysfs_state);
5756 }
5757 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5758 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005759 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005760 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005761 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005762 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005763 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 }
5765 }
NeilBrown6b965622010-08-18 11:56:59 +10005766 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005767 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005768 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005770 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771}
5772
NeilBrownb8321b62011-12-23 10:17:51 +11005773static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774{
NeilBrownd1688a62011-10-11 16:49:52 +11005775 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005777 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005778 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 struct disk_info *p = conf->disks + number;
5780
5781 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005782 if (rdev == p->rdev)
5783 rdevp = &p->rdev;
5784 else if (rdev == p->replacement)
5785 rdevp = &p->replacement;
5786 else
5787 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005788
NeilBrown657e3e42011-12-23 10:17:52 +11005789 if (number >= conf->raid_disks &&
5790 conf->reshape_progress == MaxSector)
5791 clear_bit(In_sync, &rdev->flags);
5792
5793 if (test_bit(In_sync, &rdev->flags) ||
5794 atomic_read(&rdev->nr_pending)) {
5795 err = -EBUSY;
5796 goto abort;
5797 }
5798 /* Only remove non-faulty devices if recovery
5799 * isn't possible.
5800 */
5801 if (!test_bit(Faulty, &rdev->flags) &&
5802 mddev->recovery_disabled != conf->recovery_disabled &&
5803 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005804 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005805 number < conf->raid_disks) {
5806 err = -EBUSY;
5807 goto abort;
5808 }
5809 *rdevp = NULL;
5810 synchronize_rcu();
5811 if (atomic_read(&rdev->nr_pending)) {
5812 /* lost the race, try later */
5813 err = -EBUSY;
5814 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005815 } else if (p->replacement) {
5816 /* We must have just cleared 'rdev' */
5817 p->rdev = p->replacement;
5818 clear_bit(Replacement, &p->replacement->flags);
5819 smp_mb(); /* Make sure other CPUs may see both as identical
5820 * but will never see neither - if they are careful
5821 */
5822 p->replacement = NULL;
5823 clear_bit(WantReplacement, &rdev->flags);
5824 } else
5825 /* We might have just removed the Replacement as faulty-
5826 * clear the bit just in case
5827 */
5828 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829abort:
5830
5831 print_raid5_conf(conf);
5832 return err;
5833}
5834
NeilBrownfd01b882011-10-11 16:47:53 +11005835static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836{
NeilBrownd1688a62011-10-11 16:49:52 +11005837 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005838 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 int disk;
5840 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005841 int first = 0;
5842 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
NeilBrown7f0da592011-07-28 11:39:22 +10005844 if (mddev->recovery_disabled == conf->recovery_disabled)
5845 return -EBUSY;
5846
NeilBrowndc10c642012-03-19 12:46:37 +11005847 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005849 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850
Neil Brown6c2fce22008-06-28 08:31:31 +10005851 if (rdev->raid_disk >= 0)
5852 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853
5854 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005855 * find the disk ... but prefer rdev->saved_raid_disk
5856 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005858 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005859 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005860 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005861 first = rdev->saved_raid_disk;
5862
5863 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005864 p = conf->disks + disk;
5865 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005866 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005868 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005869 if (rdev->saved_raid_disk != disk)
5870 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005871 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005872 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005874 }
5875 for (disk = first; disk <= last; disk++) {
5876 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005877 if (test_bit(WantReplacement, &p->rdev->flags) &&
5878 p->replacement == NULL) {
5879 clear_bit(In_sync, &rdev->flags);
5880 set_bit(Replacement, &rdev->flags);
5881 rdev->raid_disk = disk;
5882 err = 0;
5883 conf->fullsync = 1;
5884 rcu_assign_pointer(p->replacement, rdev);
5885 break;
5886 }
5887 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005888out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005890 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891}
5892
NeilBrownfd01b882011-10-11 16:47:53 +11005893static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894{
5895 /* no resync is happening, and there is enough space
5896 * on all devices, so we can resize.
5897 * We need to make sure resync covers any new space.
5898 * If the array is shrinking we should possibly wait until
5899 * any io in the removed space completes, but it hardly seems
5900 * worth it.
5901 */
NeilBrowna4a61252012-05-22 13:55:27 +10005902 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005903 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005904 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5905 if (mddev->external_size &&
5906 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005907 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005908 if (mddev->bitmap) {
5909 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5910 if (ret)
5911 return ret;
5912 }
5913 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005914 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005915 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005916 if (sectors > mddev->dev_sectors &&
5917 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005918 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5920 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005921 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005922 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 return 0;
5924}
5925
NeilBrownfd01b882011-10-11 16:47:53 +11005926static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005927{
5928 /* Can only proceed if there are plenty of stripe_heads.
5929 * We need a minimum of one full stripe,, and for sensible progress
5930 * it is best to have about 4 times that.
5931 * If we require 4 times, then the default 256 4K stripe_heads will
5932 * allow for chunk sizes up to 256K, which is probably OK.
5933 * If the chunk size is greater, user-space should request more
5934 * stripe_heads first.
5935 */
NeilBrownd1688a62011-10-11 16:49:52 +11005936 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005937 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5938 > conf->max_nr_stripes ||
5939 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5940 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005941 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5942 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005943 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5944 / STRIPE_SIZE)*4);
5945 return 0;
5946 }
5947 return 1;
5948}
5949
NeilBrownfd01b882011-10-11 16:47:53 +11005950static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005951{
NeilBrownd1688a62011-10-11 16:49:52 +11005952 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005953
NeilBrown88ce4932009-03-31 15:24:23 +11005954 if (mddev->delta_disks == 0 &&
5955 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005956 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005957 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005958 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005959 return -EINVAL;
5960 if (mddev->delta_disks < 0) {
5961 /* We might be able to shrink, but the devices must
5962 * be made bigger first.
5963 * For raid6, 4 is the minimum size.
5964 * Otherwise 2 is the minimum
5965 */
5966 int min = 2;
5967 if (mddev->level == 6)
5968 min = 4;
5969 if (mddev->raid_disks + mddev->delta_disks < min)
5970 return -EINVAL;
5971 }
NeilBrown29269552006-03-27 01:18:10 -08005972
NeilBrown01ee22b2009-06-18 08:47:20 +10005973 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005974 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005975
NeilBrowne56108d62012-10-11 14:24:13 +11005976 return resize_stripes(conf, (conf->previous_raid_disks
5977 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08005978}
5979
NeilBrownfd01b882011-10-11 16:47:53 +11005980static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005981{
NeilBrownd1688a62011-10-11 16:49:52 +11005982 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005983 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005984 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005985 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005986
NeilBrownf4168852007-02-28 20:11:53 -08005987 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005988 return -EBUSY;
5989
NeilBrown01ee22b2009-06-18 08:47:20 +10005990 if (!check_stripe_cache(mddev))
5991 return -ENOSPC;
5992
NeilBrown30b67642012-05-22 13:55:28 +10005993 if (has_failed(conf))
5994 return -EINVAL;
5995
NeilBrownc6563a82012-05-21 09:27:00 +10005996 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005997 if (!test_bit(In_sync, &rdev->flags)
5998 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005999 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10006000 }
NeilBrown63c70c42006-03-27 01:18:13 -08006001
NeilBrownf4168852007-02-28 20:11:53 -08006002 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08006003 /* Not enough devices even to make a degraded array
6004 * of that size
6005 */
6006 return -EINVAL;
6007
NeilBrownec32a2b2009-03-31 15:17:38 +11006008 /* Refuse to reduce size of the array. Any reductions in
6009 * array size must be through explicit setting of array_size
6010 * attribute.
6011 */
6012 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
6013 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10006014 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11006015 "before number of disks\n", mdname(mddev));
6016 return -EINVAL;
6017 }
6018
NeilBrownf6705572006-03-27 01:18:11 -08006019 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08006020 spin_lock_irq(&conf->device_lock);
6021 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08006022 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006023 conf->prev_chunk_sectors = conf->chunk_sectors;
6024 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11006025 conf->prev_algo = conf->algorithm;
6026 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10006027 conf->generation++;
6028 /* Code that selects data_offset needs to see the generation update
6029 * if reshape_progress has been set - so a memory barrier needed.
6030 */
6031 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10006032 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11006033 conf->reshape_progress = raid5_size(mddev, 0, 0);
6034 else
6035 conf->reshape_progress = 0;
6036 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08006037 spin_unlock_irq(&conf->device_lock);
6038
6039 /* Add some new drives, as many as will fit.
6040 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10006041 * Don't add devices if we are reducing the number of
6042 * devices in the array. This is because it is not possible
6043 * to correctly record the "partially reconstructed" state of
6044 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08006045 */
NeilBrown87a8dec2011-01-31 11:57:43 +11006046 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11006047 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11006048 if (rdev->raid_disk < 0 &&
6049 !test_bit(Faulty, &rdev->flags)) {
6050 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11006051 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11006052 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11006053 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11006054 else
NeilBrown87a8dec2011-01-31 11:57:43 +11006055 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10006056
6057 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11006058 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11006059 }
NeilBrown87a8dec2011-01-31 11:57:43 +11006060 } else if (rdev->raid_disk >= conf->previous_raid_disks
6061 && !test_bit(Faulty, &rdev->flags)) {
6062 /* This is a spare that was manually added */
6063 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11006064 }
NeilBrown29269552006-03-27 01:18:10 -08006065
NeilBrown87a8dec2011-01-31 11:57:43 +11006066 /* When a reshape changes the number of devices,
6067 * ->degraded is measured against the larger of the
6068 * pre and post number of devices.
6069 */
NeilBrownec32a2b2009-03-31 15:17:38 +11006070 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006071 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11006072 spin_unlock_irqrestore(&conf->device_lock, flags);
6073 }
NeilBrown63c70c42006-03-27 01:18:13 -08006074 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10006075 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07006076 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08006077
NeilBrown29269552006-03-27 01:18:10 -08006078 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6079 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6080 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6081 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6082 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006083 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08006084 if (!mddev->sync_thread) {
6085 mddev->recovery = 0;
6086 spin_lock_irq(&conf->device_lock);
6087 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006088 rdev_for_each(rdev, mddev)
6089 rdev->new_data_offset = rdev->data_offset;
6090 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006091 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11006092 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08006093 spin_unlock_irq(&conf->device_lock);
6094 return -EAGAIN;
6095 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006096 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006097 md_wakeup_thread(mddev->sync_thread);
6098 md_new_event(mddev);
6099 return 0;
6100}
NeilBrown29269552006-03-27 01:18:10 -08006101
NeilBrownec32a2b2009-03-31 15:17:38 +11006102/* This is called from the reshape thread and should make any
6103 * changes needed in 'conf'
6104 */
NeilBrownd1688a62011-10-11 16:49:52 +11006105static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006106{
NeilBrown29269552006-03-27 01:18:10 -08006107
NeilBrownf6705572006-03-27 01:18:11 -08006108 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006109 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006110
NeilBrownf6705572006-03-27 01:18:11 -08006111 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006112 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006113 rdev_for_each(rdev, conf->mddev)
6114 rdev->data_offset = rdev->new_data_offset;
6115 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006116 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006117 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006118 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006119
6120 /* read-ahead size must cover two whole stripes, which is
6121 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6122 */
NeilBrown4a5add42010-06-01 19:37:28 +10006123 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006124 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006125 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006126 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006127 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6128 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6129 }
NeilBrown29269552006-03-27 01:18:10 -08006130 }
NeilBrown29269552006-03-27 01:18:10 -08006131}
6132
NeilBrownec32a2b2009-03-31 15:17:38 +11006133/* This is called from the raid5d thread with mddev_lock held.
6134 * It makes config changes to the device.
6135 */
NeilBrownfd01b882011-10-11 16:47:53 +11006136static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006137{
NeilBrownd1688a62011-10-11 16:49:52 +11006138 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006139
6140 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6141
NeilBrownec32a2b2009-03-31 15:17:38 +11006142 if (mddev->delta_disks > 0) {
6143 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6144 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006145 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006146 } else {
6147 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006148 spin_lock_irq(&conf->device_lock);
6149 mddev->degraded = calc_degraded(conf);
6150 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006151 for (d = conf->raid_disks ;
6152 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006153 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006154 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006155 if (rdev)
6156 clear_bit(In_sync, &rdev->flags);
6157 rdev = conf->disks[d].replacement;
6158 if (rdev)
6159 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006160 }
NeilBrowncea9c222009-03-31 15:15:05 +11006161 }
NeilBrown88ce4932009-03-31 15:24:23 +11006162 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006163 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006164 mddev->reshape_position = MaxSector;
6165 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006166 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006167 }
6168}
6169
NeilBrownfd01b882011-10-11 16:47:53 +11006170static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006171{
NeilBrownd1688a62011-10-11 16:49:52 +11006172 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006173
6174 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006175 case 2: /* resume for a suspend */
6176 wake_up(&conf->wait_for_overlap);
6177 break;
6178
NeilBrown72626682005-09-09 16:23:54 -07006179 case 1: /* stop all writes */
6180 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006181 /* '2' tells resync/reshape to pause so that all
6182 * active stripes can drain
6183 */
6184 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006185 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006186 atomic_read(&conf->active_stripes) == 0 &&
6187 atomic_read(&conf->active_aligned_reads) == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01006188 conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006189 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006190 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006191 /* allow reshape to continue */
6192 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006193 break;
6194
6195 case 0: /* re-enable writes */
6196 spin_lock_irq(&conf->device_lock);
6197 conf->quiesce = 0;
6198 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006199 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006200 spin_unlock_irq(&conf->device_lock);
6201 break;
6202 }
NeilBrown72626682005-09-09 16:23:54 -07006203}
NeilBrownb15c2e52006-01-06 00:20:16 -08006204
NeilBrownd562b0c2009-03-31 14:39:39 +11006205
NeilBrownfd01b882011-10-11 16:47:53 +11006206static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006207{
NeilBrowne373ab12011-10-11 16:48:59 +11006208 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006209 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006210
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006211 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006212 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006213 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6214 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006215 return ERR_PTR(-EINVAL);
6216 }
6217
NeilBrowne373ab12011-10-11 16:48:59 +11006218 sectors = raid0_conf->strip_zone[0].zone_end;
6219 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006220 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006221 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006222 mddev->new_layout = ALGORITHM_PARITY_N;
6223 mddev->new_chunk_sectors = mddev->chunk_sectors;
6224 mddev->raid_disks += 1;
6225 mddev->delta_disks = 1;
6226 /* make sure it will be not marked as dirty */
6227 mddev->recovery_cp = MaxSector;
6228
6229 return setup_conf(mddev);
6230}
6231
6232
NeilBrownfd01b882011-10-11 16:47:53 +11006233static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006234{
6235 int chunksect;
6236
6237 if (mddev->raid_disks != 2 ||
6238 mddev->degraded > 1)
6239 return ERR_PTR(-EINVAL);
6240
6241 /* Should check if there are write-behind devices? */
6242
6243 chunksect = 64*2; /* 64K by default */
6244
6245 /* The array must be an exact multiple of chunksize */
6246 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6247 chunksect >>= 1;
6248
6249 if ((chunksect<<9) < STRIPE_SIZE)
6250 /* array size does not allow a suitable chunk size */
6251 return ERR_PTR(-EINVAL);
6252
6253 mddev->new_level = 5;
6254 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006255 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006256
6257 return setup_conf(mddev);
6258}
6259
NeilBrownfd01b882011-10-11 16:47:53 +11006260static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006261{
6262 int new_layout;
6263
6264 switch (mddev->layout) {
6265 case ALGORITHM_LEFT_ASYMMETRIC_6:
6266 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6267 break;
6268 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6269 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6270 break;
6271 case ALGORITHM_LEFT_SYMMETRIC_6:
6272 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6273 break;
6274 case ALGORITHM_RIGHT_SYMMETRIC_6:
6275 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6276 break;
6277 case ALGORITHM_PARITY_0_6:
6278 new_layout = ALGORITHM_PARITY_0;
6279 break;
6280 case ALGORITHM_PARITY_N:
6281 new_layout = ALGORITHM_PARITY_N;
6282 break;
6283 default:
6284 return ERR_PTR(-EINVAL);
6285 }
6286 mddev->new_level = 5;
6287 mddev->new_layout = new_layout;
6288 mddev->delta_disks = -1;
6289 mddev->raid_disks -= 1;
6290 return setup_conf(mddev);
6291}
6292
NeilBrownd562b0c2009-03-31 14:39:39 +11006293
NeilBrownfd01b882011-10-11 16:47:53 +11006294static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006295{
NeilBrown88ce4932009-03-31 15:24:23 +11006296 /* For a 2-drive array, the layout and chunk size can be changed
6297 * immediately as not restriping is needed.
6298 * For larger arrays we record the new value - after validation
6299 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006300 */
NeilBrownd1688a62011-10-11 16:49:52 +11006301 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006302 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006303
NeilBrown597a7112009-06-18 08:47:42 +10006304 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006305 return -EINVAL;
6306 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006307 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006308 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006309 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006310 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006311 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006312 /* not factor of array size */
6313 return -EINVAL;
6314 }
6315
6316 /* They look valid */
6317
NeilBrown88ce4932009-03-31 15:24:23 +11006318 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006319 /* can make the change immediately */
6320 if (mddev->new_layout >= 0) {
6321 conf->algorithm = mddev->new_layout;
6322 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006323 }
6324 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006325 conf->chunk_sectors = new_chunk ;
6326 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006327 }
6328 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6329 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006330 }
NeilBrown50ac1682009-06-18 08:47:55 +10006331 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006332}
6333
NeilBrownfd01b882011-10-11 16:47:53 +11006334static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006335{
NeilBrown597a7112009-06-18 08:47:42 +10006336 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006337
NeilBrown597a7112009-06-18 08:47:42 +10006338 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006339 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006340 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006341 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006342 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006343 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006344 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006345 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006346 /* not factor of array size */
6347 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006348 }
NeilBrown88ce4932009-03-31 15:24:23 +11006349
6350 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006351 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006352}
6353
NeilBrownfd01b882011-10-11 16:47:53 +11006354static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006355{
6356 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006357 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006358 * raid1 - if there are two drives. We need to know the chunk size
6359 * raid4 - trivial - just use a raid4 layout.
6360 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006361 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006362 if (mddev->level == 0)
6363 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006364 if (mddev->level == 1)
6365 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006366 if (mddev->level == 4) {
6367 mddev->new_layout = ALGORITHM_PARITY_N;
6368 mddev->new_level = 5;
6369 return setup_conf(mddev);
6370 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006371 if (mddev->level == 6)
6372 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006373
6374 return ERR_PTR(-EINVAL);
6375}
6376
NeilBrownfd01b882011-10-11 16:47:53 +11006377static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006378{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006379 /* raid4 can take over:
6380 * raid0 - if there is only one strip zone
6381 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006382 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006383 if (mddev->level == 0)
6384 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006385 if (mddev->level == 5 &&
6386 mddev->layout == ALGORITHM_PARITY_N) {
6387 mddev->new_layout = 0;
6388 mddev->new_level = 4;
6389 return setup_conf(mddev);
6390 }
6391 return ERR_PTR(-EINVAL);
6392}
NeilBrownd562b0c2009-03-31 14:39:39 +11006393
NeilBrown84fc4b52011-10-11 16:49:58 +11006394static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006395
NeilBrownfd01b882011-10-11 16:47:53 +11006396static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006397{
6398 /* Currently can only take over a raid5. We map the
6399 * personality to an equivalent raid6 personality
6400 * with the Q block at the end.
6401 */
6402 int new_layout;
6403
6404 if (mddev->pers != &raid5_personality)
6405 return ERR_PTR(-EINVAL);
6406 if (mddev->degraded > 1)
6407 return ERR_PTR(-EINVAL);
6408 if (mddev->raid_disks > 253)
6409 return ERR_PTR(-EINVAL);
6410 if (mddev->raid_disks < 3)
6411 return ERR_PTR(-EINVAL);
6412
6413 switch (mddev->layout) {
6414 case ALGORITHM_LEFT_ASYMMETRIC:
6415 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6416 break;
6417 case ALGORITHM_RIGHT_ASYMMETRIC:
6418 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6419 break;
6420 case ALGORITHM_LEFT_SYMMETRIC:
6421 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6422 break;
6423 case ALGORITHM_RIGHT_SYMMETRIC:
6424 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6425 break;
6426 case ALGORITHM_PARITY_0:
6427 new_layout = ALGORITHM_PARITY_0_6;
6428 break;
6429 case ALGORITHM_PARITY_N:
6430 new_layout = ALGORITHM_PARITY_N;
6431 break;
6432 default:
6433 return ERR_PTR(-EINVAL);
6434 }
6435 mddev->new_level = 6;
6436 mddev->new_layout = new_layout;
6437 mddev->delta_disks = 1;
6438 mddev->raid_disks += 1;
6439 return setup_conf(mddev);
6440}
6441
6442
NeilBrown84fc4b52011-10-11 16:49:58 +11006443static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006444{
6445 .name = "raid6",
6446 .level = 6,
6447 .owner = THIS_MODULE,
6448 .make_request = make_request,
6449 .run = run,
6450 .stop = stop,
6451 .status = status,
6452 .error_handler = error,
6453 .hot_add_disk = raid5_add_disk,
6454 .hot_remove_disk= raid5_remove_disk,
6455 .spare_active = raid5_spare_active,
6456 .sync_request = sync_request,
6457 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006458 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006459 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006460 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006461 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006462 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006463 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006464};
NeilBrown84fc4b52011-10-11 16:49:58 +11006465static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466{
6467 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006468 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 .owner = THIS_MODULE,
6470 .make_request = make_request,
6471 .run = run,
6472 .stop = stop,
6473 .status = status,
6474 .error_handler = error,
6475 .hot_add_disk = raid5_add_disk,
6476 .hot_remove_disk= raid5_remove_disk,
6477 .spare_active = raid5_spare_active,
6478 .sync_request = sync_request,
6479 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006480 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006481 .check_reshape = raid5_check_reshape,
6482 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006483 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006484 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006485 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486};
6487
NeilBrown84fc4b52011-10-11 16:49:58 +11006488static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489{
NeilBrown2604b702006-01-06 00:20:36 -08006490 .name = "raid4",
6491 .level = 4,
6492 .owner = THIS_MODULE,
6493 .make_request = make_request,
6494 .run = run,
6495 .stop = stop,
6496 .status = status,
6497 .error_handler = error,
6498 .hot_add_disk = raid5_add_disk,
6499 .hot_remove_disk= raid5_remove_disk,
6500 .spare_active = raid5_spare_active,
6501 .sync_request = sync_request,
6502 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006503 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006504 .check_reshape = raid5_check_reshape,
6505 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006506 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006507 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006508 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006509};
6510
6511static int __init raid5_init(void)
6512{
NeilBrown16a53ec2006-06-26 00:27:38 -07006513 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006514 register_md_personality(&raid5_personality);
6515 register_md_personality(&raid4_personality);
6516 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517}
6518
NeilBrown2604b702006-01-06 00:20:36 -08006519static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520{
NeilBrown16a53ec2006-06-26 00:27:38 -07006521 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006522 unregister_md_personality(&raid5_personality);
6523 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524}
6525
6526module_init(raid5_init);
6527module_exit(raid5_exit);
6528MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006529MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006531MODULE_ALIAS("md-raid5");
6532MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006533MODULE_ALIAS("md-level-5");
6534MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006535MODULE_ALIAS("md-personality-8"); /* RAID6 */
6536MODULE_ALIAS("md-raid6");
6537MODULE_ALIAS("md-level-6");
6538
6539/* This used to be two separate modules, they were: */
6540MODULE_ALIAS("raid5");
6541MODULE_ALIAS("raid6");