blob: 1b6986ce5da9af6c4cc424c56b15c5ebe73f91ad [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 ||
NeilBrown58f0e962015-02-18 11:35:14 +11002856 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
2857 s->failed == 0)) {
Alexander Lyakasa7854482012-10-11 13:50:12 +11002858 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002859 * look like rcw is cheaper
2860 */
2861 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002862 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2863 conf->max_degraded, (unsigned long long)recovery_cp,
2864 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002865 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002866 /* would I have to read this buffer for read_modify_write */
2867 struct r5dev *dev = &sh->dev[i];
2868 if ((dev->towrite || i == sh->pd_idx) &&
2869 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002870 !(test_bit(R5_UPTODATE, &dev->flags) ||
2871 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002872 if (test_bit(R5_Insync, &dev->flags))
2873 rmw++;
2874 else
2875 rmw += 2*disks; /* cannot read it */
2876 }
2877 /* Would I have to read this buffer for reconstruct_write */
2878 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2879 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002880 !(test_bit(R5_UPTODATE, &dev->flags) ||
2881 test_bit(R5_Wantcompute, &dev->flags))) {
2882 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002883 else
2884 rcw += 2*disks;
2885 }
2886 }
Dan Williams45b42332007-07-09 11:56:43 -07002887 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002888 (unsigned long long)sh->sector, rmw, rcw);
2889 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11002890 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002891 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06002892 if (conf->mddev->queue)
2893 blk_add_trace_msg(conf->mddev->queue,
2894 "raid5 rmw %llu %d",
2895 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07002896 for (i = disks; i--; ) {
2897 struct r5dev *dev = &sh->dev[i];
2898 if ((dev->towrite || i == sh->pd_idx) &&
2899 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002900 !(test_bit(R5_UPTODATE, &dev->flags) ||
2901 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002902 test_bit(R5_Insync, &dev->flags)) {
2903 if (
2904 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002905 pr_debug("Read_old block "
NeilBrowna9add5d2012-10-31 11:59:09 +11002906 "%d for r-m-w\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002907 set_bit(R5_LOCKED, &dev->flags);
2908 set_bit(R5_Wantread, &dev->flags);
2909 s->locked++;
2910 } else {
2911 set_bit(STRIPE_DELAYED, &sh->state);
2912 set_bit(STRIPE_HANDLE, &sh->state);
2913 }
2914 }
2915 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002916 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002917 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002918 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002919 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10002920 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002921 for (i = disks; i--; ) {
2922 struct r5dev *dev = &sh->dev[i];
2923 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002924 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002925 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002926 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002927 test_bit(R5_Wantcompute, &dev->flags))) {
2928 rcw++;
2929 if (!test_bit(R5_Insync, &dev->flags))
2930 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002931 if (
2932 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002933 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002934 "%d for Reconstruct\n", i);
2935 set_bit(R5_LOCKED, &dev->flags);
2936 set_bit(R5_Wantread, &dev->flags);
2937 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11002938 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07002939 } else {
2940 set_bit(STRIPE_DELAYED, &sh->state);
2941 set_bit(STRIPE_HANDLE, &sh->state);
2942 }
2943 }
2944 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06002945 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11002946 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
2947 (unsigned long long)sh->sector,
2948 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10002949 }
Dan Williamsa4456852007-07-09 11:56:43 -07002950 /* now if nothing is locked, and if we have enough data,
2951 * we can start a write request
2952 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002953 /* since handle_stripe can be called at any time we need to handle the
2954 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002955 * subsequent call wants to start a write request. raid_run_ops only
2956 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002957 * simultaneously. If this is not the case then new writes need to be
2958 * held off until the compute completes.
2959 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002960 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2961 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2962 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002963 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002964}
2965
NeilBrownd1688a62011-10-11 16:49:52 +11002966static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002967 struct stripe_head_state *s, int disks)
2968{
Dan Williamsecc65c92008-06-28 08:31:57 +10002969 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002970
Dan Williamsbd2ab672008-04-10 21:29:27 -07002971 set_bit(STRIPE_HANDLE, &sh->state);
2972
Dan Williamsecc65c92008-06-28 08:31:57 +10002973 switch (sh->check_state) {
2974 case check_state_idle:
2975 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002976 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002977 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002978 sh->check_state = check_state_run;
2979 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002980 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002981 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002982 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002983 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002984 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002985 /* fall through */
2986 case check_state_compute_result:
2987 sh->check_state = check_state_idle;
2988 if (!dev)
2989 dev = &sh->dev[sh->pd_idx];
2990
2991 /* check that a write has not made the stripe insync */
2992 if (test_bit(STRIPE_INSYNC, &sh->state))
2993 break;
2994
2995 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002996 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2997 BUG_ON(s->uptodate != disks);
2998
2999 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10003000 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07003001 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07003002
Dan Williamsa4456852007-07-09 11:56:43 -07003003 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07003004 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003005 break;
3006 case check_state_run:
3007 break; /* we will be called again upon completion */
3008 case check_state_check_result:
3009 sh->check_state = check_state_idle;
3010
3011 /* if a failure occurred during the check operation, leave
3012 * STRIPE_INSYNC not set and let the stripe be handled again
3013 */
3014 if (s->failed)
3015 break;
3016
3017 /* handle a successful check operation, if parity is correct
3018 * we are done. Otherwise update the mismatch count and repair
3019 * parity if !MD_RECOVERY_CHECK
3020 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003021 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003022 /* parity is correct (on disc,
3023 * not in buffer any more)
3024 */
3025 set_bit(STRIPE_INSYNC, &sh->state);
3026 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003027 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003028 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3029 /* don't try to repair!! */
3030 set_bit(STRIPE_INSYNC, &sh->state);
3031 else {
3032 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003033 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003034 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3035 set_bit(R5_Wantcompute,
3036 &sh->dev[sh->pd_idx].flags);
3037 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003038 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003039 s->uptodate++;
3040 }
3041 }
3042 break;
3043 case check_state_compute_run:
3044 break;
3045 default:
3046 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3047 __func__, sh->check_state,
3048 (unsigned long long) sh->sector);
3049 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003050 }
3051}
3052
3053
NeilBrownd1688a62011-10-11 16:49:52 +11003054static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003055 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003056 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003057{
Dan Williamsa4456852007-07-09 11:56:43 -07003058 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003059 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003060 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003061
3062 set_bit(STRIPE_HANDLE, &sh->state);
3063
3064 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003065
Dan Williamsa4456852007-07-09 11:56:43 -07003066 /* Want to check and possibly repair P and Q.
3067 * However there could be one 'failed' device, in which
3068 * case we can only check one of them, possibly using the
3069 * other to generate missing data
3070 */
3071
Dan Williamsd82dfee2009-07-14 13:40:57 -07003072 switch (sh->check_state) {
3073 case check_state_idle:
3074 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003075 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003076 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003077 * makes sense to check P (If anything else were failed,
3078 * we would have used P to recreate it).
3079 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003080 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003081 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003082 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003083 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003084 * anything, so it makes sense to check it
3085 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003086 if (sh->check_state == check_state_run)
3087 sh->check_state = check_state_run_pq;
3088 else
3089 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003090 }
Dan Williams36d1c642009-07-14 11:48:22 -07003091
Dan Williamsd82dfee2009-07-14 13:40:57 -07003092 /* discard potentially stale zero_sum_result */
3093 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003094
Dan Williamsd82dfee2009-07-14 13:40:57 -07003095 if (sh->check_state == check_state_run) {
3096 /* async_xor_zero_sum destroys the contents of P */
3097 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3098 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003099 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003100 if (sh->check_state >= check_state_run &&
3101 sh->check_state <= check_state_run_pq) {
3102 /* async_syndrome_zero_sum preserves P and Q, so
3103 * no need to mark them !uptodate here
3104 */
3105 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3106 break;
3107 }
Dan Williams36d1c642009-07-14 11:48:22 -07003108
Dan Williamsd82dfee2009-07-14 13:40:57 -07003109 /* we have 2-disk failure */
3110 BUG_ON(s->failed != 2);
3111 /* fall through */
3112 case check_state_compute_result:
3113 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003114
Dan Williamsd82dfee2009-07-14 13:40:57 -07003115 /* check that a write has not made the stripe insync */
3116 if (test_bit(STRIPE_INSYNC, &sh->state))
3117 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003118
3119 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003120 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003121 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003122 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003123 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003124 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003125 s->locked++;
3126 set_bit(R5_LOCKED, &dev->flags);
3127 set_bit(R5_Wantwrite, &dev->flags);
3128 }
3129 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003130 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003131 s->locked++;
3132 set_bit(R5_LOCKED, &dev->flags);
3133 set_bit(R5_Wantwrite, &dev->flags);
3134 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003135 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003136 dev = &sh->dev[pd_idx];
3137 s->locked++;
3138 set_bit(R5_LOCKED, &dev->flags);
3139 set_bit(R5_Wantwrite, &dev->flags);
3140 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003141 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003142 dev = &sh->dev[qd_idx];
3143 s->locked++;
3144 set_bit(R5_LOCKED, &dev->flags);
3145 set_bit(R5_Wantwrite, &dev->flags);
3146 }
3147 clear_bit(STRIPE_DEGRADED, &sh->state);
3148
3149 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003150 break;
3151 case check_state_run:
3152 case check_state_run_q:
3153 case check_state_run_pq:
3154 break; /* we will be called again upon completion */
3155 case check_state_check_result:
3156 sh->check_state = check_state_idle;
3157
3158 /* handle a successful check operation, if parity is correct
3159 * we are done. Otherwise update the mismatch count and repair
3160 * parity if !MD_RECOVERY_CHECK
3161 */
3162 if (sh->ops.zero_sum_result == 0) {
3163 /* both parities are correct */
3164 if (!s->failed)
3165 set_bit(STRIPE_INSYNC, &sh->state);
3166 else {
3167 /* in contrast to the raid5 case we can validate
3168 * parity, but still have a failure to write
3169 * back
3170 */
3171 sh->check_state = check_state_compute_result;
3172 /* Returning at this point means that we may go
3173 * off and bring p and/or q uptodate again so
3174 * we make sure to check zero_sum_result again
3175 * to verify if p or q need writeback
3176 */
3177 }
3178 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003179 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003180 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3181 /* don't try to repair!! */
3182 set_bit(STRIPE_INSYNC, &sh->state);
3183 else {
3184 int *target = &sh->ops.target;
3185
3186 sh->ops.target = -1;
3187 sh->ops.target2 = -1;
3188 sh->check_state = check_state_compute_run;
3189 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3190 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3191 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3192 set_bit(R5_Wantcompute,
3193 &sh->dev[pd_idx].flags);
3194 *target = pd_idx;
3195 target = &sh->ops.target2;
3196 s->uptodate++;
3197 }
3198 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3199 set_bit(R5_Wantcompute,
3200 &sh->dev[qd_idx].flags);
3201 *target = qd_idx;
3202 s->uptodate++;
3203 }
3204 }
3205 }
3206 break;
3207 case check_state_compute_run:
3208 break;
3209 default:
3210 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3211 __func__, sh->check_state,
3212 (unsigned long long) sh->sector);
3213 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003214 }
3215}
3216
NeilBrownd1688a62011-10-11 16:49:52 +11003217static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003218{
3219 int i;
3220
3221 /* We have read all the blocks in this stripe and now we need to
3222 * copy some of them into a target stripe for expand.
3223 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003224 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003225 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3226 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003227 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003228 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003229 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003230 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003231
NeilBrown784052e2009-03-31 15:19:07 +11003232 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003233 sector_t s = raid5_compute_sector(conf, bn, 0,
3234 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003235 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003236 if (sh2 == NULL)
3237 /* so far only the early blocks of this stripe
3238 * have been requested. When later blocks
3239 * get requested, we will try again
3240 */
3241 continue;
3242 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3243 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3244 /* must have already done this block */
3245 release_stripe(sh2);
3246 continue;
3247 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003248
3249 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003250 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003251 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003252 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003253 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003254
Dan Williamsa4456852007-07-09 11:56:43 -07003255 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3256 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3257 for (j = 0; j < conf->raid_disks; j++)
3258 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003259 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003260 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3261 break;
3262 if (j == conf->raid_disks) {
3263 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3264 set_bit(STRIPE_HANDLE, &sh2->state);
3265 }
3266 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003267
Dan Williamsa4456852007-07-09 11:56:43 -07003268 }
NeilBrowna2e08552007-09-11 15:23:36 -07003269 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003270 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003271}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273/*
3274 * handle_stripe - do things to a stripe.
3275 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003276 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3277 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003279 * return some read requests which now have data
3280 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 * schedule a read on some buffers
3282 * schedule a write of some buffers
3283 * return confirmation of parity correctness
3284 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 */
Dan Williamsa4456852007-07-09 11:56:43 -07003286
NeilBrownacfe7262011-07-27 11:00:36 +10003287static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003288{
NeilBrownd1688a62011-10-11 16:49:52 +11003289 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003290 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003291 struct r5dev *dev;
3292 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003293 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003294
NeilBrownacfe7262011-07-27 11:00:36 +10003295 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003296
NeilBrownacfe7262011-07-27 11:00:36 +10003297 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3298 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3299 s->failed_num[0] = -1;
3300 s->failed_num[1] = -1;
3301
3302 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003303 rcu_read_lock();
3304 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003305 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003306 sector_t first_bad;
3307 int bad_sectors;
3308 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003309
NeilBrown16a53ec2006-06-26 00:27:38 -07003310 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003311
Dan Williams45b42332007-07-09 11:56:43 -07003312 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003313 i, dev->flags,
3314 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003315 /* maybe we can reply to a read
3316 *
3317 * new wantfill requests are only permitted while
3318 * ops_complete_biofill is guaranteed to be inactive
3319 */
3320 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3321 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3322 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003323
3324 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003325 if (test_bit(R5_LOCKED, &dev->flags))
3326 s->locked++;
3327 if (test_bit(R5_UPTODATE, &dev->flags))
3328 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003329 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003330 s->compute++;
3331 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003332 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003333
NeilBrownacfe7262011-07-27 11:00:36 +10003334 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003335 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003336 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003337 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003338 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003339 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003340 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003341 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003342 }
Dan Williamsa4456852007-07-09 11:56:43 -07003343 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003344 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003345 /* Prefer to use the replacement for reads, but only
3346 * if it is recovered enough and has no bad blocks.
3347 */
3348 rdev = rcu_dereference(conf->disks[i].replacement);
3349 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3350 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3351 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3352 &first_bad, &bad_sectors))
3353 set_bit(R5_ReadRepl, &dev->flags);
3354 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003355 if (rdev)
3356 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003357 rdev = rcu_dereference(conf->disks[i].rdev);
3358 clear_bit(R5_ReadRepl, &dev->flags);
3359 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003360 if (rdev && test_bit(Faulty, &rdev->flags))
3361 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003362 if (rdev) {
3363 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3364 &first_bad, &bad_sectors);
3365 if (s->blocked_rdev == NULL
3366 && (test_bit(Blocked, &rdev->flags)
3367 || is_bad < 0)) {
3368 if (is_bad < 0)
3369 set_bit(BlockedBadBlocks,
3370 &rdev->flags);
3371 s->blocked_rdev = rdev;
3372 atomic_inc(&rdev->nr_pending);
3373 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003374 }
NeilBrown415e72d2010-06-17 17:25:21 +10003375 clear_bit(R5_Insync, &dev->flags);
3376 if (!rdev)
3377 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003378 else if (is_bad) {
3379 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003380 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3381 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003382 /* treat as in-sync, but with a read error
3383 * which we can now try to correct
3384 */
3385 set_bit(R5_Insync, &dev->flags);
3386 set_bit(R5_ReadError, &dev->flags);
3387 }
3388 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003389 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003390 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003391 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003392 set_bit(R5_Insync, &dev->flags);
3393 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3394 test_bit(R5_Expanded, &dev->flags))
3395 /* If we've reshaped into here, we assume it is Insync.
3396 * We will shortly update recovery_offset to make
3397 * it official.
3398 */
3399 set_bit(R5_Insync, &dev->flags);
3400
NeilBrownc44bc442014-01-06 13:19:42 +11003401 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003402 /* This flag does not apply to '.replacement'
3403 * only to .rdev, so make sure to check that*/
3404 struct md_rdev *rdev2 = rcu_dereference(
3405 conf->disks[i].rdev);
3406 if (rdev2 == rdev)
3407 clear_bit(R5_Insync, &dev->flags);
3408 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003409 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003410 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003411 } else
3412 clear_bit(R5_WriteError, &dev->flags);
3413 }
NeilBrownc44bc442014-01-06 13:19:42 +11003414 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003415 /* This flag does not apply to '.replacement'
3416 * only to .rdev, so make sure to check that*/
3417 struct md_rdev *rdev2 = rcu_dereference(
3418 conf->disks[i].rdev);
3419 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003420 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003421 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003422 } else
3423 clear_bit(R5_MadeGood, &dev->flags);
3424 }
NeilBrown977df362011-12-23 10:17:53 +11003425 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3426 struct md_rdev *rdev2 = rcu_dereference(
3427 conf->disks[i].replacement);
3428 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3429 s->handle_bad_blocks = 1;
3430 atomic_inc(&rdev2->nr_pending);
3431 } else
3432 clear_bit(R5_MadeGoodRepl, &dev->flags);
3433 }
NeilBrown415e72d2010-06-17 17:25:21 +10003434 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003435 /* The ReadError flag will just be confusing now */
3436 clear_bit(R5_ReadError, &dev->flags);
3437 clear_bit(R5_ReWrite, &dev->flags);
3438 }
NeilBrown415e72d2010-06-17 17:25:21 +10003439 if (test_bit(R5_ReadError, &dev->flags))
3440 clear_bit(R5_Insync, &dev->flags);
3441 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003442 if (s->failed < 2)
3443 s->failed_num[s->failed] = i;
3444 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003445 if (rdev && !test_bit(Faulty, &rdev->flags))
3446 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003447 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003448 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003449 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3450 /* If there is a failed device being replaced,
3451 * we must be recovering.
3452 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003453 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003454 * else we can only be replacing
3455 * sync and recovery both need to read all devices, and so
3456 * use the same flag.
3457 */
3458 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003459 sh->sector >= conf->mddev->recovery_cp ||
3460 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003461 s->syncing = 1;
3462 else
3463 s->replacing = 1;
3464 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003465 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003466}
NeilBrownf4168852007-02-28 20:11:53 -08003467
NeilBrowncc940152011-07-26 11:35:35 +10003468static void handle_stripe(struct stripe_head *sh)
3469{
3470 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003471 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003472 int i;
NeilBrown84789552011-07-27 11:00:36 +10003473 int prexor;
3474 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003475 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003476
3477 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003478 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003479 /* already being handled, ensure it gets handled
3480 * again when current action finishes */
3481 set_bit(STRIPE_HANDLE, &sh->state);
3482 return;
3483 }
3484
NeilBrownf8dfcff2013-03-12 12:18:06 +11003485 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3486 spin_lock(&sh->stripe_lock);
3487 /* Cannot process 'sync' concurrently with 'discard' */
3488 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
3489 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3490 set_bit(STRIPE_SYNCING, &sh->state);
3491 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownc1dadcc2013-07-22 12:57:21 +10003492 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003493 }
3494 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10003495 }
3496 clear_bit(STRIPE_DELAYED, &sh->state);
3497
3498 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3499 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3500 (unsigned long long)sh->sector, sh->state,
3501 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3502 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003503
NeilBrownacfe7262011-07-27 11:00:36 +10003504 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003505
NeilBrownbc2607f2011-07-28 11:39:22 +10003506 if (s.handle_bad_blocks) {
3507 set_bit(STRIPE_HANDLE, &sh->state);
3508 goto finish;
3509 }
3510
NeilBrown474af965fe2011-07-27 11:00:36 +10003511 if (unlikely(s.blocked_rdev)) {
3512 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003513 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003514 set_bit(STRIPE_HANDLE, &sh->state);
3515 goto finish;
3516 }
3517 /* There is nothing for the blocked_rdev to block */
3518 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3519 s.blocked_rdev = NULL;
3520 }
3521
3522 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3523 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3524 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3525 }
3526
3527 pr_debug("locked=%d uptodate=%d to_read=%d"
3528 " to_write=%d failed=%d failed_num=%d,%d\n",
3529 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3530 s.failed_num[0], s.failed_num[1]);
3531 /* check if the array has lost more than max_degraded devices and,
3532 * if so, some requests might need to be failed.
3533 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003534 if (s.failed > conf->max_degraded) {
3535 sh->check_state = 0;
3536 sh->reconstruct_state = 0;
3537 if (s.to_read+s.to_write+s.written)
3538 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003539 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003540 handle_failed_sync(conf, sh, &s);
3541 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003542
NeilBrown84789552011-07-27 11:00:36 +10003543 /* Now we check to see if any write operations have recently
3544 * completed
3545 */
3546 prexor = 0;
3547 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3548 prexor = 1;
3549 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3550 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3551 sh->reconstruct_state = reconstruct_state_idle;
3552
3553 /* All the 'written' buffers and the parity block are ready to
3554 * be written back to disk
3555 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003556 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3557 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003558 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003559 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3560 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003561 for (i = disks; i--; ) {
3562 struct r5dev *dev = &sh->dev[i];
3563 if (test_bit(R5_LOCKED, &dev->flags) &&
3564 (i == sh->pd_idx || i == sh->qd_idx ||
3565 dev->written)) {
3566 pr_debug("Writing block %d\n", i);
3567 set_bit(R5_Wantwrite, &dev->flags);
3568 if (prexor)
3569 continue;
NeilBrown318a3d52014-08-13 09:57:07 +10003570 if (s.failed > 1)
3571 continue;
NeilBrown84789552011-07-27 11:00:36 +10003572 if (!test_bit(R5_Insync, &dev->flags) ||
3573 ((i == sh->pd_idx || i == sh->qd_idx) &&
3574 s.failed == 0))
3575 set_bit(STRIPE_INSYNC, &sh->state);
3576 }
3577 }
3578 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3579 s.dec_preread_active = 1;
3580 }
3581
NeilBrownef5b7c62012-11-22 09:13:36 +11003582 /*
3583 * might be able to return some write requests if the parity blocks
3584 * are safe, or on a failed drive
3585 */
3586 pdev = &sh->dev[sh->pd_idx];
3587 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3588 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3589 qdev = &sh->dev[sh->qd_idx];
3590 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3591 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3592 || conf->level < 6;
3593
3594 if (s.written &&
3595 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3596 && !test_bit(R5_LOCKED, &pdev->flags)
3597 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3598 test_bit(R5_Discard, &pdev->flags))))) &&
3599 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3600 && !test_bit(R5_LOCKED, &qdev->flags)
3601 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3602 test_bit(R5_Discard, &qdev->flags))))))
3603 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3604
3605 /* Now we might consider reading some blocks, either to check/generate
3606 * parity, or to satisfy requests
3607 * or to load a block that is being partially written.
3608 */
3609 if (s.to_read || s.non_overwrite
3610 || (conf->level == 6 && s.to_write && s.failed)
3611 || (s.syncing && (s.uptodate + s.compute < disks))
3612 || s.replacing
3613 || s.expanding)
3614 handle_stripe_fill(sh, &s, disks);
3615
NeilBrown84789552011-07-27 11:00:36 +10003616 /* Now to consider new write requests and what else, if anything
3617 * should be read. We do not handle new writes when:
3618 * 1/ A 'write' operation (copy+xor) is already in flight.
3619 * 2/ A 'check' operation is in flight, as it may clobber the parity
3620 * block.
3621 */
3622 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3623 handle_stripe_dirtying(conf, sh, &s, disks);
3624
3625 /* maybe we need to check and possibly fix the parity for this stripe
3626 * Any reads will already have been scheduled, so we just see if enough
3627 * data is available. The parity check is held off while parity
3628 * dependent operations are in flight.
3629 */
3630 if (sh->check_state ||
3631 (s.syncing && s.locked == 0 &&
3632 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3633 !test_bit(STRIPE_INSYNC, &sh->state))) {
3634 if (conf->level == 6)
3635 handle_parity_checks6(conf, sh, &s, disks);
3636 else
3637 handle_parity_checks5(conf, sh, &s, disks);
3638 }
NeilBrownc5a31002011-07-27 11:00:36 +10003639
NeilBrownc1dadcc2013-07-22 12:57:21 +10003640 if ((s.replacing || s.syncing) && s.locked == 0
3641 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
3642 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11003643 /* Write out to replacement devices where possible */
3644 for (i = 0; i < conf->raid_disks; i++)
NeilBrownc1dadcc2013-07-22 12:57:21 +10003645 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3646 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11003647 set_bit(R5_WantReplace, &sh->dev[i].flags);
3648 set_bit(R5_LOCKED, &sh->dev[i].flags);
3649 s.locked++;
3650 }
NeilBrownc1dadcc2013-07-22 12:57:21 +10003651 if (s.replacing)
3652 set_bit(STRIPE_INSYNC, &sh->state);
3653 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11003654 }
3655 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownc1dadcc2013-07-22 12:57:21 +10003656 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11003657 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003658 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3659 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003660 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3661 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10003662 }
3663
3664 /* If the failed drives are just a ReadError, then we might need
3665 * to progress the repair/check process
3666 */
3667 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3668 for (i = 0; i < s.failed; i++) {
3669 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3670 if (test_bit(R5_ReadError, &dev->flags)
3671 && !test_bit(R5_LOCKED, &dev->flags)
3672 && test_bit(R5_UPTODATE, &dev->flags)
3673 ) {
3674 if (!test_bit(R5_ReWrite, &dev->flags)) {
3675 set_bit(R5_Wantwrite, &dev->flags);
3676 set_bit(R5_ReWrite, &dev->flags);
3677 set_bit(R5_LOCKED, &dev->flags);
3678 s.locked++;
3679 } else {
3680 /* let's read it back */
3681 set_bit(R5_Wantread, &dev->flags);
3682 set_bit(R5_LOCKED, &dev->flags);
3683 s.locked++;
3684 }
3685 }
3686 }
3687
3688
NeilBrown3687c062011-07-27 11:00:36 +10003689 /* Finish reconstruct operations initiated by the expansion process */
3690 if (sh->reconstruct_state == reconstruct_state_result) {
3691 struct stripe_head *sh_src
3692 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3693 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3694 /* sh cannot be written until sh_src has been read.
3695 * so arrange for sh to be delayed a little
3696 */
3697 set_bit(STRIPE_DELAYED, &sh->state);
3698 set_bit(STRIPE_HANDLE, &sh->state);
3699 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3700 &sh_src->state))
3701 atomic_inc(&conf->preread_active_stripes);
3702 release_stripe(sh_src);
3703 goto finish;
3704 }
3705 if (sh_src)
3706 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003707
NeilBrown3687c062011-07-27 11:00:36 +10003708 sh->reconstruct_state = reconstruct_state_idle;
3709 clear_bit(STRIPE_EXPANDING, &sh->state);
3710 for (i = conf->raid_disks; i--; ) {
3711 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3712 set_bit(R5_LOCKED, &sh->dev[i].flags);
3713 s.locked++;
3714 }
3715 }
3716
3717 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3718 !sh->reconstruct_state) {
3719 /* Need to write out all blocks after computing parity */
3720 sh->disks = conf->raid_disks;
3721 stripe_set_idx(sh->sector, conf, 0, sh);
3722 schedule_reconstruction(sh, &s, 1, 1);
3723 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3724 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3725 atomic_dec(&conf->reshape_stripes);
3726 wake_up(&conf->wait_for_overlap);
3727 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3728 }
3729
3730 if (s.expanding && s.locked == 0 &&
3731 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3732 handle_stripe_expansion(conf, sh);
3733
3734finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003735 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003736 if (unlikely(s.blocked_rdev)) {
3737 if (conf->mddev->external)
3738 md_wait_for_blocked_rdev(s.blocked_rdev,
3739 conf->mddev);
3740 else
3741 /* Internal metadata will immediately
3742 * be written by raid5d, so we don't
3743 * need to wait here.
3744 */
3745 rdev_dec_pending(s.blocked_rdev,
3746 conf->mddev);
3747 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003748
NeilBrownbc2607f2011-07-28 11:39:22 +10003749 if (s.handle_bad_blocks)
3750 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003751 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003752 struct r5dev *dev = &sh->dev[i];
3753 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3754 /* We own a safe reference to the rdev */
3755 rdev = conf->disks[i].rdev;
3756 if (!rdev_set_badblocks(rdev, sh->sector,
3757 STRIPE_SECTORS, 0))
3758 md_error(conf->mddev, rdev);
3759 rdev_dec_pending(rdev, conf->mddev);
3760 }
NeilBrownb84db562011-07-28 11:39:23 +10003761 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3762 rdev = conf->disks[i].rdev;
3763 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003764 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003765 rdev_dec_pending(rdev, conf->mddev);
3766 }
NeilBrown977df362011-12-23 10:17:53 +11003767 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3768 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003769 if (!rdev)
3770 /* rdev have been moved down */
3771 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003772 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003773 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003774 rdev_dec_pending(rdev, conf->mddev);
3775 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003776 }
3777
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003778 if (s.ops_request)
3779 raid_run_ops(sh, s.ops_request);
3780
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003781 ops_run_io(sh, &s);
3782
NeilBrownc5709ef2011-07-26 11:35:20 +10003783 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003784 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003785 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003786 * have actually been submitted.
3787 */
3788 atomic_dec(&conf->preread_active_stripes);
3789 if (atomic_read(&conf->preread_active_stripes) <
3790 IO_THRESHOLD)
3791 md_wakeup_thread(conf->mddev->thread);
3792 }
3793
NeilBrownc5709ef2011-07-26 11:35:20 +10003794 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003795
Dan Williams257a4b42011-11-08 16:22:06 +11003796 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003797}
3798
NeilBrownd1688a62011-10-11 16:49:52 +11003799static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800{
3801 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3802 while (!list_empty(&conf->delayed_list)) {
3803 struct list_head *l = conf->delayed_list.next;
3804 struct stripe_head *sh;
3805 sh = list_entry(l, struct stripe_head, lru);
3806 list_del_init(l);
3807 clear_bit(STRIPE_DELAYED, &sh->state);
3808 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3809 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003810 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 }
NeilBrown482c0832011-04-18 18:25:42 +10003812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813}
3814
NeilBrownd1688a62011-10-11 16:49:52 +11003815static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003816{
3817 /* device_lock is held */
3818 struct list_head head;
3819 list_add(&head, &conf->bitmap_list);
3820 list_del_init(&conf->bitmap_list);
3821 while (!list_empty(&head)) {
3822 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3823 list_del_init(&sh->lru);
3824 atomic_inc(&sh->count);
3825 __release_stripe(conf, sh);
3826 }
3827}
3828
NeilBrownfd01b882011-10-11 16:47:53 +11003829int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003830{
NeilBrownd1688a62011-10-11 16:49:52 +11003831 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003832
3833 /* No difference between reads and writes. Just check
3834 * how busy the stripe_cache is
3835 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003836
NeilBrownf022b2f2006-10-03 01:15:56 -07003837 if (conf->inactive_blocked)
3838 return 1;
3839 if (conf->quiesce)
3840 return 1;
3841 if (list_empty_careful(&conf->inactive_list))
3842 return 1;
3843
3844 return 0;
3845}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003846EXPORT_SYMBOL_GPL(md_raid5_congested);
3847
3848static int raid5_congested(void *data, int bits)
3849{
NeilBrownfd01b882011-10-11 16:47:53 +11003850 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003851
3852 return mddev_congested(mddev, bits) ||
3853 md_raid5_congested(mddev, bits);
3854}
NeilBrownf022b2f2006-10-03 01:15:56 -07003855
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003856/* We want read requests to align with chunks where possible,
3857 * but write requests don't need to.
3858 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003859static int raid5_mergeable_bvec(struct request_queue *q,
3860 struct bvec_merge_data *bvm,
3861 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003862{
NeilBrownfd01b882011-10-11 16:47:53 +11003863 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003864 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003865 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003866 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003867 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003868
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003869 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003870 return biovec->bv_len; /* always allow writes to be mergeable */
3871
Andre Noll664e7c42009-06-18 08:45:27 +10003872 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3873 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003874 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3875 if (max < 0) max = 0;
3876 if (max <= biovec->bv_len && bio_sectors == 0)
3877 return biovec->bv_len;
3878 else
3879 return max;
3880}
3881
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003882
NeilBrownfd01b882011-10-11 16:47:53 +11003883static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003884{
3885 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003886 unsigned int chunk_sectors = mddev->chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08003887 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003888
Andre Noll664e7c42009-06-18 08:45:27 +10003889 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3890 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003891 return chunk_sectors >=
3892 ((sector & (chunk_sectors - 1)) + bio_sectors);
3893}
3894
3895/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003896 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3897 * later sampled by raid5d.
3898 */
NeilBrownd1688a62011-10-11 16:49:52 +11003899static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003900{
3901 unsigned long flags;
3902
3903 spin_lock_irqsave(&conf->device_lock, flags);
3904
3905 bi->bi_next = conf->retry_read_aligned_list;
3906 conf->retry_read_aligned_list = bi;
3907
3908 spin_unlock_irqrestore(&conf->device_lock, flags);
3909 md_wakeup_thread(conf->mddev->thread);
3910}
3911
3912
NeilBrownd1688a62011-10-11 16:49:52 +11003913static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003914{
3915 struct bio *bi;
3916
3917 bi = conf->retry_read_aligned;
3918 if (bi) {
3919 conf->retry_read_aligned = NULL;
3920 return bi;
3921 }
3922 bi = conf->retry_read_aligned_list;
3923 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003924 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003925 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003926 /*
3927 * this sets the active strip count to 1 and the processed
3928 * strip count to zero (upper 8 bits)
3929 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003930 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003931 }
3932
3933 return bi;
3934}
3935
3936
3937/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003938 * The "raid5_align_endio" should check if the read succeeded and if it
3939 * did, call bio_endio on the original bio (having bio_put the new bio
3940 * first).
3941 * If the read failed..
3942 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003943static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003944{
3945 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003946 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003947 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003948 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003949 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003950
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003951 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003952
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003953 rdev = (void*)raid_bi->bi_next;
3954 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003955 mddev = rdev->mddev;
3956 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003957
3958 rdev_dec_pending(rdev, conf->mddev);
3959
3960 if (!error && uptodate) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07003961 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
3962 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02003963 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003964 if (atomic_dec_and_test(&conf->active_aligned_reads))
3965 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003966 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003967 }
3968
3969
Dan Williams45b42332007-07-09 11:56:43 -07003970 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003971
3972 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003973}
3974
Neil Brown387bb172007-02-08 14:20:29 -08003975static int bio_fits_rdev(struct bio *bi)
3976{
Jens Axboe165125e2007-07-24 09:28:11 +02003977 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003978
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08003979 if (bio_sectors(bi) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003980 return 0;
3981 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003982 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003983 return 0;
3984
3985 if (q->merge_bvec_fn)
3986 /* it's too hard to apply the merge_bvec_fn at this stage,
3987 * just just give up
3988 */
3989 return 0;
3990
3991 return 1;
3992}
3993
3994
NeilBrownfd01b882011-10-11 16:47:53 +11003995static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003996{
NeilBrownd1688a62011-10-11 16:49:52 +11003997 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003998 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003999 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11004000 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11004001 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004002
4003 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07004004 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004005 return 0;
4006 }
4007 /*
NeilBrowna167f662010-10-26 18:31:13 +11004008 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004009 */
NeilBrowna167f662010-10-26 18:31:13 +11004010 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004011 if (!align_bi)
4012 return 0;
4013 /*
4014 * set bi_end_io to a new function, and set bi_private to the
4015 * original bio.
4016 */
4017 align_bi->bi_end_io = raid5_align_endio;
4018 align_bi->bi_private = raid_bio;
4019 /*
4020 * compute position
4021 */
NeilBrown112bf892009-03-31 14:39:38 +11004022 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
4023 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11004024 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004025
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004026 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004027 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11004028 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4029 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4030 rdev->recovery_offset < end_sector) {
4031 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4032 if (rdev &&
4033 (test_bit(Faulty, &rdev->flags) ||
4034 !(test_bit(In_sync, &rdev->flags) ||
4035 rdev->recovery_offset >= end_sector)))
4036 rdev = NULL;
4037 }
4038 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10004039 sector_t first_bad;
4040 int bad_sectors;
4041
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004042 atomic_inc(&rdev->nr_pending);
4043 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004044 raid_bio->bi_next = (void*)rdev;
4045 align_bi->bi_bdev = rdev->bdev;
4046 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004047
NeilBrown31c176e2011-07-28 11:39:22 +10004048 if (!bio_fits_rdev(align_bi) ||
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004049 is_badblock(rdev, align_bi->bi_sector, bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10004050 &first_bad, &bad_sectors)) {
4051 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004052 bio_put(align_bi);
4053 rdev_dec_pending(rdev, mddev);
4054 return 0;
4055 }
4056
majianpeng6c0544e2012-06-12 08:31:10 +08004057 /* No reshape active, so we can trust rdev->data_offset */
4058 align_bi->bi_sector += rdev->data_offset;
4059
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004060 spin_lock_irq(&conf->device_lock);
4061 wait_event_lock_irq(conf->wait_for_stripe,
4062 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004063 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004064 atomic_inc(&conf->active_aligned_reads);
4065 spin_unlock_irq(&conf->device_lock);
4066
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004067 if (mddev->gendisk)
4068 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4069 align_bi, disk_devt(mddev->gendisk),
4070 raid_bio->bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004071 generic_make_request(align_bi);
4072 return 1;
4073 } else {
4074 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004075 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004076 return 0;
4077 }
4078}
4079
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004080/* __get_priority_stripe - get the next stripe to process
4081 *
4082 * Full stripe writes are allowed to pass preread active stripes up until
4083 * the bypass_threshold is exceeded. In general the bypass_count
4084 * increments when the handle_list is handled before the hold_list; however, it
4085 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4086 * stripe with in flight i/o. The bypass_count will be reset when the
4087 * head of the hold_list has changed, i.e. the head was promoted to the
4088 * handle_list.
4089 */
NeilBrownd1688a62011-10-11 16:49:52 +11004090static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004091{
4092 struct stripe_head *sh;
4093
4094 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4095 __func__,
4096 list_empty(&conf->handle_list) ? "empty" : "busy",
4097 list_empty(&conf->hold_list) ? "empty" : "busy",
4098 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4099
4100 if (!list_empty(&conf->handle_list)) {
4101 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4102
4103 if (list_empty(&conf->hold_list))
4104 conf->bypass_count = 0;
4105 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4106 if (conf->hold_list.next == conf->last_hold)
4107 conf->bypass_count++;
4108 else {
4109 conf->last_hold = conf->hold_list.next;
4110 conf->bypass_count -= conf->bypass_threshold;
4111 if (conf->bypass_count < 0)
4112 conf->bypass_count = 0;
4113 }
4114 }
4115 } else if (!list_empty(&conf->hold_list) &&
4116 ((conf->bypass_threshold &&
4117 conf->bypass_count > conf->bypass_threshold) ||
4118 atomic_read(&conf->pending_full_writes) == 0)) {
4119 sh = list_entry(conf->hold_list.next,
4120 typeof(*sh), lru);
4121 conf->bypass_count -= conf->bypass_threshold;
4122 if (conf->bypass_count < 0)
4123 conf->bypass_count = 0;
4124 } else
4125 return NULL;
4126
4127 list_del_init(&sh->lru);
4128 atomic_inc(&sh->count);
4129 BUG_ON(atomic_read(&sh->count) != 1);
4130 return sh;
4131}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004132
Shaohua Li8811b592012-08-02 08:33:00 +10004133struct raid5_plug_cb {
4134 struct blk_plug_cb cb;
4135 struct list_head list;
4136};
4137
4138static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4139{
4140 struct raid5_plug_cb *cb = container_of(
4141 blk_cb, struct raid5_plug_cb, cb);
4142 struct stripe_head *sh;
4143 struct mddev *mddev = cb->cb.data;
4144 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004145 int cnt = 0;
Shaohua Li8811b592012-08-02 08:33:00 +10004146
4147 if (cb->list.next && !list_empty(&cb->list)) {
4148 spin_lock_irq(&conf->device_lock);
4149 while (!list_empty(&cb->list)) {
4150 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4151 list_del_init(&sh->lru);
4152 /*
4153 * avoid race release_stripe_plug() sees
4154 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4155 * is still in our list
4156 */
4157 smp_mb__before_clear_bit();
4158 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4159 __release_stripe(conf, sh);
NeilBrowna9add5d2012-10-31 11:59:09 +11004160 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004161 }
4162 spin_unlock_irq(&conf->device_lock);
4163 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004164 if (mddev->queue)
4165 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004166 kfree(cb);
4167}
4168
4169static void release_stripe_plug(struct mddev *mddev,
4170 struct stripe_head *sh)
4171{
4172 struct blk_plug_cb *blk_cb = blk_check_plugged(
4173 raid5_unplug, mddev,
4174 sizeof(struct raid5_plug_cb));
4175 struct raid5_plug_cb *cb;
4176
4177 if (!blk_cb) {
4178 release_stripe(sh);
4179 return;
4180 }
4181
4182 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4183
4184 if (cb->list.next == NULL)
4185 INIT_LIST_HEAD(&cb->list);
4186
4187 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4188 list_add_tail(&sh->lru, &cb->list);
4189 else
4190 release_stripe(sh);
4191}
4192
Shaohua Li620125f2012-10-11 13:49:05 +11004193static void make_discard_request(struct mddev *mddev, struct bio *bi)
4194{
4195 struct r5conf *conf = mddev->private;
4196 sector_t logical_sector, last_sector;
4197 struct stripe_head *sh;
4198 int remaining;
4199 int stripe_sectors;
4200
4201 if (mddev->reshape_position != MaxSector)
4202 /* Skip discard while reshape is happening */
4203 return;
4204
4205 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4206 last_sector = bi->bi_sector + (bi->bi_size>>9);
4207
4208 bi->bi_next = NULL;
4209 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4210
4211 stripe_sectors = conf->chunk_sectors *
4212 (conf->raid_disks - conf->max_degraded);
4213 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4214 stripe_sectors);
4215 sector_div(last_sector, stripe_sectors);
4216
4217 logical_sector *= conf->chunk_sectors;
4218 last_sector *= conf->chunk_sectors;
4219
4220 for (; logical_sector < last_sector;
4221 logical_sector += STRIPE_SECTORS) {
4222 DEFINE_WAIT(w);
4223 int d;
4224 again:
4225 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4226 prepare_to_wait(&conf->wait_for_overlap, &w,
4227 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004228 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4229 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4230 release_stripe(sh);
4231 schedule();
4232 goto again;
4233 }
4234 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11004235 spin_lock_irq(&sh->stripe_lock);
4236 for (d = 0; d < conf->raid_disks; d++) {
4237 if (d == sh->pd_idx || d == sh->qd_idx)
4238 continue;
4239 if (sh->dev[d].towrite || sh->dev[d].toread) {
4240 set_bit(R5_Overlap, &sh->dev[d].flags);
4241 spin_unlock_irq(&sh->stripe_lock);
4242 release_stripe(sh);
4243 schedule();
4244 goto again;
4245 }
4246 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11004247 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11004248 finish_wait(&conf->wait_for_overlap, &w);
4249 for (d = 0; d < conf->raid_disks; d++) {
4250 if (d == sh->pd_idx || d == sh->qd_idx)
4251 continue;
4252 sh->dev[d].towrite = bi;
4253 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4254 raid5_inc_bi_active_stripes(bi);
4255 }
4256 spin_unlock_irq(&sh->stripe_lock);
4257 if (conf->mddev->bitmap) {
4258 for (d = 0;
4259 d < conf->raid_disks - conf->max_degraded;
4260 d++)
4261 bitmap_startwrite(mddev->bitmap,
4262 sh->sector,
4263 STRIPE_SECTORS,
4264 0);
4265 sh->bm_seq = conf->seq_flush + 1;
4266 set_bit(STRIPE_BIT_DELAY, &sh->state);
4267 }
4268
4269 set_bit(STRIPE_HANDLE, &sh->state);
4270 clear_bit(STRIPE_DELAYED, &sh->state);
4271 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4272 atomic_inc(&conf->preread_active_stripes);
4273 release_stripe_plug(mddev, sh);
4274 }
4275
4276 remaining = raid5_dec_bi_active_stripes(bi);
4277 if (remaining == 0) {
4278 md_write_end(mddev);
4279 bio_endio(bi, 0);
4280 }
4281}
4282
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004283static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
NeilBrownd1688a62011-10-11 16:49:52 +11004285 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004286 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 sector_t new_sector;
4288 sector_t logical_sector, last_sector;
4289 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004290 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004291 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Tejun Heoe9c74692010-09-03 11:56:18 +02004293 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4294 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004295 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004296 }
4297
NeilBrown3d310eb2005-06-21 17:17:26 -07004298 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004299
NeilBrown802ba062006-12-13 00:34:13 -08004300 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004301 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004302 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004303 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004304
Shaohua Li620125f2012-10-11 13:49:05 +11004305 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4306 make_discard_request(mddev, bi);
4307 return;
4308 }
4309
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004311 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 bi->bi_next = NULL;
4313 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004314
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4316 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004317 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004318
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004319 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004320 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004321 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004322 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004323 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004324 * 64bit on a 32bit platform, and so it might be
4325 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004326 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004327 * the lock is dropped, so once we get a reference
4328 * to the stripe that we think it is, we will have
4329 * to check again.
4330 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004331 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004332 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004333 ? logical_sector < conf->reshape_progress
4334 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004335 previous = 1;
4336 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004337 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004338 ? logical_sector < conf->reshape_safe
4339 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004340 spin_unlock_irq(&conf->device_lock);
4341 schedule();
4342 goto retry;
4343 }
4344 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004345 spin_unlock_irq(&conf->device_lock);
4346 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004347
NeilBrown112bf892009-03-31 14:39:38 +11004348 new_sector = raid5_compute_sector(conf, logical_sector,
4349 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004350 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004351 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 (unsigned long long)new_sector,
4353 (unsigned long long)logical_sector);
4354
NeilBrownb5663ba2009-03-31 14:39:38 +11004355 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004356 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004358 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004359 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004360 * stripe, so we must do the range check again.
4361 * Expansion could still move past after this
4362 * test, but as we are holding a reference to
4363 * 'sh', we know that if that happens,
4364 * STRIPE_EXPANDING will get set and the expansion
4365 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004366 */
4367 int must_retry = 0;
4368 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004369 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004370 ? logical_sector >= conf->reshape_progress
4371 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004372 /* mismatch, need to try again */
4373 must_retry = 1;
4374 spin_unlock_irq(&conf->device_lock);
4375 if (must_retry) {
4376 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004377 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004378 goto retry;
4379 }
4380 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004381
Namhyung Kimffd96e32011-07-18 17:38:51 +10004382 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004383 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004384 logical_sector < mddev->suspend_hi) {
4385 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004386 /* As the suspend_* range is controlled by
4387 * userspace, we want an interruptible
4388 * wait.
4389 */
4390 flush_signals(current);
4391 prepare_to_wait(&conf->wait_for_overlap,
4392 &w, TASK_INTERRUPTIBLE);
4393 if (logical_sector >= mddev->suspend_lo &&
4394 logical_sector < mddev->suspend_hi)
4395 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004396 goto retry;
4397 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004398
4399 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004400 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004401 /* Stripe is busy expanding or
4402 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 * and wait a while
4404 */
NeilBrown482c0832011-04-18 18:25:42 +10004405 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 release_stripe(sh);
4407 schedule();
4408 goto retry;
4409 }
4410 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004411 set_bit(STRIPE_HANDLE, &sh->state);
4412 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004413 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004414 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4415 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004416 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 } else {
4418 /* cannot get stripe for read-ahead, just give-up */
4419 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4420 finish_wait(&conf->wait_for_overlap, &w);
4421 break;
4422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004424
Shaohua Lie7836bd62012-07-19 16:01:31 +10004425 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004426 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
NeilBrown16a53ec2006-06-26 00:27:38 -07004428 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004430
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004431 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4432 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004433 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435}
4436
NeilBrownfd01b882011-10-11 16:47:53 +11004437static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004438
NeilBrownfd01b882011-10-11 16:47:53 +11004439static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440{
NeilBrown52c03292006-06-26 00:27:43 -07004441 /* reshaping is quite different to recovery/resync so it is
4442 * handled quite separately ... here.
4443 *
4444 * On each call to sync_request, we gather one chunk worth of
4445 * destination stripes and flag them as expanding.
4446 * Then we find all the source stripes and request reads.
4447 * As the reads complete, handle_stripe will copy the data
4448 * into the destination stripe and release that stripe.
4449 */
NeilBrownd1688a62011-10-11 16:49:52 +11004450 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004452 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004453 int raid_disks = conf->previous_raid_disks;
4454 int data_disks = raid_disks - conf->max_degraded;
4455 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004456 int i;
4457 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004458 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004459 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004460 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004461 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004462
NeilBrownfef9c612009-03-31 15:16:46 +11004463 if (sector_nr == 0) {
4464 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004465 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004466 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4467 sector_nr = raid5_size(mddev, 0, 0)
4468 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004469 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004470 conf->reshape_progress > 0)
4471 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004472 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004473 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004474 mddev->curr_resync_completed = sector_nr;
4475 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004476 *skipped = 1;
4477 return sector_nr;
4478 }
NeilBrown52c03292006-06-26 00:27:43 -07004479 }
4480
NeilBrown7a661382009-03-31 15:21:40 +11004481 /* We need to process a full chunk at a time.
4482 * If old and new chunk sizes differ, we need to process the
4483 * largest of these
4484 */
Andre Noll664e7c42009-06-18 08:45:27 +10004485 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4486 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004487 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004488 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004489
NeilBrownb5254dd2012-05-21 09:27:01 +10004490 /* We update the metadata at least every 10 seconds, or when
4491 * the data about to be copied would over-write the source of
4492 * the data at the front of the range. i.e. one new_stripe
4493 * along from reshape_progress new_maps to after where
4494 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004495 */
NeilBrownfef9c612009-03-31 15:16:46 +11004496 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004497 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004498 readpos = conf->reshape_progress;
4499 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004500 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004501 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004502 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004503 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004504 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004505 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004506 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004507 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004508 readpos -= min_t(sector_t, reshape_sectors, readpos);
4509 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004510 }
NeilBrown52c03292006-06-26 00:27:43 -07004511
NeilBrownb5254dd2012-05-21 09:27:01 +10004512 /* Having calculated the 'writepos' possibly use it
4513 * to set 'stripe_addr' which is where we will write to.
4514 */
4515 if (mddev->reshape_backwards) {
4516 BUG_ON(conf->reshape_progress == 0);
4517 stripe_addr = writepos;
4518 BUG_ON((mddev->dev_sectors &
4519 ~((sector_t)reshape_sectors - 1))
4520 - reshape_sectors - stripe_addr
4521 != sector_nr);
4522 } else {
4523 BUG_ON(writepos != sector_nr + reshape_sectors);
4524 stripe_addr = sector_nr;
4525 }
4526
NeilBrownc8f517c2009-03-31 15:28:40 +11004527 /* 'writepos' is the most advanced device address we might write.
4528 * 'readpos' is the least advanced device address we might read.
4529 * 'safepos' is the least address recorded in the metadata as having
4530 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004531 * If there is a min_offset_diff, these are adjusted either by
4532 * increasing the safepos/readpos if diff is negative, or
4533 * increasing writepos if diff is positive.
4534 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004535 * ensure safety in the face of a crash - that must be done by userspace
4536 * making a backup of the data. So in that case there is no particular
4537 * rush to update metadata.
4538 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4539 * update the metadata to advance 'safepos' to match 'readpos' so that
4540 * we can be safe in the event of a crash.
4541 * So we insist on updating metadata if safepos is behind writepos and
4542 * readpos is beyond writepos.
4543 * In any case, update the metadata every 10 seconds.
4544 * Maybe that number should be configurable, but I'm not sure it is
4545 * worth it.... maybe it could be a multiple of safemode_delay???
4546 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004547 if (conf->min_offset_diff < 0) {
4548 safepos += -conf->min_offset_diff;
4549 readpos += -conf->min_offset_diff;
4550 } else
4551 writepos += conf->min_offset_diff;
4552
NeilBrown2c810cd2012-05-21 09:27:00 +10004553 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004554 ? (safepos > writepos && readpos < writepos)
4555 : (safepos < writepos && readpos > writepos)) ||
4556 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004557 /* Cannot proceed until we've updated the superblock... */
4558 wait_event(conf->wait_for_overlap,
4559 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004560 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004561 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004562 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004563 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004564 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004565 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004566 kthread_should_stop());
4567 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004568 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004569 spin_unlock_irq(&conf->device_lock);
4570 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004571 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004572 }
4573
NeilBrownab69ae12009-03-31 15:26:47 +11004574 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004575 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004576 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004577 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004578 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004579 set_bit(STRIPE_EXPANDING, &sh->state);
4580 atomic_inc(&conf->reshape_stripes);
4581 /* If any of this stripe is beyond the end of the old
4582 * array, then we need to zero those blocks
4583 */
4584 for (j=sh->disks; j--;) {
4585 sector_t s;
4586 if (j == sh->pd_idx)
4587 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004588 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004589 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004590 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004591 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004592 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004593 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004594 continue;
4595 }
4596 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4597 set_bit(R5_Expanded, &sh->dev[j].flags);
4598 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4599 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004600 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004601 set_bit(STRIPE_EXPAND_READY, &sh->state);
4602 set_bit(STRIPE_HANDLE, &sh->state);
4603 }
NeilBrownab69ae12009-03-31 15:26:47 +11004604 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004605 }
4606 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004607 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004608 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004609 else
NeilBrown7a661382009-03-31 15:21:40 +11004610 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004611 spin_unlock_irq(&conf->device_lock);
4612 /* Ok, those stripe are ready. We can start scheduling
4613 * reads on the source stripes.
4614 * The source stripes are determined by mapping the first and last
4615 * block on the destination stripes.
4616 */
NeilBrown52c03292006-06-26 00:27:43 -07004617 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004618 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004619 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004620 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004621 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004622 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004623 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004624 if (last_sector >= mddev->dev_sectors)
4625 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004626 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004627 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004628 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4629 set_bit(STRIPE_HANDLE, &sh->state);
4630 release_stripe(sh);
4631 first_sector += STRIPE_SECTORS;
4632 }
NeilBrownab69ae12009-03-31 15:26:47 +11004633 /* Now that the sources are clearly marked, we can release
4634 * the destination stripes
4635 */
4636 while (!list_empty(&stripes)) {
4637 sh = list_entry(stripes.next, struct stripe_head, lru);
4638 list_del_init(&sh->lru);
4639 release_stripe(sh);
4640 }
NeilBrownc6207272008-02-06 01:39:52 -08004641 /* If this takes us to the resync_max point where we have to pause,
4642 * then we need to write out the superblock.
4643 */
NeilBrown7a661382009-03-31 15:21:40 +11004644 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004645 if ((sector_nr - mddev->curr_resync_completed) * 2
4646 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004647 /* Cannot proceed until we've updated the superblock... */
4648 wait_event(conf->wait_for_overlap,
4649 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004650 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004651 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004652 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004653 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4654 md_wakeup_thread(mddev->thread);
4655 wait_event(mddev->sb_wait,
4656 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4657 || kthread_should_stop());
4658 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004659 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004660 spin_unlock_irq(&conf->device_lock);
4661 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004662 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004663 }
NeilBrown7a661382009-03-31 15:21:40 +11004664 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004665}
4666
4667/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004668static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004669{
NeilBrownd1688a62011-10-11 16:49:52 +11004670 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004671 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004672 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004673 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004674 int still_degraded = 0;
4675 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
NeilBrown72626682005-09-09 16:23:54 -07004677 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004679
NeilBrown29269552006-03-27 01:18:10 -08004680 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4681 end_reshape(conf);
4682 return 0;
4683 }
NeilBrown72626682005-09-09 16:23:54 -07004684
4685 if (mddev->curr_resync < max_sector) /* aborted */
4686 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4687 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004688 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004689 conf->fullsync = 0;
4690 bitmap_close_sync(mddev->bitmap);
4691
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 return 0;
4693 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004694
NeilBrown64bd6602009-08-03 10:59:58 +10004695 /* Allow raid5_quiesce to complete */
4696 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4697
NeilBrown52c03292006-06-26 00:27:43 -07004698 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4699 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004700
NeilBrownc6207272008-02-06 01:39:52 -08004701 /* No need to check resync_max as we never do more than one
4702 * stripe, and as resync_max will always be on a chunk boundary,
4703 * if the check in md_do_sync didn't fire, there is no chance
4704 * of overstepping resync_max here
4705 */
4706
NeilBrown16a53ec2006-06-26 00:27:38 -07004707 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 * to resync, then assert that we are finished, because there is
4709 * nothing we can do.
4710 */
NeilBrown3285edf2006-06-26 00:27:55 -07004711 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004712 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004713 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004714 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 return rv;
4716 }
majianpeng6f608042013-04-24 11:42:41 +10004717 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
4718 !conf->fullsync &&
4719 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
4720 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07004721 /* we can skip this block, and probably more */
4722 sync_blocks /= STRIPE_SECTORS;
4723 *skipped = 1;
4724 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
NeilBrownb47490c2008-02-06 01:39:50 -08004727 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4728
NeilBrowna8c906c2009-06-09 14:39:59 +10004729 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004731 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004733 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004735 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004737 /* Need to check if array will still be degraded after recovery/resync
4738 * We don't need to check the 'failed' flag as when that gets set,
4739 * recovery aborts.
4740 */
NeilBrownf001a702009-06-09 14:30:31 +10004741 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004742 if (conf->disks[i].rdev == NULL)
4743 still_degraded = 1;
4744
4745 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4746
NeilBrown83206d62011-07-26 11:19:49 +10004747 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748
NeilBrown14425772009-10-16 15:55:25 +11004749 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 release_stripe(sh);
4751
4752 return STRIPE_SECTORS;
4753}
4754
NeilBrownd1688a62011-10-11 16:49:52 +11004755static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004756{
4757 /* We may not be able to submit a whole bio at once as there
4758 * may not be enough stripe_heads available.
4759 * We cannot pre-allocate enough stripe_heads as we may need
4760 * more than exist in the cache (if we allow ever large chunks).
4761 * So we do one stripe head at a time and record in
4762 * ->bi_hw_segments how many have been done.
4763 *
4764 * We *know* that this entire raid_bio is in one chunk, so
4765 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4766 */
4767 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004768 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004769 sector_t sector, logical_sector, last_sector;
4770 int scnt = 0;
4771 int remaining;
4772 int handled = 0;
4773
4774 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004775 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004776 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004777 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004778
4779 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004780 logical_sector += STRIPE_SECTORS,
4781 sector += STRIPE_SECTORS,
4782 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004783
Shaohua Lie7836bd62012-07-19 16:01:31 +10004784 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004785 /* already done this stripe */
4786 continue;
4787
NeilBrowna8c906c2009-06-09 14:39:59 +10004788 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004789
4790 if (!sh) {
4791 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004792 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004793 conf->retry_read_aligned = raid_bio;
4794 return handled;
4795 }
4796
Neil Brown387bb172007-02-08 14:20:29 -08004797 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4798 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004799 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004800 conf->retry_read_aligned = raid_bio;
4801 return handled;
4802 }
4803
majianpeng3f9e7c12012-07-31 10:04:21 +10004804 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004805 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004806 release_stripe(sh);
4807 handled++;
4808 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004809 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004810 if (remaining == 0) {
4811 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
4812 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004813 bio_endio(raid_bio, 0);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004814 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004815 if (atomic_dec_and_test(&conf->active_aligned_reads))
4816 wake_up(&conf->wait_for_stripe);
4817 return handled;
4818}
4819
Shaohua Li46a06402012-08-02 08:33:15 +10004820#define MAX_STRIPE_BATCH 8
4821static int handle_active_stripes(struct r5conf *conf)
4822{
4823 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4824 int i, batch_size = 0;
4825
4826 while (batch_size < MAX_STRIPE_BATCH &&
4827 (sh = __get_priority_stripe(conf)) != NULL)
4828 batch[batch_size++] = sh;
4829
4830 if (batch_size == 0)
4831 return batch_size;
4832 spin_unlock_irq(&conf->device_lock);
4833
4834 for (i = 0; i < batch_size; i++)
4835 handle_stripe(batch[i]);
4836
4837 cond_resched();
4838
4839 spin_lock_irq(&conf->device_lock);
4840 for (i = 0; i < batch_size; i++)
4841 __release_stripe(conf, batch[i]);
4842 return batch_size;
4843}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004844
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845/*
4846 * This is our raid5 kernel thread.
4847 *
4848 * We scan the hash table for stripes which can be handled now.
4849 * During the scan, completed stripes are saved for us by the interrupt
4850 * handler, so that they will not have to wait for our next wakeup.
4851 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004852static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853{
Shaohua Li4ed87312012-10-11 13:34:00 +11004854 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004855 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004857 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
Dan Williams45b42332007-07-09 11:56:43 -07004859 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
4861 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004863 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 handled = 0;
4865 spin_lock_irq(&conf->device_lock);
4866 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004867 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004868 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
NeilBrown0021b7b2012-07-31 09:08:14 +02004870 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004871 !list_empty(&conf->bitmap_list)) {
4872 /* Now is a good time to flush some bitmap updates */
4873 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004874 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004875 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004876 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004877 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004878 activate_bit_delay(conf);
4879 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004880 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004881
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004882 while ((bio = remove_bio_from_retry(conf))) {
4883 int ok;
4884 spin_unlock_irq(&conf->device_lock);
4885 ok = retry_aligned_read(conf, bio);
4886 spin_lock_irq(&conf->device_lock);
4887 if (!ok)
4888 break;
4889 handled++;
4890 }
4891
Shaohua Li46a06402012-08-02 08:33:15 +10004892 batch_size = handle_active_stripes(conf);
4893 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004895 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896
Shaohua Li46a06402012-08-02 08:33:15 +10004897 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4898 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004899 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004900 spin_lock_irq(&conf->device_lock);
4901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 }
Dan Williams45b42332007-07-09 11:56:43 -07004903 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
4905 spin_unlock_irq(&conf->device_lock);
4906
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004907 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004908 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
Dan Williams45b42332007-07-09 11:56:43 -07004910 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911}
4912
NeilBrown3f294f42005-11-08 21:39:25 -08004913static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004914raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004915{
NeilBrownd1688a62011-10-11 16:49:52 +11004916 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004917 if (conf)
4918 return sprintf(page, "%d\n", conf->max_nr_stripes);
4919 else
4920 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004921}
4922
NeilBrownc41d4ac2010-06-01 19:37:24 +10004923int
NeilBrownfd01b882011-10-11 16:47:53 +11004924raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004925{
NeilBrownd1688a62011-10-11 16:49:52 +11004926 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004927 int err;
4928
4929 if (size <= 16 || size > 32768)
4930 return -EINVAL;
4931 while (size < conf->max_nr_stripes) {
4932 if (drop_one_stripe(conf))
4933 conf->max_nr_stripes--;
4934 else
4935 break;
4936 }
4937 err = md_allow_write(mddev);
4938 if (err)
4939 return err;
4940 while (size > conf->max_nr_stripes) {
4941 if (grow_one_stripe(conf))
4942 conf->max_nr_stripes++;
4943 else break;
4944 }
4945 return 0;
4946}
4947EXPORT_SYMBOL(raid5_set_cache_size);
4948
NeilBrown3f294f42005-11-08 21:39:25 -08004949static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004950raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004951{
NeilBrownd1688a62011-10-11 16:49:52 +11004952 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004953 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004954 int err;
4955
NeilBrown3f294f42005-11-08 21:39:25 -08004956 if (len >= PAGE_SIZE)
4957 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004958 if (!conf)
4959 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004960
Dan Williams4ef197d82008-04-28 02:15:54 -07004961 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004962 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004963 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004964 if (err)
4965 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004966 return len;
4967}
NeilBrown007583c2005-11-08 21:39:30 -08004968
NeilBrown96de1e62005-11-08 21:39:39 -08004969static struct md_sysfs_entry
4970raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4971 raid5_show_stripe_cache_size,
4972 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004973
4974static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004975raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004976{
NeilBrownd1688a62011-10-11 16:49:52 +11004977 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004978 if (conf)
4979 return sprintf(page, "%d\n", conf->bypass_threshold);
4980 else
4981 return 0;
4982}
4983
4984static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004985raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004986{
NeilBrownd1688a62011-10-11 16:49:52 +11004987 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004988 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004989 if (len >= PAGE_SIZE)
4990 return -EINVAL;
4991 if (!conf)
4992 return -ENODEV;
4993
Dan Williams4ef197d82008-04-28 02:15:54 -07004994 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004995 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004996 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004997 return -EINVAL;
4998 conf->bypass_threshold = new;
4999 return len;
5000}
5001
5002static struct md_sysfs_entry
5003raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
5004 S_IRUGO | S_IWUSR,
5005 raid5_show_preread_threshold,
5006 raid5_store_preread_threshold);
5007
5008static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005009stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005010{
NeilBrownd1688a62011-10-11 16:49:52 +11005011 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005012 if (conf)
5013 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
5014 else
5015 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005016}
5017
NeilBrown96de1e62005-11-08 21:39:39 -08005018static struct md_sysfs_entry
5019raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08005020
NeilBrown007583c2005-11-08 21:39:30 -08005021static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08005022 &raid5_stripecache_size.attr,
5023 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005024 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08005025 NULL,
5026};
NeilBrown007583c2005-11-08 21:39:30 -08005027static struct attribute_group raid5_attrs_group = {
5028 .name = NULL,
5029 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08005030};
5031
Dan Williams80c3a6c2009-03-17 18:10:40 -07005032static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11005033raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07005034{
NeilBrownd1688a62011-10-11 16:49:52 +11005035 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005036
5037 if (!sectors)
5038 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005039 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11005040 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11005041 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005042
Andre Noll9d8f0362009-06-18 08:45:01 +10005043 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10005044 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005045 return sectors * (raid_disks - conf->max_degraded);
5046}
5047
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305048static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
5049{
5050 safe_put_page(percpu->spare_page);
5051 kfree(percpu->scribble);
5052 percpu->spare_page = NULL;
5053 percpu->scribble = NULL;
5054}
5055
5056static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
5057{
5058 if (conf->level == 6 && !percpu->spare_page)
5059 percpu->spare_page = alloc_page(GFP_KERNEL);
5060 if (!percpu->scribble)
5061 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5062
5063 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
5064 free_scratch_buffer(conf, percpu);
5065 return -ENOMEM;
5066 }
5067
5068 return 0;
5069}
5070
NeilBrownd1688a62011-10-11 16:49:52 +11005071static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005072{
Dan Williams36d1c642009-07-14 11:48:22 -07005073 unsigned long cpu;
5074
5075 if (!conf->percpu)
5076 return;
5077
Dan Williams36d1c642009-07-14 11:48:22 -07005078#ifdef CONFIG_HOTPLUG_CPU
5079 unregister_cpu_notifier(&conf->cpu_notify);
5080#endif
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305081
5082 get_online_cpus();
5083 for_each_possible_cpu(cpu)
5084 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07005085 put_online_cpus();
5086
5087 free_percpu(conf->percpu);
5088}
5089
NeilBrownd1688a62011-10-11 16:49:52 +11005090static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10005091{
5092 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07005093 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005094 kfree(conf->disks);
5095 kfree(conf->stripe_hashtbl);
5096 kfree(conf);
5097}
5098
Dan Williams36d1c642009-07-14 11:48:22 -07005099#ifdef CONFIG_HOTPLUG_CPU
5100static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5101 void *hcpu)
5102{
NeilBrownd1688a62011-10-11 16:49:52 +11005103 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005104 long cpu = (long)hcpu;
5105 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5106
5107 switch (action) {
5108 case CPU_UP_PREPARE:
5109 case CPU_UP_PREPARE_FROZEN:
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305110 if (alloc_scratch_buffer(conf, percpu)) {
Dan Williams36d1c642009-07-14 11:48:22 -07005111 pr_err("%s: failed memory allocation for cpu%ld\n",
5112 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005113 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005114 }
5115 break;
5116 case CPU_DEAD:
5117 case CPU_DEAD_FROZEN:
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305118 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07005119 break;
5120 default:
5121 break;
5122 }
5123 return NOTIFY_OK;
5124}
5125#endif
5126
NeilBrownd1688a62011-10-11 16:49:52 +11005127static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005128{
5129 unsigned long cpu;
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305130 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07005131
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305132 conf->percpu = alloc_percpu(struct raid5_percpu);
5133 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07005134 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07005135
Dan Williams36d1c642009-07-14 11:48:22 -07005136#ifdef CONFIG_HOTPLUG_CPU
5137 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5138 conf->cpu_notify.priority = 0;
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305139 err = register_cpu_notifier(&conf->cpu_notify);
5140 if (err)
5141 return err;
Dan Williams36d1c642009-07-14 11:48:22 -07005142#endif
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305143
5144 get_online_cpus();
5145 for_each_present_cpu(cpu) {
5146 err = alloc_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
5147 if (err) {
5148 pr_err("%s: failed memory allocation for cpu%ld\n",
5149 __func__, cpu);
5150 break;
5151 }
5152 }
Dan Williams36d1c642009-07-14 11:48:22 -07005153 put_online_cpus();
5154
5155 return err;
5156}
5157
NeilBrownd1688a62011-10-11 16:49:52 +11005158static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159{
NeilBrownd1688a62011-10-11 16:49:52 +11005160 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005161 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005162 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005164 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
NeilBrown91adb562009-03-31 14:39:39 +11005166 if (mddev->new_level != 5
5167 && mddev->new_level != 4
5168 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005169 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005170 mdname(mddev), mddev->new_level);
5171 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 }
NeilBrown91adb562009-03-31 14:39:39 +11005173 if ((mddev->new_level == 5
5174 && !algorithm_valid_raid5(mddev->new_layout)) ||
5175 (mddev->new_level == 6
5176 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005177 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005178 mdname(mddev), mddev->new_layout);
5179 return ERR_PTR(-EIO);
5180 }
5181 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005182 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005183 mdname(mddev), mddev->raid_disks);
5184 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
Andre Noll664e7c42009-06-18 08:45:27 +10005187 if (!mddev->new_chunk_sectors ||
5188 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5189 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005190 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5191 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005192 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005193 }
5194
NeilBrownd1688a62011-10-11 16:49:52 +11005195 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005196 if (conf == NULL)
5197 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005198 spin_lock_init(&conf->device_lock);
5199 init_waitqueue_head(&conf->wait_for_stripe);
5200 init_waitqueue_head(&conf->wait_for_overlap);
5201 INIT_LIST_HEAD(&conf->handle_list);
5202 INIT_LIST_HEAD(&conf->hold_list);
5203 INIT_LIST_HEAD(&conf->delayed_list);
5204 INIT_LIST_HEAD(&conf->bitmap_list);
5205 INIT_LIST_HEAD(&conf->inactive_list);
5206 atomic_set(&conf->active_stripes, 0);
5207 atomic_set(&conf->preread_active_stripes, 0);
5208 atomic_set(&conf->active_aligned_reads, 0);
5209 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005210 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005211
5212 conf->raid_disks = mddev->raid_disks;
5213 if (mddev->reshape_position == MaxSector)
5214 conf->previous_raid_disks = mddev->raid_disks;
5215 else
5216 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005217 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5218 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005219
NeilBrown5e5e3e72009-10-16 16:35:30 +11005220 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005221 GFP_KERNEL);
5222 if (!conf->disks)
5223 goto abort;
5224
5225 conf->mddev = mddev;
5226
5227 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5228 goto abort;
5229
Dan Williams36d1c642009-07-14 11:48:22 -07005230 conf->level = mddev->new_level;
5231 if (raid5_alloc_percpu(conf) != 0)
5232 goto abort;
5233
NeilBrown0c55e022010-05-03 14:09:02 +10005234 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005235
NeilBrowndafb20f2012-03-19 12:46:39 +11005236 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005237 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005238 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005239 || raid_disk < 0)
5240 continue;
5241 disk = conf->disks + raid_disk;
5242
NeilBrown17045f52011-12-23 10:17:53 +11005243 if (test_bit(Replacement, &rdev->flags)) {
5244 if (disk->replacement)
5245 goto abort;
5246 disk->replacement = rdev;
5247 } else {
5248 if (disk->rdev)
5249 goto abort;
5250 disk->rdev = rdev;
5251 }
NeilBrown91adb562009-03-31 14:39:39 +11005252
5253 if (test_bit(In_sync, &rdev->flags)) {
5254 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005255 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5256 " disk %d\n",
5257 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005258 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005259 /* Cannot rely on bitmap to complete recovery */
5260 conf->fullsync = 1;
5261 }
5262
Andre Noll09c9e5f2009-06-18 08:45:55 +10005263 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005264 conf->level = mddev->new_level;
5265 if (conf->level == 6)
5266 conf->max_degraded = 2;
5267 else
5268 conf->max_degraded = 1;
5269 conf->algorithm = mddev->new_layout;
5270 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005271 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005272 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005273 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005274 conf->prev_algo = mddev->layout;
5275 }
NeilBrown91adb562009-03-31 14:39:39 +11005276
5277 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005278 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005279 if (grow_stripes(conf, conf->max_nr_stripes)) {
5280 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005281 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5282 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005283 goto abort;
5284 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005285 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5286 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005287
NeilBrown02326052012-07-03 15:56:52 +10005288 sprintf(pers_name, "raid%d", mddev->new_level);
5289 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005290 if (!conf->thread) {
5291 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005292 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005293 mdname(mddev));
5294 goto abort;
5295 }
5296
5297 return conf;
5298
5299 abort:
5300 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005301 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005302 return ERR_PTR(-EIO);
5303 } else
5304 return ERR_PTR(-ENOMEM);
5305}
5306
NeilBrownc148ffd2009-11-13 17:47:00 +11005307
5308static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5309{
5310 switch (algo) {
5311 case ALGORITHM_PARITY_0:
5312 if (raid_disk < max_degraded)
5313 return 1;
5314 break;
5315 case ALGORITHM_PARITY_N:
5316 if (raid_disk >= raid_disks - max_degraded)
5317 return 1;
5318 break;
5319 case ALGORITHM_PARITY_0_6:
5320 if (raid_disk == 0 ||
5321 raid_disk == raid_disks - 1)
5322 return 1;
5323 break;
5324 case ALGORITHM_LEFT_ASYMMETRIC_6:
5325 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5326 case ALGORITHM_LEFT_SYMMETRIC_6:
5327 case ALGORITHM_RIGHT_SYMMETRIC_6:
5328 if (raid_disk == raid_disks - 1)
5329 return 1;
5330 }
5331 return 0;
5332}
5333
NeilBrownfd01b882011-10-11 16:47:53 +11005334static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005335{
NeilBrownd1688a62011-10-11 16:49:52 +11005336 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005337 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005338 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005339 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005340 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005341 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005342 long long min_offset_diff = 0;
5343 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005344
Andre Noll8c6ac862009-06-18 08:48:06 +10005345 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005346 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005347 " -- starting background reconstruction\n",
5348 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005349
5350 rdev_for_each(rdev, mddev) {
5351 long long diff;
5352 if (rdev->raid_disk < 0)
5353 continue;
5354 diff = (rdev->new_data_offset - rdev->data_offset);
5355 if (first) {
5356 min_offset_diff = diff;
5357 first = 0;
5358 } else if (mddev->reshape_backwards &&
5359 diff < min_offset_diff)
5360 min_offset_diff = diff;
5361 else if (!mddev->reshape_backwards &&
5362 diff > min_offset_diff)
5363 min_offset_diff = diff;
5364 }
5365
NeilBrownf6705572006-03-27 01:18:11 -08005366 if (mddev->reshape_position != MaxSector) {
5367 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005368 * Difficulties arise if the stripe we would write to
5369 * next is at or after the stripe we would read from next.
5370 * For a reshape that changes the number of devices, this
5371 * is only possible for a very short time, and mdadm makes
5372 * sure that time appears to have past before assembling
5373 * the array. So we fail if that time hasn't passed.
5374 * For a reshape that keeps the number of devices the same
5375 * mdadm must be monitoring the reshape can keeping the
5376 * critical areas read-only and backed up. It will start
5377 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005378 */
5379 sector_t here_new, here_old;
5380 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005381 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005382
NeilBrown88ce4932009-03-31 15:24:23 +11005383 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005384 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005385 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005386 mdname(mddev));
5387 return -EINVAL;
5388 }
NeilBrownf6705572006-03-27 01:18:11 -08005389 old_disks = mddev->raid_disks - mddev->delta_disks;
5390 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005391 * further up in new geometry must map after here in old
5392 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005393 */
5394 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005395 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005396 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005397 printk(KERN_ERR "md/raid:%s: reshape_position not "
5398 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005399 return -EINVAL;
5400 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005401 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005402 /* here_new is the stripe we will write to */
5403 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005404 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005405 (old_disks-max_degraded));
5406 /* here_old is the first stripe that we might need to read
5407 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005408 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005409 if ((here_new * mddev->new_chunk_sectors !=
5410 here_old * mddev->chunk_sectors)) {
5411 printk(KERN_ERR "md/raid:%s: reshape position is"
5412 " confused - aborting\n", mdname(mddev));
5413 return -EINVAL;
5414 }
NeilBrown67ac6012009-08-13 10:06:24 +10005415 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005416 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005417 * and taking constant backups.
5418 * mdadm always starts a situation like this in
5419 * readonly mode so it can take control before
5420 * allowing any writes. So just check for that.
5421 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005422 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5423 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5424 /* not really in-place - so OK */;
5425 else if (mddev->ro == 0) {
5426 printk(KERN_ERR "md/raid:%s: in-place reshape "
5427 "must be started in read-only mode "
5428 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005429 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005430 return -EINVAL;
5431 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005432 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005433 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005434 here_old * mddev->chunk_sectors)
5435 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005436 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005437 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005438 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5439 "auto-recovery - aborting.\n",
5440 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005441 return -EINVAL;
5442 }
NeilBrown0c55e022010-05-03 14:09:02 +10005443 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5444 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005445 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005446 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005447 BUG_ON(mddev->level != mddev->new_level);
5448 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005449 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005450 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005451 }
5452
NeilBrown245f46c2009-03-31 14:39:39 +11005453 if (mddev->private == NULL)
5454 conf = setup_conf(mddev);
5455 else
5456 conf = mddev->private;
5457
NeilBrown91adb562009-03-31 14:39:39 +11005458 if (IS_ERR(conf))
5459 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005460
NeilBrownb5254dd2012-05-21 09:27:01 +10005461 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005462 mddev->thread = conf->thread;
5463 conf->thread = NULL;
5464 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
NeilBrown17045f52011-12-23 10:17:53 +11005466 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5467 i++) {
5468 rdev = conf->disks[i].rdev;
5469 if (!rdev && conf->disks[i].replacement) {
5470 /* The replacement is all we have yet */
5471 rdev = conf->disks[i].replacement;
5472 conf->disks[i].replacement = NULL;
5473 clear_bit(Replacement, &rdev->flags);
5474 conf->disks[i].rdev = rdev;
5475 }
5476 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005477 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005478 if (conf->disks[i].replacement &&
5479 conf->reshape_progress != MaxSector) {
5480 /* replacements and reshape simply do not mix. */
5481 printk(KERN_ERR "md: cannot handle concurrent "
5482 "replacement and reshape.\n");
5483 goto abort;
5484 }
NeilBrown2f115882010-06-17 17:41:03 +10005485 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005486 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005487 continue;
5488 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005489 /* This disc is not fully in-sync. However if it
5490 * just stored parity (beyond the recovery_offset),
5491 * when we don't need to be concerned about the
5492 * array being dirty.
5493 * When reshape goes 'backwards', we never have
5494 * partially completed devices, so we only need
5495 * to worry about reshape going forwards.
5496 */
5497 /* Hack because v0.91 doesn't store recovery_offset properly. */
5498 if (mddev->major_version == 0 &&
5499 mddev->minor_version > 90)
5500 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005501
NeilBrownc148ffd2009-11-13 17:47:00 +11005502 if (rdev->recovery_offset < reshape_offset) {
5503 /* We need to check old and new layout */
5504 if (!only_parity(rdev->raid_disk,
5505 conf->algorithm,
5506 conf->raid_disks,
5507 conf->max_degraded))
5508 continue;
5509 }
5510 if (!only_parity(rdev->raid_disk,
5511 conf->prev_algo,
5512 conf->previous_raid_disks,
5513 conf->max_degraded))
5514 continue;
5515 dirty_parity_disks++;
5516 }
NeilBrown91adb562009-03-31 14:39:39 +11005517
NeilBrown17045f52011-12-23 10:17:53 +11005518 /*
5519 * 0 for a fully functional array, 1 or 2 for a degraded array.
5520 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005521 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522
NeilBrown674806d2010-06-16 17:17:53 +10005523 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005524 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005526 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 goto abort;
5528 }
5529
NeilBrown91adb562009-03-31 14:39:39 +11005530 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005531 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005532 mddev->resync_max_sectors = mddev->dev_sectors;
5533
NeilBrownc148ffd2009-11-13 17:47:00 +11005534 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005536 if (mddev->ok_start_degraded)
5537 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005538 "md/raid:%s: starting dirty degraded array"
5539 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005540 mdname(mddev));
5541 else {
5542 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005543 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005544 mdname(mddev));
5545 goto abort;
5546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 }
5548
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005550 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5551 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005552 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5553 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 else
NeilBrown0c55e022010-05-03 14:09:02 +10005555 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5556 " out of %d devices, algorithm %d\n",
5557 mdname(mddev), conf->level,
5558 mddev->raid_disks - mddev->degraded,
5559 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560
5561 print_raid5_conf(conf);
5562
NeilBrownfef9c612009-03-31 15:16:46 +11005563 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005564 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005565 atomic_set(&conf->reshape_stripes, 0);
5566 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5567 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5568 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5569 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5570 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005571 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005572 }
5573
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574
5575 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005576 if (mddev->to_remove == &raid5_attrs_group)
5577 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005578 else if (mddev->kobj.sd &&
5579 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005580 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005581 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005582 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005583 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5584
5585 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005586 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005587 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005588 /* read-ahead size must cover two whole stripes, which
5589 * is 2 * (datadisks) * chunksize where 'n' is the
5590 * number of raid devices
5591 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5593 int stripe = data_disks *
5594 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5595 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5596 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005597
5598 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005599
5600 mddev->queue->backing_dev_info.congested_data = mddev;
5601 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005602
5603 chunk_size = mddev->chunk_sectors << 9;
5604 blk_queue_io_min(mddev->queue, chunk_size);
5605 blk_queue_io_opt(mddev->queue, chunk_size *
5606 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005607 /*
5608 * We can only discard a whole stripe. It doesn't make sense to
5609 * discard data disk but write parity disk
5610 */
5611 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11005612 /* Round up to power of 2, as discard handling
5613 * currently assumes that */
5614 while ((stripe-1) & stripe)
5615 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11005616 mddev->queue->limits.discard_alignment = stripe;
5617 mddev->queue->limits.discard_granularity = stripe;
5618 /*
5619 * unaligned part of discard request will be ignored, so can't
NeilBrown06905ff2014-10-02 13:45:00 +10005620 * guarantee discard_zeroes_data
Shaohua Li620125f2012-10-11 13:49:05 +11005621 */
5622 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005623
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005624 blk_queue_max_write_same_sectors(mddev->queue, 0);
5625
NeilBrown05616be2012-05-21 09:27:00 +10005626 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005627 disk_stack_limits(mddev->gendisk, rdev->bdev,
5628 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005629 disk_stack_limits(mddev->gendisk, rdev->bdev,
5630 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005631 /*
5632 * discard_zeroes_data is required, otherwise data
5633 * could be lost. Consider a scenario: discard a stripe
5634 * (the stripe could be inconsistent if
5635 * discard_zeroes_data is 0); write one disk of the
5636 * stripe (the stripe could be inconsistent again
5637 * depending on which disks are used to calculate
5638 * parity); the disk is broken; The stripe data of this
5639 * disk is lost.
5640 */
5641 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5642 !bdev_get_queue(rdev->bdev)->
5643 limits.discard_zeroes_data)
5644 discard_supported = false;
NeilBrown06905ff2014-10-02 13:45:00 +10005645 /* Unfortunately, discard_zeroes_data is not currently
5646 * a guarantee - just a hint. So we only allow DISCARD
5647 * if the sysadmin has confirmed that only safe devices
5648 * are in use by setting a module parameter.
5649 */
5650 if (!devices_handle_discard_safely) {
5651 if (discard_supported) {
5652 pr_info("md/raid456: discard support disabled due to uncertainty.\n");
5653 pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n");
5654 }
5655 discard_supported = false;
5656 }
NeilBrown05616be2012-05-21 09:27:00 +10005657 }
Shaohua Li620125f2012-10-11 13:49:05 +11005658
5659 if (discard_supported &&
5660 mddev->queue->limits.max_discard_sectors >= stripe &&
5661 mddev->queue->limits.discard_granularity >= stripe)
5662 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5663 mddev->queue);
5664 else
5665 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5666 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 }
5668
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 return 0;
5670abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005671 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005672 print_raid5_conf(conf);
5673 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005675 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 return -EIO;
5677}
5678
NeilBrownfd01b882011-10-11 16:47:53 +11005679static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680{
NeilBrownd1688a62011-10-11 16:49:52 +11005681 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682
NeilBrown01f96c02011-09-21 15:30:20 +10005683 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005684 if (mddev->queue)
5685 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005686 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005687 mddev->private = NULL;
5688 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 return 0;
5690}
5691
NeilBrownfd01b882011-10-11 16:47:53 +11005692static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693{
NeilBrownd1688a62011-10-11 16:49:52 +11005694 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 int i;
5696
Andre Noll9d8f0362009-06-18 08:45:01 +10005697 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5698 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005699 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 for (i = 0; i < conf->raid_disks; i++)
5701 seq_printf (seq, "%s",
5702 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005703 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705}
5706
NeilBrownd1688a62011-10-11 16:49:52 +11005707static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708{
5709 int i;
5710 struct disk_info *tmp;
5711
NeilBrown0c55e022010-05-03 14:09:02 +10005712 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 if (!conf) {
5714 printk("(conf==NULL)\n");
5715 return;
5716 }
NeilBrown0c55e022010-05-03 14:09:02 +10005717 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5718 conf->raid_disks,
5719 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
5721 for (i = 0; i < conf->raid_disks; i++) {
5722 char b[BDEVNAME_SIZE];
5723 tmp = conf->disks + i;
5724 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005725 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5726 i, !test_bit(Faulty, &tmp->rdev->flags),
5727 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 }
5729}
5730
NeilBrownfd01b882011-10-11 16:47:53 +11005731static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732{
5733 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005734 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005736 int count = 0;
5737 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738
5739 for (i = 0; i < conf->raid_disks; i++) {
5740 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005741 if (tmp->replacement
5742 && tmp->replacement->recovery_offset == MaxSector
5743 && !test_bit(Faulty, &tmp->replacement->flags)
5744 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5745 /* Replacement has just become active. */
5746 if (!tmp->rdev
5747 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5748 count++;
5749 if (tmp->rdev) {
5750 /* Replaced device not technically faulty,
5751 * but we need to be sure it gets removed
5752 * and never re-added.
5753 */
5754 set_bit(Faulty, &tmp->rdev->flags);
5755 sysfs_notify_dirent_safe(
5756 tmp->rdev->sysfs_state);
5757 }
5758 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5759 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005760 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005761 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005762 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005763 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005764 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 }
5766 }
NeilBrown6b965622010-08-18 11:56:59 +10005767 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005768 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005769 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005771 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772}
5773
NeilBrownb8321b62011-12-23 10:17:51 +11005774static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775{
NeilBrownd1688a62011-10-11 16:49:52 +11005776 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005778 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005779 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 struct disk_info *p = conf->disks + number;
5781
5782 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005783 if (rdev == p->rdev)
5784 rdevp = &p->rdev;
5785 else if (rdev == p->replacement)
5786 rdevp = &p->replacement;
5787 else
5788 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005789
NeilBrown657e3e42011-12-23 10:17:52 +11005790 if (number >= conf->raid_disks &&
5791 conf->reshape_progress == MaxSector)
5792 clear_bit(In_sync, &rdev->flags);
5793
5794 if (test_bit(In_sync, &rdev->flags) ||
5795 atomic_read(&rdev->nr_pending)) {
5796 err = -EBUSY;
5797 goto abort;
5798 }
5799 /* Only remove non-faulty devices if recovery
5800 * isn't possible.
5801 */
5802 if (!test_bit(Faulty, &rdev->flags) &&
5803 mddev->recovery_disabled != conf->recovery_disabled &&
5804 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005805 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005806 number < conf->raid_disks) {
5807 err = -EBUSY;
5808 goto abort;
5809 }
5810 *rdevp = NULL;
5811 synchronize_rcu();
5812 if (atomic_read(&rdev->nr_pending)) {
5813 /* lost the race, try later */
5814 err = -EBUSY;
5815 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005816 } else if (p->replacement) {
5817 /* We must have just cleared 'rdev' */
5818 p->rdev = p->replacement;
5819 clear_bit(Replacement, &p->replacement->flags);
5820 smp_mb(); /* Make sure other CPUs may see both as identical
5821 * but will never see neither - if they are careful
5822 */
5823 p->replacement = NULL;
5824 clear_bit(WantReplacement, &rdev->flags);
5825 } else
5826 /* We might have just removed the Replacement as faulty-
5827 * clear the bit just in case
5828 */
5829 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830abort:
5831
5832 print_raid5_conf(conf);
5833 return err;
5834}
5835
NeilBrownfd01b882011-10-11 16:47:53 +11005836static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837{
NeilBrownd1688a62011-10-11 16:49:52 +11005838 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005839 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 int disk;
5841 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005842 int first = 0;
5843 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844
NeilBrown7f0da592011-07-28 11:39:22 +10005845 if (mddev->recovery_disabled == conf->recovery_disabled)
5846 return -EBUSY;
5847
NeilBrowndc10c642012-03-19 12:46:37 +11005848 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005850 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
Neil Brown6c2fce22008-06-28 08:31:31 +10005852 if (rdev->raid_disk >= 0)
5853 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854
5855 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005856 * find the disk ... but prefer rdev->saved_raid_disk
5857 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005859 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005860 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005861 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005862 first = rdev->saved_raid_disk;
5863
5864 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005865 p = conf->disks + disk;
5866 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005867 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005869 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005870 if (rdev->saved_raid_disk != disk)
5871 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005872 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005873 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005875 }
5876 for (disk = first; disk <= last; disk++) {
5877 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005878 if (test_bit(WantReplacement, &p->rdev->flags) &&
5879 p->replacement == NULL) {
5880 clear_bit(In_sync, &rdev->flags);
5881 set_bit(Replacement, &rdev->flags);
5882 rdev->raid_disk = disk;
5883 err = 0;
5884 conf->fullsync = 1;
5885 rcu_assign_pointer(p->replacement, rdev);
5886 break;
5887 }
5888 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005889out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005891 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892}
5893
NeilBrownfd01b882011-10-11 16:47:53 +11005894static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895{
5896 /* no resync is happening, and there is enough space
5897 * on all devices, so we can resize.
5898 * We need to make sure resync covers any new space.
5899 * If the array is shrinking we should possibly wait until
5900 * any io in the removed space completes, but it hardly seems
5901 * worth it.
5902 */
NeilBrowna4a61252012-05-22 13:55:27 +10005903 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005904 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005905 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5906 if (mddev->external_size &&
5907 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005908 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005909 if (mddev->bitmap) {
5910 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5911 if (ret)
5912 return ret;
5913 }
5914 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005915 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005916 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005917 if (sectors > mddev->dev_sectors &&
5918 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005919 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5921 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005922 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005923 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 return 0;
5925}
5926
NeilBrownfd01b882011-10-11 16:47:53 +11005927static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005928{
5929 /* Can only proceed if there are plenty of stripe_heads.
5930 * We need a minimum of one full stripe,, and for sensible progress
5931 * it is best to have about 4 times that.
5932 * If we require 4 times, then the default 256 4K stripe_heads will
5933 * allow for chunk sizes up to 256K, which is probably OK.
5934 * If the chunk size is greater, user-space should request more
5935 * stripe_heads first.
5936 */
NeilBrownd1688a62011-10-11 16:49:52 +11005937 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005938 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5939 > conf->max_nr_stripes ||
5940 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5941 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005942 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5943 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005944 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5945 / STRIPE_SIZE)*4);
5946 return 0;
5947 }
5948 return 1;
5949}
5950
NeilBrownfd01b882011-10-11 16:47:53 +11005951static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005952{
NeilBrownd1688a62011-10-11 16:49:52 +11005953 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005954
NeilBrown88ce4932009-03-31 15:24:23 +11005955 if (mddev->delta_disks == 0 &&
5956 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005957 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005958 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005959 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005960 return -EINVAL;
5961 if (mddev->delta_disks < 0) {
5962 /* We might be able to shrink, but the devices must
5963 * be made bigger first.
5964 * For raid6, 4 is the minimum size.
5965 * Otherwise 2 is the minimum
5966 */
5967 int min = 2;
5968 if (mddev->level == 6)
5969 min = 4;
5970 if (mddev->raid_disks + mddev->delta_disks < min)
5971 return -EINVAL;
5972 }
NeilBrown29269552006-03-27 01:18:10 -08005973
NeilBrown01ee22b2009-06-18 08:47:20 +10005974 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005975 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005976
NeilBrowne56108d62012-10-11 14:24:13 +11005977 return resize_stripes(conf, (conf->previous_raid_disks
5978 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08005979}
5980
NeilBrownfd01b882011-10-11 16:47:53 +11005981static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005982{
NeilBrownd1688a62011-10-11 16:49:52 +11005983 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005984 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005985 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005986 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005987
NeilBrownf4168852007-02-28 20:11:53 -08005988 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005989 return -EBUSY;
5990
NeilBrown01ee22b2009-06-18 08:47:20 +10005991 if (!check_stripe_cache(mddev))
5992 return -ENOSPC;
5993
NeilBrown30b67642012-05-22 13:55:28 +10005994 if (has_failed(conf))
5995 return -EINVAL;
5996
NeilBrownc6563a82012-05-21 09:27:00 +10005997 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005998 if (!test_bit(In_sync, &rdev->flags)
5999 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08006000 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10006001 }
NeilBrown63c70c42006-03-27 01:18:13 -08006002
NeilBrownf4168852007-02-28 20:11:53 -08006003 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08006004 /* Not enough devices even to make a degraded array
6005 * of that size
6006 */
6007 return -EINVAL;
6008
NeilBrownec32a2b2009-03-31 15:17:38 +11006009 /* Refuse to reduce size of the array. Any reductions in
6010 * array size must be through explicit setting of array_size
6011 * attribute.
6012 */
6013 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
6014 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10006015 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11006016 "before number of disks\n", mdname(mddev));
6017 return -EINVAL;
6018 }
6019
NeilBrownf6705572006-03-27 01:18:11 -08006020 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08006021 spin_lock_irq(&conf->device_lock);
6022 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08006023 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006024 conf->prev_chunk_sectors = conf->chunk_sectors;
6025 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11006026 conf->prev_algo = conf->algorithm;
6027 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10006028 conf->generation++;
6029 /* Code that selects data_offset needs to see the generation update
6030 * if reshape_progress has been set - so a memory barrier needed.
6031 */
6032 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10006033 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11006034 conf->reshape_progress = raid5_size(mddev, 0, 0);
6035 else
6036 conf->reshape_progress = 0;
6037 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08006038 spin_unlock_irq(&conf->device_lock);
6039
6040 /* Add some new drives, as many as will fit.
6041 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10006042 * Don't add devices if we are reducing the number of
6043 * devices in the array. This is because it is not possible
6044 * to correctly record the "partially reconstructed" state of
6045 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08006046 */
NeilBrown87a8dec2011-01-31 11:57:43 +11006047 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11006048 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11006049 if (rdev->raid_disk < 0 &&
6050 !test_bit(Faulty, &rdev->flags)) {
6051 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11006052 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11006053 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11006054 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11006055 else
NeilBrown87a8dec2011-01-31 11:57:43 +11006056 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10006057
6058 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11006059 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11006060 }
NeilBrown87a8dec2011-01-31 11:57:43 +11006061 } else if (rdev->raid_disk >= conf->previous_raid_disks
6062 && !test_bit(Faulty, &rdev->flags)) {
6063 /* This is a spare that was manually added */
6064 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11006065 }
NeilBrown29269552006-03-27 01:18:10 -08006066
NeilBrown87a8dec2011-01-31 11:57:43 +11006067 /* When a reshape changes the number of devices,
6068 * ->degraded is measured against the larger of the
6069 * pre and post number of devices.
6070 */
NeilBrownec32a2b2009-03-31 15:17:38 +11006071 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006072 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11006073 spin_unlock_irqrestore(&conf->device_lock, flags);
6074 }
NeilBrown63c70c42006-03-27 01:18:13 -08006075 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10006076 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07006077 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08006078
NeilBrown29269552006-03-27 01:18:10 -08006079 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6080 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6081 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6082 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6083 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006084 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08006085 if (!mddev->sync_thread) {
6086 mddev->recovery = 0;
6087 spin_lock_irq(&conf->device_lock);
6088 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006089 rdev_for_each(rdev, mddev)
6090 rdev->new_data_offset = rdev->data_offset;
6091 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006092 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11006093 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08006094 spin_unlock_irq(&conf->device_lock);
6095 return -EAGAIN;
6096 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006097 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006098 md_wakeup_thread(mddev->sync_thread);
6099 md_new_event(mddev);
6100 return 0;
6101}
NeilBrown29269552006-03-27 01:18:10 -08006102
NeilBrownec32a2b2009-03-31 15:17:38 +11006103/* This is called from the reshape thread and should make any
6104 * changes needed in 'conf'
6105 */
NeilBrownd1688a62011-10-11 16:49:52 +11006106static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006107{
NeilBrown29269552006-03-27 01:18:10 -08006108
NeilBrownf6705572006-03-27 01:18:11 -08006109 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006110 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006111
NeilBrownf6705572006-03-27 01:18:11 -08006112 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006113 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006114 rdev_for_each(rdev, conf->mddev)
6115 rdev->data_offset = rdev->new_data_offset;
6116 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006117 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006118 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006119 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006120
6121 /* read-ahead size must cover two whole stripes, which is
6122 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6123 */
NeilBrown4a5add42010-06-01 19:37:28 +10006124 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006125 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006126 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006127 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006128 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6129 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6130 }
NeilBrown29269552006-03-27 01:18:10 -08006131 }
NeilBrown29269552006-03-27 01:18:10 -08006132}
6133
NeilBrownec32a2b2009-03-31 15:17:38 +11006134/* This is called from the raid5d thread with mddev_lock held.
6135 * It makes config changes to the device.
6136 */
NeilBrownfd01b882011-10-11 16:47:53 +11006137static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006138{
NeilBrownd1688a62011-10-11 16:49:52 +11006139 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006140
6141 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6142
NeilBrownec32a2b2009-03-31 15:17:38 +11006143 if (mddev->delta_disks > 0) {
6144 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6145 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006146 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006147 } else {
6148 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006149 spin_lock_irq(&conf->device_lock);
6150 mddev->degraded = calc_degraded(conf);
6151 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006152 for (d = conf->raid_disks ;
6153 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006154 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006155 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006156 if (rdev)
6157 clear_bit(In_sync, &rdev->flags);
6158 rdev = conf->disks[d].replacement;
6159 if (rdev)
6160 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006161 }
NeilBrowncea9c222009-03-31 15:15:05 +11006162 }
NeilBrown88ce4932009-03-31 15:24:23 +11006163 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006164 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006165 mddev->reshape_position = MaxSector;
6166 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006167 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006168 }
6169}
6170
NeilBrownfd01b882011-10-11 16:47:53 +11006171static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006172{
NeilBrownd1688a62011-10-11 16:49:52 +11006173 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006174
6175 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006176 case 2: /* resume for a suspend */
6177 wake_up(&conf->wait_for_overlap);
6178 break;
6179
NeilBrown72626682005-09-09 16:23:54 -07006180 case 1: /* stop all writes */
6181 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006182 /* '2' tells resync/reshape to pause so that all
6183 * active stripes can drain
6184 */
6185 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006186 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006187 atomic_read(&conf->active_stripes) == 0 &&
6188 atomic_read(&conf->active_aligned_reads) == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01006189 conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006190 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006191 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006192 /* allow reshape to continue */
6193 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006194 break;
6195
6196 case 0: /* re-enable writes */
6197 spin_lock_irq(&conf->device_lock);
6198 conf->quiesce = 0;
6199 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006200 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006201 spin_unlock_irq(&conf->device_lock);
6202 break;
6203 }
NeilBrown72626682005-09-09 16:23:54 -07006204}
NeilBrownb15c2e52006-01-06 00:20:16 -08006205
NeilBrownd562b0c2009-03-31 14:39:39 +11006206
NeilBrownfd01b882011-10-11 16:47:53 +11006207static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006208{
NeilBrowne373ab12011-10-11 16:48:59 +11006209 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006210 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006211
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006212 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006213 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006214 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6215 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006216 return ERR_PTR(-EINVAL);
6217 }
6218
NeilBrowne373ab12011-10-11 16:48:59 +11006219 sectors = raid0_conf->strip_zone[0].zone_end;
6220 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006221 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006222 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006223 mddev->new_layout = ALGORITHM_PARITY_N;
6224 mddev->new_chunk_sectors = mddev->chunk_sectors;
6225 mddev->raid_disks += 1;
6226 mddev->delta_disks = 1;
6227 /* make sure it will be not marked as dirty */
6228 mddev->recovery_cp = MaxSector;
6229
6230 return setup_conf(mddev);
6231}
6232
6233
NeilBrownfd01b882011-10-11 16:47:53 +11006234static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006235{
6236 int chunksect;
6237
6238 if (mddev->raid_disks != 2 ||
6239 mddev->degraded > 1)
6240 return ERR_PTR(-EINVAL);
6241
6242 /* Should check if there are write-behind devices? */
6243
6244 chunksect = 64*2; /* 64K by default */
6245
6246 /* The array must be an exact multiple of chunksize */
6247 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6248 chunksect >>= 1;
6249
6250 if ((chunksect<<9) < STRIPE_SIZE)
6251 /* array size does not allow a suitable chunk size */
6252 return ERR_PTR(-EINVAL);
6253
6254 mddev->new_level = 5;
6255 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006256 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006257
6258 return setup_conf(mddev);
6259}
6260
NeilBrownfd01b882011-10-11 16:47:53 +11006261static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006262{
6263 int new_layout;
6264
6265 switch (mddev->layout) {
6266 case ALGORITHM_LEFT_ASYMMETRIC_6:
6267 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6268 break;
6269 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6270 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6271 break;
6272 case ALGORITHM_LEFT_SYMMETRIC_6:
6273 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6274 break;
6275 case ALGORITHM_RIGHT_SYMMETRIC_6:
6276 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6277 break;
6278 case ALGORITHM_PARITY_0_6:
6279 new_layout = ALGORITHM_PARITY_0;
6280 break;
6281 case ALGORITHM_PARITY_N:
6282 new_layout = ALGORITHM_PARITY_N;
6283 break;
6284 default:
6285 return ERR_PTR(-EINVAL);
6286 }
6287 mddev->new_level = 5;
6288 mddev->new_layout = new_layout;
6289 mddev->delta_disks = -1;
6290 mddev->raid_disks -= 1;
6291 return setup_conf(mddev);
6292}
6293
NeilBrownd562b0c2009-03-31 14:39:39 +11006294
NeilBrownfd01b882011-10-11 16:47:53 +11006295static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006296{
NeilBrown88ce4932009-03-31 15:24:23 +11006297 /* For a 2-drive array, the layout and chunk size can be changed
6298 * immediately as not restriping is needed.
6299 * For larger arrays we record the new value - after validation
6300 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006301 */
NeilBrownd1688a62011-10-11 16:49:52 +11006302 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006303 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006304
NeilBrown597a7112009-06-18 08:47:42 +10006305 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006306 return -EINVAL;
6307 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006308 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006309 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006310 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006311 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006312 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006313 /* not factor of array size */
6314 return -EINVAL;
6315 }
6316
6317 /* They look valid */
6318
NeilBrown88ce4932009-03-31 15:24:23 +11006319 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006320 /* can make the change immediately */
6321 if (mddev->new_layout >= 0) {
6322 conf->algorithm = mddev->new_layout;
6323 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006324 }
6325 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006326 conf->chunk_sectors = new_chunk ;
6327 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006328 }
6329 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6330 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006331 }
NeilBrown50ac1682009-06-18 08:47:55 +10006332 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006333}
6334
NeilBrownfd01b882011-10-11 16:47:53 +11006335static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006336{
NeilBrown597a7112009-06-18 08:47:42 +10006337 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006338
NeilBrown597a7112009-06-18 08:47:42 +10006339 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006340 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006341 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006342 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006343 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006344 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006345 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006346 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006347 /* not factor of array size */
6348 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006349 }
NeilBrown88ce4932009-03-31 15:24:23 +11006350
6351 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006352 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006353}
6354
NeilBrownfd01b882011-10-11 16:47:53 +11006355static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006356{
6357 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006358 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006359 * raid1 - if there are two drives. We need to know the chunk size
6360 * raid4 - trivial - just use a raid4 layout.
6361 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006362 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006363 if (mddev->level == 0)
6364 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006365 if (mddev->level == 1)
6366 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006367 if (mddev->level == 4) {
6368 mddev->new_layout = ALGORITHM_PARITY_N;
6369 mddev->new_level = 5;
6370 return setup_conf(mddev);
6371 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006372 if (mddev->level == 6)
6373 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006374
6375 return ERR_PTR(-EINVAL);
6376}
6377
NeilBrownfd01b882011-10-11 16:47:53 +11006378static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006379{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006380 /* raid4 can take over:
6381 * raid0 - if there is only one strip zone
6382 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006383 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006384 if (mddev->level == 0)
6385 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006386 if (mddev->level == 5 &&
6387 mddev->layout == ALGORITHM_PARITY_N) {
6388 mddev->new_layout = 0;
6389 mddev->new_level = 4;
6390 return setup_conf(mddev);
6391 }
6392 return ERR_PTR(-EINVAL);
6393}
NeilBrownd562b0c2009-03-31 14:39:39 +11006394
NeilBrown84fc4b52011-10-11 16:49:58 +11006395static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006396
NeilBrownfd01b882011-10-11 16:47:53 +11006397static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006398{
6399 /* Currently can only take over a raid5. We map the
6400 * personality to an equivalent raid6 personality
6401 * with the Q block at the end.
6402 */
6403 int new_layout;
6404
6405 if (mddev->pers != &raid5_personality)
6406 return ERR_PTR(-EINVAL);
6407 if (mddev->degraded > 1)
6408 return ERR_PTR(-EINVAL);
6409 if (mddev->raid_disks > 253)
6410 return ERR_PTR(-EINVAL);
6411 if (mddev->raid_disks < 3)
6412 return ERR_PTR(-EINVAL);
6413
6414 switch (mddev->layout) {
6415 case ALGORITHM_LEFT_ASYMMETRIC:
6416 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6417 break;
6418 case ALGORITHM_RIGHT_ASYMMETRIC:
6419 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6420 break;
6421 case ALGORITHM_LEFT_SYMMETRIC:
6422 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6423 break;
6424 case ALGORITHM_RIGHT_SYMMETRIC:
6425 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6426 break;
6427 case ALGORITHM_PARITY_0:
6428 new_layout = ALGORITHM_PARITY_0_6;
6429 break;
6430 case ALGORITHM_PARITY_N:
6431 new_layout = ALGORITHM_PARITY_N;
6432 break;
6433 default:
6434 return ERR_PTR(-EINVAL);
6435 }
6436 mddev->new_level = 6;
6437 mddev->new_layout = new_layout;
6438 mddev->delta_disks = 1;
6439 mddev->raid_disks += 1;
6440 return setup_conf(mddev);
6441}
6442
6443
NeilBrown84fc4b52011-10-11 16:49:58 +11006444static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006445{
6446 .name = "raid6",
6447 .level = 6,
6448 .owner = THIS_MODULE,
6449 .make_request = make_request,
6450 .run = run,
6451 .stop = stop,
6452 .status = status,
6453 .error_handler = error,
6454 .hot_add_disk = raid5_add_disk,
6455 .hot_remove_disk= raid5_remove_disk,
6456 .spare_active = raid5_spare_active,
6457 .sync_request = sync_request,
6458 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006459 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006460 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006461 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006462 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006463 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006464 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006465};
NeilBrown84fc4b52011-10-11 16:49:58 +11006466static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467{
6468 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006469 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 .owner = THIS_MODULE,
6471 .make_request = make_request,
6472 .run = run,
6473 .stop = stop,
6474 .status = status,
6475 .error_handler = error,
6476 .hot_add_disk = raid5_add_disk,
6477 .hot_remove_disk= raid5_remove_disk,
6478 .spare_active = raid5_spare_active,
6479 .sync_request = sync_request,
6480 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006481 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006482 .check_reshape = raid5_check_reshape,
6483 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006484 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006485 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006486 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487};
6488
NeilBrown84fc4b52011-10-11 16:49:58 +11006489static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490{
NeilBrown2604b702006-01-06 00:20:36 -08006491 .name = "raid4",
6492 .level = 4,
6493 .owner = THIS_MODULE,
6494 .make_request = make_request,
6495 .run = run,
6496 .stop = stop,
6497 .status = status,
6498 .error_handler = error,
6499 .hot_add_disk = raid5_add_disk,
6500 .hot_remove_disk= raid5_remove_disk,
6501 .spare_active = raid5_spare_active,
6502 .sync_request = sync_request,
6503 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006504 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006505 .check_reshape = raid5_check_reshape,
6506 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006507 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006508 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006509 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006510};
6511
6512static int __init raid5_init(void)
6513{
NeilBrown16a53ec2006-06-26 00:27:38 -07006514 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006515 register_md_personality(&raid5_personality);
6516 register_md_personality(&raid4_personality);
6517 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518}
6519
NeilBrown2604b702006-01-06 00:20:36 -08006520static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521{
NeilBrown16a53ec2006-06-26 00:27:38 -07006522 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006523 unregister_md_personality(&raid5_personality);
6524 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525}
6526
6527module_init(raid5_init);
6528module_exit(raid5_exit);
6529MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006530MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006532MODULE_ALIAS("md-raid5");
6533MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006534MODULE_ALIAS("md-level-5");
6535MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006536MODULE_ALIAS("md-personality-8"); /* RAID6 */
6537MODULE_ALIAS("md-raid6");
6538MODULE_ALIAS("md-level-6");
6539
6540/* This used to be two separate modules, they were: */
6541MODULE_ALIAS("raid5");
6542MODULE_ALIAS("raid6");