blob: 81c02d63440b647e9fc6145206f0d53b6efc90d9 [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);
NeilBrown674806d2010-06-16 17:17:53 +1000396 if (!rdev || test_bit(Faulty, &rdev->flags))
397 degraded++;
398 else if (test_bit(In_sync, &rdev->flags))
399 ;
400 else
401 /* not in-sync or faulty.
402 * If the reshape increases the number of devices,
403 * this is being recovered by the reshape, so
404 * this 'previous' section is not in_sync.
405 * If the number of devices is being reduced however,
406 * the device can only be part of the array if
407 * we are reverting a reshape, so this section will
408 * be in-sync.
409 */
410 if (conf->raid_disks >= conf->previous_raid_disks)
411 degraded++;
412 }
413 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100414 if (conf->raid_disks == conf->previous_raid_disks)
415 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000416 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100417 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000418 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100419 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000420 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100421 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000422 else if (test_bit(In_sync, &rdev->flags))
423 ;
424 else
425 /* not in-sync or faulty.
426 * If reshape increases the number of devices, this
427 * section has already been recovered, else it
428 * almost certainly hasn't.
429 */
430 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100431 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000432 }
433 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100434 if (degraded2 > degraded)
435 return degraded2;
436 return degraded;
437}
438
439static int has_failed(struct r5conf *conf)
440{
441 int degraded;
442
443 if (conf->mddev->reshape_position == MaxSector)
444 return conf->mddev->degraded > conf->max_degraded;
445
446 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000447 if (degraded > conf->max_degraded)
448 return 1;
449 return 0;
450}
451
NeilBrownb5663ba2009-03-31 14:39:38 +1100452static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100453get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000454 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 struct stripe_head *sh;
457
Dan Williams45b42332007-07-09 11:56:43 -0700458 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 spin_lock_irq(&conf->device_lock);
461
462 do {
NeilBrown72626682005-09-09 16:23:54 -0700463 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000464 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700465 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100466 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (!sh) {
468 if (!conf->inactive_blocked)
469 sh = get_free_stripe(conf);
470 if (noblock && sh == NULL)
471 break;
472 if (!sh) {
473 conf->inactive_blocked = 1;
474 wait_event_lock_irq(conf->wait_for_stripe,
475 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800476 (atomic_read(&conf->active_stripes)
477 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 || !conf->inactive_blocked),
479 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000480 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 conf->inactive_blocked = 0;
482 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100483 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 } else {
485 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100486 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000487 && !test_bit(STRIPE_EXPANDING, &sh->state)
488 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 } else {
490 if (!test_bit(STRIPE_HANDLE, &sh->state))
491 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700492 if (list_empty(&sh->lru) &&
493 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700494 BUG();
495 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 }
498 } while (sh == NULL);
499
500 if (sh)
501 atomic_inc(&sh->count);
502
503 spin_unlock_irq(&conf->device_lock);
504 return sh;
505}
506
NeilBrown05616be2012-05-21 09:27:00 +1000507/* Determine if 'data_offset' or 'new_data_offset' should be used
508 * in this stripe_head.
509 */
510static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
511{
512 sector_t progress = conf->reshape_progress;
513 /* Need a memory barrier to make sure we see the value
514 * of conf->generation, or ->data_offset that was set before
515 * reshape_progress was updated.
516 */
517 smp_rmb();
518 if (progress == MaxSector)
519 return 0;
520 if (sh->generation == conf->generation - 1)
521 return 0;
522 /* We are in a reshape, and this is a new-generation stripe,
523 * so use new_data_offset.
524 */
525 return 1;
526}
527
NeilBrown6712ecf2007-09-27 12:47:43 +0200528static void
529raid5_end_read_request(struct bio *bi, int error);
530static void
531raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700532
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000533static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700534{
NeilBrownd1688a62011-10-11 16:49:52 +1100535 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700536 int i, disks = sh->disks;
537
538 might_sleep();
539
540 for (i = disks; i--; ) {
541 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100542 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100543 struct bio *bi, *rbi;
544 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200545 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
546 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
547 rw = WRITE_FUA;
548 else
549 rw = WRITE;
550 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700551 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100552 else if (test_and_clear_bit(R5_WantReplace,
553 &sh->dev[i].flags)) {
554 rw = WRITE;
555 replace_only = 1;
556 } else
Dan Williams91c00922007-01-02 13:52:30 -0700557 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000558 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
559 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700560
561 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100562 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700563
564 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100565 rbi->bi_rw = rw;
566 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700567 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100568 rbi->bi_end_io = raid5_end_write_request;
569 } else
Dan Williams91c00922007-01-02 13:52:30 -0700570 bi->bi_end_io = raid5_end_read_request;
571
572 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100573 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100574 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
575 rdev = rcu_dereference(conf->disks[i].rdev);
576 if (!rdev) {
577 rdev = rrdev;
578 rrdev = NULL;
579 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100580 if (rw & WRITE) {
581 if (replace_only)
582 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100583 if (rdev == rrdev)
584 /* We raced and saw duplicates */
585 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100586 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100587 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100588 rdev = rrdev;
589 rrdev = NULL;
590 }
NeilBrown977df362011-12-23 10:17:53 +1100591
Dan Williams91c00922007-01-02 13:52:30 -0700592 if (rdev && test_bit(Faulty, &rdev->flags))
593 rdev = NULL;
594 if (rdev)
595 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100596 if (rrdev && test_bit(Faulty, &rrdev->flags))
597 rrdev = NULL;
598 if (rrdev)
599 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700600 rcu_read_unlock();
601
NeilBrown73e92e52011-07-28 11:39:22 +1000602 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100603 * need to check for writes. We never accept write errors
604 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000605 */
606 while ((rw & WRITE) && rdev &&
607 test_bit(WriteErrorSeen, &rdev->flags)) {
608 sector_t first_bad;
609 int bad_sectors;
610 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
611 &first_bad, &bad_sectors);
612 if (!bad)
613 break;
614
615 if (bad < 0) {
616 set_bit(BlockedBadBlocks, &rdev->flags);
617 if (!conf->mddev->external &&
618 conf->mddev->flags) {
619 /* It is very unlikely, but we might
620 * still need to write out the
621 * bad block log - better give it
622 * a chance*/
623 md_check_recovery(conf->mddev);
624 }
majianpeng18507532012-07-03 12:11:54 +1000625 /*
626 * Because md_wait_for_blocked_rdev
627 * will dec nr_pending, we must
628 * increment it first.
629 */
630 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000631 md_wait_for_blocked_rdev(rdev, conf->mddev);
632 } else {
633 /* Acknowledged bad block - skip the write */
634 rdev_dec_pending(rdev, conf->mddev);
635 rdev = NULL;
636 }
637 }
638
Dan Williams91c00922007-01-02 13:52:30 -0700639 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100640 if (s->syncing || s->expanding || s->expanded
641 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700642 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
643
Dan Williams2b7497f2008-06-28 08:31:52 +1000644 set_bit(STRIPE_IO_STARTED, &sh->state);
645
Dan Williams91c00922007-01-02 13:52:30 -0700646 bi->bi_bdev = rdev->bdev;
647 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700648 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700649 bi->bi_rw, i);
650 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000651 if (use_new_offset(conf, sh))
652 bi->bi_sector = (sh->sector
653 + rdev->new_data_offset);
654 else
655 bi->bi_sector = (sh->sector
656 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000657 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
658 bi->bi_rw |= REQ_FLUSH;
659
Dan Williams91c00922007-01-02 13:52:30 -0700660 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700661 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700662 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
663 bi->bi_io_vec[0].bv_offset = 0;
664 bi->bi_size = STRIPE_SIZE;
665 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100666 if (rrdev)
667 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700668 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100669 }
670 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100671 if (s->syncing || s->expanding || s->expanded
672 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100673 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
674
675 set_bit(STRIPE_IO_STARTED, &sh->state);
676
677 rbi->bi_bdev = rrdev->bdev;
678 pr_debug("%s: for %llu schedule op %ld on "
679 "replacement disc %d\n",
680 __func__, (unsigned long long)sh->sector,
681 rbi->bi_rw, i);
682 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000683 if (use_new_offset(conf, sh))
684 rbi->bi_sector = (sh->sector
685 + rrdev->new_data_offset);
686 else
687 rbi->bi_sector = (sh->sector
688 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100689 rbi->bi_flags = 1 << BIO_UPTODATE;
690 rbi->bi_idx = 0;
691 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
692 rbi->bi_io_vec[0].bv_offset = 0;
693 rbi->bi_size = STRIPE_SIZE;
694 rbi->bi_next = NULL;
695 generic_make_request(rbi);
696 }
697 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000698 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700699 set_bit(STRIPE_DEGRADED, &sh->state);
700 pr_debug("skip op %ld on disc %d for sector %llu\n",
701 bi->bi_rw, i, (unsigned long long)sh->sector);
702 clear_bit(R5_LOCKED, &sh->dev[i].flags);
703 set_bit(STRIPE_HANDLE, &sh->state);
704 }
705 }
706}
707
708static struct dma_async_tx_descriptor *
709async_copy_data(int frombio, struct bio *bio, struct page *page,
710 sector_t sector, struct dma_async_tx_descriptor *tx)
711{
712 struct bio_vec *bvl;
713 struct page *bio_page;
714 int i;
715 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700716 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700717 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700718
719 if (bio->bi_sector >= sector)
720 page_offset = (signed)(bio->bi_sector - sector) * 512;
721 else
722 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700723
Dan Williams0403e382009-09-08 17:42:50 -0700724 if (frombio)
725 flags |= ASYNC_TX_FENCE;
726 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
727
Dan Williams91c00922007-01-02 13:52:30 -0700728 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000729 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700730 int clen;
731 int b_offset = 0;
732
733 if (page_offset < 0) {
734 b_offset = -page_offset;
735 page_offset += b_offset;
736 len -= b_offset;
737 }
738
739 if (len > 0 && page_offset + len > STRIPE_SIZE)
740 clen = STRIPE_SIZE - page_offset;
741 else
742 clen = len;
743
744 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000745 b_offset += bvl->bv_offset;
746 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700747 if (frombio)
748 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700749 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700750 else
751 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700752 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700753 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700754 /* chain the operations */
755 submit.depend_tx = tx;
756
Dan Williams91c00922007-01-02 13:52:30 -0700757 if (clen < len) /* hit end of page */
758 break;
759 page_offset += len;
760 }
761
762 return tx;
763}
764
765static void ops_complete_biofill(void *stripe_head_ref)
766{
767 struct stripe_head *sh = stripe_head_ref;
768 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700769 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700770
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700771 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700772 (unsigned long long)sh->sector);
773
774 /* clear completed biofills */
775 for (i = sh->disks; i--; ) {
776 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700777
778 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700779 /* and check if we need to reply to a read request,
780 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000781 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700782 */
783 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700784 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700785
Dan Williams91c00922007-01-02 13:52:30 -0700786 BUG_ON(!dev->read);
787 rbi = dev->read;
788 dev->read = NULL;
789 while (rbi && rbi->bi_sector <
790 dev->sector + STRIPE_SECTORS) {
791 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000792 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700793 rbi->bi_next = return_bi;
794 return_bi = rbi;
795 }
Dan Williams91c00922007-01-02 13:52:30 -0700796 rbi = rbi2;
797 }
798 }
799 }
Dan Williams83de75c2008-06-28 08:31:58 +1000800 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700801
802 return_io(return_bi);
803
Dan Williamse4d84902007-09-24 10:06:13 -0700804 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700805 release_stripe(sh);
806}
807
808static void ops_run_biofill(struct stripe_head *sh)
809{
810 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700811 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700812 int i;
813
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700814 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700815 (unsigned long long)sh->sector);
816
817 for (i = sh->disks; i--; ) {
818 struct r5dev *dev = &sh->dev[i];
819 if (test_bit(R5_Wantfill, &dev->flags)) {
820 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000821 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700822 dev->read = rbi = dev->toread;
823 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000824 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700825 while (rbi && rbi->bi_sector <
826 dev->sector + STRIPE_SECTORS) {
827 tx = async_copy_data(0, rbi, dev->page,
828 dev->sector, tx);
829 rbi = r5_next_bio(rbi, dev->sector);
830 }
831 }
832 }
833
834 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700835 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
836 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700837}
838
Dan Williams4e7d2c02009-08-29 19:13:11 -0700839static void mark_target_uptodate(struct stripe_head *sh, int target)
840{
841 struct r5dev *tgt;
842
843 if (target < 0)
844 return;
845
846 tgt = &sh->dev[target];
847 set_bit(R5_UPTODATE, &tgt->flags);
848 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
849 clear_bit(R5_Wantcompute, &tgt->flags);
850}
851
Dan Williamsac6b53b2009-07-14 13:40:19 -0700852static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700853{
854 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700855
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700856 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700857 (unsigned long long)sh->sector);
858
Dan Williamsac6b53b2009-07-14 13:40:19 -0700859 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700860 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700861 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700862
Dan Williamsecc65c92008-06-28 08:31:57 +1000863 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
864 if (sh->check_state == check_state_compute_run)
865 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700866 set_bit(STRIPE_HANDLE, &sh->state);
867 release_stripe(sh);
868}
869
Dan Williamsd6f38f32009-07-14 11:50:52 -0700870/* return a pointer to the address conversion region of the scribble buffer */
871static addr_conv_t *to_addr_conv(struct stripe_head *sh,
872 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700873{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700874 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
875}
876
877static struct dma_async_tx_descriptor *
878ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
879{
Dan Williams91c00922007-01-02 13:52:30 -0700880 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700881 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700882 int target = sh->ops.target;
883 struct r5dev *tgt = &sh->dev[target];
884 struct page *xor_dest = tgt->page;
885 int count = 0;
886 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700887 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700888 int i;
889
890 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -0700891 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700892 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
893
894 for (i = disks; i--; )
895 if (i != target)
896 xor_srcs[count++] = sh->dev[i].page;
897
898 atomic_inc(&sh->count);
899
Dan Williams0403e382009-09-08 17:42:50 -0700900 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700901 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700902 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700903 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700904 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700905 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700906
Dan Williams91c00922007-01-02 13:52:30 -0700907 return tx;
908}
909
Dan Williamsac6b53b2009-07-14 13:40:19 -0700910/* set_syndrome_sources - populate source buffers for gen_syndrome
911 * @srcs - (struct page *) array of size sh->disks
912 * @sh - stripe_head to parse
913 *
914 * Populates srcs in proper layout order for the stripe and returns the
915 * 'count' of sources to be used in a call to async_gen_syndrome. The P
916 * destination buffer is recorded in srcs[count] and the Q destination
917 * is recorded in srcs[count+1]].
918 */
919static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
920{
921 int disks = sh->disks;
922 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
923 int d0_idx = raid6_d0(sh);
924 int count;
925 int i;
926
927 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100928 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700929
930 count = 0;
931 i = d0_idx;
932 do {
933 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
934
935 srcs[slot] = sh->dev[i].page;
936 i = raid6_next_disk(i, disks);
937 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700938
NeilBrowne4424fe2009-10-16 16:27:34 +1100939 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700940}
941
942static struct dma_async_tx_descriptor *
943ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
944{
945 int disks = sh->disks;
946 struct page **blocks = percpu->scribble;
947 int target;
948 int qd_idx = sh->qd_idx;
949 struct dma_async_tx_descriptor *tx;
950 struct async_submit_ctl submit;
951 struct r5dev *tgt;
952 struct page *dest;
953 int i;
954 int count;
955
956 if (sh->ops.target < 0)
957 target = sh->ops.target2;
958 else if (sh->ops.target2 < 0)
959 target = sh->ops.target;
960 else
961 /* we should only have one valid target */
962 BUG();
963 BUG_ON(target < 0);
964 pr_debug("%s: stripe %llu block: %d\n",
965 __func__, (unsigned long long)sh->sector, target);
966
967 tgt = &sh->dev[target];
968 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
969 dest = tgt->page;
970
971 atomic_inc(&sh->count);
972
973 if (target == qd_idx) {
974 count = set_syndrome_sources(blocks, sh);
975 blocks[count] = NULL; /* regenerating p is not necessary */
976 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700977 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
978 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700979 to_addr_conv(sh, percpu));
980 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
981 } else {
982 /* Compute any data- or p-drive using XOR */
983 count = 0;
984 for (i = disks; i-- ; ) {
985 if (i == target || i == qd_idx)
986 continue;
987 blocks[count++] = sh->dev[i].page;
988 }
989
Dan Williams0403e382009-09-08 17:42:50 -0700990 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
991 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700992 to_addr_conv(sh, percpu));
993 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
994 }
995
996 return tx;
997}
998
999static struct dma_async_tx_descriptor *
1000ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1001{
1002 int i, count, disks = sh->disks;
1003 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1004 int d0_idx = raid6_d0(sh);
1005 int faila = -1, failb = -1;
1006 int target = sh->ops.target;
1007 int target2 = sh->ops.target2;
1008 struct r5dev *tgt = &sh->dev[target];
1009 struct r5dev *tgt2 = &sh->dev[target2];
1010 struct dma_async_tx_descriptor *tx;
1011 struct page **blocks = percpu->scribble;
1012 struct async_submit_ctl submit;
1013
1014 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1015 __func__, (unsigned long long)sh->sector, target, target2);
1016 BUG_ON(target < 0 || target2 < 0);
1017 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1018 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1019
Dan Williams6c910a72009-09-16 12:24:54 -07001020 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001021 * slot number conversion for 'faila' and 'failb'
1022 */
1023 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001024 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001025 count = 0;
1026 i = d0_idx;
1027 do {
1028 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1029
1030 blocks[slot] = sh->dev[i].page;
1031
1032 if (i == target)
1033 faila = slot;
1034 if (i == target2)
1035 failb = slot;
1036 i = raid6_next_disk(i, disks);
1037 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001038
1039 BUG_ON(faila == failb);
1040 if (failb < faila)
1041 swap(faila, failb);
1042 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1043 __func__, (unsigned long long)sh->sector, faila, failb);
1044
1045 atomic_inc(&sh->count);
1046
1047 if (failb == syndrome_disks+1) {
1048 /* Q disk is one of the missing disks */
1049 if (faila == syndrome_disks) {
1050 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001051 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1052 ops_complete_compute, sh,
1053 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001054 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001055 STRIPE_SIZE, &submit);
1056 } else {
1057 struct page *dest;
1058 int data_target;
1059 int qd_idx = sh->qd_idx;
1060
1061 /* Missing D+Q: recompute D from P, then recompute Q */
1062 if (target == qd_idx)
1063 data_target = target2;
1064 else
1065 data_target = target;
1066
1067 count = 0;
1068 for (i = disks; i-- ; ) {
1069 if (i == data_target || i == qd_idx)
1070 continue;
1071 blocks[count++] = sh->dev[i].page;
1072 }
1073 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001074 init_async_submit(&submit,
1075 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1076 NULL, NULL, NULL,
1077 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001078 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1079 &submit);
1080
1081 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001082 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1083 ops_complete_compute, sh,
1084 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001085 return async_gen_syndrome(blocks, 0, count+2,
1086 STRIPE_SIZE, &submit);
1087 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001088 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001089 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1090 ops_complete_compute, sh,
1091 to_addr_conv(sh, percpu));
1092 if (failb == syndrome_disks) {
1093 /* We're missing D+P. */
1094 return async_raid6_datap_recov(syndrome_disks+2,
1095 STRIPE_SIZE, faila,
1096 blocks, &submit);
1097 } else {
1098 /* We're missing D+D. */
1099 return async_raid6_2data_recov(syndrome_disks+2,
1100 STRIPE_SIZE, faila, failb,
1101 blocks, &submit);
1102 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001103 }
1104}
1105
1106
Dan Williams91c00922007-01-02 13:52:30 -07001107static void ops_complete_prexor(void *stripe_head_ref)
1108{
1109 struct stripe_head *sh = stripe_head_ref;
1110
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001111 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001112 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001113}
1114
1115static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001116ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1117 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001118{
Dan Williams91c00922007-01-02 13:52:30 -07001119 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001120 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001121 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001122 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001123
1124 /* existing parity data subtracted */
1125 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1126
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001127 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001128 (unsigned long long)sh->sector);
1129
1130 for (i = disks; i--; ) {
1131 struct r5dev *dev = &sh->dev[i];
1132 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001133 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001134 xor_srcs[count++] = dev->page;
1135 }
1136
Dan Williams0403e382009-09-08 17:42:50 -07001137 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001138 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001139 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001140
1141 return tx;
1142}
1143
1144static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001145ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001146{
1147 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001148 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001149
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001150 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001151 (unsigned long long)sh->sector);
1152
1153 for (i = disks; i--; ) {
1154 struct r5dev *dev = &sh->dev[i];
1155 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001156
Dan Williamsd8ee0722008-06-28 08:32:06 +10001157 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001158 struct bio *wbi;
1159
Shaohua Lib17459c2012-07-19 16:01:31 +10001160 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001161 chosen = dev->towrite;
1162 dev->towrite = NULL;
1163 BUG_ON(dev->written);
1164 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001165 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001166
1167 while (wbi && wbi->bi_sector <
1168 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001169 if (wbi->bi_rw & REQ_FUA)
1170 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001171 if (wbi->bi_rw & REQ_SYNC)
1172 set_bit(R5_SyncIO, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001173 tx = async_copy_data(1, wbi, dev->page,
1174 dev->sector, tx);
1175 wbi = r5_next_bio(wbi, dev->sector);
1176 }
1177 }
1178 }
1179
1180 return tx;
1181}
1182
Dan Williamsac6b53b2009-07-14 13:40:19 -07001183static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001184{
1185 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001186 int disks = sh->disks;
1187 int pd_idx = sh->pd_idx;
1188 int qd_idx = sh->qd_idx;
1189 int i;
Shaohua Libc0934f2012-05-22 13:55:05 +10001190 bool fua = false, sync = false;
Dan Williams91c00922007-01-02 13:52:30 -07001191
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001192 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001193 (unsigned long long)sh->sector);
1194
Shaohua Libc0934f2012-05-22 13:55:05 +10001195 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001196 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001197 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1198 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001199
Dan Williams91c00922007-01-02 13:52:30 -07001200 for (i = disks; i--; ) {
1201 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001202
Tejun Heoe9c74692010-09-03 11:56:18 +02001203 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001204 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001205 if (fua)
1206 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001207 if (sync)
1208 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001209 }
Dan Williams91c00922007-01-02 13:52:30 -07001210 }
1211
Dan Williamsd8ee0722008-06-28 08:32:06 +10001212 if (sh->reconstruct_state == reconstruct_state_drain_run)
1213 sh->reconstruct_state = reconstruct_state_drain_result;
1214 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1215 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1216 else {
1217 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1218 sh->reconstruct_state = reconstruct_state_result;
1219 }
Dan Williams91c00922007-01-02 13:52:30 -07001220
1221 set_bit(STRIPE_HANDLE, &sh->state);
1222 release_stripe(sh);
1223}
1224
1225static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001226ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1227 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001228{
Dan Williams91c00922007-01-02 13:52:30 -07001229 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001230 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001231 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001232 int count = 0, pd_idx = sh->pd_idx, i;
1233 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001234 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001235 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001236
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001237 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001238 (unsigned long long)sh->sector);
1239
1240 /* check if prexor is active which means only process blocks
1241 * that are part of a read-modify-write (written)
1242 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001243 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1244 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001245 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1246 for (i = disks; i--; ) {
1247 struct r5dev *dev = &sh->dev[i];
1248 if (dev->written)
1249 xor_srcs[count++] = dev->page;
1250 }
1251 } else {
1252 xor_dest = sh->dev[pd_idx].page;
1253 for (i = disks; i--; ) {
1254 struct r5dev *dev = &sh->dev[i];
1255 if (i != pd_idx)
1256 xor_srcs[count++] = dev->page;
1257 }
1258 }
1259
Dan Williams91c00922007-01-02 13:52:30 -07001260 /* 1/ if we prexor'd then the dest is reused as a source
1261 * 2/ if we did not prexor then we are redoing the parity
1262 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1263 * for the synchronous xor case
1264 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001265 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001266 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1267
1268 atomic_inc(&sh->count);
1269
Dan Williamsac6b53b2009-07-14 13:40:19 -07001270 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001271 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001272 if (unlikely(count == 1))
1273 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1274 else
1275 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001276}
1277
Dan Williamsac6b53b2009-07-14 13:40:19 -07001278static void
1279ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1280 struct dma_async_tx_descriptor *tx)
1281{
1282 struct async_submit_ctl submit;
1283 struct page **blocks = percpu->scribble;
1284 int count;
1285
1286 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1287
1288 count = set_syndrome_sources(blocks, sh);
1289
1290 atomic_inc(&sh->count);
1291
1292 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1293 sh, to_addr_conv(sh, percpu));
1294 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001295}
1296
1297static void ops_complete_check(void *stripe_head_ref)
1298{
1299 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001300
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001301 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001302 (unsigned long long)sh->sector);
1303
Dan Williamsecc65c92008-06-28 08:31:57 +10001304 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001305 set_bit(STRIPE_HANDLE, &sh->state);
1306 release_stripe(sh);
1307}
1308
Dan Williamsac6b53b2009-07-14 13:40:19 -07001309static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001310{
Dan Williams91c00922007-01-02 13:52:30 -07001311 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001312 int pd_idx = sh->pd_idx;
1313 int qd_idx = sh->qd_idx;
1314 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001315 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001316 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001317 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001318 int count;
1319 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001320
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001321 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001322 (unsigned long long)sh->sector);
1323
Dan Williamsac6b53b2009-07-14 13:40:19 -07001324 count = 0;
1325 xor_dest = sh->dev[pd_idx].page;
1326 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001327 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001328 if (i == pd_idx || i == qd_idx)
1329 continue;
1330 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001331 }
1332
Dan Williamsd6f38f32009-07-14 11:50:52 -07001333 init_async_submit(&submit, 0, NULL, NULL, NULL,
1334 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001335 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001336 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001337
Dan Williams91c00922007-01-02 13:52:30 -07001338 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001339 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1340 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001341}
1342
Dan Williamsac6b53b2009-07-14 13:40:19 -07001343static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1344{
1345 struct page **srcs = percpu->scribble;
1346 struct async_submit_ctl submit;
1347 int count;
1348
1349 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1350 (unsigned long long)sh->sector, checkp);
1351
1352 count = set_syndrome_sources(srcs, sh);
1353 if (!checkp)
1354 srcs[count] = NULL;
1355
1356 atomic_inc(&sh->count);
1357 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1358 sh, to_addr_conv(sh, percpu));
1359 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1360 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1361}
1362
Dan Williams417b8d42009-10-16 16:25:22 +11001363static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001364{
1365 int overlap_clear = 0, i, disks = sh->disks;
1366 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001367 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001368 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001369 struct raid5_percpu *percpu;
1370 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001371
Dan Williamsd6f38f32009-07-14 11:50:52 -07001372 cpu = get_cpu();
1373 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001374 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001375 ops_run_biofill(sh);
1376 overlap_clear++;
1377 }
1378
Dan Williams7b3a8712008-06-28 08:32:09 +10001379 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001380 if (level < 6)
1381 tx = ops_run_compute5(sh, percpu);
1382 else {
1383 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1384 tx = ops_run_compute6_1(sh, percpu);
1385 else
1386 tx = ops_run_compute6_2(sh, percpu);
1387 }
1388 /* terminate the chain if reconstruct is not set to be run */
1389 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001390 async_tx_ack(tx);
1391 }
Dan Williams91c00922007-01-02 13:52:30 -07001392
Dan Williams600aa102008-06-28 08:32:05 +10001393 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001394 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001395
Dan Williams600aa102008-06-28 08:32:05 +10001396 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001397 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001398 overlap_clear++;
1399 }
1400
Dan Williamsac6b53b2009-07-14 13:40:19 -07001401 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1402 if (level < 6)
1403 ops_run_reconstruct5(sh, percpu, tx);
1404 else
1405 ops_run_reconstruct6(sh, percpu, tx);
1406 }
Dan Williams91c00922007-01-02 13:52:30 -07001407
Dan Williamsac6b53b2009-07-14 13:40:19 -07001408 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1409 if (sh->check_state == check_state_run)
1410 ops_run_check_p(sh, percpu);
1411 else if (sh->check_state == check_state_run_q)
1412 ops_run_check_pq(sh, percpu, 0);
1413 else if (sh->check_state == check_state_run_pq)
1414 ops_run_check_pq(sh, percpu, 1);
1415 else
1416 BUG();
1417 }
Dan Williams91c00922007-01-02 13:52:30 -07001418
Dan Williams91c00922007-01-02 13:52:30 -07001419 if (overlap_clear)
1420 for (i = disks; i--; ) {
1421 struct r5dev *dev = &sh->dev[i];
1422 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1423 wake_up(&sh->raid_conf->wait_for_overlap);
1424 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001425 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001426}
1427
Dan Williams417b8d42009-10-16 16:25:22 +11001428#ifdef CONFIG_MULTICORE_RAID456
1429static void async_run_ops(void *param, async_cookie_t cookie)
1430{
1431 struct stripe_head *sh = param;
1432 unsigned long ops_request = sh->ops.request;
1433
1434 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1435 wake_up(&sh->ops.wait_for_ops);
1436
1437 __raid_run_ops(sh, ops_request);
1438 release_stripe(sh);
1439}
1440
1441static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1442{
1443 /* since handle_stripe can be called outside of raid5d context
1444 * we need to ensure sh->ops.request is de-staged before another
1445 * request arrives
1446 */
1447 wait_event(sh->ops.wait_for_ops,
1448 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1449 sh->ops.request = ops_request;
1450
1451 atomic_inc(&sh->count);
1452 async_schedule(async_run_ops, sh);
1453}
1454#else
1455#define raid_run_ops __raid_run_ops
1456#endif
1457
NeilBrownd1688a62011-10-11 16:49:52 +11001458static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001461 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001462 if (!sh)
1463 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001464
NeilBrown3f294f42005-11-08 21:39:25 -08001465 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001466 #ifdef CONFIG_MULTICORE_RAID456
1467 init_waitqueue_head(&sh->ops.wait_for_ops);
1468 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001469
Shaohua Lib17459c2012-07-19 16:01:31 +10001470 spin_lock_init(&sh->stripe_lock);
1471
NeilBrowne4e11e32010-06-16 16:45:16 +10001472 if (grow_buffers(sh)) {
1473 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001474 kmem_cache_free(conf->slab_cache, sh);
1475 return 0;
1476 }
1477 /* we just created an active stripe so... */
1478 atomic_set(&sh->count, 1);
1479 atomic_inc(&conf->active_stripes);
1480 INIT_LIST_HEAD(&sh->lru);
1481 release_stripe(sh);
1482 return 1;
1483}
1484
NeilBrownd1688a62011-10-11 16:49:52 +11001485static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001486{
Christoph Lametere18b8902006-12-06 20:33:20 -08001487 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001488 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
NeilBrownf4be6b42010-06-01 19:37:25 +10001490 if (conf->mddev->gendisk)
1491 sprintf(conf->cache_name[0],
1492 "raid%d-%s", conf->level, mdname(conf->mddev));
1493 else
1494 sprintf(conf->cache_name[0],
1495 "raid%d-%p", conf->level, conf->mddev);
1496 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1497
NeilBrownad01c9e2006-03-27 01:18:07 -08001498 conf->active_name = 0;
1499 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001501 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (!sc)
1503 return 1;
1504 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001505 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001506 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001507 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return 0;
1510}
NeilBrown29269552006-03-27 01:18:10 -08001511
Dan Williamsd6f38f32009-07-14 11:50:52 -07001512/**
1513 * scribble_len - return the required size of the scribble region
1514 * @num - total number of disks in the array
1515 *
1516 * The size must be enough to contain:
1517 * 1/ a struct page pointer for each device in the array +2
1518 * 2/ room to convert each entry in (1) to its corresponding dma
1519 * (dma_map_page()) or page (page_address()) address.
1520 *
1521 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1522 * calculate over all devices (not just the data blocks), using zeros in place
1523 * of the P and Q blocks.
1524 */
1525static size_t scribble_len(int num)
1526{
1527 size_t len;
1528
1529 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1530
1531 return len;
1532}
1533
NeilBrownd1688a62011-10-11 16:49:52 +11001534static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001535{
1536 /* Make all the stripes able to hold 'newsize' devices.
1537 * New slots in each stripe get 'page' set to a new page.
1538 *
1539 * This happens in stages:
1540 * 1/ create a new kmem_cache and allocate the required number of
1541 * stripe_heads.
1542 * 2/ gather all the old stripe_heads and tranfer the pages across
1543 * to the new stripe_heads. This will have the side effect of
1544 * freezing the array as once all stripe_heads have been collected,
1545 * no IO will be possible. Old stripe heads are freed once their
1546 * pages have been transferred over, and the old kmem_cache is
1547 * freed when all stripes are done.
1548 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1549 * we simple return a failre status - no need to clean anything up.
1550 * 4/ allocate new pages for the new slots in the new stripe_heads.
1551 * If this fails, we don't bother trying the shrink the
1552 * stripe_heads down again, we just leave them as they are.
1553 * As each stripe_head is processed the new one is released into
1554 * active service.
1555 *
1556 * Once step2 is started, we cannot afford to wait for a write,
1557 * so we use GFP_NOIO allocations.
1558 */
1559 struct stripe_head *osh, *nsh;
1560 LIST_HEAD(newstripes);
1561 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001562 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001563 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001564 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001565 int i;
1566
1567 if (newsize <= conf->pool_size)
1568 return 0; /* never bother to shrink */
1569
Dan Williamsb5470dc2008-06-27 21:44:04 -07001570 err = md_allow_write(conf->mddev);
1571 if (err)
1572 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001573
NeilBrownad01c9e2006-03-27 01:18:07 -08001574 /* Step 1 */
1575 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1576 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001577 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001578 if (!sc)
1579 return -ENOMEM;
1580
1581 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001582 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001583 if (!nsh)
1584 break;
1585
NeilBrownad01c9e2006-03-27 01:18:07 -08001586 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001587 #ifdef CONFIG_MULTICORE_RAID456
1588 init_waitqueue_head(&nsh->ops.wait_for_ops);
1589 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001590
1591 list_add(&nsh->lru, &newstripes);
1592 }
1593 if (i) {
1594 /* didn't get enough, give up */
1595 while (!list_empty(&newstripes)) {
1596 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1597 list_del(&nsh->lru);
1598 kmem_cache_free(sc, nsh);
1599 }
1600 kmem_cache_destroy(sc);
1601 return -ENOMEM;
1602 }
1603 /* Step 2 - Must use GFP_NOIO now.
1604 * OK, we have enough stripes, start collecting inactive
1605 * stripes and copying them over
1606 */
1607 list_for_each_entry(nsh, &newstripes, lru) {
1608 spin_lock_irq(&conf->device_lock);
1609 wait_event_lock_irq(conf->wait_for_stripe,
1610 !list_empty(&conf->inactive_list),
1611 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001612 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001613 osh = get_free_stripe(conf);
1614 spin_unlock_irq(&conf->device_lock);
1615 atomic_set(&nsh->count, 1);
1616 for(i=0; i<conf->pool_size; i++)
1617 nsh->dev[i].page = osh->dev[i].page;
1618 for( ; i<newsize; i++)
1619 nsh->dev[i].page = NULL;
1620 kmem_cache_free(conf->slab_cache, osh);
1621 }
1622 kmem_cache_destroy(conf->slab_cache);
1623
1624 /* Step 3.
1625 * At this point, we are holding all the stripes so the array
1626 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001627 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001628 */
1629 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1630 if (ndisks) {
1631 for (i=0; i<conf->raid_disks; i++)
1632 ndisks[i] = conf->disks[i];
1633 kfree(conf->disks);
1634 conf->disks = ndisks;
1635 } else
1636 err = -ENOMEM;
1637
Dan Williamsd6f38f32009-07-14 11:50:52 -07001638 get_online_cpus();
1639 conf->scribble_len = scribble_len(newsize);
1640 for_each_present_cpu(cpu) {
1641 struct raid5_percpu *percpu;
1642 void *scribble;
1643
1644 percpu = per_cpu_ptr(conf->percpu, cpu);
1645 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1646
1647 if (scribble) {
1648 kfree(percpu->scribble);
1649 percpu->scribble = scribble;
1650 } else {
1651 err = -ENOMEM;
1652 break;
1653 }
1654 }
1655 put_online_cpus();
1656
NeilBrownad01c9e2006-03-27 01:18:07 -08001657 /* Step 4, return new stripes to service */
1658 while(!list_empty(&newstripes)) {
1659 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1660 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001661
NeilBrownad01c9e2006-03-27 01:18:07 -08001662 for (i=conf->raid_disks; i < newsize; i++)
1663 if (nsh->dev[i].page == NULL) {
1664 struct page *p = alloc_page(GFP_NOIO);
1665 nsh->dev[i].page = p;
1666 if (!p)
1667 err = -ENOMEM;
1668 }
1669 release_stripe(nsh);
1670 }
1671 /* critical section pass, GFP_NOIO no longer needed */
1672
1673 conf->slab_cache = sc;
1674 conf->active_name = 1-conf->active_name;
1675 conf->pool_size = newsize;
1676 return err;
1677}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
NeilBrownd1688a62011-10-11 16:49:52 +11001679static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
1681 struct stripe_head *sh;
1682
NeilBrown3f294f42005-11-08 21:39:25 -08001683 spin_lock_irq(&conf->device_lock);
1684 sh = get_free_stripe(conf);
1685 spin_unlock_irq(&conf->device_lock);
1686 if (!sh)
1687 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001688 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001689 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001690 kmem_cache_free(conf->slab_cache, sh);
1691 atomic_dec(&conf->active_stripes);
1692 return 1;
1693}
1694
NeilBrownd1688a62011-10-11 16:49:52 +11001695static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001696{
1697 while (drop_one_stripe(conf))
1698 ;
1699
NeilBrown29fc7e32006-02-03 03:03:41 -08001700 if (conf->slab_cache)
1701 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 conf->slab_cache = NULL;
1703}
1704
NeilBrown6712ecf2007-09-27 12:47:43 +02001705static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
NeilBrown99c0fb52009-03-31 14:39:38 +11001707 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001708 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001709 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001711 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001712 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001713 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 for (i=0 ; i<disks; i++)
1716 if (bi == &sh->dev[i].req)
1717 break;
1718
Dan Williams45b42332007-07-09 11:56:43 -07001719 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1720 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 uptodate);
1722 if (i == disks) {
1723 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001724 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
NeilBrown14a75d32011-12-23 10:17:52 +11001726 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001727 /* If replacement finished while this request was outstanding,
1728 * 'replacement' might be NULL already.
1729 * In that case it moved down to 'rdev'.
1730 * rdev is not removed until all requests are finished.
1731 */
NeilBrown14a75d32011-12-23 10:17:52 +11001732 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001733 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001734 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
NeilBrown05616be2012-05-21 09:27:00 +10001736 if (use_new_offset(conf, sh))
1737 s = sh->sector + rdev->new_data_offset;
1738 else
1739 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001742 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001743 /* Note that this cannot happen on a
1744 * replacement device. We just fail those on
1745 * any error
1746 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001747 printk_ratelimited(
1748 KERN_INFO
1749 "md/raid:%s: read error corrected"
1750 " (%lu sectors at %llu on %s)\n",
1751 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001752 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001753 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001754 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001755 clear_bit(R5_ReadError, &sh->dev[i].flags);
1756 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001757 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1758 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1759
NeilBrown14a75d32011-12-23 10:17:52 +11001760 if (atomic_read(&rdev->read_errors))
1761 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001763 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001764 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001765 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001768 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001769 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1770 printk_ratelimited(
1771 KERN_WARNING
1772 "md/raid:%s: read error on replacement device "
1773 "(sector %llu on %s).\n",
1774 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001775 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001776 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001777 else if (conf->mddev->degraded >= conf->max_degraded) {
1778 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001779 printk_ratelimited(
1780 KERN_WARNING
1781 "md/raid:%s: read error not correctable "
1782 "(sector %llu on %s).\n",
1783 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001784 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001785 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001786 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001787 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001788 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001789 printk_ratelimited(
1790 KERN_WARNING
1791 "md/raid:%s: read error NOT corrected!! "
1792 "(sector %llu on %s).\n",
1793 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001794 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001795 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001796 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001797 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001798 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001799 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001800 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001801 else
1802 retry = 1;
1803 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001804 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1805 set_bit(R5_ReadError, &sh->dev[i].flags);
1806 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1807 } else
1808 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001809 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001810 clear_bit(R5_ReadError, &sh->dev[i].flags);
1811 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001812 if (!(set_bad
1813 && test_bit(In_sync, &rdev->flags)
1814 && rdev_set_badblocks(
1815 rdev, sh->sector, STRIPE_SECTORS, 0)))
1816 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
NeilBrown14a75d32011-12-23 10:17:52 +11001819 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1821 set_bit(STRIPE_HANDLE, &sh->state);
1822 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
NeilBrownd710e132008-10-13 11:55:12 +11001825static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
NeilBrown99c0fb52009-03-31 14:39:38 +11001827 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001828 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001829 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001830 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001832 sector_t first_bad;
1833 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001834 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
NeilBrown977df362011-12-23 10:17:53 +11001836 for (i = 0 ; i < disks; i++) {
1837 if (bi == &sh->dev[i].req) {
1838 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 break;
NeilBrown977df362011-12-23 10:17:53 +11001840 }
1841 if (bi == &sh->dev[i].rreq) {
1842 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001843 if (rdev)
1844 replacement = 1;
1845 else
1846 /* rdev was removed and 'replacement'
1847 * replaced it. rdev is not removed
1848 * until all requests are finished.
1849 */
1850 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001851 break;
1852 }
1853 }
Dan Williams45b42332007-07-09 11:56:43 -07001854 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1856 uptodate);
1857 if (i == disks) {
1858 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001859 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
1861
NeilBrown977df362011-12-23 10:17:53 +11001862 if (replacement) {
1863 if (!uptodate)
1864 md_error(conf->mddev, rdev);
1865 else if (is_badblock(rdev, sh->sector,
1866 STRIPE_SECTORS,
1867 &first_bad, &bad_sectors))
1868 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1869 } else {
1870 if (!uptodate) {
1871 set_bit(WriteErrorSeen, &rdev->flags);
1872 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001873 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1874 set_bit(MD_RECOVERY_NEEDED,
1875 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001876 } else if (is_badblock(rdev, sh->sector,
1877 STRIPE_SECTORS,
1878 &first_bad, &bad_sectors))
1879 set_bit(R5_MadeGood, &sh->dev[i].flags);
1880 }
1881 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
NeilBrown977df362011-12-23 10:17:53 +11001883 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1884 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001886 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
NeilBrown784052e2009-03-31 15:19:07 +11001889static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
NeilBrown784052e2009-03-31 15:19:07 +11001891static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 struct r5dev *dev = &sh->dev[i];
1894
1895 bio_init(&dev->req);
1896 dev->req.bi_io_vec = &dev->vec;
1897 dev->req.bi_vcnt++;
1898 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001900 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
NeilBrown977df362011-12-23 10:17:53 +11001902 bio_init(&dev->rreq);
1903 dev->rreq.bi_io_vec = &dev->rvec;
1904 dev->rreq.bi_vcnt++;
1905 dev->rreq.bi_max_vecs++;
1906 dev->rreq.bi_private = sh;
1907 dev->rvec.bv_page = dev->page;
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001910 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
NeilBrownfd01b882011-10-11 16:47:53 +11001913static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
1915 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001916 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001917 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001918 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
NeilBrown908f4fb2011-12-23 10:17:50 +11001920 spin_lock_irqsave(&conf->device_lock, flags);
1921 clear_bit(In_sync, &rdev->flags);
1922 mddev->degraded = calc_degraded(conf);
1923 spin_unlock_irqrestore(&conf->device_lock, flags);
1924 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1925
NeilBrownde393cd2011-07-28 11:31:48 +10001926 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001927 set_bit(Faulty, &rdev->flags);
1928 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1929 printk(KERN_ALERT
1930 "md/raid:%s: Disk failure on %s, disabling device.\n"
1931 "md/raid:%s: Operation continuing on %d devices.\n",
1932 mdname(mddev),
1933 bdevname(rdev->bdev, b),
1934 mdname(mddev),
1935 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001936}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938/*
1939 * Input: a 'big' sector number,
1940 * Output: index of the data and parity disk, and the sector # in them.
1941 */
NeilBrownd1688a62011-10-11 16:49:52 +11001942static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001943 int previous, int *dd_idx,
1944 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001946 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001947 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001949 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001950 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001952 int algorithm = previous ? conf->prev_algo
1953 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001954 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1955 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001956 int raid_disks = previous ? conf->previous_raid_disks
1957 : conf->raid_disks;
1958 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 /* First compute the information on this sector */
1961
1962 /*
1963 * Compute the chunk number and the sector offset inside the chunk
1964 */
1965 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1966 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 /*
1969 * Compute the stripe number
1970 */
NeilBrown35f2a592010-04-20 14:13:34 +10001971 stripe = chunk_number;
1972 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001973 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /*
1975 * Select the parity disk based on the user selected algorithm.
1976 */
NeilBrown84789552011-07-27 11:00:36 +10001977 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001978 switch(conf->level) {
1979 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001980 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001981 break;
1982 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001983 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001985 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001986 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 (*dd_idx)++;
1988 break;
1989 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001990 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001991 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 (*dd_idx)++;
1993 break;
1994 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001995 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001996 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 break;
1998 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001999 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002000 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002002 case ALGORITHM_PARITY_0:
2003 pd_idx = 0;
2004 (*dd_idx)++;
2005 break;
2006 case ALGORITHM_PARITY_N:
2007 pd_idx = data_disks;
2008 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002010 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002011 }
2012 break;
2013 case 6:
2014
NeilBrowne183eae2009-03-31 15:20:22 +11002015 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002016 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002017 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002018 qd_idx = pd_idx + 1;
2019 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002020 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002021 qd_idx = 0;
2022 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002023 (*dd_idx) += 2; /* D D P Q D */
2024 break;
2025 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002026 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002027 qd_idx = pd_idx + 1;
2028 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002029 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002030 qd_idx = 0;
2031 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002032 (*dd_idx) += 2; /* D D P Q D */
2033 break;
2034 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002035 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002036 qd_idx = (pd_idx + 1) % raid_disks;
2037 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002038 break;
2039 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002040 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002041 qd_idx = (pd_idx + 1) % raid_disks;
2042 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002043 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002044
2045 case ALGORITHM_PARITY_0:
2046 pd_idx = 0;
2047 qd_idx = 1;
2048 (*dd_idx) += 2;
2049 break;
2050 case ALGORITHM_PARITY_N:
2051 pd_idx = data_disks;
2052 qd_idx = data_disks + 1;
2053 break;
2054
2055 case ALGORITHM_ROTATING_ZERO_RESTART:
2056 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2057 * of blocks for computing Q is different.
2058 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002059 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002060 qd_idx = pd_idx + 1;
2061 if (pd_idx == raid_disks-1) {
2062 (*dd_idx)++; /* Q D D D P */
2063 qd_idx = 0;
2064 } else if (*dd_idx >= pd_idx)
2065 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002066 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002067 break;
2068
2069 case ALGORITHM_ROTATING_N_RESTART:
2070 /* Same a left_asymmetric, by first stripe is
2071 * D D D P Q rather than
2072 * Q D D D P
2073 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002074 stripe2 += 1;
2075 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002076 qd_idx = pd_idx + 1;
2077 if (pd_idx == raid_disks-1) {
2078 (*dd_idx)++; /* Q D D D P */
2079 qd_idx = 0;
2080 } else if (*dd_idx >= pd_idx)
2081 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002082 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002083 break;
2084
2085 case ALGORITHM_ROTATING_N_CONTINUE:
2086 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002087 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002088 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2089 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002090 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002091 break;
2092
2093 case ALGORITHM_LEFT_ASYMMETRIC_6:
2094 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002095 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002096 if (*dd_idx >= pd_idx)
2097 (*dd_idx)++;
2098 qd_idx = raid_disks - 1;
2099 break;
2100
2101 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002102 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002103 if (*dd_idx >= pd_idx)
2104 (*dd_idx)++;
2105 qd_idx = raid_disks - 1;
2106 break;
2107
2108 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002109 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002110 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2111 qd_idx = raid_disks - 1;
2112 break;
2113
2114 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002115 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002116 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2117 qd_idx = raid_disks - 1;
2118 break;
2119
2120 case ALGORITHM_PARITY_0_6:
2121 pd_idx = 0;
2122 (*dd_idx)++;
2123 qd_idx = raid_disks - 1;
2124 break;
2125
NeilBrown16a53ec2006-06-26 00:27:38 -07002126 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002127 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002128 }
2129 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
2131
NeilBrown911d4ee2009-03-31 14:39:38 +11002132 if (sh) {
2133 sh->pd_idx = pd_idx;
2134 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002135 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 /*
2138 * Finally, compute the new sector number
2139 */
2140 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2141 return new_sector;
2142}
2143
2144
NeilBrown784052e2009-03-31 15:19:07 +11002145static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
NeilBrownd1688a62011-10-11 16:49:52 +11002147 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002148 int raid_disks = sh->disks;
2149 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002151 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2152 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002153 int algorithm = previous ? conf->prev_algo
2154 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 sector_t stripe;
2156 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002157 sector_t chunk_number;
2158 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002160 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
NeilBrown16a53ec2006-06-26 00:27:38 -07002162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2164 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
NeilBrown16a53ec2006-06-26 00:27:38 -07002166 if (i == sh->pd_idx)
2167 return 0;
2168 switch(conf->level) {
2169 case 4: break;
2170 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002171 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 case ALGORITHM_LEFT_ASYMMETRIC:
2173 case ALGORITHM_RIGHT_ASYMMETRIC:
2174 if (i > sh->pd_idx)
2175 i--;
2176 break;
2177 case ALGORITHM_LEFT_SYMMETRIC:
2178 case ALGORITHM_RIGHT_SYMMETRIC:
2179 if (i < sh->pd_idx)
2180 i += raid_disks;
2181 i -= (sh->pd_idx + 1);
2182 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002183 case ALGORITHM_PARITY_0:
2184 i -= 1;
2185 break;
2186 case ALGORITHM_PARITY_N:
2187 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002189 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002190 }
2191 break;
2192 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002193 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002194 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002195 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002196 case ALGORITHM_LEFT_ASYMMETRIC:
2197 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002198 case ALGORITHM_ROTATING_ZERO_RESTART:
2199 case ALGORITHM_ROTATING_N_RESTART:
2200 if (sh->pd_idx == raid_disks-1)
2201 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002202 else if (i > sh->pd_idx)
2203 i -= 2; /* D D P Q D */
2204 break;
2205 case ALGORITHM_LEFT_SYMMETRIC:
2206 case ALGORITHM_RIGHT_SYMMETRIC:
2207 if (sh->pd_idx == raid_disks-1)
2208 i--; /* Q D D D P */
2209 else {
2210 /* D D P Q D */
2211 if (i < sh->pd_idx)
2212 i += raid_disks;
2213 i -= (sh->pd_idx + 2);
2214 }
2215 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002216 case ALGORITHM_PARITY_0:
2217 i -= 2;
2218 break;
2219 case ALGORITHM_PARITY_N:
2220 break;
2221 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002222 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002223 if (sh->pd_idx == 0)
2224 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002225 else {
2226 /* D D Q P D */
2227 if (i < sh->pd_idx)
2228 i += raid_disks;
2229 i -= (sh->pd_idx + 1);
2230 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002231 break;
2232 case ALGORITHM_LEFT_ASYMMETRIC_6:
2233 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2234 if (i > sh->pd_idx)
2235 i--;
2236 break;
2237 case ALGORITHM_LEFT_SYMMETRIC_6:
2238 case ALGORITHM_RIGHT_SYMMETRIC_6:
2239 if (i < sh->pd_idx)
2240 i += data_disks + 1;
2241 i -= (sh->pd_idx + 1);
2242 break;
2243 case ALGORITHM_PARITY_0_6:
2244 i -= 1;
2245 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002246 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002247 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002248 }
2249 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 }
2251
2252 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002253 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
NeilBrown112bf892009-03-31 14:39:38 +11002255 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002256 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002257 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2258 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002259 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2260 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return 0;
2262 }
2263 return r_sector;
2264}
2265
2266
Dan Williams600aa102008-06-28 08:32:05 +10002267static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002268schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002269 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002270{
2271 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002272 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002273 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002274
Dan Williamse33129d2007-01-02 13:52:30 -07002275 if (rcw) {
2276 /* if we are not expanding this is a proper write request, and
2277 * there will be bios with new data to be drained into the
2278 * stripe cache
2279 */
2280 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002281 sh->reconstruct_state = reconstruct_state_drain_run;
2282 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2283 } else
2284 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002285
Dan Williamsac6b53b2009-07-14 13:40:19 -07002286 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002287
2288 for (i = disks; i--; ) {
2289 struct r5dev *dev = &sh->dev[i];
2290
2291 if (dev->towrite) {
2292 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002293 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002294 if (!expand)
2295 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002296 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002297 }
2298 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002299 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002300 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002301 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002302 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002303 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002304 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2305 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2306
Dan Williamsd8ee0722008-06-28 08:32:06 +10002307 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002308 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2309 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002310 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002311
2312 for (i = disks; i--; ) {
2313 struct r5dev *dev = &sh->dev[i];
2314 if (i == pd_idx)
2315 continue;
2316
Dan Williamse33129d2007-01-02 13:52:30 -07002317 if (dev->towrite &&
2318 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002319 test_bit(R5_Wantcompute, &dev->flags))) {
2320 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002321 set_bit(R5_LOCKED, &dev->flags);
2322 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002323 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002324 }
2325 }
2326 }
2327
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002328 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002329 * are in flight
2330 */
2331 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2332 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002333 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002334
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002335 if (level == 6) {
2336 int qd_idx = sh->qd_idx;
2337 struct r5dev *dev = &sh->dev[qd_idx];
2338
2339 set_bit(R5_LOCKED, &dev->flags);
2340 clear_bit(R5_UPTODATE, &dev->flags);
2341 s->locked++;
2342 }
2343
Dan Williams600aa102008-06-28 08:32:05 +10002344 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002345 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002346 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002347}
NeilBrown16a53ec2006-06-26 00:27:38 -07002348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349/*
2350 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002351 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 * The bi_next chain must be in order.
2353 */
2354static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2355{
2356 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002357 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002358 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
NeilBrowncbe47ec2011-07-26 11:20:35 +10002360 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 (unsigned long long)bi->bi_sector,
2362 (unsigned long long)sh->sector);
2363
Shaohua Lib17459c2012-07-19 16:01:31 +10002364 /*
2365 * If several bio share a stripe. The bio bi_phys_segments acts as a
2366 * reference count to avoid race. The reference count should already be
2367 * increased before this function is called (for example, in
2368 * make_request()), so other bio sharing this stripe will not free the
2369 * stripe. If a stripe is owned by one stripe, the stripe lock will
2370 * protect it.
2371 */
2372 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002373 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002375 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002376 firstwrite = 1;
2377 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 bip = &sh->dev[dd_idx].toread;
2379 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2380 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2381 goto overlap;
2382 bip = & (*bip)->bi_next;
2383 }
2384 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2385 goto overlap;
2386
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002387 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (*bip)
2389 bi->bi_next = *bip;
2390 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002391 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (forwrite) {
2394 /* check if page is covered */
2395 sector_t sector = sh->dev[dd_idx].sector;
2396 for (bi=sh->dev[dd_idx].towrite;
2397 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2398 bi && bi->bi_sector <= sector;
2399 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2400 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2401 sector = bi->bi_sector + (bi->bi_size>>9);
2402 }
2403 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2404 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2405 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002406 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002407
2408 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2409 (unsigned long long)(*bip)->bi_sector,
2410 (unsigned long long)sh->sector, dd_idx);
2411
2412 if (conf->mddev->bitmap && firstwrite) {
2413 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2414 STRIPE_SECTORS, 0);
2415 sh->bm_seq = conf->seq_flush+1;
2416 set_bit(STRIPE_BIT_DELAY, &sh->state);
2417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return 1;
2419
2420 overlap:
2421 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002422 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return 0;
2424}
2425
NeilBrownd1688a62011-10-11 16:49:52 +11002426static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002427
NeilBrownd1688a62011-10-11 16:49:52 +11002428static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002429 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002430{
NeilBrown784052e2009-03-31 15:19:07 +11002431 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002432 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002433 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002434 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002435 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002436
NeilBrown112bf892009-03-31 14:39:38 +11002437 raid5_compute_sector(conf,
2438 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002439 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002440 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002441 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002442}
2443
Dan Williamsa4456852007-07-09 11:56:43 -07002444static void
NeilBrownd1688a62011-10-11 16:49:52 +11002445handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002446 struct stripe_head_state *s, int disks,
2447 struct bio **return_bi)
2448{
2449 int i;
2450 for (i = disks; i--; ) {
2451 struct bio *bi;
2452 int bitmap_end = 0;
2453
2454 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002455 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002456 rcu_read_lock();
2457 rdev = rcu_dereference(conf->disks[i].rdev);
2458 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002459 atomic_inc(&rdev->nr_pending);
2460 else
2461 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002462 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002463 if (rdev) {
2464 if (!rdev_set_badblocks(
2465 rdev,
2466 sh->sector,
2467 STRIPE_SECTORS, 0))
2468 md_error(conf->mddev, rdev);
2469 rdev_dec_pending(rdev, conf->mddev);
2470 }
Dan Williamsa4456852007-07-09 11:56:43 -07002471 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002472 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002473 /* fail all writes first */
2474 bi = sh->dev[i].towrite;
2475 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002476 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002477 if (bi) {
2478 s->to_write--;
2479 bitmap_end = 1;
2480 }
2481
2482 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2483 wake_up(&conf->wait_for_overlap);
2484
2485 while (bi && bi->bi_sector <
2486 sh->dev[i].sector + STRIPE_SECTORS) {
2487 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2488 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002489 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002490 md_write_end(conf->mddev);
2491 bi->bi_next = *return_bi;
2492 *return_bi = bi;
2493 }
2494 bi = nextbi;
2495 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002496 if (bitmap_end)
2497 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2498 STRIPE_SECTORS, 0, 0);
2499 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002500 /* and fail all 'written' */
2501 bi = sh->dev[i].written;
2502 sh->dev[i].written = NULL;
2503 if (bi) bitmap_end = 1;
2504 while (bi && bi->bi_sector <
2505 sh->dev[i].sector + STRIPE_SECTORS) {
2506 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2507 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002508 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002509 md_write_end(conf->mddev);
2510 bi->bi_next = *return_bi;
2511 *return_bi = bi;
2512 }
2513 bi = bi2;
2514 }
2515
Dan Williamsb5e98d62007-01-02 13:52:31 -07002516 /* fail any reads if this device is non-operational and
2517 * the data has not reached the cache yet.
2518 */
2519 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2520 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2521 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002522 bi = sh->dev[i].toread;
2523 sh->dev[i].toread = NULL;
2524 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2525 wake_up(&conf->wait_for_overlap);
2526 if (bi) s->to_read--;
2527 while (bi && bi->bi_sector <
2528 sh->dev[i].sector + STRIPE_SECTORS) {
2529 struct bio *nextbi =
2530 r5_next_bio(bi, sh->dev[i].sector);
2531 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002532 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002533 bi->bi_next = *return_bi;
2534 *return_bi = bi;
2535 }
2536 bi = nextbi;
2537 }
2538 }
Dan Williamsa4456852007-07-09 11:56:43 -07002539 if (bitmap_end)
2540 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2541 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002542 /* If we were in the middle of a write the parity block might
2543 * still be locked - so just clear all R5_LOCKED flags
2544 */
2545 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002546 }
2547
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002548 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2549 if (atomic_dec_and_test(&conf->pending_full_writes))
2550 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002551}
2552
NeilBrown7f0da592011-07-28 11:39:22 +10002553static void
NeilBrownd1688a62011-10-11 16:49:52 +11002554handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002555 struct stripe_head_state *s)
2556{
2557 int abort = 0;
2558 int i;
2559
NeilBrown7f0da592011-07-28 11:39:22 +10002560 clear_bit(STRIPE_SYNCING, &sh->state);
2561 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002562 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002563 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002564 * Don't even need to abort as that is handled elsewhere
2565 * if needed, and not always wanted e.g. if there is a known
2566 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002567 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002568 * non-sync devices, or abort the recovery
2569 */
NeilBrown18b98372012-04-01 23:48:38 +10002570 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2571 /* During recovery devices cannot be removed, so
2572 * locking and refcounting of rdevs is not needed
2573 */
2574 for (i = 0; i < conf->raid_disks; i++) {
2575 struct md_rdev *rdev = conf->disks[i].rdev;
2576 if (rdev
2577 && !test_bit(Faulty, &rdev->flags)
2578 && !test_bit(In_sync, &rdev->flags)
2579 && !rdev_set_badblocks(rdev, sh->sector,
2580 STRIPE_SECTORS, 0))
2581 abort = 1;
2582 rdev = conf->disks[i].replacement;
2583 if (rdev
2584 && !test_bit(Faulty, &rdev->flags)
2585 && !test_bit(In_sync, &rdev->flags)
2586 && !rdev_set_badblocks(rdev, sh->sector,
2587 STRIPE_SECTORS, 0))
2588 abort = 1;
2589 }
2590 if (abort)
2591 conf->recovery_disabled =
2592 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002593 }
NeilBrown18b98372012-04-01 23:48:38 +10002594 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002595}
2596
NeilBrown9a3e1102011-12-23 10:17:53 +11002597static int want_replace(struct stripe_head *sh, int disk_idx)
2598{
2599 struct md_rdev *rdev;
2600 int rv = 0;
2601 /* Doing recovery so rcu locking not required */
2602 rdev = sh->raid_conf->disks[disk_idx].replacement;
2603 if (rdev
2604 && !test_bit(Faulty, &rdev->flags)
2605 && !test_bit(In_sync, &rdev->flags)
2606 && (rdev->recovery_offset <= sh->sector
2607 || rdev->mddev->recovery_cp <= sh->sector))
2608 rv = 1;
2609
2610 return rv;
2611}
2612
NeilBrown93b3dbc2011-07-27 11:00:36 +10002613/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002614 * to be read or computed to satisfy a request.
2615 *
2616 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002617 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002618 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002619static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2620 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002621{
2622 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002623 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2624 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002625
Dan Williamsf38e1212007-01-02 13:52:30 -07002626 /* is the data in this block needed, and can we get it? */
2627 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002628 !test_bit(R5_UPTODATE, &dev->flags) &&
2629 (dev->toread ||
2630 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2631 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002632 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002633 (s->failed >= 1 && fdev[0]->toread) ||
2634 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002635 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2636 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2637 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002638 /* we would like to get this block, possibly by computing it,
2639 * otherwise read it if the backing disk is insync
2640 */
2641 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2642 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2643 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002644 (s->failed && (disk_idx == s->failed_num[0] ||
2645 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002646 /* have disk failed, and we're requested to fetch it;
2647 * do compute it
2648 */
2649 pr_debug("Computing stripe %llu block %d\n",
2650 (unsigned long long)sh->sector, disk_idx);
2651 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2652 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2653 set_bit(R5_Wantcompute, &dev->flags);
2654 sh->ops.target = disk_idx;
2655 sh->ops.target2 = -1; /* no 2nd target */
2656 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002657 /* Careful: from this point on 'uptodate' is in the eye
2658 * of raid_run_ops which services 'compute' operations
2659 * before writes. R5_Wantcompute flags a block that will
2660 * be R5_UPTODATE by the time it is needed for a
2661 * subsequent operation.
2662 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002663 s->uptodate++;
2664 return 1;
2665 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2666 /* Computing 2-failure is *very* expensive; only
2667 * do it if failed >= 2
2668 */
2669 int other;
2670 for (other = disks; other--; ) {
2671 if (other == disk_idx)
2672 continue;
2673 if (!test_bit(R5_UPTODATE,
2674 &sh->dev[other].flags))
2675 break;
2676 }
2677 BUG_ON(other < 0);
2678 pr_debug("Computing stripe %llu blocks %d,%d\n",
2679 (unsigned long long)sh->sector,
2680 disk_idx, other);
2681 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2682 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2683 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2684 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2685 sh->ops.target = disk_idx;
2686 sh->ops.target2 = other;
2687 s->uptodate += 2;
2688 s->req_compute = 1;
2689 return 1;
2690 } else if (test_bit(R5_Insync, &dev->flags)) {
2691 set_bit(R5_LOCKED, &dev->flags);
2692 set_bit(R5_Wantread, &dev->flags);
2693 s->locked++;
2694 pr_debug("Reading block %d (sync=%d)\n",
2695 disk_idx, s->syncing);
2696 }
2697 }
2698
2699 return 0;
2700}
2701
2702/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002703 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002704 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002705static void handle_stripe_fill(struct stripe_head *sh,
2706 struct stripe_head_state *s,
2707 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002708{
2709 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002710
2711 /* look for blocks to read/compute, skip this if a compute
2712 * is already in flight, or if the stripe contents are in the
2713 * midst of changing due to a write
2714 */
2715 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2716 !sh->reconstruct_state)
2717 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002718 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002719 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002720 set_bit(STRIPE_HANDLE, &sh->state);
2721}
2722
2723
Dan Williams1fe797e2008-06-28 09:16:30 +10002724/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002725 * any written block on an uptodate or failed drive can be returned.
2726 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2727 * never LOCKED, so we don't need to test 'failed' directly.
2728 */
NeilBrownd1688a62011-10-11 16:49:52 +11002729static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002730 struct stripe_head *sh, int disks, struct bio **return_bi)
2731{
2732 int i;
2733 struct r5dev *dev;
2734
2735 for (i = disks; i--; )
2736 if (sh->dev[i].written) {
2737 dev = &sh->dev[i];
2738 if (!test_bit(R5_LOCKED, &dev->flags) &&
2739 test_bit(R5_UPTODATE, &dev->flags)) {
2740 /* We can return any write requests */
2741 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002742 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002743 wbi = dev->written;
2744 dev->written = NULL;
2745 while (wbi && wbi->bi_sector <
2746 dev->sector + STRIPE_SECTORS) {
2747 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002748 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002749 md_write_end(conf->mddev);
2750 wbi->bi_next = *return_bi;
2751 *return_bi = wbi;
2752 }
2753 wbi = wbi2;
2754 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002755 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2756 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002757 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002758 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002759 }
2760 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002761
2762 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2763 if (atomic_dec_and_test(&conf->pending_full_writes))
2764 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002765}
2766
NeilBrownd1688a62011-10-11 16:49:52 +11002767static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002768 struct stripe_head *sh,
2769 struct stripe_head_state *s,
2770 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002771{
2772 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002773 if (conf->max_degraded == 2) {
2774 /* RAID6 requires 'rcw' in current implementation
2775 * Calculate the real rcw later - for now fake it
2776 * look like rcw is cheaper
2777 */
2778 rcw = 1; rmw = 2;
2779 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002780 /* would I have to read this buffer for read_modify_write */
2781 struct r5dev *dev = &sh->dev[i];
2782 if ((dev->towrite || i == sh->pd_idx) &&
2783 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002784 !(test_bit(R5_UPTODATE, &dev->flags) ||
2785 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002786 if (test_bit(R5_Insync, &dev->flags))
2787 rmw++;
2788 else
2789 rmw += 2*disks; /* cannot read it */
2790 }
2791 /* Would I have to read this buffer for reconstruct_write */
2792 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2793 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002794 !(test_bit(R5_UPTODATE, &dev->flags) ||
2795 test_bit(R5_Wantcompute, &dev->flags))) {
2796 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002797 else
2798 rcw += 2*disks;
2799 }
2800 }
Dan Williams45b42332007-07-09 11:56:43 -07002801 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002802 (unsigned long long)sh->sector, rmw, rcw);
2803 set_bit(STRIPE_HANDLE, &sh->state);
2804 if (rmw < rcw && rmw > 0)
2805 /* prefer read-modify-write, but need to get some data */
2806 for (i = disks; i--; ) {
2807 struct r5dev *dev = &sh->dev[i];
2808 if ((dev->towrite || i == sh->pd_idx) &&
2809 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002810 !(test_bit(R5_UPTODATE, &dev->flags) ||
2811 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002812 test_bit(R5_Insync, &dev->flags)) {
2813 if (
2814 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002815 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002816 "%d for r-m-w\n", i);
2817 set_bit(R5_LOCKED, &dev->flags);
2818 set_bit(R5_Wantread, &dev->flags);
2819 s->locked++;
2820 } else {
2821 set_bit(STRIPE_DELAYED, &sh->state);
2822 set_bit(STRIPE_HANDLE, &sh->state);
2823 }
2824 }
2825 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002826 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002827 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002828 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002829 for (i = disks; i--; ) {
2830 struct r5dev *dev = &sh->dev[i];
2831 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002832 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002833 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002834 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002835 test_bit(R5_Wantcompute, &dev->flags))) {
2836 rcw++;
2837 if (!test_bit(R5_Insync, &dev->flags))
2838 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002839 if (
2840 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002841 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002842 "%d for Reconstruct\n", i);
2843 set_bit(R5_LOCKED, &dev->flags);
2844 set_bit(R5_Wantread, &dev->flags);
2845 s->locked++;
2846 } else {
2847 set_bit(STRIPE_DELAYED, &sh->state);
2848 set_bit(STRIPE_HANDLE, &sh->state);
2849 }
2850 }
2851 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002852 }
Dan Williamsa4456852007-07-09 11:56:43 -07002853 /* now if nothing is locked, and if we have enough data,
2854 * we can start a write request
2855 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002856 /* since handle_stripe can be called at any time we need to handle the
2857 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002858 * subsequent call wants to start a write request. raid_run_ops only
2859 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002860 * simultaneously. If this is not the case then new writes need to be
2861 * held off until the compute completes.
2862 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002863 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2864 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2865 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002866 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002867}
2868
NeilBrownd1688a62011-10-11 16:49:52 +11002869static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002870 struct stripe_head_state *s, int disks)
2871{
Dan Williamsecc65c92008-06-28 08:31:57 +10002872 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002873
Dan Williamsbd2ab672008-04-10 21:29:27 -07002874 set_bit(STRIPE_HANDLE, &sh->state);
2875
Dan Williamsecc65c92008-06-28 08:31:57 +10002876 switch (sh->check_state) {
2877 case check_state_idle:
2878 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002879 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002880 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002881 sh->check_state = check_state_run;
2882 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002883 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002884 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002885 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002886 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002887 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002888 /* fall through */
2889 case check_state_compute_result:
2890 sh->check_state = check_state_idle;
2891 if (!dev)
2892 dev = &sh->dev[sh->pd_idx];
2893
2894 /* check that a write has not made the stripe insync */
2895 if (test_bit(STRIPE_INSYNC, &sh->state))
2896 break;
2897
2898 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002899 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2900 BUG_ON(s->uptodate != disks);
2901
2902 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002903 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002904 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002905
Dan Williamsa4456852007-07-09 11:56:43 -07002906 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002907 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002908 break;
2909 case check_state_run:
2910 break; /* we will be called again upon completion */
2911 case check_state_check_result:
2912 sh->check_state = check_state_idle;
2913
2914 /* if a failure occurred during the check operation, leave
2915 * STRIPE_INSYNC not set and let the stripe be handled again
2916 */
2917 if (s->failed)
2918 break;
2919
2920 /* handle a successful check operation, if parity is correct
2921 * we are done. Otherwise update the mismatch count and repair
2922 * parity if !MD_RECOVERY_CHECK
2923 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002924 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002925 /* parity is correct (on disc,
2926 * not in buffer any more)
2927 */
2928 set_bit(STRIPE_INSYNC, &sh->state);
2929 else {
2930 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2931 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2932 /* don't try to repair!! */
2933 set_bit(STRIPE_INSYNC, &sh->state);
2934 else {
2935 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002936 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002937 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2938 set_bit(R5_Wantcompute,
2939 &sh->dev[sh->pd_idx].flags);
2940 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002941 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002942 s->uptodate++;
2943 }
2944 }
2945 break;
2946 case check_state_compute_run:
2947 break;
2948 default:
2949 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2950 __func__, sh->check_state,
2951 (unsigned long long) sh->sector);
2952 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002953 }
2954}
2955
2956
NeilBrownd1688a62011-10-11 16:49:52 +11002957static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002958 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002959 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002960{
Dan Williamsa4456852007-07-09 11:56:43 -07002961 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002962 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002963 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002964
2965 set_bit(STRIPE_HANDLE, &sh->state);
2966
2967 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002968
Dan Williamsa4456852007-07-09 11:56:43 -07002969 /* Want to check and possibly repair P and Q.
2970 * However there could be one 'failed' device, in which
2971 * case we can only check one of them, possibly using the
2972 * other to generate missing data
2973 */
2974
Dan Williamsd82dfee2009-07-14 13:40:57 -07002975 switch (sh->check_state) {
2976 case check_state_idle:
2977 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002978 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002979 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002980 * makes sense to check P (If anything else were failed,
2981 * we would have used P to recreate it).
2982 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002983 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002984 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002985 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002986 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002987 * anything, so it makes sense to check it
2988 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002989 if (sh->check_state == check_state_run)
2990 sh->check_state = check_state_run_pq;
2991 else
2992 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002993 }
Dan Williams36d1c642009-07-14 11:48:22 -07002994
Dan Williamsd82dfee2009-07-14 13:40:57 -07002995 /* discard potentially stale zero_sum_result */
2996 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002997
Dan Williamsd82dfee2009-07-14 13:40:57 -07002998 if (sh->check_state == check_state_run) {
2999 /* async_xor_zero_sum destroys the contents of P */
3000 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3001 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003002 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003003 if (sh->check_state >= check_state_run &&
3004 sh->check_state <= check_state_run_pq) {
3005 /* async_syndrome_zero_sum preserves P and Q, so
3006 * no need to mark them !uptodate here
3007 */
3008 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3009 break;
3010 }
Dan Williams36d1c642009-07-14 11:48:22 -07003011
Dan Williamsd82dfee2009-07-14 13:40:57 -07003012 /* we have 2-disk failure */
3013 BUG_ON(s->failed != 2);
3014 /* fall through */
3015 case check_state_compute_result:
3016 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003017
Dan Williamsd82dfee2009-07-14 13:40:57 -07003018 /* check that a write has not made the stripe insync */
3019 if (test_bit(STRIPE_INSYNC, &sh->state))
3020 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003021
3022 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003023 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003024 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003025 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003026 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003027 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003028 s->locked++;
3029 set_bit(R5_LOCKED, &dev->flags);
3030 set_bit(R5_Wantwrite, &dev->flags);
3031 }
3032 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003033 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003034 s->locked++;
3035 set_bit(R5_LOCKED, &dev->flags);
3036 set_bit(R5_Wantwrite, &dev->flags);
3037 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003038 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003039 dev = &sh->dev[pd_idx];
3040 s->locked++;
3041 set_bit(R5_LOCKED, &dev->flags);
3042 set_bit(R5_Wantwrite, &dev->flags);
3043 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003044 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003045 dev = &sh->dev[qd_idx];
3046 s->locked++;
3047 set_bit(R5_LOCKED, &dev->flags);
3048 set_bit(R5_Wantwrite, &dev->flags);
3049 }
3050 clear_bit(STRIPE_DEGRADED, &sh->state);
3051
3052 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003053 break;
3054 case check_state_run:
3055 case check_state_run_q:
3056 case check_state_run_pq:
3057 break; /* we will be called again upon completion */
3058 case check_state_check_result:
3059 sh->check_state = check_state_idle;
3060
3061 /* handle a successful check operation, if parity is correct
3062 * we are done. Otherwise update the mismatch count and repair
3063 * parity if !MD_RECOVERY_CHECK
3064 */
3065 if (sh->ops.zero_sum_result == 0) {
3066 /* both parities are correct */
3067 if (!s->failed)
3068 set_bit(STRIPE_INSYNC, &sh->state);
3069 else {
3070 /* in contrast to the raid5 case we can validate
3071 * parity, but still have a failure to write
3072 * back
3073 */
3074 sh->check_state = check_state_compute_result;
3075 /* Returning at this point means that we may go
3076 * off and bring p and/or q uptodate again so
3077 * we make sure to check zero_sum_result again
3078 * to verify if p or q need writeback
3079 */
3080 }
3081 } else {
3082 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3083 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3084 /* don't try to repair!! */
3085 set_bit(STRIPE_INSYNC, &sh->state);
3086 else {
3087 int *target = &sh->ops.target;
3088
3089 sh->ops.target = -1;
3090 sh->ops.target2 = -1;
3091 sh->check_state = check_state_compute_run;
3092 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3093 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3094 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3095 set_bit(R5_Wantcompute,
3096 &sh->dev[pd_idx].flags);
3097 *target = pd_idx;
3098 target = &sh->ops.target2;
3099 s->uptodate++;
3100 }
3101 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3102 set_bit(R5_Wantcompute,
3103 &sh->dev[qd_idx].flags);
3104 *target = qd_idx;
3105 s->uptodate++;
3106 }
3107 }
3108 }
3109 break;
3110 case check_state_compute_run:
3111 break;
3112 default:
3113 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3114 __func__, sh->check_state,
3115 (unsigned long long) sh->sector);
3116 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003117 }
3118}
3119
NeilBrownd1688a62011-10-11 16:49:52 +11003120static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003121{
3122 int i;
3123
3124 /* We have read all the blocks in this stripe and now we need to
3125 * copy some of them into a target stripe for expand.
3126 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003127 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003128 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3129 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003130 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003131 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003132 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003133 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003134
NeilBrown784052e2009-03-31 15:19:07 +11003135 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003136 sector_t s = raid5_compute_sector(conf, bn, 0,
3137 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003138 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003139 if (sh2 == NULL)
3140 /* so far only the early blocks of this stripe
3141 * have been requested. When later blocks
3142 * get requested, we will try again
3143 */
3144 continue;
3145 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3146 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3147 /* must have already done this block */
3148 release_stripe(sh2);
3149 continue;
3150 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003151
3152 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003153 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003154 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003155 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003156 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003157
Dan Williamsa4456852007-07-09 11:56:43 -07003158 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3159 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3160 for (j = 0; j < conf->raid_disks; j++)
3161 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003162 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003163 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3164 break;
3165 if (j == conf->raid_disks) {
3166 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3167 set_bit(STRIPE_HANDLE, &sh2->state);
3168 }
3169 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003170
Dan Williamsa4456852007-07-09 11:56:43 -07003171 }
NeilBrowna2e08552007-09-11 15:23:36 -07003172 /* done submitting copies, wait for them to complete */
3173 if (tx) {
3174 async_tx_ack(tx);
3175 dma_wait_for_async_tx(tx);
3176 }
Dan Williamsa4456852007-07-09 11:56:43 -07003177}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
3179/*
3180 * handle_stripe - do things to a stripe.
3181 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003182 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3183 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003185 * return some read requests which now have data
3186 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 * schedule a read on some buffers
3188 * schedule a write of some buffers
3189 * return confirmation of parity correctness
3190 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 */
Dan Williamsa4456852007-07-09 11:56:43 -07003192
NeilBrownacfe7262011-07-27 11:00:36 +10003193static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003194{
NeilBrownd1688a62011-10-11 16:49:52 +11003195 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003196 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003197 struct r5dev *dev;
3198 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003199 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003200
NeilBrownacfe7262011-07-27 11:00:36 +10003201 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003202
NeilBrownacfe7262011-07-27 11:00:36 +10003203 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3204 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3205 s->failed_num[0] = -1;
3206 s->failed_num[1] = -1;
3207
3208 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003209 rcu_read_lock();
3210 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003211 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003212 sector_t first_bad;
3213 int bad_sectors;
3214 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003215
NeilBrown16a53ec2006-06-26 00:27:38 -07003216 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003217
Dan Williams45b42332007-07-09 11:56:43 -07003218 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003219 i, dev->flags,
3220 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003221 /* maybe we can reply to a read
3222 *
3223 * new wantfill requests are only permitted while
3224 * ops_complete_biofill is guaranteed to be inactive
3225 */
3226 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3227 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3228 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003229
3230 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003231 if (test_bit(R5_LOCKED, &dev->flags))
3232 s->locked++;
3233 if (test_bit(R5_UPTODATE, &dev->flags))
3234 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003235 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003236 s->compute++;
3237 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003238 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003239
NeilBrownacfe7262011-07-27 11:00:36 +10003240 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003241 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003242 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003243 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003244 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003245 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003246 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003247 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003248 }
Dan Williamsa4456852007-07-09 11:56:43 -07003249 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003250 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003251 /* Prefer to use the replacement for reads, but only
3252 * if it is recovered enough and has no bad blocks.
3253 */
3254 rdev = rcu_dereference(conf->disks[i].replacement);
3255 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3256 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3257 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3258 &first_bad, &bad_sectors))
3259 set_bit(R5_ReadRepl, &dev->flags);
3260 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003261 if (rdev)
3262 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003263 rdev = rcu_dereference(conf->disks[i].rdev);
3264 clear_bit(R5_ReadRepl, &dev->flags);
3265 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003266 if (rdev && test_bit(Faulty, &rdev->flags))
3267 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003268 if (rdev) {
3269 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3270 &first_bad, &bad_sectors);
3271 if (s->blocked_rdev == NULL
3272 && (test_bit(Blocked, &rdev->flags)
3273 || is_bad < 0)) {
3274 if (is_bad < 0)
3275 set_bit(BlockedBadBlocks,
3276 &rdev->flags);
3277 s->blocked_rdev = rdev;
3278 atomic_inc(&rdev->nr_pending);
3279 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003280 }
NeilBrown415e72d2010-06-17 17:25:21 +10003281 clear_bit(R5_Insync, &dev->flags);
3282 if (!rdev)
3283 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003284 else if (is_bad) {
3285 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003286 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3287 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003288 /* treat as in-sync, but with a read error
3289 * which we can now try to correct
3290 */
3291 set_bit(R5_Insync, &dev->flags);
3292 set_bit(R5_ReadError, &dev->flags);
3293 }
3294 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003295 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003296 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003297 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003298 set_bit(R5_Insync, &dev->flags);
3299 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3300 test_bit(R5_Expanded, &dev->flags))
3301 /* If we've reshaped into here, we assume it is Insync.
3302 * We will shortly update recovery_offset to make
3303 * it official.
3304 */
3305 set_bit(R5_Insync, &dev->flags);
3306
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003307 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003308 /* This flag does not apply to '.replacement'
3309 * only to .rdev, so make sure to check that*/
3310 struct md_rdev *rdev2 = rcu_dereference(
3311 conf->disks[i].rdev);
3312 if (rdev2 == rdev)
3313 clear_bit(R5_Insync, &dev->flags);
3314 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003315 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003316 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003317 } else
3318 clear_bit(R5_WriteError, &dev->flags);
3319 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003320 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003321 /* This flag does not apply to '.replacement'
3322 * only to .rdev, so make sure to check that*/
3323 struct md_rdev *rdev2 = rcu_dereference(
3324 conf->disks[i].rdev);
3325 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003326 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003327 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003328 } else
3329 clear_bit(R5_MadeGood, &dev->flags);
3330 }
NeilBrown977df362011-12-23 10:17:53 +11003331 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3332 struct md_rdev *rdev2 = rcu_dereference(
3333 conf->disks[i].replacement);
3334 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3335 s->handle_bad_blocks = 1;
3336 atomic_inc(&rdev2->nr_pending);
3337 } else
3338 clear_bit(R5_MadeGoodRepl, &dev->flags);
3339 }
NeilBrown415e72d2010-06-17 17:25:21 +10003340 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003341 /* The ReadError flag will just be confusing now */
3342 clear_bit(R5_ReadError, &dev->flags);
3343 clear_bit(R5_ReWrite, &dev->flags);
3344 }
NeilBrown415e72d2010-06-17 17:25:21 +10003345 if (test_bit(R5_ReadError, &dev->flags))
3346 clear_bit(R5_Insync, &dev->flags);
3347 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003348 if (s->failed < 2)
3349 s->failed_num[s->failed] = i;
3350 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003351 if (rdev && !test_bit(Faulty, &rdev->flags))
3352 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003353 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003354 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003355 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3356 /* If there is a failed device being replaced,
3357 * we must be recovering.
3358 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003359 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003360 * else we can only be replacing
3361 * sync and recovery both need to read all devices, and so
3362 * use the same flag.
3363 */
3364 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003365 sh->sector >= conf->mddev->recovery_cp ||
3366 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003367 s->syncing = 1;
3368 else
3369 s->replacing = 1;
3370 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003371 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003372}
NeilBrownf4168852007-02-28 20:11:53 -08003373
NeilBrowncc940152011-07-26 11:35:35 +10003374static void handle_stripe(struct stripe_head *sh)
3375{
3376 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003377 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003378 int i;
NeilBrown84789552011-07-27 11:00:36 +10003379 int prexor;
3380 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003381 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003382
3383 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003384 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003385 /* already being handled, ensure it gets handled
3386 * again when current action finishes */
3387 set_bit(STRIPE_HANDLE, &sh->state);
3388 return;
3389 }
3390
3391 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3392 set_bit(STRIPE_SYNCING, &sh->state);
3393 clear_bit(STRIPE_INSYNC, &sh->state);
3394 }
3395 clear_bit(STRIPE_DELAYED, &sh->state);
3396
3397 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3398 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3399 (unsigned long long)sh->sector, sh->state,
3400 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3401 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003402
NeilBrownacfe7262011-07-27 11:00:36 +10003403 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003404
NeilBrownbc2607f2011-07-28 11:39:22 +10003405 if (s.handle_bad_blocks) {
3406 set_bit(STRIPE_HANDLE, &sh->state);
3407 goto finish;
3408 }
3409
NeilBrown474af965fe2011-07-27 11:00:36 +10003410 if (unlikely(s.blocked_rdev)) {
3411 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003412 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003413 set_bit(STRIPE_HANDLE, &sh->state);
3414 goto finish;
3415 }
3416 /* There is nothing for the blocked_rdev to block */
3417 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3418 s.blocked_rdev = NULL;
3419 }
3420
3421 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3422 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3423 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3424 }
3425
3426 pr_debug("locked=%d uptodate=%d to_read=%d"
3427 " to_write=%d failed=%d failed_num=%d,%d\n",
3428 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3429 s.failed_num[0], s.failed_num[1]);
3430 /* check if the array has lost more than max_degraded devices and,
3431 * if so, some requests might need to be failed.
3432 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003433 if (s.failed > conf->max_degraded) {
3434 sh->check_state = 0;
3435 sh->reconstruct_state = 0;
3436 if (s.to_read+s.to_write+s.written)
3437 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003438 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003439 handle_failed_sync(conf, sh, &s);
3440 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003441
3442 /*
3443 * might be able to return some write requests if the parity blocks
3444 * are safe, or on a failed drive
3445 */
3446 pdev = &sh->dev[sh->pd_idx];
3447 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3448 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3449 qdev = &sh->dev[sh->qd_idx];
3450 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3451 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3452 || conf->level < 6;
3453
3454 if (s.written &&
3455 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3456 && !test_bit(R5_LOCKED, &pdev->flags)
3457 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3458 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3459 && !test_bit(R5_LOCKED, &qdev->flags)
3460 && test_bit(R5_UPTODATE, &qdev->flags)))))
3461 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3462
3463 /* Now we might consider reading some blocks, either to check/generate
3464 * parity, or to satisfy requests
3465 * or to load a block that is being partially written.
3466 */
3467 if (s.to_read || s.non_overwrite
3468 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003469 || (s.syncing && (s.uptodate + s.compute < disks))
3470 || s.replacing
3471 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003472 handle_stripe_fill(sh, &s, disks);
3473
NeilBrown84789552011-07-27 11:00:36 +10003474 /* Now we check to see if any write operations have recently
3475 * completed
3476 */
3477 prexor = 0;
3478 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3479 prexor = 1;
3480 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3481 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3482 sh->reconstruct_state = reconstruct_state_idle;
3483
3484 /* All the 'written' buffers and the parity block are ready to
3485 * be written back to disk
3486 */
3487 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3488 BUG_ON(sh->qd_idx >= 0 &&
3489 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3490 for (i = disks; i--; ) {
3491 struct r5dev *dev = &sh->dev[i];
3492 if (test_bit(R5_LOCKED, &dev->flags) &&
3493 (i == sh->pd_idx || i == sh->qd_idx ||
3494 dev->written)) {
3495 pr_debug("Writing block %d\n", i);
3496 set_bit(R5_Wantwrite, &dev->flags);
3497 if (prexor)
3498 continue;
3499 if (!test_bit(R5_Insync, &dev->flags) ||
3500 ((i == sh->pd_idx || i == sh->qd_idx) &&
3501 s.failed == 0))
3502 set_bit(STRIPE_INSYNC, &sh->state);
3503 }
3504 }
3505 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3506 s.dec_preread_active = 1;
3507 }
3508
3509 /* Now to consider new write requests and what else, if anything
3510 * should be read. We do not handle new writes when:
3511 * 1/ A 'write' operation (copy+xor) is already in flight.
3512 * 2/ A 'check' operation is in flight, as it may clobber the parity
3513 * block.
3514 */
3515 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3516 handle_stripe_dirtying(conf, sh, &s, disks);
3517
3518 /* maybe we need to check and possibly fix the parity for this stripe
3519 * Any reads will already have been scheduled, so we just see if enough
3520 * data is available. The parity check is held off while parity
3521 * dependent operations are in flight.
3522 */
3523 if (sh->check_state ||
3524 (s.syncing && s.locked == 0 &&
3525 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3526 !test_bit(STRIPE_INSYNC, &sh->state))) {
3527 if (conf->level == 6)
3528 handle_parity_checks6(conf, sh, &s, disks);
3529 else
3530 handle_parity_checks5(conf, sh, &s, disks);
3531 }
NeilBrownc5a31002011-07-27 11:00:36 +10003532
NeilBrown9a3e1102011-12-23 10:17:53 +11003533 if (s.replacing && s.locked == 0
3534 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3535 /* Write out to replacement devices where possible */
3536 for (i = 0; i < conf->raid_disks; i++)
3537 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3538 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3539 set_bit(R5_WantReplace, &sh->dev[i].flags);
3540 set_bit(R5_LOCKED, &sh->dev[i].flags);
3541 s.locked++;
3542 }
3543 set_bit(STRIPE_INSYNC, &sh->state);
3544 }
3545 if ((s.syncing || s.replacing) && s.locked == 0 &&
3546 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003547 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3548 clear_bit(STRIPE_SYNCING, &sh->state);
3549 }
3550
3551 /* If the failed drives are just a ReadError, then we might need
3552 * to progress the repair/check process
3553 */
3554 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3555 for (i = 0; i < s.failed; i++) {
3556 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3557 if (test_bit(R5_ReadError, &dev->flags)
3558 && !test_bit(R5_LOCKED, &dev->flags)
3559 && test_bit(R5_UPTODATE, &dev->flags)
3560 ) {
3561 if (!test_bit(R5_ReWrite, &dev->flags)) {
3562 set_bit(R5_Wantwrite, &dev->flags);
3563 set_bit(R5_ReWrite, &dev->flags);
3564 set_bit(R5_LOCKED, &dev->flags);
3565 s.locked++;
3566 } else {
3567 /* let's read it back */
3568 set_bit(R5_Wantread, &dev->flags);
3569 set_bit(R5_LOCKED, &dev->flags);
3570 s.locked++;
3571 }
3572 }
3573 }
3574
3575
NeilBrown3687c062011-07-27 11:00:36 +10003576 /* Finish reconstruct operations initiated by the expansion process */
3577 if (sh->reconstruct_state == reconstruct_state_result) {
3578 struct stripe_head *sh_src
3579 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3580 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3581 /* sh cannot be written until sh_src has been read.
3582 * so arrange for sh to be delayed a little
3583 */
3584 set_bit(STRIPE_DELAYED, &sh->state);
3585 set_bit(STRIPE_HANDLE, &sh->state);
3586 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3587 &sh_src->state))
3588 atomic_inc(&conf->preread_active_stripes);
3589 release_stripe(sh_src);
3590 goto finish;
3591 }
3592 if (sh_src)
3593 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003594
NeilBrown3687c062011-07-27 11:00:36 +10003595 sh->reconstruct_state = reconstruct_state_idle;
3596 clear_bit(STRIPE_EXPANDING, &sh->state);
3597 for (i = conf->raid_disks; i--; ) {
3598 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3599 set_bit(R5_LOCKED, &sh->dev[i].flags);
3600 s.locked++;
3601 }
3602 }
3603
3604 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3605 !sh->reconstruct_state) {
3606 /* Need to write out all blocks after computing parity */
3607 sh->disks = conf->raid_disks;
3608 stripe_set_idx(sh->sector, conf, 0, sh);
3609 schedule_reconstruction(sh, &s, 1, 1);
3610 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3611 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3612 atomic_dec(&conf->reshape_stripes);
3613 wake_up(&conf->wait_for_overlap);
3614 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3615 }
3616
3617 if (s.expanding && s.locked == 0 &&
3618 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3619 handle_stripe_expansion(conf, sh);
3620
3621finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003622 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003623 if (unlikely(s.blocked_rdev)) {
3624 if (conf->mddev->external)
3625 md_wait_for_blocked_rdev(s.blocked_rdev,
3626 conf->mddev);
3627 else
3628 /* Internal metadata will immediately
3629 * be written by raid5d, so we don't
3630 * need to wait here.
3631 */
3632 rdev_dec_pending(s.blocked_rdev,
3633 conf->mddev);
3634 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003635
NeilBrownbc2607f2011-07-28 11:39:22 +10003636 if (s.handle_bad_blocks)
3637 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003638 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003639 struct r5dev *dev = &sh->dev[i];
3640 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3641 /* We own a safe reference to the rdev */
3642 rdev = conf->disks[i].rdev;
3643 if (!rdev_set_badblocks(rdev, sh->sector,
3644 STRIPE_SECTORS, 0))
3645 md_error(conf->mddev, rdev);
3646 rdev_dec_pending(rdev, conf->mddev);
3647 }
NeilBrownb84db562011-07-28 11:39:23 +10003648 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3649 rdev = conf->disks[i].rdev;
3650 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003651 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003652 rdev_dec_pending(rdev, conf->mddev);
3653 }
NeilBrown977df362011-12-23 10:17:53 +11003654 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3655 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003656 if (!rdev)
3657 /* rdev have been moved down */
3658 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003659 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003660 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003661 rdev_dec_pending(rdev, conf->mddev);
3662 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003663 }
3664
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003665 if (s.ops_request)
3666 raid_run_ops(sh, s.ops_request);
3667
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003668 ops_run_io(sh, &s);
3669
NeilBrownc5709ef2011-07-26 11:35:20 +10003670 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003671 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003672 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003673 * have actually been submitted.
3674 */
3675 atomic_dec(&conf->preread_active_stripes);
3676 if (atomic_read(&conf->preread_active_stripes) <
3677 IO_THRESHOLD)
3678 md_wakeup_thread(conf->mddev->thread);
3679 }
3680
NeilBrownc5709ef2011-07-26 11:35:20 +10003681 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003682
Dan Williams257a4b42011-11-08 16:22:06 +11003683 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003684}
3685
NeilBrownd1688a62011-10-11 16:49:52 +11003686static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687{
3688 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3689 while (!list_empty(&conf->delayed_list)) {
3690 struct list_head *l = conf->delayed_list.next;
3691 struct stripe_head *sh;
3692 sh = list_entry(l, struct stripe_head, lru);
3693 list_del_init(l);
3694 clear_bit(STRIPE_DELAYED, &sh->state);
3695 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3696 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003697 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 }
NeilBrown482c0832011-04-18 18:25:42 +10003699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700}
3701
NeilBrownd1688a62011-10-11 16:49:52 +11003702static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003703{
3704 /* device_lock is held */
3705 struct list_head head;
3706 list_add(&head, &conf->bitmap_list);
3707 list_del_init(&conf->bitmap_list);
3708 while (!list_empty(&head)) {
3709 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3710 list_del_init(&sh->lru);
3711 atomic_inc(&sh->count);
3712 __release_stripe(conf, sh);
3713 }
3714}
3715
NeilBrownfd01b882011-10-11 16:47:53 +11003716int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003717{
NeilBrownd1688a62011-10-11 16:49:52 +11003718 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003719
3720 /* No difference between reads and writes. Just check
3721 * how busy the stripe_cache is
3722 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003723
NeilBrownf022b2f2006-10-03 01:15:56 -07003724 if (conf->inactive_blocked)
3725 return 1;
3726 if (conf->quiesce)
3727 return 1;
3728 if (list_empty_careful(&conf->inactive_list))
3729 return 1;
3730
3731 return 0;
3732}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003733EXPORT_SYMBOL_GPL(md_raid5_congested);
3734
3735static int raid5_congested(void *data, int bits)
3736{
NeilBrownfd01b882011-10-11 16:47:53 +11003737 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003738
3739 return mddev_congested(mddev, bits) ||
3740 md_raid5_congested(mddev, bits);
3741}
NeilBrownf022b2f2006-10-03 01:15:56 -07003742
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003743/* We want read requests to align with chunks where possible,
3744 * but write requests don't need to.
3745 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003746static int raid5_mergeable_bvec(struct request_queue *q,
3747 struct bvec_merge_data *bvm,
3748 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003749{
NeilBrownfd01b882011-10-11 16:47:53 +11003750 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003751 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003752 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003753 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003754 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003755
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003756 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003757 return biovec->bv_len; /* always allow writes to be mergeable */
3758
Andre Noll664e7c42009-06-18 08:45:27 +10003759 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3760 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003761 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3762 if (max < 0) max = 0;
3763 if (max <= biovec->bv_len && bio_sectors == 0)
3764 return biovec->bv_len;
3765 else
3766 return max;
3767}
3768
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003769
NeilBrownfd01b882011-10-11 16:47:53 +11003770static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003771{
3772 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003773 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003774 unsigned int bio_sectors = bio->bi_size >> 9;
3775
Andre Noll664e7c42009-06-18 08:45:27 +10003776 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3777 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003778 return chunk_sectors >=
3779 ((sector & (chunk_sectors - 1)) + bio_sectors);
3780}
3781
3782/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003783 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3784 * later sampled by raid5d.
3785 */
NeilBrownd1688a62011-10-11 16:49:52 +11003786static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003787{
3788 unsigned long flags;
3789
3790 spin_lock_irqsave(&conf->device_lock, flags);
3791
3792 bi->bi_next = conf->retry_read_aligned_list;
3793 conf->retry_read_aligned_list = bi;
3794
3795 spin_unlock_irqrestore(&conf->device_lock, flags);
3796 md_wakeup_thread(conf->mddev->thread);
3797}
3798
3799
NeilBrownd1688a62011-10-11 16:49:52 +11003800static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003801{
3802 struct bio *bi;
3803
3804 bi = conf->retry_read_aligned;
3805 if (bi) {
3806 conf->retry_read_aligned = NULL;
3807 return bi;
3808 }
3809 bi = conf->retry_read_aligned_list;
3810 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003811 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003812 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003813 /*
3814 * this sets the active strip count to 1 and the processed
3815 * strip count to zero (upper 8 bits)
3816 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003817 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003818 }
3819
3820 return bi;
3821}
3822
3823
3824/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003825 * The "raid5_align_endio" should check if the read succeeded and if it
3826 * did, call bio_endio on the original bio (having bio_put the new bio
3827 * first).
3828 * If the read failed..
3829 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003830static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003831{
3832 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003833 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003834 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003835 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003836 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003837
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003838 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003839
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003840 rdev = (void*)raid_bi->bi_next;
3841 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003842 mddev = rdev->mddev;
3843 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003844
3845 rdev_dec_pending(rdev, conf->mddev);
3846
3847 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003848 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003849 if (atomic_dec_and_test(&conf->active_aligned_reads))
3850 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003851 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003852 }
3853
3854
Dan Williams45b42332007-07-09 11:56:43 -07003855 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003856
3857 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003858}
3859
Neil Brown387bb172007-02-08 14:20:29 -08003860static int bio_fits_rdev(struct bio *bi)
3861{
Jens Axboe165125e2007-07-24 09:28:11 +02003862 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003863
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003864 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003865 return 0;
3866 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003867 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003868 return 0;
3869
3870 if (q->merge_bvec_fn)
3871 /* it's too hard to apply the merge_bvec_fn at this stage,
3872 * just just give up
3873 */
3874 return 0;
3875
3876 return 1;
3877}
3878
3879
NeilBrownfd01b882011-10-11 16:47:53 +11003880static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003881{
NeilBrownd1688a62011-10-11 16:49:52 +11003882 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003883 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003884 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003885 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003886 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003887
3888 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003889 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003890 return 0;
3891 }
3892 /*
NeilBrowna167f662010-10-26 18:31:13 +11003893 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003894 */
NeilBrowna167f662010-10-26 18:31:13 +11003895 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003896 if (!align_bi)
3897 return 0;
3898 /*
3899 * set bi_end_io to a new function, and set bi_private to the
3900 * original bio.
3901 */
3902 align_bi->bi_end_io = raid5_align_endio;
3903 align_bi->bi_private = raid_bio;
3904 /*
3905 * compute position
3906 */
NeilBrown112bf892009-03-31 14:39:38 +11003907 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3908 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003909 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003910
NeilBrown671488c2011-12-23 10:17:52 +11003911 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003912 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003913 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3914 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3915 rdev->recovery_offset < end_sector) {
3916 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3917 if (rdev &&
3918 (test_bit(Faulty, &rdev->flags) ||
3919 !(test_bit(In_sync, &rdev->flags) ||
3920 rdev->recovery_offset >= end_sector)))
3921 rdev = NULL;
3922 }
3923 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003924 sector_t first_bad;
3925 int bad_sectors;
3926
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003927 atomic_inc(&rdev->nr_pending);
3928 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003929 raid_bio->bi_next = (void*)rdev;
3930 align_bi->bi_bdev = rdev->bdev;
3931 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003932
NeilBrown31c176e2011-07-28 11:39:22 +10003933 if (!bio_fits_rdev(align_bi) ||
3934 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3935 &first_bad, &bad_sectors)) {
3936 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003937 bio_put(align_bi);
3938 rdev_dec_pending(rdev, mddev);
3939 return 0;
3940 }
3941
majianpeng6c0544e2012-06-12 08:31:10 +08003942 /* No reshape active, so we can trust rdev->data_offset */
3943 align_bi->bi_sector += rdev->data_offset;
3944
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003945 spin_lock_irq(&conf->device_lock);
3946 wait_event_lock_irq(conf->wait_for_stripe,
3947 conf->quiesce == 0,
3948 conf->device_lock, /* nothing */);
3949 atomic_inc(&conf->active_aligned_reads);
3950 spin_unlock_irq(&conf->device_lock);
3951
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003952 generic_make_request(align_bi);
3953 return 1;
3954 } else {
3955 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003956 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003957 return 0;
3958 }
3959}
3960
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003961/* __get_priority_stripe - get the next stripe to process
3962 *
3963 * Full stripe writes are allowed to pass preread active stripes up until
3964 * the bypass_threshold is exceeded. In general the bypass_count
3965 * increments when the handle_list is handled before the hold_list; however, it
3966 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3967 * stripe with in flight i/o. The bypass_count will be reset when the
3968 * head of the hold_list has changed, i.e. the head was promoted to the
3969 * handle_list.
3970 */
NeilBrownd1688a62011-10-11 16:49:52 +11003971static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003972{
3973 struct stripe_head *sh;
3974
3975 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3976 __func__,
3977 list_empty(&conf->handle_list) ? "empty" : "busy",
3978 list_empty(&conf->hold_list) ? "empty" : "busy",
3979 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3980
3981 if (!list_empty(&conf->handle_list)) {
3982 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3983
3984 if (list_empty(&conf->hold_list))
3985 conf->bypass_count = 0;
3986 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3987 if (conf->hold_list.next == conf->last_hold)
3988 conf->bypass_count++;
3989 else {
3990 conf->last_hold = conf->hold_list.next;
3991 conf->bypass_count -= conf->bypass_threshold;
3992 if (conf->bypass_count < 0)
3993 conf->bypass_count = 0;
3994 }
3995 }
3996 } else if (!list_empty(&conf->hold_list) &&
3997 ((conf->bypass_threshold &&
3998 conf->bypass_count > conf->bypass_threshold) ||
3999 atomic_read(&conf->pending_full_writes) == 0)) {
4000 sh = list_entry(conf->hold_list.next,
4001 typeof(*sh), lru);
4002 conf->bypass_count -= conf->bypass_threshold;
4003 if (conf->bypass_count < 0)
4004 conf->bypass_count = 0;
4005 } else
4006 return NULL;
4007
4008 list_del_init(&sh->lru);
4009 atomic_inc(&sh->count);
4010 BUG_ON(atomic_read(&sh->count) != 1);
4011 return sh;
4012}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004013
Shaohua Li8811b592012-08-02 08:33:00 +10004014struct raid5_plug_cb {
4015 struct blk_plug_cb cb;
4016 struct list_head list;
4017};
4018
4019static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4020{
4021 struct raid5_plug_cb *cb = container_of(
4022 blk_cb, struct raid5_plug_cb, cb);
4023 struct stripe_head *sh;
4024 struct mddev *mddev = cb->cb.data;
4025 struct r5conf *conf = mddev->private;
4026
4027 if (cb->list.next && !list_empty(&cb->list)) {
4028 spin_lock_irq(&conf->device_lock);
4029 while (!list_empty(&cb->list)) {
4030 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4031 list_del_init(&sh->lru);
4032 /*
4033 * avoid race release_stripe_plug() sees
4034 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4035 * is still in our list
4036 */
4037 smp_mb__before_clear_bit();
4038 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4039 __release_stripe(conf, sh);
4040 }
4041 spin_unlock_irq(&conf->device_lock);
4042 }
4043 kfree(cb);
4044}
4045
4046static void release_stripe_plug(struct mddev *mddev,
4047 struct stripe_head *sh)
4048{
4049 struct blk_plug_cb *blk_cb = blk_check_plugged(
4050 raid5_unplug, mddev,
4051 sizeof(struct raid5_plug_cb));
4052 struct raid5_plug_cb *cb;
4053
4054 if (!blk_cb) {
4055 release_stripe(sh);
4056 return;
4057 }
4058
4059 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4060
4061 if (cb->list.next == NULL)
4062 INIT_LIST_HEAD(&cb->list);
4063
4064 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4065 list_add_tail(&sh->lru, &cb->list);
4066 else
4067 release_stripe(sh);
4068}
4069
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004070static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071{
NeilBrownd1688a62011-10-11 16:49:52 +11004072 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004073 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 sector_t new_sector;
4075 sector_t logical_sector, last_sector;
4076 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004077 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004078 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
Tejun Heoe9c74692010-09-03 11:56:18 +02004080 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4081 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004082 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004083 }
4084
NeilBrown3d310eb2005-06-21 17:17:26 -07004085 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004086
NeilBrown802ba062006-12-13 00:34:13 -08004087 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004088 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004089 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004090 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004091
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4093 last_sector = bi->bi_sector + (bi->bi_size>>9);
4094 bi->bi_next = NULL;
4095 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004096
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4098 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004099 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004100
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004101 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004102 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004103 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004104 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004105 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08004106 * 64bit on a 32bit platform, and so it might be
4107 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004108 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08004109 * the lock is dropped, so once we get a reference
4110 * to the stripe that we think it is, we will have
4111 * to check again.
4112 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004113 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004114 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004115 ? logical_sector < conf->reshape_progress
4116 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004117 previous = 1;
4118 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004119 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004120 ? logical_sector < conf->reshape_safe
4121 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004122 spin_unlock_irq(&conf->device_lock);
4123 schedule();
4124 goto retry;
4125 }
4126 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004127 spin_unlock_irq(&conf->device_lock);
4128 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004129
NeilBrown112bf892009-03-31 14:39:38 +11004130 new_sector = raid5_compute_sector(conf, logical_sector,
4131 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004132 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004133 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 (unsigned long long)new_sector,
4135 (unsigned long long)logical_sector);
4136
NeilBrownb5663ba2009-03-31 14:39:38 +11004137 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004138 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004140 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004141 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08004142 * stripe, so we must do the range check again.
4143 * Expansion could still move past after this
4144 * test, but as we are holding a reference to
4145 * 'sh', we know that if that happens,
4146 * STRIPE_EXPANDING will get set and the expansion
4147 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004148 */
4149 int must_retry = 0;
4150 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004151 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004152 ? logical_sector >= conf->reshape_progress
4153 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004154 /* mismatch, need to try again */
4155 must_retry = 1;
4156 spin_unlock_irq(&conf->device_lock);
4157 if (must_retry) {
4158 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004159 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004160 goto retry;
4161 }
4162 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004163
Namhyung Kimffd96e32011-07-18 17:38:51 +10004164 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004165 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004166 logical_sector < mddev->suspend_hi) {
4167 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004168 /* As the suspend_* range is controlled by
4169 * userspace, we want an interruptible
4170 * wait.
4171 */
4172 flush_signals(current);
4173 prepare_to_wait(&conf->wait_for_overlap,
4174 &w, TASK_INTERRUPTIBLE);
4175 if (logical_sector >= mddev->suspend_lo &&
4176 logical_sector < mddev->suspend_hi)
4177 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004178 goto retry;
4179 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004180
4181 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004182 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004183 /* Stripe is busy expanding or
4184 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 * and wait a while
4186 */
NeilBrown482c0832011-04-18 18:25:42 +10004187 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 release_stripe(sh);
4189 schedule();
4190 goto retry;
4191 }
4192 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004193 set_bit(STRIPE_HANDLE, &sh->state);
4194 clear_bit(STRIPE_DELAYED, &sh->state);
majianpeng895e3c52012-07-31 10:05:44 +10004195 if ((bi->bi_rw & REQ_NOIDLE) &&
NeilBrown729a1862009-12-14 12:49:50 +11004196 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4197 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004198 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 } else {
4200 /* cannot get stripe for read-ahead, just give-up */
4201 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4202 finish_wait(&conf->wait_for_overlap, &w);
4203 break;
4204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004206
Shaohua Lie7836bd62012-07-19 16:01:31 +10004207 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004208 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
NeilBrown16a53ec2006-06-26 00:27:38 -07004210 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004212
Neil Brown0e13fe232008-06-28 08:31:20 +10004213 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215}
4216
NeilBrownfd01b882011-10-11 16:47:53 +11004217static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004218
NeilBrownfd01b882011-10-11 16:47:53 +11004219static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220{
NeilBrown52c03292006-06-26 00:27:43 -07004221 /* reshaping is quite different to recovery/resync so it is
4222 * handled quite separately ... here.
4223 *
4224 * On each call to sync_request, we gather one chunk worth of
4225 * destination stripes and flag them as expanding.
4226 * Then we find all the source stripes and request reads.
4227 * As the reads complete, handle_stripe will copy the data
4228 * into the destination stripe and release that stripe.
4229 */
NeilBrownd1688a62011-10-11 16:49:52 +11004230 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004232 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004233 int raid_disks = conf->previous_raid_disks;
4234 int data_disks = raid_disks - conf->max_degraded;
4235 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004236 int i;
4237 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004238 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004239 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004240 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004241 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004242
NeilBrownfef9c612009-03-31 15:16:46 +11004243 if (sector_nr == 0) {
4244 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004245 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004246 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4247 sector_nr = raid5_size(mddev, 0, 0)
4248 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004249 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004250 conf->reshape_progress > 0)
4251 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004252 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004253 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004254 mddev->curr_resync_completed = sector_nr;
4255 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004256 *skipped = 1;
4257 return sector_nr;
4258 }
NeilBrown52c03292006-06-26 00:27:43 -07004259 }
4260
NeilBrown7a661382009-03-31 15:21:40 +11004261 /* We need to process a full chunk at a time.
4262 * If old and new chunk sizes differ, we need to process the
4263 * largest of these
4264 */
Andre Noll664e7c42009-06-18 08:45:27 +10004265 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4266 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004267 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004268 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004269
NeilBrownb5254dd2012-05-21 09:27:01 +10004270 /* We update the metadata at least every 10 seconds, or when
4271 * the data about to be copied would over-write the source of
4272 * the data at the front of the range. i.e. one new_stripe
4273 * along from reshape_progress new_maps to after where
4274 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004275 */
NeilBrownfef9c612009-03-31 15:16:46 +11004276 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004277 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004278 readpos = conf->reshape_progress;
4279 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004280 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004281 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004282 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004283 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004284 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004285 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004286 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004287 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004288 readpos -= min_t(sector_t, reshape_sectors, readpos);
4289 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004290 }
NeilBrown52c03292006-06-26 00:27:43 -07004291
NeilBrownb5254dd2012-05-21 09:27:01 +10004292 /* Having calculated the 'writepos' possibly use it
4293 * to set 'stripe_addr' which is where we will write to.
4294 */
4295 if (mddev->reshape_backwards) {
4296 BUG_ON(conf->reshape_progress == 0);
4297 stripe_addr = writepos;
4298 BUG_ON((mddev->dev_sectors &
4299 ~((sector_t)reshape_sectors - 1))
4300 - reshape_sectors - stripe_addr
4301 != sector_nr);
4302 } else {
4303 BUG_ON(writepos != sector_nr + reshape_sectors);
4304 stripe_addr = sector_nr;
4305 }
4306
NeilBrownc8f517c2009-03-31 15:28:40 +11004307 /* 'writepos' is the most advanced device address we might write.
4308 * 'readpos' is the least advanced device address we might read.
4309 * 'safepos' is the least address recorded in the metadata as having
4310 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004311 * If there is a min_offset_diff, these are adjusted either by
4312 * increasing the safepos/readpos if diff is negative, or
4313 * increasing writepos if diff is positive.
4314 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004315 * ensure safety in the face of a crash - that must be done by userspace
4316 * making a backup of the data. So in that case there is no particular
4317 * rush to update metadata.
4318 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4319 * update the metadata to advance 'safepos' to match 'readpos' so that
4320 * we can be safe in the event of a crash.
4321 * So we insist on updating metadata if safepos is behind writepos and
4322 * readpos is beyond writepos.
4323 * In any case, update the metadata every 10 seconds.
4324 * Maybe that number should be configurable, but I'm not sure it is
4325 * worth it.... maybe it could be a multiple of safemode_delay???
4326 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004327 if (conf->min_offset_diff < 0) {
4328 safepos += -conf->min_offset_diff;
4329 readpos += -conf->min_offset_diff;
4330 } else
4331 writepos += conf->min_offset_diff;
4332
NeilBrown2c810cd2012-05-21 09:27:00 +10004333 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004334 ? (safepos > writepos && readpos < writepos)
4335 : (safepos < writepos && readpos > writepos)) ||
4336 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004337 /* Cannot proceed until we've updated the superblock... */
4338 wait_event(conf->wait_for_overlap,
4339 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004340 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004341 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004342 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b422006-10-03 01:15:46 -07004343 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004344 md_wakeup_thread(mddev->thread);
NeilBrown850b2b422006-10-03 01:15:46 -07004345 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004346 kthread_should_stop());
4347 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004348 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004349 spin_unlock_irq(&conf->device_lock);
4350 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004351 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004352 }
4353
NeilBrownab69ae12009-03-31 15:26:47 +11004354 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004355 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004356 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004357 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004358 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004359 set_bit(STRIPE_EXPANDING, &sh->state);
4360 atomic_inc(&conf->reshape_stripes);
4361 /* If any of this stripe is beyond the end of the old
4362 * array, then we need to zero those blocks
4363 */
4364 for (j=sh->disks; j--;) {
4365 sector_t s;
4366 if (j == sh->pd_idx)
4367 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004368 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004369 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004370 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004371 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004372 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004373 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004374 continue;
4375 }
4376 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4377 set_bit(R5_Expanded, &sh->dev[j].flags);
4378 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4379 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004380 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004381 set_bit(STRIPE_EXPAND_READY, &sh->state);
4382 set_bit(STRIPE_HANDLE, &sh->state);
4383 }
NeilBrownab69ae12009-03-31 15:26:47 +11004384 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004385 }
4386 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004387 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004388 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004389 else
NeilBrown7a661382009-03-31 15:21:40 +11004390 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004391 spin_unlock_irq(&conf->device_lock);
4392 /* Ok, those stripe are ready. We can start scheduling
4393 * reads on the source stripes.
4394 * The source stripes are determined by mapping the first and last
4395 * block on the destination stripes.
4396 */
NeilBrown52c03292006-06-26 00:27:43 -07004397 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004398 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004399 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004400 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004401 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004402 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004403 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004404 if (last_sector >= mddev->dev_sectors)
4405 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004406 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004407 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004408 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4409 set_bit(STRIPE_HANDLE, &sh->state);
4410 release_stripe(sh);
4411 first_sector += STRIPE_SECTORS;
4412 }
NeilBrownab69ae12009-03-31 15:26:47 +11004413 /* Now that the sources are clearly marked, we can release
4414 * the destination stripes
4415 */
4416 while (!list_empty(&stripes)) {
4417 sh = list_entry(stripes.next, struct stripe_head, lru);
4418 list_del_init(&sh->lru);
4419 release_stripe(sh);
4420 }
NeilBrownc6207272008-02-06 01:39:52 -08004421 /* If this takes us to the resync_max point where we have to pause,
4422 * then we need to write out the superblock.
4423 */
NeilBrown7a661382009-03-31 15:21:40 +11004424 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004425 if ((sector_nr - mddev->curr_resync_completed) * 2
4426 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004427 /* Cannot proceed until we've updated the superblock... */
4428 wait_event(conf->wait_for_overlap,
4429 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004430 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004431 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004432 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004433 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4434 md_wakeup_thread(mddev->thread);
4435 wait_event(mddev->sb_wait,
4436 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4437 || kthread_should_stop());
4438 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004439 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004440 spin_unlock_irq(&conf->device_lock);
4441 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004442 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004443 }
NeilBrown7a661382009-03-31 15:21:40 +11004444 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004445}
4446
4447/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004448static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004449{
NeilBrownd1688a62011-10-11 16:49:52 +11004450 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004451 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004452 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004453 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004454 int still_degraded = 0;
4455 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
NeilBrown72626682005-09-09 16:23:54 -07004457 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004459
NeilBrown29269552006-03-27 01:18:10 -08004460 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4461 end_reshape(conf);
4462 return 0;
4463 }
NeilBrown72626682005-09-09 16:23:54 -07004464
4465 if (mddev->curr_resync < max_sector) /* aborted */
4466 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4467 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004468 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004469 conf->fullsync = 0;
4470 bitmap_close_sync(mddev->bitmap);
4471
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 return 0;
4473 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004474
NeilBrown64bd6602009-08-03 10:59:58 +10004475 /* Allow raid5_quiesce to complete */
4476 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4477
NeilBrown52c03292006-06-26 00:27:43 -07004478 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4479 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004480
NeilBrownc6207272008-02-06 01:39:52 -08004481 /* No need to check resync_max as we never do more than one
4482 * stripe, and as resync_max will always be on a chunk boundary,
4483 * if the check in md_do_sync didn't fire, there is no chance
4484 * of overstepping resync_max here
4485 */
4486
NeilBrown16a53ec2006-06-26 00:27:38 -07004487 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 * to resync, then assert that we are finished, because there is
4489 * nothing we can do.
4490 */
NeilBrown3285edf2006-06-26 00:27:55 -07004491 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004492 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004493 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004494 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 return rv;
4496 }
NeilBrown72626682005-09-09 16:23:54 -07004497 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004498 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004499 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4500 /* we can skip this block, and probably more */
4501 sync_blocks /= STRIPE_SECTORS;
4502 *skipped = 1;
4503 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505
NeilBrownb47490c2008-02-06 01:39:50 -08004506 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4507
NeilBrowna8c906c2009-06-09 14:39:59 +10004508 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004510 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004512 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004514 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004516 /* Need to check if array will still be degraded after recovery/resync
4517 * We don't need to check the 'failed' flag as when that gets set,
4518 * recovery aborts.
4519 */
NeilBrownf001a702009-06-09 14:30:31 +10004520 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004521 if (conf->disks[i].rdev == NULL)
4522 still_degraded = 1;
4523
4524 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4525
NeilBrown83206d62011-07-26 11:19:49 +10004526 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
NeilBrown14425772009-10-16 15:55:25 +11004528 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 release_stripe(sh);
4530
4531 return STRIPE_SECTORS;
4532}
4533
NeilBrownd1688a62011-10-11 16:49:52 +11004534static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004535{
4536 /* We may not be able to submit a whole bio at once as there
4537 * may not be enough stripe_heads available.
4538 * We cannot pre-allocate enough stripe_heads as we may need
4539 * more than exist in the cache (if we allow ever large chunks).
4540 * So we do one stripe head at a time and record in
4541 * ->bi_hw_segments how many have been done.
4542 *
4543 * We *know* that this entire raid_bio is in one chunk, so
4544 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4545 */
4546 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004547 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004548 sector_t sector, logical_sector, last_sector;
4549 int scnt = 0;
4550 int remaining;
4551 int handled = 0;
4552
4553 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004554 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004555 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004556 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4557
4558 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004559 logical_sector += STRIPE_SECTORS,
4560 sector += STRIPE_SECTORS,
4561 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004562
Shaohua Lie7836bd62012-07-19 16:01:31 +10004563 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004564 /* already done this stripe */
4565 continue;
4566
NeilBrowna8c906c2009-06-09 14:39:59 +10004567 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004568
4569 if (!sh) {
4570 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004571 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004572 conf->retry_read_aligned = raid_bio;
4573 return handled;
4574 }
4575
Neil Brown387bb172007-02-08 14:20:29 -08004576 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4577 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004578 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004579 conf->retry_read_aligned = raid_bio;
4580 return handled;
4581 }
4582
majianpeng3f9e7c12012-07-31 10:04:21 +10004583 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004584 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004585 release_stripe(sh);
4586 handled++;
4587 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004588 remaining = raid5_dec_bi_active_stripes(raid_bio);
Neil Brown0e13fe232008-06-28 08:31:20 +10004589 if (remaining == 0)
4590 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004591 if (atomic_dec_and_test(&conf->active_aligned_reads))
4592 wake_up(&conf->wait_for_stripe);
4593 return handled;
4594}
4595
Shaohua Li46a06402012-08-02 08:33:15 +10004596#define MAX_STRIPE_BATCH 8
4597static int handle_active_stripes(struct r5conf *conf)
4598{
4599 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4600 int i, batch_size = 0;
4601
4602 while (batch_size < MAX_STRIPE_BATCH &&
4603 (sh = __get_priority_stripe(conf)) != NULL)
4604 batch[batch_size++] = sh;
4605
4606 if (batch_size == 0)
4607 return batch_size;
4608 spin_unlock_irq(&conf->device_lock);
4609
4610 for (i = 0; i < batch_size; i++)
4611 handle_stripe(batch[i]);
4612
4613 cond_resched();
4614
4615 spin_lock_irq(&conf->device_lock);
4616 for (i = 0; i < batch_size; i++)
4617 __release_stripe(conf, batch[i]);
4618 return batch_size;
4619}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004620
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621/*
4622 * This is our raid5 kernel thread.
4623 *
4624 * We scan the hash table for stripes which can be handled now.
4625 * During the scan, completed stripes are saved for us by the interrupt
4626 * handler, so that they will not have to wait for our next wakeup.
4627 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004628static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629{
Shaohua Li4ed87312012-10-11 13:34:00 +11004630 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004631 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004633 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
Dan Williams45b42332007-07-09 11:56:43 -07004635 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
4637 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004639 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 handled = 0;
4641 spin_lock_irq(&conf->device_lock);
4642 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004643 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004644 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
NeilBrown0021b7b2012-07-31 09:08:14 +02004646 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004647 !list_empty(&conf->bitmap_list)) {
4648 /* Now is a good time to flush some bitmap updates */
4649 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004650 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004651 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004652 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004653 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004654 activate_bit_delay(conf);
4655 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004656 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004657
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004658 while ((bio = remove_bio_from_retry(conf))) {
4659 int ok;
4660 spin_unlock_irq(&conf->device_lock);
4661 ok = retry_aligned_read(conf, bio);
4662 spin_lock_irq(&conf->device_lock);
4663 if (!ok)
4664 break;
4665 handled++;
4666 }
4667
Shaohua Li46a06402012-08-02 08:33:15 +10004668 batch_size = handle_active_stripes(conf);
4669 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004671 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
Shaohua Li46a06402012-08-02 08:33:15 +10004673 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4674 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004675 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004676 spin_lock_irq(&conf->device_lock);
4677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 }
Dan Williams45b42332007-07-09 11:56:43 -07004679 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
4681 spin_unlock_irq(&conf->device_lock);
4682
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004683 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004684 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
Dan Williams45b42332007-07-09 11:56:43 -07004686 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687}
4688
NeilBrown3f294f42005-11-08 21:39:25 -08004689static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004690raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004691{
NeilBrownd1688a62011-10-11 16:49:52 +11004692 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004693 if (conf)
4694 return sprintf(page, "%d\n", conf->max_nr_stripes);
4695 else
4696 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004697}
4698
NeilBrownc41d4ac2010-06-01 19:37:24 +10004699int
NeilBrownfd01b882011-10-11 16:47:53 +11004700raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004701{
NeilBrownd1688a62011-10-11 16:49:52 +11004702 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004703 int err;
4704
4705 if (size <= 16 || size > 32768)
4706 return -EINVAL;
4707 while (size < conf->max_nr_stripes) {
4708 if (drop_one_stripe(conf))
4709 conf->max_nr_stripes--;
4710 else
4711 break;
4712 }
4713 err = md_allow_write(mddev);
4714 if (err)
4715 return err;
4716 while (size > conf->max_nr_stripes) {
4717 if (grow_one_stripe(conf))
4718 conf->max_nr_stripes++;
4719 else break;
4720 }
4721 return 0;
4722}
4723EXPORT_SYMBOL(raid5_set_cache_size);
4724
NeilBrown3f294f42005-11-08 21:39:25 -08004725static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004726raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004727{
NeilBrownd1688a62011-10-11 16:49:52 +11004728 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004729 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004730 int err;
4731
NeilBrown3f294f42005-11-08 21:39:25 -08004732 if (len >= PAGE_SIZE)
4733 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004734 if (!conf)
4735 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004736
Dan Williams4ef197d82008-04-28 02:15:54 -07004737 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004738 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004739 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004740 if (err)
4741 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004742 return len;
4743}
NeilBrown007583c2005-11-08 21:39:30 -08004744
NeilBrown96de1e62005-11-08 21:39:39 -08004745static struct md_sysfs_entry
4746raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4747 raid5_show_stripe_cache_size,
4748 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004749
4750static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004751raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004752{
NeilBrownd1688a62011-10-11 16:49:52 +11004753 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004754 if (conf)
4755 return sprintf(page, "%d\n", conf->bypass_threshold);
4756 else
4757 return 0;
4758}
4759
4760static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004761raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004762{
NeilBrownd1688a62011-10-11 16:49:52 +11004763 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004764 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004765 if (len >= PAGE_SIZE)
4766 return -EINVAL;
4767 if (!conf)
4768 return -ENODEV;
4769
Dan Williams4ef197d82008-04-28 02:15:54 -07004770 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004771 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004772 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004773 return -EINVAL;
4774 conf->bypass_threshold = new;
4775 return len;
4776}
4777
4778static struct md_sysfs_entry
4779raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4780 S_IRUGO | S_IWUSR,
4781 raid5_show_preread_threshold,
4782 raid5_store_preread_threshold);
4783
4784static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004785stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004786{
NeilBrownd1688a62011-10-11 16:49:52 +11004787 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004788 if (conf)
4789 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4790 else
4791 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004792}
4793
NeilBrown96de1e62005-11-08 21:39:39 -08004794static struct md_sysfs_entry
4795raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004796
NeilBrown007583c2005-11-08 21:39:30 -08004797static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004798 &raid5_stripecache_size.attr,
4799 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004800 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004801 NULL,
4802};
NeilBrown007583c2005-11-08 21:39:30 -08004803static struct attribute_group raid5_attrs_group = {
4804 .name = NULL,
4805 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004806};
4807
Dan Williams80c3a6c2009-03-17 18:10:40 -07004808static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004809raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004810{
NeilBrownd1688a62011-10-11 16:49:52 +11004811 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004812
4813 if (!sectors)
4814 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004815 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004816 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004817 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004818
Andre Noll9d8f0362009-06-18 08:45:01 +10004819 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004820 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004821 return sectors * (raid_disks - conf->max_degraded);
4822}
4823
NeilBrownd1688a62011-10-11 16:49:52 +11004824static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004825{
4826 struct raid5_percpu *percpu;
4827 unsigned long cpu;
4828
4829 if (!conf->percpu)
4830 return;
4831
4832 get_online_cpus();
4833 for_each_possible_cpu(cpu) {
4834 percpu = per_cpu_ptr(conf->percpu, cpu);
4835 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004836 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004837 }
4838#ifdef CONFIG_HOTPLUG_CPU
4839 unregister_cpu_notifier(&conf->cpu_notify);
4840#endif
4841 put_online_cpus();
4842
4843 free_percpu(conf->percpu);
4844}
4845
NeilBrownd1688a62011-10-11 16:49:52 +11004846static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004847{
4848 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004849 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004850 kfree(conf->disks);
4851 kfree(conf->stripe_hashtbl);
4852 kfree(conf);
4853}
4854
Dan Williams36d1c642009-07-14 11:48:22 -07004855#ifdef CONFIG_HOTPLUG_CPU
4856static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4857 void *hcpu)
4858{
NeilBrownd1688a62011-10-11 16:49:52 +11004859 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004860 long cpu = (long)hcpu;
4861 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4862
4863 switch (action) {
4864 case CPU_UP_PREPARE:
4865 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004866 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004867 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004868 if (!percpu->scribble)
4869 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4870
4871 if (!percpu->scribble ||
4872 (conf->level == 6 && !percpu->spare_page)) {
4873 safe_put_page(percpu->spare_page);
4874 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004875 pr_err("%s: failed memory allocation for cpu%ld\n",
4876 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004877 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004878 }
4879 break;
4880 case CPU_DEAD:
4881 case CPU_DEAD_FROZEN:
4882 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004883 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004884 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004885 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004886 break;
4887 default:
4888 break;
4889 }
4890 return NOTIFY_OK;
4891}
4892#endif
4893
NeilBrownd1688a62011-10-11 16:49:52 +11004894static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004895{
4896 unsigned long cpu;
4897 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004898 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004899 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004900 int err;
4901
Dan Williams36d1c642009-07-14 11:48:22 -07004902 allcpus = alloc_percpu(struct raid5_percpu);
4903 if (!allcpus)
4904 return -ENOMEM;
4905 conf->percpu = allcpus;
4906
4907 get_online_cpus();
4908 err = 0;
4909 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004910 if (conf->level == 6) {
4911 spare_page = alloc_page(GFP_KERNEL);
4912 if (!spare_page) {
4913 err = -ENOMEM;
4914 break;
4915 }
4916 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4917 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004918 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004919 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004920 err = -ENOMEM;
4921 break;
4922 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004923 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004924 }
4925#ifdef CONFIG_HOTPLUG_CPU
4926 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4927 conf->cpu_notify.priority = 0;
4928 if (err == 0)
4929 err = register_cpu_notifier(&conf->cpu_notify);
4930#endif
4931 put_online_cpus();
4932
4933 return err;
4934}
4935
NeilBrownd1688a62011-10-11 16:49:52 +11004936static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937{
NeilBrownd1688a62011-10-11 16:49:52 +11004938 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004939 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004940 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10004942 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
NeilBrown91adb562009-03-31 14:39:39 +11004944 if (mddev->new_level != 5
4945 && mddev->new_level != 4
4946 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004947 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004948 mdname(mddev), mddev->new_level);
4949 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 }
NeilBrown91adb562009-03-31 14:39:39 +11004951 if ((mddev->new_level == 5
4952 && !algorithm_valid_raid5(mddev->new_layout)) ||
4953 (mddev->new_level == 6
4954 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004955 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004956 mdname(mddev), mddev->new_layout);
4957 return ERR_PTR(-EIO);
4958 }
4959 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004960 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004961 mdname(mddev), mddev->raid_disks);
4962 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964
Andre Noll664e7c42009-06-18 08:45:27 +10004965 if (!mddev->new_chunk_sectors ||
4966 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4967 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004968 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4969 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004970 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004971 }
4972
NeilBrownd1688a62011-10-11 16:49:52 +11004973 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004974 if (conf == NULL)
4975 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004976 spin_lock_init(&conf->device_lock);
4977 init_waitqueue_head(&conf->wait_for_stripe);
4978 init_waitqueue_head(&conf->wait_for_overlap);
4979 INIT_LIST_HEAD(&conf->handle_list);
4980 INIT_LIST_HEAD(&conf->hold_list);
4981 INIT_LIST_HEAD(&conf->delayed_list);
4982 INIT_LIST_HEAD(&conf->bitmap_list);
4983 INIT_LIST_HEAD(&conf->inactive_list);
4984 atomic_set(&conf->active_stripes, 0);
4985 atomic_set(&conf->preread_active_stripes, 0);
4986 atomic_set(&conf->active_aligned_reads, 0);
4987 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004988 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004989
4990 conf->raid_disks = mddev->raid_disks;
4991 if (mddev->reshape_position == MaxSector)
4992 conf->previous_raid_disks = mddev->raid_disks;
4993 else
4994 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004995 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4996 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004997
NeilBrown5e5e3e72009-10-16 16:35:30 +11004998 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004999 GFP_KERNEL);
5000 if (!conf->disks)
5001 goto abort;
5002
5003 conf->mddev = mddev;
5004
5005 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5006 goto abort;
5007
Dan Williams36d1c642009-07-14 11:48:22 -07005008 conf->level = mddev->new_level;
5009 if (raid5_alloc_percpu(conf) != 0)
5010 goto abort;
5011
NeilBrown0c55e022010-05-03 14:09:02 +10005012 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005013
NeilBrowndafb20f2012-03-19 12:46:39 +11005014 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005015 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005016 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005017 || raid_disk < 0)
5018 continue;
5019 disk = conf->disks + raid_disk;
5020
NeilBrown17045f52011-12-23 10:17:53 +11005021 if (test_bit(Replacement, &rdev->flags)) {
5022 if (disk->replacement)
5023 goto abort;
5024 disk->replacement = rdev;
5025 } else {
5026 if (disk->rdev)
5027 goto abort;
5028 disk->rdev = rdev;
5029 }
NeilBrown91adb562009-03-31 14:39:39 +11005030
5031 if (test_bit(In_sync, &rdev->flags)) {
5032 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005033 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5034 " disk %d\n",
5035 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005036 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005037 /* Cannot rely on bitmap to complete recovery */
5038 conf->fullsync = 1;
5039 }
5040
Andre Noll09c9e5f2009-06-18 08:45:55 +10005041 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005042 conf->level = mddev->new_level;
5043 if (conf->level == 6)
5044 conf->max_degraded = 2;
5045 else
5046 conf->max_degraded = 1;
5047 conf->algorithm = mddev->new_layout;
5048 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005049 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005050 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005051 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005052 conf->prev_algo = mddev->layout;
5053 }
NeilBrown91adb562009-03-31 14:39:39 +11005054
5055 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005056 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005057 if (grow_stripes(conf, conf->max_nr_stripes)) {
5058 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005059 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5060 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005061 goto abort;
5062 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005063 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5064 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005065
NeilBrown02326052012-07-03 15:56:52 +10005066 sprintf(pers_name, "raid%d", mddev->new_level);
5067 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005068 if (!conf->thread) {
5069 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005070 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005071 mdname(mddev));
5072 goto abort;
5073 }
5074
5075 return conf;
5076
5077 abort:
5078 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005079 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005080 return ERR_PTR(-EIO);
5081 } else
5082 return ERR_PTR(-ENOMEM);
5083}
5084
NeilBrownc148ffd2009-11-13 17:47:00 +11005085
5086static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5087{
5088 switch (algo) {
5089 case ALGORITHM_PARITY_0:
5090 if (raid_disk < max_degraded)
5091 return 1;
5092 break;
5093 case ALGORITHM_PARITY_N:
5094 if (raid_disk >= raid_disks - max_degraded)
5095 return 1;
5096 break;
5097 case ALGORITHM_PARITY_0_6:
5098 if (raid_disk == 0 ||
5099 raid_disk == raid_disks - 1)
5100 return 1;
5101 break;
5102 case ALGORITHM_LEFT_ASYMMETRIC_6:
5103 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5104 case ALGORITHM_LEFT_SYMMETRIC_6:
5105 case ALGORITHM_RIGHT_SYMMETRIC_6:
5106 if (raid_disk == raid_disks - 1)
5107 return 1;
5108 }
5109 return 0;
5110}
5111
NeilBrownfd01b882011-10-11 16:47:53 +11005112static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005113{
NeilBrownd1688a62011-10-11 16:49:52 +11005114 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005115 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005116 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005117 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005118 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005119 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005120 long long min_offset_diff = 0;
5121 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005122
Andre Noll8c6ac8682009-06-18 08:48:06 +10005123 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005124 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac8682009-06-18 08:48:06 +10005125 " -- starting background reconstruction\n",
5126 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005127
5128 rdev_for_each(rdev, mddev) {
5129 long long diff;
5130 if (rdev->raid_disk < 0)
5131 continue;
5132 diff = (rdev->new_data_offset - rdev->data_offset);
5133 if (first) {
5134 min_offset_diff = diff;
5135 first = 0;
5136 } else if (mddev->reshape_backwards &&
5137 diff < min_offset_diff)
5138 min_offset_diff = diff;
5139 else if (!mddev->reshape_backwards &&
5140 diff > min_offset_diff)
5141 min_offset_diff = diff;
5142 }
5143
NeilBrownf6705572006-03-27 01:18:11 -08005144 if (mddev->reshape_position != MaxSector) {
5145 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005146 * Difficulties arise if the stripe we would write to
5147 * next is at or after the stripe we would read from next.
5148 * For a reshape that changes the number of devices, this
5149 * is only possible for a very short time, and mdadm makes
5150 * sure that time appears to have past before assembling
5151 * the array. So we fail if that time hasn't passed.
5152 * For a reshape that keeps the number of devices the same
5153 * mdadm must be monitoring the reshape can keeping the
5154 * critical areas read-only and backed up. It will start
5155 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005156 */
5157 sector_t here_new, here_old;
5158 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005159 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005160
NeilBrown88ce4932009-03-31 15:24:23 +11005161 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005162 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005163 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005164 mdname(mddev));
5165 return -EINVAL;
5166 }
NeilBrownf6705572006-03-27 01:18:11 -08005167 old_disks = mddev->raid_disks - mddev->delta_disks;
5168 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005169 * further up in new geometry must map after here in old
5170 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005171 */
5172 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005173 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005174 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005175 printk(KERN_ERR "md/raid:%s: reshape_position not "
5176 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005177 return -EINVAL;
5178 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005179 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005180 /* here_new is the stripe we will write to */
5181 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005182 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005183 (old_disks-max_degraded));
5184 /* here_old is the first stripe that we might need to read
5185 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005186 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005187 if ((here_new * mddev->new_chunk_sectors !=
5188 here_old * mddev->chunk_sectors)) {
5189 printk(KERN_ERR "md/raid:%s: reshape position is"
5190 " confused - aborting\n", mdname(mddev));
5191 return -EINVAL;
5192 }
NeilBrown67ac6012009-08-13 10:06:24 +10005193 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005194 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005195 * and taking constant backups.
5196 * mdadm always starts a situation like this in
5197 * readonly mode so it can take control before
5198 * allowing any writes. So just check for that.
5199 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005200 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5201 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5202 /* not really in-place - so OK */;
5203 else if (mddev->ro == 0) {
5204 printk(KERN_ERR "md/raid:%s: in-place reshape "
5205 "must be started in read-only mode "
5206 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005207 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005208 return -EINVAL;
5209 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005210 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005211 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005212 here_old * mddev->chunk_sectors)
5213 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005214 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005215 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005216 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5217 "auto-recovery - aborting.\n",
5218 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005219 return -EINVAL;
5220 }
NeilBrown0c55e022010-05-03 14:09:02 +10005221 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5222 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005223 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005224 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005225 BUG_ON(mddev->level != mddev->new_level);
5226 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005227 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005228 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005229 }
5230
NeilBrown245f46c2009-03-31 14:39:39 +11005231 if (mddev->private == NULL)
5232 conf = setup_conf(mddev);
5233 else
5234 conf = mddev->private;
5235
NeilBrown91adb562009-03-31 14:39:39 +11005236 if (IS_ERR(conf))
5237 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005238
NeilBrownb5254dd2012-05-21 09:27:01 +10005239 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005240 mddev->thread = conf->thread;
5241 conf->thread = NULL;
5242 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243
NeilBrown17045f52011-12-23 10:17:53 +11005244 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5245 i++) {
5246 rdev = conf->disks[i].rdev;
5247 if (!rdev && conf->disks[i].replacement) {
5248 /* The replacement is all we have yet */
5249 rdev = conf->disks[i].replacement;
5250 conf->disks[i].replacement = NULL;
5251 clear_bit(Replacement, &rdev->flags);
5252 conf->disks[i].rdev = rdev;
5253 }
5254 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005255 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005256 if (conf->disks[i].replacement &&
5257 conf->reshape_progress != MaxSector) {
5258 /* replacements and reshape simply do not mix. */
5259 printk(KERN_ERR "md: cannot handle concurrent "
5260 "replacement and reshape.\n");
5261 goto abort;
5262 }
NeilBrown2f115882010-06-17 17:41:03 +10005263 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005264 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005265 continue;
5266 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005267 /* This disc is not fully in-sync. However if it
5268 * just stored parity (beyond the recovery_offset),
5269 * when we don't need to be concerned about the
5270 * array being dirty.
5271 * When reshape goes 'backwards', we never have
5272 * partially completed devices, so we only need
5273 * to worry about reshape going forwards.
5274 */
5275 /* Hack because v0.91 doesn't store recovery_offset properly. */
5276 if (mddev->major_version == 0 &&
5277 mddev->minor_version > 90)
5278 rdev->recovery_offset = reshape_offset;
5279
NeilBrownc148ffd2009-11-13 17:47:00 +11005280 if (rdev->recovery_offset < reshape_offset) {
5281 /* We need to check old and new layout */
5282 if (!only_parity(rdev->raid_disk,
5283 conf->algorithm,
5284 conf->raid_disks,
5285 conf->max_degraded))
5286 continue;
5287 }
5288 if (!only_parity(rdev->raid_disk,
5289 conf->prev_algo,
5290 conf->previous_raid_disks,
5291 conf->max_degraded))
5292 continue;
5293 dirty_parity_disks++;
5294 }
NeilBrown91adb562009-03-31 14:39:39 +11005295
NeilBrown17045f52011-12-23 10:17:53 +11005296 /*
5297 * 0 for a fully functional array, 1 or 2 for a degraded array.
5298 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005299 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300
NeilBrown674806d2010-06-16 17:17:53 +10005301 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005302 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005304 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 goto abort;
5306 }
5307
NeilBrown91adb562009-03-31 14:39:39 +11005308 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005309 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005310 mddev->resync_max_sectors = mddev->dev_sectors;
5311
NeilBrownc148ffd2009-11-13 17:47:00 +11005312 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005314 if (mddev->ok_start_degraded)
5315 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005316 "md/raid:%s: starting dirty degraded array"
5317 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005318 mdname(mddev));
5319 else {
5320 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005321 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005322 mdname(mddev));
5323 goto abort;
5324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 }
5326
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005328 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5329 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005330 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5331 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 else
NeilBrown0c55e022010-05-03 14:09:02 +10005333 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5334 " out of %d devices, algorithm %d\n",
5335 mdname(mddev), conf->level,
5336 mddev->raid_disks - mddev->degraded,
5337 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
5339 print_raid5_conf(conf);
5340
NeilBrownfef9c612009-03-31 15:16:46 +11005341 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005342 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005343 atomic_set(&conf->reshape_stripes, 0);
5344 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5345 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5346 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5347 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5348 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005349 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005350 }
5351
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
5353 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005354 if (mddev->to_remove == &raid5_attrs_group)
5355 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005356 else if (mddev->kobj.sd &&
5357 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005358 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005359 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005360 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005361 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5362
5363 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005364 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005365 /* read-ahead size must cover two whole stripes, which
5366 * is 2 * (datadisks) * chunksize where 'n' is the
5367 * number of raid devices
5368 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5370 int stripe = data_disks *
5371 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5372 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5373 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005374
5375 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005376
5377 mddev->queue->backing_dev_info.congested_data = mddev;
5378 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005379
5380 chunk_size = mddev->chunk_sectors << 9;
5381 blk_queue_io_min(mddev->queue, chunk_size);
5382 blk_queue_io_opt(mddev->queue, chunk_size *
5383 (conf->raid_disks - conf->max_degraded));
5384
NeilBrown05616be2012-05-21 09:27:00 +10005385 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005386 disk_stack_limits(mddev->gendisk, rdev->bdev,
5387 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005388 disk_stack_limits(mddev->gendisk, rdev->bdev,
5389 rdev->new_data_offset << 9);
5390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 }
5392
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 return 0;
5394abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005395 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005396 print_raid5_conf(conf);
5397 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005399 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 return -EIO;
5401}
5402
NeilBrownfd01b882011-10-11 16:47:53 +11005403static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404{
NeilBrownd1688a62011-10-11 16:49:52 +11005405 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
NeilBrown01f96c02011-09-21 15:30:20 +10005407 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005408 if (mddev->queue)
5409 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005410 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005411 mddev->private = NULL;
5412 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 return 0;
5414}
5415
NeilBrownfd01b882011-10-11 16:47:53 +11005416static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417{
NeilBrownd1688a62011-10-11 16:49:52 +11005418 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 int i;
5420
Andre Noll9d8f0362009-06-18 08:45:01 +10005421 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5422 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005423 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 for (i = 0; i < conf->raid_disks; i++)
5425 seq_printf (seq, "%s",
5426 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005427 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429}
5430
NeilBrownd1688a62011-10-11 16:49:52 +11005431static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432{
5433 int i;
5434 struct disk_info *tmp;
5435
NeilBrown0c55e022010-05-03 14:09:02 +10005436 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 if (!conf) {
5438 printk("(conf==NULL)\n");
5439 return;
5440 }
NeilBrown0c55e022010-05-03 14:09:02 +10005441 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5442 conf->raid_disks,
5443 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
5445 for (i = 0; i < conf->raid_disks; i++) {
5446 char b[BDEVNAME_SIZE];
5447 tmp = conf->disks + i;
5448 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005449 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5450 i, !test_bit(Faulty, &tmp->rdev->flags),
5451 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 }
5453}
5454
NeilBrownfd01b882011-10-11 16:47:53 +11005455static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456{
5457 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005458 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005460 int count = 0;
5461 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
5463 for (i = 0; i < conf->raid_disks; i++) {
5464 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005465 if (tmp->replacement
5466 && tmp->replacement->recovery_offset == MaxSector
5467 && !test_bit(Faulty, &tmp->replacement->flags)
5468 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5469 /* Replacement has just become active. */
5470 if (!tmp->rdev
5471 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5472 count++;
5473 if (tmp->rdev) {
5474 /* Replaced device not technically faulty,
5475 * but we need to be sure it gets removed
5476 * and never re-added.
5477 */
5478 set_bit(Faulty, &tmp->rdev->flags);
5479 sysfs_notify_dirent_safe(
5480 tmp->rdev->sysfs_state);
5481 }
5482 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5483 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005484 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005485 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005486 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005487 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005488 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 }
5490 }
NeilBrown6b965622010-08-18 11:56:59 +10005491 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005492 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005493 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005495 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496}
5497
NeilBrownb8321b62011-12-23 10:17:51 +11005498static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499{
NeilBrownd1688a62011-10-11 16:49:52 +11005500 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005502 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005503 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 struct disk_info *p = conf->disks + number;
5505
5506 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005507 if (rdev == p->rdev)
5508 rdevp = &p->rdev;
5509 else if (rdev == p->replacement)
5510 rdevp = &p->replacement;
5511 else
5512 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005513
NeilBrown657e3e42011-12-23 10:17:52 +11005514 if (number >= conf->raid_disks &&
5515 conf->reshape_progress == MaxSector)
5516 clear_bit(In_sync, &rdev->flags);
5517
5518 if (test_bit(In_sync, &rdev->flags) ||
5519 atomic_read(&rdev->nr_pending)) {
5520 err = -EBUSY;
5521 goto abort;
5522 }
5523 /* Only remove non-faulty devices if recovery
5524 * isn't possible.
5525 */
5526 if (!test_bit(Faulty, &rdev->flags) &&
5527 mddev->recovery_disabled != conf->recovery_disabled &&
5528 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005529 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005530 number < conf->raid_disks) {
5531 err = -EBUSY;
5532 goto abort;
5533 }
5534 *rdevp = NULL;
5535 synchronize_rcu();
5536 if (atomic_read(&rdev->nr_pending)) {
5537 /* lost the race, try later */
5538 err = -EBUSY;
5539 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005540 } else if (p->replacement) {
5541 /* We must have just cleared 'rdev' */
5542 p->rdev = p->replacement;
5543 clear_bit(Replacement, &p->replacement->flags);
5544 smp_mb(); /* Make sure other CPUs may see both as identical
5545 * but will never see neither - if they are careful
5546 */
5547 p->replacement = NULL;
5548 clear_bit(WantReplacement, &rdev->flags);
5549 } else
5550 /* We might have just removed the Replacement as faulty-
5551 * clear the bit just in case
5552 */
5553 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554abort:
5555
5556 print_raid5_conf(conf);
5557 return err;
5558}
5559
NeilBrownfd01b882011-10-11 16:47:53 +11005560static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561{
NeilBrownd1688a62011-10-11 16:49:52 +11005562 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005563 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 int disk;
5565 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005566 int first = 0;
5567 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568
NeilBrown7f0da592011-07-28 11:39:22 +10005569 if (mddev->recovery_disabled == conf->recovery_disabled)
5570 return -EBUSY;
5571
NeilBrowndc10c642012-03-19 12:46:37 +11005572 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005574 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
Neil Brown6c2fce22008-06-28 08:31:31 +10005576 if (rdev->raid_disk >= 0)
5577 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578
5579 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005580 * find the disk ... but prefer rdev->saved_raid_disk
5581 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005583 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005584 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005585 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005586 first = rdev->saved_raid_disk;
5587
5588 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005589 p = conf->disks + disk;
5590 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005591 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005593 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005594 if (rdev->saved_raid_disk != disk)
5595 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005596 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005597 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005599 }
5600 for (disk = first; disk <= last; disk++) {
5601 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005602 if (test_bit(WantReplacement, &p->rdev->flags) &&
5603 p->replacement == NULL) {
5604 clear_bit(In_sync, &rdev->flags);
5605 set_bit(Replacement, &rdev->flags);
5606 rdev->raid_disk = disk;
5607 err = 0;
5608 conf->fullsync = 1;
5609 rcu_assign_pointer(p->replacement, rdev);
5610 break;
5611 }
5612 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005613out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005615 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616}
5617
NeilBrownfd01b882011-10-11 16:47:53 +11005618static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619{
5620 /* no resync is happening, and there is enough space
5621 * on all devices, so we can resize.
5622 * We need to make sure resync covers any new space.
5623 * If the array is shrinking we should possibly wait until
5624 * any io in the removed space completes, but it hardly seems
5625 * worth it.
5626 */
NeilBrowna4a61252012-05-22 13:55:27 +10005627 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005628 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005629 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5630 if (mddev->external_size &&
5631 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005632 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005633 if (mddev->bitmap) {
5634 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5635 if (ret)
5636 return ret;
5637 }
5638 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005639 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005640 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005641 if (sectors > mddev->dev_sectors &&
5642 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005643 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5645 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005646 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005647 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 return 0;
5649}
5650
NeilBrownfd01b882011-10-11 16:47:53 +11005651static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005652{
5653 /* Can only proceed if there are plenty of stripe_heads.
5654 * We need a minimum of one full stripe,, and for sensible progress
5655 * it is best to have about 4 times that.
5656 * If we require 4 times, then the default 256 4K stripe_heads will
5657 * allow for chunk sizes up to 256K, which is probably OK.
5658 * If the chunk size is greater, user-space should request more
5659 * stripe_heads first.
5660 */
NeilBrownd1688a62011-10-11 16:49:52 +11005661 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005662 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5663 > conf->max_nr_stripes ||
5664 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5665 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005666 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5667 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005668 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5669 / STRIPE_SIZE)*4);
5670 return 0;
5671 }
5672 return 1;
5673}
5674
NeilBrownfd01b882011-10-11 16:47:53 +11005675static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005676{
NeilBrownd1688a62011-10-11 16:49:52 +11005677 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005678
NeilBrown88ce4932009-03-31 15:24:23 +11005679 if (mddev->delta_disks == 0 &&
5680 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005681 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005682 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005683 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005684 return -EINVAL;
5685 if (mddev->delta_disks < 0) {
5686 /* We might be able to shrink, but the devices must
5687 * be made bigger first.
5688 * For raid6, 4 is the minimum size.
5689 * Otherwise 2 is the minimum
5690 */
5691 int min = 2;
5692 if (mddev->level == 6)
5693 min = 4;
5694 if (mddev->raid_disks + mddev->delta_disks < min)
5695 return -EINVAL;
5696 }
NeilBrown29269552006-03-27 01:18:10 -08005697
NeilBrown01ee22b2009-06-18 08:47:20 +10005698 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005699 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005700
NeilBrownec32a2b2009-03-31 15:17:38 +11005701 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005702}
5703
NeilBrownfd01b882011-10-11 16:47:53 +11005704static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005705{
NeilBrownd1688a62011-10-11 16:49:52 +11005706 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005707 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005708 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005709 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005710
NeilBrownf4168852007-02-28 20:11:53 -08005711 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005712 return -EBUSY;
5713
NeilBrown01ee22b2009-06-18 08:47:20 +10005714 if (!check_stripe_cache(mddev))
5715 return -ENOSPC;
5716
NeilBrown30b67642012-05-22 13:55:28 +10005717 if (has_failed(conf))
5718 return -EINVAL;
5719
NeilBrownc6563a82012-05-21 09:27:00 +10005720 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005721 if (!test_bit(In_sync, &rdev->flags)
5722 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005723 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005724 }
NeilBrown63c70c42006-03-27 01:18:13 -08005725
NeilBrownf4168852007-02-28 20:11:53 -08005726 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005727 /* Not enough devices even to make a degraded array
5728 * of that size
5729 */
5730 return -EINVAL;
5731
NeilBrownec32a2b2009-03-31 15:17:38 +11005732 /* Refuse to reduce size of the array. Any reductions in
5733 * array size must be through explicit setting of array_size
5734 * attribute.
5735 */
5736 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5737 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005738 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005739 "before number of disks\n", mdname(mddev));
5740 return -EINVAL;
5741 }
5742
NeilBrownf6705572006-03-27 01:18:11 -08005743 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005744 spin_lock_irq(&conf->device_lock);
5745 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005746 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005747 conf->prev_chunk_sectors = conf->chunk_sectors;
5748 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005749 conf->prev_algo = conf->algorithm;
5750 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005751 conf->generation++;
5752 /* Code that selects data_offset needs to see the generation update
5753 * if reshape_progress has been set - so a memory barrier needed.
5754 */
5755 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005756 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005757 conf->reshape_progress = raid5_size(mddev, 0, 0);
5758 else
5759 conf->reshape_progress = 0;
5760 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005761 spin_unlock_irq(&conf->device_lock);
5762
5763 /* Add some new drives, as many as will fit.
5764 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005765 * Don't add devices if we are reducing the number of
5766 * devices in the array. This is because it is not possible
5767 * to correctly record the "partially reconstructed" state of
5768 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005769 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005770 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005771 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005772 if (rdev->raid_disk < 0 &&
5773 !test_bit(Faulty, &rdev->flags)) {
5774 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005775 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005776 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005777 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005778 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005779 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005780
5781 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005782 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005783 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005784 } else if (rdev->raid_disk >= conf->previous_raid_disks
5785 && !test_bit(Faulty, &rdev->flags)) {
5786 /* This is a spare that was manually added */
5787 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005788 }
NeilBrown29269552006-03-27 01:18:10 -08005789
NeilBrown87a8dec2011-01-31 11:57:43 +11005790 /* When a reshape changes the number of devices,
5791 * ->degraded is measured against the larger of the
5792 * pre and post number of devices.
5793 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005794 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005795 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005796 spin_unlock_irqrestore(&conf->device_lock, flags);
5797 }
NeilBrown63c70c42006-03-27 01:18:13 -08005798 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005799 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b422006-10-03 01:15:46 -07005800 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005801
NeilBrown29269552006-03-27 01:18:10 -08005802 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5803 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5804 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5805 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5806 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005807 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005808 if (!mddev->sync_thread) {
5809 mddev->recovery = 0;
5810 spin_lock_irq(&conf->device_lock);
5811 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005812 rdev_for_each(rdev, mddev)
5813 rdev->new_data_offset = rdev->data_offset;
5814 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005815 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005816 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005817 spin_unlock_irq(&conf->device_lock);
5818 return -EAGAIN;
5819 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005820 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005821 md_wakeup_thread(mddev->sync_thread);
5822 md_new_event(mddev);
5823 return 0;
5824}
NeilBrown29269552006-03-27 01:18:10 -08005825
NeilBrownec32a2b2009-03-31 15:17:38 +11005826/* This is called from the reshape thread and should make any
5827 * changes needed in 'conf'
5828 */
NeilBrownd1688a62011-10-11 16:49:52 +11005829static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005830{
NeilBrown29269552006-03-27 01:18:10 -08005831
NeilBrownf6705572006-03-27 01:18:11 -08005832 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10005833 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005834
NeilBrownf6705572006-03-27 01:18:11 -08005835 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005836 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005837 rdev_for_each(rdev, conf->mddev)
5838 rdev->data_offset = rdev->new_data_offset;
5839 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005840 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005841 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005842 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005843
5844 /* read-ahead size must cover two whole stripes, which is
5845 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5846 */
NeilBrown4a5add42010-06-01 19:37:28 +10005847 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005848 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005849 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005850 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005851 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5852 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5853 }
NeilBrown29269552006-03-27 01:18:10 -08005854 }
NeilBrown29269552006-03-27 01:18:10 -08005855}
5856
NeilBrownec32a2b2009-03-31 15:17:38 +11005857/* This is called from the raid5d thread with mddev_lock held.
5858 * It makes config changes to the device.
5859 */
NeilBrownfd01b882011-10-11 16:47:53 +11005860static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005861{
NeilBrownd1688a62011-10-11 16:49:52 +11005862 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005863
5864 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5865
NeilBrownec32a2b2009-03-31 15:17:38 +11005866 if (mddev->delta_disks > 0) {
5867 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5868 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005869 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005870 } else {
5871 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005872 spin_lock_irq(&conf->device_lock);
5873 mddev->degraded = calc_degraded(conf);
5874 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005875 for (d = conf->raid_disks ;
5876 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005877 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005878 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10005879 if (rdev)
5880 clear_bit(In_sync, &rdev->flags);
5881 rdev = conf->disks[d].replacement;
5882 if (rdev)
5883 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10005884 }
NeilBrowncea9c222009-03-31 15:15:05 +11005885 }
NeilBrown88ce4932009-03-31 15:24:23 +11005886 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005887 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005888 mddev->reshape_position = MaxSector;
5889 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005890 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005891 }
5892}
5893
NeilBrownfd01b882011-10-11 16:47:53 +11005894static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005895{
NeilBrownd1688a62011-10-11 16:49:52 +11005896 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005897
5898 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005899 case 2: /* resume for a suspend */
5900 wake_up(&conf->wait_for_overlap);
5901 break;
5902
NeilBrown72626682005-09-09 16:23:54 -07005903 case 1: /* stop all writes */
5904 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005905 /* '2' tells resync/reshape to pause so that all
5906 * active stripes can drain
5907 */
5908 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005909 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005910 atomic_read(&conf->active_stripes) == 0 &&
5911 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005912 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005913 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005914 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005915 /* allow reshape to continue */
5916 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005917 break;
5918
5919 case 0: /* re-enable writes */
5920 spin_lock_irq(&conf->device_lock);
5921 conf->quiesce = 0;
5922 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005923 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005924 spin_unlock_irq(&conf->device_lock);
5925 break;
5926 }
NeilBrown72626682005-09-09 16:23:54 -07005927}
NeilBrownb15c2e52006-01-06 00:20:16 -08005928
NeilBrownd562b0c2009-03-31 14:39:39 +11005929
NeilBrownfd01b882011-10-11 16:47:53 +11005930static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005931{
NeilBrowne373ab12011-10-11 16:48:59 +11005932 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005933 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005934
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005935 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005936 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005937 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5938 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005939 return ERR_PTR(-EINVAL);
5940 }
5941
NeilBrowne373ab12011-10-11 16:48:59 +11005942 sectors = raid0_conf->strip_zone[0].zone_end;
5943 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005944 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005945 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005946 mddev->new_layout = ALGORITHM_PARITY_N;
5947 mddev->new_chunk_sectors = mddev->chunk_sectors;
5948 mddev->raid_disks += 1;
5949 mddev->delta_disks = 1;
5950 /* make sure it will be not marked as dirty */
5951 mddev->recovery_cp = MaxSector;
5952
5953 return setup_conf(mddev);
5954}
5955
5956
NeilBrownfd01b882011-10-11 16:47:53 +11005957static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005958{
5959 int chunksect;
5960
5961 if (mddev->raid_disks != 2 ||
5962 mddev->degraded > 1)
5963 return ERR_PTR(-EINVAL);
5964
5965 /* Should check if there are write-behind devices? */
5966
5967 chunksect = 64*2; /* 64K by default */
5968
5969 /* The array must be an exact multiple of chunksize */
5970 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5971 chunksect >>= 1;
5972
5973 if ((chunksect<<9) < STRIPE_SIZE)
5974 /* array size does not allow a suitable chunk size */
5975 return ERR_PTR(-EINVAL);
5976
5977 mddev->new_level = 5;
5978 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005979 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005980
5981 return setup_conf(mddev);
5982}
5983
NeilBrownfd01b882011-10-11 16:47:53 +11005984static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005985{
5986 int new_layout;
5987
5988 switch (mddev->layout) {
5989 case ALGORITHM_LEFT_ASYMMETRIC_6:
5990 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5991 break;
5992 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5993 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5994 break;
5995 case ALGORITHM_LEFT_SYMMETRIC_6:
5996 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5997 break;
5998 case ALGORITHM_RIGHT_SYMMETRIC_6:
5999 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6000 break;
6001 case ALGORITHM_PARITY_0_6:
6002 new_layout = ALGORITHM_PARITY_0;
6003 break;
6004 case ALGORITHM_PARITY_N:
6005 new_layout = ALGORITHM_PARITY_N;
6006 break;
6007 default:
6008 return ERR_PTR(-EINVAL);
6009 }
6010 mddev->new_level = 5;
6011 mddev->new_layout = new_layout;
6012 mddev->delta_disks = -1;
6013 mddev->raid_disks -= 1;
6014 return setup_conf(mddev);
6015}
6016
NeilBrownd562b0c2009-03-31 14:39:39 +11006017
NeilBrownfd01b882011-10-11 16:47:53 +11006018static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006019{
NeilBrown88ce4932009-03-31 15:24:23 +11006020 /* For a 2-drive array, the layout and chunk size can be changed
6021 * immediately as not restriping is needed.
6022 * For larger arrays we record the new value - after validation
6023 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006024 */
NeilBrownd1688a62011-10-11 16:49:52 +11006025 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006026 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006027
NeilBrown597a7112009-06-18 08:47:42 +10006028 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006029 return -EINVAL;
6030 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006031 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006032 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006033 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006034 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006035 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006036 /* not factor of array size */
6037 return -EINVAL;
6038 }
6039
6040 /* They look valid */
6041
NeilBrown88ce4932009-03-31 15:24:23 +11006042 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006043 /* can make the change immediately */
6044 if (mddev->new_layout >= 0) {
6045 conf->algorithm = mddev->new_layout;
6046 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006047 }
6048 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006049 conf->chunk_sectors = new_chunk ;
6050 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006051 }
6052 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6053 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006054 }
NeilBrown50ac1682009-06-18 08:47:55 +10006055 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006056}
6057
NeilBrownfd01b882011-10-11 16:47:53 +11006058static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006059{
NeilBrown597a7112009-06-18 08:47:42 +10006060 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006061
NeilBrown597a7112009-06-18 08:47:42 +10006062 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006063 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006064 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006065 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006066 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006067 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006068 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006069 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006070 /* not factor of array size */
6071 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006072 }
NeilBrown88ce4932009-03-31 15:24:23 +11006073
6074 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006075 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006076}
6077
NeilBrownfd01b882011-10-11 16:47:53 +11006078static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006079{
6080 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006081 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006082 * raid1 - if there are two drives. We need to know the chunk size
6083 * raid4 - trivial - just use a raid4 layout.
6084 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006085 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006086 if (mddev->level == 0)
6087 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006088 if (mddev->level == 1)
6089 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006090 if (mddev->level == 4) {
6091 mddev->new_layout = ALGORITHM_PARITY_N;
6092 mddev->new_level = 5;
6093 return setup_conf(mddev);
6094 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006095 if (mddev->level == 6)
6096 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006097
6098 return ERR_PTR(-EINVAL);
6099}
6100
NeilBrownfd01b882011-10-11 16:47:53 +11006101static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006102{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006103 /* raid4 can take over:
6104 * raid0 - if there is only one strip zone
6105 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006106 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006107 if (mddev->level == 0)
6108 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006109 if (mddev->level == 5 &&
6110 mddev->layout == ALGORITHM_PARITY_N) {
6111 mddev->new_layout = 0;
6112 mddev->new_level = 4;
6113 return setup_conf(mddev);
6114 }
6115 return ERR_PTR(-EINVAL);
6116}
NeilBrownd562b0c2009-03-31 14:39:39 +11006117
NeilBrown84fc4b52011-10-11 16:49:58 +11006118static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006119
NeilBrownfd01b882011-10-11 16:47:53 +11006120static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006121{
6122 /* Currently can only take over a raid5. We map the
6123 * personality to an equivalent raid6 personality
6124 * with the Q block at the end.
6125 */
6126 int new_layout;
6127
6128 if (mddev->pers != &raid5_personality)
6129 return ERR_PTR(-EINVAL);
6130 if (mddev->degraded > 1)
6131 return ERR_PTR(-EINVAL);
6132 if (mddev->raid_disks > 253)
6133 return ERR_PTR(-EINVAL);
6134 if (mddev->raid_disks < 3)
6135 return ERR_PTR(-EINVAL);
6136
6137 switch (mddev->layout) {
6138 case ALGORITHM_LEFT_ASYMMETRIC:
6139 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6140 break;
6141 case ALGORITHM_RIGHT_ASYMMETRIC:
6142 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6143 break;
6144 case ALGORITHM_LEFT_SYMMETRIC:
6145 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6146 break;
6147 case ALGORITHM_RIGHT_SYMMETRIC:
6148 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6149 break;
6150 case ALGORITHM_PARITY_0:
6151 new_layout = ALGORITHM_PARITY_0_6;
6152 break;
6153 case ALGORITHM_PARITY_N:
6154 new_layout = ALGORITHM_PARITY_N;
6155 break;
6156 default:
6157 return ERR_PTR(-EINVAL);
6158 }
6159 mddev->new_level = 6;
6160 mddev->new_layout = new_layout;
6161 mddev->delta_disks = 1;
6162 mddev->raid_disks += 1;
6163 return setup_conf(mddev);
6164}
6165
6166
NeilBrown84fc4b52011-10-11 16:49:58 +11006167static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006168{
6169 .name = "raid6",
6170 .level = 6,
6171 .owner = THIS_MODULE,
6172 .make_request = make_request,
6173 .run = run,
6174 .stop = stop,
6175 .status = status,
6176 .error_handler = error,
6177 .hot_add_disk = raid5_add_disk,
6178 .hot_remove_disk= raid5_remove_disk,
6179 .spare_active = raid5_spare_active,
6180 .sync_request = sync_request,
6181 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006182 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006183 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006184 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006185 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006186 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006187 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006188};
NeilBrown84fc4b52011-10-11 16:49:58 +11006189static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190{
6191 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006192 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 .owner = THIS_MODULE,
6194 .make_request = make_request,
6195 .run = run,
6196 .stop = stop,
6197 .status = status,
6198 .error_handler = error,
6199 .hot_add_disk = raid5_add_disk,
6200 .hot_remove_disk= raid5_remove_disk,
6201 .spare_active = raid5_spare_active,
6202 .sync_request = sync_request,
6203 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006204 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006205 .check_reshape = raid5_check_reshape,
6206 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006207 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006208 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006209 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210};
6211
NeilBrown84fc4b52011-10-11 16:49:58 +11006212static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213{
NeilBrown2604b702006-01-06 00:20:36 -08006214 .name = "raid4",
6215 .level = 4,
6216 .owner = THIS_MODULE,
6217 .make_request = make_request,
6218 .run = run,
6219 .stop = stop,
6220 .status = status,
6221 .error_handler = error,
6222 .hot_add_disk = raid5_add_disk,
6223 .hot_remove_disk= raid5_remove_disk,
6224 .spare_active = raid5_spare_active,
6225 .sync_request = sync_request,
6226 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006227 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006228 .check_reshape = raid5_check_reshape,
6229 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006230 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006231 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006232 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006233};
6234
6235static int __init raid5_init(void)
6236{
NeilBrown16a53ec2006-06-26 00:27:38 -07006237 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006238 register_md_personality(&raid5_personality);
6239 register_md_personality(&raid4_personality);
6240 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241}
6242
NeilBrown2604b702006-01-06 00:20:36 -08006243static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244{
NeilBrown16a53ec2006-06-26 00:27:38 -07006245 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006246 unregister_md_personality(&raid5_personality);
6247 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248}
6249
6250module_init(raid5_init);
6251module_exit(raid5_exit);
6252MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006253MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006255MODULE_ALIAS("md-raid5");
6256MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006257MODULE_ALIAS("md-level-5");
6258MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006259MODULE_ALIAS("md-personality-8"); /* RAID6 */
6260MODULE_ALIAS("md-raid6");
6261MODULE_ALIAS("md-level-6");
6262
6263/* This used to be two separate modules, they were: */
6264MODULE_ALIAS("raid5");
6265MODULE_ALIAS("raid6");