blob: 35031c8b2d024294991120855b8bac3e077fce8e [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{
93 int sectors = bio->bi_size >> 9;
94 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;
NeilBrowna9add5d2012-10-31 11:59:09 +1100187 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;
NeilBrownfccddba2006-01-06 00:20:33 -0800368 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Dan Williams45b42332007-07-09 11:56:43 -0700370 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800371 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100372 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700374 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return NULL;
376}
377
NeilBrown674806d2010-06-16 17:17:53 +1000378/*
379 * Need to check if array has failed when deciding whether to:
380 * - start an array
381 * - remove non-faulty devices
382 * - add a spare
383 * - allow a reshape
384 * This determination is simple when no reshape is happening.
385 * However if there is a reshape, we need to carefully check
386 * both the before and after sections.
387 * This is because some failed devices may only affect one
388 * of the two sections, and some non-in_sync devices may
389 * be insync in the section most affected by failed devices.
390 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100391static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000392{
NeilBrown908f4fb2011-12-23 10:17:50 +1100393 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000394 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000395
396 rcu_read_lock();
397 degraded = 0;
398 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100399 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000400 if (rdev && test_bit(Faulty, &rdev->flags))
401 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000402 if (!rdev || test_bit(Faulty, &rdev->flags))
403 degraded++;
404 else if (test_bit(In_sync, &rdev->flags))
405 ;
406 else
407 /* not in-sync or faulty.
408 * If the reshape increases the number of devices,
409 * this is being recovered by the reshape, so
410 * this 'previous' section is not in_sync.
411 * If the number of devices is being reduced however,
412 * the device can only be part of the array if
413 * we are reverting a reshape, so this section will
414 * be in-sync.
415 */
416 if (conf->raid_disks >= conf->previous_raid_disks)
417 degraded++;
418 }
419 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100420 if (conf->raid_disks == conf->previous_raid_disks)
421 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000422 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100423 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000424 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100425 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000426 if (rdev && test_bit(Faulty, &rdev->flags))
427 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000428 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100429 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000430 else if (test_bit(In_sync, &rdev->flags))
431 ;
432 else
433 /* not in-sync or faulty.
434 * If reshape increases the number of devices, this
435 * section has already been recovered, else it
436 * almost certainly hasn't.
437 */
438 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100439 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000440 }
441 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100442 if (degraded2 > degraded)
443 return degraded2;
444 return degraded;
445}
446
447static int has_failed(struct r5conf *conf)
448{
449 int degraded;
450
451 if (conf->mddev->reshape_position == MaxSector)
452 return conf->mddev->degraded > conf->max_degraded;
453
454 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000455 if (degraded > conf->max_degraded)
456 return 1;
457 return 0;
458}
459
NeilBrownb5663ba2009-03-31 14:39:38 +1100460static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100461get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000462 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct stripe_head *sh;
465
Dan Williams45b42332007-07-09 11:56:43 -0700466 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 spin_lock_irq(&conf->device_lock);
469
470 do {
NeilBrown72626682005-09-09 16:23:54 -0700471 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000472 conf->quiesce == 0 || noquiesce,
Lukas Czernereed8c022012-11-30 11:42:40 +0100473 conf->device_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100474 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (!sh) {
476 if (!conf->inactive_blocked)
477 sh = get_free_stripe(conf);
478 if (noblock && sh == NULL)
479 break;
480 if (!sh) {
481 conf->inactive_blocked = 1;
482 wait_event_lock_irq(conf->wait_for_stripe,
483 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800484 (atomic_read(&conf->active_stripes)
485 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 || !conf->inactive_blocked),
Lukas Czernereed8c022012-11-30 11:42:40 +0100487 conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 conf->inactive_blocked = 0;
489 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100490 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 } else {
492 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100493 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000494 && !test_bit(STRIPE_EXPANDING, &sh->state)
495 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 } else {
497 if (!test_bit(STRIPE_HANDLE, &sh->state))
498 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700499 if (list_empty(&sh->lru) &&
500 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700501 BUG();
502 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 }
505 } while (sh == NULL);
506
507 if (sh)
508 atomic_inc(&sh->count);
509
510 spin_unlock_irq(&conf->device_lock);
511 return sh;
512}
513
NeilBrown05616be2012-05-21 09:27:00 +1000514/* Determine if 'data_offset' or 'new_data_offset' should be used
515 * in this stripe_head.
516 */
517static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
518{
519 sector_t progress = conf->reshape_progress;
520 /* Need a memory barrier to make sure we see the value
521 * of conf->generation, or ->data_offset that was set before
522 * reshape_progress was updated.
523 */
524 smp_rmb();
525 if (progress == MaxSector)
526 return 0;
527 if (sh->generation == conf->generation - 1)
528 return 0;
529 /* We are in a reshape, and this is a new-generation stripe,
530 * so use new_data_offset.
531 */
532 return 1;
533}
534
NeilBrown6712ecf2007-09-27 12:47:43 +0200535static void
536raid5_end_read_request(struct bio *bi, int error);
537static void
538raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700539
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000540static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700541{
NeilBrownd1688a62011-10-11 16:49:52 +1100542 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700543 int i, disks = sh->disks;
544
545 might_sleep();
546
547 for (i = disks; i--; ) {
548 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100549 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100550 struct bio *bi, *rbi;
551 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200552 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
553 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
554 rw = WRITE_FUA;
555 else
556 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100557 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100558 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200559 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700560 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100561 else if (test_and_clear_bit(R5_WantReplace,
562 &sh->dev[i].flags)) {
563 rw = WRITE;
564 replace_only = 1;
565 } else
Dan Williams91c00922007-01-02 13:52:30 -0700566 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000567 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
568 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700569
570 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100571 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700572
573 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100574 rbi->bi_rw = rw;
575 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700576 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100577 rbi->bi_end_io = raid5_end_write_request;
578 } else
Dan Williams91c00922007-01-02 13:52:30 -0700579 bi->bi_end_io = raid5_end_read_request;
580
581 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100582 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100583 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
584 rdev = rcu_dereference(conf->disks[i].rdev);
585 if (!rdev) {
586 rdev = rrdev;
587 rrdev = NULL;
588 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100589 if (rw & WRITE) {
590 if (replace_only)
591 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100592 if (rdev == rrdev)
593 /* We raced and saw duplicates */
594 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100595 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100596 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100597 rdev = rrdev;
598 rrdev = NULL;
599 }
NeilBrown977df362011-12-23 10:17:53 +1100600
Dan Williams91c00922007-01-02 13:52:30 -0700601 if (rdev && test_bit(Faulty, &rdev->flags))
602 rdev = NULL;
603 if (rdev)
604 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100605 if (rrdev && test_bit(Faulty, &rrdev->flags))
606 rrdev = NULL;
607 if (rrdev)
608 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700609 rcu_read_unlock();
610
NeilBrown73e92e52011-07-28 11:39:22 +1000611 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100612 * need to check for writes. We never accept write errors
613 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000614 */
615 while ((rw & WRITE) && rdev &&
616 test_bit(WriteErrorSeen, &rdev->flags)) {
617 sector_t first_bad;
618 int bad_sectors;
619 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
620 &first_bad, &bad_sectors);
621 if (!bad)
622 break;
623
624 if (bad < 0) {
625 set_bit(BlockedBadBlocks, &rdev->flags);
626 if (!conf->mddev->external &&
627 conf->mddev->flags) {
628 /* It is very unlikely, but we might
629 * still need to write out the
630 * bad block log - better give it
631 * a chance*/
632 md_check_recovery(conf->mddev);
633 }
majianpeng18507532012-07-03 12:11:54 +1000634 /*
635 * Because md_wait_for_blocked_rdev
636 * will dec nr_pending, we must
637 * increment it first.
638 */
639 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000640 md_wait_for_blocked_rdev(rdev, conf->mddev);
641 } else {
642 /* Acknowledged bad block - skip the write */
643 rdev_dec_pending(rdev, conf->mddev);
644 rdev = NULL;
645 }
646 }
647
Dan Williams91c00922007-01-02 13:52:30 -0700648 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100649 if (s->syncing || s->expanding || s->expanded
650 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700651 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
652
Dan Williams2b7497f2008-06-28 08:31:52 +1000653 set_bit(STRIPE_IO_STARTED, &sh->state);
654
Dan Williams91c00922007-01-02 13:52:30 -0700655 bi->bi_bdev = rdev->bdev;
656 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700657 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700658 bi->bi_rw, i);
659 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000660 if (use_new_offset(conf, sh))
661 bi->bi_sector = (sh->sector
662 + rdev->new_data_offset);
663 else
664 bi->bi_sector = (sh->sector
665 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000666 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
667 bi->bi_rw |= REQ_FLUSH;
668
Dan Williams91c00922007-01-02 13:52:30 -0700669 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700670 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700671 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
672 bi->bi_io_vec[0].bv_offset = 0;
673 bi->bi_size = STRIPE_SIZE;
674 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100675 if (rrdev)
676 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
NeilBrowna9add5d2012-10-31 11:59:09 +1100677 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
678 bi, disk_devt(conf->mddev->gendisk),
679 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700680 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100681 }
682 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100683 if (s->syncing || s->expanding || s->expanded
684 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100685 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
686
687 set_bit(STRIPE_IO_STARTED, &sh->state);
688
689 rbi->bi_bdev = rrdev->bdev;
690 pr_debug("%s: for %llu schedule op %ld on "
691 "replacement disc %d\n",
692 __func__, (unsigned long long)sh->sector,
693 rbi->bi_rw, i);
694 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000695 if (use_new_offset(conf, sh))
696 rbi->bi_sector = (sh->sector
697 + rrdev->new_data_offset);
698 else
699 rbi->bi_sector = (sh->sector
700 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100701 rbi->bi_flags = 1 << BIO_UPTODATE;
702 rbi->bi_idx = 0;
703 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
704 rbi->bi_io_vec[0].bv_offset = 0;
705 rbi->bi_size = STRIPE_SIZE;
706 rbi->bi_next = NULL;
NeilBrowna9add5d2012-10-31 11:59:09 +1100707 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
708 rbi, disk_devt(conf->mddev->gendisk),
709 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100710 generic_make_request(rbi);
711 }
712 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000713 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700714 set_bit(STRIPE_DEGRADED, &sh->state);
715 pr_debug("skip op %ld on disc %d for sector %llu\n",
716 bi->bi_rw, i, (unsigned long long)sh->sector);
717 clear_bit(R5_LOCKED, &sh->dev[i].flags);
718 set_bit(STRIPE_HANDLE, &sh->state);
719 }
720 }
721}
722
723static struct dma_async_tx_descriptor *
724async_copy_data(int frombio, struct bio *bio, struct page *page,
725 sector_t sector, struct dma_async_tx_descriptor *tx)
726{
727 struct bio_vec *bvl;
728 struct page *bio_page;
729 int i;
730 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700731 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700732 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700733
734 if (bio->bi_sector >= sector)
735 page_offset = (signed)(bio->bi_sector - sector) * 512;
736 else
737 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700738
Dan Williams0403e382009-09-08 17:42:50 -0700739 if (frombio)
740 flags |= ASYNC_TX_FENCE;
741 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
742
Dan Williams91c00922007-01-02 13:52:30 -0700743 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000744 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700745 int clen;
746 int b_offset = 0;
747
748 if (page_offset < 0) {
749 b_offset = -page_offset;
750 page_offset += b_offset;
751 len -= b_offset;
752 }
753
754 if (len > 0 && page_offset + len > STRIPE_SIZE)
755 clen = STRIPE_SIZE - page_offset;
756 else
757 clen = len;
758
759 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000760 b_offset += bvl->bv_offset;
761 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700762 if (frombio)
763 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700764 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700765 else
766 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700767 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700768 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700769 /* chain the operations */
770 submit.depend_tx = tx;
771
Dan Williams91c00922007-01-02 13:52:30 -0700772 if (clen < len) /* hit end of page */
773 break;
774 page_offset += len;
775 }
776
777 return tx;
778}
779
780static void ops_complete_biofill(void *stripe_head_ref)
781{
782 struct stripe_head *sh = stripe_head_ref;
783 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700784 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700785
Harvey Harrisone46b2722008-04-28 02:15:50 -0700786 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700787 (unsigned long long)sh->sector);
788
789 /* clear completed biofills */
790 for (i = sh->disks; i--; ) {
791 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700792
793 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700794 /* and check if we need to reply to a read request,
795 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000796 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700797 */
798 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700799 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700800
Dan Williams91c00922007-01-02 13:52:30 -0700801 BUG_ON(!dev->read);
802 rbi = dev->read;
803 dev->read = NULL;
804 while (rbi && rbi->bi_sector <
805 dev->sector + STRIPE_SECTORS) {
806 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000807 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700808 rbi->bi_next = return_bi;
809 return_bi = rbi;
810 }
Dan Williams91c00922007-01-02 13:52:30 -0700811 rbi = rbi2;
812 }
813 }
814 }
Dan Williams83de75c2008-06-28 08:31:58 +1000815 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700816
817 return_io(return_bi);
818
Dan Williamse4d84902007-09-24 10:06:13 -0700819 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700820 release_stripe(sh);
821}
822
823static void ops_run_biofill(struct stripe_head *sh)
824{
825 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700826 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700827 int i;
828
Harvey Harrisone46b2722008-04-28 02:15:50 -0700829 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700830 (unsigned long long)sh->sector);
831
832 for (i = sh->disks; i--; ) {
833 struct r5dev *dev = &sh->dev[i];
834 if (test_bit(R5_Wantfill, &dev->flags)) {
835 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000836 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700837 dev->read = rbi = dev->toread;
838 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000839 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700840 while (rbi && rbi->bi_sector <
841 dev->sector + STRIPE_SECTORS) {
842 tx = async_copy_data(0, rbi, dev->page,
843 dev->sector, tx);
844 rbi = r5_next_bio(rbi, dev->sector);
845 }
846 }
847 }
848
849 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700850 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
851 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700852}
853
Dan Williams4e7d2c02009-08-29 19:13:11 -0700854static void mark_target_uptodate(struct stripe_head *sh, int target)
855{
856 struct r5dev *tgt;
857
858 if (target < 0)
859 return;
860
861 tgt = &sh->dev[target];
862 set_bit(R5_UPTODATE, &tgt->flags);
863 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
864 clear_bit(R5_Wantcompute, &tgt->flags);
865}
866
Dan Williamsac6b53b2009-07-14 13:40:19 -0700867static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700868{
869 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700870
Harvey Harrisone46b2722008-04-28 02:15:50 -0700871 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700872 (unsigned long long)sh->sector);
873
Dan Williamsac6b53b2009-07-14 13:40:19 -0700874 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700875 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700876 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700877
Dan Williamsecc65c92008-06-28 08:31:57 +1000878 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
879 if (sh->check_state == check_state_compute_run)
880 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700881 set_bit(STRIPE_HANDLE, &sh->state);
882 release_stripe(sh);
883}
884
Dan Williamsd6f38f32009-07-14 11:50:52 -0700885/* return a pointer to the address conversion region of the scribble buffer */
886static addr_conv_t *to_addr_conv(struct stripe_head *sh,
887 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700888{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700889 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
890}
891
892static struct dma_async_tx_descriptor *
893ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
894{
Dan Williams91c00922007-01-02 13:52:30 -0700895 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700896 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700897 int target = sh->ops.target;
898 struct r5dev *tgt = &sh->dev[target];
899 struct page *xor_dest = tgt->page;
900 int count = 0;
901 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700902 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700903 int i;
904
905 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700906 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700907 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
908
909 for (i = disks; i--; )
910 if (i != target)
911 xor_srcs[count++] = sh->dev[i].page;
912
913 atomic_inc(&sh->count);
914
Dan Williams0403e382009-09-08 17:42:50 -0700915 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700916 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700917 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700918 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700919 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700920 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700921
Dan Williams91c00922007-01-02 13:52:30 -0700922 return tx;
923}
924
Dan Williamsac6b53b2009-07-14 13:40:19 -0700925/* set_syndrome_sources - populate source buffers for gen_syndrome
926 * @srcs - (struct page *) array of size sh->disks
927 * @sh - stripe_head to parse
928 *
929 * Populates srcs in proper layout order for the stripe and returns the
930 * 'count' of sources to be used in a call to async_gen_syndrome. The P
931 * destination buffer is recorded in srcs[count] and the Q destination
932 * is recorded in srcs[count+1]].
933 */
934static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
935{
936 int disks = sh->disks;
937 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
938 int d0_idx = raid6_d0(sh);
939 int count;
940 int i;
941
942 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100943 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700944
945 count = 0;
946 i = d0_idx;
947 do {
948 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
949
950 srcs[slot] = sh->dev[i].page;
951 i = raid6_next_disk(i, disks);
952 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700953
NeilBrowne4424fe2009-10-16 16:27:34 +1100954 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700955}
956
957static struct dma_async_tx_descriptor *
958ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
959{
960 int disks = sh->disks;
961 struct page **blocks = percpu->scribble;
962 int target;
963 int qd_idx = sh->qd_idx;
964 struct dma_async_tx_descriptor *tx;
965 struct async_submit_ctl submit;
966 struct r5dev *tgt;
967 struct page *dest;
968 int i;
969 int count;
970
971 if (sh->ops.target < 0)
972 target = sh->ops.target2;
973 else if (sh->ops.target2 < 0)
974 target = sh->ops.target;
975 else
976 /* we should only have one valid target */
977 BUG();
978 BUG_ON(target < 0);
979 pr_debug("%s: stripe %llu block: %d\n",
980 __func__, (unsigned long long)sh->sector, target);
981
982 tgt = &sh->dev[target];
983 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
984 dest = tgt->page;
985
986 atomic_inc(&sh->count);
987
988 if (target == qd_idx) {
989 count = set_syndrome_sources(blocks, sh);
990 blocks[count] = NULL; /* regenerating p is not necessary */
991 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700992 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
993 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700994 to_addr_conv(sh, percpu));
995 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
996 } else {
997 /* Compute any data- or p-drive using XOR */
998 count = 0;
999 for (i = disks; i-- ; ) {
1000 if (i == target || i == qd_idx)
1001 continue;
1002 blocks[count++] = sh->dev[i].page;
1003 }
1004
Dan Williams0403e382009-09-08 17:42:50 -07001005 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1006 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001007 to_addr_conv(sh, percpu));
1008 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1009 }
1010
1011 return tx;
1012}
1013
1014static struct dma_async_tx_descriptor *
1015ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1016{
1017 int i, count, disks = sh->disks;
1018 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1019 int d0_idx = raid6_d0(sh);
1020 int faila = -1, failb = -1;
1021 int target = sh->ops.target;
1022 int target2 = sh->ops.target2;
1023 struct r5dev *tgt = &sh->dev[target];
1024 struct r5dev *tgt2 = &sh->dev[target2];
1025 struct dma_async_tx_descriptor *tx;
1026 struct page **blocks = percpu->scribble;
1027 struct async_submit_ctl submit;
1028
1029 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1030 __func__, (unsigned long long)sh->sector, target, target2);
1031 BUG_ON(target < 0 || target2 < 0);
1032 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1033 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1034
Dan Williams6c910a72009-09-16 12:24:54 -07001035 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001036 * slot number conversion for 'faila' and 'failb'
1037 */
1038 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001039 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001040 count = 0;
1041 i = d0_idx;
1042 do {
1043 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1044
1045 blocks[slot] = sh->dev[i].page;
1046
1047 if (i == target)
1048 faila = slot;
1049 if (i == target2)
1050 failb = slot;
1051 i = raid6_next_disk(i, disks);
1052 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001053
1054 BUG_ON(faila == failb);
1055 if (failb < faila)
1056 swap(faila, failb);
1057 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1058 __func__, (unsigned long long)sh->sector, faila, failb);
1059
1060 atomic_inc(&sh->count);
1061
1062 if (failb == syndrome_disks+1) {
1063 /* Q disk is one of the missing disks */
1064 if (faila == syndrome_disks) {
1065 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001066 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1067 ops_complete_compute, sh,
1068 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001069 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001070 STRIPE_SIZE, &submit);
1071 } else {
1072 struct page *dest;
1073 int data_target;
1074 int qd_idx = sh->qd_idx;
1075
1076 /* Missing D+Q: recompute D from P, then recompute Q */
1077 if (target == qd_idx)
1078 data_target = target2;
1079 else
1080 data_target = target;
1081
1082 count = 0;
1083 for (i = disks; i-- ; ) {
1084 if (i == data_target || i == qd_idx)
1085 continue;
1086 blocks[count++] = sh->dev[i].page;
1087 }
1088 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001089 init_async_submit(&submit,
1090 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1091 NULL, NULL, NULL,
1092 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001093 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1094 &submit);
1095
1096 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001097 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1098 ops_complete_compute, sh,
1099 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001100 return async_gen_syndrome(blocks, 0, count+2,
1101 STRIPE_SIZE, &submit);
1102 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001103 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001104 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1105 ops_complete_compute, sh,
1106 to_addr_conv(sh, percpu));
1107 if (failb == syndrome_disks) {
1108 /* We're missing D+P. */
1109 return async_raid6_datap_recov(syndrome_disks+2,
1110 STRIPE_SIZE, faila,
1111 blocks, &submit);
1112 } else {
1113 /* We're missing D+D. */
1114 return async_raid6_2data_recov(syndrome_disks+2,
1115 STRIPE_SIZE, faila, failb,
1116 blocks, &submit);
1117 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001118 }
1119}
1120
1121
Dan Williams91c00922007-01-02 13:52:30 -07001122static void ops_complete_prexor(void *stripe_head_ref)
1123{
1124 struct stripe_head *sh = stripe_head_ref;
1125
Harvey Harrisone46b2722008-04-28 02:15:50 -07001126 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001127 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001128}
1129
1130static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001131ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1132 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001133{
Dan Williams91c00922007-01-02 13:52:30 -07001134 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001135 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001136 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001137 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001138
1139 /* existing parity data subtracted */
1140 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1141
Harvey Harrisone46b2722008-04-28 02:15:50 -07001142 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001143 (unsigned long long)sh->sector);
1144
1145 for (i = disks; i--; ) {
1146 struct r5dev *dev = &sh->dev[i];
1147 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001148 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001149 xor_srcs[count++] = dev->page;
1150 }
1151
Dan Williams0403e382009-09-08 17:42:50 -07001152 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001153 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001154 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001155
1156 return tx;
1157}
1158
1159static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001160ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001161{
1162 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001163 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001164
Harvey Harrisone46b2722008-04-28 02:15:50 -07001165 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001166 (unsigned long long)sh->sector);
1167
1168 for (i = disks; i--; ) {
1169 struct r5dev *dev = &sh->dev[i];
1170 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001171
Dan Williamsd8ee0722008-06-28 08:32:06 +10001172 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001173 struct bio *wbi;
1174
Shaohua Lib17459c2012-07-19 16:01:31 +10001175 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001176 chosen = dev->towrite;
1177 dev->towrite = NULL;
1178 BUG_ON(dev->written);
1179 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001180 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001181
1182 while (wbi && wbi->bi_sector <
1183 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001184 if (wbi->bi_rw & REQ_FUA)
1185 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001186 if (wbi->bi_rw & REQ_SYNC)
1187 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001188 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001189 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001190 else
Shaohua Li620125f2012-10-11 13:49:05 +11001191 tx = async_copy_data(1, wbi, dev->page,
1192 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001193 wbi = r5_next_bio(wbi, dev->sector);
1194 }
1195 }
1196 }
1197
1198 return tx;
1199}
1200
Dan Williamsac6b53b2009-07-14 13:40:19 -07001201static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001202{
1203 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001204 int disks = sh->disks;
1205 int pd_idx = sh->pd_idx;
1206 int qd_idx = sh->qd_idx;
1207 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001208 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001209
Harvey Harrisone46b2722008-04-28 02:15:50 -07001210 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001211 (unsigned long long)sh->sector);
1212
Shaohua Libc0934f2012-05-22 13:55:05 +10001213 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001214 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001215 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001216 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001217 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001218
Dan Williams91c00922007-01-02 13:52:30 -07001219 for (i = disks; i--; ) {
1220 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001221
Tejun Heoe9c74692010-09-03 11:56:18 +02001222 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001223 if (!discard)
1224 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001225 if (fua)
1226 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001227 if (sync)
1228 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001229 }
Dan Williams91c00922007-01-02 13:52:30 -07001230 }
1231
Dan Williamsd8ee0722008-06-28 08:32:06 +10001232 if (sh->reconstruct_state == reconstruct_state_drain_run)
1233 sh->reconstruct_state = reconstruct_state_drain_result;
1234 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1235 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1236 else {
1237 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1238 sh->reconstruct_state = reconstruct_state_result;
1239 }
Dan Williams91c00922007-01-02 13:52:30 -07001240
1241 set_bit(STRIPE_HANDLE, &sh->state);
1242 release_stripe(sh);
1243}
1244
1245static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001246ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1247 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001248{
Dan Williams91c00922007-01-02 13:52:30 -07001249 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001250 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001251 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001252 int count = 0, pd_idx = sh->pd_idx, i;
1253 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001254 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001255 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001256
Harvey Harrisone46b2722008-04-28 02:15:50 -07001257 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001258 (unsigned long long)sh->sector);
1259
Shaohua Li620125f2012-10-11 13:49:05 +11001260 for (i = 0; i < sh->disks; i++) {
1261 if (pd_idx == i)
1262 continue;
1263 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1264 break;
1265 }
1266 if (i >= sh->disks) {
1267 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001268 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1269 ops_complete_reconstruct(sh);
1270 return;
1271 }
Dan Williams91c00922007-01-02 13:52:30 -07001272 /* check if prexor is active which means only process blocks
1273 * that are part of a read-modify-write (written)
1274 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001275 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1276 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001277 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1278 for (i = disks; i--; ) {
1279 struct r5dev *dev = &sh->dev[i];
1280 if (dev->written)
1281 xor_srcs[count++] = dev->page;
1282 }
1283 } else {
1284 xor_dest = sh->dev[pd_idx].page;
1285 for (i = disks; i--; ) {
1286 struct r5dev *dev = &sh->dev[i];
1287 if (i != pd_idx)
1288 xor_srcs[count++] = dev->page;
1289 }
1290 }
1291
Dan Williams91c00922007-01-02 13:52:30 -07001292 /* 1/ if we prexor'd then the dest is reused as a source
1293 * 2/ if we did not prexor then we are redoing the parity
1294 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1295 * for the synchronous xor case
1296 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001297 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001298 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1299
1300 atomic_inc(&sh->count);
1301
Dan Williamsac6b53b2009-07-14 13:40:19 -07001302 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001303 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001304 if (unlikely(count == 1))
1305 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1306 else
1307 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001308}
1309
Dan Williamsac6b53b2009-07-14 13:40:19 -07001310static void
1311ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1312 struct dma_async_tx_descriptor *tx)
1313{
1314 struct async_submit_ctl submit;
1315 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001316 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001317
1318 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1319
Shaohua Li620125f2012-10-11 13:49:05 +11001320 for (i = 0; i < sh->disks; i++) {
1321 if (sh->pd_idx == i || sh->qd_idx == i)
1322 continue;
1323 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1324 break;
1325 }
1326 if (i >= sh->disks) {
1327 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001328 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1329 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1330 ops_complete_reconstruct(sh);
1331 return;
1332 }
1333
Dan Williamsac6b53b2009-07-14 13:40:19 -07001334 count = set_syndrome_sources(blocks, sh);
1335
1336 atomic_inc(&sh->count);
1337
1338 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1339 sh, to_addr_conv(sh, percpu));
1340 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001341}
1342
1343static void ops_complete_check(void *stripe_head_ref)
1344{
1345 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001346
Harvey Harrisone46b2722008-04-28 02:15:50 -07001347 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001348 (unsigned long long)sh->sector);
1349
Dan Williamsecc65c92008-06-28 08:31:57 +10001350 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001351 set_bit(STRIPE_HANDLE, &sh->state);
1352 release_stripe(sh);
1353}
1354
Dan Williamsac6b53b2009-07-14 13:40:19 -07001355static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001356{
Dan Williams91c00922007-01-02 13:52:30 -07001357 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001358 int pd_idx = sh->pd_idx;
1359 int qd_idx = sh->qd_idx;
1360 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001361 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001362 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001363 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001364 int count;
1365 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001366
Harvey Harrisone46b2722008-04-28 02:15:50 -07001367 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001368 (unsigned long long)sh->sector);
1369
Dan Williamsac6b53b2009-07-14 13:40:19 -07001370 count = 0;
1371 xor_dest = sh->dev[pd_idx].page;
1372 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001373 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001374 if (i == pd_idx || i == qd_idx)
1375 continue;
1376 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001377 }
1378
Dan Williamsd6f38f32009-07-14 11:50:52 -07001379 init_async_submit(&submit, 0, NULL, NULL, NULL,
1380 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001381 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001382 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001383
Dan Williams91c00922007-01-02 13:52:30 -07001384 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001385 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1386 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001387}
1388
Dan Williamsac6b53b2009-07-14 13:40:19 -07001389static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1390{
1391 struct page **srcs = percpu->scribble;
1392 struct async_submit_ctl submit;
1393 int count;
1394
1395 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1396 (unsigned long long)sh->sector, checkp);
1397
1398 count = set_syndrome_sources(srcs, sh);
1399 if (!checkp)
1400 srcs[count] = NULL;
1401
1402 atomic_inc(&sh->count);
1403 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1404 sh, to_addr_conv(sh, percpu));
1405 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1406 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1407}
1408
NeilBrown51acbce2013-02-28 09:08:34 +11001409static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001410{
1411 int overlap_clear = 0, i, disks = sh->disks;
1412 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001413 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001414 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001415 struct raid5_percpu *percpu;
1416 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001417
Dan Williamsd6f38f32009-07-14 11:50:52 -07001418 cpu = get_cpu();
1419 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001420 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001421 ops_run_biofill(sh);
1422 overlap_clear++;
1423 }
1424
Dan Williams7b3a8712008-06-28 08:32:09 +10001425 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001426 if (level < 6)
1427 tx = ops_run_compute5(sh, percpu);
1428 else {
1429 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1430 tx = ops_run_compute6_1(sh, percpu);
1431 else
1432 tx = ops_run_compute6_2(sh, percpu);
1433 }
1434 /* terminate the chain if reconstruct is not set to be run */
1435 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001436 async_tx_ack(tx);
1437 }
Dan Williams91c00922007-01-02 13:52:30 -07001438
Dan Williams600aa102008-06-28 08:32:05 +10001439 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001440 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001441
Dan Williams600aa102008-06-28 08:32:05 +10001442 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001443 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001444 overlap_clear++;
1445 }
1446
Dan Williamsac6b53b2009-07-14 13:40:19 -07001447 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1448 if (level < 6)
1449 ops_run_reconstruct5(sh, percpu, tx);
1450 else
1451 ops_run_reconstruct6(sh, percpu, tx);
1452 }
Dan Williams91c00922007-01-02 13:52:30 -07001453
Dan Williamsac6b53b2009-07-14 13:40:19 -07001454 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1455 if (sh->check_state == check_state_run)
1456 ops_run_check_p(sh, percpu);
1457 else if (sh->check_state == check_state_run_q)
1458 ops_run_check_pq(sh, percpu, 0);
1459 else if (sh->check_state == check_state_run_pq)
1460 ops_run_check_pq(sh, percpu, 1);
1461 else
1462 BUG();
1463 }
Dan Williams91c00922007-01-02 13:52:30 -07001464
Dan Williams91c00922007-01-02 13:52:30 -07001465 if (overlap_clear)
1466 for (i = disks; i--; ) {
1467 struct r5dev *dev = &sh->dev[i];
1468 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1469 wake_up(&sh->raid_conf->wait_for_overlap);
1470 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001471 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001472}
1473
NeilBrownd1688a62011-10-11 16:49:52 +11001474static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
1476 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001477 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001478 if (!sh)
1479 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001480
NeilBrown3f294f42005-11-08 21:39:25 -08001481 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08001482
Shaohua Lib17459c2012-07-19 16:01:31 +10001483 spin_lock_init(&sh->stripe_lock);
1484
NeilBrowne4e11e32010-06-16 16:45:16 +10001485 if (grow_buffers(sh)) {
1486 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001487 kmem_cache_free(conf->slab_cache, sh);
1488 return 0;
1489 }
1490 /* we just created an active stripe so... */
1491 atomic_set(&sh->count, 1);
1492 atomic_inc(&conf->active_stripes);
1493 INIT_LIST_HEAD(&sh->lru);
1494 release_stripe(sh);
1495 return 1;
1496}
1497
NeilBrownd1688a62011-10-11 16:49:52 +11001498static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001499{
Christoph Lametere18b8902006-12-06 20:33:20 -08001500 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001501 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
NeilBrownf4be6b42010-06-01 19:37:25 +10001503 if (conf->mddev->gendisk)
1504 sprintf(conf->cache_name[0],
1505 "raid%d-%s", conf->level, mdname(conf->mddev));
1506 else
1507 sprintf(conf->cache_name[0],
1508 "raid%d-%p", conf->level, conf->mddev);
1509 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1510
NeilBrownad01c9e2006-03-27 01:18:07 -08001511 conf->active_name = 0;
1512 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001514 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 if (!sc)
1516 return 1;
1517 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001518 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001519 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001520 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return 0;
1523}
NeilBrown29269552006-03-27 01:18:10 -08001524
Dan Williamsd6f38f32009-07-14 11:50:52 -07001525/**
1526 * scribble_len - return the required size of the scribble region
1527 * @num - total number of disks in the array
1528 *
1529 * The size must be enough to contain:
1530 * 1/ a struct page pointer for each device in the array +2
1531 * 2/ room to convert each entry in (1) to its corresponding dma
1532 * (dma_map_page()) or page (page_address()) address.
1533 *
1534 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1535 * calculate over all devices (not just the data blocks), using zeros in place
1536 * of the P and Q blocks.
1537 */
1538static size_t scribble_len(int num)
1539{
1540 size_t len;
1541
1542 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1543
1544 return len;
1545}
1546
NeilBrownd1688a62011-10-11 16:49:52 +11001547static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001548{
1549 /* Make all the stripes able to hold 'newsize' devices.
1550 * New slots in each stripe get 'page' set to a new page.
1551 *
1552 * This happens in stages:
1553 * 1/ create a new kmem_cache and allocate the required number of
1554 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09001555 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08001556 * to the new stripe_heads. This will have the side effect of
1557 * freezing the array as once all stripe_heads have been collected,
1558 * no IO will be possible. Old stripe heads are freed once their
1559 * pages have been transferred over, and the old kmem_cache is
1560 * freed when all stripes are done.
1561 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1562 * we simple return a failre status - no need to clean anything up.
1563 * 4/ allocate new pages for the new slots in the new stripe_heads.
1564 * If this fails, we don't bother trying the shrink the
1565 * stripe_heads down again, we just leave them as they are.
1566 * As each stripe_head is processed the new one is released into
1567 * active service.
1568 *
1569 * Once step2 is started, we cannot afford to wait for a write,
1570 * so we use GFP_NOIO allocations.
1571 */
1572 struct stripe_head *osh, *nsh;
1573 LIST_HEAD(newstripes);
1574 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001575 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001576 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001577 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001578 int i;
1579
1580 if (newsize <= conf->pool_size)
1581 return 0; /* never bother to shrink */
1582
Dan Williamsb5470dc2008-06-27 21:44:04 -07001583 err = md_allow_write(conf->mddev);
1584 if (err)
1585 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001586
NeilBrownad01c9e2006-03-27 01:18:07 -08001587 /* Step 1 */
1588 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1589 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001590 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001591 if (!sc)
1592 return -ENOMEM;
1593
1594 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001595 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001596 if (!nsh)
1597 break;
1598
NeilBrownad01c9e2006-03-27 01:18:07 -08001599 nsh->raid_conf = conf;
NeilBrowncb13ff62012-09-24 16:27:20 +10001600 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001601
1602 list_add(&nsh->lru, &newstripes);
1603 }
1604 if (i) {
1605 /* didn't get enough, give up */
1606 while (!list_empty(&newstripes)) {
1607 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1608 list_del(&nsh->lru);
1609 kmem_cache_free(sc, nsh);
1610 }
1611 kmem_cache_destroy(sc);
1612 return -ENOMEM;
1613 }
1614 /* Step 2 - Must use GFP_NOIO now.
1615 * OK, we have enough stripes, start collecting inactive
1616 * stripes and copying them over
1617 */
1618 list_for_each_entry(nsh, &newstripes, lru) {
1619 spin_lock_irq(&conf->device_lock);
1620 wait_event_lock_irq(conf->wait_for_stripe,
1621 !list_empty(&conf->inactive_list),
Lukas Czernereed8c022012-11-30 11:42:40 +01001622 conf->device_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001623 osh = get_free_stripe(conf);
1624 spin_unlock_irq(&conf->device_lock);
1625 atomic_set(&nsh->count, 1);
1626 for(i=0; i<conf->pool_size; i++)
1627 nsh->dev[i].page = osh->dev[i].page;
1628 for( ; i<newsize; i++)
1629 nsh->dev[i].page = NULL;
1630 kmem_cache_free(conf->slab_cache, osh);
1631 }
1632 kmem_cache_destroy(conf->slab_cache);
1633
1634 /* Step 3.
1635 * At this point, we are holding all the stripes so the array
1636 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001637 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001638 */
1639 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1640 if (ndisks) {
1641 for (i=0; i<conf->raid_disks; i++)
1642 ndisks[i] = conf->disks[i];
1643 kfree(conf->disks);
1644 conf->disks = ndisks;
1645 } else
1646 err = -ENOMEM;
1647
Dan Williamsd6f38f32009-07-14 11:50:52 -07001648 get_online_cpus();
1649 conf->scribble_len = scribble_len(newsize);
1650 for_each_present_cpu(cpu) {
1651 struct raid5_percpu *percpu;
1652 void *scribble;
1653
1654 percpu = per_cpu_ptr(conf->percpu, cpu);
1655 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1656
1657 if (scribble) {
1658 kfree(percpu->scribble);
1659 percpu->scribble = scribble;
1660 } else {
1661 err = -ENOMEM;
1662 break;
1663 }
1664 }
1665 put_online_cpus();
1666
NeilBrownad01c9e2006-03-27 01:18:07 -08001667 /* Step 4, return new stripes to service */
1668 while(!list_empty(&newstripes)) {
1669 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1670 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001671
NeilBrownad01c9e2006-03-27 01:18:07 -08001672 for (i=conf->raid_disks; i < newsize; i++)
1673 if (nsh->dev[i].page == NULL) {
1674 struct page *p = alloc_page(GFP_NOIO);
1675 nsh->dev[i].page = p;
1676 if (!p)
1677 err = -ENOMEM;
1678 }
1679 release_stripe(nsh);
1680 }
1681 /* critical section pass, GFP_NOIO no longer needed */
1682
1683 conf->slab_cache = sc;
1684 conf->active_name = 1-conf->active_name;
1685 conf->pool_size = newsize;
1686 return err;
1687}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
NeilBrownd1688a62011-10-11 16:49:52 +11001689static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 struct stripe_head *sh;
1692
NeilBrown3f294f42005-11-08 21:39:25 -08001693 spin_lock_irq(&conf->device_lock);
1694 sh = get_free_stripe(conf);
1695 spin_unlock_irq(&conf->device_lock);
1696 if (!sh)
1697 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001698 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001699 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001700 kmem_cache_free(conf->slab_cache, sh);
1701 atomic_dec(&conf->active_stripes);
1702 return 1;
1703}
1704
NeilBrownd1688a62011-10-11 16:49:52 +11001705static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001706{
1707 while (drop_one_stripe(conf))
1708 ;
1709
NeilBrown29fc7e32006-02-03 03:03:41 -08001710 if (conf->slab_cache)
1711 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 conf->slab_cache = NULL;
1713}
1714
NeilBrown6712ecf2007-09-27 12:47:43 +02001715static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
NeilBrown99c0fb52009-03-31 14:39:38 +11001717 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001718 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001719 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001721 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001722 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001723 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 for (i=0 ; i<disks; i++)
1726 if (bi == &sh->dev[i].req)
1727 break;
1728
Dan Williams45b42332007-07-09 11:56:43 -07001729 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1730 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 uptodate);
1732 if (i == disks) {
1733 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001734 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 }
NeilBrown14a75d32011-12-23 10:17:52 +11001736 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001737 /* If replacement finished while this request was outstanding,
1738 * 'replacement' might be NULL already.
1739 * In that case it moved down to 'rdev'.
1740 * rdev is not removed until all requests are finished.
1741 */
NeilBrown14a75d32011-12-23 10:17:52 +11001742 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001743 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001744 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
NeilBrown05616be2012-05-21 09:27:00 +10001746 if (use_new_offset(conf, sh))
1747 s = sh->sector + rdev->new_data_offset;
1748 else
1749 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001752 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001753 /* Note that this cannot happen on a
1754 * replacement device. We just fail those on
1755 * any error
1756 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001757 printk_ratelimited(
1758 KERN_INFO
1759 "md/raid:%s: read error corrected"
1760 " (%lu sectors at %llu on %s)\n",
1761 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001762 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001763 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001764 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001765 clear_bit(R5_ReadError, &sh->dev[i].flags);
1766 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001767 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1768 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1769
NeilBrown14a75d32011-12-23 10:17:52 +11001770 if (atomic_read(&rdev->read_errors))
1771 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001773 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001774 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001775 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001778 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001779 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1780 printk_ratelimited(
1781 KERN_WARNING
1782 "md/raid:%s: read error on replacement device "
1783 "(sector %llu on %s).\n",
1784 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001785 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001786 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001787 else if (conf->mddev->degraded >= conf->max_degraded) {
1788 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001789 printk_ratelimited(
1790 KERN_WARNING
1791 "md/raid:%s: read error not correctable "
1792 "(sector %llu on %s).\n",
1793 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001794 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001795 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001796 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001797 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001798 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001799 printk_ratelimited(
1800 KERN_WARNING
1801 "md/raid:%s: read error NOT corrected!! "
1802 "(sector %llu on %s).\n",
1803 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001804 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001805 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001806 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001807 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001808 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001809 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001810 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001811 else
1812 retry = 1;
1813 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001814 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1815 set_bit(R5_ReadError, &sh->dev[i].flags);
1816 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1817 } else
1818 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001819 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001820 clear_bit(R5_ReadError, &sh->dev[i].flags);
1821 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001822 if (!(set_bad
1823 && test_bit(In_sync, &rdev->flags)
1824 && rdev_set_badblocks(
1825 rdev, sh->sector, STRIPE_SECTORS, 0)))
1826 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 }
NeilBrown14a75d32011-12-23 10:17:52 +11001829 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1831 set_bit(STRIPE_HANDLE, &sh->state);
1832 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
NeilBrownd710e132008-10-13 11:55:12 +11001835static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
NeilBrown99c0fb52009-03-31 14:39:38 +11001837 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001838 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001839 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001840 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001842 sector_t first_bad;
1843 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001844 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
NeilBrown977df362011-12-23 10:17:53 +11001846 for (i = 0 ; i < disks; i++) {
1847 if (bi == &sh->dev[i].req) {
1848 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 break;
NeilBrown977df362011-12-23 10:17:53 +11001850 }
1851 if (bi == &sh->dev[i].rreq) {
1852 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001853 if (rdev)
1854 replacement = 1;
1855 else
1856 /* rdev was removed and 'replacement'
1857 * replaced it. rdev is not removed
1858 * until all requests are finished.
1859 */
1860 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001861 break;
1862 }
1863 }
Dan Williams45b42332007-07-09 11:56:43 -07001864 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1866 uptodate);
1867 if (i == disks) {
1868 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001869 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
1871
NeilBrown977df362011-12-23 10:17:53 +11001872 if (replacement) {
1873 if (!uptodate)
1874 md_error(conf->mddev, rdev);
1875 else if (is_badblock(rdev, sh->sector,
1876 STRIPE_SECTORS,
1877 &first_bad, &bad_sectors))
1878 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1879 } else {
1880 if (!uptodate) {
1881 set_bit(WriteErrorSeen, &rdev->flags);
1882 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001883 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1884 set_bit(MD_RECOVERY_NEEDED,
1885 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001886 } else if (is_badblock(rdev, sh->sector,
1887 STRIPE_SECTORS,
1888 &first_bad, &bad_sectors))
1889 set_bit(R5_MadeGood, &sh->dev[i].flags);
1890 }
1891 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
NeilBrown977df362011-12-23 10:17:53 +11001893 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1894 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001896 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
NeilBrown784052e2009-03-31 15:19:07 +11001899static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
NeilBrown784052e2009-03-31 15:19:07 +11001901static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
1903 struct r5dev *dev = &sh->dev[i];
1904
1905 bio_init(&dev->req);
1906 dev->req.bi_io_vec = &dev->vec;
1907 dev->req.bi_vcnt++;
1908 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001910 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
NeilBrown977df362011-12-23 10:17:53 +11001912 bio_init(&dev->rreq);
1913 dev->rreq.bi_io_vec = &dev->rvec;
1914 dev->rreq.bi_vcnt++;
1915 dev->rreq.bi_max_vecs++;
1916 dev->rreq.bi_private = sh;
1917 dev->rvec.bv_page = dev->page;
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001920 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
NeilBrownfd01b882011-10-11 16:47:53 +11001923static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001926 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001927 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001928 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
NeilBrown908f4fb2011-12-23 10:17:50 +11001930 spin_lock_irqsave(&conf->device_lock, flags);
1931 clear_bit(In_sync, &rdev->flags);
1932 mddev->degraded = calc_degraded(conf);
1933 spin_unlock_irqrestore(&conf->device_lock, flags);
1934 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1935
NeilBrownde393cd2011-07-28 11:31:48 +10001936 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001937 set_bit(Faulty, &rdev->flags);
1938 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1939 printk(KERN_ALERT
1940 "md/raid:%s: Disk failure on %s, disabling device.\n"
1941 "md/raid:%s: Operation continuing on %d devices.\n",
1942 mdname(mddev),
1943 bdevname(rdev->bdev, b),
1944 mdname(mddev),
1945 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001946}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948/*
1949 * Input: a 'big' sector number,
1950 * Output: index of the data and parity disk, and the sector # in them.
1951 */
NeilBrownd1688a62011-10-11 16:49:52 +11001952static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001953 int previous, int *dd_idx,
1954 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001956 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001957 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001959 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001960 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001962 int algorithm = previous ? conf->prev_algo
1963 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001964 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1965 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001966 int raid_disks = previous ? conf->previous_raid_disks
1967 : conf->raid_disks;
1968 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 /* First compute the information on this sector */
1971
1972 /*
1973 * Compute the chunk number and the sector offset inside the chunk
1974 */
1975 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1976 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 /*
1979 * Compute the stripe number
1980 */
NeilBrown35f2a592010-04-20 14:13:34 +10001981 stripe = chunk_number;
1982 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001983 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 /*
1985 * Select the parity disk based on the user selected algorithm.
1986 */
NeilBrown84789552011-07-27 11:00:36 +10001987 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001988 switch(conf->level) {
1989 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001990 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001991 break;
1992 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001993 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001995 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001996 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 (*dd_idx)++;
1998 break;
1999 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002000 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002001 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 (*dd_idx)++;
2003 break;
2004 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002005 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002006 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 break;
2008 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002009 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002010 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002012 case ALGORITHM_PARITY_0:
2013 pd_idx = 0;
2014 (*dd_idx)++;
2015 break;
2016 case ALGORITHM_PARITY_N:
2017 pd_idx = data_disks;
2018 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002020 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002021 }
2022 break;
2023 case 6:
2024
NeilBrowne183eae2009-03-31 15:20:22 +11002025 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002026 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002027 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002028 qd_idx = pd_idx + 1;
2029 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002030 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002031 qd_idx = 0;
2032 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002033 (*dd_idx) += 2; /* D D P Q D */
2034 break;
2035 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002036 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002037 qd_idx = pd_idx + 1;
2038 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002039 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002040 qd_idx = 0;
2041 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002042 (*dd_idx) += 2; /* D D P Q D */
2043 break;
2044 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002045 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002046 qd_idx = (pd_idx + 1) % raid_disks;
2047 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002048 break;
2049 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002050 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002051 qd_idx = (pd_idx + 1) % raid_disks;
2052 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002053 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002054
2055 case ALGORITHM_PARITY_0:
2056 pd_idx = 0;
2057 qd_idx = 1;
2058 (*dd_idx) += 2;
2059 break;
2060 case ALGORITHM_PARITY_N:
2061 pd_idx = data_disks;
2062 qd_idx = data_disks + 1;
2063 break;
2064
2065 case ALGORITHM_ROTATING_ZERO_RESTART:
2066 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2067 * of blocks for computing Q is different.
2068 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002069 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002070 qd_idx = pd_idx + 1;
2071 if (pd_idx == raid_disks-1) {
2072 (*dd_idx)++; /* Q D D D P */
2073 qd_idx = 0;
2074 } else if (*dd_idx >= pd_idx)
2075 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002076 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002077 break;
2078
2079 case ALGORITHM_ROTATING_N_RESTART:
2080 /* Same a left_asymmetric, by first stripe is
2081 * D D D P Q rather than
2082 * Q D D D P
2083 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002084 stripe2 += 1;
2085 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002086 qd_idx = pd_idx + 1;
2087 if (pd_idx == raid_disks-1) {
2088 (*dd_idx)++; /* Q D D D P */
2089 qd_idx = 0;
2090 } else if (*dd_idx >= pd_idx)
2091 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002092 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002093 break;
2094
2095 case ALGORITHM_ROTATING_N_CONTINUE:
2096 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002097 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002098 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2099 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002100 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002101 break;
2102
2103 case ALGORITHM_LEFT_ASYMMETRIC_6:
2104 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002105 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002106 if (*dd_idx >= pd_idx)
2107 (*dd_idx)++;
2108 qd_idx = raid_disks - 1;
2109 break;
2110
2111 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002112 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002113 if (*dd_idx >= pd_idx)
2114 (*dd_idx)++;
2115 qd_idx = raid_disks - 1;
2116 break;
2117
2118 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002119 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002120 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2121 qd_idx = raid_disks - 1;
2122 break;
2123
2124 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002125 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002126 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2127 qd_idx = raid_disks - 1;
2128 break;
2129
2130 case ALGORITHM_PARITY_0_6:
2131 pd_idx = 0;
2132 (*dd_idx)++;
2133 qd_idx = raid_disks - 1;
2134 break;
2135
NeilBrown16a53ec2006-06-26 00:27:38 -07002136 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002137 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002138 }
2139 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141
NeilBrown911d4ee2009-03-31 14:39:38 +11002142 if (sh) {
2143 sh->pd_idx = pd_idx;
2144 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002145 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 /*
2148 * Finally, compute the new sector number
2149 */
2150 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2151 return new_sector;
2152}
2153
2154
NeilBrown784052e2009-03-31 15:19:07 +11002155static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
NeilBrownd1688a62011-10-11 16:49:52 +11002157 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002158 int raid_disks = sh->disks;
2159 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002161 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2162 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002163 int algorithm = previous ? conf->prev_algo
2164 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 sector_t stripe;
2166 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002167 sector_t chunk_number;
2168 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002170 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
NeilBrown16a53ec2006-06-26 00:27:38 -07002172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2174 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
NeilBrown16a53ec2006-06-26 00:27:38 -07002176 if (i == sh->pd_idx)
2177 return 0;
2178 switch(conf->level) {
2179 case 4: break;
2180 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002181 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 case ALGORITHM_LEFT_ASYMMETRIC:
2183 case ALGORITHM_RIGHT_ASYMMETRIC:
2184 if (i > sh->pd_idx)
2185 i--;
2186 break;
2187 case ALGORITHM_LEFT_SYMMETRIC:
2188 case ALGORITHM_RIGHT_SYMMETRIC:
2189 if (i < sh->pd_idx)
2190 i += raid_disks;
2191 i -= (sh->pd_idx + 1);
2192 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002193 case ALGORITHM_PARITY_0:
2194 i -= 1;
2195 break;
2196 case ALGORITHM_PARITY_N:
2197 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002199 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002200 }
2201 break;
2202 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002203 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002204 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002205 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002206 case ALGORITHM_LEFT_ASYMMETRIC:
2207 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002208 case ALGORITHM_ROTATING_ZERO_RESTART:
2209 case ALGORITHM_ROTATING_N_RESTART:
2210 if (sh->pd_idx == raid_disks-1)
2211 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002212 else if (i > sh->pd_idx)
2213 i -= 2; /* D D P Q D */
2214 break;
2215 case ALGORITHM_LEFT_SYMMETRIC:
2216 case ALGORITHM_RIGHT_SYMMETRIC:
2217 if (sh->pd_idx == raid_disks-1)
2218 i--; /* Q D D D P */
2219 else {
2220 /* D D P Q D */
2221 if (i < sh->pd_idx)
2222 i += raid_disks;
2223 i -= (sh->pd_idx + 2);
2224 }
2225 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002226 case ALGORITHM_PARITY_0:
2227 i -= 2;
2228 break;
2229 case ALGORITHM_PARITY_N:
2230 break;
2231 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002232 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002233 if (sh->pd_idx == 0)
2234 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002235 else {
2236 /* D D Q P D */
2237 if (i < sh->pd_idx)
2238 i += raid_disks;
2239 i -= (sh->pd_idx + 1);
2240 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002241 break;
2242 case ALGORITHM_LEFT_ASYMMETRIC_6:
2243 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2244 if (i > sh->pd_idx)
2245 i--;
2246 break;
2247 case ALGORITHM_LEFT_SYMMETRIC_6:
2248 case ALGORITHM_RIGHT_SYMMETRIC_6:
2249 if (i < sh->pd_idx)
2250 i += data_disks + 1;
2251 i -= (sh->pd_idx + 1);
2252 break;
2253 case ALGORITHM_PARITY_0_6:
2254 i -= 1;
2255 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002256 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002257 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002258 }
2259 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261
2262 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002263 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
NeilBrown112bf892009-03-31 14:39:38 +11002265 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002266 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002267 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2268 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002269 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2270 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 return 0;
2272 }
2273 return r_sector;
2274}
2275
2276
Dan Williams600aa102008-06-28 08:32:05 +10002277static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002278schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002279 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002280{
2281 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002282 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002283 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002284
Dan Williamse33129d2007-01-02 13:52:30 -07002285 if (rcw) {
2286 /* if we are not expanding this is a proper write request, and
2287 * there will be bios with new data to be drained into the
2288 * stripe cache
2289 */
2290 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002291 sh->reconstruct_state = reconstruct_state_drain_run;
2292 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2293 } else
2294 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002295
Dan Williamsac6b53b2009-07-14 13:40:19 -07002296 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002297
2298 for (i = disks; i--; ) {
2299 struct r5dev *dev = &sh->dev[i];
2300
2301 if (dev->towrite) {
2302 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002303 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002304 if (!expand)
2305 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002306 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002307 }
2308 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002309 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002310 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002311 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002312 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002313 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002314 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2315 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2316
Dan Williamsd8ee0722008-06-28 08:32:06 +10002317 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002318 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2319 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002320 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002321
2322 for (i = disks; i--; ) {
2323 struct r5dev *dev = &sh->dev[i];
2324 if (i == pd_idx)
2325 continue;
2326
Dan Williamse33129d2007-01-02 13:52:30 -07002327 if (dev->towrite &&
2328 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002329 test_bit(R5_Wantcompute, &dev->flags))) {
2330 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002331 set_bit(R5_LOCKED, &dev->flags);
2332 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002333 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002334 }
2335 }
2336 }
2337
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002338 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002339 * are in flight
2340 */
2341 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2342 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002343 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002344
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002345 if (level == 6) {
2346 int qd_idx = sh->qd_idx;
2347 struct r5dev *dev = &sh->dev[qd_idx];
2348
2349 set_bit(R5_LOCKED, &dev->flags);
2350 clear_bit(R5_UPTODATE, &dev->flags);
2351 s->locked++;
2352 }
2353
Dan Williams600aa102008-06-28 08:32:05 +10002354 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002355 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002356 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002357}
NeilBrown16a53ec2006-06-26 00:27:38 -07002358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359/*
2360 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002361 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 * The bi_next chain must be in order.
2363 */
2364static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2365{
2366 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002367 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002368 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
NeilBrowncbe47ec2011-07-26 11:20:35 +10002370 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 (unsigned long long)bi->bi_sector,
2372 (unsigned long long)sh->sector);
2373
Shaohua Lib17459c2012-07-19 16:01:31 +10002374 /*
2375 * If several bio share a stripe. The bio bi_phys_segments acts as a
2376 * reference count to avoid race. The reference count should already be
2377 * increased before this function is called (for example, in
2378 * make_request()), so other bio sharing this stripe will not free the
2379 * stripe. If a stripe is owned by one stripe, the stripe lock will
2380 * protect it.
2381 */
2382 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002383 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002385 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002386 firstwrite = 1;
2387 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 bip = &sh->dev[dd_idx].toread;
2389 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2390 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2391 goto overlap;
2392 bip = & (*bip)->bi_next;
2393 }
2394 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2395 goto overlap;
2396
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002397 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (*bip)
2399 bi->bi_next = *bip;
2400 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002401 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 if (forwrite) {
2404 /* check if page is covered */
2405 sector_t sector = sh->dev[dd_idx].sector;
2406 for (bi=sh->dev[dd_idx].towrite;
2407 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2408 bi && bi->bi_sector <= sector;
2409 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2410 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2411 sector = bi->bi_sector + (bi->bi_size>>9);
2412 }
2413 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2414 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2415 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002416
2417 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2418 (unsigned long long)(*bip)->bi_sector,
2419 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002420 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002421
2422 if (conf->mddev->bitmap && firstwrite) {
2423 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2424 STRIPE_SECTORS, 0);
2425 sh->bm_seq = conf->seq_flush+1;
2426 set_bit(STRIPE_BIT_DELAY, &sh->state);
2427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return 1;
2429
2430 overlap:
2431 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002432 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return 0;
2434}
2435
NeilBrownd1688a62011-10-11 16:49:52 +11002436static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002437
NeilBrownd1688a62011-10-11 16:49:52 +11002438static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002439 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002440{
NeilBrown784052e2009-03-31 15:19:07 +11002441 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002442 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002443 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002444 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002445 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002446
NeilBrown112bf892009-03-31 14:39:38 +11002447 raid5_compute_sector(conf,
2448 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002449 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002450 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002451 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002452}
2453
Dan Williamsa4456852007-07-09 11:56:43 -07002454static void
NeilBrownd1688a62011-10-11 16:49:52 +11002455handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002456 struct stripe_head_state *s, int disks,
2457 struct bio **return_bi)
2458{
2459 int i;
2460 for (i = disks; i--; ) {
2461 struct bio *bi;
2462 int bitmap_end = 0;
2463
2464 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002465 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002466 rcu_read_lock();
2467 rdev = rcu_dereference(conf->disks[i].rdev);
2468 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002469 atomic_inc(&rdev->nr_pending);
2470 else
2471 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002472 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002473 if (rdev) {
2474 if (!rdev_set_badblocks(
2475 rdev,
2476 sh->sector,
2477 STRIPE_SECTORS, 0))
2478 md_error(conf->mddev, rdev);
2479 rdev_dec_pending(rdev, conf->mddev);
2480 }
Dan Williamsa4456852007-07-09 11:56:43 -07002481 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002482 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002483 /* fail all writes first */
2484 bi = sh->dev[i].towrite;
2485 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002486 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002487 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002488 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002489
2490 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2491 wake_up(&conf->wait_for_overlap);
2492
2493 while (bi && bi->bi_sector <
2494 sh->dev[i].sector + STRIPE_SECTORS) {
2495 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2496 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002497 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002498 md_write_end(conf->mddev);
2499 bi->bi_next = *return_bi;
2500 *return_bi = bi;
2501 }
2502 bi = nextbi;
2503 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002504 if (bitmap_end)
2505 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2506 STRIPE_SECTORS, 0, 0);
2507 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002508 /* and fail all 'written' */
2509 bi = sh->dev[i].written;
2510 sh->dev[i].written = NULL;
2511 if (bi) bitmap_end = 1;
2512 while (bi && bi->bi_sector <
2513 sh->dev[i].sector + STRIPE_SECTORS) {
2514 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2515 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002516 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002517 md_write_end(conf->mddev);
2518 bi->bi_next = *return_bi;
2519 *return_bi = bi;
2520 }
2521 bi = bi2;
2522 }
2523
Dan Williamsb5e98d62007-01-02 13:52:31 -07002524 /* fail any reads if this device is non-operational and
2525 * the data has not reached the cache yet.
2526 */
2527 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2528 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2529 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002530 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002531 bi = sh->dev[i].toread;
2532 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002533 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002534 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2535 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002536 while (bi && bi->bi_sector <
2537 sh->dev[i].sector + STRIPE_SECTORS) {
2538 struct bio *nextbi =
2539 r5_next_bio(bi, sh->dev[i].sector);
2540 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002541 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002542 bi->bi_next = *return_bi;
2543 *return_bi = bi;
2544 }
2545 bi = nextbi;
2546 }
2547 }
Dan Williamsa4456852007-07-09 11:56:43 -07002548 if (bitmap_end)
2549 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2550 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002551 /* If we were in the middle of a write the parity block might
2552 * still be locked - so just clear all R5_LOCKED flags
2553 */
2554 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002555 }
2556
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002557 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2558 if (atomic_dec_and_test(&conf->pending_full_writes))
2559 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002560}
2561
NeilBrown7f0da592011-07-28 11:39:22 +10002562static void
NeilBrownd1688a62011-10-11 16:49:52 +11002563handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002564 struct stripe_head_state *s)
2565{
2566 int abort = 0;
2567 int i;
2568
NeilBrown7f0da592011-07-28 11:39:22 +10002569 clear_bit(STRIPE_SYNCING, &sh->state);
2570 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002571 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002572 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002573 * Don't even need to abort as that is handled elsewhere
2574 * if needed, and not always wanted e.g. if there is a known
2575 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002576 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002577 * non-sync devices, or abort the recovery
2578 */
NeilBrown18b98372012-04-01 23:48:38 +10002579 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2580 /* During recovery devices cannot be removed, so
2581 * locking and refcounting of rdevs is not needed
2582 */
2583 for (i = 0; i < conf->raid_disks; i++) {
2584 struct md_rdev *rdev = conf->disks[i].rdev;
2585 if (rdev
2586 && !test_bit(Faulty, &rdev->flags)
2587 && !test_bit(In_sync, &rdev->flags)
2588 && !rdev_set_badblocks(rdev, sh->sector,
2589 STRIPE_SECTORS, 0))
2590 abort = 1;
2591 rdev = conf->disks[i].replacement;
2592 if (rdev
2593 && !test_bit(Faulty, &rdev->flags)
2594 && !test_bit(In_sync, &rdev->flags)
2595 && !rdev_set_badblocks(rdev, sh->sector,
2596 STRIPE_SECTORS, 0))
2597 abort = 1;
2598 }
2599 if (abort)
2600 conf->recovery_disabled =
2601 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002602 }
NeilBrown18b98372012-04-01 23:48:38 +10002603 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002604}
2605
NeilBrown9a3e1102011-12-23 10:17:53 +11002606static int want_replace(struct stripe_head *sh, int disk_idx)
2607{
2608 struct md_rdev *rdev;
2609 int rv = 0;
2610 /* Doing recovery so rcu locking not required */
2611 rdev = sh->raid_conf->disks[disk_idx].replacement;
2612 if (rdev
2613 && !test_bit(Faulty, &rdev->flags)
2614 && !test_bit(In_sync, &rdev->flags)
2615 && (rdev->recovery_offset <= sh->sector
2616 || rdev->mddev->recovery_cp <= sh->sector))
2617 rv = 1;
2618
2619 return rv;
2620}
2621
NeilBrown93b3dbc2011-07-27 11:00:36 +10002622/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002623 * to be read or computed to satisfy a request.
2624 *
2625 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002626 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002627 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002628static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2629 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002630{
2631 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002632 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2633 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002634
Dan Williamsf38e1212007-01-02 13:52:30 -07002635 /* is the data in this block needed, and can we get it? */
2636 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002637 !test_bit(R5_UPTODATE, &dev->flags) &&
2638 (dev->toread ||
2639 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2640 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002641 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002642 (s->failed >= 1 && fdev[0]->toread) ||
2643 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002644 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2645 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2646 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002647 /* we would like to get this block, possibly by computing it,
2648 * otherwise read it if the backing disk is insync
2649 */
2650 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2651 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2652 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002653 (s->failed && (disk_idx == s->failed_num[0] ||
2654 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002655 /* have disk failed, and we're requested to fetch it;
2656 * do compute it
2657 */
2658 pr_debug("Computing stripe %llu block %d\n",
2659 (unsigned long long)sh->sector, disk_idx);
2660 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2661 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2662 set_bit(R5_Wantcompute, &dev->flags);
2663 sh->ops.target = disk_idx;
2664 sh->ops.target2 = -1; /* no 2nd target */
2665 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002666 /* Careful: from this point on 'uptodate' is in the eye
2667 * of raid_run_ops which services 'compute' operations
2668 * before writes. R5_Wantcompute flags a block that will
2669 * be R5_UPTODATE by the time it is needed for a
2670 * subsequent operation.
2671 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002672 s->uptodate++;
2673 return 1;
2674 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2675 /* Computing 2-failure is *very* expensive; only
2676 * do it if failed >= 2
2677 */
2678 int other;
2679 for (other = disks; other--; ) {
2680 if (other == disk_idx)
2681 continue;
2682 if (!test_bit(R5_UPTODATE,
2683 &sh->dev[other].flags))
2684 break;
2685 }
2686 BUG_ON(other < 0);
2687 pr_debug("Computing stripe %llu blocks %d,%d\n",
2688 (unsigned long long)sh->sector,
2689 disk_idx, other);
2690 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2691 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2692 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2693 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2694 sh->ops.target = disk_idx;
2695 sh->ops.target2 = other;
2696 s->uptodate += 2;
2697 s->req_compute = 1;
2698 return 1;
2699 } else if (test_bit(R5_Insync, &dev->flags)) {
2700 set_bit(R5_LOCKED, &dev->flags);
2701 set_bit(R5_Wantread, &dev->flags);
2702 s->locked++;
2703 pr_debug("Reading block %d (sync=%d)\n",
2704 disk_idx, s->syncing);
2705 }
2706 }
2707
2708 return 0;
2709}
2710
2711/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002712 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002713 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002714static void handle_stripe_fill(struct stripe_head *sh,
2715 struct stripe_head_state *s,
2716 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002717{
2718 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002719
2720 /* look for blocks to read/compute, skip this if a compute
2721 * is already in flight, or if the stripe contents are in the
2722 * midst of changing due to a write
2723 */
2724 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2725 !sh->reconstruct_state)
2726 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002727 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002728 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002729 set_bit(STRIPE_HANDLE, &sh->state);
2730}
2731
2732
Dan Williams1fe797e2008-06-28 09:16:30 +10002733/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002734 * any written block on an uptodate or failed drive can be returned.
2735 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2736 * never LOCKED, so we don't need to test 'failed' directly.
2737 */
NeilBrownd1688a62011-10-11 16:49:52 +11002738static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002739 struct stripe_head *sh, int disks, struct bio **return_bi)
2740{
2741 int i;
2742 struct r5dev *dev;
2743
2744 for (i = disks; i--; )
2745 if (sh->dev[i].written) {
2746 dev = &sh->dev[i];
2747 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11002748 (test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownca64cae2012-11-21 16:33:40 +11002749 test_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002750 /* We can return any write requests */
2751 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002752 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11002753 if (test_and_clear_bit(R5_Discard, &dev->flags))
2754 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002755 wbi = dev->written;
2756 dev->written = NULL;
2757 while (wbi && wbi->bi_sector <
2758 dev->sector + STRIPE_SECTORS) {
2759 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002760 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002761 md_write_end(conf->mddev);
2762 wbi->bi_next = *return_bi;
2763 *return_bi = wbi;
2764 }
2765 wbi = wbi2;
2766 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002767 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2768 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002769 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002770 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002771 }
NeilBrownca64cae2012-11-21 16:33:40 +11002772 } else if (test_bit(R5_Discard, &sh->dev[i].flags))
2773 clear_bit(R5_Discard, &sh->dev[i].flags);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002774
2775 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2776 if (atomic_dec_and_test(&conf->pending_full_writes))
2777 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002778}
2779
NeilBrownd1688a62011-10-11 16:49:52 +11002780static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002781 struct stripe_head *sh,
2782 struct stripe_head_state *s,
2783 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002784{
2785 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002786 sector_t recovery_cp = conf->mddev->recovery_cp;
2787
2788 /* RAID6 requires 'rcw' in current implementation.
2789 * Otherwise, check whether resync is now happening or should start.
2790 * If yes, then the array is dirty (after unclean shutdown or
2791 * initial creation), so parity in some stripes might be inconsistent.
2792 * In this case, we need to always do reconstruct-write, to ensure
2793 * that in case of drive failure or read-error correction, we
2794 * generate correct data from the parity.
2795 */
2796 if (conf->max_degraded == 2 ||
2797 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2798 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002799 * look like rcw is cheaper
2800 */
2801 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002802 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2803 conf->max_degraded, (unsigned long long)recovery_cp,
2804 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002805 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002806 /* would I have to read this buffer for read_modify_write */
2807 struct r5dev *dev = &sh->dev[i];
2808 if ((dev->towrite || i == sh->pd_idx) &&
2809 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002810 !(test_bit(R5_UPTODATE, &dev->flags) ||
2811 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002812 if (test_bit(R5_Insync, &dev->flags))
2813 rmw++;
2814 else
2815 rmw += 2*disks; /* cannot read it */
2816 }
2817 /* Would I have to read this buffer for reconstruct_write */
2818 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2819 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002820 !(test_bit(R5_UPTODATE, &dev->flags) ||
2821 test_bit(R5_Wantcompute, &dev->flags))) {
2822 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002823 else
2824 rcw += 2*disks;
2825 }
2826 }
Dan Williams45b42332007-07-09 11:56:43 -07002827 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002828 (unsigned long long)sh->sector, rmw, rcw);
2829 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11002830 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002831 /* prefer read-modify-write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002832 blk_add_trace_msg(conf->mddev->queue, "raid5 rmw %llu %d",
2833 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07002834 for (i = disks; i--; ) {
2835 struct r5dev *dev = &sh->dev[i];
2836 if ((dev->towrite || i == sh->pd_idx) &&
2837 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002838 !(test_bit(R5_UPTODATE, &dev->flags) ||
2839 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002840 test_bit(R5_Insync, &dev->flags)) {
2841 if (
2842 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002843 pr_debug("Read_old block "
NeilBrowna9add5d2012-10-31 11:59:09 +11002844 "%d for r-m-w\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002845 set_bit(R5_LOCKED, &dev->flags);
2846 set_bit(R5_Wantread, &dev->flags);
2847 s->locked++;
2848 } else {
2849 set_bit(STRIPE_DELAYED, &sh->state);
2850 set_bit(STRIPE_HANDLE, &sh->state);
2851 }
2852 }
2853 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002854 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002855 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002856 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002857 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10002858 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002859 for (i = disks; i--; ) {
2860 struct r5dev *dev = &sh->dev[i];
2861 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002862 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002863 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002864 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002865 test_bit(R5_Wantcompute, &dev->flags))) {
2866 rcw++;
2867 if (!test_bit(R5_Insync, &dev->flags))
2868 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002869 if (
2870 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002871 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002872 "%d for Reconstruct\n", i);
2873 set_bit(R5_LOCKED, &dev->flags);
2874 set_bit(R5_Wantread, &dev->flags);
2875 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11002876 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07002877 } else {
2878 set_bit(STRIPE_DELAYED, &sh->state);
2879 set_bit(STRIPE_HANDLE, &sh->state);
2880 }
2881 }
2882 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002883 if (rcw)
2884 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
2885 (unsigned long long)sh->sector,
2886 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10002887 }
Dan Williamsa4456852007-07-09 11:56:43 -07002888 /* now if nothing is locked, and if we have enough data,
2889 * we can start a write request
2890 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002891 /* since handle_stripe can be called at any time we need to handle the
2892 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002893 * subsequent call wants to start a write request. raid_run_ops only
2894 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002895 * simultaneously. If this is not the case then new writes need to be
2896 * held off until the compute completes.
2897 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002898 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2899 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2900 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002901 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002902}
2903
NeilBrownd1688a62011-10-11 16:49:52 +11002904static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002905 struct stripe_head_state *s, int disks)
2906{
Dan Williamsecc65c92008-06-28 08:31:57 +10002907 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002908
Dan Williamsbd2ab672008-04-10 21:29:27 -07002909 set_bit(STRIPE_HANDLE, &sh->state);
2910
Dan Williamsecc65c92008-06-28 08:31:57 +10002911 switch (sh->check_state) {
2912 case check_state_idle:
2913 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002914 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002915 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002916 sh->check_state = check_state_run;
2917 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002918 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002919 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002920 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002921 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002922 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002923 /* fall through */
2924 case check_state_compute_result:
2925 sh->check_state = check_state_idle;
2926 if (!dev)
2927 dev = &sh->dev[sh->pd_idx];
2928
2929 /* check that a write has not made the stripe insync */
2930 if (test_bit(STRIPE_INSYNC, &sh->state))
2931 break;
2932
2933 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002934 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2935 BUG_ON(s->uptodate != disks);
2936
2937 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002938 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002939 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002940
Dan Williamsa4456852007-07-09 11:56:43 -07002941 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002942 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002943 break;
2944 case check_state_run:
2945 break; /* we will be called again upon completion */
2946 case check_state_check_result:
2947 sh->check_state = check_state_idle;
2948
2949 /* if a failure occurred during the check operation, leave
2950 * STRIPE_INSYNC not set and let the stripe be handled again
2951 */
2952 if (s->failed)
2953 break;
2954
2955 /* handle a successful check operation, if parity is correct
2956 * we are done. Otherwise update the mismatch count and repair
2957 * parity if !MD_RECOVERY_CHECK
2958 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002959 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002960 /* parity is correct (on disc,
2961 * not in buffer any more)
2962 */
2963 set_bit(STRIPE_INSYNC, &sh->state);
2964 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002965 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10002966 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2967 /* don't try to repair!! */
2968 set_bit(STRIPE_INSYNC, &sh->state);
2969 else {
2970 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002971 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002972 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2973 set_bit(R5_Wantcompute,
2974 &sh->dev[sh->pd_idx].flags);
2975 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002976 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002977 s->uptodate++;
2978 }
2979 }
2980 break;
2981 case check_state_compute_run:
2982 break;
2983 default:
2984 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2985 __func__, sh->check_state,
2986 (unsigned long long) sh->sector);
2987 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002988 }
2989}
2990
2991
NeilBrownd1688a62011-10-11 16:49:52 +11002992static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002993 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002994 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002995{
Dan Williamsa4456852007-07-09 11:56:43 -07002996 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002997 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002998 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002999
3000 set_bit(STRIPE_HANDLE, &sh->state);
3001
3002 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003003
Dan Williamsa4456852007-07-09 11:56:43 -07003004 /* Want to check and possibly repair P and Q.
3005 * However there could be one 'failed' device, in which
3006 * case we can only check one of them, possibly using the
3007 * other to generate missing data
3008 */
3009
Dan Williamsd82dfee2009-07-14 13:40:57 -07003010 switch (sh->check_state) {
3011 case check_state_idle:
3012 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003013 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003014 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003015 * makes sense to check P (If anything else were failed,
3016 * we would have used P to recreate it).
3017 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003018 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003019 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003020 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003021 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003022 * anything, so it makes sense to check it
3023 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003024 if (sh->check_state == check_state_run)
3025 sh->check_state = check_state_run_pq;
3026 else
3027 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003028 }
Dan Williams36d1c642009-07-14 11:48:22 -07003029
Dan Williamsd82dfee2009-07-14 13:40:57 -07003030 /* discard potentially stale zero_sum_result */
3031 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003032
Dan Williamsd82dfee2009-07-14 13:40:57 -07003033 if (sh->check_state == check_state_run) {
3034 /* async_xor_zero_sum destroys the contents of P */
3035 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3036 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003037 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003038 if (sh->check_state >= check_state_run &&
3039 sh->check_state <= check_state_run_pq) {
3040 /* async_syndrome_zero_sum preserves P and Q, so
3041 * no need to mark them !uptodate here
3042 */
3043 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3044 break;
3045 }
Dan Williams36d1c642009-07-14 11:48:22 -07003046
Dan Williamsd82dfee2009-07-14 13:40:57 -07003047 /* we have 2-disk failure */
3048 BUG_ON(s->failed != 2);
3049 /* fall through */
3050 case check_state_compute_result:
3051 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003052
Dan Williamsd82dfee2009-07-14 13:40:57 -07003053 /* check that a write has not made the stripe insync */
3054 if (test_bit(STRIPE_INSYNC, &sh->state))
3055 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003056
3057 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003058 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003059 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003060 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003061 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003062 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003063 s->locked++;
3064 set_bit(R5_LOCKED, &dev->flags);
3065 set_bit(R5_Wantwrite, &dev->flags);
3066 }
3067 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003068 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003069 s->locked++;
3070 set_bit(R5_LOCKED, &dev->flags);
3071 set_bit(R5_Wantwrite, &dev->flags);
3072 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003073 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003074 dev = &sh->dev[pd_idx];
3075 s->locked++;
3076 set_bit(R5_LOCKED, &dev->flags);
3077 set_bit(R5_Wantwrite, &dev->flags);
3078 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003079 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003080 dev = &sh->dev[qd_idx];
3081 s->locked++;
3082 set_bit(R5_LOCKED, &dev->flags);
3083 set_bit(R5_Wantwrite, &dev->flags);
3084 }
3085 clear_bit(STRIPE_DEGRADED, &sh->state);
3086
3087 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003088 break;
3089 case check_state_run:
3090 case check_state_run_q:
3091 case check_state_run_pq:
3092 break; /* we will be called again upon completion */
3093 case check_state_check_result:
3094 sh->check_state = check_state_idle;
3095
3096 /* handle a successful check operation, if parity is correct
3097 * we are done. Otherwise update the mismatch count and repair
3098 * parity if !MD_RECOVERY_CHECK
3099 */
3100 if (sh->ops.zero_sum_result == 0) {
3101 /* both parities are correct */
3102 if (!s->failed)
3103 set_bit(STRIPE_INSYNC, &sh->state);
3104 else {
3105 /* in contrast to the raid5 case we can validate
3106 * parity, but still have a failure to write
3107 * back
3108 */
3109 sh->check_state = check_state_compute_result;
3110 /* Returning at this point means that we may go
3111 * off and bring p and/or q uptodate again so
3112 * we make sure to check zero_sum_result again
3113 * to verify if p or q need writeback
3114 */
3115 }
3116 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003117 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003118 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3119 /* don't try to repair!! */
3120 set_bit(STRIPE_INSYNC, &sh->state);
3121 else {
3122 int *target = &sh->ops.target;
3123
3124 sh->ops.target = -1;
3125 sh->ops.target2 = -1;
3126 sh->check_state = check_state_compute_run;
3127 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3128 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3129 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3130 set_bit(R5_Wantcompute,
3131 &sh->dev[pd_idx].flags);
3132 *target = pd_idx;
3133 target = &sh->ops.target2;
3134 s->uptodate++;
3135 }
3136 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3137 set_bit(R5_Wantcompute,
3138 &sh->dev[qd_idx].flags);
3139 *target = qd_idx;
3140 s->uptodate++;
3141 }
3142 }
3143 }
3144 break;
3145 case check_state_compute_run:
3146 break;
3147 default:
3148 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3149 __func__, sh->check_state,
3150 (unsigned long long) sh->sector);
3151 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003152 }
3153}
3154
NeilBrownd1688a62011-10-11 16:49:52 +11003155static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003156{
3157 int i;
3158
3159 /* We have read all the blocks in this stripe and now we need to
3160 * copy some of them into a target stripe for expand.
3161 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003162 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003163 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3164 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003165 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003166 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003167 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003168 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003169
NeilBrown784052e2009-03-31 15:19:07 +11003170 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003171 sector_t s = raid5_compute_sector(conf, bn, 0,
3172 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003173 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003174 if (sh2 == NULL)
3175 /* so far only the early blocks of this stripe
3176 * have been requested. When later blocks
3177 * get requested, we will try again
3178 */
3179 continue;
3180 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3181 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3182 /* must have already done this block */
3183 release_stripe(sh2);
3184 continue;
3185 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003186
3187 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003188 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003189 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003190 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003191 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003192
Dan Williamsa4456852007-07-09 11:56:43 -07003193 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3194 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3195 for (j = 0; j < conf->raid_disks; j++)
3196 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003197 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003198 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3199 break;
3200 if (j == conf->raid_disks) {
3201 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3202 set_bit(STRIPE_HANDLE, &sh2->state);
3203 }
3204 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003205
Dan Williamsa4456852007-07-09 11:56:43 -07003206 }
NeilBrowna2e08552007-09-11 15:23:36 -07003207 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003208 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003209}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
3211/*
3212 * handle_stripe - do things to a stripe.
3213 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003214 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3215 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003217 * return some read requests which now have data
3218 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 * schedule a read on some buffers
3220 * schedule a write of some buffers
3221 * return confirmation of parity correctness
3222 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 */
Dan Williamsa4456852007-07-09 11:56:43 -07003224
NeilBrownacfe7262011-07-27 11:00:36 +10003225static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003226{
NeilBrownd1688a62011-10-11 16:49:52 +11003227 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003228 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003229 struct r5dev *dev;
3230 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003231 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003232
NeilBrownacfe7262011-07-27 11:00:36 +10003233 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003234
NeilBrownacfe7262011-07-27 11:00:36 +10003235 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3236 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3237 s->failed_num[0] = -1;
3238 s->failed_num[1] = -1;
3239
3240 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003241 rcu_read_lock();
3242 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003243 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003244 sector_t first_bad;
3245 int bad_sectors;
3246 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003247
NeilBrown16a53ec2006-06-26 00:27:38 -07003248 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003249
Dan Williams45b42332007-07-09 11:56:43 -07003250 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003251 i, dev->flags,
3252 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003253 /* maybe we can reply to a read
3254 *
3255 * new wantfill requests are only permitted while
3256 * ops_complete_biofill is guaranteed to be inactive
3257 */
3258 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3259 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3260 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003261
3262 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003263 if (test_bit(R5_LOCKED, &dev->flags))
3264 s->locked++;
3265 if (test_bit(R5_UPTODATE, &dev->flags))
3266 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003267 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003268 s->compute++;
3269 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003270 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003271
NeilBrownacfe7262011-07-27 11:00:36 +10003272 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003273 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003274 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003275 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003276 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003277 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003278 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003279 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003280 }
Dan Williamsa4456852007-07-09 11:56:43 -07003281 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003282 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003283 /* Prefer to use the replacement for reads, but only
3284 * if it is recovered enough and has no bad blocks.
3285 */
3286 rdev = rcu_dereference(conf->disks[i].replacement);
3287 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3288 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3289 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3290 &first_bad, &bad_sectors))
3291 set_bit(R5_ReadRepl, &dev->flags);
3292 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003293 if (rdev)
3294 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003295 rdev = rcu_dereference(conf->disks[i].rdev);
3296 clear_bit(R5_ReadRepl, &dev->flags);
3297 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003298 if (rdev && test_bit(Faulty, &rdev->flags))
3299 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003300 if (rdev) {
3301 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3302 &first_bad, &bad_sectors);
3303 if (s->blocked_rdev == NULL
3304 && (test_bit(Blocked, &rdev->flags)
3305 || is_bad < 0)) {
3306 if (is_bad < 0)
3307 set_bit(BlockedBadBlocks,
3308 &rdev->flags);
3309 s->blocked_rdev = rdev;
3310 atomic_inc(&rdev->nr_pending);
3311 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003312 }
NeilBrown415e72d2010-06-17 17:25:21 +10003313 clear_bit(R5_Insync, &dev->flags);
3314 if (!rdev)
3315 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003316 else if (is_bad) {
3317 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003318 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3319 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003320 /* treat as in-sync, but with a read error
3321 * which we can now try to correct
3322 */
3323 set_bit(R5_Insync, &dev->flags);
3324 set_bit(R5_ReadError, &dev->flags);
3325 }
3326 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003327 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003328 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003329 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003330 set_bit(R5_Insync, &dev->flags);
3331 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3332 test_bit(R5_Expanded, &dev->flags))
3333 /* If we've reshaped into here, we assume it is Insync.
3334 * We will shortly update recovery_offset to make
3335 * it official.
3336 */
3337 set_bit(R5_Insync, &dev->flags);
3338
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003339 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003340 /* This flag does not apply to '.replacement'
3341 * only to .rdev, so make sure to check that*/
3342 struct md_rdev *rdev2 = rcu_dereference(
3343 conf->disks[i].rdev);
3344 if (rdev2 == rdev)
3345 clear_bit(R5_Insync, &dev->flags);
3346 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003347 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003348 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003349 } else
3350 clear_bit(R5_WriteError, &dev->flags);
3351 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003352 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003353 /* This flag does not apply to '.replacement'
3354 * only to .rdev, so make sure to check that*/
3355 struct md_rdev *rdev2 = rcu_dereference(
3356 conf->disks[i].rdev);
3357 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003358 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003359 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003360 } else
3361 clear_bit(R5_MadeGood, &dev->flags);
3362 }
NeilBrown977df362011-12-23 10:17:53 +11003363 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3364 struct md_rdev *rdev2 = rcu_dereference(
3365 conf->disks[i].replacement);
3366 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3367 s->handle_bad_blocks = 1;
3368 atomic_inc(&rdev2->nr_pending);
3369 } else
3370 clear_bit(R5_MadeGoodRepl, &dev->flags);
3371 }
NeilBrown415e72d2010-06-17 17:25:21 +10003372 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003373 /* The ReadError flag will just be confusing now */
3374 clear_bit(R5_ReadError, &dev->flags);
3375 clear_bit(R5_ReWrite, &dev->flags);
3376 }
NeilBrown415e72d2010-06-17 17:25:21 +10003377 if (test_bit(R5_ReadError, &dev->flags))
3378 clear_bit(R5_Insync, &dev->flags);
3379 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003380 if (s->failed < 2)
3381 s->failed_num[s->failed] = i;
3382 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003383 if (rdev && !test_bit(Faulty, &rdev->flags))
3384 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003385 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003386 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003387 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3388 /* If there is a failed device being replaced,
3389 * we must be recovering.
3390 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003391 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003392 * else we can only be replacing
3393 * sync and recovery both need to read all devices, and so
3394 * use the same flag.
3395 */
3396 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003397 sh->sector >= conf->mddev->recovery_cp ||
3398 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003399 s->syncing = 1;
3400 else
3401 s->replacing = 1;
3402 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003403 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003404}
NeilBrownf4168852007-02-28 20:11:53 -08003405
NeilBrowncc940152011-07-26 11:35:35 +10003406static void handle_stripe(struct stripe_head *sh)
3407{
3408 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003409 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003410 int i;
NeilBrown84789552011-07-27 11:00:36 +10003411 int prexor;
3412 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003413 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003414
3415 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003416 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003417 /* already being handled, ensure it gets handled
3418 * again when current action finishes */
3419 set_bit(STRIPE_HANDLE, &sh->state);
3420 return;
3421 }
3422
3423 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3424 set_bit(STRIPE_SYNCING, &sh->state);
3425 clear_bit(STRIPE_INSYNC, &sh->state);
3426 }
3427 clear_bit(STRIPE_DELAYED, &sh->state);
3428
3429 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3430 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3431 (unsigned long long)sh->sector, sh->state,
3432 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3433 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003434
NeilBrownacfe7262011-07-27 11:00:36 +10003435 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003436
NeilBrownbc2607f2011-07-28 11:39:22 +10003437 if (s.handle_bad_blocks) {
3438 set_bit(STRIPE_HANDLE, &sh->state);
3439 goto finish;
3440 }
3441
NeilBrown474af965fe2011-07-27 11:00:36 +10003442 if (unlikely(s.blocked_rdev)) {
3443 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003444 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003445 set_bit(STRIPE_HANDLE, &sh->state);
3446 goto finish;
3447 }
3448 /* There is nothing for the blocked_rdev to block */
3449 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3450 s.blocked_rdev = NULL;
3451 }
3452
3453 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3454 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3455 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3456 }
3457
3458 pr_debug("locked=%d uptodate=%d to_read=%d"
3459 " to_write=%d failed=%d failed_num=%d,%d\n",
3460 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3461 s.failed_num[0], s.failed_num[1]);
3462 /* check if the array has lost more than max_degraded devices and,
3463 * if so, some requests might need to be failed.
3464 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003465 if (s.failed > conf->max_degraded) {
3466 sh->check_state = 0;
3467 sh->reconstruct_state = 0;
3468 if (s.to_read+s.to_write+s.written)
3469 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003470 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003471 handle_failed_sync(conf, sh, &s);
3472 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003473
NeilBrown84789552011-07-27 11:00:36 +10003474 /* Now we check to see if any write operations have recently
3475 * completed
3476 */
3477 prexor = 0;
3478 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3479 prexor = 1;
3480 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3481 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3482 sh->reconstruct_state = reconstruct_state_idle;
3483
3484 /* All the 'written' buffers and the parity block are ready to
3485 * be written back to disk
3486 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003487 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3488 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003489 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003490 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3491 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003492 for (i = disks; i--; ) {
3493 struct r5dev *dev = &sh->dev[i];
3494 if (test_bit(R5_LOCKED, &dev->flags) &&
3495 (i == sh->pd_idx || i == sh->qd_idx ||
3496 dev->written)) {
3497 pr_debug("Writing block %d\n", i);
3498 set_bit(R5_Wantwrite, &dev->flags);
3499 if (prexor)
3500 continue;
3501 if (!test_bit(R5_Insync, &dev->flags) ||
3502 ((i == sh->pd_idx || i == sh->qd_idx) &&
3503 s.failed == 0))
3504 set_bit(STRIPE_INSYNC, &sh->state);
3505 }
3506 }
3507 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3508 s.dec_preread_active = 1;
3509 }
3510
NeilBrownef5b7c62012-11-22 09:13:36 +11003511 /*
3512 * might be able to return some write requests if the parity blocks
3513 * are safe, or on a failed drive
3514 */
3515 pdev = &sh->dev[sh->pd_idx];
3516 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3517 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3518 qdev = &sh->dev[sh->qd_idx];
3519 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3520 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3521 || conf->level < 6;
3522
3523 if (s.written &&
3524 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3525 && !test_bit(R5_LOCKED, &pdev->flags)
3526 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3527 test_bit(R5_Discard, &pdev->flags))))) &&
3528 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3529 && !test_bit(R5_LOCKED, &qdev->flags)
3530 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3531 test_bit(R5_Discard, &qdev->flags))))))
3532 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3533
3534 /* Now we might consider reading some blocks, either to check/generate
3535 * parity, or to satisfy requests
3536 * or to load a block that is being partially written.
3537 */
3538 if (s.to_read || s.non_overwrite
3539 || (conf->level == 6 && s.to_write && s.failed)
3540 || (s.syncing && (s.uptodate + s.compute < disks))
3541 || s.replacing
3542 || s.expanding)
3543 handle_stripe_fill(sh, &s, disks);
3544
NeilBrown84789552011-07-27 11:00:36 +10003545 /* Now to consider new write requests and what else, if anything
3546 * should be read. We do not handle new writes when:
3547 * 1/ A 'write' operation (copy+xor) is already in flight.
3548 * 2/ A 'check' operation is in flight, as it may clobber the parity
3549 * block.
3550 */
3551 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3552 handle_stripe_dirtying(conf, sh, &s, disks);
3553
3554 /* maybe we need to check and possibly fix the parity for this stripe
3555 * Any reads will already have been scheduled, so we just see if enough
3556 * data is available. The parity check is held off while parity
3557 * dependent operations are in flight.
3558 */
3559 if (sh->check_state ||
3560 (s.syncing && s.locked == 0 &&
3561 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3562 !test_bit(STRIPE_INSYNC, &sh->state))) {
3563 if (conf->level == 6)
3564 handle_parity_checks6(conf, sh, &s, disks);
3565 else
3566 handle_parity_checks5(conf, sh, &s, disks);
3567 }
NeilBrownc5a31002011-07-27 11:00:36 +10003568
NeilBrown9a3e1102011-12-23 10:17:53 +11003569 if (s.replacing && s.locked == 0
3570 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3571 /* Write out to replacement devices where possible */
3572 for (i = 0; i < conf->raid_disks; i++)
3573 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3574 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3575 set_bit(R5_WantReplace, &sh->dev[i].flags);
3576 set_bit(R5_LOCKED, &sh->dev[i].flags);
3577 s.locked++;
3578 }
3579 set_bit(STRIPE_INSYNC, &sh->state);
3580 }
3581 if ((s.syncing || s.replacing) && s.locked == 0 &&
3582 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003583 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3584 clear_bit(STRIPE_SYNCING, &sh->state);
3585 }
3586
3587 /* If the failed drives are just a ReadError, then we might need
3588 * to progress the repair/check process
3589 */
3590 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3591 for (i = 0; i < s.failed; i++) {
3592 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3593 if (test_bit(R5_ReadError, &dev->flags)
3594 && !test_bit(R5_LOCKED, &dev->flags)
3595 && test_bit(R5_UPTODATE, &dev->flags)
3596 ) {
3597 if (!test_bit(R5_ReWrite, &dev->flags)) {
3598 set_bit(R5_Wantwrite, &dev->flags);
3599 set_bit(R5_ReWrite, &dev->flags);
3600 set_bit(R5_LOCKED, &dev->flags);
3601 s.locked++;
3602 } else {
3603 /* let's read it back */
3604 set_bit(R5_Wantread, &dev->flags);
3605 set_bit(R5_LOCKED, &dev->flags);
3606 s.locked++;
3607 }
3608 }
3609 }
3610
3611
NeilBrown3687c062011-07-27 11:00:36 +10003612 /* Finish reconstruct operations initiated by the expansion process */
3613 if (sh->reconstruct_state == reconstruct_state_result) {
3614 struct stripe_head *sh_src
3615 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3616 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3617 /* sh cannot be written until sh_src has been read.
3618 * so arrange for sh to be delayed a little
3619 */
3620 set_bit(STRIPE_DELAYED, &sh->state);
3621 set_bit(STRIPE_HANDLE, &sh->state);
3622 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3623 &sh_src->state))
3624 atomic_inc(&conf->preread_active_stripes);
3625 release_stripe(sh_src);
3626 goto finish;
3627 }
3628 if (sh_src)
3629 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003630
NeilBrown3687c062011-07-27 11:00:36 +10003631 sh->reconstruct_state = reconstruct_state_idle;
3632 clear_bit(STRIPE_EXPANDING, &sh->state);
3633 for (i = conf->raid_disks; i--; ) {
3634 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3635 set_bit(R5_LOCKED, &sh->dev[i].flags);
3636 s.locked++;
3637 }
3638 }
3639
3640 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3641 !sh->reconstruct_state) {
3642 /* Need to write out all blocks after computing parity */
3643 sh->disks = conf->raid_disks;
3644 stripe_set_idx(sh->sector, conf, 0, sh);
3645 schedule_reconstruction(sh, &s, 1, 1);
3646 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3647 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3648 atomic_dec(&conf->reshape_stripes);
3649 wake_up(&conf->wait_for_overlap);
3650 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3651 }
3652
3653 if (s.expanding && s.locked == 0 &&
3654 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3655 handle_stripe_expansion(conf, sh);
3656
3657finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003658 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003659 if (unlikely(s.blocked_rdev)) {
3660 if (conf->mddev->external)
3661 md_wait_for_blocked_rdev(s.blocked_rdev,
3662 conf->mddev);
3663 else
3664 /* Internal metadata will immediately
3665 * be written by raid5d, so we don't
3666 * need to wait here.
3667 */
3668 rdev_dec_pending(s.blocked_rdev,
3669 conf->mddev);
3670 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003671
NeilBrownbc2607f2011-07-28 11:39:22 +10003672 if (s.handle_bad_blocks)
3673 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003674 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003675 struct r5dev *dev = &sh->dev[i];
3676 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3677 /* We own a safe reference to the rdev */
3678 rdev = conf->disks[i].rdev;
3679 if (!rdev_set_badblocks(rdev, sh->sector,
3680 STRIPE_SECTORS, 0))
3681 md_error(conf->mddev, rdev);
3682 rdev_dec_pending(rdev, conf->mddev);
3683 }
NeilBrownb84db562011-07-28 11:39:23 +10003684 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3685 rdev = conf->disks[i].rdev;
3686 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003687 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003688 rdev_dec_pending(rdev, conf->mddev);
3689 }
NeilBrown977df362011-12-23 10:17:53 +11003690 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3691 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003692 if (!rdev)
3693 /* rdev have been moved down */
3694 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003695 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003696 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003697 rdev_dec_pending(rdev, conf->mddev);
3698 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003699 }
3700
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003701 if (s.ops_request)
3702 raid_run_ops(sh, s.ops_request);
3703
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003704 ops_run_io(sh, &s);
3705
NeilBrownc5709ef2011-07-26 11:35:20 +10003706 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003707 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003708 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003709 * have actually been submitted.
3710 */
3711 atomic_dec(&conf->preread_active_stripes);
3712 if (atomic_read(&conf->preread_active_stripes) <
3713 IO_THRESHOLD)
3714 md_wakeup_thread(conf->mddev->thread);
3715 }
3716
NeilBrownc5709ef2011-07-26 11:35:20 +10003717 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003718
Dan Williams257a4b42011-11-08 16:22:06 +11003719 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003720}
3721
NeilBrownd1688a62011-10-11 16:49:52 +11003722static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723{
3724 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3725 while (!list_empty(&conf->delayed_list)) {
3726 struct list_head *l = conf->delayed_list.next;
3727 struct stripe_head *sh;
3728 sh = list_entry(l, struct stripe_head, lru);
3729 list_del_init(l);
3730 clear_bit(STRIPE_DELAYED, &sh->state);
3731 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3732 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003733 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 }
NeilBrown482c0832011-04-18 18:25:42 +10003735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736}
3737
NeilBrownd1688a62011-10-11 16:49:52 +11003738static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003739{
3740 /* device_lock is held */
3741 struct list_head head;
3742 list_add(&head, &conf->bitmap_list);
3743 list_del_init(&conf->bitmap_list);
3744 while (!list_empty(&head)) {
3745 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3746 list_del_init(&sh->lru);
3747 atomic_inc(&sh->count);
3748 __release_stripe(conf, sh);
3749 }
3750}
3751
NeilBrownfd01b882011-10-11 16:47:53 +11003752int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003753{
NeilBrownd1688a62011-10-11 16:49:52 +11003754 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003755
3756 /* No difference between reads and writes. Just check
3757 * how busy the stripe_cache is
3758 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003759
NeilBrownf022b2f2006-10-03 01:15:56 -07003760 if (conf->inactive_blocked)
3761 return 1;
3762 if (conf->quiesce)
3763 return 1;
3764 if (list_empty_careful(&conf->inactive_list))
3765 return 1;
3766
3767 return 0;
3768}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003769EXPORT_SYMBOL_GPL(md_raid5_congested);
3770
3771static int raid5_congested(void *data, int bits)
3772{
NeilBrownfd01b882011-10-11 16:47:53 +11003773 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003774
3775 return mddev_congested(mddev, bits) ||
3776 md_raid5_congested(mddev, bits);
3777}
NeilBrownf022b2f2006-10-03 01:15:56 -07003778
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003779/* We want read requests to align with chunks where possible,
3780 * but write requests don't need to.
3781 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003782static int raid5_mergeable_bvec(struct request_queue *q,
3783 struct bvec_merge_data *bvm,
3784 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003785{
NeilBrownfd01b882011-10-11 16:47:53 +11003786 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003787 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003788 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003789 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003790 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003791
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003792 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003793 return biovec->bv_len; /* always allow writes to be mergeable */
3794
Andre Noll664e7c42009-06-18 08:45:27 +10003795 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3796 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003797 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3798 if (max < 0) max = 0;
3799 if (max <= biovec->bv_len && bio_sectors == 0)
3800 return biovec->bv_len;
3801 else
3802 return max;
3803}
3804
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003805
NeilBrownfd01b882011-10-11 16:47:53 +11003806static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003807{
3808 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003809 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003810 unsigned int bio_sectors = bio->bi_size >> 9;
3811
Andre Noll664e7c42009-06-18 08:45:27 +10003812 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3813 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003814 return chunk_sectors >=
3815 ((sector & (chunk_sectors - 1)) + bio_sectors);
3816}
3817
3818/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003819 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3820 * later sampled by raid5d.
3821 */
NeilBrownd1688a62011-10-11 16:49:52 +11003822static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003823{
3824 unsigned long flags;
3825
3826 spin_lock_irqsave(&conf->device_lock, flags);
3827
3828 bi->bi_next = conf->retry_read_aligned_list;
3829 conf->retry_read_aligned_list = bi;
3830
3831 spin_unlock_irqrestore(&conf->device_lock, flags);
3832 md_wakeup_thread(conf->mddev->thread);
3833}
3834
3835
NeilBrownd1688a62011-10-11 16:49:52 +11003836static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003837{
3838 struct bio *bi;
3839
3840 bi = conf->retry_read_aligned;
3841 if (bi) {
3842 conf->retry_read_aligned = NULL;
3843 return bi;
3844 }
3845 bi = conf->retry_read_aligned_list;
3846 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003847 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003848 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003849 /*
3850 * this sets the active strip count to 1 and the processed
3851 * strip count to zero (upper 8 bits)
3852 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003853 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003854 }
3855
3856 return bi;
3857}
3858
3859
3860/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003861 * The "raid5_align_endio" should check if the read succeeded and if it
3862 * did, call bio_endio on the original bio (having bio_put the new bio
3863 * first).
3864 * If the read failed..
3865 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003866static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003867{
3868 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003869 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003870 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003871 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003872 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003873
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003874 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003875
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003876 rdev = (void*)raid_bi->bi_next;
3877 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003878 mddev = rdev->mddev;
3879 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003880
3881 rdev_dec_pending(rdev, conf->mddev);
3882
3883 if (!error && uptodate) {
NeilBrowna9add5d2012-10-31 11:59:09 +11003884 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
3885 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02003886 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003887 if (atomic_dec_and_test(&conf->active_aligned_reads))
3888 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003889 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003890 }
3891
3892
Dan Williams45b42332007-07-09 11:56:43 -07003893 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003894
3895 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003896}
3897
Neil Brown387bb172007-02-08 14:20:29 -08003898static int bio_fits_rdev(struct bio *bi)
3899{
Jens Axboe165125e2007-07-24 09:28:11 +02003900 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003901
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003902 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003903 return 0;
3904 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003905 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003906 return 0;
3907
3908 if (q->merge_bvec_fn)
3909 /* it's too hard to apply the merge_bvec_fn at this stage,
3910 * just just give up
3911 */
3912 return 0;
3913
3914 return 1;
3915}
3916
3917
NeilBrownfd01b882011-10-11 16:47:53 +11003918static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003919{
NeilBrownd1688a62011-10-11 16:49:52 +11003920 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11003921 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003922 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003923 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003924 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003925
3926 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003927 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003928 return 0;
3929 }
3930 /*
NeilBrowna167f662010-10-26 18:31:13 +11003931 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003932 */
NeilBrowna167f662010-10-26 18:31:13 +11003933 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003934 if (!align_bi)
3935 return 0;
3936 /*
3937 * set bi_end_io to a new function, and set bi_private to the
3938 * original bio.
3939 */
3940 align_bi->bi_end_io = raid5_align_endio;
3941 align_bi->bi_private = raid_bio;
3942 /*
3943 * compute position
3944 */
NeilBrown112bf892009-03-31 14:39:38 +11003945 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3946 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003947 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003948
NeilBrown671488c2011-12-23 10:17:52 +11003949 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003950 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003951 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3952 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3953 rdev->recovery_offset < end_sector) {
3954 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3955 if (rdev &&
3956 (test_bit(Faulty, &rdev->flags) ||
3957 !(test_bit(In_sync, &rdev->flags) ||
3958 rdev->recovery_offset >= end_sector)))
3959 rdev = NULL;
3960 }
3961 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003962 sector_t first_bad;
3963 int bad_sectors;
3964
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003965 atomic_inc(&rdev->nr_pending);
3966 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003967 raid_bio->bi_next = (void*)rdev;
3968 align_bi->bi_bdev = rdev->bdev;
3969 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003970
NeilBrown31c176e2011-07-28 11:39:22 +10003971 if (!bio_fits_rdev(align_bi) ||
3972 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3973 &first_bad, &bad_sectors)) {
3974 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003975 bio_put(align_bi);
3976 rdev_dec_pending(rdev, mddev);
3977 return 0;
3978 }
3979
majianpeng6c0544e2012-06-12 08:31:10 +08003980 /* No reshape active, so we can trust rdev->data_offset */
3981 align_bi->bi_sector += rdev->data_offset;
3982
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003983 spin_lock_irq(&conf->device_lock);
3984 wait_event_lock_irq(conf->wait_for_stripe,
3985 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01003986 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003987 atomic_inc(&conf->active_aligned_reads);
3988 spin_unlock_irq(&conf->device_lock);
3989
NeilBrowna9add5d2012-10-31 11:59:09 +11003990 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
3991 align_bi, disk_devt(mddev->gendisk),
3992 raid_bio->bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003993 generic_make_request(align_bi);
3994 return 1;
3995 } else {
3996 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003997 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003998 return 0;
3999 }
4000}
4001
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004002/* __get_priority_stripe - get the next stripe to process
4003 *
4004 * Full stripe writes are allowed to pass preread active stripes up until
4005 * the bypass_threshold is exceeded. In general the bypass_count
4006 * increments when the handle_list is handled before the hold_list; however, it
4007 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4008 * stripe with in flight i/o. The bypass_count will be reset when the
4009 * head of the hold_list has changed, i.e. the head was promoted to the
4010 * handle_list.
4011 */
NeilBrownd1688a62011-10-11 16:49:52 +11004012static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004013{
4014 struct stripe_head *sh;
4015
4016 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4017 __func__,
4018 list_empty(&conf->handle_list) ? "empty" : "busy",
4019 list_empty(&conf->hold_list) ? "empty" : "busy",
4020 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4021
4022 if (!list_empty(&conf->handle_list)) {
4023 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4024
4025 if (list_empty(&conf->hold_list))
4026 conf->bypass_count = 0;
4027 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4028 if (conf->hold_list.next == conf->last_hold)
4029 conf->bypass_count++;
4030 else {
4031 conf->last_hold = conf->hold_list.next;
4032 conf->bypass_count -= conf->bypass_threshold;
4033 if (conf->bypass_count < 0)
4034 conf->bypass_count = 0;
4035 }
4036 }
4037 } else if (!list_empty(&conf->hold_list) &&
4038 ((conf->bypass_threshold &&
4039 conf->bypass_count > conf->bypass_threshold) ||
4040 atomic_read(&conf->pending_full_writes) == 0)) {
4041 sh = list_entry(conf->hold_list.next,
4042 typeof(*sh), lru);
4043 conf->bypass_count -= conf->bypass_threshold;
4044 if (conf->bypass_count < 0)
4045 conf->bypass_count = 0;
4046 } else
4047 return NULL;
4048
4049 list_del_init(&sh->lru);
4050 atomic_inc(&sh->count);
4051 BUG_ON(atomic_read(&sh->count) != 1);
4052 return sh;
4053}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004054
Shaohua Li8811b592012-08-02 08:33:00 +10004055struct raid5_plug_cb {
4056 struct blk_plug_cb cb;
4057 struct list_head list;
4058};
4059
4060static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4061{
4062 struct raid5_plug_cb *cb = container_of(
4063 blk_cb, struct raid5_plug_cb, cb);
4064 struct stripe_head *sh;
4065 struct mddev *mddev = cb->cb.data;
4066 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004067 int cnt = 0;
Shaohua Li8811b592012-08-02 08:33:00 +10004068
4069 if (cb->list.next && !list_empty(&cb->list)) {
4070 spin_lock_irq(&conf->device_lock);
4071 while (!list_empty(&cb->list)) {
4072 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4073 list_del_init(&sh->lru);
4074 /*
4075 * avoid race release_stripe_plug() sees
4076 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4077 * is still in our list
4078 */
4079 smp_mb__before_clear_bit();
4080 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4081 __release_stripe(conf, sh);
NeilBrowna9add5d2012-10-31 11:59:09 +11004082 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004083 }
4084 spin_unlock_irq(&conf->device_lock);
4085 }
NeilBrowna9add5d2012-10-31 11:59:09 +11004086 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004087 kfree(cb);
4088}
4089
4090static void release_stripe_plug(struct mddev *mddev,
4091 struct stripe_head *sh)
4092{
4093 struct blk_plug_cb *blk_cb = blk_check_plugged(
4094 raid5_unplug, mddev,
4095 sizeof(struct raid5_plug_cb));
4096 struct raid5_plug_cb *cb;
4097
4098 if (!blk_cb) {
4099 release_stripe(sh);
4100 return;
4101 }
4102
4103 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4104
4105 if (cb->list.next == NULL)
4106 INIT_LIST_HEAD(&cb->list);
4107
4108 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4109 list_add_tail(&sh->lru, &cb->list);
4110 else
4111 release_stripe(sh);
4112}
4113
Shaohua Li620125f2012-10-11 13:49:05 +11004114static void make_discard_request(struct mddev *mddev, struct bio *bi)
4115{
4116 struct r5conf *conf = mddev->private;
4117 sector_t logical_sector, last_sector;
4118 struct stripe_head *sh;
4119 int remaining;
4120 int stripe_sectors;
4121
4122 if (mddev->reshape_position != MaxSector)
4123 /* Skip discard while reshape is happening */
4124 return;
4125
4126 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4127 last_sector = bi->bi_sector + (bi->bi_size>>9);
4128
4129 bi->bi_next = NULL;
4130 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4131
4132 stripe_sectors = conf->chunk_sectors *
4133 (conf->raid_disks - conf->max_degraded);
4134 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4135 stripe_sectors);
4136 sector_div(last_sector, stripe_sectors);
4137
4138 logical_sector *= conf->chunk_sectors;
4139 last_sector *= conf->chunk_sectors;
4140
4141 for (; logical_sector < last_sector;
4142 logical_sector += STRIPE_SECTORS) {
4143 DEFINE_WAIT(w);
4144 int d;
4145 again:
4146 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4147 prepare_to_wait(&conf->wait_for_overlap, &w,
4148 TASK_UNINTERRUPTIBLE);
4149 spin_lock_irq(&sh->stripe_lock);
4150 for (d = 0; d < conf->raid_disks; d++) {
4151 if (d == sh->pd_idx || d == sh->qd_idx)
4152 continue;
4153 if (sh->dev[d].towrite || sh->dev[d].toread) {
4154 set_bit(R5_Overlap, &sh->dev[d].flags);
4155 spin_unlock_irq(&sh->stripe_lock);
4156 release_stripe(sh);
4157 schedule();
4158 goto again;
4159 }
4160 }
4161 finish_wait(&conf->wait_for_overlap, &w);
4162 for (d = 0; d < conf->raid_disks; d++) {
4163 if (d == sh->pd_idx || d == sh->qd_idx)
4164 continue;
4165 sh->dev[d].towrite = bi;
4166 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4167 raid5_inc_bi_active_stripes(bi);
4168 }
4169 spin_unlock_irq(&sh->stripe_lock);
4170 if (conf->mddev->bitmap) {
4171 for (d = 0;
4172 d < conf->raid_disks - conf->max_degraded;
4173 d++)
4174 bitmap_startwrite(mddev->bitmap,
4175 sh->sector,
4176 STRIPE_SECTORS,
4177 0);
4178 sh->bm_seq = conf->seq_flush + 1;
4179 set_bit(STRIPE_BIT_DELAY, &sh->state);
4180 }
4181
4182 set_bit(STRIPE_HANDLE, &sh->state);
4183 clear_bit(STRIPE_DELAYED, &sh->state);
4184 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4185 atomic_inc(&conf->preread_active_stripes);
4186 release_stripe_plug(mddev, sh);
4187 }
4188
4189 remaining = raid5_dec_bi_active_stripes(bi);
4190 if (remaining == 0) {
4191 md_write_end(mddev);
4192 bio_endio(bi, 0);
4193 }
4194}
4195
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004196static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197{
NeilBrownd1688a62011-10-11 16:49:52 +11004198 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004199 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 sector_t new_sector;
4201 sector_t logical_sector, last_sector;
4202 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004203 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004204 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
Tejun Heoe9c74692010-09-03 11:56:18 +02004206 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4207 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004208 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004209 }
4210
NeilBrown3d310eb2005-06-21 17:17:26 -07004211 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004212
NeilBrown802ba062006-12-13 00:34:13 -08004213 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004214 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004215 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004216 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004217
Shaohua Li620125f2012-10-11 13:49:05 +11004218 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4219 make_discard_request(mddev, bi);
4220 return;
4221 }
4222
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4224 last_sector = bi->bi_sector + (bi->bi_size>>9);
4225 bi->bi_next = NULL;
4226 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004227
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4229 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004230 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004231
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004232 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004233 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004234 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004235 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004236 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004237 * 64bit on a 32bit platform, and so it might be
4238 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004239 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004240 * the lock is dropped, so once we get a reference
4241 * to the stripe that we think it is, we will have
4242 * to check again.
4243 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004244 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004245 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004246 ? logical_sector < conf->reshape_progress
4247 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004248 previous = 1;
4249 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004250 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004251 ? logical_sector < conf->reshape_safe
4252 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004253 spin_unlock_irq(&conf->device_lock);
4254 schedule();
4255 goto retry;
4256 }
4257 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004258 spin_unlock_irq(&conf->device_lock);
4259 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004260
NeilBrown112bf892009-03-31 14:39:38 +11004261 new_sector = raid5_compute_sector(conf, logical_sector,
4262 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004263 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004264 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 (unsigned long long)new_sector,
4266 (unsigned long long)logical_sector);
4267
NeilBrownb5663ba2009-03-31 14:39:38 +11004268 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004269 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004271 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004272 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004273 * stripe, so we must do the range check again.
4274 * Expansion could still move past after this
4275 * test, but as we are holding a reference to
4276 * 'sh', we know that if that happens,
4277 * STRIPE_EXPANDING will get set and the expansion
4278 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004279 */
4280 int must_retry = 0;
4281 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004282 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004283 ? logical_sector >= conf->reshape_progress
4284 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004285 /* mismatch, need to try again */
4286 must_retry = 1;
4287 spin_unlock_irq(&conf->device_lock);
4288 if (must_retry) {
4289 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004290 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004291 goto retry;
4292 }
4293 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004294
Namhyung Kimffd96e32011-07-18 17:38:51 +10004295 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004296 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004297 logical_sector < mddev->suspend_hi) {
4298 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004299 /* As the suspend_* range is controlled by
4300 * userspace, we want an interruptible
4301 * wait.
4302 */
4303 flush_signals(current);
4304 prepare_to_wait(&conf->wait_for_overlap,
4305 &w, TASK_INTERRUPTIBLE);
4306 if (logical_sector >= mddev->suspend_lo &&
4307 logical_sector < mddev->suspend_hi)
4308 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004309 goto retry;
4310 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004311
4312 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004313 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004314 /* Stripe is busy expanding or
4315 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 * and wait a while
4317 */
NeilBrown482c0832011-04-18 18:25:42 +10004318 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 release_stripe(sh);
4320 schedule();
4321 goto retry;
4322 }
4323 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004324 set_bit(STRIPE_HANDLE, &sh->state);
4325 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004326 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004327 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4328 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004329 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 } else {
4331 /* cannot get stripe for read-ahead, just give-up */
4332 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4333 finish_wait(&conf->wait_for_overlap, &w);
4334 break;
4335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004337
Shaohua Lie7836bd62012-07-19 16:01:31 +10004338 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004339 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
NeilBrown16a53ec2006-06-26 00:27:38 -07004341 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004343
NeilBrowna9add5d2012-10-31 11:59:09 +11004344 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4345 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004346 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348}
4349
NeilBrownfd01b882011-10-11 16:47:53 +11004350static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004351
NeilBrownfd01b882011-10-11 16:47:53 +11004352static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353{
NeilBrown52c03292006-06-26 00:27:43 -07004354 /* reshaping is quite different to recovery/resync so it is
4355 * handled quite separately ... here.
4356 *
4357 * On each call to sync_request, we gather one chunk worth of
4358 * destination stripes and flag them as expanding.
4359 * Then we find all the source stripes and request reads.
4360 * As the reads complete, handle_stripe will copy the data
4361 * into the destination stripe and release that stripe.
4362 */
NeilBrownd1688a62011-10-11 16:49:52 +11004363 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004365 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004366 int raid_disks = conf->previous_raid_disks;
4367 int data_disks = raid_disks - conf->max_degraded;
4368 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004369 int i;
4370 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004371 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004372 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004373 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004374 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004375
NeilBrownfef9c612009-03-31 15:16:46 +11004376 if (sector_nr == 0) {
4377 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004378 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004379 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4380 sector_nr = raid5_size(mddev, 0, 0)
4381 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004382 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004383 conf->reshape_progress > 0)
4384 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004385 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004386 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004387 mddev->curr_resync_completed = sector_nr;
4388 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004389 *skipped = 1;
4390 return sector_nr;
4391 }
NeilBrown52c03292006-06-26 00:27:43 -07004392 }
4393
NeilBrown7a661382009-03-31 15:21:40 +11004394 /* We need to process a full chunk at a time.
4395 * If old and new chunk sizes differ, we need to process the
4396 * largest of these
4397 */
Andre Noll664e7c42009-06-18 08:45:27 +10004398 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4399 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004400 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004401 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004402
NeilBrownb5254dd2012-05-21 09:27:01 +10004403 /* We update the metadata at least every 10 seconds, or when
4404 * the data about to be copied would over-write the source of
4405 * the data at the front of the range. i.e. one new_stripe
4406 * along from reshape_progress new_maps to after where
4407 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004408 */
NeilBrownfef9c612009-03-31 15:16:46 +11004409 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004410 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004411 readpos = conf->reshape_progress;
4412 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004413 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004414 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004415 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004416 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004417 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004418 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004419 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004420 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004421 readpos -= min_t(sector_t, reshape_sectors, readpos);
4422 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004423 }
NeilBrown52c03292006-06-26 00:27:43 -07004424
NeilBrownb5254dd2012-05-21 09:27:01 +10004425 /* Having calculated the 'writepos' possibly use it
4426 * to set 'stripe_addr' which is where we will write to.
4427 */
4428 if (mddev->reshape_backwards) {
4429 BUG_ON(conf->reshape_progress == 0);
4430 stripe_addr = writepos;
4431 BUG_ON((mddev->dev_sectors &
4432 ~((sector_t)reshape_sectors - 1))
4433 - reshape_sectors - stripe_addr
4434 != sector_nr);
4435 } else {
4436 BUG_ON(writepos != sector_nr + reshape_sectors);
4437 stripe_addr = sector_nr;
4438 }
4439
NeilBrownc8f517c2009-03-31 15:28:40 +11004440 /* 'writepos' is the most advanced device address we might write.
4441 * 'readpos' is the least advanced device address we might read.
4442 * 'safepos' is the least address recorded in the metadata as having
4443 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004444 * If there is a min_offset_diff, these are adjusted either by
4445 * increasing the safepos/readpos if diff is negative, or
4446 * increasing writepos if diff is positive.
4447 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004448 * ensure safety in the face of a crash - that must be done by userspace
4449 * making a backup of the data. So in that case there is no particular
4450 * rush to update metadata.
4451 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4452 * update the metadata to advance 'safepos' to match 'readpos' so that
4453 * we can be safe in the event of a crash.
4454 * So we insist on updating metadata if safepos is behind writepos and
4455 * readpos is beyond writepos.
4456 * In any case, update the metadata every 10 seconds.
4457 * Maybe that number should be configurable, but I'm not sure it is
4458 * worth it.... maybe it could be a multiple of safemode_delay???
4459 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004460 if (conf->min_offset_diff < 0) {
4461 safepos += -conf->min_offset_diff;
4462 readpos += -conf->min_offset_diff;
4463 } else
4464 writepos += conf->min_offset_diff;
4465
NeilBrown2c810cd2012-05-21 09:27:00 +10004466 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004467 ? (safepos > writepos && readpos < writepos)
4468 : (safepos < writepos && readpos > writepos)) ||
4469 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004470 /* Cannot proceed until we've updated the superblock... */
4471 wait_event(conf->wait_for_overlap,
4472 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004473 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004474 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004475 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004476 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004477 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004478 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004479 kthread_should_stop());
4480 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004481 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004482 spin_unlock_irq(&conf->device_lock);
4483 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004484 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004485 }
4486
NeilBrownab69ae12009-03-31 15:26:47 +11004487 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004488 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004489 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004490 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004491 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004492 set_bit(STRIPE_EXPANDING, &sh->state);
4493 atomic_inc(&conf->reshape_stripes);
4494 /* If any of this stripe is beyond the end of the old
4495 * array, then we need to zero those blocks
4496 */
4497 for (j=sh->disks; j--;) {
4498 sector_t s;
4499 if (j == sh->pd_idx)
4500 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004501 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004502 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004503 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004504 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004505 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004506 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004507 continue;
4508 }
4509 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4510 set_bit(R5_Expanded, &sh->dev[j].flags);
4511 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4512 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004513 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004514 set_bit(STRIPE_EXPAND_READY, &sh->state);
4515 set_bit(STRIPE_HANDLE, &sh->state);
4516 }
NeilBrownab69ae12009-03-31 15:26:47 +11004517 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004518 }
4519 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004520 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004521 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004522 else
NeilBrown7a661382009-03-31 15:21:40 +11004523 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004524 spin_unlock_irq(&conf->device_lock);
4525 /* Ok, those stripe are ready. We can start scheduling
4526 * reads on the source stripes.
4527 * The source stripes are determined by mapping the first and last
4528 * block on the destination stripes.
4529 */
NeilBrown52c03292006-06-26 00:27:43 -07004530 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004531 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004532 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004533 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004534 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004535 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004536 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004537 if (last_sector >= mddev->dev_sectors)
4538 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004539 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004540 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004541 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4542 set_bit(STRIPE_HANDLE, &sh->state);
4543 release_stripe(sh);
4544 first_sector += STRIPE_SECTORS;
4545 }
NeilBrownab69ae12009-03-31 15:26:47 +11004546 /* Now that the sources are clearly marked, we can release
4547 * the destination stripes
4548 */
4549 while (!list_empty(&stripes)) {
4550 sh = list_entry(stripes.next, struct stripe_head, lru);
4551 list_del_init(&sh->lru);
4552 release_stripe(sh);
4553 }
NeilBrownc6207272008-02-06 01:39:52 -08004554 /* If this takes us to the resync_max point where we have to pause,
4555 * then we need to write out the superblock.
4556 */
NeilBrown7a661382009-03-31 15:21:40 +11004557 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004558 if ((sector_nr - mddev->curr_resync_completed) * 2
4559 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004560 /* Cannot proceed until we've updated the superblock... */
4561 wait_event(conf->wait_for_overlap,
4562 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004563 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004564 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004565 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004566 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4567 md_wakeup_thread(mddev->thread);
4568 wait_event(mddev->sb_wait,
4569 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4570 || kthread_should_stop());
4571 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004572 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004573 spin_unlock_irq(&conf->device_lock);
4574 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004575 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004576 }
NeilBrown7a661382009-03-31 15:21:40 +11004577 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004578}
4579
4580/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004581static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004582{
NeilBrownd1688a62011-10-11 16:49:52 +11004583 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004584 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004585 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004586 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004587 int still_degraded = 0;
4588 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
NeilBrown72626682005-09-09 16:23:54 -07004590 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004592
NeilBrown29269552006-03-27 01:18:10 -08004593 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4594 end_reshape(conf);
4595 return 0;
4596 }
NeilBrown72626682005-09-09 16:23:54 -07004597
4598 if (mddev->curr_resync < max_sector) /* aborted */
4599 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4600 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004601 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004602 conf->fullsync = 0;
4603 bitmap_close_sync(mddev->bitmap);
4604
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 return 0;
4606 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004607
NeilBrown64bd6602009-08-03 10:59:58 +10004608 /* Allow raid5_quiesce to complete */
4609 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4610
NeilBrown52c03292006-06-26 00:27:43 -07004611 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4612 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004613
NeilBrownc6207272008-02-06 01:39:52 -08004614 /* No need to check resync_max as we never do more than one
4615 * stripe, and as resync_max will always be on a chunk boundary,
4616 * if the check in md_do_sync didn't fire, there is no chance
4617 * of overstepping resync_max here
4618 */
4619
NeilBrown16a53ec2006-06-26 00:27:38 -07004620 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 * to resync, then assert that we are finished, because there is
4622 * nothing we can do.
4623 */
NeilBrown3285edf2006-06-26 00:27:55 -07004624 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004625 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004626 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004627 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 return rv;
4629 }
NeilBrown72626682005-09-09 16:23:54 -07004630 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004631 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004632 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4633 /* we can skip this block, and probably more */
4634 sync_blocks /= STRIPE_SECTORS;
4635 *skipped = 1;
4636 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638
NeilBrownb47490c2008-02-06 01:39:50 -08004639 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4640
NeilBrowna8c906c2009-06-09 14:39:59 +10004641 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004643 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004645 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004647 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004649 /* Need to check if array will still be degraded after recovery/resync
4650 * We don't need to check the 'failed' flag as when that gets set,
4651 * recovery aborts.
4652 */
NeilBrownf001a702009-06-09 14:30:31 +10004653 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004654 if (conf->disks[i].rdev == NULL)
4655 still_degraded = 1;
4656
4657 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4658
NeilBrown83206d62011-07-26 11:19:49 +10004659 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
NeilBrown14425772009-10-16 15:55:25 +11004661 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 release_stripe(sh);
4663
4664 return STRIPE_SECTORS;
4665}
4666
NeilBrownd1688a62011-10-11 16:49:52 +11004667static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004668{
4669 /* We may not be able to submit a whole bio at once as there
4670 * may not be enough stripe_heads available.
4671 * We cannot pre-allocate enough stripe_heads as we may need
4672 * more than exist in the cache (if we allow ever large chunks).
4673 * So we do one stripe head at a time and record in
4674 * ->bi_hw_segments how many have been done.
4675 *
4676 * We *know* that this entire raid_bio is in one chunk, so
4677 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4678 */
4679 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004680 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004681 sector_t sector, logical_sector, last_sector;
4682 int scnt = 0;
4683 int remaining;
4684 int handled = 0;
4685
4686 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004687 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004688 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004689 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4690
4691 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004692 logical_sector += STRIPE_SECTORS,
4693 sector += STRIPE_SECTORS,
4694 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004695
Shaohua Lie7836bd62012-07-19 16:01:31 +10004696 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004697 /* already done this stripe */
4698 continue;
4699
NeilBrowna8c906c2009-06-09 14:39:59 +10004700 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004701
4702 if (!sh) {
4703 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004704 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004705 conf->retry_read_aligned = raid_bio;
4706 return handled;
4707 }
4708
Neil Brown387bb172007-02-08 14:20:29 -08004709 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4710 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004711 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004712 conf->retry_read_aligned = raid_bio;
4713 return handled;
4714 }
4715
majianpeng3f9e7c12012-07-31 10:04:21 +10004716 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004717 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004718 release_stripe(sh);
4719 handled++;
4720 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004721 remaining = raid5_dec_bi_active_stripes(raid_bio);
NeilBrowna9add5d2012-10-31 11:59:09 +11004722 if (remaining == 0) {
4723 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
4724 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004725 bio_endio(raid_bio, 0);
NeilBrowna9add5d2012-10-31 11:59:09 +11004726 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004727 if (atomic_dec_and_test(&conf->active_aligned_reads))
4728 wake_up(&conf->wait_for_stripe);
4729 return handled;
4730}
4731
Shaohua Li46a06402012-08-02 08:33:15 +10004732#define MAX_STRIPE_BATCH 8
4733static int handle_active_stripes(struct r5conf *conf)
4734{
4735 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4736 int i, batch_size = 0;
4737
4738 while (batch_size < MAX_STRIPE_BATCH &&
4739 (sh = __get_priority_stripe(conf)) != NULL)
4740 batch[batch_size++] = sh;
4741
4742 if (batch_size == 0)
4743 return batch_size;
4744 spin_unlock_irq(&conf->device_lock);
4745
4746 for (i = 0; i < batch_size; i++)
4747 handle_stripe(batch[i]);
4748
4749 cond_resched();
4750
4751 spin_lock_irq(&conf->device_lock);
4752 for (i = 0; i < batch_size; i++)
4753 __release_stripe(conf, batch[i]);
4754 return batch_size;
4755}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004756
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757/*
4758 * This is our raid5 kernel thread.
4759 *
4760 * We scan the hash table for stripes which can be handled now.
4761 * During the scan, completed stripes are saved for us by the interrupt
4762 * handler, so that they will not have to wait for our next wakeup.
4763 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004764static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765{
Shaohua Li4ed87312012-10-11 13:34:00 +11004766 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004767 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004769 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Dan Williams45b42332007-07-09 11:56:43 -07004771 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772
4773 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004775 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 handled = 0;
4777 spin_lock_irq(&conf->device_lock);
4778 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004779 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004780 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
NeilBrown0021b7b2012-07-31 09:08:14 +02004782 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004783 !list_empty(&conf->bitmap_list)) {
4784 /* Now is a good time to flush some bitmap updates */
4785 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004786 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004787 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004788 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004789 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004790 activate_bit_delay(conf);
4791 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004792 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004793
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004794 while ((bio = remove_bio_from_retry(conf))) {
4795 int ok;
4796 spin_unlock_irq(&conf->device_lock);
4797 ok = retry_aligned_read(conf, bio);
4798 spin_lock_irq(&conf->device_lock);
4799 if (!ok)
4800 break;
4801 handled++;
4802 }
4803
Shaohua Li46a06402012-08-02 08:33:15 +10004804 batch_size = handle_active_stripes(conf);
4805 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004807 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
Shaohua Li46a06402012-08-02 08:33:15 +10004809 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4810 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004811 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004812 spin_lock_irq(&conf->device_lock);
4813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 }
Dan Williams45b42332007-07-09 11:56:43 -07004815 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
4817 spin_unlock_irq(&conf->device_lock);
4818
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004819 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004820 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821
Dan Williams45b42332007-07-09 11:56:43 -07004822 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823}
4824
NeilBrown3f294f42005-11-08 21:39:25 -08004825static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004826raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004827{
NeilBrownd1688a62011-10-11 16:49:52 +11004828 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004829 if (conf)
4830 return sprintf(page, "%d\n", conf->max_nr_stripes);
4831 else
4832 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004833}
4834
NeilBrownc41d4ac2010-06-01 19:37:24 +10004835int
NeilBrownfd01b882011-10-11 16:47:53 +11004836raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004837{
NeilBrownd1688a62011-10-11 16:49:52 +11004838 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004839 int err;
4840
4841 if (size <= 16 || size > 32768)
4842 return -EINVAL;
4843 while (size < conf->max_nr_stripes) {
4844 if (drop_one_stripe(conf))
4845 conf->max_nr_stripes--;
4846 else
4847 break;
4848 }
4849 err = md_allow_write(mddev);
4850 if (err)
4851 return err;
4852 while (size > conf->max_nr_stripes) {
4853 if (grow_one_stripe(conf))
4854 conf->max_nr_stripes++;
4855 else break;
4856 }
4857 return 0;
4858}
4859EXPORT_SYMBOL(raid5_set_cache_size);
4860
NeilBrown3f294f42005-11-08 21:39:25 -08004861static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004862raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004863{
NeilBrownd1688a62011-10-11 16:49:52 +11004864 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004865 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004866 int err;
4867
NeilBrown3f294f42005-11-08 21:39:25 -08004868 if (len >= PAGE_SIZE)
4869 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004870 if (!conf)
4871 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004872
Dan Williams4ef197d82008-04-28 02:15:54 -07004873 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004874 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004875 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004876 if (err)
4877 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004878 return len;
4879}
NeilBrown007583c2005-11-08 21:39:30 -08004880
NeilBrown96de1e62005-11-08 21:39:39 -08004881static struct md_sysfs_entry
4882raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4883 raid5_show_stripe_cache_size,
4884 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004885
4886static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004887raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004888{
NeilBrownd1688a62011-10-11 16:49:52 +11004889 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004890 if (conf)
4891 return sprintf(page, "%d\n", conf->bypass_threshold);
4892 else
4893 return 0;
4894}
4895
4896static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004897raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004898{
NeilBrownd1688a62011-10-11 16:49:52 +11004899 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004900 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004901 if (len >= PAGE_SIZE)
4902 return -EINVAL;
4903 if (!conf)
4904 return -ENODEV;
4905
Dan Williams4ef197d82008-04-28 02:15:54 -07004906 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004907 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004908 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004909 return -EINVAL;
4910 conf->bypass_threshold = new;
4911 return len;
4912}
4913
4914static struct md_sysfs_entry
4915raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4916 S_IRUGO | S_IWUSR,
4917 raid5_show_preread_threshold,
4918 raid5_store_preread_threshold);
4919
4920static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004921stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004922{
NeilBrownd1688a62011-10-11 16:49:52 +11004923 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004924 if (conf)
4925 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4926 else
4927 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004928}
4929
NeilBrown96de1e62005-11-08 21:39:39 -08004930static struct md_sysfs_entry
4931raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004932
NeilBrown007583c2005-11-08 21:39:30 -08004933static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004934 &raid5_stripecache_size.attr,
4935 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004936 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004937 NULL,
4938};
NeilBrown007583c2005-11-08 21:39:30 -08004939static struct attribute_group raid5_attrs_group = {
4940 .name = NULL,
4941 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004942};
4943
Dan Williams80c3a6c2009-03-17 18:10:40 -07004944static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004945raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004946{
NeilBrownd1688a62011-10-11 16:49:52 +11004947 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004948
4949 if (!sectors)
4950 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004951 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004952 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004953 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004954
Andre Noll9d8f0362009-06-18 08:45:01 +10004955 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004956 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004957 return sectors * (raid_disks - conf->max_degraded);
4958}
4959
NeilBrownd1688a62011-10-11 16:49:52 +11004960static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004961{
4962 struct raid5_percpu *percpu;
4963 unsigned long cpu;
4964
4965 if (!conf->percpu)
4966 return;
4967
4968 get_online_cpus();
4969 for_each_possible_cpu(cpu) {
4970 percpu = per_cpu_ptr(conf->percpu, cpu);
4971 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004972 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004973 }
4974#ifdef CONFIG_HOTPLUG_CPU
4975 unregister_cpu_notifier(&conf->cpu_notify);
4976#endif
4977 put_online_cpus();
4978
4979 free_percpu(conf->percpu);
4980}
4981
NeilBrownd1688a62011-10-11 16:49:52 +11004982static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004983{
4984 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004985 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004986 kfree(conf->disks);
4987 kfree(conf->stripe_hashtbl);
4988 kfree(conf);
4989}
4990
Dan Williams36d1c642009-07-14 11:48:22 -07004991#ifdef CONFIG_HOTPLUG_CPU
4992static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4993 void *hcpu)
4994{
NeilBrownd1688a62011-10-11 16:49:52 +11004995 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004996 long cpu = (long)hcpu;
4997 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4998
4999 switch (action) {
5000 case CPU_UP_PREPARE:
5001 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07005002 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07005003 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005004 if (!percpu->scribble)
5005 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5006
5007 if (!percpu->scribble ||
5008 (conf->level == 6 && !percpu->spare_page)) {
5009 safe_put_page(percpu->spare_page);
5010 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005011 pr_err("%s: failed memory allocation for cpu%ld\n",
5012 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005013 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005014 }
5015 break;
5016 case CPU_DEAD:
5017 case CPU_DEAD_FROZEN:
5018 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005019 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005020 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005021 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005022 break;
5023 default:
5024 break;
5025 }
5026 return NOTIFY_OK;
5027}
5028#endif
5029
NeilBrownd1688a62011-10-11 16:49:52 +11005030static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005031{
5032 unsigned long cpu;
5033 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005034 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005035 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005036 int err;
5037
Dan Williams36d1c642009-07-14 11:48:22 -07005038 allcpus = alloc_percpu(struct raid5_percpu);
5039 if (!allcpus)
5040 return -ENOMEM;
5041 conf->percpu = allcpus;
5042
5043 get_online_cpus();
5044 err = 0;
5045 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005046 if (conf->level == 6) {
5047 spare_page = alloc_page(GFP_KERNEL);
5048 if (!spare_page) {
5049 err = -ENOMEM;
5050 break;
5051 }
5052 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5053 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005054 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005055 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005056 err = -ENOMEM;
5057 break;
5058 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005059 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005060 }
5061#ifdef CONFIG_HOTPLUG_CPU
5062 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5063 conf->cpu_notify.priority = 0;
5064 if (err == 0)
5065 err = register_cpu_notifier(&conf->cpu_notify);
5066#endif
5067 put_online_cpus();
5068
5069 return err;
5070}
5071
NeilBrownd1688a62011-10-11 16:49:52 +11005072static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073{
NeilBrownd1688a62011-10-11 16:49:52 +11005074 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005075 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005076 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005078 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
NeilBrown91adb562009-03-31 14:39:39 +11005080 if (mddev->new_level != 5
5081 && mddev->new_level != 4
5082 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005083 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005084 mdname(mddev), mddev->new_level);
5085 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 }
NeilBrown91adb562009-03-31 14:39:39 +11005087 if ((mddev->new_level == 5
5088 && !algorithm_valid_raid5(mddev->new_layout)) ||
5089 (mddev->new_level == 6
5090 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005091 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005092 mdname(mddev), mddev->new_layout);
5093 return ERR_PTR(-EIO);
5094 }
5095 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005096 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005097 mdname(mddev), mddev->raid_disks);
5098 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
Andre Noll664e7c42009-06-18 08:45:27 +10005101 if (!mddev->new_chunk_sectors ||
5102 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5103 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005104 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5105 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005106 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005107 }
5108
NeilBrownd1688a62011-10-11 16:49:52 +11005109 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005110 if (conf == NULL)
5111 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005112 spin_lock_init(&conf->device_lock);
5113 init_waitqueue_head(&conf->wait_for_stripe);
5114 init_waitqueue_head(&conf->wait_for_overlap);
5115 INIT_LIST_HEAD(&conf->handle_list);
5116 INIT_LIST_HEAD(&conf->hold_list);
5117 INIT_LIST_HEAD(&conf->delayed_list);
5118 INIT_LIST_HEAD(&conf->bitmap_list);
5119 INIT_LIST_HEAD(&conf->inactive_list);
5120 atomic_set(&conf->active_stripes, 0);
5121 atomic_set(&conf->preread_active_stripes, 0);
5122 atomic_set(&conf->active_aligned_reads, 0);
5123 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005124 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005125
5126 conf->raid_disks = mddev->raid_disks;
5127 if (mddev->reshape_position == MaxSector)
5128 conf->previous_raid_disks = mddev->raid_disks;
5129 else
5130 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005131 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5132 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005133
NeilBrown5e5e3e72009-10-16 16:35:30 +11005134 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005135 GFP_KERNEL);
5136 if (!conf->disks)
5137 goto abort;
5138
5139 conf->mddev = mddev;
5140
5141 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5142 goto abort;
5143
Dan Williams36d1c642009-07-14 11:48:22 -07005144 conf->level = mddev->new_level;
5145 if (raid5_alloc_percpu(conf) != 0)
5146 goto abort;
5147
NeilBrown0c55e022010-05-03 14:09:02 +10005148 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005149
NeilBrowndafb20f2012-03-19 12:46:39 +11005150 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005151 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005152 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005153 || raid_disk < 0)
5154 continue;
5155 disk = conf->disks + raid_disk;
5156
NeilBrown17045f52011-12-23 10:17:53 +11005157 if (test_bit(Replacement, &rdev->flags)) {
5158 if (disk->replacement)
5159 goto abort;
5160 disk->replacement = rdev;
5161 } else {
5162 if (disk->rdev)
5163 goto abort;
5164 disk->rdev = rdev;
5165 }
NeilBrown91adb562009-03-31 14:39:39 +11005166
5167 if (test_bit(In_sync, &rdev->flags)) {
5168 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005169 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5170 " disk %d\n",
5171 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005172 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005173 /* Cannot rely on bitmap to complete recovery */
5174 conf->fullsync = 1;
5175 }
5176
Andre Noll09c9e5f2009-06-18 08:45:55 +10005177 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005178 conf->level = mddev->new_level;
5179 if (conf->level == 6)
5180 conf->max_degraded = 2;
5181 else
5182 conf->max_degraded = 1;
5183 conf->algorithm = mddev->new_layout;
5184 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005185 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005186 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005187 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005188 conf->prev_algo = mddev->layout;
5189 }
NeilBrown91adb562009-03-31 14:39:39 +11005190
5191 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005192 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005193 if (grow_stripes(conf, conf->max_nr_stripes)) {
5194 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005195 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5196 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005197 goto abort;
5198 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005199 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5200 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005201
NeilBrown02326052012-07-03 15:56:52 +10005202 sprintf(pers_name, "raid%d", mddev->new_level);
5203 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005204 if (!conf->thread) {
5205 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005206 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005207 mdname(mddev));
5208 goto abort;
5209 }
5210
5211 return conf;
5212
5213 abort:
5214 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005215 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005216 return ERR_PTR(-EIO);
5217 } else
5218 return ERR_PTR(-ENOMEM);
5219}
5220
NeilBrownc148ffd2009-11-13 17:47:00 +11005221
5222static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5223{
5224 switch (algo) {
5225 case ALGORITHM_PARITY_0:
5226 if (raid_disk < max_degraded)
5227 return 1;
5228 break;
5229 case ALGORITHM_PARITY_N:
5230 if (raid_disk >= raid_disks - max_degraded)
5231 return 1;
5232 break;
5233 case ALGORITHM_PARITY_0_6:
5234 if (raid_disk == 0 ||
5235 raid_disk == raid_disks - 1)
5236 return 1;
5237 break;
5238 case ALGORITHM_LEFT_ASYMMETRIC_6:
5239 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5240 case ALGORITHM_LEFT_SYMMETRIC_6:
5241 case ALGORITHM_RIGHT_SYMMETRIC_6:
5242 if (raid_disk == raid_disks - 1)
5243 return 1;
5244 }
5245 return 0;
5246}
5247
NeilBrownfd01b882011-10-11 16:47:53 +11005248static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005249{
NeilBrownd1688a62011-10-11 16:49:52 +11005250 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005251 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005252 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005253 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005254 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005255 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005256 long long min_offset_diff = 0;
5257 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005258
Andre Noll8c6ac862009-06-18 08:48:06 +10005259 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005260 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005261 " -- starting background reconstruction\n",
5262 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005263
5264 rdev_for_each(rdev, mddev) {
5265 long long diff;
5266 if (rdev->raid_disk < 0)
5267 continue;
5268 diff = (rdev->new_data_offset - rdev->data_offset);
5269 if (first) {
5270 min_offset_diff = diff;
5271 first = 0;
5272 } else if (mddev->reshape_backwards &&
5273 diff < min_offset_diff)
5274 min_offset_diff = diff;
5275 else if (!mddev->reshape_backwards &&
5276 diff > min_offset_diff)
5277 min_offset_diff = diff;
5278 }
5279
NeilBrownf6705572006-03-27 01:18:11 -08005280 if (mddev->reshape_position != MaxSector) {
5281 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005282 * Difficulties arise if the stripe we would write to
5283 * next is at or after the stripe we would read from next.
5284 * For a reshape that changes the number of devices, this
5285 * is only possible for a very short time, and mdadm makes
5286 * sure that time appears to have past before assembling
5287 * the array. So we fail if that time hasn't passed.
5288 * For a reshape that keeps the number of devices the same
5289 * mdadm must be monitoring the reshape can keeping the
5290 * critical areas read-only and backed up. It will start
5291 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005292 */
5293 sector_t here_new, here_old;
5294 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005295 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005296
NeilBrown88ce4932009-03-31 15:24:23 +11005297 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005298 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005299 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005300 mdname(mddev));
5301 return -EINVAL;
5302 }
NeilBrownf6705572006-03-27 01:18:11 -08005303 old_disks = mddev->raid_disks - mddev->delta_disks;
5304 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005305 * further up in new geometry must map after here in old
5306 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005307 */
5308 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005309 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005310 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005311 printk(KERN_ERR "md/raid:%s: reshape_position not "
5312 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005313 return -EINVAL;
5314 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005315 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005316 /* here_new is the stripe we will write to */
5317 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005318 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005319 (old_disks-max_degraded));
5320 /* here_old is the first stripe that we might need to read
5321 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005322 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005323 if ((here_new * mddev->new_chunk_sectors !=
5324 here_old * mddev->chunk_sectors)) {
5325 printk(KERN_ERR "md/raid:%s: reshape position is"
5326 " confused - aborting\n", mdname(mddev));
5327 return -EINVAL;
5328 }
NeilBrown67ac6012009-08-13 10:06:24 +10005329 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005330 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005331 * and taking constant backups.
5332 * mdadm always starts a situation like this in
5333 * readonly mode so it can take control before
5334 * allowing any writes. So just check for that.
5335 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005336 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5337 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5338 /* not really in-place - so OK */;
5339 else if (mddev->ro == 0) {
5340 printk(KERN_ERR "md/raid:%s: in-place reshape "
5341 "must be started in read-only mode "
5342 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005343 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005344 return -EINVAL;
5345 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005346 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005347 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005348 here_old * mddev->chunk_sectors)
5349 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005350 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005351 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005352 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5353 "auto-recovery - aborting.\n",
5354 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005355 return -EINVAL;
5356 }
NeilBrown0c55e022010-05-03 14:09:02 +10005357 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5358 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005359 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005360 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005361 BUG_ON(mddev->level != mddev->new_level);
5362 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005363 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005364 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005365 }
5366
NeilBrown245f46c2009-03-31 14:39:39 +11005367 if (mddev->private == NULL)
5368 conf = setup_conf(mddev);
5369 else
5370 conf = mddev->private;
5371
NeilBrown91adb562009-03-31 14:39:39 +11005372 if (IS_ERR(conf))
5373 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005374
NeilBrownb5254dd2012-05-21 09:27:01 +10005375 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005376 mddev->thread = conf->thread;
5377 conf->thread = NULL;
5378 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379
NeilBrown17045f52011-12-23 10:17:53 +11005380 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5381 i++) {
5382 rdev = conf->disks[i].rdev;
5383 if (!rdev && conf->disks[i].replacement) {
5384 /* The replacement is all we have yet */
5385 rdev = conf->disks[i].replacement;
5386 conf->disks[i].replacement = NULL;
5387 clear_bit(Replacement, &rdev->flags);
5388 conf->disks[i].rdev = rdev;
5389 }
5390 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005391 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005392 if (conf->disks[i].replacement &&
5393 conf->reshape_progress != MaxSector) {
5394 /* replacements and reshape simply do not mix. */
5395 printk(KERN_ERR "md: cannot handle concurrent "
5396 "replacement and reshape.\n");
5397 goto abort;
5398 }
NeilBrown2f115882010-06-17 17:41:03 +10005399 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005400 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005401 continue;
5402 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005403 /* This disc is not fully in-sync. However if it
5404 * just stored parity (beyond the recovery_offset),
5405 * when we don't need to be concerned about the
5406 * array being dirty.
5407 * When reshape goes 'backwards', we never have
5408 * partially completed devices, so we only need
5409 * to worry about reshape going forwards.
5410 */
5411 /* Hack because v0.91 doesn't store recovery_offset properly. */
5412 if (mddev->major_version == 0 &&
5413 mddev->minor_version > 90)
5414 rdev->recovery_offset = reshape_offset;
5415
NeilBrownc148ffd2009-11-13 17:47:00 +11005416 if (rdev->recovery_offset < reshape_offset) {
5417 /* We need to check old and new layout */
5418 if (!only_parity(rdev->raid_disk,
5419 conf->algorithm,
5420 conf->raid_disks,
5421 conf->max_degraded))
5422 continue;
5423 }
5424 if (!only_parity(rdev->raid_disk,
5425 conf->prev_algo,
5426 conf->previous_raid_disks,
5427 conf->max_degraded))
5428 continue;
5429 dirty_parity_disks++;
5430 }
NeilBrown91adb562009-03-31 14:39:39 +11005431
NeilBrown17045f52011-12-23 10:17:53 +11005432 /*
5433 * 0 for a fully functional array, 1 or 2 for a degraded array.
5434 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005435 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436
NeilBrown674806d2010-06-16 17:17:53 +10005437 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005438 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005440 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 goto abort;
5442 }
5443
NeilBrown91adb562009-03-31 14:39:39 +11005444 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005445 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005446 mddev->resync_max_sectors = mddev->dev_sectors;
5447
NeilBrownc148ffd2009-11-13 17:47:00 +11005448 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005450 if (mddev->ok_start_degraded)
5451 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005452 "md/raid:%s: starting dirty degraded array"
5453 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005454 mdname(mddev));
5455 else {
5456 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005457 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005458 mdname(mddev));
5459 goto abort;
5460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 }
5462
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005464 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5465 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005466 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5467 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 else
NeilBrown0c55e022010-05-03 14:09:02 +10005469 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5470 " out of %d devices, algorithm %d\n",
5471 mdname(mddev), conf->level,
5472 mddev->raid_disks - mddev->degraded,
5473 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
5475 print_raid5_conf(conf);
5476
NeilBrownfef9c612009-03-31 15:16:46 +11005477 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005478 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005479 atomic_set(&conf->reshape_stripes, 0);
5480 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5481 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5482 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5483 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5484 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005485 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005486 }
5487
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488
5489 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005490 if (mddev->to_remove == &raid5_attrs_group)
5491 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005492 else if (mddev->kobj.sd &&
5493 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005494 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005495 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005496 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005497 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5498
5499 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005500 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005501 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005502 /* read-ahead size must cover two whole stripes, which
5503 * is 2 * (datadisks) * chunksize where 'n' is the
5504 * number of raid devices
5505 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5507 int stripe = data_disks *
5508 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5509 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5510 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005511
5512 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005513
5514 mddev->queue->backing_dev_info.congested_data = mddev;
5515 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005516
5517 chunk_size = mddev->chunk_sectors << 9;
5518 blk_queue_io_min(mddev->queue, chunk_size);
5519 blk_queue_io_opt(mddev->queue, chunk_size *
5520 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005521 /*
5522 * We can only discard a whole stripe. It doesn't make sense to
5523 * discard data disk but write parity disk
5524 */
5525 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11005526 /* Round up to power of 2, as discard handling
5527 * currently assumes that */
5528 while ((stripe-1) & stripe)
5529 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11005530 mddev->queue->limits.discard_alignment = stripe;
5531 mddev->queue->limits.discard_granularity = stripe;
5532 /*
5533 * unaligned part of discard request will be ignored, so can't
5534 * guarantee discard_zerors_data
5535 */
5536 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005537
NeilBrown05616be2012-05-21 09:27:00 +10005538 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005539 disk_stack_limits(mddev->gendisk, rdev->bdev,
5540 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005541 disk_stack_limits(mddev->gendisk, rdev->bdev,
5542 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005543 /*
5544 * discard_zeroes_data is required, otherwise data
5545 * could be lost. Consider a scenario: discard a stripe
5546 * (the stripe could be inconsistent if
5547 * discard_zeroes_data is 0); write one disk of the
5548 * stripe (the stripe could be inconsistent again
5549 * depending on which disks are used to calculate
5550 * parity); the disk is broken; The stripe data of this
5551 * disk is lost.
5552 */
5553 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5554 !bdev_get_queue(rdev->bdev)->
5555 limits.discard_zeroes_data)
5556 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10005557 }
Shaohua Li620125f2012-10-11 13:49:05 +11005558
5559 if (discard_supported &&
5560 mddev->queue->limits.max_discard_sectors >= stripe &&
5561 mddev->queue->limits.discard_granularity >= stripe)
5562 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5563 mddev->queue);
5564 else
5565 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5566 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 }
5568
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 return 0;
5570abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005571 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005572 print_raid5_conf(conf);
5573 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005575 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 return -EIO;
5577}
5578
NeilBrownfd01b882011-10-11 16:47:53 +11005579static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580{
NeilBrownd1688a62011-10-11 16:49:52 +11005581 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
NeilBrown01f96c02011-09-21 15:30:20 +10005583 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005584 if (mddev->queue)
5585 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005586 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005587 mddev->private = NULL;
5588 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 return 0;
5590}
5591
NeilBrownfd01b882011-10-11 16:47:53 +11005592static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593{
NeilBrownd1688a62011-10-11 16:49:52 +11005594 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 int i;
5596
Andre Noll9d8f0362009-06-18 08:45:01 +10005597 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5598 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005599 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 for (i = 0; i < conf->raid_disks; i++)
5601 seq_printf (seq, "%s",
5602 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005603 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605}
5606
NeilBrownd1688a62011-10-11 16:49:52 +11005607static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608{
5609 int i;
5610 struct disk_info *tmp;
5611
NeilBrown0c55e022010-05-03 14:09:02 +10005612 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 if (!conf) {
5614 printk("(conf==NULL)\n");
5615 return;
5616 }
NeilBrown0c55e022010-05-03 14:09:02 +10005617 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5618 conf->raid_disks,
5619 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620
5621 for (i = 0; i < conf->raid_disks; i++) {
5622 char b[BDEVNAME_SIZE];
5623 tmp = conf->disks + i;
5624 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005625 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5626 i, !test_bit(Faulty, &tmp->rdev->flags),
5627 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 }
5629}
5630
NeilBrownfd01b882011-10-11 16:47:53 +11005631static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632{
5633 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005634 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005636 int count = 0;
5637 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638
5639 for (i = 0; i < conf->raid_disks; i++) {
5640 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005641 if (tmp->replacement
5642 && tmp->replacement->recovery_offset == MaxSector
5643 && !test_bit(Faulty, &tmp->replacement->flags)
5644 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5645 /* Replacement has just become active. */
5646 if (!tmp->rdev
5647 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5648 count++;
5649 if (tmp->rdev) {
5650 /* Replaced device not technically faulty,
5651 * but we need to be sure it gets removed
5652 * and never re-added.
5653 */
5654 set_bit(Faulty, &tmp->rdev->flags);
5655 sysfs_notify_dirent_safe(
5656 tmp->rdev->sysfs_state);
5657 }
5658 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5659 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005660 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005661 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005662 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005663 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005664 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 }
5666 }
NeilBrown6b965622010-08-18 11:56:59 +10005667 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005668 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005669 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005671 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672}
5673
NeilBrownb8321b62011-12-23 10:17:51 +11005674static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675{
NeilBrownd1688a62011-10-11 16:49:52 +11005676 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005678 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005679 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 struct disk_info *p = conf->disks + number;
5681
5682 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005683 if (rdev == p->rdev)
5684 rdevp = &p->rdev;
5685 else if (rdev == p->replacement)
5686 rdevp = &p->replacement;
5687 else
5688 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005689
NeilBrown657e3e42011-12-23 10:17:52 +11005690 if (number >= conf->raid_disks &&
5691 conf->reshape_progress == MaxSector)
5692 clear_bit(In_sync, &rdev->flags);
5693
5694 if (test_bit(In_sync, &rdev->flags) ||
5695 atomic_read(&rdev->nr_pending)) {
5696 err = -EBUSY;
5697 goto abort;
5698 }
5699 /* Only remove non-faulty devices if recovery
5700 * isn't possible.
5701 */
5702 if (!test_bit(Faulty, &rdev->flags) &&
5703 mddev->recovery_disabled != conf->recovery_disabled &&
5704 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005705 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005706 number < conf->raid_disks) {
5707 err = -EBUSY;
5708 goto abort;
5709 }
5710 *rdevp = NULL;
5711 synchronize_rcu();
5712 if (atomic_read(&rdev->nr_pending)) {
5713 /* lost the race, try later */
5714 err = -EBUSY;
5715 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005716 } else if (p->replacement) {
5717 /* We must have just cleared 'rdev' */
5718 p->rdev = p->replacement;
5719 clear_bit(Replacement, &p->replacement->flags);
5720 smp_mb(); /* Make sure other CPUs may see both as identical
5721 * but will never see neither - if they are careful
5722 */
5723 p->replacement = NULL;
5724 clear_bit(WantReplacement, &rdev->flags);
5725 } else
5726 /* We might have just removed the Replacement as faulty-
5727 * clear the bit just in case
5728 */
5729 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730abort:
5731
5732 print_raid5_conf(conf);
5733 return err;
5734}
5735
NeilBrownfd01b882011-10-11 16:47:53 +11005736static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737{
NeilBrownd1688a62011-10-11 16:49:52 +11005738 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005739 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 int disk;
5741 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005742 int first = 0;
5743 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744
NeilBrown7f0da592011-07-28 11:39:22 +10005745 if (mddev->recovery_disabled == conf->recovery_disabled)
5746 return -EBUSY;
5747
NeilBrowndc10c642012-03-19 12:46:37 +11005748 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005750 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751
Neil Brown6c2fce22008-06-28 08:31:31 +10005752 if (rdev->raid_disk >= 0)
5753 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754
5755 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005756 * find the disk ... but prefer rdev->saved_raid_disk
5757 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005759 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005760 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005761 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005762 first = rdev->saved_raid_disk;
5763
5764 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005765 p = conf->disks + disk;
5766 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005767 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005769 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005770 if (rdev->saved_raid_disk != disk)
5771 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005772 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005773 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005775 }
5776 for (disk = first; disk <= last; disk++) {
5777 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005778 if (test_bit(WantReplacement, &p->rdev->flags) &&
5779 p->replacement == NULL) {
5780 clear_bit(In_sync, &rdev->flags);
5781 set_bit(Replacement, &rdev->flags);
5782 rdev->raid_disk = disk;
5783 err = 0;
5784 conf->fullsync = 1;
5785 rcu_assign_pointer(p->replacement, rdev);
5786 break;
5787 }
5788 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005789out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005791 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792}
5793
NeilBrownfd01b882011-10-11 16:47:53 +11005794static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795{
5796 /* no resync is happening, and there is enough space
5797 * on all devices, so we can resize.
5798 * We need to make sure resync covers any new space.
5799 * If the array is shrinking we should possibly wait until
5800 * any io in the removed space completes, but it hardly seems
5801 * worth it.
5802 */
NeilBrowna4a61252012-05-22 13:55:27 +10005803 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005804 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005805 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5806 if (mddev->external_size &&
5807 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005808 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005809 if (mddev->bitmap) {
5810 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5811 if (ret)
5812 return ret;
5813 }
5814 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005815 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005816 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005817 if (sectors > mddev->dev_sectors &&
5818 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005819 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5821 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005822 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005823 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 return 0;
5825}
5826
NeilBrownfd01b882011-10-11 16:47:53 +11005827static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005828{
5829 /* Can only proceed if there are plenty of stripe_heads.
5830 * We need a minimum of one full stripe,, and for sensible progress
5831 * it is best to have about 4 times that.
5832 * If we require 4 times, then the default 256 4K stripe_heads will
5833 * allow for chunk sizes up to 256K, which is probably OK.
5834 * If the chunk size is greater, user-space should request more
5835 * stripe_heads first.
5836 */
NeilBrownd1688a62011-10-11 16:49:52 +11005837 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005838 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5839 > conf->max_nr_stripes ||
5840 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5841 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005842 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5843 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005844 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5845 / STRIPE_SIZE)*4);
5846 return 0;
5847 }
5848 return 1;
5849}
5850
NeilBrownfd01b882011-10-11 16:47:53 +11005851static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005852{
NeilBrownd1688a62011-10-11 16:49:52 +11005853 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005854
NeilBrown88ce4932009-03-31 15:24:23 +11005855 if (mddev->delta_disks == 0 &&
5856 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005857 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005858 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005859 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005860 return -EINVAL;
5861 if (mddev->delta_disks < 0) {
5862 /* We might be able to shrink, but the devices must
5863 * be made bigger first.
5864 * For raid6, 4 is the minimum size.
5865 * Otherwise 2 is the minimum
5866 */
5867 int min = 2;
5868 if (mddev->level == 6)
5869 min = 4;
5870 if (mddev->raid_disks + mddev->delta_disks < min)
5871 return -EINVAL;
5872 }
NeilBrown29269552006-03-27 01:18:10 -08005873
NeilBrown01ee22b2009-06-18 08:47:20 +10005874 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005875 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005876
NeilBrowne56108d62012-10-11 14:24:13 +11005877 return resize_stripes(conf, (conf->previous_raid_disks
5878 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08005879}
5880
NeilBrownfd01b882011-10-11 16:47:53 +11005881static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005882{
NeilBrownd1688a62011-10-11 16:49:52 +11005883 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005884 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005885 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005886 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005887
NeilBrownf4168852007-02-28 20:11:53 -08005888 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005889 return -EBUSY;
5890
NeilBrown01ee22b2009-06-18 08:47:20 +10005891 if (!check_stripe_cache(mddev))
5892 return -ENOSPC;
5893
NeilBrown30b67642012-05-22 13:55:28 +10005894 if (has_failed(conf))
5895 return -EINVAL;
5896
NeilBrownc6563a82012-05-21 09:27:00 +10005897 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005898 if (!test_bit(In_sync, &rdev->flags)
5899 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005900 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005901 }
NeilBrown63c70c42006-03-27 01:18:13 -08005902
NeilBrownf4168852007-02-28 20:11:53 -08005903 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005904 /* Not enough devices even to make a degraded array
5905 * of that size
5906 */
5907 return -EINVAL;
5908
NeilBrownec32a2b2009-03-31 15:17:38 +11005909 /* Refuse to reduce size of the array. Any reductions in
5910 * array size must be through explicit setting of array_size
5911 * attribute.
5912 */
5913 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5914 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005915 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005916 "before number of disks\n", mdname(mddev));
5917 return -EINVAL;
5918 }
5919
NeilBrownf6705572006-03-27 01:18:11 -08005920 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005921 spin_lock_irq(&conf->device_lock);
5922 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005923 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005924 conf->prev_chunk_sectors = conf->chunk_sectors;
5925 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005926 conf->prev_algo = conf->algorithm;
5927 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005928 conf->generation++;
5929 /* Code that selects data_offset needs to see the generation update
5930 * if reshape_progress has been set - so a memory barrier needed.
5931 */
5932 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005933 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005934 conf->reshape_progress = raid5_size(mddev, 0, 0);
5935 else
5936 conf->reshape_progress = 0;
5937 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005938 spin_unlock_irq(&conf->device_lock);
5939
5940 /* Add some new drives, as many as will fit.
5941 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005942 * Don't add devices if we are reducing the number of
5943 * devices in the array. This is because it is not possible
5944 * to correctly record the "partially reconstructed" state of
5945 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005946 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005947 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005948 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005949 if (rdev->raid_disk < 0 &&
5950 !test_bit(Faulty, &rdev->flags)) {
5951 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005952 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005953 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005954 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005955 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005956 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005957
5958 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005959 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005960 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005961 } else if (rdev->raid_disk >= conf->previous_raid_disks
5962 && !test_bit(Faulty, &rdev->flags)) {
5963 /* This is a spare that was manually added */
5964 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005965 }
NeilBrown29269552006-03-27 01:18:10 -08005966
NeilBrown87a8dec2011-01-31 11:57:43 +11005967 /* When a reshape changes the number of devices,
5968 * ->degraded is measured against the larger of the
5969 * pre and post number of devices.
5970 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005971 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005972 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005973 spin_unlock_irqrestore(&conf->device_lock, flags);
5974 }
NeilBrown63c70c42006-03-27 01:18:13 -08005975 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005976 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005977 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005978
NeilBrown29269552006-03-27 01:18:10 -08005979 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5980 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5981 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5982 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5983 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005984 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005985 if (!mddev->sync_thread) {
5986 mddev->recovery = 0;
5987 spin_lock_irq(&conf->device_lock);
5988 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005989 rdev_for_each(rdev, mddev)
5990 rdev->new_data_offset = rdev->data_offset;
5991 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005992 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005993 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005994 spin_unlock_irq(&conf->device_lock);
5995 return -EAGAIN;
5996 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005997 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005998 md_wakeup_thread(mddev->sync_thread);
5999 md_new_event(mddev);
6000 return 0;
6001}
NeilBrown29269552006-03-27 01:18:10 -08006002
NeilBrownec32a2b2009-03-31 15:17:38 +11006003/* This is called from the reshape thread and should make any
6004 * changes needed in 'conf'
6005 */
NeilBrownd1688a62011-10-11 16:49:52 +11006006static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006007{
NeilBrown29269552006-03-27 01:18:10 -08006008
NeilBrownf6705572006-03-27 01:18:11 -08006009 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006010 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006011
NeilBrownf6705572006-03-27 01:18:11 -08006012 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006013 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006014 rdev_for_each(rdev, conf->mddev)
6015 rdev->data_offset = rdev->new_data_offset;
6016 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006017 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006018 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006019 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006020
6021 /* read-ahead size must cover two whole stripes, which is
6022 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6023 */
NeilBrown4a5add42010-06-01 19:37:28 +10006024 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006025 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006026 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006027 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006028 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6029 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6030 }
NeilBrown29269552006-03-27 01:18:10 -08006031 }
NeilBrown29269552006-03-27 01:18:10 -08006032}
6033
NeilBrownec32a2b2009-03-31 15:17:38 +11006034/* This is called from the raid5d thread with mddev_lock held.
6035 * It makes config changes to the device.
6036 */
NeilBrownfd01b882011-10-11 16:47:53 +11006037static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006038{
NeilBrownd1688a62011-10-11 16:49:52 +11006039 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006040
6041 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6042
NeilBrownec32a2b2009-03-31 15:17:38 +11006043 if (mddev->delta_disks > 0) {
6044 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6045 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006046 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006047 } else {
6048 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006049 spin_lock_irq(&conf->device_lock);
6050 mddev->degraded = calc_degraded(conf);
6051 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006052 for (d = conf->raid_disks ;
6053 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006054 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006055 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006056 if (rdev)
6057 clear_bit(In_sync, &rdev->flags);
6058 rdev = conf->disks[d].replacement;
6059 if (rdev)
6060 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006061 }
NeilBrowncea9c222009-03-31 15:15:05 +11006062 }
NeilBrown88ce4932009-03-31 15:24:23 +11006063 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006064 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006065 mddev->reshape_position = MaxSector;
6066 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006067 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006068 }
6069}
6070
NeilBrownfd01b882011-10-11 16:47:53 +11006071static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006072{
NeilBrownd1688a62011-10-11 16:49:52 +11006073 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006074
6075 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006076 case 2: /* resume for a suspend */
6077 wake_up(&conf->wait_for_overlap);
6078 break;
6079
NeilBrown72626682005-09-09 16:23:54 -07006080 case 1: /* stop all writes */
6081 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006082 /* '2' tells resync/reshape to pause so that all
6083 * active stripes can drain
6084 */
6085 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006086 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006087 atomic_read(&conf->active_stripes) == 0 &&
6088 atomic_read(&conf->active_aligned_reads) == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01006089 conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006090 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006091 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006092 /* allow reshape to continue */
6093 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006094 break;
6095
6096 case 0: /* re-enable writes */
6097 spin_lock_irq(&conf->device_lock);
6098 conf->quiesce = 0;
6099 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006100 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006101 spin_unlock_irq(&conf->device_lock);
6102 break;
6103 }
NeilBrown72626682005-09-09 16:23:54 -07006104}
NeilBrownb15c2e52006-01-06 00:20:16 -08006105
NeilBrownd562b0c2009-03-31 14:39:39 +11006106
NeilBrownfd01b882011-10-11 16:47:53 +11006107static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006108{
NeilBrowne373ab12011-10-11 16:48:59 +11006109 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006110 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006111
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006112 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006113 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006114 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6115 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006116 return ERR_PTR(-EINVAL);
6117 }
6118
NeilBrowne373ab12011-10-11 16:48:59 +11006119 sectors = raid0_conf->strip_zone[0].zone_end;
6120 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006121 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006122 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006123 mddev->new_layout = ALGORITHM_PARITY_N;
6124 mddev->new_chunk_sectors = mddev->chunk_sectors;
6125 mddev->raid_disks += 1;
6126 mddev->delta_disks = 1;
6127 /* make sure it will be not marked as dirty */
6128 mddev->recovery_cp = MaxSector;
6129
6130 return setup_conf(mddev);
6131}
6132
6133
NeilBrownfd01b882011-10-11 16:47:53 +11006134static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006135{
6136 int chunksect;
6137
6138 if (mddev->raid_disks != 2 ||
6139 mddev->degraded > 1)
6140 return ERR_PTR(-EINVAL);
6141
6142 /* Should check if there are write-behind devices? */
6143
6144 chunksect = 64*2; /* 64K by default */
6145
6146 /* The array must be an exact multiple of chunksize */
6147 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6148 chunksect >>= 1;
6149
6150 if ((chunksect<<9) < STRIPE_SIZE)
6151 /* array size does not allow a suitable chunk size */
6152 return ERR_PTR(-EINVAL);
6153
6154 mddev->new_level = 5;
6155 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006156 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006157
6158 return setup_conf(mddev);
6159}
6160
NeilBrownfd01b882011-10-11 16:47:53 +11006161static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006162{
6163 int new_layout;
6164
6165 switch (mddev->layout) {
6166 case ALGORITHM_LEFT_ASYMMETRIC_6:
6167 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6168 break;
6169 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6170 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6171 break;
6172 case ALGORITHM_LEFT_SYMMETRIC_6:
6173 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6174 break;
6175 case ALGORITHM_RIGHT_SYMMETRIC_6:
6176 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6177 break;
6178 case ALGORITHM_PARITY_0_6:
6179 new_layout = ALGORITHM_PARITY_0;
6180 break;
6181 case ALGORITHM_PARITY_N:
6182 new_layout = ALGORITHM_PARITY_N;
6183 break;
6184 default:
6185 return ERR_PTR(-EINVAL);
6186 }
6187 mddev->new_level = 5;
6188 mddev->new_layout = new_layout;
6189 mddev->delta_disks = -1;
6190 mddev->raid_disks -= 1;
6191 return setup_conf(mddev);
6192}
6193
NeilBrownd562b0c2009-03-31 14:39:39 +11006194
NeilBrownfd01b882011-10-11 16:47:53 +11006195static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006196{
NeilBrown88ce4932009-03-31 15:24:23 +11006197 /* For a 2-drive array, the layout and chunk size can be changed
6198 * immediately as not restriping is needed.
6199 * For larger arrays we record the new value - after validation
6200 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006201 */
NeilBrownd1688a62011-10-11 16:49:52 +11006202 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006203 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006204
NeilBrown597a7112009-06-18 08:47:42 +10006205 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006206 return -EINVAL;
6207 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006208 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006209 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006210 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006211 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006212 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006213 /* not factor of array size */
6214 return -EINVAL;
6215 }
6216
6217 /* They look valid */
6218
NeilBrown88ce4932009-03-31 15:24:23 +11006219 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006220 /* can make the change immediately */
6221 if (mddev->new_layout >= 0) {
6222 conf->algorithm = mddev->new_layout;
6223 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006224 }
6225 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006226 conf->chunk_sectors = new_chunk ;
6227 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006228 }
6229 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6230 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006231 }
NeilBrown50ac1682009-06-18 08:47:55 +10006232 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006233}
6234
NeilBrownfd01b882011-10-11 16:47:53 +11006235static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006236{
NeilBrown597a7112009-06-18 08:47:42 +10006237 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006238
NeilBrown597a7112009-06-18 08:47:42 +10006239 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006240 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006241 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006242 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006243 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006244 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006245 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006246 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006247 /* not factor of array size */
6248 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006249 }
NeilBrown88ce4932009-03-31 15:24:23 +11006250
6251 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006252 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006253}
6254
NeilBrownfd01b882011-10-11 16:47:53 +11006255static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006256{
6257 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006258 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006259 * raid1 - if there are two drives. We need to know the chunk size
6260 * raid4 - trivial - just use a raid4 layout.
6261 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006262 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006263 if (mddev->level == 0)
6264 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006265 if (mddev->level == 1)
6266 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006267 if (mddev->level == 4) {
6268 mddev->new_layout = ALGORITHM_PARITY_N;
6269 mddev->new_level = 5;
6270 return setup_conf(mddev);
6271 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006272 if (mddev->level == 6)
6273 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006274
6275 return ERR_PTR(-EINVAL);
6276}
6277
NeilBrownfd01b882011-10-11 16:47:53 +11006278static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006279{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006280 /* raid4 can take over:
6281 * raid0 - if there is only one strip zone
6282 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006283 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006284 if (mddev->level == 0)
6285 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006286 if (mddev->level == 5 &&
6287 mddev->layout == ALGORITHM_PARITY_N) {
6288 mddev->new_layout = 0;
6289 mddev->new_level = 4;
6290 return setup_conf(mddev);
6291 }
6292 return ERR_PTR(-EINVAL);
6293}
NeilBrownd562b0c2009-03-31 14:39:39 +11006294
NeilBrown84fc4b52011-10-11 16:49:58 +11006295static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006296
NeilBrownfd01b882011-10-11 16:47:53 +11006297static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006298{
6299 /* Currently can only take over a raid5. We map the
6300 * personality to an equivalent raid6 personality
6301 * with the Q block at the end.
6302 */
6303 int new_layout;
6304
6305 if (mddev->pers != &raid5_personality)
6306 return ERR_PTR(-EINVAL);
6307 if (mddev->degraded > 1)
6308 return ERR_PTR(-EINVAL);
6309 if (mddev->raid_disks > 253)
6310 return ERR_PTR(-EINVAL);
6311 if (mddev->raid_disks < 3)
6312 return ERR_PTR(-EINVAL);
6313
6314 switch (mddev->layout) {
6315 case ALGORITHM_LEFT_ASYMMETRIC:
6316 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6317 break;
6318 case ALGORITHM_RIGHT_ASYMMETRIC:
6319 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6320 break;
6321 case ALGORITHM_LEFT_SYMMETRIC:
6322 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6323 break;
6324 case ALGORITHM_RIGHT_SYMMETRIC:
6325 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6326 break;
6327 case ALGORITHM_PARITY_0:
6328 new_layout = ALGORITHM_PARITY_0_6;
6329 break;
6330 case ALGORITHM_PARITY_N:
6331 new_layout = ALGORITHM_PARITY_N;
6332 break;
6333 default:
6334 return ERR_PTR(-EINVAL);
6335 }
6336 mddev->new_level = 6;
6337 mddev->new_layout = new_layout;
6338 mddev->delta_disks = 1;
6339 mddev->raid_disks += 1;
6340 return setup_conf(mddev);
6341}
6342
6343
NeilBrown84fc4b52011-10-11 16:49:58 +11006344static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006345{
6346 .name = "raid6",
6347 .level = 6,
6348 .owner = THIS_MODULE,
6349 .make_request = make_request,
6350 .run = run,
6351 .stop = stop,
6352 .status = status,
6353 .error_handler = error,
6354 .hot_add_disk = raid5_add_disk,
6355 .hot_remove_disk= raid5_remove_disk,
6356 .spare_active = raid5_spare_active,
6357 .sync_request = sync_request,
6358 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006359 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006360 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006361 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006362 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006363 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006364 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006365};
NeilBrown84fc4b52011-10-11 16:49:58 +11006366static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367{
6368 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006369 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 .owner = THIS_MODULE,
6371 .make_request = make_request,
6372 .run = run,
6373 .stop = stop,
6374 .status = status,
6375 .error_handler = error,
6376 .hot_add_disk = raid5_add_disk,
6377 .hot_remove_disk= raid5_remove_disk,
6378 .spare_active = raid5_spare_active,
6379 .sync_request = sync_request,
6380 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006381 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006382 .check_reshape = raid5_check_reshape,
6383 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006384 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006385 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006386 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387};
6388
NeilBrown84fc4b52011-10-11 16:49:58 +11006389static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390{
NeilBrown2604b702006-01-06 00:20:36 -08006391 .name = "raid4",
6392 .level = 4,
6393 .owner = THIS_MODULE,
6394 .make_request = make_request,
6395 .run = run,
6396 .stop = stop,
6397 .status = status,
6398 .error_handler = error,
6399 .hot_add_disk = raid5_add_disk,
6400 .hot_remove_disk= raid5_remove_disk,
6401 .spare_active = raid5_spare_active,
6402 .sync_request = sync_request,
6403 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006404 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006405 .check_reshape = raid5_check_reshape,
6406 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006407 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006408 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006409 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006410};
6411
6412static int __init raid5_init(void)
6413{
NeilBrown16a53ec2006-06-26 00:27:38 -07006414 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006415 register_md_personality(&raid5_personality);
6416 register_md_personality(&raid4_personality);
6417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418}
6419
NeilBrown2604b702006-01-06 00:20:36 -08006420static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421{
NeilBrown16a53ec2006-06-26 00:27:38 -07006422 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006423 unregister_md_personality(&raid5_personality);
6424 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425}
6426
6427module_init(raid5_init);
6428module_exit(raid5_exit);
6429MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006430MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006432MODULE_ALIAS("md-raid5");
6433MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006434MODULE_ALIAS("md-level-5");
6435MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006436MODULE_ALIAS("md-personality-8"); /* RAID6 */
6437MODULE_ALIAS("md-raid6");
6438MODULE_ALIAS("md-level-6");
6439
6440/* This used to be two separate modules, they were: */
6441MODULE_ALIAS("raid5");
6442MODULE_ALIAS("raid6");