blob: 4bed5454b8dc100b7fe47a60e0b48f6d2c504270 [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) {
1896 set_bit(WriteErrorSeen, &rdev->flags);
1897 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001898 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1899 set_bit(MD_RECOVERY_NEEDED,
1900 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001901 } else if (is_badblock(rdev, sh->sector,
1902 STRIPE_SECTORS,
NeilBrownc0b32972013-04-24 11:42:42 +10001903 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11001904 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10001905 if (test_bit(R5_ReadError, &sh->dev[i].flags))
1906 /* That was a successful write so make
1907 * sure it looks like we already did
1908 * a re-write.
1909 */
1910 set_bit(R5_ReWrite, &sh->dev[i].flags);
1911 }
NeilBrown977df362011-12-23 10:17:53 +11001912 }
1913 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
NeilBrown977df362011-12-23 10:17:53 +11001915 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1916 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001918 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
1920
NeilBrown784052e2009-03-31 15:19:07 +11001921static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
NeilBrown784052e2009-03-31 15:19:07 +11001923static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 struct r5dev *dev = &sh->dev[i];
1926
1927 bio_init(&dev->req);
1928 dev->req.bi_io_vec = &dev->vec;
1929 dev->req.bi_vcnt++;
1930 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001932 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
NeilBrown977df362011-12-23 10:17:53 +11001934 bio_init(&dev->rreq);
1935 dev->rreq.bi_io_vec = &dev->rvec;
1936 dev->rreq.bi_vcnt++;
1937 dev->rreq.bi_max_vecs++;
1938 dev->rreq.bi_private = sh;
1939 dev->rvec.bv_page = dev->page;
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001942 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
NeilBrownfd01b882011-10-11 16:47:53 +11001945static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
1947 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001948 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001949 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001950 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
NeilBrown908f4fb2011-12-23 10:17:50 +11001952 spin_lock_irqsave(&conf->device_lock, flags);
1953 clear_bit(In_sync, &rdev->flags);
1954 mddev->degraded = calc_degraded(conf);
1955 spin_unlock_irqrestore(&conf->device_lock, flags);
1956 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1957
NeilBrownde393cd2011-07-28 11:31:48 +10001958 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001959 set_bit(Faulty, &rdev->flags);
1960 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1961 printk(KERN_ALERT
1962 "md/raid:%s: Disk failure on %s, disabling device.\n"
1963 "md/raid:%s: Operation continuing on %d devices.\n",
1964 mdname(mddev),
1965 bdevname(rdev->bdev, b),
1966 mdname(mddev),
1967 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001968}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970/*
1971 * Input: a 'big' sector number,
1972 * Output: index of the data and parity disk, and the sector # in them.
1973 */
NeilBrownd1688a62011-10-11 16:49:52 +11001974static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001975 int previous, int *dd_idx,
1976 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001978 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001979 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001981 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001982 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001984 int algorithm = previous ? conf->prev_algo
1985 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001986 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1987 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001988 int raid_disks = previous ? conf->previous_raid_disks
1989 : conf->raid_disks;
1990 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 /* First compute the information on this sector */
1993
1994 /*
1995 * Compute the chunk number and the sector offset inside the chunk
1996 */
1997 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1998 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 /*
2001 * Compute the stripe number
2002 */
NeilBrown35f2a592010-04-20 14:13:34 +10002003 stripe = chunk_number;
2004 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002005 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 /*
2007 * Select the parity disk based on the user selected algorithm.
2008 */
NeilBrown84789552011-07-27 11:00:36 +10002009 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002010 switch(conf->level) {
2011 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002012 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002013 break;
2014 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002015 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002017 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002018 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 (*dd_idx)++;
2020 break;
2021 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002022 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002023 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 (*dd_idx)++;
2025 break;
2026 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002027 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002028 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 break;
2030 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002031 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002032 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002034 case ALGORITHM_PARITY_0:
2035 pd_idx = 0;
2036 (*dd_idx)++;
2037 break;
2038 case ALGORITHM_PARITY_N:
2039 pd_idx = data_disks;
2040 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002042 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002043 }
2044 break;
2045 case 6:
2046
NeilBrowne183eae2009-03-31 15:20:22 +11002047 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002048 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002049 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002050 qd_idx = pd_idx + 1;
2051 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002052 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002053 qd_idx = 0;
2054 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002055 (*dd_idx) += 2; /* D D P Q D */
2056 break;
2057 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002058 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002059 qd_idx = pd_idx + 1;
2060 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002061 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002062 qd_idx = 0;
2063 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002064 (*dd_idx) += 2; /* D D P Q D */
2065 break;
2066 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002067 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002068 qd_idx = (pd_idx + 1) % raid_disks;
2069 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002070 break;
2071 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002072 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002073 qd_idx = (pd_idx + 1) % raid_disks;
2074 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002075 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002076
2077 case ALGORITHM_PARITY_0:
2078 pd_idx = 0;
2079 qd_idx = 1;
2080 (*dd_idx) += 2;
2081 break;
2082 case ALGORITHM_PARITY_N:
2083 pd_idx = data_disks;
2084 qd_idx = data_disks + 1;
2085 break;
2086
2087 case ALGORITHM_ROTATING_ZERO_RESTART:
2088 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2089 * of blocks for computing Q is different.
2090 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002091 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002092 qd_idx = pd_idx + 1;
2093 if (pd_idx == raid_disks-1) {
2094 (*dd_idx)++; /* Q D D D P */
2095 qd_idx = 0;
2096 } else if (*dd_idx >= pd_idx)
2097 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002098 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002099 break;
2100
2101 case ALGORITHM_ROTATING_N_RESTART:
2102 /* Same a left_asymmetric, by first stripe is
2103 * D D D P Q rather than
2104 * Q D D D P
2105 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002106 stripe2 += 1;
2107 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002108 qd_idx = pd_idx + 1;
2109 if (pd_idx == raid_disks-1) {
2110 (*dd_idx)++; /* Q D D D P */
2111 qd_idx = 0;
2112 } else if (*dd_idx >= pd_idx)
2113 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002114 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002115 break;
2116
2117 case ALGORITHM_ROTATING_N_CONTINUE:
2118 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002119 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002120 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2121 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002122 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002123 break;
2124
2125 case ALGORITHM_LEFT_ASYMMETRIC_6:
2126 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002127 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002128 if (*dd_idx >= pd_idx)
2129 (*dd_idx)++;
2130 qd_idx = raid_disks - 1;
2131 break;
2132
2133 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002134 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002135 if (*dd_idx >= pd_idx)
2136 (*dd_idx)++;
2137 qd_idx = raid_disks - 1;
2138 break;
2139
2140 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002141 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002142 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2143 qd_idx = raid_disks - 1;
2144 break;
2145
2146 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002147 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002148 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2149 qd_idx = raid_disks - 1;
2150 break;
2151
2152 case ALGORITHM_PARITY_0_6:
2153 pd_idx = 0;
2154 (*dd_idx)++;
2155 qd_idx = raid_disks - 1;
2156 break;
2157
NeilBrown16a53ec2006-06-26 00:27:38 -07002158 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002159 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002160 }
2161 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163
NeilBrown911d4ee2009-03-31 14:39:38 +11002164 if (sh) {
2165 sh->pd_idx = pd_idx;
2166 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002167 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 /*
2170 * Finally, compute the new sector number
2171 */
2172 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2173 return new_sector;
2174}
2175
2176
NeilBrown784052e2009-03-31 15:19:07 +11002177static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
NeilBrownd1688a62011-10-11 16:49:52 +11002179 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002180 int raid_disks = sh->disks;
2181 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002183 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2184 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002185 int algorithm = previous ? conf->prev_algo
2186 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 sector_t stripe;
2188 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002189 sector_t chunk_number;
2190 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002192 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
NeilBrown16a53ec2006-06-26 00:27:38 -07002194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2196 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
NeilBrown16a53ec2006-06-26 00:27:38 -07002198 if (i == sh->pd_idx)
2199 return 0;
2200 switch(conf->level) {
2201 case 4: break;
2202 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002203 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 case ALGORITHM_LEFT_ASYMMETRIC:
2205 case ALGORITHM_RIGHT_ASYMMETRIC:
2206 if (i > sh->pd_idx)
2207 i--;
2208 break;
2209 case ALGORITHM_LEFT_SYMMETRIC:
2210 case ALGORITHM_RIGHT_SYMMETRIC:
2211 if (i < sh->pd_idx)
2212 i += raid_disks;
2213 i -= (sh->pd_idx + 1);
2214 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002215 case ALGORITHM_PARITY_0:
2216 i -= 1;
2217 break;
2218 case ALGORITHM_PARITY_N:
2219 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002221 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002222 }
2223 break;
2224 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002225 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002226 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002227 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002228 case ALGORITHM_LEFT_ASYMMETRIC:
2229 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002230 case ALGORITHM_ROTATING_ZERO_RESTART:
2231 case ALGORITHM_ROTATING_N_RESTART:
2232 if (sh->pd_idx == raid_disks-1)
2233 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002234 else if (i > sh->pd_idx)
2235 i -= 2; /* D D P Q D */
2236 break;
2237 case ALGORITHM_LEFT_SYMMETRIC:
2238 case ALGORITHM_RIGHT_SYMMETRIC:
2239 if (sh->pd_idx == raid_disks-1)
2240 i--; /* Q D D D P */
2241 else {
2242 /* D D P Q D */
2243 if (i < sh->pd_idx)
2244 i += raid_disks;
2245 i -= (sh->pd_idx + 2);
2246 }
2247 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002248 case ALGORITHM_PARITY_0:
2249 i -= 2;
2250 break;
2251 case ALGORITHM_PARITY_N:
2252 break;
2253 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002254 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002255 if (sh->pd_idx == 0)
2256 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002257 else {
2258 /* D D Q P D */
2259 if (i < sh->pd_idx)
2260 i += raid_disks;
2261 i -= (sh->pd_idx + 1);
2262 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002263 break;
2264 case ALGORITHM_LEFT_ASYMMETRIC_6:
2265 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2266 if (i > sh->pd_idx)
2267 i--;
2268 break;
2269 case ALGORITHM_LEFT_SYMMETRIC_6:
2270 case ALGORITHM_RIGHT_SYMMETRIC_6:
2271 if (i < sh->pd_idx)
2272 i += data_disks + 1;
2273 i -= (sh->pd_idx + 1);
2274 break;
2275 case ALGORITHM_PARITY_0_6:
2276 i -= 1;
2277 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002278 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002279 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002280 }
2281 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
2283
2284 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002285 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
NeilBrown112bf892009-03-31 14:39:38 +11002287 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002288 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002289 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2290 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002291 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2292 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return 0;
2294 }
2295 return r_sector;
2296}
2297
2298
Dan Williams600aa102008-06-28 08:32:05 +10002299static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002300schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002301 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002302{
2303 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002304 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002305 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002306
Dan Williamse33129d2007-01-02 13:52:30 -07002307 if (rcw) {
Dan Williamse33129d2007-01-02 13:52:30 -07002308
2309 for (i = disks; i--; ) {
2310 struct r5dev *dev = &sh->dev[i];
2311
2312 if (dev->towrite) {
2313 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002314 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002315 if (!expand)
2316 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002317 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002318 }
2319 }
NeilBrownce7d3632013-03-04 12:37:14 +11002320 /* if we are not expanding this is a proper write request, and
2321 * there will be bios with new data to be drained into the
2322 * stripe cache
2323 */
2324 if (!expand) {
2325 if (!s->locked)
2326 /* False alarm, nothing to do */
2327 return;
2328 sh->reconstruct_state = reconstruct_state_drain_run;
2329 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2330 } else
2331 sh->reconstruct_state = reconstruct_state_run;
2332
2333 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2334
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002335 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002336 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002337 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002338 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002339 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002340 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2341 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2342
Dan Williamse33129d2007-01-02 13:52:30 -07002343 for (i = disks; i--; ) {
2344 struct r5dev *dev = &sh->dev[i];
2345 if (i == pd_idx)
2346 continue;
2347
Dan Williamse33129d2007-01-02 13:52:30 -07002348 if (dev->towrite &&
2349 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002350 test_bit(R5_Wantcompute, &dev->flags))) {
2351 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002352 set_bit(R5_LOCKED, &dev->flags);
2353 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002354 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002355 }
2356 }
NeilBrownce7d3632013-03-04 12:37:14 +11002357 if (!s->locked)
2358 /* False alarm - nothing to do */
2359 return;
2360 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2361 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2362 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2363 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002364 }
2365
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002366 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002367 * are in flight
2368 */
2369 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2370 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002371 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002372
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002373 if (level == 6) {
2374 int qd_idx = sh->qd_idx;
2375 struct r5dev *dev = &sh->dev[qd_idx];
2376
2377 set_bit(R5_LOCKED, &dev->flags);
2378 clear_bit(R5_UPTODATE, &dev->flags);
2379 s->locked++;
2380 }
2381
Dan Williams600aa102008-06-28 08:32:05 +10002382 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002383 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002384 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002385}
NeilBrown16a53ec2006-06-26 00:27:38 -07002386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387/*
2388 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002389 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 * The bi_next chain must be in order.
2391 */
2392static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2393{
2394 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002395 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002396 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
NeilBrowncbe47ec2011-07-26 11:20:35 +10002398 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 (unsigned long long)bi->bi_sector,
2400 (unsigned long long)sh->sector);
2401
Shaohua Lib17459c2012-07-19 16:01:31 +10002402 /*
2403 * If several bio share a stripe. The bio bi_phys_segments acts as a
2404 * reference count to avoid race. The reference count should already be
2405 * increased before this function is called (for example, in
2406 * make_request()), so other bio sharing this stripe will not free the
2407 * stripe. If a stripe is owned by one stripe, the stripe lock will
2408 * protect it.
2409 */
2410 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002411 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002413 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002414 firstwrite = 1;
2415 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 bip = &sh->dev[dd_idx].toread;
2417 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002418 if (bio_end_sector(*bip) > bi->bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 goto overlap;
2420 bip = & (*bip)->bi_next;
2421 }
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002422 if (*bip && (*bip)->bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 goto overlap;
2424
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002425 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (*bip)
2427 bi->bi_next = *bip;
2428 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002429 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (forwrite) {
2432 /* check if page is covered */
2433 sector_t sector = sh->dev[dd_idx].sector;
2434 for (bi=sh->dev[dd_idx].towrite;
2435 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2436 bi && bi->bi_sector <= sector;
2437 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002438 if (bio_end_sector(bi) >= sector)
2439 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 }
2441 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2442 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2443 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002444
2445 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2446 (unsigned long long)(*bip)->bi_sector,
2447 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002448 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002449
2450 if (conf->mddev->bitmap && firstwrite) {
2451 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2452 STRIPE_SECTORS, 0);
2453 sh->bm_seq = conf->seq_flush+1;
2454 set_bit(STRIPE_BIT_DELAY, &sh->state);
2455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return 1;
2457
2458 overlap:
2459 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002460 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 return 0;
2462}
2463
NeilBrownd1688a62011-10-11 16:49:52 +11002464static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002465
NeilBrownd1688a62011-10-11 16:49:52 +11002466static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002467 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002468{
NeilBrown784052e2009-03-31 15:19:07 +11002469 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002470 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002471 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002472 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002473 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002474
NeilBrown112bf892009-03-31 14:39:38 +11002475 raid5_compute_sector(conf,
2476 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002477 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002478 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002479 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002480}
2481
Dan Williamsa4456852007-07-09 11:56:43 -07002482static void
NeilBrownd1688a62011-10-11 16:49:52 +11002483handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002484 struct stripe_head_state *s, int disks,
2485 struct bio **return_bi)
2486{
2487 int i;
2488 for (i = disks; i--; ) {
2489 struct bio *bi;
2490 int bitmap_end = 0;
2491
2492 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002493 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002494 rcu_read_lock();
2495 rdev = rcu_dereference(conf->disks[i].rdev);
2496 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002497 atomic_inc(&rdev->nr_pending);
2498 else
2499 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002500 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002501 if (rdev) {
2502 if (!rdev_set_badblocks(
2503 rdev,
2504 sh->sector,
2505 STRIPE_SECTORS, 0))
2506 md_error(conf->mddev, rdev);
2507 rdev_dec_pending(rdev, conf->mddev);
2508 }
Dan Williamsa4456852007-07-09 11:56:43 -07002509 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002510 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002511 /* fail all writes first */
2512 bi = sh->dev[i].towrite;
2513 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002514 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002515 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002516 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002517
2518 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2519 wake_up(&conf->wait_for_overlap);
2520
2521 while (bi && bi->bi_sector <
2522 sh->dev[i].sector + STRIPE_SECTORS) {
2523 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2524 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002525 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002526 md_write_end(conf->mddev);
2527 bi->bi_next = *return_bi;
2528 *return_bi = bi;
2529 }
2530 bi = nextbi;
2531 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002532 if (bitmap_end)
2533 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2534 STRIPE_SECTORS, 0, 0);
2535 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002536 /* and fail all 'written' */
2537 bi = sh->dev[i].written;
2538 sh->dev[i].written = NULL;
2539 if (bi) bitmap_end = 1;
2540 while (bi && bi->bi_sector <
2541 sh->dev[i].sector + STRIPE_SECTORS) {
2542 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2543 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002544 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002545 md_write_end(conf->mddev);
2546 bi->bi_next = *return_bi;
2547 *return_bi = bi;
2548 }
2549 bi = bi2;
2550 }
2551
Dan Williamsb5e98d62007-01-02 13:52:31 -07002552 /* fail any reads if this device is non-operational and
2553 * the data has not reached the cache yet.
2554 */
2555 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2556 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2557 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002558 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002559 bi = sh->dev[i].toread;
2560 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002561 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002562 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2563 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002564 while (bi && bi->bi_sector <
2565 sh->dev[i].sector + STRIPE_SECTORS) {
2566 struct bio *nextbi =
2567 r5_next_bio(bi, sh->dev[i].sector);
2568 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002569 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002570 bi->bi_next = *return_bi;
2571 *return_bi = bi;
2572 }
2573 bi = nextbi;
2574 }
2575 }
Dan Williamsa4456852007-07-09 11:56:43 -07002576 if (bitmap_end)
2577 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2578 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002579 /* If we were in the middle of a write the parity block might
2580 * still be locked - so just clear all R5_LOCKED flags
2581 */
2582 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002583 }
2584
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002585 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2586 if (atomic_dec_and_test(&conf->pending_full_writes))
2587 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002588}
2589
NeilBrown7f0da592011-07-28 11:39:22 +10002590static void
NeilBrownd1688a62011-10-11 16:49:52 +11002591handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002592 struct stripe_head_state *s)
2593{
2594 int abort = 0;
2595 int i;
2596
NeilBrown7f0da592011-07-28 11:39:22 +10002597 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002598 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
2599 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10002600 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002601 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002602 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002603 * Don't even need to abort as that is handled elsewhere
2604 * if needed, and not always wanted e.g. if there is a known
2605 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002606 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002607 * non-sync devices, or abort the recovery
2608 */
NeilBrown18b98372012-04-01 23:48:38 +10002609 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2610 /* During recovery devices cannot be removed, so
2611 * locking and refcounting of rdevs is not needed
2612 */
2613 for (i = 0; i < conf->raid_disks; i++) {
2614 struct md_rdev *rdev = conf->disks[i].rdev;
2615 if (rdev
2616 && !test_bit(Faulty, &rdev->flags)
2617 && !test_bit(In_sync, &rdev->flags)
2618 && !rdev_set_badblocks(rdev, sh->sector,
2619 STRIPE_SECTORS, 0))
2620 abort = 1;
2621 rdev = conf->disks[i].replacement;
2622 if (rdev
2623 && !test_bit(Faulty, &rdev->flags)
2624 && !test_bit(In_sync, &rdev->flags)
2625 && !rdev_set_badblocks(rdev, sh->sector,
2626 STRIPE_SECTORS, 0))
2627 abort = 1;
2628 }
2629 if (abort)
2630 conf->recovery_disabled =
2631 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002632 }
NeilBrown18b98372012-04-01 23:48:38 +10002633 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002634}
2635
NeilBrown9a3e1102011-12-23 10:17:53 +11002636static int want_replace(struct stripe_head *sh, int disk_idx)
2637{
2638 struct md_rdev *rdev;
2639 int rv = 0;
2640 /* Doing recovery so rcu locking not required */
2641 rdev = sh->raid_conf->disks[disk_idx].replacement;
2642 if (rdev
2643 && !test_bit(Faulty, &rdev->flags)
2644 && !test_bit(In_sync, &rdev->flags)
2645 && (rdev->recovery_offset <= sh->sector
2646 || rdev->mddev->recovery_cp <= sh->sector))
2647 rv = 1;
2648
2649 return rv;
2650}
2651
NeilBrown93b3dbc2011-07-27 11:00:36 +10002652/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002653 * to be read or computed to satisfy a request.
2654 *
2655 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002656 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002657 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002658static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2659 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002660{
2661 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002662 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2663 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002664
Dan Williamsf38e1212007-01-02 13:52:30 -07002665 /* is the data in this block needed, and can we get it? */
2666 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002667 !test_bit(R5_UPTODATE, &dev->flags) &&
2668 (dev->toread ||
2669 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2670 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002671 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002672 (s->failed >= 1 && fdev[0]->toread) ||
2673 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002674 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2675 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2676 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002677 /* we would like to get this block, possibly by computing it,
2678 * otherwise read it if the backing disk is insync
2679 */
2680 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2681 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2682 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002683 (s->failed && (disk_idx == s->failed_num[0] ||
2684 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002685 /* have disk failed, and we're requested to fetch it;
2686 * do compute it
2687 */
2688 pr_debug("Computing stripe %llu block %d\n",
2689 (unsigned long long)sh->sector, disk_idx);
2690 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2691 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2692 set_bit(R5_Wantcompute, &dev->flags);
2693 sh->ops.target = disk_idx;
2694 sh->ops.target2 = -1; /* no 2nd target */
2695 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002696 /* Careful: from this point on 'uptodate' is in the eye
2697 * of raid_run_ops which services 'compute' operations
2698 * before writes. R5_Wantcompute flags a block that will
2699 * be R5_UPTODATE by the time it is needed for a
2700 * subsequent operation.
2701 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002702 s->uptodate++;
2703 return 1;
2704 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2705 /* Computing 2-failure is *very* expensive; only
2706 * do it if failed >= 2
2707 */
2708 int other;
2709 for (other = disks; other--; ) {
2710 if (other == disk_idx)
2711 continue;
2712 if (!test_bit(R5_UPTODATE,
2713 &sh->dev[other].flags))
2714 break;
2715 }
2716 BUG_ON(other < 0);
2717 pr_debug("Computing stripe %llu blocks %d,%d\n",
2718 (unsigned long long)sh->sector,
2719 disk_idx, other);
2720 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2721 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2722 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2723 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2724 sh->ops.target = disk_idx;
2725 sh->ops.target2 = other;
2726 s->uptodate += 2;
2727 s->req_compute = 1;
2728 return 1;
2729 } else if (test_bit(R5_Insync, &dev->flags)) {
2730 set_bit(R5_LOCKED, &dev->flags);
2731 set_bit(R5_Wantread, &dev->flags);
2732 s->locked++;
2733 pr_debug("Reading block %d (sync=%d)\n",
2734 disk_idx, s->syncing);
2735 }
2736 }
2737
2738 return 0;
2739}
2740
2741/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002742 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002743 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002744static void handle_stripe_fill(struct stripe_head *sh,
2745 struct stripe_head_state *s,
2746 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002747{
2748 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002749
2750 /* look for blocks to read/compute, skip this if a compute
2751 * is already in flight, or if the stripe contents are in the
2752 * midst of changing due to a write
2753 */
2754 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2755 !sh->reconstruct_state)
2756 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002757 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002758 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002759 set_bit(STRIPE_HANDLE, &sh->state);
2760}
2761
2762
Dan Williams1fe797e2008-06-28 09:16:30 +10002763/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002764 * any written block on an uptodate or failed drive can be returned.
2765 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2766 * never LOCKED, so we don't need to test 'failed' directly.
2767 */
NeilBrownd1688a62011-10-11 16:49:52 +11002768static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002769 struct stripe_head *sh, int disks, struct bio **return_bi)
2770{
2771 int i;
2772 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11002773 int discard_pending = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002774
2775 for (i = disks; i--; )
2776 if (sh->dev[i].written) {
2777 dev = &sh->dev[i];
2778 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11002779 (test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownca64cae2012-11-21 16:33:40 +11002780 test_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002781 /* We can return any write requests */
2782 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002783 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11002784 if (test_and_clear_bit(R5_Discard, &dev->flags))
2785 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002786 wbi = dev->written;
2787 dev->written = NULL;
2788 while (wbi && wbi->bi_sector <
2789 dev->sector + STRIPE_SECTORS) {
2790 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002791 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002792 md_write_end(conf->mddev);
2793 wbi->bi_next = *return_bi;
2794 *return_bi = wbi;
2795 }
2796 wbi = wbi2;
2797 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002798 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2799 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002800 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002801 0);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002802 } else if (test_bit(R5_Discard, &dev->flags))
2803 discard_pending = 1;
2804 }
2805 if (!discard_pending &&
2806 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
2807 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
2808 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
2809 if (sh->qd_idx >= 0) {
2810 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
2811 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
2812 }
2813 /* now that discard is done we can proceed with any sync */
2814 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li01e608d2013-10-19 14:51:42 +08002815 /*
2816 * SCSI discard will change some bio fields and the stripe has
2817 * no updated data, so remove it from hash list and the stripe
2818 * will be reinitialized
2819 */
2820 spin_lock_irq(&conf->device_lock);
2821 remove_hash(sh);
2822 spin_unlock_irq(&conf->device_lock);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002823 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
2824 set_bit(STRIPE_HANDLE, &sh->state);
2825
2826 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002827
2828 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2829 if (atomic_dec_and_test(&conf->pending_full_writes))
2830 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002831}
2832
NeilBrownd1688a62011-10-11 16:49:52 +11002833static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002834 struct stripe_head *sh,
2835 struct stripe_head_state *s,
2836 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002837{
2838 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002839 sector_t recovery_cp = conf->mddev->recovery_cp;
2840
2841 /* RAID6 requires 'rcw' in current implementation.
2842 * Otherwise, check whether resync is now happening or should start.
2843 * If yes, then the array is dirty (after unclean shutdown or
2844 * initial creation), so parity in some stripes might be inconsistent.
2845 * In this case, we need to always do reconstruct-write, to ensure
2846 * that in case of drive failure or read-error correction, we
2847 * generate correct data from the parity.
2848 */
2849 if (conf->max_degraded == 2 ||
2850 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2851 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002852 * look like rcw is cheaper
2853 */
2854 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002855 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2856 conf->max_degraded, (unsigned long long)recovery_cp,
2857 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002858 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002859 /* would I have to read this buffer for read_modify_write */
2860 struct r5dev *dev = &sh->dev[i];
2861 if ((dev->towrite || i == sh->pd_idx) &&
2862 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002863 !(test_bit(R5_UPTODATE, &dev->flags) ||
2864 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002865 if (test_bit(R5_Insync, &dev->flags))
2866 rmw++;
2867 else
2868 rmw += 2*disks; /* cannot read it */
2869 }
2870 /* Would I have to read this buffer for reconstruct_write */
2871 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2872 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002873 !(test_bit(R5_UPTODATE, &dev->flags) ||
2874 test_bit(R5_Wantcompute, &dev->flags))) {
2875 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002876 else
2877 rcw += 2*disks;
2878 }
2879 }
Dan Williams45b42332007-07-09 11:56:43 -07002880 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002881 (unsigned long long)sh->sector, rmw, rcw);
2882 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11002883 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002884 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06002885 if (conf->mddev->queue)
2886 blk_add_trace_msg(conf->mddev->queue,
2887 "raid5 rmw %llu %d",
2888 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07002889 for (i = disks; i--; ) {
2890 struct r5dev *dev = &sh->dev[i];
2891 if ((dev->towrite || i == sh->pd_idx) &&
2892 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002893 !(test_bit(R5_UPTODATE, &dev->flags) ||
2894 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002895 test_bit(R5_Insync, &dev->flags)) {
2896 if (
2897 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002898 pr_debug("Read_old block "
NeilBrowna9add5d2012-10-31 11:59:09 +11002899 "%d for r-m-w\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002900 set_bit(R5_LOCKED, &dev->flags);
2901 set_bit(R5_Wantread, &dev->flags);
2902 s->locked++;
2903 } else {
2904 set_bit(STRIPE_DELAYED, &sh->state);
2905 set_bit(STRIPE_HANDLE, &sh->state);
2906 }
2907 }
2908 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002909 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002910 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002911 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002912 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10002913 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002914 for (i = disks; i--; ) {
2915 struct r5dev *dev = &sh->dev[i];
2916 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002917 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002918 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002919 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002920 test_bit(R5_Wantcompute, &dev->flags))) {
2921 rcw++;
2922 if (!test_bit(R5_Insync, &dev->flags))
2923 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002924 if (
2925 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002926 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002927 "%d for Reconstruct\n", i);
2928 set_bit(R5_LOCKED, &dev->flags);
2929 set_bit(R5_Wantread, &dev->flags);
2930 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11002931 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07002932 } else {
2933 set_bit(STRIPE_DELAYED, &sh->state);
2934 set_bit(STRIPE_HANDLE, &sh->state);
2935 }
2936 }
2937 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06002938 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11002939 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
2940 (unsigned long long)sh->sector,
2941 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10002942 }
Dan Williamsa4456852007-07-09 11:56:43 -07002943 /* now if nothing is locked, and if we have enough data,
2944 * we can start a write request
2945 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002946 /* since handle_stripe can be called at any time we need to handle the
2947 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002948 * subsequent call wants to start a write request. raid_run_ops only
2949 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002950 * simultaneously. If this is not the case then new writes need to be
2951 * held off until the compute completes.
2952 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002953 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2954 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2955 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002956 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002957}
2958
NeilBrownd1688a62011-10-11 16:49:52 +11002959static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002960 struct stripe_head_state *s, int disks)
2961{
Dan Williamsecc65c92008-06-28 08:31:57 +10002962 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002963
Dan Williamsbd2ab672008-04-10 21:29:27 -07002964 set_bit(STRIPE_HANDLE, &sh->state);
2965
Dan Williamsecc65c92008-06-28 08:31:57 +10002966 switch (sh->check_state) {
2967 case check_state_idle:
2968 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002969 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002970 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002971 sh->check_state = check_state_run;
2972 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002973 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002974 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002975 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002976 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002977 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002978 /* fall through */
2979 case check_state_compute_result:
2980 sh->check_state = check_state_idle;
2981 if (!dev)
2982 dev = &sh->dev[sh->pd_idx];
2983
2984 /* check that a write has not made the stripe insync */
2985 if (test_bit(STRIPE_INSYNC, &sh->state))
2986 break;
2987
2988 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002989 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2990 BUG_ON(s->uptodate != disks);
2991
2992 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002993 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002994 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002995
Dan Williamsa4456852007-07-09 11:56:43 -07002996 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002997 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002998 break;
2999 case check_state_run:
3000 break; /* we will be called again upon completion */
3001 case check_state_check_result:
3002 sh->check_state = check_state_idle;
3003
3004 /* if a failure occurred during the check operation, leave
3005 * STRIPE_INSYNC not set and let the stripe be handled again
3006 */
3007 if (s->failed)
3008 break;
3009
3010 /* handle a successful check operation, if parity is correct
3011 * we are done. Otherwise update the mismatch count and repair
3012 * parity if !MD_RECOVERY_CHECK
3013 */
Dan Williamsad283ea2009-08-29 19:09:26 -07003014 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10003015 /* parity is correct (on disc,
3016 * not in buffer any more)
3017 */
3018 set_bit(STRIPE_INSYNC, &sh->state);
3019 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003020 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003021 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3022 /* don't try to repair!! */
3023 set_bit(STRIPE_INSYNC, &sh->state);
3024 else {
3025 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003026 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003027 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3028 set_bit(R5_Wantcompute,
3029 &sh->dev[sh->pd_idx].flags);
3030 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003031 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003032 s->uptodate++;
3033 }
3034 }
3035 break;
3036 case check_state_compute_run:
3037 break;
3038 default:
3039 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3040 __func__, sh->check_state,
3041 (unsigned long long) sh->sector);
3042 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003043 }
3044}
3045
3046
NeilBrownd1688a62011-10-11 16:49:52 +11003047static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003048 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003049 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003050{
Dan Williamsa4456852007-07-09 11:56:43 -07003051 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003052 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003053 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003054
3055 set_bit(STRIPE_HANDLE, &sh->state);
3056
3057 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003058
Dan Williamsa4456852007-07-09 11:56:43 -07003059 /* Want to check and possibly repair P and Q.
3060 * However there could be one 'failed' device, in which
3061 * case we can only check one of them, possibly using the
3062 * other to generate missing data
3063 */
3064
Dan Williamsd82dfee2009-07-14 13:40:57 -07003065 switch (sh->check_state) {
3066 case check_state_idle:
3067 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003068 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003069 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003070 * makes sense to check P (If anything else were failed,
3071 * we would have used P to recreate it).
3072 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003073 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003074 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003075 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003076 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003077 * anything, so it makes sense to check it
3078 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003079 if (sh->check_state == check_state_run)
3080 sh->check_state = check_state_run_pq;
3081 else
3082 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003083 }
Dan Williams36d1c642009-07-14 11:48:22 -07003084
Dan Williamsd82dfee2009-07-14 13:40:57 -07003085 /* discard potentially stale zero_sum_result */
3086 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003087
Dan Williamsd82dfee2009-07-14 13:40:57 -07003088 if (sh->check_state == check_state_run) {
3089 /* async_xor_zero_sum destroys the contents of P */
3090 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3091 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003092 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003093 if (sh->check_state >= check_state_run &&
3094 sh->check_state <= check_state_run_pq) {
3095 /* async_syndrome_zero_sum preserves P and Q, so
3096 * no need to mark them !uptodate here
3097 */
3098 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3099 break;
3100 }
Dan Williams36d1c642009-07-14 11:48:22 -07003101
Dan Williamsd82dfee2009-07-14 13:40:57 -07003102 /* we have 2-disk failure */
3103 BUG_ON(s->failed != 2);
3104 /* fall through */
3105 case check_state_compute_result:
3106 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003107
Dan Williamsd82dfee2009-07-14 13:40:57 -07003108 /* check that a write has not made the stripe insync */
3109 if (test_bit(STRIPE_INSYNC, &sh->state))
3110 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003111
3112 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003113 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003114 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003115 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003116 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003117 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003118 s->locked++;
3119 set_bit(R5_LOCKED, &dev->flags);
3120 set_bit(R5_Wantwrite, &dev->flags);
3121 }
3122 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003123 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003124 s->locked++;
3125 set_bit(R5_LOCKED, &dev->flags);
3126 set_bit(R5_Wantwrite, &dev->flags);
3127 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003128 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003129 dev = &sh->dev[pd_idx];
3130 s->locked++;
3131 set_bit(R5_LOCKED, &dev->flags);
3132 set_bit(R5_Wantwrite, &dev->flags);
3133 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003134 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003135 dev = &sh->dev[qd_idx];
3136 s->locked++;
3137 set_bit(R5_LOCKED, &dev->flags);
3138 set_bit(R5_Wantwrite, &dev->flags);
3139 }
3140 clear_bit(STRIPE_DEGRADED, &sh->state);
3141
3142 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003143 break;
3144 case check_state_run:
3145 case check_state_run_q:
3146 case check_state_run_pq:
3147 break; /* we will be called again upon completion */
3148 case check_state_check_result:
3149 sh->check_state = check_state_idle;
3150
3151 /* handle a successful check operation, if parity is correct
3152 * we are done. Otherwise update the mismatch count and repair
3153 * parity if !MD_RECOVERY_CHECK
3154 */
3155 if (sh->ops.zero_sum_result == 0) {
3156 /* both parities are correct */
3157 if (!s->failed)
3158 set_bit(STRIPE_INSYNC, &sh->state);
3159 else {
3160 /* in contrast to the raid5 case we can validate
3161 * parity, but still have a failure to write
3162 * back
3163 */
3164 sh->check_state = check_state_compute_result;
3165 /* Returning at this point means that we may go
3166 * off and bring p and/or q uptodate again so
3167 * we make sure to check zero_sum_result again
3168 * to verify if p or q need writeback
3169 */
3170 }
3171 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003172 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003173 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3174 /* don't try to repair!! */
3175 set_bit(STRIPE_INSYNC, &sh->state);
3176 else {
3177 int *target = &sh->ops.target;
3178
3179 sh->ops.target = -1;
3180 sh->ops.target2 = -1;
3181 sh->check_state = check_state_compute_run;
3182 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3183 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3184 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3185 set_bit(R5_Wantcompute,
3186 &sh->dev[pd_idx].flags);
3187 *target = pd_idx;
3188 target = &sh->ops.target2;
3189 s->uptodate++;
3190 }
3191 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3192 set_bit(R5_Wantcompute,
3193 &sh->dev[qd_idx].flags);
3194 *target = qd_idx;
3195 s->uptodate++;
3196 }
3197 }
3198 }
3199 break;
3200 case check_state_compute_run:
3201 break;
3202 default:
3203 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3204 __func__, sh->check_state,
3205 (unsigned long long) sh->sector);
3206 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003207 }
3208}
3209
NeilBrownd1688a62011-10-11 16:49:52 +11003210static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003211{
3212 int i;
3213
3214 /* We have read all the blocks in this stripe and now we need to
3215 * copy some of them into a target stripe for expand.
3216 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003217 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003218 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3219 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003220 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003221 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003222 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003223 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003224
NeilBrown784052e2009-03-31 15:19:07 +11003225 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003226 sector_t s = raid5_compute_sector(conf, bn, 0,
3227 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003228 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003229 if (sh2 == NULL)
3230 /* so far only the early blocks of this stripe
3231 * have been requested. When later blocks
3232 * get requested, we will try again
3233 */
3234 continue;
3235 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3236 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3237 /* must have already done this block */
3238 release_stripe(sh2);
3239 continue;
3240 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003241
3242 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003243 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003244 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003245 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003246 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003247
Dan Williamsa4456852007-07-09 11:56:43 -07003248 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3249 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3250 for (j = 0; j < conf->raid_disks; j++)
3251 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003252 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003253 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3254 break;
3255 if (j == conf->raid_disks) {
3256 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3257 set_bit(STRIPE_HANDLE, &sh2->state);
3258 }
3259 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003260
Dan Williamsa4456852007-07-09 11:56:43 -07003261 }
NeilBrowna2e08552007-09-11 15:23:36 -07003262 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003263 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003264}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
3266/*
3267 * handle_stripe - do things to a stripe.
3268 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003269 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3270 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003272 * return some read requests which now have data
3273 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 * schedule a read on some buffers
3275 * schedule a write of some buffers
3276 * return confirmation of parity correctness
3277 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 */
Dan Williamsa4456852007-07-09 11:56:43 -07003279
NeilBrownacfe7262011-07-27 11:00:36 +10003280static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003281{
NeilBrownd1688a62011-10-11 16:49:52 +11003282 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003283 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003284 struct r5dev *dev;
3285 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003286 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003287
NeilBrownacfe7262011-07-27 11:00:36 +10003288 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003289
NeilBrownacfe7262011-07-27 11:00:36 +10003290 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3291 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3292 s->failed_num[0] = -1;
3293 s->failed_num[1] = -1;
3294
3295 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003296 rcu_read_lock();
3297 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003298 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003299 sector_t first_bad;
3300 int bad_sectors;
3301 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003302
NeilBrown16a53ec2006-06-26 00:27:38 -07003303 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003304
Dan Williams45b42332007-07-09 11:56:43 -07003305 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003306 i, dev->flags,
3307 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003308 /* maybe we can reply to a read
3309 *
3310 * new wantfill requests are only permitted while
3311 * ops_complete_biofill is guaranteed to be inactive
3312 */
3313 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3314 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3315 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003316
3317 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003318 if (test_bit(R5_LOCKED, &dev->flags))
3319 s->locked++;
3320 if (test_bit(R5_UPTODATE, &dev->flags))
3321 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003322 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003323 s->compute++;
3324 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003325 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003326
NeilBrownacfe7262011-07-27 11:00:36 +10003327 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003328 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003329 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003330 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003331 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003332 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003333 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003334 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003335 }
Dan Williamsa4456852007-07-09 11:56:43 -07003336 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003337 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003338 /* Prefer to use the replacement for reads, but only
3339 * if it is recovered enough and has no bad blocks.
3340 */
3341 rdev = rcu_dereference(conf->disks[i].replacement);
3342 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3343 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3344 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3345 &first_bad, &bad_sectors))
3346 set_bit(R5_ReadRepl, &dev->flags);
3347 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003348 if (rdev)
3349 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003350 rdev = rcu_dereference(conf->disks[i].rdev);
3351 clear_bit(R5_ReadRepl, &dev->flags);
3352 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003353 if (rdev && test_bit(Faulty, &rdev->flags))
3354 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003355 if (rdev) {
3356 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3357 &first_bad, &bad_sectors);
3358 if (s->blocked_rdev == NULL
3359 && (test_bit(Blocked, &rdev->flags)
3360 || is_bad < 0)) {
3361 if (is_bad < 0)
3362 set_bit(BlockedBadBlocks,
3363 &rdev->flags);
3364 s->blocked_rdev = rdev;
3365 atomic_inc(&rdev->nr_pending);
3366 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003367 }
NeilBrown415e72d2010-06-17 17:25:21 +10003368 clear_bit(R5_Insync, &dev->flags);
3369 if (!rdev)
3370 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003371 else if (is_bad) {
3372 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003373 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3374 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003375 /* treat as in-sync, but with a read error
3376 * which we can now try to correct
3377 */
3378 set_bit(R5_Insync, &dev->flags);
3379 set_bit(R5_ReadError, &dev->flags);
3380 }
3381 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003382 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003383 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003384 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003385 set_bit(R5_Insync, &dev->flags);
3386 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3387 test_bit(R5_Expanded, &dev->flags))
3388 /* If we've reshaped into here, we assume it is Insync.
3389 * We will shortly update recovery_offset to make
3390 * it official.
3391 */
3392 set_bit(R5_Insync, &dev->flags);
3393
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003394 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003395 /* This flag does not apply to '.replacement'
3396 * only to .rdev, so make sure to check that*/
3397 struct md_rdev *rdev2 = rcu_dereference(
3398 conf->disks[i].rdev);
3399 if (rdev2 == rdev)
3400 clear_bit(R5_Insync, &dev->flags);
3401 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003402 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003403 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003404 } else
3405 clear_bit(R5_WriteError, &dev->flags);
3406 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003407 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003408 /* This flag does not apply to '.replacement'
3409 * only to .rdev, so make sure to check that*/
3410 struct md_rdev *rdev2 = rcu_dereference(
3411 conf->disks[i].rdev);
3412 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003413 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003414 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003415 } else
3416 clear_bit(R5_MadeGood, &dev->flags);
3417 }
NeilBrown977df362011-12-23 10:17:53 +11003418 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3419 struct md_rdev *rdev2 = rcu_dereference(
3420 conf->disks[i].replacement);
3421 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3422 s->handle_bad_blocks = 1;
3423 atomic_inc(&rdev2->nr_pending);
3424 } else
3425 clear_bit(R5_MadeGoodRepl, &dev->flags);
3426 }
NeilBrown415e72d2010-06-17 17:25:21 +10003427 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003428 /* The ReadError flag will just be confusing now */
3429 clear_bit(R5_ReadError, &dev->flags);
3430 clear_bit(R5_ReWrite, &dev->flags);
3431 }
NeilBrown415e72d2010-06-17 17:25:21 +10003432 if (test_bit(R5_ReadError, &dev->flags))
3433 clear_bit(R5_Insync, &dev->flags);
3434 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003435 if (s->failed < 2)
3436 s->failed_num[s->failed] = i;
3437 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003438 if (rdev && !test_bit(Faulty, &rdev->flags))
3439 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003440 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003441 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003442 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3443 /* If there is a failed device being replaced,
3444 * we must be recovering.
3445 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003446 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003447 * else we can only be replacing
3448 * sync and recovery both need to read all devices, and so
3449 * use the same flag.
3450 */
3451 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003452 sh->sector >= conf->mddev->recovery_cp ||
3453 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003454 s->syncing = 1;
3455 else
3456 s->replacing = 1;
3457 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003458 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003459}
NeilBrownf4168852007-02-28 20:11:53 -08003460
NeilBrowncc940152011-07-26 11:35:35 +10003461static void handle_stripe(struct stripe_head *sh)
3462{
3463 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003464 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003465 int i;
NeilBrown84789552011-07-27 11:00:36 +10003466 int prexor;
3467 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003468 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003469
3470 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003471 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003472 /* already being handled, ensure it gets handled
3473 * again when current action finishes */
3474 set_bit(STRIPE_HANDLE, &sh->state);
3475 return;
3476 }
3477
NeilBrownf8dfcff2013-03-12 12:18:06 +11003478 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3479 spin_lock(&sh->stripe_lock);
3480 /* Cannot process 'sync' concurrently with 'discard' */
3481 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
3482 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3483 set_bit(STRIPE_SYNCING, &sh->state);
3484 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownc1dadcc2013-07-22 12:57:21 +10003485 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003486 }
3487 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10003488 }
3489 clear_bit(STRIPE_DELAYED, &sh->state);
3490
3491 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3492 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3493 (unsigned long long)sh->sector, sh->state,
3494 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3495 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003496
NeilBrownacfe7262011-07-27 11:00:36 +10003497 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003498
NeilBrownbc2607f2011-07-28 11:39:22 +10003499 if (s.handle_bad_blocks) {
3500 set_bit(STRIPE_HANDLE, &sh->state);
3501 goto finish;
3502 }
3503
NeilBrown474af965fe2011-07-27 11:00:36 +10003504 if (unlikely(s.blocked_rdev)) {
3505 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003506 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003507 set_bit(STRIPE_HANDLE, &sh->state);
3508 goto finish;
3509 }
3510 /* There is nothing for the blocked_rdev to block */
3511 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3512 s.blocked_rdev = NULL;
3513 }
3514
3515 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3516 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3517 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3518 }
3519
3520 pr_debug("locked=%d uptodate=%d to_read=%d"
3521 " to_write=%d failed=%d failed_num=%d,%d\n",
3522 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3523 s.failed_num[0], s.failed_num[1]);
3524 /* check if the array has lost more than max_degraded devices and,
3525 * if so, some requests might need to be failed.
3526 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003527 if (s.failed > conf->max_degraded) {
3528 sh->check_state = 0;
3529 sh->reconstruct_state = 0;
3530 if (s.to_read+s.to_write+s.written)
3531 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003532 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003533 handle_failed_sync(conf, sh, &s);
3534 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003535
NeilBrown84789552011-07-27 11:00:36 +10003536 /* Now we check to see if any write operations have recently
3537 * completed
3538 */
3539 prexor = 0;
3540 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3541 prexor = 1;
3542 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3543 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3544 sh->reconstruct_state = reconstruct_state_idle;
3545
3546 /* All the 'written' buffers and the parity block are ready to
3547 * be written back to disk
3548 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003549 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3550 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003551 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003552 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3553 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003554 for (i = disks; i--; ) {
3555 struct r5dev *dev = &sh->dev[i];
3556 if (test_bit(R5_LOCKED, &dev->flags) &&
3557 (i == sh->pd_idx || i == sh->qd_idx ||
3558 dev->written)) {
3559 pr_debug("Writing block %d\n", i);
3560 set_bit(R5_Wantwrite, &dev->flags);
3561 if (prexor)
3562 continue;
3563 if (!test_bit(R5_Insync, &dev->flags) ||
3564 ((i == sh->pd_idx || i == sh->qd_idx) &&
3565 s.failed == 0))
3566 set_bit(STRIPE_INSYNC, &sh->state);
3567 }
3568 }
3569 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3570 s.dec_preread_active = 1;
3571 }
3572
NeilBrownef5b7c62012-11-22 09:13:36 +11003573 /*
3574 * might be able to return some write requests if the parity blocks
3575 * are safe, or on a failed drive
3576 */
3577 pdev = &sh->dev[sh->pd_idx];
3578 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3579 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3580 qdev = &sh->dev[sh->qd_idx];
3581 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3582 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3583 || conf->level < 6;
3584
3585 if (s.written &&
3586 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3587 && !test_bit(R5_LOCKED, &pdev->flags)
3588 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3589 test_bit(R5_Discard, &pdev->flags))))) &&
3590 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3591 && !test_bit(R5_LOCKED, &qdev->flags)
3592 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3593 test_bit(R5_Discard, &qdev->flags))))))
3594 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3595
3596 /* Now we might consider reading some blocks, either to check/generate
3597 * parity, or to satisfy requests
3598 * or to load a block that is being partially written.
3599 */
3600 if (s.to_read || s.non_overwrite
3601 || (conf->level == 6 && s.to_write && s.failed)
3602 || (s.syncing && (s.uptodate + s.compute < disks))
3603 || s.replacing
3604 || s.expanding)
3605 handle_stripe_fill(sh, &s, disks);
3606
NeilBrown84789552011-07-27 11:00:36 +10003607 /* Now to consider new write requests and what else, if anything
3608 * should be read. We do not handle new writes when:
3609 * 1/ A 'write' operation (copy+xor) is already in flight.
3610 * 2/ A 'check' operation is in flight, as it may clobber the parity
3611 * block.
3612 */
3613 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3614 handle_stripe_dirtying(conf, sh, &s, disks);
3615
3616 /* maybe we need to check and possibly fix the parity for this stripe
3617 * Any reads will already have been scheduled, so we just see if enough
3618 * data is available. The parity check is held off while parity
3619 * dependent operations are in flight.
3620 */
3621 if (sh->check_state ||
3622 (s.syncing && s.locked == 0 &&
3623 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3624 !test_bit(STRIPE_INSYNC, &sh->state))) {
3625 if (conf->level == 6)
3626 handle_parity_checks6(conf, sh, &s, disks);
3627 else
3628 handle_parity_checks5(conf, sh, &s, disks);
3629 }
NeilBrownc5a31002011-07-27 11:00:36 +10003630
NeilBrownc1dadcc2013-07-22 12:57:21 +10003631 if ((s.replacing || s.syncing) && s.locked == 0
3632 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
3633 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11003634 /* Write out to replacement devices where possible */
3635 for (i = 0; i < conf->raid_disks; i++)
NeilBrownc1dadcc2013-07-22 12:57:21 +10003636 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3637 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11003638 set_bit(R5_WantReplace, &sh->dev[i].flags);
3639 set_bit(R5_LOCKED, &sh->dev[i].flags);
3640 s.locked++;
3641 }
NeilBrownc1dadcc2013-07-22 12:57:21 +10003642 if (s.replacing)
3643 set_bit(STRIPE_INSYNC, &sh->state);
3644 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11003645 }
3646 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownc1dadcc2013-07-22 12:57:21 +10003647 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11003648 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003649 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3650 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003651 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3652 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10003653 }
3654
3655 /* If the failed drives are just a ReadError, then we might need
3656 * to progress the repair/check process
3657 */
3658 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3659 for (i = 0; i < s.failed; i++) {
3660 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3661 if (test_bit(R5_ReadError, &dev->flags)
3662 && !test_bit(R5_LOCKED, &dev->flags)
3663 && test_bit(R5_UPTODATE, &dev->flags)
3664 ) {
3665 if (!test_bit(R5_ReWrite, &dev->flags)) {
3666 set_bit(R5_Wantwrite, &dev->flags);
3667 set_bit(R5_ReWrite, &dev->flags);
3668 set_bit(R5_LOCKED, &dev->flags);
3669 s.locked++;
3670 } else {
3671 /* let's read it back */
3672 set_bit(R5_Wantread, &dev->flags);
3673 set_bit(R5_LOCKED, &dev->flags);
3674 s.locked++;
3675 }
3676 }
3677 }
3678
3679
NeilBrown3687c062011-07-27 11:00:36 +10003680 /* Finish reconstruct operations initiated by the expansion process */
3681 if (sh->reconstruct_state == reconstruct_state_result) {
3682 struct stripe_head *sh_src
3683 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3684 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3685 /* sh cannot be written until sh_src has been read.
3686 * so arrange for sh to be delayed a little
3687 */
3688 set_bit(STRIPE_DELAYED, &sh->state);
3689 set_bit(STRIPE_HANDLE, &sh->state);
3690 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3691 &sh_src->state))
3692 atomic_inc(&conf->preread_active_stripes);
3693 release_stripe(sh_src);
3694 goto finish;
3695 }
3696 if (sh_src)
3697 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003698
NeilBrown3687c062011-07-27 11:00:36 +10003699 sh->reconstruct_state = reconstruct_state_idle;
3700 clear_bit(STRIPE_EXPANDING, &sh->state);
3701 for (i = conf->raid_disks; i--; ) {
3702 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3703 set_bit(R5_LOCKED, &sh->dev[i].flags);
3704 s.locked++;
3705 }
3706 }
3707
3708 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3709 !sh->reconstruct_state) {
3710 /* Need to write out all blocks after computing parity */
3711 sh->disks = conf->raid_disks;
3712 stripe_set_idx(sh->sector, conf, 0, sh);
3713 schedule_reconstruction(sh, &s, 1, 1);
3714 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3715 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3716 atomic_dec(&conf->reshape_stripes);
3717 wake_up(&conf->wait_for_overlap);
3718 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3719 }
3720
3721 if (s.expanding && s.locked == 0 &&
3722 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3723 handle_stripe_expansion(conf, sh);
3724
3725finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003726 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003727 if (unlikely(s.blocked_rdev)) {
3728 if (conf->mddev->external)
3729 md_wait_for_blocked_rdev(s.blocked_rdev,
3730 conf->mddev);
3731 else
3732 /* Internal metadata will immediately
3733 * be written by raid5d, so we don't
3734 * need to wait here.
3735 */
3736 rdev_dec_pending(s.blocked_rdev,
3737 conf->mddev);
3738 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003739
NeilBrownbc2607f2011-07-28 11:39:22 +10003740 if (s.handle_bad_blocks)
3741 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003742 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003743 struct r5dev *dev = &sh->dev[i];
3744 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3745 /* We own a safe reference to the rdev */
3746 rdev = conf->disks[i].rdev;
3747 if (!rdev_set_badblocks(rdev, sh->sector,
3748 STRIPE_SECTORS, 0))
3749 md_error(conf->mddev, rdev);
3750 rdev_dec_pending(rdev, conf->mddev);
3751 }
NeilBrownb84db562011-07-28 11:39:23 +10003752 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3753 rdev = conf->disks[i].rdev;
3754 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003755 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003756 rdev_dec_pending(rdev, conf->mddev);
3757 }
NeilBrown977df362011-12-23 10:17:53 +11003758 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3759 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003760 if (!rdev)
3761 /* rdev have been moved down */
3762 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003763 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003764 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003765 rdev_dec_pending(rdev, conf->mddev);
3766 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003767 }
3768
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003769 if (s.ops_request)
3770 raid_run_ops(sh, s.ops_request);
3771
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003772 ops_run_io(sh, &s);
3773
NeilBrownc5709ef2011-07-26 11:35:20 +10003774 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003775 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003776 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003777 * have actually been submitted.
3778 */
3779 atomic_dec(&conf->preread_active_stripes);
3780 if (atomic_read(&conf->preread_active_stripes) <
3781 IO_THRESHOLD)
3782 md_wakeup_thread(conf->mddev->thread);
3783 }
3784
NeilBrownc5709ef2011-07-26 11:35:20 +10003785 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003786
Dan Williams257a4b42011-11-08 16:22:06 +11003787 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003788}
3789
NeilBrownd1688a62011-10-11 16:49:52 +11003790static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791{
3792 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3793 while (!list_empty(&conf->delayed_list)) {
3794 struct list_head *l = conf->delayed_list.next;
3795 struct stripe_head *sh;
3796 sh = list_entry(l, struct stripe_head, lru);
3797 list_del_init(l);
3798 clear_bit(STRIPE_DELAYED, &sh->state);
3799 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3800 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003801 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 }
NeilBrown482c0832011-04-18 18:25:42 +10003803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804}
3805
NeilBrownd1688a62011-10-11 16:49:52 +11003806static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003807{
3808 /* device_lock is held */
3809 struct list_head head;
3810 list_add(&head, &conf->bitmap_list);
3811 list_del_init(&conf->bitmap_list);
3812 while (!list_empty(&head)) {
3813 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3814 list_del_init(&sh->lru);
3815 atomic_inc(&sh->count);
3816 __release_stripe(conf, sh);
3817 }
3818}
3819
NeilBrownfd01b882011-10-11 16:47:53 +11003820int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003821{
NeilBrownd1688a62011-10-11 16:49:52 +11003822 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003823
3824 /* No difference between reads and writes. Just check
3825 * how busy the stripe_cache is
3826 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003827
NeilBrownf022b2f2006-10-03 01:15:56 -07003828 if (conf->inactive_blocked)
3829 return 1;
3830 if (conf->quiesce)
3831 return 1;
3832 if (list_empty_careful(&conf->inactive_list))
3833 return 1;
3834
3835 return 0;
3836}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003837EXPORT_SYMBOL_GPL(md_raid5_congested);
3838
3839static int raid5_congested(void *data, int bits)
3840{
NeilBrownfd01b882011-10-11 16:47:53 +11003841 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003842
3843 return mddev_congested(mddev, bits) ||
3844 md_raid5_congested(mddev, bits);
3845}
NeilBrownf022b2f2006-10-03 01:15:56 -07003846
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003847/* We want read requests to align with chunks where possible,
3848 * but write requests don't need to.
3849 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003850static int raid5_mergeable_bvec(struct request_queue *q,
3851 struct bvec_merge_data *bvm,
3852 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003853{
NeilBrownfd01b882011-10-11 16:47:53 +11003854 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003855 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003856 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003857 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003858 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003859
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003860 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003861 return biovec->bv_len; /* always allow writes to be mergeable */
3862
Andre Noll664e7c42009-06-18 08:45:27 +10003863 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3864 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003865 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3866 if (max < 0) max = 0;
3867 if (max <= biovec->bv_len && bio_sectors == 0)
3868 return biovec->bv_len;
3869 else
3870 return max;
3871}
3872
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003873
NeilBrownfd01b882011-10-11 16:47:53 +11003874static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003875{
3876 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003877 unsigned int chunk_sectors = mddev->chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08003878 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003879
Andre Noll664e7c42009-06-18 08:45:27 +10003880 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3881 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003882 return chunk_sectors >=
3883 ((sector & (chunk_sectors - 1)) + bio_sectors);
3884}
3885
3886/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003887 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3888 * later sampled by raid5d.
3889 */
NeilBrownd1688a62011-10-11 16:49:52 +11003890static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003891{
3892 unsigned long flags;
3893
3894 spin_lock_irqsave(&conf->device_lock, flags);
3895
3896 bi->bi_next = conf->retry_read_aligned_list;
3897 conf->retry_read_aligned_list = bi;
3898
3899 spin_unlock_irqrestore(&conf->device_lock, flags);
3900 md_wakeup_thread(conf->mddev->thread);
3901}
3902
3903
NeilBrownd1688a62011-10-11 16:49:52 +11003904static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003905{
3906 struct bio *bi;
3907
3908 bi = conf->retry_read_aligned;
3909 if (bi) {
3910 conf->retry_read_aligned = NULL;
3911 return bi;
3912 }
3913 bi = conf->retry_read_aligned_list;
3914 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003915 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003916 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003917 /*
3918 * this sets the active strip count to 1 and the processed
3919 * strip count to zero (upper 8 bits)
3920 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003921 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003922 }
3923
3924 return bi;
3925}
3926
3927
3928/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003929 * The "raid5_align_endio" should check if the read succeeded and if it
3930 * did, call bio_endio on the original bio (having bio_put the new bio
3931 * first).
3932 * If the read failed..
3933 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003934static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003935{
3936 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003937 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003938 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003939 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003940 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003941
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003942 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003943
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003944 rdev = (void*)raid_bi->bi_next;
3945 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003946 mddev = rdev->mddev;
3947 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003948
3949 rdev_dec_pending(rdev, conf->mddev);
3950
3951 if (!error && uptodate) {
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07003952 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
3953 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02003954 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003955 if (atomic_dec_and_test(&conf->active_aligned_reads))
3956 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003957 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003958 }
3959
3960
Dan Williams45b42332007-07-09 11:56:43 -07003961 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003962
3963 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003964}
3965
Neil Brown387bb172007-02-08 14:20:29 -08003966static int bio_fits_rdev(struct bio *bi)
3967{
Jens Axboe165125e2007-07-24 09:28:11 +02003968 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003969
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08003970 if (bio_sectors(bi) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003971 return 0;
3972 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003973 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003974 return 0;
3975
3976 if (q->merge_bvec_fn)
3977 /* it's too hard to apply the merge_bvec_fn at this stage,
3978 * just just give up
3979 */
3980 return 0;
3981
3982 return 1;
3983}
3984
3985
NeilBrownfd01b882011-10-11 16:47:53 +11003986static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003987{
NeilBrownd1688a62011-10-11 16:49:52 +11003988 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003989 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003990 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003991 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003992 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003993
3994 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003995 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003996 return 0;
3997 }
3998 /*
NeilBrowna167f662010-10-26 18:31:13 +11003999 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004000 */
NeilBrowna167f662010-10-26 18:31:13 +11004001 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004002 if (!align_bi)
4003 return 0;
4004 /*
4005 * set bi_end_io to a new function, and set bi_private to the
4006 * original bio.
4007 */
4008 align_bi->bi_end_io = raid5_align_endio;
4009 align_bi->bi_private = raid_bio;
4010 /*
4011 * compute position
4012 */
NeilBrown112bf892009-03-31 14:39:38 +11004013 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
4014 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11004015 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004016
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004017 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004018 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11004019 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
4020 if (!rdev || test_bit(Faulty, &rdev->flags) ||
4021 rdev->recovery_offset < end_sector) {
4022 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
4023 if (rdev &&
4024 (test_bit(Faulty, &rdev->flags) ||
4025 !(test_bit(In_sync, &rdev->flags) ||
4026 rdev->recovery_offset >= end_sector)))
4027 rdev = NULL;
4028 }
4029 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10004030 sector_t first_bad;
4031 int bad_sectors;
4032
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004033 atomic_inc(&rdev->nr_pending);
4034 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004035 raid_bio->bi_next = (void*)rdev;
4036 align_bi->bi_bdev = rdev->bdev;
4037 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004038
NeilBrown31c176e2011-07-28 11:39:22 +10004039 if (!bio_fits_rdev(align_bi) ||
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08004040 is_badblock(rdev, align_bi->bi_sector, bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10004041 &first_bad, &bad_sectors)) {
4042 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004043 bio_put(align_bi);
4044 rdev_dec_pending(rdev, mddev);
4045 return 0;
4046 }
4047
majianpeng6c0544e2012-06-12 08:31:10 +08004048 /* No reshape active, so we can trust rdev->data_offset */
4049 align_bi->bi_sector += rdev->data_offset;
4050
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004051 spin_lock_irq(&conf->device_lock);
4052 wait_event_lock_irq(conf->wait_for_stripe,
4053 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004054 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004055 atomic_inc(&conf->active_aligned_reads);
4056 spin_unlock_irq(&conf->device_lock);
4057
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004058 if (mddev->gendisk)
4059 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4060 align_bi, disk_devt(mddev->gendisk),
4061 raid_bio->bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004062 generic_make_request(align_bi);
4063 return 1;
4064 } else {
4065 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004066 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004067 return 0;
4068 }
4069}
4070
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004071/* __get_priority_stripe - get the next stripe to process
4072 *
4073 * Full stripe writes are allowed to pass preread active stripes up until
4074 * the bypass_threshold is exceeded. In general the bypass_count
4075 * increments when the handle_list is handled before the hold_list; however, it
4076 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4077 * stripe with in flight i/o. The bypass_count will be reset when the
4078 * head of the hold_list has changed, i.e. the head was promoted to the
4079 * handle_list.
4080 */
NeilBrownd1688a62011-10-11 16:49:52 +11004081static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004082{
4083 struct stripe_head *sh;
4084
4085 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4086 __func__,
4087 list_empty(&conf->handle_list) ? "empty" : "busy",
4088 list_empty(&conf->hold_list) ? "empty" : "busy",
4089 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4090
4091 if (!list_empty(&conf->handle_list)) {
4092 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4093
4094 if (list_empty(&conf->hold_list))
4095 conf->bypass_count = 0;
4096 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4097 if (conf->hold_list.next == conf->last_hold)
4098 conf->bypass_count++;
4099 else {
4100 conf->last_hold = conf->hold_list.next;
4101 conf->bypass_count -= conf->bypass_threshold;
4102 if (conf->bypass_count < 0)
4103 conf->bypass_count = 0;
4104 }
4105 }
4106 } else if (!list_empty(&conf->hold_list) &&
4107 ((conf->bypass_threshold &&
4108 conf->bypass_count > conf->bypass_threshold) ||
4109 atomic_read(&conf->pending_full_writes) == 0)) {
4110 sh = list_entry(conf->hold_list.next,
4111 typeof(*sh), lru);
4112 conf->bypass_count -= conf->bypass_threshold;
4113 if (conf->bypass_count < 0)
4114 conf->bypass_count = 0;
4115 } else
4116 return NULL;
4117
4118 list_del_init(&sh->lru);
4119 atomic_inc(&sh->count);
4120 BUG_ON(atomic_read(&sh->count) != 1);
4121 return sh;
4122}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004123
Shaohua Li8811b592012-08-02 08:33:00 +10004124struct raid5_plug_cb {
4125 struct blk_plug_cb cb;
4126 struct list_head list;
4127};
4128
4129static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4130{
4131 struct raid5_plug_cb *cb = container_of(
4132 blk_cb, struct raid5_plug_cb, cb);
4133 struct stripe_head *sh;
4134 struct mddev *mddev = cb->cb.data;
4135 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004136 int cnt = 0;
Shaohua Li8811b592012-08-02 08:33:00 +10004137
4138 if (cb->list.next && !list_empty(&cb->list)) {
4139 spin_lock_irq(&conf->device_lock);
4140 while (!list_empty(&cb->list)) {
4141 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4142 list_del_init(&sh->lru);
4143 /*
4144 * avoid race release_stripe_plug() sees
4145 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4146 * is still in our list
4147 */
4148 smp_mb__before_clear_bit();
4149 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4150 __release_stripe(conf, sh);
NeilBrowna9add5d2012-10-31 11:59:09 +11004151 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004152 }
4153 spin_unlock_irq(&conf->device_lock);
4154 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004155 if (mddev->queue)
4156 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004157 kfree(cb);
4158}
4159
4160static void release_stripe_plug(struct mddev *mddev,
4161 struct stripe_head *sh)
4162{
4163 struct blk_plug_cb *blk_cb = blk_check_plugged(
4164 raid5_unplug, mddev,
4165 sizeof(struct raid5_plug_cb));
4166 struct raid5_plug_cb *cb;
4167
4168 if (!blk_cb) {
4169 release_stripe(sh);
4170 return;
4171 }
4172
4173 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4174
4175 if (cb->list.next == NULL)
4176 INIT_LIST_HEAD(&cb->list);
4177
4178 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4179 list_add_tail(&sh->lru, &cb->list);
4180 else
4181 release_stripe(sh);
4182}
4183
Shaohua Li620125f2012-10-11 13:49:05 +11004184static void make_discard_request(struct mddev *mddev, struct bio *bi)
4185{
4186 struct r5conf *conf = mddev->private;
4187 sector_t logical_sector, last_sector;
4188 struct stripe_head *sh;
4189 int remaining;
4190 int stripe_sectors;
4191
4192 if (mddev->reshape_position != MaxSector)
4193 /* Skip discard while reshape is happening */
4194 return;
4195
4196 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4197 last_sector = bi->bi_sector + (bi->bi_size>>9);
4198
4199 bi->bi_next = NULL;
4200 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4201
4202 stripe_sectors = conf->chunk_sectors *
4203 (conf->raid_disks - conf->max_degraded);
4204 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4205 stripe_sectors);
4206 sector_div(last_sector, stripe_sectors);
4207
4208 logical_sector *= conf->chunk_sectors;
4209 last_sector *= conf->chunk_sectors;
4210
4211 for (; logical_sector < last_sector;
4212 logical_sector += STRIPE_SECTORS) {
4213 DEFINE_WAIT(w);
4214 int d;
4215 again:
4216 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4217 prepare_to_wait(&conf->wait_for_overlap, &w,
4218 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004219 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4220 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4221 release_stripe(sh);
4222 schedule();
4223 goto again;
4224 }
4225 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11004226 spin_lock_irq(&sh->stripe_lock);
4227 for (d = 0; d < conf->raid_disks; d++) {
4228 if (d == sh->pd_idx || d == sh->qd_idx)
4229 continue;
4230 if (sh->dev[d].towrite || sh->dev[d].toread) {
4231 set_bit(R5_Overlap, &sh->dev[d].flags);
4232 spin_unlock_irq(&sh->stripe_lock);
4233 release_stripe(sh);
4234 schedule();
4235 goto again;
4236 }
4237 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11004238 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11004239 finish_wait(&conf->wait_for_overlap, &w);
4240 for (d = 0; d < conf->raid_disks; d++) {
4241 if (d == sh->pd_idx || d == sh->qd_idx)
4242 continue;
4243 sh->dev[d].towrite = bi;
4244 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4245 raid5_inc_bi_active_stripes(bi);
4246 }
4247 spin_unlock_irq(&sh->stripe_lock);
4248 if (conf->mddev->bitmap) {
4249 for (d = 0;
4250 d < conf->raid_disks - conf->max_degraded;
4251 d++)
4252 bitmap_startwrite(mddev->bitmap,
4253 sh->sector,
4254 STRIPE_SECTORS,
4255 0);
4256 sh->bm_seq = conf->seq_flush + 1;
4257 set_bit(STRIPE_BIT_DELAY, &sh->state);
4258 }
4259
4260 set_bit(STRIPE_HANDLE, &sh->state);
4261 clear_bit(STRIPE_DELAYED, &sh->state);
4262 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4263 atomic_inc(&conf->preread_active_stripes);
4264 release_stripe_plug(mddev, sh);
4265 }
4266
4267 remaining = raid5_dec_bi_active_stripes(bi);
4268 if (remaining == 0) {
4269 md_write_end(mddev);
4270 bio_endio(bi, 0);
4271 }
4272}
4273
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004274static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275{
NeilBrownd1688a62011-10-11 16:49:52 +11004276 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004277 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 sector_t new_sector;
4279 sector_t logical_sector, last_sector;
4280 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004281 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004282 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
Tejun Heoe9c74692010-09-03 11:56:18 +02004284 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4285 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004286 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004287 }
4288
NeilBrown3d310eb2005-06-21 17:17:26 -07004289 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004290
NeilBrown802ba062006-12-13 00:34:13 -08004291 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004292 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004293 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004294 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004295
Shaohua Li620125f2012-10-11 13:49:05 +11004296 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4297 make_discard_request(mddev, bi);
4298 return;
4299 }
4300
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004302 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 bi->bi_next = NULL;
4304 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004305
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4307 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004308 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004309
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004310 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004311 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004312 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004313 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004314 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004315 * 64bit on a 32bit platform, and so it might be
4316 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004317 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004318 * the lock is dropped, so once we get a reference
4319 * to the stripe that we think it is, we will have
4320 * to check again.
4321 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004322 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004323 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004324 ? logical_sector < conf->reshape_progress
4325 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004326 previous = 1;
4327 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004328 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004329 ? logical_sector < conf->reshape_safe
4330 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004331 spin_unlock_irq(&conf->device_lock);
4332 schedule();
4333 goto retry;
4334 }
4335 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004336 spin_unlock_irq(&conf->device_lock);
4337 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004338
NeilBrown112bf892009-03-31 14:39:38 +11004339 new_sector = raid5_compute_sector(conf, logical_sector,
4340 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004341 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004342 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 (unsigned long long)new_sector,
4344 (unsigned long long)logical_sector);
4345
NeilBrownb5663ba2009-03-31 14:39:38 +11004346 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004347 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004349 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004350 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004351 * stripe, so we must do the range check again.
4352 * Expansion could still move past after this
4353 * test, but as we are holding a reference to
4354 * 'sh', we know that if that happens,
4355 * STRIPE_EXPANDING will get set and the expansion
4356 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004357 */
4358 int must_retry = 0;
4359 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004360 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004361 ? logical_sector >= conf->reshape_progress
4362 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004363 /* mismatch, need to try again */
4364 must_retry = 1;
4365 spin_unlock_irq(&conf->device_lock);
4366 if (must_retry) {
4367 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004368 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004369 goto retry;
4370 }
4371 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004372
Namhyung Kimffd96e32011-07-18 17:38:51 +10004373 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004374 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004375 logical_sector < mddev->suspend_hi) {
4376 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004377 /* As the suspend_* range is controlled by
4378 * userspace, we want an interruptible
4379 * wait.
4380 */
4381 flush_signals(current);
4382 prepare_to_wait(&conf->wait_for_overlap,
4383 &w, TASK_INTERRUPTIBLE);
4384 if (logical_sector >= mddev->suspend_lo &&
4385 logical_sector < mddev->suspend_hi)
4386 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004387 goto retry;
4388 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004389
4390 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004391 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004392 /* Stripe is busy expanding or
4393 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 * and wait a while
4395 */
NeilBrown482c0832011-04-18 18:25:42 +10004396 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 release_stripe(sh);
4398 schedule();
4399 goto retry;
4400 }
4401 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004402 set_bit(STRIPE_HANDLE, &sh->state);
4403 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004404 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004405 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4406 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004407 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 } else {
4409 /* cannot get stripe for read-ahead, just give-up */
4410 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4411 finish_wait(&conf->wait_for_overlap, &w);
4412 break;
4413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004415
Shaohua Lie7836bd62012-07-19 16:01:31 +10004416 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004417 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418
NeilBrown16a53ec2006-06-26 00:27:38 -07004419 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004421
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004422 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4423 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004424 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426}
4427
NeilBrownfd01b882011-10-11 16:47:53 +11004428static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004429
NeilBrownfd01b882011-10-11 16:47:53 +11004430static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431{
NeilBrown52c03292006-06-26 00:27:43 -07004432 /* reshaping is quite different to recovery/resync so it is
4433 * handled quite separately ... here.
4434 *
4435 * On each call to sync_request, we gather one chunk worth of
4436 * destination stripes and flag them as expanding.
4437 * Then we find all the source stripes and request reads.
4438 * As the reads complete, handle_stripe will copy the data
4439 * into the destination stripe and release that stripe.
4440 */
NeilBrownd1688a62011-10-11 16:49:52 +11004441 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004443 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004444 int raid_disks = conf->previous_raid_disks;
4445 int data_disks = raid_disks - conf->max_degraded;
4446 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004447 int i;
4448 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004449 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004450 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004451 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004452 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004453
NeilBrownfef9c612009-03-31 15:16:46 +11004454 if (sector_nr == 0) {
4455 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004456 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004457 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4458 sector_nr = raid5_size(mddev, 0, 0)
4459 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004460 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004461 conf->reshape_progress > 0)
4462 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004463 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004464 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004465 mddev->curr_resync_completed = sector_nr;
4466 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004467 *skipped = 1;
4468 return sector_nr;
4469 }
NeilBrown52c03292006-06-26 00:27:43 -07004470 }
4471
NeilBrown7a661382009-03-31 15:21:40 +11004472 /* We need to process a full chunk at a time.
4473 * If old and new chunk sizes differ, we need to process the
4474 * largest of these
4475 */
Andre Noll664e7c42009-06-18 08:45:27 +10004476 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4477 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004478 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004479 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004480
NeilBrownb5254dd2012-05-21 09:27:01 +10004481 /* We update the metadata at least every 10 seconds, or when
4482 * the data about to be copied would over-write the source of
4483 * the data at the front of the range. i.e. one new_stripe
4484 * along from reshape_progress new_maps to after where
4485 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004486 */
NeilBrownfef9c612009-03-31 15:16:46 +11004487 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004488 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004489 readpos = conf->reshape_progress;
4490 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004491 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004492 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004493 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004494 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004495 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004496 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004497 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004498 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004499 readpos -= min_t(sector_t, reshape_sectors, readpos);
4500 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004501 }
NeilBrown52c03292006-06-26 00:27:43 -07004502
NeilBrownb5254dd2012-05-21 09:27:01 +10004503 /* Having calculated the 'writepos' possibly use it
4504 * to set 'stripe_addr' which is where we will write to.
4505 */
4506 if (mddev->reshape_backwards) {
4507 BUG_ON(conf->reshape_progress == 0);
4508 stripe_addr = writepos;
4509 BUG_ON((mddev->dev_sectors &
4510 ~((sector_t)reshape_sectors - 1))
4511 - reshape_sectors - stripe_addr
4512 != sector_nr);
4513 } else {
4514 BUG_ON(writepos != sector_nr + reshape_sectors);
4515 stripe_addr = sector_nr;
4516 }
4517
NeilBrownc8f517c2009-03-31 15:28:40 +11004518 /* 'writepos' is the most advanced device address we might write.
4519 * 'readpos' is the least advanced device address we might read.
4520 * 'safepos' is the least address recorded in the metadata as having
4521 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004522 * If there is a min_offset_diff, these are adjusted either by
4523 * increasing the safepos/readpos if diff is negative, or
4524 * increasing writepos if diff is positive.
4525 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004526 * ensure safety in the face of a crash - that must be done by userspace
4527 * making a backup of the data. So in that case there is no particular
4528 * rush to update metadata.
4529 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4530 * update the metadata to advance 'safepos' to match 'readpos' so that
4531 * we can be safe in the event of a crash.
4532 * So we insist on updating metadata if safepos is behind writepos and
4533 * readpos is beyond writepos.
4534 * In any case, update the metadata every 10 seconds.
4535 * Maybe that number should be configurable, but I'm not sure it is
4536 * worth it.... maybe it could be a multiple of safemode_delay???
4537 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004538 if (conf->min_offset_diff < 0) {
4539 safepos += -conf->min_offset_diff;
4540 readpos += -conf->min_offset_diff;
4541 } else
4542 writepos += conf->min_offset_diff;
4543
NeilBrown2c810cd2012-05-21 09:27:00 +10004544 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004545 ? (safepos > writepos && readpos < writepos)
4546 : (safepos < writepos && readpos > writepos)) ||
4547 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004548 /* Cannot proceed until we've updated the superblock... */
4549 wait_event(conf->wait_for_overlap,
4550 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004551 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004552 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004553 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004554 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004555 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004556 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004557 kthread_should_stop());
4558 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004559 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004560 spin_unlock_irq(&conf->device_lock);
4561 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004562 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004563 }
4564
NeilBrownab69ae12009-03-31 15:26:47 +11004565 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004566 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004567 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004568 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004569 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004570 set_bit(STRIPE_EXPANDING, &sh->state);
4571 atomic_inc(&conf->reshape_stripes);
4572 /* If any of this stripe is beyond the end of the old
4573 * array, then we need to zero those blocks
4574 */
4575 for (j=sh->disks; j--;) {
4576 sector_t s;
4577 if (j == sh->pd_idx)
4578 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004579 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004580 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004581 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004582 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004583 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004584 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004585 continue;
4586 }
4587 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4588 set_bit(R5_Expanded, &sh->dev[j].flags);
4589 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4590 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004591 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004592 set_bit(STRIPE_EXPAND_READY, &sh->state);
4593 set_bit(STRIPE_HANDLE, &sh->state);
4594 }
NeilBrownab69ae12009-03-31 15:26:47 +11004595 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004596 }
4597 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004598 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004599 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004600 else
NeilBrown7a661382009-03-31 15:21:40 +11004601 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004602 spin_unlock_irq(&conf->device_lock);
4603 /* Ok, those stripe are ready. We can start scheduling
4604 * reads on the source stripes.
4605 * The source stripes are determined by mapping the first and last
4606 * block on the destination stripes.
4607 */
NeilBrown52c03292006-06-26 00:27:43 -07004608 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004609 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004610 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004611 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004612 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004613 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004614 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004615 if (last_sector >= mddev->dev_sectors)
4616 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004617 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004618 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004619 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4620 set_bit(STRIPE_HANDLE, &sh->state);
4621 release_stripe(sh);
4622 first_sector += STRIPE_SECTORS;
4623 }
NeilBrownab69ae12009-03-31 15:26:47 +11004624 /* Now that the sources are clearly marked, we can release
4625 * the destination stripes
4626 */
4627 while (!list_empty(&stripes)) {
4628 sh = list_entry(stripes.next, struct stripe_head, lru);
4629 list_del_init(&sh->lru);
4630 release_stripe(sh);
4631 }
NeilBrownc6207272008-02-06 01:39:52 -08004632 /* If this takes us to the resync_max point where we have to pause,
4633 * then we need to write out the superblock.
4634 */
NeilBrown7a661382009-03-31 15:21:40 +11004635 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004636 if ((sector_nr - mddev->curr_resync_completed) * 2
4637 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004638 /* Cannot proceed until we've updated the superblock... */
4639 wait_event(conf->wait_for_overlap,
4640 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004641 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004642 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004643 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004644 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4645 md_wakeup_thread(mddev->thread);
4646 wait_event(mddev->sb_wait,
4647 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4648 || kthread_should_stop());
4649 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004650 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004651 spin_unlock_irq(&conf->device_lock);
4652 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004653 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004654 }
NeilBrown7a661382009-03-31 15:21:40 +11004655 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004656}
4657
4658/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004659static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004660{
NeilBrownd1688a62011-10-11 16:49:52 +11004661 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004662 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004663 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004664 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004665 int still_degraded = 0;
4666 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
NeilBrown72626682005-09-09 16:23:54 -07004668 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004670
NeilBrown29269552006-03-27 01:18:10 -08004671 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4672 end_reshape(conf);
4673 return 0;
4674 }
NeilBrown72626682005-09-09 16:23:54 -07004675
4676 if (mddev->curr_resync < max_sector) /* aborted */
4677 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4678 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004679 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004680 conf->fullsync = 0;
4681 bitmap_close_sync(mddev->bitmap);
4682
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 return 0;
4684 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004685
NeilBrown64bd6602009-08-03 10:59:58 +10004686 /* Allow raid5_quiesce to complete */
4687 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4688
NeilBrown52c03292006-06-26 00:27:43 -07004689 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4690 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004691
NeilBrownc6207272008-02-06 01:39:52 -08004692 /* No need to check resync_max as we never do more than one
4693 * stripe, and as resync_max will always be on a chunk boundary,
4694 * if the check in md_do_sync didn't fire, there is no chance
4695 * of overstepping resync_max here
4696 */
4697
NeilBrown16a53ec2006-06-26 00:27:38 -07004698 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 * to resync, then assert that we are finished, because there is
4700 * nothing we can do.
4701 */
NeilBrown3285edf2006-06-26 00:27:55 -07004702 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004703 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004704 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004705 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 return rv;
4707 }
majianpeng6f608042013-04-24 11:42:41 +10004708 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
4709 !conf->fullsync &&
4710 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
4711 sync_blocks >= STRIPE_SECTORS) {
NeilBrown72626682005-09-09 16:23:54 -07004712 /* we can skip this block, and probably more */
4713 sync_blocks /= STRIPE_SECTORS;
4714 *skipped = 1;
4715 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
NeilBrownb47490c2008-02-06 01:39:50 -08004718 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4719
NeilBrowna8c906c2009-06-09 14:39:59 +10004720 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004722 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004724 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004726 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004728 /* Need to check if array will still be degraded after recovery/resync
4729 * We don't need to check the 'failed' flag as when that gets set,
4730 * recovery aborts.
4731 */
NeilBrownf001a702009-06-09 14:30:31 +10004732 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004733 if (conf->disks[i].rdev == NULL)
4734 still_degraded = 1;
4735
4736 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4737
NeilBrown83206d62011-07-26 11:19:49 +10004738 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
NeilBrown14425772009-10-16 15:55:25 +11004740 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 release_stripe(sh);
4742
4743 return STRIPE_SECTORS;
4744}
4745
NeilBrownd1688a62011-10-11 16:49:52 +11004746static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004747{
4748 /* We may not be able to submit a whole bio at once as there
4749 * may not be enough stripe_heads available.
4750 * We cannot pre-allocate enough stripe_heads as we may need
4751 * more than exist in the cache (if we allow ever large chunks).
4752 * So we do one stripe head at a time and record in
4753 * ->bi_hw_segments how many have been done.
4754 *
4755 * We *know* that this entire raid_bio is in one chunk, so
4756 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4757 */
4758 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004759 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004760 sector_t sector, logical_sector, last_sector;
4761 int scnt = 0;
4762 int remaining;
4763 int handled = 0;
4764
4765 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004766 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004767 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07004768 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004769
4770 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004771 logical_sector += STRIPE_SECTORS,
4772 sector += STRIPE_SECTORS,
4773 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004774
Shaohua Lie7836bd62012-07-19 16:01:31 +10004775 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004776 /* already done this stripe */
4777 continue;
4778
NeilBrowna8c906c2009-06-09 14:39:59 +10004779 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004780
4781 if (!sh) {
4782 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004783 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004784 conf->retry_read_aligned = raid_bio;
4785 return handled;
4786 }
4787
Neil Brown387bb172007-02-08 14:20:29 -08004788 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4789 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004790 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004791 conf->retry_read_aligned = raid_bio;
4792 return handled;
4793 }
4794
majianpeng3f9e7c12012-07-31 10:04:21 +10004795 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004796 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004797 release_stripe(sh);
4798 handled++;
4799 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004800 remaining = raid5_dec_bi_active_stripes(raid_bio);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004801 if (remaining == 0) {
4802 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
4803 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004804 bio_endio(raid_bio, 0);
Linus Torvalds0a82a8d2013-04-18 09:00:26 -07004805 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004806 if (atomic_dec_and_test(&conf->active_aligned_reads))
4807 wake_up(&conf->wait_for_stripe);
4808 return handled;
4809}
4810
Shaohua Li46a06402012-08-02 08:33:15 +10004811#define MAX_STRIPE_BATCH 8
4812static int handle_active_stripes(struct r5conf *conf)
4813{
4814 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4815 int i, batch_size = 0;
4816
4817 while (batch_size < MAX_STRIPE_BATCH &&
4818 (sh = __get_priority_stripe(conf)) != NULL)
4819 batch[batch_size++] = sh;
4820
4821 if (batch_size == 0)
4822 return batch_size;
4823 spin_unlock_irq(&conf->device_lock);
4824
4825 for (i = 0; i < batch_size; i++)
4826 handle_stripe(batch[i]);
4827
4828 cond_resched();
4829
4830 spin_lock_irq(&conf->device_lock);
4831 for (i = 0; i < batch_size; i++)
4832 __release_stripe(conf, batch[i]);
4833 return batch_size;
4834}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004835
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836/*
4837 * This is our raid5 kernel thread.
4838 *
4839 * We scan the hash table for stripes which can be handled now.
4840 * During the scan, completed stripes are saved for us by the interrupt
4841 * handler, so that they will not have to wait for our next wakeup.
4842 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004843static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844{
Shaohua Li4ed87312012-10-11 13:34:00 +11004845 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004846 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004848 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849
Dan Williams45b42332007-07-09 11:56:43 -07004850 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
4852 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004854 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 handled = 0;
4856 spin_lock_irq(&conf->device_lock);
4857 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004858 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004859 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
NeilBrown0021b7b2012-07-31 09:08:14 +02004861 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004862 !list_empty(&conf->bitmap_list)) {
4863 /* Now is a good time to flush some bitmap updates */
4864 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004865 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004866 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004867 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004868 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004869 activate_bit_delay(conf);
4870 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004871 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004872
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004873 while ((bio = remove_bio_from_retry(conf))) {
4874 int ok;
4875 spin_unlock_irq(&conf->device_lock);
4876 ok = retry_aligned_read(conf, bio);
4877 spin_lock_irq(&conf->device_lock);
4878 if (!ok)
4879 break;
4880 handled++;
4881 }
4882
Shaohua Li46a06402012-08-02 08:33:15 +10004883 batch_size = handle_active_stripes(conf);
4884 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004886 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887
Shaohua Li46a06402012-08-02 08:33:15 +10004888 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4889 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004890 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004891 spin_lock_irq(&conf->device_lock);
4892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 }
Dan Williams45b42332007-07-09 11:56:43 -07004894 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895
4896 spin_unlock_irq(&conf->device_lock);
4897
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004898 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004899 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
Dan Williams45b42332007-07-09 11:56:43 -07004901 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902}
4903
NeilBrown3f294f42005-11-08 21:39:25 -08004904static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004905raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004906{
NeilBrownd1688a62011-10-11 16:49:52 +11004907 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004908 if (conf)
4909 return sprintf(page, "%d\n", conf->max_nr_stripes);
4910 else
4911 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004912}
4913
NeilBrownc41d4ac2010-06-01 19:37:24 +10004914int
NeilBrownfd01b882011-10-11 16:47:53 +11004915raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004916{
NeilBrownd1688a62011-10-11 16:49:52 +11004917 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004918 int err;
4919
4920 if (size <= 16 || size > 32768)
4921 return -EINVAL;
4922 while (size < conf->max_nr_stripes) {
4923 if (drop_one_stripe(conf))
4924 conf->max_nr_stripes--;
4925 else
4926 break;
4927 }
4928 err = md_allow_write(mddev);
4929 if (err)
4930 return err;
4931 while (size > conf->max_nr_stripes) {
4932 if (grow_one_stripe(conf))
4933 conf->max_nr_stripes++;
4934 else break;
4935 }
4936 return 0;
4937}
4938EXPORT_SYMBOL(raid5_set_cache_size);
4939
NeilBrown3f294f42005-11-08 21:39:25 -08004940static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004941raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004942{
NeilBrownd1688a62011-10-11 16:49:52 +11004943 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004944 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004945 int err;
4946
NeilBrown3f294f42005-11-08 21:39:25 -08004947 if (len >= PAGE_SIZE)
4948 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004949 if (!conf)
4950 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004951
Dan Williams4ef197d82008-04-28 02:15:54 -07004952 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004953 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004954 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004955 if (err)
4956 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004957 return len;
4958}
NeilBrown007583c2005-11-08 21:39:30 -08004959
NeilBrown96de1e62005-11-08 21:39:39 -08004960static struct md_sysfs_entry
4961raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4962 raid5_show_stripe_cache_size,
4963 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004964
4965static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004966raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004967{
NeilBrownd1688a62011-10-11 16:49:52 +11004968 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004969 if (conf)
4970 return sprintf(page, "%d\n", conf->bypass_threshold);
4971 else
4972 return 0;
4973}
4974
4975static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004976raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004977{
NeilBrownd1688a62011-10-11 16:49:52 +11004978 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004979 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004980 if (len >= PAGE_SIZE)
4981 return -EINVAL;
4982 if (!conf)
4983 return -ENODEV;
4984
Dan Williams4ef197d82008-04-28 02:15:54 -07004985 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004986 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004987 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004988 return -EINVAL;
4989 conf->bypass_threshold = new;
4990 return len;
4991}
4992
4993static struct md_sysfs_entry
4994raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4995 S_IRUGO | S_IWUSR,
4996 raid5_show_preread_threshold,
4997 raid5_store_preread_threshold);
4998
4999static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11005000stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08005001{
NeilBrownd1688a62011-10-11 16:49:52 +11005002 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08005003 if (conf)
5004 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
5005 else
5006 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08005007}
5008
NeilBrown96de1e62005-11-08 21:39:39 -08005009static struct md_sysfs_entry
5010raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08005011
NeilBrown007583c2005-11-08 21:39:30 -08005012static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08005013 &raid5_stripecache_size.attr,
5014 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005015 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08005016 NULL,
5017};
NeilBrown007583c2005-11-08 21:39:30 -08005018static struct attribute_group raid5_attrs_group = {
5019 .name = NULL,
5020 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08005021};
5022
Dan Williams80c3a6c2009-03-17 18:10:40 -07005023static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11005024raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07005025{
NeilBrownd1688a62011-10-11 16:49:52 +11005026 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005027
5028 if (!sectors)
5029 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005030 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11005031 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11005032 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005033
Andre Noll9d8f0362009-06-18 08:45:01 +10005034 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10005035 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005036 return sectors * (raid_disks - conf->max_degraded);
5037}
5038
NeilBrownd1688a62011-10-11 16:49:52 +11005039static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005040{
5041 struct raid5_percpu *percpu;
5042 unsigned long cpu;
5043
5044 if (!conf->percpu)
5045 return;
5046
5047 get_online_cpus();
5048 for_each_possible_cpu(cpu) {
5049 percpu = per_cpu_ptr(conf->percpu, cpu);
5050 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005051 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005052 }
5053#ifdef CONFIG_HOTPLUG_CPU
5054 unregister_cpu_notifier(&conf->cpu_notify);
5055#endif
5056 put_online_cpus();
5057
5058 free_percpu(conf->percpu);
5059}
5060
NeilBrownd1688a62011-10-11 16:49:52 +11005061static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10005062{
5063 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07005064 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005065 kfree(conf->disks);
5066 kfree(conf->stripe_hashtbl);
5067 kfree(conf);
5068}
5069
Dan Williams36d1c642009-07-14 11:48:22 -07005070#ifdef CONFIG_HOTPLUG_CPU
5071static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5072 void *hcpu)
5073{
NeilBrownd1688a62011-10-11 16:49:52 +11005074 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005075 long cpu = (long)hcpu;
5076 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5077
5078 switch (action) {
5079 case CPU_UP_PREPARE:
5080 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07005081 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07005082 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005083 if (!percpu->scribble)
5084 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5085
5086 if (!percpu->scribble ||
5087 (conf->level == 6 && !percpu->spare_page)) {
5088 safe_put_page(percpu->spare_page);
5089 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005090 pr_err("%s: failed memory allocation for cpu%ld\n",
5091 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005092 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005093 }
5094 break;
5095 case CPU_DEAD:
5096 case CPU_DEAD_FROZEN:
5097 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005098 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005099 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005100 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005101 break;
5102 default:
5103 break;
5104 }
5105 return NOTIFY_OK;
5106}
5107#endif
5108
NeilBrownd1688a62011-10-11 16:49:52 +11005109static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005110{
5111 unsigned long cpu;
5112 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005113 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005114 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005115 int err;
5116
Dan Williams36d1c642009-07-14 11:48:22 -07005117 allcpus = alloc_percpu(struct raid5_percpu);
5118 if (!allcpus)
5119 return -ENOMEM;
5120 conf->percpu = allcpus;
5121
5122 get_online_cpus();
5123 err = 0;
5124 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005125 if (conf->level == 6) {
5126 spare_page = alloc_page(GFP_KERNEL);
5127 if (!spare_page) {
5128 err = -ENOMEM;
5129 break;
5130 }
5131 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5132 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005133 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005134 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005135 err = -ENOMEM;
5136 break;
5137 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005138 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005139 }
5140#ifdef CONFIG_HOTPLUG_CPU
5141 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5142 conf->cpu_notify.priority = 0;
5143 if (err == 0)
5144 err = register_cpu_notifier(&conf->cpu_notify);
5145#endif
5146 put_online_cpus();
5147
5148 return err;
5149}
5150
NeilBrownd1688a62011-10-11 16:49:52 +11005151static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152{
NeilBrownd1688a62011-10-11 16:49:52 +11005153 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005154 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005155 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005157 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158
NeilBrown91adb562009-03-31 14:39:39 +11005159 if (mddev->new_level != 5
5160 && mddev->new_level != 4
5161 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005162 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005163 mdname(mddev), mddev->new_level);
5164 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 }
NeilBrown91adb562009-03-31 14:39:39 +11005166 if ((mddev->new_level == 5
5167 && !algorithm_valid_raid5(mddev->new_layout)) ||
5168 (mddev->new_level == 6
5169 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005170 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005171 mdname(mddev), mddev->new_layout);
5172 return ERR_PTR(-EIO);
5173 }
5174 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005175 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005176 mdname(mddev), mddev->raid_disks);
5177 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179
Andre Noll664e7c42009-06-18 08:45:27 +10005180 if (!mddev->new_chunk_sectors ||
5181 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5182 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005183 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5184 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005185 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005186 }
5187
NeilBrownd1688a62011-10-11 16:49:52 +11005188 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005189 if (conf == NULL)
5190 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005191 spin_lock_init(&conf->device_lock);
5192 init_waitqueue_head(&conf->wait_for_stripe);
5193 init_waitqueue_head(&conf->wait_for_overlap);
5194 INIT_LIST_HEAD(&conf->handle_list);
5195 INIT_LIST_HEAD(&conf->hold_list);
5196 INIT_LIST_HEAD(&conf->delayed_list);
5197 INIT_LIST_HEAD(&conf->bitmap_list);
5198 INIT_LIST_HEAD(&conf->inactive_list);
5199 atomic_set(&conf->active_stripes, 0);
5200 atomic_set(&conf->preread_active_stripes, 0);
5201 atomic_set(&conf->active_aligned_reads, 0);
5202 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005203 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005204
5205 conf->raid_disks = mddev->raid_disks;
5206 if (mddev->reshape_position == MaxSector)
5207 conf->previous_raid_disks = mddev->raid_disks;
5208 else
5209 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005210 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5211 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005212
NeilBrown5e5e3e72009-10-16 16:35:30 +11005213 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005214 GFP_KERNEL);
5215 if (!conf->disks)
5216 goto abort;
5217
5218 conf->mddev = mddev;
5219
5220 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5221 goto abort;
5222
Dan Williams36d1c642009-07-14 11:48:22 -07005223 conf->level = mddev->new_level;
5224 if (raid5_alloc_percpu(conf) != 0)
5225 goto abort;
5226
NeilBrown0c55e022010-05-03 14:09:02 +10005227 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005228
NeilBrowndafb20f2012-03-19 12:46:39 +11005229 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005230 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005231 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005232 || raid_disk < 0)
5233 continue;
5234 disk = conf->disks + raid_disk;
5235
NeilBrown17045f52011-12-23 10:17:53 +11005236 if (test_bit(Replacement, &rdev->flags)) {
5237 if (disk->replacement)
5238 goto abort;
5239 disk->replacement = rdev;
5240 } else {
5241 if (disk->rdev)
5242 goto abort;
5243 disk->rdev = rdev;
5244 }
NeilBrown91adb562009-03-31 14:39:39 +11005245
5246 if (test_bit(In_sync, &rdev->flags)) {
5247 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005248 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5249 " disk %d\n",
5250 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005251 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005252 /* Cannot rely on bitmap to complete recovery */
5253 conf->fullsync = 1;
5254 }
5255
Andre Noll09c9e5f2009-06-18 08:45:55 +10005256 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005257 conf->level = mddev->new_level;
5258 if (conf->level == 6)
5259 conf->max_degraded = 2;
5260 else
5261 conf->max_degraded = 1;
5262 conf->algorithm = mddev->new_layout;
5263 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005264 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005265 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005266 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005267 conf->prev_algo = mddev->layout;
5268 }
NeilBrown91adb562009-03-31 14:39:39 +11005269
5270 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005271 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005272 if (grow_stripes(conf, conf->max_nr_stripes)) {
5273 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005274 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5275 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005276 goto abort;
5277 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005278 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5279 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005280
NeilBrown02326052012-07-03 15:56:52 +10005281 sprintf(pers_name, "raid%d", mddev->new_level);
5282 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005283 if (!conf->thread) {
5284 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005285 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005286 mdname(mddev));
5287 goto abort;
5288 }
5289
5290 return conf;
5291
5292 abort:
5293 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005294 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005295 return ERR_PTR(-EIO);
5296 } else
5297 return ERR_PTR(-ENOMEM);
5298}
5299
NeilBrownc148ffd2009-11-13 17:47:00 +11005300
5301static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5302{
5303 switch (algo) {
5304 case ALGORITHM_PARITY_0:
5305 if (raid_disk < max_degraded)
5306 return 1;
5307 break;
5308 case ALGORITHM_PARITY_N:
5309 if (raid_disk >= raid_disks - max_degraded)
5310 return 1;
5311 break;
5312 case ALGORITHM_PARITY_0_6:
5313 if (raid_disk == 0 ||
5314 raid_disk == raid_disks - 1)
5315 return 1;
5316 break;
5317 case ALGORITHM_LEFT_ASYMMETRIC_6:
5318 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5319 case ALGORITHM_LEFT_SYMMETRIC_6:
5320 case ALGORITHM_RIGHT_SYMMETRIC_6:
5321 if (raid_disk == raid_disks - 1)
5322 return 1;
5323 }
5324 return 0;
5325}
5326
NeilBrownfd01b882011-10-11 16:47:53 +11005327static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005328{
NeilBrownd1688a62011-10-11 16:49:52 +11005329 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005330 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005331 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005332 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005333 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005334 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005335 long long min_offset_diff = 0;
5336 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005337
Andre Noll8c6ac862009-06-18 08:48:06 +10005338 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005339 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005340 " -- starting background reconstruction\n",
5341 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005342
5343 rdev_for_each(rdev, mddev) {
5344 long long diff;
5345 if (rdev->raid_disk < 0)
5346 continue;
5347 diff = (rdev->new_data_offset - rdev->data_offset);
5348 if (first) {
5349 min_offset_diff = diff;
5350 first = 0;
5351 } else if (mddev->reshape_backwards &&
5352 diff < min_offset_diff)
5353 min_offset_diff = diff;
5354 else if (!mddev->reshape_backwards &&
5355 diff > min_offset_diff)
5356 min_offset_diff = diff;
5357 }
5358
NeilBrownf6705572006-03-27 01:18:11 -08005359 if (mddev->reshape_position != MaxSector) {
5360 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005361 * Difficulties arise if the stripe we would write to
5362 * next is at or after the stripe we would read from next.
5363 * For a reshape that changes the number of devices, this
5364 * is only possible for a very short time, and mdadm makes
5365 * sure that time appears to have past before assembling
5366 * the array. So we fail if that time hasn't passed.
5367 * For a reshape that keeps the number of devices the same
5368 * mdadm must be monitoring the reshape can keeping the
5369 * critical areas read-only and backed up. It will start
5370 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005371 */
5372 sector_t here_new, here_old;
5373 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005374 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005375
NeilBrown88ce4932009-03-31 15:24:23 +11005376 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005377 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005378 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005379 mdname(mddev));
5380 return -EINVAL;
5381 }
NeilBrownf6705572006-03-27 01:18:11 -08005382 old_disks = mddev->raid_disks - mddev->delta_disks;
5383 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005384 * further up in new geometry must map after here in old
5385 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005386 */
5387 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005388 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005389 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005390 printk(KERN_ERR "md/raid:%s: reshape_position not "
5391 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005392 return -EINVAL;
5393 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005394 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005395 /* here_new is the stripe we will write to */
5396 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005397 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005398 (old_disks-max_degraded));
5399 /* here_old is the first stripe that we might need to read
5400 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005401 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005402 if ((here_new * mddev->new_chunk_sectors !=
5403 here_old * mddev->chunk_sectors)) {
5404 printk(KERN_ERR "md/raid:%s: reshape position is"
5405 " confused - aborting\n", mdname(mddev));
5406 return -EINVAL;
5407 }
NeilBrown67ac6012009-08-13 10:06:24 +10005408 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005409 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005410 * and taking constant backups.
5411 * mdadm always starts a situation like this in
5412 * readonly mode so it can take control before
5413 * allowing any writes. So just check for that.
5414 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005415 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5416 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5417 /* not really in-place - so OK */;
5418 else if (mddev->ro == 0) {
5419 printk(KERN_ERR "md/raid:%s: in-place reshape "
5420 "must be started in read-only mode "
5421 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005422 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005423 return -EINVAL;
5424 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005425 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005426 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005427 here_old * mddev->chunk_sectors)
5428 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005429 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005430 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005431 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5432 "auto-recovery - aborting.\n",
5433 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005434 return -EINVAL;
5435 }
NeilBrown0c55e022010-05-03 14:09:02 +10005436 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5437 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005438 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005439 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005440 BUG_ON(mddev->level != mddev->new_level);
5441 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005442 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005443 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005444 }
5445
NeilBrown245f46c2009-03-31 14:39:39 +11005446 if (mddev->private == NULL)
5447 conf = setup_conf(mddev);
5448 else
5449 conf = mddev->private;
5450
NeilBrown91adb562009-03-31 14:39:39 +11005451 if (IS_ERR(conf))
5452 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005453
NeilBrownb5254dd2012-05-21 09:27:01 +10005454 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005455 mddev->thread = conf->thread;
5456 conf->thread = NULL;
5457 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458
NeilBrown17045f52011-12-23 10:17:53 +11005459 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5460 i++) {
5461 rdev = conf->disks[i].rdev;
5462 if (!rdev && conf->disks[i].replacement) {
5463 /* The replacement is all we have yet */
5464 rdev = conf->disks[i].replacement;
5465 conf->disks[i].replacement = NULL;
5466 clear_bit(Replacement, &rdev->flags);
5467 conf->disks[i].rdev = rdev;
5468 }
5469 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005470 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005471 if (conf->disks[i].replacement &&
5472 conf->reshape_progress != MaxSector) {
5473 /* replacements and reshape simply do not mix. */
5474 printk(KERN_ERR "md: cannot handle concurrent "
5475 "replacement and reshape.\n");
5476 goto abort;
5477 }
NeilBrown2f115882010-06-17 17:41:03 +10005478 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005479 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005480 continue;
5481 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005482 /* This disc is not fully in-sync. However if it
5483 * just stored parity (beyond the recovery_offset),
5484 * when we don't need to be concerned about the
5485 * array being dirty.
5486 * When reshape goes 'backwards', we never have
5487 * partially completed devices, so we only need
5488 * to worry about reshape going forwards.
5489 */
5490 /* Hack because v0.91 doesn't store recovery_offset properly. */
5491 if (mddev->major_version == 0 &&
5492 mddev->minor_version > 90)
5493 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005494
NeilBrownc148ffd2009-11-13 17:47:00 +11005495 if (rdev->recovery_offset < reshape_offset) {
5496 /* We need to check old and new layout */
5497 if (!only_parity(rdev->raid_disk,
5498 conf->algorithm,
5499 conf->raid_disks,
5500 conf->max_degraded))
5501 continue;
5502 }
5503 if (!only_parity(rdev->raid_disk,
5504 conf->prev_algo,
5505 conf->previous_raid_disks,
5506 conf->max_degraded))
5507 continue;
5508 dirty_parity_disks++;
5509 }
NeilBrown91adb562009-03-31 14:39:39 +11005510
NeilBrown17045f52011-12-23 10:17:53 +11005511 /*
5512 * 0 for a fully functional array, 1 or 2 for a degraded array.
5513 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005514 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515
NeilBrown674806d2010-06-16 17:17:53 +10005516 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005517 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005519 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 goto abort;
5521 }
5522
NeilBrown91adb562009-03-31 14:39:39 +11005523 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005524 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005525 mddev->resync_max_sectors = mddev->dev_sectors;
5526
NeilBrownc148ffd2009-11-13 17:47:00 +11005527 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005529 if (mddev->ok_start_degraded)
5530 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005531 "md/raid:%s: starting dirty degraded array"
5532 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005533 mdname(mddev));
5534 else {
5535 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005536 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005537 mdname(mddev));
5538 goto abort;
5539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 }
5541
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005543 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5544 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005545 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5546 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 else
NeilBrown0c55e022010-05-03 14:09:02 +10005548 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5549 " out of %d devices, algorithm %d\n",
5550 mdname(mddev), conf->level,
5551 mddev->raid_disks - mddev->degraded,
5552 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553
5554 print_raid5_conf(conf);
5555
NeilBrownfef9c612009-03-31 15:16:46 +11005556 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005557 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005558 atomic_set(&conf->reshape_stripes, 0);
5559 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5560 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5561 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5562 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5563 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005564 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005565 }
5566
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567
5568 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005569 if (mddev->to_remove == &raid5_attrs_group)
5570 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005571 else if (mddev->kobj.sd &&
5572 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005573 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005574 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005575 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005576 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5577
5578 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005579 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005580 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005581 /* read-ahead size must cover two whole stripes, which
5582 * is 2 * (datadisks) * chunksize where 'n' is the
5583 * number of raid devices
5584 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5586 int stripe = data_disks *
5587 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5588 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5589 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005590
5591 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005592
5593 mddev->queue->backing_dev_info.congested_data = mddev;
5594 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005595
5596 chunk_size = mddev->chunk_sectors << 9;
5597 blk_queue_io_min(mddev->queue, chunk_size);
5598 blk_queue_io_opt(mddev->queue, chunk_size *
5599 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005600 /*
5601 * We can only discard a whole stripe. It doesn't make sense to
5602 * discard data disk but write parity disk
5603 */
5604 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11005605 /* Round up to power of 2, as discard handling
5606 * currently assumes that */
5607 while ((stripe-1) & stripe)
5608 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11005609 mddev->queue->limits.discard_alignment = stripe;
5610 mddev->queue->limits.discard_granularity = stripe;
5611 /*
5612 * unaligned part of discard request will be ignored, so can't
5613 * guarantee discard_zerors_data
5614 */
5615 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005616
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07005617 blk_queue_max_write_same_sectors(mddev->queue, 0);
5618
NeilBrown05616be2012-05-21 09:27:00 +10005619 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005620 disk_stack_limits(mddev->gendisk, rdev->bdev,
5621 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005622 disk_stack_limits(mddev->gendisk, rdev->bdev,
5623 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005624 /*
5625 * discard_zeroes_data is required, otherwise data
5626 * could be lost. Consider a scenario: discard a stripe
5627 * (the stripe could be inconsistent if
5628 * discard_zeroes_data is 0); write one disk of the
5629 * stripe (the stripe could be inconsistent again
5630 * depending on which disks are used to calculate
5631 * parity); the disk is broken; The stripe data of this
5632 * disk is lost.
5633 */
5634 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5635 !bdev_get_queue(rdev->bdev)->
5636 limits.discard_zeroes_data)
5637 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10005638 }
Shaohua Li620125f2012-10-11 13:49:05 +11005639
5640 if (discard_supported &&
5641 mddev->queue->limits.max_discard_sectors >= stripe &&
5642 mddev->queue->limits.discard_granularity >= stripe)
5643 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5644 mddev->queue);
5645 else
5646 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5647 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 }
5649
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 return 0;
5651abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005652 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005653 print_raid5_conf(conf);
5654 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005656 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 return -EIO;
5658}
5659
NeilBrownfd01b882011-10-11 16:47:53 +11005660static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661{
NeilBrownd1688a62011-10-11 16:49:52 +11005662 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663
NeilBrown01f96c02011-09-21 15:30:20 +10005664 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005665 if (mddev->queue)
5666 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005667 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005668 mddev->private = NULL;
5669 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 return 0;
5671}
5672
NeilBrownfd01b882011-10-11 16:47:53 +11005673static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674{
NeilBrownd1688a62011-10-11 16:49:52 +11005675 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 int i;
5677
Andre Noll9d8f0362009-06-18 08:45:01 +10005678 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5679 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005680 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 for (i = 0; i < conf->raid_disks; i++)
5682 seq_printf (seq, "%s",
5683 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005684 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686}
5687
NeilBrownd1688a62011-10-11 16:49:52 +11005688static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689{
5690 int i;
5691 struct disk_info *tmp;
5692
NeilBrown0c55e022010-05-03 14:09:02 +10005693 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 if (!conf) {
5695 printk("(conf==NULL)\n");
5696 return;
5697 }
NeilBrown0c55e022010-05-03 14:09:02 +10005698 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5699 conf->raid_disks,
5700 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701
5702 for (i = 0; i < conf->raid_disks; i++) {
5703 char b[BDEVNAME_SIZE];
5704 tmp = conf->disks + i;
5705 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005706 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5707 i, !test_bit(Faulty, &tmp->rdev->flags),
5708 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 }
5710}
5711
NeilBrownfd01b882011-10-11 16:47:53 +11005712static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713{
5714 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005715 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005717 int count = 0;
5718 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719
5720 for (i = 0; i < conf->raid_disks; i++) {
5721 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005722 if (tmp->replacement
5723 && tmp->replacement->recovery_offset == MaxSector
5724 && !test_bit(Faulty, &tmp->replacement->flags)
5725 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5726 /* Replacement has just become active. */
5727 if (!tmp->rdev
5728 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5729 count++;
5730 if (tmp->rdev) {
5731 /* Replaced device not technically faulty,
5732 * but we need to be sure it gets removed
5733 * and never re-added.
5734 */
5735 set_bit(Faulty, &tmp->rdev->flags);
5736 sysfs_notify_dirent_safe(
5737 tmp->rdev->sysfs_state);
5738 }
5739 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5740 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005741 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005742 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005743 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005744 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005745 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 }
5747 }
NeilBrown6b965622010-08-18 11:56:59 +10005748 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005749 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005750 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005752 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753}
5754
NeilBrownb8321b62011-12-23 10:17:51 +11005755static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756{
NeilBrownd1688a62011-10-11 16:49:52 +11005757 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005759 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005760 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 struct disk_info *p = conf->disks + number;
5762
5763 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005764 if (rdev == p->rdev)
5765 rdevp = &p->rdev;
5766 else if (rdev == p->replacement)
5767 rdevp = &p->replacement;
5768 else
5769 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005770
NeilBrown657e3e42011-12-23 10:17:52 +11005771 if (number >= conf->raid_disks &&
5772 conf->reshape_progress == MaxSector)
5773 clear_bit(In_sync, &rdev->flags);
5774
5775 if (test_bit(In_sync, &rdev->flags) ||
5776 atomic_read(&rdev->nr_pending)) {
5777 err = -EBUSY;
5778 goto abort;
5779 }
5780 /* Only remove non-faulty devices if recovery
5781 * isn't possible.
5782 */
5783 if (!test_bit(Faulty, &rdev->flags) &&
5784 mddev->recovery_disabled != conf->recovery_disabled &&
5785 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005786 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005787 number < conf->raid_disks) {
5788 err = -EBUSY;
5789 goto abort;
5790 }
5791 *rdevp = NULL;
5792 synchronize_rcu();
5793 if (atomic_read(&rdev->nr_pending)) {
5794 /* lost the race, try later */
5795 err = -EBUSY;
5796 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005797 } else if (p->replacement) {
5798 /* We must have just cleared 'rdev' */
5799 p->rdev = p->replacement;
5800 clear_bit(Replacement, &p->replacement->flags);
5801 smp_mb(); /* Make sure other CPUs may see both as identical
5802 * but will never see neither - if they are careful
5803 */
5804 p->replacement = NULL;
5805 clear_bit(WantReplacement, &rdev->flags);
5806 } else
5807 /* We might have just removed the Replacement as faulty-
5808 * clear the bit just in case
5809 */
5810 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811abort:
5812
5813 print_raid5_conf(conf);
5814 return err;
5815}
5816
NeilBrownfd01b882011-10-11 16:47:53 +11005817static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818{
NeilBrownd1688a62011-10-11 16:49:52 +11005819 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005820 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 int disk;
5822 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005823 int first = 0;
5824 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
NeilBrown7f0da592011-07-28 11:39:22 +10005826 if (mddev->recovery_disabled == conf->recovery_disabled)
5827 return -EBUSY;
5828
NeilBrowndc10c642012-03-19 12:46:37 +11005829 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005831 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
Neil Brown6c2fce22008-06-28 08:31:31 +10005833 if (rdev->raid_disk >= 0)
5834 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835
5836 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005837 * find the disk ... but prefer rdev->saved_raid_disk
5838 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005840 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005841 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005842 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005843 first = rdev->saved_raid_disk;
5844
5845 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005846 p = conf->disks + disk;
5847 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005848 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005850 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005851 if (rdev->saved_raid_disk != disk)
5852 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005853 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005854 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005856 }
5857 for (disk = first; disk <= last; disk++) {
5858 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005859 if (test_bit(WantReplacement, &p->rdev->flags) &&
5860 p->replacement == NULL) {
5861 clear_bit(In_sync, &rdev->flags);
5862 set_bit(Replacement, &rdev->flags);
5863 rdev->raid_disk = disk;
5864 err = 0;
5865 conf->fullsync = 1;
5866 rcu_assign_pointer(p->replacement, rdev);
5867 break;
5868 }
5869 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005870out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005872 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873}
5874
NeilBrownfd01b882011-10-11 16:47:53 +11005875static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876{
5877 /* no resync is happening, and there is enough space
5878 * on all devices, so we can resize.
5879 * We need to make sure resync covers any new space.
5880 * If the array is shrinking we should possibly wait until
5881 * any io in the removed space completes, but it hardly seems
5882 * worth it.
5883 */
NeilBrowna4a61252012-05-22 13:55:27 +10005884 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005885 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005886 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5887 if (mddev->external_size &&
5888 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005889 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005890 if (mddev->bitmap) {
5891 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5892 if (ret)
5893 return ret;
5894 }
5895 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005896 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005897 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005898 if (sectors > mddev->dev_sectors &&
5899 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005900 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5902 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005903 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005904 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 return 0;
5906}
5907
NeilBrownfd01b882011-10-11 16:47:53 +11005908static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005909{
5910 /* Can only proceed if there are plenty of stripe_heads.
5911 * We need a minimum of one full stripe,, and for sensible progress
5912 * it is best to have about 4 times that.
5913 * If we require 4 times, then the default 256 4K stripe_heads will
5914 * allow for chunk sizes up to 256K, which is probably OK.
5915 * If the chunk size is greater, user-space should request more
5916 * stripe_heads first.
5917 */
NeilBrownd1688a62011-10-11 16:49:52 +11005918 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005919 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5920 > conf->max_nr_stripes ||
5921 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5922 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005923 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5924 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005925 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5926 / STRIPE_SIZE)*4);
5927 return 0;
5928 }
5929 return 1;
5930}
5931
NeilBrownfd01b882011-10-11 16:47:53 +11005932static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005933{
NeilBrownd1688a62011-10-11 16:49:52 +11005934 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005935
NeilBrown88ce4932009-03-31 15:24:23 +11005936 if (mddev->delta_disks == 0 &&
5937 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005938 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005939 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005940 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005941 return -EINVAL;
5942 if (mddev->delta_disks < 0) {
5943 /* We might be able to shrink, but the devices must
5944 * be made bigger first.
5945 * For raid6, 4 is the minimum size.
5946 * Otherwise 2 is the minimum
5947 */
5948 int min = 2;
5949 if (mddev->level == 6)
5950 min = 4;
5951 if (mddev->raid_disks + mddev->delta_disks < min)
5952 return -EINVAL;
5953 }
NeilBrown29269552006-03-27 01:18:10 -08005954
NeilBrown01ee22b2009-06-18 08:47:20 +10005955 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005956 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005957
NeilBrowne56108d62012-10-11 14:24:13 +11005958 return resize_stripes(conf, (conf->previous_raid_disks
5959 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08005960}
5961
NeilBrownfd01b882011-10-11 16:47:53 +11005962static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005963{
NeilBrownd1688a62011-10-11 16:49:52 +11005964 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005965 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005966 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005967 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005968
NeilBrownf4168852007-02-28 20:11:53 -08005969 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005970 return -EBUSY;
5971
NeilBrown01ee22b2009-06-18 08:47:20 +10005972 if (!check_stripe_cache(mddev))
5973 return -ENOSPC;
5974
NeilBrown30b67642012-05-22 13:55:28 +10005975 if (has_failed(conf))
5976 return -EINVAL;
5977
NeilBrownc6563a82012-05-21 09:27:00 +10005978 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005979 if (!test_bit(In_sync, &rdev->flags)
5980 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005981 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005982 }
NeilBrown63c70c42006-03-27 01:18:13 -08005983
NeilBrownf4168852007-02-28 20:11:53 -08005984 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005985 /* Not enough devices even to make a degraded array
5986 * of that size
5987 */
5988 return -EINVAL;
5989
NeilBrownec32a2b2009-03-31 15:17:38 +11005990 /* Refuse to reduce size of the array. Any reductions in
5991 * array size must be through explicit setting of array_size
5992 * attribute.
5993 */
5994 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5995 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005996 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005997 "before number of disks\n", mdname(mddev));
5998 return -EINVAL;
5999 }
6000
NeilBrownf6705572006-03-27 01:18:11 -08006001 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08006002 spin_lock_irq(&conf->device_lock);
6003 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08006004 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006005 conf->prev_chunk_sectors = conf->chunk_sectors;
6006 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11006007 conf->prev_algo = conf->algorithm;
6008 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10006009 conf->generation++;
6010 /* Code that selects data_offset needs to see the generation update
6011 * if reshape_progress has been set - so a memory barrier needed.
6012 */
6013 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10006014 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11006015 conf->reshape_progress = raid5_size(mddev, 0, 0);
6016 else
6017 conf->reshape_progress = 0;
6018 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08006019 spin_unlock_irq(&conf->device_lock);
6020
6021 /* Add some new drives, as many as will fit.
6022 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10006023 * Don't add devices if we are reducing the number of
6024 * devices in the array. This is because it is not possible
6025 * to correctly record the "partially reconstructed" state of
6026 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08006027 */
NeilBrown87a8dec2011-01-31 11:57:43 +11006028 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11006029 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11006030 if (rdev->raid_disk < 0 &&
6031 !test_bit(Faulty, &rdev->flags)) {
6032 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11006033 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11006034 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11006035 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11006036 else
NeilBrown87a8dec2011-01-31 11:57:43 +11006037 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10006038
6039 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11006040 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11006041 }
NeilBrown87a8dec2011-01-31 11:57:43 +11006042 } else if (rdev->raid_disk >= conf->previous_raid_disks
6043 && !test_bit(Faulty, &rdev->flags)) {
6044 /* This is a spare that was manually added */
6045 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11006046 }
NeilBrown29269552006-03-27 01:18:10 -08006047
NeilBrown87a8dec2011-01-31 11:57:43 +11006048 /* When a reshape changes the number of devices,
6049 * ->degraded is measured against the larger of the
6050 * pre and post number of devices.
6051 */
NeilBrownec32a2b2009-03-31 15:17:38 +11006052 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006053 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11006054 spin_unlock_irqrestore(&conf->device_lock, flags);
6055 }
NeilBrown63c70c42006-03-27 01:18:13 -08006056 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10006057 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07006058 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08006059
NeilBrown29269552006-03-27 01:18:10 -08006060 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6061 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6062 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6063 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6064 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006065 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08006066 if (!mddev->sync_thread) {
6067 mddev->recovery = 0;
6068 spin_lock_irq(&conf->device_lock);
6069 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006070 rdev_for_each(rdev, mddev)
6071 rdev->new_data_offset = rdev->data_offset;
6072 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006073 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11006074 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08006075 spin_unlock_irq(&conf->device_lock);
6076 return -EAGAIN;
6077 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006078 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006079 md_wakeup_thread(mddev->sync_thread);
6080 md_new_event(mddev);
6081 return 0;
6082}
NeilBrown29269552006-03-27 01:18:10 -08006083
NeilBrownec32a2b2009-03-31 15:17:38 +11006084/* This is called from the reshape thread and should make any
6085 * changes needed in 'conf'
6086 */
NeilBrownd1688a62011-10-11 16:49:52 +11006087static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006088{
NeilBrown29269552006-03-27 01:18:10 -08006089
NeilBrownf6705572006-03-27 01:18:11 -08006090 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006091 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006092
NeilBrownf6705572006-03-27 01:18:11 -08006093 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006094 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006095 rdev_for_each(rdev, conf->mddev)
6096 rdev->data_offset = rdev->new_data_offset;
6097 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006098 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006099 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006100 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006101
6102 /* read-ahead size must cover two whole stripes, which is
6103 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6104 */
NeilBrown4a5add42010-06-01 19:37:28 +10006105 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006106 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006107 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006108 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006109 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6110 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6111 }
NeilBrown29269552006-03-27 01:18:10 -08006112 }
NeilBrown29269552006-03-27 01:18:10 -08006113}
6114
NeilBrownec32a2b2009-03-31 15:17:38 +11006115/* This is called from the raid5d thread with mddev_lock held.
6116 * It makes config changes to the device.
6117 */
NeilBrownfd01b882011-10-11 16:47:53 +11006118static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006119{
NeilBrownd1688a62011-10-11 16:49:52 +11006120 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006121
6122 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6123
NeilBrownec32a2b2009-03-31 15:17:38 +11006124 if (mddev->delta_disks > 0) {
6125 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6126 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006127 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006128 } else {
6129 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006130 spin_lock_irq(&conf->device_lock);
6131 mddev->degraded = calc_degraded(conf);
6132 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006133 for (d = conf->raid_disks ;
6134 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006135 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006136 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006137 if (rdev)
6138 clear_bit(In_sync, &rdev->flags);
6139 rdev = conf->disks[d].replacement;
6140 if (rdev)
6141 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006142 }
NeilBrowncea9c222009-03-31 15:15:05 +11006143 }
NeilBrown88ce4932009-03-31 15:24:23 +11006144 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006145 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006146 mddev->reshape_position = MaxSector;
6147 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006148 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006149 }
6150}
6151
NeilBrownfd01b882011-10-11 16:47:53 +11006152static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006153{
NeilBrownd1688a62011-10-11 16:49:52 +11006154 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006155
6156 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006157 case 2: /* resume for a suspend */
6158 wake_up(&conf->wait_for_overlap);
6159 break;
6160
NeilBrown72626682005-09-09 16:23:54 -07006161 case 1: /* stop all writes */
6162 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006163 /* '2' tells resync/reshape to pause so that all
6164 * active stripes can drain
6165 */
6166 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006167 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006168 atomic_read(&conf->active_stripes) == 0 &&
6169 atomic_read(&conf->active_aligned_reads) == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01006170 conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006171 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006172 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006173 /* allow reshape to continue */
6174 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006175 break;
6176
6177 case 0: /* re-enable writes */
6178 spin_lock_irq(&conf->device_lock);
6179 conf->quiesce = 0;
6180 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006181 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006182 spin_unlock_irq(&conf->device_lock);
6183 break;
6184 }
NeilBrown72626682005-09-09 16:23:54 -07006185}
NeilBrownb15c2e52006-01-06 00:20:16 -08006186
NeilBrownd562b0c2009-03-31 14:39:39 +11006187
NeilBrownfd01b882011-10-11 16:47:53 +11006188static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006189{
NeilBrowne373ab12011-10-11 16:48:59 +11006190 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006191 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006192
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006193 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006194 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006195 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6196 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006197 return ERR_PTR(-EINVAL);
6198 }
6199
NeilBrowne373ab12011-10-11 16:48:59 +11006200 sectors = raid0_conf->strip_zone[0].zone_end;
6201 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006202 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006203 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006204 mddev->new_layout = ALGORITHM_PARITY_N;
6205 mddev->new_chunk_sectors = mddev->chunk_sectors;
6206 mddev->raid_disks += 1;
6207 mddev->delta_disks = 1;
6208 /* make sure it will be not marked as dirty */
6209 mddev->recovery_cp = MaxSector;
6210
6211 return setup_conf(mddev);
6212}
6213
6214
NeilBrownfd01b882011-10-11 16:47:53 +11006215static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006216{
6217 int chunksect;
6218
6219 if (mddev->raid_disks != 2 ||
6220 mddev->degraded > 1)
6221 return ERR_PTR(-EINVAL);
6222
6223 /* Should check if there are write-behind devices? */
6224
6225 chunksect = 64*2; /* 64K by default */
6226
6227 /* The array must be an exact multiple of chunksize */
6228 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6229 chunksect >>= 1;
6230
6231 if ((chunksect<<9) < STRIPE_SIZE)
6232 /* array size does not allow a suitable chunk size */
6233 return ERR_PTR(-EINVAL);
6234
6235 mddev->new_level = 5;
6236 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006237 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006238
6239 return setup_conf(mddev);
6240}
6241
NeilBrownfd01b882011-10-11 16:47:53 +11006242static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006243{
6244 int new_layout;
6245
6246 switch (mddev->layout) {
6247 case ALGORITHM_LEFT_ASYMMETRIC_6:
6248 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6249 break;
6250 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6251 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6252 break;
6253 case ALGORITHM_LEFT_SYMMETRIC_6:
6254 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6255 break;
6256 case ALGORITHM_RIGHT_SYMMETRIC_6:
6257 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6258 break;
6259 case ALGORITHM_PARITY_0_6:
6260 new_layout = ALGORITHM_PARITY_0;
6261 break;
6262 case ALGORITHM_PARITY_N:
6263 new_layout = ALGORITHM_PARITY_N;
6264 break;
6265 default:
6266 return ERR_PTR(-EINVAL);
6267 }
6268 mddev->new_level = 5;
6269 mddev->new_layout = new_layout;
6270 mddev->delta_disks = -1;
6271 mddev->raid_disks -= 1;
6272 return setup_conf(mddev);
6273}
6274
NeilBrownd562b0c2009-03-31 14:39:39 +11006275
NeilBrownfd01b882011-10-11 16:47:53 +11006276static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006277{
NeilBrown88ce4932009-03-31 15:24:23 +11006278 /* For a 2-drive array, the layout and chunk size can be changed
6279 * immediately as not restriping is needed.
6280 * For larger arrays we record the new value - after validation
6281 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006282 */
NeilBrownd1688a62011-10-11 16:49:52 +11006283 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006284 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006285
NeilBrown597a7112009-06-18 08:47:42 +10006286 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006287 return -EINVAL;
6288 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006289 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006290 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006291 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006292 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006293 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006294 /* not factor of array size */
6295 return -EINVAL;
6296 }
6297
6298 /* They look valid */
6299
NeilBrown88ce4932009-03-31 15:24:23 +11006300 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006301 /* can make the change immediately */
6302 if (mddev->new_layout >= 0) {
6303 conf->algorithm = mddev->new_layout;
6304 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006305 }
6306 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006307 conf->chunk_sectors = new_chunk ;
6308 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006309 }
6310 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6311 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006312 }
NeilBrown50ac1682009-06-18 08:47:55 +10006313 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006314}
6315
NeilBrownfd01b882011-10-11 16:47:53 +11006316static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006317{
NeilBrown597a7112009-06-18 08:47:42 +10006318 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006319
NeilBrown597a7112009-06-18 08:47:42 +10006320 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006321 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006322 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006323 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006324 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006325 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006326 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006327 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006328 /* not factor of array size */
6329 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006330 }
NeilBrown88ce4932009-03-31 15:24:23 +11006331
6332 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006333 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006334}
6335
NeilBrownfd01b882011-10-11 16:47:53 +11006336static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006337{
6338 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006339 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006340 * raid1 - if there are two drives. We need to know the chunk size
6341 * raid4 - trivial - just use a raid4 layout.
6342 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006343 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006344 if (mddev->level == 0)
6345 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006346 if (mddev->level == 1)
6347 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006348 if (mddev->level == 4) {
6349 mddev->new_layout = ALGORITHM_PARITY_N;
6350 mddev->new_level = 5;
6351 return setup_conf(mddev);
6352 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006353 if (mddev->level == 6)
6354 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006355
6356 return ERR_PTR(-EINVAL);
6357}
6358
NeilBrownfd01b882011-10-11 16:47:53 +11006359static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006360{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006361 /* raid4 can take over:
6362 * raid0 - if there is only one strip zone
6363 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006364 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006365 if (mddev->level == 0)
6366 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006367 if (mddev->level == 5 &&
6368 mddev->layout == ALGORITHM_PARITY_N) {
6369 mddev->new_layout = 0;
6370 mddev->new_level = 4;
6371 return setup_conf(mddev);
6372 }
6373 return ERR_PTR(-EINVAL);
6374}
NeilBrownd562b0c2009-03-31 14:39:39 +11006375
NeilBrown84fc4b52011-10-11 16:49:58 +11006376static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006377
NeilBrownfd01b882011-10-11 16:47:53 +11006378static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006379{
6380 /* Currently can only take over a raid5. We map the
6381 * personality to an equivalent raid6 personality
6382 * with the Q block at the end.
6383 */
6384 int new_layout;
6385
6386 if (mddev->pers != &raid5_personality)
6387 return ERR_PTR(-EINVAL);
6388 if (mddev->degraded > 1)
6389 return ERR_PTR(-EINVAL);
6390 if (mddev->raid_disks > 253)
6391 return ERR_PTR(-EINVAL);
6392 if (mddev->raid_disks < 3)
6393 return ERR_PTR(-EINVAL);
6394
6395 switch (mddev->layout) {
6396 case ALGORITHM_LEFT_ASYMMETRIC:
6397 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6398 break;
6399 case ALGORITHM_RIGHT_ASYMMETRIC:
6400 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6401 break;
6402 case ALGORITHM_LEFT_SYMMETRIC:
6403 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6404 break;
6405 case ALGORITHM_RIGHT_SYMMETRIC:
6406 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6407 break;
6408 case ALGORITHM_PARITY_0:
6409 new_layout = ALGORITHM_PARITY_0_6;
6410 break;
6411 case ALGORITHM_PARITY_N:
6412 new_layout = ALGORITHM_PARITY_N;
6413 break;
6414 default:
6415 return ERR_PTR(-EINVAL);
6416 }
6417 mddev->new_level = 6;
6418 mddev->new_layout = new_layout;
6419 mddev->delta_disks = 1;
6420 mddev->raid_disks += 1;
6421 return setup_conf(mddev);
6422}
6423
6424
NeilBrown84fc4b52011-10-11 16:49:58 +11006425static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006426{
6427 .name = "raid6",
6428 .level = 6,
6429 .owner = THIS_MODULE,
6430 .make_request = make_request,
6431 .run = run,
6432 .stop = stop,
6433 .status = status,
6434 .error_handler = error,
6435 .hot_add_disk = raid5_add_disk,
6436 .hot_remove_disk= raid5_remove_disk,
6437 .spare_active = raid5_spare_active,
6438 .sync_request = sync_request,
6439 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006440 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006441 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006442 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006443 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006444 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006445 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006446};
NeilBrown84fc4b52011-10-11 16:49:58 +11006447static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448{
6449 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006450 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 .owner = THIS_MODULE,
6452 .make_request = make_request,
6453 .run = run,
6454 .stop = stop,
6455 .status = status,
6456 .error_handler = error,
6457 .hot_add_disk = raid5_add_disk,
6458 .hot_remove_disk= raid5_remove_disk,
6459 .spare_active = raid5_spare_active,
6460 .sync_request = sync_request,
6461 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006462 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006463 .check_reshape = raid5_check_reshape,
6464 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006465 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006466 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006467 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468};
6469
NeilBrown84fc4b52011-10-11 16:49:58 +11006470static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471{
NeilBrown2604b702006-01-06 00:20:36 -08006472 .name = "raid4",
6473 .level = 4,
6474 .owner = THIS_MODULE,
6475 .make_request = make_request,
6476 .run = run,
6477 .stop = stop,
6478 .status = status,
6479 .error_handler = error,
6480 .hot_add_disk = raid5_add_disk,
6481 .hot_remove_disk= raid5_remove_disk,
6482 .spare_active = raid5_spare_active,
6483 .sync_request = sync_request,
6484 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006485 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006486 .check_reshape = raid5_check_reshape,
6487 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006488 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006489 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006490 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006491};
6492
6493static int __init raid5_init(void)
6494{
NeilBrown16a53ec2006-06-26 00:27:38 -07006495 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006496 register_md_personality(&raid5_personality);
6497 register_md_personality(&raid4_personality);
6498 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499}
6500
NeilBrown2604b702006-01-06 00:20:36 -08006501static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502{
NeilBrown16a53ec2006-06-26 00:27:38 -07006503 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006504 unregister_md_personality(&raid5_personality);
6505 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506}
6507
6508module_init(raid5_init);
6509module_exit(raid5_exit);
6510MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006511MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006513MODULE_ALIAS("md-raid5");
6514MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006515MODULE_ALIAS("md-level-5");
6516MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006517MODULE_ALIAS("md-personality-8"); /* RAID6 */
6518MODULE_ALIAS("md-raid6");
6519MODULE_ALIAS("md-level-6");
6520
6521/* This used to be two separate modules, they were: */
6522MODULE_ALIAS("raid5");
6523MODULE_ALIAS("raid6");