blob: 774f81423d7808d7f4223e7574d09cc3eab1c6b6 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * Stripe cache
65 */
66
67#define NR_STRIPES 256
68#define STRIPE_SIZE PAGE_SIZE
69#define STRIPE_SHIFT (PAGE_SHIFT - 9)
70#define STRIPE_SECTORS (STRIPE_SIZE>>9)
71#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070072#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080073#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define HASH_MASK (NR_HASH - 1)
75
NeilBrownd1688a62011-10-11 16:49:52 +110076static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110077{
78 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
79 return &conf->stripe_hashtbl[hash];
80}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/* bio's attached to a stripe+device for I/O are linked together in bi_sector
83 * order without overlap. There may be several bio's per stripe+device, and
84 * a bio could span several devices.
85 * When walking this list for a particular stripe+device, we must never proceed
86 * beyond a bio that extends past this device, as the next bio might no longer
87 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110088 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * of the current stripe+device
90 */
NeilBrowndb298e12011-10-07 14:23:00 +110091static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
92{
Kent Overstreetaa8b57a2013-02-05 15:19:29 -080093 int sectors = bio_sectors(bio);
NeilBrowndb298e12011-10-07 14:23:00 +110094 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
95 return bio->bi_next;
96 else
97 return NULL;
98}
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Jens Axboe960e7392008-08-15 10:41:18 +0200100/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200101 * We maintain a biased count of active stripes in the bottom 16 bits of
102 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200103 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000104static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200105{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000106 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
107 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200108}
109
Shaohua Lie7836bd62012-07-19 16:01:31 +1000110static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200111{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000112 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
113 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200114}
115
Shaohua Lie7836bd62012-07-19 16:01:31 +1000116static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200117{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000118 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
119 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200120}
121
Shaohua Lie7836bd62012-07-19 16:01:31 +1000122static inline void raid5_set_bi_processed_stripes(struct bio *bio,
123 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200124{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000125 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
126 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200127
Shaohua Lie7836bd62012-07-19 16:01:31 +1000128 do {
129 old = atomic_read(segments);
130 new = (old & 0xffff) | (cnt << 16);
131 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200132}
133
Shaohua Lie7836bd62012-07-19 16:01:31 +1000134static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200135{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000136 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
137 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200138}
139
NeilBrownd0dabf72009-03-31 14:39:38 +1100140/* Find first data disk in a raid6 stripe */
141static inline int raid6_d0(struct stripe_head *sh)
142{
NeilBrown67cc2b82009-03-31 14:39:38 +1100143 if (sh->ddf_layout)
144 /* ddf always start from first device */
145 return 0;
146 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100147 if (sh->qd_idx == sh->disks - 1)
148 return 0;
149 else
150 return sh->qd_idx + 1;
151}
NeilBrown16a53ec2006-06-26 00:27:38 -0700152static inline int raid6_next_disk(int disk, int raid_disks)
153{
154 disk++;
155 return (disk < raid_disks) ? disk : 0;
156}
Dan Williamsa4456852007-07-09 11:56:43 -0700157
NeilBrownd0dabf72009-03-31 14:39:38 +1100158/* When walking through the disks in a raid5, starting at raid6_d0,
159 * We need to map each disk to a 'slot', where the data disks are slot
160 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
161 * is raid_disks-1. This help does that mapping.
162 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100163static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
164 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100165{
Dan Williams66295422009-10-19 18:09:32 -0700166 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100167
NeilBrowne4424fe2009-10-16 16:27:34 +1100168 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700169 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100170 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100171 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100172 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100173 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100174 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700175 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100176 return slot;
177}
178
Dan Williamsa4456852007-07-09 11:56:43 -0700179static void return_io(struct bio *return_bi)
180{
181 struct bio *bi = return_bi;
182 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700183
184 return_bi = bi->bi_next;
185 bi->bi_next = NULL;
186 bi->bi_size = 0;
Linus Torvalds0a82a8d2013-04-18 09:00:26 -0700187 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
188 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +1000189 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700190 bi = return_bi;
191 }
192}
193
NeilBrownd1688a62011-10-11 16:49:52 +1100194static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Dan Williams600aa102008-06-28 08:32:05 +1000196static int stripe_operations_active(struct stripe_head *sh)
197{
198 return sh->check_state || sh->reconstruct_state ||
199 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
200 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
201}
202
Shaohua Li4eb788d2012-07-19 16:01:31 +1000203static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000205 BUG_ON(!list_empty(&sh->lru));
206 BUG_ON(atomic_read(&conf->active_stripes)==0);
207 if (test_bit(STRIPE_HANDLE, &sh->state)) {
208 if (test_bit(STRIPE_DELAYED, &sh->state) &&
209 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
210 list_add_tail(&sh->lru, &conf->delayed_list);
211 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
212 sh->bm_seq - conf->seq_write > 0)
213 list_add_tail(&sh->lru, &conf->bitmap_list);
214 else {
215 clear_bit(STRIPE_DELAYED, &sh->state);
216 clear_bit(STRIPE_BIT_DELAY, &sh->state);
217 list_add_tail(&sh->lru, &conf->handle_list);
218 }
219 md_wakeup_thread(conf->mddev->thread);
220 } else {
221 BUG_ON(stripe_operations_active(sh));
222 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
223 if (atomic_dec_return(&conf->preread_active_stripes)
224 < IO_THRESHOLD)
225 md_wakeup_thread(conf->mddev->thread);
226 atomic_dec(&conf->active_stripes);
227 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
228 list_add_tail(&sh->lru, &conf->inactive_list);
229 wake_up(&conf->wait_for_stripe);
230 if (conf->retry_read_aligned)
231 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 }
234}
NeilBrownd0dabf72009-03-31 14:39:38 +1100235
Shaohua Li4eb788d2012-07-19 16:01:31 +1000236static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
237{
238 if (atomic_dec_and_test(&sh->count))
239 do_release_stripe(conf, sh);
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static void release_stripe(struct stripe_head *sh)
243{
NeilBrownd1688a62011-10-11 16:49:52 +1100244 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700246
Shaohua Li4eb788d2012-07-19 16:01:31 +1000247 local_irq_save(flags);
248 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
249 do_release_stripe(conf, sh);
250 spin_unlock(&conf->device_lock);
251 }
252 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
NeilBrownfccddba2006-01-06 00:20:33 -0800255static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Dan Williams45b42332007-07-09 11:56:43 -0700257 pr_debug("remove_hash(), stripe %llu\n",
258 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
NeilBrownfccddba2006-01-06 00:20:33 -0800260 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
NeilBrownd1688a62011-10-11 16:49:52 +1100263static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
NeilBrownfccddba2006-01-06 00:20:33 -0800265 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Dan Williams45b42332007-07-09 11:56:43 -0700267 pr_debug("insert_hash(), stripe %llu\n",
268 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
NeilBrownfccddba2006-01-06 00:20:33 -0800270 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273
274/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100275static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct stripe_head *sh = NULL;
278 struct list_head *first;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (list_empty(&conf->inactive_list))
281 goto out;
282 first = conf->inactive_list.next;
283 sh = list_entry(first, struct stripe_head, lru);
284 list_del_init(first);
285 remove_hash(sh);
286 atomic_inc(&conf->active_stripes);
287out:
288 return sh;
289}
290
NeilBrowne4e11e32010-06-16 16:45:16 +1000291static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct page *p;
294 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000295 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
NeilBrowne4e11e32010-06-16 16:45:16 +1000297 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 p = sh->dev[i].page;
299 if (!p)
300 continue;
301 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800302 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304}
305
NeilBrowne4e11e32010-06-16 16:45:16 +1000306static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000309 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
NeilBrowne4e11e32010-06-16 16:45:16 +1000311 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct page *page;
313
314 if (!(page = alloc_page(GFP_KERNEL))) {
315 return 1;
316 }
317 sh->dev[i].page = page;
318 }
319 return 0;
320}
321
NeilBrown784052e2009-03-31 15:19:07 +1100322static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100323static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100324 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
NeilBrownb5663ba2009-03-31 14:39:38 +1100326static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
NeilBrownd1688a62011-10-11 16:49:52 +1100328 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800329 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200331 BUG_ON(atomic_read(&sh->count) != 0);
332 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000333 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700334
Dan Williams45b42332007-07-09 11:56:43 -0700335 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 (unsigned long long)sh->sector);
337
338 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700339
NeilBrown86b42c72009-03-31 15:19:03 +1100340 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100341 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100343 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 sh->state = 0;
345
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800346
347 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct r5dev *dev = &sh->dev[i];
349
Dan Williamsd84e0f12007-01-02 13:52:30 -0700350 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700352 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700354 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000356 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100359 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361 insert_hash(conf, sh);
362}
363
NeilBrownd1688a62011-10-11 16:49:52 +1100364static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100365 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct stripe_head *sh;
368
Dan Williams45b42332007-07-09 11:56:43 -0700369 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800370 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100371 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700373 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return NULL;
375}
376
NeilBrown674806d2010-06-16 17:17:53 +1000377/*
378 * Need to check if array has failed when deciding whether to:
379 * - start an array
380 * - remove non-faulty devices
381 * - add a spare
382 * - allow a reshape
383 * This determination is simple when no reshape is happening.
384 * However if there is a reshape, we need to carefully check
385 * both the before and after sections.
386 * This is because some failed devices may only affect one
387 * of the two sections, and some non-in_sync devices may
388 * be insync in the section most affected by failed devices.
389 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100390static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000391{
NeilBrown908f4fb2011-12-23 10:17:50 +1100392 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000393 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000394
395 rcu_read_lock();
396 degraded = 0;
397 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100398 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000399 if (rdev && test_bit(Faulty, &rdev->flags))
400 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000401 if (!rdev || test_bit(Faulty, &rdev->flags))
402 degraded++;
403 else if (test_bit(In_sync, &rdev->flags))
404 ;
405 else
406 /* not in-sync or faulty.
407 * If the reshape increases the number of devices,
408 * this is being recovered by the reshape, so
409 * this 'previous' section is not in_sync.
410 * If the number of devices is being reduced however,
411 * the device can only be part of the array if
412 * we are reverting a reshape, so this section will
413 * be in-sync.
414 */
415 if (conf->raid_disks >= conf->previous_raid_disks)
416 degraded++;
417 }
418 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100419 if (conf->raid_disks == conf->previous_raid_disks)
420 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000421 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100422 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000423 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100424 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000425 if (rdev && test_bit(Faulty, &rdev->flags))
426 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000427 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100428 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000429 else if (test_bit(In_sync, &rdev->flags))
430 ;
431 else
432 /* not in-sync or faulty.
433 * If reshape increases the number of devices, this
434 * section has already been recovered, else it
435 * almost certainly hasn't.
436 */
437 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100438 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000439 }
440 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100441 if (degraded2 > degraded)
442 return degraded2;
443 return degraded;
444}
445
446static int has_failed(struct r5conf *conf)
447{
448 int degraded;
449
450 if (conf->mddev->reshape_position == MaxSector)
451 return conf->mddev->degraded > conf->max_degraded;
452
453 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000454 if (degraded > conf->max_degraded)
455 return 1;
456 return 0;
457}
458
NeilBrownb5663ba2009-03-31 14:39:38 +1100459static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100460get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000461 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 struct stripe_head *sh;
464
Dan Williams45b42332007-07-09 11:56:43 -0700465 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 spin_lock_irq(&conf->device_lock);
468
469 do {
NeilBrown72626682005-09-09 16:23:54 -0700470 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000471 conf->quiesce == 0 || noquiesce,
Lukas Czernereed8c022012-11-30 11:42:40 +0100472 conf->device_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100473 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (!sh) {
475 if (!conf->inactive_blocked)
476 sh = get_free_stripe(conf);
477 if (noblock && sh == NULL)
478 break;
479 if (!sh) {
480 conf->inactive_blocked = 1;
481 wait_event_lock_irq(conf->wait_for_stripe,
482 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800483 (atomic_read(&conf->active_stripes)
484 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 || !conf->inactive_blocked),
Lukas Czernereed8c022012-11-30 11:42:40 +0100486 conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 conf->inactive_blocked = 0;
488 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100489 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 } else {
491 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100492 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000493 && !test_bit(STRIPE_EXPANDING, &sh->state)
494 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 } else {
496 if (!test_bit(STRIPE_HANDLE, &sh->state))
497 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700498 if (list_empty(&sh->lru) &&
499 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700500 BUG();
501 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 }
504 } while (sh == NULL);
505
506 if (sh)
507 atomic_inc(&sh->count);
508
509 spin_unlock_irq(&conf->device_lock);
510 return sh;
511}
512
NeilBrown05616be2012-05-21 09:27:00 +1000513/* Determine if 'data_offset' or 'new_data_offset' should be used
514 * in this stripe_head.
515 */
516static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
517{
518 sector_t progress = conf->reshape_progress;
519 /* Need a memory barrier to make sure we see the value
520 * of conf->generation, or ->data_offset that was set before
521 * reshape_progress was updated.
522 */
523 smp_rmb();
524 if (progress == MaxSector)
525 return 0;
526 if (sh->generation == conf->generation - 1)
527 return 0;
528 /* We are in a reshape, and this is a new-generation stripe,
529 * so use new_data_offset.
530 */
531 return 1;
532}
533
NeilBrown6712ecf2007-09-27 12:47:43 +0200534static void
535raid5_end_read_request(struct bio *bi, int error);
536static void
537raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700538
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000539static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700540{
NeilBrownd1688a62011-10-11 16:49:52 +1100541 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700542 int i, disks = sh->disks;
543
544 might_sleep();
545
546 for (i = disks; i--; ) {
547 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100548 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100549 struct bio *bi, *rbi;
550 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200551 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
552 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
553 rw = WRITE_FUA;
554 else
555 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100556 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100557 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200558 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700559 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100560 else if (test_and_clear_bit(R5_WantReplace,
561 &sh->dev[i].flags)) {
562 rw = WRITE;
563 replace_only = 1;
564 } else
Dan Williams91c00922007-01-02 13:52:30 -0700565 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000566 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
567 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700568
569 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100570 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700571
Dan Williams91c00922007-01-02 13:52:30 -0700572 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100573 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100574 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
575 rdev = rcu_dereference(conf->disks[i].rdev);
576 if (!rdev) {
577 rdev = rrdev;
578 rrdev = NULL;
579 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100580 if (rw & WRITE) {
581 if (replace_only)
582 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100583 if (rdev == rrdev)
584 /* We raced and saw duplicates */
585 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100586 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100587 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100588 rdev = rrdev;
589 rrdev = NULL;
590 }
NeilBrown977df362011-12-23 10:17:53 +1100591
Dan Williams91c00922007-01-02 13:52:30 -0700592 if (rdev && test_bit(Faulty, &rdev->flags))
593 rdev = NULL;
594 if (rdev)
595 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100596 if (rrdev && test_bit(Faulty, &rrdev->flags))
597 rrdev = NULL;
598 if (rrdev)
599 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700600 rcu_read_unlock();
601
NeilBrown73e92e52011-07-28 11:39:22 +1000602 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100603 * need to check for writes. We never accept write errors
604 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000605 */
606 while ((rw & WRITE) && rdev &&
607 test_bit(WriteErrorSeen, &rdev->flags)) {
608 sector_t first_bad;
609 int bad_sectors;
610 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
611 &first_bad, &bad_sectors);
612 if (!bad)
613 break;
614
615 if (bad < 0) {
616 set_bit(BlockedBadBlocks, &rdev->flags);
617 if (!conf->mddev->external &&
618 conf->mddev->flags) {
619 /* It is very unlikely, but we might
620 * still need to write out the
621 * bad block log - better give it
622 * a chance*/
623 md_check_recovery(conf->mddev);
624 }
majianpeng18507532012-07-03 12:11:54 +1000625 /*
626 * Because md_wait_for_blocked_rdev
627 * will dec nr_pending, we must
628 * increment it first.
629 */
630 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000631 md_wait_for_blocked_rdev(rdev, conf->mddev);
632 } else {
633 /* Acknowledged bad block - skip the write */
634 rdev_dec_pending(rdev, conf->mddev);
635 rdev = NULL;
636 }
637 }
638
Dan Williams91c00922007-01-02 13:52:30 -0700639 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100640 if (s->syncing || s->expanding || s->expanded
641 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700642 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
643
Dan Williams2b7497f2008-06-28 08:31:52 +1000644 set_bit(STRIPE_IO_STARTED, &sh->state);
645
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700646 bio_reset(bi);
Dan Williams91c00922007-01-02 13:52:30 -0700647 bi->bi_bdev = rdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700648 bi->bi_rw = rw;
649 bi->bi_end_io = (rw & WRITE)
650 ? raid5_end_write_request
651 : raid5_end_read_request;
652 bi->bi_private = sh;
653
Dan Williams91c00922007-01-02 13:52:30 -0700654 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700655 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700656 bi->bi_rw, i);
657 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000658 if (use_new_offset(conf, sh))
659 bi->bi_sector = (sh->sector
660 + rdev->new_data_offset);
661 else
662 bi->bi_sector = (sh->sector
663 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000664 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
665 bi->bi_rw |= REQ_FLUSH;
666
Kent Overstreet4997b722013-05-30 08:44:39 +0200667 bi->bi_vcnt = 1;
Dan Williams91c00922007-01-02 13:52:30 -0700668 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
669 bi->bi_io_vec[0].bv_offset = 0;
670 bi->bi_size = STRIPE_SIZE;
Shaohua Li7e44a922013-10-19 14:50:28 +0800671 /*
672 * If this is discard request, set bi_vcnt 0. We don't
673 * want to confuse SCSI because SCSI will replace payload
674 */
675 if (rw & REQ_DISCARD)
676 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +1100677 if (rrdev)
678 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600679
680 if (conf->mddev->gendisk)
681 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
682 bi, disk_devt(conf->mddev->gendisk),
683 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700684 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100685 }
686 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100687 if (s->syncing || s->expanding || s->expanded
688 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100689 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
690
691 set_bit(STRIPE_IO_STARTED, &sh->state);
692
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700693 bio_reset(rbi);
NeilBrown977df362011-12-23 10:17:53 +1100694 rbi->bi_bdev = rrdev->bdev;
Kent Overstreet2f6db2a2012-09-11 12:26:38 -0700695 rbi->bi_rw = rw;
696 BUG_ON(!(rw & WRITE));
697 rbi->bi_end_io = raid5_end_write_request;
698 rbi->bi_private = sh;
699
NeilBrown977df362011-12-23 10:17:53 +1100700 pr_debug("%s: for %llu schedule op %ld on "
701 "replacement disc %d\n",
702 __func__, (unsigned long long)sh->sector,
703 rbi->bi_rw, i);
704 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000705 if (use_new_offset(conf, sh))
706 rbi->bi_sector = (sh->sector
707 + rrdev->new_data_offset);
708 else
709 rbi->bi_sector = (sh->sector
710 + rrdev->data_offset);
Kent Overstreet4997b722013-05-30 08:44:39 +0200711 rbi->bi_vcnt = 1;
NeilBrown977df362011-12-23 10:17:53 +1100712 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
713 rbi->bi_io_vec[0].bv_offset = 0;
714 rbi->bi_size = STRIPE_SIZE;
Shaohua Li7e44a922013-10-19 14:50:28 +0800715 /*
716 * If this is discard request, set bi_vcnt 0. We don't
717 * want to confuse SCSI because SCSI will replace payload
718 */
719 if (rw & REQ_DISCARD)
720 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600721 if (conf->mddev->gendisk)
722 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
723 rbi, disk_devt(conf->mddev->gendisk),
724 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100725 generic_make_request(rbi);
726 }
727 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000728 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700729 set_bit(STRIPE_DEGRADED, &sh->state);
730 pr_debug("skip op %ld on disc %d for sector %llu\n",
731 bi->bi_rw, i, (unsigned long long)sh->sector);
732 clear_bit(R5_LOCKED, &sh->dev[i].flags);
733 set_bit(STRIPE_HANDLE, &sh->state);
734 }
735 }
736}
737
738static struct dma_async_tx_descriptor *
739async_copy_data(int frombio, struct bio *bio, struct page *page,
740 sector_t sector, struct dma_async_tx_descriptor *tx)
741{
742 struct bio_vec *bvl;
743 struct page *bio_page;
744 int i;
745 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700746 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700747 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700748
749 if (bio->bi_sector >= sector)
750 page_offset = (signed)(bio->bi_sector - sector) * 512;
751 else
752 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700753
Dan Williams0403e382009-09-08 17:42:50 -0700754 if (frombio)
755 flags |= ASYNC_TX_FENCE;
756 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
757
Dan Williams91c00922007-01-02 13:52:30 -0700758 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000759 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700760 int clen;
761 int b_offset = 0;
762
763 if (page_offset < 0) {
764 b_offset = -page_offset;
765 page_offset += b_offset;
766 len -= b_offset;
767 }
768
769 if (len > 0 && page_offset + len > STRIPE_SIZE)
770 clen = STRIPE_SIZE - page_offset;
771 else
772 clen = len;
773
774 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000775 b_offset += bvl->bv_offset;
776 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700777 if (frombio)
778 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700779 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700780 else
781 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700782 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700783 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700784 /* chain the operations */
785 submit.depend_tx = tx;
786
Dan Williams91c00922007-01-02 13:52:30 -0700787 if (clen < len) /* hit end of page */
788 break;
789 page_offset += len;
790 }
791
792 return tx;
793}
794
795static void ops_complete_biofill(void *stripe_head_ref)
796{
797 struct stripe_head *sh = stripe_head_ref;
798 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700799 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700800
Harvey Harrisone46b2722008-04-28 02:15:50 -0700801 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700802 (unsigned long long)sh->sector);
803
804 /* clear completed biofills */
805 for (i = sh->disks; i--; ) {
806 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700807
808 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700809 /* and check if we need to reply to a read request,
810 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000811 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700812 */
813 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700814 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700815
Dan Williams91c00922007-01-02 13:52:30 -0700816 BUG_ON(!dev->read);
817 rbi = dev->read;
818 dev->read = NULL;
819 while (rbi && rbi->bi_sector <
820 dev->sector + STRIPE_SECTORS) {
821 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000822 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700823 rbi->bi_next = return_bi;
824 return_bi = rbi;
825 }
Dan Williams91c00922007-01-02 13:52:30 -0700826 rbi = rbi2;
827 }
828 }
829 }
Dan Williams83de75c2008-06-28 08:31:58 +1000830 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700831
832 return_io(return_bi);
833
Dan Williamse4d84902007-09-24 10:06:13 -0700834 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700835 release_stripe(sh);
836}
837
838static void ops_run_biofill(struct stripe_head *sh)
839{
840 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700841 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700842 int i;
843
Harvey Harrisone46b2722008-04-28 02:15:50 -0700844 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700845 (unsigned long long)sh->sector);
846
847 for (i = sh->disks; i--; ) {
848 struct r5dev *dev = &sh->dev[i];
849 if (test_bit(R5_Wantfill, &dev->flags)) {
850 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000851 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700852 dev->read = rbi = dev->toread;
853 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000854 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700855 while (rbi && rbi->bi_sector <
856 dev->sector + STRIPE_SECTORS) {
857 tx = async_copy_data(0, rbi, dev->page,
858 dev->sector, tx);
859 rbi = r5_next_bio(rbi, dev->sector);
860 }
861 }
862 }
863
864 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700865 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
866 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700867}
868
Dan Williams4e7d2c02009-08-29 19:13:11 -0700869static void mark_target_uptodate(struct stripe_head *sh, int target)
870{
871 struct r5dev *tgt;
872
873 if (target < 0)
874 return;
875
876 tgt = &sh->dev[target];
877 set_bit(R5_UPTODATE, &tgt->flags);
878 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
879 clear_bit(R5_Wantcompute, &tgt->flags);
880}
881
Dan Williamsac6b53b2009-07-14 13:40:19 -0700882static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700883{
884 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700885
Harvey Harrisone46b2722008-04-28 02:15:50 -0700886 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700887 (unsigned long long)sh->sector);
888
Dan Williamsac6b53b2009-07-14 13:40:19 -0700889 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700890 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700891 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700892
Dan Williamsecc65c92008-06-28 08:31:57 +1000893 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
894 if (sh->check_state == check_state_compute_run)
895 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700896 set_bit(STRIPE_HANDLE, &sh->state);
897 release_stripe(sh);
898}
899
Dan Williamsd6f38f32009-07-14 11:50:52 -0700900/* return a pointer to the address conversion region of the scribble buffer */
901static addr_conv_t *to_addr_conv(struct stripe_head *sh,
902 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700903{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700904 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
905}
906
907static struct dma_async_tx_descriptor *
908ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
909{
Dan Williams91c00922007-01-02 13:52:30 -0700910 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700911 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700912 int target = sh->ops.target;
913 struct r5dev *tgt = &sh->dev[target];
914 struct page *xor_dest = tgt->page;
915 int count = 0;
916 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700917 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700918 int i;
919
920 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700921 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700922 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
923
924 for (i = disks; i--; )
925 if (i != target)
926 xor_srcs[count++] = sh->dev[i].page;
927
928 atomic_inc(&sh->count);
929
Dan Williams0403e382009-09-08 17:42:50 -0700930 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700931 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700932 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700933 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700934 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700935 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700936
Dan Williams91c00922007-01-02 13:52:30 -0700937 return tx;
938}
939
Dan Williamsac6b53b2009-07-14 13:40:19 -0700940/* set_syndrome_sources - populate source buffers for gen_syndrome
941 * @srcs - (struct page *) array of size sh->disks
942 * @sh - stripe_head to parse
943 *
944 * Populates srcs in proper layout order for the stripe and returns the
945 * 'count' of sources to be used in a call to async_gen_syndrome. The P
946 * destination buffer is recorded in srcs[count] and the Q destination
947 * is recorded in srcs[count+1]].
948 */
949static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
950{
951 int disks = sh->disks;
952 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
953 int d0_idx = raid6_d0(sh);
954 int count;
955 int i;
956
957 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100958 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700959
960 count = 0;
961 i = d0_idx;
962 do {
963 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
964
965 srcs[slot] = sh->dev[i].page;
966 i = raid6_next_disk(i, disks);
967 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700968
NeilBrowne4424fe2009-10-16 16:27:34 +1100969 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700970}
971
972static struct dma_async_tx_descriptor *
973ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
974{
975 int disks = sh->disks;
976 struct page **blocks = percpu->scribble;
977 int target;
978 int qd_idx = sh->qd_idx;
979 struct dma_async_tx_descriptor *tx;
980 struct async_submit_ctl submit;
981 struct r5dev *tgt;
982 struct page *dest;
983 int i;
984 int count;
985
986 if (sh->ops.target < 0)
987 target = sh->ops.target2;
988 else if (sh->ops.target2 < 0)
989 target = sh->ops.target;
990 else
991 /* we should only have one valid target */
992 BUG();
993 BUG_ON(target < 0);
994 pr_debug("%s: stripe %llu block: %d\n",
995 __func__, (unsigned long long)sh->sector, target);
996
997 tgt = &sh->dev[target];
998 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
999 dest = tgt->page;
1000
1001 atomic_inc(&sh->count);
1002
1003 if (target == qd_idx) {
1004 count = set_syndrome_sources(blocks, sh);
1005 blocks[count] = NULL; /* regenerating p is not necessary */
1006 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001007 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1008 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001009 to_addr_conv(sh, percpu));
1010 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1011 } else {
1012 /* Compute any data- or p-drive using XOR */
1013 count = 0;
1014 for (i = disks; i-- ; ) {
1015 if (i == target || i == qd_idx)
1016 continue;
1017 blocks[count++] = sh->dev[i].page;
1018 }
1019
Dan Williams0403e382009-09-08 17:42:50 -07001020 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1021 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001022 to_addr_conv(sh, percpu));
1023 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1024 }
1025
1026 return tx;
1027}
1028
1029static struct dma_async_tx_descriptor *
1030ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1031{
1032 int i, count, disks = sh->disks;
1033 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1034 int d0_idx = raid6_d0(sh);
1035 int faila = -1, failb = -1;
1036 int target = sh->ops.target;
1037 int target2 = sh->ops.target2;
1038 struct r5dev *tgt = &sh->dev[target];
1039 struct r5dev *tgt2 = &sh->dev[target2];
1040 struct dma_async_tx_descriptor *tx;
1041 struct page **blocks = percpu->scribble;
1042 struct async_submit_ctl submit;
1043
1044 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1045 __func__, (unsigned long long)sh->sector, target, target2);
1046 BUG_ON(target < 0 || target2 < 0);
1047 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1048 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1049
Dan Williams6c910a72009-09-16 12:24:54 -07001050 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001051 * slot number conversion for 'faila' and 'failb'
1052 */
1053 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001054 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001055 count = 0;
1056 i = d0_idx;
1057 do {
1058 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1059
1060 blocks[slot] = sh->dev[i].page;
1061
1062 if (i == target)
1063 faila = slot;
1064 if (i == target2)
1065 failb = slot;
1066 i = raid6_next_disk(i, disks);
1067 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001068
1069 BUG_ON(faila == failb);
1070 if (failb < faila)
1071 swap(faila, failb);
1072 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1073 __func__, (unsigned long long)sh->sector, faila, failb);
1074
1075 atomic_inc(&sh->count);
1076
1077 if (failb == syndrome_disks+1) {
1078 /* Q disk is one of the missing disks */
1079 if (faila == syndrome_disks) {
1080 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001081 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1082 ops_complete_compute, sh,
1083 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001084 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001085 STRIPE_SIZE, &submit);
1086 } else {
1087 struct page *dest;
1088 int data_target;
1089 int qd_idx = sh->qd_idx;
1090
1091 /* Missing D+Q: recompute D from P, then recompute Q */
1092 if (target == qd_idx)
1093 data_target = target2;
1094 else
1095 data_target = target;
1096
1097 count = 0;
1098 for (i = disks; i-- ; ) {
1099 if (i == data_target || i == qd_idx)
1100 continue;
1101 blocks[count++] = sh->dev[i].page;
1102 }
1103 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001104 init_async_submit(&submit,
1105 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1106 NULL, NULL, NULL,
1107 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001108 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1109 &submit);
1110
1111 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001112 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1113 ops_complete_compute, sh,
1114 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001115 return async_gen_syndrome(blocks, 0, count+2,
1116 STRIPE_SIZE, &submit);
1117 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001118 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001119 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1120 ops_complete_compute, sh,
1121 to_addr_conv(sh, percpu));
1122 if (failb == syndrome_disks) {
1123 /* We're missing D+P. */
1124 return async_raid6_datap_recov(syndrome_disks+2,
1125 STRIPE_SIZE, faila,
1126 blocks, &submit);
1127 } else {
1128 /* We're missing D+D. */
1129 return async_raid6_2data_recov(syndrome_disks+2,
1130 STRIPE_SIZE, faila, failb,
1131 blocks, &submit);
1132 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001133 }
1134}
1135
1136
Dan Williams91c00922007-01-02 13:52:30 -07001137static void ops_complete_prexor(void *stripe_head_ref)
1138{
1139 struct stripe_head *sh = stripe_head_ref;
1140
Harvey Harrisone46b2722008-04-28 02:15:50 -07001141 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001142 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001143}
1144
1145static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001146ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1147 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001148{
Dan Williams91c00922007-01-02 13:52:30 -07001149 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001150 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001151 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001152 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001153
1154 /* existing parity data subtracted */
1155 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1156
Harvey Harrisone46b2722008-04-28 02:15:50 -07001157 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001158 (unsigned long long)sh->sector);
1159
1160 for (i = disks; i--; ) {
1161 struct r5dev *dev = &sh->dev[i];
1162 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001163 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001164 xor_srcs[count++] = dev->page;
1165 }
1166
Dan Williams0403e382009-09-08 17:42:50 -07001167 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001168 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001169 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001170
1171 return tx;
1172}
1173
1174static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001175ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001176{
1177 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001178 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001179
Harvey Harrisone46b2722008-04-28 02:15:50 -07001180 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001181 (unsigned long long)sh->sector);
1182
1183 for (i = disks; i--; ) {
1184 struct r5dev *dev = &sh->dev[i];
1185 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001186
Dan Williamsd8ee0722008-06-28 08:32:06 +10001187 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001188 struct bio *wbi;
1189
Shaohua Lib17459c2012-07-19 16:01:31 +10001190 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001191 chosen = dev->towrite;
1192 dev->towrite = NULL;
1193 BUG_ON(dev->written);
1194 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001195 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001196
1197 while (wbi && wbi->bi_sector <
1198 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001199 if (wbi->bi_rw & REQ_FUA)
1200 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001201 if (wbi->bi_rw & REQ_SYNC)
1202 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001203 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001204 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001205 else
Shaohua Li620125f2012-10-11 13:49:05 +11001206 tx = async_copy_data(1, wbi, dev->page,
1207 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001208 wbi = r5_next_bio(wbi, dev->sector);
1209 }
1210 }
1211 }
1212
1213 return tx;
1214}
1215
Dan Williamsac6b53b2009-07-14 13:40:19 -07001216static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001217{
1218 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001219 int disks = sh->disks;
1220 int pd_idx = sh->pd_idx;
1221 int qd_idx = sh->qd_idx;
1222 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001223 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001224
Harvey Harrisone46b2722008-04-28 02:15:50 -07001225 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001226 (unsigned long long)sh->sector);
1227
Shaohua Libc0934f2012-05-22 13:55:05 +10001228 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001229 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001230 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001231 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001232 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001233
Dan Williams91c00922007-01-02 13:52:30 -07001234 for (i = disks; i--; ) {
1235 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001236
Tejun Heoe9c74692010-09-03 11:56:18 +02001237 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001238 if (!discard)
1239 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001240 if (fua)
1241 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001242 if (sync)
1243 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001244 }
Dan Williams91c00922007-01-02 13:52:30 -07001245 }
1246
Dan Williamsd8ee0722008-06-28 08:32:06 +10001247 if (sh->reconstruct_state == reconstruct_state_drain_run)
1248 sh->reconstruct_state = reconstruct_state_drain_result;
1249 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1250 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1251 else {
1252 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1253 sh->reconstruct_state = reconstruct_state_result;
1254 }
Dan Williams91c00922007-01-02 13:52:30 -07001255
1256 set_bit(STRIPE_HANDLE, &sh->state);
1257 release_stripe(sh);
1258}
1259
1260static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001261ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1262 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001263{
Dan Williams91c00922007-01-02 13:52:30 -07001264 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001265 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001266 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001267 int count = 0, pd_idx = sh->pd_idx, i;
1268 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001269 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001270 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001271
Harvey Harrisone46b2722008-04-28 02:15:50 -07001272 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001273 (unsigned long long)sh->sector);
1274
Shaohua Li620125f2012-10-11 13:49:05 +11001275 for (i = 0; i < sh->disks; i++) {
1276 if (pd_idx == i)
1277 continue;
1278 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1279 break;
1280 }
1281 if (i >= sh->disks) {
1282 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001283 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1284 ops_complete_reconstruct(sh);
1285 return;
1286 }
Dan Williams91c00922007-01-02 13:52:30 -07001287 /* check if prexor is active which means only process blocks
1288 * that are part of a read-modify-write (written)
1289 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001290 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1291 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001292 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1293 for (i = disks; i--; ) {
1294 struct r5dev *dev = &sh->dev[i];
1295 if (dev->written)
1296 xor_srcs[count++] = dev->page;
1297 }
1298 } else {
1299 xor_dest = sh->dev[pd_idx].page;
1300 for (i = disks; i--; ) {
1301 struct r5dev *dev = &sh->dev[i];
1302 if (i != pd_idx)
1303 xor_srcs[count++] = dev->page;
1304 }
1305 }
1306
Dan Williams91c00922007-01-02 13:52:30 -07001307 /* 1/ if we prexor'd then the dest is reused as a source
1308 * 2/ if we did not prexor then we are redoing the parity
1309 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1310 * for the synchronous xor case
1311 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001312 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001313 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1314
1315 atomic_inc(&sh->count);
1316
Dan Williamsac6b53b2009-07-14 13:40:19 -07001317 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001318 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001319 if (unlikely(count == 1))
1320 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1321 else
1322 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001323}
1324
Dan Williamsac6b53b2009-07-14 13:40:19 -07001325static void
1326ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1327 struct dma_async_tx_descriptor *tx)
1328{
1329 struct async_submit_ctl submit;
1330 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001331 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001332
1333 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1334
Shaohua Li620125f2012-10-11 13:49:05 +11001335 for (i = 0; i < sh->disks; i++) {
1336 if (sh->pd_idx == i || sh->qd_idx == i)
1337 continue;
1338 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1339 break;
1340 }
1341 if (i >= sh->disks) {
1342 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001343 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1344 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1345 ops_complete_reconstruct(sh);
1346 return;
1347 }
1348
Dan Williamsac6b53b2009-07-14 13:40:19 -07001349 count = set_syndrome_sources(blocks, sh);
1350
1351 atomic_inc(&sh->count);
1352
1353 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1354 sh, to_addr_conv(sh, percpu));
1355 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001356}
1357
1358static void ops_complete_check(void *stripe_head_ref)
1359{
1360 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001361
Harvey Harrisone46b2722008-04-28 02:15:50 -07001362 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001363 (unsigned long long)sh->sector);
1364
Dan Williamsecc65c92008-06-28 08:31:57 +10001365 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001366 set_bit(STRIPE_HANDLE, &sh->state);
1367 release_stripe(sh);
1368}
1369
Dan Williamsac6b53b2009-07-14 13:40:19 -07001370static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001371{
Dan Williams91c00922007-01-02 13:52:30 -07001372 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001373 int pd_idx = sh->pd_idx;
1374 int qd_idx = sh->qd_idx;
1375 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001376 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001377 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001378 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001379 int count;
1380 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001381
Harvey Harrisone46b2722008-04-28 02:15:50 -07001382 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001383 (unsigned long long)sh->sector);
1384
Dan Williamsac6b53b2009-07-14 13:40:19 -07001385 count = 0;
1386 xor_dest = sh->dev[pd_idx].page;
1387 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001388 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001389 if (i == pd_idx || i == qd_idx)
1390 continue;
1391 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001392 }
1393
Dan Williamsd6f38f32009-07-14 11:50:52 -07001394 init_async_submit(&submit, 0, NULL, NULL, NULL,
1395 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001396 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001397 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001398
Dan Williams91c00922007-01-02 13:52:30 -07001399 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001400 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1401 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001402}
1403
Dan Williamsac6b53b2009-07-14 13:40:19 -07001404static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1405{
1406 struct page **srcs = percpu->scribble;
1407 struct async_submit_ctl submit;
1408 int count;
1409
1410 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1411 (unsigned long long)sh->sector, checkp);
1412
1413 count = set_syndrome_sources(srcs, sh);
1414 if (!checkp)
1415 srcs[count] = NULL;
1416
1417 atomic_inc(&sh->count);
1418 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1419 sh, to_addr_conv(sh, percpu));
1420 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1421 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1422}
1423
NeilBrown51acbce2013-02-28 09:08:34 +11001424static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001425{
1426 int overlap_clear = 0, i, disks = sh->disks;
1427 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001428 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001429 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001430 struct raid5_percpu *percpu;
1431 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001432
Dan Williamsd6f38f32009-07-14 11:50:52 -07001433 cpu = get_cpu();
1434 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001435 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001436 ops_run_biofill(sh);
1437 overlap_clear++;
1438 }
1439
Dan Williams7b3a8712008-06-28 08:32:09 +10001440 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001441 if (level < 6)
1442 tx = ops_run_compute5(sh, percpu);
1443 else {
1444 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1445 tx = ops_run_compute6_1(sh, percpu);
1446 else
1447 tx = ops_run_compute6_2(sh, percpu);
1448 }
1449 /* terminate the chain if reconstruct is not set to be run */
1450 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001451 async_tx_ack(tx);
1452 }
Dan Williams91c00922007-01-02 13:52:30 -07001453
Dan Williams600aa102008-06-28 08:32:05 +10001454 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001455 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001456
Dan Williams600aa102008-06-28 08:32:05 +10001457 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001458 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001459 overlap_clear++;
1460 }
1461
Dan Williamsac6b53b2009-07-14 13:40:19 -07001462 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1463 if (level < 6)
1464 ops_run_reconstruct5(sh, percpu, tx);
1465 else
1466 ops_run_reconstruct6(sh, percpu, tx);
1467 }
Dan Williams91c00922007-01-02 13:52:30 -07001468
Dan Williamsac6b53b2009-07-14 13:40:19 -07001469 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1470 if (sh->check_state == check_state_run)
1471 ops_run_check_p(sh, percpu);
1472 else if (sh->check_state == check_state_run_q)
1473 ops_run_check_pq(sh, percpu, 0);
1474 else if (sh->check_state == check_state_run_pq)
1475 ops_run_check_pq(sh, percpu, 1);
1476 else
1477 BUG();
1478 }
Dan Williams91c00922007-01-02 13:52:30 -07001479
Dan Williams91c00922007-01-02 13:52:30 -07001480 if (overlap_clear)
1481 for (i = disks; i--; ) {
1482 struct r5dev *dev = &sh->dev[i];
1483 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1484 wake_up(&sh->raid_conf->wait_for_overlap);
1485 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001486 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001487}
1488
NeilBrownd1688a62011-10-11 16:49:52 +11001489static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001492 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001493 if (!sh)
1494 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001495
NeilBrown3f294f42005-11-08 21:39:25 -08001496 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08001497
Shaohua Lib17459c2012-07-19 16:01:31 +10001498 spin_lock_init(&sh->stripe_lock);
1499
NeilBrowne4e11e32010-06-16 16:45:16 +10001500 if (grow_buffers(sh)) {
1501 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001502 kmem_cache_free(conf->slab_cache, sh);
1503 return 0;
1504 }
1505 /* we just created an active stripe so... */
1506 atomic_set(&sh->count, 1);
1507 atomic_inc(&conf->active_stripes);
1508 INIT_LIST_HEAD(&sh->lru);
1509 release_stripe(sh);
1510 return 1;
1511}
1512
NeilBrownd1688a62011-10-11 16:49:52 +11001513static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001514{
Christoph Lametere18b8902006-12-06 20:33:20 -08001515 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001516 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
NeilBrownf4be6b42010-06-01 19:37:25 +10001518 if (conf->mddev->gendisk)
1519 sprintf(conf->cache_name[0],
1520 "raid%d-%s", conf->level, mdname(conf->mddev));
1521 else
1522 sprintf(conf->cache_name[0],
1523 "raid%d-%p", conf->level, conf->mddev);
1524 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1525
NeilBrownad01c9e2006-03-27 01:18:07 -08001526 conf->active_name = 0;
1527 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001529 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (!sc)
1531 return 1;
1532 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001533 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001534 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001535 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return 0;
1538}
NeilBrown29269552006-03-27 01:18:10 -08001539
Dan Williamsd6f38f32009-07-14 11:50:52 -07001540/**
1541 * scribble_len - return the required size of the scribble region
1542 * @num - total number of disks in the array
1543 *
1544 * The size must be enough to contain:
1545 * 1/ a struct page pointer for each device in the array +2
1546 * 2/ room to convert each entry in (1) to its corresponding dma
1547 * (dma_map_page()) or page (page_address()) address.
1548 *
1549 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1550 * calculate over all devices (not just the data blocks), using zeros in place
1551 * of the P and Q blocks.
1552 */
1553static size_t scribble_len(int num)
1554{
1555 size_t len;
1556
1557 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1558
1559 return len;
1560}
1561
NeilBrownd1688a62011-10-11 16:49:52 +11001562static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001563{
1564 /* Make all the stripes able to hold 'newsize' devices.
1565 * New slots in each stripe get 'page' set to a new page.
1566 *
1567 * This happens in stages:
1568 * 1/ create a new kmem_cache and allocate the required number of
1569 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09001570 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08001571 * to the new stripe_heads. This will have the side effect of
1572 * freezing the array as once all stripe_heads have been collected,
1573 * no IO will be possible. Old stripe heads are freed once their
1574 * pages have been transferred over, and the old kmem_cache is
1575 * freed when all stripes are done.
1576 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1577 * we simple return a failre status - no need to clean anything up.
1578 * 4/ allocate new pages for the new slots in the new stripe_heads.
1579 * If this fails, we don't bother trying the shrink the
1580 * stripe_heads down again, we just leave them as they are.
1581 * As each stripe_head is processed the new one is released into
1582 * active service.
1583 *
1584 * Once step2 is started, we cannot afford to wait for a write,
1585 * so we use GFP_NOIO allocations.
1586 */
1587 struct stripe_head *osh, *nsh;
1588 LIST_HEAD(newstripes);
1589 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001590 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001591 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001592 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001593 int i;
1594
1595 if (newsize <= conf->pool_size)
1596 return 0; /* never bother to shrink */
1597
Dan Williamsb5470dc2008-06-27 21:44:04 -07001598 err = md_allow_write(conf->mddev);
1599 if (err)
1600 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001601
NeilBrownad01c9e2006-03-27 01:18:07 -08001602 /* Step 1 */
1603 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1604 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001605 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001606 if (!sc)
1607 return -ENOMEM;
1608
1609 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001610 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001611 if (!nsh)
1612 break;
1613
NeilBrownad01c9e2006-03-27 01:18:07 -08001614 nsh->raid_conf = conf;
NeilBrowncb13ff62012-09-24 16:27:20 +10001615 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001616
1617 list_add(&nsh->lru, &newstripes);
1618 }
1619 if (i) {
1620 /* didn't get enough, give up */
1621 while (!list_empty(&newstripes)) {
1622 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1623 list_del(&nsh->lru);
1624 kmem_cache_free(sc, nsh);
1625 }
1626 kmem_cache_destroy(sc);
1627 return -ENOMEM;
1628 }
1629 /* Step 2 - Must use GFP_NOIO now.
1630 * OK, we have enough stripes, start collecting inactive
1631 * stripes and copying them over
1632 */
1633 list_for_each_entry(nsh, &newstripes, lru) {
1634 spin_lock_irq(&conf->device_lock);
1635 wait_event_lock_irq(conf->wait_for_stripe,
1636 !list_empty(&conf->inactive_list),
Lukas Czernereed8c022012-11-30 11:42:40 +01001637 conf->device_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001638 osh = get_free_stripe(conf);
1639 spin_unlock_irq(&conf->device_lock);
1640 atomic_set(&nsh->count, 1);
1641 for(i=0; i<conf->pool_size; i++)
1642 nsh->dev[i].page = osh->dev[i].page;
1643 for( ; i<newsize; i++)
1644 nsh->dev[i].page = NULL;
1645 kmem_cache_free(conf->slab_cache, osh);
1646 }
1647 kmem_cache_destroy(conf->slab_cache);
1648
1649 /* Step 3.
1650 * At this point, we are holding all the stripes so the array
1651 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001652 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001653 */
1654 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1655 if (ndisks) {
1656 for (i=0; i<conf->raid_disks; i++)
1657 ndisks[i] = conf->disks[i];
1658 kfree(conf->disks);
1659 conf->disks = ndisks;
1660 } else
1661 err = -ENOMEM;
1662
Dan Williamsd6f38f32009-07-14 11:50:52 -07001663 get_online_cpus();
1664 conf->scribble_len = scribble_len(newsize);
1665 for_each_present_cpu(cpu) {
1666 struct raid5_percpu *percpu;
1667 void *scribble;
1668
1669 percpu = per_cpu_ptr(conf->percpu, cpu);
1670 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1671
1672 if (scribble) {
1673 kfree(percpu->scribble);
1674 percpu->scribble = scribble;
1675 } else {
1676 err = -ENOMEM;
1677 break;
1678 }
1679 }
1680 put_online_cpus();
1681
NeilBrownad01c9e2006-03-27 01:18:07 -08001682 /* Step 4, return new stripes to service */
1683 while(!list_empty(&newstripes)) {
1684 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1685 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001686
NeilBrownad01c9e2006-03-27 01:18:07 -08001687 for (i=conf->raid_disks; i < newsize; i++)
1688 if (nsh->dev[i].page == NULL) {
1689 struct page *p = alloc_page(GFP_NOIO);
1690 nsh->dev[i].page = p;
1691 if (!p)
1692 err = -ENOMEM;
1693 }
1694 release_stripe(nsh);
1695 }
1696 /* critical section pass, GFP_NOIO no longer needed */
1697
1698 conf->slab_cache = sc;
1699 conf->active_name = 1-conf->active_name;
1700 conf->pool_size = newsize;
1701 return err;
1702}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
NeilBrownd1688a62011-10-11 16:49:52 +11001704static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
1706 struct stripe_head *sh;
1707
NeilBrown3f294f42005-11-08 21:39:25 -08001708 spin_lock_irq(&conf->device_lock);
1709 sh = get_free_stripe(conf);
1710 spin_unlock_irq(&conf->device_lock);
1711 if (!sh)
1712 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001713 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001714 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001715 kmem_cache_free(conf->slab_cache, sh);
1716 atomic_dec(&conf->active_stripes);
1717 return 1;
1718}
1719
NeilBrownd1688a62011-10-11 16:49:52 +11001720static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001721{
1722 while (drop_one_stripe(conf))
1723 ;
1724
NeilBrown29fc7e32006-02-03 03:03:41 -08001725 if (conf->slab_cache)
1726 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 conf->slab_cache = NULL;
1728}
1729
NeilBrown6712ecf2007-09-27 12:47:43 +02001730static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
NeilBrown99c0fb52009-03-31 14:39:38 +11001732 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001733 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001734 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001736 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001737 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001738 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 for (i=0 ; i<disks; i++)
1741 if (bi == &sh->dev[i].req)
1742 break;
1743
Dan Williams45b42332007-07-09 11:56:43 -07001744 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1745 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 uptodate);
1747 if (i == disks) {
1748 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001749 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 }
NeilBrown14a75d32011-12-23 10:17:52 +11001751 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001752 /* If replacement finished while this request was outstanding,
1753 * 'replacement' might be NULL already.
1754 * In that case it moved down to 'rdev'.
1755 * rdev is not removed until all requests are finished.
1756 */
NeilBrown14a75d32011-12-23 10:17:52 +11001757 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001758 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001759 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
NeilBrown05616be2012-05-21 09:27:00 +10001761 if (use_new_offset(conf, sh))
1762 s = sh->sector + rdev->new_data_offset;
1763 else
1764 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001767 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001768 /* Note that this cannot happen on a
1769 * replacement device. We just fail those on
1770 * any error
1771 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001772 printk_ratelimited(
1773 KERN_INFO
1774 "md/raid:%s: read error corrected"
1775 " (%lu sectors at %llu on %s)\n",
1776 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001777 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001778 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001779 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001780 clear_bit(R5_ReadError, &sh->dev[i].flags);
1781 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001782 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1783 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1784
NeilBrown14a75d32011-12-23 10:17:52 +11001785 if (atomic_read(&rdev->read_errors))
1786 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001788 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001789 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001790 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001793 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001794 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1795 printk_ratelimited(
1796 KERN_WARNING
1797 "md/raid:%s: read error on replacement device "
1798 "(sector %llu on %s).\n",
1799 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001800 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001801 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001802 else if (conf->mddev->degraded >= conf->max_degraded) {
1803 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001804 printk_ratelimited(
1805 KERN_WARNING
1806 "md/raid:%s: read error not correctable "
1807 "(sector %llu on %s).\n",
1808 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001809 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001810 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001811 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001812 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001813 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001814 printk_ratelimited(
1815 KERN_WARNING
1816 "md/raid:%s: read error NOT corrected!! "
1817 "(sector %llu on %s).\n",
1818 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001819 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001820 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001821 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001822 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001823 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001824 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001825 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001826 else
1827 retry = 1;
1828 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001829 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1830 set_bit(R5_ReadError, &sh->dev[i].flags);
1831 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1832 } else
1833 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001834 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001835 clear_bit(R5_ReadError, &sh->dev[i].flags);
1836 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001837 if (!(set_bad
1838 && test_bit(In_sync, &rdev->flags)
1839 && rdev_set_badblocks(
1840 rdev, sh->sector, STRIPE_SECTORS, 0)))
1841 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
NeilBrown14a75d32011-12-23 10:17:52 +11001844 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1846 set_bit(STRIPE_HANDLE, &sh->state);
1847 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848}
1849
NeilBrownd710e132008-10-13 11:55:12 +11001850static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
NeilBrown99c0fb52009-03-31 14:39:38 +11001852 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001853 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001854 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001855 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001857 sector_t first_bad;
1858 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001859 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
NeilBrown977df362011-12-23 10:17:53 +11001861 for (i = 0 ; i < disks; i++) {
1862 if (bi == &sh->dev[i].req) {
1863 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 break;
NeilBrown977df362011-12-23 10:17:53 +11001865 }
1866 if (bi == &sh->dev[i].rreq) {
1867 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001868 if (rdev)
1869 replacement = 1;
1870 else
1871 /* rdev was removed and 'replacement'
1872 * replaced it. rdev is not removed
1873 * until all requests are finished.
1874 */
1875 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001876 break;
1877 }
1878 }
Dan Williams45b42332007-07-09 11:56:43 -07001879 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1881 uptodate);
1882 if (i == disks) {
1883 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001884 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
1886
NeilBrown977df362011-12-23 10:17:53 +11001887 if (replacement) {
1888 if (!uptodate)
1889 md_error(conf->mddev, rdev);
1890 else if (is_badblock(rdev, sh->sector,
1891 STRIPE_SECTORS,
1892 &first_bad, &bad_sectors))
1893 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1894 } else {
1895 if (!uptodate) {
NeilBrown6ba854e2014-01-16 09:35:38 +11001896 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11001897 set_bit(WriteErrorSeen, &rdev->flags);
1898 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001899 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1900 set_bit(MD_RECOVERY_NEEDED,
1901 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001902 } else if (is_badblock(rdev, sh->sector,
1903 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10001904 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11001905 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10001906 if (test_bit(R5_ReadError, &sh->dev[i].flags))
1907 /* That was a successful write so make
1908 * sure it looks like we already did
1909 * a re-write.
1910 */
1911 set_bit(R5_ReWrite, &sh->dev[i].flags);
1912 }
NeilBrown977df362011-12-23 10:17:53 +11001913 }
1914 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
NeilBrown977df362011-12-23 10:17:53 +11001916 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1917 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001919 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921
NeilBrown784052e2009-03-31 15:19:07 +11001922static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
NeilBrown784052e2009-03-31 15:19:07 +11001924static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 struct r5dev *dev = &sh->dev[i];
1927
1928 bio_init(&dev->req);
1929 dev->req.bi_io_vec = &dev->vec;
1930 dev->req.bi_vcnt++;
1931 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001933 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
NeilBrown977df362011-12-23 10:17:53 +11001935 bio_init(&dev->rreq);
1936 dev->rreq.bi_io_vec = &dev->rvec;
1937 dev->rreq.bi_vcnt++;
1938 dev->rreq.bi_max_vecs++;
1939 dev->rreq.bi_private = sh;
1940 dev->rvec.bv_page = dev->page;
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001943 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
NeilBrownfd01b882011-10-11 16:47:53 +11001946static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001949 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001950 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001951 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
NeilBrown908f4fb2011-12-23 10:17:50 +11001953 spin_lock_irqsave(&conf->device_lock, flags);
1954 clear_bit(In_sync, &rdev->flags);
1955 mddev->degraded = calc_degraded(conf);
1956 spin_unlock_irqrestore(&conf->device_lock, flags);
1957 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1958
NeilBrownde393cd2011-07-28 11:31:48 +10001959 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001960 set_bit(Faulty, &rdev->flags);
1961 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1962 printk(KERN_ALERT
1963 "md/raid:%s: Disk failure on %s, disabling device.\n"
1964 "md/raid:%s: Operation continuing on %d devices.\n",
1965 mdname(mddev),
1966 bdevname(rdev->bdev, b),
1967 mdname(mddev),
1968 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001969}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971/*
1972 * Input: a 'big' sector number,
1973 * Output: index of the data and parity disk, and the sector # in them.
1974 */
NeilBrownd1688a62011-10-11 16:49:52 +11001975static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001976 int previous, int *dd_idx,
1977 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001979 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001980 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001982 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001983 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001985 int algorithm = previous ? conf->prev_algo
1986 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001987 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1988 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001989 int raid_disks = previous ? conf->previous_raid_disks
1990 : conf->raid_disks;
1991 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 /* First compute the information on this sector */
1994
1995 /*
1996 * Compute the chunk number and the sector offset inside the chunk
1997 */
1998 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1999 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 /*
2002 * Compute the stripe number
2003 */
NeilBrown35f2a592010-04-20 14:13:34 +10002004 stripe = chunk_number;
2005 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002006 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 /*
2008 * Select the parity disk based on the user selected algorithm.
2009 */
NeilBrown84789552011-07-27 11:00:36 +10002010 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002011 switch(conf->level) {
2012 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002013 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002014 break;
2015 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002016 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002018 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002019 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 (*dd_idx)++;
2021 break;
2022 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002023 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002024 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 (*dd_idx)++;
2026 break;
2027 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002028 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002029 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 break;
2031 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002032 pd_idx = 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;
NeilBrown99c0fb52009-03-31 14:39:38 +11002035 case ALGORITHM_PARITY_0:
2036 pd_idx = 0;
2037 (*dd_idx)++;
2038 break;
2039 case ALGORITHM_PARITY_N:
2040 pd_idx = data_disks;
2041 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002043 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002044 }
2045 break;
2046 case 6:
2047
NeilBrowne183eae2009-03-31 15:20:22 +11002048 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002049 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002050 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002051 qd_idx = pd_idx + 1;
2052 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002053 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002054 qd_idx = 0;
2055 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002056 (*dd_idx) += 2; /* D D P Q D */
2057 break;
2058 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002059 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002060 qd_idx = pd_idx + 1;
2061 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002062 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002063 qd_idx = 0;
2064 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002065 (*dd_idx) += 2; /* D D P Q D */
2066 break;
2067 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002068 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002069 qd_idx = (pd_idx + 1) % raid_disks;
2070 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002071 break;
2072 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002073 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002074 qd_idx = (pd_idx + 1) % raid_disks;
2075 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002076 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002077
2078 case ALGORITHM_PARITY_0:
2079 pd_idx = 0;
2080 qd_idx = 1;
2081 (*dd_idx) += 2;
2082 break;
2083 case ALGORITHM_PARITY_N:
2084 pd_idx = data_disks;
2085 qd_idx = data_disks + 1;
2086 break;
2087
2088 case ALGORITHM_ROTATING_ZERO_RESTART:
2089 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2090 * of blocks for computing Q is different.
2091 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002092 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002093 qd_idx = pd_idx + 1;
2094 if (pd_idx == raid_disks-1) {
2095 (*dd_idx)++; /* Q D D D P */
2096 qd_idx = 0;
2097 } else if (*dd_idx >= pd_idx)
2098 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002099 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002100 break;
2101
2102 case ALGORITHM_ROTATING_N_RESTART:
2103 /* Same a left_asymmetric, by first stripe is
2104 * D D D P Q rather than
2105 * Q D D D P
2106 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002107 stripe2 += 1;
2108 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002109 qd_idx = pd_idx + 1;
2110 if (pd_idx == raid_disks-1) {
2111 (*dd_idx)++; /* Q D D D P */
2112 qd_idx = 0;
2113 } else if (*dd_idx >= pd_idx)
2114 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002115 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002116 break;
2117
2118 case ALGORITHM_ROTATING_N_CONTINUE:
2119 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002120 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002121 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2122 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002123 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002124 break;
2125
2126 case ALGORITHM_LEFT_ASYMMETRIC_6:
2127 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002128 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002129 if (*dd_idx >= pd_idx)
2130 (*dd_idx)++;
2131 qd_idx = raid_disks - 1;
2132 break;
2133
2134 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002135 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002136 if (*dd_idx >= pd_idx)
2137 (*dd_idx)++;
2138 qd_idx = raid_disks - 1;
2139 break;
2140
2141 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002142 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002143 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2144 qd_idx = raid_disks - 1;
2145 break;
2146
2147 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002148 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002149 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2150 qd_idx = raid_disks - 1;
2151 break;
2152
2153 case ALGORITHM_PARITY_0_6:
2154 pd_idx = 0;
2155 (*dd_idx)++;
2156 qd_idx = raid_disks - 1;
2157 break;
2158
NeilBrown16a53ec2006-06-26 00:27:38 -07002159 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002160 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002161 }
2162 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
2164
NeilBrown911d4ee2009-03-31 14:39:38 +11002165 if (sh) {
2166 sh->pd_idx = pd_idx;
2167 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002168 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /*
2171 * Finally, compute the new sector number
2172 */
2173 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2174 return new_sector;
2175}
2176
2177
NeilBrown784052e2009-03-31 15:19:07 +11002178static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
NeilBrownd1688a62011-10-11 16:49:52 +11002180 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002181 int raid_disks = sh->disks;
2182 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002184 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2185 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002186 int algorithm = previous ? conf->prev_algo
2187 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 sector_t stripe;
2189 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002190 sector_t chunk_number;
2191 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002193 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
NeilBrown16a53ec2006-06-26 00:27:38 -07002195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2197 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
NeilBrown16a53ec2006-06-26 00:27:38 -07002199 if (i == sh->pd_idx)
2200 return 0;
2201 switch(conf->level) {
2202 case 4: break;
2203 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002204 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 case ALGORITHM_LEFT_ASYMMETRIC:
2206 case ALGORITHM_RIGHT_ASYMMETRIC:
2207 if (i > sh->pd_idx)
2208 i--;
2209 break;
2210 case ALGORITHM_LEFT_SYMMETRIC:
2211 case ALGORITHM_RIGHT_SYMMETRIC:
2212 if (i < sh->pd_idx)
2213 i += raid_disks;
2214 i -= (sh->pd_idx + 1);
2215 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002216 case ALGORITHM_PARITY_0:
2217 i -= 1;
2218 break;
2219 case ALGORITHM_PARITY_N:
2220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002222 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002223 }
2224 break;
2225 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002226 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002227 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002228 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002229 case ALGORITHM_LEFT_ASYMMETRIC:
2230 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002231 case ALGORITHM_ROTATING_ZERO_RESTART:
2232 case ALGORITHM_ROTATING_N_RESTART:
2233 if (sh->pd_idx == raid_disks-1)
2234 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002235 else if (i > sh->pd_idx)
2236 i -= 2; /* D D P Q D */
2237 break;
2238 case ALGORITHM_LEFT_SYMMETRIC:
2239 case ALGORITHM_RIGHT_SYMMETRIC:
2240 if (sh->pd_idx == raid_disks-1)
2241 i--; /* Q D D D P */
2242 else {
2243 /* D D P Q D */
2244 if (i < sh->pd_idx)
2245 i += raid_disks;
2246 i -= (sh->pd_idx + 2);
2247 }
2248 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002249 case ALGORITHM_PARITY_0:
2250 i -= 2;
2251 break;
2252 case ALGORITHM_PARITY_N:
2253 break;
2254 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002255 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002256 if (sh->pd_idx == 0)
2257 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002258 else {
2259 /* D D Q P D */
2260 if (i < sh->pd_idx)
2261 i += raid_disks;
2262 i -= (sh->pd_idx + 1);
2263 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002264 break;
2265 case ALGORITHM_LEFT_ASYMMETRIC_6:
2266 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2267 if (i > sh->pd_idx)
2268 i--;
2269 break;
2270 case ALGORITHM_LEFT_SYMMETRIC_6:
2271 case ALGORITHM_RIGHT_SYMMETRIC_6:
2272 if (i < sh->pd_idx)
2273 i += data_disks + 1;
2274 i -= (sh->pd_idx + 1);
2275 break;
2276 case ALGORITHM_PARITY_0_6:
2277 i -= 1;
2278 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002279 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002280 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002281 }
2282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
2284
2285 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002286 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
NeilBrown112bf892009-03-31 14:39:38 +11002288 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002289 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002290 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2291 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002292 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2293 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 return 0;
2295 }
2296 return r_sector;
2297}
2298
2299
Dan Williams600aa102008-06-28 08:32:05 +10002300static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002301schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002302 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002303{
2304 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002305 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002306 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002307
Dan Williamse33129d2007-01-02 13:52:30 -07002308 if (rcw) {
Dan Williamse33129d2007-01-02 13:52:30 -07002309
2310 for (i = disks; i--; ) {
2311 struct r5dev *dev = &sh->dev[i];
2312
2313 if (dev->towrite) {
2314 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002315 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002316 if (!expand)
2317 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002318 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002319 }
2320 }
NeilBrownce7d3632013-03-04 12:37:14 +11002321 /* if we are not expanding this is a proper write request, and
2322 * there will be bios with new data to be drained into the
2323 * stripe cache
2324 */
2325 if (!expand) {
2326 if (!s->locked)
2327 /* False alarm, nothing to do */
2328 return;
2329 sh->reconstruct_state = reconstruct_state_drain_run;
2330 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2331 } else
2332 sh->reconstruct_state = reconstruct_state_run;
2333
2334 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2335
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002336 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002337 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002338 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002339 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002340 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002341 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2342 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2343
Dan Williamse33129d2007-01-02 13:52:30 -07002344 for (i = disks; i--; ) {
2345 struct r5dev *dev = &sh->dev[i];
2346 if (i == pd_idx)
2347 continue;
2348
Dan Williamse33129d2007-01-02 13:52:30 -07002349 if (dev->towrite &&
2350 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002351 test_bit(R5_Wantcompute, &dev->flags))) {
2352 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002353 set_bit(R5_LOCKED, &dev->flags);
2354 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002355 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002356 }
2357 }
NeilBrownce7d3632013-03-04 12:37:14 +11002358 if (!s->locked)
2359 /* False alarm - nothing to do */
2360 return;
2361 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2362 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2363 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2364 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002365 }
2366
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002367 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002368 * are in flight
2369 */
2370 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2371 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002372 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002373
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002374 if (level == 6) {
2375 int qd_idx = sh->qd_idx;
2376 struct r5dev *dev = &sh->dev[qd_idx];
2377
2378 set_bit(R5_LOCKED, &dev->flags);
2379 clear_bit(R5_UPTODATE, &dev->flags);
2380 s->locked++;
2381 }
2382
Dan Williams600aa102008-06-28 08:32:05 +10002383 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002384 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002385 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002386}
NeilBrown16a53ec2006-06-26 00:27:38 -07002387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388/*
2389 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002390 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 * The bi_next chain must be in order.
2392 */
2393static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2394{
2395 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002396 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002397 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
NeilBrowncbe47ec2011-07-26 11:20:35 +10002399 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 (unsigned long long)bi->bi_sector,
2401 (unsigned long long)sh->sector);
2402
Shaohua Lib17459c2012-07-19 16:01:31 +10002403 /*
2404 * If several bio share a stripe. The bio bi_phys_segments acts as a
2405 * reference count to avoid race. The reference count should already be
2406 * increased before this function is called (for example, in
2407 * make_request()), so other bio sharing this stripe will not free the
2408 * stripe. If a stripe is owned by one stripe, the stripe lock will
2409 * protect it.
2410 */
2411 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002412 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002414 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002415 firstwrite = 1;
2416 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 bip = &sh->dev[dd_idx].toread;
2418 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002419 if (bio_end_sector(*bip) > bi->bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 goto overlap;
2421 bip = & (*bip)->bi_next;
2422 }
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002423 if (*bip && (*bip)->bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 goto overlap;
2425
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002426 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (*bip)
2428 bi->bi_next = *bip;
2429 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002430 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (forwrite) {
2433 /* check if page is covered */
2434 sector_t sector = sh->dev[dd_idx].sector;
2435 for (bi=sh->dev[dd_idx].towrite;
2436 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2437 bi && bi->bi_sector <= sector;
2438 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002439 if (bio_end_sector(bi) >= sector)
2440 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
2442 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2443 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2444 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002445
2446 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2447 (unsigned long long)(*bip)->bi_sector,
2448 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002449 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002450
2451 if (conf->mddev->bitmap && firstwrite) {
2452 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2453 STRIPE_SECTORS, 0);
2454 sh->bm_seq = conf->seq_flush+1;
2455 set_bit(STRIPE_BIT_DELAY, &sh->state);
2456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 return 1;
2458
2459 overlap:
2460 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002461 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 return 0;
2463}
2464
NeilBrownd1688a62011-10-11 16:49:52 +11002465static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002466
NeilBrownd1688a62011-10-11 16:49:52 +11002467static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002468 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002469{
NeilBrown784052e2009-03-31 15:19:07 +11002470 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002471 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002472 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002473 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002474 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002475
NeilBrown112bf892009-03-31 14:39:38 +11002476 raid5_compute_sector(conf,
2477 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002478 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002479 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002480 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002481}
2482
Dan Williamsa4456852007-07-09 11:56:43 -07002483static void
NeilBrownd1688a62011-10-11 16:49:52 +11002484handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002485 struct stripe_head_state *s, int disks,
2486 struct bio **return_bi)
2487{
2488 int i;
2489 for (i = disks; i--; ) {
2490 struct bio *bi;
2491 int bitmap_end = 0;
2492
2493 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002494 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002495 rcu_read_lock();
2496 rdev = rcu_dereference(conf->disks[i].rdev);
2497 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002498 atomic_inc(&rdev->nr_pending);
2499 else
2500 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002501 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002502 if (rdev) {
2503 if (!rdev_set_badblocks(
2504 rdev,
2505 sh->sector,
2506 STRIPE_SECTORS, 0))
2507 md_error(conf->mddev, rdev);
2508 rdev_dec_pending(rdev, conf->mddev);
2509 }
Dan Williamsa4456852007-07-09 11:56:43 -07002510 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002511 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002512 /* fail all writes first */
2513 bi = sh->dev[i].towrite;
2514 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002515 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002516 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002517 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002518
2519 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2520 wake_up(&conf->wait_for_overlap);
2521
2522 while (bi && bi->bi_sector <
2523 sh->dev[i].sector + STRIPE_SECTORS) {
2524 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2525 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002526 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002527 md_write_end(conf->mddev);
2528 bi->bi_next = *return_bi;
2529 *return_bi = bi;
2530 }
2531 bi = nextbi;
2532 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002533 if (bitmap_end)
2534 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2535 STRIPE_SECTORS, 0, 0);
2536 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002537 /* and fail all 'written' */
2538 bi = sh->dev[i].written;
2539 sh->dev[i].written = NULL;
2540 if (bi) bitmap_end = 1;
2541 while (bi && bi->bi_sector <
2542 sh->dev[i].sector + STRIPE_SECTORS) {
2543 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2544 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002545 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002546 md_write_end(conf->mddev);
2547 bi->bi_next = *return_bi;
2548 *return_bi = bi;
2549 }
2550 bi = bi2;
2551 }
2552
Dan Williamsb5e98d62007-01-02 13:52:31 -07002553 /* fail any reads if this device is non-operational and
2554 * the data has not reached the cache yet.
2555 */
2556 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2557 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2558 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002559 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002560 bi = sh->dev[i].toread;
2561 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002562 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002563 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2564 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002565 while (bi && bi->bi_sector <
2566 sh->dev[i].sector + STRIPE_SECTORS) {
2567 struct bio *nextbi =
2568 r5_next_bio(bi, sh->dev[i].sector);
2569 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002570 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002571 bi->bi_next = *return_bi;
2572 *return_bi = bi;
2573 }
2574 bi = nextbi;
2575 }
2576 }
Dan Williamsa4456852007-07-09 11:56:43 -07002577 if (bitmap_end)
2578 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2579 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002580 /* If we were in the middle of a write the parity block might
2581 * still be locked - so just clear all R5_LOCKED flags
2582 */
2583 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002584 }
2585
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002586 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2587 if (atomic_dec_and_test(&conf->pending_full_writes))
2588 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002589}
2590
NeilBrown7f0da592011-07-28 11:39:22 +10002591static void
NeilBrownd1688a62011-10-11 16:49:52 +11002592handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002593 struct stripe_head_state *s)
2594{
2595 int abort = 0;
2596 int i;
2597
NeilBrown7f0da592011-07-28 11:39:22 +10002598 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002599 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
2600 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10002601 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002602 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002603 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002604 * Don't even need to abort as that is handled elsewhere
2605 * if needed, and not always wanted e.g. if there is a known
2606 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002607 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002608 * non-sync devices, or abort the recovery
2609 */
NeilBrown18b98372012-04-01 23:48:38 +10002610 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2611 /* During recovery devices cannot be removed, so
2612 * locking and refcounting of rdevs is not needed
2613 */
2614 for (i = 0; i < conf->raid_disks; i++) {
2615 struct md_rdev *rdev = conf->disks[i].rdev;
2616 if (rdev
2617 && !test_bit(Faulty, &rdev->flags)
2618 && !test_bit(In_sync, &rdev->flags)
2619 && !rdev_set_badblocks(rdev, sh->sector,
2620 STRIPE_SECTORS, 0))
2621 abort = 1;
2622 rdev = conf->disks[i].replacement;
2623 if (rdev
2624 && !test_bit(Faulty, &rdev->flags)
2625 && !test_bit(In_sync, &rdev->flags)
2626 && !rdev_set_badblocks(rdev, sh->sector,
2627 STRIPE_SECTORS, 0))
2628 abort = 1;
2629 }
2630 if (abort)
2631 conf->recovery_disabled =
2632 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002633 }
NeilBrown18b98372012-04-01 23:48:38 +10002634 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002635}
2636
NeilBrown9a3e1102011-12-23 10:17:53 +11002637static int want_replace(struct stripe_head *sh, int disk_idx)
2638{
2639 struct md_rdev *rdev;
2640 int rv = 0;
2641 /* Doing recovery so rcu locking not required */
2642 rdev = sh->raid_conf->disks[disk_idx].replacement;
2643 if (rdev
2644 && !test_bit(Faulty, &rdev->flags)
2645 && !test_bit(In_sync, &rdev->flags)
2646 && (rdev->recovery_offset <= sh->sector
2647 || rdev->mddev->recovery_cp <= sh->sector))
2648 rv = 1;
2649
2650 return rv;
2651}
2652
NeilBrown93b3dbc2011-07-27 11:00:36 +10002653/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002654 * to be read or computed to satisfy a request.
2655 *
2656 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002657 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002658 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002659static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2660 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002661{
2662 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002663 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2664 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002665
Dan Williamsf38e1212007-01-02 13:52:30 -07002666 /* is the data in this block needed, and can we get it? */
2667 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002668 !test_bit(R5_UPTODATE, &dev->flags) &&
2669 (dev->toread ||
2670 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2671 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002672 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002673 (s->failed >= 1 && fdev[0]->toread) ||
2674 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002675 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2676 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2677 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002678 /* we would like to get this block, possibly by computing it,
2679 * otherwise read it if the backing disk is insync
2680 */
2681 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2682 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2683 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002684 (s->failed && (disk_idx == s->failed_num[0] ||
2685 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002686 /* have disk failed, and we're requested to fetch it;
2687 * do compute it
2688 */
2689 pr_debug("Computing stripe %llu block %d\n",
2690 (unsigned long long)sh->sector, disk_idx);
2691 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2692 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2693 set_bit(R5_Wantcompute, &dev->flags);
2694 sh->ops.target = disk_idx;
2695 sh->ops.target2 = -1; /* no 2nd target */
2696 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002697 /* Careful: from this point on 'uptodate' is in the eye
2698 * of raid_run_ops which services 'compute' operations
2699 * before writes. R5_Wantcompute flags a block that will
2700 * be R5_UPTODATE by the time it is needed for a
2701 * subsequent operation.
2702 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002703 s->uptodate++;
2704 return 1;
2705 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2706 /* Computing 2-failure is *very* expensive; only
2707 * do it if failed >= 2
2708 */
2709 int other;
2710 for (other = disks; other--; ) {
2711 if (other == disk_idx)
2712 continue;
2713 if (!test_bit(R5_UPTODATE,
2714 &sh->dev[other].flags))
2715 break;
2716 }
2717 BUG_ON(other < 0);
2718 pr_debug("Computing stripe %llu blocks %d,%d\n",
2719 (unsigned long long)sh->sector,
2720 disk_idx, other);
2721 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2722 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2723 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2724 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2725 sh->ops.target = disk_idx;
2726 sh->ops.target2 = other;
2727 s->uptodate += 2;
2728 s->req_compute = 1;
2729 return 1;
2730 } else if (test_bit(R5_Insync, &dev->flags)) {
2731 set_bit(R5_LOCKED, &dev->flags);
2732 set_bit(R5_Wantread, &dev->flags);
2733 s->locked++;
2734 pr_debug("Reading block %d (sync=%d)\n",
2735 disk_idx, s->syncing);
2736 }
2737 }
2738
2739 return 0;
2740}
2741
2742/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002743 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002744 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002745static void handle_stripe_fill(struct stripe_head *sh,
2746 struct stripe_head_state *s,
2747 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002748{
2749 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002750
2751 /* look for blocks to read/compute, skip this if a compute
2752 * is already in flight, or if the stripe contents are in the
2753 * midst of changing due to a write
2754 */
2755 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2756 !sh->reconstruct_state)
2757 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002758 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002759 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002760 set_bit(STRIPE_HANDLE, &sh->state);
2761}
2762
2763
Dan Williams1fe797e2008-06-28 09:16:30 +10002764/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002765 * any written block on an uptodate or failed drive can be returned.
2766 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2767 * never LOCKED, so we don't need to test 'failed' directly.
2768 */
NeilBrownd1688a62011-10-11 16:49:52 +11002769static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002770 struct stripe_head *sh, int disks, struct bio **return_bi)
2771{
2772 int i;
2773 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11002774 int discard_pending = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002775
2776 for (i = disks; i--; )
2777 if (sh->dev[i].written) {
2778 dev = &sh->dev[i];
2779 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11002780 (test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownca64cae2012-11-21 16:33:40 +11002781 test_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002782 /* We can return any write requests */
2783 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002784 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11002785 if (test_and_clear_bit(R5_Discard, &dev->flags))
2786 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002787 wbi = dev->written;
2788 dev->written = NULL;
2789 while (wbi && wbi->bi_sector <
2790 dev->sector + STRIPE_SECTORS) {
2791 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002792 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002793 md_write_end(conf->mddev);
2794 wbi->bi_next = *return_bi;
2795 *return_bi = wbi;
2796 }
2797 wbi = wbi2;
2798 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002799 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2800 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002801 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002802 0);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002803 } else if (test_bit(R5_Discard, &dev->flags))
2804 discard_pending = 1;
2805 }
2806 if (!discard_pending &&
2807 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
2808 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
2809 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
2810 if (sh->qd_idx >= 0) {
2811 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
2812 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
2813 }
2814 /* now that discard is done we can proceed with any sync */
2815 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li01e608d2013-10-19 14:51:42 +08002816 /*
2817 * SCSI discard will change some bio fields and the stripe has
2818 * no updated data, so remove it from hash list and the stripe
2819 * will be reinitialized
2820 */
2821 spin_lock_irq(&conf->device_lock);
2822 remove_hash(sh);
2823 spin_unlock_irq(&conf->device_lock);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002824 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
2825 set_bit(STRIPE_HANDLE, &sh->state);
2826
2827 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002828
2829 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2830 if (atomic_dec_and_test(&conf->pending_full_writes))
2831 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002832}
2833
NeilBrownd1688a62011-10-11 16:49:52 +11002834static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002835 struct stripe_head *sh,
2836 struct stripe_head_state *s,
2837 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002838{
2839 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002840 sector_t recovery_cp = conf->mddev->recovery_cp;
2841
2842 /* RAID6 requires 'rcw' in current implementation.
2843 * Otherwise, check whether resync is now happening or should start.
2844 * If yes, then the array is dirty (after unclean shutdown or
2845 * initial creation), so parity in some stripes might be inconsistent.
2846 * In this case, we need to always do reconstruct-write, to ensure
2847 * that in case of drive failure or read-error correction, we
2848 * generate correct data from the parity.
2849 */
2850 if (conf->max_degraded == 2 ||
2851 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2852 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002853 * look like rcw is cheaper
2854 */
2855 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002856 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2857 conf->max_degraded, (unsigned long long)recovery_cp,
2858 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002859 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002860 /* would I have to read this buffer for read_modify_write */
2861 struct r5dev *dev = &sh->dev[i];
2862 if ((dev->towrite || i == sh->pd_idx) &&
2863 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002864 !(test_bit(R5_UPTODATE, &dev->flags) ||
2865 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002866 if (test_bit(R5_Insync, &dev->flags))
2867 rmw++;
2868 else
2869 rmw += 2*disks; /* cannot read it */
2870 }
2871 /* Would I have to read this buffer for reconstruct_write */
2872 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2873 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002874 !(test_bit(R5_UPTODATE, &dev->flags) ||
2875 test_bit(R5_Wantcompute, &dev->flags))) {
2876 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002877 else
2878 rcw += 2*disks;
2879 }
2880 }
Dan Williams45b42332007-07-09 11:56:43 -07002881 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002882 (unsigned long long)sh->sector, rmw, rcw);
2883 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11002884 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002885 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06002886 if (conf->mddev->queue)
2887 blk_add_trace_msg(conf->mddev->queue,
2888 "raid5 rmw %llu %d",
2889 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07002890 for (i = disks; i--; ) {
2891 struct r5dev *dev = &sh->dev[i];
2892 if ((dev->towrite || i == sh->pd_idx) &&
2893 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002894 !(test_bit(R5_UPTODATE, &dev->flags) ||
2895 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002896 test_bit(R5_Insync, &dev->flags)) {
2897 if (
2898 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002899 pr_debug("Read_old block "
NeilBrowna9add5d2012-10-31 11:59:09 +11002900 "%d for r-m-w\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002901 set_bit(R5_LOCKED, &dev->flags);
2902 set_bit(R5_Wantread, &dev->flags);
2903 s->locked++;
2904 } else {
2905 set_bit(STRIPE_DELAYED, &sh->state);
2906 set_bit(STRIPE_HANDLE, &sh->state);
2907 }
2908 }
2909 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002910 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002911 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002912 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002913 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10002914 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002915 for (i = disks; i--; ) {
2916 struct r5dev *dev = &sh->dev[i];
2917 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002918 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002919 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002920 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002921 test_bit(R5_Wantcompute, &dev->flags))) {
2922 rcw++;
2923 if (!test_bit(R5_Insync, &dev->flags))
2924 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002925 if (
2926 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002927 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002928 "%d for Reconstruct\n", i);
2929 set_bit(R5_LOCKED, &dev->flags);
2930 set_bit(R5_Wantread, &dev->flags);
2931 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11002932 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07002933 } else {
2934 set_bit(STRIPE_DELAYED, &sh->state);
2935 set_bit(STRIPE_HANDLE, &sh->state);
2936 }
2937 }
2938 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06002939 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11002940 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
2941 (unsigned long long)sh->sector,
2942 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10002943 }
Dan Williamsa4456852007-07-09 11:56:43 -07002944 /* now if nothing is locked, and if we have enough data,
2945 * we can start a write request
2946 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002947 /* since handle_stripe can be called at any time we need to handle the
2948 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002949 * subsequent call wants to start a write request. raid_run_ops only
2950 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002951 * simultaneously. If this is not the case then new writes need to be
2952 * held off until the compute completes.
2953 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002954 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2955 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2956 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002957 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002958}
2959
NeilBrownd1688a62011-10-11 16:49:52 +11002960static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002961 struct stripe_head_state *s, int disks)
2962{
Dan Williamsecc65c92008-06-28 08:31:57 +10002963 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002964
Dan Williamsbd2ab672008-04-10 21:29:27 -07002965 set_bit(STRIPE_HANDLE, &sh->state);
2966
Dan Williamsecc65c92008-06-28 08:31:57 +10002967 switch (sh->check_state) {
2968 case check_state_idle:
2969 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002970 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002971 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002972 sh->check_state = check_state_run;
2973 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002974 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002975 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002976 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002977 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002978 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002979 /* fall through */
2980 case check_state_compute_result:
2981 sh->check_state = check_state_idle;
2982 if (!dev)
2983 dev = &sh->dev[sh->pd_idx];
2984
2985 /* check that a write has not made the stripe insync */
2986 if (test_bit(STRIPE_INSYNC, &sh->state))
2987 break;
2988
2989 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002990 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2991 BUG_ON(s->uptodate != disks);
2992
2993 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002994 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002995 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002996
Dan Williamsa4456852007-07-09 11:56:43 -07002997 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002998 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002999 break;
3000 case check_state_run:
3001 break; /* we will be called again upon completion */
3002 case check_state_check_result:
3003 sh->check_state = check_state_idle;
3004
3005 /* if a failure occurred during the check operation, leave
3006 * STRIPE_INSYNC not set and let the stripe be handled again
3007 */
3008 if (s->failed)
3009 break;
3010
3011 /* handle a successful check operation, if parity is correct
3012 * we are done. Otherwise update the mismatch count and repair
3013 * parity if !MD_RECOVERY_CHECK
3014 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003015 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003016 /* parity is correct (on disc,
3017 * not in buffer any more)
3018 */
3019 set_bit(STRIPE_INSYNC, &sh->state);
3020 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003021 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003022 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3023 /* don't try to repair!! */
3024 set_bit(STRIPE_INSYNC, &sh->state);
3025 else {
3026 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003027 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003028 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3029 set_bit(R5_Wantcompute,
3030 &sh->dev[sh->pd_idx].flags);
3031 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003032 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003033 s->uptodate++;
3034 }
3035 }
3036 break;
3037 case check_state_compute_run:
3038 break;
3039 default:
3040 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3041 __func__, sh->check_state,
3042 (unsigned long long) sh->sector);
3043 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003044 }
3045}
3046
3047
NeilBrownd1688a62011-10-11 16:49:52 +11003048static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003049 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003050 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003051{
Dan Williamsa4456852007-07-09 11:56:43 -07003052 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003053 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003054 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003055
3056 set_bit(STRIPE_HANDLE, &sh->state);
3057
3058 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003059
Dan Williamsa4456852007-07-09 11:56:43 -07003060 /* Want to check and possibly repair P and Q.
3061 * However there could be one 'failed' device, in which
3062 * case we can only check one of them, possibly using the
3063 * other to generate missing data
3064 */
3065
Dan Williamsd82dfee2009-07-14 13:40:57 -07003066 switch (sh->check_state) {
3067 case check_state_idle:
3068 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003069 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003070 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003071 * makes sense to check P (If anything else were failed,
3072 * we would have used P to recreate it).
3073 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003074 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003075 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003076 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003077 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003078 * anything, so it makes sense to check it
3079 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003080 if (sh->check_state == check_state_run)
3081 sh->check_state = check_state_run_pq;
3082 else
3083 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003084 }
Dan Williams36d1c642009-07-14 11:48:22 -07003085
Dan Williamsd82dfee2009-07-14 13:40:57 -07003086 /* discard potentially stale zero_sum_result */
3087 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003088
Dan Williamsd82dfee2009-07-14 13:40:57 -07003089 if (sh->check_state == check_state_run) {
3090 /* async_xor_zero_sum destroys the contents of P */
3091 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3092 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003093 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003094 if (sh->check_state >= check_state_run &&
3095 sh->check_state <= check_state_run_pq) {
3096 /* async_syndrome_zero_sum preserves P and Q, so
3097 * no need to mark them !uptodate here
3098 */
3099 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3100 break;
3101 }
Dan Williams36d1c642009-07-14 11:48:22 -07003102
Dan Williamsd82dfee2009-07-14 13:40:57 -07003103 /* we have 2-disk failure */
3104 BUG_ON(s->failed != 2);
3105 /* fall through */
3106 case check_state_compute_result:
3107 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003108
Dan Williamsd82dfee2009-07-14 13:40:57 -07003109 /* check that a write has not made the stripe insync */
3110 if (test_bit(STRIPE_INSYNC, &sh->state))
3111 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003112
3113 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003114 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003115 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003116 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003117 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003118 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003119 s->locked++;
3120 set_bit(R5_LOCKED, &dev->flags);
3121 set_bit(R5_Wantwrite, &dev->flags);
3122 }
3123 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003124 dev = &sh->dev[s->failed_num[0]];
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 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003129 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003130 dev = &sh->dev[pd_idx];
3131 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_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003136 dev = &sh->dev[qd_idx];
3137 s->locked++;
3138 set_bit(R5_LOCKED, &dev->flags);
3139 set_bit(R5_Wantwrite, &dev->flags);
3140 }
3141 clear_bit(STRIPE_DEGRADED, &sh->state);
3142
3143 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003144 break;
3145 case check_state_run:
3146 case check_state_run_q:
3147 case check_state_run_pq:
3148 break; /* we will be called again upon completion */
3149 case check_state_check_result:
3150 sh->check_state = check_state_idle;
3151
3152 /* handle a successful check operation, if parity is correct
3153 * we are done. Otherwise update the mismatch count and repair
3154 * parity if !MD_RECOVERY_CHECK
3155 */
3156 if (sh->ops.zero_sum_result == 0) {
3157 /* both parities are correct */
3158 if (!s->failed)
3159 set_bit(STRIPE_INSYNC, &sh->state);
3160 else {
3161 /* in contrast to the raid5 case we can validate
3162 * parity, but still have a failure to write
3163 * back
3164 */
3165 sh->check_state = check_state_compute_result;
3166 /* Returning at this point means that we may go
3167 * off and bring p and/or q uptodate again so
3168 * we make sure to check zero_sum_result again
3169 * to verify if p or q need writeback
3170 */
3171 }
3172 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003173 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003174 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3175 /* don't try to repair!! */
3176 set_bit(STRIPE_INSYNC, &sh->state);
3177 else {
3178 int *target = &sh->ops.target;
3179
3180 sh->ops.target = -1;
3181 sh->ops.target2 = -1;
3182 sh->check_state = check_state_compute_run;
3183 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3184 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3185 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3186 set_bit(R5_Wantcompute,
3187 &sh->dev[pd_idx].flags);
3188 *target = pd_idx;
3189 target = &sh->ops.target2;
3190 s->uptodate++;
3191 }
3192 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3193 set_bit(R5_Wantcompute,
3194 &sh->dev[qd_idx].flags);
3195 *target = qd_idx;
3196 s->uptodate++;
3197 }
3198 }
3199 }
3200 break;
3201 case check_state_compute_run:
3202 break;
3203 default:
3204 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3205 __func__, sh->check_state,
3206 (unsigned long long) sh->sector);
3207 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003208 }
3209}
3210
NeilBrownd1688a62011-10-11 16:49:52 +11003211static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003212{
3213 int i;
3214
3215 /* We have read all the blocks in this stripe and now we need to
3216 * copy some of them into a target stripe for expand.
3217 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003218 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003219 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3220 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003221 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003222 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003223 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003224 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003225
NeilBrown784052e2009-03-31 15:19:07 +11003226 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003227 sector_t s = raid5_compute_sector(conf, bn, 0,
3228 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003229 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003230 if (sh2 == NULL)
3231 /* so far only the early blocks of this stripe
3232 * have been requested. When later blocks
3233 * get requested, we will try again
3234 */
3235 continue;
3236 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3237 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3238 /* must have already done this block */
3239 release_stripe(sh2);
3240 continue;
3241 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003242
3243 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003244 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003245 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003246 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003247 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003248
Dan Williamsa4456852007-07-09 11:56:43 -07003249 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3250 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3251 for (j = 0; j < conf->raid_disks; j++)
3252 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003253 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003254 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3255 break;
3256 if (j == conf->raid_disks) {
3257 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3258 set_bit(STRIPE_HANDLE, &sh2->state);
3259 }
3260 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003261
Dan Williamsa4456852007-07-09 11:56:43 -07003262 }
NeilBrowna2e08552007-09-11 15:23:36 -07003263 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003264 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003265}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
3267/*
3268 * handle_stripe - do things to a stripe.
3269 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003270 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3271 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003273 * return some read requests which now have data
3274 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 * schedule a read on some buffers
3276 * schedule a write of some buffers
3277 * return confirmation of parity correctness
3278 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 */
Dan Williamsa4456852007-07-09 11:56:43 -07003280
NeilBrownacfe7262011-07-27 11:00:36 +10003281static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003282{
NeilBrownd1688a62011-10-11 16:49:52 +11003283 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003284 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003285 struct r5dev *dev;
3286 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003287 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003288
NeilBrownacfe7262011-07-27 11:00:36 +10003289 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003290
NeilBrownacfe7262011-07-27 11:00:36 +10003291 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3292 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3293 s->failed_num[0] = -1;
3294 s->failed_num[1] = -1;
3295
3296 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003297 rcu_read_lock();
3298 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003299 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003300 sector_t first_bad;
3301 int bad_sectors;
3302 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003303
NeilBrown16a53ec2006-06-26 00:27:38 -07003304 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003305
Dan Williams45b42332007-07-09 11:56:43 -07003306 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003307 i, dev->flags,
3308 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003309 /* maybe we can reply to a read
3310 *
3311 * new wantfill requests are only permitted while
3312 * ops_complete_biofill is guaranteed to be inactive
3313 */
3314 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3315 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3316 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003317
3318 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003319 if (test_bit(R5_LOCKED, &dev->flags))
3320 s->locked++;
3321 if (test_bit(R5_UPTODATE, &dev->flags))
3322 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003323 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003324 s->compute++;
3325 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003326 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003327
NeilBrownacfe7262011-07-27 11:00:36 +10003328 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003329 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003330 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003331 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003332 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003333 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003334 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003335 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003336 }
Dan Williamsa4456852007-07-09 11:56:43 -07003337 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003338 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003339 /* Prefer to use the replacement for reads, but only
3340 * if it is recovered enough and has no bad blocks.
3341 */
3342 rdev = rcu_dereference(conf->disks[i].replacement);
3343 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3344 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3345 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3346 &first_bad, &bad_sectors))
3347 set_bit(R5_ReadRepl, &dev->flags);
3348 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003349 if (rdev)
3350 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003351 rdev = rcu_dereference(conf->disks[i].rdev);
3352 clear_bit(R5_ReadRepl, &dev->flags);
3353 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003354 if (rdev && test_bit(Faulty, &rdev->flags))
3355 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003356 if (rdev) {
3357 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3358 &first_bad, &bad_sectors);
3359 if (s->blocked_rdev == NULL
3360 && (test_bit(Blocked, &rdev->flags)
3361 || is_bad < 0)) {
3362 if (is_bad < 0)
3363 set_bit(BlockedBadBlocks,
3364 &rdev->flags);
3365 s->blocked_rdev = rdev;
3366 atomic_inc(&rdev->nr_pending);
3367 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003368 }
NeilBrown415e72d2010-06-17 17:25:21 +10003369 clear_bit(R5_Insync, &dev->flags);
3370 if (!rdev)
3371 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003372 else if (is_bad) {
3373 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003374 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3375 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003376 /* treat as in-sync, but with a read error
3377 * which we can now try to correct
3378 */
3379 set_bit(R5_Insync, &dev->flags);
3380 set_bit(R5_ReadError, &dev->flags);
3381 }
3382 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003383 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003384 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003385 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003386 set_bit(R5_Insync, &dev->flags);
3387 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3388 test_bit(R5_Expanded, &dev->flags))
3389 /* If we've reshaped into here, we assume it is Insync.
3390 * We will shortly update recovery_offset to make
3391 * it official.
3392 */
3393 set_bit(R5_Insync, &dev->flags);
3394
NeilBrownc44bc442014-01-06 13:19:42 +11003395 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003396 /* This flag does not apply to '.replacement'
3397 * only to .rdev, so make sure to check that*/
3398 struct md_rdev *rdev2 = rcu_dereference(
3399 conf->disks[i].rdev);
3400 if (rdev2 == rdev)
3401 clear_bit(R5_Insync, &dev->flags);
3402 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003403 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003404 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003405 } else
3406 clear_bit(R5_WriteError, &dev->flags);
3407 }
NeilBrownc44bc442014-01-06 13:19:42 +11003408 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003409 /* This flag does not apply to '.replacement'
3410 * only to .rdev, so make sure to check that*/
3411 struct md_rdev *rdev2 = rcu_dereference(
3412 conf->disks[i].rdev);
3413 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003414 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003415 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003416 } else
3417 clear_bit(R5_MadeGood, &dev->flags);
3418 }
NeilBrown977df362011-12-23 10:17:53 +11003419 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3420 struct md_rdev *rdev2 = rcu_dereference(
3421 conf->disks[i].replacement);
3422 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3423 s->handle_bad_blocks = 1;
3424 atomic_inc(&rdev2->nr_pending);
3425 } else
3426 clear_bit(R5_MadeGoodRepl, &dev->flags);
3427 }
NeilBrown415e72d2010-06-17 17:25:21 +10003428 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003429 /* The ReadError flag will just be confusing now */
3430 clear_bit(R5_ReadError, &dev->flags);
3431 clear_bit(R5_ReWrite, &dev->flags);
3432 }
NeilBrown415e72d2010-06-17 17:25:21 +10003433 if (test_bit(R5_ReadError, &dev->flags))
3434 clear_bit(R5_Insync, &dev->flags);
3435 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003436 if (s->failed < 2)
3437 s->failed_num[s->failed] = i;
3438 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003439 if (rdev && !test_bit(Faulty, &rdev->flags))
3440 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003441 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003442 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003443 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3444 /* If there is a failed device being replaced,
3445 * we must be recovering.
3446 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003447 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003448 * else we can only be replacing
3449 * sync and recovery both need to read all devices, and so
3450 * use the same flag.
3451 */
3452 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003453 sh->sector >= conf->mddev->recovery_cp ||
3454 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003455 s->syncing = 1;
3456 else
3457 s->replacing = 1;
3458 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003459 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003460}
NeilBrownf4168852007-02-28 20:11:53 -08003461
NeilBrowncc940152011-07-26 11:35:35 +10003462static void handle_stripe(struct stripe_head *sh)
3463{
3464 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003465 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003466 int i;
NeilBrown84789552011-07-27 11:00:36 +10003467 int prexor;
3468 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003469 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003470
3471 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003472 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003473 /* already being handled, ensure it gets handled
3474 * again when current action finishes */
3475 set_bit(STRIPE_HANDLE, &sh->state);
3476 return;
3477 }
3478
NeilBrownf8dfcff2013-03-12 12:18:06 +11003479 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3480 spin_lock(&sh->stripe_lock);
3481 /* Cannot process 'sync' concurrently with 'discard' */
3482 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
3483 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3484 set_bit(STRIPE_SYNCING, &sh->state);
3485 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownc1dadcc2013-07-22 12:57:21 +10003486 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003487 }
3488 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10003489 }
3490 clear_bit(STRIPE_DELAYED, &sh->state);
3491
3492 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3493 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3494 (unsigned long long)sh->sector, sh->state,
3495 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3496 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003497
NeilBrownacfe7262011-07-27 11:00:36 +10003498 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003499
NeilBrownbc2607f2011-07-28 11:39:22 +10003500 if (s.handle_bad_blocks) {
3501 set_bit(STRIPE_HANDLE, &sh->state);
3502 goto finish;
3503 }
3504
NeilBrown474af965fe2011-07-27 11:00:36 +10003505 if (unlikely(s.blocked_rdev)) {
3506 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003507 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003508 set_bit(STRIPE_HANDLE, &sh->state);
3509 goto finish;
3510 }
3511 /* There is nothing for the blocked_rdev to block */
3512 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3513 s.blocked_rdev = NULL;
3514 }
3515
3516 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3517 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3518 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3519 }
3520
3521 pr_debug("locked=%d uptodate=%d to_read=%d"
3522 " to_write=%d failed=%d failed_num=%d,%d\n",
3523 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3524 s.failed_num[0], s.failed_num[1]);
3525 /* check if the array has lost more than max_degraded devices and,
3526 * if so, some requests might need to be failed.
3527 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003528 if (s.failed > conf->max_degraded) {
3529 sh->check_state = 0;
3530 sh->reconstruct_state = 0;
3531 if (s.to_read+s.to_write+s.written)
3532 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003533 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003534 handle_failed_sync(conf, sh, &s);
3535 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003536
NeilBrown84789552011-07-27 11:00:36 +10003537 /* Now we check to see if any write operations have recently
3538 * completed
3539 */
3540 prexor = 0;
3541 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3542 prexor = 1;
3543 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3544 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3545 sh->reconstruct_state = reconstruct_state_idle;
3546
3547 /* All the 'written' buffers and the parity block are ready to
3548 * be written back to disk
3549 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003550 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3551 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003552 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003553 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3554 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003555 for (i = disks; i--; ) {
3556 struct r5dev *dev = &sh->dev[i];
3557 if (test_bit(R5_LOCKED, &dev->flags) &&
3558 (i == sh->pd_idx || i == sh->qd_idx ||
3559 dev->written)) {
3560 pr_debug("Writing block %d\n", i);
3561 set_bit(R5_Wantwrite, &dev->flags);
3562 if (prexor)
3563 continue;
NeilBrown318a3d52014-08-13 09:57:07 +10003564 if (s.failed > 1)
3565 continue;
NeilBrown84789552011-07-27 11:00:36 +10003566 if (!test_bit(R5_Insync, &dev->flags) ||
3567 ((i == sh->pd_idx || i == sh->qd_idx) &&
3568 s.failed == 0))
3569 set_bit(STRIPE_INSYNC, &sh->state);
3570 }
3571 }
3572 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3573 s.dec_preread_active = 1;
3574 }
3575
NeilBrownef5b7c62012-11-22 09:13:36 +11003576 /*
3577 * might be able to return some write requests if the parity blocks
3578 * are safe, or on a failed drive
3579 */
3580 pdev = &sh->dev[sh->pd_idx];
3581 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3582 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3583 qdev = &sh->dev[sh->qd_idx];
3584 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3585 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3586 || conf->level < 6;
3587
3588 if (s.written &&
3589 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3590 && !test_bit(R5_LOCKED, &pdev->flags)
3591 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3592 test_bit(R5_Discard, &pdev->flags))))) &&
3593 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3594 && !test_bit(R5_LOCKED, &qdev->flags)
3595 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3596 test_bit(R5_Discard, &qdev->flags))))))
3597 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3598
3599 /* Now we might consider reading some blocks, either to check/generate
3600 * parity, or to satisfy requests
3601 * or to load a block that is being partially written.
3602 */
3603 if (s.to_read || s.non_overwrite
3604 || (conf->level == 6 && s.to_write && s.failed)
3605 || (s.syncing && (s.uptodate + s.compute < disks))
3606 || s.replacing
3607 || s.expanding)
3608 handle_stripe_fill(sh, &s, disks);
3609
NeilBrown84789552011-07-27 11:00:36 +10003610 /* Now to consider new write requests and what else, if anything
3611 * should be read. We do not handle new writes when:
3612 * 1/ A 'write' operation (copy+xor) is already in flight.
3613 * 2/ A 'check' operation is in flight, as it may clobber the parity
3614 * block.
3615 */
3616 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3617 handle_stripe_dirtying(conf, sh, &s, disks);
3618
3619 /* maybe we need to check and possibly fix the parity for this stripe
3620 * Any reads will already have been scheduled, so we just see if enough
3621 * data is available. The parity check is held off while parity
3622 * dependent operations are in flight.
3623 */
3624 if (sh->check_state ||
3625 (s.syncing && s.locked == 0 &&
3626 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3627 !test_bit(STRIPE_INSYNC, &sh->state))) {
3628 if (conf->level == 6)
3629 handle_parity_checks6(conf, sh, &s, disks);
3630 else
3631 handle_parity_checks5(conf, sh, &s, disks);
3632 }
NeilBrownc5a31002011-07-27 11:00:36 +10003633
NeilBrownc1dadcc2013-07-22 12:57:21 +10003634 if ((s.replacing || s.syncing) && s.locked == 0
3635 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
3636 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11003637 /* Write out to replacement devices where possible */
3638 for (i = 0; i < conf->raid_disks; i++)
NeilBrownc1dadcc2013-07-22 12:57:21 +10003639 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3640 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11003641 set_bit(R5_WantReplace, &sh->dev[i].flags);
3642 set_bit(R5_LOCKED, &sh->dev[i].flags);
3643 s.locked++;
3644 }
NeilBrownc1dadcc2013-07-22 12:57:21 +10003645 if (s.replacing)
3646 set_bit(STRIPE_INSYNC, &sh->state);
3647 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11003648 }
3649 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownc1dadcc2013-07-22 12:57:21 +10003650 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11003651 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003652 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3653 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003654 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3655 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10003656 }
3657
3658 /* If the failed drives are just a ReadError, then we might need
3659 * to progress the repair/check process
3660 */
3661 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3662 for (i = 0; i < s.failed; i++) {
3663 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3664 if (test_bit(R5_ReadError, &dev->flags)
3665 && !test_bit(R5_LOCKED, &dev->flags)
3666 && test_bit(R5_UPTODATE, &dev->flags)
3667 ) {
3668 if (!test_bit(R5_ReWrite, &dev->flags)) {
3669 set_bit(R5_Wantwrite, &dev->flags);
3670 set_bit(R5_ReWrite, &dev->flags);
3671 set_bit(R5_LOCKED, &dev->flags);
3672 s.locked++;
3673 } else {
3674 /* let's read it back */
3675 set_bit(R5_Wantread, &dev->flags);
3676 set_bit(R5_LOCKED, &dev->flags);
3677 s.locked++;
3678 }
3679 }
3680 }
3681
3682
NeilBrown3687c062011-07-27 11:00:36 +10003683 /* Finish reconstruct operations initiated by the expansion process */
3684 if (sh->reconstruct_state == reconstruct_state_result) {
3685 struct stripe_head *sh_src
3686 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3687 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3688 /* sh cannot be written until sh_src has been read.
3689 * so arrange for sh to be delayed a little
3690 */
3691 set_bit(STRIPE_DELAYED, &sh->state);
3692 set_bit(STRIPE_HANDLE, &sh->state);
3693 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3694 &sh_src->state))
3695 atomic_inc(&conf->preread_active_stripes);
3696 release_stripe(sh_src);
3697 goto finish;
3698 }
3699 if (sh_src)
3700 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003701
NeilBrown3687c062011-07-27 11:00:36 +10003702 sh->reconstruct_state = reconstruct_state_idle;
3703 clear_bit(STRIPE_EXPANDING, &sh->state);
3704 for (i = conf->raid_disks; i--; ) {
3705 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3706 set_bit(R5_LOCKED, &sh->dev[i].flags);
3707 s.locked++;
3708 }
3709 }
3710
3711 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3712 !sh->reconstruct_state) {
3713 /* Need to write out all blocks after computing parity */
3714 sh->disks = conf->raid_disks;
3715 stripe_set_idx(sh->sector, conf, 0, sh);
3716 schedule_reconstruction(sh, &s, 1, 1);
3717 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3718 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3719 atomic_dec(&conf->reshape_stripes);
3720 wake_up(&conf->wait_for_overlap);
3721 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3722 }
3723
3724 if (s.expanding && s.locked == 0 &&
3725 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3726 handle_stripe_expansion(conf, sh);
3727
3728finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003729 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003730 if (unlikely(s.blocked_rdev)) {
3731 if (conf->mddev->external)
3732 md_wait_for_blocked_rdev(s.blocked_rdev,
3733 conf->mddev);
3734 else
3735 /* Internal metadata will immediately
3736 * be written by raid5d, so we don't
3737 * need to wait here.
3738 */
3739 rdev_dec_pending(s.blocked_rdev,
3740 conf->mddev);
3741 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003742
NeilBrownbc2607f2011-07-28 11:39:22 +10003743 if (s.handle_bad_blocks)
3744 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003745 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003746 struct r5dev *dev = &sh->dev[i];
3747 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3748 /* We own a safe reference to the rdev */
3749 rdev = conf->disks[i].rdev;
3750 if (!rdev_set_badblocks(rdev, sh->sector,
3751 STRIPE_SECTORS, 0))
3752 md_error(conf->mddev, rdev);
3753 rdev_dec_pending(rdev, conf->mddev);
3754 }
NeilBrownb84db562011-07-28 11:39:23 +10003755 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3756 rdev = conf->disks[i].rdev;
3757 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003758 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003759 rdev_dec_pending(rdev, conf->mddev);
3760 }
NeilBrown977df362011-12-23 10:17:53 +11003761 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3762 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003763 if (!rdev)
3764 /* rdev have been moved down */
3765 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003766 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003767 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003768 rdev_dec_pending(rdev, conf->mddev);
3769 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003770 }
3771
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003772 if (s.ops_request)
3773 raid_run_ops(sh, s.ops_request);
3774
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003775 ops_run_io(sh, &s);
3776
NeilBrownc5709ef2011-07-26 11:35:20 +10003777 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003778 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003779 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003780 * have actually been submitted.
3781 */
3782 atomic_dec(&conf->preread_active_stripes);
3783 if (atomic_read(&conf->preread_active_stripes) <
3784 IO_THRESHOLD)
3785 md_wakeup_thread(conf->mddev->thread);
3786 }
3787
NeilBrownc5709ef2011-07-26 11:35:20 +10003788 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003789
Dan Williams257a4b42011-11-08 16:22:06 +11003790 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003791}
3792
NeilBrownd1688a62011-10-11 16:49:52 +11003793static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794{
3795 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3796 while (!list_empty(&conf->delayed_list)) {
3797 struct list_head *l = conf->delayed_list.next;
3798 struct stripe_head *sh;
3799 sh = list_entry(l, struct stripe_head, lru);
3800 list_del_init(l);
3801 clear_bit(STRIPE_DELAYED, &sh->state);
3802 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3803 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003804 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 }
NeilBrown482c0832011-04-18 18:25:42 +10003806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807}
3808
NeilBrownd1688a62011-10-11 16:49:52 +11003809static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003810{
3811 /* device_lock is held */
3812 struct list_head head;
3813 list_add(&head, &conf->bitmap_list);
3814 list_del_init(&conf->bitmap_list);
3815 while (!list_empty(&head)) {
3816 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3817 list_del_init(&sh->lru);
3818 atomic_inc(&sh->count);
3819 __release_stripe(conf, sh);
3820 }
3821}
3822
NeilBrownfd01b882011-10-11 16:47:53 +11003823int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003824{
NeilBrownd1688a62011-10-11 16:49:52 +11003825 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003826
3827 /* No difference between reads and writes. Just check
3828 * how busy the stripe_cache is
3829 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003830
NeilBrownf022b2f2006-10-03 01:15:56 -07003831 if (conf->inactive_blocked)
3832 return 1;
3833 if (conf->quiesce)
3834 return 1;
3835 if (list_empty_careful(&conf->inactive_list))
3836 return 1;
3837
3838 return 0;
3839}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003840EXPORT_SYMBOL_GPL(md_raid5_congested);
3841
3842static int raid5_congested(void *data, int bits)
3843{
NeilBrownfd01b882011-10-11 16:47:53 +11003844 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003845
3846 return mddev_congested(mddev, bits) ||
3847 md_raid5_congested(mddev, bits);
3848}
NeilBrownf022b2f2006-10-03 01:15:56 -07003849
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003850/* We want read requests to align with chunks where possible,
3851 * but write requests don't need to.
3852 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003853static int raid5_mergeable_bvec(struct request_queue *q,
3854 struct bvec_merge_data *bvm,
3855 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003856{
NeilBrownfd01b882011-10-11 16:47:53 +11003857 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003858 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003859 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003860 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003861 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003862
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003863 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003864 return biovec->bv_len; /* always allow writes to be mergeable */
3865
Andre Noll664e7c42009-06-18 08:45:27 +10003866 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3867 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003868 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3869 if (max < 0) max = 0;
3870 if (max <= biovec->bv_len && bio_sectors == 0)
3871 return biovec->bv_len;
3872 else
3873 return max;
3874}
3875
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003876
NeilBrownfd01b882011-10-11 16:47:53 +11003877static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003878{
3879 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003880 unsigned int chunk_sectors = mddev->chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08003881 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003882
Andre Noll664e7c42009-06-18 08:45:27 +10003883 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3884 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003885 return chunk_sectors >=
3886 ((sector & (chunk_sectors - 1)) + bio_sectors);
3887}
3888
3889/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003890 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3891 * later sampled by raid5d.
3892 */
NeilBrownd1688a62011-10-11 16:49:52 +11003893static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003894{
3895 unsigned long flags;
3896
3897 spin_lock_irqsave(&conf->device_lock, flags);
3898
3899 bi->bi_next = conf->retry_read_aligned_list;
3900 conf->retry_read_aligned_list = bi;
3901
3902 spin_unlock_irqrestore(&conf->device_lock, flags);
3903 md_wakeup_thread(conf->mddev->thread);
3904}
3905
3906
NeilBrownd1688a62011-10-11 16:49:52 +11003907static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003908{
3909 struct bio *bi;
3910
3911 bi = conf->retry_read_aligned;
3912 if (bi) {
3913 conf->retry_read_aligned = NULL;
3914 return bi;
3915 }
3916 bi = conf->retry_read_aligned_list;
3917 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003918 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003919 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003920 /*
3921 * this sets the active strip count to 1 and the processed
3922 * strip count to zero (upper 8 bits)
3923 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003924 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003925 }
3926
3927 return bi;
3928}
3929
3930
3931/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003932 * The "raid5_align_endio" should check if the read succeeded and if it
3933 * did, call bio_endio on the original bio (having bio_put the new bio
3934 * first).
3935 * If the read failed..
3936 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003937static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003938{
3939 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003940 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003941 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003942 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003943 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003944
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003945 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003946
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003947 rdev = (void*)raid_bi->bi_next;
3948 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003949 mddev = rdev->mddev;
3950 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003951
3952 rdev_dec_pending(rdev, conf->mddev);
3953
3954 if (!error && uptodate) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07003955 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
3956 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02003957 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003958 if (atomic_dec_and_test(&conf->active_aligned_reads))
3959 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003960 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003961 }
3962
3963
Dan Williams45b42332007-07-09 11:56:43 -07003964 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003965
3966 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003967}
3968
Neil Brown387bb172007-02-08 14:20:29 -08003969static int bio_fits_rdev(struct bio *bi)
3970{
Jens Axboe165125e2007-07-24 09:28:11 +02003971 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003972
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08003973 if (bio_sectors(bi) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003974 return 0;
3975 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003976 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003977 return 0;
3978
3979 if (q->merge_bvec_fn)
3980 /* it's too hard to apply the merge_bvec_fn at this stage,
3981 * just just give up
3982 */
3983 return 0;
3984
3985 return 1;
3986}
3987
3988
NeilBrownfd01b882011-10-11 16:47:53 +11003989static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003990{
NeilBrownd1688a62011-10-11 16:49:52 +11003991 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003992 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003993 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003994 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003995 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003996
3997 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003998 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003999 return 0;
4000 }
4001 /*
NeilBrowna167f662010-10-26 18:31:13 +11004002 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004003 */
NeilBrowna167f662010-10-26 18:31:13 +11004004 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004005 if (!align_bi)
4006 return 0;
4007 /*
4008 * set bi_end_io to a new function, and set bi_private to the
4009 * original bio.
4010 */
4011 align_bi->bi_end_io = raid5_align_endio;
4012 align_bi->bi_private = raid_bio;
4013 /*
4014 * compute position
4015 */
NeilBrown112bf892009-03-31 14:39:38 +11004016 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
4017 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11004018 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004019
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004020 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004021 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11004022 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4023 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4024 rdev->recovery_offset < end_sector) {
4025 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4026 if (rdev &&
4027 (test_bit(Faulty, &rdev->flags) ||
4028 !(test_bit(In_sync, &rdev->flags) ||
4029 rdev->recovery_offset >= end_sector)))
4030 rdev = NULL;
4031 }
4032 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10004033 sector_t first_bad;
4034 int bad_sectors;
4035
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004036 atomic_inc(&rdev->nr_pending);
4037 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004038 raid_bio->bi_next = (void*)rdev;
4039 align_bi->bi_bdev = rdev->bdev;
4040 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004041
NeilBrown31c176e2011-07-28 11:39:22 +10004042 if (!bio_fits_rdev(align_bi) ||
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004043 is_badblock(rdev, align_bi->bi_sector, bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10004044 &first_bad, &bad_sectors)) {
4045 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004046 bio_put(align_bi);
4047 rdev_dec_pending(rdev, mddev);
4048 return 0;
4049 }
4050
majianpeng6c0544e2012-06-12 08:31:10 +08004051 /* No reshape active, so we can trust rdev->data_offset */
4052 align_bi->bi_sector += rdev->data_offset;
4053
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004054 spin_lock_irq(&conf->device_lock);
4055 wait_event_lock_irq(conf->wait_for_stripe,
4056 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004057 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004058 atomic_inc(&conf->active_aligned_reads);
4059 spin_unlock_irq(&conf->device_lock);
4060
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004061 if (mddev->gendisk)
4062 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4063 align_bi, disk_devt(mddev->gendisk),
4064 raid_bio->bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004065 generic_make_request(align_bi);
4066 return 1;
4067 } else {
4068 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004069 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004070 return 0;
4071 }
4072}
4073
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004074/* __get_priority_stripe - get the next stripe to process
4075 *
4076 * Full stripe writes are allowed to pass preread active stripes up until
4077 * the bypass_threshold is exceeded. In general the bypass_count
4078 * increments when the handle_list is handled before the hold_list; however, it
4079 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4080 * stripe with in flight i/o. The bypass_count will be reset when the
4081 * head of the hold_list has changed, i.e. the head was promoted to the
4082 * handle_list.
4083 */
NeilBrownd1688a62011-10-11 16:49:52 +11004084static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004085{
4086 struct stripe_head *sh;
4087
4088 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4089 __func__,
4090 list_empty(&conf->handle_list) ? "empty" : "busy",
4091 list_empty(&conf->hold_list) ? "empty" : "busy",
4092 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4093
4094 if (!list_empty(&conf->handle_list)) {
4095 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4096
4097 if (list_empty(&conf->hold_list))
4098 conf->bypass_count = 0;
4099 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4100 if (conf->hold_list.next == conf->last_hold)
4101 conf->bypass_count++;
4102 else {
4103 conf->last_hold = conf->hold_list.next;
4104 conf->bypass_count -= conf->bypass_threshold;
4105 if (conf->bypass_count < 0)
4106 conf->bypass_count = 0;
4107 }
4108 }
4109 } else if (!list_empty(&conf->hold_list) &&
4110 ((conf->bypass_threshold &&
4111 conf->bypass_count > conf->bypass_threshold) ||
4112 atomic_read(&conf->pending_full_writes) == 0)) {
4113 sh = list_entry(conf->hold_list.next,
4114 typeof(*sh), lru);
4115 conf->bypass_count -= conf->bypass_threshold;
4116 if (conf->bypass_count < 0)
4117 conf->bypass_count = 0;
4118 } else
4119 return NULL;
4120
4121 list_del_init(&sh->lru);
4122 atomic_inc(&sh->count);
4123 BUG_ON(atomic_read(&sh->count) != 1);
4124 return sh;
4125}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004126
Shaohua Li8811b592012-08-02 08:33:00 +10004127struct raid5_plug_cb {
4128 struct blk_plug_cb cb;
4129 struct list_head list;
4130};
4131
4132static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4133{
4134 struct raid5_plug_cb *cb = container_of(
4135 blk_cb, struct raid5_plug_cb, cb);
4136 struct stripe_head *sh;
4137 struct mddev *mddev = cb->cb.data;
4138 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004139 int cnt = 0;
Shaohua Li8811b592012-08-02 08:33:00 +10004140
4141 if (cb->list.next && !list_empty(&cb->list)) {
4142 spin_lock_irq(&conf->device_lock);
4143 while (!list_empty(&cb->list)) {
4144 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4145 list_del_init(&sh->lru);
4146 /*
4147 * avoid race release_stripe_plug() sees
4148 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4149 * is still in our list
4150 */
4151 smp_mb__before_clear_bit();
4152 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4153 __release_stripe(conf, sh);
NeilBrowna9add5d2012-10-31 11:59:09 +11004154 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004155 }
4156 spin_unlock_irq(&conf->device_lock);
4157 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004158 if (mddev->queue)
4159 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004160 kfree(cb);
4161}
4162
4163static void release_stripe_plug(struct mddev *mddev,
4164 struct stripe_head *sh)
4165{
4166 struct blk_plug_cb *blk_cb = blk_check_plugged(
4167 raid5_unplug, mddev,
4168 sizeof(struct raid5_plug_cb));
4169 struct raid5_plug_cb *cb;
4170
4171 if (!blk_cb) {
4172 release_stripe(sh);
4173 return;
4174 }
4175
4176 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4177
4178 if (cb->list.next == NULL)
4179 INIT_LIST_HEAD(&cb->list);
4180
4181 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4182 list_add_tail(&sh->lru, &cb->list);
4183 else
4184 release_stripe(sh);
4185}
4186
Shaohua Li620125f2012-10-11 13:49:05 +11004187static void make_discard_request(struct mddev *mddev, struct bio *bi)
4188{
4189 struct r5conf *conf = mddev->private;
4190 sector_t logical_sector, last_sector;
4191 struct stripe_head *sh;
4192 int remaining;
4193 int stripe_sectors;
4194
4195 if (mddev->reshape_position != MaxSector)
4196 /* Skip discard while reshape is happening */
4197 return;
4198
4199 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4200 last_sector = bi->bi_sector + (bi->bi_size>>9);
4201
4202 bi->bi_next = NULL;
4203 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4204
4205 stripe_sectors = conf->chunk_sectors *
4206 (conf->raid_disks - conf->max_degraded);
4207 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4208 stripe_sectors);
4209 sector_div(last_sector, stripe_sectors);
4210
4211 logical_sector *= conf->chunk_sectors;
4212 last_sector *= conf->chunk_sectors;
4213
4214 for (; logical_sector < last_sector;
4215 logical_sector += STRIPE_SECTORS) {
4216 DEFINE_WAIT(w);
4217 int d;
4218 again:
4219 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4220 prepare_to_wait(&conf->wait_for_overlap, &w,
4221 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004222 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4223 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4224 release_stripe(sh);
4225 schedule();
4226 goto again;
4227 }
4228 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11004229 spin_lock_irq(&sh->stripe_lock);
4230 for (d = 0; d < conf->raid_disks; d++) {
4231 if (d == sh->pd_idx || d == sh->qd_idx)
4232 continue;
4233 if (sh->dev[d].towrite || sh->dev[d].toread) {
4234 set_bit(R5_Overlap, &sh->dev[d].flags);
4235 spin_unlock_irq(&sh->stripe_lock);
4236 release_stripe(sh);
4237 schedule();
4238 goto again;
4239 }
4240 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11004241 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11004242 finish_wait(&conf->wait_for_overlap, &w);
4243 for (d = 0; d < conf->raid_disks; d++) {
4244 if (d == sh->pd_idx || d == sh->qd_idx)
4245 continue;
4246 sh->dev[d].towrite = bi;
4247 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4248 raid5_inc_bi_active_stripes(bi);
4249 }
4250 spin_unlock_irq(&sh->stripe_lock);
4251 if (conf->mddev->bitmap) {
4252 for (d = 0;
4253 d < conf->raid_disks - conf->max_degraded;
4254 d++)
4255 bitmap_startwrite(mddev->bitmap,
4256 sh->sector,
4257 STRIPE_SECTORS,
4258 0);
4259 sh->bm_seq = conf->seq_flush + 1;
4260 set_bit(STRIPE_BIT_DELAY, &sh->state);
4261 }
4262
4263 set_bit(STRIPE_HANDLE, &sh->state);
4264 clear_bit(STRIPE_DELAYED, &sh->state);
4265 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4266 atomic_inc(&conf->preread_active_stripes);
4267 release_stripe_plug(mddev, sh);
4268 }
4269
4270 remaining = raid5_dec_bi_active_stripes(bi);
4271 if (remaining == 0) {
4272 md_write_end(mddev);
4273 bio_endio(bi, 0);
4274 }
4275}
4276
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004277static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278{
NeilBrownd1688a62011-10-11 16:49:52 +11004279 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004280 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 sector_t new_sector;
4282 sector_t logical_sector, last_sector;
4283 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004284 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004285 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Tejun Heoe9c74692010-09-03 11:56:18 +02004287 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4288 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004289 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004290 }
4291
NeilBrown3d310eb2005-06-21 17:17:26 -07004292 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004293
NeilBrown802ba062006-12-13 00:34:13 -08004294 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004295 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004296 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004297 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004298
Shaohua Li620125f2012-10-11 13:49:05 +11004299 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4300 make_discard_request(mddev, bi);
4301 return;
4302 }
4303
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004305 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 bi->bi_next = NULL;
4307 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004308
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4310 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004311 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004312
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004313 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004314 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004315 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004316 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004317 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004318 * 64bit on a 32bit platform, and so it might be
4319 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004320 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004321 * the lock is dropped, so once we get a reference
4322 * to the stripe that we think it is, we will have
4323 * to check again.
4324 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004325 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004326 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004327 ? logical_sector < conf->reshape_progress
4328 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004329 previous = 1;
4330 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004331 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004332 ? logical_sector < conf->reshape_safe
4333 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004334 spin_unlock_irq(&conf->device_lock);
4335 schedule();
4336 goto retry;
4337 }
4338 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004339 spin_unlock_irq(&conf->device_lock);
4340 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004341
NeilBrown112bf892009-03-31 14:39:38 +11004342 new_sector = raid5_compute_sector(conf, logical_sector,
4343 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004344 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004345 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 (unsigned long long)new_sector,
4347 (unsigned long long)logical_sector);
4348
NeilBrownb5663ba2009-03-31 14:39:38 +11004349 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004350 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004352 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004353 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004354 * stripe, so we must do the range check again.
4355 * Expansion could still move past after this
4356 * test, but as we are holding a reference to
4357 * 'sh', we know that if that happens,
4358 * STRIPE_EXPANDING will get set and the expansion
4359 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004360 */
4361 int must_retry = 0;
4362 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004363 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004364 ? logical_sector >= conf->reshape_progress
4365 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004366 /* mismatch, need to try again */
4367 must_retry = 1;
4368 spin_unlock_irq(&conf->device_lock);
4369 if (must_retry) {
4370 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004371 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004372 goto retry;
4373 }
4374 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004375
Namhyung Kimffd96e32011-07-18 17:38:51 +10004376 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004377 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004378 logical_sector < mddev->suspend_hi) {
4379 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004380 /* As the suspend_* range is controlled by
4381 * userspace, we want an interruptible
4382 * wait.
4383 */
4384 flush_signals(current);
4385 prepare_to_wait(&conf->wait_for_overlap,
4386 &w, TASK_INTERRUPTIBLE);
4387 if (logical_sector >= mddev->suspend_lo &&
4388 logical_sector < mddev->suspend_hi)
4389 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004390 goto retry;
4391 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004392
4393 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004394 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004395 /* Stripe is busy expanding or
4396 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 * and wait a while
4398 */
NeilBrown482c0832011-04-18 18:25:42 +10004399 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 release_stripe(sh);
4401 schedule();
4402 goto retry;
4403 }
4404 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004405 set_bit(STRIPE_HANDLE, &sh->state);
4406 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004407 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004408 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4409 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004410 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 } else {
4412 /* cannot get stripe for read-ahead, just give-up */
4413 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4414 finish_wait(&conf->wait_for_overlap, &w);
4415 break;
4416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004418
Shaohua Lie7836bd62012-07-19 16:01:31 +10004419 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004420 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
NeilBrown16a53ec2006-06-26 00:27:38 -07004422 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004424
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004425 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4426 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004427 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429}
4430
NeilBrownfd01b882011-10-11 16:47:53 +11004431static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004432
NeilBrownfd01b882011-10-11 16:47:53 +11004433static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434{
NeilBrown52c03292006-06-26 00:27:43 -07004435 /* reshaping is quite different to recovery/resync so it is
4436 * handled quite separately ... here.
4437 *
4438 * On each call to sync_request, we gather one chunk worth of
4439 * destination stripes and flag them as expanding.
4440 * Then we find all the source stripes and request reads.
4441 * As the reads complete, handle_stripe will copy the data
4442 * into the destination stripe and release that stripe.
4443 */
NeilBrownd1688a62011-10-11 16:49:52 +11004444 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004446 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004447 int raid_disks = conf->previous_raid_disks;
4448 int data_disks = raid_disks - conf->max_degraded;
4449 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004450 int i;
4451 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004452 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004453 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004454 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004455 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004456
NeilBrownfef9c612009-03-31 15:16:46 +11004457 if (sector_nr == 0) {
4458 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004459 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004460 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4461 sector_nr = raid5_size(mddev, 0, 0)
4462 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004463 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004464 conf->reshape_progress > 0)
4465 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004466 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004467 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004468 mddev->curr_resync_completed = sector_nr;
4469 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004470 *skipped = 1;
4471 return sector_nr;
4472 }
NeilBrown52c03292006-06-26 00:27:43 -07004473 }
4474
NeilBrown7a661382009-03-31 15:21:40 +11004475 /* We need to process a full chunk at a time.
4476 * If old and new chunk sizes differ, we need to process the
4477 * largest of these
4478 */
Andre Noll664e7c42009-06-18 08:45:27 +10004479 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4480 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004481 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004482 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004483
NeilBrownb5254dd2012-05-21 09:27:01 +10004484 /* We update the metadata at least every 10 seconds, or when
4485 * the data about to be copied would over-write the source of
4486 * the data at the front of the range. i.e. one new_stripe
4487 * along from reshape_progress new_maps to after where
4488 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004489 */
NeilBrownfef9c612009-03-31 15:16:46 +11004490 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004491 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004492 readpos = conf->reshape_progress;
4493 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004494 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004495 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004496 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004497 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004498 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004499 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004500 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004501 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004502 readpos -= min_t(sector_t, reshape_sectors, readpos);
4503 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004504 }
NeilBrown52c03292006-06-26 00:27:43 -07004505
NeilBrownb5254dd2012-05-21 09:27:01 +10004506 /* Having calculated the 'writepos' possibly use it
4507 * to set 'stripe_addr' which is where we will write to.
4508 */
4509 if (mddev->reshape_backwards) {
4510 BUG_ON(conf->reshape_progress == 0);
4511 stripe_addr = writepos;
4512 BUG_ON((mddev->dev_sectors &
4513 ~((sector_t)reshape_sectors - 1))
4514 - reshape_sectors - stripe_addr
4515 != sector_nr);
4516 } else {
4517 BUG_ON(writepos != sector_nr + reshape_sectors);
4518 stripe_addr = sector_nr;
4519 }
4520
NeilBrownc8f517c2009-03-31 15:28:40 +11004521 /* 'writepos' is the most advanced device address we might write.
4522 * 'readpos' is the least advanced device address we might read.
4523 * 'safepos' is the least address recorded in the metadata as having
4524 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004525 * If there is a min_offset_diff, these are adjusted either by
4526 * increasing the safepos/readpos if diff is negative, or
4527 * increasing writepos if diff is positive.
4528 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004529 * ensure safety in the face of a crash - that must be done by userspace
4530 * making a backup of the data. So in that case there is no particular
4531 * rush to update metadata.
4532 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4533 * update the metadata to advance 'safepos' to match 'readpos' so that
4534 * we can be safe in the event of a crash.
4535 * So we insist on updating metadata if safepos is behind writepos and
4536 * readpos is beyond writepos.
4537 * In any case, update the metadata every 10 seconds.
4538 * Maybe that number should be configurable, but I'm not sure it is
4539 * worth it.... maybe it could be a multiple of safemode_delay???
4540 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004541 if (conf->min_offset_diff < 0) {
4542 safepos += -conf->min_offset_diff;
4543 readpos += -conf->min_offset_diff;
4544 } else
4545 writepos += conf->min_offset_diff;
4546
NeilBrown2c810cd2012-05-21 09:27:00 +10004547 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004548 ? (safepos > writepos && readpos < writepos)
4549 : (safepos < writepos && readpos > writepos)) ||
4550 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004551 /* Cannot proceed until we've updated the superblock... */
4552 wait_event(conf->wait_for_overlap,
4553 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004554 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004555 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004556 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004557 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004558 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004559 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004560 kthread_should_stop());
4561 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004562 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004563 spin_unlock_irq(&conf->device_lock);
4564 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004565 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004566 }
4567
NeilBrownab69ae12009-03-31 15:26:47 +11004568 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004569 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004570 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004571 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004572 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004573 set_bit(STRIPE_EXPANDING, &sh->state);
4574 atomic_inc(&conf->reshape_stripes);
4575 /* If any of this stripe is beyond the end of the old
4576 * array, then we need to zero those blocks
4577 */
4578 for (j=sh->disks; j--;) {
4579 sector_t s;
4580 if (j == sh->pd_idx)
4581 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004582 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004583 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004584 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004585 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004586 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004587 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004588 continue;
4589 }
4590 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4591 set_bit(R5_Expanded, &sh->dev[j].flags);
4592 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4593 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004594 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004595 set_bit(STRIPE_EXPAND_READY, &sh->state);
4596 set_bit(STRIPE_HANDLE, &sh->state);
4597 }
NeilBrownab69ae12009-03-31 15:26:47 +11004598 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004599 }
4600 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004601 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004602 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004603 else
NeilBrown7a661382009-03-31 15:21:40 +11004604 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004605 spin_unlock_irq(&conf->device_lock);
4606 /* Ok, those stripe are ready. We can start scheduling
4607 * reads on the source stripes.
4608 * The source stripes are determined by mapping the first and last
4609 * block on the destination stripes.
4610 */
NeilBrown52c03292006-06-26 00:27:43 -07004611 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004612 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004613 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004614 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004615 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004616 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004617 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004618 if (last_sector >= mddev->dev_sectors)
4619 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004620 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004621 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004622 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4623 set_bit(STRIPE_HANDLE, &sh->state);
4624 release_stripe(sh);
4625 first_sector += STRIPE_SECTORS;
4626 }
NeilBrownab69ae12009-03-31 15:26:47 +11004627 /* Now that the sources are clearly marked, we can release
4628 * the destination stripes
4629 */
4630 while (!list_empty(&stripes)) {
4631 sh = list_entry(stripes.next, struct stripe_head, lru);
4632 list_del_init(&sh->lru);
4633 release_stripe(sh);
4634 }
NeilBrownc6207272008-02-06 01:39:52 -08004635 /* If this takes us to the resync_max point where we have to pause,
4636 * then we need to write out the superblock.
4637 */
NeilBrown7a661382009-03-31 15:21:40 +11004638 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004639 if ((sector_nr - mddev->curr_resync_completed) * 2
4640 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004641 /* Cannot proceed until we've updated the superblock... */
4642 wait_event(conf->wait_for_overlap,
4643 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004644 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004645 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004646 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004647 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4648 md_wakeup_thread(mddev->thread);
4649 wait_event(mddev->sb_wait,
4650 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4651 || kthread_should_stop());
4652 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004653 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004654 spin_unlock_irq(&conf->device_lock);
4655 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004656 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004657 }
NeilBrown7a661382009-03-31 15:21:40 +11004658 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004659}
4660
4661/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004662static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004663{
NeilBrownd1688a62011-10-11 16:49:52 +11004664 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004665 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004666 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004667 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004668 int still_degraded = 0;
4669 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
NeilBrown72626682005-09-09 16:23:54 -07004671 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004673
NeilBrown29269552006-03-27 01:18:10 -08004674 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4675 end_reshape(conf);
4676 return 0;
4677 }
NeilBrown72626682005-09-09 16:23:54 -07004678
4679 if (mddev->curr_resync < max_sector) /* aborted */
4680 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4681 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004682 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004683 conf->fullsync = 0;
4684 bitmap_close_sync(mddev->bitmap);
4685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 return 0;
4687 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004688
NeilBrown64bd6602009-08-03 10:59:58 +10004689 /* Allow raid5_quiesce to complete */
4690 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4691
NeilBrown52c03292006-06-26 00:27:43 -07004692 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4693 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004694
NeilBrownc6207272008-02-06 01:39:52 -08004695 /* No need to check resync_max as we never do more than one
4696 * stripe, and as resync_max will always be on a chunk boundary,
4697 * if the check in md_do_sync didn't fire, there is no chance
4698 * of overstepping resync_max here
4699 */
4700
NeilBrown16a53ec2006-06-26 00:27:38 -07004701 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 * to resync, then assert that we are finished, because there is
4703 * nothing we can do.
4704 */
NeilBrown3285edf2006-06-26 00:27:55 -07004705 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004706 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004707 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004708 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 return rv;
4710 }
majianpeng6f608042013-04-24 11:42:41 +10004711 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
4712 !conf->fullsync &&
4713 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
4714 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07004715 /* we can skip this block, and probably more */
4716 sync_blocks /= STRIPE_SECTORS;
4717 *skipped = 1;
4718 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
NeilBrownb47490c2008-02-06 01:39:50 -08004721 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4722
NeilBrowna8c906c2009-06-09 14:39:59 +10004723 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004725 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004727 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004729 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004731 /* Need to check if array will still be degraded after recovery/resync
4732 * We don't need to check the 'failed' flag as when that gets set,
4733 * recovery aborts.
4734 */
NeilBrownf001a702009-06-09 14:30:31 +10004735 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004736 if (conf->disks[i].rdev == NULL)
4737 still_degraded = 1;
4738
4739 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4740
NeilBrown83206d62011-07-26 11:19:49 +10004741 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742
NeilBrown14425772009-10-16 15:55:25 +11004743 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 release_stripe(sh);
4745
4746 return STRIPE_SECTORS;
4747}
4748
NeilBrownd1688a62011-10-11 16:49:52 +11004749static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004750{
4751 /* We may not be able to submit a whole bio at once as there
4752 * may not be enough stripe_heads available.
4753 * We cannot pre-allocate enough stripe_heads as we may need
4754 * more than exist in the cache (if we allow ever large chunks).
4755 * So we do one stripe head at a time and record in
4756 * ->bi_hw_segments how many have been done.
4757 *
4758 * We *know* that this entire raid_bio is in one chunk, so
4759 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4760 */
4761 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004762 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004763 sector_t sector, logical_sector, last_sector;
4764 int scnt = 0;
4765 int remaining;
4766 int handled = 0;
4767
4768 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004769 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004770 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004771 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004772
4773 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004774 logical_sector += STRIPE_SECTORS,
4775 sector += STRIPE_SECTORS,
4776 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004777
Shaohua Lie7836bd62012-07-19 16:01:31 +10004778 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004779 /* already done this stripe */
4780 continue;
4781
NeilBrowna8c906c2009-06-09 14:39:59 +10004782 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004783
4784 if (!sh) {
4785 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004786 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004787 conf->retry_read_aligned = raid_bio;
4788 return handled;
4789 }
4790
Neil Brown387bb172007-02-08 14:20:29 -08004791 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4792 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004793 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004794 conf->retry_read_aligned = raid_bio;
4795 return handled;
4796 }
4797
majianpeng3f9e7c12012-07-31 10:04:21 +10004798 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004799 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004800 release_stripe(sh);
4801 handled++;
4802 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004803 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004804 if (remaining == 0) {
4805 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
4806 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004807 bio_endio(raid_bio, 0);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004808 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004809 if (atomic_dec_and_test(&conf->active_aligned_reads))
4810 wake_up(&conf->wait_for_stripe);
4811 return handled;
4812}
4813
Shaohua Li46a06402012-08-02 08:33:15 +10004814#define MAX_STRIPE_BATCH 8
4815static int handle_active_stripes(struct r5conf *conf)
4816{
4817 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4818 int i, batch_size = 0;
4819
4820 while (batch_size < MAX_STRIPE_BATCH &&
4821 (sh = __get_priority_stripe(conf)) != NULL)
4822 batch[batch_size++] = sh;
4823
4824 if (batch_size == 0)
4825 return batch_size;
4826 spin_unlock_irq(&conf->device_lock);
4827
4828 for (i = 0; i < batch_size; i++)
4829 handle_stripe(batch[i]);
4830
4831 cond_resched();
4832
4833 spin_lock_irq(&conf->device_lock);
4834 for (i = 0; i < batch_size; i++)
4835 __release_stripe(conf, batch[i]);
4836 return batch_size;
4837}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004838
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839/*
4840 * This is our raid5 kernel thread.
4841 *
4842 * We scan the hash table for stripes which can be handled now.
4843 * During the scan, completed stripes are saved for us by the interrupt
4844 * handler, so that they will not have to wait for our next wakeup.
4845 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004846static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847{
Shaohua Li4ed87312012-10-11 13:34:00 +11004848 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004849 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004851 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
Dan Williams45b42332007-07-09 11:56:43 -07004853 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
4855 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004857 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 handled = 0;
4859 spin_lock_irq(&conf->device_lock);
4860 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004861 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004862 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
NeilBrown0021b7b2012-07-31 09:08:14 +02004864 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004865 !list_empty(&conf->bitmap_list)) {
4866 /* Now is a good time to flush some bitmap updates */
4867 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004868 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004869 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004870 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004871 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004872 activate_bit_delay(conf);
4873 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004874 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004875
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004876 while ((bio = remove_bio_from_retry(conf))) {
4877 int ok;
4878 spin_unlock_irq(&conf->device_lock);
4879 ok = retry_aligned_read(conf, bio);
4880 spin_lock_irq(&conf->device_lock);
4881 if (!ok)
4882 break;
4883 handled++;
4884 }
4885
Shaohua Li46a06402012-08-02 08:33:15 +10004886 batch_size = handle_active_stripes(conf);
4887 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004889 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
Shaohua Li46a06402012-08-02 08:33:15 +10004891 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4892 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004893 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004894 spin_lock_irq(&conf->device_lock);
4895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 }
Dan Williams45b42332007-07-09 11:56:43 -07004897 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898
4899 spin_unlock_irq(&conf->device_lock);
4900
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004901 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004902 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903
Dan Williams45b42332007-07-09 11:56:43 -07004904 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905}
4906
NeilBrown3f294f42005-11-08 21:39:25 -08004907static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004908raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004909{
NeilBrownd1688a62011-10-11 16:49:52 +11004910 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004911 if (conf)
4912 return sprintf(page, "%d\n", conf->max_nr_stripes);
4913 else
4914 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004915}
4916
NeilBrownc41d4ac2010-06-01 19:37:24 +10004917int
NeilBrownfd01b882011-10-11 16:47:53 +11004918raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004919{
NeilBrownd1688a62011-10-11 16:49:52 +11004920 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004921 int err;
4922
4923 if (size <= 16 || size > 32768)
4924 return -EINVAL;
4925 while (size < conf->max_nr_stripes) {
4926 if (drop_one_stripe(conf))
4927 conf->max_nr_stripes--;
4928 else
4929 break;
4930 }
4931 err = md_allow_write(mddev);
4932 if (err)
4933 return err;
4934 while (size > conf->max_nr_stripes) {
4935 if (grow_one_stripe(conf))
4936 conf->max_nr_stripes++;
4937 else break;
4938 }
4939 return 0;
4940}
4941EXPORT_SYMBOL(raid5_set_cache_size);
4942
NeilBrown3f294f42005-11-08 21:39:25 -08004943static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004944raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004945{
NeilBrownd1688a62011-10-11 16:49:52 +11004946 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004947 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004948 int err;
4949
NeilBrown3f294f42005-11-08 21:39:25 -08004950 if (len >= PAGE_SIZE)
4951 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004952 if (!conf)
4953 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004954
Dan Williams4ef197d82008-04-28 02:15:54 -07004955 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004956 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004957 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004958 if (err)
4959 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004960 return len;
4961}
NeilBrown007583c2005-11-08 21:39:30 -08004962
NeilBrown96de1e62005-11-08 21:39:39 -08004963static struct md_sysfs_entry
4964raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4965 raid5_show_stripe_cache_size,
4966 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004967
4968static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004969raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004970{
NeilBrownd1688a62011-10-11 16:49:52 +11004971 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004972 if (conf)
4973 return sprintf(page, "%d\n", conf->bypass_threshold);
4974 else
4975 return 0;
4976}
4977
4978static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004979raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004980{
NeilBrownd1688a62011-10-11 16:49:52 +11004981 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004982 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004983 if (len >= PAGE_SIZE)
4984 return -EINVAL;
4985 if (!conf)
4986 return -ENODEV;
4987
Dan Williams4ef197d82008-04-28 02:15:54 -07004988 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004989 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004990 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004991 return -EINVAL;
4992 conf->bypass_threshold = new;
4993 return len;
4994}
4995
4996static struct md_sysfs_entry
4997raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4998 S_IRUGO | S_IWUSR,
4999 raid5_show_preread_threshold,
5000 raid5_store_preread_threshold);
5001
5002static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005003stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005004{
NeilBrownd1688a62011-10-11 16:49:52 +11005005 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005006 if (conf)
5007 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
5008 else
5009 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005010}
5011
NeilBrown96de1e62005-11-08 21:39:39 -08005012static struct md_sysfs_entry
5013raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08005014
NeilBrown007583c2005-11-08 21:39:30 -08005015static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08005016 &raid5_stripecache_size.attr,
5017 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005018 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08005019 NULL,
5020};
NeilBrown007583c2005-11-08 21:39:30 -08005021static struct attribute_group raid5_attrs_group = {
5022 .name = NULL,
5023 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08005024};
5025
Dan Williams80c3a6c2009-03-17 18:10:40 -07005026static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11005027raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07005028{
NeilBrownd1688a62011-10-11 16:49:52 +11005029 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005030
5031 if (!sectors)
5032 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005033 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11005034 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11005035 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005036
Andre Noll9d8f0362009-06-18 08:45:01 +10005037 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10005038 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005039 return sectors * (raid_disks - conf->max_degraded);
5040}
5041
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305042static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
5043{
5044 safe_put_page(percpu->spare_page);
5045 kfree(percpu->scribble);
5046 percpu->spare_page = NULL;
5047 percpu->scribble = NULL;
5048}
5049
5050static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
5051{
5052 if (conf->level == 6 && !percpu->spare_page)
5053 percpu->spare_page = alloc_page(GFP_KERNEL);
5054 if (!percpu->scribble)
5055 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5056
5057 if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) {
5058 free_scratch_buffer(conf, percpu);
5059 return -ENOMEM;
5060 }
5061
5062 return 0;
5063}
5064
NeilBrownd1688a62011-10-11 16:49:52 +11005065static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005066{
Dan Williams36d1c642009-07-14 11:48:22 -07005067 unsigned long cpu;
5068
5069 if (!conf->percpu)
5070 return;
5071
Dan Williams36d1c642009-07-14 11:48:22 -07005072#ifdef CONFIG_HOTPLUG_CPU
5073 unregister_cpu_notifier(&conf->cpu_notify);
5074#endif
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305075
5076 get_online_cpus();
5077 for_each_possible_cpu(cpu)
5078 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07005079 put_online_cpus();
5080
5081 free_percpu(conf->percpu);
5082}
5083
NeilBrownd1688a62011-10-11 16:49:52 +11005084static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10005085{
5086 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07005087 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005088 kfree(conf->disks);
5089 kfree(conf->stripe_hashtbl);
5090 kfree(conf);
5091}
5092
Dan Williams36d1c642009-07-14 11:48:22 -07005093#ifdef CONFIG_HOTPLUG_CPU
5094static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5095 void *hcpu)
5096{
NeilBrownd1688a62011-10-11 16:49:52 +11005097 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005098 long cpu = (long)hcpu;
5099 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5100
5101 switch (action) {
5102 case CPU_UP_PREPARE:
5103 case CPU_UP_PREPARE_FROZEN:
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305104 if (alloc_scratch_buffer(conf, percpu)) {
Dan Williams36d1c642009-07-14 11:48:22 -07005105 pr_err("%s: failed memory allocation for cpu%ld\n",
5106 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005107 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005108 }
5109 break;
5110 case CPU_DEAD:
5111 case CPU_DEAD_FROZEN:
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305112 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
Dan Williams36d1c642009-07-14 11:48:22 -07005113 break;
5114 default:
5115 break;
5116 }
5117 return NOTIFY_OK;
5118}
5119#endif
5120
NeilBrownd1688a62011-10-11 16:49:52 +11005121static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005122{
5123 unsigned long cpu;
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305124 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07005125
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305126 conf->percpu = alloc_percpu(struct raid5_percpu);
5127 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07005128 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07005129
Dan Williams36d1c642009-07-14 11:48:22 -07005130#ifdef CONFIG_HOTPLUG_CPU
5131 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5132 conf->cpu_notify.priority = 0;
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305133 err = register_cpu_notifier(&conf->cpu_notify);
5134 if (err)
5135 return err;
Dan Williams36d1c642009-07-14 11:48:22 -07005136#endif
Oleg Nesterov4d4ef862014-02-06 03:42:45 +05305137
5138 get_online_cpus();
5139 for_each_present_cpu(cpu) {
5140 err = alloc_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
5141 if (err) {
5142 pr_err("%s: failed memory allocation for cpu%ld\n",
5143 __func__, cpu);
5144 break;
5145 }
5146 }
Dan Williams36d1c642009-07-14 11:48:22 -07005147 put_online_cpus();
5148
5149 return err;
5150}
5151
NeilBrownd1688a62011-10-11 16:49:52 +11005152static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153{
NeilBrownd1688a62011-10-11 16:49:52 +11005154 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005155 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005156 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005158 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159
NeilBrown91adb562009-03-31 14:39:39 +11005160 if (mddev->new_level != 5
5161 && mddev->new_level != 4
5162 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005163 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005164 mdname(mddev), mddev->new_level);
5165 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 }
NeilBrown91adb562009-03-31 14:39:39 +11005167 if ((mddev->new_level == 5
5168 && !algorithm_valid_raid5(mddev->new_layout)) ||
5169 (mddev->new_level == 6
5170 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005171 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005172 mdname(mddev), mddev->new_layout);
5173 return ERR_PTR(-EIO);
5174 }
5175 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005176 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005177 mdname(mddev), mddev->raid_disks);
5178 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180
Andre Noll664e7c42009-06-18 08:45:27 +10005181 if (!mddev->new_chunk_sectors ||
5182 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5183 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005184 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5185 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005186 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005187 }
5188
NeilBrownd1688a62011-10-11 16:49:52 +11005189 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005190 if (conf == NULL)
5191 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005192 spin_lock_init(&conf->device_lock);
5193 init_waitqueue_head(&conf->wait_for_stripe);
5194 init_waitqueue_head(&conf->wait_for_overlap);
5195 INIT_LIST_HEAD(&conf->handle_list);
5196 INIT_LIST_HEAD(&conf->hold_list);
5197 INIT_LIST_HEAD(&conf->delayed_list);
5198 INIT_LIST_HEAD(&conf->bitmap_list);
5199 INIT_LIST_HEAD(&conf->inactive_list);
5200 atomic_set(&conf->active_stripes, 0);
5201 atomic_set(&conf->preread_active_stripes, 0);
5202 atomic_set(&conf->active_aligned_reads, 0);
5203 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005204 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005205
5206 conf->raid_disks = mddev->raid_disks;
5207 if (mddev->reshape_position == MaxSector)
5208 conf->previous_raid_disks = mddev->raid_disks;
5209 else
5210 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005211 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5212 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005213
NeilBrown5e5e3e72009-10-16 16:35:30 +11005214 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005215 GFP_KERNEL);
5216 if (!conf->disks)
5217 goto abort;
5218
5219 conf->mddev = mddev;
5220
5221 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5222 goto abort;
5223
Dan Williams36d1c642009-07-14 11:48:22 -07005224 conf->level = mddev->new_level;
5225 if (raid5_alloc_percpu(conf) != 0)
5226 goto abort;
5227
NeilBrown0c55e022010-05-03 14:09:02 +10005228 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005229
NeilBrowndafb20f2012-03-19 12:46:39 +11005230 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005231 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005232 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005233 || raid_disk < 0)
5234 continue;
5235 disk = conf->disks + raid_disk;
5236
NeilBrown17045f52011-12-23 10:17:53 +11005237 if (test_bit(Replacement, &rdev->flags)) {
5238 if (disk->replacement)
5239 goto abort;
5240 disk->replacement = rdev;
5241 } else {
5242 if (disk->rdev)
5243 goto abort;
5244 disk->rdev = rdev;
5245 }
NeilBrown91adb562009-03-31 14:39:39 +11005246
5247 if (test_bit(In_sync, &rdev->flags)) {
5248 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005249 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5250 " disk %d\n",
5251 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005252 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005253 /* Cannot rely on bitmap to complete recovery */
5254 conf->fullsync = 1;
5255 }
5256
Andre Noll09c9e5f2009-06-18 08:45:55 +10005257 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005258 conf->level = mddev->new_level;
5259 if (conf->level == 6)
5260 conf->max_degraded = 2;
5261 else
5262 conf->max_degraded = 1;
5263 conf->algorithm = mddev->new_layout;
5264 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005265 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005266 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005267 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005268 conf->prev_algo = mddev->layout;
5269 }
NeilBrown91adb562009-03-31 14:39:39 +11005270
5271 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005272 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005273 if (grow_stripes(conf, conf->max_nr_stripes)) {
5274 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005275 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5276 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005277 goto abort;
5278 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005279 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5280 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005281
NeilBrown02326052012-07-03 15:56:52 +10005282 sprintf(pers_name, "raid%d", mddev->new_level);
5283 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005284 if (!conf->thread) {
5285 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005286 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005287 mdname(mddev));
5288 goto abort;
5289 }
5290
5291 return conf;
5292
5293 abort:
5294 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005295 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005296 return ERR_PTR(-EIO);
5297 } else
5298 return ERR_PTR(-ENOMEM);
5299}
5300
NeilBrownc148ffd2009-11-13 17:47:00 +11005301
5302static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5303{
5304 switch (algo) {
5305 case ALGORITHM_PARITY_0:
5306 if (raid_disk < max_degraded)
5307 return 1;
5308 break;
5309 case ALGORITHM_PARITY_N:
5310 if (raid_disk >= raid_disks - max_degraded)
5311 return 1;
5312 break;
5313 case ALGORITHM_PARITY_0_6:
5314 if (raid_disk == 0 ||
5315 raid_disk == raid_disks - 1)
5316 return 1;
5317 break;
5318 case ALGORITHM_LEFT_ASYMMETRIC_6:
5319 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5320 case ALGORITHM_LEFT_SYMMETRIC_6:
5321 case ALGORITHM_RIGHT_SYMMETRIC_6:
5322 if (raid_disk == raid_disks - 1)
5323 return 1;
5324 }
5325 return 0;
5326}
5327
NeilBrownfd01b882011-10-11 16:47:53 +11005328static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005329{
NeilBrownd1688a62011-10-11 16:49:52 +11005330 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005331 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005332 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005333 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005334 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005335 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005336 long long min_offset_diff = 0;
5337 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005338
Andre Noll8c6ac862009-06-18 08:48:06 +10005339 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005340 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005341 " -- starting background reconstruction\n",
5342 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005343
5344 rdev_for_each(rdev, mddev) {
5345 long long diff;
5346 if (rdev->raid_disk < 0)
5347 continue;
5348 diff = (rdev->new_data_offset - rdev->data_offset);
5349 if (first) {
5350 min_offset_diff = diff;
5351 first = 0;
5352 } else if (mddev->reshape_backwards &&
5353 diff < min_offset_diff)
5354 min_offset_diff = diff;
5355 else if (!mddev->reshape_backwards &&
5356 diff > min_offset_diff)
5357 min_offset_diff = diff;
5358 }
5359
NeilBrownf6705572006-03-27 01:18:11 -08005360 if (mddev->reshape_position != MaxSector) {
5361 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005362 * Difficulties arise if the stripe we would write to
5363 * next is at or after the stripe we would read from next.
5364 * For a reshape that changes the number of devices, this
5365 * is only possible for a very short time, and mdadm makes
5366 * sure that time appears to have past before assembling
5367 * the array. So we fail if that time hasn't passed.
5368 * For a reshape that keeps the number of devices the same
5369 * mdadm must be monitoring the reshape can keeping the
5370 * critical areas read-only and backed up. It will start
5371 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005372 */
5373 sector_t here_new, here_old;
5374 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005375 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005376
NeilBrown88ce4932009-03-31 15:24:23 +11005377 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005378 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005379 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005380 mdname(mddev));
5381 return -EINVAL;
5382 }
NeilBrownf6705572006-03-27 01:18:11 -08005383 old_disks = mddev->raid_disks - mddev->delta_disks;
5384 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005385 * further up in new geometry must map after here in old
5386 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005387 */
5388 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005389 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005390 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005391 printk(KERN_ERR "md/raid:%s: reshape_position not "
5392 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005393 return -EINVAL;
5394 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005395 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005396 /* here_new is the stripe we will write to */
5397 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005398 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005399 (old_disks-max_degraded));
5400 /* here_old is the first stripe that we might need to read
5401 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005402 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005403 if ((here_new * mddev->new_chunk_sectors !=
5404 here_old * mddev->chunk_sectors)) {
5405 printk(KERN_ERR "md/raid:%s: reshape position is"
5406 " confused - aborting\n", mdname(mddev));
5407 return -EINVAL;
5408 }
NeilBrown67ac6012009-08-13 10:06:24 +10005409 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005410 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005411 * and taking constant backups.
5412 * mdadm always starts a situation like this in
5413 * readonly mode so it can take control before
5414 * allowing any writes. So just check for that.
5415 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005416 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5417 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5418 /* not really in-place - so OK */;
5419 else if (mddev->ro == 0) {
5420 printk(KERN_ERR "md/raid:%s: in-place reshape "
5421 "must be started in read-only mode "
5422 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005423 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005424 return -EINVAL;
5425 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005426 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005427 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005428 here_old * mddev->chunk_sectors)
5429 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005430 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005431 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005432 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5433 "auto-recovery - aborting.\n",
5434 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005435 return -EINVAL;
5436 }
NeilBrown0c55e022010-05-03 14:09:02 +10005437 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5438 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005439 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005440 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005441 BUG_ON(mddev->level != mddev->new_level);
5442 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005443 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005444 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005445 }
5446
NeilBrown245f46c2009-03-31 14:39:39 +11005447 if (mddev->private == NULL)
5448 conf = setup_conf(mddev);
5449 else
5450 conf = mddev->private;
5451
NeilBrown91adb562009-03-31 14:39:39 +11005452 if (IS_ERR(conf))
5453 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005454
NeilBrownb5254dd2012-05-21 09:27:01 +10005455 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005456 mddev->thread = conf->thread;
5457 conf->thread = NULL;
5458 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
NeilBrown17045f52011-12-23 10:17:53 +11005460 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5461 i++) {
5462 rdev = conf->disks[i].rdev;
5463 if (!rdev && conf->disks[i].replacement) {
5464 /* The replacement is all we have yet */
5465 rdev = conf->disks[i].replacement;
5466 conf->disks[i].replacement = NULL;
5467 clear_bit(Replacement, &rdev->flags);
5468 conf->disks[i].rdev = rdev;
5469 }
5470 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005471 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005472 if (conf->disks[i].replacement &&
5473 conf->reshape_progress != MaxSector) {
5474 /* replacements and reshape simply do not mix. */
5475 printk(KERN_ERR "md: cannot handle concurrent "
5476 "replacement and reshape.\n");
5477 goto abort;
5478 }
NeilBrown2f115882010-06-17 17:41:03 +10005479 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005480 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005481 continue;
5482 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005483 /* This disc is not fully in-sync. However if it
5484 * just stored parity (beyond the recovery_offset),
5485 * when we don't need to be concerned about the
5486 * array being dirty.
5487 * When reshape goes 'backwards', we never have
5488 * partially completed devices, so we only need
5489 * to worry about reshape going forwards.
5490 */
5491 /* Hack because v0.91 doesn't store recovery_offset properly. */
5492 if (mddev->major_version == 0 &&
5493 mddev->minor_version > 90)
5494 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005495
NeilBrownc148ffd2009-11-13 17:47:00 +11005496 if (rdev->recovery_offset < reshape_offset) {
5497 /* We need to check old and new layout */
5498 if (!only_parity(rdev->raid_disk,
5499 conf->algorithm,
5500 conf->raid_disks,
5501 conf->max_degraded))
5502 continue;
5503 }
5504 if (!only_parity(rdev->raid_disk,
5505 conf->prev_algo,
5506 conf->previous_raid_disks,
5507 conf->max_degraded))
5508 continue;
5509 dirty_parity_disks++;
5510 }
NeilBrown91adb562009-03-31 14:39:39 +11005511
NeilBrown17045f52011-12-23 10:17:53 +11005512 /*
5513 * 0 for a fully functional array, 1 or 2 for a degraded array.
5514 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005515 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
NeilBrown674806d2010-06-16 17:17:53 +10005517 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005518 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005520 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 goto abort;
5522 }
5523
NeilBrown91adb562009-03-31 14:39:39 +11005524 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005525 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005526 mddev->resync_max_sectors = mddev->dev_sectors;
5527
NeilBrownc148ffd2009-11-13 17:47:00 +11005528 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005530 if (mddev->ok_start_degraded)
5531 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005532 "md/raid:%s: starting dirty degraded array"
5533 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005534 mdname(mddev));
5535 else {
5536 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005537 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005538 mdname(mddev));
5539 goto abort;
5540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 }
5542
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005544 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5545 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005546 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5547 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 else
NeilBrown0c55e022010-05-03 14:09:02 +10005549 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5550 " out of %d devices, algorithm %d\n",
5551 mdname(mddev), conf->level,
5552 mddev->raid_disks - mddev->degraded,
5553 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554
5555 print_raid5_conf(conf);
5556
NeilBrownfef9c612009-03-31 15:16:46 +11005557 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005558 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005559 atomic_set(&conf->reshape_stripes, 0);
5560 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5561 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5562 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5563 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5564 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005565 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005566 }
5567
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568
5569 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005570 if (mddev->to_remove == &raid5_attrs_group)
5571 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005572 else if (mddev->kobj.sd &&
5573 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005574 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005575 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005576 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005577 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5578
5579 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005580 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005581 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005582 /* read-ahead size must cover two whole stripes, which
5583 * is 2 * (datadisks) * chunksize where 'n' is the
5584 * number of raid devices
5585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5587 int stripe = data_disks *
5588 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5589 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5590 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005591
5592 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005593
5594 mddev->queue->backing_dev_info.congested_data = mddev;
5595 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005596
5597 chunk_size = mddev->chunk_sectors << 9;
5598 blk_queue_io_min(mddev->queue, chunk_size);
5599 blk_queue_io_opt(mddev->queue, chunk_size *
5600 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005601 /*
5602 * We can only discard a whole stripe. It doesn't make sense to
5603 * discard data disk but write parity disk
5604 */
5605 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11005606 /* Round up to power of 2, as discard handling
5607 * currently assumes that */
5608 while ((stripe-1) & stripe)
5609 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11005610 mddev->queue->limits.discard_alignment = stripe;
5611 mddev->queue->limits.discard_granularity = stripe;
5612 /*
5613 * unaligned part of discard request will be ignored, so can't
5614 * guarantee discard_zerors_data
5615 */
5616 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005617
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005618 blk_queue_max_write_same_sectors(mddev->queue, 0);
5619
NeilBrown05616be2012-05-21 09:27:00 +10005620 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005621 disk_stack_limits(mddev->gendisk, rdev->bdev,
5622 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005623 disk_stack_limits(mddev->gendisk, rdev->bdev,
5624 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005625 /*
5626 * discard_zeroes_data is required, otherwise data
5627 * could be lost. Consider a scenario: discard a stripe
5628 * (the stripe could be inconsistent if
5629 * discard_zeroes_data is 0); write one disk of the
5630 * stripe (the stripe could be inconsistent again
5631 * depending on which disks are used to calculate
5632 * parity); the disk is broken; The stripe data of this
5633 * disk is lost.
5634 */
5635 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5636 !bdev_get_queue(rdev->bdev)->
5637 limits.discard_zeroes_data)
5638 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10005639 }
Shaohua Li620125f2012-10-11 13:49:05 +11005640
5641 if (discard_supported &&
5642 mddev->queue->limits.max_discard_sectors >= stripe &&
5643 mddev->queue->limits.discard_granularity >= stripe)
5644 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5645 mddev->queue);
5646 else
5647 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5648 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 }
5650
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 return 0;
5652abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005653 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005654 print_raid5_conf(conf);
5655 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005657 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 return -EIO;
5659}
5660
NeilBrownfd01b882011-10-11 16:47:53 +11005661static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662{
NeilBrownd1688a62011-10-11 16:49:52 +11005663 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664
NeilBrown01f96c02011-09-21 15:30:20 +10005665 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005666 if (mddev->queue)
5667 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005668 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005669 mddev->private = NULL;
5670 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 return 0;
5672}
5673
NeilBrownfd01b882011-10-11 16:47:53 +11005674static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675{
NeilBrownd1688a62011-10-11 16:49:52 +11005676 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 int i;
5678
Andre Noll9d8f0362009-06-18 08:45:01 +10005679 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5680 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005681 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 for (i = 0; i < conf->raid_disks; i++)
5683 seq_printf (seq, "%s",
5684 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005685 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687}
5688
NeilBrownd1688a62011-10-11 16:49:52 +11005689static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690{
5691 int i;
5692 struct disk_info *tmp;
5693
NeilBrown0c55e022010-05-03 14:09:02 +10005694 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 if (!conf) {
5696 printk("(conf==NULL)\n");
5697 return;
5698 }
NeilBrown0c55e022010-05-03 14:09:02 +10005699 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5700 conf->raid_disks,
5701 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702
5703 for (i = 0; i < conf->raid_disks; i++) {
5704 char b[BDEVNAME_SIZE];
5705 tmp = conf->disks + i;
5706 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005707 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5708 i, !test_bit(Faulty, &tmp->rdev->flags),
5709 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 }
5711}
5712
NeilBrownfd01b882011-10-11 16:47:53 +11005713static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714{
5715 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005716 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005718 int count = 0;
5719 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
5721 for (i = 0; i < conf->raid_disks; i++) {
5722 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005723 if (tmp->replacement
5724 && tmp->replacement->recovery_offset == MaxSector
5725 && !test_bit(Faulty, &tmp->replacement->flags)
5726 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5727 /* Replacement has just become active. */
5728 if (!tmp->rdev
5729 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5730 count++;
5731 if (tmp->rdev) {
5732 /* Replaced device not technically faulty,
5733 * but we need to be sure it gets removed
5734 * and never re-added.
5735 */
5736 set_bit(Faulty, &tmp->rdev->flags);
5737 sysfs_notify_dirent_safe(
5738 tmp->rdev->sysfs_state);
5739 }
5740 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5741 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005742 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005743 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005744 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005745 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005746 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 }
5748 }
NeilBrown6b965622010-08-18 11:56:59 +10005749 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005750 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005751 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005753 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754}
5755
NeilBrownb8321b62011-12-23 10:17:51 +11005756static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757{
NeilBrownd1688a62011-10-11 16:49:52 +11005758 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005760 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005761 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 struct disk_info *p = conf->disks + number;
5763
5764 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005765 if (rdev == p->rdev)
5766 rdevp = &p->rdev;
5767 else if (rdev == p->replacement)
5768 rdevp = &p->replacement;
5769 else
5770 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005771
NeilBrown657e3e42011-12-23 10:17:52 +11005772 if (number >= conf->raid_disks &&
5773 conf->reshape_progress == MaxSector)
5774 clear_bit(In_sync, &rdev->flags);
5775
5776 if (test_bit(In_sync, &rdev->flags) ||
5777 atomic_read(&rdev->nr_pending)) {
5778 err = -EBUSY;
5779 goto abort;
5780 }
5781 /* Only remove non-faulty devices if recovery
5782 * isn't possible.
5783 */
5784 if (!test_bit(Faulty, &rdev->flags) &&
5785 mddev->recovery_disabled != conf->recovery_disabled &&
5786 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005787 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005788 number < conf->raid_disks) {
5789 err = -EBUSY;
5790 goto abort;
5791 }
5792 *rdevp = NULL;
5793 synchronize_rcu();
5794 if (atomic_read(&rdev->nr_pending)) {
5795 /* lost the race, try later */
5796 err = -EBUSY;
5797 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005798 } else if (p->replacement) {
5799 /* We must have just cleared 'rdev' */
5800 p->rdev = p->replacement;
5801 clear_bit(Replacement, &p->replacement->flags);
5802 smp_mb(); /* Make sure other CPUs may see both as identical
5803 * but will never see neither - if they are careful
5804 */
5805 p->replacement = NULL;
5806 clear_bit(WantReplacement, &rdev->flags);
5807 } else
5808 /* We might have just removed the Replacement as faulty-
5809 * clear the bit just in case
5810 */
5811 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812abort:
5813
5814 print_raid5_conf(conf);
5815 return err;
5816}
5817
NeilBrownfd01b882011-10-11 16:47:53 +11005818static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819{
NeilBrownd1688a62011-10-11 16:49:52 +11005820 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005821 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 int disk;
5823 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005824 int first = 0;
5825 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
NeilBrown7f0da592011-07-28 11:39:22 +10005827 if (mddev->recovery_disabled == conf->recovery_disabled)
5828 return -EBUSY;
5829
NeilBrowndc10c642012-03-19 12:46:37 +11005830 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005832 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
Neil Brown6c2fce22008-06-28 08:31:31 +10005834 if (rdev->raid_disk >= 0)
5835 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836
5837 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005838 * find the disk ... but prefer rdev->saved_raid_disk
5839 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005841 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005842 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005843 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005844 first = rdev->saved_raid_disk;
5845
5846 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005847 p = conf->disks + disk;
5848 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005849 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005851 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005852 if (rdev->saved_raid_disk != disk)
5853 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005854 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005855 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005857 }
5858 for (disk = first; disk <= last; disk++) {
5859 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005860 if (test_bit(WantReplacement, &p->rdev->flags) &&
5861 p->replacement == NULL) {
5862 clear_bit(In_sync, &rdev->flags);
5863 set_bit(Replacement, &rdev->flags);
5864 rdev->raid_disk = disk;
5865 err = 0;
5866 conf->fullsync = 1;
5867 rcu_assign_pointer(p->replacement, rdev);
5868 break;
5869 }
5870 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005871out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005873 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874}
5875
NeilBrownfd01b882011-10-11 16:47:53 +11005876static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877{
5878 /* no resync is happening, and there is enough space
5879 * on all devices, so we can resize.
5880 * We need to make sure resync covers any new space.
5881 * If the array is shrinking we should possibly wait until
5882 * any io in the removed space completes, but it hardly seems
5883 * worth it.
5884 */
NeilBrowna4a61252012-05-22 13:55:27 +10005885 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005886 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005887 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5888 if (mddev->external_size &&
5889 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005890 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005891 if (mddev->bitmap) {
5892 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5893 if (ret)
5894 return ret;
5895 }
5896 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005897 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005898 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005899 if (sectors > mddev->dev_sectors &&
5900 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005901 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5903 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005904 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005905 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 return 0;
5907}
5908
NeilBrownfd01b882011-10-11 16:47:53 +11005909static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005910{
5911 /* Can only proceed if there are plenty of stripe_heads.
5912 * We need a minimum of one full stripe,, and for sensible progress
5913 * it is best to have about 4 times that.
5914 * If we require 4 times, then the default 256 4K stripe_heads will
5915 * allow for chunk sizes up to 256K, which is probably OK.
5916 * If the chunk size is greater, user-space should request more
5917 * stripe_heads first.
5918 */
NeilBrownd1688a62011-10-11 16:49:52 +11005919 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005920 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5921 > conf->max_nr_stripes ||
5922 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5923 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005924 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5925 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005926 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5927 / STRIPE_SIZE)*4);
5928 return 0;
5929 }
5930 return 1;
5931}
5932
NeilBrownfd01b882011-10-11 16:47:53 +11005933static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005934{
NeilBrownd1688a62011-10-11 16:49:52 +11005935 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005936
NeilBrown88ce4932009-03-31 15:24:23 +11005937 if (mddev->delta_disks == 0 &&
5938 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005939 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005940 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005941 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005942 return -EINVAL;
5943 if (mddev->delta_disks < 0) {
5944 /* We might be able to shrink, but the devices must
5945 * be made bigger first.
5946 * For raid6, 4 is the minimum size.
5947 * Otherwise 2 is the minimum
5948 */
5949 int min = 2;
5950 if (mddev->level == 6)
5951 min = 4;
5952 if (mddev->raid_disks + mddev->delta_disks < min)
5953 return -EINVAL;
5954 }
NeilBrown29269552006-03-27 01:18:10 -08005955
NeilBrown01ee22b2009-06-18 08:47:20 +10005956 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005957 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005958
NeilBrowne56108d62012-10-11 14:24:13 +11005959 return resize_stripes(conf, (conf->previous_raid_disks
5960 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08005961}
5962
NeilBrownfd01b882011-10-11 16:47:53 +11005963static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005964{
NeilBrownd1688a62011-10-11 16:49:52 +11005965 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005966 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005967 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005968 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005969
NeilBrownf4168852007-02-28 20:11:53 -08005970 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005971 return -EBUSY;
5972
NeilBrown01ee22b2009-06-18 08:47:20 +10005973 if (!check_stripe_cache(mddev))
5974 return -ENOSPC;
5975
NeilBrown30b67642012-05-22 13:55:28 +10005976 if (has_failed(conf))
5977 return -EINVAL;
5978
NeilBrownc6563a82012-05-21 09:27:00 +10005979 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005980 if (!test_bit(In_sync, &rdev->flags)
5981 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005982 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005983 }
NeilBrown63c70c42006-03-27 01:18:13 -08005984
NeilBrownf4168852007-02-28 20:11:53 -08005985 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005986 /* Not enough devices even to make a degraded array
5987 * of that size
5988 */
5989 return -EINVAL;
5990
NeilBrownec32a2b2009-03-31 15:17:38 +11005991 /* Refuse to reduce size of the array. Any reductions in
5992 * array size must be through explicit setting of array_size
5993 * attribute.
5994 */
5995 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5996 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005997 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005998 "before number of disks\n", mdname(mddev));
5999 return -EINVAL;
6000 }
6001
NeilBrownf6705572006-03-27 01:18:11 -08006002 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08006003 spin_lock_irq(&conf->device_lock);
6004 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08006005 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006006 conf->prev_chunk_sectors = conf->chunk_sectors;
6007 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11006008 conf->prev_algo = conf->algorithm;
6009 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10006010 conf->generation++;
6011 /* Code that selects data_offset needs to see the generation update
6012 * if reshape_progress has been set - so a memory barrier needed.
6013 */
6014 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10006015 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11006016 conf->reshape_progress = raid5_size(mddev, 0, 0);
6017 else
6018 conf->reshape_progress = 0;
6019 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08006020 spin_unlock_irq(&conf->device_lock);
6021
6022 /* Add some new drives, as many as will fit.
6023 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10006024 * Don't add devices if we are reducing the number of
6025 * devices in the array. This is because it is not possible
6026 * to correctly record the "partially reconstructed" state of
6027 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08006028 */
NeilBrown87a8dec2011-01-31 11:57:43 +11006029 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11006030 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11006031 if (rdev->raid_disk < 0 &&
6032 !test_bit(Faulty, &rdev->flags)) {
6033 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11006034 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11006035 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11006036 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11006037 else
NeilBrown87a8dec2011-01-31 11:57:43 +11006038 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10006039
6040 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11006041 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11006042 }
NeilBrown87a8dec2011-01-31 11:57:43 +11006043 } else if (rdev->raid_disk >= conf->previous_raid_disks
6044 && !test_bit(Faulty, &rdev->flags)) {
6045 /* This is a spare that was manually added */
6046 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11006047 }
NeilBrown29269552006-03-27 01:18:10 -08006048
NeilBrown87a8dec2011-01-31 11:57:43 +11006049 /* When a reshape changes the number of devices,
6050 * ->degraded is measured against the larger of the
6051 * pre and post number of devices.
6052 */
NeilBrownec32a2b2009-03-31 15:17:38 +11006053 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006054 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11006055 spin_unlock_irqrestore(&conf->device_lock, flags);
6056 }
NeilBrown63c70c42006-03-27 01:18:13 -08006057 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10006058 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07006059 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08006060
NeilBrown29269552006-03-27 01:18:10 -08006061 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6062 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6063 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6064 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6065 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006066 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08006067 if (!mddev->sync_thread) {
6068 mddev->recovery = 0;
6069 spin_lock_irq(&conf->device_lock);
6070 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006071 rdev_for_each(rdev, mddev)
6072 rdev->new_data_offset = rdev->data_offset;
6073 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006074 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11006075 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08006076 spin_unlock_irq(&conf->device_lock);
6077 return -EAGAIN;
6078 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006079 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006080 md_wakeup_thread(mddev->sync_thread);
6081 md_new_event(mddev);
6082 return 0;
6083}
NeilBrown29269552006-03-27 01:18:10 -08006084
NeilBrownec32a2b2009-03-31 15:17:38 +11006085/* This is called from the reshape thread and should make any
6086 * changes needed in 'conf'
6087 */
NeilBrownd1688a62011-10-11 16:49:52 +11006088static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006089{
NeilBrown29269552006-03-27 01:18:10 -08006090
NeilBrownf6705572006-03-27 01:18:11 -08006091 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006092 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006093
NeilBrownf6705572006-03-27 01:18:11 -08006094 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006095 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006096 rdev_for_each(rdev, conf->mddev)
6097 rdev->data_offset = rdev->new_data_offset;
6098 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006099 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006100 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006101 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006102
6103 /* read-ahead size must cover two whole stripes, which is
6104 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6105 */
NeilBrown4a5add42010-06-01 19:37:28 +10006106 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006107 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006108 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006109 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006110 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6111 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6112 }
NeilBrown29269552006-03-27 01:18:10 -08006113 }
NeilBrown29269552006-03-27 01:18:10 -08006114}
6115
NeilBrownec32a2b2009-03-31 15:17:38 +11006116/* This is called from the raid5d thread with mddev_lock held.
6117 * It makes config changes to the device.
6118 */
NeilBrownfd01b882011-10-11 16:47:53 +11006119static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006120{
NeilBrownd1688a62011-10-11 16:49:52 +11006121 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006122
6123 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6124
NeilBrownec32a2b2009-03-31 15:17:38 +11006125 if (mddev->delta_disks > 0) {
6126 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6127 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006128 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006129 } else {
6130 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006131 spin_lock_irq(&conf->device_lock);
6132 mddev->degraded = calc_degraded(conf);
6133 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006134 for (d = conf->raid_disks ;
6135 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006136 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006137 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006138 if (rdev)
6139 clear_bit(In_sync, &rdev->flags);
6140 rdev = conf->disks[d].replacement;
6141 if (rdev)
6142 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006143 }
NeilBrowncea9c222009-03-31 15:15:05 +11006144 }
NeilBrown88ce4932009-03-31 15:24:23 +11006145 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006146 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006147 mddev->reshape_position = MaxSector;
6148 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006149 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006150 }
6151}
6152
NeilBrownfd01b882011-10-11 16:47:53 +11006153static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006154{
NeilBrownd1688a62011-10-11 16:49:52 +11006155 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006156
6157 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006158 case 2: /* resume for a suspend */
6159 wake_up(&conf->wait_for_overlap);
6160 break;
6161
NeilBrown72626682005-09-09 16:23:54 -07006162 case 1: /* stop all writes */
6163 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006164 /* '2' tells resync/reshape to pause so that all
6165 * active stripes can drain
6166 */
6167 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006168 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006169 atomic_read(&conf->active_stripes) == 0 &&
6170 atomic_read(&conf->active_aligned_reads) == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01006171 conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006172 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006173 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006174 /* allow reshape to continue */
6175 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006176 break;
6177
6178 case 0: /* re-enable writes */
6179 spin_lock_irq(&conf->device_lock);
6180 conf->quiesce = 0;
6181 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006182 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006183 spin_unlock_irq(&conf->device_lock);
6184 break;
6185 }
NeilBrown72626682005-09-09 16:23:54 -07006186}
NeilBrownb15c2e52006-01-06 00:20:16 -08006187
NeilBrownd562b0c2009-03-31 14:39:39 +11006188
NeilBrownfd01b882011-10-11 16:47:53 +11006189static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006190{
NeilBrowne373ab12011-10-11 16:48:59 +11006191 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006192 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006193
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006194 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006195 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006196 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6197 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006198 return ERR_PTR(-EINVAL);
6199 }
6200
NeilBrowne373ab12011-10-11 16:48:59 +11006201 sectors = raid0_conf->strip_zone[0].zone_end;
6202 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006203 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006204 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006205 mddev->new_layout = ALGORITHM_PARITY_N;
6206 mddev->new_chunk_sectors = mddev->chunk_sectors;
6207 mddev->raid_disks += 1;
6208 mddev->delta_disks = 1;
6209 /* make sure it will be not marked as dirty */
6210 mddev->recovery_cp = MaxSector;
6211
6212 return setup_conf(mddev);
6213}
6214
6215
NeilBrownfd01b882011-10-11 16:47:53 +11006216static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006217{
6218 int chunksect;
6219
6220 if (mddev->raid_disks != 2 ||
6221 mddev->degraded > 1)
6222 return ERR_PTR(-EINVAL);
6223
6224 /* Should check if there are write-behind devices? */
6225
6226 chunksect = 64*2; /* 64K by default */
6227
6228 /* The array must be an exact multiple of chunksize */
6229 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6230 chunksect >>= 1;
6231
6232 if ((chunksect<<9) < STRIPE_SIZE)
6233 /* array size does not allow a suitable chunk size */
6234 return ERR_PTR(-EINVAL);
6235
6236 mddev->new_level = 5;
6237 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006238 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006239
6240 return setup_conf(mddev);
6241}
6242
NeilBrownfd01b882011-10-11 16:47:53 +11006243static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006244{
6245 int new_layout;
6246
6247 switch (mddev->layout) {
6248 case ALGORITHM_LEFT_ASYMMETRIC_6:
6249 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6250 break;
6251 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6252 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6253 break;
6254 case ALGORITHM_LEFT_SYMMETRIC_6:
6255 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6256 break;
6257 case ALGORITHM_RIGHT_SYMMETRIC_6:
6258 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6259 break;
6260 case ALGORITHM_PARITY_0_6:
6261 new_layout = ALGORITHM_PARITY_0;
6262 break;
6263 case ALGORITHM_PARITY_N:
6264 new_layout = ALGORITHM_PARITY_N;
6265 break;
6266 default:
6267 return ERR_PTR(-EINVAL);
6268 }
6269 mddev->new_level = 5;
6270 mddev->new_layout = new_layout;
6271 mddev->delta_disks = -1;
6272 mddev->raid_disks -= 1;
6273 return setup_conf(mddev);
6274}
6275
NeilBrownd562b0c2009-03-31 14:39:39 +11006276
NeilBrownfd01b882011-10-11 16:47:53 +11006277static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006278{
NeilBrown88ce4932009-03-31 15:24:23 +11006279 /* For a 2-drive array, the layout and chunk size can be changed
6280 * immediately as not restriping is needed.
6281 * For larger arrays we record the new value - after validation
6282 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006283 */
NeilBrownd1688a62011-10-11 16:49:52 +11006284 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006285 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006286
NeilBrown597a7112009-06-18 08:47:42 +10006287 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006288 return -EINVAL;
6289 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006290 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006291 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006292 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006293 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006294 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006295 /* not factor of array size */
6296 return -EINVAL;
6297 }
6298
6299 /* They look valid */
6300
NeilBrown88ce4932009-03-31 15:24:23 +11006301 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006302 /* can make the change immediately */
6303 if (mddev->new_layout >= 0) {
6304 conf->algorithm = mddev->new_layout;
6305 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006306 }
6307 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006308 conf->chunk_sectors = new_chunk ;
6309 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006310 }
6311 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6312 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006313 }
NeilBrown50ac1682009-06-18 08:47:55 +10006314 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006315}
6316
NeilBrownfd01b882011-10-11 16:47:53 +11006317static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006318{
NeilBrown597a7112009-06-18 08:47:42 +10006319 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006320
NeilBrown597a7112009-06-18 08:47:42 +10006321 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006322 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006323 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006324 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006325 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006326 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006327 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006328 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006329 /* not factor of array size */
6330 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006331 }
NeilBrown88ce4932009-03-31 15:24:23 +11006332
6333 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006334 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006335}
6336
NeilBrownfd01b882011-10-11 16:47:53 +11006337static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006338{
6339 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006340 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006341 * raid1 - if there are two drives. We need to know the chunk size
6342 * raid4 - trivial - just use a raid4 layout.
6343 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006344 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006345 if (mddev->level == 0)
6346 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006347 if (mddev->level == 1)
6348 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006349 if (mddev->level == 4) {
6350 mddev->new_layout = ALGORITHM_PARITY_N;
6351 mddev->new_level = 5;
6352 return setup_conf(mddev);
6353 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006354 if (mddev->level == 6)
6355 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006356
6357 return ERR_PTR(-EINVAL);
6358}
6359
NeilBrownfd01b882011-10-11 16:47:53 +11006360static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006361{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006362 /* raid4 can take over:
6363 * raid0 - if there is only one strip zone
6364 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006365 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006366 if (mddev->level == 0)
6367 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006368 if (mddev->level == 5 &&
6369 mddev->layout == ALGORITHM_PARITY_N) {
6370 mddev->new_layout = 0;
6371 mddev->new_level = 4;
6372 return setup_conf(mddev);
6373 }
6374 return ERR_PTR(-EINVAL);
6375}
NeilBrownd562b0c2009-03-31 14:39:39 +11006376
NeilBrown84fc4b52011-10-11 16:49:58 +11006377static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006378
NeilBrownfd01b882011-10-11 16:47:53 +11006379static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006380{
6381 /* Currently can only take over a raid5. We map the
6382 * personality to an equivalent raid6 personality
6383 * with the Q block at the end.
6384 */
6385 int new_layout;
6386
6387 if (mddev->pers != &raid5_personality)
6388 return ERR_PTR(-EINVAL);
6389 if (mddev->degraded > 1)
6390 return ERR_PTR(-EINVAL);
6391 if (mddev->raid_disks > 253)
6392 return ERR_PTR(-EINVAL);
6393 if (mddev->raid_disks < 3)
6394 return ERR_PTR(-EINVAL);
6395
6396 switch (mddev->layout) {
6397 case ALGORITHM_LEFT_ASYMMETRIC:
6398 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6399 break;
6400 case ALGORITHM_RIGHT_ASYMMETRIC:
6401 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6402 break;
6403 case ALGORITHM_LEFT_SYMMETRIC:
6404 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6405 break;
6406 case ALGORITHM_RIGHT_SYMMETRIC:
6407 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6408 break;
6409 case ALGORITHM_PARITY_0:
6410 new_layout = ALGORITHM_PARITY_0_6;
6411 break;
6412 case ALGORITHM_PARITY_N:
6413 new_layout = ALGORITHM_PARITY_N;
6414 break;
6415 default:
6416 return ERR_PTR(-EINVAL);
6417 }
6418 mddev->new_level = 6;
6419 mddev->new_layout = new_layout;
6420 mddev->delta_disks = 1;
6421 mddev->raid_disks += 1;
6422 return setup_conf(mddev);
6423}
6424
6425
NeilBrown84fc4b52011-10-11 16:49:58 +11006426static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006427{
6428 .name = "raid6",
6429 .level = 6,
6430 .owner = THIS_MODULE,
6431 .make_request = make_request,
6432 .run = run,
6433 .stop = stop,
6434 .status = status,
6435 .error_handler = error,
6436 .hot_add_disk = raid5_add_disk,
6437 .hot_remove_disk= raid5_remove_disk,
6438 .spare_active = raid5_spare_active,
6439 .sync_request = sync_request,
6440 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006441 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006442 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006443 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006444 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006445 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006446 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006447};
NeilBrown84fc4b52011-10-11 16:49:58 +11006448static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449{
6450 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006451 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 .owner = THIS_MODULE,
6453 .make_request = make_request,
6454 .run = run,
6455 .stop = stop,
6456 .status = status,
6457 .error_handler = error,
6458 .hot_add_disk = raid5_add_disk,
6459 .hot_remove_disk= raid5_remove_disk,
6460 .spare_active = raid5_spare_active,
6461 .sync_request = sync_request,
6462 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006463 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006464 .check_reshape = raid5_check_reshape,
6465 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006466 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006467 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006468 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469};
6470
NeilBrown84fc4b52011-10-11 16:49:58 +11006471static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472{
NeilBrown2604b702006-01-06 00:20:36 -08006473 .name = "raid4",
6474 .level = 4,
6475 .owner = THIS_MODULE,
6476 .make_request = make_request,
6477 .run = run,
6478 .stop = stop,
6479 .status = status,
6480 .error_handler = error,
6481 .hot_add_disk = raid5_add_disk,
6482 .hot_remove_disk= raid5_remove_disk,
6483 .spare_active = raid5_spare_active,
6484 .sync_request = sync_request,
6485 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006486 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006487 .check_reshape = raid5_check_reshape,
6488 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006489 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006490 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006491 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006492};
6493
6494static int __init raid5_init(void)
6495{
NeilBrown16a53ec2006-06-26 00:27:38 -07006496 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006497 register_md_personality(&raid5_personality);
6498 register_md_personality(&raid4_personality);
6499 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500}
6501
NeilBrown2604b702006-01-06 00:20:36 -08006502static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503{
NeilBrown16a53ec2006-06-26 00:27:38 -07006504 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006505 unregister_md_personality(&raid5_personality);
6506 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507}
6508
6509module_init(raid5_init);
6510module_exit(raid5_exit);
6511MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006512MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006514MODULE_ALIAS("md-raid5");
6515MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006516MODULE_ALIAS("md-level-5");
6517MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006518MODULE_ALIAS("md-personality-8"); /* RAID6 */
6519MODULE_ALIAS("md-raid6");
6520MODULE_ALIAS("md-level-6");
6521
6522/* This used to be two separate modules, they were: */
6523MODULE_ALIAS("raid5");
6524MODULE_ALIAS("raid6");