blob: 42a899728748a7caa564fe5fc7018824d6e0f3ca [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
NeilBrowna9add5d2012-10-31 11:59:09 +110056#include <trace/events/block.h>
57
NeilBrown43b2e5d2009-03-31 14:33:13 +110058#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110059#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110060#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110061#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * Stripe cache
65 */
66
67#define NR_STRIPES 256
68#define STRIPE_SIZE PAGE_SIZE
69#define STRIPE_SHIFT (PAGE_SHIFT - 9)
70#define STRIPE_SECTORS (STRIPE_SIZE>>9)
71#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070072#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080073#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define HASH_MASK (NR_HASH - 1)
75
NeilBrownd1688a62011-10-11 16:49:52 +110076static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110077{
78 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
79 return &conf->stripe_hashtbl[hash];
80}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/* bio's attached to a stripe+device for I/O are linked together in bi_sector
83 * order without overlap. There may be several bio's per stripe+device, and
84 * a bio could span several devices.
85 * When walking this list for a particular stripe+device, we must never proceed
86 * beyond a bio that extends past this device, as the next bio might no longer
87 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110088 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * of the current stripe+device
90 */
NeilBrowndb298e12011-10-07 14:23:00 +110091static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
92{
93 int sectors = bio->bi_size >> 9;
94 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
95 return bio->bi_next;
96 else
97 return NULL;
98}
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Jens Axboe960e7392008-08-15 10:41:18 +0200100/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +0200101 * We maintain a biased count of active stripes in the bottom 16 bits of
102 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200103 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000104static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200105{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000106 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
107 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200108}
109
Shaohua Lie7836bd62012-07-19 16:01:31 +1000110static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200111{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000112 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
113 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200114}
115
Shaohua Lie7836bd62012-07-19 16:01:31 +1000116static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200117{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000118 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
119 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200120}
121
Shaohua Lie7836bd62012-07-19 16:01:31 +1000122static inline void raid5_set_bi_processed_stripes(struct bio *bio,
123 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200124{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000125 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
126 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200127
Shaohua Lie7836bd62012-07-19 16:01:31 +1000128 do {
129 old = atomic_read(segments);
130 new = (old & 0xffff) | (cnt << 16);
131 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200132}
133
Shaohua Lie7836bd62012-07-19 16:01:31 +1000134static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200135{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000136 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
137 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200138}
139
NeilBrownd0dabf72009-03-31 14:39:38 +1100140/* Find first data disk in a raid6 stripe */
141static inline int raid6_d0(struct stripe_head *sh)
142{
NeilBrown67cc2b82009-03-31 14:39:38 +1100143 if (sh->ddf_layout)
144 /* ddf always start from first device */
145 return 0;
146 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100147 if (sh->qd_idx == sh->disks - 1)
148 return 0;
149 else
150 return sh->qd_idx + 1;
151}
NeilBrown16a53ec2006-06-26 00:27:38 -0700152static inline int raid6_next_disk(int disk, int raid_disks)
153{
154 disk++;
155 return (disk < raid_disks) ? disk : 0;
156}
Dan Williamsa4456852007-07-09 11:56:43 -0700157
NeilBrownd0dabf72009-03-31 14:39:38 +1100158/* When walking through the disks in a raid5, starting at raid6_d0,
159 * We need to map each disk to a 'slot', where the data disks are slot
160 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
161 * is raid_disks-1. This help does that mapping.
162 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100163static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
164 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100165{
Dan Williams66295422009-10-19 18:09:32 -0700166 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100167
NeilBrowne4424fe2009-10-16 16:27:34 +1100168 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700169 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100170 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100171 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100172 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100173 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100174 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700175 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100176 return slot;
177}
178
Dan Williamsa4456852007-07-09 11:56:43 -0700179static void return_io(struct bio *return_bi)
180{
181 struct bio *bi = return_bi;
182 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700183
184 return_bi = bi->bi_next;
185 bi->bi_next = NULL;
186 bi->bi_size = 0;
NeilBrowna9add5d2012-10-31 11:59:09 +1100187 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
188 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +1000189 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700190 bi = return_bi;
191 }
192}
193
NeilBrownd1688a62011-10-11 16:49:52 +1100194static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Dan Williams600aa102008-06-28 08:32:05 +1000196static int stripe_operations_active(struct stripe_head *sh)
197{
198 return sh->check_state || sh->reconstruct_state ||
199 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
200 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
201}
202
Shaohua Li4eb788d2012-07-19 16:01:31 +1000203static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000205 BUG_ON(!list_empty(&sh->lru));
206 BUG_ON(atomic_read(&conf->active_stripes)==0);
207 if (test_bit(STRIPE_HANDLE, &sh->state)) {
208 if (test_bit(STRIPE_DELAYED, &sh->state) &&
209 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
210 list_add_tail(&sh->lru, &conf->delayed_list);
211 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
212 sh->bm_seq - conf->seq_write > 0)
213 list_add_tail(&sh->lru, &conf->bitmap_list);
214 else {
215 clear_bit(STRIPE_DELAYED, &sh->state);
216 clear_bit(STRIPE_BIT_DELAY, &sh->state);
217 list_add_tail(&sh->lru, &conf->handle_list);
218 }
219 md_wakeup_thread(conf->mddev->thread);
220 } else {
221 BUG_ON(stripe_operations_active(sh));
222 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
223 if (atomic_dec_return(&conf->preread_active_stripes)
224 < IO_THRESHOLD)
225 md_wakeup_thread(conf->mddev->thread);
226 atomic_dec(&conf->active_stripes);
227 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
228 list_add_tail(&sh->lru, &conf->inactive_list);
229 wake_up(&conf->wait_for_stripe);
230 if (conf->retry_read_aligned)
231 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 }
234}
NeilBrownd0dabf72009-03-31 14:39:38 +1100235
Shaohua Li4eb788d2012-07-19 16:01:31 +1000236static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
237{
238 if (atomic_dec_and_test(&sh->count))
239 do_release_stripe(conf, sh);
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static void release_stripe(struct stripe_head *sh)
243{
NeilBrownd1688a62011-10-11 16:49:52 +1100244 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700246
Shaohua Li4eb788d2012-07-19 16:01:31 +1000247 local_irq_save(flags);
248 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
249 do_release_stripe(conf, sh);
250 spin_unlock(&conf->device_lock);
251 }
252 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
NeilBrownfccddba2006-01-06 00:20:33 -0800255static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Dan Williams45b42332007-07-09 11:56:43 -0700257 pr_debug("remove_hash(), stripe %llu\n",
258 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
NeilBrownfccddba2006-01-06 00:20:33 -0800260 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
NeilBrownd1688a62011-10-11 16:49:52 +1100263static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
NeilBrownfccddba2006-01-06 00:20:33 -0800265 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Dan Williams45b42332007-07-09 11:56:43 -0700267 pr_debug("insert_hash(), stripe %llu\n",
268 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
NeilBrownfccddba2006-01-06 00:20:33 -0800270 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273
274/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100275static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct stripe_head *sh = NULL;
278 struct list_head *first;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (list_empty(&conf->inactive_list))
281 goto out;
282 first = conf->inactive_list.next;
283 sh = list_entry(first, struct stripe_head, lru);
284 list_del_init(first);
285 remove_hash(sh);
286 atomic_inc(&conf->active_stripes);
287out:
288 return sh;
289}
290
NeilBrowne4e11e32010-06-16 16:45:16 +1000291static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct page *p;
294 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000295 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
NeilBrowne4e11e32010-06-16 16:45:16 +1000297 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 p = sh->dev[i].page;
299 if (!p)
300 continue;
301 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800302 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304}
305
NeilBrowne4e11e32010-06-16 16:45:16 +1000306static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000309 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
NeilBrowne4e11e32010-06-16 16:45:16 +1000311 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct page *page;
313
314 if (!(page = alloc_page(GFP_KERNEL))) {
315 return 1;
316 }
317 sh->dev[i].page = page;
318 }
319 return 0;
320}
321
NeilBrown784052e2009-03-31 15:19:07 +1100322static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100323static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100324 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
NeilBrownb5663ba2009-03-31 14:39:38 +1100326static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
NeilBrownd1688a62011-10-11 16:49:52 +1100328 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800329 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200331 BUG_ON(atomic_read(&sh->count) != 0);
332 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000333 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700334
Dan Williams45b42332007-07-09 11:56:43 -0700335 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 (unsigned long long)sh->sector);
337
338 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700339
NeilBrown86b42c72009-03-31 15:19:03 +1100340 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100341 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100343 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 sh->state = 0;
345
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800346
347 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 struct r5dev *dev = &sh->dev[i];
349
Dan Williamsd84e0f12007-01-02 13:52:30 -0700350 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700352 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700354 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000356 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100359 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361 insert_hash(conf, sh);
362}
363
NeilBrownd1688a62011-10-11 16:49:52 +1100364static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100365 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800368 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Dan Williams45b42332007-07-09 11:56:43 -0700370 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800371 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100372 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700374 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return NULL;
376}
377
NeilBrown674806d2010-06-16 17:17:53 +1000378/*
379 * Need to check if array has failed when deciding whether to:
380 * - start an array
381 * - remove non-faulty devices
382 * - add a spare
383 * - allow a reshape
384 * This determination is simple when no reshape is happening.
385 * However if there is a reshape, we need to carefully check
386 * both the before and after sections.
387 * This is because some failed devices may only affect one
388 * of the two sections, and some non-in_sync devices may
389 * be insync in the section most affected by failed devices.
390 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100391static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000392{
NeilBrown908f4fb2011-12-23 10:17:50 +1100393 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000394 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000395
396 rcu_read_lock();
397 degraded = 0;
398 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100399 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000400 if (rdev && test_bit(Faulty, &rdev->flags))
401 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000402 if (!rdev || test_bit(Faulty, &rdev->flags))
403 degraded++;
404 else if (test_bit(In_sync, &rdev->flags))
405 ;
406 else
407 /* not in-sync or faulty.
408 * If the reshape increases the number of devices,
409 * this is being recovered by the reshape, so
410 * this 'previous' section is not in_sync.
411 * If the number of devices is being reduced however,
412 * the device can only be part of the array if
413 * we are reverting a reshape, so this section will
414 * be in-sync.
415 */
416 if (conf->raid_disks >= conf->previous_raid_disks)
417 degraded++;
418 }
419 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100420 if (conf->raid_disks == conf->previous_raid_disks)
421 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000422 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100423 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000424 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100425 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000426 if (rdev && test_bit(Faulty, &rdev->flags))
427 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000428 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100429 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000430 else if (test_bit(In_sync, &rdev->flags))
431 ;
432 else
433 /* not in-sync or faulty.
434 * If reshape increases the number of devices, this
435 * section has already been recovered, else it
436 * almost certainly hasn't.
437 */
438 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100439 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000440 }
441 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100442 if (degraded2 > degraded)
443 return degraded2;
444 return degraded;
445}
446
447static int has_failed(struct r5conf *conf)
448{
449 int degraded;
450
451 if (conf->mddev->reshape_position == MaxSector)
452 return conf->mddev->degraded > conf->max_degraded;
453
454 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000455 if (degraded > conf->max_degraded)
456 return 1;
457 return 0;
458}
459
NeilBrownb5663ba2009-03-31 14:39:38 +1100460static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100461get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000462 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct stripe_head *sh;
465
Dan Williams45b42332007-07-09 11:56:43 -0700466 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 spin_lock_irq(&conf->device_lock);
469
470 do {
NeilBrown72626682005-09-09 16:23:54 -0700471 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000472 conf->quiesce == 0 || noquiesce,
Lukas Czernereed8c022012-11-30 11:42:40 +0100473 conf->device_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100474 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (!sh) {
476 if (!conf->inactive_blocked)
477 sh = get_free_stripe(conf);
478 if (noblock && sh == NULL)
479 break;
480 if (!sh) {
481 conf->inactive_blocked = 1;
482 wait_event_lock_irq(conf->wait_for_stripe,
483 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800484 (atomic_read(&conf->active_stripes)
485 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 || !conf->inactive_blocked),
Lukas Czernereed8c022012-11-30 11:42:40 +0100487 conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 conf->inactive_blocked = 0;
489 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100490 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 } else {
492 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100493 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000494 && !test_bit(STRIPE_EXPANDING, &sh->state)
495 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 } else {
497 if (!test_bit(STRIPE_HANDLE, &sh->state))
498 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700499 if (list_empty(&sh->lru) &&
500 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700501 BUG();
502 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 }
505 } while (sh == NULL);
506
507 if (sh)
508 atomic_inc(&sh->count);
509
510 spin_unlock_irq(&conf->device_lock);
511 return sh;
512}
513
NeilBrown05616be2012-05-21 09:27:00 +1000514/* Determine if 'data_offset' or 'new_data_offset' should be used
515 * in this stripe_head.
516 */
517static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
518{
519 sector_t progress = conf->reshape_progress;
520 /* Need a memory barrier to make sure we see the value
521 * of conf->generation, or ->data_offset that was set before
522 * reshape_progress was updated.
523 */
524 smp_rmb();
525 if (progress == MaxSector)
526 return 0;
527 if (sh->generation == conf->generation - 1)
528 return 0;
529 /* We are in a reshape, and this is a new-generation stripe,
530 * so use new_data_offset.
531 */
532 return 1;
533}
534
NeilBrown6712ecf2007-09-27 12:47:43 +0200535static void
536raid5_end_read_request(struct bio *bi, int error);
537static void
538raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700539
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000540static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700541{
NeilBrownd1688a62011-10-11 16:49:52 +1100542 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700543 int i, disks = sh->disks;
544
545 might_sleep();
546
547 for (i = disks; i--; ) {
548 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100549 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100550 struct bio *bi, *rbi;
551 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200552 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
553 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
554 rw = WRITE_FUA;
555 else
556 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100557 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100558 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200559 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700560 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100561 else if (test_and_clear_bit(R5_WantReplace,
562 &sh->dev[i].flags)) {
563 rw = WRITE;
564 replace_only = 1;
565 } else
Dan Williams91c00922007-01-02 13:52:30 -0700566 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000567 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
568 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700569
570 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100571 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700572
573 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100574 rbi->bi_rw = rw;
575 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700576 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100577 rbi->bi_end_io = raid5_end_write_request;
578 } else
Dan Williams91c00922007-01-02 13:52:30 -0700579 bi->bi_end_io = raid5_end_read_request;
580
581 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100582 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100583 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
584 rdev = rcu_dereference(conf->disks[i].rdev);
585 if (!rdev) {
586 rdev = rrdev;
587 rrdev = NULL;
588 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100589 if (rw & WRITE) {
590 if (replace_only)
591 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100592 if (rdev == rrdev)
593 /* We raced and saw duplicates */
594 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100595 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100596 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100597 rdev = rrdev;
598 rrdev = NULL;
599 }
NeilBrown977df362011-12-23 10:17:53 +1100600
Dan Williams91c00922007-01-02 13:52:30 -0700601 if (rdev && test_bit(Faulty, &rdev->flags))
602 rdev = NULL;
603 if (rdev)
604 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100605 if (rrdev && test_bit(Faulty, &rrdev->flags))
606 rrdev = NULL;
607 if (rrdev)
608 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700609 rcu_read_unlock();
610
NeilBrown73e92e52011-07-28 11:39:22 +1000611 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100612 * need to check for writes. We never accept write errors
613 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000614 */
615 while ((rw & WRITE) && rdev &&
616 test_bit(WriteErrorSeen, &rdev->flags)) {
617 sector_t first_bad;
618 int bad_sectors;
619 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
620 &first_bad, &bad_sectors);
621 if (!bad)
622 break;
623
624 if (bad < 0) {
625 set_bit(BlockedBadBlocks, &rdev->flags);
626 if (!conf->mddev->external &&
627 conf->mddev->flags) {
628 /* It is very unlikely, but we might
629 * still need to write out the
630 * bad block log - better give it
631 * a chance*/
632 md_check_recovery(conf->mddev);
633 }
majianpeng18507532012-07-03 12:11:54 +1000634 /*
635 * Because md_wait_for_blocked_rdev
636 * will dec nr_pending, we must
637 * increment it first.
638 */
639 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000640 md_wait_for_blocked_rdev(rdev, conf->mddev);
641 } else {
642 /* Acknowledged bad block - skip the write */
643 rdev_dec_pending(rdev, conf->mddev);
644 rdev = NULL;
645 }
646 }
647
Dan Williams91c00922007-01-02 13:52:30 -0700648 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100649 if (s->syncing || s->expanding || s->expanded
650 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700651 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
652
Dan Williams2b7497f2008-06-28 08:31:52 +1000653 set_bit(STRIPE_IO_STARTED, &sh->state);
654
Dan Williams91c00922007-01-02 13:52:30 -0700655 bi->bi_bdev = rdev->bdev;
656 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700657 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700658 bi->bi_rw, i);
659 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000660 if (use_new_offset(conf, sh))
661 bi->bi_sector = (sh->sector
662 + rdev->new_data_offset);
663 else
664 bi->bi_sector = (sh->sector
665 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000666 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
667 bi->bi_rw |= REQ_FLUSH;
668
Dan Williams91c00922007-01-02 13:52:30 -0700669 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700670 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700671 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
672 bi->bi_io_vec[0].bv_offset = 0;
673 bi->bi_size = STRIPE_SIZE;
674 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100675 if (rrdev)
676 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600677
678 if (conf->mddev->gendisk)
679 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
680 bi, disk_devt(conf->mddev->gendisk),
681 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700682 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100683 }
684 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100685 if (s->syncing || s->expanding || s->expanded
686 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100687 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
688
689 set_bit(STRIPE_IO_STARTED, &sh->state);
690
691 rbi->bi_bdev = rrdev->bdev;
692 pr_debug("%s: for %llu schedule op %ld on "
693 "replacement disc %d\n",
694 __func__, (unsigned long long)sh->sector,
695 rbi->bi_rw, i);
696 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000697 if (use_new_offset(conf, sh))
698 rbi->bi_sector = (sh->sector
699 + rrdev->new_data_offset);
700 else
701 rbi->bi_sector = (sh->sector
702 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100703 rbi->bi_flags = 1 << BIO_UPTODATE;
704 rbi->bi_idx = 0;
705 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
706 rbi->bi_io_vec[0].bv_offset = 0;
707 rbi->bi_size = STRIPE_SIZE;
708 rbi->bi_next = NULL;
Jonathan Brassowe3620a32013-03-07 16:22:01 -0600709 if (conf->mddev->gendisk)
710 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
711 rbi, disk_devt(conf->mddev->gendisk),
712 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100713 generic_make_request(rbi);
714 }
715 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000716 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700717 set_bit(STRIPE_DEGRADED, &sh->state);
718 pr_debug("skip op %ld on disc %d for sector %llu\n",
719 bi->bi_rw, i, (unsigned long long)sh->sector);
720 clear_bit(R5_LOCKED, &sh->dev[i].flags);
721 set_bit(STRIPE_HANDLE, &sh->state);
722 }
723 }
724}
725
726static struct dma_async_tx_descriptor *
727async_copy_data(int frombio, struct bio *bio, struct page *page,
728 sector_t sector, struct dma_async_tx_descriptor *tx)
729{
730 struct bio_vec *bvl;
731 struct page *bio_page;
732 int i;
733 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700734 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700735 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700736
737 if (bio->bi_sector >= sector)
738 page_offset = (signed)(bio->bi_sector - sector) * 512;
739 else
740 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700741
Dan Williams0403e382009-09-08 17:42:50 -0700742 if (frombio)
743 flags |= ASYNC_TX_FENCE;
744 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
745
Dan Williams91c00922007-01-02 13:52:30 -0700746 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000747 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700748 int clen;
749 int b_offset = 0;
750
751 if (page_offset < 0) {
752 b_offset = -page_offset;
753 page_offset += b_offset;
754 len -= b_offset;
755 }
756
757 if (len > 0 && page_offset + len > STRIPE_SIZE)
758 clen = STRIPE_SIZE - page_offset;
759 else
760 clen = len;
761
762 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000763 b_offset += bvl->bv_offset;
764 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700765 if (frombio)
766 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700767 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700768 else
769 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700770 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700771 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700772 /* chain the operations */
773 submit.depend_tx = tx;
774
Dan Williams91c00922007-01-02 13:52:30 -0700775 if (clen < len) /* hit end of page */
776 break;
777 page_offset += len;
778 }
779
780 return tx;
781}
782
783static void ops_complete_biofill(void *stripe_head_ref)
784{
785 struct stripe_head *sh = stripe_head_ref;
786 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700787 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700788
Harvey Harrisone46b2722008-04-28 02:15:50 -0700789 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700790 (unsigned long long)sh->sector);
791
792 /* clear completed biofills */
793 for (i = sh->disks; i--; ) {
794 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700795
796 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700797 /* and check if we need to reply to a read request,
798 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000799 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700800 */
801 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700802 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700803
Dan Williams91c00922007-01-02 13:52:30 -0700804 BUG_ON(!dev->read);
805 rbi = dev->read;
806 dev->read = NULL;
807 while (rbi && rbi->bi_sector <
808 dev->sector + STRIPE_SECTORS) {
809 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000810 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700811 rbi->bi_next = return_bi;
812 return_bi = rbi;
813 }
Dan Williams91c00922007-01-02 13:52:30 -0700814 rbi = rbi2;
815 }
816 }
817 }
Dan Williams83de75c2008-06-28 08:31:58 +1000818 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700819
820 return_io(return_bi);
821
Dan Williamse4d84902007-09-24 10:06:13 -0700822 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700823 release_stripe(sh);
824}
825
826static void ops_run_biofill(struct stripe_head *sh)
827{
828 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700829 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700830 int i;
831
Harvey Harrisone46b2722008-04-28 02:15:50 -0700832 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700833 (unsigned long long)sh->sector);
834
835 for (i = sh->disks; i--; ) {
836 struct r5dev *dev = &sh->dev[i];
837 if (test_bit(R5_Wantfill, &dev->flags)) {
838 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000839 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700840 dev->read = rbi = dev->toread;
841 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000842 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700843 while (rbi && rbi->bi_sector <
844 dev->sector + STRIPE_SECTORS) {
845 tx = async_copy_data(0, rbi, dev->page,
846 dev->sector, tx);
847 rbi = r5_next_bio(rbi, dev->sector);
848 }
849 }
850 }
851
852 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700853 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
854 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700855}
856
Dan Williams4e7d2c02009-08-29 19:13:11 -0700857static void mark_target_uptodate(struct stripe_head *sh, int target)
858{
859 struct r5dev *tgt;
860
861 if (target < 0)
862 return;
863
864 tgt = &sh->dev[target];
865 set_bit(R5_UPTODATE, &tgt->flags);
866 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
867 clear_bit(R5_Wantcompute, &tgt->flags);
868}
869
Dan Williamsac6b53b2009-07-14 13:40:19 -0700870static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700871{
872 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700873
Harvey Harrisone46b2722008-04-28 02:15:50 -0700874 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700875 (unsigned long long)sh->sector);
876
Dan Williamsac6b53b2009-07-14 13:40:19 -0700877 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700878 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700879 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700880
Dan Williamsecc65c92008-06-28 08:31:57 +1000881 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
882 if (sh->check_state == check_state_compute_run)
883 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700884 set_bit(STRIPE_HANDLE, &sh->state);
885 release_stripe(sh);
886}
887
Dan Williamsd6f38f32009-07-14 11:50:52 -0700888/* return a pointer to the address conversion region of the scribble buffer */
889static addr_conv_t *to_addr_conv(struct stripe_head *sh,
890 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700891{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700892 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
893}
894
895static struct dma_async_tx_descriptor *
896ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
897{
Dan Williams91c00922007-01-02 13:52:30 -0700898 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700899 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700900 int target = sh->ops.target;
901 struct r5dev *tgt = &sh->dev[target];
902 struct page *xor_dest = tgt->page;
903 int count = 0;
904 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700905 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700906 int i;
907
908 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700909 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700910 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
911
912 for (i = disks; i--; )
913 if (i != target)
914 xor_srcs[count++] = sh->dev[i].page;
915
916 atomic_inc(&sh->count);
917
Dan Williams0403e382009-09-08 17:42:50 -0700918 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700919 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700920 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700921 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700922 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700923 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700924
Dan Williams91c00922007-01-02 13:52:30 -0700925 return tx;
926}
927
Dan Williamsac6b53b2009-07-14 13:40:19 -0700928/* set_syndrome_sources - populate source buffers for gen_syndrome
929 * @srcs - (struct page *) array of size sh->disks
930 * @sh - stripe_head to parse
931 *
932 * Populates srcs in proper layout order for the stripe and returns the
933 * 'count' of sources to be used in a call to async_gen_syndrome. The P
934 * destination buffer is recorded in srcs[count] and the Q destination
935 * is recorded in srcs[count+1]].
936 */
937static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
938{
939 int disks = sh->disks;
940 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
941 int d0_idx = raid6_d0(sh);
942 int count;
943 int i;
944
945 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100946 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700947
948 count = 0;
949 i = d0_idx;
950 do {
951 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
952
953 srcs[slot] = sh->dev[i].page;
954 i = raid6_next_disk(i, disks);
955 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700956
NeilBrowne4424fe2009-10-16 16:27:34 +1100957 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700958}
959
960static struct dma_async_tx_descriptor *
961ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
962{
963 int disks = sh->disks;
964 struct page **blocks = percpu->scribble;
965 int target;
966 int qd_idx = sh->qd_idx;
967 struct dma_async_tx_descriptor *tx;
968 struct async_submit_ctl submit;
969 struct r5dev *tgt;
970 struct page *dest;
971 int i;
972 int count;
973
974 if (sh->ops.target < 0)
975 target = sh->ops.target2;
976 else if (sh->ops.target2 < 0)
977 target = sh->ops.target;
978 else
979 /* we should only have one valid target */
980 BUG();
981 BUG_ON(target < 0);
982 pr_debug("%s: stripe %llu block: %d\n",
983 __func__, (unsigned long long)sh->sector, target);
984
985 tgt = &sh->dev[target];
986 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
987 dest = tgt->page;
988
989 atomic_inc(&sh->count);
990
991 if (target == qd_idx) {
992 count = set_syndrome_sources(blocks, sh);
993 blocks[count] = NULL; /* regenerating p is not necessary */
994 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700995 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
996 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700997 to_addr_conv(sh, percpu));
998 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
999 } else {
1000 /* Compute any data- or p-drive using XOR */
1001 count = 0;
1002 for (i = disks; i-- ; ) {
1003 if (i == target || i == qd_idx)
1004 continue;
1005 blocks[count++] = sh->dev[i].page;
1006 }
1007
Dan Williams0403e382009-09-08 17:42:50 -07001008 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1009 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001010 to_addr_conv(sh, percpu));
1011 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1012 }
1013
1014 return tx;
1015}
1016
1017static struct dma_async_tx_descriptor *
1018ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1019{
1020 int i, count, disks = sh->disks;
1021 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1022 int d0_idx = raid6_d0(sh);
1023 int faila = -1, failb = -1;
1024 int target = sh->ops.target;
1025 int target2 = sh->ops.target2;
1026 struct r5dev *tgt = &sh->dev[target];
1027 struct r5dev *tgt2 = &sh->dev[target2];
1028 struct dma_async_tx_descriptor *tx;
1029 struct page **blocks = percpu->scribble;
1030 struct async_submit_ctl submit;
1031
1032 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1033 __func__, (unsigned long long)sh->sector, target, target2);
1034 BUG_ON(target < 0 || target2 < 0);
1035 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1036 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1037
Dan Williams6c910a72009-09-16 12:24:54 -07001038 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001039 * slot number conversion for 'faila' and 'failb'
1040 */
1041 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001042 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001043 count = 0;
1044 i = d0_idx;
1045 do {
1046 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1047
1048 blocks[slot] = sh->dev[i].page;
1049
1050 if (i == target)
1051 faila = slot;
1052 if (i == target2)
1053 failb = slot;
1054 i = raid6_next_disk(i, disks);
1055 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001056
1057 BUG_ON(faila == failb);
1058 if (failb < faila)
1059 swap(faila, failb);
1060 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1061 __func__, (unsigned long long)sh->sector, faila, failb);
1062
1063 atomic_inc(&sh->count);
1064
1065 if (failb == syndrome_disks+1) {
1066 /* Q disk is one of the missing disks */
1067 if (faila == syndrome_disks) {
1068 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001069 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1070 ops_complete_compute, sh,
1071 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001072 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001073 STRIPE_SIZE, &submit);
1074 } else {
1075 struct page *dest;
1076 int data_target;
1077 int qd_idx = sh->qd_idx;
1078
1079 /* Missing D+Q: recompute D from P, then recompute Q */
1080 if (target == qd_idx)
1081 data_target = target2;
1082 else
1083 data_target = target;
1084
1085 count = 0;
1086 for (i = disks; i-- ; ) {
1087 if (i == data_target || i == qd_idx)
1088 continue;
1089 blocks[count++] = sh->dev[i].page;
1090 }
1091 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001092 init_async_submit(&submit,
1093 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1094 NULL, NULL, NULL,
1095 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001096 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1097 &submit);
1098
1099 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001100 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1101 ops_complete_compute, sh,
1102 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001103 return async_gen_syndrome(blocks, 0, count+2,
1104 STRIPE_SIZE, &submit);
1105 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001106 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001107 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1108 ops_complete_compute, sh,
1109 to_addr_conv(sh, percpu));
1110 if (failb == syndrome_disks) {
1111 /* We're missing D+P. */
1112 return async_raid6_datap_recov(syndrome_disks+2,
1113 STRIPE_SIZE, faila,
1114 blocks, &submit);
1115 } else {
1116 /* We're missing D+D. */
1117 return async_raid6_2data_recov(syndrome_disks+2,
1118 STRIPE_SIZE, faila, failb,
1119 blocks, &submit);
1120 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001121 }
1122}
1123
1124
Dan Williams91c00922007-01-02 13:52:30 -07001125static void ops_complete_prexor(void *stripe_head_ref)
1126{
1127 struct stripe_head *sh = stripe_head_ref;
1128
Harvey Harrisone46b2722008-04-28 02:15:50 -07001129 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001130 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001131}
1132
1133static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001134ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1135 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001136{
Dan Williams91c00922007-01-02 13:52:30 -07001137 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001138 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001139 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001140 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001141
1142 /* existing parity data subtracted */
1143 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1144
Harvey Harrisone46b2722008-04-28 02:15:50 -07001145 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001146 (unsigned long long)sh->sector);
1147
1148 for (i = disks; i--; ) {
1149 struct r5dev *dev = &sh->dev[i];
1150 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001151 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001152 xor_srcs[count++] = dev->page;
1153 }
1154
Dan Williams0403e382009-09-08 17:42:50 -07001155 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001156 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001157 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001158
1159 return tx;
1160}
1161
1162static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001163ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001164{
1165 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001166 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001167
Harvey Harrisone46b2722008-04-28 02:15:50 -07001168 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001169 (unsigned long long)sh->sector);
1170
1171 for (i = disks; i--; ) {
1172 struct r5dev *dev = &sh->dev[i];
1173 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001174
Dan Williamsd8ee0722008-06-28 08:32:06 +10001175 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001176 struct bio *wbi;
1177
Shaohua Lib17459c2012-07-19 16:01:31 +10001178 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001179 chosen = dev->towrite;
1180 dev->towrite = NULL;
1181 BUG_ON(dev->written);
1182 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001183 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001184
1185 while (wbi && wbi->bi_sector <
1186 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001187 if (wbi->bi_rw & REQ_FUA)
1188 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001189 if (wbi->bi_rw & REQ_SYNC)
1190 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001191 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001192 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001193 else
Shaohua Li620125f2012-10-11 13:49:05 +11001194 tx = async_copy_data(1, wbi, dev->page,
1195 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001196 wbi = r5_next_bio(wbi, dev->sector);
1197 }
1198 }
1199 }
1200
1201 return tx;
1202}
1203
Dan Williamsac6b53b2009-07-14 13:40:19 -07001204static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001205{
1206 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001207 int disks = sh->disks;
1208 int pd_idx = sh->pd_idx;
1209 int qd_idx = sh->qd_idx;
1210 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001211 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001212
Harvey Harrisone46b2722008-04-28 02:15:50 -07001213 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001214 (unsigned long long)sh->sector);
1215
Shaohua Libc0934f2012-05-22 13:55:05 +10001216 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001217 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001218 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001219 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001220 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001221
Dan Williams91c00922007-01-02 13:52:30 -07001222 for (i = disks; i--; ) {
1223 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001224
Tejun Heoe9c74692010-09-03 11:56:18 +02001225 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001226 if (!discard)
1227 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001228 if (fua)
1229 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001230 if (sync)
1231 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001232 }
Dan Williams91c00922007-01-02 13:52:30 -07001233 }
1234
Dan Williamsd8ee0722008-06-28 08:32:06 +10001235 if (sh->reconstruct_state == reconstruct_state_drain_run)
1236 sh->reconstruct_state = reconstruct_state_drain_result;
1237 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1238 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1239 else {
1240 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1241 sh->reconstruct_state = reconstruct_state_result;
1242 }
Dan Williams91c00922007-01-02 13:52:30 -07001243
1244 set_bit(STRIPE_HANDLE, &sh->state);
1245 release_stripe(sh);
1246}
1247
1248static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001249ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1250 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001251{
Dan Williams91c00922007-01-02 13:52:30 -07001252 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001253 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001254 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001255 int count = 0, pd_idx = sh->pd_idx, i;
1256 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001257 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001258 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001259
Harvey Harrisone46b2722008-04-28 02:15:50 -07001260 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001261 (unsigned long long)sh->sector);
1262
Shaohua Li620125f2012-10-11 13:49:05 +11001263 for (i = 0; i < sh->disks; i++) {
1264 if (pd_idx == i)
1265 continue;
1266 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1267 break;
1268 }
1269 if (i >= sh->disks) {
1270 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001271 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1272 ops_complete_reconstruct(sh);
1273 return;
1274 }
Dan Williams91c00922007-01-02 13:52:30 -07001275 /* check if prexor is active which means only process blocks
1276 * that are part of a read-modify-write (written)
1277 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001278 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1279 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001280 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1281 for (i = disks; i--; ) {
1282 struct r5dev *dev = &sh->dev[i];
1283 if (dev->written)
1284 xor_srcs[count++] = dev->page;
1285 }
1286 } else {
1287 xor_dest = sh->dev[pd_idx].page;
1288 for (i = disks; i--; ) {
1289 struct r5dev *dev = &sh->dev[i];
1290 if (i != pd_idx)
1291 xor_srcs[count++] = dev->page;
1292 }
1293 }
1294
Dan Williams91c00922007-01-02 13:52:30 -07001295 /* 1/ if we prexor'd then the dest is reused as a source
1296 * 2/ if we did not prexor then we are redoing the parity
1297 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1298 * for the synchronous xor case
1299 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001300 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001301 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1302
1303 atomic_inc(&sh->count);
1304
Dan Williamsac6b53b2009-07-14 13:40:19 -07001305 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001306 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001307 if (unlikely(count == 1))
1308 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1309 else
1310 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001311}
1312
Dan Williamsac6b53b2009-07-14 13:40:19 -07001313static void
1314ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1315 struct dma_async_tx_descriptor *tx)
1316{
1317 struct async_submit_ctl submit;
1318 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001319 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001320
1321 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1322
Shaohua Li620125f2012-10-11 13:49:05 +11001323 for (i = 0; i < sh->disks; i++) {
1324 if (sh->pd_idx == i || sh->qd_idx == i)
1325 continue;
1326 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1327 break;
1328 }
1329 if (i >= sh->disks) {
1330 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001331 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1332 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1333 ops_complete_reconstruct(sh);
1334 return;
1335 }
1336
Dan Williamsac6b53b2009-07-14 13:40:19 -07001337 count = set_syndrome_sources(blocks, sh);
1338
1339 atomic_inc(&sh->count);
1340
1341 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1342 sh, to_addr_conv(sh, percpu));
1343 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001344}
1345
1346static void ops_complete_check(void *stripe_head_ref)
1347{
1348 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001349
Harvey Harrisone46b2722008-04-28 02:15:50 -07001350 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001351 (unsigned long long)sh->sector);
1352
Dan Williamsecc65c92008-06-28 08:31:57 +10001353 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001354 set_bit(STRIPE_HANDLE, &sh->state);
1355 release_stripe(sh);
1356}
1357
Dan Williamsac6b53b2009-07-14 13:40:19 -07001358static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001359{
Dan Williams91c00922007-01-02 13:52:30 -07001360 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001361 int pd_idx = sh->pd_idx;
1362 int qd_idx = sh->qd_idx;
1363 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001364 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001365 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001366 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001367 int count;
1368 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001369
Harvey Harrisone46b2722008-04-28 02:15:50 -07001370 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001371 (unsigned long long)sh->sector);
1372
Dan Williamsac6b53b2009-07-14 13:40:19 -07001373 count = 0;
1374 xor_dest = sh->dev[pd_idx].page;
1375 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001376 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001377 if (i == pd_idx || i == qd_idx)
1378 continue;
1379 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001380 }
1381
Dan Williamsd6f38f32009-07-14 11:50:52 -07001382 init_async_submit(&submit, 0, NULL, NULL, NULL,
1383 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001384 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001385 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001386
Dan Williams91c00922007-01-02 13:52:30 -07001387 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001388 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1389 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001390}
1391
Dan Williamsac6b53b2009-07-14 13:40:19 -07001392static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1393{
1394 struct page **srcs = percpu->scribble;
1395 struct async_submit_ctl submit;
1396 int count;
1397
1398 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1399 (unsigned long long)sh->sector, checkp);
1400
1401 count = set_syndrome_sources(srcs, sh);
1402 if (!checkp)
1403 srcs[count] = NULL;
1404
1405 atomic_inc(&sh->count);
1406 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1407 sh, to_addr_conv(sh, percpu));
1408 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1409 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1410}
1411
NeilBrown51acbce2013-02-28 09:08:34 +11001412static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001413{
1414 int overlap_clear = 0, i, disks = sh->disks;
1415 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001416 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001417 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001418 struct raid5_percpu *percpu;
1419 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001420
Dan Williamsd6f38f32009-07-14 11:50:52 -07001421 cpu = get_cpu();
1422 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001423 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001424 ops_run_biofill(sh);
1425 overlap_clear++;
1426 }
1427
Dan Williams7b3a8712008-06-28 08:32:09 +10001428 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001429 if (level < 6)
1430 tx = ops_run_compute5(sh, percpu);
1431 else {
1432 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1433 tx = ops_run_compute6_1(sh, percpu);
1434 else
1435 tx = ops_run_compute6_2(sh, percpu);
1436 }
1437 /* terminate the chain if reconstruct is not set to be run */
1438 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001439 async_tx_ack(tx);
1440 }
Dan Williams91c00922007-01-02 13:52:30 -07001441
Dan Williams600aa102008-06-28 08:32:05 +10001442 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001443 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001444
Dan Williams600aa102008-06-28 08:32:05 +10001445 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001446 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001447 overlap_clear++;
1448 }
1449
Dan Williamsac6b53b2009-07-14 13:40:19 -07001450 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1451 if (level < 6)
1452 ops_run_reconstruct5(sh, percpu, tx);
1453 else
1454 ops_run_reconstruct6(sh, percpu, tx);
1455 }
Dan Williams91c00922007-01-02 13:52:30 -07001456
Dan Williamsac6b53b2009-07-14 13:40:19 -07001457 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1458 if (sh->check_state == check_state_run)
1459 ops_run_check_p(sh, percpu);
1460 else if (sh->check_state == check_state_run_q)
1461 ops_run_check_pq(sh, percpu, 0);
1462 else if (sh->check_state == check_state_run_pq)
1463 ops_run_check_pq(sh, percpu, 1);
1464 else
1465 BUG();
1466 }
Dan Williams91c00922007-01-02 13:52:30 -07001467
Dan Williams91c00922007-01-02 13:52:30 -07001468 if (overlap_clear)
1469 for (i = disks; i--; ) {
1470 struct r5dev *dev = &sh->dev[i];
1471 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1472 wake_up(&sh->raid_conf->wait_for_overlap);
1473 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001474 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001475}
1476
NeilBrownd1688a62011-10-11 16:49:52 +11001477static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001480 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001481 if (!sh)
1482 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001483
NeilBrown3f294f42005-11-08 21:39:25 -08001484 sh->raid_conf = conf;
NeilBrown3f294f42005-11-08 21:39:25 -08001485
Shaohua Lib17459c2012-07-19 16:01:31 +10001486 spin_lock_init(&sh->stripe_lock);
1487
NeilBrowne4e11e32010-06-16 16:45:16 +10001488 if (grow_buffers(sh)) {
1489 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001490 kmem_cache_free(conf->slab_cache, sh);
1491 return 0;
1492 }
1493 /* we just created an active stripe so... */
1494 atomic_set(&sh->count, 1);
1495 atomic_inc(&conf->active_stripes);
1496 INIT_LIST_HEAD(&sh->lru);
1497 release_stripe(sh);
1498 return 1;
1499}
1500
NeilBrownd1688a62011-10-11 16:49:52 +11001501static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001502{
Christoph Lametere18b8902006-12-06 20:33:20 -08001503 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001504 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
NeilBrownf4be6b42010-06-01 19:37:25 +10001506 if (conf->mddev->gendisk)
1507 sprintf(conf->cache_name[0],
1508 "raid%d-%s", conf->level, mdname(conf->mddev));
1509 else
1510 sprintf(conf->cache_name[0],
1511 "raid%d-%p", conf->level, conf->mddev);
1512 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1513
NeilBrownad01c9e2006-03-27 01:18:07 -08001514 conf->active_name = 0;
1515 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001517 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (!sc)
1519 return 1;
1520 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001521 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001522 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001523 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return 0;
1526}
NeilBrown29269552006-03-27 01:18:10 -08001527
Dan Williamsd6f38f32009-07-14 11:50:52 -07001528/**
1529 * scribble_len - return the required size of the scribble region
1530 * @num - total number of disks in the array
1531 *
1532 * The size must be enough to contain:
1533 * 1/ a struct page pointer for each device in the array +2
1534 * 2/ room to convert each entry in (1) to its corresponding dma
1535 * (dma_map_page()) or page (page_address()) address.
1536 *
1537 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1538 * calculate over all devices (not just the data blocks), using zeros in place
1539 * of the P and Q blocks.
1540 */
1541static size_t scribble_len(int num)
1542{
1543 size_t len;
1544
1545 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1546
1547 return len;
1548}
1549
NeilBrownd1688a62011-10-11 16:49:52 +11001550static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001551{
1552 /* Make all the stripes able to hold 'newsize' devices.
1553 * New slots in each stripe get 'page' set to a new page.
1554 *
1555 * This happens in stages:
1556 * 1/ create a new kmem_cache and allocate the required number of
1557 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09001558 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08001559 * to the new stripe_heads. This will have the side effect of
1560 * freezing the array as once all stripe_heads have been collected,
1561 * no IO will be possible. Old stripe heads are freed once their
1562 * pages have been transferred over, and the old kmem_cache is
1563 * freed when all stripes are done.
1564 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1565 * we simple return a failre status - no need to clean anything up.
1566 * 4/ allocate new pages for the new slots in the new stripe_heads.
1567 * If this fails, we don't bother trying the shrink the
1568 * stripe_heads down again, we just leave them as they are.
1569 * As each stripe_head is processed the new one is released into
1570 * active service.
1571 *
1572 * Once step2 is started, we cannot afford to wait for a write,
1573 * so we use GFP_NOIO allocations.
1574 */
1575 struct stripe_head *osh, *nsh;
1576 LIST_HEAD(newstripes);
1577 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001578 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001579 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001580 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001581 int i;
1582
1583 if (newsize <= conf->pool_size)
1584 return 0; /* never bother to shrink */
1585
Dan Williamsb5470dc2008-06-27 21:44:04 -07001586 err = md_allow_write(conf->mddev);
1587 if (err)
1588 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001589
NeilBrownad01c9e2006-03-27 01:18:07 -08001590 /* Step 1 */
1591 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1592 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001593 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001594 if (!sc)
1595 return -ENOMEM;
1596
1597 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001598 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001599 if (!nsh)
1600 break;
1601
NeilBrownad01c9e2006-03-27 01:18:07 -08001602 nsh->raid_conf = conf;
NeilBrowncb13ff62012-09-24 16:27:20 +10001603 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001604
1605 list_add(&nsh->lru, &newstripes);
1606 }
1607 if (i) {
1608 /* didn't get enough, give up */
1609 while (!list_empty(&newstripes)) {
1610 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1611 list_del(&nsh->lru);
1612 kmem_cache_free(sc, nsh);
1613 }
1614 kmem_cache_destroy(sc);
1615 return -ENOMEM;
1616 }
1617 /* Step 2 - Must use GFP_NOIO now.
1618 * OK, we have enough stripes, start collecting inactive
1619 * stripes and copying them over
1620 */
1621 list_for_each_entry(nsh, &newstripes, lru) {
1622 spin_lock_irq(&conf->device_lock);
1623 wait_event_lock_irq(conf->wait_for_stripe,
1624 !list_empty(&conf->inactive_list),
Lukas Czernereed8c022012-11-30 11:42:40 +01001625 conf->device_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001626 osh = get_free_stripe(conf);
1627 spin_unlock_irq(&conf->device_lock);
1628 atomic_set(&nsh->count, 1);
1629 for(i=0; i<conf->pool_size; i++)
1630 nsh->dev[i].page = osh->dev[i].page;
1631 for( ; i<newsize; i++)
1632 nsh->dev[i].page = NULL;
1633 kmem_cache_free(conf->slab_cache, osh);
1634 }
1635 kmem_cache_destroy(conf->slab_cache);
1636
1637 /* Step 3.
1638 * At this point, we are holding all the stripes so the array
1639 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001640 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001641 */
1642 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1643 if (ndisks) {
1644 for (i=0; i<conf->raid_disks; i++)
1645 ndisks[i] = conf->disks[i];
1646 kfree(conf->disks);
1647 conf->disks = ndisks;
1648 } else
1649 err = -ENOMEM;
1650
Dan Williamsd6f38f32009-07-14 11:50:52 -07001651 get_online_cpus();
1652 conf->scribble_len = scribble_len(newsize);
1653 for_each_present_cpu(cpu) {
1654 struct raid5_percpu *percpu;
1655 void *scribble;
1656
1657 percpu = per_cpu_ptr(conf->percpu, cpu);
1658 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1659
1660 if (scribble) {
1661 kfree(percpu->scribble);
1662 percpu->scribble = scribble;
1663 } else {
1664 err = -ENOMEM;
1665 break;
1666 }
1667 }
1668 put_online_cpus();
1669
NeilBrownad01c9e2006-03-27 01:18:07 -08001670 /* Step 4, return new stripes to service */
1671 while(!list_empty(&newstripes)) {
1672 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1673 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001674
NeilBrownad01c9e2006-03-27 01:18:07 -08001675 for (i=conf->raid_disks; i < newsize; i++)
1676 if (nsh->dev[i].page == NULL) {
1677 struct page *p = alloc_page(GFP_NOIO);
1678 nsh->dev[i].page = p;
1679 if (!p)
1680 err = -ENOMEM;
1681 }
1682 release_stripe(nsh);
1683 }
1684 /* critical section pass, GFP_NOIO no longer needed */
1685
1686 conf->slab_cache = sc;
1687 conf->active_name = 1-conf->active_name;
1688 conf->pool_size = newsize;
1689 return err;
1690}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
NeilBrownd1688a62011-10-11 16:49:52 +11001692static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
1694 struct stripe_head *sh;
1695
NeilBrown3f294f42005-11-08 21:39:25 -08001696 spin_lock_irq(&conf->device_lock);
1697 sh = get_free_stripe(conf);
1698 spin_unlock_irq(&conf->device_lock);
1699 if (!sh)
1700 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001701 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001702 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001703 kmem_cache_free(conf->slab_cache, sh);
1704 atomic_dec(&conf->active_stripes);
1705 return 1;
1706}
1707
NeilBrownd1688a62011-10-11 16:49:52 +11001708static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001709{
1710 while (drop_one_stripe(conf))
1711 ;
1712
NeilBrown29fc7e32006-02-03 03:03:41 -08001713 if (conf->slab_cache)
1714 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 conf->slab_cache = NULL;
1716}
1717
NeilBrown6712ecf2007-09-27 12:47:43 +02001718static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719{
NeilBrown99c0fb52009-03-31 14:39:38 +11001720 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001721 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001722 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001724 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001725 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001726 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 for (i=0 ; i<disks; i++)
1729 if (bi == &sh->dev[i].req)
1730 break;
1731
Dan Williams45b42332007-07-09 11:56:43 -07001732 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1733 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 uptodate);
1735 if (i == disks) {
1736 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001737 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
NeilBrown14a75d32011-12-23 10:17:52 +11001739 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001740 /* If replacement finished while this request was outstanding,
1741 * 'replacement' might be NULL already.
1742 * In that case it moved down to 'rdev'.
1743 * rdev is not removed until all requests are finished.
1744 */
NeilBrown14a75d32011-12-23 10:17:52 +11001745 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001746 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001747 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
NeilBrown05616be2012-05-21 09:27:00 +10001749 if (use_new_offset(conf, sh))
1750 s = sh->sector + rdev->new_data_offset;
1751 else
1752 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001755 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001756 /* Note that this cannot happen on a
1757 * replacement device. We just fail those on
1758 * any error
1759 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001760 printk_ratelimited(
1761 KERN_INFO
1762 "md/raid:%s: read error corrected"
1763 " (%lu sectors at %llu on %s)\n",
1764 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001765 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001766 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001767 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001768 clear_bit(R5_ReadError, &sh->dev[i].flags);
1769 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001770 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1771 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1772
NeilBrown14a75d32011-12-23 10:17:52 +11001773 if (atomic_read(&rdev->read_errors))
1774 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001776 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001777 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001778 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001781 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001782 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1783 printk_ratelimited(
1784 KERN_WARNING
1785 "md/raid:%s: read error on replacement device "
1786 "(sector %llu on %s).\n",
1787 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001788 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001789 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001790 else if (conf->mddev->degraded >= conf->max_degraded) {
1791 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001792 printk_ratelimited(
1793 KERN_WARNING
1794 "md/raid:%s: read error not correctable "
1795 "(sector %llu on %s).\n",
1796 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001797 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001798 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001799 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001800 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001801 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001802 printk_ratelimited(
1803 KERN_WARNING
1804 "md/raid:%s: read error NOT corrected!! "
1805 "(sector %llu on %s).\n",
1806 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001807 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001808 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001809 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001810 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001811 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001812 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001813 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001814 else
1815 retry = 1;
1816 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001817 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1818 set_bit(R5_ReadError, &sh->dev[i].flags);
1819 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1820 } else
1821 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001822 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001823 clear_bit(R5_ReadError, &sh->dev[i].flags);
1824 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001825 if (!(set_bad
1826 && test_bit(In_sync, &rdev->flags)
1827 && rdev_set_badblocks(
1828 rdev, sh->sector, STRIPE_SECTORS, 0)))
1829 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
NeilBrown14a75d32011-12-23 10:17:52 +11001832 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1834 set_bit(STRIPE_HANDLE, &sh->state);
1835 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
NeilBrownd710e132008-10-13 11:55:12 +11001838static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
NeilBrown99c0fb52009-03-31 14:39:38 +11001840 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001841 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001842 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001843 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001845 sector_t first_bad;
1846 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001847 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
NeilBrown977df362011-12-23 10:17:53 +11001849 for (i = 0 ; i < disks; i++) {
1850 if (bi == &sh->dev[i].req) {
1851 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 break;
NeilBrown977df362011-12-23 10:17:53 +11001853 }
1854 if (bi == &sh->dev[i].rreq) {
1855 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001856 if (rdev)
1857 replacement = 1;
1858 else
1859 /* rdev was removed and 'replacement'
1860 * replaced it. rdev is not removed
1861 * until all requests are finished.
1862 */
1863 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001864 break;
1865 }
1866 }
Dan Williams45b42332007-07-09 11:56:43 -07001867 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1869 uptodate);
1870 if (i == disks) {
1871 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001872 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
1874
NeilBrown977df362011-12-23 10:17:53 +11001875 if (replacement) {
1876 if (!uptodate)
1877 md_error(conf->mddev, rdev);
1878 else if (is_badblock(rdev, sh->sector,
1879 STRIPE_SECTORS,
1880 &first_bad, &bad_sectors))
1881 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1882 } else {
1883 if (!uptodate) {
1884 set_bit(WriteErrorSeen, &rdev->flags);
1885 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001886 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1887 set_bit(MD_RECOVERY_NEEDED,
1888 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001889 } else if (is_badblock(rdev, sh->sector,
1890 STRIPE_SECTORS,
1891 &first_bad, &bad_sectors))
1892 set_bit(R5_MadeGood, &sh->dev[i].flags);
1893 }
1894 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
NeilBrown977df362011-12-23 10:17:53 +11001896 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1897 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001899 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
1901
NeilBrown784052e2009-03-31 15:19:07 +11001902static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
NeilBrown784052e2009-03-31 15:19:07 +11001904static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
1906 struct r5dev *dev = &sh->dev[i];
1907
1908 bio_init(&dev->req);
1909 dev->req.bi_io_vec = &dev->vec;
1910 dev->req.bi_vcnt++;
1911 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001913 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
NeilBrown977df362011-12-23 10:17:53 +11001915 bio_init(&dev->rreq);
1916 dev->rreq.bi_io_vec = &dev->rvec;
1917 dev->rreq.bi_vcnt++;
1918 dev->rreq.bi_max_vecs++;
1919 dev->rreq.bi_private = sh;
1920 dev->rvec.bv_page = dev->page;
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001923 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
NeilBrownfd01b882011-10-11 16:47:53 +11001926static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
1928 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001929 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001930 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001931 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
NeilBrown908f4fb2011-12-23 10:17:50 +11001933 spin_lock_irqsave(&conf->device_lock, flags);
1934 clear_bit(In_sync, &rdev->flags);
1935 mddev->degraded = calc_degraded(conf);
1936 spin_unlock_irqrestore(&conf->device_lock, flags);
1937 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1938
NeilBrownde393cd2011-07-28 11:31:48 +10001939 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001940 set_bit(Faulty, &rdev->flags);
1941 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1942 printk(KERN_ALERT
1943 "md/raid:%s: Disk failure on %s, disabling device.\n"
1944 "md/raid:%s: Operation continuing on %d devices.\n",
1945 mdname(mddev),
1946 bdevname(rdev->bdev, b),
1947 mdname(mddev),
1948 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001949}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951/*
1952 * Input: a 'big' sector number,
1953 * Output: index of the data and parity disk, and the sector # in them.
1954 */
NeilBrownd1688a62011-10-11 16:49:52 +11001955static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001956 int previous, int *dd_idx,
1957 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001959 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001960 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001962 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001963 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001965 int algorithm = previous ? conf->prev_algo
1966 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001967 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1968 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001969 int raid_disks = previous ? conf->previous_raid_disks
1970 : conf->raid_disks;
1971 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 /* First compute the information on this sector */
1974
1975 /*
1976 * Compute the chunk number and the sector offset inside the chunk
1977 */
1978 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1979 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 /*
1982 * Compute the stripe number
1983 */
NeilBrown35f2a592010-04-20 14:13:34 +10001984 stripe = chunk_number;
1985 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001986 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 /*
1988 * Select the parity disk based on the user selected algorithm.
1989 */
NeilBrown84789552011-07-27 11:00:36 +10001990 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001991 switch(conf->level) {
1992 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001993 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001994 break;
1995 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001996 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001998 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001999 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 (*dd_idx)++;
2001 break;
2002 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002003 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002004 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 (*dd_idx)++;
2006 break;
2007 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002008 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002009 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 break;
2011 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002012 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002013 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002015 case ALGORITHM_PARITY_0:
2016 pd_idx = 0;
2017 (*dd_idx)++;
2018 break;
2019 case ALGORITHM_PARITY_N:
2020 pd_idx = data_disks;
2021 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002023 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002024 }
2025 break;
2026 case 6:
2027
NeilBrowne183eae2009-03-31 15:20:22 +11002028 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002029 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002030 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002031 qd_idx = pd_idx + 1;
2032 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002033 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002034 qd_idx = 0;
2035 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002036 (*dd_idx) += 2; /* D D P Q D */
2037 break;
2038 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002039 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002040 qd_idx = pd_idx + 1;
2041 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002042 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002043 qd_idx = 0;
2044 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002045 (*dd_idx) += 2; /* D D P Q D */
2046 break;
2047 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002048 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002049 qd_idx = (pd_idx + 1) % raid_disks;
2050 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002051 break;
2052 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002053 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002054 qd_idx = (pd_idx + 1) % raid_disks;
2055 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002056 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002057
2058 case ALGORITHM_PARITY_0:
2059 pd_idx = 0;
2060 qd_idx = 1;
2061 (*dd_idx) += 2;
2062 break;
2063 case ALGORITHM_PARITY_N:
2064 pd_idx = data_disks;
2065 qd_idx = data_disks + 1;
2066 break;
2067
2068 case ALGORITHM_ROTATING_ZERO_RESTART:
2069 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2070 * of blocks for computing Q is different.
2071 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002072 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002073 qd_idx = pd_idx + 1;
2074 if (pd_idx == raid_disks-1) {
2075 (*dd_idx)++; /* Q D D D P */
2076 qd_idx = 0;
2077 } else if (*dd_idx >= pd_idx)
2078 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002079 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002080 break;
2081
2082 case ALGORITHM_ROTATING_N_RESTART:
2083 /* Same a left_asymmetric, by first stripe is
2084 * D D D P Q rather than
2085 * Q D D D P
2086 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002087 stripe2 += 1;
2088 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002089 qd_idx = pd_idx + 1;
2090 if (pd_idx == raid_disks-1) {
2091 (*dd_idx)++; /* Q D D D P */
2092 qd_idx = 0;
2093 } else if (*dd_idx >= pd_idx)
2094 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002095 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002096 break;
2097
2098 case ALGORITHM_ROTATING_N_CONTINUE:
2099 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002100 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002101 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2102 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002103 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002104 break;
2105
2106 case ALGORITHM_LEFT_ASYMMETRIC_6:
2107 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002108 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002109 if (*dd_idx >= pd_idx)
2110 (*dd_idx)++;
2111 qd_idx = raid_disks - 1;
2112 break;
2113
2114 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002115 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002116 if (*dd_idx >= pd_idx)
2117 (*dd_idx)++;
2118 qd_idx = raid_disks - 1;
2119 break;
2120
2121 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002122 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002123 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2124 qd_idx = raid_disks - 1;
2125 break;
2126
2127 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002128 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002129 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2130 qd_idx = raid_disks - 1;
2131 break;
2132
2133 case ALGORITHM_PARITY_0_6:
2134 pd_idx = 0;
2135 (*dd_idx)++;
2136 qd_idx = raid_disks - 1;
2137 break;
2138
NeilBrown16a53ec2006-06-26 00:27:38 -07002139 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002140 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002141 }
2142 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 }
2144
NeilBrown911d4ee2009-03-31 14:39:38 +11002145 if (sh) {
2146 sh->pd_idx = pd_idx;
2147 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002148 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 /*
2151 * Finally, compute the new sector number
2152 */
2153 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2154 return new_sector;
2155}
2156
2157
NeilBrown784052e2009-03-31 15:19:07 +11002158static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
NeilBrownd1688a62011-10-11 16:49:52 +11002160 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002161 int raid_disks = sh->disks;
2162 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002164 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2165 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002166 int algorithm = previous ? conf->prev_algo
2167 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 sector_t stripe;
2169 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002170 sector_t chunk_number;
2171 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002173 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
NeilBrown16a53ec2006-06-26 00:27:38 -07002175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2177 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
NeilBrown16a53ec2006-06-26 00:27:38 -07002179 if (i == sh->pd_idx)
2180 return 0;
2181 switch(conf->level) {
2182 case 4: break;
2183 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002184 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 case ALGORITHM_LEFT_ASYMMETRIC:
2186 case ALGORITHM_RIGHT_ASYMMETRIC:
2187 if (i > sh->pd_idx)
2188 i--;
2189 break;
2190 case ALGORITHM_LEFT_SYMMETRIC:
2191 case ALGORITHM_RIGHT_SYMMETRIC:
2192 if (i < sh->pd_idx)
2193 i += raid_disks;
2194 i -= (sh->pd_idx + 1);
2195 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002196 case ALGORITHM_PARITY_0:
2197 i -= 1;
2198 break;
2199 case ALGORITHM_PARITY_N:
2200 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002202 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002203 }
2204 break;
2205 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002206 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002207 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002208 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002209 case ALGORITHM_LEFT_ASYMMETRIC:
2210 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002211 case ALGORITHM_ROTATING_ZERO_RESTART:
2212 case ALGORITHM_ROTATING_N_RESTART:
2213 if (sh->pd_idx == raid_disks-1)
2214 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002215 else if (i > sh->pd_idx)
2216 i -= 2; /* D D P Q D */
2217 break;
2218 case ALGORITHM_LEFT_SYMMETRIC:
2219 case ALGORITHM_RIGHT_SYMMETRIC:
2220 if (sh->pd_idx == raid_disks-1)
2221 i--; /* Q D D D P */
2222 else {
2223 /* D D P Q D */
2224 if (i < sh->pd_idx)
2225 i += raid_disks;
2226 i -= (sh->pd_idx + 2);
2227 }
2228 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002229 case ALGORITHM_PARITY_0:
2230 i -= 2;
2231 break;
2232 case ALGORITHM_PARITY_N:
2233 break;
2234 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002235 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002236 if (sh->pd_idx == 0)
2237 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002238 else {
2239 /* D D Q P D */
2240 if (i < sh->pd_idx)
2241 i += raid_disks;
2242 i -= (sh->pd_idx + 1);
2243 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002244 break;
2245 case ALGORITHM_LEFT_ASYMMETRIC_6:
2246 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2247 if (i > sh->pd_idx)
2248 i--;
2249 break;
2250 case ALGORITHM_LEFT_SYMMETRIC_6:
2251 case ALGORITHM_RIGHT_SYMMETRIC_6:
2252 if (i < sh->pd_idx)
2253 i += data_disks + 1;
2254 i -= (sh->pd_idx + 1);
2255 break;
2256 case ALGORITHM_PARITY_0_6:
2257 i -= 1;
2258 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002259 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002260 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002261 }
2262 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 }
2264
2265 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002266 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
NeilBrown112bf892009-03-31 14:39:38 +11002268 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002269 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002270 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2271 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002272 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2273 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 return 0;
2275 }
2276 return r_sector;
2277}
2278
2279
Dan Williams600aa102008-06-28 08:32:05 +10002280static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002281schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002282 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002283{
2284 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002285 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002286 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002287
Dan Williamse33129d2007-01-02 13:52:30 -07002288 if (rcw) {
Dan Williamse33129d2007-01-02 13:52:30 -07002289
2290 for (i = disks; i--; ) {
2291 struct r5dev *dev = &sh->dev[i];
2292
2293 if (dev->towrite) {
2294 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002295 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002296 if (!expand)
2297 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002298 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002299 }
2300 }
NeilBrownce7d3632013-03-04 12:37:14 +11002301 /* if we are not expanding this is a proper write request, and
2302 * there will be bios with new data to be drained into the
2303 * stripe cache
2304 */
2305 if (!expand) {
2306 if (!s->locked)
2307 /* False alarm, nothing to do */
2308 return;
2309 sh->reconstruct_state = reconstruct_state_drain_run;
2310 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2311 } else
2312 sh->reconstruct_state = reconstruct_state_run;
2313
2314 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2315
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002316 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002317 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002318 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002319 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002320 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002321 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2322 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2323
Dan Williamse33129d2007-01-02 13:52:30 -07002324 for (i = disks; i--; ) {
2325 struct r5dev *dev = &sh->dev[i];
2326 if (i == pd_idx)
2327 continue;
2328
Dan Williamse33129d2007-01-02 13:52:30 -07002329 if (dev->towrite &&
2330 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002331 test_bit(R5_Wantcompute, &dev->flags))) {
2332 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002333 set_bit(R5_LOCKED, &dev->flags);
2334 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002335 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002336 }
2337 }
NeilBrownce7d3632013-03-04 12:37:14 +11002338 if (!s->locked)
2339 /* False alarm - nothing to do */
2340 return;
2341 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2342 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2343 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2344 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002345 }
2346
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002347 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002348 * are in flight
2349 */
2350 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2351 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002352 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002353
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002354 if (level == 6) {
2355 int qd_idx = sh->qd_idx;
2356 struct r5dev *dev = &sh->dev[qd_idx];
2357
2358 set_bit(R5_LOCKED, &dev->flags);
2359 clear_bit(R5_UPTODATE, &dev->flags);
2360 s->locked++;
2361 }
2362
Dan Williams600aa102008-06-28 08:32:05 +10002363 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002364 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002365 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002366}
NeilBrown16a53ec2006-06-26 00:27:38 -07002367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368/*
2369 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002370 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 * The bi_next chain must be in order.
2372 */
2373static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2374{
2375 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002376 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002377 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
NeilBrowncbe47ec2011-07-26 11:20:35 +10002379 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 (unsigned long long)bi->bi_sector,
2381 (unsigned long long)sh->sector);
2382
Shaohua Lib17459c2012-07-19 16:01:31 +10002383 /*
2384 * If several bio share a stripe. The bio bi_phys_segments acts as a
2385 * reference count to avoid race. The reference count should already be
2386 * increased before this function is called (for example, in
2387 * make_request()), so other bio sharing this stripe will not free the
2388 * stripe. If a stripe is owned by one stripe, the stripe lock will
2389 * protect it.
2390 */
2391 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002392 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002394 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002395 firstwrite = 1;
2396 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 bip = &sh->dev[dd_idx].toread;
2398 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2399 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2400 goto overlap;
2401 bip = & (*bip)->bi_next;
2402 }
2403 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2404 goto overlap;
2405
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002406 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (*bip)
2408 bi->bi_next = *bip;
2409 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002410 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (forwrite) {
2413 /* check if page is covered */
2414 sector_t sector = sh->dev[dd_idx].sector;
2415 for (bi=sh->dev[dd_idx].towrite;
2416 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2417 bi && bi->bi_sector <= sector;
2418 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2419 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2420 sector = bi->bi_sector + (bi->bi_size>>9);
2421 }
2422 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2423 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2424 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002425
2426 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2427 (unsigned long long)(*bip)->bi_sector,
2428 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002429 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002430
2431 if (conf->mddev->bitmap && firstwrite) {
2432 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2433 STRIPE_SECTORS, 0);
2434 sh->bm_seq = conf->seq_flush+1;
2435 set_bit(STRIPE_BIT_DELAY, &sh->state);
2436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 return 1;
2438
2439 overlap:
2440 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002441 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 return 0;
2443}
2444
NeilBrownd1688a62011-10-11 16:49:52 +11002445static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002446
NeilBrownd1688a62011-10-11 16:49:52 +11002447static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002448 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002449{
NeilBrown784052e2009-03-31 15:19:07 +11002450 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002451 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002452 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002453 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002454 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002455
NeilBrown112bf892009-03-31 14:39:38 +11002456 raid5_compute_sector(conf,
2457 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002458 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002459 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002460 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002461}
2462
Dan Williamsa4456852007-07-09 11:56:43 -07002463static void
NeilBrownd1688a62011-10-11 16:49:52 +11002464handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002465 struct stripe_head_state *s, int disks,
2466 struct bio **return_bi)
2467{
2468 int i;
2469 for (i = disks; i--; ) {
2470 struct bio *bi;
2471 int bitmap_end = 0;
2472
2473 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002474 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002475 rcu_read_lock();
2476 rdev = rcu_dereference(conf->disks[i].rdev);
2477 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002478 atomic_inc(&rdev->nr_pending);
2479 else
2480 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002481 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002482 if (rdev) {
2483 if (!rdev_set_badblocks(
2484 rdev,
2485 sh->sector,
2486 STRIPE_SECTORS, 0))
2487 md_error(conf->mddev, rdev);
2488 rdev_dec_pending(rdev, conf->mddev);
2489 }
Dan Williamsa4456852007-07-09 11:56:43 -07002490 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002491 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002492 /* fail all writes first */
2493 bi = sh->dev[i].towrite;
2494 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002495 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002496 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002497 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002498
2499 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2500 wake_up(&conf->wait_for_overlap);
2501
2502 while (bi && bi->bi_sector <
2503 sh->dev[i].sector + STRIPE_SECTORS) {
2504 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2505 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002506 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002507 md_write_end(conf->mddev);
2508 bi->bi_next = *return_bi;
2509 *return_bi = bi;
2510 }
2511 bi = nextbi;
2512 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002513 if (bitmap_end)
2514 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2515 STRIPE_SECTORS, 0, 0);
2516 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002517 /* and fail all 'written' */
2518 bi = sh->dev[i].written;
2519 sh->dev[i].written = NULL;
2520 if (bi) bitmap_end = 1;
2521 while (bi && bi->bi_sector <
2522 sh->dev[i].sector + STRIPE_SECTORS) {
2523 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2524 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002525 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002526 md_write_end(conf->mddev);
2527 bi->bi_next = *return_bi;
2528 *return_bi = bi;
2529 }
2530 bi = bi2;
2531 }
2532
Dan Williamsb5e98d62007-01-02 13:52:31 -07002533 /* fail any reads if this device is non-operational and
2534 * the data has not reached the cache yet.
2535 */
2536 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2537 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2538 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002539 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002540 bi = sh->dev[i].toread;
2541 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002542 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002543 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2544 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002545 while (bi && bi->bi_sector <
2546 sh->dev[i].sector + STRIPE_SECTORS) {
2547 struct bio *nextbi =
2548 r5_next_bio(bi, sh->dev[i].sector);
2549 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002550 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002551 bi->bi_next = *return_bi;
2552 *return_bi = bi;
2553 }
2554 bi = nextbi;
2555 }
2556 }
Dan Williamsa4456852007-07-09 11:56:43 -07002557 if (bitmap_end)
2558 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2559 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002560 /* If we were in the middle of a write the parity block might
2561 * still be locked - so just clear all R5_LOCKED flags
2562 */
2563 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002564 }
2565
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002566 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2567 if (atomic_dec_and_test(&conf->pending_full_writes))
2568 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002569}
2570
NeilBrown7f0da592011-07-28 11:39:22 +10002571static void
NeilBrownd1688a62011-10-11 16:49:52 +11002572handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002573 struct stripe_head_state *s)
2574{
2575 int abort = 0;
2576 int i;
2577
NeilBrown7f0da592011-07-28 11:39:22 +10002578 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002579 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
2580 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10002581 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002582 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002583 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002584 * Don't even need to abort as that is handled elsewhere
2585 * if needed, and not always wanted e.g. if there is a known
2586 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002587 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002588 * non-sync devices, or abort the recovery
2589 */
NeilBrown18b98372012-04-01 23:48:38 +10002590 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2591 /* During recovery devices cannot be removed, so
2592 * locking and refcounting of rdevs is not needed
2593 */
2594 for (i = 0; i < conf->raid_disks; i++) {
2595 struct md_rdev *rdev = conf->disks[i].rdev;
2596 if (rdev
2597 && !test_bit(Faulty, &rdev->flags)
2598 && !test_bit(In_sync, &rdev->flags)
2599 && !rdev_set_badblocks(rdev, sh->sector,
2600 STRIPE_SECTORS, 0))
2601 abort = 1;
2602 rdev = conf->disks[i].replacement;
2603 if (rdev
2604 && !test_bit(Faulty, &rdev->flags)
2605 && !test_bit(In_sync, &rdev->flags)
2606 && !rdev_set_badblocks(rdev, sh->sector,
2607 STRIPE_SECTORS, 0))
2608 abort = 1;
2609 }
2610 if (abort)
2611 conf->recovery_disabled =
2612 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002613 }
NeilBrown18b98372012-04-01 23:48:38 +10002614 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002615}
2616
NeilBrown9a3e1102011-12-23 10:17:53 +11002617static int want_replace(struct stripe_head *sh, int disk_idx)
2618{
2619 struct md_rdev *rdev;
2620 int rv = 0;
2621 /* Doing recovery so rcu locking not required */
2622 rdev = sh->raid_conf->disks[disk_idx].replacement;
2623 if (rdev
2624 && !test_bit(Faulty, &rdev->flags)
2625 && !test_bit(In_sync, &rdev->flags)
2626 && (rdev->recovery_offset <= sh->sector
2627 || rdev->mddev->recovery_cp <= sh->sector))
2628 rv = 1;
2629
2630 return rv;
2631}
2632
NeilBrown93b3dbc2011-07-27 11:00:36 +10002633/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002634 * to be read or computed to satisfy a request.
2635 *
2636 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002637 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002638 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002639static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2640 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002641{
2642 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002643 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2644 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002645
Dan Williamsf38e1212007-01-02 13:52:30 -07002646 /* is the data in this block needed, and can we get it? */
2647 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002648 !test_bit(R5_UPTODATE, &dev->flags) &&
2649 (dev->toread ||
2650 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2651 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002652 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002653 (s->failed >= 1 && fdev[0]->toread) ||
2654 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002655 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2656 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2657 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002658 /* we would like to get this block, possibly by computing it,
2659 * otherwise read it if the backing disk is insync
2660 */
2661 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2662 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2663 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002664 (s->failed && (disk_idx == s->failed_num[0] ||
2665 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002666 /* have disk failed, and we're requested to fetch it;
2667 * do compute it
2668 */
2669 pr_debug("Computing stripe %llu block %d\n",
2670 (unsigned long long)sh->sector, disk_idx);
2671 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2672 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2673 set_bit(R5_Wantcompute, &dev->flags);
2674 sh->ops.target = disk_idx;
2675 sh->ops.target2 = -1; /* no 2nd target */
2676 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002677 /* Careful: from this point on 'uptodate' is in the eye
2678 * of raid_run_ops which services 'compute' operations
2679 * before writes. R5_Wantcompute flags a block that will
2680 * be R5_UPTODATE by the time it is needed for a
2681 * subsequent operation.
2682 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002683 s->uptodate++;
2684 return 1;
2685 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2686 /* Computing 2-failure is *very* expensive; only
2687 * do it if failed >= 2
2688 */
2689 int other;
2690 for (other = disks; other--; ) {
2691 if (other == disk_idx)
2692 continue;
2693 if (!test_bit(R5_UPTODATE,
2694 &sh->dev[other].flags))
2695 break;
2696 }
2697 BUG_ON(other < 0);
2698 pr_debug("Computing stripe %llu blocks %d,%d\n",
2699 (unsigned long long)sh->sector,
2700 disk_idx, other);
2701 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2702 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2703 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2704 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2705 sh->ops.target = disk_idx;
2706 sh->ops.target2 = other;
2707 s->uptodate += 2;
2708 s->req_compute = 1;
2709 return 1;
2710 } else if (test_bit(R5_Insync, &dev->flags)) {
2711 set_bit(R5_LOCKED, &dev->flags);
2712 set_bit(R5_Wantread, &dev->flags);
2713 s->locked++;
2714 pr_debug("Reading block %d (sync=%d)\n",
2715 disk_idx, s->syncing);
2716 }
2717 }
2718
2719 return 0;
2720}
2721
2722/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002723 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002724 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002725static void handle_stripe_fill(struct stripe_head *sh,
2726 struct stripe_head_state *s,
2727 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002728{
2729 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002730
2731 /* look for blocks to read/compute, skip this if a compute
2732 * is already in flight, or if the stripe contents are in the
2733 * midst of changing due to a write
2734 */
2735 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2736 !sh->reconstruct_state)
2737 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002738 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002739 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002740 set_bit(STRIPE_HANDLE, &sh->state);
2741}
2742
2743
Dan Williams1fe797e2008-06-28 09:16:30 +10002744/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002745 * any written block on an uptodate or failed drive can be returned.
2746 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2747 * never LOCKED, so we don't need to test 'failed' directly.
2748 */
NeilBrownd1688a62011-10-11 16:49:52 +11002749static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002750 struct stripe_head *sh, int disks, struct bio **return_bi)
2751{
2752 int i;
2753 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11002754 int discard_pending = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002755
2756 for (i = disks; i--; )
2757 if (sh->dev[i].written) {
2758 dev = &sh->dev[i];
2759 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11002760 (test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownca64cae2012-11-21 16:33:40 +11002761 test_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002762 /* We can return any write requests */
2763 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002764 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11002765 if (test_and_clear_bit(R5_Discard, &dev->flags))
2766 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002767 wbi = dev->written;
2768 dev->written = NULL;
2769 while (wbi && wbi->bi_sector <
2770 dev->sector + STRIPE_SECTORS) {
2771 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002772 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002773 md_write_end(conf->mddev);
2774 wbi->bi_next = *return_bi;
2775 *return_bi = wbi;
2776 }
2777 wbi = wbi2;
2778 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002779 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2780 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002781 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002782 0);
NeilBrownf8dfcff2013-03-12 12:18:06 +11002783 } else if (test_bit(R5_Discard, &dev->flags))
2784 discard_pending = 1;
2785 }
2786 if (!discard_pending &&
2787 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
2788 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
2789 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
2790 if (sh->qd_idx >= 0) {
2791 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
2792 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
2793 }
2794 /* now that discard is done we can proceed with any sync */
2795 clear_bit(STRIPE_DISCARD, &sh->state);
2796 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
2797 set_bit(STRIPE_HANDLE, &sh->state);
2798
2799 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002800
2801 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2802 if (atomic_dec_and_test(&conf->pending_full_writes))
2803 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002804}
2805
NeilBrownd1688a62011-10-11 16:49:52 +11002806static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002807 struct stripe_head *sh,
2808 struct stripe_head_state *s,
2809 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002810{
2811 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002812 sector_t recovery_cp = conf->mddev->recovery_cp;
2813
2814 /* RAID6 requires 'rcw' in current implementation.
2815 * Otherwise, check whether resync is now happening or should start.
2816 * If yes, then the array is dirty (after unclean shutdown or
2817 * initial creation), so parity in some stripes might be inconsistent.
2818 * In this case, we need to always do reconstruct-write, to ensure
2819 * that in case of drive failure or read-error correction, we
2820 * generate correct data from the parity.
2821 */
2822 if (conf->max_degraded == 2 ||
2823 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2824 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002825 * look like rcw is cheaper
2826 */
2827 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002828 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2829 conf->max_degraded, (unsigned long long)recovery_cp,
2830 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002831 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002832 /* would I have to read this buffer for read_modify_write */
2833 struct r5dev *dev = &sh->dev[i];
2834 if ((dev->towrite || i == sh->pd_idx) &&
2835 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002836 !(test_bit(R5_UPTODATE, &dev->flags) ||
2837 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002838 if (test_bit(R5_Insync, &dev->flags))
2839 rmw++;
2840 else
2841 rmw += 2*disks; /* cannot read it */
2842 }
2843 /* Would I have to read this buffer for reconstruct_write */
2844 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2845 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002846 !(test_bit(R5_UPTODATE, &dev->flags) ||
2847 test_bit(R5_Wantcompute, &dev->flags))) {
2848 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002849 else
2850 rcw += 2*disks;
2851 }
2852 }
Dan Williams45b42332007-07-09 11:56:43 -07002853 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002854 (unsigned long long)sh->sector, rmw, rcw);
2855 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11002856 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002857 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06002858 if (conf->mddev->queue)
2859 blk_add_trace_msg(conf->mddev->queue,
2860 "raid5 rmw %llu %d",
2861 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07002862 for (i = disks; i--; ) {
2863 struct r5dev *dev = &sh->dev[i];
2864 if ((dev->towrite || i == sh->pd_idx) &&
2865 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002866 !(test_bit(R5_UPTODATE, &dev->flags) ||
2867 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002868 test_bit(R5_Insync, &dev->flags)) {
2869 if (
2870 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002871 pr_debug("Read_old block "
NeilBrowna9add5d2012-10-31 11:59:09 +11002872 "%d for r-m-w\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002873 set_bit(R5_LOCKED, &dev->flags);
2874 set_bit(R5_Wantread, &dev->flags);
2875 s->locked++;
2876 } else {
2877 set_bit(STRIPE_DELAYED, &sh->state);
2878 set_bit(STRIPE_HANDLE, &sh->state);
2879 }
2880 }
2881 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002882 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002883 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002884 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002885 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10002886 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002887 for (i = disks; i--; ) {
2888 struct r5dev *dev = &sh->dev[i];
2889 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002890 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002891 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002892 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002893 test_bit(R5_Wantcompute, &dev->flags))) {
2894 rcw++;
2895 if (!test_bit(R5_Insync, &dev->flags))
2896 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002897 if (
2898 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002899 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002900 "%d for Reconstruct\n", i);
2901 set_bit(R5_LOCKED, &dev->flags);
2902 set_bit(R5_Wantread, &dev->flags);
2903 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11002904 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07002905 } else {
2906 set_bit(STRIPE_DELAYED, &sh->state);
2907 set_bit(STRIPE_HANDLE, &sh->state);
2908 }
2909 }
2910 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06002911 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11002912 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
2913 (unsigned long long)sh->sector,
2914 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10002915 }
Dan Williamsa4456852007-07-09 11:56:43 -07002916 /* now if nothing is locked, and if we have enough data,
2917 * we can start a write request
2918 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002919 /* since handle_stripe can be called at any time we need to handle the
2920 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002921 * subsequent call wants to start a write request. raid_run_ops only
2922 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002923 * simultaneously. If this is not the case then new writes need to be
2924 * held off until the compute completes.
2925 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002926 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2927 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2928 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002929 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002930}
2931
NeilBrownd1688a62011-10-11 16:49:52 +11002932static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002933 struct stripe_head_state *s, int disks)
2934{
Dan Williamsecc65c92008-06-28 08:31:57 +10002935 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002936
Dan Williamsbd2ab672008-04-10 21:29:27 -07002937 set_bit(STRIPE_HANDLE, &sh->state);
2938
Dan Williamsecc65c92008-06-28 08:31:57 +10002939 switch (sh->check_state) {
2940 case check_state_idle:
2941 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002942 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002943 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002944 sh->check_state = check_state_run;
2945 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002946 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002947 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002948 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002949 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002950 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002951 /* fall through */
2952 case check_state_compute_result:
2953 sh->check_state = check_state_idle;
2954 if (!dev)
2955 dev = &sh->dev[sh->pd_idx];
2956
2957 /* check that a write has not made the stripe insync */
2958 if (test_bit(STRIPE_INSYNC, &sh->state))
2959 break;
2960
2961 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002962 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2963 BUG_ON(s->uptodate != disks);
2964
2965 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002966 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002967 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002968
Dan Williamsa4456852007-07-09 11:56:43 -07002969 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002970 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002971 break;
2972 case check_state_run:
2973 break; /* we will be called again upon completion */
2974 case check_state_check_result:
2975 sh->check_state = check_state_idle;
2976
2977 /* if a failure occurred during the check operation, leave
2978 * STRIPE_INSYNC not set and let the stripe be handled again
2979 */
2980 if (s->failed)
2981 break;
2982
2983 /* handle a successful check operation, if parity is correct
2984 * we are done. Otherwise update the mismatch count and repair
2985 * parity if !MD_RECOVERY_CHECK
2986 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002987 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002988 /* parity is correct (on disc,
2989 * not in buffer any more)
2990 */
2991 set_bit(STRIPE_INSYNC, &sh->state);
2992 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002993 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10002994 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2995 /* don't try to repair!! */
2996 set_bit(STRIPE_INSYNC, &sh->state);
2997 else {
2998 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002999 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003000 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3001 set_bit(R5_Wantcompute,
3002 &sh->dev[sh->pd_idx].flags);
3003 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003004 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003005 s->uptodate++;
3006 }
3007 }
3008 break;
3009 case check_state_compute_run:
3010 break;
3011 default:
3012 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3013 __func__, sh->check_state,
3014 (unsigned long long) sh->sector);
3015 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003016 }
3017}
3018
3019
NeilBrownd1688a62011-10-11 16:49:52 +11003020static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003021 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003022 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003023{
Dan Williamsa4456852007-07-09 11:56:43 -07003024 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003025 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003026 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003027
3028 set_bit(STRIPE_HANDLE, &sh->state);
3029
3030 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003031
Dan Williamsa4456852007-07-09 11:56:43 -07003032 /* Want to check and possibly repair P and Q.
3033 * However there could be one 'failed' device, in which
3034 * case we can only check one of them, possibly using the
3035 * other to generate missing data
3036 */
3037
Dan Williamsd82dfee2009-07-14 13:40:57 -07003038 switch (sh->check_state) {
3039 case check_state_idle:
3040 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003041 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003042 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003043 * makes sense to check P (If anything else were failed,
3044 * we would have used P to recreate it).
3045 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003046 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003047 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003048 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003049 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003050 * anything, so it makes sense to check it
3051 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003052 if (sh->check_state == check_state_run)
3053 sh->check_state = check_state_run_pq;
3054 else
3055 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003056 }
Dan Williams36d1c642009-07-14 11:48:22 -07003057
Dan Williamsd82dfee2009-07-14 13:40:57 -07003058 /* discard potentially stale zero_sum_result */
3059 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003060
Dan Williamsd82dfee2009-07-14 13:40:57 -07003061 if (sh->check_state == check_state_run) {
3062 /* async_xor_zero_sum destroys the contents of P */
3063 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3064 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003065 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003066 if (sh->check_state >= check_state_run &&
3067 sh->check_state <= check_state_run_pq) {
3068 /* async_syndrome_zero_sum preserves P and Q, so
3069 * no need to mark them !uptodate here
3070 */
3071 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3072 break;
3073 }
Dan Williams36d1c642009-07-14 11:48:22 -07003074
Dan Williamsd82dfee2009-07-14 13:40:57 -07003075 /* we have 2-disk failure */
3076 BUG_ON(s->failed != 2);
3077 /* fall through */
3078 case check_state_compute_result:
3079 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003080
Dan Williamsd82dfee2009-07-14 13:40:57 -07003081 /* check that a write has not made the stripe insync */
3082 if (test_bit(STRIPE_INSYNC, &sh->state))
3083 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003084
3085 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003086 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003087 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003088 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003089 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003090 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003091 s->locked++;
3092 set_bit(R5_LOCKED, &dev->flags);
3093 set_bit(R5_Wantwrite, &dev->flags);
3094 }
3095 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003096 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003097 s->locked++;
3098 set_bit(R5_LOCKED, &dev->flags);
3099 set_bit(R5_Wantwrite, &dev->flags);
3100 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003101 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003102 dev = &sh->dev[pd_idx];
3103 s->locked++;
3104 set_bit(R5_LOCKED, &dev->flags);
3105 set_bit(R5_Wantwrite, &dev->flags);
3106 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003107 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003108 dev = &sh->dev[qd_idx];
3109 s->locked++;
3110 set_bit(R5_LOCKED, &dev->flags);
3111 set_bit(R5_Wantwrite, &dev->flags);
3112 }
3113 clear_bit(STRIPE_DEGRADED, &sh->state);
3114
3115 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003116 break;
3117 case check_state_run:
3118 case check_state_run_q:
3119 case check_state_run_pq:
3120 break; /* we will be called again upon completion */
3121 case check_state_check_result:
3122 sh->check_state = check_state_idle;
3123
3124 /* handle a successful check operation, if parity is correct
3125 * we are done. Otherwise update the mismatch count and repair
3126 * parity if !MD_RECOVERY_CHECK
3127 */
3128 if (sh->ops.zero_sum_result == 0) {
3129 /* both parities are correct */
3130 if (!s->failed)
3131 set_bit(STRIPE_INSYNC, &sh->state);
3132 else {
3133 /* in contrast to the raid5 case we can validate
3134 * parity, but still have a failure to write
3135 * back
3136 */
3137 sh->check_state = check_state_compute_result;
3138 /* Returning at this point means that we may go
3139 * off and bring p and/or q uptodate again so
3140 * we make sure to check zero_sum_result again
3141 * to verify if p or q need writeback
3142 */
3143 }
3144 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003145 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003146 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3147 /* don't try to repair!! */
3148 set_bit(STRIPE_INSYNC, &sh->state);
3149 else {
3150 int *target = &sh->ops.target;
3151
3152 sh->ops.target = -1;
3153 sh->ops.target2 = -1;
3154 sh->check_state = check_state_compute_run;
3155 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3156 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3157 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3158 set_bit(R5_Wantcompute,
3159 &sh->dev[pd_idx].flags);
3160 *target = pd_idx;
3161 target = &sh->ops.target2;
3162 s->uptodate++;
3163 }
3164 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3165 set_bit(R5_Wantcompute,
3166 &sh->dev[qd_idx].flags);
3167 *target = qd_idx;
3168 s->uptodate++;
3169 }
3170 }
3171 }
3172 break;
3173 case check_state_compute_run:
3174 break;
3175 default:
3176 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3177 __func__, sh->check_state,
3178 (unsigned long long) sh->sector);
3179 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003180 }
3181}
3182
NeilBrownd1688a62011-10-11 16:49:52 +11003183static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003184{
3185 int i;
3186
3187 /* We have read all the blocks in this stripe and now we need to
3188 * copy some of them into a target stripe for expand.
3189 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003190 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003191 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3192 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003193 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003194 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003195 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003196 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003197
NeilBrown784052e2009-03-31 15:19:07 +11003198 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003199 sector_t s = raid5_compute_sector(conf, bn, 0,
3200 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003201 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003202 if (sh2 == NULL)
3203 /* so far only the early blocks of this stripe
3204 * have been requested. When later blocks
3205 * get requested, we will try again
3206 */
3207 continue;
3208 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3209 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3210 /* must have already done this block */
3211 release_stripe(sh2);
3212 continue;
3213 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003214
3215 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003216 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003217 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003218 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003219 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003220
Dan Williamsa4456852007-07-09 11:56:43 -07003221 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3222 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3223 for (j = 0; j < conf->raid_disks; j++)
3224 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003225 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003226 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3227 break;
3228 if (j == conf->raid_disks) {
3229 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3230 set_bit(STRIPE_HANDLE, &sh2->state);
3231 }
3232 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003233
Dan Williamsa4456852007-07-09 11:56:43 -07003234 }
NeilBrowna2e08552007-09-11 15:23:36 -07003235 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003236 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003237}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
3239/*
3240 * handle_stripe - do things to a stripe.
3241 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003242 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3243 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003245 * return some read requests which now have data
3246 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 * schedule a read on some buffers
3248 * schedule a write of some buffers
3249 * return confirmation of parity correctness
3250 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 */
Dan Williamsa4456852007-07-09 11:56:43 -07003252
NeilBrownacfe7262011-07-27 11:00:36 +10003253static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003254{
NeilBrownd1688a62011-10-11 16:49:52 +11003255 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003256 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003257 struct r5dev *dev;
3258 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003259 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003260
NeilBrownacfe7262011-07-27 11:00:36 +10003261 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003262
NeilBrownacfe7262011-07-27 11:00:36 +10003263 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3264 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3265 s->failed_num[0] = -1;
3266 s->failed_num[1] = -1;
3267
3268 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003269 rcu_read_lock();
3270 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003271 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003272 sector_t first_bad;
3273 int bad_sectors;
3274 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003275
NeilBrown16a53ec2006-06-26 00:27:38 -07003276 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003277
Dan Williams45b42332007-07-09 11:56:43 -07003278 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003279 i, dev->flags,
3280 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003281 /* maybe we can reply to a read
3282 *
3283 * new wantfill requests are only permitted while
3284 * ops_complete_biofill is guaranteed to be inactive
3285 */
3286 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3287 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3288 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003289
3290 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003291 if (test_bit(R5_LOCKED, &dev->flags))
3292 s->locked++;
3293 if (test_bit(R5_UPTODATE, &dev->flags))
3294 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003295 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003296 s->compute++;
3297 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003298 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003299
NeilBrownacfe7262011-07-27 11:00:36 +10003300 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003301 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003302 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003303 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003304 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003305 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003306 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003307 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003308 }
Dan Williamsa4456852007-07-09 11:56:43 -07003309 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003310 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003311 /* Prefer to use the replacement for reads, but only
3312 * if it is recovered enough and has no bad blocks.
3313 */
3314 rdev = rcu_dereference(conf->disks[i].replacement);
3315 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3316 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3317 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3318 &first_bad, &bad_sectors))
3319 set_bit(R5_ReadRepl, &dev->flags);
3320 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003321 if (rdev)
3322 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003323 rdev = rcu_dereference(conf->disks[i].rdev);
3324 clear_bit(R5_ReadRepl, &dev->flags);
3325 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003326 if (rdev && test_bit(Faulty, &rdev->flags))
3327 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003328 if (rdev) {
3329 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3330 &first_bad, &bad_sectors);
3331 if (s->blocked_rdev == NULL
3332 && (test_bit(Blocked, &rdev->flags)
3333 || is_bad < 0)) {
3334 if (is_bad < 0)
3335 set_bit(BlockedBadBlocks,
3336 &rdev->flags);
3337 s->blocked_rdev = rdev;
3338 atomic_inc(&rdev->nr_pending);
3339 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003340 }
NeilBrown415e72d2010-06-17 17:25:21 +10003341 clear_bit(R5_Insync, &dev->flags);
3342 if (!rdev)
3343 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003344 else if (is_bad) {
3345 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003346 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3347 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003348 /* treat as in-sync, but with a read error
3349 * which we can now try to correct
3350 */
3351 set_bit(R5_Insync, &dev->flags);
3352 set_bit(R5_ReadError, &dev->flags);
3353 }
3354 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003355 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003356 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003357 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003358 set_bit(R5_Insync, &dev->flags);
3359 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3360 test_bit(R5_Expanded, &dev->flags))
3361 /* If we've reshaped into here, we assume it is Insync.
3362 * We will shortly update recovery_offset to make
3363 * it official.
3364 */
3365 set_bit(R5_Insync, &dev->flags);
3366
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003367 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003368 /* This flag does not apply to '.replacement'
3369 * only to .rdev, so make sure to check that*/
3370 struct md_rdev *rdev2 = rcu_dereference(
3371 conf->disks[i].rdev);
3372 if (rdev2 == rdev)
3373 clear_bit(R5_Insync, &dev->flags);
3374 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003375 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003376 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003377 } else
3378 clear_bit(R5_WriteError, &dev->flags);
3379 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003380 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003381 /* This flag does not apply to '.replacement'
3382 * only to .rdev, so make sure to check that*/
3383 struct md_rdev *rdev2 = rcu_dereference(
3384 conf->disks[i].rdev);
3385 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003386 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003387 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003388 } else
3389 clear_bit(R5_MadeGood, &dev->flags);
3390 }
NeilBrown977df362011-12-23 10:17:53 +11003391 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3392 struct md_rdev *rdev2 = rcu_dereference(
3393 conf->disks[i].replacement);
3394 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3395 s->handle_bad_blocks = 1;
3396 atomic_inc(&rdev2->nr_pending);
3397 } else
3398 clear_bit(R5_MadeGoodRepl, &dev->flags);
3399 }
NeilBrown415e72d2010-06-17 17:25:21 +10003400 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003401 /* The ReadError flag will just be confusing now */
3402 clear_bit(R5_ReadError, &dev->flags);
3403 clear_bit(R5_ReWrite, &dev->flags);
3404 }
NeilBrown415e72d2010-06-17 17:25:21 +10003405 if (test_bit(R5_ReadError, &dev->flags))
3406 clear_bit(R5_Insync, &dev->flags);
3407 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003408 if (s->failed < 2)
3409 s->failed_num[s->failed] = i;
3410 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003411 if (rdev && !test_bit(Faulty, &rdev->flags))
3412 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003413 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003414 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003415 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3416 /* If there is a failed device being replaced,
3417 * we must be recovering.
3418 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003419 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003420 * else we can only be replacing
3421 * sync and recovery both need to read all devices, and so
3422 * use the same flag.
3423 */
3424 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003425 sh->sector >= conf->mddev->recovery_cp ||
3426 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003427 s->syncing = 1;
3428 else
3429 s->replacing = 1;
3430 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003431 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003432}
NeilBrownf4168852007-02-28 20:11:53 -08003433
NeilBrowncc940152011-07-26 11:35:35 +10003434static void handle_stripe(struct stripe_head *sh)
3435{
3436 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003437 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003438 int i;
NeilBrown84789552011-07-27 11:00:36 +10003439 int prexor;
3440 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003441 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003442
3443 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003444 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003445 /* already being handled, ensure it gets handled
3446 * again when current action finishes */
3447 set_bit(STRIPE_HANDLE, &sh->state);
3448 return;
3449 }
3450
NeilBrownf8dfcff2013-03-12 12:18:06 +11003451 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3452 spin_lock(&sh->stripe_lock);
3453 /* Cannot process 'sync' concurrently with 'discard' */
3454 if (!test_bit(STRIPE_DISCARD, &sh->state) &&
3455 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3456 set_bit(STRIPE_SYNCING, &sh->state);
3457 clear_bit(STRIPE_INSYNC, &sh->state);
3458 }
3459 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10003460 }
3461 clear_bit(STRIPE_DELAYED, &sh->state);
3462
3463 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3464 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3465 (unsigned long long)sh->sector, sh->state,
3466 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3467 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003468
NeilBrownacfe7262011-07-27 11:00:36 +10003469 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003470
NeilBrownbc2607f2011-07-28 11:39:22 +10003471 if (s.handle_bad_blocks) {
3472 set_bit(STRIPE_HANDLE, &sh->state);
3473 goto finish;
3474 }
3475
NeilBrown474af965fe2011-07-27 11:00:36 +10003476 if (unlikely(s.blocked_rdev)) {
3477 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003478 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003479 set_bit(STRIPE_HANDLE, &sh->state);
3480 goto finish;
3481 }
3482 /* There is nothing for the blocked_rdev to block */
3483 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3484 s.blocked_rdev = NULL;
3485 }
3486
3487 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3488 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3489 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3490 }
3491
3492 pr_debug("locked=%d uptodate=%d to_read=%d"
3493 " to_write=%d failed=%d failed_num=%d,%d\n",
3494 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3495 s.failed_num[0], s.failed_num[1]);
3496 /* check if the array has lost more than max_degraded devices and,
3497 * if so, some requests might need to be failed.
3498 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003499 if (s.failed > conf->max_degraded) {
3500 sh->check_state = 0;
3501 sh->reconstruct_state = 0;
3502 if (s.to_read+s.to_write+s.written)
3503 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003504 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003505 handle_failed_sync(conf, sh, &s);
3506 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003507
NeilBrown84789552011-07-27 11:00:36 +10003508 /* Now we check to see if any write operations have recently
3509 * completed
3510 */
3511 prexor = 0;
3512 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3513 prexor = 1;
3514 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3515 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3516 sh->reconstruct_state = reconstruct_state_idle;
3517
3518 /* All the 'written' buffers and the parity block are ready to
3519 * be written back to disk
3520 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003521 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3522 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003523 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003524 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3525 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003526 for (i = disks; i--; ) {
3527 struct r5dev *dev = &sh->dev[i];
3528 if (test_bit(R5_LOCKED, &dev->flags) &&
3529 (i == sh->pd_idx || i == sh->qd_idx ||
3530 dev->written)) {
3531 pr_debug("Writing block %d\n", i);
3532 set_bit(R5_Wantwrite, &dev->flags);
3533 if (prexor)
3534 continue;
3535 if (!test_bit(R5_Insync, &dev->flags) ||
3536 ((i == sh->pd_idx || i == sh->qd_idx) &&
3537 s.failed == 0))
3538 set_bit(STRIPE_INSYNC, &sh->state);
3539 }
3540 }
3541 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3542 s.dec_preread_active = 1;
3543 }
3544
NeilBrownef5b7c62012-11-22 09:13:36 +11003545 /*
3546 * might be able to return some write requests if the parity blocks
3547 * are safe, or on a failed drive
3548 */
3549 pdev = &sh->dev[sh->pd_idx];
3550 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3551 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3552 qdev = &sh->dev[sh->qd_idx];
3553 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3554 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3555 || conf->level < 6;
3556
3557 if (s.written &&
3558 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3559 && !test_bit(R5_LOCKED, &pdev->flags)
3560 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3561 test_bit(R5_Discard, &pdev->flags))))) &&
3562 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3563 && !test_bit(R5_LOCKED, &qdev->flags)
3564 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3565 test_bit(R5_Discard, &qdev->flags))))))
3566 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3567
3568 /* Now we might consider reading some blocks, either to check/generate
3569 * parity, or to satisfy requests
3570 * or to load a block that is being partially written.
3571 */
3572 if (s.to_read || s.non_overwrite
3573 || (conf->level == 6 && s.to_write && s.failed)
3574 || (s.syncing && (s.uptodate + s.compute < disks))
3575 || s.replacing
3576 || s.expanding)
3577 handle_stripe_fill(sh, &s, disks);
3578
NeilBrown84789552011-07-27 11:00:36 +10003579 /* Now to consider new write requests and what else, if anything
3580 * should be read. We do not handle new writes when:
3581 * 1/ A 'write' operation (copy+xor) is already in flight.
3582 * 2/ A 'check' operation is in flight, as it may clobber the parity
3583 * block.
3584 */
3585 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3586 handle_stripe_dirtying(conf, sh, &s, disks);
3587
3588 /* maybe we need to check and possibly fix the parity for this stripe
3589 * Any reads will already have been scheduled, so we just see if enough
3590 * data is available. The parity check is held off while parity
3591 * dependent operations are in flight.
3592 */
3593 if (sh->check_state ||
3594 (s.syncing && s.locked == 0 &&
3595 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3596 !test_bit(STRIPE_INSYNC, &sh->state))) {
3597 if (conf->level == 6)
3598 handle_parity_checks6(conf, sh, &s, disks);
3599 else
3600 handle_parity_checks5(conf, sh, &s, disks);
3601 }
NeilBrownc5a31002011-07-27 11:00:36 +10003602
NeilBrown9a3e1102011-12-23 10:17:53 +11003603 if (s.replacing && s.locked == 0
3604 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3605 /* Write out to replacement devices where possible */
3606 for (i = 0; i < conf->raid_disks; i++)
3607 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3608 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3609 set_bit(R5_WantReplace, &sh->dev[i].flags);
3610 set_bit(R5_LOCKED, &sh->dev[i].flags);
3611 s.locked++;
3612 }
3613 set_bit(STRIPE_INSYNC, &sh->state);
3614 }
3615 if ((s.syncing || s.replacing) && s.locked == 0 &&
3616 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003617 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3618 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003619 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3620 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10003621 }
3622
3623 /* If the failed drives are just a ReadError, then we might need
3624 * to progress the repair/check process
3625 */
3626 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3627 for (i = 0; i < s.failed; i++) {
3628 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3629 if (test_bit(R5_ReadError, &dev->flags)
3630 && !test_bit(R5_LOCKED, &dev->flags)
3631 && test_bit(R5_UPTODATE, &dev->flags)
3632 ) {
3633 if (!test_bit(R5_ReWrite, &dev->flags)) {
3634 set_bit(R5_Wantwrite, &dev->flags);
3635 set_bit(R5_ReWrite, &dev->flags);
3636 set_bit(R5_LOCKED, &dev->flags);
3637 s.locked++;
3638 } else {
3639 /* let's read it back */
3640 set_bit(R5_Wantread, &dev->flags);
3641 set_bit(R5_LOCKED, &dev->flags);
3642 s.locked++;
3643 }
3644 }
3645 }
3646
3647
NeilBrown3687c062011-07-27 11:00:36 +10003648 /* Finish reconstruct operations initiated by the expansion process */
3649 if (sh->reconstruct_state == reconstruct_state_result) {
3650 struct stripe_head *sh_src
3651 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3652 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3653 /* sh cannot be written until sh_src has been read.
3654 * so arrange for sh to be delayed a little
3655 */
3656 set_bit(STRIPE_DELAYED, &sh->state);
3657 set_bit(STRIPE_HANDLE, &sh->state);
3658 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3659 &sh_src->state))
3660 atomic_inc(&conf->preread_active_stripes);
3661 release_stripe(sh_src);
3662 goto finish;
3663 }
3664 if (sh_src)
3665 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003666
NeilBrown3687c062011-07-27 11:00:36 +10003667 sh->reconstruct_state = reconstruct_state_idle;
3668 clear_bit(STRIPE_EXPANDING, &sh->state);
3669 for (i = conf->raid_disks; i--; ) {
3670 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3671 set_bit(R5_LOCKED, &sh->dev[i].flags);
3672 s.locked++;
3673 }
3674 }
3675
3676 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3677 !sh->reconstruct_state) {
3678 /* Need to write out all blocks after computing parity */
3679 sh->disks = conf->raid_disks;
3680 stripe_set_idx(sh->sector, conf, 0, sh);
3681 schedule_reconstruction(sh, &s, 1, 1);
3682 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3683 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3684 atomic_dec(&conf->reshape_stripes);
3685 wake_up(&conf->wait_for_overlap);
3686 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3687 }
3688
3689 if (s.expanding && s.locked == 0 &&
3690 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3691 handle_stripe_expansion(conf, sh);
3692
3693finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003694 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003695 if (unlikely(s.blocked_rdev)) {
3696 if (conf->mddev->external)
3697 md_wait_for_blocked_rdev(s.blocked_rdev,
3698 conf->mddev);
3699 else
3700 /* Internal metadata will immediately
3701 * be written by raid5d, so we don't
3702 * need to wait here.
3703 */
3704 rdev_dec_pending(s.blocked_rdev,
3705 conf->mddev);
3706 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003707
NeilBrownbc2607f2011-07-28 11:39:22 +10003708 if (s.handle_bad_blocks)
3709 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003710 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003711 struct r5dev *dev = &sh->dev[i];
3712 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3713 /* We own a safe reference to the rdev */
3714 rdev = conf->disks[i].rdev;
3715 if (!rdev_set_badblocks(rdev, sh->sector,
3716 STRIPE_SECTORS, 0))
3717 md_error(conf->mddev, rdev);
3718 rdev_dec_pending(rdev, conf->mddev);
3719 }
NeilBrownb84db562011-07-28 11:39:23 +10003720 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3721 rdev = conf->disks[i].rdev;
3722 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003723 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003724 rdev_dec_pending(rdev, conf->mddev);
3725 }
NeilBrown977df362011-12-23 10:17:53 +11003726 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3727 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003728 if (!rdev)
3729 /* rdev have been moved down */
3730 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003731 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003732 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003733 rdev_dec_pending(rdev, conf->mddev);
3734 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003735 }
3736
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003737 if (s.ops_request)
3738 raid_run_ops(sh, s.ops_request);
3739
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003740 ops_run_io(sh, &s);
3741
NeilBrownc5709ef2011-07-26 11:35:20 +10003742 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003743 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003744 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003745 * have actually been submitted.
3746 */
3747 atomic_dec(&conf->preread_active_stripes);
3748 if (atomic_read(&conf->preread_active_stripes) <
3749 IO_THRESHOLD)
3750 md_wakeup_thread(conf->mddev->thread);
3751 }
3752
NeilBrownc5709ef2011-07-26 11:35:20 +10003753 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003754
Dan Williams257a4b42011-11-08 16:22:06 +11003755 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003756}
3757
NeilBrownd1688a62011-10-11 16:49:52 +11003758static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759{
3760 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3761 while (!list_empty(&conf->delayed_list)) {
3762 struct list_head *l = conf->delayed_list.next;
3763 struct stripe_head *sh;
3764 sh = list_entry(l, struct stripe_head, lru);
3765 list_del_init(l);
3766 clear_bit(STRIPE_DELAYED, &sh->state);
3767 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3768 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003769 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 }
NeilBrown482c0832011-04-18 18:25:42 +10003771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772}
3773
NeilBrownd1688a62011-10-11 16:49:52 +11003774static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003775{
3776 /* device_lock is held */
3777 struct list_head head;
3778 list_add(&head, &conf->bitmap_list);
3779 list_del_init(&conf->bitmap_list);
3780 while (!list_empty(&head)) {
3781 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3782 list_del_init(&sh->lru);
3783 atomic_inc(&sh->count);
3784 __release_stripe(conf, sh);
3785 }
3786}
3787
NeilBrownfd01b882011-10-11 16:47:53 +11003788int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003789{
NeilBrownd1688a62011-10-11 16:49:52 +11003790 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003791
3792 /* No difference between reads and writes. Just check
3793 * how busy the stripe_cache is
3794 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003795
NeilBrownf022b2f2006-10-03 01:15:56 -07003796 if (conf->inactive_blocked)
3797 return 1;
3798 if (conf->quiesce)
3799 return 1;
3800 if (list_empty_careful(&conf->inactive_list))
3801 return 1;
3802
3803 return 0;
3804}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003805EXPORT_SYMBOL_GPL(md_raid5_congested);
3806
3807static int raid5_congested(void *data, int bits)
3808{
NeilBrownfd01b882011-10-11 16:47:53 +11003809 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003810
3811 return mddev_congested(mddev, bits) ||
3812 md_raid5_congested(mddev, bits);
3813}
NeilBrownf022b2f2006-10-03 01:15:56 -07003814
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003815/* We want read requests to align with chunks where possible,
3816 * but write requests don't need to.
3817 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003818static int raid5_mergeable_bvec(struct request_queue *q,
3819 struct bvec_merge_data *bvm,
3820 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003821{
NeilBrownfd01b882011-10-11 16:47:53 +11003822 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003823 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003824 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003825 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003826 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003827
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003828 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003829 return biovec->bv_len; /* always allow writes to be mergeable */
3830
Andre Noll664e7c42009-06-18 08:45:27 +10003831 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3832 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003833 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3834 if (max < 0) max = 0;
3835 if (max <= biovec->bv_len && bio_sectors == 0)
3836 return biovec->bv_len;
3837 else
3838 return max;
3839}
3840
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003841
NeilBrownfd01b882011-10-11 16:47:53 +11003842static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003843{
3844 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003845 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003846 unsigned int bio_sectors = bio->bi_size >> 9;
3847
Andre Noll664e7c42009-06-18 08:45:27 +10003848 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3849 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003850 return chunk_sectors >=
3851 ((sector & (chunk_sectors - 1)) + bio_sectors);
3852}
3853
3854/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003855 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3856 * later sampled by raid5d.
3857 */
NeilBrownd1688a62011-10-11 16:49:52 +11003858static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003859{
3860 unsigned long flags;
3861
3862 spin_lock_irqsave(&conf->device_lock, flags);
3863
3864 bi->bi_next = conf->retry_read_aligned_list;
3865 conf->retry_read_aligned_list = bi;
3866
3867 spin_unlock_irqrestore(&conf->device_lock, flags);
3868 md_wakeup_thread(conf->mddev->thread);
3869}
3870
3871
NeilBrownd1688a62011-10-11 16:49:52 +11003872static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003873{
3874 struct bio *bi;
3875
3876 bi = conf->retry_read_aligned;
3877 if (bi) {
3878 conf->retry_read_aligned = NULL;
3879 return bi;
3880 }
3881 bi = conf->retry_read_aligned_list;
3882 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003883 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003884 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003885 /*
3886 * this sets the active strip count to 1 and the processed
3887 * strip count to zero (upper 8 bits)
3888 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003889 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003890 }
3891
3892 return bi;
3893}
3894
3895
3896/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003897 * The "raid5_align_endio" should check if the read succeeded and if it
3898 * did, call bio_endio on the original bio (having bio_put the new bio
3899 * first).
3900 * If the read failed..
3901 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003902static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003903{
3904 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003905 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003906 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003907 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003908 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003909
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003910 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003911
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003912 rdev = (void*)raid_bi->bi_next;
3913 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003914 mddev = rdev->mddev;
3915 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003916
3917 rdev_dec_pending(rdev, conf->mddev);
3918
3919 if (!error && uptodate) {
NeilBrowna9add5d2012-10-31 11:59:09 +11003920 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
3921 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02003922 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003923 if (atomic_dec_and_test(&conf->active_aligned_reads))
3924 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003925 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003926 }
3927
3928
Dan Williams45b42332007-07-09 11:56:43 -07003929 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003930
3931 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003932}
3933
Neil Brown387bb172007-02-08 14:20:29 -08003934static int bio_fits_rdev(struct bio *bi)
3935{
Jens Axboe165125e2007-07-24 09:28:11 +02003936 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003937
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003938 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003939 return 0;
3940 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003941 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003942 return 0;
3943
3944 if (q->merge_bvec_fn)
3945 /* it's too hard to apply the merge_bvec_fn at this stage,
3946 * just just give up
3947 */
3948 return 0;
3949
3950 return 1;
3951}
3952
3953
NeilBrownfd01b882011-10-11 16:47:53 +11003954static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003955{
NeilBrownd1688a62011-10-11 16:49:52 +11003956 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11003957 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003958 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003959 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003960 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003961
3962 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003963 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003964 return 0;
3965 }
3966 /*
NeilBrowna167f662010-10-26 18:31:13 +11003967 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003968 */
NeilBrowna167f662010-10-26 18:31:13 +11003969 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003970 if (!align_bi)
3971 return 0;
3972 /*
3973 * set bi_end_io to a new function, and set bi_private to the
3974 * original bio.
3975 */
3976 align_bi->bi_end_io = raid5_align_endio;
3977 align_bi->bi_private = raid_bio;
3978 /*
3979 * compute position
3980 */
NeilBrown112bf892009-03-31 14:39:38 +11003981 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3982 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003983 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003984
NeilBrown671488c2011-12-23 10:17:52 +11003985 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003986 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003987 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3988 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3989 rdev->recovery_offset < end_sector) {
3990 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3991 if (rdev &&
3992 (test_bit(Faulty, &rdev->flags) ||
3993 !(test_bit(In_sync, &rdev->flags) ||
3994 rdev->recovery_offset >= end_sector)))
3995 rdev = NULL;
3996 }
3997 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003998 sector_t first_bad;
3999 int bad_sectors;
4000
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004001 atomic_inc(&rdev->nr_pending);
4002 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004003 raid_bio->bi_next = (void*)rdev;
4004 align_bi->bi_bdev = rdev->bdev;
4005 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004006
NeilBrown31c176e2011-07-28 11:39:22 +10004007 if (!bio_fits_rdev(align_bi) ||
4008 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
4009 &first_bad, &bad_sectors)) {
4010 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004011 bio_put(align_bi);
4012 rdev_dec_pending(rdev, mddev);
4013 return 0;
4014 }
4015
majianpeng6c0544e2012-06-12 08:31:10 +08004016 /* No reshape active, so we can trust rdev->data_offset */
4017 align_bi->bi_sector += rdev->data_offset;
4018
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004019 spin_lock_irq(&conf->device_lock);
4020 wait_event_lock_irq(conf->wait_for_stripe,
4021 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004022 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004023 atomic_inc(&conf->active_aligned_reads);
4024 spin_unlock_irq(&conf->device_lock);
4025
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004026 if (mddev->gendisk)
4027 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4028 align_bi, disk_devt(mddev->gendisk),
4029 raid_bio->bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004030 generic_make_request(align_bi);
4031 return 1;
4032 } else {
4033 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004034 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004035 return 0;
4036 }
4037}
4038
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004039/* __get_priority_stripe - get the next stripe to process
4040 *
4041 * Full stripe writes are allowed to pass preread active stripes up until
4042 * the bypass_threshold is exceeded. In general the bypass_count
4043 * increments when the handle_list is handled before the hold_list; however, it
4044 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4045 * stripe with in flight i/o. The bypass_count will be reset when the
4046 * head of the hold_list has changed, i.e. the head was promoted to the
4047 * handle_list.
4048 */
NeilBrownd1688a62011-10-11 16:49:52 +11004049static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004050{
4051 struct stripe_head *sh;
4052
4053 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4054 __func__,
4055 list_empty(&conf->handle_list) ? "empty" : "busy",
4056 list_empty(&conf->hold_list) ? "empty" : "busy",
4057 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4058
4059 if (!list_empty(&conf->handle_list)) {
4060 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4061
4062 if (list_empty(&conf->hold_list))
4063 conf->bypass_count = 0;
4064 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4065 if (conf->hold_list.next == conf->last_hold)
4066 conf->bypass_count++;
4067 else {
4068 conf->last_hold = conf->hold_list.next;
4069 conf->bypass_count -= conf->bypass_threshold;
4070 if (conf->bypass_count < 0)
4071 conf->bypass_count = 0;
4072 }
4073 }
4074 } else if (!list_empty(&conf->hold_list) &&
4075 ((conf->bypass_threshold &&
4076 conf->bypass_count > conf->bypass_threshold) ||
4077 atomic_read(&conf->pending_full_writes) == 0)) {
4078 sh = list_entry(conf->hold_list.next,
4079 typeof(*sh), lru);
4080 conf->bypass_count -= conf->bypass_threshold;
4081 if (conf->bypass_count < 0)
4082 conf->bypass_count = 0;
4083 } else
4084 return NULL;
4085
4086 list_del_init(&sh->lru);
4087 atomic_inc(&sh->count);
4088 BUG_ON(atomic_read(&sh->count) != 1);
4089 return sh;
4090}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004091
Shaohua Li8811b592012-08-02 08:33:00 +10004092struct raid5_plug_cb {
4093 struct blk_plug_cb cb;
4094 struct list_head list;
4095};
4096
4097static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4098{
4099 struct raid5_plug_cb *cb = container_of(
4100 blk_cb, struct raid5_plug_cb, cb);
4101 struct stripe_head *sh;
4102 struct mddev *mddev = cb->cb.data;
4103 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004104 int cnt = 0;
Shaohua Li8811b592012-08-02 08:33:00 +10004105
4106 if (cb->list.next && !list_empty(&cb->list)) {
4107 spin_lock_irq(&conf->device_lock);
4108 while (!list_empty(&cb->list)) {
4109 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4110 list_del_init(&sh->lru);
4111 /*
4112 * avoid race release_stripe_plug() sees
4113 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4114 * is still in our list
4115 */
4116 smp_mb__before_clear_bit();
4117 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4118 __release_stripe(conf, sh);
NeilBrowna9add5d2012-10-31 11:59:09 +11004119 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004120 }
4121 spin_unlock_irq(&conf->device_lock);
4122 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004123 if (mddev->queue)
4124 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);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004187 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
4188 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4189 release_stripe(sh);
4190 schedule();
4191 goto again;
4192 }
4193 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11004194 spin_lock_irq(&sh->stripe_lock);
4195 for (d = 0; d < conf->raid_disks; d++) {
4196 if (d == sh->pd_idx || d == sh->qd_idx)
4197 continue;
4198 if (sh->dev[d].towrite || sh->dev[d].toread) {
4199 set_bit(R5_Overlap, &sh->dev[d].flags);
4200 spin_unlock_irq(&sh->stripe_lock);
4201 release_stripe(sh);
4202 schedule();
4203 goto again;
4204 }
4205 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11004206 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11004207 finish_wait(&conf->wait_for_overlap, &w);
4208 for (d = 0; d < conf->raid_disks; d++) {
4209 if (d == sh->pd_idx || d == sh->qd_idx)
4210 continue;
4211 sh->dev[d].towrite = bi;
4212 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4213 raid5_inc_bi_active_stripes(bi);
4214 }
4215 spin_unlock_irq(&sh->stripe_lock);
4216 if (conf->mddev->bitmap) {
4217 for (d = 0;
4218 d < conf->raid_disks - conf->max_degraded;
4219 d++)
4220 bitmap_startwrite(mddev->bitmap,
4221 sh->sector,
4222 STRIPE_SECTORS,
4223 0);
4224 sh->bm_seq = conf->seq_flush + 1;
4225 set_bit(STRIPE_BIT_DELAY, &sh->state);
4226 }
4227
4228 set_bit(STRIPE_HANDLE, &sh->state);
4229 clear_bit(STRIPE_DELAYED, &sh->state);
4230 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4231 atomic_inc(&conf->preread_active_stripes);
4232 release_stripe_plug(mddev, sh);
4233 }
4234
4235 remaining = raid5_dec_bi_active_stripes(bi);
4236 if (remaining == 0) {
4237 md_write_end(mddev);
4238 bio_endio(bi, 0);
4239 }
4240}
4241
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004242static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243{
NeilBrownd1688a62011-10-11 16:49:52 +11004244 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004245 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 sector_t new_sector;
4247 sector_t logical_sector, last_sector;
4248 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004249 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004250 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
Tejun Heoe9c74692010-09-03 11:56:18 +02004252 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4253 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004254 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004255 }
4256
NeilBrown3d310eb2005-06-21 17:17:26 -07004257 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004258
NeilBrown802ba062006-12-13 00:34:13 -08004259 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004260 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004261 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004262 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004263
Shaohua Li620125f2012-10-11 13:49:05 +11004264 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4265 make_discard_request(mddev, bi);
4266 return;
4267 }
4268
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4270 last_sector = bi->bi_sector + (bi->bi_size>>9);
4271 bi->bi_next = NULL;
4272 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004273
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4275 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004276 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004277
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004278 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004279 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004280 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004281 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004282 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004283 * 64bit on a 32bit platform, and so it might be
4284 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004285 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004286 * the lock is dropped, so once we get a reference
4287 * to the stripe that we think it is, we will have
4288 * to check again.
4289 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004290 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004291 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004292 ? logical_sector < conf->reshape_progress
4293 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004294 previous = 1;
4295 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004296 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004297 ? logical_sector < conf->reshape_safe
4298 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004299 spin_unlock_irq(&conf->device_lock);
4300 schedule();
4301 goto retry;
4302 }
4303 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004304 spin_unlock_irq(&conf->device_lock);
4305 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004306
NeilBrown112bf892009-03-31 14:39:38 +11004307 new_sector = raid5_compute_sector(conf, logical_sector,
4308 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004309 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004310 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 (unsigned long long)new_sector,
4312 (unsigned long long)logical_sector);
4313
NeilBrownb5663ba2009-03-31 14:39:38 +11004314 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004315 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004317 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004318 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004319 * stripe, so we must do the range check again.
4320 * Expansion could still move past after this
4321 * test, but as we are holding a reference to
4322 * 'sh', we know that if that happens,
4323 * STRIPE_EXPANDING will get set and the expansion
4324 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004325 */
4326 int must_retry = 0;
4327 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004328 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004329 ? logical_sector >= conf->reshape_progress
4330 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004331 /* mismatch, need to try again */
4332 must_retry = 1;
4333 spin_unlock_irq(&conf->device_lock);
4334 if (must_retry) {
4335 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004336 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004337 goto retry;
4338 }
4339 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004340
Namhyung Kimffd96e32011-07-18 17:38:51 +10004341 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004342 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004343 logical_sector < mddev->suspend_hi) {
4344 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004345 /* As the suspend_* range is controlled by
4346 * userspace, we want an interruptible
4347 * wait.
4348 */
4349 flush_signals(current);
4350 prepare_to_wait(&conf->wait_for_overlap,
4351 &w, TASK_INTERRUPTIBLE);
4352 if (logical_sector >= mddev->suspend_lo &&
4353 logical_sector < mddev->suspend_hi)
4354 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004355 goto retry;
4356 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004357
4358 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004359 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004360 /* Stripe is busy expanding or
4361 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 * and wait a while
4363 */
NeilBrown482c0832011-04-18 18:25:42 +10004364 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 release_stripe(sh);
4366 schedule();
4367 goto retry;
4368 }
4369 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004370 set_bit(STRIPE_HANDLE, &sh->state);
4371 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004372 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004373 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4374 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004375 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 } else {
4377 /* cannot get stripe for read-ahead, just give-up */
4378 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4379 finish_wait(&conf->wait_for_overlap, &w);
4380 break;
4381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004383
Shaohua Lie7836bd62012-07-19 16:01:31 +10004384 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004385 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
NeilBrown16a53ec2006-06-26 00:27:38 -07004387 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004389
NeilBrowna9add5d2012-10-31 11:59:09 +11004390 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4391 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004392 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394}
4395
NeilBrownfd01b882011-10-11 16:47:53 +11004396static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004397
NeilBrownfd01b882011-10-11 16:47:53 +11004398static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399{
NeilBrown52c03292006-06-26 00:27:43 -07004400 /* reshaping is quite different to recovery/resync so it is
4401 * handled quite separately ... here.
4402 *
4403 * On each call to sync_request, we gather one chunk worth of
4404 * destination stripes and flag them as expanding.
4405 * Then we find all the source stripes and request reads.
4406 * As the reads complete, handle_stripe will copy the data
4407 * into the destination stripe and release that stripe.
4408 */
NeilBrownd1688a62011-10-11 16:49:52 +11004409 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004411 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004412 int raid_disks = conf->previous_raid_disks;
4413 int data_disks = raid_disks - conf->max_degraded;
4414 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004415 int i;
4416 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004417 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004418 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004419 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004420 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004421
NeilBrownfef9c612009-03-31 15:16:46 +11004422 if (sector_nr == 0) {
4423 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004424 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004425 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4426 sector_nr = raid5_size(mddev, 0, 0)
4427 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004428 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004429 conf->reshape_progress > 0)
4430 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004431 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004432 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004433 mddev->curr_resync_completed = sector_nr;
4434 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004435 *skipped = 1;
4436 return sector_nr;
4437 }
NeilBrown52c03292006-06-26 00:27:43 -07004438 }
4439
NeilBrown7a661382009-03-31 15:21:40 +11004440 /* We need to process a full chunk at a time.
4441 * If old and new chunk sizes differ, we need to process the
4442 * largest of these
4443 */
Andre Noll664e7c42009-06-18 08:45:27 +10004444 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4445 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004446 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004447 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004448
NeilBrownb5254dd2012-05-21 09:27:01 +10004449 /* We update the metadata at least every 10 seconds, or when
4450 * the data about to be copied would over-write the source of
4451 * the data at the front of the range. i.e. one new_stripe
4452 * along from reshape_progress new_maps to after where
4453 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004454 */
NeilBrownfef9c612009-03-31 15:16:46 +11004455 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004456 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004457 readpos = conf->reshape_progress;
4458 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004459 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004460 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004461 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004462 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004463 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004464 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004465 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004466 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004467 readpos -= min_t(sector_t, reshape_sectors, readpos);
4468 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004469 }
NeilBrown52c03292006-06-26 00:27:43 -07004470
NeilBrownb5254dd2012-05-21 09:27:01 +10004471 /* Having calculated the 'writepos' possibly use it
4472 * to set 'stripe_addr' which is where we will write to.
4473 */
4474 if (mddev->reshape_backwards) {
4475 BUG_ON(conf->reshape_progress == 0);
4476 stripe_addr = writepos;
4477 BUG_ON((mddev->dev_sectors &
4478 ~((sector_t)reshape_sectors - 1))
4479 - reshape_sectors - stripe_addr
4480 != sector_nr);
4481 } else {
4482 BUG_ON(writepos != sector_nr + reshape_sectors);
4483 stripe_addr = sector_nr;
4484 }
4485
NeilBrownc8f517c2009-03-31 15:28:40 +11004486 /* 'writepos' is the most advanced device address we might write.
4487 * 'readpos' is the least advanced device address we might read.
4488 * 'safepos' is the least address recorded in the metadata as having
4489 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004490 * If there is a min_offset_diff, these are adjusted either by
4491 * increasing the safepos/readpos if diff is negative, or
4492 * increasing writepos if diff is positive.
4493 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004494 * ensure safety in the face of a crash - that must be done by userspace
4495 * making a backup of the data. So in that case there is no particular
4496 * rush to update metadata.
4497 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4498 * update the metadata to advance 'safepos' to match 'readpos' so that
4499 * we can be safe in the event of a crash.
4500 * So we insist on updating metadata if safepos is behind writepos and
4501 * readpos is beyond writepos.
4502 * In any case, update the metadata every 10 seconds.
4503 * Maybe that number should be configurable, but I'm not sure it is
4504 * worth it.... maybe it could be a multiple of safemode_delay???
4505 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004506 if (conf->min_offset_diff < 0) {
4507 safepos += -conf->min_offset_diff;
4508 readpos += -conf->min_offset_diff;
4509 } else
4510 writepos += conf->min_offset_diff;
4511
NeilBrown2c810cd2012-05-21 09:27:00 +10004512 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004513 ? (safepos > writepos && readpos < writepos)
4514 : (safepos < writepos && readpos > writepos)) ||
4515 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004516 /* Cannot proceed until we've updated the superblock... */
4517 wait_event(conf->wait_for_overlap,
4518 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004519 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004520 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004521 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004522 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004523 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004524 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004525 kthread_should_stop());
4526 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004527 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004528 spin_unlock_irq(&conf->device_lock);
4529 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004530 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004531 }
4532
NeilBrownab69ae12009-03-31 15:26:47 +11004533 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004534 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004535 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004536 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004537 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004538 set_bit(STRIPE_EXPANDING, &sh->state);
4539 atomic_inc(&conf->reshape_stripes);
4540 /* If any of this stripe is beyond the end of the old
4541 * array, then we need to zero those blocks
4542 */
4543 for (j=sh->disks; j--;) {
4544 sector_t s;
4545 if (j == sh->pd_idx)
4546 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004547 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004548 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004549 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004550 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004551 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004552 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004553 continue;
4554 }
4555 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4556 set_bit(R5_Expanded, &sh->dev[j].flags);
4557 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4558 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004559 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004560 set_bit(STRIPE_EXPAND_READY, &sh->state);
4561 set_bit(STRIPE_HANDLE, &sh->state);
4562 }
NeilBrownab69ae12009-03-31 15:26:47 +11004563 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004564 }
4565 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004566 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004567 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004568 else
NeilBrown7a661382009-03-31 15:21:40 +11004569 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004570 spin_unlock_irq(&conf->device_lock);
4571 /* Ok, those stripe are ready. We can start scheduling
4572 * reads on the source stripes.
4573 * The source stripes are determined by mapping the first and last
4574 * block on the destination stripes.
4575 */
NeilBrown52c03292006-06-26 00:27:43 -07004576 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004577 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004578 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004579 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004580 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004581 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004582 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004583 if (last_sector >= mddev->dev_sectors)
4584 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004585 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004586 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004587 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4588 set_bit(STRIPE_HANDLE, &sh->state);
4589 release_stripe(sh);
4590 first_sector += STRIPE_SECTORS;
4591 }
NeilBrownab69ae12009-03-31 15:26:47 +11004592 /* Now that the sources are clearly marked, we can release
4593 * the destination stripes
4594 */
4595 while (!list_empty(&stripes)) {
4596 sh = list_entry(stripes.next, struct stripe_head, lru);
4597 list_del_init(&sh->lru);
4598 release_stripe(sh);
4599 }
NeilBrownc6207272008-02-06 01:39:52 -08004600 /* If this takes us to the resync_max point where we have to pause,
4601 * then we need to write out the superblock.
4602 */
NeilBrown7a661382009-03-31 15:21:40 +11004603 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004604 if ((sector_nr - mddev->curr_resync_completed) * 2
4605 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004606 /* Cannot proceed until we've updated the superblock... */
4607 wait_event(conf->wait_for_overlap,
4608 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004609 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004610 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004611 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004612 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4613 md_wakeup_thread(mddev->thread);
4614 wait_event(mddev->sb_wait,
4615 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4616 || kthread_should_stop());
4617 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004618 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004619 spin_unlock_irq(&conf->device_lock);
4620 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004621 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004622 }
NeilBrown7a661382009-03-31 15:21:40 +11004623 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004624}
4625
4626/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004627static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004628{
NeilBrownd1688a62011-10-11 16:49:52 +11004629 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004630 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004631 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004632 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004633 int still_degraded = 0;
4634 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635
NeilBrown72626682005-09-09 16:23:54 -07004636 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004638
NeilBrown29269552006-03-27 01:18:10 -08004639 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4640 end_reshape(conf);
4641 return 0;
4642 }
NeilBrown72626682005-09-09 16:23:54 -07004643
4644 if (mddev->curr_resync < max_sector) /* aborted */
4645 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4646 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004647 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004648 conf->fullsync = 0;
4649 bitmap_close_sync(mddev->bitmap);
4650
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 return 0;
4652 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004653
NeilBrown64bd6602009-08-03 10:59:58 +10004654 /* Allow raid5_quiesce to complete */
4655 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4656
NeilBrown52c03292006-06-26 00:27:43 -07004657 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4658 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004659
NeilBrownc6207272008-02-06 01:39:52 -08004660 /* No need to check resync_max as we never do more than one
4661 * stripe, and as resync_max will always be on a chunk boundary,
4662 * if the check in md_do_sync didn't fire, there is no chance
4663 * of overstepping resync_max here
4664 */
4665
NeilBrown16a53ec2006-06-26 00:27:38 -07004666 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 * to resync, then assert that we are finished, because there is
4668 * nothing we can do.
4669 */
NeilBrown3285edf2006-06-26 00:27:55 -07004670 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004671 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004672 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004673 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 return rv;
4675 }
NeilBrown72626682005-09-09 16:23:54 -07004676 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004677 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004678 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4679 /* we can skip this block, and probably more */
4680 sync_blocks /= STRIPE_SECTORS;
4681 *skipped = 1;
4682 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684
NeilBrownb47490c2008-02-06 01:39:50 -08004685 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4686
NeilBrowna8c906c2009-06-09 14:39:59 +10004687 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004689 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004691 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004693 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004695 /* Need to check if array will still be degraded after recovery/resync
4696 * We don't need to check the 'failed' flag as when that gets set,
4697 * recovery aborts.
4698 */
NeilBrownf001a702009-06-09 14:30:31 +10004699 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004700 if (conf->disks[i].rdev == NULL)
4701 still_degraded = 1;
4702
4703 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4704
NeilBrown83206d62011-07-26 11:19:49 +10004705 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
NeilBrown14425772009-10-16 15:55:25 +11004707 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 release_stripe(sh);
4709
4710 return STRIPE_SECTORS;
4711}
4712
NeilBrownd1688a62011-10-11 16:49:52 +11004713static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004714{
4715 /* We may not be able to submit a whole bio at once as there
4716 * may not be enough stripe_heads available.
4717 * We cannot pre-allocate enough stripe_heads as we may need
4718 * more than exist in the cache (if we allow ever large chunks).
4719 * So we do one stripe head at a time and record in
4720 * ->bi_hw_segments how many have been done.
4721 *
4722 * We *know* that this entire raid_bio is in one chunk, so
4723 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4724 */
4725 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004726 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004727 sector_t sector, logical_sector, last_sector;
4728 int scnt = 0;
4729 int remaining;
4730 int handled = 0;
4731
4732 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004733 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004734 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004735 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4736
4737 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004738 logical_sector += STRIPE_SECTORS,
4739 sector += STRIPE_SECTORS,
4740 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004741
Shaohua Lie7836bd62012-07-19 16:01:31 +10004742 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004743 /* already done this stripe */
4744 continue;
4745
NeilBrowna8c906c2009-06-09 14:39:59 +10004746 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004747
4748 if (!sh) {
4749 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004750 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004751 conf->retry_read_aligned = raid_bio;
4752 return handled;
4753 }
4754
Neil Brown387bb172007-02-08 14:20:29 -08004755 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4756 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004757 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004758 conf->retry_read_aligned = raid_bio;
4759 return handled;
4760 }
4761
majianpeng3f9e7c12012-07-31 10:04:21 +10004762 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004763 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004764 release_stripe(sh);
4765 handled++;
4766 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004767 remaining = raid5_dec_bi_active_stripes(raid_bio);
NeilBrowna9add5d2012-10-31 11:59:09 +11004768 if (remaining == 0) {
4769 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
4770 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004771 bio_endio(raid_bio, 0);
NeilBrowna9add5d2012-10-31 11:59:09 +11004772 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004773 if (atomic_dec_and_test(&conf->active_aligned_reads))
4774 wake_up(&conf->wait_for_stripe);
4775 return handled;
4776}
4777
Shaohua Li46a06402012-08-02 08:33:15 +10004778#define MAX_STRIPE_BATCH 8
4779static int handle_active_stripes(struct r5conf *conf)
4780{
4781 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4782 int i, batch_size = 0;
4783
4784 while (batch_size < MAX_STRIPE_BATCH &&
4785 (sh = __get_priority_stripe(conf)) != NULL)
4786 batch[batch_size++] = sh;
4787
4788 if (batch_size == 0)
4789 return batch_size;
4790 spin_unlock_irq(&conf->device_lock);
4791
4792 for (i = 0; i < batch_size; i++)
4793 handle_stripe(batch[i]);
4794
4795 cond_resched();
4796
4797 spin_lock_irq(&conf->device_lock);
4798 for (i = 0; i < batch_size; i++)
4799 __release_stripe(conf, batch[i]);
4800 return batch_size;
4801}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004802
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803/*
4804 * This is our raid5 kernel thread.
4805 *
4806 * We scan the hash table for stripes which can be handled now.
4807 * During the scan, completed stripes are saved for us by the interrupt
4808 * handler, so that they will not have to wait for our next wakeup.
4809 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004810static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811{
Shaohua Li4ed87312012-10-11 13:34:00 +11004812 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004813 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004815 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
Dan Williams45b42332007-07-09 11:56:43 -07004817 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
4819 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004821 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 handled = 0;
4823 spin_lock_irq(&conf->device_lock);
4824 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004825 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004826 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
NeilBrown0021b7b2012-07-31 09:08:14 +02004828 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004829 !list_empty(&conf->bitmap_list)) {
4830 /* Now is a good time to flush some bitmap updates */
4831 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004832 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004833 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004834 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004835 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004836 activate_bit_delay(conf);
4837 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004838 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004839
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004840 while ((bio = remove_bio_from_retry(conf))) {
4841 int ok;
4842 spin_unlock_irq(&conf->device_lock);
4843 ok = retry_aligned_read(conf, bio);
4844 spin_lock_irq(&conf->device_lock);
4845 if (!ok)
4846 break;
4847 handled++;
4848 }
4849
Shaohua Li46a06402012-08-02 08:33:15 +10004850 batch_size = handle_active_stripes(conf);
4851 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004853 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
Shaohua Li46a06402012-08-02 08:33:15 +10004855 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4856 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004857 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004858 spin_lock_irq(&conf->device_lock);
4859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 }
Dan Williams45b42332007-07-09 11:56:43 -07004861 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
4863 spin_unlock_irq(&conf->device_lock);
4864
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004865 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004866 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
Dan Williams45b42332007-07-09 11:56:43 -07004868 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869}
4870
NeilBrown3f294f42005-11-08 21:39:25 -08004871static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004872raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004873{
NeilBrownd1688a62011-10-11 16:49:52 +11004874 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004875 if (conf)
4876 return sprintf(page, "%d\n", conf->max_nr_stripes);
4877 else
4878 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004879}
4880
NeilBrownc41d4ac2010-06-01 19:37:24 +10004881int
NeilBrownfd01b882011-10-11 16:47:53 +11004882raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004883{
NeilBrownd1688a62011-10-11 16:49:52 +11004884 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004885 int err;
4886
4887 if (size <= 16 || size > 32768)
4888 return -EINVAL;
4889 while (size < conf->max_nr_stripes) {
4890 if (drop_one_stripe(conf))
4891 conf->max_nr_stripes--;
4892 else
4893 break;
4894 }
4895 err = md_allow_write(mddev);
4896 if (err)
4897 return err;
4898 while (size > conf->max_nr_stripes) {
4899 if (grow_one_stripe(conf))
4900 conf->max_nr_stripes++;
4901 else break;
4902 }
4903 return 0;
4904}
4905EXPORT_SYMBOL(raid5_set_cache_size);
4906
NeilBrown3f294f42005-11-08 21:39:25 -08004907static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004908raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004909{
NeilBrownd1688a62011-10-11 16:49:52 +11004910 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004911 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004912 int err;
4913
NeilBrown3f294f42005-11-08 21:39:25 -08004914 if (len >= PAGE_SIZE)
4915 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004916 if (!conf)
4917 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004918
Dan Williams4ef197d82008-04-28 02:15:54 -07004919 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004920 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004921 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004922 if (err)
4923 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004924 return len;
4925}
NeilBrown007583c2005-11-08 21:39:30 -08004926
NeilBrown96de1e62005-11-08 21:39:39 -08004927static struct md_sysfs_entry
4928raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4929 raid5_show_stripe_cache_size,
4930 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004931
4932static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004933raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004934{
NeilBrownd1688a62011-10-11 16:49:52 +11004935 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004936 if (conf)
4937 return sprintf(page, "%d\n", conf->bypass_threshold);
4938 else
4939 return 0;
4940}
4941
4942static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004943raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004944{
NeilBrownd1688a62011-10-11 16:49:52 +11004945 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004946 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004947 if (len >= PAGE_SIZE)
4948 return -EINVAL;
4949 if (!conf)
4950 return -ENODEV;
4951
Dan Williams4ef197d82008-04-28 02:15:54 -07004952 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004953 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004954 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004955 return -EINVAL;
4956 conf->bypass_threshold = new;
4957 return len;
4958}
4959
4960static struct md_sysfs_entry
4961raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4962 S_IRUGO | S_IWUSR,
4963 raid5_show_preread_threshold,
4964 raid5_store_preread_threshold);
4965
4966static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004967stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004968{
NeilBrownd1688a62011-10-11 16:49:52 +11004969 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004970 if (conf)
4971 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4972 else
4973 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004974}
4975
NeilBrown96de1e62005-11-08 21:39:39 -08004976static struct md_sysfs_entry
4977raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004978
NeilBrown007583c2005-11-08 21:39:30 -08004979static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004980 &raid5_stripecache_size.attr,
4981 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004982 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004983 NULL,
4984};
NeilBrown007583c2005-11-08 21:39:30 -08004985static struct attribute_group raid5_attrs_group = {
4986 .name = NULL,
4987 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004988};
4989
Dan Williams80c3a6c2009-03-17 18:10:40 -07004990static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004991raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004992{
NeilBrownd1688a62011-10-11 16:49:52 +11004993 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004994
4995 if (!sectors)
4996 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004997 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004998 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004999 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005000
Andre Noll9d8f0362009-06-18 08:45:01 +10005001 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10005002 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07005003 return sectors * (raid_disks - conf->max_degraded);
5004}
5005
NeilBrownd1688a62011-10-11 16:49:52 +11005006static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005007{
5008 struct raid5_percpu *percpu;
5009 unsigned long cpu;
5010
5011 if (!conf->percpu)
5012 return;
5013
5014 get_online_cpus();
5015 for_each_possible_cpu(cpu) {
5016 percpu = per_cpu_ptr(conf->percpu, cpu);
5017 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005018 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005019 }
5020#ifdef CONFIG_HOTPLUG_CPU
5021 unregister_cpu_notifier(&conf->cpu_notify);
5022#endif
5023 put_online_cpus();
5024
5025 free_percpu(conf->percpu);
5026}
5027
NeilBrownd1688a62011-10-11 16:49:52 +11005028static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10005029{
5030 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07005031 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005032 kfree(conf->disks);
5033 kfree(conf->stripe_hashtbl);
5034 kfree(conf);
5035}
5036
Dan Williams36d1c642009-07-14 11:48:22 -07005037#ifdef CONFIG_HOTPLUG_CPU
5038static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5039 void *hcpu)
5040{
NeilBrownd1688a62011-10-11 16:49:52 +11005041 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005042 long cpu = (long)hcpu;
5043 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5044
5045 switch (action) {
5046 case CPU_UP_PREPARE:
5047 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07005048 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07005049 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005050 if (!percpu->scribble)
5051 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5052
5053 if (!percpu->scribble ||
5054 (conf->level == 6 && !percpu->spare_page)) {
5055 safe_put_page(percpu->spare_page);
5056 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005057 pr_err("%s: failed memory allocation for cpu%ld\n",
5058 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005059 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005060 }
5061 break;
5062 case CPU_DEAD:
5063 case CPU_DEAD_FROZEN:
5064 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005065 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005066 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005067 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005068 break;
5069 default:
5070 break;
5071 }
5072 return NOTIFY_OK;
5073}
5074#endif
5075
NeilBrownd1688a62011-10-11 16:49:52 +11005076static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005077{
5078 unsigned long cpu;
5079 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005080 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005081 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005082 int err;
5083
Dan Williams36d1c642009-07-14 11:48:22 -07005084 allcpus = alloc_percpu(struct raid5_percpu);
5085 if (!allcpus)
5086 return -ENOMEM;
5087 conf->percpu = allcpus;
5088
5089 get_online_cpus();
5090 err = 0;
5091 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005092 if (conf->level == 6) {
5093 spare_page = alloc_page(GFP_KERNEL);
5094 if (!spare_page) {
5095 err = -ENOMEM;
5096 break;
5097 }
5098 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5099 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005100 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005101 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005102 err = -ENOMEM;
5103 break;
5104 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005105 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005106 }
5107#ifdef CONFIG_HOTPLUG_CPU
5108 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5109 conf->cpu_notify.priority = 0;
5110 if (err == 0)
5111 err = register_cpu_notifier(&conf->cpu_notify);
5112#endif
5113 put_online_cpus();
5114
5115 return err;
5116}
5117
NeilBrownd1688a62011-10-11 16:49:52 +11005118static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119{
NeilBrownd1688a62011-10-11 16:49:52 +11005120 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005121 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005122 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005124 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
NeilBrown91adb562009-03-31 14:39:39 +11005126 if (mddev->new_level != 5
5127 && mddev->new_level != 4
5128 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005129 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005130 mdname(mddev), mddev->new_level);
5131 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 }
NeilBrown91adb562009-03-31 14:39:39 +11005133 if ((mddev->new_level == 5
5134 && !algorithm_valid_raid5(mddev->new_layout)) ||
5135 (mddev->new_level == 6
5136 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005137 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005138 mdname(mddev), mddev->new_layout);
5139 return ERR_PTR(-EIO);
5140 }
5141 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005142 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005143 mdname(mddev), mddev->raid_disks);
5144 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146
Andre Noll664e7c42009-06-18 08:45:27 +10005147 if (!mddev->new_chunk_sectors ||
5148 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5149 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005150 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5151 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005152 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005153 }
5154
NeilBrownd1688a62011-10-11 16:49:52 +11005155 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005156 if (conf == NULL)
5157 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005158 spin_lock_init(&conf->device_lock);
5159 init_waitqueue_head(&conf->wait_for_stripe);
5160 init_waitqueue_head(&conf->wait_for_overlap);
5161 INIT_LIST_HEAD(&conf->handle_list);
5162 INIT_LIST_HEAD(&conf->hold_list);
5163 INIT_LIST_HEAD(&conf->delayed_list);
5164 INIT_LIST_HEAD(&conf->bitmap_list);
5165 INIT_LIST_HEAD(&conf->inactive_list);
5166 atomic_set(&conf->active_stripes, 0);
5167 atomic_set(&conf->preread_active_stripes, 0);
5168 atomic_set(&conf->active_aligned_reads, 0);
5169 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005170 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005171
5172 conf->raid_disks = mddev->raid_disks;
5173 if (mddev->reshape_position == MaxSector)
5174 conf->previous_raid_disks = mddev->raid_disks;
5175 else
5176 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005177 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5178 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005179
NeilBrown5e5e3e72009-10-16 16:35:30 +11005180 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005181 GFP_KERNEL);
5182 if (!conf->disks)
5183 goto abort;
5184
5185 conf->mddev = mddev;
5186
5187 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5188 goto abort;
5189
Dan Williams36d1c642009-07-14 11:48:22 -07005190 conf->level = mddev->new_level;
5191 if (raid5_alloc_percpu(conf) != 0)
5192 goto abort;
5193
NeilBrown0c55e022010-05-03 14:09:02 +10005194 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005195
NeilBrowndafb20f2012-03-19 12:46:39 +11005196 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005197 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005198 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005199 || raid_disk < 0)
5200 continue;
5201 disk = conf->disks + raid_disk;
5202
NeilBrown17045f52011-12-23 10:17:53 +11005203 if (test_bit(Replacement, &rdev->flags)) {
5204 if (disk->replacement)
5205 goto abort;
5206 disk->replacement = rdev;
5207 } else {
5208 if (disk->rdev)
5209 goto abort;
5210 disk->rdev = rdev;
5211 }
NeilBrown91adb562009-03-31 14:39:39 +11005212
5213 if (test_bit(In_sync, &rdev->flags)) {
5214 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005215 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5216 " disk %d\n",
5217 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005218 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005219 /* Cannot rely on bitmap to complete recovery */
5220 conf->fullsync = 1;
5221 }
5222
Andre Noll09c9e5f2009-06-18 08:45:55 +10005223 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005224 conf->level = mddev->new_level;
5225 if (conf->level == 6)
5226 conf->max_degraded = 2;
5227 else
5228 conf->max_degraded = 1;
5229 conf->algorithm = mddev->new_layout;
5230 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005231 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005232 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005233 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005234 conf->prev_algo = mddev->layout;
5235 }
NeilBrown91adb562009-03-31 14:39:39 +11005236
5237 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005238 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005239 if (grow_stripes(conf, conf->max_nr_stripes)) {
5240 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005241 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5242 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005243 goto abort;
5244 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005245 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5246 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005247
NeilBrown02326052012-07-03 15:56:52 +10005248 sprintf(pers_name, "raid%d", mddev->new_level);
5249 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005250 if (!conf->thread) {
5251 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005252 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005253 mdname(mddev));
5254 goto abort;
5255 }
5256
5257 return conf;
5258
5259 abort:
5260 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005261 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005262 return ERR_PTR(-EIO);
5263 } else
5264 return ERR_PTR(-ENOMEM);
5265}
5266
NeilBrownc148ffd2009-11-13 17:47:00 +11005267
5268static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5269{
5270 switch (algo) {
5271 case ALGORITHM_PARITY_0:
5272 if (raid_disk < max_degraded)
5273 return 1;
5274 break;
5275 case ALGORITHM_PARITY_N:
5276 if (raid_disk >= raid_disks - max_degraded)
5277 return 1;
5278 break;
5279 case ALGORITHM_PARITY_0_6:
5280 if (raid_disk == 0 ||
5281 raid_disk == raid_disks - 1)
5282 return 1;
5283 break;
5284 case ALGORITHM_LEFT_ASYMMETRIC_6:
5285 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5286 case ALGORITHM_LEFT_SYMMETRIC_6:
5287 case ALGORITHM_RIGHT_SYMMETRIC_6:
5288 if (raid_disk == raid_disks - 1)
5289 return 1;
5290 }
5291 return 0;
5292}
5293
NeilBrownfd01b882011-10-11 16:47:53 +11005294static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005295{
NeilBrownd1688a62011-10-11 16:49:52 +11005296 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005297 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005298 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005299 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005300 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005301 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005302 long long min_offset_diff = 0;
5303 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005304
Andre Noll8c6ac862009-06-18 08:48:06 +10005305 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005306 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005307 " -- starting background reconstruction\n",
5308 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005309
5310 rdev_for_each(rdev, mddev) {
5311 long long diff;
5312 if (rdev->raid_disk < 0)
5313 continue;
5314 diff = (rdev->new_data_offset - rdev->data_offset);
5315 if (first) {
5316 min_offset_diff = diff;
5317 first = 0;
5318 } else if (mddev->reshape_backwards &&
5319 diff < min_offset_diff)
5320 min_offset_diff = diff;
5321 else if (!mddev->reshape_backwards &&
5322 diff > min_offset_diff)
5323 min_offset_diff = diff;
5324 }
5325
NeilBrownf6705572006-03-27 01:18:11 -08005326 if (mddev->reshape_position != MaxSector) {
5327 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005328 * Difficulties arise if the stripe we would write to
5329 * next is at or after the stripe we would read from next.
5330 * For a reshape that changes the number of devices, this
5331 * is only possible for a very short time, and mdadm makes
5332 * sure that time appears to have past before assembling
5333 * the array. So we fail if that time hasn't passed.
5334 * For a reshape that keeps the number of devices the same
5335 * mdadm must be monitoring the reshape can keeping the
5336 * critical areas read-only and backed up. It will start
5337 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005338 */
5339 sector_t here_new, here_old;
5340 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005341 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005342
NeilBrown88ce4932009-03-31 15:24:23 +11005343 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005344 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005345 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005346 mdname(mddev));
5347 return -EINVAL;
5348 }
NeilBrownf6705572006-03-27 01:18:11 -08005349 old_disks = mddev->raid_disks - mddev->delta_disks;
5350 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005351 * further up in new geometry must map after here in old
5352 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005353 */
5354 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005355 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005356 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005357 printk(KERN_ERR "md/raid:%s: reshape_position not "
5358 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005359 return -EINVAL;
5360 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005361 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005362 /* here_new is the stripe we will write to */
5363 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005364 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005365 (old_disks-max_degraded));
5366 /* here_old is the first stripe that we might need to read
5367 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005368 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005369 if ((here_new * mddev->new_chunk_sectors !=
5370 here_old * mddev->chunk_sectors)) {
5371 printk(KERN_ERR "md/raid:%s: reshape position is"
5372 " confused - aborting\n", mdname(mddev));
5373 return -EINVAL;
5374 }
NeilBrown67ac6012009-08-13 10:06:24 +10005375 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005376 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005377 * and taking constant backups.
5378 * mdadm always starts a situation like this in
5379 * readonly mode so it can take control before
5380 * allowing any writes. So just check for that.
5381 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005382 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5383 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5384 /* not really in-place - so OK */;
5385 else if (mddev->ro == 0) {
5386 printk(KERN_ERR "md/raid:%s: in-place reshape "
5387 "must be started in read-only mode "
5388 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005389 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005390 return -EINVAL;
5391 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005392 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005393 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005394 here_old * mddev->chunk_sectors)
5395 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005396 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005397 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005398 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5399 "auto-recovery - aborting.\n",
5400 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005401 return -EINVAL;
5402 }
NeilBrown0c55e022010-05-03 14:09:02 +10005403 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5404 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005405 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005406 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005407 BUG_ON(mddev->level != mddev->new_level);
5408 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005409 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005410 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005411 }
5412
NeilBrown245f46c2009-03-31 14:39:39 +11005413 if (mddev->private == NULL)
5414 conf = setup_conf(mddev);
5415 else
5416 conf = mddev->private;
5417
NeilBrown91adb562009-03-31 14:39:39 +11005418 if (IS_ERR(conf))
5419 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005420
NeilBrownb5254dd2012-05-21 09:27:01 +10005421 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005422 mddev->thread = conf->thread;
5423 conf->thread = NULL;
5424 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
NeilBrown17045f52011-12-23 10:17:53 +11005426 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5427 i++) {
5428 rdev = conf->disks[i].rdev;
5429 if (!rdev && conf->disks[i].replacement) {
5430 /* The replacement is all we have yet */
5431 rdev = conf->disks[i].replacement;
5432 conf->disks[i].replacement = NULL;
5433 clear_bit(Replacement, &rdev->flags);
5434 conf->disks[i].rdev = rdev;
5435 }
5436 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005437 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005438 if (conf->disks[i].replacement &&
5439 conf->reshape_progress != MaxSector) {
5440 /* replacements and reshape simply do not mix. */
5441 printk(KERN_ERR "md: cannot handle concurrent "
5442 "replacement and reshape.\n");
5443 goto abort;
5444 }
NeilBrown2f115882010-06-17 17:41:03 +10005445 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005446 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005447 continue;
5448 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005449 /* This disc is not fully in-sync. However if it
5450 * just stored parity (beyond the recovery_offset),
5451 * when we don't need to be concerned about the
5452 * array being dirty.
5453 * When reshape goes 'backwards', we never have
5454 * partially completed devices, so we only need
5455 * to worry about reshape going forwards.
5456 */
5457 /* Hack because v0.91 doesn't store recovery_offset properly. */
5458 if (mddev->major_version == 0 &&
5459 mddev->minor_version > 90)
5460 rdev->recovery_offset = reshape_offset;
5461
NeilBrownc148ffd2009-11-13 17:47:00 +11005462 if (rdev->recovery_offset < reshape_offset) {
5463 /* We need to check old and new layout */
5464 if (!only_parity(rdev->raid_disk,
5465 conf->algorithm,
5466 conf->raid_disks,
5467 conf->max_degraded))
5468 continue;
5469 }
5470 if (!only_parity(rdev->raid_disk,
5471 conf->prev_algo,
5472 conf->previous_raid_disks,
5473 conf->max_degraded))
5474 continue;
5475 dirty_parity_disks++;
5476 }
NeilBrown91adb562009-03-31 14:39:39 +11005477
NeilBrown17045f52011-12-23 10:17:53 +11005478 /*
5479 * 0 for a fully functional array, 1 or 2 for a degraded array.
5480 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005481 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482
NeilBrown674806d2010-06-16 17:17:53 +10005483 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005484 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005486 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 goto abort;
5488 }
5489
NeilBrown91adb562009-03-31 14:39:39 +11005490 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005491 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005492 mddev->resync_max_sectors = mddev->dev_sectors;
5493
NeilBrownc148ffd2009-11-13 17:47:00 +11005494 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005496 if (mddev->ok_start_degraded)
5497 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005498 "md/raid:%s: starting dirty degraded array"
5499 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005500 mdname(mddev));
5501 else {
5502 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005503 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005504 mdname(mddev));
5505 goto abort;
5506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 }
5508
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005510 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5511 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005512 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5513 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 else
NeilBrown0c55e022010-05-03 14:09:02 +10005515 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5516 " out of %d devices, algorithm %d\n",
5517 mdname(mddev), conf->level,
5518 mddev->raid_disks - mddev->degraded,
5519 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
5521 print_raid5_conf(conf);
5522
NeilBrownfef9c612009-03-31 15:16:46 +11005523 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005524 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005525 atomic_set(&conf->reshape_stripes, 0);
5526 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5527 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5528 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5529 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5530 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005531 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005532 }
5533
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534
5535 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005536 if (mddev->to_remove == &raid5_attrs_group)
5537 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005538 else if (mddev->kobj.sd &&
5539 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005540 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005541 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005542 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005543 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5544
5545 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005546 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005547 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005548 /* read-ahead size must cover two whole stripes, which
5549 * is 2 * (datadisks) * chunksize where 'n' is the
5550 * number of raid devices
5551 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5553 int stripe = data_disks *
5554 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5555 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5556 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005557
5558 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005559
5560 mddev->queue->backing_dev_info.congested_data = mddev;
5561 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005562
5563 chunk_size = mddev->chunk_sectors << 9;
5564 blk_queue_io_min(mddev->queue, chunk_size);
5565 blk_queue_io_opt(mddev->queue, chunk_size *
5566 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005567 /*
5568 * We can only discard a whole stripe. It doesn't make sense to
5569 * discard data disk but write parity disk
5570 */
5571 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11005572 /* Round up to power of 2, as discard handling
5573 * currently assumes that */
5574 while ((stripe-1) & stripe)
5575 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11005576 mddev->queue->limits.discard_alignment = stripe;
5577 mddev->queue->limits.discard_granularity = stripe;
5578 /*
5579 * unaligned part of discard request will be ignored, so can't
5580 * guarantee discard_zerors_data
5581 */
5582 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005583
NeilBrown05616be2012-05-21 09:27:00 +10005584 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005585 disk_stack_limits(mddev->gendisk, rdev->bdev,
5586 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005587 disk_stack_limits(mddev->gendisk, rdev->bdev,
5588 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005589 /*
5590 * discard_zeroes_data is required, otherwise data
5591 * could be lost. Consider a scenario: discard a stripe
5592 * (the stripe could be inconsistent if
5593 * discard_zeroes_data is 0); write one disk of the
5594 * stripe (the stripe could be inconsistent again
5595 * depending on which disks are used to calculate
5596 * parity); the disk is broken; The stripe data of this
5597 * disk is lost.
5598 */
5599 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5600 !bdev_get_queue(rdev->bdev)->
5601 limits.discard_zeroes_data)
5602 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10005603 }
Shaohua Li620125f2012-10-11 13:49:05 +11005604
5605 if (discard_supported &&
5606 mddev->queue->limits.max_discard_sectors >= stripe &&
5607 mddev->queue->limits.discard_granularity >= stripe)
5608 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5609 mddev->queue);
5610 else
5611 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5612 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 }
5614
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 return 0;
5616abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005617 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005618 print_raid5_conf(conf);
5619 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005621 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 return -EIO;
5623}
5624
NeilBrownfd01b882011-10-11 16:47:53 +11005625static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626{
NeilBrownd1688a62011-10-11 16:49:52 +11005627 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628
NeilBrown01f96c02011-09-21 15:30:20 +10005629 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005630 if (mddev->queue)
5631 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005632 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005633 mddev->private = NULL;
5634 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 return 0;
5636}
5637
NeilBrownfd01b882011-10-11 16:47:53 +11005638static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639{
NeilBrownd1688a62011-10-11 16:49:52 +11005640 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 int i;
5642
Andre Noll9d8f0362009-06-18 08:45:01 +10005643 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5644 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005645 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 for (i = 0; i < conf->raid_disks; i++)
5647 seq_printf (seq, "%s",
5648 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005649 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651}
5652
NeilBrownd1688a62011-10-11 16:49:52 +11005653static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654{
5655 int i;
5656 struct disk_info *tmp;
5657
NeilBrown0c55e022010-05-03 14:09:02 +10005658 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 if (!conf) {
5660 printk("(conf==NULL)\n");
5661 return;
5662 }
NeilBrown0c55e022010-05-03 14:09:02 +10005663 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5664 conf->raid_disks,
5665 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666
5667 for (i = 0; i < conf->raid_disks; i++) {
5668 char b[BDEVNAME_SIZE];
5669 tmp = conf->disks + i;
5670 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005671 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5672 i, !test_bit(Faulty, &tmp->rdev->flags),
5673 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 }
5675}
5676
NeilBrownfd01b882011-10-11 16:47:53 +11005677static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678{
5679 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005680 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005682 int count = 0;
5683 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684
5685 for (i = 0; i < conf->raid_disks; i++) {
5686 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005687 if (tmp->replacement
5688 && tmp->replacement->recovery_offset == MaxSector
5689 && !test_bit(Faulty, &tmp->replacement->flags)
5690 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5691 /* Replacement has just become active. */
5692 if (!tmp->rdev
5693 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5694 count++;
5695 if (tmp->rdev) {
5696 /* Replaced device not technically faulty,
5697 * but we need to be sure it gets removed
5698 * and never re-added.
5699 */
5700 set_bit(Faulty, &tmp->rdev->flags);
5701 sysfs_notify_dirent_safe(
5702 tmp->rdev->sysfs_state);
5703 }
5704 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5705 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005706 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005707 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005708 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005709 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005710 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 }
5712 }
NeilBrown6b965622010-08-18 11:56:59 +10005713 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005714 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005715 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005717 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718}
5719
NeilBrownb8321b62011-12-23 10:17:51 +11005720static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721{
NeilBrownd1688a62011-10-11 16:49:52 +11005722 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005724 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005725 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 struct disk_info *p = conf->disks + number;
5727
5728 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005729 if (rdev == p->rdev)
5730 rdevp = &p->rdev;
5731 else if (rdev == p->replacement)
5732 rdevp = &p->replacement;
5733 else
5734 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005735
NeilBrown657e3e42011-12-23 10:17:52 +11005736 if (number >= conf->raid_disks &&
5737 conf->reshape_progress == MaxSector)
5738 clear_bit(In_sync, &rdev->flags);
5739
5740 if (test_bit(In_sync, &rdev->flags) ||
5741 atomic_read(&rdev->nr_pending)) {
5742 err = -EBUSY;
5743 goto abort;
5744 }
5745 /* Only remove non-faulty devices if recovery
5746 * isn't possible.
5747 */
5748 if (!test_bit(Faulty, &rdev->flags) &&
5749 mddev->recovery_disabled != conf->recovery_disabled &&
5750 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005751 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005752 number < conf->raid_disks) {
5753 err = -EBUSY;
5754 goto abort;
5755 }
5756 *rdevp = NULL;
5757 synchronize_rcu();
5758 if (atomic_read(&rdev->nr_pending)) {
5759 /* lost the race, try later */
5760 err = -EBUSY;
5761 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005762 } else if (p->replacement) {
5763 /* We must have just cleared 'rdev' */
5764 p->rdev = p->replacement;
5765 clear_bit(Replacement, &p->replacement->flags);
5766 smp_mb(); /* Make sure other CPUs may see both as identical
5767 * but will never see neither - if they are careful
5768 */
5769 p->replacement = NULL;
5770 clear_bit(WantReplacement, &rdev->flags);
5771 } else
5772 /* We might have just removed the Replacement as faulty-
5773 * clear the bit just in case
5774 */
5775 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776abort:
5777
5778 print_raid5_conf(conf);
5779 return err;
5780}
5781
NeilBrownfd01b882011-10-11 16:47:53 +11005782static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783{
NeilBrownd1688a62011-10-11 16:49:52 +11005784 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005785 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 int disk;
5787 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005788 int first = 0;
5789 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790
NeilBrown7f0da592011-07-28 11:39:22 +10005791 if (mddev->recovery_disabled == conf->recovery_disabled)
5792 return -EBUSY;
5793
NeilBrowndc10c642012-03-19 12:46:37 +11005794 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005796 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797
Neil Brown6c2fce22008-06-28 08:31:31 +10005798 if (rdev->raid_disk >= 0)
5799 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
5801 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005802 * find the disk ... but prefer rdev->saved_raid_disk
5803 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005805 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005806 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005807 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005808 first = rdev->saved_raid_disk;
5809
5810 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005811 p = conf->disks + disk;
5812 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005813 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005815 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005816 if (rdev->saved_raid_disk != disk)
5817 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005818 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005819 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005821 }
5822 for (disk = first; disk <= last; disk++) {
5823 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005824 if (test_bit(WantReplacement, &p->rdev->flags) &&
5825 p->replacement == NULL) {
5826 clear_bit(In_sync, &rdev->flags);
5827 set_bit(Replacement, &rdev->flags);
5828 rdev->raid_disk = disk;
5829 err = 0;
5830 conf->fullsync = 1;
5831 rcu_assign_pointer(p->replacement, rdev);
5832 break;
5833 }
5834 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005835out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005837 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838}
5839
NeilBrownfd01b882011-10-11 16:47:53 +11005840static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841{
5842 /* no resync is happening, and there is enough space
5843 * on all devices, so we can resize.
5844 * We need to make sure resync covers any new space.
5845 * If the array is shrinking we should possibly wait until
5846 * any io in the removed space completes, but it hardly seems
5847 * worth it.
5848 */
NeilBrowna4a61252012-05-22 13:55:27 +10005849 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005850 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005851 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5852 if (mddev->external_size &&
5853 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005854 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005855 if (mddev->bitmap) {
5856 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5857 if (ret)
5858 return ret;
5859 }
5860 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005861 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005862 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005863 if (sectors > mddev->dev_sectors &&
5864 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005865 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5867 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005868 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005869 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 return 0;
5871}
5872
NeilBrownfd01b882011-10-11 16:47:53 +11005873static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005874{
5875 /* Can only proceed if there are plenty of stripe_heads.
5876 * We need a minimum of one full stripe,, and for sensible progress
5877 * it is best to have about 4 times that.
5878 * If we require 4 times, then the default 256 4K stripe_heads will
5879 * allow for chunk sizes up to 256K, which is probably OK.
5880 * If the chunk size is greater, user-space should request more
5881 * stripe_heads first.
5882 */
NeilBrownd1688a62011-10-11 16:49:52 +11005883 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005884 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5885 > conf->max_nr_stripes ||
5886 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5887 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005888 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5889 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005890 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5891 / STRIPE_SIZE)*4);
5892 return 0;
5893 }
5894 return 1;
5895}
5896
NeilBrownfd01b882011-10-11 16:47:53 +11005897static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005898{
NeilBrownd1688a62011-10-11 16:49:52 +11005899 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005900
NeilBrown88ce4932009-03-31 15:24:23 +11005901 if (mddev->delta_disks == 0 &&
5902 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005903 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005904 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005905 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005906 return -EINVAL;
5907 if (mddev->delta_disks < 0) {
5908 /* We might be able to shrink, but the devices must
5909 * be made bigger first.
5910 * For raid6, 4 is the minimum size.
5911 * Otherwise 2 is the minimum
5912 */
5913 int min = 2;
5914 if (mddev->level == 6)
5915 min = 4;
5916 if (mddev->raid_disks + mddev->delta_disks < min)
5917 return -EINVAL;
5918 }
NeilBrown29269552006-03-27 01:18:10 -08005919
NeilBrown01ee22b2009-06-18 08:47:20 +10005920 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005921 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005922
NeilBrowne56108d62012-10-11 14:24:13 +11005923 return resize_stripes(conf, (conf->previous_raid_disks
5924 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08005925}
5926
NeilBrownfd01b882011-10-11 16:47:53 +11005927static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005928{
NeilBrownd1688a62011-10-11 16:49:52 +11005929 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005930 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005931 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005932 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005933
NeilBrownf4168852007-02-28 20:11:53 -08005934 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005935 return -EBUSY;
5936
NeilBrown01ee22b2009-06-18 08:47:20 +10005937 if (!check_stripe_cache(mddev))
5938 return -ENOSPC;
5939
NeilBrown30b67642012-05-22 13:55:28 +10005940 if (has_failed(conf))
5941 return -EINVAL;
5942
NeilBrownc6563a82012-05-21 09:27:00 +10005943 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005944 if (!test_bit(In_sync, &rdev->flags)
5945 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005946 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005947 }
NeilBrown63c70c42006-03-27 01:18:13 -08005948
NeilBrownf4168852007-02-28 20:11:53 -08005949 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005950 /* Not enough devices even to make a degraded array
5951 * of that size
5952 */
5953 return -EINVAL;
5954
NeilBrownec32a2b2009-03-31 15:17:38 +11005955 /* Refuse to reduce size of the array. Any reductions in
5956 * array size must be through explicit setting of array_size
5957 * attribute.
5958 */
5959 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5960 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005961 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005962 "before number of disks\n", mdname(mddev));
5963 return -EINVAL;
5964 }
5965
NeilBrownf6705572006-03-27 01:18:11 -08005966 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005967 spin_lock_irq(&conf->device_lock);
5968 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005969 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005970 conf->prev_chunk_sectors = conf->chunk_sectors;
5971 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005972 conf->prev_algo = conf->algorithm;
5973 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005974 conf->generation++;
5975 /* Code that selects data_offset needs to see the generation update
5976 * if reshape_progress has been set - so a memory barrier needed.
5977 */
5978 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005979 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005980 conf->reshape_progress = raid5_size(mddev, 0, 0);
5981 else
5982 conf->reshape_progress = 0;
5983 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005984 spin_unlock_irq(&conf->device_lock);
5985
5986 /* Add some new drives, as many as will fit.
5987 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005988 * Don't add devices if we are reducing the number of
5989 * devices in the array. This is because it is not possible
5990 * to correctly record the "partially reconstructed" state of
5991 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005992 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005993 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005994 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005995 if (rdev->raid_disk < 0 &&
5996 !test_bit(Faulty, &rdev->flags)) {
5997 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005998 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005999 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11006000 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11006001 else
NeilBrown87a8dec2011-01-31 11:57:43 +11006002 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10006003
6004 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11006005 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11006006 }
NeilBrown87a8dec2011-01-31 11:57:43 +11006007 } else if (rdev->raid_disk >= conf->previous_raid_disks
6008 && !test_bit(Faulty, &rdev->flags)) {
6009 /* This is a spare that was manually added */
6010 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11006011 }
NeilBrown29269552006-03-27 01:18:10 -08006012
NeilBrown87a8dec2011-01-31 11:57:43 +11006013 /* When a reshape changes the number of devices,
6014 * ->degraded is measured against the larger of the
6015 * pre and post number of devices.
6016 */
NeilBrownec32a2b2009-03-31 15:17:38 +11006017 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006018 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11006019 spin_unlock_irqrestore(&conf->device_lock, flags);
6020 }
NeilBrown63c70c42006-03-27 01:18:13 -08006021 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10006022 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07006023 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08006024
NeilBrown29269552006-03-27 01:18:10 -08006025 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6026 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6027 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6028 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6029 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006030 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08006031 if (!mddev->sync_thread) {
6032 mddev->recovery = 0;
6033 spin_lock_irq(&conf->device_lock);
6034 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006035 rdev_for_each(rdev, mddev)
6036 rdev->new_data_offset = rdev->data_offset;
6037 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006038 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11006039 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08006040 spin_unlock_irq(&conf->device_lock);
6041 return -EAGAIN;
6042 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006043 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006044 md_wakeup_thread(mddev->sync_thread);
6045 md_new_event(mddev);
6046 return 0;
6047}
NeilBrown29269552006-03-27 01:18:10 -08006048
NeilBrownec32a2b2009-03-31 15:17:38 +11006049/* This is called from the reshape thread and should make any
6050 * changes needed in 'conf'
6051 */
NeilBrownd1688a62011-10-11 16:49:52 +11006052static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006053{
NeilBrown29269552006-03-27 01:18:10 -08006054
NeilBrownf6705572006-03-27 01:18:11 -08006055 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006056 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006057
NeilBrownf6705572006-03-27 01:18:11 -08006058 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006059 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006060 rdev_for_each(rdev, conf->mddev)
6061 rdev->data_offset = rdev->new_data_offset;
6062 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006063 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006064 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006065 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006066
6067 /* read-ahead size must cover two whole stripes, which is
6068 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6069 */
NeilBrown4a5add42010-06-01 19:37:28 +10006070 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006071 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006072 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006073 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006074 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6075 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6076 }
NeilBrown29269552006-03-27 01:18:10 -08006077 }
NeilBrown29269552006-03-27 01:18:10 -08006078}
6079
NeilBrownec32a2b2009-03-31 15:17:38 +11006080/* This is called from the raid5d thread with mddev_lock held.
6081 * It makes config changes to the device.
6082 */
NeilBrownfd01b882011-10-11 16:47:53 +11006083static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006084{
NeilBrownd1688a62011-10-11 16:49:52 +11006085 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006086
6087 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6088
NeilBrownec32a2b2009-03-31 15:17:38 +11006089 if (mddev->delta_disks > 0) {
6090 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6091 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006092 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006093 } else {
6094 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006095 spin_lock_irq(&conf->device_lock);
6096 mddev->degraded = calc_degraded(conf);
6097 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006098 for (d = conf->raid_disks ;
6099 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006100 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006101 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006102 if (rdev)
6103 clear_bit(In_sync, &rdev->flags);
6104 rdev = conf->disks[d].replacement;
6105 if (rdev)
6106 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006107 }
NeilBrowncea9c222009-03-31 15:15:05 +11006108 }
NeilBrown88ce4932009-03-31 15:24:23 +11006109 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006110 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006111 mddev->reshape_position = MaxSector;
6112 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006113 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006114 }
6115}
6116
NeilBrownfd01b882011-10-11 16:47:53 +11006117static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006118{
NeilBrownd1688a62011-10-11 16:49:52 +11006119 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006120
6121 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006122 case 2: /* resume for a suspend */
6123 wake_up(&conf->wait_for_overlap);
6124 break;
6125
NeilBrown72626682005-09-09 16:23:54 -07006126 case 1: /* stop all writes */
6127 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006128 /* '2' tells resync/reshape to pause so that all
6129 * active stripes can drain
6130 */
6131 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006132 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006133 atomic_read(&conf->active_stripes) == 0 &&
6134 atomic_read(&conf->active_aligned_reads) == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01006135 conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006136 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006137 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006138 /* allow reshape to continue */
6139 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006140 break;
6141
6142 case 0: /* re-enable writes */
6143 spin_lock_irq(&conf->device_lock);
6144 conf->quiesce = 0;
6145 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006146 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006147 spin_unlock_irq(&conf->device_lock);
6148 break;
6149 }
NeilBrown72626682005-09-09 16:23:54 -07006150}
NeilBrownb15c2e52006-01-06 00:20:16 -08006151
NeilBrownd562b0c2009-03-31 14:39:39 +11006152
NeilBrownfd01b882011-10-11 16:47:53 +11006153static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006154{
NeilBrowne373ab12011-10-11 16:48:59 +11006155 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006156 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006157
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006158 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006159 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006160 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6161 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006162 return ERR_PTR(-EINVAL);
6163 }
6164
NeilBrowne373ab12011-10-11 16:48:59 +11006165 sectors = raid0_conf->strip_zone[0].zone_end;
6166 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006167 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006168 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006169 mddev->new_layout = ALGORITHM_PARITY_N;
6170 mddev->new_chunk_sectors = mddev->chunk_sectors;
6171 mddev->raid_disks += 1;
6172 mddev->delta_disks = 1;
6173 /* make sure it will be not marked as dirty */
6174 mddev->recovery_cp = MaxSector;
6175
6176 return setup_conf(mddev);
6177}
6178
6179
NeilBrownfd01b882011-10-11 16:47:53 +11006180static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006181{
6182 int chunksect;
6183
6184 if (mddev->raid_disks != 2 ||
6185 mddev->degraded > 1)
6186 return ERR_PTR(-EINVAL);
6187
6188 /* Should check if there are write-behind devices? */
6189
6190 chunksect = 64*2; /* 64K by default */
6191
6192 /* The array must be an exact multiple of chunksize */
6193 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6194 chunksect >>= 1;
6195
6196 if ((chunksect<<9) < STRIPE_SIZE)
6197 /* array size does not allow a suitable chunk size */
6198 return ERR_PTR(-EINVAL);
6199
6200 mddev->new_level = 5;
6201 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006202 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006203
6204 return setup_conf(mddev);
6205}
6206
NeilBrownfd01b882011-10-11 16:47:53 +11006207static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006208{
6209 int new_layout;
6210
6211 switch (mddev->layout) {
6212 case ALGORITHM_LEFT_ASYMMETRIC_6:
6213 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6214 break;
6215 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6216 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6217 break;
6218 case ALGORITHM_LEFT_SYMMETRIC_6:
6219 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6220 break;
6221 case ALGORITHM_RIGHT_SYMMETRIC_6:
6222 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6223 break;
6224 case ALGORITHM_PARITY_0_6:
6225 new_layout = ALGORITHM_PARITY_0;
6226 break;
6227 case ALGORITHM_PARITY_N:
6228 new_layout = ALGORITHM_PARITY_N;
6229 break;
6230 default:
6231 return ERR_PTR(-EINVAL);
6232 }
6233 mddev->new_level = 5;
6234 mddev->new_layout = new_layout;
6235 mddev->delta_disks = -1;
6236 mddev->raid_disks -= 1;
6237 return setup_conf(mddev);
6238}
6239
NeilBrownd562b0c2009-03-31 14:39:39 +11006240
NeilBrownfd01b882011-10-11 16:47:53 +11006241static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006242{
NeilBrown88ce4932009-03-31 15:24:23 +11006243 /* For a 2-drive array, the layout and chunk size can be changed
6244 * immediately as not restriping is needed.
6245 * For larger arrays we record the new value - after validation
6246 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006247 */
NeilBrownd1688a62011-10-11 16:49:52 +11006248 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006249 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006250
NeilBrown597a7112009-06-18 08:47:42 +10006251 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006252 return -EINVAL;
6253 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006254 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006255 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006256 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006257 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006258 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006259 /* not factor of array size */
6260 return -EINVAL;
6261 }
6262
6263 /* They look valid */
6264
NeilBrown88ce4932009-03-31 15:24:23 +11006265 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006266 /* can make the change immediately */
6267 if (mddev->new_layout >= 0) {
6268 conf->algorithm = mddev->new_layout;
6269 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006270 }
6271 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006272 conf->chunk_sectors = new_chunk ;
6273 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006274 }
6275 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6276 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006277 }
NeilBrown50ac1682009-06-18 08:47:55 +10006278 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006279}
6280
NeilBrownfd01b882011-10-11 16:47:53 +11006281static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006282{
NeilBrown597a7112009-06-18 08:47:42 +10006283 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006284
NeilBrown597a7112009-06-18 08:47:42 +10006285 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006286 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006287 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006288 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006289 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006290 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006291 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006292 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006293 /* not factor of array size */
6294 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006295 }
NeilBrown88ce4932009-03-31 15:24:23 +11006296
6297 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006298 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006299}
6300
NeilBrownfd01b882011-10-11 16:47:53 +11006301static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006302{
6303 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006304 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006305 * raid1 - if there are two drives. We need to know the chunk size
6306 * raid4 - trivial - just use a raid4 layout.
6307 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006308 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006309 if (mddev->level == 0)
6310 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006311 if (mddev->level == 1)
6312 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006313 if (mddev->level == 4) {
6314 mddev->new_layout = ALGORITHM_PARITY_N;
6315 mddev->new_level = 5;
6316 return setup_conf(mddev);
6317 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006318 if (mddev->level == 6)
6319 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006320
6321 return ERR_PTR(-EINVAL);
6322}
6323
NeilBrownfd01b882011-10-11 16:47:53 +11006324static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006325{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006326 /* raid4 can take over:
6327 * raid0 - if there is only one strip zone
6328 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006329 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006330 if (mddev->level == 0)
6331 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006332 if (mddev->level == 5 &&
6333 mddev->layout == ALGORITHM_PARITY_N) {
6334 mddev->new_layout = 0;
6335 mddev->new_level = 4;
6336 return setup_conf(mddev);
6337 }
6338 return ERR_PTR(-EINVAL);
6339}
NeilBrownd562b0c2009-03-31 14:39:39 +11006340
NeilBrown84fc4b52011-10-11 16:49:58 +11006341static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006342
NeilBrownfd01b882011-10-11 16:47:53 +11006343static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006344{
6345 /* Currently can only take over a raid5. We map the
6346 * personality to an equivalent raid6 personality
6347 * with the Q block at the end.
6348 */
6349 int new_layout;
6350
6351 if (mddev->pers != &raid5_personality)
6352 return ERR_PTR(-EINVAL);
6353 if (mddev->degraded > 1)
6354 return ERR_PTR(-EINVAL);
6355 if (mddev->raid_disks > 253)
6356 return ERR_PTR(-EINVAL);
6357 if (mddev->raid_disks < 3)
6358 return ERR_PTR(-EINVAL);
6359
6360 switch (mddev->layout) {
6361 case ALGORITHM_LEFT_ASYMMETRIC:
6362 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6363 break;
6364 case ALGORITHM_RIGHT_ASYMMETRIC:
6365 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6366 break;
6367 case ALGORITHM_LEFT_SYMMETRIC:
6368 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6369 break;
6370 case ALGORITHM_RIGHT_SYMMETRIC:
6371 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6372 break;
6373 case ALGORITHM_PARITY_0:
6374 new_layout = ALGORITHM_PARITY_0_6;
6375 break;
6376 case ALGORITHM_PARITY_N:
6377 new_layout = ALGORITHM_PARITY_N;
6378 break;
6379 default:
6380 return ERR_PTR(-EINVAL);
6381 }
6382 mddev->new_level = 6;
6383 mddev->new_layout = new_layout;
6384 mddev->delta_disks = 1;
6385 mddev->raid_disks += 1;
6386 return setup_conf(mddev);
6387}
6388
6389
NeilBrown84fc4b52011-10-11 16:49:58 +11006390static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006391{
6392 .name = "raid6",
6393 .level = 6,
6394 .owner = THIS_MODULE,
6395 .make_request = make_request,
6396 .run = run,
6397 .stop = stop,
6398 .status = status,
6399 .error_handler = error,
6400 .hot_add_disk = raid5_add_disk,
6401 .hot_remove_disk= raid5_remove_disk,
6402 .spare_active = raid5_spare_active,
6403 .sync_request = sync_request,
6404 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006405 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006406 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006407 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006408 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006409 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006410 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006411};
NeilBrown84fc4b52011-10-11 16:49:58 +11006412static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413{
6414 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006415 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 .owner = THIS_MODULE,
6417 .make_request = make_request,
6418 .run = run,
6419 .stop = stop,
6420 .status = status,
6421 .error_handler = error,
6422 .hot_add_disk = raid5_add_disk,
6423 .hot_remove_disk= raid5_remove_disk,
6424 .spare_active = raid5_spare_active,
6425 .sync_request = sync_request,
6426 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006427 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006428 .check_reshape = raid5_check_reshape,
6429 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006430 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006431 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006432 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433};
6434
NeilBrown84fc4b52011-10-11 16:49:58 +11006435static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436{
NeilBrown2604b702006-01-06 00:20:36 -08006437 .name = "raid4",
6438 .level = 4,
6439 .owner = THIS_MODULE,
6440 .make_request = make_request,
6441 .run = run,
6442 .stop = stop,
6443 .status = status,
6444 .error_handler = error,
6445 .hot_add_disk = raid5_add_disk,
6446 .hot_remove_disk= raid5_remove_disk,
6447 .spare_active = raid5_spare_active,
6448 .sync_request = sync_request,
6449 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006450 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006451 .check_reshape = raid5_check_reshape,
6452 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006453 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006454 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006455 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006456};
6457
6458static int __init raid5_init(void)
6459{
NeilBrown16a53ec2006-06-26 00:27:38 -07006460 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006461 register_md_personality(&raid5_personality);
6462 register_md_personality(&raid4_personality);
6463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464}
6465
NeilBrown2604b702006-01-06 00:20:36 -08006466static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467{
NeilBrown16a53ec2006-06-26 00:27:38 -07006468 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006469 unregister_md_personality(&raid5_personality);
6470 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471}
6472
6473module_init(raid5_init);
6474module_exit(raid5_exit);
6475MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006476MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006478MODULE_ALIAS("md-raid5");
6479MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006480MODULE_ALIAS("md-level-5");
6481MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006482MODULE_ALIAS("md-personality-8"); /* RAID6 */
6483MODULE_ALIAS("md-raid6");
6484MODULE_ALIAS("md-level-6");
6485
6486/* This used to be two separate modules, they were: */
6487MODULE_ALIAS("raid5");
6488MODULE_ALIAS("raid6");