blob: 29493982dbf5561ba5b2f0d9467bc12774cba2e9 [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)
487 && !test_bit(STRIPE_EXPANDING, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 } else {
489 if (!test_bit(STRIPE_HANDLE, &sh->state))
490 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700491 if (list_empty(&sh->lru) &&
492 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700493 BUG();
494 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496 }
497 } while (sh == NULL);
498
499 if (sh)
500 atomic_inc(&sh->count);
501
502 spin_unlock_irq(&conf->device_lock);
503 return sh;
504}
505
NeilBrown05616be2012-05-21 09:27:00 +1000506/* Determine if 'data_offset' or 'new_data_offset' should be used
507 * in this stripe_head.
508 */
509static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
510{
511 sector_t progress = conf->reshape_progress;
512 /* Need a memory barrier to make sure we see the value
513 * of conf->generation, or ->data_offset that was set before
514 * reshape_progress was updated.
515 */
516 smp_rmb();
517 if (progress == MaxSector)
518 return 0;
519 if (sh->generation == conf->generation - 1)
520 return 0;
521 /* We are in a reshape, and this is a new-generation stripe,
522 * so use new_data_offset.
523 */
524 return 1;
525}
526
NeilBrown6712ecf2007-09-27 12:47:43 +0200527static void
528raid5_end_read_request(struct bio *bi, int error);
529static void
530raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700531
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000532static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700533{
NeilBrownd1688a62011-10-11 16:49:52 +1100534 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700535 int i, disks = sh->disks;
536
537 might_sleep();
538
539 for (i = disks; i--; ) {
540 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100541 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100542 struct bio *bi, *rbi;
543 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200544 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
545 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
546 rw = WRITE_FUA;
547 else
548 rw = WRITE;
549 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700550 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100551 else if (test_and_clear_bit(R5_WantReplace,
552 &sh->dev[i].flags)) {
553 rw = WRITE;
554 replace_only = 1;
555 } else
Dan Williams91c00922007-01-02 13:52:30 -0700556 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000557 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
558 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700559
560 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100561 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700562
563 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100564 rbi->bi_rw = rw;
565 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700566 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100567 rbi->bi_end_io = raid5_end_write_request;
568 } else
Dan Williams91c00922007-01-02 13:52:30 -0700569 bi->bi_end_io = raid5_end_read_request;
570
571 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100572 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100573 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
574 rdev = rcu_dereference(conf->disks[i].rdev);
575 if (!rdev) {
576 rdev = rrdev;
577 rrdev = NULL;
578 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100579 if (rw & WRITE) {
580 if (replace_only)
581 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100582 if (rdev == rrdev)
583 /* We raced and saw duplicates */
584 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100585 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100586 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100587 rdev = rrdev;
588 rrdev = NULL;
589 }
NeilBrown977df362011-12-23 10:17:53 +1100590
Dan Williams91c00922007-01-02 13:52:30 -0700591 if (rdev && test_bit(Faulty, &rdev->flags))
592 rdev = NULL;
593 if (rdev)
594 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100595 if (rrdev && test_bit(Faulty, &rrdev->flags))
596 rrdev = NULL;
597 if (rrdev)
598 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700599 rcu_read_unlock();
600
NeilBrown73e92e52011-07-28 11:39:22 +1000601 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100602 * need to check for writes. We never accept write errors
603 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000604 */
605 while ((rw & WRITE) && rdev &&
606 test_bit(WriteErrorSeen, &rdev->flags)) {
607 sector_t first_bad;
608 int bad_sectors;
609 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
610 &first_bad, &bad_sectors);
611 if (!bad)
612 break;
613
614 if (bad < 0) {
615 set_bit(BlockedBadBlocks, &rdev->flags);
616 if (!conf->mddev->external &&
617 conf->mddev->flags) {
618 /* It is very unlikely, but we might
619 * still need to write out the
620 * bad block log - better give it
621 * a chance*/
622 md_check_recovery(conf->mddev);
623 }
majianpeng18507532012-07-03 12:11:54 +1000624 /*
625 * Because md_wait_for_blocked_rdev
626 * will dec nr_pending, we must
627 * increment it first.
628 */
629 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000630 md_wait_for_blocked_rdev(rdev, conf->mddev);
631 } else {
632 /* Acknowledged bad block - skip the write */
633 rdev_dec_pending(rdev, conf->mddev);
634 rdev = NULL;
635 }
636 }
637
Dan Williams91c00922007-01-02 13:52:30 -0700638 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100639 if (s->syncing || s->expanding || s->expanded
640 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700641 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
642
Dan Williams2b7497f2008-06-28 08:31:52 +1000643 set_bit(STRIPE_IO_STARTED, &sh->state);
644
Dan Williams91c00922007-01-02 13:52:30 -0700645 bi->bi_bdev = rdev->bdev;
646 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700647 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700648 bi->bi_rw, i);
649 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000650 if (use_new_offset(conf, sh))
651 bi->bi_sector = (sh->sector
652 + rdev->new_data_offset);
653 else
654 bi->bi_sector = (sh->sector
655 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000656 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
657 bi->bi_rw |= REQ_FLUSH;
658
Dan Williams91c00922007-01-02 13:52:30 -0700659 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700660 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700661 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
662 bi->bi_io_vec[0].bv_offset = 0;
663 bi->bi_size = STRIPE_SIZE;
664 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100665 if (rrdev)
666 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700667 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100668 }
669 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100670 if (s->syncing || s->expanding || s->expanded
671 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100672 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
673
674 set_bit(STRIPE_IO_STARTED, &sh->state);
675
676 rbi->bi_bdev = rrdev->bdev;
677 pr_debug("%s: for %llu schedule op %ld on "
678 "replacement disc %d\n",
679 __func__, (unsigned long long)sh->sector,
680 rbi->bi_rw, i);
681 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000682 if (use_new_offset(conf, sh))
683 rbi->bi_sector = (sh->sector
684 + rrdev->new_data_offset);
685 else
686 rbi->bi_sector = (sh->sector
687 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100688 rbi->bi_flags = 1 << BIO_UPTODATE;
689 rbi->bi_idx = 0;
690 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
691 rbi->bi_io_vec[0].bv_offset = 0;
692 rbi->bi_size = STRIPE_SIZE;
693 rbi->bi_next = NULL;
694 generic_make_request(rbi);
695 }
696 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000697 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700698 set_bit(STRIPE_DEGRADED, &sh->state);
699 pr_debug("skip op %ld on disc %d for sector %llu\n",
700 bi->bi_rw, i, (unsigned long long)sh->sector);
701 clear_bit(R5_LOCKED, &sh->dev[i].flags);
702 set_bit(STRIPE_HANDLE, &sh->state);
703 }
704 }
705}
706
707static struct dma_async_tx_descriptor *
708async_copy_data(int frombio, struct bio *bio, struct page *page,
709 sector_t sector, struct dma_async_tx_descriptor *tx)
710{
711 struct bio_vec *bvl;
712 struct page *bio_page;
713 int i;
714 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700715 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700716 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700717
718 if (bio->bi_sector >= sector)
719 page_offset = (signed)(bio->bi_sector - sector) * 512;
720 else
721 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700722
Dan Williams0403e382009-09-08 17:42:50 -0700723 if (frombio)
724 flags |= ASYNC_TX_FENCE;
725 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
726
Dan Williams91c00922007-01-02 13:52:30 -0700727 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000728 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700729 int clen;
730 int b_offset = 0;
731
732 if (page_offset < 0) {
733 b_offset = -page_offset;
734 page_offset += b_offset;
735 len -= b_offset;
736 }
737
738 if (len > 0 && page_offset + len > STRIPE_SIZE)
739 clen = STRIPE_SIZE - page_offset;
740 else
741 clen = len;
742
743 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000744 b_offset += bvl->bv_offset;
745 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700746 if (frombio)
747 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700748 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700749 else
750 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700751 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700752 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700753 /* chain the operations */
754 submit.depend_tx = tx;
755
Dan Williams91c00922007-01-02 13:52:30 -0700756 if (clen < len) /* hit end of page */
757 break;
758 page_offset += len;
759 }
760
761 return tx;
762}
763
764static void ops_complete_biofill(void *stripe_head_ref)
765{
766 struct stripe_head *sh = stripe_head_ref;
767 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700768 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700769
Harvey Harrisone46b2722008-04-28 02:15:50 -0700770 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700771 (unsigned long long)sh->sector);
772
773 /* clear completed biofills */
774 for (i = sh->disks; i--; ) {
775 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700776
777 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700778 /* and check if we need to reply to a read request,
779 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000780 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700781 */
782 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700783 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700784
Dan Williams91c00922007-01-02 13:52:30 -0700785 BUG_ON(!dev->read);
786 rbi = dev->read;
787 dev->read = NULL;
788 while (rbi && rbi->bi_sector <
789 dev->sector + STRIPE_SECTORS) {
790 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000791 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700792 rbi->bi_next = return_bi;
793 return_bi = rbi;
794 }
Dan Williams91c00922007-01-02 13:52:30 -0700795 rbi = rbi2;
796 }
797 }
798 }
Dan Williams83de75c2008-06-28 08:31:58 +1000799 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700800
801 return_io(return_bi);
802
Dan Williamse4d84902007-09-24 10:06:13 -0700803 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700804 release_stripe(sh);
805}
806
807static void ops_run_biofill(struct stripe_head *sh)
808{
809 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700810 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700811 int i;
812
Harvey Harrisone46b2722008-04-28 02:15:50 -0700813 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700814 (unsigned long long)sh->sector);
815
816 for (i = sh->disks; i--; ) {
817 struct r5dev *dev = &sh->dev[i];
818 if (test_bit(R5_Wantfill, &dev->flags)) {
819 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000820 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700821 dev->read = rbi = dev->toread;
822 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000823 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700824 while (rbi && rbi->bi_sector <
825 dev->sector + STRIPE_SECTORS) {
826 tx = async_copy_data(0, rbi, dev->page,
827 dev->sector, tx);
828 rbi = r5_next_bio(rbi, dev->sector);
829 }
830 }
831 }
832
833 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700834 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
835 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700836}
837
Dan Williams4e7d2c02009-08-29 19:13:11 -0700838static void mark_target_uptodate(struct stripe_head *sh, int target)
839{
840 struct r5dev *tgt;
841
842 if (target < 0)
843 return;
844
845 tgt = &sh->dev[target];
846 set_bit(R5_UPTODATE, &tgt->flags);
847 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
848 clear_bit(R5_Wantcompute, &tgt->flags);
849}
850
Dan Williamsac6b53b2009-07-14 13:40:19 -0700851static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700852{
853 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700854
Harvey Harrisone46b2722008-04-28 02:15:50 -0700855 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700856 (unsigned long long)sh->sector);
857
Dan Williamsac6b53b2009-07-14 13:40:19 -0700858 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700859 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700860 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700861
Dan Williamsecc65c92008-06-28 08:31:57 +1000862 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
863 if (sh->check_state == check_state_compute_run)
864 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700865 set_bit(STRIPE_HANDLE, &sh->state);
866 release_stripe(sh);
867}
868
Dan Williamsd6f38f32009-07-14 11:50:52 -0700869/* return a pointer to the address conversion region of the scribble buffer */
870static addr_conv_t *to_addr_conv(struct stripe_head *sh,
871 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700872{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700873 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
874}
875
876static struct dma_async_tx_descriptor *
877ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
878{
Dan Williams91c00922007-01-02 13:52:30 -0700879 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700880 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700881 int target = sh->ops.target;
882 struct r5dev *tgt = &sh->dev[target];
883 struct page *xor_dest = tgt->page;
884 int count = 0;
885 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700886 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700887 int i;
888
889 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700890 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700891 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
892
893 for (i = disks; i--; )
894 if (i != target)
895 xor_srcs[count++] = sh->dev[i].page;
896
897 atomic_inc(&sh->count);
898
Dan Williams0403e382009-09-08 17:42:50 -0700899 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700900 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700901 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700902 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700903 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700904 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700905
Dan Williams91c00922007-01-02 13:52:30 -0700906 return tx;
907}
908
Dan Williamsac6b53b2009-07-14 13:40:19 -0700909/* set_syndrome_sources - populate source buffers for gen_syndrome
910 * @srcs - (struct page *) array of size sh->disks
911 * @sh - stripe_head to parse
912 *
913 * Populates srcs in proper layout order for the stripe and returns the
914 * 'count' of sources to be used in a call to async_gen_syndrome. The P
915 * destination buffer is recorded in srcs[count] and the Q destination
916 * is recorded in srcs[count+1]].
917 */
918static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
919{
920 int disks = sh->disks;
921 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
922 int d0_idx = raid6_d0(sh);
923 int count;
924 int i;
925
926 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100927 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700928
929 count = 0;
930 i = d0_idx;
931 do {
932 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
933
934 srcs[slot] = sh->dev[i].page;
935 i = raid6_next_disk(i, disks);
936 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700937
NeilBrowne4424fe2009-10-16 16:27:34 +1100938 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700939}
940
941static struct dma_async_tx_descriptor *
942ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
943{
944 int disks = sh->disks;
945 struct page **blocks = percpu->scribble;
946 int target;
947 int qd_idx = sh->qd_idx;
948 struct dma_async_tx_descriptor *tx;
949 struct async_submit_ctl submit;
950 struct r5dev *tgt;
951 struct page *dest;
952 int i;
953 int count;
954
955 if (sh->ops.target < 0)
956 target = sh->ops.target2;
957 else if (sh->ops.target2 < 0)
958 target = sh->ops.target;
959 else
960 /* we should only have one valid target */
961 BUG();
962 BUG_ON(target < 0);
963 pr_debug("%s: stripe %llu block: %d\n",
964 __func__, (unsigned long long)sh->sector, target);
965
966 tgt = &sh->dev[target];
967 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
968 dest = tgt->page;
969
970 atomic_inc(&sh->count);
971
972 if (target == qd_idx) {
973 count = set_syndrome_sources(blocks, sh);
974 blocks[count] = NULL; /* regenerating p is not necessary */
975 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700976 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
977 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700978 to_addr_conv(sh, percpu));
979 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
980 } else {
981 /* Compute any data- or p-drive using XOR */
982 count = 0;
983 for (i = disks; i-- ; ) {
984 if (i == target || i == qd_idx)
985 continue;
986 blocks[count++] = sh->dev[i].page;
987 }
988
Dan Williams0403e382009-09-08 17:42:50 -0700989 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
990 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700991 to_addr_conv(sh, percpu));
992 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
993 }
994
995 return tx;
996}
997
998static struct dma_async_tx_descriptor *
999ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1000{
1001 int i, count, disks = sh->disks;
1002 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1003 int d0_idx = raid6_d0(sh);
1004 int faila = -1, failb = -1;
1005 int target = sh->ops.target;
1006 int target2 = sh->ops.target2;
1007 struct r5dev *tgt = &sh->dev[target];
1008 struct r5dev *tgt2 = &sh->dev[target2];
1009 struct dma_async_tx_descriptor *tx;
1010 struct page **blocks = percpu->scribble;
1011 struct async_submit_ctl submit;
1012
1013 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1014 __func__, (unsigned long long)sh->sector, target, target2);
1015 BUG_ON(target < 0 || target2 < 0);
1016 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1017 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1018
Dan Williams6c910a72009-09-16 12:24:54 -07001019 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001020 * slot number conversion for 'faila' and 'failb'
1021 */
1022 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001023 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001024 count = 0;
1025 i = d0_idx;
1026 do {
1027 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1028
1029 blocks[slot] = sh->dev[i].page;
1030
1031 if (i == target)
1032 faila = slot;
1033 if (i == target2)
1034 failb = slot;
1035 i = raid6_next_disk(i, disks);
1036 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001037
1038 BUG_ON(faila == failb);
1039 if (failb < faila)
1040 swap(faila, failb);
1041 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1042 __func__, (unsigned long long)sh->sector, faila, failb);
1043
1044 atomic_inc(&sh->count);
1045
1046 if (failb == syndrome_disks+1) {
1047 /* Q disk is one of the missing disks */
1048 if (faila == syndrome_disks) {
1049 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001050 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1051 ops_complete_compute, sh,
1052 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001053 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001054 STRIPE_SIZE, &submit);
1055 } else {
1056 struct page *dest;
1057 int data_target;
1058 int qd_idx = sh->qd_idx;
1059
1060 /* Missing D+Q: recompute D from P, then recompute Q */
1061 if (target == qd_idx)
1062 data_target = target2;
1063 else
1064 data_target = target;
1065
1066 count = 0;
1067 for (i = disks; i-- ; ) {
1068 if (i == data_target || i == qd_idx)
1069 continue;
1070 blocks[count++] = sh->dev[i].page;
1071 }
1072 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001073 init_async_submit(&submit,
1074 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1075 NULL, NULL, NULL,
1076 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001077 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1078 &submit);
1079
1080 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001081 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1082 ops_complete_compute, sh,
1083 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001084 return async_gen_syndrome(blocks, 0, count+2,
1085 STRIPE_SIZE, &submit);
1086 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001087 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001088 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1089 ops_complete_compute, sh,
1090 to_addr_conv(sh, percpu));
1091 if (failb == syndrome_disks) {
1092 /* We're missing D+P. */
1093 return async_raid6_datap_recov(syndrome_disks+2,
1094 STRIPE_SIZE, faila,
1095 blocks, &submit);
1096 } else {
1097 /* We're missing D+D. */
1098 return async_raid6_2data_recov(syndrome_disks+2,
1099 STRIPE_SIZE, faila, failb,
1100 blocks, &submit);
1101 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001102 }
1103}
1104
1105
Dan Williams91c00922007-01-02 13:52:30 -07001106static void ops_complete_prexor(void *stripe_head_ref)
1107{
1108 struct stripe_head *sh = stripe_head_ref;
1109
Harvey Harrisone46b2722008-04-28 02:15:50 -07001110 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001111 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001112}
1113
1114static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001115ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1116 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001117{
Dan Williams91c00922007-01-02 13:52:30 -07001118 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001119 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001120 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001121 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001122
1123 /* existing parity data subtracted */
1124 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1125
Harvey Harrisone46b2722008-04-28 02:15:50 -07001126 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001127 (unsigned long long)sh->sector);
1128
1129 for (i = disks; i--; ) {
1130 struct r5dev *dev = &sh->dev[i];
1131 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001132 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001133 xor_srcs[count++] = dev->page;
1134 }
1135
Dan Williams0403e382009-09-08 17:42:50 -07001136 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001137 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001138 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001139
1140 return tx;
1141}
1142
1143static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001144ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001145{
1146 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001147 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001148
Harvey Harrisone46b2722008-04-28 02:15:50 -07001149 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001150 (unsigned long long)sh->sector);
1151
1152 for (i = disks; i--; ) {
1153 struct r5dev *dev = &sh->dev[i];
1154 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001155
Dan Williamsd8ee0722008-06-28 08:32:06 +10001156 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001157 struct bio *wbi;
1158
Shaohua Lib17459c2012-07-19 16:01:31 +10001159 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001160 chosen = dev->towrite;
1161 dev->towrite = NULL;
1162 BUG_ON(dev->written);
1163 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001164 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001165
1166 while (wbi && wbi->bi_sector <
1167 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001168 if (wbi->bi_rw & REQ_FUA)
1169 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001170 if (wbi->bi_rw & REQ_SYNC)
1171 set_bit(R5_SyncIO, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001172 tx = async_copy_data(1, wbi, dev->page,
1173 dev->sector, tx);
1174 wbi = r5_next_bio(wbi, dev->sector);
1175 }
1176 }
1177 }
1178
1179 return tx;
1180}
1181
Dan Williamsac6b53b2009-07-14 13:40:19 -07001182static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001183{
1184 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001185 int disks = sh->disks;
1186 int pd_idx = sh->pd_idx;
1187 int qd_idx = sh->qd_idx;
1188 int i;
Shaohua Libc0934f2012-05-22 13:55:05 +10001189 bool fua = false, sync = false;
Dan Williams91c00922007-01-02 13:52:30 -07001190
Harvey Harrisone46b2722008-04-28 02:15:50 -07001191 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001192 (unsigned long long)sh->sector);
1193
Shaohua Libc0934f2012-05-22 13:55:05 +10001194 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001195 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001196 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1197 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001198
Dan Williams91c00922007-01-02 13:52:30 -07001199 for (i = disks; i--; ) {
1200 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001201
Tejun Heoe9c74692010-09-03 11:56:18 +02001202 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001203 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001204 if (fua)
1205 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001206 if (sync)
1207 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001208 }
Dan Williams91c00922007-01-02 13:52:30 -07001209 }
1210
Dan Williamsd8ee0722008-06-28 08:32:06 +10001211 if (sh->reconstruct_state == reconstruct_state_drain_run)
1212 sh->reconstruct_state = reconstruct_state_drain_result;
1213 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1214 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1215 else {
1216 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1217 sh->reconstruct_state = reconstruct_state_result;
1218 }
Dan Williams91c00922007-01-02 13:52:30 -07001219
1220 set_bit(STRIPE_HANDLE, &sh->state);
1221 release_stripe(sh);
1222}
1223
1224static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001225ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1226 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001227{
Dan Williams91c00922007-01-02 13:52:30 -07001228 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001229 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001230 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001231 int count = 0, pd_idx = sh->pd_idx, i;
1232 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001233 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001234 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001235
Harvey Harrisone46b2722008-04-28 02:15:50 -07001236 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001237 (unsigned long long)sh->sector);
1238
1239 /* check if prexor is active which means only process blocks
1240 * that are part of a read-modify-write (written)
1241 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001242 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1243 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001244 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1245 for (i = disks; i--; ) {
1246 struct r5dev *dev = &sh->dev[i];
1247 if (dev->written)
1248 xor_srcs[count++] = dev->page;
1249 }
1250 } else {
1251 xor_dest = sh->dev[pd_idx].page;
1252 for (i = disks; i--; ) {
1253 struct r5dev *dev = &sh->dev[i];
1254 if (i != pd_idx)
1255 xor_srcs[count++] = dev->page;
1256 }
1257 }
1258
Dan Williams91c00922007-01-02 13:52:30 -07001259 /* 1/ if we prexor'd then the dest is reused as a source
1260 * 2/ if we did not prexor then we are redoing the parity
1261 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1262 * for the synchronous xor case
1263 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001264 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001265 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1266
1267 atomic_inc(&sh->count);
1268
Dan Williamsac6b53b2009-07-14 13:40:19 -07001269 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001270 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001271 if (unlikely(count == 1))
1272 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1273 else
1274 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001275}
1276
Dan Williamsac6b53b2009-07-14 13:40:19 -07001277static void
1278ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1279 struct dma_async_tx_descriptor *tx)
1280{
1281 struct async_submit_ctl submit;
1282 struct page **blocks = percpu->scribble;
1283 int count;
1284
1285 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1286
1287 count = set_syndrome_sources(blocks, sh);
1288
1289 atomic_inc(&sh->count);
1290
1291 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1292 sh, to_addr_conv(sh, percpu));
1293 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001294}
1295
1296static void ops_complete_check(void *stripe_head_ref)
1297{
1298 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001299
Harvey Harrisone46b2722008-04-28 02:15:50 -07001300 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001301 (unsigned long long)sh->sector);
1302
Dan Williamsecc65c92008-06-28 08:31:57 +10001303 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001304 set_bit(STRIPE_HANDLE, &sh->state);
1305 release_stripe(sh);
1306}
1307
Dan Williamsac6b53b2009-07-14 13:40:19 -07001308static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001309{
Dan Williams91c00922007-01-02 13:52:30 -07001310 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001311 int pd_idx = sh->pd_idx;
1312 int qd_idx = sh->qd_idx;
1313 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001314 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001315 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001316 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001317 int count;
1318 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001319
Harvey Harrisone46b2722008-04-28 02:15:50 -07001320 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001321 (unsigned long long)sh->sector);
1322
Dan Williamsac6b53b2009-07-14 13:40:19 -07001323 count = 0;
1324 xor_dest = sh->dev[pd_idx].page;
1325 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001326 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001327 if (i == pd_idx || i == qd_idx)
1328 continue;
1329 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001330 }
1331
Dan Williamsd6f38f32009-07-14 11:50:52 -07001332 init_async_submit(&submit, 0, NULL, NULL, NULL,
1333 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001334 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001335 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001336
Dan Williams91c00922007-01-02 13:52:30 -07001337 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001338 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1339 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001340}
1341
Dan Williamsac6b53b2009-07-14 13:40:19 -07001342static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1343{
1344 struct page **srcs = percpu->scribble;
1345 struct async_submit_ctl submit;
1346 int count;
1347
1348 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1349 (unsigned long long)sh->sector, checkp);
1350
1351 count = set_syndrome_sources(srcs, sh);
1352 if (!checkp)
1353 srcs[count] = NULL;
1354
1355 atomic_inc(&sh->count);
1356 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1357 sh, to_addr_conv(sh, percpu));
1358 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1359 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1360}
1361
Dan Williams417b8d42009-10-16 16:25:22 +11001362static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001363{
1364 int overlap_clear = 0, i, disks = sh->disks;
1365 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001366 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001367 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001368 struct raid5_percpu *percpu;
1369 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001370
Dan Williamsd6f38f32009-07-14 11:50:52 -07001371 cpu = get_cpu();
1372 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001373 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001374 ops_run_biofill(sh);
1375 overlap_clear++;
1376 }
1377
Dan Williams7b3a8712008-06-28 08:32:09 +10001378 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001379 if (level < 6)
1380 tx = ops_run_compute5(sh, percpu);
1381 else {
1382 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1383 tx = ops_run_compute6_1(sh, percpu);
1384 else
1385 tx = ops_run_compute6_2(sh, percpu);
1386 }
1387 /* terminate the chain if reconstruct is not set to be run */
1388 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001389 async_tx_ack(tx);
1390 }
Dan Williams91c00922007-01-02 13:52:30 -07001391
Dan Williams600aa102008-06-28 08:32:05 +10001392 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001393 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001394
Dan Williams600aa102008-06-28 08:32:05 +10001395 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001396 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001397 overlap_clear++;
1398 }
1399
Dan Williamsac6b53b2009-07-14 13:40:19 -07001400 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1401 if (level < 6)
1402 ops_run_reconstruct5(sh, percpu, tx);
1403 else
1404 ops_run_reconstruct6(sh, percpu, tx);
1405 }
Dan Williams91c00922007-01-02 13:52:30 -07001406
Dan Williamsac6b53b2009-07-14 13:40:19 -07001407 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1408 if (sh->check_state == check_state_run)
1409 ops_run_check_p(sh, percpu);
1410 else if (sh->check_state == check_state_run_q)
1411 ops_run_check_pq(sh, percpu, 0);
1412 else if (sh->check_state == check_state_run_pq)
1413 ops_run_check_pq(sh, percpu, 1);
1414 else
1415 BUG();
1416 }
Dan Williams91c00922007-01-02 13:52:30 -07001417
Dan Williams91c00922007-01-02 13:52:30 -07001418 if (overlap_clear)
1419 for (i = disks; i--; ) {
1420 struct r5dev *dev = &sh->dev[i];
1421 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1422 wake_up(&sh->raid_conf->wait_for_overlap);
1423 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001424 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001425}
1426
Dan Williams417b8d42009-10-16 16:25:22 +11001427#ifdef CONFIG_MULTICORE_RAID456
1428static void async_run_ops(void *param, async_cookie_t cookie)
1429{
1430 struct stripe_head *sh = param;
1431 unsigned long ops_request = sh->ops.request;
1432
1433 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1434 wake_up(&sh->ops.wait_for_ops);
1435
1436 __raid_run_ops(sh, ops_request);
1437 release_stripe(sh);
1438}
1439
1440static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1441{
1442 /* since handle_stripe can be called outside of raid5d context
1443 * we need to ensure sh->ops.request is de-staged before another
1444 * request arrives
1445 */
1446 wait_event(sh->ops.wait_for_ops,
1447 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1448 sh->ops.request = ops_request;
1449
1450 atomic_inc(&sh->count);
1451 async_schedule(async_run_ops, sh);
1452}
1453#else
1454#define raid_run_ops __raid_run_ops
1455#endif
1456
NeilBrownd1688a62011-10-11 16:49:52 +11001457static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001460 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001461 if (!sh)
1462 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001463
NeilBrown3f294f42005-11-08 21:39:25 -08001464 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001465 #ifdef CONFIG_MULTICORE_RAID456
1466 init_waitqueue_head(&sh->ops.wait_for_ops);
1467 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001468
Shaohua Lib17459c2012-07-19 16:01:31 +10001469 spin_lock_init(&sh->stripe_lock);
1470
NeilBrowne4e11e32010-06-16 16:45:16 +10001471 if (grow_buffers(sh)) {
1472 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001473 kmem_cache_free(conf->slab_cache, sh);
1474 return 0;
1475 }
1476 /* we just created an active stripe so... */
1477 atomic_set(&sh->count, 1);
1478 atomic_inc(&conf->active_stripes);
1479 INIT_LIST_HEAD(&sh->lru);
1480 release_stripe(sh);
1481 return 1;
1482}
1483
NeilBrownd1688a62011-10-11 16:49:52 +11001484static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001485{
Christoph Lametere18b8902006-12-06 20:33:20 -08001486 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001487 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
NeilBrownf4be6b42010-06-01 19:37:25 +10001489 if (conf->mddev->gendisk)
1490 sprintf(conf->cache_name[0],
1491 "raid%d-%s", conf->level, mdname(conf->mddev));
1492 else
1493 sprintf(conf->cache_name[0],
1494 "raid%d-%p", conf->level, conf->mddev);
1495 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1496
NeilBrownad01c9e2006-03-27 01:18:07 -08001497 conf->active_name = 0;
1498 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001500 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (!sc)
1502 return 1;
1503 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001504 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001505 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001506 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return 0;
1509}
NeilBrown29269552006-03-27 01:18:10 -08001510
Dan Williamsd6f38f32009-07-14 11:50:52 -07001511/**
1512 * scribble_len - return the required size of the scribble region
1513 * @num - total number of disks in the array
1514 *
1515 * The size must be enough to contain:
1516 * 1/ a struct page pointer for each device in the array +2
1517 * 2/ room to convert each entry in (1) to its corresponding dma
1518 * (dma_map_page()) or page (page_address()) address.
1519 *
1520 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1521 * calculate over all devices (not just the data blocks), using zeros in place
1522 * of the P and Q blocks.
1523 */
1524static size_t scribble_len(int num)
1525{
1526 size_t len;
1527
1528 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1529
1530 return len;
1531}
1532
NeilBrownd1688a62011-10-11 16:49:52 +11001533static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001534{
1535 /* Make all the stripes able to hold 'newsize' devices.
1536 * New slots in each stripe get 'page' set to a new page.
1537 *
1538 * This happens in stages:
1539 * 1/ create a new kmem_cache and allocate the required number of
1540 * stripe_heads.
1541 * 2/ gather all the old stripe_heads and tranfer the pages across
1542 * to the new stripe_heads. This will have the side effect of
1543 * freezing the array as once all stripe_heads have been collected,
1544 * no IO will be possible. Old stripe heads are freed once their
1545 * pages have been transferred over, and the old kmem_cache is
1546 * freed when all stripes are done.
1547 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1548 * we simple return a failre status - no need to clean anything up.
1549 * 4/ allocate new pages for the new slots in the new stripe_heads.
1550 * If this fails, we don't bother trying the shrink the
1551 * stripe_heads down again, we just leave them as they are.
1552 * As each stripe_head is processed the new one is released into
1553 * active service.
1554 *
1555 * Once step2 is started, we cannot afford to wait for a write,
1556 * so we use GFP_NOIO allocations.
1557 */
1558 struct stripe_head *osh, *nsh;
1559 LIST_HEAD(newstripes);
1560 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001561 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001562 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001563 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001564 int i;
1565
1566 if (newsize <= conf->pool_size)
1567 return 0; /* never bother to shrink */
1568
Dan Williamsb5470dc2008-06-27 21:44:04 -07001569 err = md_allow_write(conf->mddev);
1570 if (err)
1571 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001572
NeilBrownad01c9e2006-03-27 01:18:07 -08001573 /* Step 1 */
1574 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1575 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001576 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001577 if (!sc)
1578 return -ENOMEM;
1579
1580 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001581 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001582 if (!nsh)
1583 break;
1584
NeilBrownad01c9e2006-03-27 01:18:07 -08001585 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001586 #ifdef CONFIG_MULTICORE_RAID456
1587 init_waitqueue_head(&nsh->ops.wait_for_ops);
1588 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001589
1590 list_add(&nsh->lru, &newstripes);
1591 }
1592 if (i) {
1593 /* didn't get enough, give up */
1594 while (!list_empty(&newstripes)) {
1595 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1596 list_del(&nsh->lru);
1597 kmem_cache_free(sc, nsh);
1598 }
1599 kmem_cache_destroy(sc);
1600 return -ENOMEM;
1601 }
1602 /* Step 2 - Must use GFP_NOIO now.
1603 * OK, we have enough stripes, start collecting inactive
1604 * stripes and copying them over
1605 */
1606 list_for_each_entry(nsh, &newstripes, lru) {
1607 spin_lock_irq(&conf->device_lock);
1608 wait_event_lock_irq(conf->wait_for_stripe,
1609 !list_empty(&conf->inactive_list),
1610 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001611 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001612 osh = get_free_stripe(conf);
1613 spin_unlock_irq(&conf->device_lock);
1614 atomic_set(&nsh->count, 1);
1615 for(i=0; i<conf->pool_size; i++)
1616 nsh->dev[i].page = osh->dev[i].page;
1617 for( ; i<newsize; i++)
1618 nsh->dev[i].page = NULL;
1619 kmem_cache_free(conf->slab_cache, osh);
1620 }
1621 kmem_cache_destroy(conf->slab_cache);
1622
1623 /* Step 3.
1624 * At this point, we are holding all the stripes so the array
1625 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001626 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001627 */
1628 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1629 if (ndisks) {
1630 for (i=0; i<conf->raid_disks; i++)
1631 ndisks[i] = conf->disks[i];
1632 kfree(conf->disks);
1633 conf->disks = ndisks;
1634 } else
1635 err = -ENOMEM;
1636
Dan Williamsd6f38f32009-07-14 11:50:52 -07001637 get_online_cpus();
1638 conf->scribble_len = scribble_len(newsize);
1639 for_each_present_cpu(cpu) {
1640 struct raid5_percpu *percpu;
1641 void *scribble;
1642
1643 percpu = per_cpu_ptr(conf->percpu, cpu);
1644 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1645
1646 if (scribble) {
1647 kfree(percpu->scribble);
1648 percpu->scribble = scribble;
1649 } else {
1650 err = -ENOMEM;
1651 break;
1652 }
1653 }
1654 put_online_cpus();
1655
NeilBrownad01c9e2006-03-27 01:18:07 -08001656 /* Step 4, return new stripes to service */
1657 while(!list_empty(&newstripes)) {
1658 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1659 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001660
NeilBrownad01c9e2006-03-27 01:18:07 -08001661 for (i=conf->raid_disks; i < newsize; i++)
1662 if (nsh->dev[i].page == NULL) {
1663 struct page *p = alloc_page(GFP_NOIO);
1664 nsh->dev[i].page = p;
1665 if (!p)
1666 err = -ENOMEM;
1667 }
1668 release_stripe(nsh);
1669 }
1670 /* critical section pass, GFP_NOIO no longer needed */
1671
1672 conf->slab_cache = sc;
1673 conf->active_name = 1-conf->active_name;
1674 conf->pool_size = newsize;
1675 return err;
1676}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
NeilBrownd1688a62011-10-11 16:49:52 +11001678static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
1680 struct stripe_head *sh;
1681
NeilBrown3f294f42005-11-08 21:39:25 -08001682 spin_lock_irq(&conf->device_lock);
1683 sh = get_free_stripe(conf);
1684 spin_unlock_irq(&conf->device_lock);
1685 if (!sh)
1686 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001687 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001688 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001689 kmem_cache_free(conf->slab_cache, sh);
1690 atomic_dec(&conf->active_stripes);
1691 return 1;
1692}
1693
NeilBrownd1688a62011-10-11 16:49:52 +11001694static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001695{
1696 while (drop_one_stripe(conf))
1697 ;
1698
NeilBrown29fc7e32006-02-03 03:03:41 -08001699 if (conf->slab_cache)
1700 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 conf->slab_cache = NULL;
1702}
1703
NeilBrown6712ecf2007-09-27 12:47:43 +02001704static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
NeilBrown99c0fb52009-03-31 14:39:38 +11001706 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001707 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001708 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001710 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001711 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001712 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 for (i=0 ; i<disks; i++)
1715 if (bi == &sh->dev[i].req)
1716 break;
1717
Dan Williams45b42332007-07-09 11:56:43 -07001718 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1719 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 uptodate);
1721 if (i == disks) {
1722 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001723 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
NeilBrown14a75d32011-12-23 10:17:52 +11001725 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001726 /* If replacement finished while this request was outstanding,
1727 * 'replacement' might be NULL already.
1728 * In that case it moved down to 'rdev'.
1729 * rdev is not removed until all requests are finished.
1730 */
NeilBrown14a75d32011-12-23 10:17:52 +11001731 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001732 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001733 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
NeilBrown05616be2012-05-21 09:27:00 +10001735 if (use_new_offset(conf, sh))
1736 s = sh->sector + rdev->new_data_offset;
1737 else
1738 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001741 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001742 /* Note that this cannot happen on a
1743 * replacement device. We just fail those on
1744 * any error
1745 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001746 printk_ratelimited(
1747 KERN_INFO
1748 "md/raid:%s: read error corrected"
1749 " (%lu sectors at %llu on %s)\n",
1750 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001751 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001752 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001753 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001754 clear_bit(R5_ReadError, &sh->dev[i].flags);
1755 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001756 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1757 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1758
NeilBrown14a75d32011-12-23 10:17:52 +11001759 if (atomic_read(&rdev->read_errors))
1760 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001762 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001763 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001764 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001767 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001768 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1769 printk_ratelimited(
1770 KERN_WARNING
1771 "md/raid:%s: read error on replacement device "
1772 "(sector %llu on %s).\n",
1773 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001774 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001775 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001776 else if (conf->mddev->degraded >= conf->max_degraded) {
1777 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001778 printk_ratelimited(
1779 KERN_WARNING
1780 "md/raid:%s: read error not correctable "
1781 "(sector %llu on %s).\n",
1782 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001783 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001784 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001785 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001786 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001787 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001788 printk_ratelimited(
1789 KERN_WARNING
1790 "md/raid:%s: read error NOT corrected!! "
1791 "(sector %llu on %s).\n",
1792 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001793 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001794 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001795 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001796 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001797 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001798 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001799 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001800 else
1801 retry = 1;
1802 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001803 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1804 set_bit(R5_ReadError, &sh->dev[i].flags);
1805 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1806 } else
1807 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001808 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001809 clear_bit(R5_ReadError, &sh->dev[i].flags);
1810 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001811 if (!(set_bad
1812 && test_bit(In_sync, &rdev->flags)
1813 && rdev_set_badblocks(
1814 rdev, sh->sector, STRIPE_SECTORS, 0)))
1815 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
NeilBrown14a75d32011-12-23 10:17:52 +11001818 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1820 set_bit(STRIPE_HANDLE, &sh->state);
1821 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
NeilBrownd710e132008-10-13 11:55:12 +11001824static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
NeilBrown99c0fb52009-03-31 14:39:38 +11001826 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001827 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001828 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001829 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001831 sector_t first_bad;
1832 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001833 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
NeilBrown977df362011-12-23 10:17:53 +11001835 for (i = 0 ; i < disks; i++) {
1836 if (bi == &sh->dev[i].req) {
1837 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 break;
NeilBrown977df362011-12-23 10:17:53 +11001839 }
1840 if (bi == &sh->dev[i].rreq) {
1841 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001842 if (rdev)
1843 replacement = 1;
1844 else
1845 /* rdev was removed and 'replacement'
1846 * replaced it. rdev is not removed
1847 * until all requests are finished.
1848 */
1849 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001850 break;
1851 }
1852 }
Dan Williams45b42332007-07-09 11:56:43 -07001853 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1855 uptodate);
1856 if (i == disks) {
1857 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001858 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860
NeilBrown977df362011-12-23 10:17:53 +11001861 if (replacement) {
1862 if (!uptodate)
1863 md_error(conf->mddev, rdev);
1864 else if (is_badblock(rdev, sh->sector,
1865 STRIPE_SECTORS,
1866 &first_bad, &bad_sectors))
1867 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1868 } else {
1869 if (!uptodate) {
1870 set_bit(WriteErrorSeen, &rdev->flags);
1871 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001872 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1873 set_bit(MD_RECOVERY_NEEDED,
1874 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001875 } else if (is_badblock(rdev, sh->sector,
1876 STRIPE_SECTORS,
1877 &first_bad, &bad_sectors))
1878 set_bit(R5_MadeGood, &sh->dev[i].flags);
1879 }
1880 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
NeilBrown977df362011-12-23 10:17:53 +11001882 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1883 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001885 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
NeilBrown784052e2009-03-31 15:19:07 +11001888static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
NeilBrown784052e2009-03-31 15:19:07 +11001890static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
1892 struct r5dev *dev = &sh->dev[i];
1893
1894 bio_init(&dev->req);
1895 dev->req.bi_io_vec = &dev->vec;
1896 dev->req.bi_vcnt++;
1897 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001899 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
NeilBrown977df362011-12-23 10:17:53 +11001901 bio_init(&dev->rreq);
1902 dev->rreq.bi_io_vec = &dev->rvec;
1903 dev->rreq.bi_vcnt++;
1904 dev->rreq.bi_max_vecs++;
1905 dev->rreq.bi_private = sh;
1906 dev->rvec.bv_page = dev->page;
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001909 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
NeilBrownfd01b882011-10-11 16:47:53 +11001912static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001915 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001916 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001917 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
NeilBrown908f4fb2011-12-23 10:17:50 +11001919 spin_lock_irqsave(&conf->device_lock, flags);
1920 clear_bit(In_sync, &rdev->flags);
1921 mddev->degraded = calc_degraded(conf);
1922 spin_unlock_irqrestore(&conf->device_lock, flags);
1923 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1924
NeilBrownde393cd2011-07-28 11:31:48 +10001925 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001926 set_bit(Faulty, &rdev->flags);
1927 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1928 printk(KERN_ALERT
1929 "md/raid:%s: Disk failure on %s, disabling device.\n"
1930 "md/raid:%s: Operation continuing on %d devices.\n",
1931 mdname(mddev),
1932 bdevname(rdev->bdev, b),
1933 mdname(mddev),
1934 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001935}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937/*
1938 * Input: a 'big' sector number,
1939 * Output: index of the data and parity disk, and the sector # in them.
1940 */
NeilBrownd1688a62011-10-11 16:49:52 +11001941static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001942 int previous, int *dd_idx,
1943 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001945 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001946 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001948 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001949 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001951 int algorithm = previous ? conf->prev_algo
1952 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001953 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1954 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001955 int raid_disks = previous ? conf->previous_raid_disks
1956 : conf->raid_disks;
1957 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 /* First compute the information on this sector */
1960
1961 /*
1962 * Compute the chunk number and the sector offset inside the chunk
1963 */
1964 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1965 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 /*
1968 * Compute the stripe number
1969 */
NeilBrown35f2a592010-04-20 14:13:34 +10001970 stripe = chunk_number;
1971 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001972 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 /*
1974 * Select the parity disk based on the user selected algorithm.
1975 */
NeilBrown84789552011-07-27 11:00:36 +10001976 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001977 switch(conf->level) {
1978 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001979 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001980 break;
1981 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001982 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001984 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001985 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 (*dd_idx)++;
1987 break;
1988 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001989 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001990 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 (*dd_idx)++;
1992 break;
1993 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001994 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001995 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 break;
1997 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001998 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001999 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002001 case ALGORITHM_PARITY_0:
2002 pd_idx = 0;
2003 (*dd_idx)++;
2004 break;
2005 case ALGORITHM_PARITY_N:
2006 pd_idx = data_disks;
2007 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002009 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002010 }
2011 break;
2012 case 6:
2013
NeilBrowne183eae2009-03-31 15:20:22 +11002014 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002015 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002016 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002017 qd_idx = pd_idx + 1;
2018 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002019 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002020 qd_idx = 0;
2021 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002022 (*dd_idx) += 2; /* D D P Q D */
2023 break;
2024 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002025 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002026 qd_idx = pd_idx + 1;
2027 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002028 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002029 qd_idx = 0;
2030 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002031 (*dd_idx) += 2; /* D D P Q D */
2032 break;
2033 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002034 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002035 qd_idx = (pd_idx + 1) % raid_disks;
2036 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002037 break;
2038 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002039 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002040 qd_idx = (pd_idx + 1) % raid_disks;
2041 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002042 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002043
2044 case ALGORITHM_PARITY_0:
2045 pd_idx = 0;
2046 qd_idx = 1;
2047 (*dd_idx) += 2;
2048 break;
2049 case ALGORITHM_PARITY_N:
2050 pd_idx = data_disks;
2051 qd_idx = data_disks + 1;
2052 break;
2053
2054 case ALGORITHM_ROTATING_ZERO_RESTART:
2055 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2056 * of blocks for computing Q is different.
2057 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002058 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002059 qd_idx = pd_idx + 1;
2060 if (pd_idx == raid_disks-1) {
2061 (*dd_idx)++; /* Q D D D P */
2062 qd_idx = 0;
2063 } else if (*dd_idx >= pd_idx)
2064 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002065 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002066 break;
2067
2068 case ALGORITHM_ROTATING_N_RESTART:
2069 /* Same a left_asymmetric, by first stripe is
2070 * D D D P Q rather than
2071 * Q D D D P
2072 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002073 stripe2 += 1;
2074 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002075 qd_idx = pd_idx + 1;
2076 if (pd_idx == raid_disks-1) {
2077 (*dd_idx)++; /* Q D D D P */
2078 qd_idx = 0;
2079 } else if (*dd_idx >= pd_idx)
2080 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002081 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002082 break;
2083
2084 case ALGORITHM_ROTATING_N_CONTINUE:
2085 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002086 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002087 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2088 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002089 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002090 break;
2091
2092 case ALGORITHM_LEFT_ASYMMETRIC_6:
2093 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002094 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002095 if (*dd_idx >= pd_idx)
2096 (*dd_idx)++;
2097 qd_idx = raid_disks - 1;
2098 break;
2099
2100 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002101 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002102 if (*dd_idx >= pd_idx)
2103 (*dd_idx)++;
2104 qd_idx = raid_disks - 1;
2105 break;
2106
2107 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002108 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002109 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2110 qd_idx = raid_disks - 1;
2111 break;
2112
2113 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002114 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002115 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2116 qd_idx = raid_disks - 1;
2117 break;
2118
2119 case ALGORITHM_PARITY_0_6:
2120 pd_idx = 0;
2121 (*dd_idx)++;
2122 qd_idx = raid_disks - 1;
2123 break;
2124
NeilBrown16a53ec2006-06-26 00:27:38 -07002125 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002126 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002127 }
2128 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 }
2130
NeilBrown911d4ee2009-03-31 14:39:38 +11002131 if (sh) {
2132 sh->pd_idx = pd_idx;
2133 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002134 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 /*
2137 * Finally, compute the new sector number
2138 */
2139 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2140 return new_sector;
2141}
2142
2143
NeilBrown784052e2009-03-31 15:19:07 +11002144static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
NeilBrownd1688a62011-10-11 16:49:52 +11002146 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002147 int raid_disks = sh->disks;
2148 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002150 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2151 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002152 int algorithm = previous ? conf->prev_algo
2153 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 sector_t stripe;
2155 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002156 sector_t chunk_number;
2157 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002159 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
NeilBrown16a53ec2006-06-26 00:27:38 -07002161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2163 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
NeilBrown16a53ec2006-06-26 00:27:38 -07002165 if (i == sh->pd_idx)
2166 return 0;
2167 switch(conf->level) {
2168 case 4: break;
2169 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002170 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 case ALGORITHM_LEFT_ASYMMETRIC:
2172 case ALGORITHM_RIGHT_ASYMMETRIC:
2173 if (i > sh->pd_idx)
2174 i--;
2175 break;
2176 case ALGORITHM_LEFT_SYMMETRIC:
2177 case ALGORITHM_RIGHT_SYMMETRIC:
2178 if (i < sh->pd_idx)
2179 i += raid_disks;
2180 i -= (sh->pd_idx + 1);
2181 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002182 case ALGORITHM_PARITY_0:
2183 i -= 1;
2184 break;
2185 case ALGORITHM_PARITY_N:
2186 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002188 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002189 }
2190 break;
2191 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002192 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002193 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002194 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002195 case ALGORITHM_LEFT_ASYMMETRIC:
2196 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002197 case ALGORITHM_ROTATING_ZERO_RESTART:
2198 case ALGORITHM_ROTATING_N_RESTART:
2199 if (sh->pd_idx == raid_disks-1)
2200 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002201 else if (i > sh->pd_idx)
2202 i -= 2; /* D D P Q D */
2203 break;
2204 case ALGORITHM_LEFT_SYMMETRIC:
2205 case ALGORITHM_RIGHT_SYMMETRIC:
2206 if (sh->pd_idx == raid_disks-1)
2207 i--; /* Q D D D P */
2208 else {
2209 /* D D P Q D */
2210 if (i < sh->pd_idx)
2211 i += raid_disks;
2212 i -= (sh->pd_idx + 2);
2213 }
2214 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002215 case ALGORITHM_PARITY_0:
2216 i -= 2;
2217 break;
2218 case ALGORITHM_PARITY_N:
2219 break;
2220 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002221 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002222 if (sh->pd_idx == 0)
2223 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002224 else {
2225 /* D D Q P D */
2226 if (i < sh->pd_idx)
2227 i += raid_disks;
2228 i -= (sh->pd_idx + 1);
2229 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002230 break;
2231 case ALGORITHM_LEFT_ASYMMETRIC_6:
2232 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2233 if (i > sh->pd_idx)
2234 i--;
2235 break;
2236 case ALGORITHM_LEFT_SYMMETRIC_6:
2237 case ALGORITHM_RIGHT_SYMMETRIC_6:
2238 if (i < sh->pd_idx)
2239 i += data_disks + 1;
2240 i -= (sh->pd_idx + 1);
2241 break;
2242 case ALGORITHM_PARITY_0_6:
2243 i -= 1;
2244 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002245 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002246 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002247 }
2248 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
2250
2251 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002252 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
NeilBrown112bf892009-03-31 14:39:38 +11002254 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002255 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002256 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2257 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002258 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2259 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return 0;
2261 }
2262 return r_sector;
2263}
2264
2265
Dan Williams600aa102008-06-28 08:32:05 +10002266static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002267schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002268 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002269{
2270 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002271 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002272 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002273
Dan Williamse33129d2007-01-02 13:52:30 -07002274 if (rcw) {
2275 /* if we are not expanding this is a proper write request, and
2276 * there will be bios with new data to be drained into the
2277 * stripe cache
2278 */
2279 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002280 sh->reconstruct_state = reconstruct_state_drain_run;
2281 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2282 } else
2283 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002284
Dan Williamsac6b53b2009-07-14 13:40:19 -07002285 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002286
2287 for (i = disks; i--; ) {
2288 struct r5dev *dev = &sh->dev[i];
2289
2290 if (dev->towrite) {
2291 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002292 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002293 if (!expand)
2294 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002295 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002296 }
2297 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002298 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002299 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002300 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002301 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002302 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002303 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2304 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2305
Dan Williamsd8ee0722008-06-28 08:32:06 +10002306 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002307 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2308 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002309 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002310
2311 for (i = disks; i--; ) {
2312 struct r5dev *dev = &sh->dev[i];
2313 if (i == pd_idx)
2314 continue;
2315
Dan Williamse33129d2007-01-02 13:52:30 -07002316 if (dev->towrite &&
2317 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002318 test_bit(R5_Wantcompute, &dev->flags))) {
2319 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002320 set_bit(R5_LOCKED, &dev->flags);
2321 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002322 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002323 }
2324 }
2325 }
2326
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002327 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002328 * are in flight
2329 */
2330 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2331 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002332 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002333
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002334 if (level == 6) {
2335 int qd_idx = sh->qd_idx;
2336 struct r5dev *dev = &sh->dev[qd_idx];
2337
2338 set_bit(R5_LOCKED, &dev->flags);
2339 clear_bit(R5_UPTODATE, &dev->flags);
2340 s->locked++;
2341 }
2342
Dan Williams600aa102008-06-28 08:32:05 +10002343 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002344 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002345 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002346}
NeilBrown16a53ec2006-06-26 00:27:38 -07002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348/*
2349 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002350 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 * The bi_next chain must be in order.
2352 */
2353static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2354{
2355 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002356 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002357 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
NeilBrowncbe47ec2011-07-26 11:20:35 +10002359 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 (unsigned long long)bi->bi_sector,
2361 (unsigned long long)sh->sector);
2362
Shaohua Lib17459c2012-07-19 16:01:31 +10002363 /*
2364 * If several bio share a stripe. The bio bi_phys_segments acts as a
2365 * reference count to avoid race. The reference count should already be
2366 * increased before this function is called (for example, in
2367 * make_request()), so other bio sharing this stripe will not free the
2368 * stripe. If a stripe is owned by one stripe, the stripe lock will
2369 * protect it.
2370 */
2371 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002372 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002374 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002375 firstwrite = 1;
2376 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 bip = &sh->dev[dd_idx].toread;
2378 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2379 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2380 goto overlap;
2381 bip = & (*bip)->bi_next;
2382 }
2383 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2384 goto overlap;
2385
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002386 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (*bip)
2388 bi->bi_next = *bip;
2389 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002390 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (forwrite) {
2393 /* check if page is covered */
2394 sector_t sector = sh->dev[dd_idx].sector;
2395 for (bi=sh->dev[dd_idx].towrite;
2396 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2397 bi && bi->bi_sector <= sector;
2398 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2399 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2400 sector = bi->bi_sector + (bi->bi_size>>9);
2401 }
2402 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2403 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2404 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002405 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002406
2407 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2408 (unsigned long long)(*bip)->bi_sector,
2409 (unsigned long long)sh->sector, dd_idx);
2410
2411 if (conf->mddev->bitmap && firstwrite) {
2412 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2413 STRIPE_SECTORS, 0);
2414 sh->bm_seq = conf->seq_flush+1;
2415 set_bit(STRIPE_BIT_DELAY, &sh->state);
2416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return 1;
2418
2419 overlap:
2420 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002421 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return 0;
2423}
2424
NeilBrownd1688a62011-10-11 16:49:52 +11002425static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002426
NeilBrownd1688a62011-10-11 16:49:52 +11002427static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002428 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002429{
NeilBrown784052e2009-03-31 15:19:07 +11002430 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002431 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002432 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002433 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002434 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002435
NeilBrown112bf892009-03-31 14:39:38 +11002436 raid5_compute_sector(conf,
2437 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002438 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002439 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002440 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002441}
2442
Dan Williamsa4456852007-07-09 11:56:43 -07002443static void
NeilBrownd1688a62011-10-11 16:49:52 +11002444handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002445 struct stripe_head_state *s, int disks,
2446 struct bio **return_bi)
2447{
2448 int i;
2449 for (i = disks; i--; ) {
2450 struct bio *bi;
2451 int bitmap_end = 0;
2452
2453 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002454 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002455 rcu_read_lock();
2456 rdev = rcu_dereference(conf->disks[i].rdev);
2457 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002458 atomic_inc(&rdev->nr_pending);
2459 else
2460 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002461 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002462 if (rdev) {
2463 if (!rdev_set_badblocks(
2464 rdev,
2465 sh->sector,
2466 STRIPE_SECTORS, 0))
2467 md_error(conf->mddev, rdev);
2468 rdev_dec_pending(rdev, conf->mddev);
2469 }
Dan Williamsa4456852007-07-09 11:56:43 -07002470 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002471 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002472 /* fail all writes first */
2473 bi = sh->dev[i].towrite;
2474 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002475 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002476 if (bi) {
2477 s->to_write--;
2478 bitmap_end = 1;
2479 }
2480
2481 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2482 wake_up(&conf->wait_for_overlap);
2483
2484 while (bi && bi->bi_sector <
2485 sh->dev[i].sector + STRIPE_SECTORS) {
2486 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2487 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002488 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002489 md_write_end(conf->mddev);
2490 bi->bi_next = *return_bi;
2491 *return_bi = bi;
2492 }
2493 bi = nextbi;
2494 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002495 if (bitmap_end)
2496 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2497 STRIPE_SECTORS, 0, 0);
2498 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002499 /* and fail all 'written' */
2500 bi = sh->dev[i].written;
2501 sh->dev[i].written = NULL;
2502 if (bi) bitmap_end = 1;
2503 while (bi && bi->bi_sector <
2504 sh->dev[i].sector + STRIPE_SECTORS) {
2505 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2506 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002507 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002508 md_write_end(conf->mddev);
2509 bi->bi_next = *return_bi;
2510 *return_bi = bi;
2511 }
2512 bi = bi2;
2513 }
2514
Dan Williamsb5e98d62007-01-02 13:52:31 -07002515 /* fail any reads if this device is non-operational and
2516 * the data has not reached the cache yet.
2517 */
2518 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2519 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2520 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002521 bi = sh->dev[i].toread;
2522 sh->dev[i].toread = NULL;
2523 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2524 wake_up(&conf->wait_for_overlap);
2525 if (bi) s->to_read--;
2526 while (bi && bi->bi_sector <
2527 sh->dev[i].sector + STRIPE_SECTORS) {
2528 struct bio *nextbi =
2529 r5_next_bio(bi, sh->dev[i].sector);
2530 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002531 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002532 bi->bi_next = *return_bi;
2533 *return_bi = bi;
2534 }
2535 bi = nextbi;
2536 }
2537 }
Dan Williamsa4456852007-07-09 11:56:43 -07002538 if (bitmap_end)
2539 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2540 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002541 /* If we were in the middle of a write the parity block might
2542 * still be locked - so just clear all R5_LOCKED flags
2543 */
2544 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002545 }
2546
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002547 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2548 if (atomic_dec_and_test(&conf->pending_full_writes))
2549 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002550}
2551
NeilBrown7f0da592011-07-28 11:39:22 +10002552static void
NeilBrownd1688a62011-10-11 16:49:52 +11002553handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002554 struct stripe_head_state *s)
2555{
2556 int abort = 0;
2557 int i;
2558
NeilBrown7f0da592011-07-28 11:39:22 +10002559 clear_bit(STRIPE_SYNCING, &sh->state);
2560 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002561 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002562 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002563 * Don't even need to abort as that is handled elsewhere
2564 * if needed, and not always wanted e.g. if there is a known
2565 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002566 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002567 * non-sync devices, or abort the recovery
2568 */
NeilBrown18b98372012-04-01 23:48:38 +10002569 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2570 /* During recovery devices cannot be removed, so
2571 * locking and refcounting of rdevs is not needed
2572 */
2573 for (i = 0; i < conf->raid_disks; i++) {
2574 struct md_rdev *rdev = conf->disks[i].rdev;
2575 if (rdev
2576 && !test_bit(Faulty, &rdev->flags)
2577 && !test_bit(In_sync, &rdev->flags)
2578 && !rdev_set_badblocks(rdev, sh->sector,
2579 STRIPE_SECTORS, 0))
2580 abort = 1;
2581 rdev = conf->disks[i].replacement;
2582 if (rdev
2583 && !test_bit(Faulty, &rdev->flags)
2584 && !test_bit(In_sync, &rdev->flags)
2585 && !rdev_set_badblocks(rdev, sh->sector,
2586 STRIPE_SECTORS, 0))
2587 abort = 1;
2588 }
2589 if (abort)
2590 conf->recovery_disabled =
2591 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002592 }
NeilBrown18b98372012-04-01 23:48:38 +10002593 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002594}
2595
NeilBrown9a3e1102011-12-23 10:17:53 +11002596static int want_replace(struct stripe_head *sh, int disk_idx)
2597{
2598 struct md_rdev *rdev;
2599 int rv = 0;
2600 /* Doing recovery so rcu locking not required */
2601 rdev = sh->raid_conf->disks[disk_idx].replacement;
2602 if (rdev
2603 && !test_bit(Faulty, &rdev->flags)
2604 && !test_bit(In_sync, &rdev->flags)
2605 && (rdev->recovery_offset <= sh->sector
2606 || rdev->mddev->recovery_cp <= sh->sector))
2607 rv = 1;
2608
2609 return rv;
2610}
2611
NeilBrown93b3dbc2011-07-27 11:00:36 +10002612/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002613 * to be read or computed to satisfy a request.
2614 *
2615 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002616 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002617 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002618static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2619 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002620{
2621 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002622 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2623 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002624
Dan Williamsf38e1212007-01-02 13:52:30 -07002625 /* is the data in this block needed, and can we get it? */
2626 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002627 !test_bit(R5_UPTODATE, &dev->flags) &&
2628 (dev->toread ||
2629 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2630 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002631 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002632 (s->failed >= 1 && fdev[0]->toread) ||
2633 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002634 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2635 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2636 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002637 /* we would like to get this block, possibly by computing it,
2638 * otherwise read it if the backing disk is insync
2639 */
2640 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2641 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2642 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002643 (s->failed && (disk_idx == s->failed_num[0] ||
2644 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002645 /* have disk failed, and we're requested to fetch it;
2646 * do compute it
2647 */
2648 pr_debug("Computing stripe %llu block %d\n",
2649 (unsigned long long)sh->sector, disk_idx);
2650 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2651 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2652 set_bit(R5_Wantcompute, &dev->flags);
2653 sh->ops.target = disk_idx;
2654 sh->ops.target2 = -1; /* no 2nd target */
2655 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002656 /* Careful: from this point on 'uptodate' is in the eye
2657 * of raid_run_ops which services 'compute' operations
2658 * before writes. R5_Wantcompute flags a block that will
2659 * be R5_UPTODATE by the time it is needed for a
2660 * subsequent operation.
2661 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002662 s->uptodate++;
2663 return 1;
2664 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2665 /* Computing 2-failure is *very* expensive; only
2666 * do it if failed >= 2
2667 */
2668 int other;
2669 for (other = disks; other--; ) {
2670 if (other == disk_idx)
2671 continue;
2672 if (!test_bit(R5_UPTODATE,
2673 &sh->dev[other].flags))
2674 break;
2675 }
2676 BUG_ON(other < 0);
2677 pr_debug("Computing stripe %llu blocks %d,%d\n",
2678 (unsigned long long)sh->sector,
2679 disk_idx, other);
2680 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2681 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2682 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2683 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2684 sh->ops.target = disk_idx;
2685 sh->ops.target2 = other;
2686 s->uptodate += 2;
2687 s->req_compute = 1;
2688 return 1;
2689 } else if (test_bit(R5_Insync, &dev->flags)) {
2690 set_bit(R5_LOCKED, &dev->flags);
2691 set_bit(R5_Wantread, &dev->flags);
2692 s->locked++;
2693 pr_debug("Reading block %d (sync=%d)\n",
2694 disk_idx, s->syncing);
2695 }
2696 }
2697
2698 return 0;
2699}
2700
2701/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002702 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002703 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002704static void handle_stripe_fill(struct stripe_head *sh,
2705 struct stripe_head_state *s,
2706 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002707{
2708 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002709
2710 /* look for blocks to read/compute, skip this if a compute
2711 * is already in flight, or if the stripe contents are in the
2712 * midst of changing due to a write
2713 */
2714 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2715 !sh->reconstruct_state)
2716 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002717 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002718 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002719 set_bit(STRIPE_HANDLE, &sh->state);
2720}
2721
2722
Dan Williams1fe797e2008-06-28 09:16:30 +10002723/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002724 * any written block on an uptodate or failed drive can be returned.
2725 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2726 * never LOCKED, so we don't need to test 'failed' directly.
2727 */
NeilBrownd1688a62011-10-11 16:49:52 +11002728static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002729 struct stripe_head *sh, int disks, struct bio **return_bi)
2730{
2731 int i;
2732 struct r5dev *dev;
2733
2734 for (i = disks; i--; )
2735 if (sh->dev[i].written) {
2736 dev = &sh->dev[i];
2737 if (!test_bit(R5_LOCKED, &dev->flags) &&
2738 test_bit(R5_UPTODATE, &dev->flags)) {
2739 /* We can return any write requests */
2740 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002741 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002742 wbi = dev->written;
2743 dev->written = NULL;
2744 while (wbi && wbi->bi_sector <
2745 dev->sector + STRIPE_SECTORS) {
2746 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002747 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002748 md_write_end(conf->mddev);
2749 wbi->bi_next = *return_bi;
2750 *return_bi = wbi;
2751 }
2752 wbi = wbi2;
2753 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002754 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2755 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002756 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002757 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002758 }
2759 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002760
2761 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2762 if (atomic_dec_and_test(&conf->pending_full_writes))
2763 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002764}
2765
NeilBrownd1688a62011-10-11 16:49:52 +11002766static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002767 struct stripe_head *sh,
2768 struct stripe_head_state *s,
2769 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002770{
2771 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002772 if (conf->max_degraded == 2) {
2773 /* RAID6 requires 'rcw' in current implementation
2774 * Calculate the real rcw later - for now fake it
2775 * look like rcw is cheaper
2776 */
2777 rcw = 1; rmw = 2;
2778 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002779 /* would I have to read this buffer for read_modify_write */
2780 struct r5dev *dev = &sh->dev[i];
2781 if ((dev->towrite || i == sh->pd_idx) &&
2782 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002783 !(test_bit(R5_UPTODATE, &dev->flags) ||
2784 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002785 if (test_bit(R5_Insync, &dev->flags))
2786 rmw++;
2787 else
2788 rmw += 2*disks; /* cannot read it */
2789 }
2790 /* Would I have to read this buffer for reconstruct_write */
2791 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2792 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002793 !(test_bit(R5_UPTODATE, &dev->flags) ||
2794 test_bit(R5_Wantcompute, &dev->flags))) {
2795 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002796 else
2797 rcw += 2*disks;
2798 }
2799 }
Dan Williams45b42332007-07-09 11:56:43 -07002800 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002801 (unsigned long long)sh->sector, rmw, rcw);
2802 set_bit(STRIPE_HANDLE, &sh->state);
2803 if (rmw < rcw && rmw > 0)
2804 /* prefer read-modify-write, but need to get some data */
2805 for (i = disks; i--; ) {
2806 struct r5dev *dev = &sh->dev[i];
2807 if ((dev->towrite || i == sh->pd_idx) &&
2808 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002809 !(test_bit(R5_UPTODATE, &dev->flags) ||
2810 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002811 test_bit(R5_Insync, &dev->flags)) {
2812 if (
2813 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002814 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002815 "%d for r-m-w\n", i);
2816 set_bit(R5_LOCKED, &dev->flags);
2817 set_bit(R5_Wantread, &dev->flags);
2818 s->locked++;
2819 } else {
2820 set_bit(STRIPE_DELAYED, &sh->state);
2821 set_bit(STRIPE_HANDLE, &sh->state);
2822 }
2823 }
2824 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002825 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002826 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002827 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002828 for (i = disks; i--; ) {
2829 struct r5dev *dev = &sh->dev[i];
2830 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002831 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002832 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002833 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002834 test_bit(R5_Wantcompute, &dev->flags))) {
2835 rcw++;
2836 if (!test_bit(R5_Insync, &dev->flags))
2837 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002838 if (
2839 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002840 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002841 "%d for Reconstruct\n", i);
2842 set_bit(R5_LOCKED, &dev->flags);
2843 set_bit(R5_Wantread, &dev->flags);
2844 s->locked++;
2845 } else {
2846 set_bit(STRIPE_DELAYED, &sh->state);
2847 set_bit(STRIPE_HANDLE, &sh->state);
2848 }
2849 }
2850 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002851 }
Dan Williamsa4456852007-07-09 11:56:43 -07002852 /* now if nothing is locked, and if we have enough data,
2853 * we can start a write request
2854 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002855 /* since handle_stripe can be called at any time we need to handle the
2856 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002857 * subsequent call wants to start a write request. raid_run_ops only
2858 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002859 * simultaneously. If this is not the case then new writes need to be
2860 * held off until the compute completes.
2861 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002862 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2863 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2864 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002865 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002866}
2867
NeilBrownd1688a62011-10-11 16:49:52 +11002868static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002869 struct stripe_head_state *s, int disks)
2870{
Dan Williamsecc65c92008-06-28 08:31:57 +10002871 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002872
Dan Williamsbd2ab672008-04-10 21:29:27 -07002873 set_bit(STRIPE_HANDLE, &sh->state);
2874
Dan Williamsecc65c92008-06-28 08:31:57 +10002875 switch (sh->check_state) {
2876 case check_state_idle:
2877 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002878 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002879 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002880 sh->check_state = check_state_run;
2881 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002882 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002883 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002884 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002885 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002886 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002887 /* fall through */
2888 case check_state_compute_result:
2889 sh->check_state = check_state_idle;
2890 if (!dev)
2891 dev = &sh->dev[sh->pd_idx];
2892
2893 /* check that a write has not made the stripe insync */
2894 if (test_bit(STRIPE_INSYNC, &sh->state))
2895 break;
2896
2897 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002898 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2899 BUG_ON(s->uptodate != disks);
2900
2901 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002902 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002903 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002904
Dan Williamsa4456852007-07-09 11:56:43 -07002905 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002906 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002907 break;
2908 case check_state_run:
2909 break; /* we will be called again upon completion */
2910 case check_state_check_result:
2911 sh->check_state = check_state_idle;
2912
2913 /* if a failure occurred during the check operation, leave
2914 * STRIPE_INSYNC not set and let the stripe be handled again
2915 */
2916 if (s->failed)
2917 break;
2918
2919 /* handle a successful check operation, if parity is correct
2920 * we are done. Otherwise update the mismatch count and repair
2921 * parity if !MD_RECOVERY_CHECK
2922 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002923 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002924 /* parity is correct (on disc,
2925 * not in buffer any more)
2926 */
2927 set_bit(STRIPE_INSYNC, &sh->state);
2928 else {
2929 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2930 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2931 /* don't try to repair!! */
2932 set_bit(STRIPE_INSYNC, &sh->state);
2933 else {
2934 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002935 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002936 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2937 set_bit(R5_Wantcompute,
2938 &sh->dev[sh->pd_idx].flags);
2939 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002940 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002941 s->uptodate++;
2942 }
2943 }
2944 break;
2945 case check_state_compute_run:
2946 break;
2947 default:
2948 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2949 __func__, sh->check_state,
2950 (unsigned long long) sh->sector);
2951 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002952 }
2953}
2954
2955
NeilBrownd1688a62011-10-11 16:49:52 +11002956static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002957 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002958 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002959{
Dan Williamsa4456852007-07-09 11:56:43 -07002960 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002961 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002962 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002963
2964 set_bit(STRIPE_HANDLE, &sh->state);
2965
2966 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002967
Dan Williamsa4456852007-07-09 11:56:43 -07002968 /* Want to check and possibly repair P and Q.
2969 * However there could be one 'failed' device, in which
2970 * case we can only check one of them, possibly using the
2971 * other to generate missing data
2972 */
2973
Dan Williamsd82dfee2009-07-14 13:40:57 -07002974 switch (sh->check_state) {
2975 case check_state_idle:
2976 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002977 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002978 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002979 * makes sense to check P (If anything else were failed,
2980 * we would have used P to recreate it).
2981 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002982 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002983 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002984 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002985 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002986 * anything, so it makes sense to check it
2987 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002988 if (sh->check_state == check_state_run)
2989 sh->check_state = check_state_run_pq;
2990 else
2991 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002992 }
Dan Williams36d1c642009-07-14 11:48:22 -07002993
Dan Williamsd82dfee2009-07-14 13:40:57 -07002994 /* discard potentially stale zero_sum_result */
2995 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07002996
Dan Williamsd82dfee2009-07-14 13:40:57 -07002997 if (sh->check_state == check_state_run) {
2998 /* async_xor_zero_sum destroys the contents of P */
2999 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3000 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003001 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003002 if (sh->check_state >= check_state_run &&
3003 sh->check_state <= check_state_run_pq) {
3004 /* async_syndrome_zero_sum preserves P and Q, so
3005 * no need to mark them !uptodate here
3006 */
3007 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3008 break;
3009 }
Dan Williams36d1c642009-07-14 11:48:22 -07003010
Dan Williamsd82dfee2009-07-14 13:40:57 -07003011 /* we have 2-disk failure */
3012 BUG_ON(s->failed != 2);
3013 /* fall through */
3014 case check_state_compute_result:
3015 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003016
Dan Williamsd82dfee2009-07-14 13:40:57 -07003017 /* check that a write has not made the stripe insync */
3018 if (test_bit(STRIPE_INSYNC, &sh->state))
3019 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003020
3021 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003022 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003023 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003024 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003025 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003026 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003027 s->locked++;
3028 set_bit(R5_LOCKED, &dev->flags);
3029 set_bit(R5_Wantwrite, &dev->flags);
3030 }
3031 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003032 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003033 s->locked++;
3034 set_bit(R5_LOCKED, &dev->flags);
3035 set_bit(R5_Wantwrite, &dev->flags);
3036 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003037 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003038 dev = &sh->dev[pd_idx];
3039 s->locked++;
3040 set_bit(R5_LOCKED, &dev->flags);
3041 set_bit(R5_Wantwrite, &dev->flags);
3042 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003043 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003044 dev = &sh->dev[qd_idx];
3045 s->locked++;
3046 set_bit(R5_LOCKED, &dev->flags);
3047 set_bit(R5_Wantwrite, &dev->flags);
3048 }
3049 clear_bit(STRIPE_DEGRADED, &sh->state);
3050
3051 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003052 break;
3053 case check_state_run:
3054 case check_state_run_q:
3055 case check_state_run_pq:
3056 break; /* we will be called again upon completion */
3057 case check_state_check_result:
3058 sh->check_state = check_state_idle;
3059
3060 /* handle a successful check operation, if parity is correct
3061 * we are done. Otherwise update the mismatch count and repair
3062 * parity if !MD_RECOVERY_CHECK
3063 */
3064 if (sh->ops.zero_sum_result == 0) {
3065 /* both parities are correct */
3066 if (!s->failed)
3067 set_bit(STRIPE_INSYNC, &sh->state);
3068 else {
3069 /* in contrast to the raid5 case we can validate
3070 * parity, but still have a failure to write
3071 * back
3072 */
3073 sh->check_state = check_state_compute_result;
3074 /* Returning at this point means that we may go
3075 * off and bring p and/or q uptodate again so
3076 * we make sure to check zero_sum_result again
3077 * to verify if p or q need writeback
3078 */
3079 }
3080 } else {
3081 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3082 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3083 /* don't try to repair!! */
3084 set_bit(STRIPE_INSYNC, &sh->state);
3085 else {
3086 int *target = &sh->ops.target;
3087
3088 sh->ops.target = -1;
3089 sh->ops.target2 = -1;
3090 sh->check_state = check_state_compute_run;
3091 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3092 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3093 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3094 set_bit(R5_Wantcompute,
3095 &sh->dev[pd_idx].flags);
3096 *target = pd_idx;
3097 target = &sh->ops.target2;
3098 s->uptodate++;
3099 }
3100 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3101 set_bit(R5_Wantcompute,
3102 &sh->dev[qd_idx].flags);
3103 *target = qd_idx;
3104 s->uptodate++;
3105 }
3106 }
3107 }
3108 break;
3109 case check_state_compute_run:
3110 break;
3111 default:
3112 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3113 __func__, sh->check_state,
3114 (unsigned long long) sh->sector);
3115 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003116 }
3117}
3118
NeilBrownd1688a62011-10-11 16:49:52 +11003119static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003120{
3121 int i;
3122
3123 /* We have read all the blocks in this stripe and now we need to
3124 * copy some of them into a target stripe for expand.
3125 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003126 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003127 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3128 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003129 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003130 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003131 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003132 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003133
NeilBrown784052e2009-03-31 15:19:07 +11003134 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003135 sector_t s = raid5_compute_sector(conf, bn, 0,
3136 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003137 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003138 if (sh2 == NULL)
3139 /* so far only the early blocks of this stripe
3140 * have been requested. When later blocks
3141 * get requested, we will try again
3142 */
3143 continue;
3144 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3145 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3146 /* must have already done this block */
3147 release_stripe(sh2);
3148 continue;
3149 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003150
3151 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003152 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003153 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003154 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003155 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003156
Dan Williamsa4456852007-07-09 11:56:43 -07003157 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3158 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3159 for (j = 0; j < conf->raid_disks; j++)
3160 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003161 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003162 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3163 break;
3164 if (j == conf->raid_disks) {
3165 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3166 set_bit(STRIPE_HANDLE, &sh2->state);
3167 }
3168 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003169
Dan Williamsa4456852007-07-09 11:56:43 -07003170 }
NeilBrowna2e08552007-09-11 15:23:36 -07003171 /* done submitting copies, wait for them to complete */
3172 if (tx) {
3173 async_tx_ack(tx);
3174 dma_wait_for_async_tx(tx);
3175 }
Dan Williamsa4456852007-07-09 11:56:43 -07003176}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
3178/*
3179 * handle_stripe - do things to a stripe.
3180 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003181 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3182 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003184 * return some read requests which now have data
3185 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 * schedule a read on some buffers
3187 * schedule a write of some buffers
3188 * return confirmation of parity correctness
3189 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 */
Dan Williamsa4456852007-07-09 11:56:43 -07003191
NeilBrownacfe7262011-07-27 11:00:36 +10003192static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003193{
NeilBrownd1688a62011-10-11 16:49:52 +11003194 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003195 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003196 struct r5dev *dev;
3197 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003198 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003199
NeilBrownacfe7262011-07-27 11:00:36 +10003200 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003201
NeilBrownacfe7262011-07-27 11:00:36 +10003202 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3203 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3204 s->failed_num[0] = -1;
3205 s->failed_num[1] = -1;
3206
3207 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003208 rcu_read_lock();
3209 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003210 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003211 sector_t first_bad;
3212 int bad_sectors;
3213 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003214
NeilBrown16a53ec2006-06-26 00:27:38 -07003215 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003216
Dan Williams45b42332007-07-09 11:56:43 -07003217 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003218 i, dev->flags,
3219 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003220 /* maybe we can reply to a read
3221 *
3222 * new wantfill requests are only permitted while
3223 * ops_complete_biofill is guaranteed to be inactive
3224 */
3225 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3226 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3227 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003228
3229 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003230 if (test_bit(R5_LOCKED, &dev->flags))
3231 s->locked++;
3232 if (test_bit(R5_UPTODATE, &dev->flags))
3233 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003234 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003235 s->compute++;
3236 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003237 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003238
NeilBrownacfe7262011-07-27 11:00:36 +10003239 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003240 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003241 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003242 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003243 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003244 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003245 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003246 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003247 }
Dan Williamsa4456852007-07-09 11:56:43 -07003248 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003249 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003250 /* Prefer to use the replacement for reads, but only
3251 * if it is recovered enough and has no bad blocks.
3252 */
3253 rdev = rcu_dereference(conf->disks[i].replacement);
3254 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3255 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3256 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3257 &first_bad, &bad_sectors))
3258 set_bit(R5_ReadRepl, &dev->flags);
3259 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003260 if (rdev)
3261 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003262 rdev = rcu_dereference(conf->disks[i].rdev);
3263 clear_bit(R5_ReadRepl, &dev->flags);
3264 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003265 if (rdev && test_bit(Faulty, &rdev->flags))
3266 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003267 if (rdev) {
3268 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3269 &first_bad, &bad_sectors);
3270 if (s->blocked_rdev == NULL
3271 && (test_bit(Blocked, &rdev->flags)
3272 || is_bad < 0)) {
3273 if (is_bad < 0)
3274 set_bit(BlockedBadBlocks,
3275 &rdev->flags);
3276 s->blocked_rdev = rdev;
3277 atomic_inc(&rdev->nr_pending);
3278 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003279 }
NeilBrown415e72d2010-06-17 17:25:21 +10003280 clear_bit(R5_Insync, &dev->flags);
3281 if (!rdev)
3282 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003283 else if (is_bad) {
3284 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003285 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3286 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003287 /* treat as in-sync, but with a read error
3288 * which we can now try to correct
3289 */
3290 set_bit(R5_Insync, &dev->flags);
3291 set_bit(R5_ReadError, &dev->flags);
3292 }
3293 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003294 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003295 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003296 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003297 set_bit(R5_Insync, &dev->flags);
3298 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3299 test_bit(R5_Expanded, &dev->flags))
3300 /* If we've reshaped into here, we assume it is Insync.
3301 * We will shortly update recovery_offset to make
3302 * it official.
3303 */
3304 set_bit(R5_Insync, &dev->flags);
3305
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003306 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003307 /* This flag does not apply to '.replacement'
3308 * only to .rdev, so make sure to check that*/
3309 struct md_rdev *rdev2 = rcu_dereference(
3310 conf->disks[i].rdev);
3311 if (rdev2 == rdev)
3312 clear_bit(R5_Insync, &dev->flags);
3313 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003314 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003315 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003316 } else
3317 clear_bit(R5_WriteError, &dev->flags);
3318 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003319 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003320 /* This flag does not apply to '.replacement'
3321 * only to .rdev, so make sure to check that*/
3322 struct md_rdev *rdev2 = rcu_dereference(
3323 conf->disks[i].rdev);
3324 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003325 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003326 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003327 } else
3328 clear_bit(R5_MadeGood, &dev->flags);
3329 }
NeilBrown977df362011-12-23 10:17:53 +11003330 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3331 struct md_rdev *rdev2 = rcu_dereference(
3332 conf->disks[i].replacement);
3333 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3334 s->handle_bad_blocks = 1;
3335 atomic_inc(&rdev2->nr_pending);
3336 } else
3337 clear_bit(R5_MadeGoodRepl, &dev->flags);
3338 }
NeilBrown415e72d2010-06-17 17:25:21 +10003339 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003340 /* The ReadError flag will just be confusing now */
3341 clear_bit(R5_ReadError, &dev->flags);
3342 clear_bit(R5_ReWrite, &dev->flags);
3343 }
NeilBrown415e72d2010-06-17 17:25:21 +10003344 if (test_bit(R5_ReadError, &dev->flags))
3345 clear_bit(R5_Insync, &dev->flags);
3346 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003347 if (s->failed < 2)
3348 s->failed_num[s->failed] = i;
3349 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003350 if (rdev && !test_bit(Faulty, &rdev->flags))
3351 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003352 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003353 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003354 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3355 /* If there is a failed device being replaced,
3356 * we must be recovering.
3357 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003358 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003359 * else we can only be replacing
3360 * sync and recovery both need to read all devices, and so
3361 * use the same flag.
3362 */
3363 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003364 sh->sector >= conf->mddev->recovery_cp ||
3365 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003366 s->syncing = 1;
3367 else
3368 s->replacing = 1;
3369 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003370 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003371}
NeilBrownf4168852007-02-28 20:11:53 -08003372
NeilBrowncc940152011-07-26 11:35:35 +10003373static void handle_stripe(struct stripe_head *sh)
3374{
3375 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003376 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003377 int i;
NeilBrown84789552011-07-27 11:00:36 +10003378 int prexor;
3379 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003380 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003381
3382 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003383 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003384 /* already being handled, ensure it gets handled
3385 * again when current action finishes */
3386 set_bit(STRIPE_HANDLE, &sh->state);
3387 return;
3388 }
3389
3390 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3391 set_bit(STRIPE_SYNCING, &sh->state);
3392 clear_bit(STRIPE_INSYNC, &sh->state);
3393 }
3394 clear_bit(STRIPE_DELAYED, &sh->state);
3395
3396 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3397 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3398 (unsigned long long)sh->sector, sh->state,
3399 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3400 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003401
NeilBrownacfe7262011-07-27 11:00:36 +10003402 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003403
NeilBrownbc2607f2011-07-28 11:39:22 +10003404 if (s.handle_bad_blocks) {
3405 set_bit(STRIPE_HANDLE, &sh->state);
3406 goto finish;
3407 }
3408
NeilBrown474af965fe2011-07-27 11:00:36 +10003409 if (unlikely(s.blocked_rdev)) {
3410 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003411 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003412 set_bit(STRIPE_HANDLE, &sh->state);
3413 goto finish;
3414 }
3415 /* There is nothing for the blocked_rdev to block */
3416 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3417 s.blocked_rdev = NULL;
3418 }
3419
3420 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3421 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3422 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3423 }
3424
3425 pr_debug("locked=%d uptodate=%d to_read=%d"
3426 " to_write=%d failed=%d failed_num=%d,%d\n",
3427 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3428 s.failed_num[0], s.failed_num[1]);
3429 /* check if the array has lost more than max_degraded devices and,
3430 * if so, some requests might need to be failed.
3431 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003432 if (s.failed > conf->max_degraded) {
3433 sh->check_state = 0;
3434 sh->reconstruct_state = 0;
3435 if (s.to_read+s.to_write+s.written)
3436 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003437 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003438 handle_failed_sync(conf, sh, &s);
3439 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003440
3441 /*
3442 * might be able to return some write requests if the parity blocks
3443 * are safe, or on a failed drive
3444 */
3445 pdev = &sh->dev[sh->pd_idx];
3446 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3447 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3448 qdev = &sh->dev[sh->qd_idx];
3449 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3450 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3451 || conf->level < 6;
3452
3453 if (s.written &&
3454 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3455 && !test_bit(R5_LOCKED, &pdev->flags)
3456 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3457 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3458 && !test_bit(R5_LOCKED, &qdev->flags)
3459 && test_bit(R5_UPTODATE, &qdev->flags)))))
3460 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3461
3462 /* Now we might consider reading some blocks, either to check/generate
3463 * parity, or to satisfy requests
3464 * or to load a block that is being partially written.
3465 */
3466 if (s.to_read || s.non_overwrite
3467 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003468 || (s.syncing && (s.uptodate + s.compute < disks))
3469 || s.replacing
3470 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003471 handle_stripe_fill(sh, &s, disks);
3472
NeilBrown84789552011-07-27 11:00:36 +10003473 /* Now we check to see if any write operations have recently
3474 * completed
3475 */
3476 prexor = 0;
3477 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3478 prexor = 1;
3479 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3480 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3481 sh->reconstruct_state = reconstruct_state_idle;
3482
3483 /* All the 'written' buffers and the parity block are ready to
3484 * be written back to disk
3485 */
3486 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3487 BUG_ON(sh->qd_idx >= 0 &&
3488 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3489 for (i = disks; i--; ) {
3490 struct r5dev *dev = &sh->dev[i];
3491 if (test_bit(R5_LOCKED, &dev->flags) &&
3492 (i == sh->pd_idx || i == sh->qd_idx ||
3493 dev->written)) {
3494 pr_debug("Writing block %d\n", i);
3495 set_bit(R5_Wantwrite, &dev->flags);
3496 if (prexor)
3497 continue;
3498 if (!test_bit(R5_Insync, &dev->flags) ||
3499 ((i == sh->pd_idx || i == sh->qd_idx) &&
3500 s.failed == 0))
3501 set_bit(STRIPE_INSYNC, &sh->state);
3502 }
3503 }
3504 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3505 s.dec_preread_active = 1;
3506 }
3507
3508 /* Now to consider new write requests and what else, if anything
3509 * should be read. We do not handle new writes when:
3510 * 1/ A 'write' operation (copy+xor) is already in flight.
3511 * 2/ A 'check' operation is in flight, as it may clobber the parity
3512 * block.
3513 */
3514 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3515 handle_stripe_dirtying(conf, sh, &s, disks);
3516
3517 /* maybe we need to check and possibly fix the parity for this stripe
3518 * Any reads will already have been scheduled, so we just see if enough
3519 * data is available. The parity check is held off while parity
3520 * dependent operations are in flight.
3521 */
3522 if (sh->check_state ||
3523 (s.syncing && s.locked == 0 &&
3524 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3525 !test_bit(STRIPE_INSYNC, &sh->state))) {
3526 if (conf->level == 6)
3527 handle_parity_checks6(conf, sh, &s, disks);
3528 else
3529 handle_parity_checks5(conf, sh, &s, disks);
3530 }
NeilBrownc5a31002011-07-27 11:00:36 +10003531
NeilBrown9a3e1102011-12-23 10:17:53 +11003532 if (s.replacing && s.locked == 0
3533 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3534 /* Write out to replacement devices where possible */
3535 for (i = 0; i < conf->raid_disks; i++)
3536 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3537 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3538 set_bit(R5_WantReplace, &sh->dev[i].flags);
3539 set_bit(R5_LOCKED, &sh->dev[i].flags);
3540 s.locked++;
3541 }
3542 set_bit(STRIPE_INSYNC, &sh->state);
3543 }
3544 if ((s.syncing || s.replacing) && s.locked == 0 &&
3545 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003546 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3547 clear_bit(STRIPE_SYNCING, &sh->state);
3548 }
3549
3550 /* If the failed drives are just a ReadError, then we might need
3551 * to progress the repair/check process
3552 */
3553 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3554 for (i = 0; i < s.failed; i++) {
3555 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3556 if (test_bit(R5_ReadError, &dev->flags)
3557 && !test_bit(R5_LOCKED, &dev->flags)
3558 && test_bit(R5_UPTODATE, &dev->flags)
3559 ) {
3560 if (!test_bit(R5_ReWrite, &dev->flags)) {
3561 set_bit(R5_Wantwrite, &dev->flags);
3562 set_bit(R5_ReWrite, &dev->flags);
3563 set_bit(R5_LOCKED, &dev->flags);
3564 s.locked++;
3565 } else {
3566 /* let's read it back */
3567 set_bit(R5_Wantread, &dev->flags);
3568 set_bit(R5_LOCKED, &dev->flags);
3569 s.locked++;
3570 }
3571 }
3572 }
3573
3574
NeilBrown3687c062011-07-27 11:00:36 +10003575 /* Finish reconstruct operations initiated by the expansion process */
3576 if (sh->reconstruct_state == reconstruct_state_result) {
3577 struct stripe_head *sh_src
3578 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3579 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3580 /* sh cannot be written until sh_src has been read.
3581 * so arrange for sh to be delayed a little
3582 */
3583 set_bit(STRIPE_DELAYED, &sh->state);
3584 set_bit(STRIPE_HANDLE, &sh->state);
3585 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3586 &sh_src->state))
3587 atomic_inc(&conf->preread_active_stripes);
3588 release_stripe(sh_src);
3589 goto finish;
3590 }
3591 if (sh_src)
3592 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003593
NeilBrown3687c062011-07-27 11:00:36 +10003594 sh->reconstruct_state = reconstruct_state_idle;
3595 clear_bit(STRIPE_EXPANDING, &sh->state);
3596 for (i = conf->raid_disks; i--; ) {
3597 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3598 set_bit(R5_LOCKED, &sh->dev[i].flags);
3599 s.locked++;
3600 }
3601 }
3602
3603 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3604 !sh->reconstruct_state) {
3605 /* Need to write out all blocks after computing parity */
3606 sh->disks = conf->raid_disks;
3607 stripe_set_idx(sh->sector, conf, 0, sh);
3608 schedule_reconstruction(sh, &s, 1, 1);
3609 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3610 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3611 atomic_dec(&conf->reshape_stripes);
3612 wake_up(&conf->wait_for_overlap);
3613 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3614 }
3615
3616 if (s.expanding && s.locked == 0 &&
3617 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3618 handle_stripe_expansion(conf, sh);
3619
3620finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003621 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003622 if (unlikely(s.blocked_rdev)) {
3623 if (conf->mddev->external)
3624 md_wait_for_blocked_rdev(s.blocked_rdev,
3625 conf->mddev);
3626 else
3627 /* Internal metadata will immediately
3628 * be written by raid5d, so we don't
3629 * need to wait here.
3630 */
3631 rdev_dec_pending(s.blocked_rdev,
3632 conf->mddev);
3633 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003634
NeilBrownbc2607f2011-07-28 11:39:22 +10003635 if (s.handle_bad_blocks)
3636 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003637 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003638 struct r5dev *dev = &sh->dev[i];
3639 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3640 /* We own a safe reference to the rdev */
3641 rdev = conf->disks[i].rdev;
3642 if (!rdev_set_badblocks(rdev, sh->sector,
3643 STRIPE_SECTORS, 0))
3644 md_error(conf->mddev, rdev);
3645 rdev_dec_pending(rdev, conf->mddev);
3646 }
NeilBrownb84db562011-07-28 11:39:23 +10003647 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3648 rdev = conf->disks[i].rdev;
3649 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003650 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003651 rdev_dec_pending(rdev, conf->mddev);
3652 }
NeilBrown977df362011-12-23 10:17:53 +11003653 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3654 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003655 if (!rdev)
3656 /* rdev have been moved down */
3657 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003658 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003659 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003660 rdev_dec_pending(rdev, conf->mddev);
3661 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003662 }
3663
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003664 if (s.ops_request)
3665 raid_run_ops(sh, s.ops_request);
3666
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003667 ops_run_io(sh, &s);
3668
NeilBrownc5709ef2011-07-26 11:35:20 +10003669 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003670 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003671 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003672 * have actually been submitted.
3673 */
3674 atomic_dec(&conf->preread_active_stripes);
3675 if (atomic_read(&conf->preread_active_stripes) <
3676 IO_THRESHOLD)
3677 md_wakeup_thread(conf->mddev->thread);
3678 }
3679
NeilBrownc5709ef2011-07-26 11:35:20 +10003680 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003681
Dan Williams257a4b42011-11-08 16:22:06 +11003682 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003683}
3684
NeilBrownd1688a62011-10-11 16:49:52 +11003685static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686{
3687 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3688 while (!list_empty(&conf->delayed_list)) {
3689 struct list_head *l = conf->delayed_list.next;
3690 struct stripe_head *sh;
3691 sh = list_entry(l, struct stripe_head, lru);
3692 list_del_init(l);
3693 clear_bit(STRIPE_DELAYED, &sh->state);
3694 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3695 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003696 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 }
NeilBrown482c0832011-04-18 18:25:42 +10003698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699}
3700
NeilBrownd1688a62011-10-11 16:49:52 +11003701static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003702{
3703 /* device_lock is held */
3704 struct list_head head;
3705 list_add(&head, &conf->bitmap_list);
3706 list_del_init(&conf->bitmap_list);
3707 while (!list_empty(&head)) {
3708 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3709 list_del_init(&sh->lru);
3710 atomic_inc(&sh->count);
3711 __release_stripe(conf, sh);
3712 }
3713}
3714
NeilBrownfd01b882011-10-11 16:47:53 +11003715int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003716{
NeilBrownd1688a62011-10-11 16:49:52 +11003717 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003718
3719 /* No difference between reads and writes. Just check
3720 * how busy the stripe_cache is
3721 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003722
NeilBrownf022b2f2006-10-03 01:15:56 -07003723 if (conf->inactive_blocked)
3724 return 1;
3725 if (conf->quiesce)
3726 return 1;
3727 if (list_empty_careful(&conf->inactive_list))
3728 return 1;
3729
3730 return 0;
3731}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003732EXPORT_SYMBOL_GPL(md_raid5_congested);
3733
3734static int raid5_congested(void *data, int bits)
3735{
NeilBrownfd01b882011-10-11 16:47:53 +11003736 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003737
3738 return mddev_congested(mddev, bits) ||
3739 md_raid5_congested(mddev, bits);
3740}
NeilBrownf022b2f2006-10-03 01:15:56 -07003741
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003742/* We want read requests to align with chunks where possible,
3743 * but write requests don't need to.
3744 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003745static int raid5_mergeable_bvec(struct request_queue *q,
3746 struct bvec_merge_data *bvm,
3747 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003748{
NeilBrownfd01b882011-10-11 16:47:53 +11003749 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003750 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003751 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003752 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003753 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003754
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003755 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003756 return biovec->bv_len; /* always allow writes to be mergeable */
3757
Andre Noll664e7c42009-06-18 08:45:27 +10003758 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3759 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003760 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3761 if (max < 0) max = 0;
3762 if (max <= biovec->bv_len && bio_sectors == 0)
3763 return biovec->bv_len;
3764 else
3765 return max;
3766}
3767
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003768
NeilBrownfd01b882011-10-11 16:47:53 +11003769static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003770{
3771 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003772 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003773 unsigned int bio_sectors = bio->bi_size >> 9;
3774
Andre Noll664e7c42009-06-18 08:45:27 +10003775 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3776 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003777 return chunk_sectors >=
3778 ((sector & (chunk_sectors - 1)) + bio_sectors);
3779}
3780
3781/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003782 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3783 * later sampled by raid5d.
3784 */
NeilBrownd1688a62011-10-11 16:49:52 +11003785static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003786{
3787 unsigned long flags;
3788
3789 spin_lock_irqsave(&conf->device_lock, flags);
3790
3791 bi->bi_next = conf->retry_read_aligned_list;
3792 conf->retry_read_aligned_list = bi;
3793
3794 spin_unlock_irqrestore(&conf->device_lock, flags);
3795 md_wakeup_thread(conf->mddev->thread);
3796}
3797
3798
NeilBrownd1688a62011-10-11 16:49:52 +11003799static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003800{
3801 struct bio *bi;
3802
3803 bi = conf->retry_read_aligned;
3804 if (bi) {
3805 conf->retry_read_aligned = NULL;
3806 return bi;
3807 }
3808 bi = conf->retry_read_aligned_list;
3809 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003810 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003811 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003812 /*
3813 * this sets the active strip count to 1 and the processed
3814 * strip count to zero (upper 8 bits)
3815 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003816 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003817 }
3818
3819 return bi;
3820}
3821
3822
3823/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003824 * The "raid5_align_endio" should check if the read succeeded and if it
3825 * did, call bio_endio on the original bio (having bio_put the new bio
3826 * first).
3827 * If the read failed..
3828 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003829static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003830{
3831 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003832 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003833 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003834 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003835 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003836
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003837 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003838
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003839 rdev = (void*)raid_bi->bi_next;
3840 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003841 mddev = rdev->mddev;
3842 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003843
3844 rdev_dec_pending(rdev, conf->mddev);
3845
3846 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003847 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003848 if (atomic_dec_and_test(&conf->active_aligned_reads))
3849 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003850 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003851 }
3852
3853
Dan Williams45b42332007-07-09 11:56:43 -07003854 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003855
3856 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003857}
3858
Neil Brown387bb172007-02-08 14:20:29 -08003859static int bio_fits_rdev(struct bio *bi)
3860{
Jens Axboe165125e2007-07-24 09:28:11 +02003861 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003862
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003863 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003864 return 0;
3865 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003866 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003867 return 0;
3868
3869 if (q->merge_bvec_fn)
3870 /* it's too hard to apply the merge_bvec_fn at this stage,
3871 * just just give up
3872 */
3873 return 0;
3874
3875 return 1;
3876}
3877
3878
NeilBrownfd01b882011-10-11 16:47:53 +11003879static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003880{
NeilBrownd1688a62011-10-11 16:49:52 +11003881 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003882 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003883 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003884 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003885 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003886
3887 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003888 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003889 return 0;
3890 }
3891 /*
NeilBrowna167f662010-10-26 18:31:13 +11003892 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003893 */
NeilBrowna167f662010-10-26 18:31:13 +11003894 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003895 if (!align_bi)
3896 return 0;
3897 /*
3898 * set bi_end_io to a new function, and set bi_private to the
3899 * original bio.
3900 */
3901 align_bi->bi_end_io = raid5_align_endio;
3902 align_bi->bi_private = raid_bio;
3903 /*
3904 * compute position
3905 */
NeilBrown112bf892009-03-31 14:39:38 +11003906 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3907 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003908 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003909
NeilBrown671488c2011-12-23 10:17:52 +11003910 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003911 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003912 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3913 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3914 rdev->recovery_offset < end_sector) {
3915 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3916 if (rdev &&
3917 (test_bit(Faulty, &rdev->flags) ||
3918 !(test_bit(In_sync, &rdev->flags) ||
3919 rdev->recovery_offset >= end_sector)))
3920 rdev = NULL;
3921 }
3922 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003923 sector_t first_bad;
3924 int bad_sectors;
3925
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003926 atomic_inc(&rdev->nr_pending);
3927 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003928 raid_bio->bi_next = (void*)rdev;
3929 align_bi->bi_bdev = rdev->bdev;
3930 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003931
NeilBrown31c176e2011-07-28 11:39:22 +10003932 if (!bio_fits_rdev(align_bi) ||
3933 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3934 &first_bad, &bad_sectors)) {
3935 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003936 bio_put(align_bi);
3937 rdev_dec_pending(rdev, mddev);
3938 return 0;
3939 }
3940
majianpeng6c0544e2012-06-12 08:31:10 +08003941 /* No reshape active, so we can trust rdev->data_offset */
3942 align_bi->bi_sector += rdev->data_offset;
3943
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003944 spin_lock_irq(&conf->device_lock);
3945 wait_event_lock_irq(conf->wait_for_stripe,
3946 conf->quiesce == 0,
3947 conf->device_lock, /* nothing */);
3948 atomic_inc(&conf->active_aligned_reads);
3949 spin_unlock_irq(&conf->device_lock);
3950
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003951 generic_make_request(align_bi);
3952 return 1;
3953 } else {
3954 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003955 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003956 return 0;
3957 }
3958}
3959
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003960/* __get_priority_stripe - get the next stripe to process
3961 *
3962 * Full stripe writes are allowed to pass preread active stripes up until
3963 * the bypass_threshold is exceeded. In general the bypass_count
3964 * increments when the handle_list is handled before the hold_list; however, it
3965 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3966 * stripe with in flight i/o. The bypass_count will be reset when the
3967 * head of the hold_list has changed, i.e. the head was promoted to the
3968 * handle_list.
3969 */
NeilBrownd1688a62011-10-11 16:49:52 +11003970static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003971{
3972 struct stripe_head *sh;
3973
3974 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3975 __func__,
3976 list_empty(&conf->handle_list) ? "empty" : "busy",
3977 list_empty(&conf->hold_list) ? "empty" : "busy",
3978 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3979
3980 if (!list_empty(&conf->handle_list)) {
3981 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3982
3983 if (list_empty(&conf->hold_list))
3984 conf->bypass_count = 0;
3985 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3986 if (conf->hold_list.next == conf->last_hold)
3987 conf->bypass_count++;
3988 else {
3989 conf->last_hold = conf->hold_list.next;
3990 conf->bypass_count -= conf->bypass_threshold;
3991 if (conf->bypass_count < 0)
3992 conf->bypass_count = 0;
3993 }
3994 }
3995 } else if (!list_empty(&conf->hold_list) &&
3996 ((conf->bypass_threshold &&
3997 conf->bypass_count > conf->bypass_threshold) ||
3998 atomic_read(&conf->pending_full_writes) == 0)) {
3999 sh = list_entry(conf->hold_list.next,
4000 typeof(*sh), lru);
4001 conf->bypass_count -= conf->bypass_threshold;
4002 if (conf->bypass_count < 0)
4003 conf->bypass_count = 0;
4004 } else
4005 return NULL;
4006
4007 list_del_init(&sh->lru);
4008 atomic_inc(&sh->count);
4009 BUG_ON(atomic_read(&sh->count) != 1);
4010 return sh;
4011}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004012
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004013static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014{
NeilBrownd1688a62011-10-11 16:49:52 +11004015 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004016 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 sector_t new_sector;
4018 sector_t logical_sector, last_sector;
4019 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004020 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004021 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Tejun Heoe9c74692010-09-03 11:56:18 +02004023 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4024 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004025 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004026 }
4027
NeilBrown3d310eb2005-06-21 17:17:26 -07004028 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004029
NeilBrown802ba062006-12-13 00:34:13 -08004030 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004031 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004032 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004033 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004034
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4036 last_sector = bi->bi_sector + (bi->bi_size>>9);
4037 bi->bi_next = NULL;
4038 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004039
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4041 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004042 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004043
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004044 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004045 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004046 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004047 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004048 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004049 * 64bit on a 32bit platform, and so it might be
4050 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004051 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004052 * the lock is dropped, so once we get a reference
4053 * to the stripe that we think it is, we will have
4054 * to check again.
4055 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004056 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004057 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004058 ? logical_sector < conf->reshape_progress
4059 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004060 previous = 1;
4061 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004062 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004063 ? logical_sector < conf->reshape_safe
4064 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004065 spin_unlock_irq(&conf->device_lock);
4066 schedule();
4067 goto retry;
4068 }
4069 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004070 spin_unlock_irq(&conf->device_lock);
4071 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004072
NeilBrown112bf892009-03-31 14:39:38 +11004073 new_sector = raid5_compute_sector(conf, logical_sector,
4074 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004075 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004076 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 (unsigned long long)new_sector,
4078 (unsigned long long)logical_sector);
4079
NeilBrownb5663ba2009-03-31 14:39:38 +11004080 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004081 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004083 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004084 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004085 * stripe, so we must do the range check again.
4086 * Expansion could still move past after this
4087 * test, but as we are holding a reference to
4088 * 'sh', we know that if that happens,
4089 * STRIPE_EXPANDING will get set and the expansion
4090 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004091 */
4092 int must_retry = 0;
4093 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004094 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004095 ? logical_sector >= conf->reshape_progress
4096 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004097 /* mismatch, need to try again */
4098 must_retry = 1;
4099 spin_unlock_irq(&conf->device_lock);
4100 if (must_retry) {
4101 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004102 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004103 goto retry;
4104 }
4105 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004106
Namhyung Kimffd96e32011-07-18 17:38:51 +10004107 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004108 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004109 logical_sector < mddev->suspend_hi) {
4110 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004111 /* As the suspend_* range is controlled by
4112 * userspace, we want an interruptible
4113 * wait.
4114 */
4115 flush_signals(current);
4116 prepare_to_wait(&conf->wait_for_overlap,
4117 &w, TASK_INTERRUPTIBLE);
4118 if (logical_sector >= mddev->suspend_lo &&
4119 logical_sector < mddev->suspend_hi)
4120 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004121 goto retry;
4122 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004123
4124 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004125 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004126 /* Stripe is busy expanding or
4127 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 * and wait a while
4129 */
NeilBrown482c0832011-04-18 18:25:42 +10004130 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 release_stripe(sh);
4132 schedule();
4133 goto retry;
4134 }
4135 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004136 set_bit(STRIPE_HANDLE, &sh->state);
4137 clear_bit(STRIPE_DELAYED, &sh->state);
Tejun Heoe9c74692010-09-03 11:56:18 +02004138 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004139 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4140 atomic_inc(&conf->preread_active_stripes);
NeilBrownb357f042012-07-03 17:45:31 +10004141 mddev_check_plugged(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 } else {
4144 /* cannot get stripe for read-ahead, just give-up */
4145 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4146 finish_wait(&conf->wait_for_overlap, &w);
4147 break;
4148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004150
Shaohua Lie7836bd62012-07-19 16:01:31 +10004151 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004152 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
NeilBrown16a53ec2006-06-26 00:27:38 -07004154 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004156
Neil Brown0e13fe232008-06-28 08:31:20 +10004157 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159}
4160
NeilBrownfd01b882011-10-11 16:47:53 +11004161static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004162
NeilBrownfd01b882011-10-11 16:47:53 +11004163static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164{
NeilBrown52c03292006-06-26 00:27:43 -07004165 /* reshaping is quite different to recovery/resync so it is
4166 * handled quite separately ... here.
4167 *
4168 * On each call to sync_request, we gather one chunk worth of
4169 * destination stripes and flag them as expanding.
4170 * Then we find all the source stripes and request reads.
4171 * As the reads complete, handle_stripe will copy the data
4172 * into the destination stripe and release that stripe.
4173 */
NeilBrownd1688a62011-10-11 16:49:52 +11004174 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004176 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004177 int raid_disks = conf->previous_raid_disks;
4178 int data_disks = raid_disks - conf->max_degraded;
4179 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004180 int i;
4181 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004182 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004183 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004184 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004185 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004186
NeilBrownfef9c612009-03-31 15:16:46 +11004187 if (sector_nr == 0) {
4188 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004189 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004190 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4191 sector_nr = raid5_size(mddev, 0, 0)
4192 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004193 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004194 conf->reshape_progress > 0)
4195 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004196 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004197 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004198 mddev->curr_resync_completed = sector_nr;
4199 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004200 *skipped = 1;
4201 return sector_nr;
4202 }
NeilBrown52c03292006-06-26 00:27:43 -07004203 }
4204
NeilBrown7a661382009-03-31 15:21:40 +11004205 /* We need to process a full chunk at a time.
4206 * If old and new chunk sizes differ, we need to process the
4207 * largest of these
4208 */
Andre Noll664e7c42009-06-18 08:45:27 +10004209 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4210 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004211 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004212 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004213
NeilBrownb5254dd2012-05-21 09:27:01 +10004214 /* We update the metadata at least every 10 seconds, or when
4215 * the data about to be copied would over-write the source of
4216 * the data at the front of the range. i.e. one new_stripe
4217 * along from reshape_progress new_maps to after where
4218 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004219 */
NeilBrownfef9c612009-03-31 15:16:46 +11004220 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004221 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004222 readpos = conf->reshape_progress;
4223 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004224 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004225 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004226 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004227 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004228 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004229 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004230 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004231 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004232 readpos -= min_t(sector_t, reshape_sectors, readpos);
4233 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004234 }
NeilBrown52c03292006-06-26 00:27:43 -07004235
NeilBrownb5254dd2012-05-21 09:27:01 +10004236 /* Having calculated the 'writepos' possibly use it
4237 * to set 'stripe_addr' which is where we will write to.
4238 */
4239 if (mddev->reshape_backwards) {
4240 BUG_ON(conf->reshape_progress == 0);
4241 stripe_addr = writepos;
4242 BUG_ON((mddev->dev_sectors &
4243 ~((sector_t)reshape_sectors - 1))
4244 - reshape_sectors - stripe_addr
4245 != sector_nr);
4246 } else {
4247 BUG_ON(writepos != sector_nr + reshape_sectors);
4248 stripe_addr = sector_nr;
4249 }
4250
NeilBrownc8f517c2009-03-31 15:28:40 +11004251 /* 'writepos' is the most advanced device address we might write.
4252 * 'readpos' is the least advanced device address we might read.
4253 * 'safepos' is the least address recorded in the metadata as having
4254 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004255 * If there is a min_offset_diff, these are adjusted either by
4256 * increasing the safepos/readpos if diff is negative, or
4257 * increasing writepos if diff is positive.
4258 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004259 * ensure safety in the face of a crash - that must be done by userspace
4260 * making a backup of the data. So in that case there is no particular
4261 * rush to update metadata.
4262 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4263 * update the metadata to advance 'safepos' to match 'readpos' so that
4264 * we can be safe in the event of a crash.
4265 * So we insist on updating metadata if safepos is behind writepos and
4266 * readpos is beyond writepos.
4267 * In any case, update the metadata every 10 seconds.
4268 * Maybe that number should be configurable, but I'm not sure it is
4269 * worth it.... maybe it could be a multiple of safemode_delay???
4270 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004271 if (conf->min_offset_diff < 0) {
4272 safepos += -conf->min_offset_diff;
4273 readpos += -conf->min_offset_diff;
4274 } else
4275 writepos += conf->min_offset_diff;
4276
NeilBrown2c810cd2012-05-21 09:27:00 +10004277 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004278 ? (safepos > writepos && readpos < writepos)
4279 : (safepos < writepos && readpos > writepos)) ||
4280 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004281 /* Cannot proceed until we've updated the superblock... */
4282 wait_event(conf->wait_for_overlap,
4283 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004284 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004285 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004286 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004287 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004288 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004289 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004290 kthread_should_stop());
4291 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004292 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004293 spin_unlock_irq(&conf->device_lock);
4294 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004295 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004296 }
4297
NeilBrownab69ae12009-03-31 15:26:47 +11004298 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004299 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004300 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004301 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004302 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004303 set_bit(STRIPE_EXPANDING, &sh->state);
4304 atomic_inc(&conf->reshape_stripes);
4305 /* If any of this stripe is beyond the end of the old
4306 * array, then we need to zero those blocks
4307 */
4308 for (j=sh->disks; j--;) {
4309 sector_t s;
4310 if (j == sh->pd_idx)
4311 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004312 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004313 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004314 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004315 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004316 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004317 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004318 continue;
4319 }
4320 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4321 set_bit(R5_Expanded, &sh->dev[j].flags);
4322 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4323 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004324 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004325 set_bit(STRIPE_EXPAND_READY, &sh->state);
4326 set_bit(STRIPE_HANDLE, &sh->state);
4327 }
NeilBrownab69ae12009-03-31 15:26:47 +11004328 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004329 }
4330 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004331 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004332 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004333 else
NeilBrown7a661382009-03-31 15:21:40 +11004334 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004335 spin_unlock_irq(&conf->device_lock);
4336 /* Ok, those stripe are ready. We can start scheduling
4337 * reads on the source stripes.
4338 * The source stripes are determined by mapping the first and last
4339 * block on the destination stripes.
4340 */
NeilBrown52c03292006-06-26 00:27:43 -07004341 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004342 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004343 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004344 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004345 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004346 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004347 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004348 if (last_sector >= mddev->dev_sectors)
4349 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004350 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004351 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004352 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4353 set_bit(STRIPE_HANDLE, &sh->state);
4354 release_stripe(sh);
4355 first_sector += STRIPE_SECTORS;
4356 }
NeilBrownab69ae12009-03-31 15:26:47 +11004357 /* Now that the sources are clearly marked, we can release
4358 * the destination stripes
4359 */
4360 while (!list_empty(&stripes)) {
4361 sh = list_entry(stripes.next, struct stripe_head, lru);
4362 list_del_init(&sh->lru);
4363 release_stripe(sh);
4364 }
NeilBrownc6207272008-02-06 01:39:52 -08004365 /* If this takes us to the resync_max point where we have to pause,
4366 * then we need to write out the superblock.
4367 */
NeilBrown7a661382009-03-31 15:21:40 +11004368 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004369 if ((sector_nr - mddev->curr_resync_completed) * 2
4370 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004371 /* Cannot proceed until we've updated the superblock... */
4372 wait_event(conf->wait_for_overlap,
4373 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004374 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004375 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004376 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004377 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4378 md_wakeup_thread(mddev->thread);
4379 wait_event(mddev->sb_wait,
4380 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4381 || kthread_should_stop());
4382 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004383 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004384 spin_unlock_irq(&conf->device_lock);
4385 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004386 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004387 }
NeilBrown7a661382009-03-31 15:21:40 +11004388 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004389}
4390
4391/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004392static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004393{
NeilBrownd1688a62011-10-11 16:49:52 +11004394 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004395 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004396 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004397 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004398 int still_degraded = 0;
4399 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
NeilBrown72626682005-09-09 16:23:54 -07004401 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004403
NeilBrown29269552006-03-27 01:18:10 -08004404 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4405 end_reshape(conf);
4406 return 0;
4407 }
NeilBrown72626682005-09-09 16:23:54 -07004408
4409 if (mddev->curr_resync < max_sector) /* aborted */
4410 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4411 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004412 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004413 conf->fullsync = 0;
4414 bitmap_close_sync(mddev->bitmap);
4415
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 return 0;
4417 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004418
NeilBrown64bd6602009-08-03 10:59:58 +10004419 /* Allow raid5_quiesce to complete */
4420 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4421
NeilBrown52c03292006-06-26 00:27:43 -07004422 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4423 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004424
NeilBrownc6207272008-02-06 01:39:52 -08004425 /* No need to check resync_max as we never do more than one
4426 * stripe, and as resync_max will always be on a chunk boundary,
4427 * if the check in md_do_sync didn't fire, there is no chance
4428 * of overstepping resync_max here
4429 */
4430
NeilBrown16a53ec2006-06-26 00:27:38 -07004431 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 * to resync, then assert that we are finished, because there is
4433 * nothing we can do.
4434 */
NeilBrown3285edf2006-06-26 00:27:55 -07004435 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004436 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004437 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004438 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 return rv;
4440 }
NeilBrown72626682005-09-09 16:23:54 -07004441 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004442 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004443 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4444 /* we can skip this block, and probably more */
4445 sync_blocks /= STRIPE_SECTORS;
4446 *skipped = 1;
4447 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
NeilBrownb47490c2008-02-06 01:39:50 -08004450 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4451
NeilBrowna8c906c2009-06-09 14:39:59 +10004452 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004454 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004456 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004458 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004460 /* Need to check if array will still be degraded after recovery/resync
4461 * We don't need to check the 'failed' flag as when that gets set,
4462 * recovery aborts.
4463 */
NeilBrownf001a702009-06-09 14:30:31 +10004464 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004465 if (conf->disks[i].rdev == NULL)
4466 still_degraded = 1;
4467
4468 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4469
NeilBrown83206d62011-07-26 11:19:49 +10004470 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
NeilBrown14425772009-10-16 15:55:25 +11004472 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 release_stripe(sh);
4474
4475 return STRIPE_SECTORS;
4476}
4477
NeilBrownd1688a62011-10-11 16:49:52 +11004478static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004479{
4480 /* We may not be able to submit a whole bio at once as there
4481 * may not be enough stripe_heads available.
4482 * We cannot pre-allocate enough stripe_heads as we may need
4483 * more than exist in the cache (if we allow ever large chunks).
4484 * So we do one stripe head at a time and record in
4485 * ->bi_hw_segments how many have been done.
4486 *
4487 * We *know* that this entire raid_bio is in one chunk, so
4488 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4489 */
4490 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004491 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004492 sector_t sector, logical_sector, last_sector;
4493 int scnt = 0;
4494 int remaining;
4495 int handled = 0;
4496
4497 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004498 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004499 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004500 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4501
4502 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004503 logical_sector += STRIPE_SECTORS,
4504 sector += STRIPE_SECTORS,
4505 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004506
Shaohua Lie7836bd62012-07-19 16:01:31 +10004507 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004508 /* already done this stripe */
4509 continue;
4510
NeilBrowna8c906c2009-06-09 14:39:59 +10004511 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004512
4513 if (!sh) {
4514 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004515 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004516 conf->retry_read_aligned = raid_bio;
4517 return handled;
4518 }
4519
Neil Brown387bb172007-02-08 14:20:29 -08004520 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4521 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004522 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004523 conf->retry_read_aligned = raid_bio;
4524 return handled;
4525 }
4526
majianpeng3f9e7c12012-07-31 10:04:21 +10004527 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004528 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004529 release_stripe(sh);
4530 handled++;
4531 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004532 remaining = raid5_dec_bi_active_stripes(raid_bio);
Neil Brown0e13fe232008-06-28 08:31:20 +10004533 if (remaining == 0)
4534 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004535 if (atomic_dec_and_test(&conf->active_aligned_reads))
4536 wake_up(&conf->wait_for_stripe);
4537 return handled;
4538}
4539
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004540
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541/*
4542 * This is our raid5 kernel thread.
4543 *
4544 * We scan the hash table for stripes which can be handled now.
4545 * During the scan, completed stripes are saved for us by the interrupt
4546 * handler, so that they will not have to wait for our next wakeup.
4547 */
NeilBrownfd01b882011-10-11 16:47:53 +11004548static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549{
4550 struct stripe_head *sh;
NeilBrownd1688a62011-10-11 16:49:52 +11004551 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004553 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
Dan Williams45b42332007-07-09 11:56:43 -07004555 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556
4557 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004559 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 handled = 0;
4561 spin_lock_irq(&conf->device_lock);
4562 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004563 struct bio *bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
NeilBrown7c13edc2011-04-18 18:25:43 +10004565 if (atomic_read(&mddev->plug_cnt) == 0 &&
4566 !list_empty(&conf->bitmap_list)) {
4567 /* Now is a good time to flush some bitmap updates */
4568 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004569 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004570 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004571 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004572 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004573 activate_bit_delay(conf);
4574 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004575 if (atomic_read(&mddev->plug_cnt) == 0)
4576 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004577
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004578 while ((bio = remove_bio_from_retry(conf))) {
4579 int ok;
4580 spin_unlock_irq(&conf->device_lock);
4581 ok = retry_aligned_read(conf, bio);
4582 spin_lock_irq(&conf->device_lock);
4583 if (!ok)
4584 break;
4585 handled++;
4586 }
4587
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004588 sh = __get_priority_stripe(conf);
4589
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004590 if (!sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 spin_unlock_irq(&conf->device_lock);
4593
4594 handled++;
Dan Williams417b8d42009-10-16 16:25:22 +11004595 handle_stripe(sh);
4596 release_stripe(sh);
4597 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
NeilBrownde393cd2011-07-28 11:31:48 +10004599 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4600 md_check_recovery(mddev);
4601
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 spin_lock_irq(&conf->device_lock);
4603 }
Dan Williams45b42332007-07-09 11:56:43 -07004604 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
4606 spin_unlock_irq(&conf->device_lock);
4607
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004608 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004609 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
Dan Williams45b42332007-07-09 11:56:43 -07004611 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612}
4613
NeilBrown3f294f42005-11-08 21:39:25 -08004614static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004615raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004616{
NeilBrownd1688a62011-10-11 16:49:52 +11004617 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004618 if (conf)
4619 return sprintf(page, "%d\n", conf->max_nr_stripes);
4620 else
4621 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004622}
4623
NeilBrownc41d4ac2010-06-01 19:37:24 +10004624int
NeilBrownfd01b882011-10-11 16:47:53 +11004625raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004626{
NeilBrownd1688a62011-10-11 16:49:52 +11004627 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004628 int err;
4629
4630 if (size <= 16 || size > 32768)
4631 return -EINVAL;
4632 while (size < conf->max_nr_stripes) {
4633 if (drop_one_stripe(conf))
4634 conf->max_nr_stripes--;
4635 else
4636 break;
4637 }
4638 err = md_allow_write(mddev);
4639 if (err)
4640 return err;
4641 while (size > conf->max_nr_stripes) {
4642 if (grow_one_stripe(conf))
4643 conf->max_nr_stripes++;
4644 else break;
4645 }
4646 return 0;
4647}
4648EXPORT_SYMBOL(raid5_set_cache_size);
4649
NeilBrown3f294f42005-11-08 21:39:25 -08004650static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004651raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004652{
NeilBrownd1688a62011-10-11 16:49:52 +11004653 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004654 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004655 int err;
4656
NeilBrown3f294f42005-11-08 21:39:25 -08004657 if (len >= PAGE_SIZE)
4658 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004659 if (!conf)
4660 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004661
Dan Williams4ef197d82008-04-28 02:15:54 -07004662 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004663 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004664 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004665 if (err)
4666 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004667 return len;
4668}
NeilBrown007583c2005-11-08 21:39:30 -08004669
NeilBrown96de1e62005-11-08 21:39:39 -08004670static struct md_sysfs_entry
4671raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4672 raid5_show_stripe_cache_size,
4673 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004674
4675static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004676raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004677{
NeilBrownd1688a62011-10-11 16:49:52 +11004678 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004679 if (conf)
4680 return sprintf(page, "%d\n", conf->bypass_threshold);
4681 else
4682 return 0;
4683}
4684
4685static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004686raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004687{
NeilBrownd1688a62011-10-11 16:49:52 +11004688 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004689 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004690 if (len >= PAGE_SIZE)
4691 return -EINVAL;
4692 if (!conf)
4693 return -ENODEV;
4694
Dan Williams4ef197d82008-04-28 02:15:54 -07004695 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004696 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004697 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004698 return -EINVAL;
4699 conf->bypass_threshold = new;
4700 return len;
4701}
4702
4703static struct md_sysfs_entry
4704raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4705 S_IRUGO | S_IWUSR,
4706 raid5_show_preread_threshold,
4707 raid5_store_preread_threshold);
4708
4709static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004710stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004711{
NeilBrownd1688a62011-10-11 16:49:52 +11004712 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004713 if (conf)
4714 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4715 else
4716 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004717}
4718
NeilBrown96de1e62005-11-08 21:39:39 -08004719static struct md_sysfs_entry
4720raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004721
NeilBrown007583c2005-11-08 21:39:30 -08004722static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004723 &raid5_stripecache_size.attr,
4724 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004725 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004726 NULL,
4727};
NeilBrown007583c2005-11-08 21:39:30 -08004728static struct attribute_group raid5_attrs_group = {
4729 .name = NULL,
4730 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004731};
4732
Dan Williams80c3a6c2009-03-17 18:10:40 -07004733static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004734raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004735{
NeilBrownd1688a62011-10-11 16:49:52 +11004736 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004737
4738 if (!sectors)
4739 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004740 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004741 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004742 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004743
Andre Noll9d8f0362009-06-18 08:45:01 +10004744 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004745 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004746 return sectors * (raid_disks - conf->max_degraded);
4747}
4748
NeilBrownd1688a62011-10-11 16:49:52 +11004749static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004750{
4751 struct raid5_percpu *percpu;
4752 unsigned long cpu;
4753
4754 if (!conf->percpu)
4755 return;
4756
4757 get_online_cpus();
4758 for_each_possible_cpu(cpu) {
4759 percpu = per_cpu_ptr(conf->percpu, cpu);
4760 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004761 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004762 }
4763#ifdef CONFIG_HOTPLUG_CPU
4764 unregister_cpu_notifier(&conf->cpu_notify);
4765#endif
4766 put_online_cpus();
4767
4768 free_percpu(conf->percpu);
4769}
4770
NeilBrownd1688a62011-10-11 16:49:52 +11004771static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004772{
4773 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004774 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004775 kfree(conf->disks);
4776 kfree(conf->stripe_hashtbl);
4777 kfree(conf);
4778}
4779
Dan Williams36d1c642009-07-14 11:48:22 -07004780#ifdef CONFIG_HOTPLUG_CPU
4781static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4782 void *hcpu)
4783{
NeilBrownd1688a62011-10-11 16:49:52 +11004784 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004785 long cpu = (long)hcpu;
4786 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4787
4788 switch (action) {
4789 case CPU_UP_PREPARE:
4790 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004791 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004792 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004793 if (!percpu->scribble)
4794 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4795
4796 if (!percpu->scribble ||
4797 (conf->level == 6 && !percpu->spare_page)) {
4798 safe_put_page(percpu->spare_page);
4799 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004800 pr_err("%s: failed memory allocation for cpu%ld\n",
4801 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004802 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004803 }
4804 break;
4805 case CPU_DEAD:
4806 case CPU_DEAD_FROZEN:
4807 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004808 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004809 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004810 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004811 break;
4812 default:
4813 break;
4814 }
4815 return NOTIFY_OK;
4816}
4817#endif
4818
NeilBrownd1688a62011-10-11 16:49:52 +11004819static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004820{
4821 unsigned long cpu;
4822 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004823 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004824 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004825 int err;
4826
Dan Williams36d1c642009-07-14 11:48:22 -07004827 allcpus = alloc_percpu(struct raid5_percpu);
4828 if (!allcpus)
4829 return -ENOMEM;
4830 conf->percpu = allcpus;
4831
4832 get_online_cpus();
4833 err = 0;
4834 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004835 if (conf->level == 6) {
4836 spare_page = alloc_page(GFP_KERNEL);
4837 if (!spare_page) {
4838 err = -ENOMEM;
4839 break;
4840 }
4841 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4842 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004843 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004844 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004845 err = -ENOMEM;
4846 break;
4847 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004848 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004849 }
4850#ifdef CONFIG_HOTPLUG_CPU
4851 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4852 conf->cpu_notify.priority = 0;
4853 if (err == 0)
4854 err = register_cpu_notifier(&conf->cpu_notify);
4855#endif
4856 put_online_cpus();
4857
4858 return err;
4859}
4860
NeilBrownd1688a62011-10-11 16:49:52 +11004861static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862{
NeilBrownd1688a62011-10-11 16:49:52 +11004863 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004864 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004865 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10004867 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
NeilBrown91adb562009-03-31 14:39:39 +11004869 if (mddev->new_level != 5
4870 && mddev->new_level != 4
4871 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004872 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004873 mdname(mddev), mddev->new_level);
4874 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 }
NeilBrown91adb562009-03-31 14:39:39 +11004876 if ((mddev->new_level == 5
4877 && !algorithm_valid_raid5(mddev->new_layout)) ||
4878 (mddev->new_level == 6
4879 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004880 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004881 mdname(mddev), mddev->new_layout);
4882 return ERR_PTR(-EIO);
4883 }
4884 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004885 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004886 mdname(mddev), mddev->raid_disks);
4887 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
Andre Noll664e7c42009-06-18 08:45:27 +10004890 if (!mddev->new_chunk_sectors ||
4891 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4892 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004893 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4894 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004895 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004896 }
4897
NeilBrownd1688a62011-10-11 16:49:52 +11004898 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004899 if (conf == NULL)
4900 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004901 spin_lock_init(&conf->device_lock);
4902 init_waitqueue_head(&conf->wait_for_stripe);
4903 init_waitqueue_head(&conf->wait_for_overlap);
4904 INIT_LIST_HEAD(&conf->handle_list);
4905 INIT_LIST_HEAD(&conf->hold_list);
4906 INIT_LIST_HEAD(&conf->delayed_list);
4907 INIT_LIST_HEAD(&conf->bitmap_list);
4908 INIT_LIST_HEAD(&conf->inactive_list);
4909 atomic_set(&conf->active_stripes, 0);
4910 atomic_set(&conf->preread_active_stripes, 0);
4911 atomic_set(&conf->active_aligned_reads, 0);
4912 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004913 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004914
4915 conf->raid_disks = mddev->raid_disks;
4916 if (mddev->reshape_position == MaxSector)
4917 conf->previous_raid_disks = mddev->raid_disks;
4918 else
4919 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004920 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4921 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11004922
NeilBrown5e5e3e72009-10-16 16:35:30 +11004923 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11004924 GFP_KERNEL);
4925 if (!conf->disks)
4926 goto abort;
4927
4928 conf->mddev = mddev;
4929
4930 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4931 goto abort;
4932
Dan Williams36d1c642009-07-14 11:48:22 -07004933 conf->level = mddev->new_level;
4934 if (raid5_alloc_percpu(conf) != 0)
4935 goto abort;
4936
NeilBrown0c55e022010-05-03 14:09:02 +10004937 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11004938
NeilBrowndafb20f2012-03-19 12:46:39 +11004939 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11004940 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004941 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11004942 || raid_disk < 0)
4943 continue;
4944 disk = conf->disks + raid_disk;
4945
NeilBrown17045f52011-12-23 10:17:53 +11004946 if (test_bit(Replacement, &rdev->flags)) {
4947 if (disk->replacement)
4948 goto abort;
4949 disk->replacement = rdev;
4950 } else {
4951 if (disk->rdev)
4952 goto abort;
4953 disk->rdev = rdev;
4954 }
NeilBrown91adb562009-03-31 14:39:39 +11004955
4956 if (test_bit(In_sync, &rdev->flags)) {
4957 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10004958 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4959 " disk %d\n",
4960 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05004961 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11004962 /* Cannot rely on bitmap to complete recovery */
4963 conf->fullsync = 1;
4964 }
4965
Andre Noll09c9e5f2009-06-18 08:45:55 +10004966 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11004967 conf->level = mddev->new_level;
4968 if (conf->level == 6)
4969 conf->max_degraded = 2;
4970 else
4971 conf->max_degraded = 1;
4972 conf->algorithm = mddev->new_layout;
4973 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11004974 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11004975 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10004976 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11004977 conf->prev_algo = mddev->layout;
4978 }
NeilBrown91adb562009-03-31 14:39:39 +11004979
4980 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11004981 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11004982 if (grow_stripes(conf, conf->max_nr_stripes)) {
4983 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004984 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4985 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004986 goto abort;
4987 } else
NeilBrown0c55e022010-05-03 14:09:02 +10004988 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4989 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11004990
NeilBrown02326052012-07-03 15:56:52 +10004991 sprintf(pers_name, "raid%d", mddev->new_level);
4992 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11004993 if (!conf->thread) {
4994 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10004995 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11004996 mdname(mddev));
4997 goto abort;
4998 }
4999
5000 return conf;
5001
5002 abort:
5003 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005004 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005005 return ERR_PTR(-EIO);
5006 } else
5007 return ERR_PTR(-ENOMEM);
5008}
5009
NeilBrownc148ffd2009-11-13 17:47:00 +11005010
5011static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5012{
5013 switch (algo) {
5014 case ALGORITHM_PARITY_0:
5015 if (raid_disk < max_degraded)
5016 return 1;
5017 break;
5018 case ALGORITHM_PARITY_N:
5019 if (raid_disk >= raid_disks - max_degraded)
5020 return 1;
5021 break;
5022 case ALGORITHM_PARITY_0_6:
5023 if (raid_disk == 0 ||
5024 raid_disk == raid_disks - 1)
5025 return 1;
5026 break;
5027 case ALGORITHM_LEFT_ASYMMETRIC_6:
5028 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5029 case ALGORITHM_LEFT_SYMMETRIC_6:
5030 case ALGORITHM_RIGHT_SYMMETRIC_6:
5031 if (raid_disk == raid_disks - 1)
5032 return 1;
5033 }
5034 return 0;
5035}
5036
NeilBrownfd01b882011-10-11 16:47:53 +11005037static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005038{
NeilBrownd1688a62011-10-11 16:49:52 +11005039 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005040 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005041 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005042 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005043 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005044 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005045 long long min_offset_diff = 0;
5046 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005047
Andre Noll8c6ac862009-06-18 08:48:06 +10005048 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005049 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005050 " -- starting background reconstruction\n",
5051 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005052
5053 rdev_for_each(rdev, mddev) {
5054 long long diff;
5055 if (rdev->raid_disk < 0)
5056 continue;
5057 diff = (rdev->new_data_offset - rdev->data_offset);
5058 if (first) {
5059 min_offset_diff = diff;
5060 first = 0;
5061 } else if (mddev->reshape_backwards &&
5062 diff < min_offset_diff)
5063 min_offset_diff = diff;
5064 else if (!mddev->reshape_backwards &&
5065 diff > min_offset_diff)
5066 min_offset_diff = diff;
5067 }
5068
NeilBrownf6705572006-03-27 01:18:11 -08005069 if (mddev->reshape_position != MaxSector) {
5070 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005071 * Difficulties arise if the stripe we would write to
5072 * next is at or after the stripe we would read from next.
5073 * For a reshape that changes the number of devices, this
5074 * is only possible for a very short time, and mdadm makes
5075 * sure that time appears to have past before assembling
5076 * the array. So we fail if that time hasn't passed.
5077 * For a reshape that keeps the number of devices the same
5078 * mdadm must be monitoring the reshape can keeping the
5079 * critical areas read-only and backed up. It will start
5080 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005081 */
5082 sector_t here_new, here_old;
5083 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005084 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005085
NeilBrown88ce4932009-03-31 15:24:23 +11005086 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005087 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005088 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005089 mdname(mddev));
5090 return -EINVAL;
5091 }
NeilBrownf6705572006-03-27 01:18:11 -08005092 old_disks = mddev->raid_disks - mddev->delta_disks;
5093 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005094 * further up in new geometry must map after here in old
5095 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005096 */
5097 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005098 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005099 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005100 printk(KERN_ERR "md/raid:%s: reshape_position not "
5101 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005102 return -EINVAL;
5103 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005104 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005105 /* here_new is the stripe we will write to */
5106 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005107 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005108 (old_disks-max_degraded));
5109 /* here_old is the first stripe that we might need to read
5110 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005111 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005112 if ((here_new * mddev->new_chunk_sectors !=
5113 here_old * mddev->chunk_sectors)) {
5114 printk(KERN_ERR "md/raid:%s: reshape position is"
5115 " confused - aborting\n", mdname(mddev));
5116 return -EINVAL;
5117 }
NeilBrown67ac6012009-08-13 10:06:24 +10005118 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005119 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005120 * and taking constant backups.
5121 * mdadm always starts a situation like this in
5122 * readonly mode so it can take control before
5123 * allowing any writes. So just check for that.
5124 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005125 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5126 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5127 /* not really in-place - so OK */;
5128 else if (mddev->ro == 0) {
5129 printk(KERN_ERR "md/raid:%s: in-place reshape "
5130 "must be started in read-only mode "
5131 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005132 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005133 return -EINVAL;
5134 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005135 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005136 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005137 here_old * mddev->chunk_sectors)
5138 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005139 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005140 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005141 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5142 "auto-recovery - aborting.\n",
5143 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005144 return -EINVAL;
5145 }
NeilBrown0c55e022010-05-03 14:09:02 +10005146 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5147 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005148 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005149 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005150 BUG_ON(mddev->level != mddev->new_level);
5151 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005152 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005153 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005154 }
5155
NeilBrown245f46c2009-03-31 14:39:39 +11005156 if (mddev->private == NULL)
5157 conf = setup_conf(mddev);
5158 else
5159 conf = mddev->private;
5160
NeilBrown91adb562009-03-31 14:39:39 +11005161 if (IS_ERR(conf))
5162 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005163
NeilBrownb5254dd2012-05-21 09:27:01 +10005164 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005165 mddev->thread = conf->thread;
5166 conf->thread = NULL;
5167 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168
NeilBrown17045f52011-12-23 10:17:53 +11005169 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5170 i++) {
5171 rdev = conf->disks[i].rdev;
5172 if (!rdev && conf->disks[i].replacement) {
5173 /* The replacement is all we have yet */
5174 rdev = conf->disks[i].replacement;
5175 conf->disks[i].replacement = NULL;
5176 clear_bit(Replacement, &rdev->flags);
5177 conf->disks[i].rdev = rdev;
5178 }
5179 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005180 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005181 if (conf->disks[i].replacement &&
5182 conf->reshape_progress != MaxSector) {
5183 /* replacements and reshape simply do not mix. */
5184 printk(KERN_ERR "md: cannot handle concurrent "
5185 "replacement and reshape.\n");
5186 goto abort;
5187 }
NeilBrown2f115882010-06-17 17:41:03 +10005188 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005189 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005190 continue;
5191 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005192 /* This disc is not fully in-sync. However if it
5193 * just stored parity (beyond the recovery_offset),
5194 * when we don't need to be concerned about the
5195 * array being dirty.
5196 * When reshape goes 'backwards', we never have
5197 * partially completed devices, so we only need
5198 * to worry about reshape going forwards.
5199 */
5200 /* Hack because v0.91 doesn't store recovery_offset properly. */
5201 if (mddev->major_version == 0 &&
5202 mddev->minor_version > 90)
5203 rdev->recovery_offset = reshape_offset;
5204
NeilBrownc148ffd2009-11-13 17:47:00 +11005205 if (rdev->recovery_offset < reshape_offset) {
5206 /* We need to check old and new layout */
5207 if (!only_parity(rdev->raid_disk,
5208 conf->algorithm,
5209 conf->raid_disks,
5210 conf->max_degraded))
5211 continue;
5212 }
5213 if (!only_parity(rdev->raid_disk,
5214 conf->prev_algo,
5215 conf->previous_raid_disks,
5216 conf->max_degraded))
5217 continue;
5218 dirty_parity_disks++;
5219 }
NeilBrown91adb562009-03-31 14:39:39 +11005220
NeilBrown17045f52011-12-23 10:17:53 +11005221 /*
5222 * 0 for a fully functional array, 1 or 2 for a degraded array.
5223 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005224 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
NeilBrown674806d2010-06-16 17:17:53 +10005226 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005227 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005229 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 goto abort;
5231 }
5232
NeilBrown91adb562009-03-31 14:39:39 +11005233 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005234 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005235 mddev->resync_max_sectors = mddev->dev_sectors;
5236
NeilBrownc148ffd2009-11-13 17:47:00 +11005237 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005239 if (mddev->ok_start_degraded)
5240 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005241 "md/raid:%s: starting dirty degraded array"
5242 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005243 mdname(mddev));
5244 else {
5245 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005246 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005247 mdname(mddev));
5248 goto abort;
5249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 }
5251
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005253 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5254 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005255 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5256 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 else
NeilBrown0c55e022010-05-03 14:09:02 +10005258 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5259 " out of %d devices, algorithm %d\n",
5260 mdname(mddev), conf->level,
5261 mddev->raid_disks - mddev->degraded,
5262 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263
5264 print_raid5_conf(conf);
5265
NeilBrownfef9c612009-03-31 15:16:46 +11005266 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005267 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005268 atomic_set(&conf->reshape_stripes, 0);
5269 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5270 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5271 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5272 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5273 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005274 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005275 }
5276
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277
5278 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005279 if (mddev->to_remove == &raid5_attrs_group)
5280 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005281 else if (mddev->kobj.sd &&
5282 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005283 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005284 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005285 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005286 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5287
5288 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005289 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005290 /* read-ahead size must cover two whole stripes, which
5291 * is 2 * (datadisks) * chunksize where 'n' is the
5292 * number of raid devices
5293 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5295 int stripe = data_disks *
5296 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5297 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5298 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005299
5300 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005301
5302 mddev->queue->backing_dev_info.congested_data = mddev;
5303 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005304
5305 chunk_size = mddev->chunk_sectors << 9;
5306 blk_queue_io_min(mddev->queue, chunk_size);
5307 blk_queue_io_opt(mddev->queue, chunk_size *
5308 (conf->raid_disks - conf->max_degraded));
5309
NeilBrown05616be2012-05-21 09:27:00 +10005310 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005311 disk_stack_limits(mddev->gendisk, rdev->bdev,
5312 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005313 disk_stack_limits(mddev->gendisk, rdev->bdev,
5314 rdev->new_data_offset << 9);
5315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 }
5317
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 return 0;
5319abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005320 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005321 print_raid5_conf(conf);
5322 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005324 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 return -EIO;
5326}
5327
NeilBrownfd01b882011-10-11 16:47:53 +11005328static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329{
NeilBrownd1688a62011-10-11 16:49:52 +11005330 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331
NeilBrown01f96c02011-09-21 15:30:20 +10005332 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005333 if (mddev->queue)
5334 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005335 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005336 mddev->private = NULL;
5337 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 return 0;
5339}
5340
NeilBrownfd01b882011-10-11 16:47:53 +11005341static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342{
NeilBrownd1688a62011-10-11 16:49:52 +11005343 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 int i;
5345
Andre Noll9d8f0362009-06-18 08:45:01 +10005346 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5347 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005348 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 for (i = 0; i < conf->raid_disks; i++)
5350 seq_printf (seq, "%s",
5351 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005352 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354}
5355
NeilBrownd1688a62011-10-11 16:49:52 +11005356static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357{
5358 int i;
5359 struct disk_info *tmp;
5360
NeilBrown0c55e022010-05-03 14:09:02 +10005361 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 if (!conf) {
5363 printk("(conf==NULL)\n");
5364 return;
5365 }
NeilBrown0c55e022010-05-03 14:09:02 +10005366 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5367 conf->raid_disks,
5368 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
5370 for (i = 0; i < conf->raid_disks; i++) {
5371 char b[BDEVNAME_SIZE];
5372 tmp = conf->disks + i;
5373 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005374 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5375 i, !test_bit(Faulty, &tmp->rdev->flags),
5376 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 }
5378}
5379
NeilBrownfd01b882011-10-11 16:47:53 +11005380static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381{
5382 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005383 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005385 int count = 0;
5386 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387
5388 for (i = 0; i < conf->raid_disks; i++) {
5389 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005390 if (tmp->replacement
5391 && tmp->replacement->recovery_offset == MaxSector
5392 && !test_bit(Faulty, &tmp->replacement->flags)
5393 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5394 /* Replacement has just become active. */
5395 if (!tmp->rdev
5396 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5397 count++;
5398 if (tmp->rdev) {
5399 /* Replaced device not technically faulty,
5400 * but we need to be sure it gets removed
5401 * and never re-added.
5402 */
5403 set_bit(Faulty, &tmp->rdev->flags);
5404 sysfs_notify_dirent_safe(
5405 tmp->rdev->sysfs_state);
5406 }
5407 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5408 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005409 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005410 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005411 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005412 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005413 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 }
5415 }
NeilBrown6b965622010-08-18 11:56:59 +10005416 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005417 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005418 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005420 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421}
5422
NeilBrownb8321b62011-12-23 10:17:51 +11005423static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424{
NeilBrownd1688a62011-10-11 16:49:52 +11005425 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005427 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005428 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 struct disk_info *p = conf->disks + number;
5430
5431 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005432 if (rdev == p->rdev)
5433 rdevp = &p->rdev;
5434 else if (rdev == p->replacement)
5435 rdevp = &p->replacement;
5436 else
5437 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005438
NeilBrown657e3e42011-12-23 10:17:52 +11005439 if (number >= conf->raid_disks &&
5440 conf->reshape_progress == MaxSector)
5441 clear_bit(In_sync, &rdev->flags);
5442
5443 if (test_bit(In_sync, &rdev->flags) ||
5444 atomic_read(&rdev->nr_pending)) {
5445 err = -EBUSY;
5446 goto abort;
5447 }
5448 /* Only remove non-faulty devices if recovery
5449 * isn't possible.
5450 */
5451 if (!test_bit(Faulty, &rdev->flags) &&
5452 mddev->recovery_disabled != conf->recovery_disabled &&
5453 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005454 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005455 number < conf->raid_disks) {
5456 err = -EBUSY;
5457 goto abort;
5458 }
5459 *rdevp = NULL;
5460 synchronize_rcu();
5461 if (atomic_read(&rdev->nr_pending)) {
5462 /* lost the race, try later */
5463 err = -EBUSY;
5464 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005465 } else if (p->replacement) {
5466 /* We must have just cleared 'rdev' */
5467 p->rdev = p->replacement;
5468 clear_bit(Replacement, &p->replacement->flags);
5469 smp_mb(); /* Make sure other CPUs may see both as identical
5470 * but will never see neither - if they are careful
5471 */
5472 p->replacement = NULL;
5473 clear_bit(WantReplacement, &rdev->flags);
5474 } else
5475 /* We might have just removed the Replacement as faulty-
5476 * clear the bit just in case
5477 */
5478 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479abort:
5480
5481 print_raid5_conf(conf);
5482 return err;
5483}
5484
NeilBrownfd01b882011-10-11 16:47:53 +11005485static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486{
NeilBrownd1688a62011-10-11 16:49:52 +11005487 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005488 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 int disk;
5490 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005491 int first = 0;
5492 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493
NeilBrown7f0da592011-07-28 11:39:22 +10005494 if (mddev->recovery_disabled == conf->recovery_disabled)
5495 return -EBUSY;
5496
NeilBrowndc10c642012-03-19 12:46:37 +11005497 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005499 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
Neil Brown6c2fce22008-06-28 08:31:31 +10005501 if (rdev->raid_disk >= 0)
5502 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503
5504 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005505 * find the disk ... but prefer rdev->saved_raid_disk
5506 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005508 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005509 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005510 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005511 first = rdev->saved_raid_disk;
5512
5513 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005514 p = conf->disks + disk;
5515 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005516 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005518 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005519 if (rdev->saved_raid_disk != disk)
5520 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005521 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005522 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005524 }
5525 for (disk = first; disk <= last; disk++) {
5526 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005527 if (test_bit(WantReplacement, &p->rdev->flags) &&
5528 p->replacement == NULL) {
5529 clear_bit(In_sync, &rdev->flags);
5530 set_bit(Replacement, &rdev->flags);
5531 rdev->raid_disk = disk;
5532 err = 0;
5533 conf->fullsync = 1;
5534 rcu_assign_pointer(p->replacement, rdev);
5535 break;
5536 }
5537 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005538out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005540 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541}
5542
NeilBrownfd01b882011-10-11 16:47:53 +11005543static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544{
5545 /* no resync is happening, and there is enough space
5546 * on all devices, so we can resize.
5547 * We need to make sure resync covers any new space.
5548 * If the array is shrinking we should possibly wait until
5549 * any io in the removed space completes, but it hardly seems
5550 * worth it.
5551 */
NeilBrowna4a61252012-05-22 13:55:27 +10005552 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005553 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005554 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5555 if (mddev->external_size &&
5556 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005557 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005558 if (mddev->bitmap) {
5559 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5560 if (ret)
5561 return ret;
5562 }
5563 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005564 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005565 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005566 if (sectors > mddev->dev_sectors &&
5567 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005568 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5570 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005571 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005572 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 return 0;
5574}
5575
NeilBrownfd01b882011-10-11 16:47:53 +11005576static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005577{
5578 /* Can only proceed if there are plenty of stripe_heads.
5579 * We need a minimum of one full stripe,, and for sensible progress
5580 * it is best to have about 4 times that.
5581 * If we require 4 times, then the default 256 4K stripe_heads will
5582 * allow for chunk sizes up to 256K, which is probably OK.
5583 * If the chunk size is greater, user-space should request more
5584 * stripe_heads first.
5585 */
NeilBrownd1688a62011-10-11 16:49:52 +11005586 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005587 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5588 > conf->max_nr_stripes ||
5589 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5590 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005591 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5592 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005593 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5594 / STRIPE_SIZE)*4);
5595 return 0;
5596 }
5597 return 1;
5598}
5599
NeilBrownfd01b882011-10-11 16:47:53 +11005600static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005601{
NeilBrownd1688a62011-10-11 16:49:52 +11005602 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005603
NeilBrown88ce4932009-03-31 15:24:23 +11005604 if (mddev->delta_disks == 0 &&
5605 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005606 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005607 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005608 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005609 return -EINVAL;
5610 if (mddev->delta_disks < 0) {
5611 /* We might be able to shrink, but the devices must
5612 * be made bigger first.
5613 * For raid6, 4 is the minimum size.
5614 * Otherwise 2 is the minimum
5615 */
5616 int min = 2;
5617 if (mddev->level == 6)
5618 min = 4;
5619 if (mddev->raid_disks + mddev->delta_disks < min)
5620 return -EINVAL;
5621 }
NeilBrown29269552006-03-27 01:18:10 -08005622
NeilBrown01ee22b2009-06-18 08:47:20 +10005623 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005624 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005625
NeilBrownec32a2b2009-03-31 15:17:38 +11005626 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005627}
5628
NeilBrownfd01b882011-10-11 16:47:53 +11005629static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005630{
NeilBrownd1688a62011-10-11 16:49:52 +11005631 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005632 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005633 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005634 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005635
NeilBrownf4168852007-02-28 20:11:53 -08005636 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005637 return -EBUSY;
5638
NeilBrown01ee22b2009-06-18 08:47:20 +10005639 if (!check_stripe_cache(mddev))
5640 return -ENOSPC;
5641
NeilBrown30b67642012-05-22 13:55:28 +10005642 if (has_failed(conf))
5643 return -EINVAL;
5644
NeilBrownc6563a82012-05-21 09:27:00 +10005645 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005646 if (!test_bit(In_sync, &rdev->flags)
5647 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005648 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005649 }
NeilBrown63c70c42006-03-27 01:18:13 -08005650
NeilBrownf4168852007-02-28 20:11:53 -08005651 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005652 /* Not enough devices even to make a degraded array
5653 * of that size
5654 */
5655 return -EINVAL;
5656
NeilBrownec32a2b2009-03-31 15:17:38 +11005657 /* Refuse to reduce size of the array. Any reductions in
5658 * array size must be through explicit setting of array_size
5659 * attribute.
5660 */
5661 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5662 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005663 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005664 "before number of disks\n", mdname(mddev));
5665 return -EINVAL;
5666 }
5667
NeilBrownf6705572006-03-27 01:18:11 -08005668 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005669 spin_lock_irq(&conf->device_lock);
5670 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005671 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005672 conf->prev_chunk_sectors = conf->chunk_sectors;
5673 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005674 conf->prev_algo = conf->algorithm;
5675 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005676 conf->generation++;
5677 /* Code that selects data_offset needs to see the generation update
5678 * if reshape_progress has been set - so a memory barrier needed.
5679 */
5680 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005681 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005682 conf->reshape_progress = raid5_size(mddev, 0, 0);
5683 else
5684 conf->reshape_progress = 0;
5685 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005686 spin_unlock_irq(&conf->device_lock);
5687
5688 /* Add some new drives, as many as will fit.
5689 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005690 * Don't add devices if we are reducing the number of
5691 * devices in the array. This is because it is not possible
5692 * to correctly record the "partially reconstructed" state of
5693 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005694 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005695 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005696 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005697 if (rdev->raid_disk < 0 &&
5698 !test_bit(Faulty, &rdev->flags)) {
5699 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005700 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005701 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005702 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005703 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005704 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005705
5706 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005707 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005708 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005709 } else if (rdev->raid_disk >= conf->previous_raid_disks
5710 && !test_bit(Faulty, &rdev->flags)) {
5711 /* This is a spare that was manually added */
5712 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005713 }
NeilBrown29269552006-03-27 01:18:10 -08005714
NeilBrown87a8dec2011-01-31 11:57:43 +11005715 /* When a reshape changes the number of devices,
5716 * ->degraded is measured against the larger of the
5717 * pre and post number of devices.
5718 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005719 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005720 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005721 spin_unlock_irqrestore(&conf->device_lock, flags);
5722 }
NeilBrown63c70c42006-03-27 01:18:13 -08005723 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005724 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005725 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005726
NeilBrown29269552006-03-27 01:18:10 -08005727 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5728 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5729 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5730 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5731 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005732 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005733 if (!mddev->sync_thread) {
5734 mddev->recovery = 0;
5735 spin_lock_irq(&conf->device_lock);
5736 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005737 rdev_for_each(rdev, mddev)
5738 rdev->new_data_offset = rdev->data_offset;
5739 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005740 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005741 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005742 spin_unlock_irq(&conf->device_lock);
5743 return -EAGAIN;
5744 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005745 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005746 md_wakeup_thread(mddev->sync_thread);
5747 md_new_event(mddev);
5748 return 0;
5749}
NeilBrown29269552006-03-27 01:18:10 -08005750
NeilBrownec32a2b2009-03-31 15:17:38 +11005751/* This is called from the reshape thread and should make any
5752 * changes needed in 'conf'
5753 */
NeilBrownd1688a62011-10-11 16:49:52 +11005754static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005755{
NeilBrown29269552006-03-27 01:18:10 -08005756
NeilBrownf6705572006-03-27 01:18:11 -08005757 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10005758 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005759
NeilBrownf6705572006-03-27 01:18:11 -08005760 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005761 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005762 rdev_for_each(rdev, conf->mddev)
5763 rdev->data_offset = rdev->new_data_offset;
5764 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005765 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005766 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005767 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005768
5769 /* read-ahead size must cover two whole stripes, which is
5770 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5771 */
NeilBrown4a5add42010-06-01 19:37:28 +10005772 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005773 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005774 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005775 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005776 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5777 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5778 }
NeilBrown29269552006-03-27 01:18:10 -08005779 }
NeilBrown29269552006-03-27 01:18:10 -08005780}
5781
NeilBrownec32a2b2009-03-31 15:17:38 +11005782/* This is called from the raid5d thread with mddev_lock held.
5783 * It makes config changes to the device.
5784 */
NeilBrownfd01b882011-10-11 16:47:53 +11005785static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005786{
NeilBrownd1688a62011-10-11 16:49:52 +11005787 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005788
5789 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5790
NeilBrownec32a2b2009-03-31 15:17:38 +11005791 if (mddev->delta_disks > 0) {
5792 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5793 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005794 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005795 } else {
5796 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005797 spin_lock_irq(&conf->device_lock);
5798 mddev->degraded = calc_degraded(conf);
5799 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005800 for (d = conf->raid_disks ;
5801 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005802 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005803 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10005804 if (rdev)
5805 clear_bit(In_sync, &rdev->flags);
5806 rdev = conf->disks[d].replacement;
5807 if (rdev)
5808 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10005809 }
NeilBrowncea9c222009-03-31 15:15:05 +11005810 }
NeilBrown88ce4932009-03-31 15:24:23 +11005811 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005812 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005813 mddev->reshape_position = MaxSector;
5814 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005815 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005816 }
5817}
5818
NeilBrownfd01b882011-10-11 16:47:53 +11005819static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005820{
NeilBrownd1688a62011-10-11 16:49:52 +11005821 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005822
5823 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005824 case 2: /* resume for a suspend */
5825 wake_up(&conf->wait_for_overlap);
5826 break;
5827
NeilBrown72626682005-09-09 16:23:54 -07005828 case 1: /* stop all writes */
5829 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005830 /* '2' tells resync/reshape to pause so that all
5831 * active stripes can drain
5832 */
5833 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005834 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005835 atomic_read(&conf->active_stripes) == 0 &&
5836 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005837 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005838 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005839 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005840 /* allow reshape to continue */
5841 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005842 break;
5843
5844 case 0: /* re-enable writes */
5845 spin_lock_irq(&conf->device_lock);
5846 conf->quiesce = 0;
5847 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005848 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005849 spin_unlock_irq(&conf->device_lock);
5850 break;
5851 }
NeilBrown72626682005-09-09 16:23:54 -07005852}
NeilBrownb15c2e52006-01-06 00:20:16 -08005853
NeilBrownd562b0c2009-03-31 14:39:39 +11005854
NeilBrownfd01b882011-10-11 16:47:53 +11005855static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005856{
NeilBrowne373ab12011-10-11 16:48:59 +11005857 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005858 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005859
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005860 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005861 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005862 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5863 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005864 return ERR_PTR(-EINVAL);
5865 }
5866
NeilBrowne373ab12011-10-11 16:48:59 +11005867 sectors = raid0_conf->strip_zone[0].zone_end;
5868 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005869 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005870 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005871 mddev->new_layout = ALGORITHM_PARITY_N;
5872 mddev->new_chunk_sectors = mddev->chunk_sectors;
5873 mddev->raid_disks += 1;
5874 mddev->delta_disks = 1;
5875 /* make sure it will be not marked as dirty */
5876 mddev->recovery_cp = MaxSector;
5877
5878 return setup_conf(mddev);
5879}
5880
5881
NeilBrownfd01b882011-10-11 16:47:53 +11005882static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005883{
5884 int chunksect;
5885
5886 if (mddev->raid_disks != 2 ||
5887 mddev->degraded > 1)
5888 return ERR_PTR(-EINVAL);
5889
5890 /* Should check if there are write-behind devices? */
5891
5892 chunksect = 64*2; /* 64K by default */
5893
5894 /* The array must be an exact multiple of chunksize */
5895 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5896 chunksect >>= 1;
5897
5898 if ((chunksect<<9) < STRIPE_SIZE)
5899 /* array size does not allow a suitable chunk size */
5900 return ERR_PTR(-EINVAL);
5901
5902 mddev->new_level = 5;
5903 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005904 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005905
5906 return setup_conf(mddev);
5907}
5908
NeilBrownfd01b882011-10-11 16:47:53 +11005909static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005910{
5911 int new_layout;
5912
5913 switch (mddev->layout) {
5914 case ALGORITHM_LEFT_ASYMMETRIC_6:
5915 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5916 break;
5917 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5918 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5919 break;
5920 case ALGORITHM_LEFT_SYMMETRIC_6:
5921 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5922 break;
5923 case ALGORITHM_RIGHT_SYMMETRIC_6:
5924 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5925 break;
5926 case ALGORITHM_PARITY_0_6:
5927 new_layout = ALGORITHM_PARITY_0;
5928 break;
5929 case ALGORITHM_PARITY_N:
5930 new_layout = ALGORITHM_PARITY_N;
5931 break;
5932 default:
5933 return ERR_PTR(-EINVAL);
5934 }
5935 mddev->new_level = 5;
5936 mddev->new_layout = new_layout;
5937 mddev->delta_disks = -1;
5938 mddev->raid_disks -= 1;
5939 return setup_conf(mddev);
5940}
5941
NeilBrownd562b0c2009-03-31 14:39:39 +11005942
NeilBrownfd01b882011-10-11 16:47:53 +11005943static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11005944{
NeilBrown88ce4932009-03-31 15:24:23 +11005945 /* For a 2-drive array, the layout and chunk size can be changed
5946 * immediately as not restriping is needed.
5947 * For larger arrays we record the new value - after validation
5948 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11005949 */
NeilBrownd1688a62011-10-11 16:49:52 +11005950 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10005951 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11005952
NeilBrown597a7112009-06-18 08:47:42 +10005953 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11005954 return -EINVAL;
5955 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005956 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11005957 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005958 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11005959 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005960 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11005961 /* not factor of array size */
5962 return -EINVAL;
5963 }
5964
5965 /* They look valid */
5966
NeilBrown88ce4932009-03-31 15:24:23 +11005967 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10005968 /* can make the change immediately */
5969 if (mddev->new_layout >= 0) {
5970 conf->algorithm = mddev->new_layout;
5971 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11005972 }
5973 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10005974 conf->chunk_sectors = new_chunk ;
5975 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11005976 }
5977 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5978 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11005979 }
NeilBrown50ac1682009-06-18 08:47:55 +10005980 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11005981}
5982
NeilBrownfd01b882011-10-11 16:47:53 +11005983static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11005984{
NeilBrown597a7112009-06-18 08:47:42 +10005985 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10005986
NeilBrown597a7112009-06-18 08:47:42 +10005987 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11005988 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005989 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10005990 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11005991 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005992 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11005993 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005994 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11005995 /* not factor of array size */
5996 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11005997 }
NeilBrown88ce4932009-03-31 15:24:23 +11005998
5999 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006000 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006001}
6002
NeilBrownfd01b882011-10-11 16:47:53 +11006003static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006004{
6005 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006006 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006007 * raid1 - if there are two drives. We need to know the chunk size
6008 * raid4 - trivial - just use a raid4 layout.
6009 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006010 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006011 if (mddev->level == 0)
6012 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006013 if (mddev->level == 1)
6014 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006015 if (mddev->level == 4) {
6016 mddev->new_layout = ALGORITHM_PARITY_N;
6017 mddev->new_level = 5;
6018 return setup_conf(mddev);
6019 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006020 if (mddev->level == 6)
6021 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006022
6023 return ERR_PTR(-EINVAL);
6024}
6025
NeilBrownfd01b882011-10-11 16:47:53 +11006026static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006027{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006028 /* raid4 can take over:
6029 * raid0 - if there is only one strip zone
6030 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006031 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006032 if (mddev->level == 0)
6033 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006034 if (mddev->level == 5 &&
6035 mddev->layout == ALGORITHM_PARITY_N) {
6036 mddev->new_layout = 0;
6037 mddev->new_level = 4;
6038 return setup_conf(mddev);
6039 }
6040 return ERR_PTR(-EINVAL);
6041}
NeilBrownd562b0c2009-03-31 14:39:39 +11006042
NeilBrown84fc4b52011-10-11 16:49:58 +11006043static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006044
NeilBrownfd01b882011-10-11 16:47:53 +11006045static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006046{
6047 /* Currently can only take over a raid5. We map the
6048 * personality to an equivalent raid6 personality
6049 * with the Q block at the end.
6050 */
6051 int new_layout;
6052
6053 if (mddev->pers != &raid5_personality)
6054 return ERR_PTR(-EINVAL);
6055 if (mddev->degraded > 1)
6056 return ERR_PTR(-EINVAL);
6057 if (mddev->raid_disks > 253)
6058 return ERR_PTR(-EINVAL);
6059 if (mddev->raid_disks < 3)
6060 return ERR_PTR(-EINVAL);
6061
6062 switch (mddev->layout) {
6063 case ALGORITHM_LEFT_ASYMMETRIC:
6064 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6065 break;
6066 case ALGORITHM_RIGHT_ASYMMETRIC:
6067 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6068 break;
6069 case ALGORITHM_LEFT_SYMMETRIC:
6070 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6071 break;
6072 case ALGORITHM_RIGHT_SYMMETRIC:
6073 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6074 break;
6075 case ALGORITHM_PARITY_0:
6076 new_layout = ALGORITHM_PARITY_0_6;
6077 break;
6078 case ALGORITHM_PARITY_N:
6079 new_layout = ALGORITHM_PARITY_N;
6080 break;
6081 default:
6082 return ERR_PTR(-EINVAL);
6083 }
6084 mddev->new_level = 6;
6085 mddev->new_layout = new_layout;
6086 mddev->delta_disks = 1;
6087 mddev->raid_disks += 1;
6088 return setup_conf(mddev);
6089}
6090
6091
NeilBrown84fc4b52011-10-11 16:49:58 +11006092static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006093{
6094 .name = "raid6",
6095 .level = 6,
6096 .owner = THIS_MODULE,
6097 .make_request = make_request,
6098 .run = run,
6099 .stop = stop,
6100 .status = status,
6101 .error_handler = error,
6102 .hot_add_disk = raid5_add_disk,
6103 .hot_remove_disk= raid5_remove_disk,
6104 .spare_active = raid5_spare_active,
6105 .sync_request = sync_request,
6106 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006107 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006108 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006109 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006110 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006111 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006112 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006113};
NeilBrown84fc4b52011-10-11 16:49:58 +11006114static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115{
6116 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006117 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 .owner = THIS_MODULE,
6119 .make_request = make_request,
6120 .run = run,
6121 .stop = stop,
6122 .status = status,
6123 .error_handler = error,
6124 .hot_add_disk = raid5_add_disk,
6125 .hot_remove_disk= raid5_remove_disk,
6126 .spare_active = raid5_spare_active,
6127 .sync_request = sync_request,
6128 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006129 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006130 .check_reshape = raid5_check_reshape,
6131 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006132 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006133 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006134 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135};
6136
NeilBrown84fc4b52011-10-11 16:49:58 +11006137static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138{
NeilBrown2604b702006-01-06 00:20:36 -08006139 .name = "raid4",
6140 .level = 4,
6141 .owner = THIS_MODULE,
6142 .make_request = make_request,
6143 .run = run,
6144 .stop = stop,
6145 .status = status,
6146 .error_handler = error,
6147 .hot_add_disk = raid5_add_disk,
6148 .hot_remove_disk= raid5_remove_disk,
6149 .spare_active = raid5_spare_active,
6150 .sync_request = sync_request,
6151 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006152 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006153 .check_reshape = raid5_check_reshape,
6154 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006155 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006156 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006157 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006158};
6159
6160static int __init raid5_init(void)
6161{
NeilBrown16a53ec2006-06-26 00:27:38 -07006162 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006163 register_md_personality(&raid5_personality);
6164 register_md_personality(&raid4_personality);
6165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166}
6167
NeilBrown2604b702006-01-06 00:20:36 -08006168static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169{
NeilBrown16a53ec2006-06-26 00:27:38 -07006170 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006171 unregister_md_personality(&raid5_personality);
6172 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173}
6174
6175module_init(raid5_init);
6176module_exit(raid5_exit);
6177MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006178MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006180MODULE_ALIAS("md-raid5");
6181MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006182MODULE_ALIAS("md-level-5");
6183MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006184MODULE_ALIAS("md-personality-8"); /* RAID6 */
6185MODULE_ALIAS("md-raid6");
6186MODULE_ALIAS("md-level-6");
6187
6188/* This used to be two separate modules, they were: */
6189MODULE_ALIAS("raid5");
6190MODULE_ALIAS("raid6");