blob: ffebc1e8f483179a809bf6a87132898d8238a5c3 [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,
NeilBrown72626682005-09-09 16:23:54 -0700473 conf->device_lock, /* nothing */);
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),
487 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000488 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 conf->inactive_blocked = 0;
490 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100491 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 } else {
493 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100494 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000495 && !test_bit(STRIPE_EXPANDING, &sh->state)
496 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 } else {
498 if (!test_bit(STRIPE_HANDLE, &sh->state))
499 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700500 if (list_empty(&sh->lru) &&
501 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700502 BUG();
503 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505 }
506 } while (sh == NULL);
507
508 if (sh)
509 atomic_inc(&sh->count);
510
511 spin_unlock_irq(&conf->device_lock);
512 return sh;
513}
514
NeilBrown05616be2012-05-21 09:27:00 +1000515/* Determine if 'data_offset' or 'new_data_offset' should be used
516 * in this stripe_head.
517 */
518static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
519{
520 sector_t progress = conf->reshape_progress;
521 /* Need a memory barrier to make sure we see the value
522 * of conf->generation, or ->data_offset that was set before
523 * reshape_progress was updated.
524 */
525 smp_rmb();
526 if (progress == MaxSector)
527 return 0;
528 if (sh->generation == conf->generation - 1)
529 return 0;
530 /* We are in a reshape, and this is a new-generation stripe,
531 * so use new_data_offset.
532 */
533 return 1;
534}
535
NeilBrown6712ecf2007-09-27 12:47:43 +0200536static void
537raid5_end_read_request(struct bio *bi, int error);
538static void
539raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700540
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000541static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700542{
NeilBrownd1688a62011-10-11 16:49:52 +1100543 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700544 int i, disks = sh->disks;
545
546 might_sleep();
547
548 for (i = disks; i--; ) {
549 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100550 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100551 struct bio *bi, *rbi;
552 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200553 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
554 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
555 rw = WRITE_FUA;
556 else
557 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100558 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100559 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200560 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700561 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100562 else if (test_and_clear_bit(R5_WantReplace,
563 &sh->dev[i].flags)) {
564 rw = WRITE;
565 replace_only = 1;
566 } else
Dan Williams91c00922007-01-02 13:52:30 -0700567 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000568 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
569 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700570
571 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100572 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700573
574 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100575 rbi->bi_rw = rw;
576 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700577 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100578 rbi->bi_end_io = raid5_end_write_request;
579 } else
Dan Williams91c00922007-01-02 13:52:30 -0700580 bi->bi_end_io = raid5_end_read_request;
581
582 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100583 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100584 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
585 rdev = rcu_dereference(conf->disks[i].rdev);
586 if (!rdev) {
587 rdev = rrdev;
588 rrdev = NULL;
589 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100590 if (rw & WRITE) {
591 if (replace_only)
592 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100593 if (rdev == rrdev)
594 /* We raced and saw duplicates */
595 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100596 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100597 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100598 rdev = rrdev;
599 rrdev = NULL;
600 }
NeilBrown977df362011-12-23 10:17:53 +1100601
Dan Williams91c00922007-01-02 13:52:30 -0700602 if (rdev && test_bit(Faulty, &rdev->flags))
603 rdev = NULL;
604 if (rdev)
605 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100606 if (rrdev && test_bit(Faulty, &rrdev->flags))
607 rrdev = NULL;
608 if (rrdev)
609 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700610 rcu_read_unlock();
611
NeilBrown73e92e52011-07-28 11:39:22 +1000612 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100613 * need to check for writes. We never accept write errors
614 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000615 */
616 while ((rw & WRITE) && rdev &&
617 test_bit(WriteErrorSeen, &rdev->flags)) {
618 sector_t first_bad;
619 int bad_sectors;
620 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
621 &first_bad, &bad_sectors);
622 if (!bad)
623 break;
624
625 if (bad < 0) {
626 set_bit(BlockedBadBlocks, &rdev->flags);
627 if (!conf->mddev->external &&
628 conf->mddev->flags) {
629 /* It is very unlikely, but we might
630 * still need to write out the
631 * bad block log - better give it
632 * a chance*/
633 md_check_recovery(conf->mddev);
634 }
majianpeng18507532012-07-03 12:11:54 +1000635 /*
636 * Because md_wait_for_blocked_rdev
637 * will dec nr_pending, we must
638 * increment it first.
639 */
640 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000641 md_wait_for_blocked_rdev(rdev, conf->mddev);
642 } else {
643 /* Acknowledged bad block - skip the write */
644 rdev_dec_pending(rdev, conf->mddev);
645 rdev = NULL;
646 }
647 }
648
Dan Williams91c00922007-01-02 13:52:30 -0700649 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100650 if (s->syncing || s->expanding || s->expanded
651 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700652 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
653
Dan Williams2b7497f2008-06-28 08:31:52 +1000654 set_bit(STRIPE_IO_STARTED, &sh->state);
655
Dan Williams91c00922007-01-02 13:52:30 -0700656 bi->bi_bdev = rdev->bdev;
657 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700658 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700659 bi->bi_rw, i);
660 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000661 if (use_new_offset(conf, sh))
662 bi->bi_sector = (sh->sector
663 + rdev->new_data_offset);
664 else
665 bi->bi_sector = (sh->sector
666 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000667 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
668 bi->bi_rw |= REQ_FLUSH;
669
Dan Williams91c00922007-01-02 13:52:30 -0700670 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700671 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700672 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
673 bi->bi_io_vec[0].bv_offset = 0;
674 bi->bi_size = STRIPE_SIZE;
675 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100676 if (rrdev)
677 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
NeilBrowna9add5d2012-10-31 11:59:09 +1100678 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
679 bi, disk_devt(conf->mddev->gendisk),
680 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700681 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100682 }
683 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100684 if (s->syncing || s->expanding || s->expanded
685 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100686 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
687
688 set_bit(STRIPE_IO_STARTED, &sh->state);
689
690 rbi->bi_bdev = rrdev->bdev;
691 pr_debug("%s: for %llu schedule op %ld on "
692 "replacement disc %d\n",
693 __func__, (unsigned long long)sh->sector,
694 rbi->bi_rw, i);
695 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000696 if (use_new_offset(conf, sh))
697 rbi->bi_sector = (sh->sector
698 + rrdev->new_data_offset);
699 else
700 rbi->bi_sector = (sh->sector
701 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100702 rbi->bi_flags = 1 << BIO_UPTODATE;
703 rbi->bi_idx = 0;
704 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
705 rbi->bi_io_vec[0].bv_offset = 0;
706 rbi->bi_size = STRIPE_SIZE;
707 rbi->bi_next = NULL;
NeilBrowna9add5d2012-10-31 11:59:09 +1100708 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
709 rbi, disk_devt(conf->mddev->gendisk),
710 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100711 generic_make_request(rbi);
712 }
713 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000714 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700715 set_bit(STRIPE_DEGRADED, &sh->state);
716 pr_debug("skip op %ld on disc %d for sector %llu\n",
717 bi->bi_rw, i, (unsigned long long)sh->sector);
718 clear_bit(R5_LOCKED, &sh->dev[i].flags);
719 set_bit(STRIPE_HANDLE, &sh->state);
720 }
721 }
722}
723
724static struct dma_async_tx_descriptor *
725async_copy_data(int frombio, struct bio *bio, struct page *page,
726 sector_t sector, struct dma_async_tx_descriptor *tx)
727{
728 struct bio_vec *bvl;
729 struct page *bio_page;
730 int i;
731 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700732 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700733 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700734
735 if (bio->bi_sector >= sector)
736 page_offset = (signed)(bio->bi_sector - sector) * 512;
737 else
738 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700739
Dan Williams0403e382009-09-08 17:42:50 -0700740 if (frombio)
741 flags |= ASYNC_TX_FENCE;
742 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
743
Dan Williams91c00922007-01-02 13:52:30 -0700744 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000745 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700746 int clen;
747 int b_offset = 0;
748
749 if (page_offset < 0) {
750 b_offset = -page_offset;
751 page_offset += b_offset;
752 len -= b_offset;
753 }
754
755 if (len > 0 && page_offset + len > STRIPE_SIZE)
756 clen = STRIPE_SIZE - page_offset;
757 else
758 clen = len;
759
760 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000761 b_offset += bvl->bv_offset;
762 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700763 if (frombio)
764 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700765 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700766 else
767 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700768 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700769 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700770 /* chain the operations */
771 submit.depend_tx = tx;
772
Dan Williams91c00922007-01-02 13:52:30 -0700773 if (clen < len) /* hit end of page */
774 break;
775 page_offset += len;
776 }
777
778 return tx;
779}
780
781static void ops_complete_biofill(void *stripe_head_ref)
782{
783 struct stripe_head *sh = stripe_head_ref;
784 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700785 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700786
Harvey Harrisone46b2722008-04-28 02:15:50 -0700787 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700788 (unsigned long long)sh->sector);
789
790 /* clear completed biofills */
791 for (i = sh->disks; i--; ) {
792 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700793
794 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700795 /* and check if we need to reply to a read request,
796 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000797 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700798 */
799 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700800 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700801
Dan Williams91c00922007-01-02 13:52:30 -0700802 BUG_ON(!dev->read);
803 rbi = dev->read;
804 dev->read = NULL;
805 while (rbi && rbi->bi_sector <
806 dev->sector + STRIPE_SECTORS) {
807 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000808 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700809 rbi->bi_next = return_bi;
810 return_bi = rbi;
811 }
Dan Williams91c00922007-01-02 13:52:30 -0700812 rbi = rbi2;
813 }
814 }
815 }
Dan Williams83de75c2008-06-28 08:31:58 +1000816 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700817
818 return_io(return_bi);
819
Dan Williamse4d84902007-09-24 10:06:13 -0700820 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700821 release_stripe(sh);
822}
823
824static void ops_run_biofill(struct stripe_head *sh)
825{
826 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700827 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700828 int i;
829
Harvey Harrisone46b2722008-04-28 02:15:50 -0700830 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700831 (unsigned long long)sh->sector);
832
833 for (i = sh->disks; i--; ) {
834 struct r5dev *dev = &sh->dev[i];
835 if (test_bit(R5_Wantfill, &dev->flags)) {
836 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000837 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700838 dev->read = rbi = dev->toread;
839 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000840 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700841 while (rbi && rbi->bi_sector <
842 dev->sector + STRIPE_SECTORS) {
843 tx = async_copy_data(0, rbi, dev->page,
844 dev->sector, tx);
845 rbi = r5_next_bio(rbi, dev->sector);
846 }
847 }
848 }
849
850 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700851 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
852 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700853}
854
Dan Williams4e7d2c02009-08-29 19:13:11 -0700855static void mark_target_uptodate(struct stripe_head *sh, int target)
856{
857 struct r5dev *tgt;
858
859 if (target < 0)
860 return;
861
862 tgt = &sh->dev[target];
863 set_bit(R5_UPTODATE, &tgt->flags);
864 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
865 clear_bit(R5_Wantcompute, &tgt->flags);
866}
867
Dan Williamsac6b53b2009-07-14 13:40:19 -0700868static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700869{
870 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700871
Harvey Harrisone46b2722008-04-28 02:15:50 -0700872 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700873 (unsigned long long)sh->sector);
874
Dan Williamsac6b53b2009-07-14 13:40:19 -0700875 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700876 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700877 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700878
Dan Williamsecc65c92008-06-28 08:31:57 +1000879 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
880 if (sh->check_state == check_state_compute_run)
881 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700882 set_bit(STRIPE_HANDLE, &sh->state);
883 release_stripe(sh);
884}
885
Dan Williamsd6f38f32009-07-14 11:50:52 -0700886/* return a pointer to the address conversion region of the scribble buffer */
887static addr_conv_t *to_addr_conv(struct stripe_head *sh,
888 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700889{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700890 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
891}
892
893static struct dma_async_tx_descriptor *
894ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
895{
Dan Williams91c00922007-01-02 13:52:30 -0700896 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700897 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700898 int target = sh->ops.target;
899 struct r5dev *tgt = &sh->dev[target];
900 struct page *xor_dest = tgt->page;
901 int count = 0;
902 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700903 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700904 int i;
905
906 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700907 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700908 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
909
910 for (i = disks; i--; )
911 if (i != target)
912 xor_srcs[count++] = sh->dev[i].page;
913
914 atomic_inc(&sh->count);
915
Dan Williams0403e382009-09-08 17:42:50 -0700916 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700917 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700918 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700919 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700920 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700921 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700922
Dan Williams91c00922007-01-02 13:52:30 -0700923 return tx;
924}
925
Dan Williamsac6b53b2009-07-14 13:40:19 -0700926/* set_syndrome_sources - populate source buffers for gen_syndrome
927 * @srcs - (struct page *) array of size sh->disks
928 * @sh - stripe_head to parse
929 *
930 * Populates srcs in proper layout order for the stripe and returns the
931 * 'count' of sources to be used in a call to async_gen_syndrome. The P
932 * destination buffer is recorded in srcs[count] and the Q destination
933 * is recorded in srcs[count+1]].
934 */
935static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
936{
937 int disks = sh->disks;
938 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
939 int d0_idx = raid6_d0(sh);
940 int count;
941 int i;
942
943 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100944 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700945
946 count = 0;
947 i = d0_idx;
948 do {
949 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
950
951 srcs[slot] = sh->dev[i].page;
952 i = raid6_next_disk(i, disks);
953 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700954
NeilBrowne4424fe2009-10-16 16:27:34 +1100955 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700956}
957
958static struct dma_async_tx_descriptor *
959ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
960{
961 int disks = sh->disks;
962 struct page **blocks = percpu->scribble;
963 int target;
964 int qd_idx = sh->qd_idx;
965 struct dma_async_tx_descriptor *tx;
966 struct async_submit_ctl submit;
967 struct r5dev *tgt;
968 struct page *dest;
969 int i;
970 int count;
971
972 if (sh->ops.target < 0)
973 target = sh->ops.target2;
974 else if (sh->ops.target2 < 0)
975 target = sh->ops.target;
976 else
977 /* we should only have one valid target */
978 BUG();
979 BUG_ON(target < 0);
980 pr_debug("%s: stripe %llu block: %d\n",
981 __func__, (unsigned long long)sh->sector, target);
982
983 tgt = &sh->dev[target];
984 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
985 dest = tgt->page;
986
987 atomic_inc(&sh->count);
988
989 if (target == qd_idx) {
990 count = set_syndrome_sources(blocks, sh);
991 blocks[count] = NULL; /* regenerating p is not necessary */
992 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700993 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
994 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700995 to_addr_conv(sh, percpu));
996 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
997 } else {
998 /* Compute any data- or p-drive using XOR */
999 count = 0;
1000 for (i = disks; i-- ; ) {
1001 if (i == target || i == qd_idx)
1002 continue;
1003 blocks[count++] = sh->dev[i].page;
1004 }
1005
Dan Williams0403e382009-09-08 17:42:50 -07001006 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1007 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001008 to_addr_conv(sh, percpu));
1009 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1010 }
1011
1012 return tx;
1013}
1014
1015static struct dma_async_tx_descriptor *
1016ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1017{
1018 int i, count, disks = sh->disks;
1019 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1020 int d0_idx = raid6_d0(sh);
1021 int faila = -1, failb = -1;
1022 int target = sh->ops.target;
1023 int target2 = sh->ops.target2;
1024 struct r5dev *tgt = &sh->dev[target];
1025 struct r5dev *tgt2 = &sh->dev[target2];
1026 struct dma_async_tx_descriptor *tx;
1027 struct page **blocks = percpu->scribble;
1028 struct async_submit_ctl submit;
1029
1030 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1031 __func__, (unsigned long long)sh->sector, target, target2);
1032 BUG_ON(target < 0 || target2 < 0);
1033 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1034 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1035
Dan Williams6c910a72009-09-16 12:24:54 -07001036 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001037 * slot number conversion for 'faila' and 'failb'
1038 */
1039 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001040 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001041 count = 0;
1042 i = d0_idx;
1043 do {
1044 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1045
1046 blocks[slot] = sh->dev[i].page;
1047
1048 if (i == target)
1049 faila = slot;
1050 if (i == target2)
1051 failb = slot;
1052 i = raid6_next_disk(i, disks);
1053 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001054
1055 BUG_ON(faila == failb);
1056 if (failb < faila)
1057 swap(faila, failb);
1058 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1059 __func__, (unsigned long long)sh->sector, faila, failb);
1060
1061 atomic_inc(&sh->count);
1062
1063 if (failb == syndrome_disks+1) {
1064 /* Q disk is one of the missing disks */
1065 if (faila == syndrome_disks) {
1066 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001067 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1068 ops_complete_compute, sh,
1069 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001070 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001071 STRIPE_SIZE, &submit);
1072 } else {
1073 struct page *dest;
1074 int data_target;
1075 int qd_idx = sh->qd_idx;
1076
1077 /* Missing D+Q: recompute D from P, then recompute Q */
1078 if (target == qd_idx)
1079 data_target = target2;
1080 else
1081 data_target = target;
1082
1083 count = 0;
1084 for (i = disks; i-- ; ) {
1085 if (i == data_target || i == qd_idx)
1086 continue;
1087 blocks[count++] = sh->dev[i].page;
1088 }
1089 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001090 init_async_submit(&submit,
1091 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1092 NULL, NULL, NULL,
1093 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001094 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1095 &submit);
1096
1097 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001098 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1099 ops_complete_compute, sh,
1100 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001101 return async_gen_syndrome(blocks, 0, count+2,
1102 STRIPE_SIZE, &submit);
1103 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001104 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001105 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1106 ops_complete_compute, sh,
1107 to_addr_conv(sh, percpu));
1108 if (failb == syndrome_disks) {
1109 /* We're missing D+P. */
1110 return async_raid6_datap_recov(syndrome_disks+2,
1111 STRIPE_SIZE, faila,
1112 blocks, &submit);
1113 } else {
1114 /* We're missing D+D. */
1115 return async_raid6_2data_recov(syndrome_disks+2,
1116 STRIPE_SIZE, faila, failb,
1117 blocks, &submit);
1118 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001119 }
1120}
1121
1122
Dan Williams91c00922007-01-02 13:52:30 -07001123static void ops_complete_prexor(void *stripe_head_ref)
1124{
1125 struct stripe_head *sh = stripe_head_ref;
1126
Harvey Harrisone46b2722008-04-28 02:15:50 -07001127 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001128 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001129}
1130
1131static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001132ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1133 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001134{
Dan Williams91c00922007-01-02 13:52:30 -07001135 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001136 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001137 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001138 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001139
1140 /* existing parity data subtracted */
1141 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1142
Harvey Harrisone46b2722008-04-28 02:15:50 -07001143 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001144 (unsigned long long)sh->sector);
1145
1146 for (i = disks; i--; ) {
1147 struct r5dev *dev = &sh->dev[i];
1148 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001149 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001150 xor_srcs[count++] = dev->page;
1151 }
1152
Dan Williams0403e382009-09-08 17:42:50 -07001153 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001154 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001155 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001156
1157 return tx;
1158}
1159
1160static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001161ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001162{
1163 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001164 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001165
Harvey Harrisone46b2722008-04-28 02:15:50 -07001166 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001167 (unsigned long long)sh->sector);
1168
1169 for (i = disks; i--; ) {
1170 struct r5dev *dev = &sh->dev[i];
1171 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001172
Dan Williamsd8ee0722008-06-28 08:32:06 +10001173 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001174 struct bio *wbi;
1175
Shaohua Lib17459c2012-07-19 16:01:31 +10001176 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001177 chosen = dev->towrite;
1178 dev->towrite = NULL;
1179 BUG_ON(dev->written);
1180 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001181 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001182
1183 while (wbi && wbi->bi_sector <
1184 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001185 if (wbi->bi_rw & REQ_FUA)
1186 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001187 if (wbi->bi_rw & REQ_SYNC)
1188 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001189 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001190 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001191 else
Shaohua Li620125f2012-10-11 13:49:05 +11001192 tx = async_copy_data(1, wbi, dev->page,
1193 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001194 wbi = r5_next_bio(wbi, dev->sector);
1195 }
1196 }
1197 }
1198
1199 return tx;
1200}
1201
Dan Williamsac6b53b2009-07-14 13:40:19 -07001202static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001203{
1204 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001205 int disks = sh->disks;
1206 int pd_idx = sh->pd_idx;
1207 int qd_idx = sh->qd_idx;
1208 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001209 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001210
Harvey Harrisone46b2722008-04-28 02:15:50 -07001211 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001212 (unsigned long long)sh->sector);
1213
Shaohua Libc0934f2012-05-22 13:55:05 +10001214 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001215 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001216 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001217 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001218 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001219
Dan Williams91c00922007-01-02 13:52:30 -07001220 for (i = disks; i--; ) {
1221 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001222
Tejun Heoe9c74692010-09-03 11:56:18 +02001223 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001224 if (!discard)
1225 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001226 if (fua)
1227 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001228 if (sync)
1229 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001230 }
Dan Williams91c00922007-01-02 13:52:30 -07001231 }
1232
Dan Williamsd8ee0722008-06-28 08:32:06 +10001233 if (sh->reconstruct_state == reconstruct_state_drain_run)
1234 sh->reconstruct_state = reconstruct_state_drain_result;
1235 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1236 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1237 else {
1238 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1239 sh->reconstruct_state = reconstruct_state_result;
1240 }
Dan Williams91c00922007-01-02 13:52:30 -07001241
1242 set_bit(STRIPE_HANDLE, &sh->state);
1243 release_stripe(sh);
1244}
1245
1246static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001247ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1248 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001249{
Dan Williams91c00922007-01-02 13:52:30 -07001250 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001251 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001252 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001253 int count = 0, pd_idx = sh->pd_idx, i;
1254 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001255 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001256 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001257
Harvey Harrisone46b2722008-04-28 02:15:50 -07001258 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001259 (unsigned long long)sh->sector);
1260
Shaohua Li620125f2012-10-11 13:49:05 +11001261 for (i = 0; i < sh->disks; i++) {
1262 if (pd_idx == i)
1263 continue;
1264 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1265 break;
1266 }
1267 if (i >= sh->disks) {
1268 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001269 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1270 ops_complete_reconstruct(sh);
1271 return;
1272 }
Dan Williams91c00922007-01-02 13:52:30 -07001273 /* check if prexor is active which means only process blocks
1274 * that are part of a read-modify-write (written)
1275 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001276 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1277 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001278 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1279 for (i = disks; i--; ) {
1280 struct r5dev *dev = &sh->dev[i];
1281 if (dev->written)
1282 xor_srcs[count++] = dev->page;
1283 }
1284 } else {
1285 xor_dest = sh->dev[pd_idx].page;
1286 for (i = disks; i--; ) {
1287 struct r5dev *dev = &sh->dev[i];
1288 if (i != pd_idx)
1289 xor_srcs[count++] = dev->page;
1290 }
1291 }
1292
Dan Williams91c00922007-01-02 13:52:30 -07001293 /* 1/ if we prexor'd then the dest is reused as a source
1294 * 2/ if we did not prexor then we are redoing the parity
1295 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1296 * for the synchronous xor case
1297 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001298 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001299 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1300
1301 atomic_inc(&sh->count);
1302
Dan Williamsac6b53b2009-07-14 13:40:19 -07001303 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001304 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001305 if (unlikely(count == 1))
1306 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1307 else
1308 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001309}
1310
Dan Williamsac6b53b2009-07-14 13:40:19 -07001311static void
1312ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1313 struct dma_async_tx_descriptor *tx)
1314{
1315 struct async_submit_ctl submit;
1316 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001317 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001318
1319 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1320
Shaohua Li620125f2012-10-11 13:49:05 +11001321 for (i = 0; i < sh->disks; i++) {
1322 if (sh->pd_idx == i || sh->qd_idx == i)
1323 continue;
1324 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1325 break;
1326 }
1327 if (i >= sh->disks) {
1328 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001329 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1330 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1331 ops_complete_reconstruct(sh);
1332 return;
1333 }
1334
Dan Williamsac6b53b2009-07-14 13:40:19 -07001335 count = set_syndrome_sources(blocks, sh);
1336
1337 atomic_inc(&sh->count);
1338
1339 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1340 sh, to_addr_conv(sh, percpu));
1341 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001342}
1343
1344static void ops_complete_check(void *stripe_head_ref)
1345{
1346 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001347
Harvey Harrisone46b2722008-04-28 02:15:50 -07001348 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001349 (unsigned long long)sh->sector);
1350
Dan Williamsecc65c92008-06-28 08:31:57 +10001351 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001352 set_bit(STRIPE_HANDLE, &sh->state);
1353 release_stripe(sh);
1354}
1355
Dan Williamsac6b53b2009-07-14 13:40:19 -07001356static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001357{
Dan Williams91c00922007-01-02 13:52:30 -07001358 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001359 int pd_idx = sh->pd_idx;
1360 int qd_idx = sh->qd_idx;
1361 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001362 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001363 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001364 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001365 int count;
1366 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001367
Harvey Harrisone46b2722008-04-28 02:15:50 -07001368 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001369 (unsigned long long)sh->sector);
1370
Dan Williamsac6b53b2009-07-14 13:40:19 -07001371 count = 0;
1372 xor_dest = sh->dev[pd_idx].page;
1373 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001374 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001375 if (i == pd_idx || i == qd_idx)
1376 continue;
1377 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001378 }
1379
Dan Williamsd6f38f32009-07-14 11:50:52 -07001380 init_async_submit(&submit, 0, NULL, NULL, NULL,
1381 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001382 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001383 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001384
Dan Williams91c00922007-01-02 13:52:30 -07001385 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001386 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1387 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001388}
1389
Dan Williamsac6b53b2009-07-14 13:40:19 -07001390static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1391{
1392 struct page **srcs = percpu->scribble;
1393 struct async_submit_ctl submit;
1394 int count;
1395
1396 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1397 (unsigned long long)sh->sector, checkp);
1398
1399 count = set_syndrome_sources(srcs, sh);
1400 if (!checkp)
1401 srcs[count] = NULL;
1402
1403 atomic_inc(&sh->count);
1404 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1405 sh, to_addr_conv(sh, percpu));
1406 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1407 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1408}
1409
Dan Williams417b8d42009-10-16 16:25:22 +11001410static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001411{
1412 int overlap_clear = 0, i, disks = sh->disks;
1413 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001414 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001415 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001416 struct raid5_percpu *percpu;
1417 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001418
Dan Williamsd6f38f32009-07-14 11:50:52 -07001419 cpu = get_cpu();
1420 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001421 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001422 ops_run_biofill(sh);
1423 overlap_clear++;
1424 }
1425
Dan Williams7b3a8712008-06-28 08:32:09 +10001426 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001427 if (level < 6)
1428 tx = ops_run_compute5(sh, percpu);
1429 else {
1430 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1431 tx = ops_run_compute6_1(sh, percpu);
1432 else
1433 tx = ops_run_compute6_2(sh, percpu);
1434 }
1435 /* terminate the chain if reconstruct is not set to be run */
1436 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001437 async_tx_ack(tx);
1438 }
Dan Williams91c00922007-01-02 13:52:30 -07001439
Dan Williams600aa102008-06-28 08:32:05 +10001440 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001441 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001442
Dan Williams600aa102008-06-28 08:32:05 +10001443 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001444 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001445 overlap_clear++;
1446 }
1447
Dan Williamsac6b53b2009-07-14 13:40:19 -07001448 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1449 if (level < 6)
1450 ops_run_reconstruct5(sh, percpu, tx);
1451 else
1452 ops_run_reconstruct6(sh, percpu, tx);
1453 }
Dan Williams91c00922007-01-02 13:52:30 -07001454
Dan Williamsac6b53b2009-07-14 13:40:19 -07001455 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1456 if (sh->check_state == check_state_run)
1457 ops_run_check_p(sh, percpu);
1458 else if (sh->check_state == check_state_run_q)
1459 ops_run_check_pq(sh, percpu, 0);
1460 else if (sh->check_state == check_state_run_pq)
1461 ops_run_check_pq(sh, percpu, 1);
1462 else
1463 BUG();
1464 }
Dan Williams91c00922007-01-02 13:52:30 -07001465
Dan Williams91c00922007-01-02 13:52:30 -07001466 if (overlap_clear)
1467 for (i = disks; i--; ) {
1468 struct r5dev *dev = &sh->dev[i];
1469 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1470 wake_up(&sh->raid_conf->wait_for_overlap);
1471 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001472 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001473}
1474
Dan Williams417b8d42009-10-16 16:25:22 +11001475#ifdef CONFIG_MULTICORE_RAID456
1476static void async_run_ops(void *param, async_cookie_t cookie)
1477{
1478 struct stripe_head *sh = param;
1479 unsigned long ops_request = sh->ops.request;
1480
1481 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1482 wake_up(&sh->ops.wait_for_ops);
1483
1484 __raid_run_ops(sh, ops_request);
1485 release_stripe(sh);
1486}
1487
1488static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1489{
1490 /* since handle_stripe can be called outside of raid5d context
1491 * we need to ensure sh->ops.request is de-staged before another
1492 * request arrives
1493 */
1494 wait_event(sh->ops.wait_for_ops,
1495 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1496 sh->ops.request = ops_request;
1497
1498 atomic_inc(&sh->count);
1499 async_schedule(async_run_ops, sh);
1500}
1501#else
1502#define raid_run_ops __raid_run_ops
1503#endif
1504
NeilBrownd1688a62011-10-11 16:49:52 +11001505static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
1507 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001508 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001509 if (!sh)
1510 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001511
NeilBrown3f294f42005-11-08 21:39:25 -08001512 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001513 #ifdef CONFIG_MULTICORE_RAID456
1514 init_waitqueue_head(&sh->ops.wait_for_ops);
1515 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001516
Shaohua Lib17459c2012-07-19 16:01:31 +10001517 spin_lock_init(&sh->stripe_lock);
1518
NeilBrowne4e11e32010-06-16 16:45:16 +10001519 if (grow_buffers(sh)) {
1520 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001521 kmem_cache_free(conf->slab_cache, sh);
1522 return 0;
1523 }
1524 /* we just created an active stripe so... */
1525 atomic_set(&sh->count, 1);
1526 atomic_inc(&conf->active_stripes);
1527 INIT_LIST_HEAD(&sh->lru);
1528 release_stripe(sh);
1529 return 1;
1530}
1531
NeilBrownd1688a62011-10-11 16:49:52 +11001532static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001533{
Christoph Lametere18b8902006-12-06 20:33:20 -08001534 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001535 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
NeilBrownf4be6b42010-06-01 19:37:25 +10001537 if (conf->mddev->gendisk)
1538 sprintf(conf->cache_name[0],
1539 "raid%d-%s", conf->level, mdname(conf->mddev));
1540 else
1541 sprintf(conf->cache_name[0],
1542 "raid%d-%p", conf->level, conf->mddev);
1543 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1544
NeilBrownad01c9e2006-03-27 01:18:07 -08001545 conf->active_name = 0;
1546 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001548 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (!sc)
1550 return 1;
1551 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001552 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001553 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001554 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return 0;
1557}
NeilBrown29269552006-03-27 01:18:10 -08001558
Dan Williamsd6f38f32009-07-14 11:50:52 -07001559/**
1560 * scribble_len - return the required size of the scribble region
1561 * @num - total number of disks in the array
1562 *
1563 * The size must be enough to contain:
1564 * 1/ a struct page pointer for each device in the array +2
1565 * 2/ room to convert each entry in (1) to its corresponding dma
1566 * (dma_map_page()) or page (page_address()) address.
1567 *
1568 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1569 * calculate over all devices (not just the data blocks), using zeros in place
1570 * of the P and Q blocks.
1571 */
1572static size_t scribble_len(int num)
1573{
1574 size_t len;
1575
1576 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1577
1578 return len;
1579}
1580
NeilBrownd1688a62011-10-11 16:49:52 +11001581static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001582{
1583 /* Make all the stripes able to hold 'newsize' devices.
1584 * New slots in each stripe get 'page' set to a new page.
1585 *
1586 * This happens in stages:
1587 * 1/ create a new kmem_cache and allocate the required number of
1588 * stripe_heads.
1589 * 2/ gather all the old stripe_heads and tranfer the pages across
1590 * to the new stripe_heads. This will have the side effect of
1591 * freezing the array as once all stripe_heads have been collected,
1592 * no IO will be possible. Old stripe heads are freed once their
1593 * pages have been transferred over, and the old kmem_cache is
1594 * freed when all stripes are done.
1595 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1596 * we simple return a failre status - no need to clean anything up.
1597 * 4/ allocate new pages for the new slots in the new stripe_heads.
1598 * If this fails, we don't bother trying the shrink the
1599 * stripe_heads down again, we just leave them as they are.
1600 * As each stripe_head is processed the new one is released into
1601 * active service.
1602 *
1603 * Once step2 is started, we cannot afford to wait for a write,
1604 * so we use GFP_NOIO allocations.
1605 */
1606 struct stripe_head *osh, *nsh;
1607 LIST_HEAD(newstripes);
1608 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001609 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001610 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001611 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001612 int i;
1613
1614 if (newsize <= conf->pool_size)
1615 return 0; /* never bother to shrink */
1616
Dan Williamsb5470dc2008-06-27 21:44:04 -07001617 err = md_allow_write(conf->mddev);
1618 if (err)
1619 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001620
NeilBrownad01c9e2006-03-27 01:18:07 -08001621 /* Step 1 */
1622 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1623 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001624 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001625 if (!sc)
1626 return -ENOMEM;
1627
1628 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001629 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001630 if (!nsh)
1631 break;
1632
NeilBrownad01c9e2006-03-27 01:18:07 -08001633 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001634 #ifdef CONFIG_MULTICORE_RAID456
1635 init_waitqueue_head(&nsh->ops.wait_for_ops);
1636 #endif
NeilBrowncb13ff62012-09-24 16:27:20 +10001637 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001638
1639 list_add(&nsh->lru, &newstripes);
1640 }
1641 if (i) {
1642 /* didn't get enough, give up */
1643 while (!list_empty(&newstripes)) {
1644 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1645 list_del(&nsh->lru);
1646 kmem_cache_free(sc, nsh);
1647 }
1648 kmem_cache_destroy(sc);
1649 return -ENOMEM;
1650 }
1651 /* Step 2 - Must use GFP_NOIO now.
1652 * OK, we have enough stripes, start collecting inactive
1653 * stripes and copying them over
1654 */
1655 list_for_each_entry(nsh, &newstripes, lru) {
1656 spin_lock_irq(&conf->device_lock);
1657 wait_event_lock_irq(conf->wait_for_stripe,
1658 !list_empty(&conf->inactive_list),
1659 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001660 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001661 osh = get_free_stripe(conf);
1662 spin_unlock_irq(&conf->device_lock);
1663 atomic_set(&nsh->count, 1);
1664 for(i=0; i<conf->pool_size; i++)
1665 nsh->dev[i].page = osh->dev[i].page;
1666 for( ; i<newsize; i++)
1667 nsh->dev[i].page = NULL;
1668 kmem_cache_free(conf->slab_cache, osh);
1669 }
1670 kmem_cache_destroy(conf->slab_cache);
1671
1672 /* Step 3.
1673 * At this point, we are holding all the stripes so the array
1674 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001675 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001676 */
1677 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1678 if (ndisks) {
1679 for (i=0; i<conf->raid_disks; i++)
1680 ndisks[i] = conf->disks[i];
1681 kfree(conf->disks);
1682 conf->disks = ndisks;
1683 } else
1684 err = -ENOMEM;
1685
Dan Williamsd6f38f32009-07-14 11:50:52 -07001686 get_online_cpus();
1687 conf->scribble_len = scribble_len(newsize);
1688 for_each_present_cpu(cpu) {
1689 struct raid5_percpu *percpu;
1690 void *scribble;
1691
1692 percpu = per_cpu_ptr(conf->percpu, cpu);
1693 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1694
1695 if (scribble) {
1696 kfree(percpu->scribble);
1697 percpu->scribble = scribble;
1698 } else {
1699 err = -ENOMEM;
1700 break;
1701 }
1702 }
1703 put_online_cpus();
1704
NeilBrownad01c9e2006-03-27 01:18:07 -08001705 /* Step 4, return new stripes to service */
1706 while(!list_empty(&newstripes)) {
1707 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1708 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001709
NeilBrownad01c9e2006-03-27 01:18:07 -08001710 for (i=conf->raid_disks; i < newsize; i++)
1711 if (nsh->dev[i].page == NULL) {
1712 struct page *p = alloc_page(GFP_NOIO);
1713 nsh->dev[i].page = p;
1714 if (!p)
1715 err = -ENOMEM;
1716 }
1717 release_stripe(nsh);
1718 }
1719 /* critical section pass, GFP_NOIO no longer needed */
1720
1721 conf->slab_cache = sc;
1722 conf->active_name = 1-conf->active_name;
1723 conf->pool_size = newsize;
1724 return err;
1725}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
NeilBrownd1688a62011-10-11 16:49:52 +11001727static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 struct stripe_head *sh;
1730
NeilBrown3f294f42005-11-08 21:39:25 -08001731 spin_lock_irq(&conf->device_lock);
1732 sh = get_free_stripe(conf);
1733 spin_unlock_irq(&conf->device_lock);
1734 if (!sh)
1735 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001736 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001737 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001738 kmem_cache_free(conf->slab_cache, sh);
1739 atomic_dec(&conf->active_stripes);
1740 return 1;
1741}
1742
NeilBrownd1688a62011-10-11 16:49:52 +11001743static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001744{
1745 while (drop_one_stripe(conf))
1746 ;
1747
NeilBrown29fc7e32006-02-03 03:03:41 -08001748 if (conf->slab_cache)
1749 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 conf->slab_cache = NULL;
1751}
1752
NeilBrown6712ecf2007-09-27 12:47:43 +02001753static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754{
NeilBrown99c0fb52009-03-31 14:39:38 +11001755 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001756 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001757 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001759 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001760 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001761 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 for (i=0 ; i<disks; i++)
1764 if (bi == &sh->dev[i].req)
1765 break;
1766
Dan Williams45b42332007-07-09 11:56:43 -07001767 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1768 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 uptodate);
1770 if (i == disks) {
1771 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001772 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
NeilBrown14a75d32011-12-23 10:17:52 +11001774 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001775 /* If replacement finished while this request was outstanding,
1776 * 'replacement' might be NULL already.
1777 * In that case it moved down to 'rdev'.
1778 * rdev is not removed until all requests are finished.
1779 */
NeilBrown14a75d32011-12-23 10:17:52 +11001780 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001781 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001782 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
NeilBrown05616be2012-05-21 09:27:00 +10001784 if (use_new_offset(conf, sh))
1785 s = sh->sector + rdev->new_data_offset;
1786 else
1787 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001790 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001791 /* Note that this cannot happen on a
1792 * replacement device. We just fail those on
1793 * any error
1794 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001795 printk_ratelimited(
1796 KERN_INFO
1797 "md/raid:%s: read error corrected"
1798 " (%lu sectors at %llu on %s)\n",
1799 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001800 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001801 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001802 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001803 clear_bit(R5_ReadError, &sh->dev[i].flags);
1804 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001805 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1806 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1807
NeilBrown14a75d32011-12-23 10:17:52 +11001808 if (atomic_read(&rdev->read_errors))
1809 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001811 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001812 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001813 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001816 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001817 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1818 printk_ratelimited(
1819 KERN_WARNING
1820 "md/raid:%s: read error on replacement device "
1821 "(sector %llu on %s).\n",
1822 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001823 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001824 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001825 else if (conf->mddev->degraded >= conf->max_degraded) {
1826 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001827 printk_ratelimited(
1828 KERN_WARNING
1829 "md/raid:%s: read error not correctable "
1830 "(sector %llu on %s).\n",
1831 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001832 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001833 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001834 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001835 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001836 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001837 printk_ratelimited(
1838 KERN_WARNING
1839 "md/raid:%s: read error NOT corrected!! "
1840 "(sector %llu on %s).\n",
1841 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001842 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001843 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001844 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001845 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001846 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001847 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001848 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001849 else
1850 retry = 1;
1851 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001852 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1853 set_bit(R5_ReadError, &sh->dev[i].flags);
1854 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1855 } else
1856 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001857 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001858 clear_bit(R5_ReadError, &sh->dev[i].flags);
1859 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001860 if (!(set_bad
1861 && test_bit(In_sync, &rdev->flags)
1862 && rdev_set_badblocks(
1863 rdev, sh->sector, STRIPE_SECTORS, 0)))
1864 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
NeilBrown14a75d32011-12-23 10:17:52 +11001867 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1869 set_bit(STRIPE_HANDLE, &sh->state);
1870 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872
NeilBrownd710e132008-10-13 11:55:12 +11001873static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
NeilBrown99c0fb52009-03-31 14:39:38 +11001875 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001876 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001877 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001878 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001880 sector_t first_bad;
1881 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001882 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
NeilBrown977df362011-12-23 10:17:53 +11001884 for (i = 0 ; i < disks; i++) {
1885 if (bi == &sh->dev[i].req) {
1886 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 break;
NeilBrown977df362011-12-23 10:17:53 +11001888 }
1889 if (bi == &sh->dev[i].rreq) {
1890 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001891 if (rdev)
1892 replacement = 1;
1893 else
1894 /* rdev was removed and 'replacement'
1895 * replaced it. rdev is not removed
1896 * until all requests are finished.
1897 */
1898 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001899 break;
1900 }
1901 }
Dan Williams45b42332007-07-09 11:56:43 -07001902 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1904 uptodate);
1905 if (i == disks) {
1906 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001907 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
1909
NeilBrown977df362011-12-23 10:17:53 +11001910 if (replacement) {
1911 if (!uptodate)
1912 md_error(conf->mddev, rdev);
1913 else if (is_badblock(rdev, sh->sector,
1914 STRIPE_SECTORS,
1915 &first_bad, &bad_sectors))
1916 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1917 } else {
1918 if (!uptodate) {
1919 set_bit(WriteErrorSeen, &rdev->flags);
1920 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001921 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1922 set_bit(MD_RECOVERY_NEEDED,
1923 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001924 } else if (is_badblock(rdev, sh->sector,
1925 STRIPE_SECTORS,
1926 &first_bad, &bad_sectors))
1927 set_bit(R5_MadeGood, &sh->dev[i].flags);
1928 }
1929 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
NeilBrown977df362011-12-23 10:17:53 +11001931 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1932 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001934 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935}
1936
NeilBrown784052e2009-03-31 15:19:07 +11001937static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
NeilBrown784052e2009-03-31 15:19:07 +11001939static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
1941 struct r5dev *dev = &sh->dev[i];
1942
1943 bio_init(&dev->req);
1944 dev->req.bi_io_vec = &dev->vec;
1945 dev->req.bi_vcnt++;
1946 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001948 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
NeilBrown977df362011-12-23 10:17:53 +11001950 bio_init(&dev->rreq);
1951 dev->rreq.bi_io_vec = &dev->rvec;
1952 dev->rreq.bi_vcnt++;
1953 dev->rreq.bi_max_vecs++;
1954 dev->rreq.bi_private = sh;
1955 dev->rvec.bv_page = dev->page;
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001958 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
NeilBrownfd01b882011-10-11 16:47:53 +11001961static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001964 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001965 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001966 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
NeilBrown908f4fb2011-12-23 10:17:50 +11001968 spin_lock_irqsave(&conf->device_lock, flags);
1969 clear_bit(In_sync, &rdev->flags);
1970 mddev->degraded = calc_degraded(conf);
1971 spin_unlock_irqrestore(&conf->device_lock, flags);
1972 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1973
NeilBrownde393cd2011-07-28 11:31:48 +10001974 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001975 set_bit(Faulty, &rdev->flags);
1976 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1977 printk(KERN_ALERT
1978 "md/raid:%s: Disk failure on %s, disabling device.\n"
1979 "md/raid:%s: Operation continuing on %d devices.\n",
1980 mdname(mddev),
1981 bdevname(rdev->bdev, b),
1982 mdname(mddev),
1983 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001984}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986/*
1987 * Input: a 'big' sector number,
1988 * Output: index of the data and parity disk, and the sector # in them.
1989 */
NeilBrownd1688a62011-10-11 16:49:52 +11001990static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001991 int previous, int *dd_idx,
1992 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001994 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001995 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001997 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001998 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002000 int algorithm = previous ? conf->prev_algo
2001 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002002 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2003 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002004 int raid_disks = previous ? conf->previous_raid_disks
2005 : conf->raid_disks;
2006 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 /* First compute the information on this sector */
2009
2010 /*
2011 * Compute the chunk number and the sector offset inside the chunk
2012 */
2013 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2014 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 /*
2017 * Compute the stripe number
2018 */
NeilBrown35f2a592010-04-20 14:13:34 +10002019 stripe = chunk_number;
2020 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002021 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 /*
2023 * Select the parity disk based on the user selected algorithm.
2024 */
NeilBrown84789552011-07-27 11:00:36 +10002025 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002026 switch(conf->level) {
2027 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002028 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002029 break;
2030 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002031 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002033 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002034 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 (*dd_idx)++;
2036 break;
2037 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002038 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002039 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 (*dd_idx)++;
2041 break;
2042 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002043 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002044 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 break;
2046 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002047 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002048 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002050 case ALGORITHM_PARITY_0:
2051 pd_idx = 0;
2052 (*dd_idx)++;
2053 break;
2054 case ALGORITHM_PARITY_N:
2055 pd_idx = data_disks;
2056 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002058 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002059 }
2060 break;
2061 case 6:
2062
NeilBrowne183eae2009-03-31 15:20:22 +11002063 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002064 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002065 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002066 qd_idx = pd_idx + 1;
2067 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002068 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002069 qd_idx = 0;
2070 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002071 (*dd_idx) += 2; /* D D P Q D */
2072 break;
2073 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002074 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002075 qd_idx = pd_idx + 1;
2076 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002077 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002078 qd_idx = 0;
2079 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002080 (*dd_idx) += 2; /* D D P Q D */
2081 break;
2082 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002083 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002084 qd_idx = (pd_idx + 1) % raid_disks;
2085 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002086 break;
2087 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002088 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002089 qd_idx = (pd_idx + 1) % raid_disks;
2090 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002091 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002092
2093 case ALGORITHM_PARITY_0:
2094 pd_idx = 0;
2095 qd_idx = 1;
2096 (*dd_idx) += 2;
2097 break;
2098 case ALGORITHM_PARITY_N:
2099 pd_idx = data_disks;
2100 qd_idx = data_disks + 1;
2101 break;
2102
2103 case ALGORITHM_ROTATING_ZERO_RESTART:
2104 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2105 * of blocks for computing Q is different.
2106 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002107 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002108 qd_idx = pd_idx + 1;
2109 if (pd_idx == raid_disks-1) {
2110 (*dd_idx)++; /* Q D D D P */
2111 qd_idx = 0;
2112 } else if (*dd_idx >= pd_idx)
2113 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002114 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002115 break;
2116
2117 case ALGORITHM_ROTATING_N_RESTART:
2118 /* Same a left_asymmetric, by first stripe is
2119 * D D D P Q rather than
2120 * Q D D D P
2121 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002122 stripe2 += 1;
2123 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002124 qd_idx = pd_idx + 1;
2125 if (pd_idx == raid_disks-1) {
2126 (*dd_idx)++; /* Q D D D P */
2127 qd_idx = 0;
2128 } else if (*dd_idx >= pd_idx)
2129 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002130 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002131 break;
2132
2133 case ALGORITHM_ROTATING_N_CONTINUE:
2134 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002135 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002136 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2137 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002138 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002139 break;
2140
2141 case ALGORITHM_LEFT_ASYMMETRIC_6:
2142 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002143 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002144 if (*dd_idx >= pd_idx)
2145 (*dd_idx)++;
2146 qd_idx = raid_disks - 1;
2147 break;
2148
2149 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002150 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002151 if (*dd_idx >= pd_idx)
2152 (*dd_idx)++;
2153 qd_idx = raid_disks - 1;
2154 break;
2155
2156 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002157 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002158 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2159 qd_idx = raid_disks - 1;
2160 break;
2161
2162 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002163 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002164 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2165 qd_idx = raid_disks - 1;
2166 break;
2167
2168 case ALGORITHM_PARITY_0_6:
2169 pd_idx = 0;
2170 (*dd_idx)++;
2171 qd_idx = raid_disks - 1;
2172 break;
2173
NeilBrown16a53ec2006-06-26 00:27:38 -07002174 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002175 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002176 }
2177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 }
2179
NeilBrown911d4ee2009-03-31 14:39:38 +11002180 if (sh) {
2181 sh->pd_idx = pd_idx;
2182 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002183 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 /*
2186 * Finally, compute the new sector number
2187 */
2188 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2189 return new_sector;
2190}
2191
2192
NeilBrown784052e2009-03-31 15:19:07 +11002193static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
NeilBrownd1688a62011-10-11 16:49:52 +11002195 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002196 int raid_disks = sh->disks;
2197 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002199 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2200 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002201 int algorithm = previous ? conf->prev_algo
2202 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 sector_t stripe;
2204 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002205 sector_t chunk_number;
2206 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002208 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
NeilBrown16a53ec2006-06-26 00:27:38 -07002210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2212 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
NeilBrown16a53ec2006-06-26 00:27:38 -07002214 if (i == sh->pd_idx)
2215 return 0;
2216 switch(conf->level) {
2217 case 4: break;
2218 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002219 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 case ALGORITHM_LEFT_ASYMMETRIC:
2221 case ALGORITHM_RIGHT_ASYMMETRIC:
2222 if (i > sh->pd_idx)
2223 i--;
2224 break;
2225 case ALGORITHM_LEFT_SYMMETRIC:
2226 case ALGORITHM_RIGHT_SYMMETRIC:
2227 if (i < sh->pd_idx)
2228 i += raid_disks;
2229 i -= (sh->pd_idx + 1);
2230 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002231 case ALGORITHM_PARITY_0:
2232 i -= 1;
2233 break;
2234 case ALGORITHM_PARITY_N:
2235 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002237 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002238 }
2239 break;
2240 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002241 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002242 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002243 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002244 case ALGORITHM_LEFT_ASYMMETRIC:
2245 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002246 case ALGORITHM_ROTATING_ZERO_RESTART:
2247 case ALGORITHM_ROTATING_N_RESTART:
2248 if (sh->pd_idx == raid_disks-1)
2249 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002250 else if (i > sh->pd_idx)
2251 i -= 2; /* D D P Q D */
2252 break;
2253 case ALGORITHM_LEFT_SYMMETRIC:
2254 case ALGORITHM_RIGHT_SYMMETRIC:
2255 if (sh->pd_idx == raid_disks-1)
2256 i--; /* Q D D D P */
2257 else {
2258 /* D D P Q D */
2259 if (i < sh->pd_idx)
2260 i += raid_disks;
2261 i -= (sh->pd_idx + 2);
2262 }
2263 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002264 case ALGORITHM_PARITY_0:
2265 i -= 2;
2266 break;
2267 case ALGORITHM_PARITY_N:
2268 break;
2269 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002270 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002271 if (sh->pd_idx == 0)
2272 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002273 else {
2274 /* D D Q P D */
2275 if (i < sh->pd_idx)
2276 i += raid_disks;
2277 i -= (sh->pd_idx + 1);
2278 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002279 break;
2280 case ALGORITHM_LEFT_ASYMMETRIC_6:
2281 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2282 if (i > sh->pd_idx)
2283 i--;
2284 break;
2285 case ALGORITHM_LEFT_SYMMETRIC_6:
2286 case ALGORITHM_RIGHT_SYMMETRIC_6:
2287 if (i < sh->pd_idx)
2288 i += data_disks + 1;
2289 i -= (sh->pd_idx + 1);
2290 break;
2291 case ALGORITHM_PARITY_0_6:
2292 i -= 1;
2293 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002294 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002295 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002296 }
2297 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 }
2299
2300 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002301 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
NeilBrown112bf892009-03-31 14:39:38 +11002303 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002304 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002305 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2306 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002307 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2308 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return 0;
2310 }
2311 return r_sector;
2312}
2313
2314
Dan Williams600aa102008-06-28 08:32:05 +10002315static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002316schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002317 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002318{
2319 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002320 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002321 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002322
Dan Williamse33129d2007-01-02 13:52:30 -07002323 if (rcw) {
2324 /* if we are not expanding this is a proper write request, and
2325 * there will be bios with new data to be drained into the
2326 * stripe cache
2327 */
2328 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002329 sh->reconstruct_state = reconstruct_state_drain_run;
2330 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2331 } else
2332 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002333
Dan Williamsac6b53b2009-07-14 13:40:19 -07002334 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002335
2336 for (i = disks; i--; ) {
2337 struct r5dev *dev = &sh->dev[i];
2338
2339 if (dev->towrite) {
2340 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002341 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002342 if (!expand)
2343 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002344 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002345 }
2346 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002347 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002348 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002349 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002350 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002351 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002352 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2353 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2354
Dan Williamsd8ee0722008-06-28 08:32:06 +10002355 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002356 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2357 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002358 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002359
2360 for (i = disks; i--; ) {
2361 struct r5dev *dev = &sh->dev[i];
2362 if (i == pd_idx)
2363 continue;
2364
Dan Williamse33129d2007-01-02 13:52:30 -07002365 if (dev->towrite &&
2366 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002367 test_bit(R5_Wantcompute, &dev->flags))) {
2368 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002369 set_bit(R5_LOCKED, &dev->flags);
2370 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002371 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002372 }
2373 }
2374 }
2375
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002376 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002377 * are in flight
2378 */
2379 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2380 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002381 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002382
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002383 if (level == 6) {
2384 int qd_idx = sh->qd_idx;
2385 struct r5dev *dev = &sh->dev[qd_idx];
2386
2387 set_bit(R5_LOCKED, &dev->flags);
2388 clear_bit(R5_UPTODATE, &dev->flags);
2389 s->locked++;
2390 }
2391
Dan Williams600aa102008-06-28 08:32:05 +10002392 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002393 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002394 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002395}
NeilBrown16a53ec2006-06-26 00:27:38 -07002396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397/*
2398 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002399 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 * The bi_next chain must be in order.
2401 */
2402static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2403{
2404 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002405 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002406 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
NeilBrowncbe47ec2011-07-26 11:20:35 +10002408 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 (unsigned long long)bi->bi_sector,
2410 (unsigned long long)sh->sector);
2411
Shaohua Lib17459c2012-07-19 16:01:31 +10002412 /*
2413 * If several bio share a stripe. The bio bi_phys_segments acts as a
2414 * reference count to avoid race. The reference count should already be
2415 * increased before this function is called (for example, in
2416 * make_request()), so other bio sharing this stripe will not free the
2417 * stripe. If a stripe is owned by one stripe, the stripe lock will
2418 * protect it.
2419 */
2420 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002421 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002423 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002424 firstwrite = 1;
2425 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 bip = &sh->dev[dd_idx].toread;
2427 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2428 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2429 goto overlap;
2430 bip = & (*bip)->bi_next;
2431 }
2432 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2433 goto overlap;
2434
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002435 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (*bip)
2437 bi->bi_next = *bip;
2438 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002439 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if (forwrite) {
2442 /* check if page is covered */
2443 sector_t sector = sh->dev[dd_idx].sector;
2444 for (bi=sh->dev[dd_idx].towrite;
2445 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2446 bi && bi->bi_sector <= sector;
2447 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2448 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2449 sector = bi->bi_sector + (bi->bi_size>>9);
2450 }
2451 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2452 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2453 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002454
2455 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2456 (unsigned long long)(*bip)->bi_sector,
2457 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002458 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002459
2460 if (conf->mddev->bitmap && firstwrite) {
2461 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2462 STRIPE_SECTORS, 0);
2463 sh->bm_seq = conf->seq_flush+1;
2464 set_bit(STRIPE_BIT_DELAY, &sh->state);
2465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return 1;
2467
2468 overlap:
2469 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002470 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 return 0;
2472}
2473
NeilBrownd1688a62011-10-11 16:49:52 +11002474static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002475
NeilBrownd1688a62011-10-11 16:49:52 +11002476static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002477 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002478{
NeilBrown784052e2009-03-31 15:19:07 +11002479 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002480 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002481 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002482 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002483 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002484
NeilBrown112bf892009-03-31 14:39:38 +11002485 raid5_compute_sector(conf,
2486 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002487 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002488 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002489 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002490}
2491
Dan Williamsa4456852007-07-09 11:56:43 -07002492static void
NeilBrownd1688a62011-10-11 16:49:52 +11002493handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002494 struct stripe_head_state *s, int disks,
2495 struct bio **return_bi)
2496{
2497 int i;
2498 for (i = disks; i--; ) {
2499 struct bio *bi;
2500 int bitmap_end = 0;
2501
2502 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002503 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002504 rcu_read_lock();
2505 rdev = rcu_dereference(conf->disks[i].rdev);
2506 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002507 atomic_inc(&rdev->nr_pending);
2508 else
2509 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002510 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002511 if (rdev) {
2512 if (!rdev_set_badblocks(
2513 rdev,
2514 sh->sector,
2515 STRIPE_SECTORS, 0))
2516 md_error(conf->mddev, rdev);
2517 rdev_dec_pending(rdev, conf->mddev);
2518 }
Dan Williamsa4456852007-07-09 11:56:43 -07002519 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002520 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002521 /* fail all writes first */
2522 bi = sh->dev[i].towrite;
2523 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002524 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002525 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002526 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002527
2528 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2529 wake_up(&conf->wait_for_overlap);
2530
2531 while (bi && bi->bi_sector <
2532 sh->dev[i].sector + STRIPE_SECTORS) {
2533 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2534 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002535 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002536 md_write_end(conf->mddev);
2537 bi->bi_next = *return_bi;
2538 *return_bi = bi;
2539 }
2540 bi = nextbi;
2541 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002542 if (bitmap_end)
2543 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2544 STRIPE_SECTORS, 0, 0);
2545 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002546 /* and fail all 'written' */
2547 bi = sh->dev[i].written;
2548 sh->dev[i].written = NULL;
2549 if (bi) bitmap_end = 1;
2550 while (bi && bi->bi_sector <
2551 sh->dev[i].sector + STRIPE_SECTORS) {
2552 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2553 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002554 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002555 md_write_end(conf->mddev);
2556 bi->bi_next = *return_bi;
2557 *return_bi = bi;
2558 }
2559 bi = bi2;
2560 }
2561
Dan Williamsb5e98d62007-01-02 13:52:31 -07002562 /* fail any reads if this device is non-operational and
2563 * the data has not reached the cache yet.
2564 */
2565 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2566 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2567 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002568 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002569 bi = sh->dev[i].toread;
2570 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002571 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002572 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2573 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002574 while (bi && bi->bi_sector <
2575 sh->dev[i].sector + STRIPE_SECTORS) {
2576 struct bio *nextbi =
2577 r5_next_bio(bi, sh->dev[i].sector);
2578 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002579 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002580 bi->bi_next = *return_bi;
2581 *return_bi = bi;
2582 }
2583 bi = nextbi;
2584 }
2585 }
Dan Williamsa4456852007-07-09 11:56:43 -07002586 if (bitmap_end)
2587 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2588 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002589 /* If we were in the middle of a write the parity block might
2590 * still be locked - so just clear all R5_LOCKED flags
2591 */
2592 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002593 }
2594
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002595 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2596 if (atomic_dec_and_test(&conf->pending_full_writes))
2597 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002598}
2599
NeilBrown7f0da592011-07-28 11:39:22 +10002600static void
NeilBrownd1688a62011-10-11 16:49:52 +11002601handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002602 struct stripe_head_state *s)
2603{
2604 int abort = 0;
2605 int i;
2606
NeilBrown7f0da592011-07-28 11:39:22 +10002607 clear_bit(STRIPE_SYNCING, &sh->state);
2608 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002609 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002610 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002611 * Don't even need to abort as that is handled elsewhere
2612 * if needed, and not always wanted e.g. if there is a known
2613 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002614 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002615 * non-sync devices, or abort the recovery
2616 */
NeilBrown18b98372012-04-01 23:48:38 +10002617 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2618 /* During recovery devices cannot be removed, so
2619 * locking and refcounting of rdevs is not needed
2620 */
2621 for (i = 0; i < conf->raid_disks; i++) {
2622 struct md_rdev *rdev = conf->disks[i].rdev;
2623 if (rdev
2624 && !test_bit(Faulty, &rdev->flags)
2625 && !test_bit(In_sync, &rdev->flags)
2626 && !rdev_set_badblocks(rdev, sh->sector,
2627 STRIPE_SECTORS, 0))
2628 abort = 1;
2629 rdev = conf->disks[i].replacement;
2630 if (rdev
2631 && !test_bit(Faulty, &rdev->flags)
2632 && !test_bit(In_sync, &rdev->flags)
2633 && !rdev_set_badblocks(rdev, sh->sector,
2634 STRIPE_SECTORS, 0))
2635 abort = 1;
2636 }
2637 if (abort)
2638 conf->recovery_disabled =
2639 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002640 }
NeilBrown18b98372012-04-01 23:48:38 +10002641 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002642}
2643
NeilBrown9a3e1102011-12-23 10:17:53 +11002644static int want_replace(struct stripe_head *sh, int disk_idx)
2645{
2646 struct md_rdev *rdev;
2647 int rv = 0;
2648 /* Doing recovery so rcu locking not required */
2649 rdev = sh->raid_conf->disks[disk_idx].replacement;
2650 if (rdev
2651 && !test_bit(Faulty, &rdev->flags)
2652 && !test_bit(In_sync, &rdev->flags)
2653 && (rdev->recovery_offset <= sh->sector
2654 || rdev->mddev->recovery_cp <= sh->sector))
2655 rv = 1;
2656
2657 return rv;
2658}
2659
NeilBrown93b3dbc2011-07-27 11:00:36 +10002660/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002661 * to be read or computed to satisfy a request.
2662 *
2663 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002664 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002665 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002666static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2667 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002668{
2669 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002670 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2671 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002672
Dan Williamsf38e1212007-01-02 13:52:30 -07002673 /* is the data in this block needed, and can we get it? */
2674 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002675 !test_bit(R5_UPTODATE, &dev->flags) &&
2676 (dev->toread ||
2677 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2678 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002679 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002680 (s->failed >= 1 && fdev[0]->toread) ||
2681 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002682 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2683 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2684 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002685 /* we would like to get this block, possibly by computing it,
2686 * otherwise read it if the backing disk is insync
2687 */
2688 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2689 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2690 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002691 (s->failed && (disk_idx == s->failed_num[0] ||
2692 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002693 /* have disk failed, and we're requested to fetch it;
2694 * do compute it
2695 */
2696 pr_debug("Computing stripe %llu block %d\n",
2697 (unsigned long long)sh->sector, disk_idx);
2698 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2699 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2700 set_bit(R5_Wantcompute, &dev->flags);
2701 sh->ops.target = disk_idx;
2702 sh->ops.target2 = -1; /* no 2nd target */
2703 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002704 /* Careful: from this point on 'uptodate' is in the eye
2705 * of raid_run_ops which services 'compute' operations
2706 * before writes. R5_Wantcompute flags a block that will
2707 * be R5_UPTODATE by the time it is needed for a
2708 * subsequent operation.
2709 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002710 s->uptodate++;
2711 return 1;
2712 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2713 /* Computing 2-failure is *very* expensive; only
2714 * do it if failed >= 2
2715 */
2716 int other;
2717 for (other = disks; other--; ) {
2718 if (other == disk_idx)
2719 continue;
2720 if (!test_bit(R5_UPTODATE,
2721 &sh->dev[other].flags))
2722 break;
2723 }
2724 BUG_ON(other < 0);
2725 pr_debug("Computing stripe %llu blocks %d,%d\n",
2726 (unsigned long long)sh->sector,
2727 disk_idx, other);
2728 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2729 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2730 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2731 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2732 sh->ops.target = disk_idx;
2733 sh->ops.target2 = other;
2734 s->uptodate += 2;
2735 s->req_compute = 1;
2736 return 1;
2737 } else if (test_bit(R5_Insync, &dev->flags)) {
2738 set_bit(R5_LOCKED, &dev->flags);
2739 set_bit(R5_Wantread, &dev->flags);
2740 s->locked++;
2741 pr_debug("Reading block %d (sync=%d)\n",
2742 disk_idx, s->syncing);
2743 }
2744 }
2745
2746 return 0;
2747}
2748
2749/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002750 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002751 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002752static void handle_stripe_fill(struct stripe_head *sh,
2753 struct stripe_head_state *s,
2754 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002755{
2756 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002757
2758 /* look for blocks to read/compute, skip this if a compute
2759 * is already in flight, or if the stripe contents are in the
2760 * midst of changing due to a write
2761 */
2762 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2763 !sh->reconstruct_state)
2764 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002765 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002766 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002767 set_bit(STRIPE_HANDLE, &sh->state);
2768}
2769
2770
Dan Williams1fe797e2008-06-28 09:16:30 +10002771/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002772 * any written block on an uptodate or failed drive can be returned.
2773 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2774 * never LOCKED, so we don't need to test 'failed' directly.
2775 */
NeilBrownd1688a62011-10-11 16:49:52 +11002776static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002777 struct stripe_head *sh, int disks, struct bio **return_bi)
2778{
2779 int i;
2780 struct r5dev *dev;
2781
2782 for (i = disks; i--; )
2783 if (sh->dev[i].written) {
2784 dev = &sh->dev[i];
2785 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11002786 (test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownca64cae2012-11-21 16:33:40 +11002787 test_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002788 /* We can return any write requests */
2789 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002790 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11002791 if (test_and_clear_bit(R5_Discard, &dev->flags))
2792 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002793 wbi = dev->written;
2794 dev->written = NULL;
2795 while (wbi && wbi->bi_sector <
2796 dev->sector + STRIPE_SECTORS) {
2797 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002798 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002799 md_write_end(conf->mddev);
2800 wbi->bi_next = *return_bi;
2801 *return_bi = wbi;
2802 }
2803 wbi = wbi2;
2804 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002805 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2806 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002807 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002808 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002809 }
NeilBrownca64cae2012-11-21 16:33:40 +11002810 } else if (test_bit(R5_Discard, &sh->dev[i].flags))
2811 clear_bit(R5_Discard, &sh->dev[i].flags);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002812
2813 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2814 if (atomic_dec_and_test(&conf->pending_full_writes))
2815 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002816}
2817
NeilBrownd1688a62011-10-11 16:49:52 +11002818static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002819 struct stripe_head *sh,
2820 struct stripe_head_state *s,
2821 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002822{
2823 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002824 sector_t recovery_cp = conf->mddev->recovery_cp;
2825
2826 /* RAID6 requires 'rcw' in current implementation.
2827 * Otherwise, check whether resync is now happening or should start.
2828 * If yes, then the array is dirty (after unclean shutdown or
2829 * initial creation), so parity in some stripes might be inconsistent.
2830 * In this case, we need to always do reconstruct-write, to ensure
2831 * that in case of drive failure or read-error correction, we
2832 * generate correct data from the parity.
2833 */
2834 if (conf->max_degraded == 2 ||
2835 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2836 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002837 * look like rcw is cheaper
2838 */
2839 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002840 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2841 conf->max_degraded, (unsigned long long)recovery_cp,
2842 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002843 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002844 /* would I have to read this buffer for read_modify_write */
2845 struct r5dev *dev = &sh->dev[i];
2846 if ((dev->towrite || i == sh->pd_idx) &&
2847 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002848 !(test_bit(R5_UPTODATE, &dev->flags) ||
2849 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002850 if (test_bit(R5_Insync, &dev->flags))
2851 rmw++;
2852 else
2853 rmw += 2*disks; /* cannot read it */
2854 }
2855 /* Would I have to read this buffer for reconstruct_write */
2856 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2857 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002858 !(test_bit(R5_UPTODATE, &dev->flags) ||
2859 test_bit(R5_Wantcompute, &dev->flags))) {
2860 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002861 else
2862 rcw += 2*disks;
2863 }
2864 }
Dan Williams45b42332007-07-09 11:56:43 -07002865 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002866 (unsigned long long)sh->sector, rmw, rcw);
2867 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11002868 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002869 /* prefer read-modify-write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002870 blk_add_trace_msg(conf->mddev->queue, "raid5 rmw %llu %d",
2871 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07002872 for (i = disks; i--; ) {
2873 struct r5dev *dev = &sh->dev[i];
2874 if ((dev->towrite || i == sh->pd_idx) &&
2875 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002876 !(test_bit(R5_UPTODATE, &dev->flags) ||
2877 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002878 test_bit(R5_Insync, &dev->flags)) {
2879 if (
2880 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002881 pr_debug("Read_old block "
NeilBrowna9add5d2012-10-31 11:59:09 +11002882 "%d for r-m-w\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002883 set_bit(R5_LOCKED, &dev->flags);
2884 set_bit(R5_Wantread, &dev->flags);
2885 s->locked++;
2886 } else {
2887 set_bit(STRIPE_DELAYED, &sh->state);
2888 set_bit(STRIPE_HANDLE, &sh->state);
2889 }
2890 }
2891 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002892 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002893 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002894 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002895 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10002896 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002897 for (i = disks; i--; ) {
2898 struct r5dev *dev = &sh->dev[i];
2899 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002900 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002901 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002902 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002903 test_bit(R5_Wantcompute, &dev->flags))) {
2904 rcw++;
2905 if (!test_bit(R5_Insync, &dev->flags))
2906 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002907 if (
2908 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002909 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002910 "%d for Reconstruct\n", i);
2911 set_bit(R5_LOCKED, &dev->flags);
2912 set_bit(R5_Wantread, &dev->flags);
2913 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11002914 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07002915 } else {
2916 set_bit(STRIPE_DELAYED, &sh->state);
2917 set_bit(STRIPE_HANDLE, &sh->state);
2918 }
2919 }
2920 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002921 if (rcw)
2922 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
2923 (unsigned long long)sh->sector,
2924 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10002925 }
Dan Williamsa4456852007-07-09 11:56:43 -07002926 /* now if nothing is locked, and if we have enough data,
2927 * we can start a write request
2928 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002929 /* since handle_stripe can be called at any time we need to handle the
2930 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002931 * subsequent call wants to start a write request. raid_run_ops only
2932 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002933 * simultaneously. If this is not the case then new writes need to be
2934 * held off until the compute completes.
2935 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002936 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2937 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2938 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002939 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002940}
2941
NeilBrownd1688a62011-10-11 16:49:52 +11002942static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002943 struct stripe_head_state *s, int disks)
2944{
Dan Williamsecc65c92008-06-28 08:31:57 +10002945 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002946
Dan Williamsbd2ab672008-04-10 21:29:27 -07002947 set_bit(STRIPE_HANDLE, &sh->state);
2948
Dan Williamsecc65c92008-06-28 08:31:57 +10002949 switch (sh->check_state) {
2950 case check_state_idle:
2951 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002952 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002953 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002954 sh->check_state = check_state_run;
2955 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002956 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002957 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002958 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002959 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002960 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002961 /* fall through */
2962 case check_state_compute_result:
2963 sh->check_state = check_state_idle;
2964 if (!dev)
2965 dev = &sh->dev[sh->pd_idx];
2966
2967 /* check that a write has not made the stripe insync */
2968 if (test_bit(STRIPE_INSYNC, &sh->state))
2969 break;
2970
2971 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002972 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2973 BUG_ON(s->uptodate != disks);
2974
2975 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002976 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002977 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002978
Dan Williamsa4456852007-07-09 11:56:43 -07002979 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002980 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002981 break;
2982 case check_state_run:
2983 break; /* we will be called again upon completion */
2984 case check_state_check_result:
2985 sh->check_state = check_state_idle;
2986
2987 /* if a failure occurred during the check operation, leave
2988 * STRIPE_INSYNC not set and let the stripe be handled again
2989 */
2990 if (s->failed)
2991 break;
2992
2993 /* handle a successful check operation, if parity is correct
2994 * we are done. Otherwise update the mismatch count and repair
2995 * parity if !MD_RECOVERY_CHECK
2996 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002997 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002998 /* parity is correct (on disc,
2999 * not in buffer any more)
3000 */
3001 set_bit(STRIPE_INSYNC, &sh->state);
3002 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003003 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003004 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3005 /* don't try to repair!! */
3006 set_bit(STRIPE_INSYNC, &sh->state);
3007 else {
3008 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003009 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003010 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3011 set_bit(R5_Wantcompute,
3012 &sh->dev[sh->pd_idx].flags);
3013 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003014 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003015 s->uptodate++;
3016 }
3017 }
3018 break;
3019 case check_state_compute_run:
3020 break;
3021 default:
3022 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3023 __func__, sh->check_state,
3024 (unsigned long long) sh->sector);
3025 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003026 }
3027}
3028
3029
NeilBrownd1688a62011-10-11 16:49:52 +11003030static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003031 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003032 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003033{
Dan Williamsa4456852007-07-09 11:56:43 -07003034 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003035 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003036 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003037
3038 set_bit(STRIPE_HANDLE, &sh->state);
3039
3040 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003041
Dan Williamsa4456852007-07-09 11:56:43 -07003042 /* Want to check and possibly repair P and Q.
3043 * However there could be one 'failed' device, in which
3044 * case we can only check one of them, possibly using the
3045 * other to generate missing data
3046 */
3047
Dan Williamsd82dfee2009-07-14 13:40:57 -07003048 switch (sh->check_state) {
3049 case check_state_idle:
3050 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003051 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003052 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003053 * makes sense to check P (If anything else were failed,
3054 * we would have used P to recreate it).
3055 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003056 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003057 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003058 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003059 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003060 * anything, so it makes sense to check it
3061 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003062 if (sh->check_state == check_state_run)
3063 sh->check_state = check_state_run_pq;
3064 else
3065 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003066 }
Dan Williams36d1c642009-07-14 11:48:22 -07003067
Dan Williamsd82dfee2009-07-14 13:40:57 -07003068 /* discard potentially stale zero_sum_result */
3069 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003070
Dan Williamsd82dfee2009-07-14 13:40:57 -07003071 if (sh->check_state == check_state_run) {
3072 /* async_xor_zero_sum destroys the contents of P */
3073 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3074 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003075 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003076 if (sh->check_state >= check_state_run &&
3077 sh->check_state <= check_state_run_pq) {
3078 /* async_syndrome_zero_sum preserves P and Q, so
3079 * no need to mark them !uptodate here
3080 */
3081 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3082 break;
3083 }
Dan Williams36d1c642009-07-14 11:48:22 -07003084
Dan Williamsd82dfee2009-07-14 13:40:57 -07003085 /* we have 2-disk failure */
3086 BUG_ON(s->failed != 2);
3087 /* fall through */
3088 case check_state_compute_result:
3089 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003090
Dan Williamsd82dfee2009-07-14 13:40:57 -07003091 /* check that a write has not made the stripe insync */
3092 if (test_bit(STRIPE_INSYNC, &sh->state))
3093 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003094
3095 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003096 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003097 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003098 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003099 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003100 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003101 s->locked++;
3102 set_bit(R5_LOCKED, &dev->flags);
3103 set_bit(R5_Wantwrite, &dev->flags);
3104 }
3105 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003106 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003107 s->locked++;
3108 set_bit(R5_LOCKED, &dev->flags);
3109 set_bit(R5_Wantwrite, &dev->flags);
3110 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003111 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003112 dev = &sh->dev[pd_idx];
3113 s->locked++;
3114 set_bit(R5_LOCKED, &dev->flags);
3115 set_bit(R5_Wantwrite, &dev->flags);
3116 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003117 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003118 dev = &sh->dev[qd_idx];
3119 s->locked++;
3120 set_bit(R5_LOCKED, &dev->flags);
3121 set_bit(R5_Wantwrite, &dev->flags);
3122 }
3123 clear_bit(STRIPE_DEGRADED, &sh->state);
3124
3125 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003126 break;
3127 case check_state_run:
3128 case check_state_run_q:
3129 case check_state_run_pq:
3130 break; /* we will be called again upon completion */
3131 case check_state_check_result:
3132 sh->check_state = check_state_idle;
3133
3134 /* handle a successful check operation, if parity is correct
3135 * we are done. Otherwise update the mismatch count and repair
3136 * parity if !MD_RECOVERY_CHECK
3137 */
3138 if (sh->ops.zero_sum_result == 0) {
3139 /* both parities are correct */
3140 if (!s->failed)
3141 set_bit(STRIPE_INSYNC, &sh->state);
3142 else {
3143 /* in contrast to the raid5 case we can validate
3144 * parity, but still have a failure to write
3145 * back
3146 */
3147 sh->check_state = check_state_compute_result;
3148 /* Returning at this point means that we may go
3149 * off and bring p and/or q uptodate again so
3150 * we make sure to check zero_sum_result again
3151 * to verify if p or q need writeback
3152 */
3153 }
3154 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003155 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003156 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3157 /* don't try to repair!! */
3158 set_bit(STRIPE_INSYNC, &sh->state);
3159 else {
3160 int *target = &sh->ops.target;
3161
3162 sh->ops.target = -1;
3163 sh->ops.target2 = -1;
3164 sh->check_state = check_state_compute_run;
3165 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3166 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3167 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3168 set_bit(R5_Wantcompute,
3169 &sh->dev[pd_idx].flags);
3170 *target = pd_idx;
3171 target = &sh->ops.target2;
3172 s->uptodate++;
3173 }
3174 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3175 set_bit(R5_Wantcompute,
3176 &sh->dev[qd_idx].flags);
3177 *target = qd_idx;
3178 s->uptodate++;
3179 }
3180 }
3181 }
3182 break;
3183 case check_state_compute_run:
3184 break;
3185 default:
3186 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3187 __func__, sh->check_state,
3188 (unsigned long long) sh->sector);
3189 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003190 }
3191}
3192
NeilBrownd1688a62011-10-11 16:49:52 +11003193static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003194{
3195 int i;
3196
3197 /* We have read all the blocks in this stripe and now we need to
3198 * copy some of them into a target stripe for expand.
3199 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003200 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003201 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3202 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003203 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003204 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003205 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003206 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003207
NeilBrown784052e2009-03-31 15:19:07 +11003208 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003209 sector_t s = raid5_compute_sector(conf, bn, 0,
3210 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003211 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003212 if (sh2 == NULL)
3213 /* so far only the early blocks of this stripe
3214 * have been requested. When later blocks
3215 * get requested, we will try again
3216 */
3217 continue;
3218 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3219 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3220 /* must have already done this block */
3221 release_stripe(sh2);
3222 continue;
3223 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003224
3225 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003226 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003227 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003228 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003229 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003230
Dan Williamsa4456852007-07-09 11:56:43 -07003231 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3232 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3233 for (j = 0; j < conf->raid_disks; j++)
3234 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003235 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003236 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3237 break;
3238 if (j == conf->raid_disks) {
3239 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3240 set_bit(STRIPE_HANDLE, &sh2->state);
3241 }
3242 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003243
Dan Williamsa4456852007-07-09 11:56:43 -07003244 }
NeilBrowna2e08552007-09-11 15:23:36 -07003245 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003246 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003247}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248
3249/*
3250 * handle_stripe - do things to a stripe.
3251 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003252 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3253 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003255 * return some read requests which now have data
3256 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 * schedule a read on some buffers
3258 * schedule a write of some buffers
3259 * return confirmation of parity correctness
3260 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 */
Dan Williamsa4456852007-07-09 11:56:43 -07003262
NeilBrownacfe7262011-07-27 11:00:36 +10003263static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003264{
NeilBrownd1688a62011-10-11 16:49:52 +11003265 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003266 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003267 struct r5dev *dev;
3268 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003269 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003270
NeilBrownacfe7262011-07-27 11:00:36 +10003271 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003272
NeilBrownacfe7262011-07-27 11:00:36 +10003273 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3274 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3275 s->failed_num[0] = -1;
3276 s->failed_num[1] = -1;
3277
3278 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003279 rcu_read_lock();
3280 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003281 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003282 sector_t first_bad;
3283 int bad_sectors;
3284 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003285
NeilBrown16a53ec2006-06-26 00:27:38 -07003286 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003287
Dan Williams45b42332007-07-09 11:56:43 -07003288 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003289 i, dev->flags,
3290 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003291 /* maybe we can reply to a read
3292 *
3293 * new wantfill requests are only permitted while
3294 * ops_complete_biofill is guaranteed to be inactive
3295 */
3296 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3297 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3298 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003299
3300 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003301 if (test_bit(R5_LOCKED, &dev->flags))
3302 s->locked++;
3303 if (test_bit(R5_UPTODATE, &dev->flags))
3304 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003305 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003306 s->compute++;
3307 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003308 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003309
NeilBrownacfe7262011-07-27 11:00:36 +10003310 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003311 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003312 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003313 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003314 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003315 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003316 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003317 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003318 }
Dan Williamsa4456852007-07-09 11:56:43 -07003319 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003320 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003321 /* Prefer to use the replacement for reads, but only
3322 * if it is recovered enough and has no bad blocks.
3323 */
3324 rdev = rcu_dereference(conf->disks[i].replacement);
3325 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3326 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3327 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3328 &first_bad, &bad_sectors))
3329 set_bit(R5_ReadRepl, &dev->flags);
3330 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003331 if (rdev)
3332 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003333 rdev = rcu_dereference(conf->disks[i].rdev);
3334 clear_bit(R5_ReadRepl, &dev->flags);
3335 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003336 if (rdev && test_bit(Faulty, &rdev->flags))
3337 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003338 if (rdev) {
3339 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3340 &first_bad, &bad_sectors);
3341 if (s->blocked_rdev == NULL
3342 && (test_bit(Blocked, &rdev->flags)
3343 || is_bad < 0)) {
3344 if (is_bad < 0)
3345 set_bit(BlockedBadBlocks,
3346 &rdev->flags);
3347 s->blocked_rdev = rdev;
3348 atomic_inc(&rdev->nr_pending);
3349 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003350 }
NeilBrown415e72d2010-06-17 17:25:21 +10003351 clear_bit(R5_Insync, &dev->flags);
3352 if (!rdev)
3353 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003354 else if (is_bad) {
3355 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003356 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3357 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003358 /* treat as in-sync, but with a read error
3359 * which we can now try to correct
3360 */
3361 set_bit(R5_Insync, &dev->flags);
3362 set_bit(R5_ReadError, &dev->flags);
3363 }
3364 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003365 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003366 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003367 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003368 set_bit(R5_Insync, &dev->flags);
3369 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3370 test_bit(R5_Expanded, &dev->flags))
3371 /* If we've reshaped into here, we assume it is Insync.
3372 * We will shortly update recovery_offset to make
3373 * it official.
3374 */
3375 set_bit(R5_Insync, &dev->flags);
3376
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003377 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003378 /* This flag does not apply to '.replacement'
3379 * only to .rdev, so make sure to check that*/
3380 struct md_rdev *rdev2 = rcu_dereference(
3381 conf->disks[i].rdev);
3382 if (rdev2 == rdev)
3383 clear_bit(R5_Insync, &dev->flags);
3384 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003385 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003386 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003387 } else
3388 clear_bit(R5_WriteError, &dev->flags);
3389 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003390 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003391 /* This flag does not apply to '.replacement'
3392 * only to .rdev, so make sure to check that*/
3393 struct md_rdev *rdev2 = rcu_dereference(
3394 conf->disks[i].rdev);
3395 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003396 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003397 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003398 } else
3399 clear_bit(R5_MadeGood, &dev->flags);
3400 }
NeilBrown977df362011-12-23 10:17:53 +11003401 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3402 struct md_rdev *rdev2 = rcu_dereference(
3403 conf->disks[i].replacement);
3404 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3405 s->handle_bad_blocks = 1;
3406 atomic_inc(&rdev2->nr_pending);
3407 } else
3408 clear_bit(R5_MadeGoodRepl, &dev->flags);
3409 }
NeilBrown415e72d2010-06-17 17:25:21 +10003410 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003411 /* The ReadError flag will just be confusing now */
3412 clear_bit(R5_ReadError, &dev->flags);
3413 clear_bit(R5_ReWrite, &dev->flags);
3414 }
NeilBrown415e72d2010-06-17 17:25:21 +10003415 if (test_bit(R5_ReadError, &dev->flags))
3416 clear_bit(R5_Insync, &dev->flags);
3417 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003418 if (s->failed < 2)
3419 s->failed_num[s->failed] = i;
3420 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003421 if (rdev && !test_bit(Faulty, &rdev->flags))
3422 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003423 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003424 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003425 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3426 /* If there is a failed device being replaced,
3427 * we must be recovering.
3428 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003429 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003430 * else we can only be replacing
3431 * sync and recovery both need to read all devices, and so
3432 * use the same flag.
3433 */
3434 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003435 sh->sector >= conf->mddev->recovery_cp ||
3436 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003437 s->syncing = 1;
3438 else
3439 s->replacing = 1;
3440 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003441 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003442}
NeilBrownf4168852007-02-28 20:11:53 -08003443
NeilBrowncc940152011-07-26 11:35:35 +10003444static void handle_stripe(struct stripe_head *sh)
3445{
3446 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003447 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003448 int i;
NeilBrown84789552011-07-27 11:00:36 +10003449 int prexor;
3450 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003451 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003452
3453 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003454 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003455 /* already being handled, ensure it gets handled
3456 * again when current action finishes */
3457 set_bit(STRIPE_HANDLE, &sh->state);
3458 return;
3459 }
3460
3461 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3462 set_bit(STRIPE_SYNCING, &sh->state);
3463 clear_bit(STRIPE_INSYNC, &sh->state);
3464 }
3465 clear_bit(STRIPE_DELAYED, &sh->state);
3466
3467 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3468 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3469 (unsigned long long)sh->sector, sh->state,
3470 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3471 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003472
NeilBrownacfe7262011-07-27 11:00:36 +10003473 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003474
NeilBrownbc2607f2011-07-28 11:39:22 +10003475 if (s.handle_bad_blocks) {
3476 set_bit(STRIPE_HANDLE, &sh->state);
3477 goto finish;
3478 }
3479
NeilBrown474af965fe2011-07-27 11:00:36 +10003480 if (unlikely(s.blocked_rdev)) {
3481 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003482 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003483 set_bit(STRIPE_HANDLE, &sh->state);
3484 goto finish;
3485 }
3486 /* There is nothing for the blocked_rdev to block */
3487 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3488 s.blocked_rdev = NULL;
3489 }
3490
3491 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3492 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3493 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3494 }
3495
3496 pr_debug("locked=%d uptodate=%d to_read=%d"
3497 " to_write=%d failed=%d failed_num=%d,%d\n",
3498 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3499 s.failed_num[0], s.failed_num[1]);
3500 /* check if the array has lost more than max_degraded devices and,
3501 * if so, some requests might need to be failed.
3502 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003503 if (s.failed > conf->max_degraded) {
3504 sh->check_state = 0;
3505 sh->reconstruct_state = 0;
3506 if (s.to_read+s.to_write+s.written)
3507 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003508 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003509 handle_failed_sync(conf, sh, &s);
3510 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003511
NeilBrown84789552011-07-27 11:00:36 +10003512 /* Now we check to see if any write operations have recently
3513 * completed
3514 */
3515 prexor = 0;
3516 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3517 prexor = 1;
3518 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3519 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3520 sh->reconstruct_state = reconstruct_state_idle;
3521
3522 /* All the 'written' buffers and the parity block are ready to
3523 * be written back to disk
3524 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003525 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3526 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003527 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003528 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3529 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003530 for (i = disks; i--; ) {
3531 struct r5dev *dev = &sh->dev[i];
3532 if (test_bit(R5_LOCKED, &dev->flags) &&
3533 (i == sh->pd_idx || i == sh->qd_idx ||
3534 dev->written)) {
3535 pr_debug("Writing block %d\n", i);
3536 set_bit(R5_Wantwrite, &dev->flags);
3537 if (prexor)
3538 continue;
3539 if (!test_bit(R5_Insync, &dev->flags) ||
3540 ((i == sh->pd_idx || i == sh->qd_idx) &&
3541 s.failed == 0))
3542 set_bit(STRIPE_INSYNC, &sh->state);
3543 }
3544 }
3545 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3546 s.dec_preread_active = 1;
3547 }
3548
NeilBrownef5b7c62012-11-22 09:13:36 +11003549 /*
3550 * might be able to return some write requests if the parity blocks
3551 * are safe, or on a failed drive
3552 */
3553 pdev = &sh->dev[sh->pd_idx];
3554 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3555 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3556 qdev = &sh->dev[sh->qd_idx];
3557 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3558 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3559 || conf->level < 6;
3560
3561 if (s.written &&
3562 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3563 && !test_bit(R5_LOCKED, &pdev->flags)
3564 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3565 test_bit(R5_Discard, &pdev->flags))))) &&
3566 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3567 && !test_bit(R5_LOCKED, &qdev->flags)
3568 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3569 test_bit(R5_Discard, &qdev->flags))))))
3570 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3571
3572 /* Now we might consider reading some blocks, either to check/generate
3573 * parity, or to satisfy requests
3574 * or to load a block that is being partially written.
3575 */
3576 if (s.to_read || s.non_overwrite
3577 || (conf->level == 6 && s.to_write && s.failed)
3578 || (s.syncing && (s.uptodate + s.compute < disks))
3579 || s.replacing
3580 || s.expanding)
3581 handle_stripe_fill(sh, &s, disks);
3582
NeilBrown84789552011-07-27 11:00:36 +10003583 /* Now to consider new write requests and what else, if anything
3584 * should be read. We do not handle new writes when:
3585 * 1/ A 'write' operation (copy+xor) is already in flight.
3586 * 2/ A 'check' operation is in flight, as it may clobber the parity
3587 * block.
3588 */
3589 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3590 handle_stripe_dirtying(conf, sh, &s, disks);
3591
3592 /* maybe we need to check and possibly fix the parity for this stripe
3593 * Any reads will already have been scheduled, so we just see if enough
3594 * data is available. The parity check is held off while parity
3595 * dependent operations are in flight.
3596 */
3597 if (sh->check_state ||
3598 (s.syncing && s.locked == 0 &&
3599 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3600 !test_bit(STRIPE_INSYNC, &sh->state))) {
3601 if (conf->level == 6)
3602 handle_parity_checks6(conf, sh, &s, disks);
3603 else
3604 handle_parity_checks5(conf, sh, &s, disks);
3605 }
NeilBrownc5a31002011-07-27 11:00:36 +10003606
NeilBrown9a3e1102011-12-23 10:17:53 +11003607 if (s.replacing && s.locked == 0
3608 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3609 /* Write out to replacement devices where possible */
3610 for (i = 0; i < conf->raid_disks; i++)
3611 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3612 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3613 set_bit(R5_WantReplace, &sh->dev[i].flags);
3614 set_bit(R5_LOCKED, &sh->dev[i].flags);
3615 s.locked++;
3616 }
3617 set_bit(STRIPE_INSYNC, &sh->state);
3618 }
3619 if ((s.syncing || s.replacing) && s.locked == 0 &&
3620 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003621 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3622 clear_bit(STRIPE_SYNCING, &sh->state);
3623 }
3624
3625 /* If the failed drives are just a ReadError, then we might need
3626 * to progress the repair/check process
3627 */
3628 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3629 for (i = 0; i < s.failed; i++) {
3630 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3631 if (test_bit(R5_ReadError, &dev->flags)
3632 && !test_bit(R5_LOCKED, &dev->flags)
3633 && test_bit(R5_UPTODATE, &dev->flags)
3634 ) {
3635 if (!test_bit(R5_ReWrite, &dev->flags)) {
3636 set_bit(R5_Wantwrite, &dev->flags);
3637 set_bit(R5_ReWrite, &dev->flags);
3638 set_bit(R5_LOCKED, &dev->flags);
3639 s.locked++;
3640 } else {
3641 /* let's read it back */
3642 set_bit(R5_Wantread, &dev->flags);
3643 set_bit(R5_LOCKED, &dev->flags);
3644 s.locked++;
3645 }
3646 }
3647 }
3648
3649
NeilBrown3687c062011-07-27 11:00:36 +10003650 /* Finish reconstruct operations initiated by the expansion process */
3651 if (sh->reconstruct_state == reconstruct_state_result) {
3652 struct stripe_head *sh_src
3653 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3654 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3655 /* sh cannot be written until sh_src has been read.
3656 * so arrange for sh to be delayed a little
3657 */
3658 set_bit(STRIPE_DELAYED, &sh->state);
3659 set_bit(STRIPE_HANDLE, &sh->state);
3660 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3661 &sh_src->state))
3662 atomic_inc(&conf->preread_active_stripes);
3663 release_stripe(sh_src);
3664 goto finish;
3665 }
3666 if (sh_src)
3667 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003668
NeilBrown3687c062011-07-27 11:00:36 +10003669 sh->reconstruct_state = reconstruct_state_idle;
3670 clear_bit(STRIPE_EXPANDING, &sh->state);
3671 for (i = conf->raid_disks; i--; ) {
3672 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3673 set_bit(R5_LOCKED, &sh->dev[i].flags);
3674 s.locked++;
3675 }
3676 }
3677
3678 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3679 !sh->reconstruct_state) {
3680 /* Need to write out all blocks after computing parity */
3681 sh->disks = conf->raid_disks;
3682 stripe_set_idx(sh->sector, conf, 0, sh);
3683 schedule_reconstruction(sh, &s, 1, 1);
3684 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3685 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3686 atomic_dec(&conf->reshape_stripes);
3687 wake_up(&conf->wait_for_overlap);
3688 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3689 }
3690
3691 if (s.expanding && s.locked == 0 &&
3692 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3693 handle_stripe_expansion(conf, sh);
3694
3695finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003696 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003697 if (unlikely(s.blocked_rdev)) {
3698 if (conf->mddev->external)
3699 md_wait_for_blocked_rdev(s.blocked_rdev,
3700 conf->mddev);
3701 else
3702 /* Internal metadata will immediately
3703 * be written by raid5d, so we don't
3704 * need to wait here.
3705 */
3706 rdev_dec_pending(s.blocked_rdev,
3707 conf->mddev);
3708 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003709
NeilBrownbc2607f2011-07-28 11:39:22 +10003710 if (s.handle_bad_blocks)
3711 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003712 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003713 struct r5dev *dev = &sh->dev[i];
3714 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3715 /* We own a safe reference to the rdev */
3716 rdev = conf->disks[i].rdev;
3717 if (!rdev_set_badblocks(rdev, sh->sector,
3718 STRIPE_SECTORS, 0))
3719 md_error(conf->mddev, rdev);
3720 rdev_dec_pending(rdev, conf->mddev);
3721 }
NeilBrownb84db562011-07-28 11:39:23 +10003722 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3723 rdev = conf->disks[i].rdev;
3724 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003725 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003726 rdev_dec_pending(rdev, conf->mddev);
3727 }
NeilBrown977df362011-12-23 10:17:53 +11003728 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3729 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003730 if (!rdev)
3731 /* rdev have been moved down */
3732 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003733 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003734 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003735 rdev_dec_pending(rdev, conf->mddev);
3736 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003737 }
3738
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003739 if (s.ops_request)
3740 raid_run_ops(sh, s.ops_request);
3741
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003742 ops_run_io(sh, &s);
3743
NeilBrownc5709ef2011-07-26 11:35:20 +10003744 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003745 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003746 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003747 * have actually been submitted.
3748 */
3749 atomic_dec(&conf->preread_active_stripes);
3750 if (atomic_read(&conf->preread_active_stripes) <
3751 IO_THRESHOLD)
3752 md_wakeup_thread(conf->mddev->thread);
3753 }
3754
NeilBrownc5709ef2011-07-26 11:35:20 +10003755 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003756
Dan Williams257a4b42011-11-08 16:22:06 +11003757 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003758}
3759
NeilBrownd1688a62011-10-11 16:49:52 +11003760static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
3762 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3763 while (!list_empty(&conf->delayed_list)) {
3764 struct list_head *l = conf->delayed_list.next;
3765 struct stripe_head *sh;
3766 sh = list_entry(l, struct stripe_head, lru);
3767 list_del_init(l);
3768 clear_bit(STRIPE_DELAYED, &sh->state);
3769 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3770 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003771 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 }
NeilBrown482c0832011-04-18 18:25:42 +10003773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774}
3775
NeilBrownd1688a62011-10-11 16:49:52 +11003776static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003777{
3778 /* device_lock is held */
3779 struct list_head head;
3780 list_add(&head, &conf->bitmap_list);
3781 list_del_init(&conf->bitmap_list);
3782 while (!list_empty(&head)) {
3783 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3784 list_del_init(&sh->lru);
3785 atomic_inc(&sh->count);
3786 __release_stripe(conf, sh);
3787 }
3788}
3789
NeilBrownfd01b882011-10-11 16:47:53 +11003790int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003791{
NeilBrownd1688a62011-10-11 16:49:52 +11003792 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003793
3794 /* No difference between reads and writes. Just check
3795 * how busy the stripe_cache is
3796 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003797
NeilBrownf022b2f2006-10-03 01:15:56 -07003798 if (conf->inactive_blocked)
3799 return 1;
3800 if (conf->quiesce)
3801 return 1;
3802 if (list_empty_careful(&conf->inactive_list))
3803 return 1;
3804
3805 return 0;
3806}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003807EXPORT_SYMBOL_GPL(md_raid5_congested);
3808
3809static int raid5_congested(void *data, int bits)
3810{
NeilBrownfd01b882011-10-11 16:47:53 +11003811 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003812
3813 return mddev_congested(mddev, bits) ||
3814 md_raid5_congested(mddev, bits);
3815}
NeilBrownf022b2f2006-10-03 01:15:56 -07003816
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003817/* We want read requests to align with chunks where possible,
3818 * but write requests don't need to.
3819 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003820static int raid5_mergeable_bvec(struct request_queue *q,
3821 struct bvec_merge_data *bvm,
3822 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003823{
NeilBrownfd01b882011-10-11 16:47:53 +11003824 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003825 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003826 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003827 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003828 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003829
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003830 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003831 return biovec->bv_len; /* always allow writes to be mergeable */
3832
Andre Noll664e7c42009-06-18 08:45:27 +10003833 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3834 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003835 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3836 if (max < 0) max = 0;
3837 if (max <= biovec->bv_len && bio_sectors == 0)
3838 return biovec->bv_len;
3839 else
3840 return max;
3841}
3842
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003843
NeilBrownfd01b882011-10-11 16:47:53 +11003844static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003845{
3846 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003847 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003848 unsigned int bio_sectors = bio->bi_size >> 9;
3849
Andre Noll664e7c42009-06-18 08:45:27 +10003850 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3851 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003852 return chunk_sectors >=
3853 ((sector & (chunk_sectors - 1)) + bio_sectors);
3854}
3855
3856/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003857 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3858 * later sampled by raid5d.
3859 */
NeilBrownd1688a62011-10-11 16:49:52 +11003860static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003861{
3862 unsigned long flags;
3863
3864 spin_lock_irqsave(&conf->device_lock, flags);
3865
3866 bi->bi_next = conf->retry_read_aligned_list;
3867 conf->retry_read_aligned_list = bi;
3868
3869 spin_unlock_irqrestore(&conf->device_lock, flags);
3870 md_wakeup_thread(conf->mddev->thread);
3871}
3872
3873
NeilBrownd1688a62011-10-11 16:49:52 +11003874static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003875{
3876 struct bio *bi;
3877
3878 bi = conf->retry_read_aligned;
3879 if (bi) {
3880 conf->retry_read_aligned = NULL;
3881 return bi;
3882 }
3883 bi = conf->retry_read_aligned_list;
3884 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003885 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003886 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003887 /*
3888 * this sets the active strip count to 1 and the processed
3889 * strip count to zero (upper 8 bits)
3890 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003891 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003892 }
3893
3894 return bi;
3895}
3896
3897
3898/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003899 * The "raid5_align_endio" should check if the read succeeded and if it
3900 * did, call bio_endio on the original bio (having bio_put the new bio
3901 * first).
3902 * If the read failed..
3903 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003904static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003905{
3906 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003907 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003908 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003909 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003910 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003911
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003912 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003913
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003914 rdev = (void*)raid_bi->bi_next;
3915 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003916 mddev = rdev->mddev;
3917 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003918
3919 rdev_dec_pending(rdev, conf->mddev);
3920
3921 if (!error && uptodate) {
NeilBrowna9add5d2012-10-31 11:59:09 +11003922 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
3923 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02003924 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003925 if (atomic_dec_and_test(&conf->active_aligned_reads))
3926 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003927 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003928 }
3929
3930
Dan Williams45b42332007-07-09 11:56:43 -07003931 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003932
3933 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003934}
3935
Neil Brown387bb172007-02-08 14:20:29 -08003936static int bio_fits_rdev(struct bio *bi)
3937{
Jens Axboe165125e2007-07-24 09:28:11 +02003938 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003939
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003940 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003941 return 0;
3942 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003943 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003944 return 0;
3945
3946 if (q->merge_bvec_fn)
3947 /* it's too hard to apply the merge_bvec_fn at this stage,
3948 * just just give up
3949 */
3950 return 0;
3951
3952 return 1;
3953}
3954
3955
NeilBrownfd01b882011-10-11 16:47:53 +11003956static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003957{
NeilBrownd1688a62011-10-11 16:49:52 +11003958 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003959 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003960 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003961 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003962 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003963
3964 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003965 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003966 return 0;
3967 }
3968 /*
NeilBrowna167f662010-10-26 18:31:13 +11003969 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003970 */
NeilBrowna167f662010-10-26 18:31:13 +11003971 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003972 if (!align_bi)
3973 return 0;
3974 /*
3975 * set bi_end_io to a new function, and set bi_private to the
3976 * original bio.
3977 */
3978 align_bi->bi_end_io = raid5_align_endio;
3979 align_bi->bi_private = raid_bio;
3980 /*
3981 * compute position
3982 */
NeilBrown112bf892009-03-31 14:39:38 +11003983 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3984 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003985 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003986
NeilBrown671488c2011-12-23 10:17:52 +11003987 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003988 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003989 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3990 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3991 rdev->recovery_offset < end_sector) {
3992 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3993 if (rdev &&
3994 (test_bit(Faulty, &rdev->flags) ||
3995 !(test_bit(In_sync, &rdev->flags) ||
3996 rdev->recovery_offset >= end_sector)))
3997 rdev = NULL;
3998 }
3999 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10004000 sector_t first_bad;
4001 int bad_sectors;
4002
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004003 atomic_inc(&rdev->nr_pending);
4004 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004005 raid_bio->bi_next = (void*)rdev;
4006 align_bi->bi_bdev = rdev->bdev;
4007 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004008
NeilBrown31c176e2011-07-28 11:39:22 +10004009 if (!bio_fits_rdev(align_bi) ||
4010 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
4011 &first_bad, &bad_sectors)) {
4012 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004013 bio_put(align_bi);
4014 rdev_dec_pending(rdev, mddev);
4015 return 0;
4016 }
4017
majianpeng6c0544e2012-06-12 08:31:10 +08004018 /* No reshape active, so we can trust rdev->data_offset */
4019 align_bi->bi_sector += rdev->data_offset;
4020
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004021 spin_lock_irq(&conf->device_lock);
4022 wait_event_lock_irq(conf->wait_for_stripe,
4023 conf->quiesce == 0,
4024 conf->device_lock, /* nothing */);
4025 atomic_inc(&conf->active_aligned_reads);
4026 spin_unlock_irq(&conf->device_lock);
4027
NeilBrowna9add5d2012-10-31 11:59:09 +11004028 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4029 align_bi, disk_devt(mddev->gendisk),
4030 raid_bio->bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004031 generic_make_request(align_bi);
4032 return 1;
4033 } else {
4034 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004035 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004036 return 0;
4037 }
4038}
4039
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004040/* __get_priority_stripe - get the next stripe to process
4041 *
4042 * Full stripe writes are allowed to pass preread active stripes up until
4043 * the bypass_threshold is exceeded. In general the bypass_count
4044 * increments when the handle_list is handled before the hold_list; however, it
4045 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4046 * stripe with in flight i/o. The bypass_count will be reset when the
4047 * head of the hold_list has changed, i.e. the head was promoted to the
4048 * handle_list.
4049 */
NeilBrownd1688a62011-10-11 16:49:52 +11004050static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004051{
4052 struct stripe_head *sh;
4053
4054 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4055 __func__,
4056 list_empty(&conf->handle_list) ? "empty" : "busy",
4057 list_empty(&conf->hold_list) ? "empty" : "busy",
4058 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4059
4060 if (!list_empty(&conf->handle_list)) {
4061 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4062
4063 if (list_empty(&conf->hold_list))
4064 conf->bypass_count = 0;
4065 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4066 if (conf->hold_list.next == conf->last_hold)
4067 conf->bypass_count++;
4068 else {
4069 conf->last_hold = conf->hold_list.next;
4070 conf->bypass_count -= conf->bypass_threshold;
4071 if (conf->bypass_count < 0)
4072 conf->bypass_count = 0;
4073 }
4074 }
4075 } else if (!list_empty(&conf->hold_list) &&
4076 ((conf->bypass_threshold &&
4077 conf->bypass_count > conf->bypass_threshold) ||
4078 atomic_read(&conf->pending_full_writes) == 0)) {
4079 sh = list_entry(conf->hold_list.next,
4080 typeof(*sh), lru);
4081 conf->bypass_count -= conf->bypass_threshold;
4082 if (conf->bypass_count < 0)
4083 conf->bypass_count = 0;
4084 } else
4085 return NULL;
4086
4087 list_del_init(&sh->lru);
4088 atomic_inc(&sh->count);
4089 BUG_ON(atomic_read(&sh->count) != 1);
4090 return sh;
4091}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004092
Shaohua Li8811b592012-08-02 08:33:00 +10004093struct raid5_plug_cb {
4094 struct blk_plug_cb cb;
4095 struct list_head list;
4096};
4097
4098static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4099{
4100 struct raid5_plug_cb *cb = container_of(
4101 blk_cb, struct raid5_plug_cb, cb);
4102 struct stripe_head *sh;
4103 struct mddev *mddev = cb->cb.data;
4104 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004105 int cnt = 0;
Shaohua Li8811b592012-08-02 08:33:00 +10004106
4107 if (cb->list.next && !list_empty(&cb->list)) {
4108 spin_lock_irq(&conf->device_lock);
4109 while (!list_empty(&cb->list)) {
4110 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4111 list_del_init(&sh->lru);
4112 /*
4113 * avoid race release_stripe_plug() sees
4114 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4115 * is still in our list
4116 */
4117 smp_mb__before_clear_bit();
4118 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4119 __release_stripe(conf, sh);
NeilBrowna9add5d2012-10-31 11:59:09 +11004120 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004121 }
4122 spin_unlock_irq(&conf->device_lock);
4123 }
NeilBrowna9add5d2012-10-31 11:59:09 +11004124 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004125 kfree(cb);
4126}
4127
4128static void release_stripe_plug(struct mddev *mddev,
4129 struct stripe_head *sh)
4130{
4131 struct blk_plug_cb *blk_cb = blk_check_plugged(
4132 raid5_unplug, mddev,
4133 sizeof(struct raid5_plug_cb));
4134 struct raid5_plug_cb *cb;
4135
4136 if (!blk_cb) {
4137 release_stripe(sh);
4138 return;
4139 }
4140
4141 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4142
4143 if (cb->list.next == NULL)
4144 INIT_LIST_HEAD(&cb->list);
4145
4146 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4147 list_add_tail(&sh->lru, &cb->list);
4148 else
4149 release_stripe(sh);
4150}
4151
Shaohua Li620125f2012-10-11 13:49:05 +11004152static void make_discard_request(struct mddev *mddev, struct bio *bi)
4153{
4154 struct r5conf *conf = mddev->private;
4155 sector_t logical_sector, last_sector;
4156 struct stripe_head *sh;
4157 int remaining;
4158 int stripe_sectors;
4159
4160 if (mddev->reshape_position != MaxSector)
4161 /* Skip discard while reshape is happening */
4162 return;
4163
4164 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4165 last_sector = bi->bi_sector + (bi->bi_size>>9);
4166
4167 bi->bi_next = NULL;
4168 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4169
4170 stripe_sectors = conf->chunk_sectors *
4171 (conf->raid_disks - conf->max_degraded);
4172 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4173 stripe_sectors);
4174 sector_div(last_sector, stripe_sectors);
4175
4176 logical_sector *= conf->chunk_sectors;
4177 last_sector *= conf->chunk_sectors;
4178
4179 for (; logical_sector < last_sector;
4180 logical_sector += STRIPE_SECTORS) {
4181 DEFINE_WAIT(w);
4182 int d;
4183 again:
4184 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4185 prepare_to_wait(&conf->wait_for_overlap, &w,
4186 TASK_UNINTERRUPTIBLE);
4187 spin_lock_irq(&sh->stripe_lock);
4188 for (d = 0; d < conf->raid_disks; d++) {
4189 if (d == sh->pd_idx || d == sh->qd_idx)
4190 continue;
4191 if (sh->dev[d].towrite || sh->dev[d].toread) {
4192 set_bit(R5_Overlap, &sh->dev[d].flags);
4193 spin_unlock_irq(&sh->stripe_lock);
4194 release_stripe(sh);
4195 schedule();
4196 goto again;
4197 }
4198 }
4199 finish_wait(&conf->wait_for_overlap, &w);
4200 for (d = 0; d < conf->raid_disks; d++) {
4201 if (d == sh->pd_idx || d == sh->qd_idx)
4202 continue;
4203 sh->dev[d].towrite = bi;
4204 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4205 raid5_inc_bi_active_stripes(bi);
4206 }
4207 spin_unlock_irq(&sh->stripe_lock);
4208 if (conf->mddev->bitmap) {
4209 for (d = 0;
4210 d < conf->raid_disks - conf->max_degraded;
4211 d++)
4212 bitmap_startwrite(mddev->bitmap,
4213 sh->sector,
4214 STRIPE_SECTORS,
4215 0);
4216 sh->bm_seq = conf->seq_flush + 1;
4217 set_bit(STRIPE_BIT_DELAY, &sh->state);
4218 }
4219
4220 set_bit(STRIPE_HANDLE, &sh->state);
4221 clear_bit(STRIPE_DELAYED, &sh->state);
4222 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4223 atomic_inc(&conf->preread_active_stripes);
4224 release_stripe_plug(mddev, sh);
4225 }
4226
4227 remaining = raid5_dec_bi_active_stripes(bi);
4228 if (remaining == 0) {
4229 md_write_end(mddev);
4230 bio_endio(bi, 0);
4231 }
4232}
4233
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004234static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235{
NeilBrownd1688a62011-10-11 16:49:52 +11004236 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004237 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 sector_t new_sector;
4239 sector_t logical_sector, last_sector;
4240 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004241 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004242 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Tejun Heoe9c74692010-09-03 11:56:18 +02004244 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4245 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004246 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004247 }
4248
NeilBrown3d310eb2005-06-21 17:17:26 -07004249 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004250
NeilBrown802ba062006-12-13 00:34:13 -08004251 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004252 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004253 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004254 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004255
Shaohua Li620125f2012-10-11 13:49:05 +11004256 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4257 make_discard_request(mddev, bi);
4258 return;
4259 }
4260
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4262 last_sector = bi->bi_sector + (bi->bi_size>>9);
4263 bi->bi_next = NULL;
4264 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004265
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4267 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004268 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004269
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004270 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004271 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004272 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004273 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004274 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004275 * 64bit on a 32bit platform, and so it might be
4276 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004277 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004278 * the lock is dropped, so once we get a reference
4279 * to the stripe that we think it is, we will have
4280 * to check again.
4281 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004282 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004283 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004284 ? logical_sector < conf->reshape_progress
4285 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004286 previous = 1;
4287 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004288 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004289 ? logical_sector < conf->reshape_safe
4290 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004291 spin_unlock_irq(&conf->device_lock);
4292 schedule();
4293 goto retry;
4294 }
4295 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004296 spin_unlock_irq(&conf->device_lock);
4297 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004298
NeilBrown112bf892009-03-31 14:39:38 +11004299 new_sector = raid5_compute_sector(conf, logical_sector,
4300 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004301 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004302 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 (unsigned long long)new_sector,
4304 (unsigned long long)logical_sector);
4305
NeilBrownb5663ba2009-03-31 14:39:38 +11004306 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004307 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004309 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004310 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004311 * stripe, so we must do the range check again.
4312 * Expansion could still move past after this
4313 * test, but as we are holding a reference to
4314 * 'sh', we know that if that happens,
4315 * STRIPE_EXPANDING will get set and the expansion
4316 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004317 */
4318 int must_retry = 0;
4319 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004320 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004321 ? logical_sector >= conf->reshape_progress
4322 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004323 /* mismatch, need to try again */
4324 must_retry = 1;
4325 spin_unlock_irq(&conf->device_lock);
4326 if (must_retry) {
4327 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004328 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004329 goto retry;
4330 }
4331 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004332
Namhyung Kimffd96e32011-07-18 17:38:51 +10004333 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004334 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004335 logical_sector < mddev->suspend_hi) {
4336 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004337 /* As the suspend_* range is controlled by
4338 * userspace, we want an interruptible
4339 * wait.
4340 */
4341 flush_signals(current);
4342 prepare_to_wait(&conf->wait_for_overlap,
4343 &w, TASK_INTERRUPTIBLE);
4344 if (logical_sector >= mddev->suspend_lo &&
4345 logical_sector < mddev->suspend_hi)
4346 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004347 goto retry;
4348 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004349
4350 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004351 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004352 /* Stripe is busy expanding or
4353 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 * and wait a while
4355 */
NeilBrown482c0832011-04-18 18:25:42 +10004356 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 release_stripe(sh);
4358 schedule();
4359 goto retry;
4360 }
4361 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004362 set_bit(STRIPE_HANDLE, &sh->state);
4363 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004364 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004365 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4366 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004367 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 } else {
4369 /* cannot get stripe for read-ahead, just give-up */
4370 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4371 finish_wait(&conf->wait_for_overlap, &w);
4372 break;
4373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004375
Shaohua Lie7836bd62012-07-19 16:01:31 +10004376 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004377 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
NeilBrown16a53ec2006-06-26 00:27:38 -07004379 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004381
NeilBrowna9add5d2012-10-31 11:59:09 +11004382 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4383 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004384 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386}
4387
NeilBrownfd01b882011-10-11 16:47:53 +11004388static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004389
NeilBrownfd01b882011-10-11 16:47:53 +11004390static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391{
NeilBrown52c03292006-06-26 00:27:43 -07004392 /* reshaping is quite different to recovery/resync so it is
4393 * handled quite separately ... here.
4394 *
4395 * On each call to sync_request, we gather one chunk worth of
4396 * destination stripes and flag them as expanding.
4397 * Then we find all the source stripes and request reads.
4398 * As the reads complete, handle_stripe will copy the data
4399 * into the destination stripe and release that stripe.
4400 */
NeilBrownd1688a62011-10-11 16:49:52 +11004401 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004403 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004404 int raid_disks = conf->previous_raid_disks;
4405 int data_disks = raid_disks - conf->max_degraded;
4406 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004407 int i;
4408 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004409 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004410 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004411 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004412 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004413
NeilBrownfef9c612009-03-31 15:16:46 +11004414 if (sector_nr == 0) {
4415 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004416 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004417 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4418 sector_nr = raid5_size(mddev, 0, 0)
4419 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004420 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004421 conf->reshape_progress > 0)
4422 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004423 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004424 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004425 mddev->curr_resync_completed = sector_nr;
4426 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004427 *skipped = 1;
4428 return sector_nr;
4429 }
NeilBrown52c03292006-06-26 00:27:43 -07004430 }
4431
NeilBrown7a661382009-03-31 15:21:40 +11004432 /* We need to process a full chunk at a time.
4433 * If old and new chunk sizes differ, we need to process the
4434 * largest of these
4435 */
Andre Noll664e7c42009-06-18 08:45:27 +10004436 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4437 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004438 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004439 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004440
NeilBrownb5254dd2012-05-21 09:27:01 +10004441 /* We update the metadata at least every 10 seconds, or when
4442 * the data about to be copied would over-write the source of
4443 * the data at the front of the range. i.e. one new_stripe
4444 * along from reshape_progress new_maps to after where
4445 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004446 */
NeilBrownfef9c612009-03-31 15:16:46 +11004447 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004448 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004449 readpos = conf->reshape_progress;
4450 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004451 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004452 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004453 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004454 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004455 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004456 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004457 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004458 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004459 readpos -= min_t(sector_t, reshape_sectors, readpos);
4460 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004461 }
NeilBrown52c03292006-06-26 00:27:43 -07004462
NeilBrownb5254dd2012-05-21 09:27:01 +10004463 /* Having calculated the 'writepos' possibly use it
4464 * to set 'stripe_addr' which is where we will write to.
4465 */
4466 if (mddev->reshape_backwards) {
4467 BUG_ON(conf->reshape_progress == 0);
4468 stripe_addr = writepos;
4469 BUG_ON((mddev->dev_sectors &
4470 ~((sector_t)reshape_sectors - 1))
4471 - reshape_sectors - stripe_addr
4472 != sector_nr);
4473 } else {
4474 BUG_ON(writepos != sector_nr + reshape_sectors);
4475 stripe_addr = sector_nr;
4476 }
4477
NeilBrownc8f517c2009-03-31 15:28:40 +11004478 /* 'writepos' is the most advanced device address we might write.
4479 * 'readpos' is the least advanced device address we might read.
4480 * 'safepos' is the least address recorded in the metadata as having
4481 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004482 * If there is a min_offset_diff, these are adjusted either by
4483 * increasing the safepos/readpos if diff is negative, or
4484 * increasing writepos if diff is positive.
4485 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004486 * ensure safety in the face of a crash - that must be done by userspace
4487 * making a backup of the data. So in that case there is no particular
4488 * rush to update metadata.
4489 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4490 * update the metadata to advance 'safepos' to match 'readpos' so that
4491 * we can be safe in the event of a crash.
4492 * So we insist on updating metadata if safepos is behind writepos and
4493 * readpos is beyond writepos.
4494 * In any case, update the metadata every 10 seconds.
4495 * Maybe that number should be configurable, but I'm not sure it is
4496 * worth it.... maybe it could be a multiple of safemode_delay???
4497 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004498 if (conf->min_offset_diff < 0) {
4499 safepos += -conf->min_offset_diff;
4500 readpos += -conf->min_offset_diff;
4501 } else
4502 writepos += conf->min_offset_diff;
4503
NeilBrown2c810cd2012-05-21 09:27:00 +10004504 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004505 ? (safepos > writepos && readpos < writepos)
4506 : (safepos < writepos && readpos > writepos)) ||
4507 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004508 /* Cannot proceed until we've updated the superblock... */
4509 wait_event(conf->wait_for_overlap,
4510 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004511 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004512 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004513 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004514 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004515 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004516 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004517 kthread_should_stop());
4518 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004519 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004520 spin_unlock_irq(&conf->device_lock);
4521 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004522 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004523 }
4524
NeilBrownab69ae12009-03-31 15:26:47 +11004525 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004526 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004527 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004528 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004529 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004530 set_bit(STRIPE_EXPANDING, &sh->state);
4531 atomic_inc(&conf->reshape_stripes);
4532 /* If any of this stripe is beyond the end of the old
4533 * array, then we need to zero those blocks
4534 */
4535 for (j=sh->disks; j--;) {
4536 sector_t s;
4537 if (j == sh->pd_idx)
4538 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004539 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004540 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004541 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004542 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004543 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004544 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004545 continue;
4546 }
4547 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4548 set_bit(R5_Expanded, &sh->dev[j].flags);
4549 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4550 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004551 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004552 set_bit(STRIPE_EXPAND_READY, &sh->state);
4553 set_bit(STRIPE_HANDLE, &sh->state);
4554 }
NeilBrownab69ae12009-03-31 15:26:47 +11004555 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004556 }
4557 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004558 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004559 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004560 else
NeilBrown7a661382009-03-31 15:21:40 +11004561 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004562 spin_unlock_irq(&conf->device_lock);
4563 /* Ok, those stripe are ready. We can start scheduling
4564 * reads on the source stripes.
4565 * The source stripes are determined by mapping the first and last
4566 * block on the destination stripes.
4567 */
NeilBrown52c03292006-06-26 00:27:43 -07004568 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004569 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004570 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004571 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004572 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004573 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004574 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004575 if (last_sector >= mddev->dev_sectors)
4576 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004577 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004578 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004579 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4580 set_bit(STRIPE_HANDLE, &sh->state);
4581 release_stripe(sh);
4582 first_sector += STRIPE_SECTORS;
4583 }
NeilBrownab69ae12009-03-31 15:26:47 +11004584 /* Now that the sources are clearly marked, we can release
4585 * the destination stripes
4586 */
4587 while (!list_empty(&stripes)) {
4588 sh = list_entry(stripes.next, struct stripe_head, lru);
4589 list_del_init(&sh->lru);
4590 release_stripe(sh);
4591 }
NeilBrownc6207272008-02-06 01:39:52 -08004592 /* If this takes us to the resync_max point where we have to pause,
4593 * then we need to write out the superblock.
4594 */
NeilBrown7a661382009-03-31 15:21:40 +11004595 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004596 if ((sector_nr - mddev->curr_resync_completed) * 2
4597 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004598 /* Cannot proceed until we've updated the superblock... */
4599 wait_event(conf->wait_for_overlap,
4600 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004601 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004602 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004603 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004604 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4605 md_wakeup_thread(mddev->thread);
4606 wait_event(mddev->sb_wait,
4607 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4608 || kthread_should_stop());
4609 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004610 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004611 spin_unlock_irq(&conf->device_lock);
4612 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004613 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004614 }
NeilBrown7a661382009-03-31 15:21:40 +11004615 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004616}
4617
4618/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004619static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004620{
NeilBrownd1688a62011-10-11 16:49:52 +11004621 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004622 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004623 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004624 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004625 int still_degraded = 0;
4626 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
NeilBrown72626682005-09-09 16:23:54 -07004628 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004630
NeilBrown29269552006-03-27 01:18:10 -08004631 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4632 end_reshape(conf);
4633 return 0;
4634 }
NeilBrown72626682005-09-09 16:23:54 -07004635
4636 if (mddev->curr_resync < max_sector) /* aborted */
4637 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4638 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004639 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004640 conf->fullsync = 0;
4641 bitmap_close_sync(mddev->bitmap);
4642
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 return 0;
4644 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004645
NeilBrown64bd6602009-08-03 10:59:58 +10004646 /* Allow raid5_quiesce to complete */
4647 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4648
NeilBrown52c03292006-06-26 00:27:43 -07004649 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4650 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004651
NeilBrownc6207272008-02-06 01:39:52 -08004652 /* No need to check resync_max as we never do more than one
4653 * stripe, and as resync_max will always be on a chunk boundary,
4654 * if the check in md_do_sync didn't fire, there is no chance
4655 * of overstepping resync_max here
4656 */
4657
NeilBrown16a53ec2006-06-26 00:27:38 -07004658 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 * to resync, then assert that we are finished, because there is
4660 * nothing we can do.
4661 */
NeilBrown3285edf2006-06-26 00:27:55 -07004662 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004663 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004664 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004665 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 return rv;
4667 }
NeilBrown72626682005-09-09 16:23:54 -07004668 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004669 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004670 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4671 /* we can skip this block, and probably more */
4672 sync_blocks /= STRIPE_SECTORS;
4673 *skipped = 1;
4674 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
NeilBrownb47490c2008-02-06 01:39:50 -08004677 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4678
NeilBrowna8c906c2009-06-09 14:39:59 +10004679 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004681 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004683 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004685 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004687 /* Need to check if array will still be degraded after recovery/resync
4688 * We don't need to check the 'failed' flag as when that gets set,
4689 * recovery aborts.
4690 */
NeilBrownf001a702009-06-09 14:30:31 +10004691 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004692 if (conf->disks[i].rdev == NULL)
4693 still_degraded = 1;
4694
4695 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4696
NeilBrown83206d62011-07-26 11:19:49 +10004697 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
NeilBrown14425772009-10-16 15:55:25 +11004699 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 release_stripe(sh);
4701
4702 return STRIPE_SECTORS;
4703}
4704
NeilBrownd1688a62011-10-11 16:49:52 +11004705static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004706{
4707 /* We may not be able to submit a whole bio at once as there
4708 * may not be enough stripe_heads available.
4709 * We cannot pre-allocate enough stripe_heads as we may need
4710 * more than exist in the cache (if we allow ever large chunks).
4711 * So we do one stripe head at a time and record in
4712 * ->bi_hw_segments how many have been done.
4713 *
4714 * We *know* that this entire raid_bio is in one chunk, so
4715 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4716 */
4717 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004718 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004719 sector_t sector, logical_sector, last_sector;
4720 int scnt = 0;
4721 int remaining;
4722 int handled = 0;
4723
4724 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004725 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004726 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004727 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4728
4729 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004730 logical_sector += STRIPE_SECTORS,
4731 sector += STRIPE_SECTORS,
4732 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004733
Shaohua Lie7836bd62012-07-19 16:01:31 +10004734 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004735 /* already done this stripe */
4736 continue;
4737
NeilBrowna8c906c2009-06-09 14:39:59 +10004738 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004739
4740 if (!sh) {
4741 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004742 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004743 conf->retry_read_aligned = raid_bio;
4744 return handled;
4745 }
4746
Neil Brown387bb172007-02-08 14:20:29 -08004747 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4748 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004749 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004750 conf->retry_read_aligned = raid_bio;
4751 return handled;
4752 }
4753
majianpeng3f9e7c12012-07-31 10:04:21 +10004754 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004755 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004756 release_stripe(sh);
4757 handled++;
4758 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004759 remaining = raid5_dec_bi_active_stripes(raid_bio);
NeilBrowna9add5d2012-10-31 11:59:09 +11004760 if (remaining == 0) {
4761 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
4762 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004763 bio_endio(raid_bio, 0);
NeilBrowna9add5d2012-10-31 11:59:09 +11004764 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004765 if (atomic_dec_and_test(&conf->active_aligned_reads))
4766 wake_up(&conf->wait_for_stripe);
4767 return handled;
4768}
4769
Shaohua Li46a06402012-08-02 08:33:15 +10004770#define MAX_STRIPE_BATCH 8
4771static int handle_active_stripes(struct r5conf *conf)
4772{
4773 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4774 int i, batch_size = 0;
4775
4776 while (batch_size < MAX_STRIPE_BATCH &&
4777 (sh = __get_priority_stripe(conf)) != NULL)
4778 batch[batch_size++] = sh;
4779
4780 if (batch_size == 0)
4781 return batch_size;
4782 spin_unlock_irq(&conf->device_lock);
4783
4784 for (i = 0; i < batch_size; i++)
4785 handle_stripe(batch[i]);
4786
4787 cond_resched();
4788
4789 spin_lock_irq(&conf->device_lock);
4790 for (i = 0; i < batch_size; i++)
4791 __release_stripe(conf, batch[i]);
4792 return batch_size;
4793}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795/*
4796 * This is our raid5 kernel thread.
4797 *
4798 * We scan the hash table for stripes which can be handled now.
4799 * During the scan, completed stripes are saved for us by the interrupt
4800 * handler, so that they will not have to wait for our next wakeup.
4801 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004802static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803{
Shaohua Li4ed87312012-10-11 13:34:00 +11004804 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004805 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004807 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
Dan Williams45b42332007-07-09 11:56:43 -07004809 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
4811 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004813 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 handled = 0;
4815 spin_lock_irq(&conf->device_lock);
4816 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004817 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004818 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
NeilBrown0021b7b2012-07-31 09:08:14 +02004820 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004821 !list_empty(&conf->bitmap_list)) {
4822 /* Now is a good time to flush some bitmap updates */
4823 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004824 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004825 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004826 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004827 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004828 activate_bit_delay(conf);
4829 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004830 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004831
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004832 while ((bio = remove_bio_from_retry(conf))) {
4833 int ok;
4834 spin_unlock_irq(&conf->device_lock);
4835 ok = retry_aligned_read(conf, bio);
4836 spin_lock_irq(&conf->device_lock);
4837 if (!ok)
4838 break;
4839 handled++;
4840 }
4841
Shaohua Li46a06402012-08-02 08:33:15 +10004842 batch_size = handle_active_stripes(conf);
4843 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004845 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
Shaohua Li46a06402012-08-02 08:33:15 +10004847 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4848 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004849 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004850 spin_lock_irq(&conf->device_lock);
4851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 }
Dan Williams45b42332007-07-09 11:56:43 -07004853 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
4855 spin_unlock_irq(&conf->device_lock);
4856
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004857 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004858 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
Dan Williams45b42332007-07-09 11:56:43 -07004860 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861}
4862
NeilBrown3f294f42005-11-08 21:39:25 -08004863static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004864raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004865{
NeilBrownd1688a62011-10-11 16:49:52 +11004866 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004867 if (conf)
4868 return sprintf(page, "%d\n", conf->max_nr_stripes);
4869 else
4870 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004871}
4872
NeilBrownc41d4ac2010-06-01 19:37:24 +10004873int
NeilBrownfd01b882011-10-11 16:47:53 +11004874raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004875{
NeilBrownd1688a62011-10-11 16:49:52 +11004876 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004877 int err;
4878
4879 if (size <= 16 || size > 32768)
4880 return -EINVAL;
4881 while (size < conf->max_nr_stripes) {
4882 if (drop_one_stripe(conf))
4883 conf->max_nr_stripes--;
4884 else
4885 break;
4886 }
4887 err = md_allow_write(mddev);
4888 if (err)
4889 return err;
4890 while (size > conf->max_nr_stripes) {
4891 if (grow_one_stripe(conf))
4892 conf->max_nr_stripes++;
4893 else break;
4894 }
4895 return 0;
4896}
4897EXPORT_SYMBOL(raid5_set_cache_size);
4898
NeilBrown3f294f42005-11-08 21:39:25 -08004899static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004900raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004901{
NeilBrownd1688a62011-10-11 16:49:52 +11004902 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004903 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004904 int err;
4905
NeilBrown3f294f42005-11-08 21:39:25 -08004906 if (len >= PAGE_SIZE)
4907 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004908 if (!conf)
4909 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004910
Dan Williams4ef197d82008-04-28 02:15:54 -07004911 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004912 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004913 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004914 if (err)
4915 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004916 return len;
4917}
NeilBrown007583c2005-11-08 21:39:30 -08004918
NeilBrown96de1e62005-11-08 21:39:39 -08004919static struct md_sysfs_entry
4920raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4921 raid5_show_stripe_cache_size,
4922 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004923
4924static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004925raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004926{
NeilBrownd1688a62011-10-11 16:49:52 +11004927 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004928 if (conf)
4929 return sprintf(page, "%d\n", conf->bypass_threshold);
4930 else
4931 return 0;
4932}
4933
4934static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004935raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004936{
NeilBrownd1688a62011-10-11 16:49:52 +11004937 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004938 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004939 if (len >= PAGE_SIZE)
4940 return -EINVAL;
4941 if (!conf)
4942 return -ENODEV;
4943
Dan Williams4ef197d82008-04-28 02:15:54 -07004944 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004945 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004946 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004947 return -EINVAL;
4948 conf->bypass_threshold = new;
4949 return len;
4950}
4951
4952static struct md_sysfs_entry
4953raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4954 S_IRUGO | S_IWUSR,
4955 raid5_show_preread_threshold,
4956 raid5_store_preread_threshold);
4957
4958static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004959stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004960{
NeilBrownd1688a62011-10-11 16:49:52 +11004961 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004962 if (conf)
4963 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4964 else
4965 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004966}
4967
NeilBrown96de1e62005-11-08 21:39:39 -08004968static struct md_sysfs_entry
4969raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004970
NeilBrown007583c2005-11-08 21:39:30 -08004971static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004972 &raid5_stripecache_size.attr,
4973 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004974 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004975 NULL,
4976};
NeilBrown007583c2005-11-08 21:39:30 -08004977static struct attribute_group raid5_attrs_group = {
4978 .name = NULL,
4979 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004980};
4981
Dan Williams80c3a6c2009-03-17 18:10:40 -07004982static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004983raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004984{
NeilBrownd1688a62011-10-11 16:49:52 +11004985 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004986
4987 if (!sectors)
4988 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004989 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004990 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004991 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004992
Andre Noll9d8f0362009-06-18 08:45:01 +10004993 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004994 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004995 return sectors * (raid_disks - conf->max_degraded);
4996}
4997
NeilBrownd1688a62011-10-11 16:49:52 +11004998static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004999{
5000 struct raid5_percpu *percpu;
5001 unsigned long cpu;
5002
5003 if (!conf->percpu)
5004 return;
5005
5006 get_online_cpus();
5007 for_each_possible_cpu(cpu) {
5008 percpu = per_cpu_ptr(conf->percpu, cpu);
5009 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005010 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005011 }
5012#ifdef CONFIG_HOTPLUG_CPU
5013 unregister_cpu_notifier(&conf->cpu_notify);
5014#endif
5015 put_online_cpus();
5016
5017 free_percpu(conf->percpu);
5018}
5019
NeilBrownd1688a62011-10-11 16:49:52 +11005020static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10005021{
5022 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07005023 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005024 kfree(conf->disks);
5025 kfree(conf->stripe_hashtbl);
5026 kfree(conf);
5027}
5028
Dan Williams36d1c642009-07-14 11:48:22 -07005029#ifdef CONFIG_HOTPLUG_CPU
5030static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5031 void *hcpu)
5032{
NeilBrownd1688a62011-10-11 16:49:52 +11005033 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005034 long cpu = (long)hcpu;
5035 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5036
5037 switch (action) {
5038 case CPU_UP_PREPARE:
5039 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07005040 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07005041 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005042 if (!percpu->scribble)
5043 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5044
5045 if (!percpu->scribble ||
5046 (conf->level == 6 && !percpu->spare_page)) {
5047 safe_put_page(percpu->spare_page);
5048 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005049 pr_err("%s: failed memory allocation for cpu%ld\n",
5050 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005051 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005052 }
5053 break;
5054 case CPU_DEAD:
5055 case CPU_DEAD_FROZEN:
5056 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005057 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005058 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005059 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005060 break;
5061 default:
5062 break;
5063 }
5064 return NOTIFY_OK;
5065}
5066#endif
5067
NeilBrownd1688a62011-10-11 16:49:52 +11005068static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005069{
5070 unsigned long cpu;
5071 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005072 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005073 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005074 int err;
5075
Dan Williams36d1c642009-07-14 11:48:22 -07005076 allcpus = alloc_percpu(struct raid5_percpu);
5077 if (!allcpus)
5078 return -ENOMEM;
5079 conf->percpu = allcpus;
5080
5081 get_online_cpus();
5082 err = 0;
5083 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005084 if (conf->level == 6) {
5085 spare_page = alloc_page(GFP_KERNEL);
5086 if (!spare_page) {
5087 err = -ENOMEM;
5088 break;
5089 }
5090 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5091 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005092 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005093 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005094 err = -ENOMEM;
5095 break;
5096 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005097 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005098 }
5099#ifdef CONFIG_HOTPLUG_CPU
5100 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5101 conf->cpu_notify.priority = 0;
5102 if (err == 0)
5103 err = register_cpu_notifier(&conf->cpu_notify);
5104#endif
5105 put_online_cpus();
5106
5107 return err;
5108}
5109
NeilBrownd1688a62011-10-11 16:49:52 +11005110static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111{
NeilBrownd1688a62011-10-11 16:49:52 +11005112 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005113 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005114 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005116 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117
NeilBrown91adb562009-03-31 14:39:39 +11005118 if (mddev->new_level != 5
5119 && mddev->new_level != 4
5120 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005121 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005122 mdname(mddev), mddev->new_level);
5123 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 }
NeilBrown91adb562009-03-31 14:39:39 +11005125 if ((mddev->new_level == 5
5126 && !algorithm_valid_raid5(mddev->new_layout)) ||
5127 (mddev->new_level == 6
5128 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005129 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005130 mdname(mddev), mddev->new_layout);
5131 return ERR_PTR(-EIO);
5132 }
5133 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005134 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005135 mdname(mddev), mddev->raid_disks);
5136 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138
Andre Noll664e7c42009-06-18 08:45:27 +10005139 if (!mddev->new_chunk_sectors ||
5140 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5141 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005142 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5143 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005144 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005145 }
5146
NeilBrownd1688a62011-10-11 16:49:52 +11005147 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005148 if (conf == NULL)
5149 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005150 spin_lock_init(&conf->device_lock);
5151 init_waitqueue_head(&conf->wait_for_stripe);
5152 init_waitqueue_head(&conf->wait_for_overlap);
5153 INIT_LIST_HEAD(&conf->handle_list);
5154 INIT_LIST_HEAD(&conf->hold_list);
5155 INIT_LIST_HEAD(&conf->delayed_list);
5156 INIT_LIST_HEAD(&conf->bitmap_list);
5157 INIT_LIST_HEAD(&conf->inactive_list);
5158 atomic_set(&conf->active_stripes, 0);
5159 atomic_set(&conf->preread_active_stripes, 0);
5160 atomic_set(&conf->active_aligned_reads, 0);
5161 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005162 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005163
5164 conf->raid_disks = mddev->raid_disks;
5165 if (mddev->reshape_position == MaxSector)
5166 conf->previous_raid_disks = mddev->raid_disks;
5167 else
5168 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005169 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5170 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005171
NeilBrown5e5e3e72009-10-16 16:35:30 +11005172 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005173 GFP_KERNEL);
5174 if (!conf->disks)
5175 goto abort;
5176
5177 conf->mddev = mddev;
5178
5179 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5180 goto abort;
5181
Dan Williams36d1c642009-07-14 11:48:22 -07005182 conf->level = mddev->new_level;
5183 if (raid5_alloc_percpu(conf) != 0)
5184 goto abort;
5185
NeilBrown0c55e022010-05-03 14:09:02 +10005186 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005187
NeilBrowndafb20f2012-03-19 12:46:39 +11005188 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005189 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005190 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005191 || raid_disk < 0)
5192 continue;
5193 disk = conf->disks + raid_disk;
5194
NeilBrown17045f52011-12-23 10:17:53 +11005195 if (test_bit(Replacement, &rdev->flags)) {
5196 if (disk->replacement)
5197 goto abort;
5198 disk->replacement = rdev;
5199 } else {
5200 if (disk->rdev)
5201 goto abort;
5202 disk->rdev = rdev;
5203 }
NeilBrown91adb562009-03-31 14:39:39 +11005204
5205 if (test_bit(In_sync, &rdev->flags)) {
5206 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005207 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5208 " disk %d\n",
5209 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005210 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005211 /* Cannot rely on bitmap to complete recovery */
5212 conf->fullsync = 1;
5213 }
5214
Andre Noll09c9e5f2009-06-18 08:45:55 +10005215 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005216 conf->level = mddev->new_level;
5217 if (conf->level == 6)
5218 conf->max_degraded = 2;
5219 else
5220 conf->max_degraded = 1;
5221 conf->algorithm = mddev->new_layout;
5222 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005223 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005224 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005225 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005226 conf->prev_algo = mddev->layout;
5227 }
NeilBrown91adb562009-03-31 14:39:39 +11005228
5229 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005230 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005231 if (grow_stripes(conf, conf->max_nr_stripes)) {
5232 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005233 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5234 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005235 goto abort;
5236 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005237 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5238 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005239
NeilBrown02326052012-07-03 15:56:52 +10005240 sprintf(pers_name, "raid%d", mddev->new_level);
5241 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005242 if (!conf->thread) {
5243 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005244 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005245 mdname(mddev));
5246 goto abort;
5247 }
5248
5249 return conf;
5250
5251 abort:
5252 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005253 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005254 return ERR_PTR(-EIO);
5255 } else
5256 return ERR_PTR(-ENOMEM);
5257}
5258
NeilBrownc148ffd2009-11-13 17:47:00 +11005259
5260static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5261{
5262 switch (algo) {
5263 case ALGORITHM_PARITY_0:
5264 if (raid_disk < max_degraded)
5265 return 1;
5266 break;
5267 case ALGORITHM_PARITY_N:
5268 if (raid_disk >= raid_disks - max_degraded)
5269 return 1;
5270 break;
5271 case ALGORITHM_PARITY_0_6:
5272 if (raid_disk == 0 ||
5273 raid_disk == raid_disks - 1)
5274 return 1;
5275 break;
5276 case ALGORITHM_LEFT_ASYMMETRIC_6:
5277 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5278 case ALGORITHM_LEFT_SYMMETRIC_6:
5279 case ALGORITHM_RIGHT_SYMMETRIC_6:
5280 if (raid_disk == raid_disks - 1)
5281 return 1;
5282 }
5283 return 0;
5284}
5285
NeilBrownfd01b882011-10-11 16:47:53 +11005286static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005287{
NeilBrownd1688a62011-10-11 16:49:52 +11005288 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005289 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005290 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005291 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005292 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005293 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005294 long long min_offset_diff = 0;
5295 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005296
Andre Noll8c6ac862009-06-18 08:48:06 +10005297 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005298 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005299 " -- starting background reconstruction\n",
5300 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005301
5302 rdev_for_each(rdev, mddev) {
5303 long long diff;
5304 if (rdev->raid_disk < 0)
5305 continue;
5306 diff = (rdev->new_data_offset - rdev->data_offset);
5307 if (first) {
5308 min_offset_diff = diff;
5309 first = 0;
5310 } else if (mddev->reshape_backwards &&
5311 diff < min_offset_diff)
5312 min_offset_diff = diff;
5313 else if (!mddev->reshape_backwards &&
5314 diff > min_offset_diff)
5315 min_offset_diff = diff;
5316 }
5317
NeilBrownf6705572006-03-27 01:18:11 -08005318 if (mddev->reshape_position != MaxSector) {
5319 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005320 * Difficulties arise if the stripe we would write to
5321 * next is at or after the stripe we would read from next.
5322 * For a reshape that changes the number of devices, this
5323 * is only possible for a very short time, and mdadm makes
5324 * sure that time appears to have past before assembling
5325 * the array. So we fail if that time hasn't passed.
5326 * For a reshape that keeps the number of devices the same
5327 * mdadm must be monitoring the reshape can keeping the
5328 * critical areas read-only and backed up. It will start
5329 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005330 */
5331 sector_t here_new, here_old;
5332 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005333 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005334
NeilBrown88ce4932009-03-31 15:24:23 +11005335 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005336 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005337 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005338 mdname(mddev));
5339 return -EINVAL;
5340 }
NeilBrownf6705572006-03-27 01:18:11 -08005341 old_disks = mddev->raid_disks - mddev->delta_disks;
5342 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005343 * further up in new geometry must map after here in old
5344 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005345 */
5346 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005347 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005348 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005349 printk(KERN_ERR "md/raid:%s: reshape_position not "
5350 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005351 return -EINVAL;
5352 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005353 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005354 /* here_new is the stripe we will write to */
5355 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005356 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005357 (old_disks-max_degraded));
5358 /* here_old is the first stripe that we might need to read
5359 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005360 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005361 if ((here_new * mddev->new_chunk_sectors !=
5362 here_old * mddev->chunk_sectors)) {
5363 printk(KERN_ERR "md/raid:%s: reshape position is"
5364 " confused - aborting\n", mdname(mddev));
5365 return -EINVAL;
5366 }
NeilBrown67ac6012009-08-13 10:06:24 +10005367 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005368 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005369 * and taking constant backups.
5370 * mdadm always starts a situation like this in
5371 * readonly mode so it can take control before
5372 * allowing any writes. So just check for that.
5373 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005374 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5375 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5376 /* not really in-place - so OK */;
5377 else if (mddev->ro == 0) {
5378 printk(KERN_ERR "md/raid:%s: in-place reshape "
5379 "must be started in read-only mode "
5380 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005381 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005382 return -EINVAL;
5383 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005384 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005385 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005386 here_old * mddev->chunk_sectors)
5387 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005388 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005389 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005390 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5391 "auto-recovery - aborting.\n",
5392 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005393 return -EINVAL;
5394 }
NeilBrown0c55e022010-05-03 14:09:02 +10005395 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5396 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005397 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005398 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005399 BUG_ON(mddev->level != mddev->new_level);
5400 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005401 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005402 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005403 }
5404
NeilBrown245f46c2009-03-31 14:39:39 +11005405 if (mddev->private == NULL)
5406 conf = setup_conf(mddev);
5407 else
5408 conf = mddev->private;
5409
NeilBrown91adb562009-03-31 14:39:39 +11005410 if (IS_ERR(conf))
5411 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005412
NeilBrownb5254dd2012-05-21 09:27:01 +10005413 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005414 mddev->thread = conf->thread;
5415 conf->thread = NULL;
5416 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
NeilBrown17045f52011-12-23 10:17:53 +11005418 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5419 i++) {
5420 rdev = conf->disks[i].rdev;
5421 if (!rdev && conf->disks[i].replacement) {
5422 /* The replacement is all we have yet */
5423 rdev = conf->disks[i].replacement;
5424 conf->disks[i].replacement = NULL;
5425 clear_bit(Replacement, &rdev->flags);
5426 conf->disks[i].rdev = rdev;
5427 }
5428 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005429 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005430 if (conf->disks[i].replacement &&
5431 conf->reshape_progress != MaxSector) {
5432 /* replacements and reshape simply do not mix. */
5433 printk(KERN_ERR "md: cannot handle concurrent "
5434 "replacement and reshape.\n");
5435 goto abort;
5436 }
NeilBrown2f115882010-06-17 17:41:03 +10005437 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005438 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005439 continue;
5440 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005441 /* This disc is not fully in-sync. However if it
5442 * just stored parity (beyond the recovery_offset),
5443 * when we don't need to be concerned about the
5444 * array being dirty.
5445 * When reshape goes 'backwards', we never have
5446 * partially completed devices, so we only need
5447 * to worry about reshape going forwards.
5448 */
5449 /* Hack because v0.91 doesn't store recovery_offset properly. */
5450 if (mddev->major_version == 0 &&
5451 mddev->minor_version > 90)
5452 rdev->recovery_offset = reshape_offset;
5453
NeilBrownc148ffd2009-11-13 17:47:00 +11005454 if (rdev->recovery_offset < reshape_offset) {
5455 /* We need to check old and new layout */
5456 if (!only_parity(rdev->raid_disk,
5457 conf->algorithm,
5458 conf->raid_disks,
5459 conf->max_degraded))
5460 continue;
5461 }
5462 if (!only_parity(rdev->raid_disk,
5463 conf->prev_algo,
5464 conf->previous_raid_disks,
5465 conf->max_degraded))
5466 continue;
5467 dirty_parity_disks++;
5468 }
NeilBrown91adb562009-03-31 14:39:39 +11005469
NeilBrown17045f52011-12-23 10:17:53 +11005470 /*
5471 * 0 for a fully functional array, 1 or 2 for a degraded array.
5472 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005473 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
NeilBrown674806d2010-06-16 17:17:53 +10005475 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005476 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005478 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 goto abort;
5480 }
5481
NeilBrown91adb562009-03-31 14:39:39 +11005482 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005483 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005484 mddev->resync_max_sectors = mddev->dev_sectors;
5485
NeilBrownc148ffd2009-11-13 17:47:00 +11005486 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005488 if (mddev->ok_start_degraded)
5489 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005490 "md/raid:%s: starting dirty degraded array"
5491 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005492 mdname(mddev));
5493 else {
5494 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005495 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005496 mdname(mddev));
5497 goto abort;
5498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 }
5500
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005502 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5503 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005504 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5505 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 else
NeilBrown0c55e022010-05-03 14:09:02 +10005507 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5508 " out of %d devices, algorithm %d\n",
5509 mdname(mddev), conf->level,
5510 mddev->raid_disks - mddev->degraded,
5511 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512
5513 print_raid5_conf(conf);
5514
NeilBrownfef9c612009-03-31 15:16:46 +11005515 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005516 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005517 atomic_set(&conf->reshape_stripes, 0);
5518 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5519 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5520 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5521 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5522 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005523 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005524 }
5525
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526
5527 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005528 if (mddev->to_remove == &raid5_attrs_group)
5529 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005530 else if (mddev->kobj.sd &&
5531 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005532 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005533 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005534 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005535 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5536
5537 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005538 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005539 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005540 /* read-ahead size must cover two whole stripes, which
5541 * is 2 * (datadisks) * chunksize where 'n' is the
5542 * number of raid devices
5543 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5545 int stripe = data_disks *
5546 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5547 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5548 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005549
5550 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005551
5552 mddev->queue->backing_dev_info.congested_data = mddev;
5553 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005554
5555 chunk_size = mddev->chunk_sectors << 9;
5556 blk_queue_io_min(mddev->queue, chunk_size);
5557 blk_queue_io_opt(mddev->queue, chunk_size *
5558 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005559 /*
5560 * We can only discard a whole stripe. It doesn't make sense to
5561 * discard data disk but write parity disk
5562 */
5563 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11005564 /* Round up to power of 2, as discard handling
5565 * currently assumes that */
5566 while ((stripe-1) & stripe)
5567 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11005568 mddev->queue->limits.discard_alignment = stripe;
5569 mddev->queue->limits.discard_granularity = stripe;
5570 /*
5571 * unaligned part of discard request will be ignored, so can't
5572 * guarantee discard_zerors_data
5573 */
5574 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005575
NeilBrown05616be2012-05-21 09:27:00 +10005576 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005577 disk_stack_limits(mddev->gendisk, rdev->bdev,
5578 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005579 disk_stack_limits(mddev->gendisk, rdev->bdev,
5580 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005581 /*
5582 * discard_zeroes_data is required, otherwise data
5583 * could be lost. Consider a scenario: discard a stripe
5584 * (the stripe could be inconsistent if
5585 * discard_zeroes_data is 0); write one disk of the
5586 * stripe (the stripe could be inconsistent again
5587 * depending on which disks are used to calculate
5588 * parity); the disk is broken; The stripe data of this
5589 * disk is lost.
5590 */
5591 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5592 !bdev_get_queue(rdev->bdev)->
5593 limits.discard_zeroes_data)
5594 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10005595 }
Shaohua Li620125f2012-10-11 13:49:05 +11005596
5597 if (discard_supported &&
5598 mddev->queue->limits.max_discard_sectors >= stripe &&
5599 mddev->queue->limits.discard_granularity >= stripe)
5600 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5601 mddev->queue);
5602 else
5603 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5604 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 }
5606
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 return 0;
5608abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005609 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005610 print_raid5_conf(conf);
5611 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005613 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 return -EIO;
5615}
5616
NeilBrownfd01b882011-10-11 16:47:53 +11005617static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618{
NeilBrownd1688a62011-10-11 16:49:52 +11005619 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620
NeilBrown01f96c02011-09-21 15:30:20 +10005621 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005622 if (mddev->queue)
5623 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005624 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005625 mddev->private = NULL;
5626 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 return 0;
5628}
5629
NeilBrownfd01b882011-10-11 16:47:53 +11005630static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631{
NeilBrownd1688a62011-10-11 16:49:52 +11005632 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 int i;
5634
Andre Noll9d8f0362009-06-18 08:45:01 +10005635 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5636 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005637 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 for (i = 0; i < conf->raid_disks; i++)
5639 seq_printf (seq, "%s",
5640 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005641 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643}
5644
NeilBrownd1688a62011-10-11 16:49:52 +11005645static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646{
5647 int i;
5648 struct disk_info *tmp;
5649
NeilBrown0c55e022010-05-03 14:09:02 +10005650 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 if (!conf) {
5652 printk("(conf==NULL)\n");
5653 return;
5654 }
NeilBrown0c55e022010-05-03 14:09:02 +10005655 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5656 conf->raid_disks,
5657 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658
5659 for (i = 0; i < conf->raid_disks; i++) {
5660 char b[BDEVNAME_SIZE];
5661 tmp = conf->disks + i;
5662 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005663 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5664 i, !test_bit(Faulty, &tmp->rdev->flags),
5665 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 }
5667}
5668
NeilBrownfd01b882011-10-11 16:47:53 +11005669static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670{
5671 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005672 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005674 int count = 0;
5675 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676
5677 for (i = 0; i < conf->raid_disks; i++) {
5678 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005679 if (tmp->replacement
5680 && tmp->replacement->recovery_offset == MaxSector
5681 && !test_bit(Faulty, &tmp->replacement->flags)
5682 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5683 /* Replacement has just become active. */
5684 if (!tmp->rdev
5685 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5686 count++;
5687 if (tmp->rdev) {
5688 /* Replaced device not technically faulty,
5689 * but we need to be sure it gets removed
5690 * and never re-added.
5691 */
5692 set_bit(Faulty, &tmp->rdev->flags);
5693 sysfs_notify_dirent_safe(
5694 tmp->rdev->sysfs_state);
5695 }
5696 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5697 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005698 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005699 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005700 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005701 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005702 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 }
5704 }
NeilBrown6b965622010-08-18 11:56:59 +10005705 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005706 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005707 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005709 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710}
5711
NeilBrownb8321b62011-12-23 10:17:51 +11005712static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713{
NeilBrownd1688a62011-10-11 16:49:52 +11005714 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005716 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005717 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 struct disk_info *p = conf->disks + number;
5719
5720 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005721 if (rdev == p->rdev)
5722 rdevp = &p->rdev;
5723 else if (rdev == p->replacement)
5724 rdevp = &p->replacement;
5725 else
5726 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005727
NeilBrown657e3e42011-12-23 10:17:52 +11005728 if (number >= conf->raid_disks &&
5729 conf->reshape_progress == MaxSector)
5730 clear_bit(In_sync, &rdev->flags);
5731
5732 if (test_bit(In_sync, &rdev->flags) ||
5733 atomic_read(&rdev->nr_pending)) {
5734 err = -EBUSY;
5735 goto abort;
5736 }
5737 /* Only remove non-faulty devices if recovery
5738 * isn't possible.
5739 */
5740 if (!test_bit(Faulty, &rdev->flags) &&
5741 mddev->recovery_disabled != conf->recovery_disabled &&
5742 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005743 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005744 number < conf->raid_disks) {
5745 err = -EBUSY;
5746 goto abort;
5747 }
5748 *rdevp = NULL;
5749 synchronize_rcu();
5750 if (atomic_read(&rdev->nr_pending)) {
5751 /* lost the race, try later */
5752 err = -EBUSY;
5753 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005754 } else if (p->replacement) {
5755 /* We must have just cleared 'rdev' */
5756 p->rdev = p->replacement;
5757 clear_bit(Replacement, &p->replacement->flags);
5758 smp_mb(); /* Make sure other CPUs may see both as identical
5759 * but will never see neither - if they are careful
5760 */
5761 p->replacement = NULL;
5762 clear_bit(WantReplacement, &rdev->flags);
5763 } else
5764 /* We might have just removed the Replacement as faulty-
5765 * clear the bit just in case
5766 */
5767 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768abort:
5769
5770 print_raid5_conf(conf);
5771 return err;
5772}
5773
NeilBrownfd01b882011-10-11 16:47:53 +11005774static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775{
NeilBrownd1688a62011-10-11 16:49:52 +11005776 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005777 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 int disk;
5779 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005780 int first = 0;
5781 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782
NeilBrown7f0da592011-07-28 11:39:22 +10005783 if (mddev->recovery_disabled == conf->recovery_disabled)
5784 return -EBUSY;
5785
NeilBrowndc10c642012-03-19 12:46:37 +11005786 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005788 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789
Neil Brown6c2fce22008-06-28 08:31:31 +10005790 if (rdev->raid_disk >= 0)
5791 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792
5793 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005794 * find the disk ... but prefer rdev->saved_raid_disk
5795 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005797 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005798 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005799 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005800 first = rdev->saved_raid_disk;
5801
5802 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005803 p = conf->disks + disk;
5804 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005805 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005807 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005808 if (rdev->saved_raid_disk != disk)
5809 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005810 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005811 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005813 }
5814 for (disk = first; disk <= last; disk++) {
5815 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005816 if (test_bit(WantReplacement, &p->rdev->flags) &&
5817 p->replacement == NULL) {
5818 clear_bit(In_sync, &rdev->flags);
5819 set_bit(Replacement, &rdev->flags);
5820 rdev->raid_disk = disk;
5821 err = 0;
5822 conf->fullsync = 1;
5823 rcu_assign_pointer(p->replacement, rdev);
5824 break;
5825 }
5826 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005827out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005829 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830}
5831
NeilBrownfd01b882011-10-11 16:47:53 +11005832static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833{
5834 /* no resync is happening, and there is enough space
5835 * on all devices, so we can resize.
5836 * We need to make sure resync covers any new space.
5837 * If the array is shrinking we should possibly wait until
5838 * any io in the removed space completes, but it hardly seems
5839 * worth it.
5840 */
NeilBrowna4a61252012-05-22 13:55:27 +10005841 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005842 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005843 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5844 if (mddev->external_size &&
5845 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005846 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005847 if (mddev->bitmap) {
5848 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5849 if (ret)
5850 return ret;
5851 }
5852 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005853 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005854 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005855 if (sectors > mddev->dev_sectors &&
5856 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005857 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5859 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005860 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005861 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 return 0;
5863}
5864
NeilBrownfd01b882011-10-11 16:47:53 +11005865static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005866{
5867 /* Can only proceed if there are plenty of stripe_heads.
5868 * We need a minimum of one full stripe,, and for sensible progress
5869 * it is best to have about 4 times that.
5870 * If we require 4 times, then the default 256 4K stripe_heads will
5871 * allow for chunk sizes up to 256K, which is probably OK.
5872 * If the chunk size is greater, user-space should request more
5873 * stripe_heads first.
5874 */
NeilBrownd1688a62011-10-11 16:49:52 +11005875 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005876 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5877 > conf->max_nr_stripes ||
5878 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5879 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005880 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5881 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005882 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5883 / STRIPE_SIZE)*4);
5884 return 0;
5885 }
5886 return 1;
5887}
5888
NeilBrownfd01b882011-10-11 16:47:53 +11005889static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005890{
NeilBrownd1688a62011-10-11 16:49:52 +11005891 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005892
NeilBrown88ce4932009-03-31 15:24:23 +11005893 if (mddev->delta_disks == 0 &&
5894 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005895 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005896 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005897 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005898 return -EINVAL;
5899 if (mddev->delta_disks < 0) {
5900 /* We might be able to shrink, but the devices must
5901 * be made bigger first.
5902 * For raid6, 4 is the minimum size.
5903 * Otherwise 2 is the minimum
5904 */
5905 int min = 2;
5906 if (mddev->level == 6)
5907 min = 4;
5908 if (mddev->raid_disks + mddev->delta_disks < min)
5909 return -EINVAL;
5910 }
NeilBrown29269552006-03-27 01:18:10 -08005911
NeilBrown01ee22b2009-06-18 08:47:20 +10005912 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005913 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005914
NeilBrowne56108d62012-10-11 14:24:13 +11005915 return resize_stripes(conf, (conf->previous_raid_disks
5916 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08005917}
5918
NeilBrownfd01b882011-10-11 16:47:53 +11005919static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005920{
NeilBrownd1688a62011-10-11 16:49:52 +11005921 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005922 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005923 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005924 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005925
NeilBrownf4168852007-02-28 20:11:53 -08005926 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005927 return -EBUSY;
5928
NeilBrown01ee22b2009-06-18 08:47:20 +10005929 if (!check_stripe_cache(mddev))
5930 return -ENOSPC;
5931
NeilBrown30b67642012-05-22 13:55:28 +10005932 if (has_failed(conf))
5933 return -EINVAL;
5934
NeilBrownc6563a82012-05-21 09:27:00 +10005935 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005936 if (!test_bit(In_sync, &rdev->flags)
5937 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005938 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005939 }
NeilBrown63c70c42006-03-27 01:18:13 -08005940
NeilBrownf4168852007-02-28 20:11:53 -08005941 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005942 /* Not enough devices even to make a degraded array
5943 * of that size
5944 */
5945 return -EINVAL;
5946
NeilBrownec32a2b2009-03-31 15:17:38 +11005947 /* Refuse to reduce size of the array. Any reductions in
5948 * array size must be through explicit setting of array_size
5949 * attribute.
5950 */
5951 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5952 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005953 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005954 "before number of disks\n", mdname(mddev));
5955 return -EINVAL;
5956 }
5957
NeilBrownf6705572006-03-27 01:18:11 -08005958 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005959 spin_lock_irq(&conf->device_lock);
5960 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005961 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005962 conf->prev_chunk_sectors = conf->chunk_sectors;
5963 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005964 conf->prev_algo = conf->algorithm;
5965 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005966 conf->generation++;
5967 /* Code that selects data_offset needs to see the generation update
5968 * if reshape_progress has been set - so a memory barrier needed.
5969 */
5970 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005971 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005972 conf->reshape_progress = raid5_size(mddev, 0, 0);
5973 else
5974 conf->reshape_progress = 0;
5975 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005976 spin_unlock_irq(&conf->device_lock);
5977
5978 /* Add some new drives, as many as will fit.
5979 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005980 * Don't add devices if we are reducing the number of
5981 * devices in the array. This is because it is not possible
5982 * to correctly record the "partially reconstructed" state of
5983 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005984 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005985 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005986 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005987 if (rdev->raid_disk < 0 &&
5988 !test_bit(Faulty, &rdev->flags)) {
5989 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005990 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005991 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005992 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005993 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005994 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005995
5996 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005997 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005998 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005999 } else if (rdev->raid_disk >= conf->previous_raid_disks
6000 && !test_bit(Faulty, &rdev->flags)) {
6001 /* This is a spare that was manually added */
6002 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11006003 }
NeilBrown29269552006-03-27 01:18:10 -08006004
NeilBrown87a8dec2011-01-31 11:57:43 +11006005 /* When a reshape changes the number of devices,
6006 * ->degraded is measured against the larger of the
6007 * pre and post number of devices.
6008 */
NeilBrownec32a2b2009-03-31 15:17:38 +11006009 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006010 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11006011 spin_unlock_irqrestore(&conf->device_lock, flags);
6012 }
NeilBrown63c70c42006-03-27 01:18:13 -08006013 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10006014 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07006015 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08006016
NeilBrown29269552006-03-27 01:18:10 -08006017 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6018 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6019 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6020 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6021 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006022 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08006023 if (!mddev->sync_thread) {
6024 mddev->recovery = 0;
6025 spin_lock_irq(&conf->device_lock);
6026 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006027 rdev_for_each(rdev, mddev)
6028 rdev->new_data_offset = rdev->data_offset;
6029 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006030 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11006031 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08006032 spin_unlock_irq(&conf->device_lock);
6033 return -EAGAIN;
6034 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006035 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006036 md_wakeup_thread(mddev->sync_thread);
6037 md_new_event(mddev);
6038 return 0;
6039}
NeilBrown29269552006-03-27 01:18:10 -08006040
NeilBrownec32a2b2009-03-31 15:17:38 +11006041/* This is called from the reshape thread and should make any
6042 * changes needed in 'conf'
6043 */
NeilBrownd1688a62011-10-11 16:49:52 +11006044static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006045{
NeilBrown29269552006-03-27 01:18:10 -08006046
NeilBrownf6705572006-03-27 01:18:11 -08006047 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006048 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006049
NeilBrownf6705572006-03-27 01:18:11 -08006050 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006051 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006052 rdev_for_each(rdev, conf->mddev)
6053 rdev->data_offset = rdev->new_data_offset;
6054 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006055 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006056 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006057 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006058
6059 /* read-ahead size must cover two whole stripes, which is
6060 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6061 */
NeilBrown4a5add42010-06-01 19:37:28 +10006062 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006063 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006064 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006065 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006066 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6067 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6068 }
NeilBrown29269552006-03-27 01:18:10 -08006069 }
NeilBrown29269552006-03-27 01:18:10 -08006070}
6071
NeilBrownec32a2b2009-03-31 15:17:38 +11006072/* This is called from the raid5d thread with mddev_lock held.
6073 * It makes config changes to the device.
6074 */
NeilBrownfd01b882011-10-11 16:47:53 +11006075static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006076{
NeilBrownd1688a62011-10-11 16:49:52 +11006077 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006078
6079 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6080
NeilBrownec32a2b2009-03-31 15:17:38 +11006081 if (mddev->delta_disks > 0) {
6082 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6083 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006084 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006085 } else {
6086 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006087 spin_lock_irq(&conf->device_lock);
6088 mddev->degraded = calc_degraded(conf);
6089 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006090 for (d = conf->raid_disks ;
6091 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006092 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006093 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006094 if (rdev)
6095 clear_bit(In_sync, &rdev->flags);
6096 rdev = conf->disks[d].replacement;
6097 if (rdev)
6098 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006099 }
NeilBrowncea9c222009-03-31 15:15:05 +11006100 }
NeilBrown88ce4932009-03-31 15:24:23 +11006101 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006102 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006103 mddev->reshape_position = MaxSector;
6104 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006105 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006106 }
6107}
6108
NeilBrownfd01b882011-10-11 16:47:53 +11006109static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006110{
NeilBrownd1688a62011-10-11 16:49:52 +11006111 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006112
6113 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006114 case 2: /* resume for a suspend */
6115 wake_up(&conf->wait_for_overlap);
6116 break;
6117
NeilBrown72626682005-09-09 16:23:54 -07006118 case 1: /* stop all writes */
6119 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006120 /* '2' tells resync/reshape to pause so that all
6121 * active stripes can drain
6122 */
6123 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006124 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006125 atomic_read(&conf->active_stripes) == 0 &&
6126 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07006127 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10006128 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006129 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006130 /* allow reshape to continue */
6131 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006132 break;
6133
6134 case 0: /* re-enable writes */
6135 spin_lock_irq(&conf->device_lock);
6136 conf->quiesce = 0;
6137 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006138 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006139 spin_unlock_irq(&conf->device_lock);
6140 break;
6141 }
NeilBrown72626682005-09-09 16:23:54 -07006142}
NeilBrownb15c2e52006-01-06 00:20:16 -08006143
NeilBrownd562b0c2009-03-31 14:39:39 +11006144
NeilBrownfd01b882011-10-11 16:47:53 +11006145static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006146{
NeilBrowne373ab12011-10-11 16:48:59 +11006147 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006148 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006149
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006150 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006151 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006152 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6153 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006154 return ERR_PTR(-EINVAL);
6155 }
6156
NeilBrowne373ab12011-10-11 16:48:59 +11006157 sectors = raid0_conf->strip_zone[0].zone_end;
6158 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006159 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006160 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006161 mddev->new_layout = ALGORITHM_PARITY_N;
6162 mddev->new_chunk_sectors = mddev->chunk_sectors;
6163 mddev->raid_disks += 1;
6164 mddev->delta_disks = 1;
6165 /* make sure it will be not marked as dirty */
6166 mddev->recovery_cp = MaxSector;
6167
6168 return setup_conf(mddev);
6169}
6170
6171
NeilBrownfd01b882011-10-11 16:47:53 +11006172static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006173{
6174 int chunksect;
6175
6176 if (mddev->raid_disks != 2 ||
6177 mddev->degraded > 1)
6178 return ERR_PTR(-EINVAL);
6179
6180 /* Should check if there are write-behind devices? */
6181
6182 chunksect = 64*2; /* 64K by default */
6183
6184 /* The array must be an exact multiple of chunksize */
6185 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6186 chunksect >>= 1;
6187
6188 if ((chunksect<<9) < STRIPE_SIZE)
6189 /* array size does not allow a suitable chunk size */
6190 return ERR_PTR(-EINVAL);
6191
6192 mddev->new_level = 5;
6193 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006194 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006195
6196 return setup_conf(mddev);
6197}
6198
NeilBrownfd01b882011-10-11 16:47:53 +11006199static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006200{
6201 int new_layout;
6202
6203 switch (mddev->layout) {
6204 case ALGORITHM_LEFT_ASYMMETRIC_6:
6205 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6206 break;
6207 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6208 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6209 break;
6210 case ALGORITHM_LEFT_SYMMETRIC_6:
6211 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6212 break;
6213 case ALGORITHM_RIGHT_SYMMETRIC_6:
6214 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6215 break;
6216 case ALGORITHM_PARITY_0_6:
6217 new_layout = ALGORITHM_PARITY_0;
6218 break;
6219 case ALGORITHM_PARITY_N:
6220 new_layout = ALGORITHM_PARITY_N;
6221 break;
6222 default:
6223 return ERR_PTR(-EINVAL);
6224 }
6225 mddev->new_level = 5;
6226 mddev->new_layout = new_layout;
6227 mddev->delta_disks = -1;
6228 mddev->raid_disks -= 1;
6229 return setup_conf(mddev);
6230}
6231
NeilBrownd562b0c2009-03-31 14:39:39 +11006232
NeilBrownfd01b882011-10-11 16:47:53 +11006233static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006234{
NeilBrown88ce4932009-03-31 15:24:23 +11006235 /* For a 2-drive array, the layout and chunk size can be changed
6236 * immediately as not restriping is needed.
6237 * For larger arrays we record the new value - after validation
6238 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006239 */
NeilBrownd1688a62011-10-11 16:49:52 +11006240 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006241 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006242
NeilBrown597a7112009-06-18 08:47:42 +10006243 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006244 return -EINVAL;
6245 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006246 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006247 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006248 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006249 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006250 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006251 /* not factor of array size */
6252 return -EINVAL;
6253 }
6254
6255 /* They look valid */
6256
NeilBrown88ce4932009-03-31 15:24:23 +11006257 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006258 /* can make the change immediately */
6259 if (mddev->new_layout >= 0) {
6260 conf->algorithm = mddev->new_layout;
6261 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006262 }
6263 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006264 conf->chunk_sectors = new_chunk ;
6265 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006266 }
6267 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6268 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006269 }
NeilBrown50ac1682009-06-18 08:47:55 +10006270 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006271}
6272
NeilBrownfd01b882011-10-11 16:47:53 +11006273static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006274{
NeilBrown597a7112009-06-18 08:47:42 +10006275 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006276
NeilBrown597a7112009-06-18 08:47:42 +10006277 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006278 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006279 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006280 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006281 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006282 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006283 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006284 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006285 /* not factor of array size */
6286 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006287 }
NeilBrown88ce4932009-03-31 15:24:23 +11006288
6289 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006290 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006291}
6292
NeilBrownfd01b882011-10-11 16:47:53 +11006293static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006294{
6295 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006296 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006297 * raid1 - if there are two drives. We need to know the chunk size
6298 * raid4 - trivial - just use a raid4 layout.
6299 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006300 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006301 if (mddev->level == 0)
6302 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006303 if (mddev->level == 1)
6304 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006305 if (mddev->level == 4) {
6306 mddev->new_layout = ALGORITHM_PARITY_N;
6307 mddev->new_level = 5;
6308 return setup_conf(mddev);
6309 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006310 if (mddev->level == 6)
6311 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006312
6313 return ERR_PTR(-EINVAL);
6314}
6315
NeilBrownfd01b882011-10-11 16:47:53 +11006316static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006317{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006318 /* raid4 can take over:
6319 * raid0 - if there is only one strip zone
6320 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006321 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006322 if (mddev->level == 0)
6323 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006324 if (mddev->level == 5 &&
6325 mddev->layout == ALGORITHM_PARITY_N) {
6326 mddev->new_layout = 0;
6327 mddev->new_level = 4;
6328 return setup_conf(mddev);
6329 }
6330 return ERR_PTR(-EINVAL);
6331}
NeilBrownd562b0c2009-03-31 14:39:39 +11006332
NeilBrown84fc4b52011-10-11 16:49:58 +11006333static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006334
NeilBrownfd01b882011-10-11 16:47:53 +11006335static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006336{
6337 /* Currently can only take over a raid5. We map the
6338 * personality to an equivalent raid6 personality
6339 * with the Q block at the end.
6340 */
6341 int new_layout;
6342
6343 if (mddev->pers != &raid5_personality)
6344 return ERR_PTR(-EINVAL);
6345 if (mddev->degraded > 1)
6346 return ERR_PTR(-EINVAL);
6347 if (mddev->raid_disks > 253)
6348 return ERR_PTR(-EINVAL);
6349 if (mddev->raid_disks < 3)
6350 return ERR_PTR(-EINVAL);
6351
6352 switch (mddev->layout) {
6353 case ALGORITHM_LEFT_ASYMMETRIC:
6354 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6355 break;
6356 case ALGORITHM_RIGHT_ASYMMETRIC:
6357 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6358 break;
6359 case ALGORITHM_LEFT_SYMMETRIC:
6360 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6361 break;
6362 case ALGORITHM_RIGHT_SYMMETRIC:
6363 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6364 break;
6365 case ALGORITHM_PARITY_0:
6366 new_layout = ALGORITHM_PARITY_0_6;
6367 break;
6368 case ALGORITHM_PARITY_N:
6369 new_layout = ALGORITHM_PARITY_N;
6370 break;
6371 default:
6372 return ERR_PTR(-EINVAL);
6373 }
6374 mddev->new_level = 6;
6375 mddev->new_layout = new_layout;
6376 mddev->delta_disks = 1;
6377 mddev->raid_disks += 1;
6378 return setup_conf(mddev);
6379}
6380
6381
NeilBrown84fc4b52011-10-11 16:49:58 +11006382static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006383{
6384 .name = "raid6",
6385 .level = 6,
6386 .owner = THIS_MODULE,
6387 .make_request = make_request,
6388 .run = run,
6389 .stop = stop,
6390 .status = status,
6391 .error_handler = error,
6392 .hot_add_disk = raid5_add_disk,
6393 .hot_remove_disk= raid5_remove_disk,
6394 .spare_active = raid5_spare_active,
6395 .sync_request = sync_request,
6396 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006397 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006398 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006399 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006400 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006401 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006402 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006403};
NeilBrown84fc4b52011-10-11 16:49:58 +11006404static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405{
6406 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006407 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 .owner = THIS_MODULE,
6409 .make_request = make_request,
6410 .run = run,
6411 .stop = stop,
6412 .status = status,
6413 .error_handler = error,
6414 .hot_add_disk = raid5_add_disk,
6415 .hot_remove_disk= raid5_remove_disk,
6416 .spare_active = raid5_spare_active,
6417 .sync_request = sync_request,
6418 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006419 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006420 .check_reshape = raid5_check_reshape,
6421 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006422 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006423 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006424 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425};
6426
NeilBrown84fc4b52011-10-11 16:49:58 +11006427static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428{
NeilBrown2604b702006-01-06 00:20:36 -08006429 .name = "raid4",
6430 .level = 4,
6431 .owner = THIS_MODULE,
6432 .make_request = make_request,
6433 .run = run,
6434 .stop = stop,
6435 .status = status,
6436 .error_handler = error,
6437 .hot_add_disk = raid5_add_disk,
6438 .hot_remove_disk= raid5_remove_disk,
6439 .spare_active = raid5_spare_active,
6440 .sync_request = sync_request,
6441 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006442 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006443 .check_reshape = raid5_check_reshape,
6444 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006445 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006446 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006447 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006448};
6449
6450static int __init raid5_init(void)
6451{
NeilBrown16a53ec2006-06-26 00:27:38 -07006452 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006453 register_md_personality(&raid5_personality);
6454 register_md_personality(&raid4_personality);
6455 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456}
6457
NeilBrown2604b702006-01-06 00:20:36 -08006458static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459{
NeilBrown16a53ec2006-06-26 00:27:38 -07006460 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006461 unregister_md_personality(&raid5_personality);
6462 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463}
6464
6465module_init(raid5_init);
6466module_exit(raid5_exit);
6467MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006468MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006470MODULE_ALIAS("md-raid5");
6471MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006472MODULE_ALIAS("md-level-5");
6473MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006474MODULE_ALIAS("md-personality-8"); /* RAID6 */
6475MODULE_ALIAS("md-raid6");
6476MODULE_ALIAS("md-level-6");
6477
6478/* This used to be two separate modules, they were: */
6479MODULE_ALIAS("raid5");
6480MODULE_ALIAS("raid6");