blob: 697f026cb318d74874329e2585c8f148738371b3 [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;
368
Dan Williams45b42332007-07-09 11:56:43 -0700369 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800370 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100371 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700373 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return NULL;
375}
376
NeilBrown674806d2010-06-16 17:17:53 +1000377/*
378 * Need to check if array has failed when deciding whether to:
379 * - start an array
380 * - remove non-faulty devices
381 * - add a spare
382 * - allow a reshape
383 * This determination is simple when no reshape is happening.
384 * However if there is a reshape, we need to carefully check
385 * both the before and after sections.
386 * This is because some failed devices may only affect one
387 * of the two sections, and some non-in_sync devices may
388 * be insync in the section most affected by failed devices.
389 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100390static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000391{
NeilBrown908f4fb2011-12-23 10:17:50 +1100392 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000393 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000394
395 rcu_read_lock();
396 degraded = 0;
397 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100398 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000399 if (rdev && test_bit(Faulty, &rdev->flags))
400 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000401 if (!rdev || test_bit(Faulty, &rdev->flags))
402 degraded++;
403 else if (test_bit(In_sync, &rdev->flags))
404 ;
405 else
406 /* not in-sync or faulty.
407 * If the reshape increases the number of devices,
408 * this is being recovered by the reshape, so
409 * this 'previous' section is not in_sync.
410 * If the number of devices is being reduced however,
411 * the device can only be part of the array if
412 * we are reverting a reshape, so this section will
413 * be in-sync.
414 */
415 if (conf->raid_disks >= conf->previous_raid_disks)
416 degraded++;
417 }
418 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100419 if (conf->raid_disks == conf->previous_raid_disks)
420 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000421 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100422 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000423 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100424 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000425 if (rdev && test_bit(Faulty, &rdev->flags))
426 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000427 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100428 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000429 else if (test_bit(In_sync, &rdev->flags))
430 ;
431 else
432 /* not in-sync or faulty.
433 * If reshape increases the number of devices, this
434 * section has already been recovered, else it
435 * almost certainly hasn't.
436 */
437 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100438 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000439 }
440 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100441 if (degraded2 > degraded)
442 return degraded2;
443 return degraded;
444}
445
446static int has_failed(struct r5conf *conf)
447{
448 int degraded;
449
450 if (conf->mddev->reshape_position == MaxSector)
451 return conf->mddev->degraded > conf->max_degraded;
452
453 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000454 if (degraded > conf->max_degraded)
455 return 1;
456 return 0;
457}
458
NeilBrownb5663ba2009-03-31 14:39:38 +1100459static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100460get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000461 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 struct stripe_head *sh;
464
Dan Williams45b42332007-07-09 11:56:43 -0700465 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 spin_lock_irq(&conf->device_lock);
468
469 do {
NeilBrown72626682005-09-09 16:23:54 -0700470 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000471 conf->quiesce == 0 || noquiesce,
Lukas Czernereed8c022012-11-30 11:42:40 +0100472 conf->device_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100473 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (!sh) {
475 if (!conf->inactive_blocked)
476 sh = get_free_stripe(conf);
477 if (noblock && sh == NULL)
478 break;
479 if (!sh) {
480 conf->inactive_blocked = 1;
481 wait_event_lock_irq(conf->wait_for_stripe,
482 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800483 (atomic_read(&conf->active_stripes)
484 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 || !conf->inactive_blocked),
Lukas Czernereed8c022012-11-30 11:42:40 +0100486 conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 conf->inactive_blocked = 0;
488 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100489 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 } else {
491 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100492 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000493 && !test_bit(STRIPE_EXPANDING, &sh->state)
494 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 } else {
496 if (!test_bit(STRIPE_HANDLE, &sh->state))
497 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700498 if (list_empty(&sh->lru) &&
499 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700500 BUG();
501 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 }
504 } while (sh == NULL);
505
506 if (sh)
507 atomic_inc(&sh->count);
508
509 spin_unlock_irq(&conf->device_lock);
510 return sh;
511}
512
NeilBrown05616be2012-05-21 09:27:00 +1000513/* Determine if 'data_offset' or 'new_data_offset' should be used
514 * in this stripe_head.
515 */
516static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
517{
518 sector_t progress = conf->reshape_progress;
519 /* Need a memory barrier to make sure we see the value
520 * of conf->generation, or ->data_offset that was set before
521 * reshape_progress was updated.
522 */
523 smp_rmb();
524 if (progress == MaxSector)
525 return 0;
526 if (sh->generation == conf->generation - 1)
527 return 0;
528 /* We are in a reshape, and this is a new-generation stripe,
529 * so use new_data_offset.
530 */
531 return 1;
532}
533
NeilBrown6712ecf2007-09-27 12:47:43 +0200534static void
535raid5_end_read_request(struct bio *bi, int error);
536static void
537raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700538
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000539static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700540{
NeilBrownd1688a62011-10-11 16:49:52 +1100541 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700542 int i, disks = sh->disks;
543
544 might_sleep();
545
546 for (i = disks; i--; ) {
547 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100548 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100549 struct bio *bi, *rbi;
550 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200551 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
552 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
553 rw = WRITE_FUA;
554 else
555 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100556 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100557 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200558 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700559 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100560 else if (test_and_clear_bit(R5_WantReplace,
561 &sh->dev[i].flags)) {
562 rw = WRITE;
563 replace_only = 1;
564 } else
Dan Williams91c00922007-01-02 13:52:30 -0700565 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000566 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
567 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700568
569 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100570 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700571
572 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100573 rbi->bi_rw = rw;
574 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700575 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100576 rbi->bi_end_io = raid5_end_write_request;
577 } else
Dan Williams91c00922007-01-02 13:52:30 -0700578 bi->bi_end_io = raid5_end_read_request;
579
580 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100581 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100582 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
583 rdev = rcu_dereference(conf->disks[i].rdev);
584 if (!rdev) {
585 rdev = rrdev;
586 rrdev = NULL;
587 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100588 if (rw & WRITE) {
589 if (replace_only)
590 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100591 if (rdev == rrdev)
592 /* We raced and saw duplicates */
593 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100594 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100595 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100596 rdev = rrdev;
597 rrdev = NULL;
598 }
NeilBrown977df362011-12-23 10:17:53 +1100599
Dan Williams91c00922007-01-02 13:52:30 -0700600 if (rdev && test_bit(Faulty, &rdev->flags))
601 rdev = NULL;
602 if (rdev)
603 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100604 if (rrdev && test_bit(Faulty, &rrdev->flags))
605 rrdev = NULL;
606 if (rrdev)
607 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700608 rcu_read_unlock();
609
NeilBrown73e92e52011-07-28 11:39:22 +1000610 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100611 * need to check for writes. We never accept write errors
612 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000613 */
614 while ((rw & WRITE) && rdev &&
615 test_bit(WriteErrorSeen, &rdev->flags)) {
616 sector_t first_bad;
617 int bad_sectors;
618 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
619 &first_bad, &bad_sectors);
620 if (!bad)
621 break;
622
623 if (bad < 0) {
624 set_bit(BlockedBadBlocks, &rdev->flags);
625 if (!conf->mddev->external &&
626 conf->mddev->flags) {
627 /* It is very unlikely, but we might
628 * still need to write out the
629 * bad block log - better give it
630 * a chance*/
631 md_check_recovery(conf->mddev);
632 }
majianpeng18507532012-07-03 12:11:54 +1000633 /*
634 * Because md_wait_for_blocked_rdev
635 * will dec nr_pending, we must
636 * increment it first.
637 */
638 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000639 md_wait_for_blocked_rdev(rdev, conf->mddev);
640 } else {
641 /* Acknowledged bad block - skip the write */
642 rdev_dec_pending(rdev, conf->mddev);
643 rdev = NULL;
644 }
645 }
646
Dan Williams91c00922007-01-02 13:52:30 -0700647 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100648 if (s->syncing || s->expanding || s->expanded
649 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700650 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
651
Dan Williams2b7497f2008-06-28 08:31:52 +1000652 set_bit(STRIPE_IO_STARTED, &sh->state);
653
Dan Williams91c00922007-01-02 13:52:30 -0700654 bi->bi_bdev = rdev->bdev;
655 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700656 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700657 bi->bi_rw, i);
658 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000659 if (use_new_offset(conf, sh))
660 bi->bi_sector = (sh->sector
661 + rdev->new_data_offset);
662 else
663 bi->bi_sector = (sh->sector
664 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000665 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
666 bi->bi_rw |= REQ_FLUSH;
667
Dan Williams91c00922007-01-02 13:52:30 -0700668 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700669 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700670 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
671 bi->bi_io_vec[0].bv_offset = 0;
672 bi->bi_size = STRIPE_SIZE;
673 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100674 if (rrdev)
675 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
NeilBrowna9add5d2012-10-31 11:59:09 +1100676 trace_block_bio_remap(bdev_get_queue(bi->bi_bdev),
677 bi, disk_devt(conf->mddev->gendisk),
678 sh->dev[i].sector);
Dan Williams91c00922007-01-02 13:52:30 -0700679 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100680 }
681 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100682 if (s->syncing || s->expanding || s->expanded
683 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100684 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
685
686 set_bit(STRIPE_IO_STARTED, &sh->state);
687
688 rbi->bi_bdev = rrdev->bdev;
689 pr_debug("%s: for %llu schedule op %ld on "
690 "replacement disc %d\n",
691 __func__, (unsigned long long)sh->sector,
692 rbi->bi_rw, i);
693 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000694 if (use_new_offset(conf, sh))
695 rbi->bi_sector = (sh->sector
696 + rrdev->new_data_offset);
697 else
698 rbi->bi_sector = (sh->sector
699 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100700 rbi->bi_flags = 1 << BIO_UPTODATE;
701 rbi->bi_idx = 0;
702 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
703 rbi->bi_io_vec[0].bv_offset = 0;
704 rbi->bi_size = STRIPE_SIZE;
705 rbi->bi_next = NULL;
NeilBrowna9add5d2012-10-31 11:59:09 +1100706 trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev),
707 rbi, disk_devt(conf->mddev->gendisk),
708 sh->dev[i].sector);
NeilBrown977df362011-12-23 10:17:53 +1100709 generic_make_request(rbi);
710 }
711 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000712 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700713 set_bit(STRIPE_DEGRADED, &sh->state);
714 pr_debug("skip op %ld on disc %d for sector %llu\n",
715 bi->bi_rw, i, (unsigned long long)sh->sector);
716 clear_bit(R5_LOCKED, &sh->dev[i].flags);
717 set_bit(STRIPE_HANDLE, &sh->state);
718 }
719 }
720}
721
722static struct dma_async_tx_descriptor *
723async_copy_data(int frombio, struct bio *bio, struct page *page,
724 sector_t sector, struct dma_async_tx_descriptor *tx)
725{
726 struct bio_vec *bvl;
727 struct page *bio_page;
728 int i;
729 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700730 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700731 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700732
733 if (bio->bi_sector >= sector)
734 page_offset = (signed)(bio->bi_sector - sector) * 512;
735 else
736 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700737
Dan Williams0403e382009-09-08 17:42:50 -0700738 if (frombio)
739 flags |= ASYNC_TX_FENCE;
740 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
741
Dan Williams91c00922007-01-02 13:52:30 -0700742 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000743 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700744 int clen;
745 int b_offset = 0;
746
747 if (page_offset < 0) {
748 b_offset = -page_offset;
749 page_offset += b_offset;
750 len -= b_offset;
751 }
752
753 if (len > 0 && page_offset + len > STRIPE_SIZE)
754 clen = STRIPE_SIZE - page_offset;
755 else
756 clen = len;
757
758 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000759 b_offset += bvl->bv_offset;
760 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700761 if (frombio)
762 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700763 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700764 else
765 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700766 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700767 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700768 /* chain the operations */
769 submit.depend_tx = tx;
770
Dan Williams91c00922007-01-02 13:52:30 -0700771 if (clen < len) /* hit end of page */
772 break;
773 page_offset += len;
774 }
775
776 return tx;
777}
778
779static void ops_complete_biofill(void *stripe_head_ref)
780{
781 struct stripe_head *sh = stripe_head_ref;
782 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700783 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700784
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700785 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700786 (unsigned long long)sh->sector);
787
788 /* clear completed biofills */
789 for (i = sh->disks; i--; ) {
790 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700791
792 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700793 /* and check if we need to reply to a read request,
794 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000795 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700796 */
797 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700798 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700799
Dan Williams91c00922007-01-02 13:52:30 -0700800 BUG_ON(!dev->read);
801 rbi = dev->read;
802 dev->read = NULL;
803 while (rbi && rbi->bi_sector <
804 dev->sector + STRIPE_SECTORS) {
805 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000806 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700807 rbi->bi_next = return_bi;
808 return_bi = rbi;
809 }
Dan Williams91c00922007-01-02 13:52:30 -0700810 rbi = rbi2;
811 }
812 }
813 }
Dan Williams83de75c2008-06-28 08:31:58 +1000814 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700815
816 return_io(return_bi);
817
Dan Williamse4d84902007-09-24 10:06:13 -0700818 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700819 release_stripe(sh);
820}
821
822static void ops_run_biofill(struct stripe_head *sh)
823{
824 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700825 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700826 int i;
827
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700828 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700829 (unsigned long long)sh->sector);
830
831 for (i = sh->disks; i--; ) {
832 struct r5dev *dev = &sh->dev[i];
833 if (test_bit(R5_Wantfill, &dev->flags)) {
834 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000835 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700836 dev->read = rbi = dev->toread;
837 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000838 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700839 while (rbi && rbi->bi_sector <
840 dev->sector + STRIPE_SECTORS) {
841 tx = async_copy_data(0, rbi, dev->page,
842 dev->sector, tx);
843 rbi = r5_next_bio(rbi, dev->sector);
844 }
845 }
846 }
847
848 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700849 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
850 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700851}
852
Dan Williams4e7d2c02009-08-29 19:13:11 -0700853static void mark_target_uptodate(struct stripe_head *sh, int target)
854{
855 struct r5dev *tgt;
856
857 if (target < 0)
858 return;
859
860 tgt = &sh->dev[target];
861 set_bit(R5_UPTODATE, &tgt->flags);
862 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
863 clear_bit(R5_Wantcompute, &tgt->flags);
864}
865
Dan Williamsac6b53b2009-07-14 13:40:19 -0700866static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700867{
868 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700869
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700870 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700871 (unsigned long long)sh->sector);
872
Dan Williamsac6b53b2009-07-14 13:40:19 -0700873 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700874 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700875 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700876
Dan Williamsecc65c92008-06-28 08:31:57 +1000877 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
878 if (sh->check_state == check_state_compute_run)
879 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700880 set_bit(STRIPE_HANDLE, &sh->state);
881 release_stripe(sh);
882}
883
Dan Williamsd6f38f32009-07-14 11:50:52 -0700884/* return a pointer to the address conversion region of the scribble buffer */
885static addr_conv_t *to_addr_conv(struct stripe_head *sh,
886 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700887{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700888 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
889}
890
891static struct dma_async_tx_descriptor *
892ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
893{
Dan Williams91c00922007-01-02 13:52:30 -0700894 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700895 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700896 int target = sh->ops.target;
897 struct r5dev *tgt = &sh->dev[target];
898 struct page *xor_dest = tgt->page;
899 int count = 0;
900 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700901 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700902 int i;
903
904 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700905 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700906 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
907
908 for (i = disks; i--; )
909 if (i != target)
910 xor_srcs[count++] = sh->dev[i].page;
911
912 atomic_inc(&sh->count);
913
Dan Williams0403e382009-09-08 17:42:50 -0700914 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700915 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700916 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700917 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700918 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700919 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700920
Dan Williams91c00922007-01-02 13:52:30 -0700921 return tx;
922}
923
Dan Williamsac6b53b2009-07-14 13:40:19 -0700924/* set_syndrome_sources - populate source buffers for gen_syndrome
925 * @srcs - (struct page *) array of size sh->disks
926 * @sh - stripe_head to parse
927 *
928 * Populates srcs in proper layout order for the stripe and returns the
929 * 'count' of sources to be used in a call to async_gen_syndrome. The P
930 * destination buffer is recorded in srcs[count] and the Q destination
931 * is recorded in srcs[count+1]].
932 */
933static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
934{
935 int disks = sh->disks;
936 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
937 int d0_idx = raid6_d0(sh);
938 int count;
939 int i;
940
941 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100942 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700943
944 count = 0;
945 i = d0_idx;
946 do {
947 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
948
949 srcs[slot] = sh->dev[i].page;
950 i = raid6_next_disk(i, disks);
951 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700952
NeilBrowne4424fe2009-10-16 16:27:34 +1100953 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700954}
955
956static struct dma_async_tx_descriptor *
957ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
958{
959 int disks = sh->disks;
960 struct page **blocks = percpu->scribble;
961 int target;
962 int qd_idx = sh->qd_idx;
963 struct dma_async_tx_descriptor *tx;
964 struct async_submit_ctl submit;
965 struct r5dev *tgt;
966 struct page *dest;
967 int i;
968 int count;
969
970 if (sh->ops.target < 0)
971 target = sh->ops.target2;
972 else if (sh->ops.target2 < 0)
973 target = sh->ops.target;
974 else
975 /* we should only have one valid target */
976 BUG();
977 BUG_ON(target < 0);
978 pr_debug("%s: stripe %llu block: %d\n",
979 __func__, (unsigned long long)sh->sector, target);
980
981 tgt = &sh->dev[target];
982 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
983 dest = tgt->page;
984
985 atomic_inc(&sh->count);
986
987 if (target == qd_idx) {
988 count = set_syndrome_sources(blocks, sh);
989 blocks[count] = NULL; /* regenerating p is not necessary */
990 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700991 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
992 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700993 to_addr_conv(sh, percpu));
994 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
995 } else {
996 /* Compute any data- or p-drive using XOR */
997 count = 0;
998 for (i = disks; i-- ; ) {
999 if (i == target || i == qd_idx)
1000 continue;
1001 blocks[count++] = sh->dev[i].page;
1002 }
1003
Dan Williams0403e382009-09-08 17:42:50 -07001004 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1005 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001006 to_addr_conv(sh, percpu));
1007 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1008 }
1009
1010 return tx;
1011}
1012
1013static struct dma_async_tx_descriptor *
1014ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1015{
1016 int i, count, disks = sh->disks;
1017 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1018 int d0_idx = raid6_d0(sh);
1019 int faila = -1, failb = -1;
1020 int target = sh->ops.target;
1021 int target2 = sh->ops.target2;
1022 struct r5dev *tgt = &sh->dev[target];
1023 struct r5dev *tgt2 = &sh->dev[target2];
1024 struct dma_async_tx_descriptor *tx;
1025 struct page **blocks = percpu->scribble;
1026 struct async_submit_ctl submit;
1027
1028 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1029 __func__, (unsigned long long)sh->sector, target, target2);
1030 BUG_ON(target < 0 || target2 < 0);
1031 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1032 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1033
Dan Williams6c910a72009-09-16 12:24:54 -07001034 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001035 * slot number conversion for 'faila' and 'failb'
1036 */
1037 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001038 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001039 count = 0;
1040 i = d0_idx;
1041 do {
1042 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1043
1044 blocks[slot] = sh->dev[i].page;
1045
1046 if (i == target)
1047 faila = slot;
1048 if (i == target2)
1049 failb = slot;
1050 i = raid6_next_disk(i, disks);
1051 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001052
1053 BUG_ON(faila == failb);
1054 if (failb < faila)
1055 swap(faila, failb);
1056 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1057 __func__, (unsigned long long)sh->sector, faila, failb);
1058
1059 atomic_inc(&sh->count);
1060
1061 if (failb == syndrome_disks+1) {
1062 /* Q disk is one of the missing disks */
1063 if (faila == syndrome_disks) {
1064 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001065 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1066 ops_complete_compute, sh,
1067 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001068 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001069 STRIPE_SIZE, &submit);
1070 } else {
1071 struct page *dest;
1072 int data_target;
1073 int qd_idx = sh->qd_idx;
1074
1075 /* Missing D+Q: recompute D from P, then recompute Q */
1076 if (target == qd_idx)
1077 data_target = target2;
1078 else
1079 data_target = target;
1080
1081 count = 0;
1082 for (i = disks; i-- ; ) {
1083 if (i == data_target || i == qd_idx)
1084 continue;
1085 blocks[count++] = sh->dev[i].page;
1086 }
1087 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001088 init_async_submit(&submit,
1089 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1090 NULL, NULL, NULL,
1091 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001092 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1093 &submit);
1094
1095 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001096 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1097 ops_complete_compute, sh,
1098 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001099 return async_gen_syndrome(blocks, 0, count+2,
1100 STRIPE_SIZE, &submit);
1101 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001102 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001103 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1104 ops_complete_compute, sh,
1105 to_addr_conv(sh, percpu));
1106 if (failb == syndrome_disks) {
1107 /* We're missing D+P. */
1108 return async_raid6_datap_recov(syndrome_disks+2,
1109 STRIPE_SIZE, faila,
1110 blocks, &submit);
1111 } else {
1112 /* We're missing D+D. */
1113 return async_raid6_2data_recov(syndrome_disks+2,
1114 STRIPE_SIZE, faila, failb,
1115 blocks, &submit);
1116 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001117 }
1118}
1119
1120
Dan Williams91c00922007-01-02 13:52:30 -07001121static void ops_complete_prexor(void *stripe_head_ref)
1122{
1123 struct stripe_head *sh = stripe_head_ref;
1124
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001125 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001126 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001127}
1128
1129static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001130ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1131 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001132{
Dan Williams91c00922007-01-02 13:52:30 -07001133 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001134 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001135 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001136 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001137
1138 /* existing parity data subtracted */
1139 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1140
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001141 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001142 (unsigned long long)sh->sector);
1143
1144 for (i = disks; i--; ) {
1145 struct r5dev *dev = &sh->dev[i];
1146 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001147 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001148 xor_srcs[count++] = dev->page;
1149 }
1150
Dan Williams0403e382009-09-08 17:42:50 -07001151 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001152 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001153 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001154
1155 return tx;
1156}
1157
1158static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001159ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001160{
1161 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001162 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001163
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001164 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001165 (unsigned long long)sh->sector);
1166
1167 for (i = disks; i--; ) {
1168 struct r5dev *dev = &sh->dev[i];
1169 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001170
Dan Williamsd8ee0722008-06-28 08:32:06 +10001171 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001172 struct bio *wbi;
1173
Shaohua Lib17459c2012-07-19 16:01:31 +10001174 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001175 chosen = dev->towrite;
1176 dev->towrite = NULL;
1177 BUG_ON(dev->written);
1178 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001179 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001180
1181 while (wbi && wbi->bi_sector <
1182 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001183 if (wbi->bi_rw & REQ_FUA)
1184 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001185 if (wbi->bi_rw & REQ_SYNC)
1186 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001187 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001188 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001189 else
Shaohua Li620125f2012-10-11 13:49:05 +11001190 tx = async_copy_data(1, wbi, dev->page,
1191 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001192 wbi = r5_next_bio(wbi, dev->sector);
1193 }
1194 }
1195 }
1196
1197 return tx;
1198}
1199
Dan Williamsac6b53b2009-07-14 13:40:19 -07001200static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001201{
1202 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001203 int disks = sh->disks;
1204 int pd_idx = sh->pd_idx;
1205 int qd_idx = sh->qd_idx;
1206 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001207 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001208
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001209 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001210 (unsigned long long)sh->sector);
1211
Shaohua Libc0934f2012-05-22 13:55:05 +10001212 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001213 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001214 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001215 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001216 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001217
Dan Williams91c00922007-01-02 13:52:30 -07001218 for (i = disks; i--; ) {
1219 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001220
Tejun Heoe9c74692010-09-03 11:56:18 +02001221 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001222 if (!discard)
1223 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001224 if (fua)
1225 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001226 if (sync)
1227 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001228 }
Dan Williams91c00922007-01-02 13:52:30 -07001229 }
1230
Dan Williamsd8ee0722008-06-28 08:32:06 +10001231 if (sh->reconstruct_state == reconstruct_state_drain_run)
1232 sh->reconstruct_state = reconstruct_state_drain_result;
1233 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1234 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1235 else {
1236 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1237 sh->reconstruct_state = reconstruct_state_result;
1238 }
Dan Williams91c00922007-01-02 13:52:30 -07001239
1240 set_bit(STRIPE_HANDLE, &sh->state);
1241 release_stripe(sh);
1242}
1243
1244static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001245ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1246 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001247{
Dan Williams91c00922007-01-02 13:52:30 -07001248 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001249 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001250 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001251 int count = 0, pd_idx = sh->pd_idx, i;
1252 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001253 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001254 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001255
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001256 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001257 (unsigned long long)sh->sector);
1258
Shaohua Li620125f2012-10-11 13:49:05 +11001259 for (i = 0; i < sh->disks; i++) {
1260 if (pd_idx == i)
1261 continue;
1262 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1263 break;
1264 }
1265 if (i >= sh->disks) {
1266 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001267 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1268 ops_complete_reconstruct(sh);
1269 return;
1270 }
Dan Williams91c00922007-01-02 13:52:30 -07001271 /* check if prexor is active which means only process blocks
1272 * that are part of a read-modify-write (written)
1273 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001274 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1275 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001276 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1277 for (i = disks; i--; ) {
1278 struct r5dev *dev = &sh->dev[i];
1279 if (dev->written)
1280 xor_srcs[count++] = dev->page;
1281 }
1282 } else {
1283 xor_dest = sh->dev[pd_idx].page;
1284 for (i = disks; i--; ) {
1285 struct r5dev *dev = &sh->dev[i];
1286 if (i != pd_idx)
1287 xor_srcs[count++] = dev->page;
1288 }
1289 }
1290
Dan Williams91c00922007-01-02 13:52:30 -07001291 /* 1/ if we prexor'd then the dest is reused as a source
1292 * 2/ if we did not prexor then we are redoing the parity
1293 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1294 * for the synchronous xor case
1295 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001296 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001297 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1298
1299 atomic_inc(&sh->count);
1300
Dan Williamsac6b53b2009-07-14 13:40:19 -07001301 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001302 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001303 if (unlikely(count == 1))
1304 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1305 else
1306 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001307}
1308
Dan Williamsac6b53b2009-07-14 13:40:19 -07001309static void
1310ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1311 struct dma_async_tx_descriptor *tx)
1312{
1313 struct async_submit_ctl submit;
1314 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001315 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001316
1317 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1318
Shaohua Li620125f2012-10-11 13:49:05 +11001319 for (i = 0; i < sh->disks; i++) {
1320 if (sh->pd_idx == i || sh->qd_idx == i)
1321 continue;
1322 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1323 break;
1324 }
1325 if (i >= sh->disks) {
1326 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001327 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1328 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1329 ops_complete_reconstruct(sh);
1330 return;
1331 }
1332
Dan Williamsac6b53b2009-07-14 13:40:19 -07001333 count = set_syndrome_sources(blocks, sh);
1334
1335 atomic_inc(&sh->count);
1336
1337 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1338 sh, to_addr_conv(sh, percpu));
1339 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001340}
1341
1342static void ops_complete_check(void *stripe_head_ref)
1343{
1344 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001345
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001346 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001347 (unsigned long long)sh->sector);
1348
Dan Williamsecc65c92008-06-28 08:31:57 +10001349 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001350 set_bit(STRIPE_HANDLE, &sh->state);
1351 release_stripe(sh);
1352}
1353
Dan Williamsac6b53b2009-07-14 13:40:19 -07001354static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001355{
Dan Williams91c00922007-01-02 13:52:30 -07001356 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001357 int pd_idx = sh->pd_idx;
1358 int qd_idx = sh->qd_idx;
1359 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001360 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001361 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001362 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001363 int count;
1364 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001365
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001366 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001367 (unsigned long long)sh->sector);
1368
Dan Williamsac6b53b2009-07-14 13:40:19 -07001369 count = 0;
1370 xor_dest = sh->dev[pd_idx].page;
1371 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001372 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001373 if (i == pd_idx || i == qd_idx)
1374 continue;
1375 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001376 }
1377
Dan Williamsd6f38f32009-07-14 11:50:52 -07001378 init_async_submit(&submit, 0, NULL, NULL, NULL,
1379 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001380 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001381 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001382
Dan Williams91c00922007-01-02 13:52:30 -07001383 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001384 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1385 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001386}
1387
Dan Williamsac6b53b2009-07-14 13:40:19 -07001388static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1389{
1390 struct page **srcs = percpu->scribble;
1391 struct async_submit_ctl submit;
1392 int count;
1393
1394 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1395 (unsigned long long)sh->sector, checkp);
1396
1397 count = set_syndrome_sources(srcs, sh);
1398 if (!checkp)
1399 srcs[count] = NULL;
1400
1401 atomic_inc(&sh->count);
1402 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1403 sh, to_addr_conv(sh, percpu));
1404 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1405 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1406}
1407
Dan Williams417b8d42009-10-16 16:25:22 +11001408static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001409{
1410 int overlap_clear = 0, i, disks = sh->disks;
1411 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001412 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001413 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001414 struct raid5_percpu *percpu;
1415 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001416
Dan Williamsd6f38f32009-07-14 11:50:52 -07001417 cpu = get_cpu();
1418 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001419 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001420 ops_run_biofill(sh);
1421 overlap_clear++;
1422 }
1423
Dan Williams7b3a8712008-06-28 08:32:09 +10001424 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001425 if (level < 6)
1426 tx = ops_run_compute5(sh, percpu);
1427 else {
1428 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1429 tx = ops_run_compute6_1(sh, percpu);
1430 else
1431 tx = ops_run_compute6_2(sh, percpu);
1432 }
1433 /* terminate the chain if reconstruct is not set to be run */
1434 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001435 async_tx_ack(tx);
1436 }
Dan Williams91c00922007-01-02 13:52:30 -07001437
Dan Williams600aa102008-06-28 08:32:05 +10001438 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001439 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001440
Dan Williams600aa102008-06-28 08:32:05 +10001441 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001442 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001443 overlap_clear++;
1444 }
1445
Dan Williamsac6b53b2009-07-14 13:40:19 -07001446 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1447 if (level < 6)
1448 ops_run_reconstruct5(sh, percpu, tx);
1449 else
1450 ops_run_reconstruct6(sh, percpu, tx);
1451 }
Dan Williams91c00922007-01-02 13:52:30 -07001452
Dan Williamsac6b53b2009-07-14 13:40:19 -07001453 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1454 if (sh->check_state == check_state_run)
1455 ops_run_check_p(sh, percpu);
1456 else if (sh->check_state == check_state_run_q)
1457 ops_run_check_pq(sh, percpu, 0);
1458 else if (sh->check_state == check_state_run_pq)
1459 ops_run_check_pq(sh, percpu, 1);
1460 else
1461 BUG();
1462 }
Dan Williams91c00922007-01-02 13:52:30 -07001463
Dan Williams91c00922007-01-02 13:52:30 -07001464 if (overlap_clear)
1465 for (i = disks; i--; ) {
1466 struct r5dev *dev = &sh->dev[i];
1467 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1468 wake_up(&sh->raid_conf->wait_for_overlap);
1469 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001470 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001471}
1472
Dan Williams417b8d42009-10-16 16:25:22 +11001473#ifdef CONFIG_MULTICORE_RAID456
1474static void async_run_ops(void *param, async_cookie_t cookie)
1475{
1476 struct stripe_head *sh = param;
1477 unsigned long ops_request = sh->ops.request;
1478
1479 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1480 wake_up(&sh->ops.wait_for_ops);
1481
1482 __raid_run_ops(sh, ops_request);
1483 release_stripe(sh);
1484}
1485
1486static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1487{
1488 /* since handle_stripe can be called outside of raid5d context
1489 * we need to ensure sh->ops.request is de-staged before another
1490 * request arrives
1491 */
1492 wait_event(sh->ops.wait_for_ops,
1493 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1494 sh->ops.request = ops_request;
1495
1496 atomic_inc(&sh->count);
1497 async_schedule(async_run_ops, sh);
1498}
1499#else
1500#define raid_run_ops __raid_run_ops
1501#endif
1502
NeilBrownd1688a62011-10-11 16:49:52 +11001503static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001506 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001507 if (!sh)
1508 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001509
NeilBrown3f294f42005-11-08 21:39:25 -08001510 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001511 #ifdef CONFIG_MULTICORE_RAID456
1512 init_waitqueue_head(&sh->ops.wait_for_ops);
1513 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001514
Shaohua Lib17459c2012-07-19 16:01:31 +10001515 spin_lock_init(&sh->stripe_lock);
1516
NeilBrowne4e11e32010-06-16 16:45:16 +10001517 if (grow_buffers(sh)) {
1518 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001519 kmem_cache_free(conf->slab_cache, sh);
1520 return 0;
1521 }
1522 /* we just created an active stripe so... */
1523 atomic_set(&sh->count, 1);
1524 atomic_inc(&conf->active_stripes);
1525 INIT_LIST_HEAD(&sh->lru);
1526 release_stripe(sh);
1527 return 1;
1528}
1529
NeilBrownd1688a62011-10-11 16:49:52 +11001530static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001531{
Christoph Lametere18b8902006-12-06 20:33:20 -08001532 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001533 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
NeilBrownf4be6b42010-06-01 19:37:25 +10001535 if (conf->mddev->gendisk)
1536 sprintf(conf->cache_name[0],
1537 "raid%d-%s", conf->level, mdname(conf->mddev));
1538 else
1539 sprintf(conf->cache_name[0],
1540 "raid%d-%p", conf->level, conf->mddev);
1541 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1542
NeilBrownad01c9e2006-03-27 01:18:07 -08001543 conf->active_name = 0;
1544 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001546 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (!sc)
1548 return 1;
1549 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001550 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001551 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001552 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return 0;
1555}
NeilBrown29269552006-03-27 01:18:10 -08001556
Dan Williamsd6f38f32009-07-14 11:50:52 -07001557/**
1558 * scribble_len - return the required size of the scribble region
1559 * @num - total number of disks in the array
1560 *
1561 * The size must be enough to contain:
1562 * 1/ a struct page pointer for each device in the array +2
1563 * 2/ room to convert each entry in (1) to its corresponding dma
1564 * (dma_map_page()) or page (page_address()) address.
1565 *
1566 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1567 * calculate over all devices (not just the data blocks), using zeros in place
1568 * of the P and Q blocks.
1569 */
1570static size_t scribble_len(int num)
1571{
1572 size_t len;
1573
1574 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1575
1576 return len;
1577}
1578
NeilBrownd1688a62011-10-11 16:49:52 +11001579static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001580{
1581 /* Make all the stripes able to hold 'newsize' devices.
1582 * New slots in each stripe get 'page' set to a new page.
1583 *
1584 * This happens in stages:
1585 * 1/ create a new kmem_cache and allocate the required number of
1586 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09001587 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08001588 * to the new stripe_heads. This will have the side effect of
1589 * freezing the array as once all stripe_heads have been collected,
1590 * no IO will be possible. Old stripe heads are freed once their
1591 * pages have been transferred over, and the old kmem_cache is
1592 * freed when all stripes are done.
1593 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1594 * we simple return a failre status - no need to clean anything up.
1595 * 4/ allocate new pages for the new slots in the new stripe_heads.
1596 * If this fails, we don't bother trying the shrink the
1597 * stripe_heads down again, we just leave them as they are.
1598 * As each stripe_head is processed the new one is released into
1599 * active service.
1600 *
1601 * Once step2 is started, we cannot afford to wait for a write,
1602 * so we use GFP_NOIO allocations.
1603 */
1604 struct stripe_head *osh, *nsh;
1605 LIST_HEAD(newstripes);
1606 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001607 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001608 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001609 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001610 int i;
1611
1612 if (newsize <= conf->pool_size)
1613 return 0; /* never bother to shrink */
1614
Dan Williamsb5470dc2008-06-27 21:44:04 -07001615 err = md_allow_write(conf->mddev);
1616 if (err)
1617 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001618
NeilBrownad01c9e2006-03-27 01:18:07 -08001619 /* Step 1 */
1620 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1621 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001622 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001623 if (!sc)
1624 return -ENOMEM;
1625
1626 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001627 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001628 if (!nsh)
1629 break;
1630
NeilBrownad01c9e2006-03-27 01:18:07 -08001631 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001632 #ifdef CONFIG_MULTICORE_RAID456
1633 init_waitqueue_head(&nsh->ops.wait_for_ops);
1634 #endif
NeilBrowncb13ff62012-09-24 16:27:20 +10001635 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001636
1637 list_add(&nsh->lru, &newstripes);
1638 }
1639 if (i) {
1640 /* didn't get enough, give up */
1641 while (!list_empty(&newstripes)) {
1642 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1643 list_del(&nsh->lru);
1644 kmem_cache_free(sc, nsh);
1645 }
1646 kmem_cache_destroy(sc);
1647 return -ENOMEM;
1648 }
1649 /* Step 2 - Must use GFP_NOIO now.
1650 * OK, we have enough stripes, start collecting inactive
1651 * stripes and copying them over
1652 */
1653 list_for_each_entry(nsh, &newstripes, lru) {
1654 spin_lock_irq(&conf->device_lock);
1655 wait_event_lock_irq(conf->wait_for_stripe,
1656 !list_empty(&conf->inactive_list),
Lukas Czernereed8c022012-11-30 11:42:40 +01001657 conf->device_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001658 osh = get_free_stripe(conf);
1659 spin_unlock_irq(&conf->device_lock);
1660 atomic_set(&nsh->count, 1);
1661 for(i=0; i<conf->pool_size; i++)
1662 nsh->dev[i].page = osh->dev[i].page;
1663 for( ; i<newsize; i++)
1664 nsh->dev[i].page = NULL;
1665 kmem_cache_free(conf->slab_cache, osh);
1666 }
1667 kmem_cache_destroy(conf->slab_cache);
1668
1669 /* Step 3.
1670 * At this point, we are holding all the stripes so the array
1671 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001672 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001673 */
1674 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1675 if (ndisks) {
1676 for (i=0; i<conf->raid_disks; i++)
1677 ndisks[i] = conf->disks[i];
1678 kfree(conf->disks);
1679 conf->disks = ndisks;
1680 } else
1681 err = -ENOMEM;
1682
Dan Williamsd6f38f32009-07-14 11:50:52 -07001683 get_online_cpus();
1684 conf->scribble_len = scribble_len(newsize);
1685 for_each_present_cpu(cpu) {
1686 struct raid5_percpu *percpu;
1687 void *scribble;
1688
1689 percpu = per_cpu_ptr(conf->percpu, cpu);
1690 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1691
1692 if (scribble) {
1693 kfree(percpu->scribble);
1694 percpu->scribble = scribble;
1695 } else {
1696 err = -ENOMEM;
1697 break;
1698 }
1699 }
1700 put_online_cpus();
1701
NeilBrownad01c9e2006-03-27 01:18:07 -08001702 /* Step 4, return new stripes to service */
1703 while(!list_empty(&newstripes)) {
1704 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1705 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001706
NeilBrownad01c9e2006-03-27 01:18:07 -08001707 for (i=conf->raid_disks; i < newsize; i++)
1708 if (nsh->dev[i].page == NULL) {
1709 struct page *p = alloc_page(GFP_NOIO);
1710 nsh->dev[i].page = p;
1711 if (!p)
1712 err = -ENOMEM;
1713 }
1714 release_stripe(nsh);
1715 }
1716 /* critical section pass, GFP_NOIO no longer needed */
1717
1718 conf->slab_cache = sc;
1719 conf->active_name = 1-conf->active_name;
1720 conf->pool_size = newsize;
1721 return err;
1722}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
NeilBrownd1688a62011-10-11 16:49:52 +11001724static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
1726 struct stripe_head *sh;
1727
NeilBrown3f294f42005-11-08 21:39:25 -08001728 spin_lock_irq(&conf->device_lock);
1729 sh = get_free_stripe(conf);
1730 spin_unlock_irq(&conf->device_lock);
1731 if (!sh)
1732 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001733 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001734 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001735 kmem_cache_free(conf->slab_cache, sh);
1736 atomic_dec(&conf->active_stripes);
1737 return 1;
1738}
1739
NeilBrownd1688a62011-10-11 16:49:52 +11001740static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001741{
1742 while (drop_one_stripe(conf))
1743 ;
1744
NeilBrown29fc7e32006-02-03 03:03:41 -08001745 if (conf->slab_cache)
1746 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 conf->slab_cache = NULL;
1748}
1749
NeilBrown6712ecf2007-09-27 12:47:43 +02001750static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
NeilBrown99c0fb52009-03-31 14:39:38 +11001752 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001753 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001754 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001756 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001757 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001758 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 for (i=0 ; i<disks; i++)
1761 if (bi == &sh->dev[i].req)
1762 break;
1763
Dan Williams45b42332007-07-09 11:56:43 -07001764 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1765 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 uptodate);
1767 if (i == disks) {
1768 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001769 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
NeilBrown14a75d32011-12-23 10:17:52 +11001771 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001772 /* If replacement finished while this request was outstanding,
1773 * 'replacement' might be NULL already.
1774 * In that case it moved down to 'rdev'.
1775 * rdev is not removed until all requests are finished.
1776 */
NeilBrown14a75d32011-12-23 10:17:52 +11001777 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001778 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001779 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
NeilBrown05616be2012-05-21 09:27:00 +10001781 if (use_new_offset(conf, sh))
1782 s = sh->sector + rdev->new_data_offset;
1783 else
1784 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001787 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001788 /* Note that this cannot happen on a
1789 * replacement device. We just fail those on
1790 * any error
1791 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001792 printk_ratelimited(
1793 KERN_INFO
1794 "md/raid:%s: read error corrected"
1795 " (%lu sectors at %llu on %s)\n",
1796 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001797 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001798 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001799 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001800 clear_bit(R5_ReadError, &sh->dev[i].flags);
1801 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001802 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1803 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1804
NeilBrown14a75d32011-12-23 10:17:52 +11001805 if (atomic_read(&rdev->read_errors))
1806 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001808 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001809 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001810 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001813 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001814 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1815 printk_ratelimited(
1816 KERN_WARNING
1817 "md/raid:%s: read error on replacement device "
1818 "(sector %llu on %s).\n",
1819 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001820 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001821 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001822 else if (conf->mddev->degraded >= conf->max_degraded) {
1823 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001824 printk_ratelimited(
1825 KERN_WARNING
1826 "md/raid:%s: read error not correctable "
1827 "(sector %llu on %s).\n",
1828 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001829 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001830 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001831 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001832 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001833 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001834 printk_ratelimited(
1835 KERN_WARNING
1836 "md/raid:%s: read error NOT corrected!! "
1837 "(sector %llu on %s).\n",
1838 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001839 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001840 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001841 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001842 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001843 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001844 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001845 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001846 else
1847 retry = 1;
1848 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001849 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1850 set_bit(R5_ReadError, &sh->dev[i].flags);
1851 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1852 } else
1853 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001854 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001855 clear_bit(R5_ReadError, &sh->dev[i].flags);
1856 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001857 if (!(set_bad
1858 && test_bit(In_sync, &rdev->flags)
1859 && rdev_set_badblocks(
1860 rdev, sh->sector, STRIPE_SECTORS, 0)))
1861 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
NeilBrown14a75d32011-12-23 10:17:52 +11001864 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1866 set_bit(STRIPE_HANDLE, &sh->state);
1867 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
NeilBrownd710e132008-10-13 11:55:12 +11001870static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
NeilBrown99c0fb52009-03-31 14:39:38 +11001872 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001873 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001874 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001875 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001877 sector_t first_bad;
1878 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001879 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
NeilBrown977df362011-12-23 10:17:53 +11001881 for (i = 0 ; i < disks; i++) {
1882 if (bi == &sh->dev[i].req) {
1883 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 break;
NeilBrown977df362011-12-23 10:17:53 +11001885 }
1886 if (bi == &sh->dev[i].rreq) {
1887 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001888 if (rdev)
1889 replacement = 1;
1890 else
1891 /* rdev was removed and 'replacement'
1892 * replaced it. rdev is not removed
1893 * until all requests are finished.
1894 */
1895 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001896 break;
1897 }
1898 }
Dan Williams45b42332007-07-09 11:56:43 -07001899 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1901 uptodate);
1902 if (i == disks) {
1903 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001904 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
1906
NeilBrown977df362011-12-23 10:17:53 +11001907 if (replacement) {
1908 if (!uptodate)
1909 md_error(conf->mddev, rdev);
1910 else if (is_badblock(rdev, sh->sector,
1911 STRIPE_SECTORS,
1912 &first_bad, &bad_sectors))
1913 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1914 } else {
1915 if (!uptodate) {
1916 set_bit(WriteErrorSeen, &rdev->flags);
1917 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001918 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1919 set_bit(MD_RECOVERY_NEEDED,
1920 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001921 } else if (is_badblock(rdev, sh->sector,
1922 STRIPE_SECTORS,
1923 &first_bad, &bad_sectors))
1924 set_bit(R5_MadeGood, &sh->dev[i].flags);
1925 }
1926 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
NeilBrown977df362011-12-23 10:17:53 +11001928 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1929 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001931 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
NeilBrown784052e2009-03-31 15:19:07 +11001934static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
NeilBrown784052e2009-03-31 15:19:07 +11001936static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937{
1938 struct r5dev *dev = &sh->dev[i];
1939
1940 bio_init(&dev->req);
1941 dev->req.bi_io_vec = &dev->vec;
1942 dev->req.bi_vcnt++;
1943 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001945 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
NeilBrown977df362011-12-23 10:17:53 +11001947 bio_init(&dev->rreq);
1948 dev->rreq.bi_io_vec = &dev->rvec;
1949 dev->rreq.bi_vcnt++;
1950 dev->rreq.bi_max_vecs++;
1951 dev->rreq.bi_private = sh;
1952 dev->rvec.bv_page = dev->page;
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001955 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
NeilBrownfd01b882011-10-11 16:47:53 +11001958static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
1960 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001961 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001962 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001963 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
NeilBrown908f4fb2011-12-23 10:17:50 +11001965 spin_lock_irqsave(&conf->device_lock, flags);
1966 clear_bit(In_sync, &rdev->flags);
1967 mddev->degraded = calc_degraded(conf);
1968 spin_unlock_irqrestore(&conf->device_lock, flags);
1969 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1970
NeilBrownde393cd2011-07-28 11:31:48 +10001971 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001972 set_bit(Faulty, &rdev->flags);
1973 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1974 printk(KERN_ALERT
1975 "md/raid:%s: Disk failure on %s, disabling device.\n"
1976 "md/raid:%s: Operation continuing on %d devices.\n",
1977 mdname(mddev),
1978 bdevname(rdev->bdev, b),
1979 mdname(mddev),
1980 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001981}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983/*
1984 * Input: a 'big' sector number,
1985 * Output: index of the data and parity disk, and the sector # in them.
1986 */
NeilBrownd1688a62011-10-11 16:49:52 +11001987static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001988 int previous, int *dd_idx,
1989 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001991 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001992 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001994 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001995 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001997 int algorithm = previous ? conf->prev_algo
1998 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001999 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2000 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002001 int raid_disks = previous ? conf->previous_raid_disks
2002 : conf->raid_disks;
2003 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 /* First compute the information on this sector */
2006
2007 /*
2008 * Compute the chunk number and the sector offset inside the chunk
2009 */
2010 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2011 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 /*
2014 * Compute the stripe number
2015 */
NeilBrown35f2a592010-04-20 14:13:34 +10002016 stripe = chunk_number;
2017 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002018 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 /*
2020 * Select the parity disk based on the user selected algorithm.
2021 */
NeilBrown84789552011-07-27 11:00:36 +10002022 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002023 switch(conf->level) {
2024 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002025 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002026 break;
2027 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002028 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002030 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002031 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 (*dd_idx)++;
2033 break;
2034 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002035 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002036 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 (*dd_idx)++;
2038 break;
2039 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002040 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002041 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 break;
2043 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002044 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002045 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002047 case ALGORITHM_PARITY_0:
2048 pd_idx = 0;
2049 (*dd_idx)++;
2050 break;
2051 case ALGORITHM_PARITY_N:
2052 pd_idx = data_disks;
2053 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002055 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002056 }
2057 break;
2058 case 6:
2059
NeilBrowne183eae2009-03-31 15:20:22 +11002060 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002061 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002062 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002063 qd_idx = pd_idx + 1;
2064 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002065 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002066 qd_idx = 0;
2067 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002068 (*dd_idx) += 2; /* D D P Q D */
2069 break;
2070 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002071 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002072 qd_idx = pd_idx + 1;
2073 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002074 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002075 qd_idx = 0;
2076 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002077 (*dd_idx) += 2; /* D D P Q D */
2078 break;
2079 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002080 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002081 qd_idx = (pd_idx + 1) % raid_disks;
2082 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002083 break;
2084 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002085 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002086 qd_idx = (pd_idx + 1) % raid_disks;
2087 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002088 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002089
2090 case ALGORITHM_PARITY_0:
2091 pd_idx = 0;
2092 qd_idx = 1;
2093 (*dd_idx) += 2;
2094 break;
2095 case ALGORITHM_PARITY_N:
2096 pd_idx = data_disks;
2097 qd_idx = data_disks + 1;
2098 break;
2099
2100 case ALGORITHM_ROTATING_ZERO_RESTART:
2101 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2102 * of blocks for computing Q is different.
2103 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002104 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002105 qd_idx = pd_idx + 1;
2106 if (pd_idx == raid_disks-1) {
2107 (*dd_idx)++; /* Q D D D P */
2108 qd_idx = 0;
2109 } else if (*dd_idx >= pd_idx)
2110 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002111 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002112 break;
2113
2114 case ALGORITHM_ROTATING_N_RESTART:
2115 /* Same a left_asymmetric, by first stripe is
2116 * D D D P Q rather than
2117 * Q D D D P
2118 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002119 stripe2 += 1;
2120 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002121 qd_idx = pd_idx + 1;
2122 if (pd_idx == raid_disks-1) {
2123 (*dd_idx)++; /* Q D D D P */
2124 qd_idx = 0;
2125 } else if (*dd_idx >= pd_idx)
2126 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002127 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002128 break;
2129
2130 case ALGORITHM_ROTATING_N_CONTINUE:
2131 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002132 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002133 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2134 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002135 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002136 break;
2137
2138 case ALGORITHM_LEFT_ASYMMETRIC_6:
2139 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002140 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002141 if (*dd_idx >= pd_idx)
2142 (*dd_idx)++;
2143 qd_idx = raid_disks - 1;
2144 break;
2145
2146 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002147 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002148 if (*dd_idx >= pd_idx)
2149 (*dd_idx)++;
2150 qd_idx = raid_disks - 1;
2151 break;
2152
2153 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002154 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002155 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2156 qd_idx = raid_disks - 1;
2157 break;
2158
2159 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002160 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002161 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2162 qd_idx = raid_disks - 1;
2163 break;
2164
2165 case ALGORITHM_PARITY_0_6:
2166 pd_idx = 0;
2167 (*dd_idx)++;
2168 qd_idx = raid_disks - 1;
2169 break;
2170
NeilBrown16a53ec2006-06-26 00:27:38 -07002171 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002172 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002173 }
2174 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 }
2176
NeilBrown911d4ee2009-03-31 14:39:38 +11002177 if (sh) {
2178 sh->pd_idx = pd_idx;
2179 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002180 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 /*
2183 * Finally, compute the new sector number
2184 */
2185 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2186 return new_sector;
2187}
2188
2189
NeilBrown784052e2009-03-31 15:19:07 +11002190static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
NeilBrownd1688a62011-10-11 16:49:52 +11002192 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002193 int raid_disks = sh->disks;
2194 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002196 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2197 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002198 int algorithm = previous ? conf->prev_algo
2199 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 sector_t stripe;
2201 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002202 sector_t chunk_number;
2203 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002205 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
NeilBrown16a53ec2006-06-26 00:27:38 -07002207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2209 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
NeilBrown16a53ec2006-06-26 00:27:38 -07002211 if (i == sh->pd_idx)
2212 return 0;
2213 switch(conf->level) {
2214 case 4: break;
2215 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002216 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 case ALGORITHM_LEFT_ASYMMETRIC:
2218 case ALGORITHM_RIGHT_ASYMMETRIC:
2219 if (i > sh->pd_idx)
2220 i--;
2221 break;
2222 case ALGORITHM_LEFT_SYMMETRIC:
2223 case ALGORITHM_RIGHT_SYMMETRIC:
2224 if (i < sh->pd_idx)
2225 i += raid_disks;
2226 i -= (sh->pd_idx + 1);
2227 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002228 case ALGORITHM_PARITY_0:
2229 i -= 1;
2230 break;
2231 case ALGORITHM_PARITY_N:
2232 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002234 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002235 }
2236 break;
2237 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002238 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002239 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002240 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002241 case ALGORITHM_LEFT_ASYMMETRIC:
2242 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002243 case ALGORITHM_ROTATING_ZERO_RESTART:
2244 case ALGORITHM_ROTATING_N_RESTART:
2245 if (sh->pd_idx == raid_disks-1)
2246 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002247 else if (i > sh->pd_idx)
2248 i -= 2; /* D D P Q D */
2249 break;
2250 case ALGORITHM_LEFT_SYMMETRIC:
2251 case ALGORITHM_RIGHT_SYMMETRIC:
2252 if (sh->pd_idx == raid_disks-1)
2253 i--; /* Q D D D P */
2254 else {
2255 /* D D P Q D */
2256 if (i < sh->pd_idx)
2257 i += raid_disks;
2258 i -= (sh->pd_idx + 2);
2259 }
2260 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002261 case ALGORITHM_PARITY_0:
2262 i -= 2;
2263 break;
2264 case ALGORITHM_PARITY_N:
2265 break;
2266 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002267 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002268 if (sh->pd_idx == 0)
2269 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002270 else {
2271 /* D D Q P D */
2272 if (i < sh->pd_idx)
2273 i += raid_disks;
2274 i -= (sh->pd_idx + 1);
2275 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002276 break;
2277 case ALGORITHM_LEFT_ASYMMETRIC_6:
2278 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2279 if (i > sh->pd_idx)
2280 i--;
2281 break;
2282 case ALGORITHM_LEFT_SYMMETRIC_6:
2283 case ALGORITHM_RIGHT_SYMMETRIC_6:
2284 if (i < sh->pd_idx)
2285 i += data_disks + 1;
2286 i -= (sh->pd_idx + 1);
2287 break;
2288 case ALGORITHM_PARITY_0_6:
2289 i -= 1;
2290 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002291 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002292 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002293 }
2294 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 }
2296
2297 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002298 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
NeilBrown112bf892009-03-31 14:39:38 +11002300 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002301 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002302 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2303 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002304 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2305 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 return 0;
2307 }
2308 return r_sector;
2309}
2310
2311
Dan Williams600aa102008-06-28 08:32:05 +10002312static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002313schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002314 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002315{
2316 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002317 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002318 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002319
Dan Williamse33129d2007-01-02 13:52:30 -07002320 if (rcw) {
2321 /* if we are not expanding this is a proper write request, and
2322 * there will be bios with new data to be drained into the
2323 * stripe cache
2324 */
2325 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002326 sh->reconstruct_state = reconstruct_state_drain_run;
2327 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2328 } else
2329 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002330
Dan Williamsac6b53b2009-07-14 13:40:19 -07002331 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002332
2333 for (i = disks; i--; ) {
2334 struct r5dev *dev = &sh->dev[i];
2335
2336 if (dev->towrite) {
2337 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002338 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002339 if (!expand)
2340 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002341 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002342 }
2343 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002344 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002345 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002346 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002347 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002348 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002349 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2350 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2351
Dan Williamsd8ee0722008-06-28 08:32:06 +10002352 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002353 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2354 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002355 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002356
2357 for (i = disks; i--; ) {
2358 struct r5dev *dev = &sh->dev[i];
2359 if (i == pd_idx)
2360 continue;
2361
Dan Williamse33129d2007-01-02 13:52:30 -07002362 if (dev->towrite &&
2363 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002364 test_bit(R5_Wantcompute, &dev->flags))) {
2365 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002366 set_bit(R5_LOCKED, &dev->flags);
2367 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002368 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002369 }
2370 }
2371 }
2372
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002373 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002374 * are in flight
2375 */
2376 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2377 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002378 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002379
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002380 if (level == 6) {
2381 int qd_idx = sh->qd_idx;
2382 struct r5dev *dev = &sh->dev[qd_idx];
2383
2384 set_bit(R5_LOCKED, &dev->flags);
2385 clear_bit(R5_UPTODATE, &dev->flags);
2386 s->locked++;
2387 }
2388
Dan Williams600aa102008-06-28 08:32:05 +10002389 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002390 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002391 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002392}
NeilBrown16a53ec2006-06-26 00:27:38 -07002393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394/*
2395 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002396 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 * The bi_next chain must be in order.
2398 */
2399static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2400{
2401 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002402 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002403 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
NeilBrowncbe47ec2011-07-26 11:20:35 +10002405 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 (unsigned long long)bi->bi_sector,
2407 (unsigned long long)sh->sector);
2408
Shaohua Lib17459c2012-07-19 16:01:31 +10002409 /*
2410 * If several bio share a stripe. The bio bi_phys_segments acts as a
2411 * reference count to avoid race. The reference count should already be
2412 * increased before this function is called (for example, in
2413 * make_request()), so other bio sharing this stripe will not free the
2414 * stripe. If a stripe is owned by one stripe, the stripe lock will
2415 * protect it.
2416 */
2417 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002418 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002420 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002421 firstwrite = 1;
2422 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 bip = &sh->dev[dd_idx].toread;
2424 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2425 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2426 goto overlap;
2427 bip = & (*bip)->bi_next;
2428 }
2429 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2430 goto overlap;
2431
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002432 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if (*bip)
2434 bi->bi_next = *bip;
2435 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002436 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 if (forwrite) {
2439 /* check if page is covered */
2440 sector_t sector = sh->dev[dd_idx].sector;
2441 for (bi=sh->dev[dd_idx].towrite;
2442 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2443 bi && bi->bi_sector <= sector;
2444 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2445 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2446 sector = bi->bi_sector + (bi->bi_size>>9);
2447 }
2448 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2449 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2450 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002451
2452 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2453 (unsigned long long)(*bip)->bi_sector,
2454 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002455 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002456
2457 if (conf->mddev->bitmap && firstwrite) {
2458 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2459 STRIPE_SECTORS, 0);
2460 sh->bm_seq = conf->seq_flush+1;
2461 set_bit(STRIPE_BIT_DELAY, &sh->state);
2462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return 1;
2464
2465 overlap:
2466 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002467 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 return 0;
2469}
2470
NeilBrownd1688a62011-10-11 16:49:52 +11002471static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002472
NeilBrownd1688a62011-10-11 16:49:52 +11002473static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002474 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002475{
NeilBrown784052e2009-03-31 15:19:07 +11002476 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002477 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002478 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002479 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002480 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002481
NeilBrown112bf892009-03-31 14:39:38 +11002482 raid5_compute_sector(conf,
2483 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002484 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002485 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002486 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002487}
2488
Dan Williamsa4456852007-07-09 11:56:43 -07002489static void
NeilBrownd1688a62011-10-11 16:49:52 +11002490handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002491 struct stripe_head_state *s, int disks,
2492 struct bio **return_bi)
2493{
2494 int i;
2495 for (i = disks; i--; ) {
2496 struct bio *bi;
2497 int bitmap_end = 0;
2498
2499 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002500 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002501 rcu_read_lock();
2502 rdev = rcu_dereference(conf->disks[i].rdev);
2503 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002504 atomic_inc(&rdev->nr_pending);
2505 else
2506 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002507 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002508 if (rdev) {
2509 if (!rdev_set_badblocks(
2510 rdev,
2511 sh->sector,
2512 STRIPE_SECTORS, 0))
2513 md_error(conf->mddev, rdev);
2514 rdev_dec_pending(rdev, conf->mddev);
2515 }
Dan Williamsa4456852007-07-09 11:56:43 -07002516 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002517 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002518 /* fail all writes first */
2519 bi = sh->dev[i].towrite;
2520 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002521 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002522 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002523 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002524
2525 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2526 wake_up(&conf->wait_for_overlap);
2527
2528 while (bi && bi->bi_sector <
2529 sh->dev[i].sector + STRIPE_SECTORS) {
2530 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2531 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002532 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002533 md_write_end(conf->mddev);
2534 bi->bi_next = *return_bi;
2535 *return_bi = bi;
2536 }
2537 bi = nextbi;
2538 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002539 if (bitmap_end)
2540 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2541 STRIPE_SECTORS, 0, 0);
2542 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002543 /* and fail all 'written' */
2544 bi = sh->dev[i].written;
2545 sh->dev[i].written = NULL;
2546 if (bi) bitmap_end = 1;
2547 while (bi && bi->bi_sector <
2548 sh->dev[i].sector + STRIPE_SECTORS) {
2549 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2550 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002551 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002552 md_write_end(conf->mddev);
2553 bi->bi_next = *return_bi;
2554 *return_bi = bi;
2555 }
2556 bi = bi2;
2557 }
2558
Dan Williamsb5e98d62007-01-02 13:52:31 -07002559 /* fail any reads if this device is non-operational and
2560 * the data has not reached the cache yet.
2561 */
2562 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2563 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2564 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002565 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002566 bi = sh->dev[i].toread;
2567 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002568 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002569 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2570 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002571 while (bi && bi->bi_sector <
2572 sh->dev[i].sector + STRIPE_SECTORS) {
2573 struct bio *nextbi =
2574 r5_next_bio(bi, sh->dev[i].sector);
2575 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002576 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002577 bi->bi_next = *return_bi;
2578 *return_bi = bi;
2579 }
2580 bi = nextbi;
2581 }
2582 }
Dan Williamsa4456852007-07-09 11:56:43 -07002583 if (bitmap_end)
2584 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2585 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002586 /* If we were in the middle of a write the parity block might
2587 * still be locked - so just clear all R5_LOCKED flags
2588 */
2589 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002590 }
2591
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002592 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2593 if (atomic_dec_and_test(&conf->pending_full_writes))
2594 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002595}
2596
NeilBrown7f0da592011-07-28 11:39:22 +10002597static void
NeilBrownd1688a62011-10-11 16:49:52 +11002598handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002599 struct stripe_head_state *s)
2600{
2601 int abort = 0;
2602 int i;
2603
NeilBrown7f0da592011-07-28 11:39:22 +10002604 clear_bit(STRIPE_SYNCING, &sh->state);
2605 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002606 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002607 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002608 * Don't even need to abort as that is handled elsewhere
2609 * if needed, and not always wanted e.g. if there is a known
2610 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002611 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002612 * non-sync devices, or abort the recovery
2613 */
NeilBrown18b98372012-04-01 23:48:38 +10002614 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2615 /* During recovery devices cannot be removed, so
2616 * locking and refcounting of rdevs is not needed
2617 */
2618 for (i = 0; i < conf->raid_disks; i++) {
2619 struct md_rdev *rdev = conf->disks[i].rdev;
2620 if (rdev
2621 && !test_bit(Faulty, &rdev->flags)
2622 && !test_bit(In_sync, &rdev->flags)
2623 && !rdev_set_badblocks(rdev, sh->sector,
2624 STRIPE_SECTORS, 0))
2625 abort = 1;
2626 rdev = conf->disks[i].replacement;
2627 if (rdev
2628 && !test_bit(Faulty, &rdev->flags)
2629 && !test_bit(In_sync, &rdev->flags)
2630 && !rdev_set_badblocks(rdev, sh->sector,
2631 STRIPE_SECTORS, 0))
2632 abort = 1;
2633 }
2634 if (abort)
2635 conf->recovery_disabled =
2636 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002637 }
NeilBrown18b98372012-04-01 23:48:38 +10002638 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002639}
2640
NeilBrown9a3e1102011-12-23 10:17:53 +11002641static int want_replace(struct stripe_head *sh, int disk_idx)
2642{
2643 struct md_rdev *rdev;
2644 int rv = 0;
2645 /* Doing recovery so rcu locking not required */
2646 rdev = sh->raid_conf->disks[disk_idx].replacement;
2647 if (rdev
2648 && !test_bit(Faulty, &rdev->flags)
2649 && !test_bit(In_sync, &rdev->flags)
2650 && (rdev->recovery_offset <= sh->sector
2651 || rdev->mddev->recovery_cp <= sh->sector))
2652 rv = 1;
2653
2654 return rv;
2655}
2656
NeilBrown93b3dbc2011-07-27 11:00:36 +10002657/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002658 * to be read or computed to satisfy a request.
2659 *
2660 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002661 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002662 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002663static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2664 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002665{
2666 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002667 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2668 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002669
Dan Williamsf38e1212007-01-02 13:52:30 -07002670 /* is the data in this block needed, and can we get it? */
2671 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002672 !test_bit(R5_UPTODATE, &dev->flags) &&
2673 (dev->toread ||
2674 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2675 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002676 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002677 (s->failed >= 1 && fdev[0]->toread) ||
2678 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002679 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2680 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2681 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002682 /* we would like to get this block, possibly by computing it,
2683 * otherwise read it if the backing disk is insync
2684 */
2685 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2686 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2687 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002688 (s->failed && (disk_idx == s->failed_num[0] ||
2689 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002690 /* have disk failed, and we're requested to fetch it;
2691 * do compute it
2692 */
2693 pr_debug("Computing stripe %llu block %d\n",
2694 (unsigned long long)sh->sector, disk_idx);
2695 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2696 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2697 set_bit(R5_Wantcompute, &dev->flags);
2698 sh->ops.target = disk_idx;
2699 sh->ops.target2 = -1; /* no 2nd target */
2700 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002701 /* Careful: from this point on 'uptodate' is in the eye
2702 * of raid_run_ops which services 'compute' operations
2703 * before writes. R5_Wantcompute flags a block that will
2704 * be R5_UPTODATE by the time it is needed for a
2705 * subsequent operation.
2706 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002707 s->uptodate++;
2708 return 1;
2709 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2710 /* Computing 2-failure is *very* expensive; only
2711 * do it if failed >= 2
2712 */
2713 int other;
2714 for (other = disks; other--; ) {
2715 if (other == disk_idx)
2716 continue;
2717 if (!test_bit(R5_UPTODATE,
2718 &sh->dev[other].flags))
2719 break;
2720 }
2721 BUG_ON(other < 0);
2722 pr_debug("Computing stripe %llu blocks %d,%d\n",
2723 (unsigned long long)sh->sector,
2724 disk_idx, other);
2725 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2726 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2727 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2728 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2729 sh->ops.target = disk_idx;
2730 sh->ops.target2 = other;
2731 s->uptodate += 2;
2732 s->req_compute = 1;
2733 return 1;
2734 } else if (test_bit(R5_Insync, &dev->flags)) {
2735 set_bit(R5_LOCKED, &dev->flags);
2736 set_bit(R5_Wantread, &dev->flags);
2737 s->locked++;
2738 pr_debug("Reading block %d (sync=%d)\n",
2739 disk_idx, s->syncing);
2740 }
2741 }
2742
2743 return 0;
2744}
2745
2746/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002747 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002748 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002749static void handle_stripe_fill(struct stripe_head *sh,
2750 struct stripe_head_state *s,
2751 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002752{
2753 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002754
2755 /* look for blocks to read/compute, skip this if a compute
2756 * is already in flight, or if the stripe contents are in the
2757 * midst of changing due to a write
2758 */
2759 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2760 !sh->reconstruct_state)
2761 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002762 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002763 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002764 set_bit(STRIPE_HANDLE, &sh->state);
2765}
2766
2767
Dan Williams1fe797e2008-06-28 09:16:30 +10002768/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002769 * any written block on an uptodate or failed drive can be returned.
2770 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2771 * never LOCKED, so we don't need to test 'failed' directly.
2772 */
NeilBrownd1688a62011-10-11 16:49:52 +11002773static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002774 struct stripe_head *sh, int disks, struct bio **return_bi)
2775{
2776 int i;
2777 struct r5dev *dev;
2778
2779 for (i = disks; i--; )
2780 if (sh->dev[i].written) {
2781 dev = &sh->dev[i];
2782 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11002783 (test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownca64cae2012-11-21 16:33:40 +11002784 test_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002785 /* We can return any write requests */
2786 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002787 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11002788 if (test_and_clear_bit(R5_Discard, &dev->flags))
2789 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002790 wbi = dev->written;
2791 dev->written = NULL;
2792 while (wbi && wbi->bi_sector <
2793 dev->sector + STRIPE_SECTORS) {
2794 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002795 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002796 md_write_end(conf->mddev);
2797 wbi->bi_next = *return_bi;
2798 *return_bi = wbi;
2799 }
2800 wbi = wbi2;
2801 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002802 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2803 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002804 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002805 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002806 }
NeilBrownca64cae2012-11-21 16:33:40 +11002807 } else if (test_bit(R5_Discard, &sh->dev[i].flags))
2808 clear_bit(R5_Discard, &sh->dev[i].flags);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002809
2810 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2811 if (atomic_dec_and_test(&conf->pending_full_writes))
2812 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002813}
2814
NeilBrownd1688a62011-10-11 16:49:52 +11002815static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002816 struct stripe_head *sh,
2817 struct stripe_head_state *s,
2818 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002819{
2820 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002821 sector_t recovery_cp = conf->mddev->recovery_cp;
2822
2823 /* RAID6 requires 'rcw' in current implementation.
2824 * Otherwise, check whether resync is now happening or should start.
2825 * If yes, then the array is dirty (after unclean shutdown or
2826 * initial creation), so parity in some stripes might be inconsistent.
2827 * In this case, we need to always do reconstruct-write, to ensure
2828 * that in case of drive failure or read-error correction, we
2829 * generate correct data from the parity.
2830 */
2831 if (conf->max_degraded == 2 ||
2832 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2833 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002834 * look like rcw is cheaper
2835 */
2836 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002837 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2838 conf->max_degraded, (unsigned long long)recovery_cp,
2839 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002840 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002841 /* would I have to read this buffer for read_modify_write */
2842 struct r5dev *dev = &sh->dev[i];
2843 if ((dev->towrite || i == sh->pd_idx) &&
2844 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002845 !(test_bit(R5_UPTODATE, &dev->flags) ||
2846 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002847 if (test_bit(R5_Insync, &dev->flags))
2848 rmw++;
2849 else
2850 rmw += 2*disks; /* cannot read it */
2851 }
2852 /* Would I have to read this buffer for reconstruct_write */
2853 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2854 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002855 !(test_bit(R5_UPTODATE, &dev->flags) ||
2856 test_bit(R5_Wantcompute, &dev->flags))) {
2857 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002858 else
2859 rcw += 2*disks;
2860 }
2861 }
Dan Williams45b42332007-07-09 11:56:43 -07002862 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002863 (unsigned long long)sh->sector, rmw, rcw);
2864 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrowna9add5d2012-10-31 11:59:09 +11002865 if (rmw < rcw && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002866 /* prefer read-modify-write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002867 blk_add_trace_msg(conf->mddev->queue, "raid5 rmw %llu %d",
2868 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07002869 for (i = disks; i--; ) {
2870 struct r5dev *dev = &sh->dev[i];
2871 if ((dev->towrite || i == sh->pd_idx) &&
2872 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002873 !(test_bit(R5_UPTODATE, &dev->flags) ||
2874 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002875 test_bit(R5_Insync, &dev->flags)) {
2876 if (
2877 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002878 pr_debug("Read_old block "
NeilBrowna9add5d2012-10-31 11:59:09 +11002879 "%d for r-m-w\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002880 set_bit(R5_LOCKED, &dev->flags);
2881 set_bit(R5_Wantread, &dev->flags);
2882 s->locked++;
2883 } else {
2884 set_bit(STRIPE_DELAYED, &sh->state);
2885 set_bit(STRIPE_HANDLE, &sh->state);
2886 }
2887 }
2888 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002889 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002890 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002891 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11002892 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10002893 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002894 for (i = disks; i--; ) {
2895 struct r5dev *dev = &sh->dev[i];
2896 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002897 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002898 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002899 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002900 test_bit(R5_Wantcompute, &dev->flags))) {
2901 rcw++;
2902 if (!test_bit(R5_Insync, &dev->flags))
2903 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002904 if (
2905 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002906 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002907 "%d for Reconstruct\n", i);
2908 set_bit(R5_LOCKED, &dev->flags);
2909 set_bit(R5_Wantread, &dev->flags);
2910 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11002911 qread++;
Dan Williamsa4456852007-07-09 11:56:43 -07002912 } else {
2913 set_bit(STRIPE_DELAYED, &sh->state);
2914 set_bit(STRIPE_HANDLE, &sh->state);
2915 }
2916 }
2917 }
NeilBrowna9add5d2012-10-31 11:59:09 +11002918 if (rcw)
2919 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
2920 (unsigned long long)sh->sector,
2921 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10002922 }
Dan Williamsa4456852007-07-09 11:56:43 -07002923 /* now if nothing is locked, and if we have enough data,
2924 * we can start a write request
2925 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002926 /* since handle_stripe can be called at any time we need to handle the
2927 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002928 * subsequent call wants to start a write request. raid_run_ops only
2929 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002930 * simultaneously. If this is not the case then new writes need to be
2931 * held off until the compute completes.
2932 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002933 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2934 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2935 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002936 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002937}
2938
NeilBrownd1688a62011-10-11 16:49:52 +11002939static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002940 struct stripe_head_state *s, int disks)
2941{
Dan Williamsecc65c92008-06-28 08:31:57 +10002942 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002943
Dan Williamsbd2ab672008-04-10 21:29:27 -07002944 set_bit(STRIPE_HANDLE, &sh->state);
2945
Dan Williamsecc65c92008-06-28 08:31:57 +10002946 switch (sh->check_state) {
2947 case check_state_idle:
2948 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002949 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002950 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002951 sh->check_state = check_state_run;
2952 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002953 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002954 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002955 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002956 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002957 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002958 /* fall through */
2959 case check_state_compute_result:
2960 sh->check_state = check_state_idle;
2961 if (!dev)
2962 dev = &sh->dev[sh->pd_idx];
2963
2964 /* check that a write has not made the stripe insync */
2965 if (test_bit(STRIPE_INSYNC, &sh->state))
2966 break;
2967
2968 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002969 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2970 BUG_ON(s->uptodate != disks);
2971
2972 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002973 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002974 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002975
Dan Williamsa4456852007-07-09 11:56:43 -07002976 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002977 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002978 break;
2979 case check_state_run:
2980 break; /* we will be called again upon completion */
2981 case check_state_check_result:
2982 sh->check_state = check_state_idle;
2983
2984 /* if a failure occurred during the check operation, leave
2985 * STRIPE_INSYNC not set and let the stripe be handled again
2986 */
2987 if (s->failed)
2988 break;
2989
2990 /* handle a successful check operation, if parity is correct
2991 * we are done. Otherwise update the mismatch count and repair
2992 * parity if !MD_RECOVERY_CHECK
2993 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002994 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002995 /* parity is correct (on disc,
2996 * not in buffer any more)
2997 */
2998 set_bit(STRIPE_INSYNC, &sh->state);
2999 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003000 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10003001 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3002 /* don't try to repair!! */
3003 set_bit(STRIPE_INSYNC, &sh->state);
3004 else {
3005 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10003006 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10003007 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3008 set_bit(R5_Wantcompute,
3009 &sh->dev[sh->pd_idx].flags);
3010 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07003011 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10003012 s->uptodate++;
3013 }
3014 }
3015 break;
3016 case check_state_compute_run:
3017 break;
3018 default:
3019 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3020 __func__, sh->check_state,
3021 (unsigned long long) sh->sector);
3022 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003023 }
3024}
3025
3026
NeilBrownd1688a62011-10-11 16:49:52 +11003027static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003028 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003029 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003030{
Dan Williamsa4456852007-07-09 11:56:43 -07003031 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003032 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003033 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003034
3035 set_bit(STRIPE_HANDLE, &sh->state);
3036
3037 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003038
Dan Williamsa4456852007-07-09 11:56:43 -07003039 /* Want to check and possibly repair P and Q.
3040 * However there could be one 'failed' device, in which
3041 * case we can only check one of them, possibly using the
3042 * other to generate missing data
3043 */
3044
Dan Williamsd82dfee2009-07-14 13:40:57 -07003045 switch (sh->check_state) {
3046 case check_state_idle:
3047 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003048 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003049 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003050 * makes sense to check P (If anything else were failed,
3051 * we would have used P to recreate it).
3052 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003053 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003054 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003055 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003056 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003057 * anything, so it makes sense to check it
3058 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003059 if (sh->check_state == check_state_run)
3060 sh->check_state = check_state_run_pq;
3061 else
3062 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003063 }
Dan Williams36d1c642009-07-14 11:48:22 -07003064
Dan Williamsd82dfee2009-07-14 13:40:57 -07003065 /* discard potentially stale zero_sum_result */
3066 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003067
Dan Williamsd82dfee2009-07-14 13:40:57 -07003068 if (sh->check_state == check_state_run) {
3069 /* async_xor_zero_sum destroys the contents of P */
3070 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3071 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003072 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003073 if (sh->check_state >= check_state_run &&
3074 sh->check_state <= check_state_run_pq) {
3075 /* async_syndrome_zero_sum preserves P and Q, so
3076 * no need to mark them !uptodate here
3077 */
3078 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3079 break;
3080 }
Dan Williams36d1c642009-07-14 11:48:22 -07003081
Dan Williamsd82dfee2009-07-14 13:40:57 -07003082 /* we have 2-disk failure */
3083 BUG_ON(s->failed != 2);
3084 /* fall through */
3085 case check_state_compute_result:
3086 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003087
Dan Williamsd82dfee2009-07-14 13:40:57 -07003088 /* check that a write has not made the stripe insync */
3089 if (test_bit(STRIPE_INSYNC, &sh->state))
3090 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003091
3092 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003093 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003094 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003095 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003096 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003097 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003098 s->locked++;
3099 set_bit(R5_LOCKED, &dev->flags);
3100 set_bit(R5_Wantwrite, &dev->flags);
3101 }
3102 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003103 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003104 s->locked++;
3105 set_bit(R5_LOCKED, &dev->flags);
3106 set_bit(R5_Wantwrite, &dev->flags);
3107 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003108 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003109 dev = &sh->dev[pd_idx];
3110 s->locked++;
3111 set_bit(R5_LOCKED, &dev->flags);
3112 set_bit(R5_Wantwrite, &dev->flags);
3113 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003114 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003115 dev = &sh->dev[qd_idx];
3116 s->locked++;
3117 set_bit(R5_LOCKED, &dev->flags);
3118 set_bit(R5_Wantwrite, &dev->flags);
3119 }
3120 clear_bit(STRIPE_DEGRADED, &sh->state);
3121
3122 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003123 break;
3124 case check_state_run:
3125 case check_state_run_q:
3126 case check_state_run_pq:
3127 break; /* we will be called again upon completion */
3128 case check_state_check_result:
3129 sh->check_state = check_state_idle;
3130
3131 /* handle a successful check operation, if parity is correct
3132 * we are done. Otherwise update the mismatch count and repair
3133 * parity if !MD_RECOVERY_CHECK
3134 */
3135 if (sh->ops.zero_sum_result == 0) {
3136 /* both parities are correct */
3137 if (!s->failed)
3138 set_bit(STRIPE_INSYNC, &sh->state);
3139 else {
3140 /* in contrast to the raid5 case we can validate
3141 * parity, but still have a failure to write
3142 * back
3143 */
3144 sh->check_state = check_state_compute_result;
3145 /* Returning at this point means that we may go
3146 * off and bring p and/or q uptodate again so
3147 * we make sure to check zero_sum_result again
3148 * to verify if p or q need writeback
3149 */
3150 }
3151 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003152 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003153 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3154 /* don't try to repair!! */
3155 set_bit(STRIPE_INSYNC, &sh->state);
3156 else {
3157 int *target = &sh->ops.target;
3158
3159 sh->ops.target = -1;
3160 sh->ops.target2 = -1;
3161 sh->check_state = check_state_compute_run;
3162 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3163 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3164 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3165 set_bit(R5_Wantcompute,
3166 &sh->dev[pd_idx].flags);
3167 *target = pd_idx;
3168 target = &sh->ops.target2;
3169 s->uptodate++;
3170 }
3171 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3172 set_bit(R5_Wantcompute,
3173 &sh->dev[qd_idx].flags);
3174 *target = qd_idx;
3175 s->uptodate++;
3176 }
3177 }
3178 }
3179 break;
3180 case check_state_compute_run:
3181 break;
3182 default:
3183 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3184 __func__, sh->check_state,
3185 (unsigned long long) sh->sector);
3186 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003187 }
3188}
3189
NeilBrownd1688a62011-10-11 16:49:52 +11003190static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003191{
3192 int i;
3193
3194 /* We have read all the blocks in this stripe and now we need to
3195 * copy some of them into a target stripe for expand.
3196 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003197 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003198 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3199 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003200 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003201 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003202 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003203 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003204
NeilBrown784052e2009-03-31 15:19:07 +11003205 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003206 sector_t s = raid5_compute_sector(conf, bn, 0,
3207 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003208 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003209 if (sh2 == NULL)
3210 /* so far only the early blocks of this stripe
3211 * have been requested. When later blocks
3212 * get requested, we will try again
3213 */
3214 continue;
3215 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3216 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3217 /* must have already done this block */
3218 release_stripe(sh2);
3219 continue;
3220 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003221
3222 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003223 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003224 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003225 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003226 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003227
Dan Williamsa4456852007-07-09 11:56:43 -07003228 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3229 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3230 for (j = 0; j < conf->raid_disks; j++)
3231 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003232 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003233 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3234 break;
3235 if (j == conf->raid_disks) {
3236 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3237 set_bit(STRIPE_HANDLE, &sh2->state);
3238 }
3239 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003240
Dan Williamsa4456852007-07-09 11:56:43 -07003241 }
NeilBrowna2e08552007-09-11 15:23:36 -07003242 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11003243 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07003244}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
3246/*
3247 * handle_stripe - do things to a stripe.
3248 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003249 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3250 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003252 * return some read requests which now have data
3253 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 * schedule a read on some buffers
3255 * schedule a write of some buffers
3256 * return confirmation of parity correctness
3257 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 */
Dan Williamsa4456852007-07-09 11:56:43 -07003259
NeilBrownacfe7262011-07-27 11:00:36 +10003260static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003261{
NeilBrownd1688a62011-10-11 16:49:52 +11003262 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003263 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003264 struct r5dev *dev;
3265 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003266 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003267
NeilBrownacfe7262011-07-27 11:00:36 +10003268 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003269
NeilBrownacfe7262011-07-27 11:00:36 +10003270 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3271 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3272 s->failed_num[0] = -1;
3273 s->failed_num[1] = -1;
3274
3275 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003276 rcu_read_lock();
3277 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003278 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003279 sector_t first_bad;
3280 int bad_sectors;
3281 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003282
NeilBrown16a53ec2006-06-26 00:27:38 -07003283 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003284
Dan Williams45b42332007-07-09 11:56:43 -07003285 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003286 i, dev->flags,
3287 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003288 /* maybe we can reply to a read
3289 *
3290 * new wantfill requests are only permitted while
3291 * ops_complete_biofill is guaranteed to be inactive
3292 */
3293 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3294 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3295 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003296
3297 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003298 if (test_bit(R5_LOCKED, &dev->flags))
3299 s->locked++;
3300 if (test_bit(R5_UPTODATE, &dev->flags))
3301 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003302 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003303 s->compute++;
3304 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003305 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003306
NeilBrownacfe7262011-07-27 11:00:36 +10003307 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003308 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003309 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003310 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003311 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003312 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003313 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003314 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003315 }
Dan Williamsa4456852007-07-09 11:56:43 -07003316 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003317 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003318 /* Prefer to use the replacement for reads, but only
3319 * if it is recovered enough and has no bad blocks.
3320 */
3321 rdev = rcu_dereference(conf->disks[i].replacement);
3322 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3323 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3324 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3325 &first_bad, &bad_sectors))
3326 set_bit(R5_ReadRepl, &dev->flags);
3327 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003328 if (rdev)
3329 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003330 rdev = rcu_dereference(conf->disks[i].rdev);
3331 clear_bit(R5_ReadRepl, &dev->flags);
3332 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003333 if (rdev && test_bit(Faulty, &rdev->flags))
3334 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003335 if (rdev) {
3336 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3337 &first_bad, &bad_sectors);
3338 if (s->blocked_rdev == NULL
3339 && (test_bit(Blocked, &rdev->flags)
3340 || is_bad < 0)) {
3341 if (is_bad < 0)
3342 set_bit(BlockedBadBlocks,
3343 &rdev->flags);
3344 s->blocked_rdev = rdev;
3345 atomic_inc(&rdev->nr_pending);
3346 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003347 }
NeilBrown415e72d2010-06-17 17:25:21 +10003348 clear_bit(R5_Insync, &dev->flags);
3349 if (!rdev)
3350 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003351 else if (is_bad) {
3352 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003353 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3354 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003355 /* treat as in-sync, but with a read error
3356 * which we can now try to correct
3357 */
3358 set_bit(R5_Insync, &dev->flags);
3359 set_bit(R5_ReadError, &dev->flags);
3360 }
3361 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003362 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003363 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003364 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003365 set_bit(R5_Insync, &dev->flags);
3366 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3367 test_bit(R5_Expanded, &dev->flags))
3368 /* If we've reshaped into here, we assume it is Insync.
3369 * We will shortly update recovery_offset to make
3370 * it official.
3371 */
3372 set_bit(R5_Insync, &dev->flags);
3373
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003374 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003375 /* This flag does not apply to '.replacement'
3376 * only to .rdev, so make sure to check that*/
3377 struct md_rdev *rdev2 = rcu_dereference(
3378 conf->disks[i].rdev);
3379 if (rdev2 == rdev)
3380 clear_bit(R5_Insync, &dev->flags);
3381 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003382 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003383 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003384 } else
3385 clear_bit(R5_WriteError, &dev->flags);
3386 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003387 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003388 /* This flag does not apply to '.replacement'
3389 * only to .rdev, so make sure to check that*/
3390 struct md_rdev *rdev2 = rcu_dereference(
3391 conf->disks[i].rdev);
3392 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003393 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003394 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003395 } else
3396 clear_bit(R5_MadeGood, &dev->flags);
3397 }
NeilBrown977df362011-12-23 10:17:53 +11003398 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3399 struct md_rdev *rdev2 = rcu_dereference(
3400 conf->disks[i].replacement);
3401 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3402 s->handle_bad_blocks = 1;
3403 atomic_inc(&rdev2->nr_pending);
3404 } else
3405 clear_bit(R5_MadeGoodRepl, &dev->flags);
3406 }
NeilBrown415e72d2010-06-17 17:25:21 +10003407 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003408 /* The ReadError flag will just be confusing now */
3409 clear_bit(R5_ReadError, &dev->flags);
3410 clear_bit(R5_ReWrite, &dev->flags);
3411 }
NeilBrown415e72d2010-06-17 17:25:21 +10003412 if (test_bit(R5_ReadError, &dev->flags))
3413 clear_bit(R5_Insync, &dev->flags);
3414 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003415 if (s->failed < 2)
3416 s->failed_num[s->failed] = i;
3417 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003418 if (rdev && !test_bit(Faulty, &rdev->flags))
3419 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003420 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003421 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003422 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3423 /* If there is a failed device being replaced,
3424 * we must be recovering.
3425 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003426 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003427 * else we can only be replacing
3428 * sync and recovery both need to read all devices, and so
3429 * use the same flag.
3430 */
3431 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003432 sh->sector >= conf->mddev->recovery_cp ||
3433 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003434 s->syncing = 1;
3435 else
3436 s->replacing = 1;
3437 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003438 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003439}
NeilBrownf4168852007-02-28 20:11:53 -08003440
NeilBrowncc940152011-07-26 11:35:35 +10003441static void handle_stripe(struct stripe_head *sh)
3442{
3443 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003444 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003445 int i;
NeilBrown84789552011-07-27 11:00:36 +10003446 int prexor;
3447 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003448 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003449
3450 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003451 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003452 /* already being handled, ensure it gets handled
3453 * again when current action finishes */
3454 set_bit(STRIPE_HANDLE, &sh->state);
3455 return;
3456 }
3457
3458 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3459 set_bit(STRIPE_SYNCING, &sh->state);
3460 clear_bit(STRIPE_INSYNC, &sh->state);
3461 }
3462 clear_bit(STRIPE_DELAYED, &sh->state);
3463
3464 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3465 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3466 (unsigned long long)sh->sector, sh->state,
3467 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3468 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003469
NeilBrownacfe7262011-07-27 11:00:36 +10003470 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003471
NeilBrownbc2607f2011-07-28 11:39:22 +10003472 if (s.handle_bad_blocks) {
3473 set_bit(STRIPE_HANDLE, &sh->state);
3474 goto finish;
3475 }
3476
NeilBrown474af965fe2011-07-27 11:00:36 +10003477 if (unlikely(s.blocked_rdev)) {
3478 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003479 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003480 set_bit(STRIPE_HANDLE, &sh->state);
3481 goto finish;
3482 }
3483 /* There is nothing for the blocked_rdev to block */
3484 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3485 s.blocked_rdev = NULL;
3486 }
3487
3488 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3489 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3490 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3491 }
3492
3493 pr_debug("locked=%d uptodate=%d to_read=%d"
3494 " to_write=%d failed=%d failed_num=%d,%d\n",
3495 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3496 s.failed_num[0], s.failed_num[1]);
3497 /* check if the array has lost more than max_degraded devices and,
3498 * if so, some requests might need to be failed.
3499 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003500 if (s.failed > conf->max_degraded) {
3501 sh->check_state = 0;
3502 sh->reconstruct_state = 0;
3503 if (s.to_read+s.to_write+s.written)
3504 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003505 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003506 handle_failed_sync(conf, sh, &s);
3507 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003508
NeilBrown84789552011-07-27 11:00:36 +10003509 /* Now we check to see if any write operations have recently
3510 * completed
3511 */
3512 prexor = 0;
3513 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3514 prexor = 1;
3515 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3516 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3517 sh->reconstruct_state = reconstruct_state_idle;
3518
3519 /* All the 'written' buffers and the parity block are ready to
3520 * be written back to disk
3521 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003522 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3523 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003524 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003525 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3526 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003527 for (i = disks; i--; ) {
3528 struct r5dev *dev = &sh->dev[i];
3529 if (test_bit(R5_LOCKED, &dev->flags) &&
3530 (i == sh->pd_idx || i == sh->qd_idx ||
3531 dev->written)) {
3532 pr_debug("Writing block %d\n", i);
3533 set_bit(R5_Wantwrite, &dev->flags);
3534 if (prexor)
3535 continue;
3536 if (!test_bit(R5_Insync, &dev->flags) ||
3537 ((i == sh->pd_idx || i == sh->qd_idx) &&
3538 s.failed == 0))
3539 set_bit(STRIPE_INSYNC, &sh->state);
3540 }
3541 }
3542 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3543 s.dec_preread_active = 1;
3544 }
3545
NeilBrownef5b7c62012-11-22 09:13:36 +11003546 /*
3547 * might be able to return some write requests if the parity blocks
3548 * are safe, or on a failed drive
3549 */
3550 pdev = &sh->dev[sh->pd_idx];
3551 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3552 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3553 qdev = &sh->dev[sh->qd_idx];
3554 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3555 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3556 || conf->level < 6;
3557
3558 if (s.written &&
3559 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3560 && !test_bit(R5_LOCKED, &pdev->flags)
3561 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3562 test_bit(R5_Discard, &pdev->flags))))) &&
3563 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3564 && !test_bit(R5_LOCKED, &qdev->flags)
3565 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3566 test_bit(R5_Discard, &qdev->flags))))))
3567 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3568
3569 /* Now we might consider reading some blocks, either to check/generate
3570 * parity, or to satisfy requests
3571 * or to load a block that is being partially written.
3572 */
3573 if (s.to_read || s.non_overwrite
3574 || (conf->level == 6 && s.to_write && s.failed)
3575 || (s.syncing && (s.uptodate + s.compute < disks))
3576 || s.replacing
3577 || s.expanding)
3578 handle_stripe_fill(sh, &s, disks);
3579
NeilBrown84789552011-07-27 11:00:36 +10003580 /* Now to consider new write requests and what else, if anything
3581 * should be read. We do not handle new writes when:
3582 * 1/ A 'write' operation (copy+xor) is already in flight.
3583 * 2/ A 'check' operation is in flight, as it may clobber the parity
3584 * block.
3585 */
3586 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3587 handle_stripe_dirtying(conf, sh, &s, disks);
3588
3589 /* maybe we need to check and possibly fix the parity for this stripe
3590 * Any reads will already have been scheduled, so we just see if enough
3591 * data is available. The parity check is held off while parity
3592 * dependent operations are in flight.
3593 */
3594 if (sh->check_state ||
3595 (s.syncing && s.locked == 0 &&
3596 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3597 !test_bit(STRIPE_INSYNC, &sh->state))) {
3598 if (conf->level == 6)
3599 handle_parity_checks6(conf, sh, &s, disks);
3600 else
3601 handle_parity_checks5(conf, sh, &s, disks);
3602 }
NeilBrownc5a31002011-07-27 11:00:36 +10003603
NeilBrown9a3e1102011-12-23 10:17:53 +11003604 if (s.replacing && s.locked == 0
3605 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3606 /* Write out to replacement devices where possible */
3607 for (i = 0; i < conf->raid_disks; i++)
3608 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3609 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3610 set_bit(R5_WantReplace, &sh->dev[i].flags);
3611 set_bit(R5_LOCKED, &sh->dev[i].flags);
3612 s.locked++;
3613 }
3614 set_bit(STRIPE_INSYNC, &sh->state);
3615 }
3616 if ((s.syncing || s.replacing) && s.locked == 0 &&
3617 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003618 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3619 clear_bit(STRIPE_SYNCING, &sh->state);
3620 }
3621
3622 /* If the failed drives are just a ReadError, then we might need
3623 * to progress the repair/check process
3624 */
3625 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3626 for (i = 0; i < s.failed; i++) {
3627 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3628 if (test_bit(R5_ReadError, &dev->flags)
3629 && !test_bit(R5_LOCKED, &dev->flags)
3630 && test_bit(R5_UPTODATE, &dev->flags)
3631 ) {
3632 if (!test_bit(R5_ReWrite, &dev->flags)) {
3633 set_bit(R5_Wantwrite, &dev->flags);
3634 set_bit(R5_ReWrite, &dev->flags);
3635 set_bit(R5_LOCKED, &dev->flags);
3636 s.locked++;
3637 } else {
3638 /* let's read it back */
3639 set_bit(R5_Wantread, &dev->flags);
3640 set_bit(R5_LOCKED, &dev->flags);
3641 s.locked++;
3642 }
3643 }
3644 }
3645
3646
NeilBrown3687c062011-07-27 11:00:36 +10003647 /* Finish reconstruct operations initiated by the expansion process */
3648 if (sh->reconstruct_state == reconstruct_state_result) {
3649 struct stripe_head *sh_src
3650 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3651 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3652 /* sh cannot be written until sh_src has been read.
3653 * so arrange for sh to be delayed a little
3654 */
3655 set_bit(STRIPE_DELAYED, &sh->state);
3656 set_bit(STRIPE_HANDLE, &sh->state);
3657 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3658 &sh_src->state))
3659 atomic_inc(&conf->preread_active_stripes);
3660 release_stripe(sh_src);
3661 goto finish;
3662 }
3663 if (sh_src)
3664 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003665
NeilBrown3687c062011-07-27 11:00:36 +10003666 sh->reconstruct_state = reconstruct_state_idle;
3667 clear_bit(STRIPE_EXPANDING, &sh->state);
3668 for (i = conf->raid_disks; i--; ) {
3669 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3670 set_bit(R5_LOCKED, &sh->dev[i].flags);
3671 s.locked++;
3672 }
3673 }
3674
3675 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3676 !sh->reconstruct_state) {
3677 /* Need to write out all blocks after computing parity */
3678 sh->disks = conf->raid_disks;
3679 stripe_set_idx(sh->sector, conf, 0, sh);
3680 schedule_reconstruction(sh, &s, 1, 1);
3681 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3682 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3683 atomic_dec(&conf->reshape_stripes);
3684 wake_up(&conf->wait_for_overlap);
3685 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3686 }
3687
3688 if (s.expanding && s.locked == 0 &&
3689 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3690 handle_stripe_expansion(conf, sh);
3691
3692finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003693 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003694 if (unlikely(s.blocked_rdev)) {
3695 if (conf->mddev->external)
3696 md_wait_for_blocked_rdev(s.blocked_rdev,
3697 conf->mddev);
3698 else
3699 /* Internal metadata will immediately
3700 * be written by raid5d, so we don't
3701 * need to wait here.
3702 */
3703 rdev_dec_pending(s.blocked_rdev,
3704 conf->mddev);
3705 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003706
NeilBrownbc2607f2011-07-28 11:39:22 +10003707 if (s.handle_bad_blocks)
3708 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003709 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003710 struct r5dev *dev = &sh->dev[i];
3711 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3712 /* We own a safe reference to the rdev */
3713 rdev = conf->disks[i].rdev;
3714 if (!rdev_set_badblocks(rdev, sh->sector,
3715 STRIPE_SECTORS, 0))
3716 md_error(conf->mddev, rdev);
3717 rdev_dec_pending(rdev, conf->mddev);
3718 }
NeilBrownb84db562011-07-28 11:39:23 +10003719 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3720 rdev = conf->disks[i].rdev;
3721 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003722 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003723 rdev_dec_pending(rdev, conf->mddev);
3724 }
NeilBrown977df362011-12-23 10:17:53 +11003725 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3726 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003727 if (!rdev)
3728 /* rdev have been moved down */
3729 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003730 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003731 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003732 rdev_dec_pending(rdev, conf->mddev);
3733 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003734 }
3735
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003736 if (s.ops_request)
3737 raid_run_ops(sh, s.ops_request);
3738
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003739 ops_run_io(sh, &s);
3740
NeilBrownc5709ef2011-07-26 11:35:20 +10003741 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003742 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003743 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003744 * have actually been submitted.
3745 */
3746 atomic_dec(&conf->preread_active_stripes);
3747 if (atomic_read(&conf->preread_active_stripes) <
3748 IO_THRESHOLD)
3749 md_wakeup_thread(conf->mddev->thread);
3750 }
3751
NeilBrownc5709ef2011-07-26 11:35:20 +10003752 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003753
Dan Williams257a4b42011-11-08 16:22:06 +11003754 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003755}
3756
NeilBrownd1688a62011-10-11 16:49:52 +11003757static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758{
3759 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3760 while (!list_empty(&conf->delayed_list)) {
3761 struct list_head *l = conf->delayed_list.next;
3762 struct stripe_head *sh;
3763 sh = list_entry(l, struct stripe_head, lru);
3764 list_del_init(l);
3765 clear_bit(STRIPE_DELAYED, &sh->state);
3766 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3767 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003768 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 }
NeilBrown482c0832011-04-18 18:25:42 +10003770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771}
3772
NeilBrownd1688a62011-10-11 16:49:52 +11003773static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003774{
3775 /* device_lock is held */
3776 struct list_head head;
3777 list_add(&head, &conf->bitmap_list);
3778 list_del_init(&conf->bitmap_list);
3779 while (!list_empty(&head)) {
3780 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3781 list_del_init(&sh->lru);
3782 atomic_inc(&sh->count);
3783 __release_stripe(conf, sh);
3784 }
3785}
3786
NeilBrownfd01b882011-10-11 16:47:53 +11003787int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003788{
NeilBrownd1688a62011-10-11 16:49:52 +11003789 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003790
3791 /* No difference between reads and writes. Just check
3792 * how busy the stripe_cache is
3793 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003794
NeilBrownf022b2f2006-10-03 01:15:56 -07003795 if (conf->inactive_blocked)
3796 return 1;
3797 if (conf->quiesce)
3798 return 1;
3799 if (list_empty_careful(&conf->inactive_list))
3800 return 1;
3801
3802 return 0;
3803}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003804EXPORT_SYMBOL_GPL(md_raid5_congested);
3805
3806static int raid5_congested(void *data, int bits)
3807{
NeilBrownfd01b882011-10-11 16:47:53 +11003808 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003809
3810 return mddev_congested(mddev, bits) ||
3811 md_raid5_congested(mddev, bits);
3812}
NeilBrownf022b2f2006-10-03 01:15:56 -07003813
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003814/* We want read requests to align with chunks where possible,
3815 * but write requests don't need to.
3816 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003817static int raid5_mergeable_bvec(struct request_queue *q,
3818 struct bvec_merge_data *bvm,
3819 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003820{
NeilBrownfd01b882011-10-11 16:47:53 +11003821 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003822 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003823 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003824 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003825 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003826
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003827 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003828 return biovec->bv_len; /* always allow writes to be mergeable */
3829
Andre Noll664e7c42009-06-18 08:45:27 +10003830 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3831 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003832 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3833 if (max < 0) max = 0;
3834 if (max <= biovec->bv_len && bio_sectors == 0)
3835 return biovec->bv_len;
3836 else
3837 return max;
3838}
3839
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003840
NeilBrownfd01b882011-10-11 16:47:53 +11003841static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003842{
3843 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003844 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003845 unsigned int bio_sectors = bio->bi_size >> 9;
3846
Andre Noll664e7c42009-06-18 08:45:27 +10003847 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3848 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003849 return chunk_sectors >=
3850 ((sector & (chunk_sectors - 1)) + bio_sectors);
3851}
3852
3853/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003854 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3855 * later sampled by raid5d.
3856 */
NeilBrownd1688a62011-10-11 16:49:52 +11003857static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003858{
3859 unsigned long flags;
3860
3861 spin_lock_irqsave(&conf->device_lock, flags);
3862
3863 bi->bi_next = conf->retry_read_aligned_list;
3864 conf->retry_read_aligned_list = bi;
3865
3866 spin_unlock_irqrestore(&conf->device_lock, flags);
3867 md_wakeup_thread(conf->mddev->thread);
3868}
3869
3870
NeilBrownd1688a62011-10-11 16:49:52 +11003871static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003872{
3873 struct bio *bi;
3874
3875 bi = conf->retry_read_aligned;
3876 if (bi) {
3877 conf->retry_read_aligned = NULL;
3878 return bi;
3879 }
3880 bi = conf->retry_read_aligned_list;
3881 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003882 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003883 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003884 /*
3885 * this sets the active strip count to 1 and the processed
3886 * strip count to zero (upper 8 bits)
3887 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003888 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003889 }
3890
3891 return bi;
3892}
3893
3894
3895/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003896 * The "raid5_align_endio" should check if the read succeeded and if it
3897 * did, call bio_endio on the original bio (having bio_put the new bio
3898 * first).
3899 * If the read failed..
3900 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003901static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003902{
3903 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003904 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003905 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003906 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003907 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003908
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003909 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003910
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003911 rdev = (void*)raid_bi->bi_next;
3912 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003913 mddev = rdev->mddev;
3914 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003915
3916 rdev_dec_pending(rdev, conf->mddev);
3917
3918 if (!error && uptodate) {
NeilBrowna9add5d2012-10-31 11:59:09 +11003919 trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev),
3920 raid_bi, 0);
NeilBrown6712ecf2007-09-27 12:47:43 +02003921 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003922 if (atomic_dec_and_test(&conf->active_aligned_reads))
3923 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003924 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003925 }
3926
3927
Dan Williams45b42332007-07-09 11:56:43 -07003928 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003929
3930 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003931}
3932
Neil Brown387bb172007-02-08 14:20:29 -08003933static int bio_fits_rdev(struct bio *bi)
3934{
Jens Axboe165125e2007-07-24 09:28:11 +02003935 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003936
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003937 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003938 return 0;
3939 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003940 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003941 return 0;
3942
3943 if (q->merge_bvec_fn)
3944 /* it's too hard to apply the merge_bvec_fn at this stage,
3945 * just just give up
3946 */
3947 return 0;
3948
3949 return 1;
3950}
3951
3952
NeilBrownfd01b882011-10-11 16:47:53 +11003953static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003954{
NeilBrownd1688a62011-10-11 16:49:52 +11003955 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003956 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003957 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003958 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003959 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003960
3961 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003962 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003963 return 0;
3964 }
3965 /*
NeilBrowna167f662010-10-26 18:31:13 +11003966 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003967 */
NeilBrowna167f662010-10-26 18:31:13 +11003968 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003969 if (!align_bi)
3970 return 0;
3971 /*
3972 * set bi_end_io to a new function, and set bi_private to the
3973 * original bio.
3974 */
3975 align_bi->bi_end_io = raid5_align_endio;
3976 align_bi->bi_private = raid_bio;
3977 /*
3978 * compute position
3979 */
NeilBrown112bf892009-03-31 14:39:38 +11003980 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3981 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003982 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003983
NeilBrown671488c2011-12-23 10:17:52 +11003984 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003985 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003986 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3987 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3988 rdev->recovery_offset < end_sector) {
3989 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3990 if (rdev &&
3991 (test_bit(Faulty, &rdev->flags) ||
3992 !(test_bit(In_sync, &rdev->flags) ||
3993 rdev->recovery_offset >= end_sector)))
3994 rdev = NULL;
3995 }
3996 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003997 sector_t first_bad;
3998 int bad_sectors;
3999
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004000 atomic_inc(&rdev->nr_pending);
4001 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004002 raid_bio->bi_next = (void*)rdev;
4003 align_bi->bi_bdev = rdev->bdev;
4004 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004005
NeilBrown31c176e2011-07-28 11:39:22 +10004006 if (!bio_fits_rdev(align_bi) ||
4007 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
4008 &first_bad, &bad_sectors)) {
4009 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08004010 bio_put(align_bi);
4011 rdev_dec_pending(rdev, mddev);
4012 return 0;
4013 }
4014
majianpeng6c0544e2012-06-12 08:31:10 +08004015 /* No reshape active, so we can trust rdev->data_offset */
4016 align_bi->bi_sector += rdev->data_offset;
4017
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004018 spin_lock_irq(&conf->device_lock);
4019 wait_event_lock_irq(conf->wait_for_stripe,
4020 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004021 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004022 atomic_inc(&conf->active_aligned_reads);
4023 spin_unlock_irq(&conf->device_lock);
4024
NeilBrowna9add5d2012-10-31 11:59:09 +11004025 trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev),
4026 align_bi, disk_devt(mddev->gendisk),
4027 raid_bio->bi_sector);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004028 generic_make_request(align_bi);
4029 return 1;
4030 } else {
4031 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004032 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004033 return 0;
4034 }
4035}
4036
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004037/* __get_priority_stripe - get the next stripe to process
4038 *
4039 * Full stripe writes are allowed to pass preread active stripes up until
4040 * the bypass_threshold is exceeded. In general the bypass_count
4041 * increments when the handle_list is handled before the hold_list; however, it
4042 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4043 * stripe with in flight i/o. The bypass_count will be reset when the
4044 * head of the hold_list has changed, i.e. the head was promoted to the
4045 * handle_list.
4046 */
NeilBrownd1688a62011-10-11 16:49:52 +11004047static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004048{
4049 struct stripe_head *sh;
4050
4051 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4052 __func__,
4053 list_empty(&conf->handle_list) ? "empty" : "busy",
4054 list_empty(&conf->hold_list) ? "empty" : "busy",
4055 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4056
4057 if (!list_empty(&conf->handle_list)) {
4058 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4059
4060 if (list_empty(&conf->hold_list))
4061 conf->bypass_count = 0;
4062 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4063 if (conf->hold_list.next == conf->last_hold)
4064 conf->bypass_count++;
4065 else {
4066 conf->last_hold = conf->hold_list.next;
4067 conf->bypass_count -= conf->bypass_threshold;
4068 if (conf->bypass_count < 0)
4069 conf->bypass_count = 0;
4070 }
4071 }
4072 } else if (!list_empty(&conf->hold_list) &&
4073 ((conf->bypass_threshold &&
4074 conf->bypass_count > conf->bypass_threshold) ||
4075 atomic_read(&conf->pending_full_writes) == 0)) {
4076 sh = list_entry(conf->hold_list.next,
4077 typeof(*sh), lru);
4078 conf->bypass_count -= conf->bypass_threshold;
4079 if (conf->bypass_count < 0)
4080 conf->bypass_count = 0;
4081 } else
4082 return NULL;
4083
4084 list_del_init(&sh->lru);
4085 atomic_inc(&sh->count);
4086 BUG_ON(atomic_read(&sh->count) != 1);
4087 return sh;
4088}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004089
Shaohua Li8811b592012-08-02 08:33:00 +10004090struct raid5_plug_cb {
4091 struct blk_plug_cb cb;
4092 struct list_head list;
4093};
4094
4095static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4096{
4097 struct raid5_plug_cb *cb = container_of(
4098 blk_cb, struct raid5_plug_cb, cb);
4099 struct stripe_head *sh;
4100 struct mddev *mddev = cb->cb.data;
4101 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11004102 int cnt = 0;
Shaohua Li8811b592012-08-02 08:33:00 +10004103
4104 if (cb->list.next && !list_empty(&cb->list)) {
4105 spin_lock_irq(&conf->device_lock);
4106 while (!list_empty(&cb->list)) {
4107 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4108 list_del_init(&sh->lru);
4109 /*
4110 * avoid race release_stripe_plug() sees
4111 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4112 * is still in our list
4113 */
4114 smp_mb__before_clear_bit();
4115 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4116 __release_stripe(conf, sh);
NeilBrowna9add5d2012-10-31 11:59:09 +11004117 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10004118 }
4119 spin_unlock_irq(&conf->device_lock);
4120 }
NeilBrowna9add5d2012-10-31 11:59:09 +11004121 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10004122 kfree(cb);
4123}
4124
4125static void release_stripe_plug(struct mddev *mddev,
4126 struct stripe_head *sh)
4127{
4128 struct blk_plug_cb *blk_cb = blk_check_plugged(
4129 raid5_unplug, mddev,
4130 sizeof(struct raid5_plug_cb));
4131 struct raid5_plug_cb *cb;
4132
4133 if (!blk_cb) {
4134 release_stripe(sh);
4135 return;
4136 }
4137
4138 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4139
4140 if (cb->list.next == NULL)
4141 INIT_LIST_HEAD(&cb->list);
4142
4143 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4144 list_add_tail(&sh->lru, &cb->list);
4145 else
4146 release_stripe(sh);
4147}
4148
Shaohua Li620125f2012-10-11 13:49:05 +11004149static void make_discard_request(struct mddev *mddev, struct bio *bi)
4150{
4151 struct r5conf *conf = mddev->private;
4152 sector_t logical_sector, last_sector;
4153 struct stripe_head *sh;
4154 int remaining;
4155 int stripe_sectors;
4156
4157 if (mddev->reshape_position != MaxSector)
4158 /* Skip discard while reshape is happening */
4159 return;
4160
4161 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4162 last_sector = bi->bi_sector + (bi->bi_size>>9);
4163
4164 bi->bi_next = NULL;
4165 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4166
4167 stripe_sectors = conf->chunk_sectors *
4168 (conf->raid_disks - conf->max_degraded);
4169 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4170 stripe_sectors);
4171 sector_div(last_sector, stripe_sectors);
4172
4173 logical_sector *= conf->chunk_sectors;
4174 last_sector *= conf->chunk_sectors;
4175
4176 for (; logical_sector < last_sector;
4177 logical_sector += STRIPE_SECTORS) {
4178 DEFINE_WAIT(w);
4179 int d;
4180 again:
4181 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4182 prepare_to_wait(&conf->wait_for_overlap, &w,
4183 TASK_UNINTERRUPTIBLE);
4184 spin_lock_irq(&sh->stripe_lock);
4185 for (d = 0; d < conf->raid_disks; d++) {
4186 if (d == sh->pd_idx || d == sh->qd_idx)
4187 continue;
4188 if (sh->dev[d].towrite || sh->dev[d].toread) {
4189 set_bit(R5_Overlap, &sh->dev[d].flags);
4190 spin_unlock_irq(&sh->stripe_lock);
4191 release_stripe(sh);
4192 schedule();
4193 goto again;
4194 }
4195 }
4196 finish_wait(&conf->wait_for_overlap, &w);
4197 for (d = 0; d < conf->raid_disks; d++) {
4198 if (d == sh->pd_idx || d == sh->qd_idx)
4199 continue;
4200 sh->dev[d].towrite = bi;
4201 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4202 raid5_inc_bi_active_stripes(bi);
4203 }
4204 spin_unlock_irq(&sh->stripe_lock);
4205 if (conf->mddev->bitmap) {
4206 for (d = 0;
4207 d < conf->raid_disks - conf->max_degraded;
4208 d++)
4209 bitmap_startwrite(mddev->bitmap,
4210 sh->sector,
4211 STRIPE_SECTORS,
4212 0);
4213 sh->bm_seq = conf->seq_flush + 1;
4214 set_bit(STRIPE_BIT_DELAY, &sh->state);
4215 }
4216
4217 set_bit(STRIPE_HANDLE, &sh->state);
4218 clear_bit(STRIPE_DELAYED, &sh->state);
4219 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4220 atomic_inc(&conf->preread_active_stripes);
4221 release_stripe_plug(mddev, sh);
4222 }
4223
4224 remaining = raid5_dec_bi_active_stripes(bi);
4225 if (remaining == 0) {
4226 md_write_end(mddev);
4227 bio_endio(bi, 0);
4228 }
4229}
4230
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004231static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232{
NeilBrownd1688a62011-10-11 16:49:52 +11004233 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004234 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 sector_t new_sector;
4236 sector_t logical_sector, last_sector;
4237 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004238 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004239 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
Tejun Heoe9c74692010-09-03 11:56:18 +02004241 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4242 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004243 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004244 }
4245
NeilBrown3d310eb2005-06-21 17:17:26 -07004246 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004247
NeilBrown802ba062006-12-13 00:34:13 -08004248 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004249 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004250 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004251 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004252
Shaohua Li620125f2012-10-11 13:49:05 +11004253 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4254 make_discard_request(mddev, bi);
4255 return;
4256 }
4257
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4259 last_sector = bi->bi_sector + (bi->bi_size>>9);
4260 bi->bi_next = NULL;
4261 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004262
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4264 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004265 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004266
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004267 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004268 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004269 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004270 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004271 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08004272 * 64bit on a 32bit platform, and so it might be
4273 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004274 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08004275 * the lock is dropped, so once we get a reference
4276 * to the stripe that we think it is, we will have
4277 * to check again.
4278 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004279 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004280 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004281 ? logical_sector < conf->reshape_progress
4282 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004283 previous = 1;
4284 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004285 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004286 ? logical_sector < conf->reshape_safe
4287 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004288 spin_unlock_irq(&conf->device_lock);
4289 schedule();
4290 goto retry;
4291 }
4292 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004293 spin_unlock_irq(&conf->device_lock);
4294 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004295
NeilBrown112bf892009-03-31 14:39:38 +11004296 new_sector = raid5_compute_sector(conf, logical_sector,
4297 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004298 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004299 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 (unsigned long long)new_sector,
4301 (unsigned long long)logical_sector);
4302
NeilBrownb5663ba2009-03-31 14:39:38 +11004303 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004304 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004306 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004307 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08004308 * stripe, so we must do the range check again.
4309 * Expansion could still move past after this
4310 * test, but as we are holding a reference to
4311 * 'sh', we know that if that happens,
4312 * STRIPE_EXPANDING will get set and the expansion
4313 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004314 */
4315 int must_retry = 0;
4316 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004317 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004318 ? logical_sector >= conf->reshape_progress
4319 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004320 /* mismatch, need to try again */
4321 must_retry = 1;
4322 spin_unlock_irq(&conf->device_lock);
4323 if (must_retry) {
4324 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004325 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004326 goto retry;
4327 }
4328 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004329
Namhyung Kimffd96e32011-07-18 17:38:51 +10004330 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004331 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004332 logical_sector < mddev->suspend_hi) {
4333 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004334 /* As the suspend_* range is controlled by
4335 * userspace, we want an interruptible
4336 * wait.
4337 */
4338 flush_signals(current);
4339 prepare_to_wait(&conf->wait_for_overlap,
4340 &w, TASK_INTERRUPTIBLE);
4341 if (logical_sector >= mddev->suspend_lo &&
4342 logical_sector < mddev->suspend_hi)
4343 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004344 goto retry;
4345 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004346
4347 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004348 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004349 /* Stripe is busy expanding or
4350 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 * and wait a while
4352 */
NeilBrown482c0832011-04-18 18:25:42 +10004353 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 release_stripe(sh);
4355 schedule();
4356 goto retry;
4357 }
4358 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004359 set_bit(STRIPE_HANDLE, &sh->state);
4360 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004361 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004362 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4363 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004364 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 } else {
4366 /* cannot get stripe for read-ahead, just give-up */
4367 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4368 finish_wait(&conf->wait_for_overlap, &w);
4369 break;
4370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004372
Shaohua Lie7836bd62012-07-19 16:01:31 +10004373 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004374 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375
NeilBrown16a53ec2006-06-26 00:27:38 -07004376 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004378
NeilBrowna9add5d2012-10-31 11:59:09 +11004379 trace_block_bio_complete(bdev_get_queue(bi->bi_bdev),
4380 bi, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004381 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383}
4384
NeilBrownfd01b882011-10-11 16:47:53 +11004385static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004386
NeilBrownfd01b882011-10-11 16:47:53 +11004387static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388{
NeilBrown52c03292006-06-26 00:27:43 -07004389 /* reshaping is quite different to recovery/resync so it is
4390 * handled quite separately ... here.
4391 *
4392 * On each call to sync_request, we gather one chunk worth of
4393 * destination stripes and flag them as expanding.
4394 * Then we find all the source stripes and request reads.
4395 * As the reads complete, handle_stripe will copy the data
4396 * into the destination stripe and release that stripe.
4397 */
NeilBrownd1688a62011-10-11 16:49:52 +11004398 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004400 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004401 int raid_disks = conf->previous_raid_disks;
4402 int data_disks = raid_disks - conf->max_degraded;
4403 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004404 int i;
4405 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004406 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004407 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004408 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004409 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004410
NeilBrownfef9c612009-03-31 15:16:46 +11004411 if (sector_nr == 0) {
4412 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004413 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004414 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4415 sector_nr = raid5_size(mddev, 0, 0)
4416 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004417 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004418 conf->reshape_progress > 0)
4419 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004420 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004421 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004422 mddev->curr_resync_completed = sector_nr;
4423 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004424 *skipped = 1;
4425 return sector_nr;
4426 }
NeilBrown52c03292006-06-26 00:27:43 -07004427 }
4428
NeilBrown7a661382009-03-31 15:21:40 +11004429 /* We need to process a full chunk at a time.
4430 * If old and new chunk sizes differ, we need to process the
4431 * largest of these
4432 */
Andre Noll664e7c42009-06-18 08:45:27 +10004433 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4434 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004435 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004436 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004437
NeilBrownb5254dd2012-05-21 09:27:01 +10004438 /* We update the metadata at least every 10 seconds, or when
4439 * the data about to be copied would over-write the source of
4440 * the data at the front of the range. i.e. one new_stripe
4441 * along from reshape_progress new_maps to after where
4442 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004443 */
NeilBrownfef9c612009-03-31 15:16:46 +11004444 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004445 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004446 readpos = conf->reshape_progress;
4447 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004448 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004449 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004450 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004451 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004452 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004453 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004454 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004455 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004456 readpos -= min_t(sector_t, reshape_sectors, readpos);
4457 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004458 }
NeilBrown52c03292006-06-26 00:27:43 -07004459
NeilBrownb5254dd2012-05-21 09:27:01 +10004460 /* Having calculated the 'writepos' possibly use it
4461 * to set 'stripe_addr' which is where we will write to.
4462 */
4463 if (mddev->reshape_backwards) {
4464 BUG_ON(conf->reshape_progress == 0);
4465 stripe_addr = writepos;
4466 BUG_ON((mddev->dev_sectors &
4467 ~((sector_t)reshape_sectors - 1))
4468 - reshape_sectors - stripe_addr
4469 != sector_nr);
4470 } else {
4471 BUG_ON(writepos != sector_nr + reshape_sectors);
4472 stripe_addr = sector_nr;
4473 }
4474
NeilBrownc8f517c2009-03-31 15:28:40 +11004475 /* 'writepos' is the most advanced device address we might write.
4476 * 'readpos' is the least advanced device address we might read.
4477 * 'safepos' is the least address recorded in the metadata as having
4478 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004479 * If there is a min_offset_diff, these are adjusted either by
4480 * increasing the safepos/readpos if diff is negative, or
4481 * increasing writepos if diff is positive.
4482 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004483 * ensure safety in the face of a crash - that must be done by userspace
4484 * making a backup of the data. So in that case there is no particular
4485 * rush to update metadata.
4486 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4487 * update the metadata to advance 'safepos' to match 'readpos' so that
4488 * we can be safe in the event of a crash.
4489 * So we insist on updating metadata if safepos is behind writepos and
4490 * readpos is beyond writepos.
4491 * In any case, update the metadata every 10 seconds.
4492 * Maybe that number should be configurable, but I'm not sure it is
4493 * worth it.... maybe it could be a multiple of safemode_delay???
4494 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004495 if (conf->min_offset_diff < 0) {
4496 safepos += -conf->min_offset_diff;
4497 readpos += -conf->min_offset_diff;
4498 } else
4499 writepos += conf->min_offset_diff;
4500
NeilBrown2c810cd2012-05-21 09:27:00 +10004501 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004502 ? (safepos > writepos && readpos < writepos)
4503 : (safepos < writepos && readpos > writepos)) ||
4504 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004505 /* Cannot proceed until we've updated the superblock... */
4506 wait_event(conf->wait_for_overlap,
4507 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004508 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004509 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004510 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b422006-10-03 01:15:46 -07004511 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004512 md_wakeup_thread(mddev->thread);
NeilBrown850b2b422006-10-03 01:15:46 -07004513 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004514 kthread_should_stop());
4515 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004516 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004517 spin_unlock_irq(&conf->device_lock);
4518 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004519 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004520 }
4521
NeilBrownab69ae12009-03-31 15:26:47 +11004522 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004523 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004524 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004525 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004526 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004527 set_bit(STRIPE_EXPANDING, &sh->state);
4528 atomic_inc(&conf->reshape_stripes);
4529 /* If any of this stripe is beyond the end of the old
4530 * array, then we need to zero those blocks
4531 */
4532 for (j=sh->disks; j--;) {
4533 sector_t s;
4534 if (j == sh->pd_idx)
4535 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004536 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004537 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004538 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004539 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004540 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004541 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004542 continue;
4543 }
4544 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4545 set_bit(R5_Expanded, &sh->dev[j].flags);
4546 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4547 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004548 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004549 set_bit(STRIPE_EXPAND_READY, &sh->state);
4550 set_bit(STRIPE_HANDLE, &sh->state);
4551 }
NeilBrownab69ae12009-03-31 15:26:47 +11004552 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004553 }
4554 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004555 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004556 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004557 else
NeilBrown7a661382009-03-31 15:21:40 +11004558 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004559 spin_unlock_irq(&conf->device_lock);
4560 /* Ok, those stripe are ready. We can start scheduling
4561 * reads on the source stripes.
4562 * The source stripes are determined by mapping the first and last
4563 * block on the destination stripes.
4564 */
NeilBrown52c03292006-06-26 00:27:43 -07004565 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004566 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004567 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004568 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004569 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004570 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004571 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004572 if (last_sector >= mddev->dev_sectors)
4573 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004574 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004575 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004576 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4577 set_bit(STRIPE_HANDLE, &sh->state);
4578 release_stripe(sh);
4579 first_sector += STRIPE_SECTORS;
4580 }
NeilBrownab69ae12009-03-31 15:26:47 +11004581 /* Now that the sources are clearly marked, we can release
4582 * the destination stripes
4583 */
4584 while (!list_empty(&stripes)) {
4585 sh = list_entry(stripes.next, struct stripe_head, lru);
4586 list_del_init(&sh->lru);
4587 release_stripe(sh);
4588 }
NeilBrownc6207272008-02-06 01:39:52 -08004589 /* If this takes us to the resync_max point where we have to pause,
4590 * then we need to write out the superblock.
4591 */
NeilBrown7a661382009-03-31 15:21:40 +11004592 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004593 if ((sector_nr - mddev->curr_resync_completed) * 2
4594 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004595 /* Cannot proceed until we've updated the superblock... */
4596 wait_event(conf->wait_for_overlap,
4597 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004598 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004599 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004600 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004601 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4602 md_wakeup_thread(mddev->thread);
4603 wait_event(mddev->sb_wait,
4604 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4605 || kthread_should_stop());
4606 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004607 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004608 spin_unlock_irq(&conf->device_lock);
4609 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004610 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004611 }
NeilBrown7a661382009-03-31 15:21:40 +11004612 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004613}
4614
4615/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004616static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004617{
NeilBrownd1688a62011-10-11 16:49:52 +11004618 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004619 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004620 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004621 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004622 int still_degraded = 0;
4623 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
NeilBrown72626682005-09-09 16:23:54 -07004625 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004627
NeilBrown29269552006-03-27 01:18:10 -08004628 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4629 end_reshape(conf);
4630 return 0;
4631 }
NeilBrown72626682005-09-09 16:23:54 -07004632
4633 if (mddev->curr_resync < max_sector) /* aborted */
4634 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4635 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004636 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004637 conf->fullsync = 0;
4638 bitmap_close_sync(mddev->bitmap);
4639
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 return 0;
4641 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004642
NeilBrown64bd6602009-08-03 10:59:58 +10004643 /* Allow raid5_quiesce to complete */
4644 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4645
NeilBrown52c03292006-06-26 00:27:43 -07004646 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4647 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004648
NeilBrownc6207272008-02-06 01:39:52 -08004649 /* No need to check resync_max as we never do more than one
4650 * stripe, and as resync_max will always be on a chunk boundary,
4651 * if the check in md_do_sync didn't fire, there is no chance
4652 * of overstepping resync_max here
4653 */
4654
NeilBrown16a53ec2006-06-26 00:27:38 -07004655 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 * to resync, then assert that we are finished, because there is
4657 * nothing we can do.
4658 */
NeilBrown3285edf2006-06-26 00:27:55 -07004659 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004660 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004661 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004662 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 return rv;
4664 }
NeilBrown72626682005-09-09 16:23:54 -07004665 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004666 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004667 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4668 /* we can skip this block, and probably more */
4669 sync_blocks /= STRIPE_SECTORS;
4670 *skipped = 1;
4671 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
NeilBrownb47490c2008-02-06 01:39:50 -08004674 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4675
NeilBrowna8c906c2009-06-09 14:39:59 +10004676 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004678 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004680 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004682 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004684 /* Need to check if array will still be degraded after recovery/resync
4685 * We don't need to check the 'failed' flag as when that gets set,
4686 * recovery aborts.
4687 */
NeilBrownf001a702009-06-09 14:30:31 +10004688 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004689 if (conf->disks[i].rdev == NULL)
4690 still_degraded = 1;
4691
4692 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4693
NeilBrown83206d62011-07-26 11:19:49 +10004694 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
NeilBrown14425772009-10-16 15:55:25 +11004696 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 release_stripe(sh);
4698
4699 return STRIPE_SECTORS;
4700}
4701
NeilBrownd1688a62011-10-11 16:49:52 +11004702static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004703{
4704 /* We may not be able to submit a whole bio at once as there
4705 * may not be enough stripe_heads available.
4706 * We cannot pre-allocate enough stripe_heads as we may need
4707 * more than exist in the cache (if we allow ever large chunks).
4708 * So we do one stripe head at a time and record in
4709 * ->bi_hw_segments how many have been done.
4710 *
4711 * We *know* that this entire raid_bio is in one chunk, so
4712 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4713 */
4714 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004715 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004716 sector_t sector, logical_sector, last_sector;
4717 int scnt = 0;
4718 int remaining;
4719 int handled = 0;
4720
4721 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004722 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004723 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004724 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4725
4726 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004727 logical_sector += STRIPE_SECTORS,
4728 sector += STRIPE_SECTORS,
4729 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004730
Shaohua Lie7836bd62012-07-19 16:01:31 +10004731 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004732 /* already done this stripe */
4733 continue;
4734
NeilBrowna8c906c2009-06-09 14:39:59 +10004735 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004736
4737 if (!sh) {
4738 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004739 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004740 conf->retry_read_aligned = raid_bio;
4741 return handled;
4742 }
4743
Neil Brown387bb172007-02-08 14:20:29 -08004744 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4745 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004746 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004747 conf->retry_read_aligned = raid_bio;
4748 return handled;
4749 }
4750
majianpeng3f9e7c12012-07-31 10:04:21 +10004751 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004752 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004753 release_stripe(sh);
4754 handled++;
4755 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004756 remaining = raid5_dec_bi_active_stripes(raid_bio);
NeilBrowna9add5d2012-10-31 11:59:09 +11004757 if (remaining == 0) {
4758 trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev),
4759 raid_bio, 0);
Neil Brown0e13fe232008-06-28 08:31:20 +10004760 bio_endio(raid_bio, 0);
NeilBrowna9add5d2012-10-31 11:59:09 +11004761 }
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004762 if (atomic_dec_and_test(&conf->active_aligned_reads))
4763 wake_up(&conf->wait_for_stripe);
4764 return handled;
4765}
4766
Shaohua Li46a06402012-08-02 08:33:15 +10004767#define MAX_STRIPE_BATCH 8
4768static int handle_active_stripes(struct r5conf *conf)
4769{
4770 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4771 int i, batch_size = 0;
4772
4773 while (batch_size < MAX_STRIPE_BATCH &&
4774 (sh = __get_priority_stripe(conf)) != NULL)
4775 batch[batch_size++] = sh;
4776
4777 if (batch_size == 0)
4778 return batch_size;
4779 spin_unlock_irq(&conf->device_lock);
4780
4781 for (i = 0; i < batch_size; i++)
4782 handle_stripe(batch[i]);
4783
4784 cond_resched();
4785
4786 spin_lock_irq(&conf->device_lock);
4787 for (i = 0; i < batch_size; i++)
4788 __release_stripe(conf, batch[i]);
4789 return batch_size;
4790}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004791
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792/*
4793 * This is our raid5 kernel thread.
4794 *
4795 * We scan the hash table for stripes which can be handled now.
4796 * During the scan, completed stripes are saved for us by the interrupt
4797 * handler, so that they will not have to wait for our next wakeup.
4798 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004799static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800{
Shaohua Li4ed87312012-10-11 13:34:00 +11004801 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004802 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004804 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805
Dan Williams45b42332007-07-09 11:56:43 -07004806 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
4808 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004810 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 handled = 0;
4812 spin_lock_irq(&conf->device_lock);
4813 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004814 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004815 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
NeilBrown0021b7b2012-07-31 09:08:14 +02004817 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004818 !list_empty(&conf->bitmap_list)) {
4819 /* Now is a good time to flush some bitmap updates */
4820 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004821 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004822 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004823 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004824 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004825 activate_bit_delay(conf);
4826 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004827 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004828
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004829 while ((bio = remove_bio_from_retry(conf))) {
4830 int ok;
4831 spin_unlock_irq(&conf->device_lock);
4832 ok = retry_aligned_read(conf, bio);
4833 spin_lock_irq(&conf->device_lock);
4834 if (!ok)
4835 break;
4836 handled++;
4837 }
4838
Shaohua Li46a06402012-08-02 08:33:15 +10004839 batch_size = handle_active_stripes(conf);
4840 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004842 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
Shaohua Li46a06402012-08-02 08:33:15 +10004844 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4845 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004846 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004847 spin_lock_irq(&conf->device_lock);
4848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 }
Dan Williams45b42332007-07-09 11:56:43 -07004850 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
4852 spin_unlock_irq(&conf->device_lock);
4853
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004854 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004855 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
Dan Williams45b42332007-07-09 11:56:43 -07004857 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858}
4859
NeilBrown3f294f42005-11-08 21:39:25 -08004860static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004861raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004862{
NeilBrownd1688a62011-10-11 16:49:52 +11004863 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004864 if (conf)
4865 return sprintf(page, "%d\n", conf->max_nr_stripes);
4866 else
4867 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004868}
4869
NeilBrownc41d4ac2010-06-01 19:37:24 +10004870int
NeilBrownfd01b882011-10-11 16:47:53 +11004871raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004872{
NeilBrownd1688a62011-10-11 16:49:52 +11004873 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004874 int err;
4875
4876 if (size <= 16 || size > 32768)
4877 return -EINVAL;
4878 while (size < conf->max_nr_stripes) {
4879 if (drop_one_stripe(conf))
4880 conf->max_nr_stripes--;
4881 else
4882 break;
4883 }
4884 err = md_allow_write(mddev);
4885 if (err)
4886 return err;
4887 while (size > conf->max_nr_stripes) {
4888 if (grow_one_stripe(conf))
4889 conf->max_nr_stripes++;
4890 else break;
4891 }
4892 return 0;
4893}
4894EXPORT_SYMBOL(raid5_set_cache_size);
4895
NeilBrown3f294f42005-11-08 21:39:25 -08004896static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004897raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004898{
NeilBrownd1688a62011-10-11 16:49:52 +11004899 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004900 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004901 int err;
4902
NeilBrown3f294f42005-11-08 21:39:25 -08004903 if (len >= PAGE_SIZE)
4904 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004905 if (!conf)
4906 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004907
Dan Williams4ef197d82008-04-28 02:15:54 -07004908 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004909 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004910 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004911 if (err)
4912 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004913 return len;
4914}
NeilBrown007583c2005-11-08 21:39:30 -08004915
NeilBrown96de1e62005-11-08 21:39:39 -08004916static struct md_sysfs_entry
4917raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4918 raid5_show_stripe_cache_size,
4919 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004920
4921static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004922raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004923{
NeilBrownd1688a62011-10-11 16:49:52 +11004924 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004925 if (conf)
4926 return sprintf(page, "%d\n", conf->bypass_threshold);
4927 else
4928 return 0;
4929}
4930
4931static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004932raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004933{
NeilBrownd1688a62011-10-11 16:49:52 +11004934 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004935 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004936 if (len >= PAGE_SIZE)
4937 return -EINVAL;
4938 if (!conf)
4939 return -ENODEV;
4940
Dan Williams4ef197d82008-04-28 02:15:54 -07004941 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004942 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004943 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004944 return -EINVAL;
4945 conf->bypass_threshold = new;
4946 return len;
4947}
4948
4949static struct md_sysfs_entry
4950raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4951 S_IRUGO | S_IWUSR,
4952 raid5_show_preread_threshold,
4953 raid5_store_preread_threshold);
4954
4955static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004956stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004957{
NeilBrownd1688a62011-10-11 16:49:52 +11004958 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004959 if (conf)
4960 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4961 else
4962 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004963}
4964
NeilBrown96de1e62005-11-08 21:39:39 -08004965static struct md_sysfs_entry
4966raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004967
NeilBrown007583c2005-11-08 21:39:30 -08004968static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004969 &raid5_stripecache_size.attr,
4970 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004971 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004972 NULL,
4973};
NeilBrown007583c2005-11-08 21:39:30 -08004974static struct attribute_group raid5_attrs_group = {
4975 .name = NULL,
4976 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004977};
4978
Dan Williams80c3a6c2009-03-17 18:10:40 -07004979static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004980raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004981{
NeilBrownd1688a62011-10-11 16:49:52 +11004982 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004983
4984 if (!sectors)
4985 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004986 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004987 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004988 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004989
Andre Noll9d8f0362009-06-18 08:45:01 +10004990 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004991 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004992 return sectors * (raid_disks - conf->max_degraded);
4993}
4994
NeilBrownd1688a62011-10-11 16:49:52 +11004995static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004996{
4997 struct raid5_percpu *percpu;
4998 unsigned long cpu;
4999
5000 if (!conf->percpu)
5001 return;
5002
5003 get_online_cpus();
5004 for_each_possible_cpu(cpu) {
5005 percpu = per_cpu_ptr(conf->percpu, cpu);
5006 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005007 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005008 }
5009#ifdef CONFIG_HOTPLUG_CPU
5010 unregister_cpu_notifier(&conf->cpu_notify);
5011#endif
5012 put_online_cpus();
5013
5014 free_percpu(conf->percpu);
5015}
5016
NeilBrownd1688a62011-10-11 16:49:52 +11005017static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10005018{
5019 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07005020 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10005021 kfree(conf->disks);
5022 kfree(conf->stripe_hashtbl);
5023 kfree(conf);
5024}
5025
Dan Williams36d1c642009-07-14 11:48:22 -07005026#ifdef CONFIG_HOTPLUG_CPU
5027static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
5028 void *hcpu)
5029{
NeilBrownd1688a62011-10-11 16:49:52 +11005030 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005031 long cpu = (long)hcpu;
5032 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5033
5034 switch (action) {
5035 case CPU_UP_PREPARE:
5036 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07005037 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07005038 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005039 if (!percpu->scribble)
5040 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5041
5042 if (!percpu->scribble ||
5043 (conf->level == 6 && !percpu->spare_page)) {
5044 safe_put_page(percpu->spare_page);
5045 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005046 pr_err("%s: failed memory allocation for cpu%ld\n",
5047 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005048 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005049 }
5050 break;
5051 case CPU_DEAD:
5052 case CPU_DEAD_FROZEN:
5053 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005054 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005055 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005056 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005057 break;
5058 default:
5059 break;
5060 }
5061 return NOTIFY_OK;
5062}
5063#endif
5064
NeilBrownd1688a62011-10-11 16:49:52 +11005065static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005066{
5067 unsigned long cpu;
5068 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005069 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005070 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005071 int err;
5072
Dan Williams36d1c642009-07-14 11:48:22 -07005073 allcpus = alloc_percpu(struct raid5_percpu);
5074 if (!allcpus)
5075 return -ENOMEM;
5076 conf->percpu = allcpus;
5077
5078 get_online_cpus();
5079 err = 0;
5080 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005081 if (conf->level == 6) {
5082 spare_page = alloc_page(GFP_KERNEL);
5083 if (!spare_page) {
5084 err = -ENOMEM;
5085 break;
5086 }
5087 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5088 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005089 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005090 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005091 err = -ENOMEM;
5092 break;
5093 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005094 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005095 }
5096#ifdef CONFIG_HOTPLUG_CPU
5097 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5098 conf->cpu_notify.priority = 0;
5099 if (err == 0)
5100 err = register_cpu_notifier(&conf->cpu_notify);
5101#endif
5102 put_online_cpus();
5103
5104 return err;
5105}
5106
NeilBrownd1688a62011-10-11 16:49:52 +11005107static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108{
NeilBrownd1688a62011-10-11 16:49:52 +11005109 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005110 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005111 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005113 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114
NeilBrown91adb562009-03-31 14:39:39 +11005115 if (mddev->new_level != 5
5116 && mddev->new_level != 4
5117 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005118 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005119 mdname(mddev), mddev->new_level);
5120 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 }
NeilBrown91adb562009-03-31 14:39:39 +11005122 if ((mddev->new_level == 5
5123 && !algorithm_valid_raid5(mddev->new_layout)) ||
5124 (mddev->new_level == 6
5125 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005126 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005127 mdname(mddev), mddev->new_layout);
5128 return ERR_PTR(-EIO);
5129 }
5130 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005131 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005132 mdname(mddev), mddev->raid_disks);
5133 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135
Andre Noll664e7c42009-06-18 08:45:27 +10005136 if (!mddev->new_chunk_sectors ||
5137 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5138 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005139 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5140 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005141 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005142 }
5143
NeilBrownd1688a62011-10-11 16:49:52 +11005144 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005145 if (conf == NULL)
5146 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005147 spin_lock_init(&conf->device_lock);
5148 init_waitqueue_head(&conf->wait_for_stripe);
5149 init_waitqueue_head(&conf->wait_for_overlap);
5150 INIT_LIST_HEAD(&conf->handle_list);
5151 INIT_LIST_HEAD(&conf->hold_list);
5152 INIT_LIST_HEAD(&conf->delayed_list);
5153 INIT_LIST_HEAD(&conf->bitmap_list);
5154 INIT_LIST_HEAD(&conf->inactive_list);
5155 atomic_set(&conf->active_stripes, 0);
5156 atomic_set(&conf->preread_active_stripes, 0);
5157 atomic_set(&conf->active_aligned_reads, 0);
5158 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005159 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005160
5161 conf->raid_disks = mddev->raid_disks;
5162 if (mddev->reshape_position == MaxSector)
5163 conf->previous_raid_disks = mddev->raid_disks;
5164 else
5165 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005166 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5167 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005168
NeilBrown5e5e3e72009-10-16 16:35:30 +11005169 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005170 GFP_KERNEL);
5171 if (!conf->disks)
5172 goto abort;
5173
5174 conf->mddev = mddev;
5175
5176 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5177 goto abort;
5178
Dan Williams36d1c642009-07-14 11:48:22 -07005179 conf->level = mddev->new_level;
5180 if (raid5_alloc_percpu(conf) != 0)
5181 goto abort;
5182
NeilBrown0c55e022010-05-03 14:09:02 +10005183 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005184
NeilBrowndafb20f2012-03-19 12:46:39 +11005185 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005186 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005187 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005188 || raid_disk < 0)
5189 continue;
5190 disk = conf->disks + raid_disk;
5191
NeilBrown17045f52011-12-23 10:17:53 +11005192 if (test_bit(Replacement, &rdev->flags)) {
5193 if (disk->replacement)
5194 goto abort;
5195 disk->replacement = rdev;
5196 } else {
5197 if (disk->rdev)
5198 goto abort;
5199 disk->rdev = rdev;
5200 }
NeilBrown91adb562009-03-31 14:39:39 +11005201
5202 if (test_bit(In_sync, &rdev->flags)) {
5203 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005204 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5205 " disk %d\n",
5206 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005207 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005208 /* Cannot rely on bitmap to complete recovery */
5209 conf->fullsync = 1;
5210 }
5211
Andre Noll09c9e5f2009-06-18 08:45:55 +10005212 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005213 conf->level = mddev->new_level;
5214 if (conf->level == 6)
5215 conf->max_degraded = 2;
5216 else
5217 conf->max_degraded = 1;
5218 conf->algorithm = mddev->new_layout;
5219 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005220 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005221 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005222 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005223 conf->prev_algo = mddev->layout;
5224 }
NeilBrown91adb562009-03-31 14:39:39 +11005225
5226 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005227 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005228 if (grow_stripes(conf, conf->max_nr_stripes)) {
5229 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005230 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5231 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005232 goto abort;
5233 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005234 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5235 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005236
NeilBrown02326052012-07-03 15:56:52 +10005237 sprintf(pers_name, "raid%d", mddev->new_level);
5238 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005239 if (!conf->thread) {
5240 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005241 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005242 mdname(mddev));
5243 goto abort;
5244 }
5245
5246 return conf;
5247
5248 abort:
5249 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005250 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005251 return ERR_PTR(-EIO);
5252 } else
5253 return ERR_PTR(-ENOMEM);
5254}
5255
NeilBrownc148ffd2009-11-13 17:47:00 +11005256
5257static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5258{
5259 switch (algo) {
5260 case ALGORITHM_PARITY_0:
5261 if (raid_disk < max_degraded)
5262 return 1;
5263 break;
5264 case ALGORITHM_PARITY_N:
5265 if (raid_disk >= raid_disks - max_degraded)
5266 return 1;
5267 break;
5268 case ALGORITHM_PARITY_0_6:
5269 if (raid_disk == 0 ||
5270 raid_disk == raid_disks - 1)
5271 return 1;
5272 break;
5273 case ALGORITHM_LEFT_ASYMMETRIC_6:
5274 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5275 case ALGORITHM_LEFT_SYMMETRIC_6:
5276 case ALGORITHM_RIGHT_SYMMETRIC_6:
5277 if (raid_disk == raid_disks - 1)
5278 return 1;
5279 }
5280 return 0;
5281}
5282
NeilBrownfd01b882011-10-11 16:47:53 +11005283static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005284{
NeilBrownd1688a62011-10-11 16:49:52 +11005285 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005286 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005287 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005288 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005289 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005290 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005291 long long min_offset_diff = 0;
5292 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005293
Andre Noll8c6ac8682009-06-18 08:48:06 +10005294 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005295 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac8682009-06-18 08:48:06 +10005296 " -- starting background reconstruction\n",
5297 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005298
5299 rdev_for_each(rdev, mddev) {
5300 long long diff;
5301 if (rdev->raid_disk < 0)
5302 continue;
5303 diff = (rdev->new_data_offset - rdev->data_offset);
5304 if (first) {
5305 min_offset_diff = diff;
5306 first = 0;
5307 } else if (mddev->reshape_backwards &&
5308 diff < min_offset_diff)
5309 min_offset_diff = diff;
5310 else if (!mddev->reshape_backwards &&
5311 diff > min_offset_diff)
5312 min_offset_diff = diff;
5313 }
5314
NeilBrownf6705572006-03-27 01:18:11 -08005315 if (mddev->reshape_position != MaxSector) {
5316 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005317 * Difficulties arise if the stripe we would write to
5318 * next is at or after the stripe we would read from next.
5319 * For a reshape that changes the number of devices, this
5320 * is only possible for a very short time, and mdadm makes
5321 * sure that time appears to have past before assembling
5322 * the array. So we fail if that time hasn't passed.
5323 * For a reshape that keeps the number of devices the same
5324 * mdadm must be monitoring the reshape can keeping the
5325 * critical areas read-only and backed up. It will start
5326 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005327 */
5328 sector_t here_new, here_old;
5329 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005330 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005331
NeilBrown88ce4932009-03-31 15:24:23 +11005332 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005333 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005334 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005335 mdname(mddev));
5336 return -EINVAL;
5337 }
NeilBrownf6705572006-03-27 01:18:11 -08005338 old_disks = mddev->raid_disks - mddev->delta_disks;
5339 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005340 * further up in new geometry must map after here in old
5341 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005342 */
5343 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005344 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005345 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005346 printk(KERN_ERR "md/raid:%s: reshape_position not "
5347 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005348 return -EINVAL;
5349 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005350 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005351 /* here_new is the stripe we will write to */
5352 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005353 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005354 (old_disks-max_degraded));
5355 /* here_old is the first stripe that we might need to read
5356 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005357 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005358 if ((here_new * mddev->new_chunk_sectors !=
5359 here_old * mddev->chunk_sectors)) {
5360 printk(KERN_ERR "md/raid:%s: reshape position is"
5361 " confused - aborting\n", mdname(mddev));
5362 return -EINVAL;
5363 }
NeilBrown67ac6012009-08-13 10:06:24 +10005364 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005365 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005366 * and taking constant backups.
5367 * mdadm always starts a situation like this in
5368 * readonly mode so it can take control before
5369 * allowing any writes. So just check for that.
5370 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005371 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5372 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5373 /* not really in-place - so OK */;
5374 else if (mddev->ro == 0) {
5375 printk(KERN_ERR "md/raid:%s: in-place reshape "
5376 "must be started in read-only mode "
5377 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005378 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005379 return -EINVAL;
5380 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005381 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005382 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005383 here_old * mddev->chunk_sectors)
5384 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005385 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005386 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005387 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5388 "auto-recovery - aborting.\n",
5389 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005390 return -EINVAL;
5391 }
NeilBrown0c55e022010-05-03 14:09:02 +10005392 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5393 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005394 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005395 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005396 BUG_ON(mddev->level != mddev->new_level);
5397 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005398 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005399 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005400 }
5401
NeilBrown245f46c2009-03-31 14:39:39 +11005402 if (mddev->private == NULL)
5403 conf = setup_conf(mddev);
5404 else
5405 conf = mddev->private;
5406
NeilBrown91adb562009-03-31 14:39:39 +11005407 if (IS_ERR(conf))
5408 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005409
NeilBrownb5254dd2012-05-21 09:27:01 +10005410 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005411 mddev->thread = conf->thread;
5412 conf->thread = NULL;
5413 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414
NeilBrown17045f52011-12-23 10:17:53 +11005415 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5416 i++) {
5417 rdev = conf->disks[i].rdev;
5418 if (!rdev && conf->disks[i].replacement) {
5419 /* The replacement is all we have yet */
5420 rdev = conf->disks[i].replacement;
5421 conf->disks[i].replacement = NULL;
5422 clear_bit(Replacement, &rdev->flags);
5423 conf->disks[i].rdev = rdev;
5424 }
5425 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005426 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005427 if (conf->disks[i].replacement &&
5428 conf->reshape_progress != MaxSector) {
5429 /* replacements and reshape simply do not mix. */
5430 printk(KERN_ERR "md: cannot handle concurrent "
5431 "replacement and reshape.\n");
5432 goto abort;
5433 }
NeilBrown2f115882010-06-17 17:41:03 +10005434 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005435 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005436 continue;
5437 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005438 /* This disc is not fully in-sync. However if it
5439 * just stored parity (beyond the recovery_offset),
5440 * when we don't need to be concerned about the
5441 * array being dirty.
5442 * When reshape goes 'backwards', we never have
5443 * partially completed devices, so we only need
5444 * to worry about reshape going forwards.
5445 */
5446 /* Hack because v0.91 doesn't store recovery_offset properly. */
5447 if (mddev->major_version == 0 &&
5448 mddev->minor_version > 90)
5449 rdev->recovery_offset = reshape_offset;
5450
NeilBrownc148ffd2009-11-13 17:47:00 +11005451 if (rdev->recovery_offset < reshape_offset) {
5452 /* We need to check old and new layout */
5453 if (!only_parity(rdev->raid_disk,
5454 conf->algorithm,
5455 conf->raid_disks,
5456 conf->max_degraded))
5457 continue;
5458 }
5459 if (!only_parity(rdev->raid_disk,
5460 conf->prev_algo,
5461 conf->previous_raid_disks,
5462 conf->max_degraded))
5463 continue;
5464 dirty_parity_disks++;
5465 }
NeilBrown91adb562009-03-31 14:39:39 +11005466
NeilBrown17045f52011-12-23 10:17:53 +11005467 /*
5468 * 0 for a fully functional array, 1 or 2 for a degraded array.
5469 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005470 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471
NeilBrown674806d2010-06-16 17:17:53 +10005472 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005473 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005475 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 goto abort;
5477 }
5478
NeilBrown91adb562009-03-31 14:39:39 +11005479 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005480 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005481 mddev->resync_max_sectors = mddev->dev_sectors;
5482
NeilBrownc148ffd2009-11-13 17:47:00 +11005483 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005485 if (mddev->ok_start_degraded)
5486 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005487 "md/raid:%s: starting dirty degraded array"
5488 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005489 mdname(mddev));
5490 else {
5491 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005492 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005493 mdname(mddev));
5494 goto abort;
5495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 }
5497
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005499 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5500 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005501 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5502 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 else
NeilBrown0c55e022010-05-03 14:09:02 +10005504 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5505 " out of %d devices, algorithm %d\n",
5506 mdname(mddev), conf->level,
5507 mddev->raid_disks - mddev->degraded,
5508 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509
5510 print_raid5_conf(conf);
5511
NeilBrownfef9c612009-03-31 15:16:46 +11005512 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005513 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005514 atomic_set(&conf->reshape_stripes, 0);
5515 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5516 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5517 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5518 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5519 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005520 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005521 }
5522
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523
5524 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005525 if (mddev->to_remove == &raid5_attrs_group)
5526 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005527 else if (mddev->kobj.sd &&
5528 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005529 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005530 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005531 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005532 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5533
5534 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005535 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005536 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005537 /* read-ahead size must cover two whole stripes, which
5538 * is 2 * (datadisks) * chunksize where 'n' is the
5539 * number of raid devices
5540 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5542 int stripe = data_disks *
5543 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5544 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5545 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005546
5547 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005548
5549 mddev->queue->backing_dev_info.congested_data = mddev;
5550 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005551
5552 chunk_size = mddev->chunk_sectors << 9;
5553 blk_queue_io_min(mddev->queue, chunk_size);
5554 blk_queue_io_opt(mddev->queue, chunk_size *
5555 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005556 /*
5557 * We can only discard a whole stripe. It doesn't make sense to
5558 * discard data disk but write parity disk
5559 */
5560 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11005561 /* Round up to power of 2, as discard handling
5562 * currently assumes that */
5563 while ((stripe-1) & stripe)
5564 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11005565 mddev->queue->limits.discard_alignment = stripe;
5566 mddev->queue->limits.discard_granularity = stripe;
5567 /*
5568 * unaligned part of discard request will be ignored, so can't
5569 * guarantee discard_zerors_data
5570 */
5571 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005572
NeilBrown05616be2012-05-21 09:27:00 +10005573 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005574 disk_stack_limits(mddev->gendisk, rdev->bdev,
5575 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005576 disk_stack_limits(mddev->gendisk, rdev->bdev,
5577 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005578 /*
5579 * discard_zeroes_data is required, otherwise data
5580 * could be lost. Consider a scenario: discard a stripe
5581 * (the stripe could be inconsistent if
5582 * discard_zeroes_data is 0); write one disk of the
5583 * stripe (the stripe could be inconsistent again
5584 * depending on which disks are used to calculate
5585 * parity); the disk is broken; The stripe data of this
5586 * disk is lost.
5587 */
5588 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5589 !bdev_get_queue(rdev->bdev)->
5590 limits.discard_zeroes_data)
5591 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10005592 }
Shaohua Li620125f2012-10-11 13:49:05 +11005593
5594 if (discard_supported &&
5595 mddev->queue->limits.max_discard_sectors >= stripe &&
5596 mddev->queue->limits.discard_granularity >= stripe)
5597 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5598 mddev->queue);
5599 else
5600 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5601 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 }
5603
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 return 0;
5605abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005606 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005607 print_raid5_conf(conf);
5608 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005610 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 return -EIO;
5612}
5613
NeilBrownfd01b882011-10-11 16:47:53 +11005614static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615{
NeilBrownd1688a62011-10-11 16:49:52 +11005616 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
NeilBrown01f96c02011-09-21 15:30:20 +10005618 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005619 if (mddev->queue)
5620 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005621 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005622 mddev->private = NULL;
5623 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 return 0;
5625}
5626
NeilBrownfd01b882011-10-11 16:47:53 +11005627static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628{
NeilBrownd1688a62011-10-11 16:49:52 +11005629 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 int i;
5631
Andre Noll9d8f0362009-06-18 08:45:01 +10005632 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5633 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005634 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 for (i = 0; i < conf->raid_disks; i++)
5636 seq_printf (seq, "%s",
5637 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005638 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640}
5641
NeilBrownd1688a62011-10-11 16:49:52 +11005642static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643{
5644 int i;
5645 struct disk_info *tmp;
5646
NeilBrown0c55e022010-05-03 14:09:02 +10005647 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 if (!conf) {
5649 printk("(conf==NULL)\n");
5650 return;
5651 }
NeilBrown0c55e022010-05-03 14:09:02 +10005652 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5653 conf->raid_disks,
5654 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655
5656 for (i = 0; i < conf->raid_disks; i++) {
5657 char b[BDEVNAME_SIZE];
5658 tmp = conf->disks + i;
5659 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005660 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5661 i, !test_bit(Faulty, &tmp->rdev->flags),
5662 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 }
5664}
5665
NeilBrownfd01b882011-10-11 16:47:53 +11005666static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667{
5668 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005669 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005671 int count = 0;
5672 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673
5674 for (i = 0; i < conf->raid_disks; i++) {
5675 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005676 if (tmp->replacement
5677 && tmp->replacement->recovery_offset == MaxSector
5678 && !test_bit(Faulty, &tmp->replacement->flags)
5679 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5680 /* Replacement has just become active. */
5681 if (!tmp->rdev
5682 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5683 count++;
5684 if (tmp->rdev) {
5685 /* Replaced device not technically faulty,
5686 * but we need to be sure it gets removed
5687 * and never re-added.
5688 */
5689 set_bit(Faulty, &tmp->rdev->flags);
5690 sysfs_notify_dirent_safe(
5691 tmp->rdev->sysfs_state);
5692 }
5693 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5694 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005695 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005696 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005697 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005698 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005699 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 }
5701 }
NeilBrown6b965622010-08-18 11:56:59 +10005702 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005703 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005704 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005706 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707}
5708
NeilBrownb8321b62011-12-23 10:17:51 +11005709static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710{
NeilBrownd1688a62011-10-11 16:49:52 +11005711 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005713 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005714 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 struct disk_info *p = conf->disks + number;
5716
5717 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005718 if (rdev == p->rdev)
5719 rdevp = &p->rdev;
5720 else if (rdev == p->replacement)
5721 rdevp = &p->replacement;
5722 else
5723 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005724
NeilBrown657e3e42011-12-23 10:17:52 +11005725 if (number >= conf->raid_disks &&
5726 conf->reshape_progress == MaxSector)
5727 clear_bit(In_sync, &rdev->flags);
5728
5729 if (test_bit(In_sync, &rdev->flags) ||
5730 atomic_read(&rdev->nr_pending)) {
5731 err = -EBUSY;
5732 goto abort;
5733 }
5734 /* Only remove non-faulty devices if recovery
5735 * isn't possible.
5736 */
5737 if (!test_bit(Faulty, &rdev->flags) &&
5738 mddev->recovery_disabled != conf->recovery_disabled &&
5739 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005740 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005741 number < conf->raid_disks) {
5742 err = -EBUSY;
5743 goto abort;
5744 }
5745 *rdevp = NULL;
5746 synchronize_rcu();
5747 if (atomic_read(&rdev->nr_pending)) {
5748 /* lost the race, try later */
5749 err = -EBUSY;
5750 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005751 } else if (p->replacement) {
5752 /* We must have just cleared 'rdev' */
5753 p->rdev = p->replacement;
5754 clear_bit(Replacement, &p->replacement->flags);
5755 smp_mb(); /* Make sure other CPUs may see both as identical
5756 * but will never see neither - if they are careful
5757 */
5758 p->replacement = NULL;
5759 clear_bit(WantReplacement, &rdev->flags);
5760 } else
5761 /* We might have just removed the Replacement as faulty-
5762 * clear the bit just in case
5763 */
5764 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765abort:
5766
5767 print_raid5_conf(conf);
5768 return err;
5769}
5770
NeilBrownfd01b882011-10-11 16:47:53 +11005771static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772{
NeilBrownd1688a62011-10-11 16:49:52 +11005773 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005774 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 int disk;
5776 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005777 int first = 0;
5778 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779
NeilBrown7f0da592011-07-28 11:39:22 +10005780 if (mddev->recovery_disabled == conf->recovery_disabled)
5781 return -EBUSY;
5782
NeilBrowndc10c642012-03-19 12:46:37 +11005783 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005785 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786
Neil Brown6c2fce22008-06-28 08:31:31 +10005787 if (rdev->raid_disk >= 0)
5788 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789
5790 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005791 * find the disk ... but prefer rdev->saved_raid_disk
5792 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005794 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005795 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005796 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005797 first = rdev->saved_raid_disk;
5798
5799 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005800 p = conf->disks + disk;
5801 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005802 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005804 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005805 if (rdev->saved_raid_disk != disk)
5806 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005807 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005808 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005810 }
5811 for (disk = first; disk <= last; disk++) {
5812 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005813 if (test_bit(WantReplacement, &p->rdev->flags) &&
5814 p->replacement == NULL) {
5815 clear_bit(In_sync, &rdev->flags);
5816 set_bit(Replacement, &rdev->flags);
5817 rdev->raid_disk = disk;
5818 err = 0;
5819 conf->fullsync = 1;
5820 rcu_assign_pointer(p->replacement, rdev);
5821 break;
5822 }
5823 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005824out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005826 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827}
5828
NeilBrownfd01b882011-10-11 16:47:53 +11005829static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830{
5831 /* no resync is happening, and there is enough space
5832 * on all devices, so we can resize.
5833 * We need to make sure resync covers any new space.
5834 * If the array is shrinking we should possibly wait until
5835 * any io in the removed space completes, but it hardly seems
5836 * worth it.
5837 */
NeilBrowna4a61252012-05-22 13:55:27 +10005838 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005839 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005840 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5841 if (mddev->external_size &&
5842 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005843 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005844 if (mddev->bitmap) {
5845 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5846 if (ret)
5847 return ret;
5848 }
5849 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005850 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005851 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005852 if (sectors > mddev->dev_sectors &&
5853 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005854 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5856 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005857 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005858 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 return 0;
5860}
5861
NeilBrownfd01b882011-10-11 16:47:53 +11005862static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005863{
5864 /* Can only proceed if there are plenty of stripe_heads.
5865 * We need a minimum of one full stripe,, and for sensible progress
5866 * it is best to have about 4 times that.
5867 * If we require 4 times, then the default 256 4K stripe_heads will
5868 * allow for chunk sizes up to 256K, which is probably OK.
5869 * If the chunk size is greater, user-space should request more
5870 * stripe_heads first.
5871 */
NeilBrownd1688a62011-10-11 16:49:52 +11005872 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005873 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5874 > conf->max_nr_stripes ||
5875 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5876 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005877 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5878 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005879 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5880 / STRIPE_SIZE)*4);
5881 return 0;
5882 }
5883 return 1;
5884}
5885
NeilBrownfd01b882011-10-11 16:47:53 +11005886static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005887{
NeilBrownd1688a62011-10-11 16:49:52 +11005888 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005889
NeilBrown88ce4932009-03-31 15:24:23 +11005890 if (mddev->delta_disks == 0 &&
5891 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005892 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005893 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005894 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005895 return -EINVAL;
5896 if (mddev->delta_disks < 0) {
5897 /* We might be able to shrink, but the devices must
5898 * be made bigger first.
5899 * For raid6, 4 is the minimum size.
5900 * Otherwise 2 is the minimum
5901 */
5902 int min = 2;
5903 if (mddev->level == 6)
5904 min = 4;
5905 if (mddev->raid_disks + mddev->delta_disks < min)
5906 return -EINVAL;
5907 }
NeilBrown29269552006-03-27 01:18:10 -08005908
NeilBrown01ee22b2009-06-18 08:47:20 +10005909 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005910 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005911
NeilBrowne56108d62012-10-11 14:24:13 +11005912 return resize_stripes(conf, (conf->previous_raid_disks
5913 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08005914}
5915
NeilBrownfd01b882011-10-11 16:47:53 +11005916static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005917{
NeilBrownd1688a62011-10-11 16:49:52 +11005918 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005919 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005920 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005921 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005922
NeilBrownf4168852007-02-28 20:11:53 -08005923 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005924 return -EBUSY;
5925
NeilBrown01ee22b2009-06-18 08:47:20 +10005926 if (!check_stripe_cache(mddev))
5927 return -ENOSPC;
5928
NeilBrown30b67642012-05-22 13:55:28 +10005929 if (has_failed(conf))
5930 return -EINVAL;
5931
NeilBrownc6563a82012-05-21 09:27:00 +10005932 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005933 if (!test_bit(In_sync, &rdev->flags)
5934 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005935 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005936 }
NeilBrown63c70c42006-03-27 01:18:13 -08005937
NeilBrownf4168852007-02-28 20:11:53 -08005938 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005939 /* Not enough devices even to make a degraded array
5940 * of that size
5941 */
5942 return -EINVAL;
5943
NeilBrownec32a2b2009-03-31 15:17:38 +11005944 /* Refuse to reduce size of the array. Any reductions in
5945 * array size must be through explicit setting of array_size
5946 * attribute.
5947 */
5948 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5949 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005950 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005951 "before number of disks\n", mdname(mddev));
5952 return -EINVAL;
5953 }
5954
NeilBrownf6705572006-03-27 01:18:11 -08005955 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005956 spin_lock_irq(&conf->device_lock);
5957 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005958 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005959 conf->prev_chunk_sectors = conf->chunk_sectors;
5960 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005961 conf->prev_algo = conf->algorithm;
5962 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005963 conf->generation++;
5964 /* Code that selects data_offset needs to see the generation update
5965 * if reshape_progress has been set - so a memory barrier needed.
5966 */
5967 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005968 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005969 conf->reshape_progress = raid5_size(mddev, 0, 0);
5970 else
5971 conf->reshape_progress = 0;
5972 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005973 spin_unlock_irq(&conf->device_lock);
5974
5975 /* Add some new drives, as many as will fit.
5976 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005977 * Don't add devices if we are reducing the number of
5978 * devices in the array. This is because it is not possible
5979 * to correctly record the "partially reconstructed" state of
5980 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005981 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005982 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005983 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005984 if (rdev->raid_disk < 0 &&
5985 !test_bit(Faulty, &rdev->flags)) {
5986 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005987 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005988 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005989 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005990 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005991 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005992
5993 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005994 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005995 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005996 } else if (rdev->raid_disk >= conf->previous_raid_disks
5997 && !test_bit(Faulty, &rdev->flags)) {
5998 /* This is a spare that was manually added */
5999 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11006000 }
NeilBrown29269552006-03-27 01:18:10 -08006001
NeilBrown87a8dec2011-01-31 11:57:43 +11006002 /* When a reshape changes the number of devices,
6003 * ->degraded is measured against the larger of the
6004 * pre and post number of devices.
6005 */
NeilBrownec32a2b2009-03-31 15:17:38 +11006006 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11006007 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11006008 spin_unlock_irqrestore(&conf->device_lock, flags);
6009 }
NeilBrown63c70c42006-03-27 01:18:13 -08006010 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10006011 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b422006-10-03 01:15:46 -07006012 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08006013
NeilBrown29269552006-03-27 01:18:10 -08006014 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6015 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6016 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6017 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6018 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006019 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08006020 if (!mddev->sync_thread) {
6021 mddev->recovery = 0;
6022 spin_lock_irq(&conf->device_lock);
6023 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006024 rdev_for_each(rdev, mddev)
6025 rdev->new_data_offset = rdev->data_offset;
6026 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006027 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11006028 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08006029 spin_unlock_irq(&conf->device_lock);
6030 return -EAGAIN;
6031 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006032 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006033 md_wakeup_thread(mddev->sync_thread);
6034 md_new_event(mddev);
6035 return 0;
6036}
NeilBrown29269552006-03-27 01:18:10 -08006037
NeilBrownec32a2b2009-03-31 15:17:38 +11006038/* This is called from the reshape thread and should make any
6039 * changes needed in 'conf'
6040 */
NeilBrownd1688a62011-10-11 16:49:52 +11006041static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006042{
NeilBrown29269552006-03-27 01:18:10 -08006043
NeilBrownf6705572006-03-27 01:18:11 -08006044 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006045 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006046
NeilBrownf6705572006-03-27 01:18:11 -08006047 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006048 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006049 rdev_for_each(rdev, conf->mddev)
6050 rdev->data_offset = rdev->new_data_offset;
6051 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006052 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006053 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006054 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006055
6056 /* read-ahead size must cover two whole stripes, which is
6057 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6058 */
NeilBrown4a5add42010-06-01 19:37:28 +10006059 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006060 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006061 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006062 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006063 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6064 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6065 }
NeilBrown29269552006-03-27 01:18:10 -08006066 }
NeilBrown29269552006-03-27 01:18:10 -08006067}
6068
NeilBrownec32a2b2009-03-31 15:17:38 +11006069/* This is called from the raid5d thread with mddev_lock held.
6070 * It makes config changes to the device.
6071 */
NeilBrownfd01b882011-10-11 16:47:53 +11006072static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006073{
NeilBrownd1688a62011-10-11 16:49:52 +11006074 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006075
6076 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6077
NeilBrownec32a2b2009-03-31 15:17:38 +11006078 if (mddev->delta_disks > 0) {
6079 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6080 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006081 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006082 } else {
6083 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006084 spin_lock_irq(&conf->device_lock);
6085 mddev->degraded = calc_degraded(conf);
6086 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006087 for (d = conf->raid_disks ;
6088 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006089 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006090 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006091 if (rdev)
6092 clear_bit(In_sync, &rdev->flags);
6093 rdev = conf->disks[d].replacement;
6094 if (rdev)
6095 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006096 }
NeilBrowncea9c222009-03-31 15:15:05 +11006097 }
NeilBrown88ce4932009-03-31 15:24:23 +11006098 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006099 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006100 mddev->reshape_position = MaxSector;
6101 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006102 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006103 }
6104}
6105
NeilBrownfd01b882011-10-11 16:47:53 +11006106static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006107{
NeilBrownd1688a62011-10-11 16:49:52 +11006108 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006109
6110 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006111 case 2: /* resume for a suspend */
6112 wake_up(&conf->wait_for_overlap);
6113 break;
6114
NeilBrown72626682005-09-09 16:23:54 -07006115 case 1: /* stop all writes */
6116 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006117 /* '2' tells resync/reshape to pause so that all
6118 * active stripes can drain
6119 */
6120 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006121 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006122 atomic_read(&conf->active_stripes) == 0 &&
6123 atomic_read(&conf->active_aligned_reads) == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01006124 conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006125 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006126 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006127 /* allow reshape to continue */
6128 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006129 break;
6130
6131 case 0: /* re-enable writes */
6132 spin_lock_irq(&conf->device_lock);
6133 conf->quiesce = 0;
6134 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006135 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006136 spin_unlock_irq(&conf->device_lock);
6137 break;
6138 }
NeilBrown72626682005-09-09 16:23:54 -07006139}
NeilBrownb15c2e52006-01-06 00:20:16 -08006140
NeilBrownd562b0c2009-03-31 14:39:39 +11006141
NeilBrownfd01b882011-10-11 16:47:53 +11006142static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006143{
NeilBrowne373ab12011-10-11 16:48:59 +11006144 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006145 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006146
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006147 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006148 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006149 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6150 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006151 return ERR_PTR(-EINVAL);
6152 }
6153
NeilBrowne373ab12011-10-11 16:48:59 +11006154 sectors = raid0_conf->strip_zone[0].zone_end;
6155 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006156 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006157 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006158 mddev->new_layout = ALGORITHM_PARITY_N;
6159 mddev->new_chunk_sectors = mddev->chunk_sectors;
6160 mddev->raid_disks += 1;
6161 mddev->delta_disks = 1;
6162 /* make sure it will be not marked as dirty */
6163 mddev->recovery_cp = MaxSector;
6164
6165 return setup_conf(mddev);
6166}
6167
6168
NeilBrownfd01b882011-10-11 16:47:53 +11006169static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006170{
6171 int chunksect;
6172
6173 if (mddev->raid_disks != 2 ||
6174 mddev->degraded > 1)
6175 return ERR_PTR(-EINVAL);
6176
6177 /* Should check if there are write-behind devices? */
6178
6179 chunksect = 64*2; /* 64K by default */
6180
6181 /* The array must be an exact multiple of chunksize */
6182 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6183 chunksect >>= 1;
6184
6185 if ((chunksect<<9) < STRIPE_SIZE)
6186 /* array size does not allow a suitable chunk size */
6187 return ERR_PTR(-EINVAL);
6188
6189 mddev->new_level = 5;
6190 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006191 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006192
6193 return setup_conf(mddev);
6194}
6195
NeilBrownfd01b882011-10-11 16:47:53 +11006196static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006197{
6198 int new_layout;
6199
6200 switch (mddev->layout) {
6201 case ALGORITHM_LEFT_ASYMMETRIC_6:
6202 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6203 break;
6204 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6205 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6206 break;
6207 case ALGORITHM_LEFT_SYMMETRIC_6:
6208 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6209 break;
6210 case ALGORITHM_RIGHT_SYMMETRIC_6:
6211 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6212 break;
6213 case ALGORITHM_PARITY_0_6:
6214 new_layout = ALGORITHM_PARITY_0;
6215 break;
6216 case ALGORITHM_PARITY_N:
6217 new_layout = ALGORITHM_PARITY_N;
6218 break;
6219 default:
6220 return ERR_PTR(-EINVAL);
6221 }
6222 mddev->new_level = 5;
6223 mddev->new_layout = new_layout;
6224 mddev->delta_disks = -1;
6225 mddev->raid_disks -= 1;
6226 return setup_conf(mddev);
6227}
6228
NeilBrownd562b0c2009-03-31 14:39:39 +11006229
NeilBrownfd01b882011-10-11 16:47:53 +11006230static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006231{
NeilBrown88ce4932009-03-31 15:24:23 +11006232 /* For a 2-drive array, the layout and chunk size can be changed
6233 * immediately as not restriping is needed.
6234 * For larger arrays we record the new value - after validation
6235 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006236 */
NeilBrownd1688a62011-10-11 16:49:52 +11006237 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006238 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006239
NeilBrown597a7112009-06-18 08:47:42 +10006240 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006241 return -EINVAL;
6242 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006243 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006244 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006245 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006246 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006247 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006248 /* not factor of array size */
6249 return -EINVAL;
6250 }
6251
6252 /* They look valid */
6253
NeilBrown88ce4932009-03-31 15:24:23 +11006254 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006255 /* can make the change immediately */
6256 if (mddev->new_layout >= 0) {
6257 conf->algorithm = mddev->new_layout;
6258 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006259 }
6260 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006261 conf->chunk_sectors = new_chunk ;
6262 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006263 }
6264 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6265 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006266 }
NeilBrown50ac1682009-06-18 08:47:55 +10006267 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006268}
6269
NeilBrownfd01b882011-10-11 16:47:53 +11006270static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006271{
NeilBrown597a7112009-06-18 08:47:42 +10006272 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006273
NeilBrown597a7112009-06-18 08:47:42 +10006274 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006275 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006276 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006277 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006278 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006279 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006280 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006281 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006282 /* not factor of array size */
6283 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006284 }
NeilBrown88ce4932009-03-31 15:24:23 +11006285
6286 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006287 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006288}
6289
NeilBrownfd01b882011-10-11 16:47:53 +11006290static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006291{
6292 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006293 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006294 * raid1 - if there are two drives. We need to know the chunk size
6295 * raid4 - trivial - just use a raid4 layout.
6296 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006297 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006298 if (mddev->level == 0)
6299 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006300 if (mddev->level == 1)
6301 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006302 if (mddev->level == 4) {
6303 mddev->new_layout = ALGORITHM_PARITY_N;
6304 mddev->new_level = 5;
6305 return setup_conf(mddev);
6306 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006307 if (mddev->level == 6)
6308 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006309
6310 return ERR_PTR(-EINVAL);
6311}
6312
NeilBrownfd01b882011-10-11 16:47:53 +11006313static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006314{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006315 /* raid4 can take over:
6316 * raid0 - if there is only one strip zone
6317 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006318 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006319 if (mddev->level == 0)
6320 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006321 if (mddev->level == 5 &&
6322 mddev->layout == ALGORITHM_PARITY_N) {
6323 mddev->new_layout = 0;
6324 mddev->new_level = 4;
6325 return setup_conf(mddev);
6326 }
6327 return ERR_PTR(-EINVAL);
6328}
NeilBrownd562b0c2009-03-31 14:39:39 +11006329
NeilBrown84fc4b52011-10-11 16:49:58 +11006330static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006331
NeilBrownfd01b882011-10-11 16:47:53 +11006332static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006333{
6334 /* Currently can only take over a raid5. We map the
6335 * personality to an equivalent raid6 personality
6336 * with the Q block at the end.
6337 */
6338 int new_layout;
6339
6340 if (mddev->pers != &raid5_personality)
6341 return ERR_PTR(-EINVAL);
6342 if (mddev->degraded > 1)
6343 return ERR_PTR(-EINVAL);
6344 if (mddev->raid_disks > 253)
6345 return ERR_PTR(-EINVAL);
6346 if (mddev->raid_disks < 3)
6347 return ERR_PTR(-EINVAL);
6348
6349 switch (mddev->layout) {
6350 case ALGORITHM_LEFT_ASYMMETRIC:
6351 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6352 break;
6353 case ALGORITHM_RIGHT_ASYMMETRIC:
6354 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6355 break;
6356 case ALGORITHM_LEFT_SYMMETRIC:
6357 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6358 break;
6359 case ALGORITHM_RIGHT_SYMMETRIC:
6360 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6361 break;
6362 case ALGORITHM_PARITY_0:
6363 new_layout = ALGORITHM_PARITY_0_6;
6364 break;
6365 case ALGORITHM_PARITY_N:
6366 new_layout = ALGORITHM_PARITY_N;
6367 break;
6368 default:
6369 return ERR_PTR(-EINVAL);
6370 }
6371 mddev->new_level = 6;
6372 mddev->new_layout = new_layout;
6373 mddev->delta_disks = 1;
6374 mddev->raid_disks += 1;
6375 return setup_conf(mddev);
6376}
6377
6378
NeilBrown84fc4b52011-10-11 16:49:58 +11006379static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006380{
6381 .name = "raid6",
6382 .level = 6,
6383 .owner = THIS_MODULE,
6384 .make_request = make_request,
6385 .run = run,
6386 .stop = stop,
6387 .status = status,
6388 .error_handler = error,
6389 .hot_add_disk = raid5_add_disk,
6390 .hot_remove_disk= raid5_remove_disk,
6391 .spare_active = raid5_spare_active,
6392 .sync_request = sync_request,
6393 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006394 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006395 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006396 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006397 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006398 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006399 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006400};
NeilBrown84fc4b52011-10-11 16:49:58 +11006401static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402{
6403 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006404 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405 .owner = THIS_MODULE,
6406 .make_request = make_request,
6407 .run = run,
6408 .stop = stop,
6409 .status = status,
6410 .error_handler = error,
6411 .hot_add_disk = raid5_add_disk,
6412 .hot_remove_disk= raid5_remove_disk,
6413 .spare_active = raid5_spare_active,
6414 .sync_request = sync_request,
6415 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006416 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006417 .check_reshape = raid5_check_reshape,
6418 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006419 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006420 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006421 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422};
6423
NeilBrown84fc4b52011-10-11 16:49:58 +11006424static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425{
NeilBrown2604b702006-01-06 00:20:36 -08006426 .name = "raid4",
6427 .level = 4,
6428 .owner = THIS_MODULE,
6429 .make_request = make_request,
6430 .run = run,
6431 .stop = stop,
6432 .status = status,
6433 .error_handler = error,
6434 .hot_add_disk = raid5_add_disk,
6435 .hot_remove_disk= raid5_remove_disk,
6436 .spare_active = raid5_spare_active,
6437 .sync_request = sync_request,
6438 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006439 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006440 .check_reshape = raid5_check_reshape,
6441 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006442 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006443 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006444 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006445};
6446
6447static int __init raid5_init(void)
6448{
NeilBrown16a53ec2006-06-26 00:27:38 -07006449 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006450 register_md_personality(&raid5_personality);
6451 register_md_personality(&raid4_personality);
6452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453}
6454
NeilBrown2604b702006-01-06 00:20:36 -08006455static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456{
NeilBrown16a53ec2006-06-26 00:27:38 -07006457 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006458 unregister_md_personality(&raid5_personality);
6459 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460}
6461
6462module_init(raid5_init);
6463module_exit(raid5_exit);
6464MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006465MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006467MODULE_ALIAS("md-raid5");
6468MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006469MODULE_ALIAS("md-level-5");
6470MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006471MODULE_ALIAS("md-personality-8"); /* RAID6 */
6472MODULE_ALIAS("md-raid6");
6473MODULE_ALIAS("md-level-6");
6474
6475/* This used to be two separate modules, they were: */
6476MODULE_ALIAS("raid5");
6477MODULE_ALIAS("raid6");