blob: 0fb988556eea4f67bc0db2f62d0694cbe61dc32e [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>
NeilBrown43b2e5d2009-03-31 14:33:13 +110056#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110057#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110058#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110059#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * Stripe cache
63 */
64
65#define NR_STRIPES 256
66#define STRIPE_SIZE PAGE_SIZE
67#define STRIPE_SHIFT (PAGE_SHIFT - 9)
68#define STRIPE_SECTORS (STRIPE_SIZE>>9)
69#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070070#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080071#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define HASH_MASK (NR_HASH - 1)
73
NeilBrownd1688a62011-10-11 16:49:52 +110074static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110075{
76 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
77 return &conf->stripe_hashtbl[hash];
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* bio's attached to a stripe+device for I/O are linked together in bi_sector
81 * order without overlap. There may be several bio's per stripe+device, and
82 * a bio could span several devices.
83 * When walking this list for a particular stripe+device, we must never proceed
84 * beyond a bio that extends past this device, as the next bio might no longer
85 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110086 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * of the current stripe+device
88 */
NeilBrowndb298e12011-10-07 14:23:00 +110089static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
90{
91 int sectors = bio->bi_size >> 9;
92 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
93 return bio->bi_next;
94 else
95 return NULL;
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jens Axboe960e7392008-08-15 10:41:18 +020098/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +020099 * We maintain a biased count of active stripes in the bottom 16 bits of
100 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200101 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000102static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200103{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000104 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
105 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200106}
107
Shaohua Lie7836bd62012-07-19 16:01:31 +1000108static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200109{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000110 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
111 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200112}
113
Shaohua Lie7836bd62012-07-19 16:01:31 +1000114static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200115{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000116 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
117 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200118}
119
Shaohua Lie7836bd62012-07-19 16:01:31 +1000120static inline void raid5_set_bi_processed_stripes(struct bio *bio,
121 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200122{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000123 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
124 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200125
Shaohua Lie7836bd62012-07-19 16:01:31 +1000126 do {
127 old = atomic_read(segments);
128 new = (old & 0xffff) | (cnt << 16);
129 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200130}
131
Shaohua Lie7836bd62012-07-19 16:01:31 +1000132static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200133{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000134 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
135 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200136}
137
NeilBrownd0dabf72009-03-31 14:39:38 +1100138/* Find first data disk in a raid6 stripe */
139static inline int raid6_d0(struct stripe_head *sh)
140{
NeilBrown67cc2b82009-03-31 14:39:38 +1100141 if (sh->ddf_layout)
142 /* ddf always start from first device */
143 return 0;
144 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100145 if (sh->qd_idx == sh->disks - 1)
146 return 0;
147 else
148 return sh->qd_idx + 1;
149}
NeilBrown16a53ec2006-06-26 00:27:38 -0700150static inline int raid6_next_disk(int disk, int raid_disks)
151{
152 disk++;
153 return (disk < raid_disks) ? disk : 0;
154}
Dan Williamsa4456852007-07-09 11:56:43 -0700155
NeilBrownd0dabf72009-03-31 14:39:38 +1100156/* When walking through the disks in a raid5, starting at raid6_d0,
157 * We need to map each disk to a 'slot', where the data disks are slot
158 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
159 * is raid_disks-1. This help does that mapping.
160 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100161static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
162 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100163{
Dan Williams66295422009-10-19 18:09:32 -0700164 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100165
NeilBrowne4424fe2009-10-16 16:27:34 +1100166 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700167 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100168 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100169 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100170 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100171 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100172 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700173 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100174 return slot;
175}
176
Dan Williamsa4456852007-07-09 11:56:43 -0700177static void return_io(struct bio *return_bi)
178{
179 struct bio *bi = return_bi;
180 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700181
182 return_bi = bi->bi_next;
183 bi->bi_next = NULL;
184 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000185 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700186 bi = return_bi;
187 }
188}
189
NeilBrownd1688a62011-10-11 16:49:52 +1100190static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Dan Williams600aa102008-06-28 08:32:05 +1000192static int stripe_operations_active(struct stripe_head *sh)
193{
194 return sh->check_state || sh->reconstruct_state ||
195 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
196 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
197}
198
Shaohua Li4eb788d2012-07-19 16:01:31 +1000199static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000201 BUG_ON(!list_empty(&sh->lru));
202 BUG_ON(atomic_read(&conf->active_stripes)==0);
203 if (test_bit(STRIPE_HANDLE, &sh->state)) {
204 if (test_bit(STRIPE_DELAYED, &sh->state) &&
205 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
206 list_add_tail(&sh->lru, &conf->delayed_list);
207 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
208 sh->bm_seq - conf->seq_write > 0)
209 list_add_tail(&sh->lru, &conf->bitmap_list);
210 else {
211 clear_bit(STRIPE_DELAYED, &sh->state);
212 clear_bit(STRIPE_BIT_DELAY, &sh->state);
213 list_add_tail(&sh->lru, &conf->handle_list);
214 }
215 md_wakeup_thread(conf->mddev->thread);
216 } else {
217 BUG_ON(stripe_operations_active(sh));
218 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
219 if (atomic_dec_return(&conf->preread_active_stripes)
220 < IO_THRESHOLD)
221 md_wakeup_thread(conf->mddev->thread);
222 atomic_dec(&conf->active_stripes);
223 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
224 list_add_tail(&sh->lru, &conf->inactive_list);
225 wake_up(&conf->wait_for_stripe);
226 if (conf->retry_read_aligned)
227 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 }
230}
NeilBrownd0dabf72009-03-31 14:39:38 +1100231
Shaohua Li4eb788d2012-07-19 16:01:31 +1000232static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
233{
234 if (atomic_dec_and_test(&sh->count))
235 do_release_stripe(conf, sh);
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static void release_stripe(struct stripe_head *sh)
239{
NeilBrownd1688a62011-10-11 16:49:52 +1100240 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700242
Shaohua Li4eb788d2012-07-19 16:01:31 +1000243 local_irq_save(flags);
244 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
245 do_release_stripe(conf, sh);
246 spin_unlock(&conf->device_lock);
247 }
248 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
NeilBrownfccddba2006-01-06 00:20:33 -0800251static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Dan Williams45b42332007-07-09 11:56:43 -0700253 pr_debug("remove_hash(), stripe %llu\n",
254 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
NeilBrownfccddba2006-01-06 00:20:33 -0800256 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
NeilBrownd1688a62011-10-11 16:49:52 +1100259static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
NeilBrownfccddba2006-01-06 00:20:33 -0800261 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Dan Williams45b42332007-07-09 11:56:43 -0700263 pr_debug("insert_hash(), stripe %llu\n",
264 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
NeilBrownfccddba2006-01-06 00:20:33 -0800266 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269
270/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100271static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct stripe_head *sh = NULL;
274 struct list_head *first;
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (list_empty(&conf->inactive_list))
277 goto out;
278 first = conf->inactive_list.next;
279 sh = list_entry(first, struct stripe_head, lru);
280 list_del_init(first);
281 remove_hash(sh);
282 atomic_inc(&conf->active_stripes);
283out:
284 return sh;
285}
286
NeilBrowne4e11e32010-06-16 16:45:16 +1000287static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct page *p;
290 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000291 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
NeilBrowne4e11e32010-06-16 16:45:16 +1000293 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 p = sh->dev[i].page;
295 if (!p)
296 continue;
297 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800298 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300}
301
NeilBrowne4e11e32010-06-16 16:45:16 +1000302static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000305 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
NeilBrowne4e11e32010-06-16 16:45:16 +1000307 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct page *page;
309
310 if (!(page = alloc_page(GFP_KERNEL))) {
311 return 1;
312 }
313 sh->dev[i].page = page;
314 }
315 return 0;
316}
317
NeilBrown784052e2009-03-31 15:19:07 +1100318static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100319static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100320 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
NeilBrownb5663ba2009-03-31 14:39:38 +1100322static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
NeilBrownd1688a62011-10-11 16:49:52 +1100324 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800325 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200327 BUG_ON(atomic_read(&sh->count) != 0);
328 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000329 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700330
Dan Williams45b42332007-07-09 11:56:43 -0700331 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 (unsigned long long)sh->sector);
333
334 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700335
NeilBrown86b42c72009-03-31 15:19:03 +1100336 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100337 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100339 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 sh->state = 0;
341
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800342
343 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct r5dev *dev = &sh->dev[i];
345
Dan Williamsd84e0f12007-01-02 13:52:30 -0700346 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700348 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700350 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000352 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100355 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357 insert_hash(conf, sh);
358}
359
NeilBrownd1688a62011-10-11 16:49:52 +1100360static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100361 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800364 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Dan Williams45b42332007-07-09 11:56:43 -0700366 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800367 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100368 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700370 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return NULL;
372}
373
NeilBrown674806d2010-06-16 17:17:53 +1000374/*
375 * Need to check if array has failed when deciding whether to:
376 * - start an array
377 * - remove non-faulty devices
378 * - add a spare
379 * - allow a reshape
380 * This determination is simple when no reshape is happening.
381 * However if there is a reshape, we need to carefully check
382 * both the before and after sections.
383 * This is because some failed devices may only affect one
384 * of the two sections, and some non-in_sync devices may
385 * be insync in the section most affected by failed devices.
386 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100387static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000388{
NeilBrown908f4fb2011-12-23 10:17:50 +1100389 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000390 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000391
392 rcu_read_lock();
393 degraded = 0;
394 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100395 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000396 if (rdev && test_bit(Faulty, &rdev->flags))
397 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000398 if (!rdev || test_bit(Faulty, &rdev->flags))
399 degraded++;
400 else if (test_bit(In_sync, &rdev->flags))
401 ;
402 else
403 /* not in-sync or faulty.
404 * If the reshape increases the number of devices,
405 * this is being recovered by the reshape, so
406 * this 'previous' section is not in_sync.
407 * If the number of devices is being reduced however,
408 * the device can only be part of the array if
409 * we are reverting a reshape, so this section will
410 * be in-sync.
411 */
412 if (conf->raid_disks >= conf->previous_raid_disks)
413 degraded++;
414 }
415 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100416 if (conf->raid_disks == conf->previous_raid_disks)
417 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000418 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100419 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000420 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100421 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000422 if (rdev && test_bit(Faulty, &rdev->flags))
423 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000424 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100425 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000426 else if (test_bit(In_sync, &rdev->flags))
427 ;
428 else
429 /* not in-sync or faulty.
430 * If reshape increases the number of devices, this
431 * section has already been recovered, else it
432 * almost certainly hasn't.
433 */
434 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100435 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000436 }
437 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100438 if (degraded2 > degraded)
439 return degraded2;
440 return degraded;
441}
442
443static int has_failed(struct r5conf *conf)
444{
445 int degraded;
446
447 if (conf->mddev->reshape_position == MaxSector)
448 return conf->mddev->degraded > conf->max_degraded;
449
450 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000451 if (degraded > conf->max_degraded)
452 return 1;
453 return 0;
454}
455
NeilBrownb5663ba2009-03-31 14:39:38 +1100456static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100457get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000458 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct stripe_head *sh;
461
Dan Williams45b42332007-07-09 11:56:43 -0700462 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 spin_lock_irq(&conf->device_lock);
465
466 do {
NeilBrown72626682005-09-09 16:23:54 -0700467 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000468 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700469 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100470 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (!sh) {
472 if (!conf->inactive_blocked)
473 sh = get_free_stripe(conf);
474 if (noblock && sh == NULL)
475 break;
476 if (!sh) {
477 conf->inactive_blocked = 1;
478 wait_event_lock_irq(conf->wait_for_stripe,
479 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800480 (atomic_read(&conf->active_stripes)
481 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 || !conf->inactive_blocked),
483 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000484 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 conf->inactive_blocked = 0;
486 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100487 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 } else {
489 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100490 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000491 && !test_bit(STRIPE_EXPANDING, &sh->state)
492 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 } else {
494 if (!test_bit(STRIPE_HANDLE, &sh->state))
495 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700496 if (list_empty(&sh->lru) &&
497 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700498 BUG();
499 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501 }
502 } while (sh == NULL);
503
504 if (sh)
505 atomic_inc(&sh->count);
506
507 spin_unlock_irq(&conf->device_lock);
508 return sh;
509}
510
NeilBrown05616be2012-05-21 09:27:00 +1000511/* Determine if 'data_offset' or 'new_data_offset' should be used
512 * in this stripe_head.
513 */
514static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
515{
516 sector_t progress = conf->reshape_progress;
517 /* Need a memory barrier to make sure we see the value
518 * of conf->generation, or ->data_offset that was set before
519 * reshape_progress was updated.
520 */
521 smp_rmb();
522 if (progress == MaxSector)
523 return 0;
524 if (sh->generation == conf->generation - 1)
525 return 0;
526 /* We are in a reshape, and this is a new-generation stripe,
527 * so use new_data_offset.
528 */
529 return 1;
530}
531
NeilBrown6712ecf2007-09-27 12:47:43 +0200532static void
533raid5_end_read_request(struct bio *bi, int error);
534static void
535raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700536
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000537static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700538{
NeilBrownd1688a62011-10-11 16:49:52 +1100539 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700540 int i, disks = sh->disks;
541
542 might_sleep();
543
544 for (i = disks; i--; ) {
545 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100546 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100547 struct bio *bi, *rbi;
548 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200549 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
550 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
551 rw = WRITE_FUA;
552 else
553 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100554 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100555 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200556 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700557 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100558 else if (test_and_clear_bit(R5_WantReplace,
559 &sh->dev[i].flags)) {
560 rw = WRITE;
561 replace_only = 1;
562 } else
Dan Williams91c00922007-01-02 13:52:30 -0700563 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000564 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
565 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700566
567 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100568 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700569
570 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100571 rbi->bi_rw = rw;
572 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700573 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100574 rbi->bi_end_io = raid5_end_write_request;
575 } else
Dan Williams91c00922007-01-02 13:52:30 -0700576 bi->bi_end_io = raid5_end_read_request;
577
578 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100579 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100580 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
581 rdev = rcu_dereference(conf->disks[i].rdev);
582 if (!rdev) {
583 rdev = rrdev;
584 rrdev = NULL;
585 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100586 if (rw & WRITE) {
587 if (replace_only)
588 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100589 if (rdev == rrdev)
590 /* We raced and saw duplicates */
591 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100592 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100593 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100594 rdev = rrdev;
595 rrdev = NULL;
596 }
NeilBrown977df362011-12-23 10:17:53 +1100597
Dan Williams91c00922007-01-02 13:52:30 -0700598 if (rdev && test_bit(Faulty, &rdev->flags))
599 rdev = NULL;
600 if (rdev)
601 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100602 if (rrdev && test_bit(Faulty, &rrdev->flags))
603 rrdev = NULL;
604 if (rrdev)
605 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700606 rcu_read_unlock();
607
NeilBrown73e92e52011-07-28 11:39:22 +1000608 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100609 * need to check for writes. We never accept write errors
610 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000611 */
612 while ((rw & WRITE) && rdev &&
613 test_bit(WriteErrorSeen, &rdev->flags)) {
614 sector_t first_bad;
615 int bad_sectors;
616 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
617 &first_bad, &bad_sectors);
618 if (!bad)
619 break;
620
621 if (bad < 0) {
622 set_bit(BlockedBadBlocks, &rdev->flags);
623 if (!conf->mddev->external &&
624 conf->mddev->flags) {
625 /* It is very unlikely, but we might
626 * still need to write out the
627 * bad block log - better give it
628 * a chance*/
629 md_check_recovery(conf->mddev);
630 }
majianpeng18507532012-07-03 12:11:54 +1000631 /*
632 * Because md_wait_for_blocked_rdev
633 * will dec nr_pending, we must
634 * increment it first.
635 */
636 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000637 md_wait_for_blocked_rdev(rdev, conf->mddev);
638 } else {
639 /* Acknowledged bad block - skip the write */
640 rdev_dec_pending(rdev, conf->mddev);
641 rdev = NULL;
642 }
643 }
644
Dan Williams91c00922007-01-02 13:52:30 -0700645 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100646 if (s->syncing || s->expanding || s->expanded
647 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700648 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
649
Dan Williams2b7497f2008-06-28 08:31:52 +1000650 set_bit(STRIPE_IO_STARTED, &sh->state);
651
Dan Williams91c00922007-01-02 13:52:30 -0700652 bi->bi_bdev = rdev->bdev;
653 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700654 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700655 bi->bi_rw, i);
656 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000657 if (use_new_offset(conf, sh))
658 bi->bi_sector = (sh->sector
659 + rdev->new_data_offset);
660 else
661 bi->bi_sector = (sh->sector
662 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000663 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
664 bi->bi_rw |= REQ_FLUSH;
665
Dan Williams91c00922007-01-02 13:52:30 -0700666 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700667 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700668 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
669 bi->bi_io_vec[0].bv_offset = 0;
670 bi->bi_size = STRIPE_SIZE;
671 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100672 if (rrdev)
673 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700674 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100675 }
676 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100677 if (s->syncing || s->expanding || s->expanded
678 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100679 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
680
681 set_bit(STRIPE_IO_STARTED, &sh->state);
682
683 rbi->bi_bdev = rrdev->bdev;
684 pr_debug("%s: for %llu schedule op %ld on "
685 "replacement disc %d\n",
686 __func__, (unsigned long long)sh->sector,
687 rbi->bi_rw, i);
688 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000689 if (use_new_offset(conf, sh))
690 rbi->bi_sector = (sh->sector
691 + rrdev->new_data_offset);
692 else
693 rbi->bi_sector = (sh->sector
694 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100695 rbi->bi_flags = 1 << BIO_UPTODATE;
696 rbi->bi_idx = 0;
697 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
698 rbi->bi_io_vec[0].bv_offset = 0;
699 rbi->bi_size = STRIPE_SIZE;
700 rbi->bi_next = NULL;
701 generic_make_request(rbi);
702 }
703 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000704 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700705 set_bit(STRIPE_DEGRADED, &sh->state);
706 pr_debug("skip op %ld on disc %d for sector %llu\n",
707 bi->bi_rw, i, (unsigned long long)sh->sector);
708 clear_bit(R5_LOCKED, &sh->dev[i].flags);
709 set_bit(STRIPE_HANDLE, &sh->state);
710 }
711 }
712}
713
714static struct dma_async_tx_descriptor *
715async_copy_data(int frombio, struct bio *bio, struct page *page,
716 sector_t sector, struct dma_async_tx_descriptor *tx)
717{
718 struct bio_vec *bvl;
719 struct page *bio_page;
720 int i;
721 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700722 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700723 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700724
725 if (bio->bi_sector >= sector)
726 page_offset = (signed)(bio->bi_sector - sector) * 512;
727 else
728 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700729
Dan Williams0403e382009-09-08 17:42:50 -0700730 if (frombio)
731 flags |= ASYNC_TX_FENCE;
732 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
733
Dan Williams91c00922007-01-02 13:52:30 -0700734 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000735 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700736 int clen;
737 int b_offset = 0;
738
739 if (page_offset < 0) {
740 b_offset = -page_offset;
741 page_offset += b_offset;
742 len -= b_offset;
743 }
744
745 if (len > 0 && page_offset + len > STRIPE_SIZE)
746 clen = STRIPE_SIZE - page_offset;
747 else
748 clen = len;
749
750 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000751 b_offset += bvl->bv_offset;
752 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700753 if (frombio)
754 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700755 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700756 else
757 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700758 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700759 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700760 /* chain the operations */
761 submit.depend_tx = tx;
762
Dan Williams91c00922007-01-02 13:52:30 -0700763 if (clen < len) /* hit end of page */
764 break;
765 page_offset += len;
766 }
767
768 return tx;
769}
770
771static void ops_complete_biofill(void *stripe_head_ref)
772{
773 struct stripe_head *sh = stripe_head_ref;
774 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700775 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700776
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700777 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700778 (unsigned long long)sh->sector);
779
780 /* clear completed biofills */
781 for (i = sh->disks; i--; ) {
782 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700783
784 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700785 /* and check if we need to reply to a read request,
786 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000787 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700788 */
789 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700790 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700791
Dan Williams91c00922007-01-02 13:52:30 -0700792 BUG_ON(!dev->read);
793 rbi = dev->read;
794 dev->read = NULL;
795 while (rbi && rbi->bi_sector <
796 dev->sector + STRIPE_SECTORS) {
797 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000798 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700799 rbi->bi_next = return_bi;
800 return_bi = rbi;
801 }
Dan Williams91c00922007-01-02 13:52:30 -0700802 rbi = rbi2;
803 }
804 }
805 }
Dan Williams83de75c2008-06-28 08:31:58 +1000806 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700807
808 return_io(return_bi);
809
Dan Williamse4d84902007-09-24 10:06:13 -0700810 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700811 release_stripe(sh);
812}
813
814static void ops_run_biofill(struct stripe_head *sh)
815{
816 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700817 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700818 int i;
819
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700820 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700821 (unsigned long long)sh->sector);
822
823 for (i = sh->disks; i--; ) {
824 struct r5dev *dev = &sh->dev[i];
825 if (test_bit(R5_Wantfill, &dev->flags)) {
826 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000827 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700828 dev->read = rbi = dev->toread;
829 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000830 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700831 while (rbi && rbi->bi_sector <
832 dev->sector + STRIPE_SECTORS) {
833 tx = async_copy_data(0, rbi, dev->page,
834 dev->sector, tx);
835 rbi = r5_next_bio(rbi, dev->sector);
836 }
837 }
838 }
839
840 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700841 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
842 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700843}
844
Dan Williams4e7d2c02009-08-29 19:13:11 -0700845static void mark_target_uptodate(struct stripe_head *sh, int target)
846{
847 struct r5dev *tgt;
848
849 if (target < 0)
850 return;
851
852 tgt = &sh->dev[target];
853 set_bit(R5_UPTODATE, &tgt->flags);
854 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
855 clear_bit(R5_Wantcompute, &tgt->flags);
856}
857
Dan Williamsac6b53b2009-07-14 13:40:19 -0700858static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700859{
860 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700861
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700862 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700863 (unsigned long long)sh->sector);
864
Dan Williamsac6b53b2009-07-14 13:40:19 -0700865 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700866 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700867 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700868
Dan Williamsecc65c92008-06-28 08:31:57 +1000869 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
870 if (sh->check_state == check_state_compute_run)
871 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700872 set_bit(STRIPE_HANDLE, &sh->state);
873 release_stripe(sh);
874}
875
Dan Williamsd6f38f32009-07-14 11:50:52 -0700876/* return a pointer to the address conversion region of the scribble buffer */
877static addr_conv_t *to_addr_conv(struct stripe_head *sh,
878 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700879{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700880 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
881}
882
883static struct dma_async_tx_descriptor *
884ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
885{
Dan Williams91c00922007-01-02 13:52:30 -0700886 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700887 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700888 int target = sh->ops.target;
889 struct r5dev *tgt = &sh->dev[target];
890 struct page *xor_dest = tgt->page;
891 int count = 0;
892 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700893 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700894 int i;
895
896 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700897 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700898 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
899
900 for (i = disks; i--; )
901 if (i != target)
902 xor_srcs[count++] = sh->dev[i].page;
903
904 atomic_inc(&sh->count);
905
Dan Williams0403e382009-09-08 17:42:50 -0700906 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700907 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700908 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700909 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700910 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700911 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700912
Dan Williams91c00922007-01-02 13:52:30 -0700913 return tx;
914}
915
Dan Williamsac6b53b2009-07-14 13:40:19 -0700916/* set_syndrome_sources - populate source buffers for gen_syndrome
917 * @srcs - (struct page *) array of size sh->disks
918 * @sh - stripe_head to parse
919 *
920 * Populates srcs in proper layout order for the stripe and returns the
921 * 'count' of sources to be used in a call to async_gen_syndrome. The P
922 * destination buffer is recorded in srcs[count] and the Q destination
923 * is recorded in srcs[count+1]].
924 */
925static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
926{
927 int disks = sh->disks;
928 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
929 int d0_idx = raid6_d0(sh);
930 int count;
931 int i;
932
933 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100934 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700935
936 count = 0;
937 i = d0_idx;
938 do {
939 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
940
941 srcs[slot] = sh->dev[i].page;
942 i = raid6_next_disk(i, disks);
943 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700944
NeilBrowne4424fe2009-10-16 16:27:34 +1100945 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700946}
947
948static struct dma_async_tx_descriptor *
949ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
950{
951 int disks = sh->disks;
952 struct page **blocks = percpu->scribble;
953 int target;
954 int qd_idx = sh->qd_idx;
955 struct dma_async_tx_descriptor *tx;
956 struct async_submit_ctl submit;
957 struct r5dev *tgt;
958 struct page *dest;
959 int i;
960 int count;
961
962 if (sh->ops.target < 0)
963 target = sh->ops.target2;
964 else if (sh->ops.target2 < 0)
965 target = sh->ops.target;
966 else
967 /* we should only have one valid target */
968 BUG();
969 BUG_ON(target < 0);
970 pr_debug("%s: stripe %llu block: %d\n",
971 __func__, (unsigned long long)sh->sector, target);
972
973 tgt = &sh->dev[target];
974 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
975 dest = tgt->page;
976
977 atomic_inc(&sh->count);
978
979 if (target == qd_idx) {
980 count = set_syndrome_sources(blocks, sh);
981 blocks[count] = NULL; /* regenerating p is not necessary */
982 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700983 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
984 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700985 to_addr_conv(sh, percpu));
986 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
987 } else {
988 /* Compute any data- or p-drive using XOR */
989 count = 0;
990 for (i = disks; i-- ; ) {
991 if (i == target || i == qd_idx)
992 continue;
993 blocks[count++] = sh->dev[i].page;
994 }
995
Dan Williams0403e382009-09-08 17:42:50 -0700996 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
997 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700998 to_addr_conv(sh, percpu));
999 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
1000 }
1001
1002 return tx;
1003}
1004
1005static struct dma_async_tx_descriptor *
1006ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1007{
1008 int i, count, disks = sh->disks;
1009 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1010 int d0_idx = raid6_d0(sh);
1011 int faila = -1, failb = -1;
1012 int target = sh->ops.target;
1013 int target2 = sh->ops.target2;
1014 struct r5dev *tgt = &sh->dev[target];
1015 struct r5dev *tgt2 = &sh->dev[target2];
1016 struct dma_async_tx_descriptor *tx;
1017 struct page **blocks = percpu->scribble;
1018 struct async_submit_ctl submit;
1019
1020 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1021 __func__, (unsigned long long)sh->sector, target, target2);
1022 BUG_ON(target < 0 || target2 < 0);
1023 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1024 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1025
Dan Williams6c910a72009-09-16 12:24:54 -07001026 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001027 * slot number conversion for 'faila' and 'failb'
1028 */
1029 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001030 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001031 count = 0;
1032 i = d0_idx;
1033 do {
1034 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1035
1036 blocks[slot] = sh->dev[i].page;
1037
1038 if (i == target)
1039 faila = slot;
1040 if (i == target2)
1041 failb = slot;
1042 i = raid6_next_disk(i, disks);
1043 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001044
1045 BUG_ON(faila == failb);
1046 if (failb < faila)
1047 swap(faila, failb);
1048 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1049 __func__, (unsigned long long)sh->sector, faila, failb);
1050
1051 atomic_inc(&sh->count);
1052
1053 if (failb == syndrome_disks+1) {
1054 /* Q disk is one of the missing disks */
1055 if (faila == syndrome_disks) {
1056 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001057 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1058 ops_complete_compute, sh,
1059 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001060 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001061 STRIPE_SIZE, &submit);
1062 } else {
1063 struct page *dest;
1064 int data_target;
1065 int qd_idx = sh->qd_idx;
1066
1067 /* Missing D+Q: recompute D from P, then recompute Q */
1068 if (target == qd_idx)
1069 data_target = target2;
1070 else
1071 data_target = target;
1072
1073 count = 0;
1074 for (i = disks; i-- ; ) {
1075 if (i == data_target || i == qd_idx)
1076 continue;
1077 blocks[count++] = sh->dev[i].page;
1078 }
1079 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001080 init_async_submit(&submit,
1081 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1082 NULL, NULL, NULL,
1083 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001084 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1085 &submit);
1086
1087 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001088 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1089 ops_complete_compute, sh,
1090 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001091 return async_gen_syndrome(blocks, 0, count+2,
1092 STRIPE_SIZE, &submit);
1093 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001094 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001095 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1096 ops_complete_compute, sh,
1097 to_addr_conv(sh, percpu));
1098 if (failb == syndrome_disks) {
1099 /* We're missing D+P. */
1100 return async_raid6_datap_recov(syndrome_disks+2,
1101 STRIPE_SIZE, faila,
1102 blocks, &submit);
1103 } else {
1104 /* We're missing D+D. */
1105 return async_raid6_2data_recov(syndrome_disks+2,
1106 STRIPE_SIZE, faila, failb,
1107 blocks, &submit);
1108 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001109 }
1110}
1111
1112
Dan Williams91c00922007-01-02 13:52:30 -07001113static void ops_complete_prexor(void *stripe_head_ref)
1114{
1115 struct stripe_head *sh = stripe_head_ref;
1116
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001117 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001118 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001119}
1120
1121static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001122ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1123 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001124{
Dan Williams91c00922007-01-02 13:52:30 -07001125 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001126 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001127 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001128 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001129
1130 /* existing parity data subtracted */
1131 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1132
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001133 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001134 (unsigned long long)sh->sector);
1135
1136 for (i = disks; i--; ) {
1137 struct r5dev *dev = &sh->dev[i];
1138 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001139 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001140 xor_srcs[count++] = dev->page;
1141 }
1142
Dan Williams0403e382009-09-08 17:42:50 -07001143 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001144 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001145 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001146
1147 return tx;
1148}
1149
1150static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001151ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001152{
1153 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001154 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001155
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001156 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001157 (unsigned long long)sh->sector);
1158
1159 for (i = disks; i--; ) {
1160 struct r5dev *dev = &sh->dev[i];
1161 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001162
Dan Williamsd8ee0722008-06-28 08:32:06 +10001163 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001164 struct bio *wbi;
1165
Shaohua Lib17459c2012-07-19 16:01:31 +10001166 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001167 chosen = dev->towrite;
1168 dev->towrite = NULL;
1169 BUG_ON(dev->written);
1170 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001171 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001172
1173 while (wbi && wbi->bi_sector <
1174 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001175 if (wbi->bi_rw & REQ_FUA)
1176 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001177 if (wbi->bi_rw & REQ_SYNC)
1178 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001179 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001180 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001181 else
Shaohua Li620125f2012-10-11 13:49:05 +11001182 tx = async_copy_data(1, wbi, dev->page,
1183 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001184 wbi = r5_next_bio(wbi, dev->sector);
1185 }
1186 }
1187 }
1188
1189 return tx;
1190}
1191
Dan Williamsac6b53b2009-07-14 13:40:19 -07001192static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001193{
1194 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001195 int disks = sh->disks;
1196 int pd_idx = sh->pd_idx;
1197 int qd_idx = sh->qd_idx;
1198 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001199 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001200
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001201 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001202 (unsigned long long)sh->sector);
1203
Shaohua Libc0934f2012-05-22 13:55:05 +10001204 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001205 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001206 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001207 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001208 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001209
Dan Williams91c00922007-01-02 13:52:30 -07001210 for (i = disks; i--; ) {
1211 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001212
Tejun Heoe9c74692010-09-03 11:56:18 +02001213 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001214 if (!discard)
1215 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001216 if (fua)
1217 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001218 if (sync)
1219 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001220 }
Dan Williams91c00922007-01-02 13:52:30 -07001221 }
1222
Dan Williamsd8ee0722008-06-28 08:32:06 +10001223 if (sh->reconstruct_state == reconstruct_state_drain_run)
1224 sh->reconstruct_state = reconstruct_state_drain_result;
1225 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1226 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1227 else {
1228 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1229 sh->reconstruct_state = reconstruct_state_result;
1230 }
Dan Williams91c00922007-01-02 13:52:30 -07001231
1232 set_bit(STRIPE_HANDLE, &sh->state);
1233 release_stripe(sh);
1234}
1235
1236static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001237ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1238 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001239{
Dan Williams91c00922007-01-02 13:52:30 -07001240 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001241 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001242 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001243 int count = 0, pd_idx = sh->pd_idx, i;
1244 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001245 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001246 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001247
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001248 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001249 (unsigned long long)sh->sector);
1250
Shaohua Li620125f2012-10-11 13:49:05 +11001251 for (i = 0; i < sh->disks; i++) {
1252 if (pd_idx == i)
1253 continue;
1254 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1255 break;
1256 }
1257 if (i >= sh->disks) {
1258 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001259 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1260 ops_complete_reconstruct(sh);
1261 return;
1262 }
Dan Williams91c00922007-01-02 13:52:30 -07001263 /* check if prexor is active which means only process blocks
1264 * that are part of a read-modify-write (written)
1265 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001266 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1267 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001268 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1269 for (i = disks; i--; ) {
1270 struct r5dev *dev = &sh->dev[i];
1271 if (dev->written)
1272 xor_srcs[count++] = dev->page;
1273 }
1274 } else {
1275 xor_dest = sh->dev[pd_idx].page;
1276 for (i = disks; i--; ) {
1277 struct r5dev *dev = &sh->dev[i];
1278 if (i != pd_idx)
1279 xor_srcs[count++] = dev->page;
1280 }
1281 }
1282
Dan Williams91c00922007-01-02 13:52:30 -07001283 /* 1/ if we prexor'd then the dest is reused as a source
1284 * 2/ if we did not prexor then we are redoing the parity
1285 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1286 * for the synchronous xor case
1287 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001288 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001289 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1290
1291 atomic_inc(&sh->count);
1292
Dan Williamsac6b53b2009-07-14 13:40:19 -07001293 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001294 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001295 if (unlikely(count == 1))
1296 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1297 else
1298 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001299}
1300
Dan Williamsac6b53b2009-07-14 13:40:19 -07001301static void
1302ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1303 struct dma_async_tx_descriptor *tx)
1304{
1305 struct async_submit_ctl submit;
1306 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001307 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001308
1309 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1310
Shaohua Li620125f2012-10-11 13:49:05 +11001311 for (i = 0; i < sh->disks; i++) {
1312 if (sh->pd_idx == i || sh->qd_idx == i)
1313 continue;
1314 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1315 break;
1316 }
1317 if (i >= sh->disks) {
1318 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001319 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1320 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1321 ops_complete_reconstruct(sh);
1322 return;
1323 }
1324
Dan Williamsac6b53b2009-07-14 13:40:19 -07001325 count = set_syndrome_sources(blocks, sh);
1326
1327 atomic_inc(&sh->count);
1328
1329 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1330 sh, to_addr_conv(sh, percpu));
1331 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001332}
1333
1334static void ops_complete_check(void *stripe_head_ref)
1335{
1336 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001337
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001338 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001339 (unsigned long long)sh->sector);
1340
Dan Williamsecc65c92008-06-28 08:31:57 +10001341 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001342 set_bit(STRIPE_HANDLE, &sh->state);
1343 release_stripe(sh);
1344}
1345
Dan Williamsac6b53b2009-07-14 13:40:19 -07001346static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001347{
Dan Williams91c00922007-01-02 13:52:30 -07001348 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001349 int pd_idx = sh->pd_idx;
1350 int qd_idx = sh->qd_idx;
1351 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001352 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001353 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001354 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001355 int count;
1356 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001357
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001358 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001359 (unsigned long long)sh->sector);
1360
Dan Williamsac6b53b2009-07-14 13:40:19 -07001361 count = 0;
1362 xor_dest = sh->dev[pd_idx].page;
1363 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001364 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001365 if (i == pd_idx || i == qd_idx)
1366 continue;
1367 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001368 }
1369
Dan Williamsd6f38f32009-07-14 11:50:52 -07001370 init_async_submit(&submit, 0, NULL, NULL, NULL,
1371 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001372 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001373 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001374
Dan Williams91c00922007-01-02 13:52:30 -07001375 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001376 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1377 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001378}
1379
Dan Williamsac6b53b2009-07-14 13:40:19 -07001380static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1381{
1382 struct page **srcs = percpu->scribble;
1383 struct async_submit_ctl submit;
1384 int count;
1385
1386 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1387 (unsigned long long)sh->sector, checkp);
1388
1389 count = set_syndrome_sources(srcs, sh);
1390 if (!checkp)
1391 srcs[count] = NULL;
1392
1393 atomic_inc(&sh->count);
1394 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1395 sh, to_addr_conv(sh, percpu));
1396 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1397 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1398}
1399
Dan Williams417b8d42009-10-16 16:25:22 +11001400static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001401{
1402 int overlap_clear = 0, i, disks = sh->disks;
1403 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001404 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001405 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001406 struct raid5_percpu *percpu;
1407 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001408
Dan Williamsd6f38f32009-07-14 11:50:52 -07001409 cpu = get_cpu();
1410 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001411 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001412 ops_run_biofill(sh);
1413 overlap_clear++;
1414 }
1415
Dan Williams7b3a8712008-06-28 08:32:09 +10001416 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001417 if (level < 6)
1418 tx = ops_run_compute5(sh, percpu);
1419 else {
1420 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1421 tx = ops_run_compute6_1(sh, percpu);
1422 else
1423 tx = ops_run_compute6_2(sh, percpu);
1424 }
1425 /* terminate the chain if reconstruct is not set to be run */
1426 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001427 async_tx_ack(tx);
1428 }
Dan Williams91c00922007-01-02 13:52:30 -07001429
Dan Williams600aa102008-06-28 08:32:05 +10001430 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001431 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001432
Dan Williams600aa102008-06-28 08:32:05 +10001433 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001434 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001435 overlap_clear++;
1436 }
1437
Dan Williamsac6b53b2009-07-14 13:40:19 -07001438 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1439 if (level < 6)
1440 ops_run_reconstruct5(sh, percpu, tx);
1441 else
1442 ops_run_reconstruct6(sh, percpu, tx);
1443 }
Dan Williams91c00922007-01-02 13:52:30 -07001444
Dan Williamsac6b53b2009-07-14 13:40:19 -07001445 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1446 if (sh->check_state == check_state_run)
1447 ops_run_check_p(sh, percpu);
1448 else if (sh->check_state == check_state_run_q)
1449 ops_run_check_pq(sh, percpu, 0);
1450 else if (sh->check_state == check_state_run_pq)
1451 ops_run_check_pq(sh, percpu, 1);
1452 else
1453 BUG();
1454 }
Dan Williams91c00922007-01-02 13:52:30 -07001455
Dan Williams91c00922007-01-02 13:52:30 -07001456 if (overlap_clear)
1457 for (i = disks; i--; ) {
1458 struct r5dev *dev = &sh->dev[i];
1459 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1460 wake_up(&sh->raid_conf->wait_for_overlap);
1461 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001462 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001463}
1464
Dan Williams417b8d42009-10-16 16:25:22 +11001465#ifdef CONFIG_MULTICORE_RAID456
1466static void async_run_ops(void *param, async_cookie_t cookie)
1467{
1468 struct stripe_head *sh = param;
1469 unsigned long ops_request = sh->ops.request;
1470
1471 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1472 wake_up(&sh->ops.wait_for_ops);
1473
1474 __raid_run_ops(sh, ops_request);
1475 release_stripe(sh);
1476}
1477
1478static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1479{
1480 /* since handle_stripe can be called outside of raid5d context
1481 * we need to ensure sh->ops.request is de-staged before another
1482 * request arrives
1483 */
1484 wait_event(sh->ops.wait_for_ops,
1485 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1486 sh->ops.request = ops_request;
1487
1488 atomic_inc(&sh->count);
1489 async_schedule(async_run_ops, sh);
1490}
1491#else
1492#define raid_run_ops __raid_run_ops
1493#endif
1494
NeilBrownd1688a62011-10-11 16:49:52 +11001495static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001498 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001499 if (!sh)
1500 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001501
NeilBrown3f294f42005-11-08 21:39:25 -08001502 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001503 #ifdef CONFIG_MULTICORE_RAID456
1504 init_waitqueue_head(&sh->ops.wait_for_ops);
1505 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001506
Shaohua Lib17459c2012-07-19 16:01:31 +10001507 spin_lock_init(&sh->stripe_lock);
1508
NeilBrowne4e11e32010-06-16 16:45:16 +10001509 if (grow_buffers(sh)) {
1510 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001511 kmem_cache_free(conf->slab_cache, sh);
1512 return 0;
1513 }
1514 /* we just created an active stripe so... */
1515 atomic_set(&sh->count, 1);
1516 atomic_inc(&conf->active_stripes);
1517 INIT_LIST_HEAD(&sh->lru);
1518 release_stripe(sh);
1519 return 1;
1520}
1521
NeilBrownd1688a62011-10-11 16:49:52 +11001522static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001523{
Christoph Lametere18b8902006-12-06 20:33:20 -08001524 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001525 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
NeilBrownf4be6b42010-06-01 19:37:25 +10001527 if (conf->mddev->gendisk)
1528 sprintf(conf->cache_name[0],
1529 "raid%d-%s", conf->level, mdname(conf->mddev));
1530 else
1531 sprintf(conf->cache_name[0],
1532 "raid%d-%p", conf->level, conf->mddev);
1533 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1534
NeilBrownad01c9e2006-03-27 01:18:07 -08001535 conf->active_name = 0;
1536 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001538 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (!sc)
1540 return 1;
1541 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001542 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001543 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001544 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return 0;
1547}
NeilBrown29269552006-03-27 01:18:10 -08001548
Dan Williamsd6f38f32009-07-14 11:50:52 -07001549/**
1550 * scribble_len - return the required size of the scribble region
1551 * @num - total number of disks in the array
1552 *
1553 * The size must be enough to contain:
1554 * 1/ a struct page pointer for each device in the array +2
1555 * 2/ room to convert each entry in (1) to its corresponding dma
1556 * (dma_map_page()) or page (page_address()) address.
1557 *
1558 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1559 * calculate over all devices (not just the data blocks), using zeros in place
1560 * of the P and Q blocks.
1561 */
1562static size_t scribble_len(int num)
1563{
1564 size_t len;
1565
1566 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1567
1568 return len;
1569}
1570
NeilBrownd1688a62011-10-11 16:49:52 +11001571static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001572{
1573 /* Make all the stripes able to hold 'newsize' devices.
1574 * New slots in each stripe get 'page' set to a new page.
1575 *
1576 * This happens in stages:
1577 * 1/ create a new kmem_cache and allocate the required number of
1578 * stripe_heads.
1579 * 2/ gather all the old stripe_heads and tranfer the pages across
1580 * to the new stripe_heads. This will have the side effect of
1581 * freezing the array as once all stripe_heads have been collected,
1582 * no IO will be possible. Old stripe heads are freed once their
1583 * pages have been transferred over, and the old kmem_cache is
1584 * freed when all stripes are done.
1585 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1586 * we simple return a failre status - no need to clean anything up.
1587 * 4/ allocate new pages for the new slots in the new stripe_heads.
1588 * If this fails, we don't bother trying the shrink the
1589 * stripe_heads down again, we just leave them as they are.
1590 * As each stripe_head is processed the new one is released into
1591 * active service.
1592 *
1593 * Once step2 is started, we cannot afford to wait for a write,
1594 * so we use GFP_NOIO allocations.
1595 */
1596 struct stripe_head *osh, *nsh;
1597 LIST_HEAD(newstripes);
1598 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001599 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001600 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001601 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001602 int i;
1603
1604 if (newsize <= conf->pool_size)
1605 return 0; /* never bother to shrink */
1606
Dan Williamsb5470dc2008-06-27 21:44:04 -07001607 err = md_allow_write(conf->mddev);
1608 if (err)
1609 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001610
NeilBrownad01c9e2006-03-27 01:18:07 -08001611 /* Step 1 */
1612 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1613 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001614 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001615 if (!sc)
1616 return -ENOMEM;
1617
1618 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001619 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001620 if (!nsh)
1621 break;
1622
NeilBrownad01c9e2006-03-27 01:18:07 -08001623 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001624 #ifdef CONFIG_MULTICORE_RAID456
1625 init_waitqueue_head(&nsh->ops.wait_for_ops);
1626 #endif
NeilBrowncb13ff62012-09-24 16:27:20 +10001627 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001628
1629 list_add(&nsh->lru, &newstripes);
1630 }
1631 if (i) {
1632 /* didn't get enough, give up */
1633 while (!list_empty(&newstripes)) {
1634 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1635 list_del(&nsh->lru);
1636 kmem_cache_free(sc, nsh);
1637 }
1638 kmem_cache_destroy(sc);
1639 return -ENOMEM;
1640 }
1641 /* Step 2 - Must use GFP_NOIO now.
1642 * OK, we have enough stripes, start collecting inactive
1643 * stripes and copying them over
1644 */
1645 list_for_each_entry(nsh, &newstripes, lru) {
1646 spin_lock_irq(&conf->device_lock);
1647 wait_event_lock_irq(conf->wait_for_stripe,
1648 !list_empty(&conf->inactive_list),
1649 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001650 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001651 osh = get_free_stripe(conf);
1652 spin_unlock_irq(&conf->device_lock);
1653 atomic_set(&nsh->count, 1);
1654 for(i=0; i<conf->pool_size; i++)
1655 nsh->dev[i].page = osh->dev[i].page;
1656 for( ; i<newsize; i++)
1657 nsh->dev[i].page = NULL;
1658 kmem_cache_free(conf->slab_cache, osh);
1659 }
1660 kmem_cache_destroy(conf->slab_cache);
1661
1662 /* Step 3.
1663 * At this point, we are holding all the stripes so the array
1664 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001665 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001666 */
1667 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1668 if (ndisks) {
1669 for (i=0; i<conf->raid_disks; i++)
1670 ndisks[i] = conf->disks[i];
1671 kfree(conf->disks);
1672 conf->disks = ndisks;
1673 } else
1674 err = -ENOMEM;
1675
Dan Williamsd6f38f32009-07-14 11:50:52 -07001676 get_online_cpus();
1677 conf->scribble_len = scribble_len(newsize);
1678 for_each_present_cpu(cpu) {
1679 struct raid5_percpu *percpu;
1680 void *scribble;
1681
1682 percpu = per_cpu_ptr(conf->percpu, cpu);
1683 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1684
1685 if (scribble) {
1686 kfree(percpu->scribble);
1687 percpu->scribble = scribble;
1688 } else {
1689 err = -ENOMEM;
1690 break;
1691 }
1692 }
1693 put_online_cpus();
1694
NeilBrownad01c9e2006-03-27 01:18:07 -08001695 /* Step 4, return new stripes to service */
1696 while(!list_empty(&newstripes)) {
1697 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1698 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001699
NeilBrownad01c9e2006-03-27 01:18:07 -08001700 for (i=conf->raid_disks; i < newsize; i++)
1701 if (nsh->dev[i].page == NULL) {
1702 struct page *p = alloc_page(GFP_NOIO);
1703 nsh->dev[i].page = p;
1704 if (!p)
1705 err = -ENOMEM;
1706 }
1707 release_stripe(nsh);
1708 }
1709 /* critical section pass, GFP_NOIO no longer needed */
1710
1711 conf->slab_cache = sc;
1712 conf->active_name = 1-conf->active_name;
1713 conf->pool_size = newsize;
1714 return err;
1715}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
NeilBrownd1688a62011-10-11 16:49:52 +11001717static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
1719 struct stripe_head *sh;
1720
NeilBrown3f294f42005-11-08 21:39:25 -08001721 spin_lock_irq(&conf->device_lock);
1722 sh = get_free_stripe(conf);
1723 spin_unlock_irq(&conf->device_lock);
1724 if (!sh)
1725 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001726 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001727 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001728 kmem_cache_free(conf->slab_cache, sh);
1729 atomic_dec(&conf->active_stripes);
1730 return 1;
1731}
1732
NeilBrownd1688a62011-10-11 16:49:52 +11001733static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001734{
1735 while (drop_one_stripe(conf))
1736 ;
1737
NeilBrown29fc7e32006-02-03 03:03:41 -08001738 if (conf->slab_cache)
1739 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 conf->slab_cache = NULL;
1741}
1742
NeilBrown6712ecf2007-09-27 12:47:43 +02001743static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744{
NeilBrown99c0fb52009-03-31 14:39:38 +11001745 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001746 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001747 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001749 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001750 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001751 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 for (i=0 ; i<disks; i++)
1754 if (bi == &sh->dev[i].req)
1755 break;
1756
Dan Williams45b42332007-07-09 11:56:43 -07001757 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1758 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 uptodate);
1760 if (i == disks) {
1761 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001762 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 }
NeilBrown14a75d32011-12-23 10:17:52 +11001764 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001765 /* If replacement finished while this request was outstanding,
1766 * 'replacement' might be NULL already.
1767 * In that case it moved down to 'rdev'.
1768 * rdev is not removed until all requests are finished.
1769 */
NeilBrown14a75d32011-12-23 10:17:52 +11001770 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001771 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001772 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
NeilBrown05616be2012-05-21 09:27:00 +10001774 if (use_new_offset(conf, sh))
1775 s = sh->sector + rdev->new_data_offset;
1776 else
1777 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001780 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001781 /* Note that this cannot happen on a
1782 * replacement device. We just fail those on
1783 * any error
1784 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001785 printk_ratelimited(
1786 KERN_INFO
1787 "md/raid:%s: read error corrected"
1788 " (%lu sectors at %llu on %s)\n",
1789 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001790 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001791 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001792 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001793 clear_bit(R5_ReadError, &sh->dev[i].flags);
1794 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001795 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1796 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1797
NeilBrown14a75d32011-12-23 10:17:52 +11001798 if (atomic_read(&rdev->read_errors))
1799 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001801 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001802 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001803 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001806 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001807 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1808 printk_ratelimited(
1809 KERN_WARNING
1810 "md/raid:%s: read error on replacement device "
1811 "(sector %llu on %s).\n",
1812 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001813 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001814 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001815 else if (conf->mddev->degraded >= conf->max_degraded) {
1816 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001817 printk_ratelimited(
1818 KERN_WARNING
1819 "md/raid:%s: read error not correctable "
1820 "(sector %llu on %s).\n",
1821 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001822 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001823 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001824 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001825 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001826 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001827 printk_ratelimited(
1828 KERN_WARNING
1829 "md/raid:%s: read error NOT corrected!! "
1830 "(sector %llu on %s).\n",
1831 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001832 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001833 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001834 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001835 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001836 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001837 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001838 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001839 else
1840 retry = 1;
1841 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001842 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1843 set_bit(R5_ReadError, &sh->dev[i].flags);
1844 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1845 } else
1846 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001847 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001848 clear_bit(R5_ReadError, &sh->dev[i].flags);
1849 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001850 if (!(set_bad
1851 && test_bit(In_sync, &rdev->flags)
1852 && rdev_set_badblocks(
1853 rdev, sh->sector, STRIPE_SECTORS, 0)))
1854 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 }
NeilBrown14a75d32011-12-23 10:17:52 +11001857 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1859 set_bit(STRIPE_HANDLE, &sh->state);
1860 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
1862
NeilBrownd710e132008-10-13 11:55:12 +11001863static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
NeilBrown99c0fb52009-03-31 14:39:38 +11001865 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001866 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001867 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001868 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001870 sector_t first_bad;
1871 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001872 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
NeilBrown977df362011-12-23 10:17:53 +11001874 for (i = 0 ; i < disks; i++) {
1875 if (bi == &sh->dev[i].req) {
1876 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 break;
NeilBrown977df362011-12-23 10:17:53 +11001878 }
1879 if (bi == &sh->dev[i].rreq) {
1880 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001881 if (rdev)
1882 replacement = 1;
1883 else
1884 /* rdev was removed and 'replacement'
1885 * replaced it. rdev is not removed
1886 * until all requests are finished.
1887 */
1888 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001889 break;
1890 }
1891 }
Dan Williams45b42332007-07-09 11:56:43 -07001892 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1894 uptodate);
1895 if (i == disks) {
1896 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001897 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
1899
NeilBrown977df362011-12-23 10:17:53 +11001900 if (replacement) {
1901 if (!uptodate)
1902 md_error(conf->mddev, rdev);
1903 else if (is_badblock(rdev, sh->sector,
1904 STRIPE_SECTORS,
1905 &first_bad, &bad_sectors))
1906 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1907 } else {
1908 if (!uptodate) {
1909 set_bit(WriteErrorSeen, &rdev->flags);
1910 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001911 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1912 set_bit(MD_RECOVERY_NEEDED,
1913 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001914 } else if (is_badblock(rdev, sh->sector,
1915 STRIPE_SECTORS,
1916 &first_bad, &bad_sectors))
1917 set_bit(R5_MadeGood, &sh->dev[i].flags);
1918 }
1919 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
NeilBrown977df362011-12-23 10:17:53 +11001921 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1922 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001924 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
NeilBrown784052e2009-03-31 15:19:07 +11001927static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
NeilBrown784052e2009-03-31 15:19:07 +11001929static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
1931 struct r5dev *dev = &sh->dev[i];
1932
1933 bio_init(&dev->req);
1934 dev->req.bi_io_vec = &dev->vec;
1935 dev->req.bi_vcnt++;
1936 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001938 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
NeilBrown977df362011-12-23 10:17:53 +11001940 bio_init(&dev->rreq);
1941 dev->rreq.bi_io_vec = &dev->rvec;
1942 dev->rreq.bi_vcnt++;
1943 dev->rreq.bi_max_vecs++;
1944 dev->rreq.bi_private = sh;
1945 dev->rvec.bv_page = dev->page;
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001948 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
NeilBrownfd01b882011-10-11 16:47:53 +11001951static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
1953 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001954 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001955 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001956 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
NeilBrown908f4fb2011-12-23 10:17:50 +11001958 spin_lock_irqsave(&conf->device_lock, flags);
1959 clear_bit(In_sync, &rdev->flags);
1960 mddev->degraded = calc_degraded(conf);
1961 spin_unlock_irqrestore(&conf->device_lock, flags);
1962 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1963
NeilBrownde393cd2011-07-28 11:31:48 +10001964 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001965 set_bit(Faulty, &rdev->flags);
1966 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1967 printk(KERN_ALERT
1968 "md/raid:%s: Disk failure on %s, disabling device.\n"
1969 "md/raid:%s: Operation continuing on %d devices.\n",
1970 mdname(mddev),
1971 bdevname(rdev->bdev, b),
1972 mdname(mddev),
1973 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001974}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976/*
1977 * Input: a 'big' sector number,
1978 * Output: index of the data and parity disk, and the sector # in them.
1979 */
NeilBrownd1688a62011-10-11 16:49:52 +11001980static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001981 int previous, int *dd_idx,
1982 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001984 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001985 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001987 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001988 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001990 int algorithm = previous ? conf->prev_algo
1991 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001992 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1993 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001994 int raid_disks = previous ? conf->previous_raid_disks
1995 : conf->raid_disks;
1996 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 /* First compute the information on this sector */
1999
2000 /*
2001 * Compute the chunk number and the sector offset inside the chunk
2002 */
2003 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2004 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 /*
2007 * Compute the stripe number
2008 */
NeilBrown35f2a592010-04-20 14:13:34 +10002009 stripe = chunk_number;
2010 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002011 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 /*
2013 * Select the parity disk based on the user selected algorithm.
2014 */
NeilBrown84789552011-07-27 11:00:36 +10002015 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002016 switch(conf->level) {
2017 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002018 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002019 break;
2020 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002021 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002023 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002024 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 (*dd_idx)++;
2026 break;
2027 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002028 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002029 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 (*dd_idx)++;
2031 break;
2032 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002033 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002034 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 break;
2036 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002037 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002038 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002040 case ALGORITHM_PARITY_0:
2041 pd_idx = 0;
2042 (*dd_idx)++;
2043 break;
2044 case ALGORITHM_PARITY_N:
2045 pd_idx = data_disks;
2046 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002048 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002049 }
2050 break;
2051 case 6:
2052
NeilBrowne183eae2009-03-31 15:20:22 +11002053 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002054 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002055 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002056 qd_idx = pd_idx + 1;
2057 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002058 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002059 qd_idx = 0;
2060 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002061 (*dd_idx) += 2; /* D D P Q D */
2062 break;
2063 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002064 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002065 qd_idx = pd_idx + 1;
2066 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002067 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002068 qd_idx = 0;
2069 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002070 (*dd_idx) += 2; /* D D P Q D */
2071 break;
2072 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002073 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002074 qd_idx = (pd_idx + 1) % raid_disks;
2075 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002076 break;
2077 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002078 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002079 qd_idx = (pd_idx + 1) % raid_disks;
2080 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002081 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002082
2083 case ALGORITHM_PARITY_0:
2084 pd_idx = 0;
2085 qd_idx = 1;
2086 (*dd_idx) += 2;
2087 break;
2088 case ALGORITHM_PARITY_N:
2089 pd_idx = data_disks;
2090 qd_idx = data_disks + 1;
2091 break;
2092
2093 case ALGORITHM_ROTATING_ZERO_RESTART:
2094 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2095 * of blocks for computing Q is different.
2096 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002097 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002098 qd_idx = pd_idx + 1;
2099 if (pd_idx == raid_disks-1) {
2100 (*dd_idx)++; /* Q D D D P */
2101 qd_idx = 0;
2102 } else if (*dd_idx >= pd_idx)
2103 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002104 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002105 break;
2106
2107 case ALGORITHM_ROTATING_N_RESTART:
2108 /* Same a left_asymmetric, by first stripe is
2109 * D D D P Q rather than
2110 * Q D D D P
2111 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002112 stripe2 += 1;
2113 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002114 qd_idx = pd_idx + 1;
2115 if (pd_idx == raid_disks-1) {
2116 (*dd_idx)++; /* Q D D D P */
2117 qd_idx = 0;
2118 } else if (*dd_idx >= pd_idx)
2119 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002120 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002121 break;
2122
2123 case ALGORITHM_ROTATING_N_CONTINUE:
2124 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002125 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002126 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2127 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002128 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002129 break;
2130
2131 case ALGORITHM_LEFT_ASYMMETRIC_6:
2132 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002133 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002134 if (*dd_idx >= pd_idx)
2135 (*dd_idx)++;
2136 qd_idx = raid_disks - 1;
2137 break;
2138
2139 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002140 pd_idx = 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_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002147 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002148 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2149 qd_idx = raid_disks - 1;
2150 break;
2151
2152 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002153 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002154 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2155 qd_idx = raid_disks - 1;
2156 break;
2157
2158 case ALGORITHM_PARITY_0_6:
2159 pd_idx = 0;
2160 (*dd_idx)++;
2161 qd_idx = raid_disks - 1;
2162 break;
2163
NeilBrown16a53ec2006-06-26 00:27:38 -07002164 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002165 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002166 }
2167 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
2169
NeilBrown911d4ee2009-03-31 14:39:38 +11002170 if (sh) {
2171 sh->pd_idx = pd_idx;
2172 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002173 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 /*
2176 * Finally, compute the new sector number
2177 */
2178 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2179 return new_sector;
2180}
2181
2182
NeilBrown784052e2009-03-31 15:19:07 +11002183static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
NeilBrownd1688a62011-10-11 16:49:52 +11002185 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002186 int raid_disks = sh->disks;
2187 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002189 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2190 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002191 int algorithm = previous ? conf->prev_algo
2192 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 sector_t stripe;
2194 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002195 sector_t chunk_number;
2196 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002198 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
NeilBrown16a53ec2006-06-26 00:27:38 -07002200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2202 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
NeilBrown16a53ec2006-06-26 00:27:38 -07002204 if (i == sh->pd_idx)
2205 return 0;
2206 switch(conf->level) {
2207 case 4: break;
2208 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002209 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 case ALGORITHM_LEFT_ASYMMETRIC:
2211 case ALGORITHM_RIGHT_ASYMMETRIC:
2212 if (i > sh->pd_idx)
2213 i--;
2214 break;
2215 case ALGORITHM_LEFT_SYMMETRIC:
2216 case ALGORITHM_RIGHT_SYMMETRIC:
2217 if (i < sh->pd_idx)
2218 i += raid_disks;
2219 i -= (sh->pd_idx + 1);
2220 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002221 case ALGORITHM_PARITY_0:
2222 i -= 1;
2223 break;
2224 case ALGORITHM_PARITY_N:
2225 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002227 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002228 }
2229 break;
2230 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002231 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002232 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002233 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002234 case ALGORITHM_LEFT_ASYMMETRIC:
2235 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002236 case ALGORITHM_ROTATING_ZERO_RESTART:
2237 case ALGORITHM_ROTATING_N_RESTART:
2238 if (sh->pd_idx == raid_disks-1)
2239 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002240 else if (i > sh->pd_idx)
2241 i -= 2; /* D D P Q D */
2242 break;
2243 case ALGORITHM_LEFT_SYMMETRIC:
2244 case ALGORITHM_RIGHT_SYMMETRIC:
2245 if (sh->pd_idx == raid_disks-1)
2246 i--; /* Q D D D P */
2247 else {
2248 /* D D P Q D */
2249 if (i < sh->pd_idx)
2250 i += raid_disks;
2251 i -= (sh->pd_idx + 2);
2252 }
2253 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002254 case ALGORITHM_PARITY_0:
2255 i -= 2;
2256 break;
2257 case ALGORITHM_PARITY_N:
2258 break;
2259 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002260 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002261 if (sh->pd_idx == 0)
2262 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002263 else {
2264 /* D D Q P D */
2265 if (i < sh->pd_idx)
2266 i += raid_disks;
2267 i -= (sh->pd_idx + 1);
2268 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002269 break;
2270 case ALGORITHM_LEFT_ASYMMETRIC_6:
2271 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2272 if (i > sh->pd_idx)
2273 i--;
2274 break;
2275 case ALGORITHM_LEFT_SYMMETRIC_6:
2276 case ALGORITHM_RIGHT_SYMMETRIC_6:
2277 if (i < sh->pd_idx)
2278 i += data_disks + 1;
2279 i -= (sh->pd_idx + 1);
2280 break;
2281 case ALGORITHM_PARITY_0_6:
2282 i -= 1;
2283 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002284 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002285 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002286 }
2287 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 }
2289
2290 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002291 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
NeilBrown112bf892009-03-31 14:39:38 +11002293 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002294 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002295 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2296 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002297 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2298 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 return 0;
2300 }
2301 return r_sector;
2302}
2303
2304
Dan Williams600aa102008-06-28 08:32:05 +10002305static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002306schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002307 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002308{
2309 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002310 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002311 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002312
Dan Williamse33129d2007-01-02 13:52:30 -07002313 if (rcw) {
2314 /* if we are not expanding this is a proper write request, and
2315 * there will be bios with new data to be drained into the
2316 * stripe cache
2317 */
2318 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002319 sh->reconstruct_state = reconstruct_state_drain_run;
2320 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2321 } else
2322 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002323
Dan Williamsac6b53b2009-07-14 13:40:19 -07002324 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002325
2326 for (i = disks; i--; ) {
2327 struct r5dev *dev = &sh->dev[i];
2328
2329 if (dev->towrite) {
2330 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002331 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002332 if (!expand)
2333 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002334 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002335 }
2336 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002337 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002338 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002339 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002340 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002341 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002342 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2343 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2344
Dan Williamsd8ee0722008-06-28 08:32:06 +10002345 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002346 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2347 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002348 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002349
2350 for (i = disks; i--; ) {
2351 struct r5dev *dev = &sh->dev[i];
2352 if (i == pd_idx)
2353 continue;
2354
Dan Williamse33129d2007-01-02 13:52:30 -07002355 if (dev->towrite &&
2356 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002357 test_bit(R5_Wantcompute, &dev->flags))) {
2358 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002359 set_bit(R5_LOCKED, &dev->flags);
2360 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002361 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002362 }
2363 }
2364 }
2365
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002366 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002367 * are in flight
2368 */
2369 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2370 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002371 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002372
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002373 if (level == 6) {
2374 int qd_idx = sh->qd_idx;
2375 struct r5dev *dev = &sh->dev[qd_idx];
2376
2377 set_bit(R5_LOCKED, &dev->flags);
2378 clear_bit(R5_UPTODATE, &dev->flags);
2379 s->locked++;
2380 }
2381
Dan Williams600aa102008-06-28 08:32:05 +10002382 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002383 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002384 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002385}
NeilBrown16a53ec2006-06-26 00:27:38 -07002386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387/*
2388 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002389 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 * The bi_next chain must be in order.
2391 */
2392static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2393{
2394 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002395 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002396 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
NeilBrowncbe47ec2011-07-26 11:20:35 +10002398 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 (unsigned long long)bi->bi_sector,
2400 (unsigned long long)sh->sector);
2401
Shaohua Lib17459c2012-07-19 16:01:31 +10002402 /*
2403 * If several bio share a stripe. The bio bi_phys_segments acts as a
2404 * reference count to avoid race. The reference count should already be
2405 * increased before this function is called (for example, in
2406 * make_request()), so other bio sharing this stripe will not free the
2407 * stripe. If a stripe is owned by one stripe, the stripe lock will
2408 * protect it.
2409 */
2410 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002411 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002413 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002414 firstwrite = 1;
2415 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 bip = &sh->dev[dd_idx].toread;
2417 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2418 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2419 goto overlap;
2420 bip = & (*bip)->bi_next;
2421 }
2422 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2423 goto overlap;
2424
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002425 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (*bip)
2427 bi->bi_next = *bip;
2428 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002429 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (forwrite) {
2432 /* check if page is covered */
2433 sector_t sector = sh->dev[dd_idx].sector;
2434 for (bi=sh->dev[dd_idx].towrite;
2435 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2436 bi && bi->bi_sector <= sector;
2437 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2438 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2439 sector = bi->bi_sector + (bi->bi_size>>9);
2440 }
2441 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2442 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2443 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002444
2445 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2446 (unsigned long long)(*bip)->bi_sector,
2447 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002448 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002449
2450 if (conf->mddev->bitmap && firstwrite) {
2451 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2452 STRIPE_SECTORS, 0);
2453 sh->bm_seq = conf->seq_flush+1;
2454 set_bit(STRIPE_BIT_DELAY, &sh->state);
2455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return 1;
2457
2458 overlap:
2459 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002460 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 return 0;
2462}
2463
NeilBrownd1688a62011-10-11 16:49:52 +11002464static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002465
NeilBrownd1688a62011-10-11 16:49:52 +11002466static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002467 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002468{
NeilBrown784052e2009-03-31 15:19:07 +11002469 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002470 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002471 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002472 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002473 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002474
NeilBrown112bf892009-03-31 14:39:38 +11002475 raid5_compute_sector(conf,
2476 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002477 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002478 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002479 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002480}
2481
Dan Williamsa4456852007-07-09 11:56:43 -07002482static void
NeilBrownd1688a62011-10-11 16:49:52 +11002483handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002484 struct stripe_head_state *s, int disks,
2485 struct bio **return_bi)
2486{
2487 int i;
2488 for (i = disks; i--; ) {
2489 struct bio *bi;
2490 int bitmap_end = 0;
2491
2492 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002493 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002494 rcu_read_lock();
2495 rdev = rcu_dereference(conf->disks[i].rdev);
2496 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002497 atomic_inc(&rdev->nr_pending);
2498 else
2499 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002500 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002501 if (rdev) {
2502 if (!rdev_set_badblocks(
2503 rdev,
2504 sh->sector,
2505 STRIPE_SECTORS, 0))
2506 md_error(conf->mddev, rdev);
2507 rdev_dec_pending(rdev, conf->mddev);
2508 }
Dan Williamsa4456852007-07-09 11:56:43 -07002509 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002510 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002511 /* fail all writes first */
2512 bi = sh->dev[i].towrite;
2513 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002514 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002515 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002516 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002517
2518 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2519 wake_up(&conf->wait_for_overlap);
2520
2521 while (bi && bi->bi_sector <
2522 sh->dev[i].sector + STRIPE_SECTORS) {
2523 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2524 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002525 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002526 md_write_end(conf->mddev);
2527 bi->bi_next = *return_bi;
2528 *return_bi = bi;
2529 }
2530 bi = nextbi;
2531 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002532 if (bitmap_end)
2533 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2534 STRIPE_SECTORS, 0, 0);
2535 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002536 /* and fail all 'written' */
2537 bi = sh->dev[i].written;
2538 sh->dev[i].written = NULL;
2539 if (bi) bitmap_end = 1;
2540 while (bi && bi->bi_sector <
2541 sh->dev[i].sector + STRIPE_SECTORS) {
2542 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2543 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002544 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002545 md_write_end(conf->mddev);
2546 bi->bi_next = *return_bi;
2547 *return_bi = bi;
2548 }
2549 bi = bi2;
2550 }
2551
Dan Williamsb5e98d62007-01-02 13:52:31 -07002552 /* fail any reads if this device is non-operational and
2553 * the data has not reached the cache yet.
2554 */
2555 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2556 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2557 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002558 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002559 bi = sh->dev[i].toread;
2560 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002561 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002562 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2563 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002564 while (bi && bi->bi_sector <
2565 sh->dev[i].sector + STRIPE_SECTORS) {
2566 struct bio *nextbi =
2567 r5_next_bio(bi, sh->dev[i].sector);
2568 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002569 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002570 bi->bi_next = *return_bi;
2571 *return_bi = bi;
2572 }
2573 bi = nextbi;
2574 }
2575 }
Dan Williamsa4456852007-07-09 11:56:43 -07002576 if (bitmap_end)
2577 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2578 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002579 /* If we were in the middle of a write the parity block might
2580 * still be locked - so just clear all R5_LOCKED flags
2581 */
2582 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002583 }
2584
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002585 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2586 if (atomic_dec_and_test(&conf->pending_full_writes))
2587 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002588}
2589
NeilBrown7f0da592011-07-28 11:39:22 +10002590static void
NeilBrownd1688a62011-10-11 16:49:52 +11002591handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002592 struct stripe_head_state *s)
2593{
2594 int abort = 0;
2595 int i;
2596
NeilBrown7f0da592011-07-28 11:39:22 +10002597 clear_bit(STRIPE_SYNCING, &sh->state);
2598 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002599 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002600 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002601 * Don't even need to abort as that is handled elsewhere
2602 * if needed, and not always wanted e.g. if there is a known
2603 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002604 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002605 * non-sync devices, or abort the recovery
2606 */
NeilBrown18b98372012-04-01 23:48:38 +10002607 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2608 /* During recovery devices cannot be removed, so
2609 * locking and refcounting of rdevs is not needed
2610 */
2611 for (i = 0; i < conf->raid_disks; i++) {
2612 struct md_rdev *rdev = conf->disks[i].rdev;
2613 if (rdev
2614 && !test_bit(Faulty, &rdev->flags)
2615 && !test_bit(In_sync, &rdev->flags)
2616 && !rdev_set_badblocks(rdev, sh->sector,
2617 STRIPE_SECTORS, 0))
2618 abort = 1;
2619 rdev = conf->disks[i].replacement;
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 }
2627 if (abort)
2628 conf->recovery_disabled =
2629 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002630 }
NeilBrown18b98372012-04-01 23:48:38 +10002631 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002632}
2633
NeilBrown9a3e1102011-12-23 10:17:53 +11002634static int want_replace(struct stripe_head *sh, int disk_idx)
2635{
2636 struct md_rdev *rdev;
2637 int rv = 0;
2638 /* Doing recovery so rcu locking not required */
2639 rdev = sh->raid_conf->disks[disk_idx].replacement;
2640 if (rdev
2641 && !test_bit(Faulty, &rdev->flags)
2642 && !test_bit(In_sync, &rdev->flags)
2643 && (rdev->recovery_offset <= sh->sector
2644 || rdev->mddev->recovery_cp <= sh->sector))
2645 rv = 1;
2646
2647 return rv;
2648}
2649
NeilBrown93b3dbc2011-07-27 11:00:36 +10002650/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002651 * to be read or computed to satisfy a request.
2652 *
2653 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002654 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002655 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002656static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2657 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002658{
2659 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002660 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2661 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002662
Dan Williamsf38e1212007-01-02 13:52:30 -07002663 /* is the data in this block needed, and can we get it? */
2664 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002665 !test_bit(R5_UPTODATE, &dev->flags) &&
2666 (dev->toread ||
2667 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2668 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002669 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002670 (s->failed >= 1 && fdev[0]->toread) ||
2671 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002672 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2673 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2674 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002675 /* we would like to get this block, possibly by computing it,
2676 * otherwise read it if the backing disk is insync
2677 */
2678 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2679 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2680 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002681 (s->failed && (disk_idx == s->failed_num[0] ||
2682 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002683 /* have disk failed, and we're requested to fetch it;
2684 * do compute it
2685 */
2686 pr_debug("Computing stripe %llu block %d\n",
2687 (unsigned long long)sh->sector, disk_idx);
2688 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2689 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2690 set_bit(R5_Wantcompute, &dev->flags);
2691 sh->ops.target = disk_idx;
2692 sh->ops.target2 = -1; /* no 2nd target */
2693 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002694 /* Careful: from this point on 'uptodate' is in the eye
2695 * of raid_run_ops which services 'compute' operations
2696 * before writes. R5_Wantcompute flags a block that will
2697 * be R5_UPTODATE by the time it is needed for a
2698 * subsequent operation.
2699 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002700 s->uptodate++;
2701 return 1;
2702 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2703 /* Computing 2-failure is *very* expensive; only
2704 * do it if failed >= 2
2705 */
2706 int other;
2707 for (other = disks; other--; ) {
2708 if (other == disk_idx)
2709 continue;
2710 if (!test_bit(R5_UPTODATE,
2711 &sh->dev[other].flags))
2712 break;
2713 }
2714 BUG_ON(other < 0);
2715 pr_debug("Computing stripe %llu blocks %d,%d\n",
2716 (unsigned long long)sh->sector,
2717 disk_idx, other);
2718 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2719 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2720 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2721 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2722 sh->ops.target = disk_idx;
2723 sh->ops.target2 = other;
2724 s->uptodate += 2;
2725 s->req_compute = 1;
2726 return 1;
2727 } else if (test_bit(R5_Insync, &dev->flags)) {
2728 set_bit(R5_LOCKED, &dev->flags);
2729 set_bit(R5_Wantread, &dev->flags);
2730 s->locked++;
2731 pr_debug("Reading block %d (sync=%d)\n",
2732 disk_idx, s->syncing);
2733 }
2734 }
2735
2736 return 0;
2737}
2738
2739/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002740 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002741 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002742static void handle_stripe_fill(struct stripe_head *sh,
2743 struct stripe_head_state *s,
2744 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002745{
2746 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002747
2748 /* look for blocks to read/compute, skip this if a compute
2749 * is already in flight, or if the stripe contents are in the
2750 * midst of changing due to a write
2751 */
2752 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2753 !sh->reconstruct_state)
2754 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002755 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002756 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002757 set_bit(STRIPE_HANDLE, &sh->state);
2758}
2759
2760
Dan Williams1fe797e2008-06-28 09:16:30 +10002761/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002762 * any written block on an uptodate or failed drive can be returned.
2763 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2764 * never LOCKED, so we don't need to test 'failed' directly.
2765 */
NeilBrownd1688a62011-10-11 16:49:52 +11002766static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002767 struct stripe_head *sh, int disks, struct bio **return_bi)
2768{
2769 int i;
2770 struct r5dev *dev;
2771
2772 for (i = disks; i--; )
2773 if (sh->dev[i].written) {
2774 dev = &sh->dev[i];
2775 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11002776 (test_bit(R5_UPTODATE, &dev->flags) ||
2777 test_and_clear_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002778 /* We can return any write requests */
2779 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002780 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002781 wbi = dev->written;
2782 dev->written = NULL;
2783 while (wbi && wbi->bi_sector <
2784 dev->sector + STRIPE_SECTORS) {
2785 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002786 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002787 md_write_end(conf->mddev);
2788 wbi->bi_next = *return_bi;
2789 *return_bi = wbi;
2790 }
2791 wbi = wbi2;
2792 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002793 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2794 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002795 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002796 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002797 }
2798 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002799
2800 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2801 if (atomic_dec_and_test(&conf->pending_full_writes))
2802 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002803}
2804
NeilBrownd1688a62011-10-11 16:49:52 +11002805static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002806 struct stripe_head *sh,
2807 struct stripe_head_state *s,
2808 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002809{
2810 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002811 sector_t recovery_cp = conf->mddev->recovery_cp;
2812
2813 /* RAID6 requires 'rcw' in current implementation.
2814 * Otherwise, check whether resync is now happening or should start.
2815 * If yes, then the array is dirty (after unclean shutdown or
2816 * initial creation), so parity in some stripes might be inconsistent.
2817 * In this case, we need to always do reconstruct-write, to ensure
2818 * that in case of drive failure or read-error correction, we
2819 * generate correct data from the parity.
2820 */
2821 if (conf->max_degraded == 2 ||
2822 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2823 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002824 * look like rcw is cheaper
2825 */
2826 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002827 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2828 conf->max_degraded, (unsigned long long)recovery_cp,
2829 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002830 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002831 /* would I have to read this buffer for read_modify_write */
2832 struct r5dev *dev = &sh->dev[i];
2833 if ((dev->towrite || i == sh->pd_idx) &&
2834 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002835 !(test_bit(R5_UPTODATE, &dev->flags) ||
2836 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002837 if (test_bit(R5_Insync, &dev->flags))
2838 rmw++;
2839 else
2840 rmw += 2*disks; /* cannot read it */
2841 }
2842 /* Would I have to read this buffer for reconstruct_write */
2843 if (!test_bit(R5_OVERWRITE, &dev->flags) && 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))) {
2847 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002848 else
2849 rcw += 2*disks;
2850 }
2851 }
Dan Williams45b42332007-07-09 11:56:43 -07002852 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002853 (unsigned long long)sh->sector, rmw, rcw);
2854 set_bit(STRIPE_HANDLE, &sh->state);
2855 if (rmw < rcw && rmw > 0)
2856 /* prefer read-modify-write, but need to get some data */
2857 for (i = disks; i--; ) {
2858 struct r5dev *dev = &sh->dev[i];
2859 if ((dev->towrite || i == sh->pd_idx) &&
2860 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002861 !(test_bit(R5_UPTODATE, &dev->flags) ||
2862 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002863 test_bit(R5_Insync, &dev->flags)) {
2864 if (
2865 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002866 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002867 "%d for r-m-w\n", i);
2868 set_bit(R5_LOCKED, &dev->flags);
2869 set_bit(R5_Wantread, &dev->flags);
2870 s->locked++;
2871 } else {
2872 set_bit(STRIPE_DELAYED, &sh->state);
2873 set_bit(STRIPE_HANDLE, &sh->state);
2874 }
2875 }
2876 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002877 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002878 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002879 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002880 for (i = disks; i--; ) {
2881 struct r5dev *dev = &sh->dev[i];
2882 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002883 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002884 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002885 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002886 test_bit(R5_Wantcompute, &dev->flags))) {
2887 rcw++;
2888 if (!test_bit(R5_Insync, &dev->flags))
2889 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002890 if (
2891 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002892 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002893 "%d for Reconstruct\n", i);
2894 set_bit(R5_LOCKED, &dev->flags);
2895 set_bit(R5_Wantread, &dev->flags);
2896 s->locked++;
2897 } else {
2898 set_bit(STRIPE_DELAYED, &sh->state);
2899 set_bit(STRIPE_HANDLE, &sh->state);
2900 }
2901 }
2902 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002903 }
Dan Williamsa4456852007-07-09 11:56:43 -07002904 /* now if nothing is locked, and if we have enough data,
2905 * we can start a write request
2906 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002907 /* since handle_stripe can be called at any time we need to handle the
2908 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002909 * subsequent call wants to start a write request. raid_run_ops only
2910 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002911 * simultaneously. If this is not the case then new writes need to be
2912 * held off until the compute completes.
2913 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002914 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2915 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2916 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002917 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002918}
2919
NeilBrownd1688a62011-10-11 16:49:52 +11002920static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002921 struct stripe_head_state *s, int disks)
2922{
Dan Williamsecc65c92008-06-28 08:31:57 +10002923 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002924
Dan Williamsbd2ab672008-04-10 21:29:27 -07002925 set_bit(STRIPE_HANDLE, &sh->state);
2926
Dan Williamsecc65c92008-06-28 08:31:57 +10002927 switch (sh->check_state) {
2928 case check_state_idle:
2929 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002930 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002931 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002932 sh->check_state = check_state_run;
2933 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002934 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002935 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002936 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002937 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002938 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002939 /* fall through */
2940 case check_state_compute_result:
2941 sh->check_state = check_state_idle;
2942 if (!dev)
2943 dev = &sh->dev[sh->pd_idx];
2944
2945 /* check that a write has not made the stripe insync */
2946 if (test_bit(STRIPE_INSYNC, &sh->state))
2947 break;
2948
2949 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002950 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2951 BUG_ON(s->uptodate != disks);
2952
2953 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002954 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002955 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002956
Dan Williamsa4456852007-07-09 11:56:43 -07002957 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002958 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002959 break;
2960 case check_state_run:
2961 break; /* we will be called again upon completion */
2962 case check_state_check_result:
2963 sh->check_state = check_state_idle;
2964
2965 /* if a failure occurred during the check operation, leave
2966 * STRIPE_INSYNC not set and let the stripe be handled again
2967 */
2968 if (s->failed)
2969 break;
2970
2971 /* handle a successful check operation, if parity is correct
2972 * we are done. Otherwise update the mismatch count and repair
2973 * parity if !MD_RECOVERY_CHECK
2974 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002975 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002976 /* parity is correct (on disc,
2977 * not in buffer any more)
2978 */
2979 set_bit(STRIPE_INSYNC, &sh->state);
2980 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002981 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10002982 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2983 /* don't try to repair!! */
2984 set_bit(STRIPE_INSYNC, &sh->state);
2985 else {
2986 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002987 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002988 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2989 set_bit(R5_Wantcompute,
2990 &sh->dev[sh->pd_idx].flags);
2991 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002992 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002993 s->uptodate++;
2994 }
2995 }
2996 break;
2997 case check_state_compute_run:
2998 break;
2999 default:
3000 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3001 __func__, sh->check_state,
3002 (unsigned long long) sh->sector);
3003 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003004 }
3005}
3006
3007
NeilBrownd1688a62011-10-11 16:49:52 +11003008static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003009 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003010 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003011{
Dan Williamsa4456852007-07-09 11:56:43 -07003012 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003013 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003014 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003015
3016 set_bit(STRIPE_HANDLE, &sh->state);
3017
3018 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003019
Dan Williamsa4456852007-07-09 11:56:43 -07003020 /* Want to check and possibly repair P and Q.
3021 * However there could be one 'failed' device, in which
3022 * case we can only check one of them, possibly using the
3023 * other to generate missing data
3024 */
3025
Dan Williamsd82dfee2009-07-14 13:40:57 -07003026 switch (sh->check_state) {
3027 case check_state_idle:
3028 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003029 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003030 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003031 * makes sense to check P (If anything else were failed,
3032 * we would have used P to recreate it).
3033 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003034 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003035 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003036 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003037 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003038 * anything, so it makes sense to check it
3039 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003040 if (sh->check_state == check_state_run)
3041 sh->check_state = check_state_run_pq;
3042 else
3043 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003044 }
Dan Williams36d1c642009-07-14 11:48:22 -07003045
Dan Williamsd82dfee2009-07-14 13:40:57 -07003046 /* discard potentially stale zero_sum_result */
3047 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003048
Dan Williamsd82dfee2009-07-14 13:40:57 -07003049 if (sh->check_state == check_state_run) {
3050 /* async_xor_zero_sum destroys the contents of P */
3051 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3052 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003053 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003054 if (sh->check_state >= check_state_run &&
3055 sh->check_state <= check_state_run_pq) {
3056 /* async_syndrome_zero_sum preserves P and Q, so
3057 * no need to mark them !uptodate here
3058 */
3059 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3060 break;
3061 }
Dan Williams36d1c642009-07-14 11:48:22 -07003062
Dan Williamsd82dfee2009-07-14 13:40:57 -07003063 /* we have 2-disk failure */
3064 BUG_ON(s->failed != 2);
3065 /* fall through */
3066 case check_state_compute_result:
3067 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003068
Dan Williamsd82dfee2009-07-14 13:40:57 -07003069 /* check that a write has not made the stripe insync */
3070 if (test_bit(STRIPE_INSYNC, &sh->state))
3071 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003072
3073 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003074 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003075 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003076 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003077 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003078 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003079 s->locked++;
3080 set_bit(R5_LOCKED, &dev->flags);
3081 set_bit(R5_Wantwrite, &dev->flags);
3082 }
3083 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003084 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003085 s->locked++;
3086 set_bit(R5_LOCKED, &dev->flags);
3087 set_bit(R5_Wantwrite, &dev->flags);
3088 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003089 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003090 dev = &sh->dev[pd_idx];
3091 s->locked++;
3092 set_bit(R5_LOCKED, &dev->flags);
3093 set_bit(R5_Wantwrite, &dev->flags);
3094 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003095 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003096 dev = &sh->dev[qd_idx];
3097 s->locked++;
3098 set_bit(R5_LOCKED, &dev->flags);
3099 set_bit(R5_Wantwrite, &dev->flags);
3100 }
3101 clear_bit(STRIPE_DEGRADED, &sh->state);
3102
3103 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003104 break;
3105 case check_state_run:
3106 case check_state_run_q:
3107 case check_state_run_pq:
3108 break; /* we will be called again upon completion */
3109 case check_state_check_result:
3110 sh->check_state = check_state_idle;
3111
3112 /* handle a successful check operation, if parity is correct
3113 * we are done. Otherwise update the mismatch count and repair
3114 * parity if !MD_RECOVERY_CHECK
3115 */
3116 if (sh->ops.zero_sum_result == 0) {
3117 /* both parities are correct */
3118 if (!s->failed)
3119 set_bit(STRIPE_INSYNC, &sh->state);
3120 else {
3121 /* in contrast to the raid5 case we can validate
3122 * parity, but still have a failure to write
3123 * back
3124 */
3125 sh->check_state = check_state_compute_result;
3126 /* Returning at this point means that we may go
3127 * off and bring p and/or q uptodate again so
3128 * we make sure to check zero_sum_result again
3129 * to verify if p or q need writeback
3130 */
3131 }
3132 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003133 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003134 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3135 /* don't try to repair!! */
3136 set_bit(STRIPE_INSYNC, &sh->state);
3137 else {
3138 int *target = &sh->ops.target;
3139
3140 sh->ops.target = -1;
3141 sh->ops.target2 = -1;
3142 sh->check_state = check_state_compute_run;
3143 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3144 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3145 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3146 set_bit(R5_Wantcompute,
3147 &sh->dev[pd_idx].flags);
3148 *target = pd_idx;
3149 target = &sh->ops.target2;
3150 s->uptodate++;
3151 }
3152 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3153 set_bit(R5_Wantcompute,
3154 &sh->dev[qd_idx].flags);
3155 *target = qd_idx;
3156 s->uptodate++;
3157 }
3158 }
3159 }
3160 break;
3161 case check_state_compute_run:
3162 break;
3163 default:
3164 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3165 __func__, sh->check_state,
3166 (unsigned long long) sh->sector);
3167 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003168 }
3169}
3170
NeilBrownd1688a62011-10-11 16:49:52 +11003171static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003172{
3173 int i;
3174
3175 /* We have read all the blocks in this stripe and now we need to
3176 * copy some of them into a target stripe for expand.
3177 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003178 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003179 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3180 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003181 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003182 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003183 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003184 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003185
NeilBrown784052e2009-03-31 15:19:07 +11003186 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003187 sector_t s = raid5_compute_sector(conf, bn, 0,
3188 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003189 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003190 if (sh2 == NULL)
3191 /* so far only the early blocks of this stripe
3192 * have been requested. When later blocks
3193 * get requested, we will try again
3194 */
3195 continue;
3196 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3197 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3198 /* must have already done this block */
3199 release_stripe(sh2);
3200 continue;
3201 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003202
3203 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003204 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003205 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003206 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003207 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003208
Dan Williamsa4456852007-07-09 11:56:43 -07003209 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3210 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3211 for (j = 0; j < conf->raid_disks; j++)
3212 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003213 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003214 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3215 break;
3216 if (j == conf->raid_disks) {
3217 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3218 set_bit(STRIPE_HANDLE, &sh2->state);
3219 }
3220 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003221
Dan Williamsa4456852007-07-09 11:56:43 -07003222 }
NeilBrowna2e08552007-09-11 15:23:36 -07003223 /* done submitting copies, wait for them to complete */
3224 if (tx) {
3225 async_tx_ack(tx);
3226 dma_wait_for_async_tx(tx);
3227 }
Dan Williamsa4456852007-07-09 11:56:43 -07003228}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229
3230/*
3231 * handle_stripe - do things to a stripe.
3232 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003233 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3234 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003236 * return some read requests which now have data
3237 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 * schedule a read on some buffers
3239 * schedule a write of some buffers
3240 * return confirmation of parity correctness
3241 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 */
Dan Williamsa4456852007-07-09 11:56:43 -07003243
NeilBrownacfe7262011-07-27 11:00:36 +10003244static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003245{
NeilBrownd1688a62011-10-11 16:49:52 +11003246 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003247 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003248 struct r5dev *dev;
3249 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003250 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003251
NeilBrownacfe7262011-07-27 11:00:36 +10003252 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003253
NeilBrownacfe7262011-07-27 11:00:36 +10003254 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3255 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3256 s->failed_num[0] = -1;
3257 s->failed_num[1] = -1;
3258
3259 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003260 rcu_read_lock();
3261 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003262 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003263 sector_t first_bad;
3264 int bad_sectors;
3265 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003266
NeilBrown16a53ec2006-06-26 00:27:38 -07003267 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003268
Dan Williams45b42332007-07-09 11:56:43 -07003269 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003270 i, dev->flags,
3271 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003272 /* maybe we can reply to a read
3273 *
3274 * new wantfill requests are only permitted while
3275 * ops_complete_biofill is guaranteed to be inactive
3276 */
3277 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3278 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3279 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003280
3281 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003282 if (test_bit(R5_LOCKED, &dev->flags))
3283 s->locked++;
3284 if (test_bit(R5_UPTODATE, &dev->flags))
3285 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003286 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003287 s->compute++;
3288 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003289 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003290
NeilBrownacfe7262011-07-27 11:00:36 +10003291 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003292 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003293 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003294 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003295 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003296 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003297 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003298 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003299 }
Dan Williamsa4456852007-07-09 11:56:43 -07003300 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003301 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003302 /* Prefer to use the replacement for reads, but only
3303 * if it is recovered enough and has no bad blocks.
3304 */
3305 rdev = rcu_dereference(conf->disks[i].replacement);
3306 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3307 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3308 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3309 &first_bad, &bad_sectors))
3310 set_bit(R5_ReadRepl, &dev->flags);
3311 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003312 if (rdev)
3313 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003314 rdev = rcu_dereference(conf->disks[i].rdev);
3315 clear_bit(R5_ReadRepl, &dev->flags);
3316 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003317 if (rdev && test_bit(Faulty, &rdev->flags))
3318 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003319 if (rdev) {
3320 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3321 &first_bad, &bad_sectors);
3322 if (s->blocked_rdev == NULL
3323 && (test_bit(Blocked, &rdev->flags)
3324 || is_bad < 0)) {
3325 if (is_bad < 0)
3326 set_bit(BlockedBadBlocks,
3327 &rdev->flags);
3328 s->blocked_rdev = rdev;
3329 atomic_inc(&rdev->nr_pending);
3330 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003331 }
NeilBrown415e72d2010-06-17 17:25:21 +10003332 clear_bit(R5_Insync, &dev->flags);
3333 if (!rdev)
3334 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003335 else if (is_bad) {
3336 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003337 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3338 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003339 /* treat as in-sync, but with a read error
3340 * which we can now try to correct
3341 */
3342 set_bit(R5_Insync, &dev->flags);
3343 set_bit(R5_ReadError, &dev->flags);
3344 }
3345 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003346 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003347 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003348 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003349 set_bit(R5_Insync, &dev->flags);
3350 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3351 test_bit(R5_Expanded, &dev->flags))
3352 /* If we've reshaped into here, we assume it is Insync.
3353 * We will shortly update recovery_offset to make
3354 * it official.
3355 */
3356 set_bit(R5_Insync, &dev->flags);
3357
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003358 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003359 /* This flag does not apply to '.replacement'
3360 * only to .rdev, so make sure to check that*/
3361 struct md_rdev *rdev2 = rcu_dereference(
3362 conf->disks[i].rdev);
3363 if (rdev2 == rdev)
3364 clear_bit(R5_Insync, &dev->flags);
3365 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003366 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003367 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003368 } else
3369 clear_bit(R5_WriteError, &dev->flags);
3370 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003371 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003372 /* This flag does not apply to '.replacement'
3373 * only to .rdev, so make sure to check that*/
3374 struct md_rdev *rdev2 = rcu_dereference(
3375 conf->disks[i].rdev);
3376 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003377 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003378 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003379 } else
3380 clear_bit(R5_MadeGood, &dev->flags);
3381 }
NeilBrown977df362011-12-23 10:17:53 +11003382 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3383 struct md_rdev *rdev2 = rcu_dereference(
3384 conf->disks[i].replacement);
3385 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3386 s->handle_bad_blocks = 1;
3387 atomic_inc(&rdev2->nr_pending);
3388 } else
3389 clear_bit(R5_MadeGoodRepl, &dev->flags);
3390 }
NeilBrown415e72d2010-06-17 17:25:21 +10003391 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003392 /* The ReadError flag will just be confusing now */
3393 clear_bit(R5_ReadError, &dev->flags);
3394 clear_bit(R5_ReWrite, &dev->flags);
3395 }
NeilBrown415e72d2010-06-17 17:25:21 +10003396 if (test_bit(R5_ReadError, &dev->flags))
3397 clear_bit(R5_Insync, &dev->flags);
3398 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003399 if (s->failed < 2)
3400 s->failed_num[s->failed] = i;
3401 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003402 if (rdev && !test_bit(Faulty, &rdev->flags))
3403 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003404 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003405 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003406 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3407 /* If there is a failed device being replaced,
3408 * we must be recovering.
3409 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003410 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003411 * else we can only be replacing
3412 * sync and recovery both need to read all devices, and so
3413 * use the same flag.
3414 */
3415 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003416 sh->sector >= conf->mddev->recovery_cp ||
3417 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003418 s->syncing = 1;
3419 else
3420 s->replacing = 1;
3421 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003422 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003423}
NeilBrownf4168852007-02-28 20:11:53 -08003424
NeilBrowncc940152011-07-26 11:35:35 +10003425static void handle_stripe(struct stripe_head *sh)
3426{
3427 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003428 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003429 int i;
NeilBrown84789552011-07-27 11:00:36 +10003430 int prexor;
3431 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003432 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003433
3434 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003435 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003436 /* already being handled, ensure it gets handled
3437 * again when current action finishes */
3438 set_bit(STRIPE_HANDLE, &sh->state);
3439 return;
3440 }
3441
3442 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3443 set_bit(STRIPE_SYNCING, &sh->state);
3444 clear_bit(STRIPE_INSYNC, &sh->state);
3445 }
3446 clear_bit(STRIPE_DELAYED, &sh->state);
3447
3448 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3449 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3450 (unsigned long long)sh->sector, sh->state,
3451 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3452 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003453
NeilBrownacfe7262011-07-27 11:00:36 +10003454 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003455
NeilBrownbc2607f2011-07-28 11:39:22 +10003456 if (s.handle_bad_blocks) {
3457 set_bit(STRIPE_HANDLE, &sh->state);
3458 goto finish;
3459 }
3460
NeilBrown474af965fe2011-07-27 11:00:36 +10003461 if (unlikely(s.blocked_rdev)) {
3462 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003463 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003464 set_bit(STRIPE_HANDLE, &sh->state);
3465 goto finish;
3466 }
3467 /* There is nothing for the blocked_rdev to block */
3468 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3469 s.blocked_rdev = NULL;
3470 }
3471
3472 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3473 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3474 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3475 }
3476
3477 pr_debug("locked=%d uptodate=%d to_read=%d"
3478 " to_write=%d failed=%d failed_num=%d,%d\n",
3479 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3480 s.failed_num[0], s.failed_num[1]);
3481 /* check if the array has lost more than max_degraded devices and,
3482 * if so, some requests might need to be failed.
3483 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003484 if (s.failed > conf->max_degraded) {
3485 sh->check_state = 0;
3486 sh->reconstruct_state = 0;
3487 if (s.to_read+s.to_write+s.written)
3488 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003489 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003490 handle_failed_sync(conf, sh, &s);
3491 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003492
NeilBrown84789552011-07-27 11:00:36 +10003493 /* Now we check to see if any write operations have recently
3494 * completed
3495 */
3496 prexor = 0;
3497 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3498 prexor = 1;
3499 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3500 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3501 sh->reconstruct_state = reconstruct_state_idle;
3502
3503 /* All the 'written' buffers and the parity block are ready to
3504 * be written back to disk
3505 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003506 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3507 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003508 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003509 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3510 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003511 for (i = disks; i--; ) {
3512 struct r5dev *dev = &sh->dev[i];
3513 if (test_bit(R5_LOCKED, &dev->flags) &&
3514 (i == sh->pd_idx || i == sh->qd_idx ||
3515 dev->written)) {
3516 pr_debug("Writing block %d\n", i);
3517 set_bit(R5_Wantwrite, &dev->flags);
3518 if (prexor)
3519 continue;
3520 if (!test_bit(R5_Insync, &dev->flags) ||
3521 ((i == sh->pd_idx || i == sh->qd_idx) &&
3522 s.failed == 0))
3523 set_bit(STRIPE_INSYNC, &sh->state);
3524 }
3525 }
3526 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3527 s.dec_preread_active = 1;
3528 }
3529
NeilBrownef5b7c62012-11-22 09:13:36 +11003530 /*
3531 * might be able to return some write requests if the parity blocks
3532 * are safe, or on a failed drive
3533 */
3534 pdev = &sh->dev[sh->pd_idx];
3535 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3536 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3537 qdev = &sh->dev[sh->qd_idx];
3538 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3539 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3540 || conf->level < 6;
3541
3542 if (s.written &&
3543 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3544 && !test_bit(R5_LOCKED, &pdev->flags)
3545 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3546 test_bit(R5_Discard, &pdev->flags))))) &&
3547 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3548 && !test_bit(R5_LOCKED, &qdev->flags)
3549 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3550 test_bit(R5_Discard, &qdev->flags))))))
3551 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3552
3553 /* Now we might consider reading some blocks, either to check/generate
3554 * parity, or to satisfy requests
3555 * or to load a block that is being partially written.
3556 */
3557 if (s.to_read || s.non_overwrite
3558 || (conf->level == 6 && s.to_write && s.failed)
3559 || (s.syncing && (s.uptodate + s.compute < disks))
3560 || s.replacing
3561 || s.expanding)
3562 handle_stripe_fill(sh, &s, disks);
3563
NeilBrown84789552011-07-27 11:00:36 +10003564 /* Now to consider new write requests and what else, if anything
3565 * should be read. We do not handle new writes when:
3566 * 1/ A 'write' operation (copy+xor) is already in flight.
3567 * 2/ A 'check' operation is in flight, as it may clobber the parity
3568 * block.
3569 */
3570 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3571 handle_stripe_dirtying(conf, sh, &s, disks);
3572
3573 /* maybe we need to check and possibly fix the parity for this stripe
3574 * Any reads will already have been scheduled, so we just see if enough
3575 * data is available. The parity check is held off while parity
3576 * dependent operations are in flight.
3577 */
3578 if (sh->check_state ||
3579 (s.syncing && s.locked == 0 &&
3580 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3581 !test_bit(STRIPE_INSYNC, &sh->state))) {
3582 if (conf->level == 6)
3583 handle_parity_checks6(conf, sh, &s, disks);
3584 else
3585 handle_parity_checks5(conf, sh, &s, disks);
3586 }
NeilBrownc5a31002011-07-27 11:00:36 +10003587
NeilBrown9a3e1102011-12-23 10:17:53 +11003588 if (s.replacing && s.locked == 0
3589 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3590 /* Write out to replacement devices where possible */
3591 for (i = 0; i < conf->raid_disks; i++)
3592 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3593 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3594 set_bit(R5_WantReplace, &sh->dev[i].flags);
3595 set_bit(R5_LOCKED, &sh->dev[i].flags);
3596 s.locked++;
3597 }
3598 set_bit(STRIPE_INSYNC, &sh->state);
3599 }
3600 if ((s.syncing || s.replacing) && s.locked == 0 &&
3601 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003602 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3603 clear_bit(STRIPE_SYNCING, &sh->state);
3604 }
3605
3606 /* If the failed drives are just a ReadError, then we might need
3607 * to progress the repair/check process
3608 */
3609 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3610 for (i = 0; i < s.failed; i++) {
3611 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3612 if (test_bit(R5_ReadError, &dev->flags)
3613 && !test_bit(R5_LOCKED, &dev->flags)
3614 && test_bit(R5_UPTODATE, &dev->flags)
3615 ) {
3616 if (!test_bit(R5_ReWrite, &dev->flags)) {
3617 set_bit(R5_Wantwrite, &dev->flags);
3618 set_bit(R5_ReWrite, &dev->flags);
3619 set_bit(R5_LOCKED, &dev->flags);
3620 s.locked++;
3621 } else {
3622 /* let's read it back */
3623 set_bit(R5_Wantread, &dev->flags);
3624 set_bit(R5_LOCKED, &dev->flags);
3625 s.locked++;
3626 }
3627 }
3628 }
3629
3630
NeilBrown3687c062011-07-27 11:00:36 +10003631 /* Finish reconstruct operations initiated by the expansion process */
3632 if (sh->reconstruct_state == reconstruct_state_result) {
3633 struct stripe_head *sh_src
3634 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3635 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3636 /* sh cannot be written until sh_src has been read.
3637 * so arrange for sh to be delayed a little
3638 */
3639 set_bit(STRIPE_DELAYED, &sh->state);
3640 set_bit(STRIPE_HANDLE, &sh->state);
3641 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3642 &sh_src->state))
3643 atomic_inc(&conf->preread_active_stripes);
3644 release_stripe(sh_src);
3645 goto finish;
3646 }
3647 if (sh_src)
3648 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003649
NeilBrown3687c062011-07-27 11:00:36 +10003650 sh->reconstruct_state = reconstruct_state_idle;
3651 clear_bit(STRIPE_EXPANDING, &sh->state);
3652 for (i = conf->raid_disks; i--; ) {
3653 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3654 set_bit(R5_LOCKED, &sh->dev[i].flags);
3655 s.locked++;
3656 }
3657 }
3658
3659 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3660 !sh->reconstruct_state) {
3661 /* Need to write out all blocks after computing parity */
3662 sh->disks = conf->raid_disks;
3663 stripe_set_idx(sh->sector, conf, 0, sh);
3664 schedule_reconstruction(sh, &s, 1, 1);
3665 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3666 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3667 atomic_dec(&conf->reshape_stripes);
3668 wake_up(&conf->wait_for_overlap);
3669 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3670 }
3671
3672 if (s.expanding && s.locked == 0 &&
3673 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3674 handle_stripe_expansion(conf, sh);
3675
3676finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003677 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003678 if (unlikely(s.blocked_rdev)) {
3679 if (conf->mddev->external)
3680 md_wait_for_blocked_rdev(s.blocked_rdev,
3681 conf->mddev);
3682 else
3683 /* Internal metadata will immediately
3684 * be written by raid5d, so we don't
3685 * need to wait here.
3686 */
3687 rdev_dec_pending(s.blocked_rdev,
3688 conf->mddev);
3689 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003690
NeilBrownbc2607f2011-07-28 11:39:22 +10003691 if (s.handle_bad_blocks)
3692 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003693 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003694 struct r5dev *dev = &sh->dev[i];
3695 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3696 /* We own a safe reference to the rdev */
3697 rdev = conf->disks[i].rdev;
3698 if (!rdev_set_badblocks(rdev, sh->sector,
3699 STRIPE_SECTORS, 0))
3700 md_error(conf->mddev, rdev);
3701 rdev_dec_pending(rdev, conf->mddev);
3702 }
NeilBrownb84db562011-07-28 11:39:23 +10003703 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3704 rdev = conf->disks[i].rdev;
3705 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003706 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003707 rdev_dec_pending(rdev, conf->mddev);
3708 }
NeilBrown977df362011-12-23 10:17:53 +11003709 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3710 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003711 if (!rdev)
3712 /* rdev have been moved down */
3713 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003714 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003715 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003716 rdev_dec_pending(rdev, conf->mddev);
3717 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003718 }
3719
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003720 if (s.ops_request)
3721 raid_run_ops(sh, s.ops_request);
3722
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003723 ops_run_io(sh, &s);
3724
NeilBrownc5709ef2011-07-26 11:35:20 +10003725 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003726 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003727 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003728 * have actually been submitted.
3729 */
3730 atomic_dec(&conf->preread_active_stripes);
3731 if (atomic_read(&conf->preread_active_stripes) <
3732 IO_THRESHOLD)
3733 md_wakeup_thread(conf->mddev->thread);
3734 }
3735
NeilBrownc5709ef2011-07-26 11:35:20 +10003736 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003737
Dan Williams257a4b42011-11-08 16:22:06 +11003738 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003739}
3740
NeilBrownd1688a62011-10-11 16:49:52 +11003741static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742{
3743 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3744 while (!list_empty(&conf->delayed_list)) {
3745 struct list_head *l = conf->delayed_list.next;
3746 struct stripe_head *sh;
3747 sh = list_entry(l, struct stripe_head, lru);
3748 list_del_init(l);
3749 clear_bit(STRIPE_DELAYED, &sh->state);
3750 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3751 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003752 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 }
NeilBrown482c0832011-04-18 18:25:42 +10003754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755}
3756
NeilBrownd1688a62011-10-11 16:49:52 +11003757static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003758{
3759 /* device_lock is held */
3760 struct list_head head;
3761 list_add(&head, &conf->bitmap_list);
3762 list_del_init(&conf->bitmap_list);
3763 while (!list_empty(&head)) {
3764 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3765 list_del_init(&sh->lru);
3766 atomic_inc(&sh->count);
3767 __release_stripe(conf, sh);
3768 }
3769}
3770
NeilBrownfd01b882011-10-11 16:47:53 +11003771int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003772{
NeilBrownd1688a62011-10-11 16:49:52 +11003773 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003774
3775 /* No difference between reads and writes. Just check
3776 * how busy the stripe_cache is
3777 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003778
NeilBrownf022b2f2006-10-03 01:15:56 -07003779 if (conf->inactive_blocked)
3780 return 1;
3781 if (conf->quiesce)
3782 return 1;
3783 if (list_empty_careful(&conf->inactive_list))
3784 return 1;
3785
3786 return 0;
3787}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003788EXPORT_SYMBOL_GPL(md_raid5_congested);
3789
3790static int raid5_congested(void *data, int bits)
3791{
NeilBrownfd01b882011-10-11 16:47:53 +11003792 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003793
3794 return mddev_congested(mddev, bits) ||
3795 md_raid5_congested(mddev, bits);
3796}
NeilBrownf022b2f2006-10-03 01:15:56 -07003797
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003798/* We want read requests to align with chunks where possible,
3799 * but write requests don't need to.
3800 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003801static int raid5_mergeable_bvec(struct request_queue *q,
3802 struct bvec_merge_data *bvm,
3803 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003804{
NeilBrownfd01b882011-10-11 16:47:53 +11003805 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003806 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003807 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003808 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003809 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003810
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003811 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003812 return biovec->bv_len; /* always allow writes to be mergeable */
3813
Andre Noll664e7c42009-06-18 08:45:27 +10003814 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3815 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003816 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3817 if (max < 0) max = 0;
3818 if (max <= biovec->bv_len && bio_sectors == 0)
3819 return biovec->bv_len;
3820 else
3821 return max;
3822}
3823
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003824
NeilBrownfd01b882011-10-11 16:47:53 +11003825static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003826{
3827 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003828 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003829 unsigned int bio_sectors = bio->bi_size >> 9;
3830
Andre Noll664e7c42009-06-18 08:45:27 +10003831 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3832 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003833 return chunk_sectors >=
3834 ((sector & (chunk_sectors - 1)) + bio_sectors);
3835}
3836
3837/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003838 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3839 * later sampled by raid5d.
3840 */
NeilBrownd1688a62011-10-11 16:49:52 +11003841static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003842{
3843 unsigned long flags;
3844
3845 spin_lock_irqsave(&conf->device_lock, flags);
3846
3847 bi->bi_next = conf->retry_read_aligned_list;
3848 conf->retry_read_aligned_list = bi;
3849
3850 spin_unlock_irqrestore(&conf->device_lock, flags);
3851 md_wakeup_thread(conf->mddev->thread);
3852}
3853
3854
NeilBrownd1688a62011-10-11 16:49:52 +11003855static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003856{
3857 struct bio *bi;
3858
3859 bi = conf->retry_read_aligned;
3860 if (bi) {
3861 conf->retry_read_aligned = NULL;
3862 return bi;
3863 }
3864 bi = conf->retry_read_aligned_list;
3865 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003866 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003867 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003868 /*
3869 * this sets the active strip count to 1 and the processed
3870 * strip count to zero (upper 8 bits)
3871 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003872 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003873 }
3874
3875 return bi;
3876}
3877
3878
3879/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003880 * The "raid5_align_endio" should check if the read succeeded and if it
3881 * did, call bio_endio on the original bio (having bio_put the new bio
3882 * first).
3883 * If the read failed..
3884 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003885static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003886{
3887 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003888 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003889 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003890 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003891 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003892
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003893 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003894
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003895 rdev = (void*)raid_bi->bi_next;
3896 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003897 mddev = rdev->mddev;
3898 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003899
3900 rdev_dec_pending(rdev, conf->mddev);
3901
3902 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003903 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003904 if (atomic_dec_and_test(&conf->active_aligned_reads))
3905 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003906 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003907 }
3908
3909
Dan Williams45b42332007-07-09 11:56:43 -07003910 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003911
3912 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003913}
3914
Neil Brown387bb172007-02-08 14:20:29 -08003915static int bio_fits_rdev(struct bio *bi)
3916{
Jens Axboe165125e2007-07-24 09:28:11 +02003917 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003918
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003919 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003920 return 0;
3921 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003922 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003923 return 0;
3924
3925 if (q->merge_bvec_fn)
3926 /* it's too hard to apply the merge_bvec_fn at this stage,
3927 * just just give up
3928 */
3929 return 0;
3930
3931 return 1;
3932}
3933
3934
NeilBrownfd01b882011-10-11 16:47:53 +11003935static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003936{
NeilBrownd1688a62011-10-11 16:49:52 +11003937 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003938 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003939 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003940 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003941 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003942
3943 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003944 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003945 return 0;
3946 }
3947 /*
NeilBrowna167f662010-10-26 18:31:13 +11003948 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003949 */
NeilBrowna167f662010-10-26 18:31:13 +11003950 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003951 if (!align_bi)
3952 return 0;
3953 /*
3954 * set bi_end_io to a new function, and set bi_private to the
3955 * original bio.
3956 */
3957 align_bi->bi_end_io = raid5_align_endio;
3958 align_bi->bi_private = raid_bio;
3959 /*
3960 * compute position
3961 */
NeilBrown112bf892009-03-31 14:39:38 +11003962 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3963 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003964 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003965
NeilBrown671488c2011-12-23 10:17:52 +11003966 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003967 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003968 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3969 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3970 rdev->recovery_offset < end_sector) {
3971 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3972 if (rdev &&
3973 (test_bit(Faulty, &rdev->flags) ||
3974 !(test_bit(In_sync, &rdev->flags) ||
3975 rdev->recovery_offset >= end_sector)))
3976 rdev = NULL;
3977 }
3978 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003979 sector_t first_bad;
3980 int bad_sectors;
3981
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003982 atomic_inc(&rdev->nr_pending);
3983 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003984 raid_bio->bi_next = (void*)rdev;
3985 align_bi->bi_bdev = rdev->bdev;
3986 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003987
NeilBrown31c176e2011-07-28 11:39:22 +10003988 if (!bio_fits_rdev(align_bi) ||
3989 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3990 &first_bad, &bad_sectors)) {
3991 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003992 bio_put(align_bi);
3993 rdev_dec_pending(rdev, mddev);
3994 return 0;
3995 }
3996
majianpeng6c0544e2012-06-12 08:31:10 +08003997 /* No reshape active, so we can trust rdev->data_offset */
3998 align_bi->bi_sector += rdev->data_offset;
3999
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004000 spin_lock_irq(&conf->device_lock);
4001 wait_event_lock_irq(conf->wait_for_stripe,
4002 conf->quiesce == 0,
4003 conf->device_lock, /* nothing */);
4004 atomic_inc(&conf->active_aligned_reads);
4005 spin_unlock_irq(&conf->device_lock);
4006
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004007 generic_make_request(align_bi);
4008 return 1;
4009 } else {
4010 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004011 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004012 return 0;
4013 }
4014}
4015
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004016/* __get_priority_stripe - get the next stripe to process
4017 *
4018 * Full stripe writes are allowed to pass preread active stripes up until
4019 * the bypass_threshold is exceeded. In general the bypass_count
4020 * increments when the handle_list is handled before the hold_list; however, it
4021 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4022 * stripe with in flight i/o. The bypass_count will be reset when the
4023 * head of the hold_list has changed, i.e. the head was promoted to the
4024 * handle_list.
4025 */
NeilBrownd1688a62011-10-11 16:49:52 +11004026static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004027{
4028 struct stripe_head *sh;
4029
4030 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4031 __func__,
4032 list_empty(&conf->handle_list) ? "empty" : "busy",
4033 list_empty(&conf->hold_list) ? "empty" : "busy",
4034 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4035
4036 if (!list_empty(&conf->handle_list)) {
4037 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4038
4039 if (list_empty(&conf->hold_list))
4040 conf->bypass_count = 0;
4041 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4042 if (conf->hold_list.next == conf->last_hold)
4043 conf->bypass_count++;
4044 else {
4045 conf->last_hold = conf->hold_list.next;
4046 conf->bypass_count -= conf->bypass_threshold;
4047 if (conf->bypass_count < 0)
4048 conf->bypass_count = 0;
4049 }
4050 }
4051 } else if (!list_empty(&conf->hold_list) &&
4052 ((conf->bypass_threshold &&
4053 conf->bypass_count > conf->bypass_threshold) ||
4054 atomic_read(&conf->pending_full_writes) == 0)) {
4055 sh = list_entry(conf->hold_list.next,
4056 typeof(*sh), lru);
4057 conf->bypass_count -= conf->bypass_threshold;
4058 if (conf->bypass_count < 0)
4059 conf->bypass_count = 0;
4060 } else
4061 return NULL;
4062
4063 list_del_init(&sh->lru);
4064 atomic_inc(&sh->count);
4065 BUG_ON(atomic_read(&sh->count) != 1);
4066 return sh;
4067}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004068
Shaohua Li8811b592012-08-02 08:33:00 +10004069struct raid5_plug_cb {
4070 struct blk_plug_cb cb;
4071 struct list_head list;
4072};
4073
4074static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4075{
4076 struct raid5_plug_cb *cb = container_of(
4077 blk_cb, struct raid5_plug_cb, cb);
4078 struct stripe_head *sh;
4079 struct mddev *mddev = cb->cb.data;
4080 struct r5conf *conf = mddev->private;
4081
4082 if (cb->list.next && !list_empty(&cb->list)) {
4083 spin_lock_irq(&conf->device_lock);
4084 while (!list_empty(&cb->list)) {
4085 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4086 list_del_init(&sh->lru);
4087 /*
4088 * avoid race release_stripe_plug() sees
4089 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4090 * is still in our list
4091 */
4092 smp_mb__before_clear_bit();
4093 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4094 __release_stripe(conf, sh);
4095 }
4096 spin_unlock_irq(&conf->device_lock);
4097 }
4098 kfree(cb);
4099}
4100
4101static void release_stripe_plug(struct mddev *mddev,
4102 struct stripe_head *sh)
4103{
4104 struct blk_plug_cb *blk_cb = blk_check_plugged(
4105 raid5_unplug, mddev,
4106 sizeof(struct raid5_plug_cb));
4107 struct raid5_plug_cb *cb;
4108
4109 if (!blk_cb) {
4110 release_stripe(sh);
4111 return;
4112 }
4113
4114 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4115
4116 if (cb->list.next == NULL)
4117 INIT_LIST_HEAD(&cb->list);
4118
4119 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4120 list_add_tail(&sh->lru, &cb->list);
4121 else
4122 release_stripe(sh);
4123}
4124
Shaohua Li620125f2012-10-11 13:49:05 +11004125static void make_discard_request(struct mddev *mddev, struct bio *bi)
4126{
4127 struct r5conf *conf = mddev->private;
4128 sector_t logical_sector, last_sector;
4129 struct stripe_head *sh;
4130 int remaining;
4131 int stripe_sectors;
4132
4133 if (mddev->reshape_position != MaxSector)
4134 /* Skip discard while reshape is happening */
4135 return;
4136
4137 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4138 last_sector = bi->bi_sector + (bi->bi_size>>9);
4139
4140 bi->bi_next = NULL;
4141 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4142
4143 stripe_sectors = conf->chunk_sectors *
4144 (conf->raid_disks - conf->max_degraded);
4145 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4146 stripe_sectors);
4147 sector_div(last_sector, stripe_sectors);
4148
4149 logical_sector *= conf->chunk_sectors;
4150 last_sector *= conf->chunk_sectors;
4151
4152 for (; logical_sector < last_sector;
4153 logical_sector += STRIPE_SECTORS) {
4154 DEFINE_WAIT(w);
4155 int d;
4156 again:
4157 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4158 prepare_to_wait(&conf->wait_for_overlap, &w,
4159 TASK_UNINTERRUPTIBLE);
4160 spin_lock_irq(&sh->stripe_lock);
4161 for (d = 0; d < conf->raid_disks; d++) {
4162 if (d == sh->pd_idx || d == sh->qd_idx)
4163 continue;
4164 if (sh->dev[d].towrite || sh->dev[d].toread) {
4165 set_bit(R5_Overlap, &sh->dev[d].flags);
4166 spin_unlock_irq(&sh->stripe_lock);
4167 release_stripe(sh);
4168 schedule();
4169 goto again;
4170 }
4171 }
4172 finish_wait(&conf->wait_for_overlap, &w);
4173 for (d = 0; d < conf->raid_disks; d++) {
4174 if (d == sh->pd_idx || d == sh->qd_idx)
4175 continue;
4176 sh->dev[d].towrite = bi;
4177 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4178 raid5_inc_bi_active_stripes(bi);
4179 }
4180 spin_unlock_irq(&sh->stripe_lock);
4181 if (conf->mddev->bitmap) {
4182 for (d = 0;
4183 d < conf->raid_disks - conf->max_degraded;
4184 d++)
4185 bitmap_startwrite(mddev->bitmap,
4186 sh->sector,
4187 STRIPE_SECTORS,
4188 0);
4189 sh->bm_seq = conf->seq_flush + 1;
4190 set_bit(STRIPE_BIT_DELAY, &sh->state);
4191 }
4192
4193 set_bit(STRIPE_HANDLE, &sh->state);
4194 clear_bit(STRIPE_DELAYED, &sh->state);
4195 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4196 atomic_inc(&conf->preread_active_stripes);
4197 release_stripe_plug(mddev, sh);
4198 }
4199
4200 remaining = raid5_dec_bi_active_stripes(bi);
4201 if (remaining == 0) {
4202 md_write_end(mddev);
4203 bio_endio(bi, 0);
4204 }
4205}
4206
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004207static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208{
NeilBrownd1688a62011-10-11 16:49:52 +11004209 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004210 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 sector_t new_sector;
4212 sector_t logical_sector, last_sector;
4213 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004214 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004215 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216
Tejun Heoe9c74692010-09-03 11:56:18 +02004217 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4218 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004219 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004220 }
4221
NeilBrown3d310eb2005-06-21 17:17:26 -07004222 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004223
NeilBrown802ba062006-12-13 00:34:13 -08004224 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004225 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004226 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004227 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004228
Shaohua Li620125f2012-10-11 13:49:05 +11004229 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4230 make_discard_request(mddev, bi);
4231 return;
4232 }
4233
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4235 last_sector = bi->bi_sector + (bi->bi_size>>9);
4236 bi->bi_next = NULL;
4237 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004238
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4240 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004241 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004242
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004243 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004244 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004245 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004246 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004247 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08004248 * 64bit on a 32bit platform, and so it might be
4249 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004250 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08004251 * the lock is dropped, so once we get a reference
4252 * to the stripe that we think it is, we will have
4253 * to check again.
4254 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004255 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004256 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004257 ? logical_sector < conf->reshape_progress
4258 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004259 previous = 1;
4260 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004261 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004262 ? logical_sector < conf->reshape_safe
4263 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004264 spin_unlock_irq(&conf->device_lock);
4265 schedule();
4266 goto retry;
4267 }
4268 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004269 spin_unlock_irq(&conf->device_lock);
4270 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004271
NeilBrown112bf892009-03-31 14:39:38 +11004272 new_sector = raid5_compute_sector(conf, logical_sector,
4273 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004274 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004275 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 (unsigned long long)new_sector,
4277 (unsigned long long)logical_sector);
4278
NeilBrownb5663ba2009-03-31 14:39:38 +11004279 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004280 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004282 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004283 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08004284 * stripe, so we must do the range check again.
4285 * Expansion could still move past after this
4286 * test, but as we are holding a reference to
4287 * 'sh', we know that if that happens,
4288 * STRIPE_EXPANDING will get set and the expansion
4289 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004290 */
4291 int must_retry = 0;
4292 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004293 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004294 ? logical_sector >= conf->reshape_progress
4295 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004296 /* mismatch, need to try again */
4297 must_retry = 1;
4298 spin_unlock_irq(&conf->device_lock);
4299 if (must_retry) {
4300 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004301 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004302 goto retry;
4303 }
4304 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004305
Namhyung Kimffd96e32011-07-18 17:38:51 +10004306 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004307 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004308 logical_sector < mddev->suspend_hi) {
4309 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004310 /* As the suspend_* range is controlled by
4311 * userspace, we want an interruptible
4312 * wait.
4313 */
4314 flush_signals(current);
4315 prepare_to_wait(&conf->wait_for_overlap,
4316 &w, TASK_INTERRUPTIBLE);
4317 if (logical_sector >= mddev->suspend_lo &&
4318 logical_sector < mddev->suspend_hi)
4319 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004320 goto retry;
4321 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004322
4323 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004324 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004325 /* Stripe is busy expanding or
4326 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 * and wait a while
4328 */
NeilBrown482c0832011-04-18 18:25:42 +10004329 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 release_stripe(sh);
4331 schedule();
4332 goto retry;
4333 }
4334 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004335 set_bit(STRIPE_HANDLE, &sh->state);
4336 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004337 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004338 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4339 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004340 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 } else {
4342 /* cannot get stripe for read-ahead, just give-up */
4343 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4344 finish_wait(&conf->wait_for_overlap, &w);
4345 break;
4346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004348
Shaohua Lie7836bd62012-07-19 16:01:31 +10004349 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004350 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
NeilBrown16a53ec2006-06-26 00:27:38 -07004352 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004354
Neil Brown0e13fe232008-06-28 08:31:20 +10004355 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357}
4358
NeilBrownfd01b882011-10-11 16:47:53 +11004359static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004360
NeilBrownfd01b882011-10-11 16:47:53 +11004361static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362{
NeilBrown52c03292006-06-26 00:27:43 -07004363 /* reshaping is quite different to recovery/resync so it is
4364 * handled quite separately ... here.
4365 *
4366 * On each call to sync_request, we gather one chunk worth of
4367 * destination stripes and flag them as expanding.
4368 * Then we find all the source stripes and request reads.
4369 * As the reads complete, handle_stripe will copy the data
4370 * into the destination stripe and release that stripe.
4371 */
NeilBrownd1688a62011-10-11 16:49:52 +11004372 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004374 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004375 int raid_disks = conf->previous_raid_disks;
4376 int data_disks = raid_disks - conf->max_degraded;
4377 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004378 int i;
4379 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004380 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004381 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004382 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004383 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004384
NeilBrownfef9c612009-03-31 15:16:46 +11004385 if (sector_nr == 0) {
4386 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004387 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004388 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4389 sector_nr = raid5_size(mddev, 0, 0)
4390 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004391 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004392 conf->reshape_progress > 0)
4393 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004394 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004395 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004396 mddev->curr_resync_completed = sector_nr;
4397 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004398 *skipped = 1;
4399 return sector_nr;
4400 }
NeilBrown52c03292006-06-26 00:27:43 -07004401 }
4402
NeilBrown7a661382009-03-31 15:21:40 +11004403 /* We need to process a full chunk at a time.
4404 * If old and new chunk sizes differ, we need to process the
4405 * largest of these
4406 */
Andre Noll664e7c42009-06-18 08:45:27 +10004407 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4408 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004409 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004410 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004411
NeilBrownb5254dd2012-05-21 09:27:01 +10004412 /* We update the metadata at least every 10 seconds, or when
4413 * the data about to be copied would over-write the source of
4414 * the data at the front of the range. i.e. one new_stripe
4415 * along from reshape_progress new_maps to after where
4416 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004417 */
NeilBrownfef9c612009-03-31 15:16:46 +11004418 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004419 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004420 readpos = conf->reshape_progress;
4421 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004422 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004423 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004424 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004425 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004426 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004427 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004428 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004429 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004430 readpos -= min_t(sector_t, reshape_sectors, readpos);
4431 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004432 }
NeilBrown52c03292006-06-26 00:27:43 -07004433
NeilBrownb5254dd2012-05-21 09:27:01 +10004434 /* Having calculated the 'writepos' possibly use it
4435 * to set 'stripe_addr' which is where we will write to.
4436 */
4437 if (mddev->reshape_backwards) {
4438 BUG_ON(conf->reshape_progress == 0);
4439 stripe_addr = writepos;
4440 BUG_ON((mddev->dev_sectors &
4441 ~((sector_t)reshape_sectors - 1))
4442 - reshape_sectors - stripe_addr
4443 != sector_nr);
4444 } else {
4445 BUG_ON(writepos != sector_nr + reshape_sectors);
4446 stripe_addr = sector_nr;
4447 }
4448
NeilBrownc8f517c2009-03-31 15:28:40 +11004449 /* 'writepos' is the most advanced device address we might write.
4450 * 'readpos' is the least advanced device address we might read.
4451 * 'safepos' is the least address recorded in the metadata as having
4452 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004453 * If there is a min_offset_diff, these are adjusted either by
4454 * increasing the safepos/readpos if diff is negative, or
4455 * increasing writepos if diff is positive.
4456 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004457 * ensure safety in the face of a crash - that must be done by userspace
4458 * making a backup of the data. So in that case there is no particular
4459 * rush to update metadata.
4460 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4461 * update the metadata to advance 'safepos' to match 'readpos' so that
4462 * we can be safe in the event of a crash.
4463 * So we insist on updating metadata if safepos is behind writepos and
4464 * readpos is beyond writepos.
4465 * In any case, update the metadata every 10 seconds.
4466 * Maybe that number should be configurable, but I'm not sure it is
4467 * worth it.... maybe it could be a multiple of safemode_delay???
4468 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004469 if (conf->min_offset_diff < 0) {
4470 safepos += -conf->min_offset_diff;
4471 readpos += -conf->min_offset_diff;
4472 } else
4473 writepos += conf->min_offset_diff;
4474
NeilBrown2c810cd2012-05-21 09:27:00 +10004475 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004476 ? (safepos > writepos && readpos < writepos)
4477 : (safepos < writepos && readpos > writepos)) ||
4478 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004479 /* Cannot proceed until we've updated the superblock... */
4480 wait_event(conf->wait_for_overlap,
4481 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004482 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004483 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004484 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b422006-10-03 01:15:46 -07004485 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004486 md_wakeup_thread(mddev->thread);
NeilBrown850b2b422006-10-03 01:15:46 -07004487 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004488 kthread_should_stop());
4489 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004490 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004491 spin_unlock_irq(&conf->device_lock);
4492 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004493 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004494 }
4495
NeilBrownab69ae12009-03-31 15:26:47 +11004496 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004497 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004498 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004499 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004500 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004501 set_bit(STRIPE_EXPANDING, &sh->state);
4502 atomic_inc(&conf->reshape_stripes);
4503 /* If any of this stripe is beyond the end of the old
4504 * array, then we need to zero those blocks
4505 */
4506 for (j=sh->disks; j--;) {
4507 sector_t s;
4508 if (j == sh->pd_idx)
4509 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004510 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004511 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004512 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004513 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004514 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004515 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004516 continue;
4517 }
4518 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4519 set_bit(R5_Expanded, &sh->dev[j].flags);
4520 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4521 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004522 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004523 set_bit(STRIPE_EXPAND_READY, &sh->state);
4524 set_bit(STRIPE_HANDLE, &sh->state);
4525 }
NeilBrownab69ae12009-03-31 15:26:47 +11004526 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004527 }
4528 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004529 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004530 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004531 else
NeilBrown7a661382009-03-31 15:21:40 +11004532 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004533 spin_unlock_irq(&conf->device_lock);
4534 /* Ok, those stripe are ready. We can start scheduling
4535 * reads on the source stripes.
4536 * The source stripes are determined by mapping the first and last
4537 * block on the destination stripes.
4538 */
NeilBrown52c03292006-06-26 00:27:43 -07004539 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004540 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004541 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004542 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004543 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004544 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004545 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004546 if (last_sector >= mddev->dev_sectors)
4547 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004548 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004549 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004550 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4551 set_bit(STRIPE_HANDLE, &sh->state);
4552 release_stripe(sh);
4553 first_sector += STRIPE_SECTORS;
4554 }
NeilBrownab69ae12009-03-31 15:26:47 +11004555 /* Now that the sources are clearly marked, we can release
4556 * the destination stripes
4557 */
4558 while (!list_empty(&stripes)) {
4559 sh = list_entry(stripes.next, struct stripe_head, lru);
4560 list_del_init(&sh->lru);
4561 release_stripe(sh);
4562 }
NeilBrownc6207272008-02-06 01:39:52 -08004563 /* If this takes us to the resync_max point where we have to pause,
4564 * then we need to write out the superblock.
4565 */
NeilBrown7a661382009-03-31 15:21:40 +11004566 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004567 if ((sector_nr - mddev->curr_resync_completed) * 2
4568 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004569 /* Cannot proceed until we've updated the superblock... */
4570 wait_event(conf->wait_for_overlap,
4571 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004572 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004573 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004574 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004575 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4576 md_wakeup_thread(mddev->thread);
4577 wait_event(mddev->sb_wait,
4578 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4579 || kthread_should_stop());
4580 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004581 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004582 spin_unlock_irq(&conf->device_lock);
4583 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004584 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004585 }
NeilBrown7a661382009-03-31 15:21:40 +11004586 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004587}
4588
4589/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004590static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004591{
NeilBrownd1688a62011-10-11 16:49:52 +11004592 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004593 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004594 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004595 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004596 int still_degraded = 0;
4597 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
NeilBrown72626682005-09-09 16:23:54 -07004599 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004601
NeilBrown29269552006-03-27 01:18:10 -08004602 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4603 end_reshape(conf);
4604 return 0;
4605 }
NeilBrown72626682005-09-09 16:23:54 -07004606
4607 if (mddev->curr_resync < max_sector) /* aborted */
4608 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4609 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004610 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004611 conf->fullsync = 0;
4612 bitmap_close_sync(mddev->bitmap);
4613
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 return 0;
4615 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004616
NeilBrown64bd6602009-08-03 10:59:58 +10004617 /* Allow raid5_quiesce to complete */
4618 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4619
NeilBrown52c03292006-06-26 00:27:43 -07004620 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4621 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004622
NeilBrownc6207272008-02-06 01:39:52 -08004623 /* No need to check resync_max as we never do more than one
4624 * stripe, and as resync_max will always be on a chunk boundary,
4625 * if the check in md_do_sync didn't fire, there is no chance
4626 * of overstepping resync_max here
4627 */
4628
NeilBrown16a53ec2006-06-26 00:27:38 -07004629 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 * to resync, then assert that we are finished, because there is
4631 * nothing we can do.
4632 */
NeilBrown3285edf2006-06-26 00:27:55 -07004633 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004634 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004635 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004636 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 return rv;
4638 }
NeilBrown72626682005-09-09 16:23:54 -07004639 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004640 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004641 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4642 /* we can skip this block, and probably more */
4643 sync_blocks /= STRIPE_SECTORS;
4644 *skipped = 1;
4645 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
NeilBrownb47490c2008-02-06 01:39:50 -08004648 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4649
NeilBrowna8c906c2009-06-09 14:39:59 +10004650 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004652 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004654 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004656 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004658 /* Need to check if array will still be degraded after recovery/resync
4659 * We don't need to check the 'failed' flag as when that gets set,
4660 * recovery aborts.
4661 */
NeilBrownf001a702009-06-09 14:30:31 +10004662 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004663 if (conf->disks[i].rdev == NULL)
4664 still_degraded = 1;
4665
4666 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4667
NeilBrown83206d62011-07-26 11:19:49 +10004668 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
NeilBrown14425772009-10-16 15:55:25 +11004670 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 release_stripe(sh);
4672
4673 return STRIPE_SECTORS;
4674}
4675
NeilBrownd1688a62011-10-11 16:49:52 +11004676static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004677{
4678 /* We may not be able to submit a whole bio at once as there
4679 * may not be enough stripe_heads available.
4680 * We cannot pre-allocate enough stripe_heads as we may need
4681 * more than exist in the cache (if we allow ever large chunks).
4682 * So we do one stripe head at a time and record in
4683 * ->bi_hw_segments how many have been done.
4684 *
4685 * We *know* that this entire raid_bio is in one chunk, so
4686 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4687 */
4688 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004689 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004690 sector_t sector, logical_sector, last_sector;
4691 int scnt = 0;
4692 int remaining;
4693 int handled = 0;
4694
4695 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004696 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004697 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004698 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4699
4700 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004701 logical_sector += STRIPE_SECTORS,
4702 sector += STRIPE_SECTORS,
4703 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004704
Shaohua Lie7836bd62012-07-19 16:01:31 +10004705 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004706 /* already done this stripe */
4707 continue;
4708
NeilBrowna8c906c2009-06-09 14:39:59 +10004709 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004710
4711 if (!sh) {
4712 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004713 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004714 conf->retry_read_aligned = raid_bio;
4715 return handled;
4716 }
4717
Neil Brown387bb172007-02-08 14:20:29 -08004718 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4719 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004720 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004721 conf->retry_read_aligned = raid_bio;
4722 return handled;
4723 }
4724
majianpeng3f9e7c12012-07-31 10:04:21 +10004725 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004726 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004727 release_stripe(sh);
4728 handled++;
4729 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004730 remaining = raid5_dec_bi_active_stripes(raid_bio);
Neil Brown0e13fe232008-06-28 08:31:20 +10004731 if (remaining == 0)
4732 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004733 if (atomic_dec_and_test(&conf->active_aligned_reads))
4734 wake_up(&conf->wait_for_stripe);
4735 return handled;
4736}
4737
Shaohua Li46a06402012-08-02 08:33:15 +10004738#define MAX_STRIPE_BATCH 8
4739static int handle_active_stripes(struct r5conf *conf)
4740{
4741 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4742 int i, batch_size = 0;
4743
4744 while (batch_size < MAX_STRIPE_BATCH &&
4745 (sh = __get_priority_stripe(conf)) != NULL)
4746 batch[batch_size++] = sh;
4747
4748 if (batch_size == 0)
4749 return batch_size;
4750 spin_unlock_irq(&conf->device_lock);
4751
4752 for (i = 0; i < batch_size; i++)
4753 handle_stripe(batch[i]);
4754
4755 cond_resched();
4756
4757 spin_lock_irq(&conf->device_lock);
4758 for (i = 0; i < batch_size; i++)
4759 __release_stripe(conf, batch[i]);
4760 return batch_size;
4761}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004762
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763/*
4764 * This is our raid5 kernel thread.
4765 *
4766 * We scan the hash table for stripes which can be handled now.
4767 * During the scan, completed stripes are saved for us by the interrupt
4768 * handler, so that they will not have to wait for our next wakeup.
4769 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004770static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771{
Shaohua Li4ed87312012-10-11 13:34:00 +11004772 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004773 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004775 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
Dan Williams45b42332007-07-09 11:56:43 -07004777 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
4779 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004781 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 handled = 0;
4783 spin_lock_irq(&conf->device_lock);
4784 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004785 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004786 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
NeilBrown0021b7b2012-07-31 09:08:14 +02004788 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004789 !list_empty(&conf->bitmap_list)) {
4790 /* Now is a good time to flush some bitmap updates */
4791 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004792 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004793 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004794 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004795 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004796 activate_bit_delay(conf);
4797 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004798 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004799
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004800 while ((bio = remove_bio_from_retry(conf))) {
4801 int ok;
4802 spin_unlock_irq(&conf->device_lock);
4803 ok = retry_aligned_read(conf, bio);
4804 spin_lock_irq(&conf->device_lock);
4805 if (!ok)
4806 break;
4807 handled++;
4808 }
4809
Shaohua Li46a06402012-08-02 08:33:15 +10004810 batch_size = handle_active_stripes(conf);
4811 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004813 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Shaohua Li46a06402012-08-02 08:33:15 +10004815 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4816 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004817 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004818 spin_lock_irq(&conf->device_lock);
4819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 }
Dan Williams45b42332007-07-09 11:56:43 -07004821 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822
4823 spin_unlock_irq(&conf->device_lock);
4824
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004825 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004826 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
Dan Williams45b42332007-07-09 11:56:43 -07004828 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829}
4830
NeilBrown3f294f42005-11-08 21:39:25 -08004831static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004832raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004833{
NeilBrownd1688a62011-10-11 16:49:52 +11004834 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004835 if (conf)
4836 return sprintf(page, "%d\n", conf->max_nr_stripes);
4837 else
4838 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004839}
4840
NeilBrownc41d4ac2010-06-01 19:37:24 +10004841int
NeilBrownfd01b882011-10-11 16:47:53 +11004842raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004843{
NeilBrownd1688a62011-10-11 16:49:52 +11004844 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004845 int err;
4846
4847 if (size <= 16 || size > 32768)
4848 return -EINVAL;
4849 while (size < conf->max_nr_stripes) {
4850 if (drop_one_stripe(conf))
4851 conf->max_nr_stripes--;
4852 else
4853 break;
4854 }
4855 err = md_allow_write(mddev);
4856 if (err)
4857 return err;
4858 while (size > conf->max_nr_stripes) {
4859 if (grow_one_stripe(conf))
4860 conf->max_nr_stripes++;
4861 else break;
4862 }
4863 return 0;
4864}
4865EXPORT_SYMBOL(raid5_set_cache_size);
4866
NeilBrown3f294f42005-11-08 21:39:25 -08004867static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004868raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004869{
NeilBrownd1688a62011-10-11 16:49:52 +11004870 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004871 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004872 int err;
4873
NeilBrown3f294f42005-11-08 21:39:25 -08004874 if (len >= PAGE_SIZE)
4875 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004876 if (!conf)
4877 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004878
Dan Williams4ef197d82008-04-28 02:15:54 -07004879 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004880 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004881 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004882 if (err)
4883 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004884 return len;
4885}
NeilBrown007583c2005-11-08 21:39:30 -08004886
NeilBrown96de1e62005-11-08 21:39:39 -08004887static struct md_sysfs_entry
4888raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4889 raid5_show_stripe_cache_size,
4890 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004891
4892static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004893raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004894{
NeilBrownd1688a62011-10-11 16:49:52 +11004895 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004896 if (conf)
4897 return sprintf(page, "%d\n", conf->bypass_threshold);
4898 else
4899 return 0;
4900}
4901
4902static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004903raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004904{
NeilBrownd1688a62011-10-11 16:49:52 +11004905 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004906 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004907 if (len >= PAGE_SIZE)
4908 return -EINVAL;
4909 if (!conf)
4910 return -ENODEV;
4911
Dan Williams4ef197d82008-04-28 02:15:54 -07004912 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004913 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004914 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004915 return -EINVAL;
4916 conf->bypass_threshold = new;
4917 return len;
4918}
4919
4920static struct md_sysfs_entry
4921raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4922 S_IRUGO | S_IWUSR,
4923 raid5_show_preread_threshold,
4924 raid5_store_preread_threshold);
4925
4926static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004927stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004928{
NeilBrownd1688a62011-10-11 16:49:52 +11004929 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004930 if (conf)
4931 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4932 else
4933 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004934}
4935
NeilBrown96de1e62005-11-08 21:39:39 -08004936static struct md_sysfs_entry
4937raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004938
NeilBrown007583c2005-11-08 21:39:30 -08004939static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004940 &raid5_stripecache_size.attr,
4941 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004942 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004943 NULL,
4944};
NeilBrown007583c2005-11-08 21:39:30 -08004945static struct attribute_group raid5_attrs_group = {
4946 .name = NULL,
4947 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004948};
4949
Dan Williams80c3a6c2009-03-17 18:10:40 -07004950static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004951raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004952{
NeilBrownd1688a62011-10-11 16:49:52 +11004953 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004954
4955 if (!sectors)
4956 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004957 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004958 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004959 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004960
Andre Noll9d8f0362009-06-18 08:45:01 +10004961 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004962 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004963 return sectors * (raid_disks - conf->max_degraded);
4964}
4965
NeilBrownd1688a62011-10-11 16:49:52 +11004966static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004967{
4968 struct raid5_percpu *percpu;
4969 unsigned long cpu;
4970
4971 if (!conf->percpu)
4972 return;
4973
4974 get_online_cpus();
4975 for_each_possible_cpu(cpu) {
4976 percpu = per_cpu_ptr(conf->percpu, cpu);
4977 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004978 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004979 }
4980#ifdef CONFIG_HOTPLUG_CPU
4981 unregister_cpu_notifier(&conf->cpu_notify);
4982#endif
4983 put_online_cpus();
4984
4985 free_percpu(conf->percpu);
4986}
4987
NeilBrownd1688a62011-10-11 16:49:52 +11004988static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004989{
4990 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004991 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004992 kfree(conf->disks);
4993 kfree(conf->stripe_hashtbl);
4994 kfree(conf);
4995}
4996
Dan Williams36d1c642009-07-14 11:48:22 -07004997#ifdef CONFIG_HOTPLUG_CPU
4998static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4999 void *hcpu)
5000{
NeilBrownd1688a62011-10-11 16:49:52 +11005001 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005002 long cpu = (long)hcpu;
5003 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5004
5005 switch (action) {
5006 case CPU_UP_PREPARE:
5007 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07005008 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07005009 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005010 if (!percpu->scribble)
5011 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5012
5013 if (!percpu->scribble ||
5014 (conf->level == 6 && !percpu->spare_page)) {
5015 safe_put_page(percpu->spare_page);
5016 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005017 pr_err("%s: failed memory allocation for cpu%ld\n",
5018 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005019 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005020 }
5021 break;
5022 case CPU_DEAD:
5023 case CPU_DEAD_FROZEN:
5024 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005025 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005026 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005027 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005028 break;
5029 default:
5030 break;
5031 }
5032 return NOTIFY_OK;
5033}
5034#endif
5035
NeilBrownd1688a62011-10-11 16:49:52 +11005036static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005037{
5038 unsigned long cpu;
5039 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005040 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005041 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005042 int err;
5043
Dan Williams36d1c642009-07-14 11:48:22 -07005044 allcpus = alloc_percpu(struct raid5_percpu);
5045 if (!allcpus)
5046 return -ENOMEM;
5047 conf->percpu = allcpus;
5048
5049 get_online_cpus();
5050 err = 0;
5051 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005052 if (conf->level == 6) {
5053 spare_page = alloc_page(GFP_KERNEL);
5054 if (!spare_page) {
5055 err = -ENOMEM;
5056 break;
5057 }
5058 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5059 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005060 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005061 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005062 err = -ENOMEM;
5063 break;
5064 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005065 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005066 }
5067#ifdef CONFIG_HOTPLUG_CPU
5068 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5069 conf->cpu_notify.priority = 0;
5070 if (err == 0)
5071 err = register_cpu_notifier(&conf->cpu_notify);
5072#endif
5073 put_online_cpus();
5074
5075 return err;
5076}
5077
NeilBrownd1688a62011-10-11 16:49:52 +11005078static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079{
NeilBrownd1688a62011-10-11 16:49:52 +11005080 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005081 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005082 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005084 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085
NeilBrown91adb562009-03-31 14:39:39 +11005086 if (mddev->new_level != 5
5087 && mddev->new_level != 4
5088 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005089 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005090 mdname(mddev), mddev->new_level);
5091 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 }
NeilBrown91adb562009-03-31 14:39:39 +11005093 if ((mddev->new_level == 5
5094 && !algorithm_valid_raid5(mddev->new_layout)) ||
5095 (mddev->new_level == 6
5096 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005097 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005098 mdname(mddev), mddev->new_layout);
5099 return ERR_PTR(-EIO);
5100 }
5101 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005102 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005103 mdname(mddev), mddev->raid_disks);
5104 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106
Andre Noll664e7c42009-06-18 08:45:27 +10005107 if (!mddev->new_chunk_sectors ||
5108 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5109 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005110 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5111 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005112 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005113 }
5114
NeilBrownd1688a62011-10-11 16:49:52 +11005115 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005116 if (conf == NULL)
5117 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005118 spin_lock_init(&conf->device_lock);
5119 init_waitqueue_head(&conf->wait_for_stripe);
5120 init_waitqueue_head(&conf->wait_for_overlap);
5121 INIT_LIST_HEAD(&conf->handle_list);
5122 INIT_LIST_HEAD(&conf->hold_list);
5123 INIT_LIST_HEAD(&conf->delayed_list);
5124 INIT_LIST_HEAD(&conf->bitmap_list);
5125 INIT_LIST_HEAD(&conf->inactive_list);
5126 atomic_set(&conf->active_stripes, 0);
5127 atomic_set(&conf->preread_active_stripes, 0);
5128 atomic_set(&conf->active_aligned_reads, 0);
5129 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005130 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005131
5132 conf->raid_disks = mddev->raid_disks;
5133 if (mddev->reshape_position == MaxSector)
5134 conf->previous_raid_disks = mddev->raid_disks;
5135 else
5136 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005137 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5138 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005139
NeilBrown5e5e3e72009-10-16 16:35:30 +11005140 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005141 GFP_KERNEL);
5142 if (!conf->disks)
5143 goto abort;
5144
5145 conf->mddev = mddev;
5146
5147 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5148 goto abort;
5149
Dan Williams36d1c642009-07-14 11:48:22 -07005150 conf->level = mddev->new_level;
5151 if (raid5_alloc_percpu(conf) != 0)
5152 goto abort;
5153
NeilBrown0c55e022010-05-03 14:09:02 +10005154 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005155
NeilBrowndafb20f2012-03-19 12:46:39 +11005156 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005157 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005158 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005159 || raid_disk < 0)
5160 continue;
5161 disk = conf->disks + raid_disk;
5162
NeilBrown17045f52011-12-23 10:17:53 +11005163 if (test_bit(Replacement, &rdev->flags)) {
5164 if (disk->replacement)
5165 goto abort;
5166 disk->replacement = rdev;
5167 } else {
5168 if (disk->rdev)
5169 goto abort;
5170 disk->rdev = rdev;
5171 }
NeilBrown91adb562009-03-31 14:39:39 +11005172
5173 if (test_bit(In_sync, &rdev->flags)) {
5174 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005175 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5176 " disk %d\n",
5177 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005178 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005179 /* Cannot rely on bitmap to complete recovery */
5180 conf->fullsync = 1;
5181 }
5182
Andre Noll09c9e5f2009-06-18 08:45:55 +10005183 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005184 conf->level = mddev->new_level;
5185 if (conf->level == 6)
5186 conf->max_degraded = 2;
5187 else
5188 conf->max_degraded = 1;
5189 conf->algorithm = mddev->new_layout;
5190 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005191 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005192 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005193 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005194 conf->prev_algo = mddev->layout;
5195 }
NeilBrown91adb562009-03-31 14:39:39 +11005196
5197 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005198 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005199 if (grow_stripes(conf, conf->max_nr_stripes)) {
5200 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005201 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5202 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005203 goto abort;
5204 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005205 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5206 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005207
NeilBrown02326052012-07-03 15:56:52 +10005208 sprintf(pers_name, "raid%d", mddev->new_level);
5209 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005210 if (!conf->thread) {
5211 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005212 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005213 mdname(mddev));
5214 goto abort;
5215 }
5216
5217 return conf;
5218
5219 abort:
5220 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005221 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005222 return ERR_PTR(-EIO);
5223 } else
5224 return ERR_PTR(-ENOMEM);
5225}
5226
NeilBrownc148ffd2009-11-13 17:47:00 +11005227
5228static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5229{
5230 switch (algo) {
5231 case ALGORITHM_PARITY_0:
5232 if (raid_disk < max_degraded)
5233 return 1;
5234 break;
5235 case ALGORITHM_PARITY_N:
5236 if (raid_disk >= raid_disks - max_degraded)
5237 return 1;
5238 break;
5239 case ALGORITHM_PARITY_0_6:
5240 if (raid_disk == 0 ||
5241 raid_disk == raid_disks - 1)
5242 return 1;
5243 break;
5244 case ALGORITHM_LEFT_ASYMMETRIC_6:
5245 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5246 case ALGORITHM_LEFT_SYMMETRIC_6:
5247 case ALGORITHM_RIGHT_SYMMETRIC_6:
5248 if (raid_disk == raid_disks - 1)
5249 return 1;
5250 }
5251 return 0;
5252}
5253
NeilBrownfd01b882011-10-11 16:47:53 +11005254static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005255{
NeilBrownd1688a62011-10-11 16:49:52 +11005256 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005257 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005258 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005259 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005260 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005261 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005262 long long min_offset_diff = 0;
5263 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005264
Andre Noll8c6ac8682009-06-18 08:48:06 +10005265 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005266 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac8682009-06-18 08:48:06 +10005267 " -- starting background reconstruction\n",
5268 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005269
5270 rdev_for_each(rdev, mddev) {
5271 long long diff;
5272 if (rdev->raid_disk < 0)
5273 continue;
5274 diff = (rdev->new_data_offset - rdev->data_offset);
5275 if (first) {
5276 min_offset_diff = diff;
5277 first = 0;
5278 } else if (mddev->reshape_backwards &&
5279 diff < min_offset_diff)
5280 min_offset_diff = diff;
5281 else if (!mddev->reshape_backwards &&
5282 diff > min_offset_diff)
5283 min_offset_diff = diff;
5284 }
5285
NeilBrownf6705572006-03-27 01:18:11 -08005286 if (mddev->reshape_position != MaxSector) {
5287 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005288 * Difficulties arise if the stripe we would write to
5289 * next is at or after the stripe we would read from next.
5290 * For a reshape that changes the number of devices, this
5291 * is only possible for a very short time, and mdadm makes
5292 * sure that time appears to have past before assembling
5293 * the array. So we fail if that time hasn't passed.
5294 * For a reshape that keeps the number of devices the same
5295 * mdadm must be monitoring the reshape can keeping the
5296 * critical areas read-only and backed up. It will start
5297 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005298 */
5299 sector_t here_new, here_old;
5300 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005301 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005302
NeilBrown88ce4932009-03-31 15:24:23 +11005303 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005304 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005305 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005306 mdname(mddev));
5307 return -EINVAL;
5308 }
NeilBrownf6705572006-03-27 01:18:11 -08005309 old_disks = mddev->raid_disks - mddev->delta_disks;
5310 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005311 * further up in new geometry must map after here in old
5312 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005313 */
5314 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005315 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005316 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005317 printk(KERN_ERR "md/raid:%s: reshape_position not "
5318 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005319 return -EINVAL;
5320 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005321 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005322 /* here_new is the stripe we will write to */
5323 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005324 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005325 (old_disks-max_degraded));
5326 /* here_old is the first stripe that we might need to read
5327 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005328 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005329 if ((here_new * mddev->new_chunk_sectors !=
5330 here_old * mddev->chunk_sectors)) {
5331 printk(KERN_ERR "md/raid:%s: reshape position is"
5332 " confused - aborting\n", mdname(mddev));
5333 return -EINVAL;
5334 }
NeilBrown67ac6012009-08-13 10:06:24 +10005335 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005336 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005337 * and taking constant backups.
5338 * mdadm always starts a situation like this in
5339 * readonly mode so it can take control before
5340 * allowing any writes. So just check for that.
5341 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005342 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5343 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5344 /* not really in-place - so OK */;
5345 else if (mddev->ro == 0) {
5346 printk(KERN_ERR "md/raid:%s: in-place reshape "
5347 "must be started in read-only mode "
5348 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005349 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005350 return -EINVAL;
5351 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005352 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005353 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005354 here_old * mddev->chunk_sectors)
5355 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005356 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005357 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005358 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5359 "auto-recovery - aborting.\n",
5360 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005361 return -EINVAL;
5362 }
NeilBrown0c55e022010-05-03 14:09:02 +10005363 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5364 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005365 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005366 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005367 BUG_ON(mddev->level != mddev->new_level);
5368 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005369 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005370 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005371 }
5372
NeilBrown245f46c2009-03-31 14:39:39 +11005373 if (mddev->private == NULL)
5374 conf = setup_conf(mddev);
5375 else
5376 conf = mddev->private;
5377
NeilBrown91adb562009-03-31 14:39:39 +11005378 if (IS_ERR(conf))
5379 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005380
NeilBrownb5254dd2012-05-21 09:27:01 +10005381 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005382 mddev->thread = conf->thread;
5383 conf->thread = NULL;
5384 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
NeilBrown17045f52011-12-23 10:17:53 +11005386 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5387 i++) {
5388 rdev = conf->disks[i].rdev;
5389 if (!rdev && conf->disks[i].replacement) {
5390 /* The replacement is all we have yet */
5391 rdev = conf->disks[i].replacement;
5392 conf->disks[i].replacement = NULL;
5393 clear_bit(Replacement, &rdev->flags);
5394 conf->disks[i].rdev = rdev;
5395 }
5396 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005397 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005398 if (conf->disks[i].replacement &&
5399 conf->reshape_progress != MaxSector) {
5400 /* replacements and reshape simply do not mix. */
5401 printk(KERN_ERR "md: cannot handle concurrent "
5402 "replacement and reshape.\n");
5403 goto abort;
5404 }
NeilBrown2f115882010-06-17 17:41:03 +10005405 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005406 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005407 continue;
5408 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005409 /* This disc is not fully in-sync. However if it
5410 * just stored parity (beyond the recovery_offset),
5411 * when we don't need to be concerned about the
5412 * array being dirty.
5413 * When reshape goes 'backwards', we never have
5414 * partially completed devices, so we only need
5415 * to worry about reshape going forwards.
5416 */
5417 /* Hack because v0.91 doesn't store recovery_offset properly. */
5418 if (mddev->major_version == 0 &&
5419 mddev->minor_version > 90)
5420 rdev->recovery_offset = reshape_offset;
5421
NeilBrownc148ffd2009-11-13 17:47:00 +11005422 if (rdev->recovery_offset < reshape_offset) {
5423 /* We need to check old and new layout */
5424 if (!only_parity(rdev->raid_disk,
5425 conf->algorithm,
5426 conf->raid_disks,
5427 conf->max_degraded))
5428 continue;
5429 }
5430 if (!only_parity(rdev->raid_disk,
5431 conf->prev_algo,
5432 conf->previous_raid_disks,
5433 conf->max_degraded))
5434 continue;
5435 dirty_parity_disks++;
5436 }
NeilBrown91adb562009-03-31 14:39:39 +11005437
NeilBrown17045f52011-12-23 10:17:53 +11005438 /*
5439 * 0 for a fully functional array, 1 or 2 for a degraded array.
5440 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005441 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
NeilBrown674806d2010-06-16 17:17:53 +10005443 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005444 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005446 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 goto abort;
5448 }
5449
NeilBrown91adb562009-03-31 14:39:39 +11005450 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005451 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005452 mddev->resync_max_sectors = mddev->dev_sectors;
5453
NeilBrownc148ffd2009-11-13 17:47:00 +11005454 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005456 if (mddev->ok_start_degraded)
5457 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005458 "md/raid:%s: starting dirty degraded array"
5459 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005460 mdname(mddev));
5461 else {
5462 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005463 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005464 mdname(mddev));
5465 goto abort;
5466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 }
5468
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005470 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5471 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005472 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5473 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 else
NeilBrown0c55e022010-05-03 14:09:02 +10005475 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5476 " out of %d devices, algorithm %d\n",
5477 mdname(mddev), conf->level,
5478 mddev->raid_disks - mddev->degraded,
5479 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
5481 print_raid5_conf(conf);
5482
NeilBrownfef9c612009-03-31 15:16:46 +11005483 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005484 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005485 atomic_set(&conf->reshape_stripes, 0);
5486 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5487 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5488 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5489 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5490 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005491 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005492 }
5493
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494
5495 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005496 if (mddev->to_remove == &raid5_attrs_group)
5497 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005498 else if (mddev->kobj.sd &&
5499 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005500 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005501 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005502 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005503 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5504
5505 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005506 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005507 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005508 /* read-ahead size must cover two whole stripes, which
5509 * is 2 * (datadisks) * chunksize where 'n' is the
5510 * number of raid devices
5511 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5513 int stripe = data_disks *
5514 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5515 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5516 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005517
5518 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005519
5520 mddev->queue->backing_dev_info.congested_data = mddev;
5521 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005522
5523 chunk_size = mddev->chunk_sectors << 9;
5524 blk_queue_io_min(mddev->queue, chunk_size);
5525 blk_queue_io_opt(mddev->queue, chunk_size *
5526 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005527 /*
5528 * We can only discard a whole stripe. It doesn't make sense to
5529 * discard data disk but write parity disk
5530 */
5531 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11005532 /* Round up to power of 2, as discard handling
5533 * currently assumes that */
5534 while ((stripe-1) & stripe)
5535 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11005536 mddev->queue->limits.discard_alignment = stripe;
5537 mddev->queue->limits.discard_granularity = stripe;
5538 /*
5539 * unaligned part of discard request will be ignored, so can't
5540 * guarantee discard_zerors_data
5541 */
5542 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005543
NeilBrown05616be2012-05-21 09:27:00 +10005544 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005545 disk_stack_limits(mddev->gendisk, rdev->bdev,
5546 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005547 disk_stack_limits(mddev->gendisk, rdev->bdev,
5548 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005549 /*
5550 * discard_zeroes_data is required, otherwise data
5551 * could be lost. Consider a scenario: discard a stripe
5552 * (the stripe could be inconsistent if
5553 * discard_zeroes_data is 0); write one disk of the
5554 * stripe (the stripe could be inconsistent again
5555 * depending on which disks are used to calculate
5556 * parity); the disk is broken; The stripe data of this
5557 * disk is lost.
5558 */
5559 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5560 !bdev_get_queue(rdev->bdev)->
5561 limits.discard_zeroes_data)
5562 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10005563 }
Shaohua Li620125f2012-10-11 13:49:05 +11005564
5565 if (discard_supported &&
5566 mddev->queue->limits.max_discard_sectors >= stripe &&
5567 mddev->queue->limits.discard_granularity >= stripe)
5568 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5569 mddev->queue);
5570 else
5571 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5572 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 }
5574
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 return 0;
5576abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005577 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005578 print_raid5_conf(conf);
5579 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005581 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 return -EIO;
5583}
5584
NeilBrownfd01b882011-10-11 16:47:53 +11005585static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586{
NeilBrownd1688a62011-10-11 16:49:52 +11005587 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
NeilBrown01f96c02011-09-21 15:30:20 +10005589 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005590 if (mddev->queue)
5591 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005592 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005593 mddev->private = NULL;
5594 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 return 0;
5596}
5597
NeilBrownfd01b882011-10-11 16:47:53 +11005598static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599{
NeilBrownd1688a62011-10-11 16:49:52 +11005600 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 int i;
5602
Andre Noll9d8f0362009-06-18 08:45:01 +10005603 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5604 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005605 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 for (i = 0; i < conf->raid_disks; i++)
5607 seq_printf (seq, "%s",
5608 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005609 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611}
5612
NeilBrownd1688a62011-10-11 16:49:52 +11005613static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614{
5615 int i;
5616 struct disk_info *tmp;
5617
NeilBrown0c55e022010-05-03 14:09:02 +10005618 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 if (!conf) {
5620 printk("(conf==NULL)\n");
5621 return;
5622 }
NeilBrown0c55e022010-05-03 14:09:02 +10005623 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5624 conf->raid_disks,
5625 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626
5627 for (i = 0; i < conf->raid_disks; i++) {
5628 char b[BDEVNAME_SIZE];
5629 tmp = conf->disks + i;
5630 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005631 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5632 i, !test_bit(Faulty, &tmp->rdev->flags),
5633 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 }
5635}
5636
NeilBrownfd01b882011-10-11 16:47:53 +11005637static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638{
5639 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005640 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005642 int count = 0;
5643 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644
5645 for (i = 0; i < conf->raid_disks; i++) {
5646 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005647 if (tmp->replacement
5648 && tmp->replacement->recovery_offset == MaxSector
5649 && !test_bit(Faulty, &tmp->replacement->flags)
5650 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5651 /* Replacement has just become active. */
5652 if (!tmp->rdev
5653 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5654 count++;
5655 if (tmp->rdev) {
5656 /* Replaced device not technically faulty,
5657 * but we need to be sure it gets removed
5658 * and never re-added.
5659 */
5660 set_bit(Faulty, &tmp->rdev->flags);
5661 sysfs_notify_dirent_safe(
5662 tmp->rdev->sysfs_state);
5663 }
5664 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5665 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005666 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005667 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005668 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005669 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005670 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 }
5672 }
NeilBrown6b965622010-08-18 11:56:59 +10005673 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005674 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005675 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005677 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678}
5679
NeilBrownb8321b62011-12-23 10:17:51 +11005680static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681{
NeilBrownd1688a62011-10-11 16:49:52 +11005682 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005684 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005685 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 struct disk_info *p = conf->disks + number;
5687
5688 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005689 if (rdev == p->rdev)
5690 rdevp = &p->rdev;
5691 else if (rdev == p->replacement)
5692 rdevp = &p->replacement;
5693 else
5694 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005695
NeilBrown657e3e42011-12-23 10:17:52 +11005696 if (number >= conf->raid_disks &&
5697 conf->reshape_progress == MaxSector)
5698 clear_bit(In_sync, &rdev->flags);
5699
5700 if (test_bit(In_sync, &rdev->flags) ||
5701 atomic_read(&rdev->nr_pending)) {
5702 err = -EBUSY;
5703 goto abort;
5704 }
5705 /* Only remove non-faulty devices if recovery
5706 * isn't possible.
5707 */
5708 if (!test_bit(Faulty, &rdev->flags) &&
5709 mddev->recovery_disabled != conf->recovery_disabled &&
5710 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005711 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005712 number < conf->raid_disks) {
5713 err = -EBUSY;
5714 goto abort;
5715 }
5716 *rdevp = NULL;
5717 synchronize_rcu();
5718 if (atomic_read(&rdev->nr_pending)) {
5719 /* lost the race, try later */
5720 err = -EBUSY;
5721 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005722 } else if (p->replacement) {
5723 /* We must have just cleared 'rdev' */
5724 p->rdev = p->replacement;
5725 clear_bit(Replacement, &p->replacement->flags);
5726 smp_mb(); /* Make sure other CPUs may see both as identical
5727 * but will never see neither - if they are careful
5728 */
5729 p->replacement = NULL;
5730 clear_bit(WantReplacement, &rdev->flags);
5731 } else
5732 /* We might have just removed the Replacement as faulty-
5733 * clear the bit just in case
5734 */
5735 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736abort:
5737
5738 print_raid5_conf(conf);
5739 return err;
5740}
5741
NeilBrownfd01b882011-10-11 16:47:53 +11005742static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743{
NeilBrownd1688a62011-10-11 16:49:52 +11005744 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005745 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 int disk;
5747 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005748 int first = 0;
5749 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750
NeilBrown7f0da592011-07-28 11:39:22 +10005751 if (mddev->recovery_disabled == conf->recovery_disabled)
5752 return -EBUSY;
5753
NeilBrowndc10c642012-03-19 12:46:37 +11005754 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005756 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757
Neil Brown6c2fce22008-06-28 08:31:31 +10005758 if (rdev->raid_disk >= 0)
5759 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760
5761 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005762 * find the disk ... but prefer rdev->saved_raid_disk
5763 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005765 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005766 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005767 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005768 first = rdev->saved_raid_disk;
5769
5770 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005771 p = conf->disks + disk;
5772 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005773 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005775 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005776 if (rdev->saved_raid_disk != disk)
5777 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005778 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005779 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005781 }
5782 for (disk = first; disk <= last; disk++) {
5783 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005784 if (test_bit(WantReplacement, &p->rdev->flags) &&
5785 p->replacement == NULL) {
5786 clear_bit(In_sync, &rdev->flags);
5787 set_bit(Replacement, &rdev->flags);
5788 rdev->raid_disk = disk;
5789 err = 0;
5790 conf->fullsync = 1;
5791 rcu_assign_pointer(p->replacement, rdev);
5792 break;
5793 }
5794 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005795out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005797 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798}
5799
NeilBrownfd01b882011-10-11 16:47:53 +11005800static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801{
5802 /* no resync is happening, and there is enough space
5803 * on all devices, so we can resize.
5804 * We need to make sure resync covers any new space.
5805 * If the array is shrinking we should possibly wait until
5806 * any io in the removed space completes, but it hardly seems
5807 * worth it.
5808 */
NeilBrowna4a61252012-05-22 13:55:27 +10005809 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005810 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005811 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5812 if (mddev->external_size &&
5813 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005814 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005815 if (mddev->bitmap) {
5816 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5817 if (ret)
5818 return ret;
5819 }
5820 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005821 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005822 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005823 if (sectors > mddev->dev_sectors &&
5824 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005825 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5827 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005828 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005829 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 return 0;
5831}
5832
NeilBrownfd01b882011-10-11 16:47:53 +11005833static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005834{
5835 /* Can only proceed if there are plenty of stripe_heads.
5836 * We need a minimum of one full stripe,, and for sensible progress
5837 * it is best to have about 4 times that.
5838 * If we require 4 times, then the default 256 4K stripe_heads will
5839 * allow for chunk sizes up to 256K, which is probably OK.
5840 * If the chunk size is greater, user-space should request more
5841 * stripe_heads first.
5842 */
NeilBrownd1688a62011-10-11 16:49:52 +11005843 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005844 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5845 > conf->max_nr_stripes ||
5846 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5847 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005848 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5849 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005850 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5851 / STRIPE_SIZE)*4);
5852 return 0;
5853 }
5854 return 1;
5855}
5856
NeilBrownfd01b882011-10-11 16:47:53 +11005857static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005858{
NeilBrownd1688a62011-10-11 16:49:52 +11005859 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005860
NeilBrown88ce4932009-03-31 15:24:23 +11005861 if (mddev->delta_disks == 0 &&
5862 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005863 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005864 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005865 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005866 return -EINVAL;
5867 if (mddev->delta_disks < 0) {
5868 /* We might be able to shrink, but the devices must
5869 * be made bigger first.
5870 * For raid6, 4 is the minimum size.
5871 * Otherwise 2 is the minimum
5872 */
5873 int min = 2;
5874 if (mddev->level == 6)
5875 min = 4;
5876 if (mddev->raid_disks + mddev->delta_disks < min)
5877 return -EINVAL;
5878 }
NeilBrown29269552006-03-27 01:18:10 -08005879
NeilBrown01ee22b2009-06-18 08:47:20 +10005880 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005881 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005882
NeilBrowne56108d62012-10-11 14:24:13 +11005883 return resize_stripes(conf, (conf->previous_raid_disks
5884 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08005885}
5886
NeilBrownfd01b882011-10-11 16:47:53 +11005887static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005888{
NeilBrownd1688a62011-10-11 16:49:52 +11005889 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005890 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005891 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005892 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005893
NeilBrownf4168852007-02-28 20:11:53 -08005894 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005895 return -EBUSY;
5896
NeilBrown01ee22b2009-06-18 08:47:20 +10005897 if (!check_stripe_cache(mddev))
5898 return -ENOSPC;
5899
NeilBrown30b67642012-05-22 13:55:28 +10005900 if (has_failed(conf))
5901 return -EINVAL;
5902
NeilBrownc6563a82012-05-21 09:27:00 +10005903 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005904 if (!test_bit(In_sync, &rdev->flags)
5905 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005906 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005907 }
NeilBrown63c70c42006-03-27 01:18:13 -08005908
NeilBrownf4168852007-02-28 20:11:53 -08005909 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005910 /* Not enough devices even to make a degraded array
5911 * of that size
5912 */
5913 return -EINVAL;
5914
NeilBrownec32a2b2009-03-31 15:17:38 +11005915 /* Refuse to reduce size of the array. Any reductions in
5916 * array size must be through explicit setting of array_size
5917 * attribute.
5918 */
5919 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5920 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005921 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005922 "before number of disks\n", mdname(mddev));
5923 return -EINVAL;
5924 }
5925
NeilBrownf6705572006-03-27 01:18:11 -08005926 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005927 spin_lock_irq(&conf->device_lock);
5928 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005929 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005930 conf->prev_chunk_sectors = conf->chunk_sectors;
5931 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005932 conf->prev_algo = conf->algorithm;
5933 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005934 conf->generation++;
5935 /* Code that selects data_offset needs to see the generation update
5936 * if reshape_progress has been set - so a memory barrier needed.
5937 */
5938 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005939 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005940 conf->reshape_progress = raid5_size(mddev, 0, 0);
5941 else
5942 conf->reshape_progress = 0;
5943 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005944 spin_unlock_irq(&conf->device_lock);
5945
5946 /* Add some new drives, as many as will fit.
5947 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005948 * Don't add devices if we are reducing the number of
5949 * devices in the array. This is because it is not possible
5950 * to correctly record the "partially reconstructed" state of
5951 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005952 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005953 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005954 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005955 if (rdev->raid_disk < 0 &&
5956 !test_bit(Faulty, &rdev->flags)) {
5957 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005958 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005959 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005960 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005961 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005962 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005963
5964 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005965 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005966 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005967 } else if (rdev->raid_disk >= conf->previous_raid_disks
5968 && !test_bit(Faulty, &rdev->flags)) {
5969 /* This is a spare that was manually added */
5970 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005971 }
NeilBrown29269552006-03-27 01:18:10 -08005972
NeilBrown87a8dec2011-01-31 11:57:43 +11005973 /* When a reshape changes the number of devices,
5974 * ->degraded is measured against the larger of the
5975 * pre and post number of devices.
5976 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005977 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005978 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005979 spin_unlock_irqrestore(&conf->device_lock, flags);
5980 }
NeilBrown63c70c42006-03-27 01:18:13 -08005981 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005982 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b422006-10-03 01:15:46 -07005983 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005984
NeilBrown29269552006-03-27 01:18:10 -08005985 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5986 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5987 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5988 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5989 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005990 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005991 if (!mddev->sync_thread) {
5992 mddev->recovery = 0;
5993 spin_lock_irq(&conf->device_lock);
5994 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005995 rdev_for_each(rdev, mddev)
5996 rdev->new_data_offset = rdev->data_offset;
5997 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005998 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005999 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08006000 spin_unlock_irq(&conf->device_lock);
6001 return -EAGAIN;
6002 }
NeilBrownc8f517c2009-03-31 15:28:40 +11006003 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08006004 md_wakeup_thread(mddev->sync_thread);
6005 md_new_event(mddev);
6006 return 0;
6007}
NeilBrown29269552006-03-27 01:18:10 -08006008
NeilBrownec32a2b2009-03-31 15:17:38 +11006009/* This is called from the reshape thread and should make any
6010 * changes needed in 'conf'
6011 */
NeilBrownd1688a62011-10-11 16:49:52 +11006012static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006013{
NeilBrown29269552006-03-27 01:18:10 -08006014
NeilBrownf6705572006-03-27 01:18:11 -08006015 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006016 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006017
NeilBrownf6705572006-03-27 01:18:11 -08006018 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006019 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006020 rdev_for_each(rdev, conf->mddev)
6021 rdev->data_offset = rdev->new_data_offset;
6022 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006023 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006024 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006025 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006026
6027 /* read-ahead size must cover two whole stripes, which is
6028 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6029 */
NeilBrown4a5add42010-06-01 19:37:28 +10006030 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006031 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006032 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006033 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006034 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6035 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6036 }
NeilBrown29269552006-03-27 01:18:10 -08006037 }
NeilBrown29269552006-03-27 01:18:10 -08006038}
6039
NeilBrownec32a2b2009-03-31 15:17:38 +11006040/* This is called from the raid5d thread with mddev_lock held.
6041 * It makes config changes to the device.
6042 */
NeilBrownfd01b882011-10-11 16:47:53 +11006043static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006044{
NeilBrownd1688a62011-10-11 16:49:52 +11006045 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006046
6047 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6048
NeilBrownec32a2b2009-03-31 15:17:38 +11006049 if (mddev->delta_disks > 0) {
6050 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6051 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006052 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006053 } else {
6054 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006055 spin_lock_irq(&conf->device_lock);
6056 mddev->degraded = calc_degraded(conf);
6057 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006058 for (d = conf->raid_disks ;
6059 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006060 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006061 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006062 if (rdev)
6063 clear_bit(In_sync, &rdev->flags);
6064 rdev = conf->disks[d].replacement;
6065 if (rdev)
6066 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006067 }
NeilBrowncea9c222009-03-31 15:15:05 +11006068 }
NeilBrown88ce4932009-03-31 15:24:23 +11006069 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006070 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006071 mddev->reshape_position = MaxSector;
6072 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006073 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006074 }
6075}
6076
NeilBrownfd01b882011-10-11 16:47:53 +11006077static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006078{
NeilBrownd1688a62011-10-11 16:49:52 +11006079 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006080
6081 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006082 case 2: /* resume for a suspend */
6083 wake_up(&conf->wait_for_overlap);
6084 break;
6085
NeilBrown72626682005-09-09 16:23:54 -07006086 case 1: /* stop all writes */
6087 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006088 /* '2' tells resync/reshape to pause so that all
6089 * active stripes can drain
6090 */
6091 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006092 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006093 atomic_read(&conf->active_stripes) == 0 &&
6094 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07006095 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10006096 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006097 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006098 /* allow reshape to continue */
6099 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006100 break;
6101
6102 case 0: /* re-enable writes */
6103 spin_lock_irq(&conf->device_lock);
6104 conf->quiesce = 0;
6105 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006106 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006107 spin_unlock_irq(&conf->device_lock);
6108 break;
6109 }
NeilBrown72626682005-09-09 16:23:54 -07006110}
NeilBrownb15c2e52006-01-06 00:20:16 -08006111
NeilBrownd562b0c2009-03-31 14:39:39 +11006112
NeilBrownfd01b882011-10-11 16:47:53 +11006113static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006114{
NeilBrowne373ab12011-10-11 16:48:59 +11006115 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006116 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006117
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006118 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006119 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006120 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6121 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006122 return ERR_PTR(-EINVAL);
6123 }
6124
NeilBrowne373ab12011-10-11 16:48:59 +11006125 sectors = raid0_conf->strip_zone[0].zone_end;
6126 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006127 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006128 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006129 mddev->new_layout = ALGORITHM_PARITY_N;
6130 mddev->new_chunk_sectors = mddev->chunk_sectors;
6131 mddev->raid_disks += 1;
6132 mddev->delta_disks = 1;
6133 /* make sure it will be not marked as dirty */
6134 mddev->recovery_cp = MaxSector;
6135
6136 return setup_conf(mddev);
6137}
6138
6139
NeilBrownfd01b882011-10-11 16:47:53 +11006140static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006141{
6142 int chunksect;
6143
6144 if (mddev->raid_disks != 2 ||
6145 mddev->degraded > 1)
6146 return ERR_PTR(-EINVAL);
6147
6148 /* Should check if there are write-behind devices? */
6149
6150 chunksect = 64*2; /* 64K by default */
6151
6152 /* The array must be an exact multiple of chunksize */
6153 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6154 chunksect >>= 1;
6155
6156 if ((chunksect<<9) < STRIPE_SIZE)
6157 /* array size does not allow a suitable chunk size */
6158 return ERR_PTR(-EINVAL);
6159
6160 mddev->new_level = 5;
6161 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006162 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006163
6164 return setup_conf(mddev);
6165}
6166
NeilBrownfd01b882011-10-11 16:47:53 +11006167static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006168{
6169 int new_layout;
6170
6171 switch (mddev->layout) {
6172 case ALGORITHM_LEFT_ASYMMETRIC_6:
6173 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6174 break;
6175 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6176 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6177 break;
6178 case ALGORITHM_LEFT_SYMMETRIC_6:
6179 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6180 break;
6181 case ALGORITHM_RIGHT_SYMMETRIC_6:
6182 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6183 break;
6184 case ALGORITHM_PARITY_0_6:
6185 new_layout = ALGORITHM_PARITY_0;
6186 break;
6187 case ALGORITHM_PARITY_N:
6188 new_layout = ALGORITHM_PARITY_N;
6189 break;
6190 default:
6191 return ERR_PTR(-EINVAL);
6192 }
6193 mddev->new_level = 5;
6194 mddev->new_layout = new_layout;
6195 mddev->delta_disks = -1;
6196 mddev->raid_disks -= 1;
6197 return setup_conf(mddev);
6198}
6199
NeilBrownd562b0c2009-03-31 14:39:39 +11006200
NeilBrownfd01b882011-10-11 16:47:53 +11006201static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006202{
NeilBrown88ce4932009-03-31 15:24:23 +11006203 /* For a 2-drive array, the layout and chunk size can be changed
6204 * immediately as not restriping is needed.
6205 * For larger arrays we record the new value - after validation
6206 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006207 */
NeilBrownd1688a62011-10-11 16:49:52 +11006208 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006209 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006210
NeilBrown597a7112009-06-18 08:47:42 +10006211 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006212 return -EINVAL;
6213 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006214 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006215 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006216 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006217 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006218 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006219 /* not factor of array size */
6220 return -EINVAL;
6221 }
6222
6223 /* They look valid */
6224
NeilBrown88ce4932009-03-31 15:24:23 +11006225 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006226 /* can make the change immediately */
6227 if (mddev->new_layout >= 0) {
6228 conf->algorithm = mddev->new_layout;
6229 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006230 }
6231 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006232 conf->chunk_sectors = new_chunk ;
6233 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006234 }
6235 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6236 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006237 }
NeilBrown50ac1682009-06-18 08:47:55 +10006238 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006239}
6240
NeilBrownfd01b882011-10-11 16:47:53 +11006241static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006242{
NeilBrown597a7112009-06-18 08:47:42 +10006243 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006244
NeilBrown597a7112009-06-18 08:47:42 +10006245 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006246 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006247 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006248 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006249 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006250 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006251 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006252 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006253 /* not factor of array size */
6254 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006255 }
NeilBrown88ce4932009-03-31 15:24:23 +11006256
6257 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006258 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006259}
6260
NeilBrownfd01b882011-10-11 16:47:53 +11006261static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006262{
6263 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006264 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006265 * raid1 - if there are two drives. We need to know the chunk size
6266 * raid4 - trivial - just use a raid4 layout.
6267 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006268 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006269 if (mddev->level == 0)
6270 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006271 if (mddev->level == 1)
6272 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006273 if (mddev->level == 4) {
6274 mddev->new_layout = ALGORITHM_PARITY_N;
6275 mddev->new_level = 5;
6276 return setup_conf(mddev);
6277 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006278 if (mddev->level == 6)
6279 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006280
6281 return ERR_PTR(-EINVAL);
6282}
6283
NeilBrownfd01b882011-10-11 16:47:53 +11006284static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006285{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006286 /* raid4 can take over:
6287 * raid0 - if there is only one strip zone
6288 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006289 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006290 if (mddev->level == 0)
6291 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006292 if (mddev->level == 5 &&
6293 mddev->layout == ALGORITHM_PARITY_N) {
6294 mddev->new_layout = 0;
6295 mddev->new_level = 4;
6296 return setup_conf(mddev);
6297 }
6298 return ERR_PTR(-EINVAL);
6299}
NeilBrownd562b0c2009-03-31 14:39:39 +11006300
NeilBrown84fc4b52011-10-11 16:49:58 +11006301static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006302
NeilBrownfd01b882011-10-11 16:47:53 +11006303static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006304{
6305 /* Currently can only take over a raid5. We map the
6306 * personality to an equivalent raid6 personality
6307 * with the Q block at the end.
6308 */
6309 int new_layout;
6310
6311 if (mddev->pers != &raid5_personality)
6312 return ERR_PTR(-EINVAL);
6313 if (mddev->degraded > 1)
6314 return ERR_PTR(-EINVAL);
6315 if (mddev->raid_disks > 253)
6316 return ERR_PTR(-EINVAL);
6317 if (mddev->raid_disks < 3)
6318 return ERR_PTR(-EINVAL);
6319
6320 switch (mddev->layout) {
6321 case ALGORITHM_LEFT_ASYMMETRIC:
6322 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6323 break;
6324 case ALGORITHM_RIGHT_ASYMMETRIC:
6325 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6326 break;
6327 case ALGORITHM_LEFT_SYMMETRIC:
6328 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6329 break;
6330 case ALGORITHM_RIGHT_SYMMETRIC:
6331 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6332 break;
6333 case ALGORITHM_PARITY_0:
6334 new_layout = ALGORITHM_PARITY_0_6;
6335 break;
6336 case ALGORITHM_PARITY_N:
6337 new_layout = ALGORITHM_PARITY_N;
6338 break;
6339 default:
6340 return ERR_PTR(-EINVAL);
6341 }
6342 mddev->new_level = 6;
6343 mddev->new_layout = new_layout;
6344 mddev->delta_disks = 1;
6345 mddev->raid_disks += 1;
6346 return setup_conf(mddev);
6347}
6348
6349
NeilBrown84fc4b52011-10-11 16:49:58 +11006350static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006351{
6352 .name = "raid6",
6353 .level = 6,
6354 .owner = THIS_MODULE,
6355 .make_request = make_request,
6356 .run = run,
6357 .stop = stop,
6358 .status = status,
6359 .error_handler = error,
6360 .hot_add_disk = raid5_add_disk,
6361 .hot_remove_disk= raid5_remove_disk,
6362 .spare_active = raid5_spare_active,
6363 .sync_request = sync_request,
6364 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006365 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006366 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006367 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006368 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006369 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006370 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006371};
NeilBrown84fc4b52011-10-11 16:49:58 +11006372static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373{
6374 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006375 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 .owner = THIS_MODULE,
6377 .make_request = make_request,
6378 .run = run,
6379 .stop = stop,
6380 .status = status,
6381 .error_handler = error,
6382 .hot_add_disk = raid5_add_disk,
6383 .hot_remove_disk= raid5_remove_disk,
6384 .spare_active = raid5_spare_active,
6385 .sync_request = sync_request,
6386 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006387 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006388 .check_reshape = raid5_check_reshape,
6389 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006390 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006391 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006392 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393};
6394
NeilBrown84fc4b52011-10-11 16:49:58 +11006395static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396{
NeilBrown2604b702006-01-06 00:20:36 -08006397 .name = "raid4",
6398 .level = 4,
6399 .owner = THIS_MODULE,
6400 .make_request = make_request,
6401 .run = run,
6402 .stop = stop,
6403 .status = status,
6404 .error_handler = error,
6405 .hot_add_disk = raid5_add_disk,
6406 .hot_remove_disk= raid5_remove_disk,
6407 .spare_active = raid5_spare_active,
6408 .sync_request = sync_request,
6409 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006410 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006411 .check_reshape = raid5_check_reshape,
6412 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006413 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006414 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006415 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006416};
6417
6418static int __init raid5_init(void)
6419{
NeilBrown16a53ec2006-06-26 00:27:38 -07006420 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006421 register_md_personality(&raid5_personality);
6422 register_md_personality(&raid4_personality);
6423 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424}
6425
NeilBrown2604b702006-01-06 00:20:36 -08006426static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427{
NeilBrown16a53ec2006-06-26 00:27:38 -07006428 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006429 unregister_md_personality(&raid5_personality);
6430 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431}
6432
6433module_init(raid5_init);
6434module_exit(raid5_exit);
6435MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006436MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006438MODULE_ALIAS("md-raid5");
6439MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006440MODULE_ALIAS("md-level-5");
6441MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006442MODULE_ALIAS("md-personality-8"); /* RAID6 */
6443MODULE_ALIAS("md-raid6");
6444MODULE_ALIAS("md-level-6");
6445
6446/* This used to be two separate modules, they were: */
6447MODULE_ALIAS("raid5");
6448MODULE_ALIAS("raid6");