blob: 0689173fd9f568583708c53396631ac8e1838c55 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110056#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110057#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110058#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110059#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * Stripe cache
63 */
64
65#define NR_STRIPES 256
66#define STRIPE_SIZE PAGE_SIZE
67#define STRIPE_SHIFT (PAGE_SHIFT - 9)
68#define STRIPE_SECTORS (STRIPE_SIZE>>9)
69#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070070#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080071#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define HASH_MASK (NR_HASH - 1)
73
NeilBrownd1688a62011-10-11 16:49:52 +110074static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110075{
76 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
77 return &conf->stripe_hashtbl[hash];
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* bio's attached to a stripe+device for I/O are linked together in bi_sector
81 * order without overlap. There may be several bio's per stripe+device, and
82 * a bio could span several devices.
83 * When walking this list for a particular stripe+device, we must never proceed
84 * beyond a bio that extends past this device, as the next bio might no longer
85 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110086 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * of the current stripe+device
88 */
NeilBrowndb298e12011-10-07 14:23:00 +110089static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
90{
91 int sectors = bio->bi_size >> 9;
92 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
93 return bio->bi_next;
94 else
95 return NULL;
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jens Axboe960e7392008-08-15 10:41:18 +020098/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +020099 * We maintain a biased count of active stripes in the bottom 16 bits of
100 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200101 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000102static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200103{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000104 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
105 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200106}
107
Shaohua Lie7836bd62012-07-19 16:01:31 +1000108static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200109{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000110 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
111 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200112}
113
Shaohua Lie7836bd62012-07-19 16:01:31 +1000114static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200115{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000116 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
117 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200118}
119
Shaohua Lie7836bd62012-07-19 16:01:31 +1000120static inline void raid5_set_bi_processed_stripes(struct bio *bio,
121 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200122{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000123 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
124 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200125
Shaohua Lie7836bd62012-07-19 16:01:31 +1000126 do {
127 old = atomic_read(segments);
128 new = (old & 0xffff) | (cnt << 16);
129 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200130}
131
Shaohua Lie7836bd62012-07-19 16:01:31 +1000132static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200133{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000134 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
135 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200136}
137
NeilBrownd0dabf72009-03-31 14:39:38 +1100138/* Find first data disk in a raid6 stripe */
139static inline int raid6_d0(struct stripe_head *sh)
140{
NeilBrown67cc2b82009-03-31 14:39:38 +1100141 if (sh->ddf_layout)
142 /* ddf always start from first device */
143 return 0;
144 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100145 if (sh->qd_idx == sh->disks - 1)
146 return 0;
147 else
148 return sh->qd_idx + 1;
149}
NeilBrown16a53ec2006-06-26 00:27:38 -0700150static inline int raid6_next_disk(int disk, int raid_disks)
151{
152 disk++;
153 return (disk < raid_disks) ? disk : 0;
154}
Dan Williamsa4456852007-07-09 11:56:43 -0700155
NeilBrownd0dabf72009-03-31 14:39:38 +1100156/* When walking through the disks in a raid5, starting at raid6_d0,
157 * We need to map each disk to a 'slot', where the data disks are slot
158 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
159 * is raid_disks-1. This help does that mapping.
160 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100161static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
162 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100163{
Dan Williams66295422009-10-19 18:09:32 -0700164 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100165
NeilBrowne4424fe2009-10-16 16:27:34 +1100166 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700167 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100168 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100169 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100170 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100171 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100172 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700173 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100174 return slot;
175}
176
Dan Williamsa4456852007-07-09 11:56:43 -0700177static void return_io(struct bio *return_bi)
178{
179 struct bio *bi = return_bi;
180 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700181
182 return_bi = bi->bi_next;
183 bi->bi_next = NULL;
184 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000185 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700186 bi = return_bi;
187 }
188}
189
NeilBrownd1688a62011-10-11 16:49:52 +1100190static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Dan Williams600aa102008-06-28 08:32:05 +1000192static int stripe_operations_active(struct stripe_head *sh)
193{
194 return sh->check_state || sh->reconstruct_state ||
195 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
196 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
197}
198
Shaohua Li4eb788d2012-07-19 16:01:31 +1000199static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000201 BUG_ON(!list_empty(&sh->lru));
202 BUG_ON(atomic_read(&conf->active_stripes)==0);
203 if (test_bit(STRIPE_HANDLE, &sh->state)) {
204 if (test_bit(STRIPE_DELAYED, &sh->state) &&
205 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
206 list_add_tail(&sh->lru, &conf->delayed_list);
207 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
208 sh->bm_seq - conf->seq_write > 0)
209 list_add_tail(&sh->lru, &conf->bitmap_list);
210 else {
211 clear_bit(STRIPE_DELAYED, &sh->state);
212 clear_bit(STRIPE_BIT_DELAY, &sh->state);
213 list_add_tail(&sh->lru, &conf->handle_list);
214 }
215 md_wakeup_thread(conf->mddev->thread);
216 } else {
217 BUG_ON(stripe_operations_active(sh));
218 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
219 if (atomic_dec_return(&conf->preread_active_stripes)
220 < IO_THRESHOLD)
221 md_wakeup_thread(conf->mddev->thread);
222 atomic_dec(&conf->active_stripes);
223 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
224 list_add_tail(&sh->lru, &conf->inactive_list);
225 wake_up(&conf->wait_for_stripe);
226 if (conf->retry_read_aligned)
227 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 }
230}
NeilBrownd0dabf72009-03-31 14:39:38 +1100231
Shaohua Li4eb788d2012-07-19 16:01:31 +1000232static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
233{
234 if (atomic_dec_and_test(&sh->count))
235 do_release_stripe(conf, sh);
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static void release_stripe(struct stripe_head *sh)
239{
NeilBrownd1688a62011-10-11 16:49:52 +1100240 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700242
Shaohua Li4eb788d2012-07-19 16:01:31 +1000243 local_irq_save(flags);
244 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
245 do_release_stripe(conf, sh);
246 spin_unlock(&conf->device_lock);
247 }
248 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
NeilBrownfccddba2006-01-06 00:20:33 -0800251static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Dan Williams45b42332007-07-09 11:56:43 -0700253 pr_debug("remove_hash(), stripe %llu\n",
254 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
NeilBrownfccddba2006-01-06 00:20:33 -0800256 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
NeilBrownd1688a62011-10-11 16:49:52 +1100259static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
NeilBrownfccddba2006-01-06 00:20:33 -0800261 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Dan Williams45b42332007-07-09 11:56:43 -0700263 pr_debug("insert_hash(), stripe %llu\n",
264 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
NeilBrownfccddba2006-01-06 00:20:33 -0800266 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269
270/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100271static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct stripe_head *sh = NULL;
274 struct list_head *first;
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (list_empty(&conf->inactive_list))
277 goto out;
278 first = conf->inactive_list.next;
279 sh = list_entry(first, struct stripe_head, lru);
280 list_del_init(first);
281 remove_hash(sh);
282 atomic_inc(&conf->active_stripes);
283out:
284 return sh;
285}
286
NeilBrowne4e11e32010-06-16 16:45:16 +1000287static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct page *p;
290 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000291 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
NeilBrowne4e11e32010-06-16 16:45:16 +1000293 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 p = sh->dev[i].page;
295 if (!p)
296 continue;
297 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800298 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300}
301
NeilBrowne4e11e32010-06-16 16:45:16 +1000302static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000305 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
NeilBrowne4e11e32010-06-16 16:45:16 +1000307 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct page *page;
309
310 if (!(page = alloc_page(GFP_KERNEL))) {
311 return 1;
312 }
313 sh->dev[i].page = page;
314 }
315 return 0;
316}
317
NeilBrown784052e2009-03-31 15:19:07 +1100318static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100319static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100320 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
NeilBrownb5663ba2009-03-31 14:39:38 +1100322static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
NeilBrownd1688a62011-10-11 16:49:52 +1100324 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800325 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200327 BUG_ON(atomic_read(&sh->count) != 0);
328 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000329 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700330
Dan Williams45b42332007-07-09 11:56:43 -0700331 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 (unsigned long long)sh->sector);
333
334 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700335
NeilBrown86b42c72009-03-31 15:19:03 +1100336 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100337 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100339 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 sh->state = 0;
341
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800342
343 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct r5dev *dev = &sh->dev[i];
345
Dan Williamsd84e0f12007-01-02 13:52:30 -0700346 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700348 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700350 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000352 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100355 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357 insert_hash(conf, sh);
358}
359
NeilBrownd1688a62011-10-11 16:49:52 +1100360static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100361 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800364 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Dan Williams45b42332007-07-09 11:56:43 -0700366 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800367 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100368 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700370 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return NULL;
372}
373
NeilBrown674806d2010-06-16 17:17:53 +1000374/*
375 * Need to check if array has failed when deciding whether to:
376 * - start an array
377 * - remove non-faulty devices
378 * - add a spare
379 * - allow a reshape
380 * This determination is simple when no reshape is happening.
381 * However if there is a reshape, we need to carefully check
382 * both the before and after sections.
383 * This is because some failed devices may only affect one
384 * of the two sections, and some non-in_sync devices may
385 * be insync in the section most affected by failed devices.
386 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100387static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000388{
NeilBrown908f4fb2011-12-23 10:17:50 +1100389 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000390 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000391
392 rcu_read_lock();
393 degraded = 0;
394 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100395 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000396 if (rdev && test_bit(Faulty, &rdev->flags))
397 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000398 if (!rdev || test_bit(Faulty, &rdev->flags))
399 degraded++;
400 else if (test_bit(In_sync, &rdev->flags))
401 ;
402 else
403 /* not in-sync or faulty.
404 * If the reshape increases the number of devices,
405 * this is being recovered by the reshape, so
406 * this 'previous' section is not in_sync.
407 * If the number of devices is being reduced however,
408 * the device can only be part of the array if
409 * we are reverting a reshape, so this section will
410 * be in-sync.
411 */
412 if (conf->raid_disks >= conf->previous_raid_disks)
413 degraded++;
414 }
415 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100416 if (conf->raid_disks == conf->previous_raid_disks)
417 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000418 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100419 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000420 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100421 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000422 if (rdev && test_bit(Faulty, &rdev->flags))
423 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000424 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100425 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000426 else if (test_bit(In_sync, &rdev->flags))
427 ;
428 else
429 /* not in-sync or faulty.
430 * If reshape increases the number of devices, this
431 * section has already been recovered, else it
432 * almost certainly hasn't.
433 */
434 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100435 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000436 }
437 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100438 if (degraded2 > degraded)
439 return degraded2;
440 return degraded;
441}
442
443static int has_failed(struct r5conf *conf)
444{
445 int degraded;
446
447 if (conf->mddev->reshape_position == MaxSector)
448 return conf->mddev->degraded > conf->max_degraded;
449
450 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000451 if (degraded > conf->max_degraded)
452 return 1;
453 return 0;
454}
455
NeilBrownb5663ba2009-03-31 14:39:38 +1100456static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100457get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000458 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct stripe_head *sh;
461
Dan Williams45b42332007-07-09 11:56:43 -0700462 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 spin_lock_irq(&conf->device_lock);
465
466 do {
NeilBrown72626682005-09-09 16:23:54 -0700467 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000468 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700469 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100470 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (!sh) {
472 if (!conf->inactive_blocked)
473 sh = get_free_stripe(conf);
474 if (noblock && sh == NULL)
475 break;
476 if (!sh) {
477 conf->inactive_blocked = 1;
478 wait_event_lock_irq(conf->wait_for_stripe,
479 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800480 (atomic_read(&conf->active_stripes)
481 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 || !conf->inactive_blocked),
483 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000484 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 conf->inactive_blocked = 0;
486 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100487 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 } else {
489 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100490 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000491 && !test_bit(STRIPE_EXPANDING, &sh->state)
492 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 } else {
494 if (!test_bit(STRIPE_HANDLE, &sh->state))
495 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700496 if (list_empty(&sh->lru) &&
497 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700498 BUG();
499 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501 }
502 } while (sh == NULL);
503
504 if (sh)
505 atomic_inc(&sh->count);
506
507 spin_unlock_irq(&conf->device_lock);
508 return sh;
509}
510
NeilBrown05616be2012-05-21 09:27:00 +1000511/* Determine if 'data_offset' or 'new_data_offset' should be used
512 * in this stripe_head.
513 */
514static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
515{
516 sector_t progress = conf->reshape_progress;
517 /* Need a memory barrier to make sure we see the value
518 * of conf->generation, or ->data_offset that was set before
519 * reshape_progress was updated.
520 */
521 smp_rmb();
522 if (progress == MaxSector)
523 return 0;
524 if (sh->generation == conf->generation - 1)
525 return 0;
526 /* We are in a reshape, and this is a new-generation stripe,
527 * so use new_data_offset.
528 */
529 return 1;
530}
531
NeilBrown6712ecf2007-09-27 12:47:43 +0200532static void
533raid5_end_read_request(struct bio *bi, int error);
534static void
535raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700536
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000537static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700538{
NeilBrownd1688a62011-10-11 16:49:52 +1100539 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700540 int i, disks = sh->disks;
541
542 might_sleep();
543
544 for (i = disks; i--; ) {
545 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100546 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100547 struct bio *bi, *rbi;
548 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200549 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
550 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
551 rw = WRITE_FUA;
552 else
553 rw = WRITE;
554 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700555 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100556 else if (test_and_clear_bit(R5_WantReplace,
557 &sh->dev[i].flags)) {
558 rw = WRITE;
559 replace_only = 1;
560 } else
Dan Williams91c00922007-01-02 13:52:30 -0700561 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000562 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
563 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700564
565 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100566 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700567
568 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100569 rbi->bi_rw = rw;
570 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700571 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100572 rbi->bi_end_io = raid5_end_write_request;
573 } else
Dan Williams91c00922007-01-02 13:52:30 -0700574 bi->bi_end_io = raid5_end_read_request;
575
576 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100577 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100578 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
579 rdev = rcu_dereference(conf->disks[i].rdev);
580 if (!rdev) {
581 rdev = rrdev;
582 rrdev = NULL;
583 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100584 if (rw & WRITE) {
585 if (replace_only)
586 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100587 if (rdev == rrdev)
588 /* We raced and saw duplicates */
589 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100590 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100591 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100592 rdev = rrdev;
593 rrdev = NULL;
594 }
NeilBrown977df362011-12-23 10:17:53 +1100595
Dan Williams91c00922007-01-02 13:52:30 -0700596 if (rdev && test_bit(Faulty, &rdev->flags))
597 rdev = NULL;
598 if (rdev)
599 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100600 if (rrdev && test_bit(Faulty, &rrdev->flags))
601 rrdev = NULL;
602 if (rrdev)
603 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700604 rcu_read_unlock();
605
NeilBrown73e92e52011-07-28 11:39:22 +1000606 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100607 * need to check for writes. We never accept write errors
608 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000609 */
610 while ((rw & WRITE) && rdev &&
611 test_bit(WriteErrorSeen, &rdev->flags)) {
612 sector_t first_bad;
613 int bad_sectors;
614 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
615 &first_bad, &bad_sectors);
616 if (!bad)
617 break;
618
619 if (bad < 0) {
620 set_bit(BlockedBadBlocks, &rdev->flags);
621 if (!conf->mddev->external &&
622 conf->mddev->flags) {
623 /* It is very unlikely, but we might
624 * still need to write out the
625 * bad block log - better give it
626 * a chance*/
627 md_check_recovery(conf->mddev);
628 }
majianpeng18507532012-07-03 12:11:54 +1000629 /*
630 * Because md_wait_for_blocked_rdev
631 * will dec nr_pending, we must
632 * increment it first.
633 */
634 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000635 md_wait_for_blocked_rdev(rdev, conf->mddev);
636 } else {
637 /* Acknowledged bad block - skip the write */
638 rdev_dec_pending(rdev, conf->mddev);
639 rdev = NULL;
640 }
641 }
642
Dan Williams91c00922007-01-02 13:52:30 -0700643 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100644 if (s->syncing || s->expanding || s->expanded
645 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700646 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
647
Dan Williams2b7497f2008-06-28 08:31:52 +1000648 set_bit(STRIPE_IO_STARTED, &sh->state);
649
Dan Williams91c00922007-01-02 13:52:30 -0700650 bi->bi_bdev = rdev->bdev;
651 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700652 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700653 bi->bi_rw, i);
654 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000655 if (use_new_offset(conf, sh))
656 bi->bi_sector = (sh->sector
657 + rdev->new_data_offset);
658 else
659 bi->bi_sector = (sh->sector
660 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000661 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
662 bi->bi_rw |= REQ_FLUSH;
663
Dan Williams91c00922007-01-02 13:52:30 -0700664 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700665 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700666 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
667 bi->bi_io_vec[0].bv_offset = 0;
668 bi->bi_size = STRIPE_SIZE;
669 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100670 if (rrdev)
671 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700672 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100673 }
674 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100675 if (s->syncing || s->expanding || s->expanded
676 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100677 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
678
679 set_bit(STRIPE_IO_STARTED, &sh->state);
680
681 rbi->bi_bdev = rrdev->bdev;
682 pr_debug("%s: for %llu schedule op %ld on "
683 "replacement disc %d\n",
684 __func__, (unsigned long long)sh->sector,
685 rbi->bi_rw, i);
686 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000687 if (use_new_offset(conf, sh))
688 rbi->bi_sector = (sh->sector
689 + rrdev->new_data_offset);
690 else
691 rbi->bi_sector = (sh->sector
692 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100693 rbi->bi_flags = 1 << BIO_UPTODATE;
694 rbi->bi_idx = 0;
695 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
696 rbi->bi_io_vec[0].bv_offset = 0;
697 rbi->bi_size = STRIPE_SIZE;
698 rbi->bi_next = NULL;
699 generic_make_request(rbi);
700 }
701 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000702 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700703 set_bit(STRIPE_DEGRADED, &sh->state);
704 pr_debug("skip op %ld on disc %d for sector %llu\n",
705 bi->bi_rw, i, (unsigned long long)sh->sector);
706 clear_bit(R5_LOCKED, &sh->dev[i].flags);
707 set_bit(STRIPE_HANDLE, &sh->state);
708 }
709 }
710}
711
712static struct dma_async_tx_descriptor *
713async_copy_data(int frombio, struct bio *bio, struct page *page,
714 sector_t sector, struct dma_async_tx_descriptor *tx)
715{
716 struct bio_vec *bvl;
717 struct page *bio_page;
718 int i;
719 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700720 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700721 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700722
723 if (bio->bi_sector >= sector)
724 page_offset = (signed)(bio->bi_sector - sector) * 512;
725 else
726 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700727
Dan Williams0403e382009-09-08 17:42:50 -0700728 if (frombio)
729 flags |= ASYNC_TX_FENCE;
730 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
731
Dan Williams91c00922007-01-02 13:52:30 -0700732 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000733 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700734 int clen;
735 int b_offset = 0;
736
737 if (page_offset < 0) {
738 b_offset = -page_offset;
739 page_offset += b_offset;
740 len -= b_offset;
741 }
742
743 if (len > 0 && page_offset + len > STRIPE_SIZE)
744 clen = STRIPE_SIZE - page_offset;
745 else
746 clen = len;
747
748 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000749 b_offset += bvl->bv_offset;
750 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700751 if (frombio)
752 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700753 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700754 else
755 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700756 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700757 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700758 /* chain the operations */
759 submit.depend_tx = tx;
760
Dan Williams91c00922007-01-02 13:52:30 -0700761 if (clen < len) /* hit end of page */
762 break;
763 page_offset += len;
764 }
765
766 return tx;
767}
768
769static void ops_complete_biofill(void *stripe_head_ref)
770{
771 struct stripe_head *sh = stripe_head_ref;
772 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700773 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700774
Harvey Harrisone46b2722008-04-28 02:15:50 -0700775 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700776 (unsigned long long)sh->sector);
777
778 /* clear completed biofills */
779 for (i = sh->disks; i--; ) {
780 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700781
782 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700783 /* and check if we need to reply to a read request,
784 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000785 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700786 */
787 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700788 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700789
Dan Williams91c00922007-01-02 13:52:30 -0700790 BUG_ON(!dev->read);
791 rbi = dev->read;
792 dev->read = NULL;
793 while (rbi && rbi->bi_sector <
794 dev->sector + STRIPE_SECTORS) {
795 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000796 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700797 rbi->bi_next = return_bi;
798 return_bi = rbi;
799 }
Dan Williams91c00922007-01-02 13:52:30 -0700800 rbi = rbi2;
801 }
802 }
803 }
Dan Williams83de75c2008-06-28 08:31:58 +1000804 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700805
806 return_io(return_bi);
807
Dan Williamse4d84902007-09-24 10:06:13 -0700808 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700809 release_stripe(sh);
810}
811
812static void ops_run_biofill(struct stripe_head *sh)
813{
814 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700815 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700816 int i;
817
Harvey Harrisone46b2722008-04-28 02:15:50 -0700818 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700819 (unsigned long long)sh->sector);
820
821 for (i = sh->disks; i--; ) {
822 struct r5dev *dev = &sh->dev[i];
823 if (test_bit(R5_Wantfill, &dev->flags)) {
824 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000825 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700826 dev->read = rbi = dev->toread;
827 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000828 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700829 while (rbi && rbi->bi_sector <
830 dev->sector + STRIPE_SECTORS) {
831 tx = async_copy_data(0, rbi, dev->page,
832 dev->sector, tx);
833 rbi = r5_next_bio(rbi, dev->sector);
834 }
835 }
836 }
837
838 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700839 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
840 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700841}
842
Dan Williams4e7d2c02009-08-29 19:13:11 -0700843static void mark_target_uptodate(struct stripe_head *sh, int target)
844{
845 struct r5dev *tgt;
846
847 if (target < 0)
848 return;
849
850 tgt = &sh->dev[target];
851 set_bit(R5_UPTODATE, &tgt->flags);
852 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
853 clear_bit(R5_Wantcompute, &tgt->flags);
854}
855
Dan Williamsac6b53b2009-07-14 13:40:19 -0700856static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700857{
858 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700859
Harvey Harrisone46b2722008-04-28 02:15:50 -0700860 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700861 (unsigned long long)sh->sector);
862
Dan Williamsac6b53b2009-07-14 13:40:19 -0700863 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700864 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700865 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700866
Dan Williamsecc65c92008-06-28 08:31:57 +1000867 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
868 if (sh->check_state == check_state_compute_run)
869 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700870 set_bit(STRIPE_HANDLE, &sh->state);
871 release_stripe(sh);
872}
873
Dan Williamsd6f38f32009-07-14 11:50:52 -0700874/* return a pointer to the address conversion region of the scribble buffer */
875static addr_conv_t *to_addr_conv(struct stripe_head *sh,
876 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700877{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700878 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
879}
880
881static struct dma_async_tx_descriptor *
882ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
883{
Dan Williams91c00922007-01-02 13:52:30 -0700884 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700885 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700886 int target = sh->ops.target;
887 struct r5dev *tgt = &sh->dev[target];
888 struct page *xor_dest = tgt->page;
889 int count = 0;
890 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700891 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700892 int i;
893
894 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700895 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700896 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
897
898 for (i = disks; i--; )
899 if (i != target)
900 xor_srcs[count++] = sh->dev[i].page;
901
902 atomic_inc(&sh->count);
903
Dan Williams0403e382009-09-08 17:42:50 -0700904 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700905 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700906 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700907 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700908 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700909 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700910
Dan Williams91c00922007-01-02 13:52:30 -0700911 return tx;
912}
913
Dan Williamsac6b53b2009-07-14 13:40:19 -0700914/* set_syndrome_sources - populate source buffers for gen_syndrome
915 * @srcs - (struct page *) array of size sh->disks
916 * @sh - stripe_head to parse
917 *
918 * Populates srcs in proper layout order for the stripe and returns the
919 * 'count' of sources to be used in a call to async_gen_syndrome. The P
920 * destination buffer is recorded in srcs[count] and the Q destination
921 * is recorded in srcs[count+1]].
922 */
923static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
924{
925 int disks = sh->disks;
926 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
927 int d0_idx = raid6_d0(sh);
928 int count;
929 int i;
930
931 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100932 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700933
934 count = 0;
935 i = d0_idx;
936 do {
937 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
938
939 srcs[slot] = sh->dev[i].page;
940 i = raid6_next_disk(i, disks);
941 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700942
NeilBrowne4424fe2009-10-16 16:27:34 +1100943 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700944}
945
946static struct dma_async_tx_descriptor *
947ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
948{
949 int disks = sh->disks;
950 struct page **blocks = percpu->scribble;
951 int target;
952 int qd_idx = sh->qd_idx;
953 struct dma_async_tx_descriptor *tx;
954 struct async_submit_ctl submit;
955 struct r5dev *tgt;
956 struct page *dest;
957 int i;
958 int count;
959
960 if (sh->ops.target < 0)
961 target = sh->ops.target2;
962 else if (sh->ops.target2 < 0)
963 target = sh->ops.target;
964 else
965 /* we should only have one valid target */
966 BUG();
967 BUG_ON(target < 0);
968 pr_debug("%s: stripe %llu block: %d\n",
969 __func__, (unsigned long long)sh->sector, target);
970
971 tgt = &sh->dev[target];
972 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
973 dest = tgt->page;
974
975 atomic_inc(&sh->count);
976
977 if (target == qd_idx) {
978 count = set_syndrome_sources(blocks, sh);
979 blocks[count] = NULL; /* regenerating p is not necessary */
980 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700981 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
982 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700983 to_addr_conv(sh, percpu));
984 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
985 } else {
986 /* Compute any data- or p-drive using XOR */
987 count = 0;
988 for (i = disks; i-- ; ) {
989 if (i == target || i == qd_idx)
990 continue;
991 blocks[count++] = sh->dev[i].page;
992 }
993
Dan Williams0403e382009-09-08 17:42:50 -0700994 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
995 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700996 to_addr_conv(sh, percpu));
997 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
998 }
999
1000 return tx;
1001}
1002
1003static struct dma_async_tx_descriptor *
1004ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1005{
1006 int i, count, disks = sh->disks;
1007 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1008 int d0_idx = raid6_d0(sh);
1009 int faila = -1, failb = -1;
1010 int target = sh->ops.target;
1011 int target2 = sh->ops.target2;
1012 struct r5dev *tgt = &sh->dev[target];
1013 struct r5dev *tgt2 = &sh->dev[target2];
1014 struct dma_async_tx_descriptor *tx;
1015 struct page **blocks = percpu->scribble;
1016 struct async_submit_ctl submit;
1017
1018 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1019 __func__, (unsigned long long)sh->sector, target, target2);
1020 BUG_ON(target < 0 || target2 < 0);
1021 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1022 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1023
Dan Williams6c910a72009-09-16 12:24:54 -07001024 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001025 * slot number conversion for 'faila' and 'failb'
1026 */
1027 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001028 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001029 count = 0;
1030 i = d0_idx;
1031 do {
1032 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1033
1034 blocks[slot] = sh->dev[i].page;
1035
1036 if (i == target)
1037 faila = slot;
1038 if (i == target2)
1039 failb = slot;
1040 i = raid6_next_disk(i, disks);
1041 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001042
1043 BUG_ON(faila == failb);
1044 if (failb < faila)
1045 swap(faila, failb);
1046 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1047 __func__, (unsigned long long)sh->sector, faila, failb);
1048
1049 atomic_inc(&sh->count);
1050
1051 if (failb == syndrome_disks+1) {
1052 /* Q disk is one of the missing disks */
1053 if (faila == syndrome_disks) {
1054 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001055 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1056 ops_complete_compute, sh,
1057 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001058 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001059 STRIPE_SIZE, &submit);
1060 } else {
1061 struct page *dest;
1062 int data_target;
1063 int qd_idx = sh->qd_idx;
1064
1065 /* Missing D+Q: recompute D from P, then recompute Q */
1066 if (target == qd_idx)
1067 data_target = target2;
1068 else
1069 data_target = target;
1070
1071 count = 0;
1072 for (i = disks; i-- ; ) {
1073 if (i == data_target || i == qd_idx)
1074 continue;
1075 blocks[count++] = sh->dev[i].page;
1076 }
1077 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001078 init_async_submit(&submit,
1079 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1080 NULL, NULL, NULL,
1081 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001082 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1083 &submit);
1084
1085 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001086 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1087 ops_complete_compute, sh,
1088 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001089 return async_gen_syndrome(blocks, 0, count+2,
1090 STRIPE_SIZE, &submit);
1091 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001092 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001093 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1094 ops_complete_compute, sh,
1095 to_addr_conv(sh, percpu));
1096 if (failb == syndrome_disks) {
1097 /* We're missing D+P. */
1098 return async_raid6_datap_recov(syndrome_disks+2,
1099 STRIPE_SIZE, faila,
1100 blocks, &submit);
1101 } else {
1102 /* We're missing D+D. */
1103 return async_raid6_2data_recov(syndrome_disks+2,
1104 STRIPE_SIZE, faila, failb,
1105 blocks, &submit);
1106 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001107 }
1108}
1109
1110
Dan Williams91c00922007-01-02 13:52:30 -07001111static void ops_complete_prexor(void *stripe_head_ref)
1112{
1113 struct stripe_head *sh = stripe_head_ref;
1114
Harvey Harrisone46b2722008-04-28 02:15:50 -07001115 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001116 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001117}
1118
1119static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001120ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1121 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001122{
Dan Williams91c00922007-01-02 13:52:30 -07001123 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001124 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001125 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001126 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001127
1128 /* existing parity data subtracted */
1129 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1130
Harvey Harrisone46b2722008-04-28 02:15:50 -07001131 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001132 (unsigned long long)sh->sector);
1133
1134 for (i = disks; i--; ) {
1135 struct r5dev *dev = &sh->dev[i];
1136 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001137 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001138 xor_srcs[count++] = dev->page;
1139 }
1140
Dan Williams0403e382009-09-08 17:42:50 -07001141 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001142 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001143 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001144
1145 return tx;
1146}
1147
1148static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001149ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001150{
1151 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001152 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001153
Harvey Harrisone46b2722008-04-28 02:15:50 -07001154 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001155 (unsigned long long)sh->sector);
1156
1157 for (i = disks; i--; ) {
1158 struct r5dev *dev = &sh->dev[i];
1159 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001160
Dan Williamsd8ee0722008-06-28 08:32:06 +10001161 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001162 struct bio *wbi;
1163
Shaohua Lib17459c2012-07-19 16:01:31 +10001164 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001165 chosen = dev->towrite;
1166 dev->towrite = NULL;
1167 BUG_ON(dev->written);
1168 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001169 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001170
1171 while (wbi && wbi->bi_sector <
1172 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001173 if (wbi->bi_rw & REQ_FUA)
1174 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001175 if (wbi->bi_rw & REQ_SYNC)
1176 set_bit(R5_SyncIO, &dev->flags);
Dan Williams91c00922007-01-02 13:52:30 -07001177 tx = async_copy_data(1, wbi, dev->page,
1178 dev->sector, tx);
1179 wbi = r5_next_bio(wbi, dev->sector);
1180 }
1181 }
1182 }
1183
1184 return tx;
1185}
1186
Dan Williamsac6b53b2009-07-14 13:40:19 -07001187static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001188{
1189 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001190 int disks = sh->disks;
1191 int pd_idx = sh->pd_idx;
1192 int qd_idx = sh->qd_idx;
1193 int i;
Shaohua Libc0934f2012-05-22 13:55:05 +10001194 bool fua = false, sync = false;
Dan Williams91c00922007-01-02 13:52:30 -07001195
Harvey Harrisone46b2722008-04-28 02:15:50 -07001196 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001197 (unsigned long long)sh->sector);
1198
Shaohua Libc0934f2012-05-22 13:55:05 +10001199 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001200 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001201 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
1202 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001203
Dan Williams91c00922007-01-02 13:52:30 -07001204 for (i = disks; i--; ) {
1205 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001206
Tejun Heoe9c74692010-09-03 11:56:18 +02001207 if (dev->written || i == pd_idx || i == qd_idx) {
Dan Williams91c00922007-01-02 13:52:30 -07001208 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001209 if (fua)
1210 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001211 if (sync)
1212 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001213 }
Dan Williams91c00922007-01-02 13:52:30 -07001214 }
1215
Dan Williamsd8ee0722008-06-28 08:32:06 +10001216 if (sh->reconstruct_state == reconstruct_state_drain_run)
1217 sh->reconstruct_state = reconstruct_state_drain_result;
1218 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1219 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1220 else {
1221 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1222 sh->reconstruct_state = reconstruct_state_result;
1223 }
Dan Williams91c00922007-01-02 13:52:30 -07001224
1225 set_bit(STRIPE_HANDLE, &sh->state);
1226 release_stripe(sh);
1227}
1228
1229static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001230ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1231 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001232{
Dan Williams91c00922007-01-02 13:52:30 -07001233 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001234 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001235 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001236 int count = 0, pd_idx = sh->pd_idx, i;
1237 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001238 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001239 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001240
Harvey Harrisone46b2722008-04-28 02:15:50 -07001241 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001242 (unsigned long long)sh->sector);
1243
1244 /* check if prexor is active which means only process blocks
1245 * that are part of a read-modify-write (written)
1246 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001247 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1248 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001249 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1250 for (i = disks; i--; ) {
1251 struct r5dev *dev = &sh->dev[i];
1252 if (dev->written)
1253 xor_srcs[count++] = dev->page;
1254 }
1255 } else {
1256 xor_dest = sh->dev[pd_idx].page;
1257 for (i = disks; i--; ) {
1258 struct r5dev *dev = &sh->dev[i];
1259 if (i != pd_idx)
1260 xor_srcs[count++] = dev->page;
1261 }
1262 }
1263
Dan Williams91c00922007-01-02 13:52:30 -07001264 /* 1/ if we prexor'd then the dest is reused as a source
1265 * 2/ if we did not prexor then we are redoing the parity
1266 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1267 * for the synchronous xor case
1268 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001269 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001270 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1271
1272 atomic_inc(&sh->count);
1273
Dan Williamsac6b53b2009-07-14 13:40:19 -07001274 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001275 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001276 if (unlikely(count == 1))
1277 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1278 else
1279 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001280}
1281
Dan Williamsac6b53b2009-07-14 13:40:19 -07001282static void
1283ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1284 struct dma_async_tx_descriptor *tx)
1285{
1286 struct async_submit_ctl submit;
1287 struct page **blocks = percpu->scribble;
1288 int count;
1289
1290 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1291
1292 count = set_syndrome_sources(blocks, sh);
1293
1294 atomic_inc(&sh->count);
1295
1296 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1297 sh, to_addr_conv(sh, percpu));
1298 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001299}
1300
1301static void ops_complete_check(void *stripe_head_ref)
1302{
1303 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001304
Harvey Harrisone46b2722008-04-28 02:15:50 -07001305 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001306 (unsigned long long)sh->sector);
1307
Dan Williamsecc65c92008-06-28 08:31:57 +10001308 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001309 set_bit(STRIPE_HANDLE, &sh->state);
1310 release_stripe(sh);
1311}
1312
Dan Williamsac6b53b2009-07-14 13:40:19 -07001313static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001314{
Dan Williams91c00922007-01-02 13:52:30 -07001315 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001316 int pd_idx = sh->pd_idx;
1317 int qd_idx = sh->qd_idx;
1318 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001319 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001320 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001321 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001322 int count;
1323 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001324
Harvey Harrisone46b2722008-04-28 02:15:50 -07001325 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001326 (unsigned long long)sh->sector);
1327
Dan Williamsac6b53b2009-07-14 13:40:19 -07001328 count = 0;
1329 xor_dest = sh->dev[pd_idx].page;
1330 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001331 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001332 if (i == pd_idx || i == qd_idx)
1333 continue;
1334 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001335 }
1336
Dan Williamsd6f38f32009-07-14 11:50:52 -07001337 init_async_submit(&submit, 0, NULL, NULL, NULL,
1338 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001339 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001340 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001341
Dan Williams91c00922007-01-02 13:52:30 -07001342 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001343 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1344 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001345}
1346
Dan Williamsac6b53b2009-07-14 13:40:19 -07001347static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1348{
1349 struct page **srcs = percpu->scribble;
1350 struct async_submit_ctl submit;
1351 int count;
1352
1353 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1354 (unsigned long long)sh->sector, checkp);
1355
1356 count = set_syndrome_sources(srcs, sh);
1357 if (!checkp)
1358 srcs[count] = NULL;
1359
1360 atomic_inc(&sh->count);
1361 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1362 sh, to_addr_conv(sh, percpu));
1363 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1364 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1365}
1366
Dan Williams417b8d42009-10-16 16:25:22 +11001367static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001368{
1369 int overlap_clear = 0, i, disks = sh->disks;
1370 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001371 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001372 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001373 struct raid5_percpu *percpu;
1374 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001375
Dan Williamsd6f38f32009-07-14 11:50:52 -07001376 cpu = get_cpu();
1377 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001378 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001379 ops_run_biofill(sh);
1380 overlap_clear++;
1381 }
1382
Dan Williams7b3a8712008-06-28 08:32:09 +10001383 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001384 if (level < 6)
1385 tx = ops_run_compute5(sh, percpu);
1386 else {
1387 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1388 tx = ops_run_compute6_1(sh, percpu);
1389 else
1390 tx = ops_run_compute6_2(sh, percpu);
1391 }
1392 /* terminate the chain if reconstruct is not set to be run */
1393 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001394 async_tx_ack(tx);
1395 }
Dan Williams91c00922007-01-02 13:52:30 -07001396
Dan Williams600aa102008-06-28 08:32:05 +10001397 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001398 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001399
Dan Williams600aa102008-06-28 08:32:05 +10001400 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001401 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001402 overlap_clear++;
1403 }
1404
Dan Williamsac6b53b2009-07-14 13:40:19 -07001405 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1406 if (level < 6)
1407 ops_run_reconstruct5(sh, percpu, tx);
1408 else
1409 ops_run_reconstruct6(sh, percpu, tx);
1410 }
Dan Williams91c00922007-01-02 13:52:30 -07001411
Dan Williamsac6b53b2009-07-14 13:40:19 -07001412 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1413 if (sh->check_state == check_state_run)
1414 ops_run_check_p(sh, percpu);
1415 else if (sh->check_state == check_state_run_q)
1416 ops_run_check_pq(sh, percpu, 0);
1417 else if (sh->check_state == check_state_run_pq)
1418 ops_run_check_pq(sh, percpu, 1);
1419 else
1420 BUG();
1421 }
Dan Williams91c00922007-01-02 13:52:30 -07001422
Dan Williams91c00922007-01-02 13:52:30 -07001423 if (overlap_clear)
1424 for (i = disks; i--; ) {
1425 struct r5dev *dev = &sh->dev[i];
1426 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1427 wake_up(&sh->raid_conf->wait_for_overlap);
1428 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001429 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001430}
1431
Dan Williams417b8d42009-10-16 16:25:22 +11001432#ifdef CONFIG_MULTICORE_RAID456
1433static void async_run_ops(void *param, async_cookie_t cookie)
1434{
1435 struct stripe_head *sh = param;
1436 unsigned long ops_request = sh->ops.request;
1437
1438 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1439 wake_up(&sh->ops.wait_for_ops);
1440
1441 __raid_run_ops(sh, ops_request);
1442 release_stripe(sh);
1443}
1444
1445static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1446{
1447 /* since handle_stripe can be called outside of raid5d context
1448 * we need to ensure sh->ops.request is de-staged before another
1449 * request arrives
1450 */
1451 wait_event(sh->ops.wait_for_ops,
1452 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1453 sh->ops.request = ops_request;
1454
1455 atomic_inc(&sh->count);
1456 async_schedule(async_run_ops, sh);
1457}
1458#else
1459#define raid_run_ops __raid_run_ops
1460#endif
1461
NeilBrownd1688a62011-10-11 16:49:52 +11001462static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
1464 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001465 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001466 if (!sh)
1467 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001468
NeilBrown3f294f42005-11-08 21:39:25 -08001469 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001470 #ifdef CONFIG_MULTICORE_RAID456
1471 init_waitqueue_head(&sh->ops.wait_for_ops);
1472 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001473
Shaohua Lib17459c2012-07-19 16:01:31 +10001474 spin_lock_init(&sh->stripe_lock);
1475
NeilBrowne4e11e32010-06-16 16:45:16 +10001476 if (grow_buffers(sh)) {
1477 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001478 kmem_cache_free(conf->slab_cache, sh);
1479 return 0;
1480 }
1481 /* we just created an active stripe so... */
1482 atomic_set(&sh->count, 1);
1483 atomic_inc(&conf->active_stripes);
1484 INIT_LIST_HEAD(&sh->lru);
1485 release_stripe(sh);
1486 return 1;
1487}
1488
NeilBrownd1688a62011-10-11 16:49:52 +11001489static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001490{
Christoph Lametere18b8902006-12-06 20:33:20 -08001491 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001492 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
NeilBrownf4be6b42010-06-01 19:37:25 +10001494 if (conf->mddev->gendisk)
1495 sprintf(conf->cache_name[0],
1496 "raid%d-%s", conf->level, mdname(conf->mddev));
1497 else
1498 sprintf(conf->cache_name[0],
1499 "raid%d-%p", conf->level, conf->mddev);
1500 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1501
NeilBrownad01c9e2006-03-27 01:18:07 -08001502 conf->active_name = 0;
1503 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001505 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (!sc)
1507 return 1;
1508 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001509 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001510 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001511 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return 0;
1514}
NeilBrown29269552006-03-27 01:18:10 -08001515
Dan Williamsd6f38f32009-07-14 11:50:52 -07001516/**
1517 * scribble_len - return the required size of the scribble region
1518 * @num - total number of disks in the array
1519 *
1520 * The size must be enough to contain:
1521 * 1/ a struct page pointer for each device in the array +2
1522 * 2/ room to convert each entry in (1) to its corresponding dma
1523 * (dma_map_page()) or page (page_address()) address.
1524 *
1525 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1526 * calculate over all devices (not just the data blocks), using zeros in place
1527 * of the P and Q blocks.
1528 */
1529static size_t scribble_len(int num)
1530{
1531 size_t len;
1532
1533 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1534
1535 return len;
1536}
1537
NeilBrownd1688a62011-10-11 16:49:52 +11001538static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001539{
1540 /* Make all the stripes able to hold 'newsize' devices.
1541 * New slots in each stripe get 'page' set to a new page.
1542 *
1543 * This happens in stages:
1544 * 1/ create a new kmem_cache and allocate the required number of
1545 * stripe_heads.
1546 * 2/ gather all the old stripe_heads and tranfer the pages across
1547 * to the new stripe_heads. This will have the side effect of
1548 * freezing the array as once all stripe_heads have been collected,
1549 * no IO will be possible. Old stripe heads are freed once their
1550 * pages have been transferred over, and the old kmem_cache is
1551 * freed when all stripes are done.
1552 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1553 * we simple return a failre status - no need to clean anything up.
1554 * 4/ allocate new pages for the new slots in the new stripe_heads.
1555 * If this fails, we don't bother trying the shrink the
1556 * stripe_heads down again, we just leave them as they are.
1557 * As each stripe_head is processed the new one is released into
1558 * active service.
1559 *
1560 * Once step2 is started, we cannot afford to wait for a write,
1561 * so we use GFP_NOIO allocations.
1562 */
1563 struct stripe_head *osh, *nsh;
1564 LIST_HEAD(newstripes);
1565 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001566 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001567 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001568 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001569 int i;
1570
1571 if (newsize <= conf->pool_size)
1572 return 0; /* never bother to shrink */
1573
Dan Williamsb5470dc2008-06-27 21:44:04 -07001574 err = md_allow_write(conf->mddev);
1575 if (err)
1576 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001577
NeilBrownad01c9e2006-03-27 01:18:07 -08001578 /* Step 1 */
1579 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1580 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001581 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001582 if (!sc)
1583 return -ENOMEM;
1584
1585 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001586 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001587 if (!nsh)
1588 break;
1589
NeilBrownad01c9e2006-03-27 01:18:07 -08001590 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001591 #ifdef CONFIG_MULTICORE_RAID456
1592 init_waitqueue_head(&nsh->ops.wait_for_ops);
1593 #endif
NeilBrowncb13ff62012-09-24 16:27:20 +10001594 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001595
1596 list_add(&nsh->lru, &newstripes);
1597 }
1598 if (i) {
1599 /* didn't get enough, give up */
1600 while (!list_empty(&newstripes)) {
1601 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1602 list_del(&nsh->lru);
1603 kmem_cache_free(sc, nsh);
1604 }
1605 kmem_cache_destroy(sc);
1606 return -ENOMEM;
1607 }
1608 /* Step 2 - Must use GFP_NOIO now.
1609 * OK, we have enough stripes, start collecting inactive
1610 * stripes and copying them over
1611 */
1612 list_for_each_entry(nsh, &newstripes, lru) {
1613 spin_lock_irq(&conf->device_lock);
1614 wait_event_lock_irq(conf->wait_for_stripe,
1615 !list_empty(&conf->inactive_list),
1616 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001617 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001618 osh = get_free_stripe(conf);
1619 spin_unlock_irq(&conf->device_lock);
1620 atomic_set(&nsh->count, 1);
1621 for(i=0; i<conf->pool_size; i++)
1622 nsh->dev[i].page = osh->dev[i].page;
1623 for( ; i<newsize; i++)
1624 nsh->dev[i].page = NULL;
1625 kmem_cache_free(conf->slab_cache, osh);
1626 }
1627 kmem_cache_destroy(conf->slab_cache);
1628
1629 /* Step 3.
1630 * At this point, we are holding all the stripes so the array
1631 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001632 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001633 */
1634 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1635 if (ndisks) {
1636 for (i=0; i<conf->raid_disks; i++)
1637 ndisks[i] = conf->disks[i];
1638 kfree(conf->disks);
1639 conf->disks = ndisks;
1640 } else
1641 err = -ENOMEM;
1642
Dan Williamsd6f38f32009-07-14 11:50:52 -07001643 get_online_cpus();
1644 conf->scribble_len = scribble_len(newsize);
1645 for_each_present_cpu(cpu) {
1646 struct raid5_percpu *percpu;
1647 void *scribble;
1648
1649 percpu = per_cpu_ptr(conf->percpu, cpu);
1650 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1651
1652 if (scribble) {
1653 kfree(percpu->scribble);
1654 percpu->scribble = scribble;
1655 } else {
1656 err = -ENOMEM;
1657 break;
1658 }
1659 }
1660 put_online_cpus();
1661
NeilBrownad01c9e2006-03-27 01:18:07 -08001662 /* Step 4, return new stripes to service */
1663 while(!list_empty(&newstripes)) {
1664 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1665 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001666
NeilBrownad01c9e2006-03-27 01:18:07 -08001667 for (i=conf->raid_disks; i < newsize; i++)
1668 if (nsh->dev[i].page == NULL) {
1669 struct page *p = alloc_page(GFP_NOIO);
1670 nsh->dev[i].page = p;
1671 if (!p)
1672 err = -ENOMEM;
1673 }
1674 release_stripe(nsh);
1675 }
1676 /* critical section pass, GFP_NOIO no longer needed */
1677
1678 conf->slab_cache = sc;
1679 conf->active_name = 1-conf->active_name;
1680 conf->pool_size = newsize;
1681 return err;
1682}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
NeilBrownd1688a62011-10-11 16:49:52 +11001684static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 struct stripe_head *sh;
1687
NeilBrown3f294f42005-11-08 21:39:25 -08001688 spin_lock_irq(&conf->device_lock);
1689 sh = get_free_stripe(conf);
1690 spin_unlock_irq(&conf->device_lock);
1691 if (!sh)
1692 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001693 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001694 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001695 kmem_cache_free(conf->slab_cache, sh);
1696 atomic_dec(&conf->active_stripes);
1697 return 1;
1698}
1699
NeilBrownd1688a62011-10-11 16:49:52 +11001700static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001701{
1702 while (drop_one_stripe(conf))
1703 ;
1704
NeilBrown29fc7e32006-02-03 03:03:41 -08001705 if (conf->slab_cache)
1706 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 conf->slab_cache = NULL;
1708}
1709
NeilBrown6712ecf2007-09-27 12:47:43 +02001710static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
NeilBrown99c0fb52009-03-31 14:39:38 +11001712 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001713 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001714 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001716 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001717 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001718 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 for (i=0 ; i<disks; i++)
1721 if (bi == &sh->dev[i].req)
1722 break;
1723
Dan Williams45b42332007-07-09 11:56:43 -07001724 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1725 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 uptodate);
1727 if (i == disks) {
1728 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001729 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 }
NeilBrown14a75d32011-12-23 10:17:52 +11001731 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001732 /* If replacement finished while this request was outstanding,
1733 * 'replacement' might be NULL already.
1734 * In that case it moved down to 'rdev'.
1735 * rdev is not removed until all requests are finished.
1736 */
NeilBrown14a75d32011-12-23 10:17:52 +11001737 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001738 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001739 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
NeilBrown05616be2012-05-21 09:27:00 +10001741 if (use_new_offset(conf, sh))
1742 s = sh->sector + rdev->new_data_offset;
1743 else
1744 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001747 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001748 /* Note that this cannot happen on a
1749 * replacement device. We just fail those on
1750 * any error
1751 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001752 printk_ratelimited(
1753 KERN_INFO
1754 "md/raid:%s: read error corrected"
1755 " (%lu sectors at %llu on %s)\n",
1756 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001757 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001758 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001759 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001760 clear_bit(R5_ReadError, &sh->dev[i].flags);
1761 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001762 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1763 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1764
NeilBrown14a75d32011-12-23 10:17:52 +11001765 if (atomic_read(&rdev->read_errors))
1766 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001768 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001769 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001770 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001773 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001774 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1775 printk_ratelimited(
1776 KERN_WARNING
1777 "md/raid:%s: read error on replacement device "
1778 "(sector %llu on %s).\n",
1779 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001780 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001781 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001782 else if (conf->mddev->degraded >= conf->max_degraded) {
1783 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001784 printk_ratelimited(
1785 KERN_WARNING
1786 "md/raid:%s: read error not correctable "
1787 "(sector %llu on %s).\n",
1788 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001789 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001790 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001791 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001792 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001793 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001794 printk_ratelimited(
1795 KERN_WARNING
1796 "md/raid:%s: read error NOT corrected!! "
1797 "(sector %llu on %s).\n",
1798 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001799 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001800 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001801 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001802 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001803 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001804 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001805 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001806 else
1807 retry = 1;
1808 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001809 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1810 set_bit(R5_ReadError, &sh->dev[i].flags);
1811 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1812 } else
1813 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001814 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001815 clear_bit(R5_ReadError, &sh->dev[i].flags);
1816 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001817 if (!(set_bad
1818 && test_bit(In_sync, &rdev->flags)
1819 && rdev_set_badblocks(
1820 rdev, sh->sector, STRIPE_SECTORS, 0)))
1821 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
NeilBrown14a75d32011-12-23 10:17:52 +11001824 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1826 set_bit(STRIPE_HANDLE, &sh->state);
1827 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
NeilBrownd710e132008-10-13 11:55:12 +11001830static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
NeilBrown99c0fb52009-03-31 14:39:38 +11001832 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001833 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001834 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001835 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001837 sector_t first_bad;
1838 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001839 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
NeilBrown977df362011-12-23 10:17:53 +11001841 for (i = 0 ; i < disks; i++) {
1842 if (bi == &sh->dev[i].req) {
1843 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 break;
NeilBrown977df362011-12-23 10:17:53 +11001845 }
1846 if (bi == &sh->dev[i].rreq) {
1847 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001848 if (rdev)
1849 replacement = 1;
1850 else
1851 /* rdev was removed and 'replacement'
1852 * replaced it. rdev is not removed
1853 * until all requests are finished.
1854 */
1855 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001856 break;
1857 }
1858 }
Dan Williams45b42332007-07-09 11:56:43 -07001859 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1861 uptodate);
1862 if (i == disks) {
1863 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001864 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 }
1866
NeilBrown977df362011-12-23 10:17:53 +11001867 if (replacement) {
1868 if (!uptodate)
1869 md_error(conf->mddev, rdev);
1870 else if (is_badblock(rdev, sh->sector,
1871 STRIPE_SECTORS,
1872 &first_bad, &bad_sectors))
1873 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1874 } else {
1875 if (!uptodate) {
1876 set_bit(WriteErrorSeen, &rdev->flags);
1877 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001878 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1879 set_bit(MD_RECOVERY_NEEDED,
1880 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001881 } else if (is_badblock(rdev, sh->sector,
1882 STRIPE_SECTORS,
1883 &first_bad, &bad_sectors))
1884 set_bit(R5_MadeGood, &sh->dev[i].flags);
1885 }
1886 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
NeilBrown977df362011-12-23 10:17:53 +11001888 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1889 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001891 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893
NeilBrown784052e2009-03-31 15:19:07 +11001894static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
NeilBrown784052e2009-03-31 15:19:07 +11001896static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 struct r5dev *dev = &sh->dev[i];
1899
1900 bio_init(&dev->req);
1901 dev->req.bi_io_vec = &dev->vec;
1902 dev->req.bi_vcnt++;
1903 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001905 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
NeilBrown977df362011-12-23 10:17:53 +11001907 bio_init(&dev->rreq);
1908 dev->rreq.bi_io_vec = &dev->rvec;
1909 dev->rreq.bi_vcnt++;
1910 dev->rreq.bi_max_vecs++;
1911 dev->rreq.bi_private = sh;
1912 dev->rvec.bv_page = dev->page;
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001915 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
1917
NeilBrownfd01b882011-10-11 16:47:53 +11001918static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
1920 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001921 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001922 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001923 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
NeilBrown908f4fb2011-12-23 10:17:50 +11001925 spin_lock_irqsave(&conf->device_lock, flags);
1926 clear_bit(In_sync, &rdev->flags);
1927 mddev->degraded = calc_degraded(conf);
1928 spin_unlock_irqrestore(&conf->device_lock, flags);
1929 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1930
NeilBrownde393cd2011-07-28 11:31:48 +10001931 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001932 set_bit(Faulty, &rdev->flags);
1933 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1934 printk(KERN_ALERT
1935 "md/raid:%s: Disk failure on %s, disabling device.\n"
1936 "md/raid:%s: Operation continuing on %d devices.\n",
1937 mdname(mddev),
1938 bdevname(rdev->bdev, b),
1939 mdname(mddev),
1940 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001941}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943/*
1944 * Input: a 'big' sector number,
1945 * Output: index of the data and parity disk, and the sector # in them.
1946 */
NeilBrownd1688a62011-10-11 16:49:52 +11001947static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001948 int previous, int *dd_idx,
1949 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001951 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001952 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001954 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001955 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001957 int algorithm = previous ? conf->prev_algo
1958 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001959 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1960 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001961 int raid_disks = previous ? conf->previous_raid_disks
1962 : conf->raid_disks;
1963 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 /* First compute the information on this sector */
1966
1967 /*
1968 * Compute the chunk number and the sector offset inside the chunk
1969 */
1970 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1971 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 /*
1974 * Compute the stripe number
1975 */
NeilBrown35f2a592010-04-20 14:13:34 +10001976 stripe = chunk_number;
1977 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10001978 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 /*
1980 * Select the parity disk based on the user selected algorithm.
1981 */
NeilBrown84789552011-07-27 11:00:36 +10001982 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07001983 switch(conf->level) {
1984 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11001985 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07001986 break;
1987 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11001988 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001990 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001991 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 (*dd_idx)++;
1993 break;
1994 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10001995 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11001996 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 (*dd_idx)++;
1998 break;
1999 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002000 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002001 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 break;
2003 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002004 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002005 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002007 case ALGORITHM_PARITY_0:
2008 pd_idx = 0;
2009 (*dd_idx)++;
2010 break;
2011 case ALGORITHM_PARITY_N:
2012 pd_idx = data_disks;
2013 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002015 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002016 }
2017 break;
2018 case 6:
2019
NeilBrowne183eae2009-03-31 15:20:22 +11002020 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002021 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002022 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002023 qd_idx = pd_idx + 1;
2024 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002025 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002026 qd_idx = 0;
2027 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002028 (*dd_idx) += 2; /* D D P Q D */
2029 break;
2030 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002031 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002032 qd_idx = pd_idx + 1;
2033 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002034 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002035 qd_idx = 0;
2036 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002037 (*dd_idx) += 2; /* D D P Q D */
2038 break;
2039 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002040 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002041 qd_idx = (pd_idx + 1) % raid_disks;
2042 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002043 break;
2044 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002045 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002046 qd_idx = (pd_idx + 1) % raid_disks;
2047 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002048 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002049
2050 case ALGORITHM_PARITY_0:
2051 pd_idx = 0;
2052 qd_idx = 1;
2053 (*dd_idx) += 2;
2054 break;
2055 case ALGORITHM_PARITY_N:
2056 pd_idx = data_disks;
2057 qd_idx = data_disks + 1;
2058 break;
2059
2060 case ALGORITHM_ROTATING_ZERO_RESTART:
2061 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2062 * of blocks for computing Q is different.
2063 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002064 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002065 qd_idx = pd_idx + 1;
2066 if (pd_idx == raid_disks-1) {
2067 (*dd_idx)++; /* Q D D D P */
2068 qd_idx = 0;
2069 } else if (*dd_idx >= pd_idx)
2070 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002071 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002072 break;
2073
2074 case ALGORITHM_ROTATING_N_RESTART:
2075 /* Same a left_asymmetric, by first stripe is
2076 * D D D P Q rather than
2077 * Q D D D P
2078 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002079 stripe2 += 1;
2080 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002081 qd_idx = pd_idx + 1;
2082 if (pd_idx == raid_disks-1) {
2083 (*dd_idx)++; /* Q D D D P */
2084 qd_idx = 0;
2085 } else if (*dd_idx >= pd_idx)
2086 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002087 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002088 break;
2089
2090 case ALGORITHM_ROTATING_N_CONTINUE:
2091 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002092 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002093 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2094 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002095 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002096 break;
2097
2098 case ALGORITHM_LEFT_ASYMMETRIC_6:
2099 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002100 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002101 if (*dd_idx >= pd_idx)
2102 (*dd_idx)++;
2103 qd_idx = raid_disks - 1;
2104 break;
2105
2106 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002107 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002108 if (*dd_idx >= pd_idx)
2109 (*dd_idx)++;
2110 qd_idx = raid_disks - 1;
2111 break;
2112
2113 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002114 pd_idx = data_disks - 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_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002120 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002121 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2122 qd_idx = raid_disks - 1;
2123 break;
2124
2125 case ALGORITHM_PARITY_0_6:
2126 pd_idx = 0;
2127 (*dd_idx)++;
2128 qd_idx = raid_disks - 1;
2129 break;
2130
NeilBrown16a53ec2006-06-26 00:27:38 -07002131 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002132 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002133 }
2134 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 }
2136
NeilBrown911d4ee2009-03-31 14:39:38 +11002137 if (sh) {
2138 sh->pd_idx = pd_idx;
2139 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002140 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 /*
2143 * Finally, compute the new sector number
2144 */
2145 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2146 return new_sector;
2147}
2148
2149
NeilBrown784052e2009-03-31 15:19:07 +11002150static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
NeilBrownd1688a62011-10-11 16:49:52 +11002152 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002153 int raid_disks = sh->disks;
2154 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002156 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2157 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002158 int algorithm = previous ? conf->prev_algo
2159 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 sector_t stripe;
2161 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002162 sector_t chunk_number;
2163 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002165 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
NeilBrown16a53ec2006-06-26 00:27:38 -07002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2169 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
NeilBrown16a53ec2006-06-26 00:27:38 -07002171 if (i == sh->pd_idx)
2172 return 0;
2173 switch(conf->level) {
2174 case 4: break;
2175 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002176 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 case ALGORITHM_LEFT_ASYMMETRIC:
2178 case ALGORITHM_RIGHT_ASYMMETRIC:
2179 if (i > sh->pd_idx)
2180 i--;
2181 break;
2182 case ALGORITHM_LEFT_SYMMETRIC:
2183 case ALGORITHM_RIGHT_SYMMETRIC:
2184 if (i < sh->pd_idx)
2185 i += raid_disks;
2186 i -= (sh->pd_idx + 1);
2187 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002188 case ALGORITHM_PARITY_0:
2189 i -= 1;
2190 break;
2191 case ALGORITHM_PARITY_N:
2192 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002194 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002195 }
2196 break;
2197 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002198 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002199 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002200 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002201 case ALGORITHM_LEFT_ASYMMETRIC:
2202 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002203 case ALGORITHM_ROTATING_ZERO_RESTART:
2204 case ALGORITHM_ROTATING_N_RESTART:
2205 if (sh->pd_idx == raid_disks-1)
2206 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002207 else if (i > sh->pd_idx)
2208 i -= 2; /* D D P Q D */
2209 break;
2210 case ALGORITHM_LEFT_SYMMETRIC:
2211 case ALGORITHM_RIGHT_SYMMETRIC:
2212 if (sh->pd_idx == raid_disks-1)
2213 i--; /* Q D D D P */
2214 else {
2215 /* D D P Q D */
2216 if (i < sh->pd_idx)
2217 i += raid_disks;
2218 i -= (sh->pd_idx + 2);
2219 }
2220 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002221 case ALGORITHM_PARITY_0:
2222 i -= 2;
2223 break;
2224 case ALGORITHM_PARITY_N:
2225 break;
2226 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002227 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002228 if (sh->pd_idx == 0)
2229 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002230 else {
2231 /* D D Q P D */
2232 if (i < sh->pd_idx)
2233 i += raid_disks;
2234 i -= (sh->pd_idx + 1);
2235 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002236 break;
2237 case ALGORITHM_LEFT_ASYMMETRIC_6:
2238 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2239 if (i > sh->pd_idx)
2240 i--;
2241 break;
2242 case ALGORITHM_LEFT_SYMMETRIC_6:
2243 case ALGORITHM_RIGHT_SYMMETRIC_6:
2244 if (i < sh->pd_idx)
2245 i += data_disks + 1;
2246 i -= (sh->pd_idx + 1);
2247 break;
2248 case ALGORITHM_PARITY_0_6:
2249 i -= 1;
2250 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002251 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002252 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002253 }
2254 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }
2256
2257 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002258 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
NeilBrown112bf892009-03-31 14:39:38 +11002260 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002261 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002262 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2263 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002264 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2265 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return 0;
2267 }
2268 return r_sector;
2269}
2270
2271
Dan Williams600aa102008-06-28 08:32:05 +10002272static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002273schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002274 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002275{
2276 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002277 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002278 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002279
Dan Williamse33129d2007-01-02 13:52:30 -07002280 if (rcw) {
2281 /* if we are not expanding this is a proper write request, and
2282 * there will be bios with new data to be drained into the
2283 * stripe cache
2284 */
2285 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002286 sh->reconstruct_state = reconstruct_state_drain_run;
2287 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2288 } else
2289 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002290
Dan Williamsac6b53b2009-07-14 13:40:19 -07002291 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002292
2293 for (i = disks; i--; ) {
2294 struct r5dev *dev = &sh->dev[i];
2295
2296 if (dev->towrite) {
2297 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002298 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002299 if (!expand)
2300 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002301 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002302 }
2303 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002304 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002305 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002306 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002307 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002308 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002309 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2310 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2311
Dan Williamsd8ee0722008-06-28 08:32:06 +10002312 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002313 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2314 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002315 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002316
2317 for (i = disks; i--; ) {
2318 struct r5dev *dev = &sh->dev[i];
2319 if (i == pd_idx)
2320 continue;
2321
Dan Williamse33129d2007-01-02 13:52:30 -07002322 if (dev->towrite &&
2323 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002324 test_bit(R5_Wantcompute, &dev->flags))) {
2325 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002326 set_bit(R5_LOCKED, &dev->flags);
2327 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002328 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002329 }
2330 }
2331 }
2332
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002333 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002334 * are in flight
2335 */
2336 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2337 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002338 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002339
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002340 if (level == 6) {
2341 int qd_idx = sh->qd_idx;
2342 struct r5dev *dev = &sh->dev[qd_idx];
2343
2344 set_bit(R5_LOCKED, &dev->flags);
2345 clear_bit(R5_UPTODATE, &dev->flags);
2346 s->locked++;
2347 }
2348
Dan Williams600aa102008-06-28 08:32:05 +10002349 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002350 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002351 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002352}
NeilBrown16a53ec2006-06-26 00:27:38 -07002353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354/*
2355 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002356 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 * The bi_next chain must be in order.
2358 */
2359static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2360{
2361 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002362 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002363 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
NeilBrowncbe47ec2011-07-26 11:20:35 +10002365 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 (unsigned long long)bi->bi_sector,
2367 (unsigned long long)sh->sector);
2368
Shaohua Lib17459c2012-07-19 16:01:31 +10002369 /*
2370 * If several bio share a stripe. The bio bi_phys_segments acts as a
2371 * reference count to avoid race. The reference count should already be
2372 * increased before this function is called (for example, in
2373 * make_request()), so other bio sharing this stripe will not free the
2374 * stripe. If a stripe is owned by one stripe, the stripe lock will
2375 * protect it.
2376 */
2377 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002378 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002380 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002381 firstwrite = 1;
2382 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 bip = &sh->dev[dd_idx].toread;
2384 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2385 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2386 goto overlap;
2387 bip = & (*bip)->bi_next;
2388 }
2389 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2390 goto overlap;
2391
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002392 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (*bip)
2394 bi->bi_next = *bip;
2395 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002396 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (forwrite) {
2399 /* check if page is covered */
2400 sector_t sector = sh->dev[dd_idx].sector;
2401 for (bi=sh->dev[dd_idx].towrite;
2402 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2403 bi && bi->bi_sector <= sector;
2404 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2405 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2406 sector = bi->bi_sector + (bi->bi_size>>9);
2407 }
2408 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2409 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2410 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002411 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002412
2413 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2414 (unsigned long long)(*bip)->bi_sector,
2415 (unsigned long long)sh->sector, dd_idx);
2416
2417 if (conf->mddev->bitmap && firstwrite) {
2418 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2419 STRIPE_SECTORS, 0);
2420 sh->bm_seq = conf->seq_flush+1;
2421 set_bit(STRIPE_BIT_DELAY, &sh->state);
2422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return 1;
2424
2425 overlap:
2426 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002427 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return 0;
2429}
2430
NeilBrownd1688a62011-10-11 16:49:52 +11002431static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002432
NeilBrownd1688a62011-10-11 16:49:52 +11002433static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002434 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002435{
NeilBrown784052e2009-03-31 15:19:07 +11002436 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002437 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002438 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002439 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002440 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002441
NeilBrown112bf892009-03-31 14:39:38 +11002442 raid5_compute_sector(conf,
2443 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002444 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002445 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002446 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002447}
2448
Dan Williamsa4456852007-07-09 11:56:43 -07002449static void
NeilBrownd1688a62011-10-11 16:49:52 +11002450handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002451 struct stripe_head_state *s, int disks,
2452 struct bio **return_bi)
2453{
2454 int i;
2455 for (i = disks; i--; ) {
2456 struct bio *bi;
2457 int bitmap_end = 0;
2458
2459 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002460 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002461 rcu_read_lock();
2462 rdev = rcu_dereference(conf->disks[i].rdev);
2463 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002464 atomic_inc(&rdev->nr_pending);
2465 else
2466 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002467 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002468 if (rdev) {
2469 if (!rdev_set_badblocks(
2470 rdev,
2471 sh->sector,
2472 STRIPE_SECTORS, 0))
2473 md_error(conf->mddev, rdev);
2474 rdev_dec_pending(rdev, conf->mddev);
2475 }
Dan Williamsa4456852007-07-09 11:56:43 -07002476 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002477 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002478 /* fail all writes first */
2479 bi = sh->dev[i].towrite;
2480 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002481 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002482 if (bi) {
2483 s->to_write--;
2484 bitmap_end = 1;
2485 }
2486
2487 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2488 wake_up(&conf->wait_for_overlap);
2489
2490 while (bi && bi->bi_sector <
2491 sh->dev[i].sector + STRIPE_SECTORS) {
2492 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2493 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002494 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002495 md_write_end(conf->mddev);
2496 bi->bi_next = *return_bi;
2497 *return_bi = bi;
2498 }
2499 bi = nextbi;
2500 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002501 if (bitmap_end)
2502 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2503 STRIPE_SECTORS, 0, 0);
2504 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002505 /* and fail all 'written' */
2506 bi = sh->dev[i].written;
2507 sh->dev[i].written = NULL;
2508 if (bi) bitmap_end = 1;
2509 while (bi && bi->bi_sector <
2510 sh->dev[i].sector + STRIPE_SECTORS) {
2511 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2512 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002513 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002514 md_write_end(conf->mddev);
2515 bi->bi_next = *return_bi;
2516 *return_bi = bi;
2517 }
2518 bi = bi2;
2519 }
2520
Dan Williamsb5e98d62007-01-02 13:52:31 -07002521 /* fail any reads if this device is non-operational and
2522 * the data has not reached the cache yet.
2523 */
2524 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2525 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2526 test_bit(R5_ReadError, &sh->dev[i].flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002527 bi = sh->dev[i].toread;
2528 sh->dev[i].toread = NULL;
2529 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2530 wake_up(&conf->wait_for_overlap);
2531 if (bi) s->to_read--;
2532 while (bi && bi->bi_sector <
2533 sh->dev[i].sector + STRIPE_SECTORS) {
2534 struct bio *nextbi =
2535 r5_next_bio(bi, sh->dev[i].sector);
2536 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002537 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002538 bi->bi_next = *return_bi;
2539 *return_bi = bi;
2540 }
2541 bi = nextbi;
2542 }
2543 }
Dan Williamsa4456852007-07-09 11:56:43 -07002544 if (bitmap_end)
2545 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2546 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002547 /* If we were in the middle of a write the parity block might
2548 * still be locked - so just clear all R5_LOCKED flags
2549 */
2550 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002551 }
2552
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002553 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2554 if (atomic_dec_and_test(&conf->pending_full_writes))
2555 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002556}
2557
NeilBrown7f0da592011-07-28 11:39:22 +10002558static void
NeilBrownd1688a62011-10-11 16:49:52 +11002559handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002560 struct stripe_head_state *s)
2561{
2562 int abort = 0;
2563 int i;
2564
NeilBrown7f0da592011-07-28 11:39:22 +10002565 clear_bit(STRIPE_SYNCING, &sh->state);
2566 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002567 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002568 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002569 * Don't even need to abort as that is handled elsewhere
2570 * if needed, and not always wanted e.g. if there is a known
2571 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002572 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002573 * non-sync devices, or abort the recovery
2574 */
NeilBrown18b98372012-04-01 23:48:38 +10002575 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2576 /* During recovery devices cannot be removed, so
2577 * locking and refcounting of rdevs is not needed
2578 */
2579 for (i = 0; i < conf->raid_disks; i++) {
2580 struct md_rdev *rdev = conf->disks[i].rdev;
2581 if (rdev
2582 && !test_bit(Faulty, &rdev->flags)
2583 && !test_bit(In_sync, &rdev->flags)
2584 && !rdev_set_badblocks(rdev, sh->sector,
2585 STRIPE_SECTORS, 0))
2586 abort = 1;
2587 rdev = conf->disks[i].replacement;
2588 if (rdev
2589 && !test_bit(Faulty, &rdev->flags)
2590 && !test_bit(In_sync, &rdev->flags)
2591 && !rdev_set_badblocks(rdev, sh->sector,
2592 STRIPE_SECTORS, 0))
2593 abort = 1;
2594 }
2595 if (abort)
2596 conf->recovery_disabled =
2597 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002598 }
NeilBrown18b98372012-04-01 23:48:38 +10002599 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002600}
2601
NeilBrown9a3e1102011-12-23 10:17:53 +11002602static int want_replace(struct stripe_head *sh, int disk_idx)
2603{
2604 struct md_rdev *rdev;
2605 int rv = 0;
2606 /* Doing recovery so rcu locking not required */
2607 rdev = sh->raid_conf->disks[disk_idx].replacement;
2608 if (rdev
2609 && !test_bit(Faulty, &rdev->flags)
2610 && !test_bit(In_sync, &rdev->flags)
2611 && (rdev->recovery_offset <= sh->sector
2612 || rdev->mddev->recovery_cp <= sh->sector))
2613 rv = 1;
2614
2615 return rv;
2616}
2617
NeilBrown93b3dbc2011-07-27 11:00:36 +10002618/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002619 * to be read or computed to satisfy a request.
2620 *
2621 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002622 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002623 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002624static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2625 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002626{
2627 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002628 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2629 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002630
Dan Williamsf38e1212007-01-02 13:52:30 -07002631 /* is the data in this block needed, and can we get it? */
2632 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002633 !test_bit(R5_UPTODATE, &dev->flags) &&
2634 (dev->toread ||
2635 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2636 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002637 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002638 (s->failed >= 1 && fdev[0]->toread) ||
2639 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002640 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2641 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2642 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002643 /* we would like to get this block, possibly by computing it,
2644 * otherwise read it if the backing disk is insync
2645 */
2646 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2647 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2648 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002649 (s->failed && (disk_idx == s->failed_num[0] ||
2650 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002651 /* have disk failed, and we're requested to fetch it;
2652 * do compute it
2653 */
2654 pr_debug("Computing stripe %llu block %d\n",
2655 (unsigned long long)sh->sector, disk_idx);
2656 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2657 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2658 set_bit(R5_Wantcompute, &dev->flags);
2659 sh->ops.target = disk_idx;
2660 sh->ops.target2 = -1; /* no 2nd target */
2661 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002662 /* Careful: from this point on 'uptodate' is in the eye
2663 * of raid_run_ops which services 'compute' operations
2664 * before writes. R5_Wantcompute flags a block that will
2665 * be R5_UPTODATE by the time it is needed for a
2666 * subsequent operation.
2667 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002668 s->uptodate++;
2669 return 1;
2670 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2671 /* Computing 2-failure is *very* expensive; only
2672 * do it if failed >= 2
2673 */
2674 int other;
2675 for (other = disks; other--; ) {
2676 if (other == disk_idx)
2677 continue;
2678 if (!test_bit(R5_UPTODATE,
2679 &sh->dev[other].flags))
2680 break;
2681 }
2682 BUG_ON(other < 0);
2683 pr_debug("Computing stripe %llu blocks %d,%d\n",
2684 (unsigned long long)sh->sector,
2685 disk_idx, other);
2686 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2687 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2688 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2689 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2690 sh->ops.target = disk_idx;
2691 sh->ops.target2 = other;
2692 s->uptodate += 2;
2693 s->req_compute = 1;
2694 return 1;
2695 } else if (test_bit(R5_Insync, &dev->flags)) {
2696 set_bit(R5_LOCKED, &dev->flags);
2697 set_bit(R5_Wantread, &dev->flags);
2698 s->locked++;
2699 pr_debug("Reading block %d (sync=%d)\n",
2700 disk_idx, s->syncing);
2701 }
2702 }
2703
2704 return 0;
2705}
2706
2707/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002708 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002709 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002710static void handle_stripe_fill(struct stripe_head *sh,
2711 struct stripe_head_state *s,
2712 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002713{
2714 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002715
2716 /* look for blocks to read/compute, skip this if a compute
2717 * is already in flight, or if the stripe contents are in the
2718 * midst of changing due to a write
2719 */
2720 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2721 !sh->reconstruct_state)
2722 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002723 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002724 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002725 set_bit(STRIPE_HANDLE, &sh->state);
2726}
2727
2728
Dan Williams1fe797e2008-06-28 09:16:30 +10002729/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002730 * any written block on an uptodate or failed drive can be returned.
2731 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2732 * never LOCKED, so we don't need to test 'failed' directly.
2733 */
NeilBrownd1688a62011-10-11 16:49:52 +11002734static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002735 struct stripe_head *sh, int disks, struct bio **return_bi)
2736{
2737 int i;
2738 struct r5dev *dev;
2739
2740 for (i = disks; i--; )
2741 if (sh->dev[i].written) {
2742 dev = &sh->dev[i];
2743 if (!test_bit(R5_LOCKED, &dev->flags) &&
2744 test_bit(R5_UPTODATE, &dev->flags)) {
2745 /* We can return any write requests */
2746 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002747 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002748 wbi = dev->written;
2749 dev->written = NULL;
2750 while (wbi && wbi->bi_sector <
2751 dev->sector + STRIPE_SECTORS) {
2752 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002753 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002754 md_write_end(conf->mddev);
2755 wbi->bi_next = *return_bi;
2756 *return_bi = wbi;
2757 }
2758 wbi = wbi2;
2759 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002760 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2761 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002762 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002763 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002764 }
2765 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002766
2767 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2768 if (atomic_dec_and_test(&conf->pending_full_writes))
2769 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002770}
2771
NeilBrownd1688a62011-10-11 16:49:52 +11002772static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002773 struct stripe_head *sh,
2774 struct stripe_head_state *s,
2775 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002776{
2777 int rmw = 0, rcw = 0, i;
NeilBrownc8ac1802011-07-27 11:00:36 +10002778 if (conf->max_degraded == 2) {
2779 /* RAID6 requires 'rcw' in current implementation
2780 * Calculate the real rcw later - for now fake it
2781 * look like rcw is cheaper
2782 */
2783 rcw = 1; rmw = 2;
2784 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002785 /* would I have to read this buffer for read_modify_write */
2786 struct r5dev *dev = &sh->dev[i];
2787 if ((dev->towrite || i == sh->pd_idx) &&
2788 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002789 !(test_bit(R5_UPTODATE, &dev->flags) ||
2790 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002791 if (test_bit(R5_Insync, &dev->flags))
2792 rmw++;
2793 else
2794 rmw += 2*disks; /* cannot read it */
2795 }
2796 /* Would I have to read this buffer for reconstruct_write */
2797 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2798 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002799 !(test_bit(R5_UPTODATE, &dev->flags) ||
2800 test_bit(R5_Wantcompute, &dev->flags))) {
2801 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002802 else
2803 rcw += 2*disks;
2804 }
2805 }
Dan Williams45b42332007-07-09 11:56:43 -07002806 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002807 (unsigned long long)sh->sector, rmw, rcw);
2808 set_bit(STRIPE_HANDLE, &sh->state);
2809 if (rmw < rcw && rmw > 0)
2810 /* prefer read-modify-write, but need to get some data */
2811 for (i = disks; i--; ) {
2812 struct r5dev *dev = &sh->dev[i];
2813 if ((dev->towrite || i == sh->pd_idx) &&
2814 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002815 !(test_bit(R5_UPTODATE, &dev->flags) ||
2816 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002817 test_bit(R5_Insync, &dev->flags)) {
2818 if (
2819 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002820 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002821 "%d for r-m-w\n", i);
2822 set_bit(R5_LOCKED, &dev->flags);
2823 set_bit(R5_Wantread, &dev->flags);
2824 s->locked++;
2825 } else {
2826 set_bit(STRIPE_DELAYED, &sh->state);
2827 set_bit(STRIPE_HANDLE, &sh->state);
2828 }
2829 }
2830 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002831 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002832 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002833 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002834 for (i = disks; i--; ) {
2835 struct r5dev *dev = &sh->dev[i];
2836 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002837 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002838 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002839 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002840 test_bit(R5_Wantcompute, &dev->flags))) {
2841 rcw++;
2842 if (!test_bit(R5_Insync, &dev->flags))
2843 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002844 if (
2845 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002846 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002847 "%d for Reconstruct\n", i);
2848 set_bit(R5_LOCKED, &dev->flags);
2849 set_bit(R5_Wantread, &dev->flags);
2850 s->locked++;
2851 } else {
2852 set_bit(STRIPE_DELAYED, &sh->state);
2853 set_bit(STRIPE_HANDLE, &sh->state);
2854 }
2855 }
2856 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002857 }
Dan Williamsa4456852007-07-09 11:56:43 -07002858 /* now if nothing is locked, and if we have enough data,
2859 * we can start a write request
2860 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002861 /* since handle_stripe can be called at any time we need to handle the
2862 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002863 * subsequent call wants to start a write request. raid_run_ops only
2864 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002865 * simultaneously. If this is not the case then new writes need to be
2866 * held off until the compute completes.
2867 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002868 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2869 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2870 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002871 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002872}
2873
NeilBrownd1688a62011-10-11 16:49:52 +11002874static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002875 struct stripe_head_state *s, int disks)
2876{
Dan Williamsecc65c92008-06-28 08:31:57 +10002877 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002878
Dan Williamsbd2ab672008-04-10 21:29:27 -07002879 set_bit(STRIPE_HANDLE, &sh->state);
2880
Dan Williamsecc65c92008-06-28 08:31:57 +10002881 switch (sh->check_state) {
2882 case check_state_idle:
2883 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002884 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002885 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002886 sh->check_state = check_state_run;
2887 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002888 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002889 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002890 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002891 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002892 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002893 /* fall through */
2894 case check_state_compute_result:
2895 sh->check_state = check_state_idle;
2896 if (!dev)
2897 dev = &sh->dev[sh->pd_idx];
2898
2899 /* check that a write has not made the stripe insync */
2900 if (test_bit(STRIPE_INSYNC, &sh->state))
2901 break;
2902
2903 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002904 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2905 BUG_ON(s->uptodate != disks);
2906
2907 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002908 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002909 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002910
Dan Williamsa4456852007-07-09 11:56:43 -07002911 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002912 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002913 break;
2914 case check_state_run:
2915 break; /* we will be called again upon completion */
2916 case check_state_check_result:
2917 sh->check_state = check_state_idle;
2918
2919 /* if a failure occurred during the check operation, leave
2920 * STRIPE_INSYNC not set and let the stripe be handled again
2921 */
2922 if (s->failed)
2923 break;
2924
2925 /* handle a successful check operation, if parity is correct
2926 * we are done. Otherwise update the mismatch count and repair
2927 * parity if !MD_RECOVERY_CHECK
2928 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002929 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002930 /* parity is correct (on disc,
2931 * not in buffer any more)
2932 */
2933 set_bit(STRIPE_INSYNC, &sh->state);
2934 else {
2935 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2936 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2937 /* don't try to repair!! */
2938 set_bit(STRIPE_INSYNC, &sh->state);
2939 else {
2940 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002941 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002942 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2943 set_bit(R5_Wantcompute,
2944 &sh->dev[sh->pd_idx].flags);
2945 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002946 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002947 s->uptodate++;
2948 }
2949 }
2950 break;
2951 case check_state_compute_run:
2952 break;
2953 default:
2954 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2955 __func__, sh->check_state,
2956 (unsigned long long) sh->sector);
2957 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002958 }
2959}
2960
2961
NeilBrownd1688a62011-10-11 16:49:52 +11002962static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07002963 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10002964 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002965{
Dan Williamsa4456852007-07-09 11:56:43 -07002966 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11002967 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07002968 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07002969
2970 set_bit(STRIPE_HANDLE, &sh->state);
2971
2972 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07002973
Dan Williamsa4456852007-07-09 11:56:43 -07002974 /* Want to check and possibly repair P and Q.
2975 * However there could be one 'failed' device, in which
2976 * case we can only check one of them, possibly using the
2977 * other to generate missing data
2978 */
2979
Dan Williamsd82dfee2009-07-14 13:40:57 -07002980 switch (sh->check_state) {
2981 case check_state_idle:
2982 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10002983 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002984 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07002985 * makes sense to check P (If anything else were failed,
2986 * we would have used P to recreate it).
2987 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002988 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07002989 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002990 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07002991 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07002992 * anything, so it makes sense to check it
2993 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07002994 if (sh->check_state == check_state_run)
2995 sh->check_state = check_state_run_pq;
2996 else
2997 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07002998 }
Dan Williams36d1c642009-07-14 11:48:22 -07002999
Dan Williamsd82dfee2009-07-14 13:40:57 -07003000 /* discard potentially stale zero_sum_result */
3001 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003002
Dan Williamsd82dfee2009-07-14 13:40:57 -07003003 if (sh->check_state == check_state_run) {
3004 /* async_xor_zero_sum destroys the contents of P */
3005 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3006 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003007 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003008 if (sh->check_state >= check_state_run &&
3009 sh->check_state <= check_state_run_pq) {
3010 /* async_syndrome_zero_sum preserves P and Q, so
3011 * no need to mark them !uptodate here
3012 */
3013 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3014 break;
3015 }
Dan Williams36d1c642009-07-14 11:48:22 -07003016
Dan Williamsd82dfee2009-07-14 13:40:57 -07003017 /* we have 2-disk failure */
3018 BUG_ON(s->failed != 2);
3019 /* fall through */
3020 case check_state_compute_result:
3021 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003022
Dan Williamsd82dfee2009-07-14 13:40:57 -07003023 /* check that a write has not made the stripe insync */
3024 if (test_bit(STRIPE_INSYNC, &sh->state))
3025 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003026
3027 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003028 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003029 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003030 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003031 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003032 dev = &sh->dev[s->failed_num[1]];
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 }
3037 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003038 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003039 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_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003044 dev = &sh->dev[pd_idx];
3045 s->locked++;
3046 set_bit(R5_LOCKED, &dev->flags);
3047 set_bit(R5_Wantwrite, &dev->flags);
3048 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003049 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003050 dev = &sh->dev[qd_idx];
3051 s->locked++;
3052 set_bit(R5_LOCKED, &dev->flags);
3053 set_bit(R5_Wantwrite, &dev->flags);
3054 }
3055 clear_bit(STRIPE_DEGRADED, &sh->state);
3056
3057 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003058 break;
3059 case check_state_run:
3060 case check_state_run_q:
3061 case check_state_run_pq:
3062 break; /* we will be called again upon completion */
3063 case check_state_check_result:
3064 sh->check_state = check_state_idle;
3065
3066 /* handle a successful check operation, if parity is correct
3067 * we are done. Otherwise update the mismatch count and repair
3068 * parity if !MD_RECOVERY_CHECK
3069 */
3070 if (sh->ops.zero_sum_result == 0) {
3071 /* both parities are correct */
3072 if (!s->failed)
3073 set_bit(STRIPE_INSYNC, &sh->state);
3074 else {
3075 /* in contrast to the raid5 case we can validate
3076 * parity, but still have a failure to write
3077 * back
3078 */
3079 sh->check_state = check_state_compute_result;
3080 /* Returning at this point means that we may go
3081 * off and bring p and/or q uptodate again so
3082 * we make sure to check zero_sum_result again
3083 * to verify if p or q need writeback
3084 */
3085 }
3086 } else {
3087 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3088 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3089 /* don't try to repair!! */
3090 set_bit(STRIPE_INSYNC, &sh->state);
3091 else {
3092 int *target = &sh->ops.target;
3093
3094 sh->ops.target = -1;
3095 sh->ops.target2 = -1;
3096 sh->check_state = check_state_compute_run;
3097 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3098 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3099 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3100 set_bit(R5_Wantcompute,
3101 &sh->dev[pd_idx].flags);
3102 *target = pd_idx;
3103 target = &sh->ops.target2;
3104 s->uptodate++;
3105 }
3106 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3107 set_bit(R5_Wantcompute,
3108 &sh->dev[qd_idx].flags);
3109 *target = qd_idx;
3110 s->uptodate++;
3111 }
3112 }
3113 }
3114 break;
3115 case check_state_compute_run:
3116 break;
3117 default:
3118 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3119 __func__, sh->check_state,
3120 (unsigned long long) sh->sector);
3121 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003122 }
3123}
3124
NeilBrownd1688a62011-10-11 16:49:52 +11003125static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003126{
3127 int i;
3128
3129 /* We have read all the blocks in this stripe and now we need to
3130 * copy some of them into a target stripe for expand.
3131 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003132 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003133 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3134 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003135 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003136 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003137 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003138 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003139
NeilBrown784052e2009-03-31 15:19:07 +11003140 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003141 sector_t s = raid5_compute_sector(conf, bn, 0,
3142 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003143 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003144 if (sh2 == NULL)
3145 /* so far only the early blocks of this stripe
3146 * have been requested. When later blocks
3147 * get requested, we will try again
3148 */
3149 continue;
3150 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3151 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3152 /* must have already done this block */
3153 release_stripe(sh2);
3154 continue;
3155 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003156
3157 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003158 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003159 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003160 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003161 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003162
Dan Williamsa4456852007-07-09 11:56:43 -07003163 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3164 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3165 for (j = 0; j < conf->raid_disks; j++)
3166 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003167 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003168 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3169 break;
3170 if (j == conf->raid_disks) {
3171 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3172 set_bit(STRIPE_HANDLE, &sh2->state);
3173 }
3174 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003175
Dan Williamsa4456852007-07-09 11:56:43 -07003176 }
NeilBrowna2e08552007-09-11 15:23:36 -07003177 /* done submitting copies, wait for them to complete */
3178 if (tx) {
3179 async_tx_ack(tx);
3180 dma_wait_for_async_tx(tx);
3181 }
Dan Williamsa4456852007-07-09 11:56:43 -07003182}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
3184/*
3185 * handle_stripe - do things to a stripe.
3186 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003187 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3188 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003190 * return some read requests which now have data
3191 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 * schedule a read on some buffers
3193 * schedule a write of some buffers
3194 * return confirmation of parity correctness
3195 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 */
Dan Williamsa4456852007-07-09 11:56:43 -07003197
NeilBrownacfe7262011-07-27 11:00:36 +10003198static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003199{
NeilBrownd1688a62011-10-11 16:49:52 +11003200 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003201 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003202 struct r5dev *dev;
3203 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003204 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003205
NeilBrownacfe7262011-07-27 11:00:36 +10003206 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003207
NeilBrownacfe7262011-07-27 11:00:36 +10003208 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3209 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3210 s->failed_num[0] = -1;
3211 s->failed_num[1] = -1;
3212
3213 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003214 rcu_read_lock();
3215 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003216 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003217 sector_t first_bad;
3218 int bad_sectors;
3219 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003220
NeilBrown16a53ec2006-06-26 00:27:38 -07003221 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003222
Dan Williams45b42332007-07-09 11:56:43 -07003223 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003224 i, dev->flags,
3225 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003226 /* maybe we can reply to a read
3227 *
3228 * new wantfill requests are only permitted while
3229 * ops_complete_biofill is guaranteed to be inactive
3230 */
3231 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3232 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3233 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003234
3235 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003236 if (test_bit(R5_LOCKED, &dev->flags))
3237 s->locked++;
3238 if (test_bit(R5_UPTODATE, &dev->flags))
3239 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003240 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003241 s->compute++;
3242 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003243 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003244
NeilBrownacfe7262011-07-27 11:00:36 +10003245 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003246 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003247 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003248 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003249 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003250 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003251 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003252 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003253 }
Dan Williamsa4456852007-07-09 11:56:43 -07003254 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003255 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003256 /* Prefer to use the replacement for reads, but only
3257 * if it is recovered enough and has no bad blocks.
3258 */
3259 rdev = rcu_dereference(conf->disks[i].replacement);
3260 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3261 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3262 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3263 &first_bad, &bad_sectors))
3264 set_bit(R5_ReadRepl, &dev->flags);
3265 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003266 if (rdev)
3267 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003268 rdev = rcu_dereference(conf->disks[i].rdev);
3269 clear_bit(R5_ReadRepl, &dev->flags);
3270 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003271 if (rdev && test_bit(Faulty, &rdev->flags))
3272 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003273 if (rdev) {
3274 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3275 &first_bad, &bad_sectors);
3276 if (s->blocked_rdev == NULL
3277 && (test_bit(Blocked, &rdev->flags)
3278 || is_bad < 0)) {
3279 if (is_bad < 0)
3280 set_bit(BlockedBadBlocks,
3281 &rdev->flags);
3282 s->blocked_rdev = rdev;
3283 atomic_inc(&rdev->nr_pending);
3284 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003285 }
NeilBrown415e72d2010-06-17 17:25:21 +10003286 clear_bit(R5_Insync, &dev->flags);
3287 if (!rdev)
3288 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003289 else if (is_bad) {
3290 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003291 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3292 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003293 /* treat as in-sync, but with a read error
3294 * which we can now try to correct
3295 */
3296 set_bit(R5_Insync, &dev->flags);
3297 set_bit(R5_ReadError, &dev->flags);
3298 }
3299 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003300 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003301 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003302 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003303 set_bit(R5_Insync, &dev->flags);
3304 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3305 test_bit(R5_Expanded, &dev->flags))
3306 /* If we've reshaped into here, we assume it is Insync.
3307 * We will shortly update recovery_offset to make
3308 * it official.
3309 */
3310 set_bit(R5_Insync, &dev->flags);
3311
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003312 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003313 /* This flag does not apply to '.replacement'
3314 * only to .rdev, so make sure to check that*/
3315 struct md_rdev *rdev2 = rcu_dereference(
3316 conf->disks[i].rdev);
3317 if (rdev2 == rdev)
3318 clear_bit(R5_Insync, &dev->flags);
3319 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003320 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003321 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003322 } else
3323 clear_bit(R5_WriteError, &dev->flags);
3324 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003325 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003326 /* This flag does not apply to '.replacement'
3327 * only to .rdev, so make sure to check that*/
3328 struct md_rdev *rdev2 = rcu_dereference(
3329 conf->disks[i].rdev);
3330 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003331 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003332 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003333 } else
3334 clear_bit(R5_MadeGood, &dev->flags);
3335 }
NeilBrown977df362011-12-23 10:17:53 +11003336 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3337 struct md_rdev *rdev2 = rcu_dereference(
3338 conf->disks[i].replacement);
3339 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3340 s->handle_bad_blocks = 1;
3341 atomic_inc(&rdev2->nr_pending);
3342 } else
3343 clear_bit(R5_MadeGoodRepl, &dev->flags);
3344 }
NeilBrown415e72d2010-06-17 17:25:21 +10003345 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003346 /* The ReadError flag will just be confusing now */
3347 clear_bit(R5_ReadError, &dev->flags);
3348 clear_bit(R5_ReWrite, &dev->flags);
3349 }
NeilBrown415e72d2010-06-17 17:25:21 +10003350 if (test_bit(R5_ReadError, &dev->flags))
3351 clear_bit(R5_Insync, &dev->flags);
3352 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003353 if (s->failed < 2)
3354 s->failed_num[s->failed] = i;
3355 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003356 if (rdev && !test_bit(Faulty, &rdev->flags))
3357 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003358 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003359 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003360 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3361 /* If there is a failed device being replaced,
3362 * we must be recovering.
3363 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003364 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003365 * else we can only be replacing
3366 * sync and recovery both need to read all devices, and so
3367 * use the same flag.
3368 */
3369 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003370 sh->sector >= conf->mddev->recovery_cp ||
3371 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003372 s->syncing = 1;
3373 else
3374 s->replacing = 1;
3375 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003376 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003377}
NeilBrownf4168852007-02-28 20:11:53 -08003378
NeilBrowncc940152011-07-26 11:35:35 +10003379static void handle_stripe(struct stripe_head *sh)
3380{
3381 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003382 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003383 int i;
NeilBrown84789552011-07-27 11:00:36 +10003384 int prexor;
3385 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003386 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003387
3388 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003389 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003390 /* already being handled, ensure it gets handled
3391 * again when current action finishes */
3392 set_bit(STRIPE_HANDLE, &sh->state);
3393 return;
3394 }
3395
3396 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3397 set_bit(STRIPE_SYNCING, &sh->state);
3398 clear_bit(STRIPE_INSYNC, &sh->state);
3399 }
3400 clear_bit(STRIPE_DELAYED, &sh->state);
3401
3402 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3403 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3404 (unsigned long long)sh->sector, sh->state,
3405 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3406 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003407
NeilBrownacfe7262011-07-27 11:00:36 +10003408 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003409
NeilBrownbc2607f2011-07-28 11:39:22 +10003410 if (s.handle_bad_blocks) {
3411 set_bit(STRIPE_HANDLE, &sh->state);
3412 goto finish;
3413 }
3414
NeilBrown474af965fe2011-07-27 11:00:36 +10003415 if (unlikely(s.blocked_rdev)) {
3416 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003417 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003418 set_bit(STRIPE_HANDLE, &sh->state);
3419 goto finish;
3420 }
3421 /* There is nothing for the blocked_rdev to block */
3422 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3423 s.blocked_rdev = NULL;
3424 }
3425
3426 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3427 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3428 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3429 }
3430
3431 pr_debug("locked=%d uptodate=%d to_read=%d"
3432 " to_write=%d failed=%d failed_num=%d,%d\n",
3433 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3434 s.failed_num[0], s.failed_num[1]);
3435 /* check if the array has lost more than max_degraded devices and,
3436 * if so, some requests might need to be failed.
3437 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003438 if (s.failed > conf->max_degraded) {
3439 sh->check_state = 0;
3440 sh->reconstruct_state = 0;
3441 if (s.to_read+s.to_write+s.written)
3442 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003443 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003444 handle_failed_sync(conf, sh, &s);
3445 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003446
3447 /*
3448 * might be able to return some write requests if the parity blocks
3449 * are safe, or on a failed drive
3450 */
3451 pdev = &sh->dev[sh->pd_idx];
3452 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3453 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3454 qdev = &sh->dev[sh->qd_idx];
3455 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3456 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3457 || conf->level < 6;
3458
3459 if (s.written &&
3460 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3461 && !test_bit(R5_LOCKED, &pdev->flags)
3462 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3463 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3464 && !test_bit(R5_LOCKED, &qdev->flags)
3465 && test_bit(R5_UPTODATE, &qdev->flags)))))
3466 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3467
3468 /* Now we might consider reading some blocks, either to check/generate
3469 * parity, or to satisfy requests
3470 * or to load a block that is being partially written.
3471 */
3472 if (s.to_read || s.non_overwrite
3473 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003474 || (s.syncing && (s.uptodate + s.compute < disks))
3475 || s.replacing
3476 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003477 handle_stripe_fill(sh, &s, disks);
3478
NeilBrown84789552011-07-27 11:00:36 +10003479 /* Now we check to see if any write operations have recently
3480 * completed
3481 */
3482 prexor = 0;
3483 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3484 prexor = 1;
3485 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3486 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3487 sh->reconstruct_state = reconstruct_state_idle;
3488
3489 /* All the 'written' buffers and the parity block are ready to
3490 * be written back to disk
3491 */
3492 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3493 BUG_ON(sh->qd_idx >= 0 &&
3494 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3495 for (i = disks; i--; ) {
3496 struct r5dev *dev = &sh->dev[i];
3497 if (test_bit(R5_LOCKED, &dev->flags) &&
3498 (i == sh->pd_idx || i == sh->qd_idx ||
3499 dev->written)) {
3500 pr_debug("Writing block %d\n", i);
3501 set_bit(R5_Wantwrite, &dev->flags);
3502 if (prexor)
3503 continue;
3504 if (!test_bit(R5_Insync, &dev->flags) ||
3505 ((i == sh->pd_idx || i == sh->qd_idx) &&
3506 s.failed == 0))
3507 set_bit(STRIPE_INSYNC, &sh->state);
3508 }
3509 }
3510 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3511 s.dec_preread_active = 1;
3512 }
3513
3514 /* Now to consider new write requests and what else, if anything
3515 * should be read. We do not handle new writes when:
3516 * 1/ A 'write' operation (copy+xor) is already in flight.
3517 * 2/ A 'check' operation is in flight, as it may clobber the parity
3518 * block.
3519 */
3520 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3521 handle_stripe_dirtying(conf, sh, &s, disks);
3522
3523 /* maybe we need to check and possibly fix the parity for this stripe
3524 * Any reads will already have been scheduled, so we just see if enough
3525 * data is available. The parity check is held off while parity
3526 * dependent operations are in flight.
3527 */
3528 if (sh->check_state ||
3529 (s.syncing && s.locked == 0 &&
3530 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3531 !test_bit(STRIPE_INSYNC, &sh->state))) {
3532 if (conf->level == 6)
3533 handle_parity_checks6(conf, sh, &s, disks);
3534 else
3535 handle_parity_checks5(conf, sh, &s, disks);
3536 }
NeilBrownc5a31002011-07-27 11:00:36 +10003537
NeilBrown9a3e1102011-12-23 10:17:53 +11003538 if (s.replacing && s.locked == 0
3539 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3540 /* Write out to replacement devices where possible */
3541 for (i = 0; i < conf->raid_disks; i++)
3542 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3543 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3544 set_bit(R5_WantReplace, &sh->dev[i].flags);
3545 set_bit(R5_LOCKED, &sh->dev[i].flags);
3546 s.locked++;
3547 }
3548 set_bit(STRIPE_INSYNC, &sh->state);
3549 }
3550 if ((s.syncing || s.replacing) && s.locked == 0 &&
3551 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003552 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3553 clear_bit(STRIPE_SYNCING, &sh->state);
3554 }
3555
3556 /* If the failed drives are just a ReadError, then we might need
3557 * to progress the repair/check process
3558 */
3559 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3560 for (i = 0; i < s.failed; i++) {
3561 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3562 if (test_bit(R5_ReadError, &dev->flags)
3563 && !test_bit(R5_LOCKED, &dev->flags)
3564 && test_bit(R5_UPTODATE, &dev->flags)
3565 ) {
3566 if (!test_bit(R5_ReWrite, &dev->flags)) {
3567 set_bit(R5_Wantwrite, &dev->flags);
3568 set_bit(R5_ReWrite, &dev->flags);
3569 set_bit(R5_LOCKED, &dev->flags);
3570 s.locked++;
3571 } else {
3572 /* let's read it back */
3573 set_bit(R5_Wantread, &dev->flags);
3574 set_bit(R5_LOCKED, &dev->flags);
3575 s.locked++;
3576 }
3577 }
3578 }
3579
3580
NeilBrown3687c062011-07-27 11:00:36 +10003581 /* Finish reconstruct operations initiated by the expansion process */
3582 if (sh->reconstruct_state == reconstruct_state_result) {
3583 struct stripe_head *sh_src
3584 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3585 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3586 /* sh cannot be written until sh_src has been read.
3587 * so arrange for sh to be delayed a little
3588 */
3589 set_bit(STRIPE_DELAYED, &sh->state);
3590 set_bit(STRIPE_HANDLE, &sh->state);
3591 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3592 &sh_src->state))
3593 atomic_inc(&conf->preread_active_stripes);
3594 release_stripe(sh_src);
3595 goto finish;
3596 }
3597 if (sh_src)
3598 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003599
NeilBrown3687c062011-07-27 11:00:36 +10003600 sh->reconstruct_state = reconstruct_state_idle;
3601 clear_bit(STRIPE_EXPANDING, &sh->state);
3602 for (i = conf->raid_disks; i--; ) {
3603 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3604 set_bit(R5_LOCKED, &sh->dev[i].flags);
3605 s.locked++;
3606 }
3607 }
3608
3609 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3610 !sh->reconstruct_state) {
3611 /* Need to write out all blocks after computing parity */
3612 sh->disks = conf->raid_disks;
3613 stripe_set_idx(sh->sector, conf, 0, sh);
3614 schedule_reconstruction(sh, &s, 1, 1);
3615 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3616 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3617 atomic_dec(&conf->reshape_stripes);
3618 wake_up(&conf->wait_for_overlap);
3619 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3620 }
3621
3622 if (s.expanding && s.locked == 0 &&
3623 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3624 handle_stripe_expansion(conf, sh);
3625
3626finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003627 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003628 if (unlikely(s.blocked_rdev)) {
3629 if (conf->mddev->external)
3630 md_wait_for_blocked_rdev(s.blocked_rdev,
3631 conf->mddev);
3632 else
3633 /* Internal metadata will immediately
3634 * be written by raid5d, so we don't
3635 * need to wait here.
3636 */
3637 rdev_dec_pending(s.blocked_rdev,
3638 conf->mddev);
3639 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003640
NeilBrownbc2607f2011-07-28 11:39:22 +10003641 if (s.handle_bad_blocks)
3642 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003643 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003644 struct r5dev *dev = &sh->dev[i];
3645 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3646 /* We own a safe reference to the rdev */
3647 rdev = conf->disks[i].rdev;
3648 if (!rdev_set_badblocks(rdev, sh->sector,
3649 STRIPE_SECTORS, 0))
3650 md_error(conf->mddev, rdev);
3651 rdev_dec_pending(rdev, conf->mddev);
3652 }
NeilBrownb84db562011-07-28 11:39:23 +10003653 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3654 rdev = conf->disks[i].rdev;
3655 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003656 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003657 rdev_dec_pending(rdev, conf->mddev);
3658 }
NeilBrown977df362011-12-23 10:17:53 +11003659 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3660 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003661 if (!rdev)
3662 /* rdev have been moved down */
3663 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003664 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003665 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003666 rdev_dec_pending(rdev, conf->mddev);
3667 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003668 }
3669
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003670 if (s.ops_request)
3671 raid_run_ops(sh, s.ops_request);
3672
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003673 ops_run_io(sh, &s);
3674
NeilBrownc5709ef2011-07-26 11:35:20 +10003675 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003676 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003677 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003678 * have actually been submitted.
3679 */
3680 atomic_dec(&conf->preread_active_stripes);
3681 if (atomic_read(&conf->preread_active_stripes) <
3682 IO_THRESHOLD)
3683 md_wakeup_thread(conf->mddev->thread);
3684 }
3685
NeilBrownc5709ef2011-07-26 11:35:20 +10003686 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003687
Dan Williams257a4b42011-11-08 16:22:06 +11003688 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003689}
3690
NeilBrownd1688a62011-10-11 16:49:52 +11003691static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692{
3693 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3694 while (!list_empty(&conf->delayed_list)) {
3695 struct list_head *l = conf->delayed_list.next;
3696 struct stripe_head *sh;
3697 sh = list_entry(l, struct stripe_head, lru);
3698 list_del_init(l);
3699 clear_bit(STRIPE_DELAYED, &sh->state);
3700 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3701 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003702 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 }
NeilBrown482c0832011-04-18 18:25:42 +10003704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705}
3706
NeilBrownd1688a62011-10-11 16:49:52 +11003707static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003708{
3709 /* device_lock is held */
3710 struct list_head head;
3711 list_add(&head, &conf->bitmap_list);
3712 list_del_init(&conf->bitmap_list);
3713 while (!list_empty(&head)) {
3714 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3715 list_del_init(&sh->lru);
3716 atomic_inc(&sh->count);
3717 __release_stripe(conf, sh);
3718 }
3719}
3720
NeilBrownfd01b882011-10-11 16:47:53 +11003721int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003722{
NeilBrownd1688a62011-10-11 16:49:52 +11003723 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003724
3725 /* No difference between reads and writes. Just check
3726 * how busy the stripe_cache is
3727 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003728
NeilBrownf022b2f2006-10-03 01:15:56 -07003729 if (conf->inactive_blocked)
3730 return 1;
3731 if (conf->quiesce)
3732 return 1;
3733 if (list_empty_careful(&conf->inactive_list))
3734 return 1;
3735
3736 return 0;
3737}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003738EXPORT_SYMBOL_GPL(md_raid5_congested);
3739
3740static int raid5_congested(void *data, int bits)
3741{
NeilBrownfd01b882011-10-11 16:47:53 +11003742 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003743
3744 return mddev_congested(mddev, bits) ||
3745 md_raid5_congested(mddev, bits);
3746}
NeilBrownf022b2f2006-10-03 01:15:56 -07003747
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003748/* We want read requests to align with chunks where possible,
3749 * but write requests don't need to.
3750 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003751static int raid5_mergeable_bvec(struct request_queue *q,
3752 struct bvec_merge_data *bvm,
3753 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003754{
NeilBrownfd01b882011-10-11 16:47:53 +11003755 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003756 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003757 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003758 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003759 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003760
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003761 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003762 return biovec->bv_len; /* always allow writes to be mergeable */
3763
Andre Noll664e7c42009-06-18 08:45:27 +10003764 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3765 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003766 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3767 if (max < 0) max = 0;
3768 if (max <= biovec->bv_len && bio_sectors == 0)
3769 return biovec->bv_len;
3770 else
3771 return max;
3772}
3773
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003774
NeilBrownfd01b882011-10-11 16:47:53 +11003775static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003776{
3777 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003778 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003779 unsigned int bio_sectors = bio->bi_size >> 9;
3780
Andre Noll664e7c42009-06-18 08:45:27 +10003781 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3782 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003783 return chunk_sectors >=
3784 ((sector & (chunk_sectors - 1)) + bio_sectors);
3785}
3786
3787/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003788 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3789 * later sampled by raid5d.
3790 */
NeilBrownd1688a62011-10-11 16:49:52 +11003791static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003792{
3793 unsigned long flags;
3794
3795 spin_lock_irqsave(&conf->device_lock, flags);
3796
3797 bi->bi_next = conf->retry_read_aligned_list;
3798 conf->retry_read_aligned_list = bi;
3799
3800 spin_unlock_irqrestore(&conf->device_lock, flags);
3801 md_wakeup_thread(conf->mddev->thread);
3802}
3803
3804
NeilBrownd1688a62011-10-11 16:49:52 +11003805static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003806{
3807 struct bio *bi;
3808
3809 bi = conf->retry_read_aligned;
3810 if (bi) {
3811 conf->retry_read_aligned = NULL;
3812 return bi;
3813 }
3814 bi = conf->retry_read_aligned_list;
3815 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003816 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003817 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003818 /*
3819 * this sets the active strip count to 1 and the processed
3820 * strip count to zero (upper 8 bits)
3821 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003822 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003823 }
3824
3825 return bi;
3826}
3827
3828
3829/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003830 * The "raid5_align_endio" should check if the read succeeded and if it
3831 * did, call bio_endio on the original bio (having bio_put the new bio
3832 * first).
3833 * If the read failed..
3834 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003835static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003836{
3837 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003838 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003839 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003840 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003841 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003842
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003843 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003844
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003845 rdev = (void*)raid_bi->bi_next;
3846 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003847 mddev = rdev->mddev;
3848 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003849
3850 rdev_dec_pending(rdev, conf->mddev);
3851
3852 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003853 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003854 if (atomic_dec_and_test(&conf->active_aligned_reads))
3855 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003856 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003857 }
3858
3859
Dan Williams45b42332007-07-09 11:56:43 -07003860 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003861
3862 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003863}
3864
Neil Brown387bb172007-02-08 14:20:29 -08003865static int bio_fits_rdev(struct bio *bi)
3866{
Jens Axboe165125e2007-07-24 09:28:11 +02003867 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003868
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003869 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003870 return 0;
3871 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003872 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003873 return 0;
3874
3875 if (q->merge_bvec_fn)
3876 /* it's too hard to apply the merge_bvec_fn at this stage,
3877 * just just give up
3878 */
3879 return 0;
3880
3881 return 1;
3882}
3883
3884
NeilBrownfd01b882011-10-11 16:47:53 +11003885static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003886{
NeilBrownd1688a62011-10-11 16:49:52 +11003887 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003888 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003889 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003890 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003891 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003892
3893 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003894 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003895 return 0;
3896 }
3897 /*
NeilBrowna167f662010-10-26 18:31:13 +11003898 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003899 */
NeilBrowna167f662010-10-26 18:31:13 +11003900 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003901 if (!align_bi)
3902 return 0;
3903 /*
3904 * set bi_end_io to a new function, and set bi_private to the
3905 * original bio.
3906 */
3907 align_bi->bi_end_io = raid5_align_endio;
3908 align_bi->bi_private = raid_bio;
3909 /*
3910 * compute position
3911 */
NeilBrown112bf892009-03-31 14:39:38 +11003912 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3913 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003914 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003915
NeilBrown671488c2011-12-23 10:17:52 +11003916 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003917 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003918 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3919 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3920 rdev->recovery_offset < end_sector) {
3921 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3922 if (rdev &&
3923 (test_bit(Faulty, &rdev->flags) ||
3924 !(test_bit(In_sync, &rdev->flags) ||
3925 rdev->recovery_offset >= end_sector)))
3926 rdev = NULL;
3927 }
3928 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003929 sector_t first_bad;
3930 int bad_sectors;
3931
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003932 atomic_inc(&rdev->nr_pending);
3933 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003934 raid_bio->bi_next = (void*)rdev;
3935 align_bi->bi_bdev = rdev->bdev;
3936 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003937
NeilBrown31c176e2011-07-28 11:39:22 +10003938 if (!bio_fits_rdev(align_bi) ||
3939 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3940 &first_bad, &bad_sectors)) {
3941 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003942 bio_put(align_bi);
3943 rdev_dec_pending(rdev, mddev);
3944 return 0;
3945 }
3946
majianpeng6c0544e2012-06-12 08:31:10 +08003947 /* No reshape active, so we can trust rdev->data_offset */
3948 align_bi->bi_sector += rdev->data_offset;
3949
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003950 spin_lock_irq(&conf->device_lock);
3951 wait_event_lock_irq(conf->wait_for_stripe,
3952 conf->quiesce == 0,
3953 conf->device_lock, /* nothing */);
3954 atomic_inc(&conf->active_aligned_reads);
3955 spin_unlock_irq(&conf->device_lock);
3956
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003957 generic_make_request(align_bi);
3958 return 1;
3959 } else {
3960 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003961 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003962 return 0;
3963 }
3964}
3965
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003966/* __get_priority_stripe - get the next stripe to process
3967 *
3968 * Full stripe writes are allowed to pass preread active stripes up until
3969 * the bypass_threshold is exceeded. In general the bypass_count
3970 * increments when the handle_list is handled before the hold_list; however, it
3971 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3972 * stripe with in flight i/o. The bypass_count will be reset when the
3973 * head of the hold_list has changed, i.e. the head was promoted to the
3974 * handle_list.
3975 */
NeilBrownd1688a62011-10-11 16:49:52 +11003976static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003977{
3978 struct stripe_head *sh;
3979
3980 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3981 __func__,
3982 list_empty(&conf->handle_list) ? "empty" : "busy",
3983 list_empty(&conf->hold_list) ? "empty" : "busy",
3984 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3985
3986 if (!list_empty(&conf->handle_list)) {
3987 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3988
3989 if (list_empty(&conf->hold_list))
3990 conf->bypass_count = 0;
3991 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3992 if (conf->hold_list.next == conf->last_hold)
3993 conf->bypass_count++;
3994 else {
3995 conf->last_hold = conf->hold_list.next;
3996 conf->bypass_count -= conf->bypass_threshold;
3997 if (conf->bypass_count < 0)
3998 conf->bypass_count = 0;
3999 }
4000 }
4001 } else if (!list_empty(&conf->hold_list) &&
4002 ((conf->bypass_threshold &&
4003 conf->bypass_count > conf->bypass_threshold) ||
4004 atomic_read(&conf->pending_full_writes) == 0)) {
4005 sh = list_entry(conf->hold_list.next,
4006 typeof(*sh), lru);
4007 conf->bypass_count -= conf->bypass_threshold;
4008 if (conf->bypass_count < 0)
4009 conf->bypass_count = 0;
4010 } else
4011 return NULL;
4012
4013 list_del_init(&sh->lru);
4014 atomic_inc(&sh->count);
4015 BUG_ON(atomic_read(&sh->count) != 1);
4016 return sh;
4017}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004018
Shaohua Li8811b592012-08-02 08:33:00 +10004019struct raid5_plug_cb {
4020 struct blk_plug_cb cb;
4021 struct list_head list;
4022};
4023
4024static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4025{
4026 struct raid5_plug_cb *cb = container_of(
4027 blk_cb, struct raid5_plug_cb, cb);
4028 struct stripe_head *sh;
4029 struct mddev *mddev = cb->cb.data;
4030 struct r5conf *conf = mddev->private;
4031
4032 if (cb->list.next && !list_empty(&cb->list)) {
4033 spin_lock_irq(&conf->device_lock);
4034 while (!list_empty(&cb->list)) {
4035 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4036 list_del_init(&sh->lru);
4037 /*
4038 * avoid race release_stripe_plug() sees
4039 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4040 * is still in our list
4041 */
4042 smp_mb__before_clear_bit();
4043 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4044 __release_stripe(conf, sh);
4045 }
4046 spin_unlock_irq(&conf->device_lock);
4047 }
4048 kfree(cb);
4049}
4050
4051static void release_stripe_plug(struct mddev *mddev,
4052 struct stripe_head *sh)
4053{
4054 struct blk_plug_cb *blk_cb = blk_check_plugged(
4055 raid5_unplug, mddev,
4056 sizeof(struct raid5_plug_cb));
4057 struct raid5_plug_cb *cb;
4058
4059 if (!blk_cb) {
4060 release_stripe(sh);
4061 return;
4062 }
4063
4064 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4065
4066 if (cb->list.next == NULL)
4067 INIT_LIST_HEAD(&cb->list);
4068
4069 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4070 list_add_tail(&sh->lru, &cb->list);
4071 else
4072 release_stripe(sh);
4073}
4074
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004075static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076{
NeilBrownd1688a62011-10-11 16:49:52 +11004077 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004078 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 sector_t new_sector;
4080 sector_t logical_sector, last_sector;
4081 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004082 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004083 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
Tejun Heoe9c74692010-09-03 11:56:18 +02004085 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4086 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004087 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004088 }
4089
NeilBrown3d310eb2005-06-21 17:17:26 -07004090 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004091
NeilBrown802ba062006-12-13 00:34:13 -08004092 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004093 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004094 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004095 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004096
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4098 last_sector = bi->bi_sector + (bi->bi_size>>9);
4099 bi->bi_next = NULL;
4100 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004101
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4103 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004104 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004105
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004106 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004107 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004108 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004109 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004110 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004111 * 64bit on a 32bit platform, and so it might be
4112 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004113 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004114 * the lock is dropped, so once we get a reference
4115 * to the stripe that we think it is, we will have
4116 * to check again.
4117 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004118 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004119 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004120 ? logical_sector < conf->reshape_progress
4121 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004122 previous = 1;
4123 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004124 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004125 ? logical_sector < conf->reshape_safe
4126 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004127 spin_unlock_irq(&conf->device_lock);
4128 schedule();
4129 goto retry;
4130 }
4131 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004132 spin_unlock_irq(&conf->device_lock);
4133 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004134
NeilBrown112bf892009-03-31 14:39:38 +11004135 new_sector = raid5_compute_sector(conf, logical_sector,
4136 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004137 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004138 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 (unsigned long long)new_sector,
4140 (unsigned long long)logical_sector);
4141
NeilBrownb5663ba2009-03-31 14:39:38 +11004142 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004143 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004145 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004146 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004147 * stripe, so we must do the range check again.
4148 * Expansion could still move past after this
4149 * test, but as we are holding a reference to
4150 * 'sh', we know that if that happens,
4151 * STRIPE_EXPANDING will get set and the expansion
4152 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004153 */
4154 int must_retry = 0;
4155 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004156 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004157 ? logical_sector >= conf->reshape_progress
4158 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004159 /* mismatch, need to try again */
4160 must_retry = 1;
4161 spin_unlock_irq(&conf->device_lock);
4162 if (must_retry) {
4163 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004164 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004165 goto retry;
4166 }
4167 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004168
Namhyung Kimffd96e32011-07-18 17:38:51 +10004169 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004170 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004171 logical_sector < mddev->suspend_hi) {
4172 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004173 /* As the suspend_* range is controlled by
4174 * userspace, we want an interruptible
4175 * wait.
4176 */
4177 flush_signals(current);
4178 prepare_to_wait(&conf->wait_for_overlap,
4179 &w, TASK_INTERRUPTIBLE);
4180 if (logical_sector >= mddev->suspend_lo &&
4181 logical_sector < mddev->suspend_hi)
4182 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004183 goto retry;
4184 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004185
4186 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004187 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004188 /* Stripe is busy expanding or
4189 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 * and wait a while
4191 */
NeilBrown482c0832011-04-18 18:25:42 +10004192 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 release_stripe(sh);
4194 schedule();
4195 goto retry;
4196 }
4197 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004198 set_bit(STRIPE_HANDLE, &sh->state);
4199 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004200 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004201 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4202 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004203 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 } else {
4205 /* cannot get stripe for read-ahead, just give-up */
4206 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4207 finish_wait(&conf->wait_for_overlap, &w);
4208 break;
4209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004211
Shaohua Lie7836bd62012-07-19 16:01:31 +10004212 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004213 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
NeilBrown16a53ec2006-06-26 00:27:38 -07004215 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004217
Neil Brown0e13fe232008-06-28 08:31:20 +10004218 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220}
4221
NeilBrownfd01b882011-10-11 16:47:53 +11004222static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004223
NeilBrownfd01b882011-10-11 16:47:53 +11004224static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
NeilBrown52c03292006-06-26 00:27:43 -07004226 /* reshaping is quite different to recovery/resync so it is
4227 * handled quite separately ... here.
4228 *
4229 * On each call to sync_request, we gather one chunk worth of
4230 * destination stripes and flag them as expanding.
4231 * Then we find all the source stripes and request reads.
4232 * As the reads complete, handle_stripe will copy the data
4233 * into the destination stripe and release that stripe.
4234 */
NeilBrownd1688a62011-10-11 16:49:52 +11004235 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004237 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004238 int raid_disks = conf->previous_raid_disks;
4239 int data_disks = raid_disks - conf->max_degraded;
4240 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004241 int i;
4242 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004243 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004244 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004245 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004246 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004247
NeilBrownfef9c612009-03-31 15:16:46 +11004248 if (sector_nr == 0) {
4249 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004250 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004251 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4252 sector_nr = raid5_size(mddev, 0, 0)
4253 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004254 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004255 conf->reshape_progress > 0)
4256 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004257 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004258 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004259 mddev->curr_resync_completed = sector_nr;
4260 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004261 *skipped = 1;
4262 return sector_nr;
4263 }
NeilBrown52c03292006-06-26 00:27:43 -07004264 }
4265
NeilBrown7a661382009-03-31 15:21:40 +11004266 /* We need to process a full chunk at a time.
4267 * If old and new chunk sizes differ, we need to process the
4268 * largest of these
4269 */
Andre Noll664e7c42009-06-18 08:45:27 +10004270 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4271 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004272 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004273 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004274
NeilBrownb5254dd2012-05-21 09:27:01 +10004275 /* We update the metadata at least every 10 seconds, or when
4276 * the data about to be copied would over-write the source of
4277 * the data at the front of the range. i.e. one new_stripe
4278 * along from reshape_progress new_maps to after where
4279 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004280 */
NeilBrownfef9c612009-03-31 15:16:46 +11004281 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004282 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004283 readpos = conf->reshape_progress;
4284 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004285 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004286 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004287 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004288 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004289 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004290 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004291 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004292 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004293 readpos -= min_t(sector_t, reshape_sectors, readpos);
4294 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004295 }
NeilBrown52c03292006-06-26 00:27:43 -07004296
NeilBrownb5254dd2012-05-21 09:27:01 +10004297 /* Having calculated the 'writepos' possibly use it
4298 * to set 'stripe_addr' which is where we will write to.
4299 */
4300 if (mddev->reshape_backwards) {
4301 BUG_ON(conf->reshape_progress == 0);
4302 stripe_addr = writepos;
4303 BUG_ON((mddev->dev_sectors &
4304 ~((sector_t)reshape_sectors - 1))
4305 - reshape_sectors - stripe_addr
4306 != sector_nr);
4307 } else {
4308 BUG_ON(writepos != sector_nr + reshape_sectors);
4309 stripe_addr = sector_nr;
4310 }
4311
NeilBrownc8f517c2009-03-31 15:28:40 +11004312 /* 'writepos' is the most advanced device address we might write.
4313 * 'readpos' is the least advanced device address we might read.
4314 * 'safepos' is the least address recorded in the metadata as having
4315 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004316 * If there is a min_offset_diff, these are adjusted either by
4317 * increasing the safepos/readpos if diff is negative, or
4318 * increasing writepos if diff is positive.
4319 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004320 * ensure safety in the face of a crash - that must be done by userspace
4321 * making a backup of the data. So in that case there is no particular
4322 * rush to update metadata.
4323 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4324 * update the metadata to advance 'safepos' to match 'readpos' so that
4325 * we can be safe in the event of a crash.
4326 * So we insist on updating metadata if safepos is behind writepos and
4327 * readpos is beyond writepos.
4328 * In any case, update the metadata every 10 seconds.
4329 * Maybe that number should be configurable, but I'm not sure it is
4330 * worth it.... maybe it could be a multiple of safemode_delay???
4331 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004332 if (conf->min_offset_diff < 0) {
4333 safepos += -conf->min_offset_diff;
4334 readpos += -conf->min_offset_diff;
4335 } else
4336 writepos += conf->min_offset_diff;
4337
NeilBrown2c810cd2012-05-21 09:27:00 +10004338 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004339 ? (safepos > writepos && readpos < writepos)
4340 : (safepos < writepos && readpos > writepos)) ||
4341 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004342 /* Cannot proceed until we've updated the superblock... */
4343 wait_event(conf->wait_for_overlap,
4344 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004345 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004346 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004347 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004348 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004349 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004350 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004351 kthread_should_stop());
4352 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004353 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004354 spin_unlock_irq(&conf->device_lock);
4355 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004356 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004357 }
4358
NeilBrownab69ae12009-03-31 15:26:47 +11004359 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004360 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004361 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004362 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004363 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004364 set_bit(STRIPE_EXPANDING, &sh->state);
4365 atomic_inc(&conf->reshape_stripes);
4366 /* If any of this stripe is beyond the end of the old
4367 * array, then we need to zero those blocks
4368 */
4369 for (j=sh->disks; j--;) {
4370 sector_t s;
4371 if (j == sh->pd_idx)
4372 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004373 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004374 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004375 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004376 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004377 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004378 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004379 continue;
4380 }
4381 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4382 set_bit(R5_Expanded, &sh->dev[j].flags);
4383 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4384 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004385 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004386 set_bit(STRIPE_EXPAND_READY, &sh->state);
4387 set_bit(STRIPE_HANDLE, &sh->state);
4388 }
NeilBrownab69ae12009-03-31 15:26:47 +11004389 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004390 }
4391 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004392 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004393 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004394 else
NeilBrown7a661382009-03-31 15:21:40 +11004395 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004396 spin_unlock_irq(&conf->device_lock);
4397 /* Ok, those stripe are ready. We can start scheduling
4398 * reads on the source stripes.
4399 * The source stripes are determined by mapping the first and last
4400 * block on the destination stripes.
4401 */
NeilBrown52c03292006-06-26 00:27:43 -07004402 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004403 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004404 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004405 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004406 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004407 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004408 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004409 if (last_sector >= mddev->dev_sectors)
4410 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004411 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004412 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004413 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4414 set_bit(STRIPE_HANDLE, &sh->state);
4415 release_stripe(sh);
4416 first_sector += STRIPE_SECTORS;
4417 }
NeilBrownab69ae12009-03-31 15:26:47 +11004418 /* Now that the sources are clearly marked, we can release
4419 * the destination stripes
4420 */
4421 while (!list_empty(&stripes)) {
4422 sh = list_entry(stripes.next, struct stripe_head, lru);
4423 list_del_init(&sh->lru);
4424 release_stripe(sh);
4425 }
NeilBrownc6207272008-02-06 01:39:52 -08004426 /* If this takes us to the resync_max point where we have to pause,
4427 * then we need to write out the superblock.
4428 */
NeilBrown7a661382009-03-31 15:21:40 +11004429 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004430 if ((sector_nr - mddev->curr_resync_completed) * 2
4431 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004432 /* Cannot proceed until we've updated the superblock... */
4433 wait_event(conf->wait_for_overlap,
4434 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004435 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004436 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004437 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004438 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4439 md_wakeup_thread(mddev->thread);
4440 wait_event(mddev->sb_wait,
4441 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4442 || kthread_should_stop());
4443 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004444 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004445 spin_unlock_irq(&conf->device_lock);
4446 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004447 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004448 }
NeilBrown7a661382009-03-31 15:21:40 +11004449 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004450}
4451
4452/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004453static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004454{
NeilBrownd1688a62011-10-11 16:49:52 +11004455 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004456 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004457 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004458 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004459 int still_degraded = 0;
4460 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
NeilBrown72626682005-09-09 16:23:54 -07004462 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004464
NeilBrown29269552006-03-27 01:18:10 -08004465 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4466 end_reshape(conf);
4467 return 0;
4468 }
NeilBrown72626682005-09-09 16:23:54 -07004469
4470 if (mddev->curr_resync < max_sector) /* aborted */
4471 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4472 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004473 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004474 conf->fullsync = 0;
4475 bitmap_close_sync(mddev->bitmap);
4476
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 return 0;
4478 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004479
NeilBrown64bd6602009-08-03 10:59:58 +10004480 /* Allow raid5_quiesce to complete */
4481 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4482
NeilBrown52c03292006-06-26 00:27:43 -07004483 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4484 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004485
NeilBrownc6207272008-02-06 01:39:52 -08004486 /* No need to check resync_max as we never do more than one
4487 * stripe, and as resync_max will always be on a chunk boundary,
4488 * if the check in md_do_sync didn't fire, there is no chance
4489 * of overstepping resync_max here
4490 */
4491
NeilBrown16a53ec2006-06-26 00:27:38 -07004492 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 * to resync, then assert that we are finished, because there is
4494 * nothing we can do.
4495 */
NeilBrown3285edf2006-06-26 00:27:55 -07004496 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004497 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004498 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004499 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 return rv;
4501 }
NeilBrown72626682005-09-09 16:23:54 -07004502 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004503 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004504 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4505 /* we can skip this block, and probably more */
4506 sync_blocks /= STRIPE_SECTORS;
4507 *skipped = 1;
4508 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510
NeilBrownb47490c2008-02-06 01:39:50 -08004511 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4512
NeilBrowna8c906c2009-06-09 14:39:59 +10004513 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004515 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004517 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004519 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004521 /* Need to check if array will still be degraded after recovery/resync
4522 * We don't need to check the 'failed' flag as when that gets set,
4523 * recovery aborts.
4524 */
NeilBrownf001a702009-06-09 14:30:31 +10004525 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004526 if (conf->disks[i].rdev == NULL)
4527 still_degraded = 1;
4528
4529 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4530
NeilBrown83206d62011-07-26 11:19:49 +10004531 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
NeilBrown14425772009-10-16 15:55:25 +11004533 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 release_stripe(sh);
4535
4536 return STRIPE_SECTORS;
4537}
4538
NeilBrownd1688a62011-10-11 16:49:52 +11004539static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004540{
4541 /* We may not be able to submit a whole bio at once as there
4542 * may not be enough stripe_heads available.
4543 * We cannot pre-allocate enough stripe_heads as we may need
4544 * more than exist in the cache (if we allow ever large chunks).
4545 * So we do one stripe head at a time and record in
4546 * ->bi_hw_segments how many have been done.
4547 *
4548 * We *know* that this entire raid_bio is in one chunk, so
4549 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4550 */
4551 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004552 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004553 sector_t sector, logical_sector, last_sector;
4554 int scnt = 0;
4555 int remaining;
4556 int handled = 0;
4557
4558 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004559 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004560 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004561 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4562
4563 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004564 logical_sector += STRIPE_SECTORS,
4565 sector += STRIPE_SECTORS,
4566 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004567
Shaohua Lie7836bd62012-07-19 16:01:31 +10004568 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004569 /* already done this stripe */
4570 continue;
4571
NeilBrowna8c906c2009-06-09 14:39:59 +10004572 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004573
4574 if (!sh) {
4575 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004576 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004577 conf->retry_read_aligned = raid_bio;
4578 return handled;
4579 }
4580
Neil Brown387bb172007-02-08 14:20:29 -08004581 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4582 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004583 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004584 conf->retry_read_aligned = raid_bio;
4585 return handled;
4586 }
4587
majianpeng3f9e7c12012-07-31 10:04:21 +10004588 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004589 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004590 release_stripe(sh);
4591 handled++;
4592 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004593 remaining = raid5_dec_bi_active_stripes(raid_bio);
Neil Brown0e13fe232008-06-28 08:31:20 +10004594 if (remaining == 0)
4595 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004596 if (atomic_dec_and_test(&conf->active_aligned_reads))
4597 wake_up(&conf->wait_for_stripe);
4598 return handled;
4599}
4600
Shaohua Li46a06402012-08-02 08:33:15 +10004601#define MAX_STRIPE_BATCH 8
4602static int handle_active_stripes(struct r5conf *conf)
4603{
4604 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4605 int i, batch_size = 0;
4606
4607 while (batch_size < MAX_STRIPE_BATCH &&
4608 (sh = __get_priority_stripe(conf)) != NULL)
4609 batch[batch_size++] = sh;
4610
4611 if (batch_size == 0)
4612 return batch_size;
4613 spin_unlock_irq(&conf->device_lock);
4614
4615 for (i = 0; i < batch_size; i++)
4616 handle_stripe(batch[i]);
4617
4618 cond_resched();
4619
4620 spin_lock_irq(&conf->device_lock);
4621 for (i = 0; i < batch_size; i++)
4622 __release_stripe(conf, batch[i]);
4623 return batch_size;
4624}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004625
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626/*
4627 * This is our raid5 kernel thread.
4628 *
4629 * We scan the hash table for stripes which can be handled now.
4630 * During the scan, completed stripes are saved for us by the interrupt
4631 * handler, so that they will not have to wait for our next wakeup.
4632 */
NeilBrownfd01b882011-10-11 16:47:53 +11004633static void raid5d(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634{
NeilBrownd1688a62011-10-11 16:49:52 +11004635 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004637 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638
Dan Williams45b42332007-07-09 11:56:43 -07004639 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
4641 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004643 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 handled = 0;
4645 spin_lock_irq(&conf->device_lock);
4646 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004647 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004648 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
NeilBrown0021b7b2012-07-31 09:08:14 +02004650 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004651 !list_empty(&conf->bitmap_list)) {
4652 /* Now is a good time to flush some bitmap updates */
4653 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004654 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004655 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004656 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004657 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004658 activate_bit_delay(conf);
4659 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004660 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004661
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004662 while ((bio = remove_bio_from_retry(conf))) {
4663 int ok;
4664 spin_unlock_irq(&conf->device_lock);
4665 ok = retry_aligned_read(conf, bio);
4666 spin_lock_irq(&conf->device_lock);
4667 if (!ok)
4668 break;
4669 handled++;
4670 }
4671
Shaohua Li46a06402012-08-02 08:33:15 +10004672 batch_size = handle_active_stripes(conf);
4673 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004675 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
Shaohua Li46a06402012-08-02 08:33:15 +10004677 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4678 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004679 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004680 spin_lock_irq(&conf->device_lock);
4681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 }
Dan Williams45b42332007-07-09 11:56:43 -07004683 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684
4685 spin_unlock_irq(&conf->device_lock);
4686
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004687 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004688 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
Dan Williams45b42332007-07-09 11:56:43 -07004690 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691}
4692
NeilBrown3f294f42005-11-08 21:39:25 -08004693static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004694raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004695{
NeilBrownd1688a62011-10-11 16:49:52 +11004696 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004697 if (conf)
4698 return sprintf(page, "%d\n", conf->max_nr_stripes);
4699 else
4700 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004701}
4702
NeilBrownc41d4ac2010-06-01 19:37:24 +10004703int
NeilBrownfd01b882011-10-11 16:47:53 +11004704raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004705{
NeilBrownd1688a62011-10-11 16:49:52 +11004706 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004707 int err;
4708
4709 if (size <= 16 || size > 32768)
4710 return -EINVAL;
4711 while (size < conf->max_nr_stripes) {
4712 if (drop_one_stripe(conf))
4713 conf->max_nr_stripes--;
4714 else
4715 break;
4716 }
4717 err = md_allow_write(mddev);
4718 if (err)
4719 return err;
4720 while (size > conf->max_nr_stripes) {
4721 if (grow_one_stripe(conf))
4722 conf->max_nr_stripes++;
4723 else break;
4724 }
4725 return 0;
4726}
4727EXPORT_SYMBOL(raid5_set_cache_size);
4728
NeilBrown3f294f42005-11-08 21:39:25 -08004729static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004730raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004731{
NeilBrownd1688a62011-10-11 16:49:52 +11004732 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004733 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004734 int err;
4735
NeilBrown3f294f42005-11-08 21:39:25 -08004736 if (len >= PAGE_SIZE)
4737 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004738 if (!conf)
4739 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004740
Dan Williams4ef197d82008-04-28 02:15:54 -07004741 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004742 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004743 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004744 if (err)
4745 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004746 return len;
4747}
NeilBrown007583c2005-11-08 21:39:30 -08004748
NeilBrown96de1e62005-11-08 21:39:39 -08004749static struct md_sysfs_entry
4750raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4751 raid5_show_stripe_cache_size,
4752 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004753
4754static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004755raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004756{
NeilBrownd1688a62011-10-11 16:49:52 +11004757 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004758 if (conf)
4759 return sprintf(page, "%d\n", conf->bypass_threshold);
4760 else
4761 return 0;
4762}
4763
4764static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004765raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004766{
NeilBrownd1688a62011-10-11 16:49:52 +11004767 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004768 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004769 if (len >= PAGE_SIZE)
4770 return -EINVAL;
4771 if (!conf)
4772 return -ENODEV;
4773
Dan Williams4ef197d82008-04-28 02:15:54 -07004774 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004775 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004776 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004777 return -EINVAL;
4778 conf->bypass_threshold = new;
4779 return len;
4780}
4781
4782static struct md_sysfs_entry
4783raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4784 S_IRUGO | S_IWUSR,
4785 raid5_show_preread_threshold,
4786 raid5_store_preread_threshold);
4787
4788static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004789stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004790{
NeilBrownd1688a62011-10-11 16:49:52 +11004791 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004792 if (conf)
4793 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4794 else
4795 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004796}
4797
NeilBrown96de1e62005-11-08 21:39:39 -08004798static struct md_sysfs_entry
4799raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004800
NeilBrown007583c2005-11-08 21:39:30 -08004801static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004802 &raid5_stripecache_size.attr,
4803 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004804 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004805 NULL,
4806};
NeilBrown007583c2005-11-08 21:39:30 -08004807static struct attribute_group raid5_attrs_group = {
4808 .name = NULL,
4809 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004810};
4811
Dan Williams80c3a6c2009-03-17 18:10:40 -07004812static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004813raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004814{
NeilBrownd1688a62011-10-11 16:49:52 +11004815 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004816
4817 if (!sectors)
4818 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004819 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004820 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004821 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004822
Andre Noll9d8f0362009-06-18 08:45:01 +10004823 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004824 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004825 return sectors * (raid_disks - conf->max_degraded);
4826}
4827
NeilBrownd1688a62011-10-11 16:49:52 +11004828static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004829{
4830 struct raid5_percpu *percpu;
4831 unsigned long cpu;
4832
4833 if (!conf->percpu)
4834 return;
4835
4836 get_online_cpus();
4837 for_each_possible_cpu(cpu) {
4838 percpu = per_cpu_ptr(conf->percpu, cpu);
4839 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004840 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004841 }
4842#ifdef CONFIG_HOTPLUG_CPU
4843 unregister_cpu_notifier(&conf->cpu_notify);
4844#endif
4845 put_online_cpus();
4846
4847 free_percpu(conf->percpu);
4848}
4849
NeilBrownd1688a62011-10-11 16:49:52 +11004850static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004851{
4852 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004853 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004854 kfree(conf->disks);
4855 kfree(conf->stripe_hashtbl);
4856 kfree(conf);
4857}
4858
Dan Williams36d1c642009-07-14 11:48:22 -07004859#ifdef CONFIG_HOTPLUG_CPU
4860static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4861 void *hcpu)
4862{
NeilBrownd1688a62011-10-11 16:49:52 +11004863 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004864 long cpu = (long)hcpu;
4865 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4866
4867 switch (action) {
4868 case CPU_UP_PREPARE:
4869 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07004870 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07004871 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004872 if (!percpu->scribble)
4873 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4874
4875 if (!percpu->scribble ||
4876 (conf->level == 6 && !percpu->spare_page)) {
4877 safe_put_page(percpu->spare_page);
4878 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004879 pr_err("%s: failed memory allocation for cpu%ld\n",
4880 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07004881 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07004882 }
4883 break;
4884 case CPU_DEAD:
4885 case CPU_DEAD_FROZEN:
4886 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004887 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004888 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004889 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07004890 break;
4891 default:
4892 break;
4893 }
4894 return NOTIFY_OK;
4895}
4896#endif
4897
NeilBrownd1688a62011-10-11 16:49:52 +11004898static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004899{
4900 unsigned long cpu;
4901 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09004902 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07004903 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004904 int err;
4905
Dan Williams36d1c642009-07-14 11:48:22 -07004906 allcpus = alloc_percpu(struct raid5_percpu);
4907 if (!allcpus)
4908 return -ENOMEM;
4909 conf->percpu = allcpus;
4910
4911 get_online_cpus();
4912 err = 0;
4913 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07004914 if (conf->level == 6) {
4915 spare_page = alloc_page(GFP_KERNEL);
4916 if (!spare_page) {
4917 err = -ENOMEM;
4918 break;
4919 }
4920 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4921 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11004922 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004923 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07004924 err = -ENOMEM;
4925 break;
4926 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07004927 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07004928 }
4929#ifdef CONFIG_HOTPLUG_CPU
4930 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4931 conf->cpu_notify.priority = 0;
4932 if (err == 0)
4933 err = register_cpu_notifier(&conf->cpu_notify);
4934#endif
4935 put_online_cpus();
4936
4937 return err;
4938}
4939
NeilBrownd1688a62011-10-11 16:49:52 +11004940static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941{
NeilBrownd1688a62011-10-11 16:49:52 +11004942 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004943 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11004944 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10004946 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947
NeilBrown91adb562009-03-31 14:39:39 +11004948 if (mddev->new_level != 5
4949 && mddev->new_level != 4
4950 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10004951 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004952 mdname(mddev), mddev->new_level);
4953 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 }
NeilBrown91adb562009-03-31 14:39:39 +11004955 if ((mddev->new_level == 5
4956 && !algorithm_valid_raid5(mddev->new_layout)) ||
4957 (mddev->new_level == 6
4958 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10004959 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11004960 mdname(mddev), mddev->new_layout);
4961 return ERR_PTR(-EIO);
4962 }
4963 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10004964 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11004965 mdname(mddev), mddev->raid_disks);
4966 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11004967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
Andre Noll664e7c42009-06-18 08:45:27 +10004969 if (!mddev->new_chunk_sectors ||
4970 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4971 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10004972 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4973 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11004974 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11004975 }
4976
NeilBrownd1688a62011-10-11 16:49:52 +11004977 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11004978 if (conf == NULL)
4979 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11004980 spin_lock_init(&conf->device_lock);
4981 init_waitqueue_head(&conf->wait_for_stripe);
4982 init_waitqueue_head(&conf->wait_for_overlap);
4983 INIT_LIST_HEAD(&conf->handle_list);
4984 INIT_LIST_HEAD(&conf->hold_list);
4985 INIT_LIST_HEAD(&conf->delayed_list);
4986 INIT_LIST_HEAD(&conf->bitmap_list);
4987 INIT_LIST_HEAD(&conf->inactive_list);
4988 atomic_set(&conf->active_stripes, 0);
4989 atomic_set(&conf->preread_active_stripes, 0);
4990 atomic_set(&conf->active_aligned_reads, 0);
4991 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11004992 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11004993
4994 conf->raid_disks = mddev->raid_disks;
4995 if (mddev->reshape_position == MaxSector)
4996 conf->previous_raid_disks = mddev->raid_disks;
4997 else
4998 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004999 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5000 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005001
NeilBrown5e5e3e72009-10-16 16:35:30 +11005002 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005003 GFP_KERNEL);
5004 if (!conf->disks)
5005 goto abort;
5006
5007 conf->mddev = mddev;
5008
5009 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5010 goto abort;
5011
Dan Williams36d1c642009-07-14 11:48:22 -07005012 conf->level = mddev->new_level;
5013 if (raid5_alloc_percpu(conf) != 0)
5014 goto abort;
5015
NeilBrown0c55e022010-05-03 14:09:02 +10005016 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005017
NeilBrowndafb20f2012-03-19 12:46:39 +11005018 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005019 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005020 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005021 || raid_disk < 0)
5022 continue;
5023 disk = conf->disks + raid_disk;
5024
NeilBrown17045f52011-12-23 10:17:53 +11005025 if (test_bit(Replacement, &rdev->flags)) {
5026 if (disk->replacement)
5027 goto abort;
5028 disk->replacement = rdev;
5029 } else {
5030 if (disk->rdev)
5031 goto abort;
5032 disk->rdev = rdev;
5033 }
NeilBrown91adb562009-03-31 14:39:39 +11005034
5035 if (test_bit(In_sync, &rdev->flags)) {
5036 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005037 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5038 " disk %d\n",
5039 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005040 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005041 /* Cannot rely on bitmap to complete recovery */
5042 conf->fullsync = 1;
5043 }
5044
Andre Noll09c9e5f2009-06-18 08:45:55 +10005045 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005046 conf->level = mddev->new_level;
5047 if (conf->level == 6)
5048 conf->max_degraded = 2;
5049 else
5050 conf->max_degraded = 1;
5051 conf->algorithm = mddev->new_layout;
5052 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005053 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005054 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005055 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005056 conf->prev_algo = mddev->layout;
5057 }
NeilBrown91adb562009-03-31 14:39:39 +11005058
5059 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005060 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005061 if (grow_stripes(conf, conf->max_nr_stripes)) {
5062 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005063 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5064 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005065 goto abort;
5066 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005067 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5068 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005069
NeilBrown02326052012-07-03 15:56:52 +10005070 sprintf(pers_name, "raid%d", mddev->new_level);
5071 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005072 if (!conf->thread) {
5073 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005074 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005075 mdname(mddev));
5076 goto abort;
5077 }
5078
5079 return conf;
5080
5081 abort:
5082 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005083 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005084 return ERR_PTR(-EIO);
5085 } else
5086 return ERR_PTR(-ENOMEM);
5087}
5088
NeilBrownc148ffd2009-11-13 17:47:00 +11005089
5090static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5091{
5092 switch (algo) {
5093 case ALGORITHM_PARITY_0:
5094 if (raid_disk < max_degraded)
5095 return 1;
5096 break;
5097 case ALGORITHM_PARITY_N:
5098 if (raid_disk >= raid_disks - max_degraded)
5099 return 1;
5100 break;
5101 case ALGORITHM_PARITY_0_6:
5102 if (raid_disk == 0 ||
5103 raid_disk == raid_disks - 1)
5104 return 1;
5105 break;
5106 case ALGORITHM_LEFT_ASYMMETRIC_6:
5107 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5108 case ALGORITHM_LEFT_SYMMETRIC_6:
5109 case ALGORITHM_RIGHT_SYMMETRIC_6:
5110 if (raid_disk == raid_disks - 1)
5111 return 1;
5112 }
5113 return 0;
5114}
5115
NeilBrownfd01b882011-10-11 16:47:53 +11005116static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005117{
NeilBrownd1688a62011-10-11 16:49:52 +11005118 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005119 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005120 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005121 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005122 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005123 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005124 long long min_offset_diff = 0;
5125 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005126
Andre Noll8c6ac862009-06-18 08:48:06 +10005127 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005128 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005129 " -- starting background reconstruction\n",
5130 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005131
5132 rdev_for_each(rdev, mddev) {
5133 long long diff;
5134 if (rdev->raid_disk < 0)
5135 continue;
5136 diff = (rdev->new_data_offset - rdev->data_offset);
5137 if (first) {
5138 min_offset_diff = diff;
5139 first = 0;
5140 } else if (mddev->reshape_backwards &&
5141 diff < min_offset_diff)
5142 min_offset_diff = diff;
5143 else if (!mddev->reshape_backwards &&
5144 diff > min_offset_diff)
5145 min_offset_diff = diff;
5146 }
5147
NeilBrownf6705572006-03-27 01:18:11 -08005148 if (mddev->reshape_position != MaxSector) {
5149 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005150 * Difficulties arise if the stripe we would write to
5151 * next is at or after the stripe we would read from next.
5152 * For a reshape that changes the number of devices, this
5153 * is only possible for a very short time, and mdadm makes
5154 * sure that time appears to have past before assembling
5155 * the array. So we fail if that time hasn't passed.
5156 * For a reshape that keeps the number of devices the same
5157 * mdadm must be monitoring the reshape can keeping the
5158 * critical areas read-only and backed up. It will start
5159 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005160 */
5161 sector_t here_new, here_old;
5162 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005163 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005164
NeilBrown88ce4932009-03-31 15:24:23 +11005165 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005166 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005167 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005168 mdname(mddev));
5169 return -EINVAL;
5170 }
NeilBrownf6705572006-03-27 01:18:11 -08005171 old_disks = mddev->raid_disks - mddev->delta_disks;
5172 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005173 * further up in new geometry must map after here in old
5174 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005175 */
5176 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005177 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005178 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005179 printk(KERN_ERR "md/raid:%s: reshape_position not "
5180 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005181 return -EINVAL;
5182 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005183 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005184 /* here_new is the stripe we will write to */
5185 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005186 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005187 (old_disks-max_degraded));
5188 /* here_old is the first stripe that we might need to read
5189 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005190 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005191 if ((here_new * mddev->new_chunk_sectors !=
5192 here_old * mddev->chunk_sectors)) {
5193 printk(KERN_ERR "md/raid:%s: reshape position is"
5194 " confused - aborting\n", mdname(mddev));
5195 return -EINVAL;
5196 }
NeilBrown67ac6012009-08-13 10:06:24 +10005197 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005198 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005199 * and taking constant backups.
5200 * mdadm always starts a situation like this in
5201 * readonly mode so it can take control before
5202 * allowing any writes. So just check for that.
5203 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005204 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5205 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5206 /* not really in-place - so OK */;
5207 else if (mddev->ro == 0) {
5208 printk(KERN_ERR "md/raid:%s: in-place reshape "
5209 "must be started in read-only mode "
5210 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005211 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005212 return -EINVAL;
5213 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005214 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005215 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005216 here_old * mddev->chunk_sectors)
5217 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005218 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005219 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005220 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5221 "auto-recovery - aborting.\n",
5222 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005223 return -EINVAL;
5224 }
NeilBrown0c55e022010-05-03 14:09:02 +10005225 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5226 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005227 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005228 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005229 BUG_ON(mddev->level != mddev->new_level);
5230 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005231 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005232 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005233 }
5234
NeilBrown245f46c2009-03-31 14:39:39 +11005235 if (mddev->private == NULL)
5236 conf = setup_conf(mddev);
5237 else
5238 conf = mddev->private;
5239
NeilBrown91adb562009-03-31 14:39:39 +11005240 if (IS_ERR(conf))
5241 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005242
NeilBrownb5254dd2012-05-21 09:27:01 +10005243 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005244 mddev->thread = conf->thread;
5245 conf->thread = NULL;
5246 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
NeilBrown17045f52011-12-23 10:17:53 +11005248 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5249 i++) {
5250 rdev = conf->disks[i].rdev;
5251 if (!rdev && conf->disks[i].replacement) {
5252 /* The replacement is all we have yet */
5253 rdev = conf->disks[i].replacement;
5254 conf->disks[i].replacement = NULL;
5255 clear_bit(Replacement, &rdev->flags);
5256 conf->disks[i].rdev = rdev;
5257 }
5258 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005259 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005260 if (conf->disks[i].replacement &&
5261 conf->reshape_progress != MaxSector) {
5262 /* replacements and reshape simply do not mix. */
5263 printk(KERN_ERR "md: cannot handle concurrent "
5264 "replacement and reshape.\n");
5265 goto abort;
5266 }
NeilBrown2f115882010-06-17 17:41:03 +10005267 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005268 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005269 continue;
5270 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005271 /* This disc is not fully in-sync. However if it
5272 * just stored parity (beyond the recovery_offset),
5273 * when we don't need to be concerned about the
5274 * array being dirty.
5275 * When reshape goes 'backwards', we never have
5276 * partially completed devices, so we only need
5277 * to worry about reshape going forwards.
5278 */
5279 /* Hack because v0.91 doesn't store recovery_offset properly. */
5280 if (mddev->major_version == 0 &&
5281 mddev->minor_version > 90)
5282 rdev->recovery_offset = reshape_offset;
5283
NeilBrownc148ffd2009-11-13 17:47:00 +11005284 if (rdev->recovery_offset < reshape_offset) {
5285 /* We need to check old and new layout */
5286 if (!only_parity(rdev->raid_disk,
5287 conf->algorithm,
5288 conf->raid_disks,
5289 conf->max_degraded))
5290 continue;
5291 }
5292 if (!only_parity(rdev->raid_disk,
5293 conf->prev_algo,
5294 conf->previous_raid_disks,
5295 conf->max_degraded))
5296 continue;
5297 dirty_parity_disks++;
5298 }
NeilBrown91adb562009-03-31 14:39:39 +11005299
NeilBrown17045f52011-12-23 10:17:53 +11005300 /*
5301 * 0 for a fully functional array, 1 or 2 for a degraded array.
5302 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005303 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304
NeilBrown674806d2010-06-16 17:17:53 +10005305 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005306 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005308 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 goto abort;
5310 }
5311
NeilBrown91adb562009-03-31 14:39:39 +11005312 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005313 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005314 mddev->resync_max_sectors = mddev->dev_sectors;
5315
NeilBrownc148ffd2009-11-13 17:47:00 +11005316 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005318 if (mddev->ok_start_degraded)
5319 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005320 "md/raid:%s: starting dirty degraded array"
5321 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005322 mdname(mddev));
5323 else {
5324 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005325 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005326 mdname(mddev));
5327 goto abort;
5328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 }
5330
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005332 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5333 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005334 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5335 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 else
NeilBrown0c55e022010-05-03 14:09:02 +10005337 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5338 " out of %d devices, algorithm %d\n",
5339 mdname(mddev), conf->level,
5340 mddev->raid_disks - mddev->degraded,
5341 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342
5343 print_raid5_conf(conf);
5344
NeilBrownfef9c612009-03-31 15:16:46 +11005345 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005346 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005347 atomic_set(&conf->reshape_stripes, 0);
5348 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5349 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5350 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5351 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5352 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005353 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005354 }
5355
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356
5357 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005358 if (mddev->to_remove == &raid5_attrs_group)
5359 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005360 else if (mddev->kobj.sd &&
5361 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005362 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005363 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005364 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005365 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5366
5367 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005368 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10005369 /* read-ahead size must cover two whole stripes, which
5370 * is 2 * (datadisks) * chunksize where 'n' is the
5371 * number of raid devices
5372 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5374 int stripe = data_disks *
5375 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5376 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5377 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005378
5379 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005380
5381 mddev->queue->backing_dev_info.congested_data = mddev;
5382 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005383
5384 chunk_size = mddev->chunk_sectors << 9;
5385 blk_queue_io_min(mddev->queue, chunk_size);
5386 blk_queue_io_opt(mddev->queue, chunk_size *
5387 (conf->raid_disks - conf->max_degraded));
5388
NeilBrown05616be2012-05-21 09:27:00 +10005389 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005390 disk_stack_limits(mddev->gendisk, rdev->bdev,
5391 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005392 disk_stack_limits(mddev->gendisk, rdev->bdev,
5393 rdev->new_data_offset << 9);
5394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 }
5396
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 return 0;
5398abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005399 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005400 print_raid5_conf(conf);
5401 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005403 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 return -EIO;
5405}
5406
NeilBrownfd01b882011-10-11 16:47:53 +11005407static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408{
NeilBrownd1688a62011-10-11 16:49:52 +11005409 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
NeilBrown01f96c02011-09-21 15:30:20 +10005411 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005412 if (mddev->queue)
5413 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005414 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005415 mddev->private = NULL;
5416 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 return 0;
5418}
5419
NeilBrownfd01b882011-10-11 16:47:53 +11005420static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421{
NeilBrownd1688a62011-10-11 16:49:52 +11005422 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 int i;
5424
Andre Noll9d8f0362009-06-18 08:45:01 +10005425 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5426 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005427 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 for (i = 0; i < conf->raid_disks; i++)
5429 seq_printf (seq, "%s",
5430 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005431 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433}
5434
NeilBrownd1688a62011-10-11 16:49:52 +11005435static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436{
5437 int i;
5438 struct disk_info *tmp;
5439
NeilBrown0c55e022010-05-03 14:09:02 +10005440 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 if (!conf) {
5442 printk("(conf==NULL)\n");
5443 return;
5444 }
NeilBrown0c55e022010-05-03 14:09:02 +10005445 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5446 conf->raid_disks,
5447 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448
5449 for (i = 0; i < conf->raid_disks; i++) {
5450 char b[BDEVNAME_SIZE];
5451 tmp = conf->disks + i;
5452 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005453 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5454 i, !test_bit(Faulty, &tmp->rdev->flags),
5455 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 }
5457}
5458
NeilBrownfd01b882011-10-11 16:47:53 +11005459static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460{
5461 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005462 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005464 int count = 0;
5465 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
5467 for (i = 0; i < conf->raid_disks; i++) {
5468 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005469 if (tmp->replacement
5470 && tmp->replacement->recovery_offset == MaxSector
5471 && !test_bit(Faulty, &tmp->replacement->flags)
5472 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5473 /* Replacement has just become active. */
5474 if (!tmp->rdev
5475 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5476 count++;
5477 if (tmp->rdev) {
5478 /* Replaced device not technically faulty,
5479 * but we need to be sure it gets removed
5480 * and never re-added.
5481 */
5482 set_bit(Faulty, &tmp->rdev->flags);
5483 sysfs_notify_dirent_safe(
5484 tmp->rdev->sysfs_state);
5485 }
5486 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5487 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005488 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005489 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005490 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005491 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005492 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 }
5494 }
NeilBrown6b965622010-08-18 11:56:59 +10005495 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005496 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005497 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005499 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500}
5501
NeilBrownb8321b62011-12-23 10:17:51 +11005502static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503{
NeilBrownd1688a62011-10-11 16:49:52 +11005504 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005506 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005507 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 struct disk_info *p = conf->disks + number;
5509
5510 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005511 if (rdev == p->rdev)
5512 rdevp = &p->rdev;
5513 else if (rdev == p->replacement)
5514 rdevp = &p->replacement;
5515 else
5516 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005517
NeilBrown657e3e42011-12-23 10:17:52 +11005518 if (number >= conf->raid_disks &&
5519 conf->reshape_progress == MaxSector)
5520 clear_bit(In_sync, &rdev->flags);
5521
5522 if (test_bit(In_sync, &rdev->flags) ||
5523 atomic_read(&rdev->nr_pending)) {
5524 err = -EBUSY;
5525 goto abort;
5526 }
5527 /* Only remove non-faulty devices if recovery
5528 * isn't possible.
5529 */
5530 if (!test_bit(Faulty, &rdev->flags) &&
5531 mddev->recovery_disabled != conf->recovery_disabled &&
5532 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005533 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005534 number < conf->raid_disks) {
5535 err = -EBUSY;
5536 goto abort;
5537 }
5538 *rdevp = NULL;
5539 synchronize_rcu();
5540 if (atomic_read(&rdev->nr_pending)) {
5541 /* lost the race, try later */
5542 err = -EBUSY;
5543 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005544 } else if (p->replacement) {
5545 /* We must have just cleared 'rdev' */
5546 p->rdev = p->replacement;
5547 clear_bit(Replacement, &p->replacement->flags);
5548 smp_mb(); /* Make sure other CPUs may see both as identical
5549 * but will never see neither - if they are careful
5550 */
5551 p->replacement = NULL;
5552 clear_bit(WantReplacement, &rdev->flags);
5553 } else
5554 /* We might have just removed the Replacement as faulty-
5555 * clear the bit just in case
5556 */
5557 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558abort:
5559
5560 print_raid5_conf(conf);
5561 return err;
5562}
5563
NeilBrownfd01b882011-10-11 16:47:53 +11005564static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565{
NeilBrownd1688a62011-10-11 16:49:52 +11005566 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005567 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 int disk;
5569 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005570 int first = 0;
5571 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572
NeilBrown7f0da592011-07-28 11:39:22 +10005573 if (mddev->recovery_disabled == conf->recovery_disabled)
5574 return -EBUSY;
5575
NeilBrowndc10c642012-03-19 12:46:37 +11005576 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005578 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579
Neil Brown6c2fce22008-06-28 08:31:31 +10005580 if (rdev->raid_disk >= 0)
5581 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
5583 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005584 * find the disk ... but prefer rdev->saved_raid_disk
5585 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005587 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005588 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005589 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005590 first = rdev->saved_raid_disk;
5591
5592 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005593 p = conf->disks + disk;
5594 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005595 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005597 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005598 if (rdev->saved_raid_disk != disk)
5599 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005600 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005601 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005603 }
5604 for (disk = first; disk <= last; disk++) {
5605 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005606 if (test_bit(WantReplacement, &p->rdev->flags) &&
5607 p->replacement == NULL) {
5608 clear_bit(In_sync, &rdev->flags);
5609 set_bit(Replacement, &rdev->flags);
5610 rdev->raid_disk = disk;
5611 err = 0;
5612 conf->fullsync = 1;
5613 rcu_assign_pointer(p->replacement, rdev);
5614 break;
5615 }
5616 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005617out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005619 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620}
5621
NeilBrownfd01b882011-10-11 16:47:53 +11005622static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623{
5624 /* no resync is happening, and there is enough space
5625 * on all devices, so we can resize.
5626 * We need to make sure resync covers any new space.
5627 * If the array is shrinking we should possibly wait until
5628 * any io in the removed space completes, but it hardly seems
5629 * worth it.
5630 */
NeilBrowna4a61252012-05-22 13:55:27 +10005631 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005632 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005633 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5634 if (mddev->external_size &&
5635 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005636 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005637 if (mddev->bitmap) {
5638 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5639 if (ret)
5640 return ret;
5641 }
5642 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005643 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005644 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005645 if (sectors > mddev->dev_sectors &&
5646 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005647 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5649 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005650 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005651 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 return 0;
5653}
5654
NeilBrownfd01b882011-10-11 16:47:53 +11005655static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005656{
5657 /* Can only proceed if there are plenty of stripe_heads.
5658 * We need a minimum of one full stripe,, and for sensible progress
5659 * it is best to have about 4 times that.
5660 * If we require 4 times, then the default 256 4K stripe_heads will
5661 * allow for chunk sizes up to 256K, which is probably OK.
5662 * If the chunk size is greater, user-space should request more
5663 * stripe_heads first.
5664 */
NeilBrownd1688a62011-10-11 16:49:52 +11005665 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005666 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5667 > conf->max_nr_stripes ||
5668 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5669 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005670 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5671 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005672 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5673 / STRIPE_SIZE)*4);
5674 return 0;
5675 }
5676 return 1;
5677}
5678
NeilBrownfd01b882011-10-11 16:47:53 +11005679static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005680{
NeilBrownd1688a62011-10-11 16:49:52 +11005681 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005682
NeilBrown88ce4932009-03-31 15:24:23 +11005683 if (mddev->delta_disks == 0 &&
5684 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005685 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005686 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005687 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005688 return -EINVAL;
5689 if (mddev->delta_disks < 0) {
5690 /* We might be able to shrink, but the devices must
5691 * be made bigger first.
5692 * For raid6, 4 is the minimum size.
5693 * Otherwise 2 is the minimum
5694 */
5695 int min = 2;
5696 if (mddev->level == 6)
5697 min = 4;
5698 if (mddev->raid_disks + mddev->delta_disks < min)
5699 return -EINVAL;
5700 }
NeilBrown29269552006-03-27 01:18:10 -08005701
NeilBrown01ee22b2009-06-18 08:47:20 +10005702 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005703 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005704
NeilBrownec32a2b2009-03-31 15:17:38 +11005705 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005706}
5707
NeilBrownfd01b882011-10-11 16:47:53 +11005708static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005709{
NeilBrownd1688a62011-10-11 16:49:52 +11005710 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005711 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005712 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005713 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005714
NeilBrownf4168852007-02-28 20:11:53 -08005715 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005716 return -EBUSY;
5717
NeilBrown01ee22b2009-06-18 08:47:20 +10005718 if (!check_stripe_cache(mddev))
5719 return -ENOSPC;
5720
NeilBrown30b67642012-05-22 13:55:28 +10005721 if (has_failed(conf))
5722 return -EINVAL;
5723
NeilBrownc6563a82012-05-21 09:27:00 +10005724 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005725 if (!test_bit(In_sync, &rdev->flags)
5726 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005727 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005728 }
NeilBrown63c70c42006-03-27 01:18:13 -08005729
NeilBrownf4168852007-02-28 20:11:53 -08005730 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005731 /* Not enough devices even to make a degraded array
5732 * of that size
5733 */
5734 return -EINVAL;
5735
NeilBrownec32a2b2009-03-31 15:17:38 +11005736 /* Refuse to reduce size of the array. Any reductions in
5737 * array size must be through explicit setting of array_size
5738 * attribute.
5739 */
5740 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5741 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005742 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005743 "before number of disks\n", mdname(mddev));
5744 return -EINVAL;
5745 }
5746
NeilBrownf6705572006-03-27 01:18:11 -08005747 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005748 spin_lock_irq(&conf->device_lock);
5749 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005750 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005751 conf->prev_chunk_sectors = conf->chunk_sectors;
5752 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005753 conf->prev_algo = conf->algorithm;
5754 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005755 conf->generation++;
5756 /* Code that selects data_offset needs to see the generation update
5757 * if reshape_progress has been set - so a memory barrier needed.
5758 */
5759 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005760 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005761 conf->reshape_progress = raid5_size(mddev, 0, 0);
5762 else
5763 conf->reshape_progress = 0;
5764 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005765 spin_unlock_irq(&conf->device_lock);
5766
5767 /* Add some new drives, as many as will fit.
5768 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005769 * Don't add devices if we are reducing the number of
5770 * devices in the array. This is because it is not possible
5771 * to correctly record the "partially reconstructed" state of
5772 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005773 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005774 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005775 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005776 if (rdev->raid_disk < 0 &&
5777 !test_bit(Faulty, &rdev->flags)) {
5778 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005779 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005780 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005781 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005782 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005783 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005784
5785 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005786 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005787 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005788 } else if (rdev->raid_disk >= conf->previous_raid_disks
5789 && !test_bit(Faulty, &rdev->flags)) {
5790 /* This is a spare that was manually added */
5791 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005792 }
NeilBrown29269552006-03-27 01:18:10 -08005793
NeilBrown87a8dec2011-01-31 11:57:43 +11005794 /* When a reshape changes the number of devices,
5795 * ->degraded is measured against the larger of the
5796 * pre and post number of devices.
5797 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005798 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005799 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005800 spin_unlock_irqrestore(&conf->device_lock, flags);
5801 }
NeilBrown63c70c42006-03-27 01:18:13 -08005802 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005803 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005804 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005805
NeilBrown29269552006-03-27 01:18:10 -08005806 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5807 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5808 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5809 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5810 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005811 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005812 if (!mddev->sync_thread) {
5813 mddev->recovery = 0;
5814 spin_lock_irq(&conf->device_lock);
5815 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005816 rdev_for_each(rdev, mddev)
5817 rdev->new_data_offset = rdev->data_offset;
5818 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005819 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005820 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005821 spin_unlock_irq(&conf->device_lock);
5822 return -EAGAIN;
5823 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005824 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005825 md_wakeup_thread(mddev->sync_thread);
5826 md_new_event(mddev);
5827 return 0;
5828}
NeilBrown29269552006-03-27 01:18:10 -08005829
NeilBrownec32a2b2009-03-31 15:17:38 +11005830/* This is called from the reshape thread and should make any
5831 * changes needed in 'conf'
5832 */
NeilBrownd1688a62011-10-11 16:49:52 +11005833static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08005834{
NeilBrown29269552006-03-27 01:18:10 -08005835
NeilBrownf6705572006-03-27 01:18:11 -08005836 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10005837 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07005838
NeilBrownf6705572006-03-27 01:18:11 -08005839 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11005840 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005841 rdev_for_each(rdev, conf->mddev)
5842 rdev->data_offset = rdev->new_data_offset;
5843 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005844 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08005845 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005846 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07005847
5848 /* read-ahead size must cover two whole stripes, which is
5849 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5850 */
NeilBrown4a5add42010-06-01 19:37:28 +10005851 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11005852 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005853 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11005854 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07005855 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5856 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5857 }
NeilBrown29269552006-03-27 01:18:10 -08005858 }
NeilBrown29269552006-03-27 01:18:10 -08005859}
5860
NeilBrownec32a2b2009-03-31 15:17:38 +11005861/* This is called from the raid5d thread with mddev_lock held.
5862 * It makes config changes to the device.
5863 */
NeilBrownfd01b882011-10-11 16:47:53 +11005864static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11005865{
NeilBrownd1688a62011-10-11 16:49:52 +11005866 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11005867
5868 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5869
NeilBrownec32a2b2009-03-31 15:17:38 +11005870 if (mddev->delta_disks > 0) {
5871 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5872 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005873 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11005874 } else {
5875 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11005876 spin_lock_irq(&conf->device_lock);
5877 mddev->degraded = calc_degraded(conf);
5878 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11005879 for (d = conf->raid_disks ;
5880 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10005881 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11005882 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10005883 if (rdev)
5884 clear_bit(In_sync, &rdev->flags);
5885 rdev = conf->disks[d].replacement;
5886 if (rdev)
5887 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10005888 }
NeilBrowncea9c222009-03-31 15:15:05 +11005889 }
NeilBrown88ce4932009-03-31 15:24:23 +11005890 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005891 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11005892 mddev->reshape_position = MaxSector;
5893 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10005894 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11005895 }
5896}
5897
NeilBrownfd01b882011-10-11 16:47:53 +11005898static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07005899{
NeilBrownd1688a62011-10-11 16:49:52 +11005900 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07005901
5902 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08005903 case 2: /* resume for a suspend */
5904 wake_up(&conf->wait_for_overlap);
5905 break;
5906
NeilBrown72626682005-09-09 16:23:54 -07005907 case 1: /* stop all writes */
5908 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005909 /* '2' tells resync/reshape to pause so that all
5910 * active stripes can drain
5911 */
5912 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07005913 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005914 atomic_read(&conf->active_stripes) == 0 &&
5915 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07005916 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10005917 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07005918 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10005919 /* allow reshape to continue */
5920 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005921 break;
5922
5923 case 0: /* re-enable writes */
5924 spin_lock_irq(&conf->device_lock);
5925 conf->quiesce = 0;
5926 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08005927 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07005928 spin_unlock_irq(&conf->device_lock);
5929 break;
5930 }
NeilBrown72626682005-09-09 16:23:54 -07005931}
NeilBrownb15c2e52006-01-06 00:20:16 -08005932
NeilBrownd562b0c2009-03-31 14:39:39 +11005933
NeilBrownfd01b882011-10-11 16:47:53 +11005934static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11005935{
NeilBrowne373ab12011-10-11 16:48:59 +11005936 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07005937 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11005938
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005939 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11005940 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10005941 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5942 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005943 return ERR_PTR(-EINVAL);
5944 }
5945
NeilBrowne373ab12011-10-11 16:48:59 +11005946 sectors = raid0_conf->strip_zone[0].zone_end;
5947 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10005948 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07005949 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11005950 mddev->new_layout = ALGORITHM_PARITY_N;
5951 mddev->new_chunk_sectors = mddev->chunk_sectors;
5952 mddev->raid_disks += 1;
5953 mddev->delta_disks = 1;
5954 /* make sure it will be not marked as dirty */
5955 mddev->recovery_cp = MaxSector;
5956
5957 return setup_conf(mddev);
5958}
5959
5960
NeilBrownfd01b882011-10-11 16:47:53 +11005961static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11005962{
5963 int chunksect;
5964
5965 if (mddev->raid_disks != 2 ||
5966 mddev->degraded > 1)
5967 return ERR_PTR(-EINVAL);
5968
5969 /* Should check if there are write-behind devices? */
5970
5971 chunksect = 64*2; /* 64K by default */
5972
5973 /* The array must be an exact multiple of chunksize */
5974 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5975 chunksect >>= 1;
5976
5977 if ((chunksect<<9) < STRIPE_SIZE)
5978 /* array size does not allow a suitable chunk size */
5979 return ERR_PTR(-EINVAL);
5980
5981 mddev->new_level = 5;
5982 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10005983 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11005984
5985 return setup_conf(mddev);
5986}
5987
NeilBrownfd01b882011-10-11 16:47:53 +11005988static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11005989{
5990 int new_layout;
5991
5992 switch (mddev->layout) {
5993 case ALGORITHM_LEFT_ASYMMETRIC_6:
5994 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5995 break;
5996 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5997 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5998 break;
5999 case ALGORITHM_LEFT_SYMMETRIC_6:
6000 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6001 break;
6002 case ALGORITHM_RIGHT_SYMMETRIC_6:
6003 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6004 break;
6005 case ALGORITHM_PARITY_0_6:
6006 new_layout = ALGORITHM_PARITY_0;
6007 break;
6008 case ALGORITHM_PARITY_N:
6009 new_layout = ALGORITHM_PARITY_N;
6010 break;
6011 default:
6012 return ERR_PTR(-EINVAL);
6013 }
6014 mddev->new_level = 5;
6015 mddev->new_layout = new_layout;
6016 mddev->delta_disks = -1;
6017 mddev->raid_disks -= 1;
6018 return setup_conf(mddev);
6019}
6020
NeilBrownd562b0c2009-03-31 14:39:39 +11006021
NeilBrownfd01b882011-10-11 16:47:53 +11006022static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006023{
NeilBrown88ce4932009-03-31 15:24:23 +11006024 /* For a 2-drive array, the layout and chunk size can be changed
6025 * immediately as not restriping is needed.
6026 * For larger arrays we record the new value - after validation
6027 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006028 */
NeilBrownd1688a62011-10-11 16:49:52 +11006029 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006030 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006031
NeilBrown597a7112009-06-18 08:47:42 +10006032 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006033 return -EINVAL;
6034 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006035 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006036 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006037 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006038 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006039 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006040 /* not factor of array size */
6041 return -EINVAL;
6042 }
6043
6044 /* They look valid */
6045
NeilBrown88ce4932009-03-31 15:24:23 +11006046 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006047 /* can make the change immediately */
6048 if (mddev->new_layout >= 0) {
6049 conf->algorithm = mddev->new_layout;
6050 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006051 }
6052 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006053 conf->chunk_sectors = new_chunk ;
6054 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006055 }
6056 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6057 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006058 }
NeilBrown50ac1682009-06-18 08:47:55 +10006059 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006060}
6061
NeilBrownfd01b882011-10-11 16:47:53 +11006062static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006063{
NeilBrown597a7112009-06-18 08:47:42 +10006064 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006065
NeilBrown597a7112009-06-18 08:47:42 +10006066 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006067 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006068 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006069 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006070 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006071 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006072 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006073 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006074 /* not factor of array size */
6075 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006076 }
NeilBrown88ce4932009-03-31 15:24:23 +11006077
6078 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006079 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006080}
6081
NeilBrownfd01b882011-10-11 16:47:53 +11006082static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006083{
6084 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006085 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006086 * raid1 - if there are two drives. We need to know the chunk size
6087 * raid4 - trivial - just use a raid4 layout.
6088 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006089 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006090 if (mddev->level == 0)
6091 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006092 if (mddev->level == 1)
6093 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006094 if (mddev->level == 4) {
6095 mddev->new_layout = ALGORITHM_PARITY_N;
6096 mddev->new_level = 5;
6097 return setup_conf(mddev);
6098 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006099 if (mddev->level == 6)
6100 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006101
6102 return ERR_PTR(-EINVAL);
6103}
6104
NeilBrownfd01b882011-10-11 16:47:53 +11006105static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006106{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006107 /* raid4 can take over:
6108 * raid0 - if there is only one strip zone
6109 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006110 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006111 if (mddev->level == 0)
6112 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006113 if (mddev->level == 5 &&
6114 mddev->layout == ALGORITHM_PARITY_N) {
6115 mddev->new_layout = 0;
6116 mddev->new_level = 4;
6117 return setup_conf(mddev);
6118 }
6119 return ERR_PTR(-EINVAL);
6120}
NeilBrownd562b0c2009-03-31 14:39:39 +11006121
NeilBrown84fc4b52011-10-11 16:49:58 +11006122static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006123
NeilBrownfd01b882011-10-11 16:47:53 +11006124static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006125{
6126 /* Currently can only take over a raid5. We map the
6127 * personality to an equivalent raid6 personality
6128 * with the Q block at the end.
6129 */
6130 int new_layout;
6131
6132 if (mddev->pers != &raid5_personality)
6133 return ERR_PTR(-EINVAL);
6134 if (mddev->degraded > 1)
6135 return ERR_PTR(-EINVAL);
6136 if (mddev->raid_disks > 253)
6137 return ERR_PTR(-EINVAL);
6138 if (mddev->raid_disks < 3)
6139 return ERR_PTR(-EINVAL);
6140
6141 switch (mddev->layout) {
6142 case ALGORITHM_LEFT_ASYMMETRIC:
6143 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6144 break;
6145 case ALGORITHM_RIGHT_ASYMMETRIC:
6146 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6147 break;
6148 case ALGORITHM_LEFT_SYMMETRIC:
6149 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6150 break;
6151 case ALGORITHM_RIGHT_SYMMETRIC:
6152 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6153 break;
6154 case ALGORITHM_PARITY_0:
6155 new_layout = ALGORITHM_PARITY_0_6;
6156 break;
6157 case ALGORITHM_PARITY_N:
6158 new_layout = ALGORITHM_PARITY_N;
6159 break;
6160 default:
6161 return ERR_PTR(-EINVAL);
6162 }
6163 mddev->new_level = 6;
6164 mddev->new_layout = new_layout;
6165 mddev->delta_disks = 1;
6166 mddev->raid_disks += 1;
6167 return setup_conf(mddev);
6168}
6169
6170
NeilBrown84fc4b52011-10-11 16:49:58 +11006171static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006172{
6173 .name = "raid6",
6174 .level = 6,
6175 .owner = THIS_MODULE,
6176 .make_request = make_request,
6177 .run = run,
6178 .stop = stop,
6179 .status = status,
6180 .error_handler = error,
6181 .hot_add_disk = raid5_add_disk,
6182 .hot_remove_disk= raid5_remove_disk,
6183 .spare_active = raid5_spare_active,
6184 .sync_request = sync_request,
6185 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006186 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006187 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006188 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006189 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006190 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006191 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006192};
NeilBrown84fc4b52011-10-11 16:49:58 +11006193static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194{
6195 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006196 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 .owner = THIS_MODULE,
6198 .make_request = make_request,
6199 .run = run,
6200 .stop = stop,
6201 .status = status,
6202 .error_handler = error,
6203 .hot_add_disk = raid5_add_disk,
6204 .hot_remove_disk= raid5_remove_disk,
6205 .spare_active = raid5_spare_active,
6206 .sync_request = sync_request,
6207 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006208 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006209 .check_reshape = raid5_check_reshape,
6210 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006211 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006212 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006213 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214};
6215
NeilBrown84fc4b52011-10-11 16:49:58 +11006216static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217{
NeilBrown2604b702006-01-06 00:20:36 -08006218 .name = "raid4",
6219 .level = 4,
6220 .owner = THIS_MODULE,
6221 .make_request = make_request,
6222 .run = run,
6223 .stop = stop,
6224 .status = status,
6225 .error_handler = error,
6226 .hot_add_disk = raid5_add_disk,
6227 .hot_remove_disk= raid5_remove_disk,
6228 .spare_active = raid5_spare_active,
6229 .sync_request = sync_request,
6230 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006231 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006232 .check_reshape = raid5_check_reshape,
6233 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006234 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006235 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006236 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006237};
6238
6239static int __init raid5_init(void)
6240{
NeilBrown16a53ec2006-06-26 00:27:38 -07006241 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006242 register_md_personality(&raid5_personality);
6243 register_md_personality(&raid4_personality);
6244 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245}
6246
NeilBrown2604b702006-01-06 00:20:36 -08006247static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248{
NeilBrown16a53ec2006-06-26 00:27:38 -07006249 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006250 unregister_md_personality(&raid5_personality);
6251 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252}
6253
6254module_init(raid5_init);
6255module_exit(raid5_exit);
6256MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006257MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006259MODULE_ALIAS("md-raid5");
6260MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006261MODULE_ALIAS("md-level-5");
6262MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006263MODULE_ALIAS("md-personality-8"); /* RAID6 */
6264MODULE_ALIAS("md-raid6");
6265MODULE_ALIAS("md-level-6");
6266
6267/* This used to be two separate modules, they were: */
6268MODULE_ALIAS("raid5");
6269MODULE_ALIAS("raid6");