blob: ab613efbbeadfc3b458f19c7681c20a6b4051a32 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110056#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110057#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110058#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110059#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * Stripe cache
63 */
64
65#define NR_STRIPES 256
66#define STRIPE_SIZE PAGE_SIZE
67#define STRIPE_SHIFT (PAGE_SHIFT - 9)
68#define STRIPE_SECTORS (STRIPE_SIZE>>9)
69#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070070#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080071#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define HASH_MASK (NR_HASH - 1)
73
NeilBrownd1688a62011-10-11 16:49:52 +110074static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110075{
76 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
77 return &conf->stripe_hashtbl[hash];
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* bio's attached to a stripe+device for I/O are linked together in bi_sector
81 * order without overlap. There may be several bio's per stripe+device, and
82 * a bio could span several devices.
83 * When walking this list for a particular stripe+device, we must never proceed
84 * beyond a bio that extends past this device, as the next bio might no longer
85 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110086 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * of the current stripe+device
88 */
NeilBrowndb298e12011-10-07 14:23:00 +110089static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
90{
91 int sectors = bio->bi_size >> 9;
92 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
93 return bio->bi_next;
94 else
95 return NULL;
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jens Axboe960e7392008-08-15 10:41:18 +020098/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +020099 * We maintain a biased count of active stripes in the bottom 16 bits of
100 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200101 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000102static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200103{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000104 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
105 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200106}
107
Shaohua Lie7836bd62012-07-19 16:01:31 +1000108static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200109{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000110 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
111 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200112}
113
Shaohua Lie7836bd62012-07-19 16:01:31 +1000114static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200115{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000116 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
117 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200118}
119
Shaohua Lie7836bd62012-07-19 16:01:31 +1000120static inline void raid5_set_bi_processed_stripes(struct bio *bio,
121 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200122{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000123 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
124 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200125
Shaohua Lie7836bd62012-07-19 16:01:31 +1000126 do {
127 old = atomic_read(segments);
128 new = (old & 0xffff) | (cnt << 16);
129 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200130}
131
Shaohua Lie7836bd62012-07-19 16:01:31 +1000132static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200133{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000134 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
135 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200136}
137
NeilBrownd0dabf72009-03-31 14:39:38 +1100138/* Find first data disk in a raid6 stripe */
139static inline int raid6_d0(struct stripe_head *sh)
140{
NeilBrown67cc2b82009-03-31 14:39:38 +1100141 if (sh->ddf_layout)
142 /* ddf always start from first device */
143 return 0;
144 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100145 if (sh->qd_idx == sh->disks - 1)
146 return 0;
147 else
148 return sh->qd_idx + 1;
149}
NeilBrown16a53ec2006-06-26 00:27:38 -0700150static inline int raid6_next_disk(int disk, int raid_disks)
151{
152 disk++;
153 return (disk < raid_disks) ? disk : 0;
154}
Dan Williamsa4456852007-07-09 11:56:43 -0700155
NeilBrownd0dabf72009-03-31 14:39:38 +1100156/* When walking through the disks in a raid5, starting at raid6_d0,
157 * We need to map each disk to a 'slot', where the data disks are slot
158 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
159 * is raid_disks-1. This help does that mapping.
160 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100161static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
162 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100163{
Dan Williams66295422009-10-19 18:09:32 -0700164 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100165
NeilBrowne4424fe2009-10-16 16:27:34 +1100166 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700167 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100168 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100169 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100170 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100171 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100172 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700173 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100174 return slot;
175}
176
Dan Williamsa4456852007-07-09 11:56:43 -0700177static void return_io(struct bio *return_bi)
178{
179 struct bio *bi = return_bi;
180 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700181
182 return_bi = bi->bi_next;
183 bi->bi_next = NULL;
184 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000185 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700186 bi = return_bi;
187 }
188}
189
NeilBrownd1688a62011-10-11 16:49:52 +1100190static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Dan Williams600aa102008-06-28 08:32:05 +1000192static int stripe_operations_active(struct stripe_head *sh)
193{
194 return sh->check_state || sh->reconstruct_state ||
195 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
196 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
197}
198
Shaohua Li4eb788d2012-07-19 16:01:31 +1000199static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000201 BUG_ON(!list_empty(&sh->lru));
202 BUG_ON(atomic_read(&conf->active_stripes)==0);
203 if (test_bit(STRIPE_HANDLE, &sh->state)) {
204 if (test_bit(STRIPE_DELAYED, &sh->state) &&
205 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
206 list_add_tail(&sh->lru, &conf->delayed_list);
207 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
208 sh->bm_seq - conf->seq_write > 0)
209 list_add_tail(&sh->lru, &conf->bitmap_list);
210 else {
211 clear_bit(STRIPE_DELAYED, &sh->state);
212 clear_bit(STRIPE_BIT_DELAY, &sh->state);
213 list_add_tail(&sh->lru, &conf->handle_list);
214 }
215 md_wakeup_thread(conf->mddev->thread);
216 } else {
217 BUG_ON(stripe_operations_active(sh));
218 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
219 if (atomic_dec_return(&conf->preread_active_stripes)
220 < IO_THRESHOLD)
221 md_wakeup_thread(conf->mddev->thread);
222 atomic_dec(&conf->active_stripes);
223 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
224 list_add_tail(&sh->lru, &conf->inactive_list);
225 wake_up(&conf->wait_for_stripe);
226 if (conf->retry_read_aligned)
227 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 }
230}
NeilBrownd0dabf72009-03-31 14:39:38 +1100231
Shaohua Li4eb788d2012-07-19 16:01:31 +1000232static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
233{
234 if (atomic_dec_and_test(&sh->count))
235 do_release_stripe(conf, sh);
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static void release_stripe(struct stripe_head *sh)
239{
NeilBrownd1688a62011-10-11 16:49:52 +1100240 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700242
Shaohua Li4eb788d2012-07-19 16:01:31 +1000243 local_irq_save(flags);
244 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
245 do_release_stripe(conf, sh);
246 spin_unlock(&conf->device_lock);
247 }
248 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
NeilBrownfccddba2006-01-06 00:20:33 -0800251static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Dan Williams45b42332007-07-09 11:56:43 -0700253 pr_debug("remove_hash(), stripe %llu\n",
254 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
NeilBrownfccddba2006-01-06 00:20:33 -0800256 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
NeilBrownd1688a62011-10-11 16:49:52 +1100259static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
NeilBrownfccddba2006-01-06 00:20:33 -0800261 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Dan Williams45b42332007-07-09 11:56:43 -0700263 pr_debug("insert_hash(), stripe %llu\n",
264 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
NeilBrownfccddba2006-01-06 00:20:33 -0800266 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269
270/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100271static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct stripe_head *sh = NULL;
274 struct list_head *first;
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (list_empty(&conf->inactive_list))
277 goto out;
278 first = conf->inactive_list.next;
279 sh = list_entry(first, struct stripe_head, lru);
280 list_del_init(first);
281 remove_hash(sh);
282 atomic_inc(&conf->active_stripes);
283out:
284 return sh;
285}
286
NeilBrowne4e11e32010-06-16 16:45:16 +1000287static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct page *p;
290 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000291 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
NeilBrowne4e11e32010-06-16 16:45:16 +1000293 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 p = sh->dev[i].page;
295 if (!p)
296 continue;
297 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800298 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300}
301
NeilBrowne4e11e32010-06-16 16:45:16 +1000302static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000305 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
NeilBrowne4e11e32010-06-16 16:45:16 +1000307 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct page *page;
309
310 if (!(page = alloc_page(GFP_KERNEL))) {
311 return 1;
312 }
313 sh->dev[i].page = page;
314 }
315 return 0;
316}
317
NeilBrown784052e2009-03-31 15:19:07 +1100318static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100319static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100320 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
NeilBrownb5663ba2009-03-31 14:39:38 +1100322static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
NeilBrownd1688a62011-10-11 16:49:52 +1100324 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800325 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200327 BUG_ON(atomic_read(&sh->count) != 0);
328 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000329 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700330
Dan Williams45b42332007-07-09 11:56:43 -0700331 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 (unsigned long long)sh->sector);
333
334 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700335
NeilBrown86b42c72009-03-31 15:19:03 +1100336 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100337 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100339 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 sh->state = 0;
341
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800342
343 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct r5dev *dev = &sh->dev[i];
345
Dan Williamsd84e0f12007-01-02 13:52:30 -0700346 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700348 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700350 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000352 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100355 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357 insert_hash(conf, sh);
358}
359
NeilBrownd1688a62011-10-11 16:49:52 +1100360static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100361 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800364 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Dan Williams45b42332007-07-09 11:56:43 -0700366 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800367 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100368 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700370 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return NULL;
372}
373
NeilBrown674806d2010-06-16 17:17:53 +1000374/*
375 * Need to check if array has failed when deciding whether to:
376 * - start an array
377 * - remove non-faulty devices
378 * - add a spare
379 * - allow a reshape
380 * This determination is simple when no reshape is happening.
381 * However if there is a reshape, we need to carefully check
382 * both the before and after sections.
383 * This is because some failed devices may only affect one
384 * of the two sections, and some non-in_sync devices may
385 * be insync in the section most affected by failed devices.
386 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100387static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000388{
NeilBrown908f4fb2011-12-23 10:17:50 +1100389 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000390 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000391
392 rcu_read_lock();
393 degraded = 0;
394 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100395 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000396 if (!rdev || test_bit(Faulty, &rdev->flags))
397 degraded++;
398 else if (test_bit(In_sync, &rdev->flags))
399 ;
400 else
401 /* not in-sync or faulty.
402 * If the reshape increases the number of devices,
403 * this is being recovered by the reshape, so
404 * this 'previous' section is not in_sync.
405 * If the number of devices is being reduced however,
406 * the device can only be part of the array if
407 * we are reverting a reshape, so this section will
408 * be in-sync.
409 */
410 if (conf->raid_disks >= conf->previous_raid_disks)
411 degraded++;
412 }
413 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100414 if (conf->raid_disks == conf->previous_raid_disks)
415 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000416 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100417 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000418 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100419 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown674806d2010-06-16 17:17:53 +1000420 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100421 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000422 else if (test_bit(In_sync, &rdev->flags))
423 ;
424 else
425 /* not in-sync or faulty.
426 * If reshape increases the number of devices, this
427 * section has already been recovered, else it
428 * almost certainly hasn't.
429 */
430 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100431 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000432 }
433 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100434 if (degraded2 > degraded)
435 return degraded2;
436 return degraded;
437}
438
439static int has_failed(struct r5conf *conf)
440{
441 int degraded;
442
443 if (conf->mddev->reshape_position == MaxSector)
444 return conf->mddev->degraded > conf->max_degraded;
445
446 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000447 if (degraded > conf->max_degraded)
448 return 1;
449 return 0;
450}
451
NeilBrownb5663ba2009-03-31 14:39:38 +1100452static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100453get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000454 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 struct stripe_head *sh;
457
Dan Williams45b42332007-07-09 11:56:43 -0700458 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 spin_lock_irq(&conf->device_lock);
461
462 do {
NeilBrown72626682005-09-09 16:23:54 -0700463 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000464 conf->quiesce == 0 || noquiesce,
NeilBrown72626682005-09-09 16:23:54 -0700465 conf->device_lock, /* nothing */);
NeilBrown86b42c72009-03-31 15:19:03 +1100466 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (!sh) {
468 if (!conf->inactive_blocked)
469 sh = get_free_stripe(conf);
470 if (noblock && sh == NULL)
471 break;
472 if (!sh) {
473 conf->inactive_blocked = 1;
474 wait_event_lock_irq(conf->wait_for_stripe,
475 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800476 (atomic_read(&conf->active_stripes)
477 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 || !conf->inactive_blocked),
479 conf->device_lock,
NeilBrown7c13edc2011-04-18 18:25:43 +1000480 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 conf->inactive_blocked = 0;
482 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100483 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 } else {
485 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100486 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000487 && !test_bit(STRIPE_EXPANDING, &sh->state)
488 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 } else {
490 if (!test_bit(STRIPE_HANDLE, &sh->state))
491 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700492 if (list_empty(&sh->lru) &&
493 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700494 BUG();
495 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 }
498 } while (sh == NULL);
499
500 if (sh)
501 atomic_inc(&sh->count);
502
503 spin_unlock_irq(&conf->device_lock);
504 return sh;
505}
506
NeilBrown05616be2012-05-21 09:27:00 +1000507/* Determine if 'data_offset' or 'new_data_offset' should be used
508 * in this stripe_head.
509 */
510static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
511{
512 sector_t progress = conf->reshape_progress;
513 /* Need a memory barrier to make sure we see the value
514 * of conf->generation, or ->data_offset that was set before
515 * reshape_progress was updated.
516 */
517 smp_rmb();
518 if (progress == MaxSector)
519 return 0;
520 if (sh->generation == conf->generation - 1)
521 return 0;
522 /* We are in a reshape, and this is a new-generation stripe,
523 * so use new_data_offset.
524 */
525 return 1;
526}
527
NeilBrown6712ecf2007-09-27 12:47:43 +0200528static void
529raid5_end_read_request(struct bio *bi, int error);
530static void
531raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700532
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000533static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700534{
NeilBrownd1688a62011-10-11 16:49:52 +1100535 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700536 int i, disks = sh->disks;
537
538 might_sleep();
539
540 for (i = disks; i--; ) {
541 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100542 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100543 struct bio *bi, *rbi;
544 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200545 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
546 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
547 rw = WRITE_FUA;
548 else
549 rw = WRITE;
Shaohua Li9e4447682012-10-11 13:49:49 +1100550 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100551 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200552 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700553 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100554 else if (test_and_clear_bit(R5_WantReplace,
555 &sh->dev[i].flags)) {
556 rw = WRITE;
557 replace_only = 1;
558 } else
Dan Williams91c00922007-01-02 13:52:30 -0700559 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000560 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
561 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700562
563 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100564 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700565
566 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100567 rbi->bi_rw = rw;
568 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700569 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100570 rbi->bi_end_io = raid5_end_write_request;
571 } else
Dan Williams91c00922007-01-02 13:52:30 -0700572 bi->bi_end_io = raid5_end_read_request;
573
574 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100575 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100576 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
577 rdev = rcu_dereference(conf->disks[i].rdev);
578 if (!rdev) {
579 rdev = rrdev;
580 rrdev = NULL;
581 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100582 if (rw & WRITE) {
583 if (replace_only)
584 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100585 if (rdev == rrdev)
586 /* We raced and saw duplicates */
587 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100588 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100589 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100590 rdev = rrdev;
591 rrdev = NULL;
592 }
NeilBrown977df362011-12-23 10:17:53 +1100593
Dan Williams91c00922007-01-02 13:52:30 -0700594 if (rdev && test_bit(Faulty, &rdev->flags))
595 rdev = NULL;
596 if (rdev)
597 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100598 if (rrdev && test_bit(Faulty, &rrdev->flags))
599 rrdev = NULL;
600 if (rrdev)
601 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700602 rcu_read_unlock();
603
NeilBrown73e92e52011-07-28 11:39:22 +1000604 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100605 * need to check for writes. We never accept write errors
606 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000607 */
608 while ((rw & WRITE) && rdev &&
609 test_bit(WriteErrorSeen, &rdev->flags)) {
610 sector_t first_bad;
611 int bad_sectors;
612 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
613 &first_bad, &bad_sectors);
614 if (!bad)
615 break;
616
617 if (bad < 0) {
618 set_bit(BlockedBadBlocks, &rdev->flags);
619 if (!conf->mddev->external &&
620 conf->mddev->flags) {
621 /* It is very unlikely, but we might
622 * still need to write out the
623 * bad block log - better give it
624 * a chance*/
625 md_check_recovery(conf->mddev);
626 }
majianpeng18507532012-07-03 12:11:54 +1000627 /*
628 * Because md_wait_for_blocked_rdev
629 * will dec nr_pending, we must
630 * increment it first.
631 */
632 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000633 md_wait_for_blocked_rdev(rdev, conf->mddev);
634 } else {
635 /* Acknowledged bad block - skip the write */
636 rdev_dec_pending(rdev, conf->mddev);
637 rdev = NULL;
638 }
639 }
640
Dan Williams91c00922007-01-02 13:52:30 -0700641 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100642 if (s->syncing || s->expanding || s->expanded
643 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700644 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
645
Dan Williams2b7497f2008-06-28 08:31:52 +1000646 set_bit(STRIPE_IO_STARTED, &sh->state);
647
Dan Williams91c00922007-01-02 13:52:30 -0700648 bi->bi_bdev = rdev->bdev;
649 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700650 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700651 bi->bi_rw, i);
652 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000653 if (use_new_offset(conf, sh))
654 bi->bi_sector = (sh->sector
655 + rdev->new_data_offset);
656 else
657 bi->bi_sector = (sh->sector
658 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000659 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
660 bi->bi_rw |= REQ_FLUSH;
661
Dan Williams91c00922007-01-02 13:52:30 -0700662 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700663 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700664 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
665 bi->bi_io_vec[0].bv_offset = 0;
666 bi->bi_size = STRIPE_SIZE;
667 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100668 if (rrdev)
669 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700670 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100671 }
672 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100673 if (s->syncing || s->expanding || s->expanded
674 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100675 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
676
677 set_bit(STRIPE_IO_STARTED, &sh->state);
678
679 rbi->bi_bdev = rrdev->bdev;
680 pr_debug("%s: for %llu schedule op %ld on "
681 "replacement disc %d\n",
682 __func__, (unsigned long long)sh->sector,
683 rbi->bi_rw, i);
684 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000685 if (use_new_offset(conf, sh))
686 rbi->bi_sector = (sh->sector
687 + rrdev->new_data_offset);
688 else
689 rbi->bi_sector = (sh->sector
690 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100691 rbi->bi_flags = 1 << BIO_UPTODATE;
692 rbi->bi_idx = 0;
693 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
694 rbi->bi_io_vec[0].bv_offset = 0;
695 rbi->bi_size = STRIPE_SIZE;
696 rbi->bi_next = NULL;
697 generic_make_request(rbi);
698 }
699 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000700 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700701 set_bit(STRIPE_DEGRADED, &sh->state);
702 pr_debug("skip op %ld on disc %d for sector %llu\n",
703 bi->bi_rw, i, (unsigned long long)sh->sector);
704 clear_bit(R5_LOCKED, &sh->dev[i].flags);
705 set_bit(STRIPE_HANDLE, &sh->state);
706 }
707 }
708}
709
710static struct dma_async_tx_descriptor *
711async_copy_data(int frombio, struct bio *bio, struct page *page,
712 sector_t sector, struct dma_async_tx_descriptor *tx)
713{
714 struct bio_vec *bvl;
715 struct page *bio_page;
716 int i;
717 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700718 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700719 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700720
721 if (bio->bi_sector >= sector)
722 page_offset = (signed)(bio->bi_sector - sector) * 512;
723 else
724 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700725
Dan Williams0403e382009-09-08 17:42:50 -0700726 if (frombio)
727 flags |= ASYNC_TX_FENCE;
728 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
729
Dan Williams91c00922007-01-02 13:52:30 -0700730 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000731 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700732 int clen;
733 int b_offset = 0;
734
735 if (page_offset < 0) {
736 b_offset = -page_offset;
737 page_offset += b_offset;
738 len -= b_offset;
739 }
740
741 if (len > 0 && page_offset + len > STRIPE_SIZE)
742 clen = STRIPE_SIZE - page_offset;
743 else
744 clen = len;
745
746 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000747 b_offset += bvl->bv_offset;
748 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700749 if (frombio)
750 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700751 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700752 else
753 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700754 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700755 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700756 /* chain the operations */
757 submit.depend_tx = tx;
758
Dan Williams91c00922007-01-02 13:52:30 -0700759 if (clen < len) /* hit end of page */
760 break;
761 page_offset += len;
762 }
763
764 return tx;
765}
766
767static void ops_complete_biofill(void *stripe_head_ref)
768{
769 struct stripe_head *sh = stripe_head_ref;
770 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700771 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700772
Harvey Harrisone46b2722008-04-28 02:15:50 -0700773 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700774 (unsigned long long)sh->sector);
775
776 /* clear completed biofills */
777 for (i = sh->disks; i--; ) {
778 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700779
780 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700781 /* and check if we need to reply to a read request,
782 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000783 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700784 */
785 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700786 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700787
Dan Williams91c00922007-01-02 13:52:30 -0700788 BUG_ON(!dev->read);
789 rbi = dev->read;
790 dev->read = NULL;
791 while (rbi && rbi->bi_sector <
792 dev->sector + STRIPE_SECTORS) {
793 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000794 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700795 rbi->bi_next = return_bi;
796 return_bi = rbi;
797 }
Dan Williams91c00922007-01-02 13:52:30 -0700798 rbi = rbi2;
799 }
800 }
801 }
Dan Williams83de75c2008-06-28 08:31:58 +1000802 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700803
804 return_io(return_bi);
805
Dan Williamse4d84902007-09-24 10:06:13 -0700806 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700807 release_stripe(sh);
808}
809
810static void ops_run_biofill(struct stripe_head *sh)
811{
812 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700813 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700814 int i;
815
Harvey Harrisone46b2722008-04-28 02:15:50 -0700816 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700817 (unsigned long long)sh->sector);
818
819 for (i = sh->disks; i--; ) {
820 struct r5dev *dev = &sh->dev[i];
821 if (test_bit(R5_Wantfill, &dev->flags)) {
822 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000823 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700824 dev->read = rbi = dev->toread;
825 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000826 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700827 while (rbi && rbi->bi_sector <
828 dev->sector + STRIPE_SECTORS) {
829 tx = async_copy_data(0, rbi, dev->page,
830 dev->sector, tx);
831 rbi = r5_next_bio(rbi, dev->sector);
832 }
833 }
834 }
835
836 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700837 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
838 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700839}
840
Dan Williams4e7d2c02009-08-29 19:13:11 -0700841static void mark_target_uptodate(struct stripe_head *sh, int target)
842{
843 struct r5dev *tgt;
844
845 if (target < 0)
846 return;
847
848 tgt = &sh->dev[target];
849 set_bit(R5_UPTODATE, &tgt->flags);
850 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
851 clear_bit(R5_Wantcompute, &tgt->flags);
852}
853
Dan Williamsac6b53b2009-07-14 13:40:19 -0700854static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700855{
856 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700857
Harvey Harrisone46b2722008-04-28 02:15:50 -0700858 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700859 (unsigned long long)sh->sector);
860
Dan Williamsac6b53b2009-07-14 13:40:19 -0700861 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700862 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700863 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700864
Dan Williamsecc65c92008-06-28 08:31:57 +1000865 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
866 if (sh->check_state == check_state_compute_run)
867 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700868 set_bit(STRIPE_HANDLE, &sh->state);
869 release_stripe(sh);
870}
871
Dan Williamsd6f38f32009-07-14 11:50:52 -0700872/* return a pointer to the address conversion region of the scribble buffer */
873static addr_conv_t *to_addr_conv(struct stripe_head *sh,
874 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700875{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700876 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
877}
878
879static struct dma_async_tx_descriptor *
880ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
881{
Dan Williams91c00922007-01-02 13:52:30 -0700882 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700883 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700884 int target = sh->ops.target;
885 struct r5dev *tgt = &sh->dev[target];
886 struct page *xor_dest = tgt->page;
887 int count = 0;
888 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700889 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700890 int i;
891
892 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700893 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700894 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
895
896 for (i = disks; i--; )
897 if (i != target)
898 xor_srcs[count++] = sh->dev[i].page;
899
900 atomic_inc(&sh->count);
901
Dan Williams0403e382009-09-08 17:42:50 -0700902 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700903 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700904 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700905 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700906 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700907 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700908
Dan Williams91c00922007-01-02 13:52:30 -0700909 return tx;
910}
911
Dan Williamsac6b53b2009-07-14 13:40:19 -0700912/* set_syndrome_sources - populate source buffers for gen_syndrome
913 * @srcs - (struct page *) array of size sh->disks
914 * @sh - stripe_head to parse
915 *
916 * Populates srcs in proper layout order for the stripe and returns the
917 * 'count' of sources to be used in a call to async_gen_syndrome. The P
918 * destination buffer is recorded in srcs[count] and the Q destination
919 * is recorded in srcs[count+1]].
920 */
921static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
922{
923 int disks = sh->disks;
924 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
925 int d0_idx = raid6_d0(sh);
926 int count;
927 int i;
928
929 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100930 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700931
932 count = 0;
933 i = d0_idx;
934 do {
935 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
936
937 srcs[slot] = sh->dev[i].page;
938 i = raid6_next_disk(i, disks);
939 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700940
NeilBrowne4424fe2009-10-16 16:27:34 +1100941 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700942}
943
944static struct dma_async_tx_descriptor *
945ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
946{
947 int disks = sh->disks;
948 struct page **blocks = percpu->scribble;
949 int target;
950 int qd_idx = sh->qd_idx;
951 struct dma_async_tx_descriptor *tx;
952 struct async_submit_ctl submit;
953 struct r5dev *tgt;
954 struct page *dest;
955 int i;
956 int count;
957
958 if (sh->ops.target < 0)
959 target = sh->ops.target2;
960 else if (sh->ops.target2 < 0)
961 target = sh->ops.target;
962 else
963 /* we should only have one valid target */
964 BUG();
965 BUG_ON(target < 0);
966 pr_debug("%s: stripe %llu block: %d\n",
967 __func__, (unsigned long long)sh->sector, target);
968
969 tgt = &sh->dev[target];
970 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
971 dest = tgt->page;
972
973 atomic_inc(&sh->count);
974
975 if (target == qd_idx) {
976 count = set_syndrome_sources(blocks, sh);
977 blocks[count] = NULL; /* regenerating p is not necessary */
978 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700979 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
980 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700981 to_addr_conv(sh, percpu));
982 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
983 } else {
984 /* Compute any data- or p-drive using XOR */
985 count = 0;
986 for (i = disks; i-- ; ) {
987 if (i == target || i == qd_idx)
988 continue;
989 blocks[count++] = sh->dev[i].page;
990 }
991
Dan Williams0403e382009-09-08 17:42:50 -0700992 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
993 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700994 to_addr_conv(sh, percpu));
995 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
996 }
997
998 return tx;
999}
1000
1001static struct dma_async_tx_descriptor *
1002ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1003{
1004 int i, count, disks = sh->disks;
1005 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1006 int d0_idx = raid6_d0(sh);
1007 int faila = -1, failb = -1;
1008 int target = sh->ops.target;
1009 int target2 = sh->ops.target2;
1010 struct r5dev *tgt = &sh->dev[target];
1011 struct r5dev *tgt2 = &sh->dev[target2];
1012 struct dma_async_tx_descriptor *tx;
1013 struct page **blocks = percpu->scribble;
1014 struct async_submit_ctl submit;
1015
1016 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1017 __func__, (unsigned long long)sh->sector, target, target2);
1018 BUG_ON(target < 0 || target2 < 0);
1019 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1020 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1021
Dan Williams6c910a72009-09-16 12:24:54 -07001022 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001023 * slot number conversion for 'faila' and 'failb'
1024 */
1025 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001026 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001027 count = 0;
1028 i = d0_idx;
1029 do {
1030 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1031
1032 blocks[slot] = sh->dev[i].page;
1033
1034 if (i == target)
1035 faila = slot;
1036 if (i == target2)
1037 failb = slot;
1038 i = raid6_next_disk(i, disks);
1039 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001040
1041 BUG_ON(faila == failb);
1042 if (failb < faila)
1043 swap(faila, failb);
1044 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1045 __func__, (unsigned long long)sh->sector, faila, failb);
1046
1047 atomic_inc(&sh->count);
1048
1049 if (failb == syndrome_disks+1) {
1050 /* Q disk is one of the missing disks */
1051 if (faila == syndrome_disks) {
1052 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001053 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1054 ops_complete_compute, sh,
1055 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001056 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001057 STRIPE_SIZE, &submit);
1058 } else {
1059 struct page *dest;
1060 int data_target;
1061 int qd_idx = sh->qd_idx;
1062
1063 /* Missing D+Q: recompute D from P, then recompute Q */
1064 if (target == qd_idx)
1065 data_target = target2;
1066 else
1067 data_target = target;
1068
1069 count = 0;
1070 for (i = disks; i-- ; ) {
1071 if (i == data_target || i == qd_idx)
1072 continue;
1073 blocks[count++] = sh->dev[i].page;
1074 }
1075 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001076 init_async_submit(&submit,
1077 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1078 NULL, NULL, NULL,
1079 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001080 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1081 &submit);
1082
1083 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001084 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1085 ops_complete_compute, sh,
1086 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001087 return async_gen_syndrome(blocks, 0, count+2,
1088 STRIPE_SIZE, &submit);
1089 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001090 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001091 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1092 ops_complete_compute, sh,
1093 to_addr_conv(sh, percpu));
1094 if (failb == syndrome_disks) {
1095 /* We're missing D+P. */
1096 return async_raid6_datap_recov(syndrome_disks+2,
1097 STRIPE_SIZE, faila,
1098 blocks, &submit);
1099 } else {
1100 /* We're missing D+D. */
1101 return async_raid6_2data_recov(syndrome_disks+2,
1102 STRIPE_SIZE, faila, failb,
1103 blocks, &submit);
1104 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001105 }
1106}
1107
1108
Dan Williams91c00922007-01-02 13:52:30 -07001109static void ops_complete_prexor(void *stripe_head_ref)
1110{
1111 struct stripe_head *sh = stripe_head_ref;
1112
Harvey Harrisone46b2722008-04-28 02:15:50 -07001113 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001114 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001115}
1116
1117static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001118ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1119 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001120{
Dan Williams91c00922007-01-02 13:52:30 -07001121 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001122 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001123 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001124 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001125
1126 /* existing parity data subtracted */
1127 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1128
Harvey Harrisone46b2722008-04-28 02:15:50 -07001129 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001130 (unsigned long long)sh->sector);
1131
1132 for (i = disks; i--; ) {
1133 struct r5dev *dev = &sh->dev[i];
1134 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001135 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001136 xor_srcs[count++] = dev->page;
1137 }
1138
Dan Williams0403e382009-09-08 17:42:50 -07001139 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001140 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001141 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001142
1143 return tx;
1144}
1145
1146static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001147ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001148{
1149 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001150 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001151
Harvey Harrisone46b2722008-04-28 02:15:50 -07001152 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001153 (unsigned long long)sh->sector);
1154
1155 for (i = disks; i--; ) {
1156 struct r5dev *dev = &sh->dev[i];
1157 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001158
Dan Williamsd8ee0722008-06-28 08:32:06 +10001159 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001160 struct bio *wbi;
1161
Shaohua Lib17459c2012-07-19 16:01:31 +10001162 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001163 chosen = dev->towrite;
1164 dev->towrite = NULL;
1165 BUG_ON(dev->written);
1166 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001167 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001168
1169 while (wbi && wbi->bi_sector <
1170 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001171 if (wbi->bi_rw & REQ_FUA)
1172 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001173 if (wbi->bi_rw & REQ_SYNC)
1174 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001175 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001176 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001177 else
Shaohua Li620125f2012-10-11 13:49:05 +11001178 tx = async_copy_data(1, wbi, dev->page,
1179 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001180 wbi = r5_next_bio(wbi, dev->sector);
1181 }
1182 }
1183 }
1184
1185 return tx;
1186}
1187
Dan Williamsac6b53b2009-07-14 13:40:19 -07001188static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001189{
1190 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001191 int disks = sh->disks;
1192 int pd_idx = sh->pd_idx;
1193 int qd_idx = sh->qd_idx;
1194 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001195 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001196
Harvey Harrisone46b2722008-04-28 02:15:50 -07001197 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001198 (unsigned long long)sh->sector);
1199
Shaohua Libc0934f2012-05-22 13:55:05 +10001200 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001201 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001202 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001203 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001204 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001205
Dan Williams91c00922007-01-02 13:52:30 -07001206 for (i = disks; i--; ) {
1207 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001208
Tejun Heoe9c74692010-09-03 11:56:18 +02001209 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001210 if (!discard)
1211 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001212 if (fua)
1213 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001214 if (sync)
1215 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001216 }
Dan Williams91c00922007-01-02 13:52:30 -07001217 }
1218
Dan Williamsd8ee0722008-06-28 08:32:06 +10001219 if (sh->reconstruct_state == reconstruct_state_drain_run)
1220 sh->reconstruct_state = reconstruct_state_drain_result;
1221 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1222 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1223 else {
1224 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1225 sh->reconstruct_state = reconstruct_state_result;
1226 }
Dan Williams91c00922007-01-02 13:52:30 -07001227
1228 set_bit(STRIPE_HANDLE, &sh->state);
1229 release_stripe(sh);
1230}
1231
1232static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001233ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1234 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001235{
Dan Williams91c00922007-01-02 13:52:30 -07001236 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001237 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001238 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001239 int count = 0, pd_idx = sh->pd_idx, i;
1240 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001241 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001242 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001243
Harvey Harrisone46b2722008-04-28 02:15:50 -07001244 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001245 (unsigned long long)sh->sector);
1246
Shaohua Li620125f2012-10-11 13:49:05 +11001247 for (i = 0; i < sh->disks; i++) {
1248 if (pd_idx == i)
1249 continue;
1250 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1251 break;
1252 }
1253 if (i >= sh->disks) {
1254 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001255 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1256 ops_complete_reconstruct(sh);
1257 return;
1258 }
Dan Williams91c00922007-01-02 13:52:30 -07001259 /* check if prexor is active which means only process blocks
1260 * that are part of a read-modify-write (written)
1261 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001262 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1263 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001264 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1265 for (i = disks; i--; ) {
1266 struct r5dev *dev = &sh->dev[i];
1267 if (dev->written)
1268 xor_srcs[count++] = dev->page;
1269 }
1270 } else {
1271 xor_dest = sh->dev[pd_idx].page;
1272 for (i = disks; i--; ) {
1273 struct r5dev *dev = &sh->dev[i];
1274 if (i != pd_idx)
1275 xor_srcs[count++] = dev->page;
1276 }
1277 }
1278
Dan Williams91c00922007-01-02 13:52:30 -07001279 /* 1/ if we prexor'd then the dest is reused as a source
1280 * 2/ if we did not prexor then we are redoing the parity
1281 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1282 * for the synchronous xor case
1283 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001284 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001285 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1286
1287 atomic_inc(&sh->count);
1288
Dan Williamsac6b53b2009-07-14 13:40:19 -07001289 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001290 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001291 if (unlikely(count == 1))
1292 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1293 else
1294 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001295}
1296
Dan Williamsac6b53b2009-07-14 13:40:19 -07001297static void
1298ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1299 struct dma_async_tx_descriptor *tx)
1300{
1301 struct async_submit_ctl submit;
1302 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001303 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001304
1305 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1306
Shaohua Li620125f2012-10-11 13:49:05 +11001307 for (i = 0; i < sh->disks; i++) {
1308 if (sh->pd_idx == i || sh->qd_idx == i)
1309 continue;
1310 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1311 break;
1312 }
1313 if (i >= sh->disks) {
1314 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001315 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1316 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1317 ops_complete_reconstruct(sh);
1318 return;
1319 }
1320
Dan Williamsac6b53b2009-07-14 13:40:19 -07001321 count = set_syndrome_sources(blocks, sh);
1322
1323 atomic_inc(&sh->count);
1324
1325 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1326 sh, to_addr_conv(sh, percpu));
1327 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001328}
1329
1330static void ops_complete_check(void *stripe_head_ref)
1331{
1332 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001333
Harvey Harrisone46b2722008-04-28 02:15:50 -07001334 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001335 (unsigned long long)sh->sector);
1336
Dan Williamsecc65c92008-06-28 08:31:57 +10001337 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001338 set_bit(STRIPE_HANDLE, &sh->state);
1339 release_stripe(sh);
1340}
1341
Dan Williamsac6b53b2009-07-14 13:40:19 -07001342static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001343{
Dan Williams91c00922007-01-02 13:52:30 -07001344 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001345 int pd_idx = sh->pd_idx;
1346 int qd_idx = sh->qd_idx;
1347 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001348 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001349 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001350 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001351 int count;
1352 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001353
Harvey Harrisone46b2722008-04-28 02:15:50 -07001354 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001355 (unsigned long long)sh->sector);
1356
Dan Williamsac6b53b2009-07-14 13:40:19 -07001357 count = 0;
1358 xor_dest = sh->dev[pd_idx].page;
1359 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001360 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001361 if (i == pd_idx || i == qd_idx)
1362 continue;
1363 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001364 }
1365
Dan Williamsd6f38f32009-07-14 11:50:52 -07001366 init_async_submit(&submit, 0, NULL, NULL, NULL,
1367 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001368 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001369 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001370
Dan Williams91c00922007-01-02 13:52:30 -07001371 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001372 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1373 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001374}
1375
Dan Williamsac6b53b2009-07-14 13:40:19 -07001376static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1377{
1378 struct page **srcs = percpu->scribble;
1379 struct async_submit_ctl submit;
1380 int count;
1381
1382 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1383 (unsigned long long)sh->sector, checkp);
1384
1385 count = set_syndrome_sources(srcs, sh);
1386 if (!checkp)
1387 srcs[count] = NULL;
1388
1389 atomic_inc(&sh->count);
1390 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1391 sh, to_addr_conv(sh, percpu));
1392 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1393 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1394}
1395
Dan Williams417b8d42009-10-16 16:25:22 +11001396static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001397{
1398 int overlap_clear = 0, i, disks = sh->disks;
1399 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001400 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001401 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001402 struct raid5_percpu *percpu;
1403 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001404
Dan Williamsd6f38f32009-07-14 11:50:52 -07001405 cpu = get_cpu();
1406 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001407 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001408 ops_run_biofill(sh);
1409 overlap_clear++;
1410 }
1411
Dan Williams7b3a8712008-06-28 08:32:09 +10001412 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001413 if (level < 6)
1414 tx = ops_run_compute5(sh, percpu);
1415 else {
1416 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1417 tx = ops_run_compute6_1(sh, percpu);
1418 else
1419 tx = ops_run_compute6_2(sh, percpu);
1420 }
1421 /* terminate the chain if reconstruct is not set to be run */
1422 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001423 async_tx_ack(tx);
1424 }
Dan Williams91c00922007-01-02 13:52:30 -07001425
Dan Williams600aa102008-06-28 08:32:05 +10001426 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001427 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001428
Dan Williams600aa102008-06-28 08:32:05 +10001429 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001430 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001431 overlap_clear++;
1432 }
1433
Dan Williamsac6b53b2009-07-14 13:40:19 -07001434 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1435 if (level < 6)
1436 ops_run_reconstruct5(sh, percpu, tx);
1437 else
1438 ops_run_reconstruct6(sh, percpu, tx);
1439 }
Dan Williams91c00922007-01-02 13:52:30 -07001440
Dan Williamsac6b53b2009-07-14 13:40:19 -07001441 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1442 if (sh->check_state == check_state_run)
1443 ops_run_check_p(sh, percpu);
1444 else if (sh->check_state == check_state_run_q)
1445 ops_run_check_pq(sh, percpu, 0);
1446 else if (sh->check_state == check_state_run_pq)
1447 ops_run_check_pq(sh, percpu, 1);
1448 else
1449 BUG();
1450 }
Dan Williams91c00922007-01-02 13:52:30 -07001451
Dan Williams91c00922007-01-02 13:52:30 -07001452 if (overlap_clear)
1453 for (i = disks; i--; ) {
1454 struct r5dev *dev = &sh->dev[i];
1455 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1456 wake_up(&sh->raid_conf->wait_for_overlap);
1457 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001458 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001459}
1460
Dan Williams417b8d42009-10-16 16:25:22 +11001461#ifdef CONFIG_MULTICORE_RAID456
1462static void async_run_ops(void *param, async_cookie_t cookie)
1463{
1464 struct stripe_head *sh = param;
1465 unsigned long ops_request = sh->ops.request;
1466
1467 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1468 wake_up(&sh->ops.wait_for_ops);
1469
1470 __raid_run_ops(sh, ops_request);
1471 release_stripe(sh);
1472}
1473
1474static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1475{
1476 /* since handle_stripe can be called outside of raid5d context
1477 * we need to ensure sh->ops.request is de-staged before another
1478 * request arrives
1479 */
1480 wait_event(sh->ops.wait_for_ops,
1481 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1482 sh->ops.request = ops_request;
1483
1484 atomic_inc(&sh->count);
1485 async_schedule(async_run_ops, sh);
1486}
1487#else
1488#define raid_run_ops __raid_run_ops
1489#endif
1490
NeilBrownd1688a62011-10-11 16:49:52 +11001491static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001494 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001495 if (!sh)
1496 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001497
NeilBrown3f294f42005-11-08 21:39:25 -08001498 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001499 #ifdef CONFIG_MULTICORE_RAID456
1500 init_waitqueue_head(&sh->ops.wait_for_ops);
1501 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001502
Shaohua Lib17459c2012-07-19 16:01:31 +10001503 spin_lock_init(&sh->stripe_lock);
1504
NeilBrowne4e11e32010-06-16 16:45:16 +10001505 if (grow_buffers(sh)) {
1506 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001507 kmem_cache_free(conf->slab_cache, sh);
1508 return 0;
1509 }
1510 /* we just created an active stripe so... */
1511 atomic_set(&sh->count, 1);
1512 atomic_inc(&conf->active_stripes);
1513 INIT_LIST_HEAD(&sh->lru);
1514 release_stripe(sh);
1515 return 1;
1516}
1517
NeilBrownd1688a62011-10-11 16:49:52 +11001518static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001519{
Christoph Lametere18b8902006-12-06 20:33:20 -08001520 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001521 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
NeilBrownf4be6b42010-06-01 19:37:25 +10001523 if (conf->mddev->gendisk)
1524 sprintf(conf->cache_name[0],
1525 "raid%d-%s", conf->level, mdname(conf->mddev));
1526 else
1527 sprintf(conf->cache_name[0],
1528 "raid%d-%p", conf->level, conf->mddev);
1529 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1530
NeilBrownad01c9e2006-03-27 01:18:07 -08001531 conf->active_name = 0;
1532 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001534 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (!sc)
1536 return 1;
1537 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001538 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001539 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001540 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return 0;
1543}
NeilBrown29269552006-03-27 01:18:10 -08001544
Dan Williamsd6f38f32009-07-14 11:50:52 -07001545/**
1546 * scribble_len - return the required size of the scribble region
1547 * @num - total number of disks in the array
1548 *
1549 * The size must be enough to contain:
1550 * 1/ a struct page pointer for each device in the array +2
1551 * 2/ room to convert each entry in (1) to its corresponding dma
1552 * (dma_map_page()) or page (page_address()) address.
1553 *
1554 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1555 * calculate over all devices (not just the data blocks), using zeros in place
1556 * of the P and Q blocks.
1557 */
1558static size_t scribble_len(int num)
1559{
1560 size_t len;
1561
1562 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1563
1564 return len;
1565}
1566
NeilBrownd1688a62011-10-11 16:49:52 +11001567static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001568{
1569 /* Make all the stripes able to hold 'newsize' devices.
1570 * New slots in each stripe get 'page' set to a new page.
1571 *
1572 * This happens in stages:
1573 * 1/ create a new kmem_cache and allocate the required number of
1574 * stripe_heads.
1575 * 2/ gather all the old stripe_heads and tranfer the pages across
1576 * to the new stripe_heads. This will have the side effect of
1577 * freezing the array as once all stripe_heads have been collected,
1578 * no IO will be possible. Old stripe heads are freed once their
1579 * pages have been transferred over, and the old kmem_cache is
1580 * freed when all stripes are done.
1581 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1582 * we simple return a failre status - no need to clean anything up.
1583 * 4/ allocate new pages for the new slots in the new stripe_heads.
1584 * If this fails, we don't bother trying the shrink the
1585 * stripe_heads down again, we just leave them as they are.
1586 * As each stripe_head is processed the new one is released into
1587 * active service.
1588 *
1589 * Once step2 is started, we cannot afford to wait for a write,
1590 * so we use GFP_NOIO allocations.
1591 */
1592 struct stripe_head *osh, *nsh;
1593 LIST_HEAD(newstripes);
1594 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001595 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001596 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001597 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001598 int i;
1599
1600 if (newsize <= conf->pool_size)
1601 return 0; /* never bother to shrink */
1602
Dan Williamsb5470dc2008-06-27 21:44:04 -07001603 err = md_allow_write(conf->mddev);
1604 if (err)
1605 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001606
NeilBrownad01c9e2006-03-27 01:18:07 -08001607 /* Step 1 */
1608 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1609 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001610 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001611 if (!sc)
1612 return -ENOMEM;
1613
1614 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001615 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001616 if (!nsh)
1617 break;
1618
NeilBrownad01c9e2006-03-27 01:18:07 -08001619 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001620 #ifdef CONFIG_MULTICORE_RAID456
1621 init_waitqueue_head(&nsh->ops.wait_for_ops);
1622 #endif
NeilBrownad01c9e2006-03-27 01:18:07 -08001623
1624 list_add(&nsh->lru, &newstripes);
1625 }
1626 if (i) {
1627 /* didn't get enough, give up */
1628 while (!list_empty(&newstripes)) {
1629 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1630 list_del(&nsh->lru);
1631 kmem_cache_free(sc, nsh);
1632 }
1633 kmem_cache_destroy(sc);
1634 return -ENOMEM;
1635 }
1636 /* Step 2 - Must use GFP_NOIO now.
1637 * OK, we have enough stripes, start collecting inactive
1638 * stripes and copying them over
1639 */
1640 list_for_each_entry(nsh, &newstripes, lru) {
1641 spin_lock_irq(&conf->device_lock);
1642 wait_event_lock_irq(conf->wait_for_stripe,
1643 !list_empty(&conf->inactive_list),
1644 conf->device_lock,
NeilBrown482c0832011-04-18 18:25:42 +10001645 );
NeilBrownad01c9e2006-03-27 01:18:07 -08001646 osh = get_free_stripe(conf);
1647 spin_unlock_irq(&conf->device_lock);
1648 atomic_set(&nsh->count, 1);
1649 for(i=0; i<conf->pool_size; i++)
1650 nsh->dev[i].page = osh->dev[i].page;
1651 for( ; i<newsize; i++)
1652 nsh->dev[i].page = NULL;
1653 kmem_cache_free(conf->slab_cache, osh);
1654 }
1655 kmem_cache_destroy(conf->slab_cache);
1656
1657 /* Step 3.
1658 * At this point, we are holding all the stripes so the array
1659 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001660 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001661 */
1662 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1663 if (ndisks) {
1664 for (i=0; i<conf->raid_disks; i++)
1665 ndisks[i] = conf->disks[i];
1666 kfree(conf->disks);
1667 conf->disks = ndisks;
1668 } else
1669 err = -ENOMEM;
1670
Dan Williamsd6f38f32009-07-14 11:50:52 -07001671 get_online_cpus();
1672 conf->scribble_len = scribble_len(newsize);
1673 for_each_present_cpu(cpu) {
1674 struct raid5_percpu *percpu;
1675 void *scribble;
1676
1677 percpu = per_cpu_ptr(conf->percpu, cpu);
1678 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1679
1680 if (scribble) {
1681 kfree(percpu->scribble);
1682 percpu->scribble = scribble;
1683 } else {
1684 err = -ENOMEM;
1685 break;
1686 }
1687 }
1688 put_online_cpus();
1689
NeilBrownad01c9e2006-03-27 01:18:07 -08001690 /* Step 4, return new stripes to service */
1691 while(!list_empty(&newstripes)) {
1692 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1693 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001694
NeilBrownad01c9e2006-03-27 01:18:07 -08001695 for (i=conf->raid_disks; i < newsize; i++)
1696 if (nsh->dev[i].page == NULL) {
1697 struct page *p = alloc_page(GFP_NOIO);
1698 nsh->dev[i].page = p;
1699 if (!p)
1700 err = -ENOMEM;
1701 }
1702 release_stripe(nsh);
1703 }
1704 /* critical section pass, GFP_NOIO no longer needed */
1705
1706 conf->slab_cache = sc;
1707 conf->active_name = 1-conf->active_name;
1708 conf->pool_size = newsize;
1709 return err;
1710}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
NeilBrownd1688a62011-10-11 16:49:52 +11001712static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 struct stripe_head *sh;
1715
NeilBrown3f294f42005-11-08 21:39:25 -08001716 spin_lock_irq(&conf->device_lock);
1717 sh = get_free_stripe(conf);
1718 spin_unlock_irq(&conf->device_lock);
1719 if (!sh)
1720 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001721 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001722 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001723 kmem_cache_free(conf->slab_cache, sh);
1724 atomic_dec(&conf->active_stripes);
1725 return 1;
1726}
1727
NeilBrownd1688a62011-10-11 16:49:52 +11001728static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001729{
1730 while (drop_one_stripe(conf))
1731 ;
1732
NeilBrown29fc7e32006-02-03 03:03:41 -08001733 if (conf->slab_cache)
1734 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 conf->slab_cache = NULL;
1736}
1737
NeilBrown6712ecf2007-09-27 12:47:43 +02001738static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
NeilBrown99c0fb52009-03-31 14:39:38 +11001740 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001741 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001742 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001744 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001745 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001746 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 for (i=0 ; i<disks; i++)
1749 if (bi == &sh->dev[i].req)
1750 break;
1751
Dan Williams45b42332007-07-09 11:56:43 -07001752 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1753 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 uptodate);
1755 if (i == disks) {
1756 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001757 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
NeilBrown14a75d32011-12-23 10:17:52 +11001759 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001760 /* If replacement finished while this request was outstanding,
1761 * 'replacement' might be NULL already.
1762 * In that case it moved down to 'rdev'.
1763 * rdev is not removed until all requests are finished.
1764 */
NeilBrown14a75d32011-12-23 10:17:52 +11001765 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001766 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001767 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
NeilBrown05616be2012-05-21 09:27:00 +10001769 if (use_new_offset(conf, sh))
1770 s = sh->sector + rdev->new_data_offset;
1771 else
1772 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001775 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001776 /* Note that this cannot happen on a
1777 * replacement device. We just fail those on
1778 * any error
1779 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001780 printk_ratelimited(
1781 KERN_INFO
1782 "md/raid:%s: read error corrected"
1783 " (%lu sectors at %llu on %s)\n",
1784 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001785 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001786 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001787 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001788 clear_bit(R5_ReadError, &sh->dev[i].flags);
1789 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001790 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1791 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1792
NeilBrown14a75d32011-12-23 10:17:52 +11001793 if (atomic_read(&rdev->read_errors))
1794 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001796 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001797 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001798 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001801 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001802 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1803 printk_ratelimited(
1804 KERN_WARNING
1805 "md/raid:%s: read error on replacement device "
1806 "(sector %llu on %s).\n",
1807 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001808 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001809 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001810 else if (conf->mddev->degraded >= conf->max_degraded) {
1811 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001812 printk_ratelimited(
1813 KERN_WARNING
1814 "md/raid:%s: read error not correctable "
1815 "(sector %llu on %s).\n",
1816 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001817 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001818 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001819 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001820 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001821 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001822 printk_ratelimited(
1823 KERN_WARNING
1824 "md/raid:%s: read error NOT corrected!! "
1825 "(sector %llu on %s).\n",
1826 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001827 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001828 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001829 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001830 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001831 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001832 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001833 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001834 else
1835 retry = 1;
1836 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001837 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1838 set_bit(R5_ReadError, &sh->dev[i].flags);
1839 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1840 } else
1841 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001842 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001843 clear_bit(R5_ReadError, &sh->dev[i].flags);
1844 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001845 if (!(set_bad
1846 && test_bit(In_sync, &rdev->flags)
1847 && rdev_set_badblocks(
1848 rdev, sh->sector, STRIPE_SECTORS, 0)))
1849 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
NeilBrown14a75d32011-12-23 10:17:52 +11001852 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1854 set_bit(STRIPE_HANDLE, &sh->state);
1855 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
1857
NeilBrownd710e132008-10-13 11:55:12 +11001858static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
NeilBrown99c0fb52009-03-31 14:39:38 +11001860 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001861 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001862 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001863 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001865 sector_t first_bad;
1866 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001867 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
NeilBrown977df362011-12-23 10:17:53 +11001869 for (i = 0 ; i < disks; i++) {
1870 if (bi == &sh->dev[i].req) {
1871 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 break;
NeilBrown977df362011-12-23 10:17:53 +11001873 }
1874 if (bi == &sh->dev[i].rreq) {
1875 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001876 if (rdev)
1877 replacement = 1;
1878 else
1879 /* rdev was removed and 'replacement'
1880 * replaced it. rdev is not removed
1881 * until all requests are finished.
1882 */
1883 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001884 break;
1885 }
1886 }
Dan Williams45b42332007-07-09 11:56:43 -07001887 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1889 uptodate);
1890 if (i == disks) {
1891 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001892 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894
NeilBrown977df362011-12-23 10:17:53 +11001895 if (replacement) {
1896 if (!uptodate)
1897 md_error(conf->mddev, rdev);
1898 else if (is_badblock(rdev, sh->sector,
1899 STRIPE_SECTORS,
1900 &first_bad, &bad_sectors))
1901 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1902 } else {
1903 if (!uptodate) {
1904 set_bit(WriteErrorSeen, &rdev->flags);
1905 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001906 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1907 set_bit(MD_RECOVERY_NEEDED,
1908 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001909 } else if (is_badblock(rdev, sh->sector,
1910 STRIPE_SECTORS,
1911 &first_bad, &bad_sectors))
1912 set_bit(R5_MadeGood, &sh->dev[i].flags);
1913 }
1914 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
NeilBrown977df362011-12-23 10:17:53 +11001916 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1917 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001919 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920}
1921
NeilBrown784052e2009-03-31 15:19:07 +11001922static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
NeilBrown784052e2009-03-31 15:19:07 +11001924static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 struct r5dev *dev = &sh->dev[i];
1927
1928 bio_init(&dev->req);
1929 dev->req.bi_io_vec = &dev->vec;
1930 dev->req.bi_vcnt++;
1931 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001933 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
NeilBrown977df362011-12-23 10:17:53 +11001935 bio_init(&dev->rreq);
1936 dev->rreq.bi_io_vec = &dev->rvec;
1937 dev->rreq.bi_vcnt++;
1938 dev->rreq.bi_max_vecs++;
1939 dev->rreq.bi_private = sh;
1940 dev->rvec.bv_page = dev->page;
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001943 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
NeilBrownfd01b882011-10-11 16:47:53 +11001946static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001949 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001950 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001951 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
NeilBrown908f4fb2011-12-23 10:17:50 +11001953 spin_lock_irqsave(&conf->device_lock, flags);
1954 clear_bit(In_sync, &rdev->flags);
1955 mddev->degraded = calc_degraded(conf);
1956 spin_unlock_irqrestore(&conf->device_lock, flags);
1957 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1958
NeilBrownde393cd2011-07-28 11:31:48 +10001959 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001960 set_bit(Faulty, &rdev->flags);
1961 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1962 printk(KERN_ALERT
1963 "md/raid:%s: Disk failure on %s, disabling device.\n"
1964 "md/raid:%s: Operation continuing on %d devices.\n",
1965 mdname(mddev),
1966 bdevname(rdev->bdev, b),
1967 mdname(mddev),
1968 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001969}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971/*
1972 * Input: a 'big' sector number,
1973 * Output: index of the data and parity disk, and the sector # in them.
1974 */
NeilBrownd1688a62011-10-11 16:49:52 +11001975static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001976 int previous, int *dd_idx,
1977 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001979 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001980 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001982 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001983 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001985 int algorithm = previous ? conf->prev_algo
1986 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001987 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1988 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001989 int raid_disks = previous ? conf->previous_raid_disks
1990 : conf->raid_disks;
1991 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 /* First compute the information on this sector */
1994
1995 /*
1996 * Compute the chunk number and the sector offset inside the chunk
1997 */
1998 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1999 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 /*
2002 * Compute the stripe number
2003 */
NeilBrown35f2a592010-04-20 14:13:34 +10002004 stripe = chunk_number;
2005 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002006 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 /*
2008 * Select the parity disk based on the user selected algorithm.
2009 */
NeilBrown84789552011-07-27 11:00:36 +10002010 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002011 switch(conf->level) {
2012 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002013 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002014 break;
2015 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002016 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002018 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002019 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 (*dd_idx)++;
2021 break;
2022 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002023 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002024 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 (*dd_idx)++;
2026 break;
2027 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002028 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002029 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 break;
2031 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002032 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002033 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002035 case ALGORITHM_PARITY_0:
2036 pd_idx = 0;
2037 (*dd_idx)++;
2038 break;
2039 case ALGORITHM_PARITY_N:
2040 pd_idx = data_disks;
2041 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002043 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002044 }
2045 break;
2046 case 6:
2047
NeilBrowne183eae2009-03-31 15:20:22 +11002048 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002049 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002050 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002051 qd_idx = pd_idx + 1;
2052 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002053 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002054 qd_idx = 0;
2055 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002056 (*dd_idx) += 2; /* D D P Q D */
2057 break;
2058 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002059 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002060 qd_idx = pd_idx + 1;
2061 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002062 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002063 qd_idx = 0;
2064 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002065 (*dd_idx) += 2; /* D D P Q D */
2066 break;
2067 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002068 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002069 qd_idx = (pd_idx + 1) % raid_disks;
2070 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002071 break;
2072 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002073 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002074 qd_idx = (pd_idx + 1) % raid_disks;
2075 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002076 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002077
2078 case ALGORITHM_PARITY_0:
2079 pd_idx = 0;
2080 qd_idx = 1;
2081 (*dd_idx) += 2;
2082 break;
2083 case ALGORITHM_PARITY_N:
2084 pd_idx = data_disks;
2085 qd_idx = data_disks + 1;
2086 break;
2087
2088 case ALGORITHM_ROTATING_ZERO_RESTART:
2089 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2090 * of blocks for computing Q is different.
2091 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002092 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002093 qd_idx = pd_idx + 1;
2094 if (pd_idx == raid_disks-1) {
2095 (*dd_idx)++; /* Q D D D P */
2096 qd_idx = 0;
2097 } else if (*dd_idx >= pd_idx)
2098 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002099 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002100 break;
2101
2102 case ALGORITHM_ROTATING_N_RESTART:
2103 /* Same a left_asymmetric, by first stripe is
2104 * D D D P Q rather than
2105 * Q D D D P
2106 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002107 stripe2 += 1;
2108 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002109 qd_idx = pd_idx + 1;
2110 if (pd_idx == raid_disks-1) {
2111 (*dd_idx)++; /* Q D D D P */
2112 qd_idx = 0;
2113 } else if (*dd_idx >= pd_idx)
2114 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002115 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002116 break;
2117
2118 case ALGORITHM_ROTATING_N_CONTINUE:
2119 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002120 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002121 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2122 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002123 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002124 break;
2125
2126 case ALGORITHM_LEFT_ASYMMETRIC_6:
2127 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002128 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002129 if (*dd_idx >= pd_idx)
2130 (*dd_idx)++;
2131 qd_idx = raid_disks - 1;
2132 break;
2133
2134 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002135 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002136 if (*dd_idx >= pd_idx)
2137 (*dd_idx)++;
2138 qd_idx = raid_disks - 1;
2139 break;
2140
2141 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002142 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002143 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2144 qd_idx = raid_disks - 1;
2145 break;
2146
2147 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002148 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002149 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2150 qd_idx = raid_disks - 1;
2151 break;
2152
2153 case ALGORITHM_PARITY_0_6:
2154 pd_idx = 0;
2155 (*dd_idx)++;
2156 qd_idx = raid_disks - 1;
2157 break;
2158
NeilBrown16a53ec2006-06-26 00:27:38 -07002159 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002160 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002161 }
2162 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
2164
NeilBrown911d4ee2009-03-31 14:39:38 +11002165 if (sh) {
2166 sh->pd_idx = pd_idx;
2167 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002168 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /*
2171 * Finally, compute the new sector number
2172 */
2173 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2174 return new_sector;
2175}
2176
2177
NeilBrown784052e2009-03-31 15:19:07 +11002178static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
NeilBrownd1688a62011-10-11 16:49:52 +11002180 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002181 int raid_disks = sh->disks;
2182 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002184 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2185 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002186 int algorithm = previous ? conf->prev_algo
2187 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 sector_t stripe;
2189 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002190 sector_t chunk_number;
2191 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002193 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
NeilBrown16a53ec2006-06-26 00:27:38 -07002195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2197 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
NeilBrown16a53ec2006-06-26 00:27:38 -07002199 if (i == sh->pd_idx)
2200 return 0;
2201 switch(conf->level) {
2202 case 4: break;
2203 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002204 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 case ALGORITHM_LEFT_ASYMMETRIC:
2206 case ALGORITHM_RIGHT_ASYMMETRIC:
2207 if (i > sh->pd_idx)
2208 i--;
2209 break;
2210 case ALGORITHM_LEFT_SYMMETRIC:
2211 case ALGORITHM_RIGHT_SYMMETRIC:
2212 if (i < sh->pd_idx)
2213 i += raid_disks;
2214 i -= (sh->pd_idx + 1);
2215 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002216 case ALGORITHM_PARITY_0:
2217 i -= 1;
2218 break;
2219 case ALGORITHM_PARITY_N:
2220 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002222 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002223 }
2224 break;
2225 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002226 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002227 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002228 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002229 case ALGORITHM_LEFT_ASYMMETRIC:
2230 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002231 case ALGORITHM_ROTATING_ZERO_RESTART:
2232 case ALGORITHM_ROTATING_N_RESTART:
2233 if (sh->pd_idx == raid_disks-1)
2234 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002235 else if (i > sh->pd_idx)
2236 i -= 2; /* D D P Q D */
2237 break;
2238 case ALGORITHM_LEFT_SYMMETRIC:
2239 case ALGORITHM_RIGHT_SYMMETRIC:
2240 if (sh->pd_idx == raid_disks-1)
2241 i--; /* Q D D D P */
2242 else {
2243 /* D D P Q D */
2244 if (i < sh->pd_idx)
2245 i += raid_disks;
2246 i -= (sh->pd_idx + 2);
2247 }
2248 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002249 case ALGORITHM_PARITY_0:
2250 i -= 2;
2251 break;
2252 case ALGORITHM_PARITY_N:
2253 break;
2254 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002255 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002256 if (sh->pd_idx == 0)
2257 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002258 else {
2259 /* D D Q P D */
2260 if (i < sh->pd_idx)
2261 i += raid_disks;
2262 i -= (sh->pd_idx + 1);
2263 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002264 break;
2265 case ALGORITHM_LEFT_ASYMMETRIC_6:
2266 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2267 if (i > sh->pd_idx)
2268 i--;
2269 break;
2270 case ALGORITHM_LEFT_SYMMETRIC_6:
2271 case ALGORITHM_RIGHT_SYMMETRIC_6:
2272 if (i < sh->pd_idx)
2273 i += data_disks + 1;
2274 i -= (sh->pd_idx + 1);
2275 break;
2276 case ALGORITHM_PARITY_0_6:
2277 i -= 1;
2278 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002279 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002280 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002281 }
2282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
2284
2285 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002286 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
NeilBrown112bf892009-03-31 14:39:38 +11002288 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002289 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002290 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2291 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002292 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2293 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 return 0;
2295 }
2296 return r_sector;
2297}
2298
2299
Dan Williams600aa102008-06-28 08:32:05 +10002300static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002301schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002302 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002303{
2304 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002305 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002306 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002307
Dan Williamse33129d2007-01-02 13:52:30 -07002308 if (rcw) {
2309 /* if we are not expanding this is a proper write request, and
2310 * there will be bios with new data to be drained into the
2311 * stripe cache
2312 */
2313 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002314 sh->reconstruct_state = reconstruct_state_drain_run;
2315 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2316 } else
2317 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002318
Dan Williamsac6b53b2009-07-14 13:40:19 -07002319 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002320
2321 for (i = disks; i--; ) {
2322 struct r5dev *dev = &sh->dev[i];
2323
2324 if (dev->towrite) {
2325 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002326 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002327 if (!expand)
2328 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002329 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002330 }
2331 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002332 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002333 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002334 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002335 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002336 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002337 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2338 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2339
Dan Williamsd8ee0722008-06-28 08:32:06 +10002340 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002341 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2342 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002343 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002344
2345 for (i = disks; i--; ) {
2346 struct r5dev *dev = &sh->dev[i];
2347 if (i == pd_idx)
2348 continue;
2349
Dan Williamse33129d2007-01-02 13:52:30 -07002350 if (dev->towrite &&
2351 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002352 test_bit(R5_Wantcompute, &dev->flags))) {
2353 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002354 set_bit(R5_LOCKED, &dev->flags);
2355 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002356 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002357 }
2358 }
2359 }
2360
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002361 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002362 * are in flight
2363 */
2364 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2365 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002366 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002367
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002368 if (level == 6) {
2369 int qd_idx = sh->qd_idx;
2370 struct r5dev *dev = &sh->dev[qd_idx];
2371
2372 set_bit(R5_LOCKED, &dev->flags);
2373 clear_bit(R5_UPTODATE, &dev->flags);
2374 s->locked++;
2375 }
2376
Dan Williams600aa102008-06-28 08:32:05 +10002377 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002378 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002379 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002380}
NeilBrown16a53ec2006-06-26 00:27:38 -07002381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382/*
2383 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002384 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 * The bi_next chain must be in order.
2386 */
2387static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2388{
2389 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002390 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002391 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
NeilBrowncbe47ec2011-07-26 11:20:35 +10002393 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 (unsigned long long)bi->bi_sector,
2395 (unsigned long long)sh->sector);
2396
Shaohua Lib17459c2012-07-19 16:01:31 +10002397 /*
2398 * If several bio share a stripe. The bio bi_phys_segments acts as a
2399 * reference count to avoid race. The reference count should already be
2400 * increased before this function is called (for example, in
2401 * make_request()), so other bio sharing this stripe will not free the
2402 * stripe. If a stripe is owned by one stripe, the stripe lock will
2403 * protect it.
2404 */
2405 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002406 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002408 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002409 firstwrite = 1;
2410 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 bip = &sh->dev[dd_idx].toread;
2412 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2413 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2414 goto overlap;
2415 bip = & (*bip)->bi_next;
2416 }
2417 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2418 goto overlap;
2419
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002420 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 if (*bip)
2422 bi->bi_next = *bip;
2423 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002424 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (forwrite) {
2427 /* check if page is covered */
2428 sector_t sector = sh->dev[dd_idx].sector;
2429 for (bi=sh->dev[dd_idx].towrite;
2430 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2431 bi && bi->bi_sector <= sector;
2432 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2433 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2434 sector = bi->bi_sector + (bi->bi_size>>9);
2435 }
2436 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2437 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2438 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002439
2440 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2441 (unsigned long long)(*bip)->bi_sector,
2442 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002443 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002444
2445 if (conf->mddev->bitmap && firstwrite) {
2446 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2447 STRIPE_SECTORS, 0);
2448 sh->bm_seq = conf->seq_flush+1;
2449 set_bit(STRIPE_BIT_DELAY, &sh->state);
2450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 return 1;
2452
2453 overlap:
2454 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002455 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return 0;
2457}
2458
NeilBrownd1688a62011-10-11 16:49:52 +11002459static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002460
NeilBrownd1688a62011-10-11 16:49:52 +11002461static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002462 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002463{
NeilBrown784052e2009-03-31 15:19:07 +11002464 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002465 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002466 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002467 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002468 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002469
NeilBrown112bf892009-03-31 14:39:38 +11002470 raid5_compute_sector(conf,
2471 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002472 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002473 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002474 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002475}
2476
Dan Williamsa4456852007-07-09 11:56:43 -07002477static void
NeilBrownd1688a62011-10-11 16:49:52 +11002478handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002479 struct stripe_head_state *s, int disks,
2480 struct bio **return_bi)
2481{
2482 int i;
2483 for (i = disks; i--; ) {
2484 struct bio *bi;
2485 int bitmap_end = 0;
2486
2487 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002488 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002489 rcu_read_lock();
2490 rdev = rcu_dereference(conf->disks[i].rdev);
2491 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002492 atomic_inc(&rdev->nr_pending);
2493 else
2494 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002495 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002496 if (rdev) {
2497 if (!rdev_set_badblocks(
2498 rdev,
2499 sh->sector,
2500 STRIPE_SECTORS, 0))
2501 md_error(conf->mddev, rdev);
2502 rdev_dec_pending(rdev, conf->mddev);
2503 }
Dan Williamsa4456852007-07-09 11:56:43 -07002504 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002505 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002506 /* fail all writes first */
2507 bi = sh->dev[i].towrite;
2508 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002509 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002510 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002511 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002512
2513 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2514 wake_up(&conf->wait_for_overlap);
2515
2516 while (bi && bi->bi_sector <
2517 sh->dev[i].sector + STRIPE_SECTORS) {
2518 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2519 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002520 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002521 md_write_end(conf->mddev);
2522 bi->bi_next = *return_bi;
2523 *return_bi = bi;
2524 }
2525 bi = nextbi;
2526 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002527 if (bitmap_end)
2528 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2529 STRIPE_SECTORS, 0, 0);
2530 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002531 /* and fail all 'written' */
2532 bi = sh->dev[i].written;
2533 sh->dev[i].written = NULL;
2534 if (bi) bitmap_end = 1;
2535 while (bi && bi->bi_sector <
2536 sh->dev[i].sector + STRIPE_SECTORS) {
2537 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2538 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002539 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002540 md_write_end(conf->mddev);
2541 bi->bi_next = *return_bi;
2542 *return_bi = bi;
2543 }
2544 bi = bi2;
2545 }
2546
Dan Williamsb5e98d62007-01-02 13:52:31 -07002547 /* fail any reads if this device is non-operational and
2548 * the data has not reached the cache yet.
2549 */
2550 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2551 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2552 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002553 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002554 bi = sh->dev[i].toread;
2555 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002556 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002557 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2558 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002559 while (bi && bi->bi_sector <
2560 sh->dev[i].sector + STRIPE_SECTORS) {
2561 struct bio *nextbi =
2562 r5_next_bio(bi, sh->dev[i].sector);
2563 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002564 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002565 bi->bi_next = *return_bi;
2566 *return_bi = bi;
2567 }
2568 bi = nextbi;
2569 }
2570 }
Dan Williamsa4456852007-07-09 11:56:43 -07002571 if (bitmap_end)
2572 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2573 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002574 /* If we were in the middle of a write the parity block might
2575 * still be locked - so just clear all R5_LOCKED flags
2576 */
2577 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002578 }
2579
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002580 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2581 if (atomic_dec_and_test(&conf->pending_full_writes))
2582 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002583}
2584
NeilBrown7f0da592011-07-28 11:39:22 +10002585static void
NeilBrownd1688a62011-10-11 16:49:52 +11002586handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002587 struct stripe_head_state *s)
2588{
2589 int abort = 0;
2590 int i;
2591
NeilBrown7f0da592011-07-28 11:39:22 +10002592 clear_bit(STRIPE_SYNCING, &sh->state);
2593 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002594 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002595 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002596 * Don't even need to abort as that is handled elsewhere
2597 * if needed, and not always wanted e.g. if there is a known
2598 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002599 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002600 * non-sync devices, or abort the recovery
2601 */
NeilBrown18b98372012-04-01 23:48:38 +10002602 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2603 /* During recovery devices cannot be removed, so
2604 * locking and refcounting of rdevs is not needed
2605 */
2606 for (i = 0; i < conf->raid_disks; i++) {
2607 struct md_rdev *rdev = conf->disks[i].rdev;
2608 if (rdev
2609 && !test_bit(Faulty, &rdev->flags)
2610 && !test_bit(In_sync, &rdev->flags)
2611 && !rdev_set_badblocks(rdev, sh->sector,
2612 STRIPE_SECTORS, 0))
2613 abort = 1;
2614 rdev = conf->disks[i].replacement;
2615 if (rdev
2616 && !test_bit(Faulty, &rdev->flags)
2617 && !test_bit(In_sync, &rdev->flags)
2618 && !rdev_set_badblocks(rdev, sh->sector,
2619 STRIPE_SECTORS, 0))
2620 abort = 1;
2621 }
2622 if (abort)
2623 conf->recovery_disabled =
2624 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002625 }
NeilBrown18b98372012-04-01 23:48:38 +10002626 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002627}
2628
NeilBrown9a3e1102011-12-23 10:17:53 +11002629static int want_replace(struct stripe_head *sh, int disk_idx)
2630{
2631 struct md_rdev *rdev;
2632 int rv = 0;
2633 /* Doing recovery so rcu locking not required */
2634 rdev = sh->raid_conf->disks[disk_idx].replacement;
2635 if (rdev
2636 && !test_bit(Faulty, &rdev->flags)
2637 && !test_bit(In_sync, &rdev->flags)
2638 && (rdev->recovery_offset <= sh->sector
2639 || rdev->mddev->recovery_cp <= sh->sector))
2640 rv = 1;
2641
2642 return rv;
2643}
2644
NeilBrown93b3dbc2011-07-27 11:00:36 +10002645/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002646 * to be read or computed to satisfy a request.
2647 *
2648 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002649 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002650 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002651static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2652 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002653{
2654 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002655 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2656 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002657
Dan Williamsf38e1212007-01-02 13:52:30 -07002658 /* is the data in this block needed, and can we get it? */
2659 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002660 !test_bit(R5_UPTODATE, &dev->flags) &&
2661 (dev->toread ||
2662 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2663 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002664 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002665 (s->failed >= 1 && fdev[0]->toread) ||
2666 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002667 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2668 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2669 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002670 /* we would like to get this block, possibly by computing it,
2671 * otherwise read it if the backing disk is insync
2672 */
2673 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2674 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2675 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002676 (s->failed && (disk_idx == s->failed_num[0] ||
2677 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002678 /* have disk failed, and we're requested to fetch it;
2679 * do compute it
2680 */
2681 pr_debug("Computing stripe %llu block %d\n",
2682 (unsigned long long)sh->sector, disk_idx);
2683 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2684 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2685 set_bit(R5_Wantcompute, &dev->flags);
2686 sh->ops.target = disk_idx;
2687 sh->ops.target2 = -1; /* no 2nd target */
2688 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002689 /* Careful: from this point on 'uptodate' is in the eye
2690 * of raid_run_ops which services 'compute' operations
2691 * before writes. R5_Wantcompute flags a block that will
2692 * be R5_UPTODATE by the time it is needed for a
2693 * subsequent operation.
2694 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002695 s->uptodate++;
2696 return 1;
2697 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2698 /* Computing 2-failure is *very* expensive; only
2699 * do it if failed >= 2
2700 */
2701 int other;
2702 for (other = disks; other--; ) {
2703 if (other == disk_idx)
2704 continue;
2705 if (!test_bit(R5_UPTODATE,
2706 &sh->dev[other].flags))
2707 break;
2708 }
2709 BUG_ON(other < 0);
2710 pr_debug("Computing stripe %llu blocks %d,%d\n",
2711 (unsigned long long)sh->sector,
2712 disk_idx, other);
2713 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2714 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2715 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2716 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2717 sh->ops.target = disk_idx;
2718 sh->ops.target2 = other;
2719 s->uptodate += 2;
2720 s->req_compute = 1;
2721 return 1;
2722 } else if (test_bit(R5_Insync, &dev->flags)) {
2723 set_bit(R5_LOCKED, &dev->flags);
2724 set_bit(R5_Wantread, &dev->flags);
2725 s->locked++;
2726 pr_debug("Reading block %d (sync=%d)\n",
2727 disk_idx, s->syncing);
2728 }
2729 }
2730
2731 return 0;
2732}
2733
2734/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002735 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002736 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002737static void handle_stripe_fill(struct stripe_head *sh,
2738 struct stripe_head_state *s,
2739 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002740{
2741 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002742
2743 /* look for blocks to read/compute, skip this if a compute
2744 * is already in flight, or if the stripe contents are in the
2745 * midst of changing due to a write
2746 */
2747 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2748 !sh->reconstruct_state)
2749 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002750 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002751 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002752 set_bit(STRIPE_HANDLE, &sh->state);
2753}
2754
2755
Dan Williams1fe797e2008-06-28 09:16:30 +10002756/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002757 * any written block on an uptodate or failed drive can be returned.
2758 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2759 * never LOCKED, so we don't need to test 'failed' directly.
2760 */
NeilBrownd1688a62011-10-11 16:49:52 +11002761static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002762 struct stripe_head *sh, int disks, struct bio **return_bi)
2763{
2764 int i;
2765 struct r5dev *dev;
2766
2767 for (i = disks; i--; )
2768 if (sh->dev[i].written) {
2769 dev = &sh->dev[i];
2770 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11002771 (test_bit(R5_UPTODATE, &dev->flags) ||
2772 test_and_clear_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002773 /* We can return any write requests */
2774 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002775 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002776 wbi = dev->written;
2777 dev->written = NULL;
2778 while (wbi && wbi->bi_sector <
2779 dev->sector + STRIPE_SECTORS) {
2780 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002781 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002782 md_write_end(conf->mddev);
2783 wbi->bi_next = *return_bi;
2784 *return_bi = wbi;
2785 }
2786 wbi = wbi2;
2787 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002788 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2789 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002790 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002791 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002792 }
2793 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002794
2795 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2796 if (atomic_dec_and_test(&conf->pending_full_writes))
2797 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002798}
2799
NeilBrownd1688a62011-10-11 16:49:52 +11002800static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002801 struct stripe_head *sh,
2802 struct stripe_head_state *s,
2803 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002804{
2805 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002806 sector_t recovery_cp = conf->mddev->recovery_cp;
2807
2808 /* RAID6 requires 'rcw' in current implementation.
2809 * Otherwise, check whether resync is now happening or should start.
2810 * If yes, then the array is dirty (after unclean shutdown or
2811 * initial creation), so parity in some stripes might be inconsistent.
2812 * In this case, we need to always do reconstruct-write, to ensure
2813 * that in case of drive failure or read-error correction, we
2814 * generate correct data from the parity.
2815 */
2816 if (conf->max_degraded == 2 ||
2817 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2818 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002819 * look like rcw is cheaper
2820 */
2821 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002822 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2823 conf->max_degraded, (unsigned long long)recovery_cp,
2824 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002825 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002826 /* would I have to read this buffer for read_modify_write */
2827 struct r5dev *dev = &sh->dev[i];
2828 if ((dev->towrite || i == sh->pd_idx) &&
2829 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002830 !(test_bit(R5_UPTODATE, &dev->flags) ||
2831 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002832 if (test_bit(R5_Insync, &dev->flags))
2833 rmw++;
2834 else
2835 rmw += 2*disks; /* cannot read it */
2836 }
2837 /* Would I have to read this buffer for reconstruct_write */
2838 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2839 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002840 !(test_bit(R5_UPTODATE, &dev->flags) ||
2841 test_bit(R5_Wantcompute, &dev->flags))) {
2842 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002843 else
2844 rcw += 2*disks;
2845 }
2846 }
Dan Williams45b42332007-07-09 11:56:43 -07002847 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002848 (unsigned long long)sh->sector, rmw, rcw);
2849 set_bit(STRIPE_HANDLE, &sh->state);
2850 if (rmw < rcw && rmw > 0)
2851 /* prefer read-modify-write, but need to get some data */
2852 for (i = disks; i--; ) {
2853 struct r5dev *dev = &sh->dev[i];
2854 if ((dev->towrite || i == sh->pd_idx) &&
2855 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002856 !(test_bit(R5_UPTODATE, &dev->flags) ||
2857 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002858 test_bit(R5_Insync, &dev->flags)) {
2859 if (
2860 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002861 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002862 "%d for r-m-w\n", i);
2863 set_bit(R5_LOCKED, &dev->flags);
2864 set_bit(R5_Wantread, &dev->flags);
2865 s->locked++;
2866 } else {
2867 set_bit(STRIPE_DELAYED, &sh->state);
2868 set_bit(STRIPE_HANDLE, &sh->state);
2869 }
2870 }
2871 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002872 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002873 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002874 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002875 for (i = disks; i--; ) {
2876 struct r5dev *dev = &sh->dev[i];
2877 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002878 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002879 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002880 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002881 test_bit(R5_Wantcompute, &dev->flags))) {
2882 rcw++;
2883 if (!test_bit(R5_Insync, &dev->flags))
2884 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002885 if (
2886 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002887 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002888 "%d for Reconstruct\n", i);
2889 set_bit(R5_LOCKED, &dev->flags);
2890 set_bit(R5_Wantread, &dev->flags);
2891 s->locked++;
2892 } else {
2893 set_bit(STRIPE_DELAYED, &sh->state);
2894 set_bit(STRIPE_HANDLE, &sh->state);
2895 }
2896 }
2897 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002898 }
Dan Williamsa4456852007-07-09 11:56:43 -07002899 /* now if nothing is locked, and if we have enough data,
2900 * we can start a write request
2901 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002902 /* since handle_stripe can be called at any time we need to handle the
2903 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002904 * subsequent call wants to start a write request. raid_run_ops only
2905 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002906 * simultaneously. If this is not the case then new writes need to be
2907 * held off until the compute completes.
2908 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002909 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2910 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2911 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002912 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002913}
2914
NeilBrownd1688a62011-10-11 16:49:52 +11002915static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002916 struct stripe_head_state *s, int disks)
2917{
Dan Williamsecc65c92008-06-28 08:31:57 +10002918 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002919
Dan Williamsbd2ab672008-04-10 21:29:27 -07002920 set_bit(STRIPE_HANDLE, &sh->state);
2921
Dan Williamsecc65c92008-06-28 08:31:57 +10002922 switch (sh->check_state) {
2923 case check_state_idle:
2924 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002925 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002926 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002927 sh->check_state = check_state_run;
2928 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002929 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002930 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002931 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002932 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002933 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002934 /* fall through */
2935 case check_state_compute_result:
2936 sh->check_state = check_state_idle;
2937 if (!dev)
2938 dev = &sh->dev[sh->pd_idx];
2939
2940 /* check that a write has not made the stripe insync */
2941 if (test_bit(STRIPE_INSYNC, &sh->state))
2942 break;
2943
2944 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002945 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2946 BUG_ON(s->uptodate != disks);
2947
2948 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002949 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002950 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002951
Dan Williamsa4456852007-07-09 11:56:43 -07002952 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002953 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002954 break;
2955 case check_state_run:
2956 break; /* we will be called again upon completion */
2957 case check_state_check_result:
2958 sh->check_state = check_state_idle;
2959
2960 /* if a failure occurred during the check operation, leave
2961 * STRIPE_INSYNC not set and let the stripe be handled again
2962 */
2963 if (s->failed)
2964 break;
2965
2966 /* handle a successful check operation, if parity is correct
2967 * we are done. Otherwise update the mismatch count and repair
2968 * parity if !MD_RECOVERY_CHECK
2969 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002970 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002971 /* parity is correct (on disc,
2972 * not in buffer any more)
2973 */
2974 set_bit(STRIPE_INSYNC, &sh->state);
2975 else {
2976 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2977 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2978 /* don't try to repair!! */
2979 set_bit(STRIPE_INSYNC, &sh->state);
2980 else {
2981 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002982 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002983 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2984 set_bit(R5_Wantcompute,
2985 &sh->dev[sh->pd_idx].flags);
2986 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002987 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002988 s->uptodate++;
2989 }
2990 }
2991 break;
2992 case check_state_compute_run:
2993 break;
2994 default:
2995 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2996 __func__, sh->check_state,
2997 (unsigned long long) sh->sector);
2998 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07002999 }
3000}
3001
3002
NeilBrownd1688a62011-10-11 16:49:52 +11003003static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003004 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003005 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003006{
Dan Williamsa4456852007-07-09 11:56:43 -07003007 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003008 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003009 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003010
3011 set_bit(STRIPE_HANDLE, &sh->state);
3012
3013 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003014
Dan Williamsa4456852007-07-09 11:56:43 -07003015 /* Want to check and possibly repair P and Q.
3016 * However there could be one 'failed' device, in which
3017 * case we can only check one of them, possibly using the
3018 * other to generate missing data
3019 */
3020
Dan Williamsd82dfee2009-07-14 13:40:57 -07003021 switch (sh->check_state) {
3022 case check_state_idle:
3023 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003024 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003025 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003026 * makes sense to check P (If anything else were failed,
3027 * we would have used P to recreate it).
3028 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003029 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003030 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003031 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003032 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003033 * anything, so it makes sense to check it
3034 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003035 if (sh->check_state == check_state_run)
3036 sh->check_state = check_state_run_pq;
3037 else
3038 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003039 }
Dan Williams36d1c642009-07-14 11:48:22 -07003040
Dan Williamsd82dfee2009-07-14 13:40:57 -07003041 /* discard potentially stale zero_sum_result */
3042 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003043
Dan Williamsd82dfee2009-07-14 13:40:57 -07003044 if (sh->check_state == check_state_run) {
3045 /* async_xor_zero_sum destroys the contents of P */
3046 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3047 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003048 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003049 if (sh->check_state >= check_state_run &&
3050 sh->check_state <= check_state_run_pq) {
3051 /* async_syndrome_zero_sum preserves P and Q, so
3052 * no need to mark them !uptodate here
3053 */
3054 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3055 break;
3056 }
Dan Williams36d1c642009-07-14 11:48:22 -07003057
Dan Williamsd82dfee2009-07-14 13:40:57 -07003058 /* we have 2-disk failure */
3059 BUG_ON(s->failed != 2);
3060 /* fall through */
3061 case check_state_compute_result:
3062 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003063
Dan Williamsd82dfee2009-07-14 13:40:57 -07003064 /* check that a write has not made the stripe insync */
3065 if (test_bit(STRIPE_INSYNC, &sh->state))
3066 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003067
3068 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003069 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003070 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003071 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003072 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003073 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003074 s->locked++;
3075 set_bit(R5_LOCKED, &dev->flags);
3076 set_bit(R5_Wantwrite, &dev->flags);
3077 }
3078 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003079 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003080 s->locked++;
3081 set_bit(R5_LOCKED, &dev->flags);
3082 set_bit(R5_Wantwrite, &dev->flags);
3083 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003084 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003085 dev = &sh->dev[pd_idx];
3086 s->locked++;
3087 set_bit(R5_LOCKED, &dev->flags);
3088 set_bit(R5_Wantwrite, &dev->flags);
3089 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003090 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003091 dev = &sh->dev[qd_idx];
3092 s->locked++;
3093 set_bit(R5_LOCKED, &dev->flags);
3094 set_bit(R5_Wantwrite, &dev->flags);
3095 }
3096 clear_bit(STRIPE_DEGRADED, &sh->state);
3097
3098 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003099 break;
3100 case check_state_run:
3101 case check_state_run_q:
3102 case check_state_run_pq:
3103 break; /* we will be called again upon completion */
3104 case check_state_check_result:
3105 sh->check_state = check_state_idle;
3106
3107 /* handle a successful check operation, if parity is correct
3108 * we are done. Otherwise update the mismatch count and repair
3109 * parity if !MD_RECOVERY_CHECK
3110 */
3111 if (sh->ops.zero_sum_result == 0) {
3112 /* both parities are correct */
3113 if (!s->failed)
3114 set_bit(STRIPE_INSYNC, &sh->state);
3115 else {
3116 /* in contrast to the raid5 case we can validate
3117 * parity, but still have a failure to write
3118 * back
3119 */
3120 sh->check_state = check_state_compute_result;
3121 /* Returning at this point means that we may go
3122 * off and bring p and/or q uptodate again so
3123 * we make sure to check zero_sum_result again
3124 * to verify if p or q need writeback
3125 */
3126 }
3127 } else {
3128 conf->mddev->resync_mismatches += STRIPE_SECTORS;
3129 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3130 /* don't try to repair!! */
3131 set_bit(STRIPE_INSYNC, &sh->state);
3132 else {
3133 int *target = &sh->ops.target;
3134
3135 sh->ops.target = -1;
3136 sh->ops.target2 = -1;
3137 sh->check_state = check_state_compute_run;
3138 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3139 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3140 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3141 set_bit(R5_Wantcompute,
3142 &sh->dev[pd_idx].flags);
3143 *target = pd_idx;
3144 target = &sh->ops.target2;
3145 s->uptodate++;
3146 }
3147 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3148 set_bit(R5_Wantcompute,
3149 &sh->dev[qd_idx].flags);
3150 *target = qd_idx;
3151 s->uptodate++;
3152 }
3153 }
3154 }
3155 break;
3156 case check_state_compute_run:
3157 break;
3158 default:
3159 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3160 __func__, sh->check_state,
3161 (unsigned long long) sh->sector);
3162 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003163 }
3164}
3165
NeilBrownd1688a62011-10-11 16:49:52 +11003166static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003167{
3168 int i;
3169
3170 /* We have read all the blocks in this stripe and now we need to
3171 * copy some of them into a target stripe for expand.
3172 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003173 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003174 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3175 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003176 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003177 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003178 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003179 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003180
NeilBrown784052e2009-03-31 15:19:07 +11003181 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003182 sector_t s = raid5_compute_sector(conf, bn, 0,
3183 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003184 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003185 if (sh2 == NULL)
3186 /* so far only the early blocks of this stripe
3187 * have been requested. When later blocks
3188 * get requested, we will try again
3189 */
3190 continue;
3191 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3192 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3193 /* must have already done this block */
3194 release_stripe(sh2);
3195 continue;
3196 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003197
3198 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003199 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003200 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003201 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003202 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003203
Dan Williamsa4456852007-07-09 11:56:43 -07003204 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3205 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3206 for (j = 0; j < conf->raid_disks; j++)
3207 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003208 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003209 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3210 break;
3211 if (j == conf->raid_disks) {
3212 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3213 set_bit(STRIPE_HANDLE, &sh2->state);
3214 }
3215 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003216
Dan Williamsa4456852007-07-09 11:56:43 -07003217 }
NeilBrowna2e08552007-09-11 15:23:36 -07003218 /* done submitting copies, wait for them to complete */
3219 if (tx) {
3220 async_tx_ack(tx);
3221 dma_wait_for_async_tx(tx);
3222 }
Dan Williamsa4456852007-07-09 11:56:43 -07003223}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224
3225/*
3226 * handle_stripe - do things to a stripe.
3227 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003228 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3229 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003231 * return some read requests which now have data
3232 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 * schedule a read on some buffers
3234 * schedule a write of some buffers
3235 * return confirmation of parity correctness
3236 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 */
Dan Williamsa4456852007-07-09 11:56:43 -07003238
NeilBrownacfe7262011-07-27 11:00:36 +10003239static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003240{
NeilBrownd1688a62011-10-11 16:49:52 +11003241 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003242 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003243 struct r5dev *dev;
3244 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003245 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003246
NeilBrownacfe7262011-07-27 11:00:36 +10003247 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003248
NeilBrownacfe7262011-07-27 11:00:36 +10003249 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3250 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3251 s->failed_num[0] = -1;
3252 s->failed_num[1] = -1;
3253
3254 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003255 rcu_read_lock();
3256 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003257 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003258 sector_t first_bad;
3259 int bad_sectors;
3260 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003261
NeilBrown16a53ec2006-06-26 00:27:38 -07003262 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003263
Dan Williams45b42332007-07-09 11:56:43 -07003264 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003265 i, dev->flags,
3266 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003267 /* maybe we can reply to a read
3268 *
3269 * new wantfill requests are only permitted while
3270 * ops_complete_biofill is guaranteed to be inactive
3271 */
3272 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3273 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3274 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003275
3276 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003277 if (test_bit(R5_LOCKED, &dev->flags))
3278 s->locked++;
3279 if (test_bit(R5_UPTODATE, &dev->flags))
3280 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003281 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003282 s->compute++;
3283 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003284 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003285
NeilBrownacfe7262011-07-27 11:00:36 +10003286 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003287 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003288 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003289 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003290 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003291 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003292 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003293 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003294 }
Dan Williamsa4456852007-07-09 11:56:43 -07003295 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003296 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003297 /* Prefer to use the replacement for reads, but only
3298 * if it is recovered enough and has no bad blocks.
3299 */
3300 rdev = rcu_dereference(conf->disks[i].replacement);
3301 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3302 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3303 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3304 &first_bad, &bad_sectors))
3305 set_bit(R5_ReadRepl, &dev->flags);
3306 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003307 if (rdev)
3308 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003309 rdev = rcu_dereference(conf->disks[i].rdev);
3310 clear_bit(R5_ReadRepl, &dev->flags);
3311 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003312 if (rdev && test_bit(Faulty, &rdev->flags))
3313 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003314 if (rdev) {
3315 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3316 &first_bad, &bad_sectors);
3317 if (s->blocked_rdev == NULL
3318 && (test_bit(Blocked, &rdev->flags)
3319 || is_bad < 0)) {
3320 if (is_bad < 0)
3321 set_bit(BlockedBadBlocks,
3322 &rdev->flags);
3323 s->blocked_rdev = rdev;
3324 atomic_inc(&rdev->nr_pending);
3325 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003326 }
NeilBrown415e72d2010-06-17 17:25:21 +10003327 clear_bit(R5_Insync, &dev->flags);
3328 if (!rdev)
3329 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003330 else if (is_bad) {
3331 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003332 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3333 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003334 /* treat as in-sync, but with a read error
3335 * which we can now try to correct
3336 */
3337 set_bit(R5_Insync, &dev->flags);
3338 set_bit(R5_ReadError, &dev->flags);
3339 }
3340 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003341 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003342 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003343 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003344 set_bit(R5_Insync, &dev->flags);
3345 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3346 test_bit(R5_Expanded, &dev->flags))
3347 /* If we've reshaped into here, we assume it is Insync.
3348 * We will shortly update recovery_offset to make
3349 * it official.
3350 */
3351 set_bit(R5_Insync, &dev->flags);
3352
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003353 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003354 /* This flag does not apply to '.replacement'
3355 * only to .rdev, so make sure to check that*/
3356 struct md_rdev *rdev2 = rcu_dereference(
3357 conf->disks[i].rdev);
3358 if (rdev2 == rdev)
3359 clear_bit(R5_Insync, &dev->flags);
3360 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003361 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003362 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003363 } else
3364 clear_bit(R5_WriteError, &dev->flags);
3365 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003366 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003367 /* This flag does not apply to '.replacement'
3368 * only to .rdev, so make sure to check that*/
3369 struct md_rdev *rdev2 = rcu_dereference(
3370 conf->disks[i].rdev);
3371 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003372 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003373 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003374 } else
3375 clear_bit(R5_MadeGood, &dev->flags);
3376 }
NeilBrown977df362011-12-23 10:17:53 +11003377 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3378 struct md_rdev *rdev2 = rcu_dereference(
3379 conf->disks[i].replacement);
3380 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3381 s->handle_bad_blocks = 1;
3382 atomic_inc(&rdev2->nr_pending);
3383 } else
3384 clear_bit(R5_MadeGoodRepl, &dev->flags);
3385 }
NeilBrown415e72d2010-06-17 17:25:21 +10003386 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003387 /* The ReadError flag will just be confusing now */
3388 clear_bit(R5_ReadError, &dev->flags);
3389 clear_bit(R5_ReWrite, &dev->flags);
3390 }
NeilBrown415e72d2010-06-17 17:25:21 +10003391 if (test_bit(R5_ReadError, &dev->flags))
3392 clear_bit(R5_Insync, &dev->flags);
3393 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003394 if (s->failed < 2)
3395 s->failed_num[s->failed] = i;
3396 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003397 if (rdev && !test_bit(Faulty, &rdev->flags))
3398 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003399 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003400 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003401 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3402 /* If there is a failed device being replaced,
3403 * we must be recovering.
3404 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003405 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003406 * else we can only be replacing
3407 * sync and recovery both need to read all devices, and so
3408 * use the same flag.
3409 */
3410 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003411 sh->sector >= conf->mddev->recovery_cp ||
3412 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003413 s->syncing = 1;
3414 else
3415 s->replacing = 1;
3416 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003417 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003418}
NeilBrownf4168852007-02-28 20:11:53 -08003419
NeilBrowncc940152011-07-26 11:35:35 +10003420static void handle_stripe(struct stripe_head *sh)
3421{
3422 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003423 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003424 int i;
NeilBrown84789552011-07-27 11:00:36 +10003425 int prexor;
3426 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003427 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003428
3429 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003430 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003431 /* already being handled, ensure it gets handled
3432 * again when current action finishes */
3433 set_bit(STRIPE_HANDLE, &sh->state);
3434 return;
3435 }
3436
3437 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3438 set_bit(STRIPE_SYNCING, &sh->state);
3439 clear_bit(STRIPE_INSYNC, &sh->state);
3440 }
3441 clear_bit(STRIPE_DELAYED, &sh->state);
3442
3443 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3444 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3445 (unsigned long long)sh->sector, sh->state,
3446 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3447 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003448
NeilBrownacfe7262011-07-27 11:00:36 +10003449 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003450
NeilBrownbc2607f2011-07-28 11:39:22 +10003451 if (s.handle_bad_blocks) {
3452 set_bit(STRIPE_HANDLE, &sh->state);
3453 goto finish;
3454 }
3455
NeilBrown474af965fe2011-07-27 11:00:36 +10003456 if (unlikely(s.blocked_rdev)) {
3457 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003458 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003459 set_bit(STRIPE_HANDLE, &sh->state);
3460 goto finish;
3461 }
3462 /* There is nothing for the blocked_rdev to block */
3463 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3464 s.blocked_rdev = NULL;
3465 }
3466
3467 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3468 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3469 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3470 }
3471
3472 pr_debug("locked=%d uptodate=%d to_read=%d"
3473 " to_write=%d failed=%d failed_num=%d,%d\n",
3474 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3475 s.failed_num[0], s.failed_num[1]);
3476 /* check if the array has lost more than max_degraded devices and,
3477 * if so, some requests might need to be failed.
3478 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003479 if (s.failed > conf->max_degraded) {
3480 sh->check_state = 0;
3481 sh->reconstruct_state = 0;
3482 if (s.to_read+s.to_write+s.written)
3483 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003484 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003485 handle_failed_sync(conf, sh, &s);
3486 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003487
3488 /*
3489 * might be able to return some write requests if the parity blocks
3490 * are safe, or on a failed drive
3491 */
3492 pdev = &sh->dev[sh->pd_idx];
3493 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3494 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3495 qdev = &sh->dev[sh->qd_idx];
3496 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3497 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3498 || conf->level < 6;
3499
3500 if (s.written &&
3501 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3502 && !test_bit(R5_LOCKED, &pdev->flags)
Shaohua Li9e4447682012-10-11 13:49:49 +11003503 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3504 test_bit(R5_Discard, &pdev->flags))))) &&
NeilBrown474af965fe2011-07-27 11:00:36 +10003505 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3506 && !test_bit(R5_LOCKED, &qdev->flags)
Shaohua Li9e4447682012-10-11 13:49:49 +11003507 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3508 test_bit(R5_Discard, &qdev->flags))))))
NeilBrown474af965fe2011-07-27 11:00:36 +10003509 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3510
3511 /* Now we might consider reading some blocks, either to check/generate
3512 * parity, or to satisfy requests
3513 * or to load a block that is being partially written.
3514 */
3515 if (s.to_read || s.non_overwrite
3516 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003517 || (s.syncing && (s.uptodate + s.compute < disks))
3518 || s.replacing
3519 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003520 handle_stripe_fill(sh, &s, disks);
3521
NeilBrown84789552011-07-27 11:00:36 +10003522 /* Now we check to see if any write operations have recently
3523 * completed
3524 */
3525 prexor = 0;
3526 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3527 prexor = 1;
3528 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3529 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3530 sh->reconstruct_state = reconstruct_state_idle;
3531
3532 /* All the 'written' buffers and the parity block are ready to
3533 * be written back to disk
3534 */
Shaohua Li9e4447682012-10-11 13:49:49 +11003535 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3536 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003537 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003538 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3539 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003540 for (i = disks; i--; ) {
3541 struct r5dev *dev = &sh->dev[i];
3542 if (test_bit(R5_LOCKED, &dev->flags) &&
3543 (i == sh->pd_idx || i == sh->qd_idx ||
3544 dev->written)) {
3545 pr_debug("Writing block %d\n", i);
3546 set_bit(R5_Wantwrite, &dev->flags);
3547 if (prexor)
3548 continue;
3549 if (!test_bit(R5_Insync, &dev->flags) ||
3550 ((i == sh->pd_idx || i == sh->qd_idx) &&
3551 s.failed == 0))
3552 set_bit(STRIPE_INSYNC, &sh->state);
3553 }
3554 }
3555 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3556 s.dec_preread_active = 1;
3557 }
3558
3559 /* Now to consider new write requests and what else, if anything
3560 * should be read. We do not handle new writes when:
3561 * 1/ A 'write' operation (copy+xor) is already in flight.
3562 * 2/ A 'check' operation is in flight, as it may clobber the parity
3563 * block.
3564 */
3565 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3566 handle_stripe_dirtying(conf, sh, &s, disks);
3567
3568 /* maybe we need to check and possibly fix the parity for this stripe
3569 * Any reads will already have been scheduled, so we just see if enough
3570 * data is available. The parity check is held off while parity
3571 * dependent operations are in flight.
3572 */
3573 if (sh->check_state ||
3574 (s.syncing && s.locked == 0 &&
3575 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3576 !test_bit(STRIPE_INSYNC, &sh->state))) {
3577 if (conf->level == 6)
3578 handle_parity_checks6(conf, sh, &s, disks);
3579 else
3580 handle_parity_checks5(conf, sh, &s, disks);
3581 }
NeilBrownc5a31002011-07-27 11:00:36 +10003582
NeilBrown9a3e1102011-12-23 10:17:53 +11003583 if (s.replacing && s.locked == 0
3584 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3585 /* Write out to replacement devices where possible */
3586 for (i = 0; i < conf->raid_disks; i++)
3587 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3588 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3589 set_bit(R5_WantReplace, &sh->dev[i].flags);
3590 set_bit(R5_LOCKED, &sh->dev[i].flags);
3591 s.locked++;
3592 }
3593 set_bit(STRIPE_INSYNC, &sh->state);
3594 }
3595 if ((s.syncing || s.replacing) && s.locked == 0 &&
3596 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003597 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3598 clear_bit(STRIPE_SYNCING, &sh->state);
3599 }
3600
3601 /* If the failed drives are just a ReadError, then we might need
3602 * to progress the repair/check process
3603 */
3604 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3605 for (i = 0; i < s.failed; i++) {
3606 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3607 if (test_bit(R5_ReadError, &dev->flags)
3608 && !test_bit(R5_LOCKED, &dev->flags)
3609 && test_bit(R5_UPTODATE, &dev->flags)
3610 ) {
3611 if (!test_bit(R5_ReWrite, &dev->flags)) {
3612 set_bit(R5_Wantwrite, &dev->flags);
3613 set_bit(R5_ReWrite, &dev->flags);
3614 set_bit(R5_LOCKED, &dev->flags);
3615 s.locked++;
3616 } else {
3617 /* let's read it back */
3618 set_bit(R5_Wantread, &dev->flags);
3619 set_bit(R5_LOCKED, &dev->flags);
3620 s.locked++;
3621 }
3622 }
3623 }
3624
3625
NeilBrown3687c062011-07-27 11:00:36 +10003626 /* Finish reconstruct operations initiated by the expansion process */
3627 if (sh->reconstruct_state == reconstruct_state_result) {
3628 struct stripe_head *sh_src
3629 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3630 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3631 /* sh cannot be written until sh_src has been read.
3632 * so arrange for sh to be delayed a little
3633 */
3634 set_bit(STRIPE_DELAYED, &sh->state);
3635 set_bit(STRIPE_HANDLE, &sh->state);
3636 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3637 &sh_src->state))
3638 atomic_inc(&conf->preread_active_stripes);
3639 release_stripe(sh_src);
3640 goto finish;
3641 }
3642 if (sh_src)
3643 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003644
NeilBrown3687c062011-07-27 11:00:36 +10003645 sh->reconstruct_state = reconstruct_state_idle;
3646 clear_bit(STRIPE_EXPANDING, &sh->state);
3647 for (i = conf->raid_disks; i--; ) {
3648 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3649 set_bit(R5_LOCKED, &sh->dev[i].flags);
3650 s.locked++;
3651 }
3652 }
3653
3654 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3655 !sh->reconstruct_state) {
3656 /* Need to write out all blocks after computing parity */
3657 sh->disks = conf->raid_disks;
3658 stripe_set_idx(sh->sector, conf, 0, sh);
3659 schedule_reconstruction(sh, &s, 1, 1);
3660 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3661 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3662 atomic_dec(&conf->reshape_stripes);
3663 wake_up(&conf->wait_for_overlap);
3664 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3665 }
3666
3667 if (s.expanding && s.locked == 0 &&
3668 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3669 handle_stripe_expansion(conf, sh);
3670
3671finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003672 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003673 if (unlikely(s.blocked_rdev)) {
3674 if (conf->mddev->external)
3675 md_wait_for_blocked_rdev(s.blocked_rdev,
3676 conf->mddev);
3677 else
3678 /* Internal metadata will immediately
3679 * be written by raid5d, so we don't
3680 * need to wait here.
3681 */
3682 rdev_dec_pending(s.blocked_rdev,
3683 conf->mddev);
3684 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003685
NeilBrownbc2607f2011-07-28 11:39:22 +10003686 if (s.handle_bad_blocks)
3687 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003688 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003689 struct r5dev *dev = &sh->dev[i];
3690 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3691 /* We own a safe reference to the rdev */
3692 rdev = conf->disks[i].rdev;
3693 if (!rdev_set_badblocks(rdev, sh->sector,
3694 STRIPE_SECTORS, 0))
3695 md_error(conf->mddev, rdev);
3696 rdev_dec_pending(rdev, conf->mddev);
3697 }
NeilBrownb84db562011-07-28 11:39:23 +10003698 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3699 rdev = conf->disks[i].rdev;
3700 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003701 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003702 rdev_dec_pending(rdev, conf->mddev);
3703 }
NeilBrown977df362011-12-23 10:17:53 +11003704 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3705 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003706 if (!rdev)
3707 /* rdev have been moved down */
3708 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003709 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003710 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003711 rdev_dec_pending(rdev, conf->mddev);
3712 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003713 }
3714
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003715 if (s.ops_request)
3716 raid_run_ops(sh, s.ops_request);
3717
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003718 ops_run_io(sh, &s);
3719
NeilBrownc5709ef2011-07-26 11:35:20 +10003720 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003721 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003722 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003723 * have actually been submitted.
3724 */
3725 atomic_dec(&conf->preread_active_stripes);
3726 if (atomic_read(&conf->preread_active_stripes) <
3727 IO_THRESHOLD)
3728 md_wakeup_thread(conf->mddev->thread);
3729 }
3730
NeilBrownc5709ef2011-07-26 11:35:20 +10003731 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003732
Dan Williams257a4b42011-11-08 16:22:06 +11003733 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003734}
3735
NeilBrownd1688a62011-10-11 16:49:52 +11003736static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737{
3738 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3739 while (!list_empty(&conf->delayed_list)) {
3740 struct list_head *l = conf->delayed_list.next;
3741 struct stripe_head *sh;
3742 sh = list_entry(l, struct stripe_head, lru);
3743 list_del_init(l);
3744 clear_bit(STRIPE_DELAYED, &sh->state);
3745 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3746 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003747 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 }
NeilBrown482c0832011-04-18 18:25:42 +10003749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750}
3751
NeilBrownd1688a62011-10-11 16:49:52 +11003752static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003753{
3754 /* device_lock is held */
3755 struct list_head head;
3756 list_add(&head, &conf->bitmap_list);
3757 list_del_init(&conf->bitmap_list);
3758 while (!list_empty(&head)) {
3759 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3760 list_del_init(&sh->lru);
3761 atomic_inc(&sh->count);
3762 __release_stripe(conf, sh);
3763 }
3764}
3765
NeilBrownfd01b882011-10-11 16:47:53 +11003766int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003767{
NeilBrownd1688a62011-10-11 16:49:52 +11003768 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003769
3770 /* No difference between reads and writes. Just check
3771 * how busy the stripe_cache is
3772 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003773
NeilBrownf022b2f2006-10-03 01:15:56 -07003774 if (conf->inactive_blocked)
3775 return 1;
3776 if (conf->quiesce)
3777 return 1;
3778 if (list_empty_careful(&conf->inactive_list))
3779 return 1;
3780
3781 return 0;
3782}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003783EXPORT_SYMBOL_GPL(md_raid5_congested);
3784
3785static int raid5_congested(void *data, int bits)
3786{
NeilBrownfd01b882011-10-11 16:47:53 +11003787 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003788
3789 return mddev_congested(mddev, bits) ||
3790 md_raid5_congested(mddev, bits);
3791}
NeilBrownf022b2f2006-10-03 01:15:56 -07003792
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003793/* We want read requests to align with chunks where possible,
3794 * but write requests don't need to.
3795 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003796static int raid5_mergeable_bvec(struct request_queue *q,
3797 struct bvec_merge_data *bvm,
3798 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003799{
NeilBrownfd01b882011-10-11 16:47:53 +11003800 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003801 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003802 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003803 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003804 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003805
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003806 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003807 return biovec->bv_len; /* always allow writes to be mergeable */
3808
Andre Noll664e7c42009-06-18 08:45:27 +10003809 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3810 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003811 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3812 if (max < 0) max = 0;
3813 if (max <= biovec->bv_len && bio_sectors == 0)
3814 return biovec->bv_len;
3815 else
3816 return max;
3817}
3818
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003819
NeilBrownfd01b882011-10-11 16:47:53 +11003820static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003821{
3822 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003823 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003824 unsigned int bio_sectors = bio->bi_size >> 9;
3825
Andre Noll664e7c42009-06-18 08:45:27 +10003826 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3827 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003828 return chunk_sectors >=
3829 ((sector & (chunk_sectors - 1)) + bio_sectors);
3830}
3831
3832/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003833 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3834 * later sampled by raid5d.
3835 */
NeilBrownd1688a62011-10-11 16:49:52 +11003836static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003837{
3838 unsigned long flags;
3839
3840 spin_lock_irqsave(&conf->device_lock, flags);
3841
3842 bi->bi_next = conf->retry_read_aligned_list;
3843 conf->retry_read_aligned_list = bi;
3844
3845 spin_unlock_irqrestore(&conf->device_lock, flags);
3846 md_wakeup_thread(conf->mddev->thread);
3847}
3848
3849
NeilBrownd1688a62011-10-11 16:49:52 +11003850static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003851{
3852 struct bio *bi;
3853
3854 bi = conf->retry_read_aligned;
3855 if (bi) {
3856 conf->retry_read_aligned = NULL;
3857 return bi;
3858 }
3859 bi = conf->retry_read_aligned_list;
3860 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003861 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003862 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003863 /*
3864 * this sets the active strip count to 1 and the processed
3865 * strip count to zero (upper 8 bits)
3866 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003867 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003868 }
3869
3870 return bi;
3871}
3872
3873
3874/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003875 * The "raid5_align_endio" should check if the read succeeded and if it
3876 * did, call bio_endio on the original bio (having bio_put the new bio
3877 * first).
3878 * If the read failed..
3879 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003880static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003881{
3882 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003883 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003884 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003885 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003886 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003887
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003888 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003889
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003890 rdev = (void*)raid_bi->bi_next;
3891 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003892 mddev = rdev->mddev;
3893 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003894
3895 rdev_dec_pending(rdev, conf->mddev);
3896
3897 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003898 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003899 if (atomic_dec_and_test(&conf->active_aligned_reads))
3900 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003901 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003902 }
3903
3904
Dan Williams45b42332007-07-09 11:56:43 -07003905 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003906
3907 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003908}
3909
Neil Brown387bb172007-02-08 14:20:29 -08003910static int bio_fits_rdev(struct bio *bi)
3911{
Jens Axboe165125e2007-07-24 09:28:11 +02003912 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003913
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003914 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003915 return 0;
3916 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003917 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003918 return 0;
3919
3920 if (q->merge_bvec_fn)
3921 /* it's too hard to apply the merge_bvec_fn at this stage,
3922 * just just give up
3923 */
3924 return 0;
3925
3926 return 1;
3927}
3928
3929
NeilBrownfd01b882011-10-11 16:47:53 +11003930static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003931{
NeilBrownd1688a62011-10-11 16:49:52 +11003932 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11003933 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003934 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003935 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003936 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003937
3938 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003939 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003940 return 0;
3941 }
3942 /*
NeilBrowna167f662010-10-26 18:31:13 +11003943 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003944 */
NeilBrowna167f662010-10-26 18:31:13 +11003945 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003946 if (!align_bi)
3947 return 0;
3948 /*
3949 * set bi_end_io to a new function, and set bi_private to the
3950 * original bio.
3951 */
3952 align_bi->bi_end_io = raid5_align_endio;
3953 align_bi->bi_private = raid_bio;
3954 /*
3955 * compute position
3956 */
NeilBrown112bf892009-03-31 14:39:38 +11003957 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3958 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003959 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003960
NeilBrown671488c2011-12-23 10:17:52 +11003961 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003962 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003963 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3964 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3965 rdev->recovery_offset < end_sector) {
3966 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3967 if (rdev &&
3968 (test_bit(Faulty, &rdev->flags) ||
3969 !(test_bit(In_sync, &rdev->flags) ||
3970 rdev->recovery_offset >= end_sector)))
3971 rdev = NULL;
3972 }
3973 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003974 sector_t first_bad;
3975 int bad_sectors;
3976
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003977 atomic_inc(&rdev->nr_pending);
3978 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003979 raid_bio->bi_next = (void*)rdev;
3980 align_bi->bi_bdev = rdev->bdev;
3981 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003982
NeilBrown31c176e2011-07-28 11:39:22 +10003983 if (!bio_fits_rdev(align_bi) ||
3984 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3985 &first_bad, &bad_sectors)) {
3986 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003987 bio_put(align_bi);
3988 rdev_dec_pending(rdev, mddev);
3989 return 0;
3990 }
3991
majianpeng6c0544e2012-06-12 08:31:10 +08003992 /* No reshape active, so we can trust rdev->data_offset */
3993 align_bi->bi_sector += rdev->data_offset;
3994
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003995 spin_lock_irq(&conf->device_lock);
3996 wait_event_lock_irq(conf->wait_for_stripe,
3997 conf->quiesce == 0,
3998 conf->device_lock, /* nothing */);
3999 atomic_inc(&conf->active_aligned_reads);
4000 spin_unlock_irq(&conf->device_lock);
4001
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004002 generic_make_request(align_bi);
4003 return 1;
4004 } else {
4005 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004006 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004007 return 0;
4008 }
4009}
4010
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004011/* __get_priority_stripe - get the next stripe to process
4012 *
4013 * Full stripe writes are allowed to pass preread active stripes up until
4014 * the bypass_threshold is exceeded. In general the bypass_count
4015 * increments when the handle_list is handled before the hold_list; however, it
4016 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4017 * stripe with in flight i/o. The bypass_count will be reset when the
4018 * head of the hold_list has changed, i.e. the head was promoted to the
4019 * handle_list.
4020 */
NeilBrownd1688a62011-10-11 16:49:52 +11004021static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004022{
4023 struct stripe_head *sh;
4024
4025 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4026 __func__,
4027 list_empty(&conf->handle_list) ? "empty" : "busy",
4028 list_empty(&conf->hold_list) ? "empty" : "busy",
4029 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4030
4031 if (!list_empty(&conf->handle_list)) {
4032 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4033
4034 if (list_empty(&conf->hold_list))
4035 conf->bypass_count = 0;
4036 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4037 if (conf->hold_list.next == conf->last_hold)
4038 conf->bypass_count++;
4039 else {
4040 conf->last_hold = conf->hold_list.next;
4041 conf->bypass_count -= conf->bypass_threshold;
4042 if (conf->bypass_count < 0)
4043 conf->bypass_count = 0;
4044 }
4045 }
4046 } else if (!list_empty(&conf->hold_list) &&
4047 ((conf->bypass_threshold &&
4048 conf->bypass_count > conf->bypass_threshold) ||
4049 atomic_read(&conf->pending_full_writes) == 0)) {
4050 sh = list_entry(conf->hold_list.next,
4051 typeof(*sh), lru);
4052 conf->bypass_count -= conf->bypass_threshold;
4053 if (conf->bypass_count < 0)
4054 conf->bypass_count = 0;
4055 } else
4056 return NULL;
4057
4058 list_del_init(&sh->lru);
4059 atomic_inc(&sh->count);
4060 BUG_ON(atomic_read(&sh->count) != 1);
4061 return sh;
4062}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004063
Shaohua Li8811b592012-08-02 08:33:00 +10004064struct raid5_plug_cb {
4065 struct blk_plug_cb cb;
4066 struct list_head list;
4067};
4068
4069static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4070{
4071 struct raid5_plug_cb *cb = container_of(
4072 blk_cb, struct raid5_plug_cb, cb);
4073 struct stripe_head *sh;
4074 struct mddev *mddev = cb->cb.data;
4075 struct r5conf *conf = mddev->private;
4076
4077 if (cb->list.next && !list_empty(&cb->list)) {
4078 spin_lock_irq(&conf->device_lock);
4079 while (!list_empty(&cb->list)) {
4080 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4081 list_del_init(&sh->lru);
4082 /*
4083 * avoid race release_stripe_plug() sees
4084 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4085 * is still in our list
4086 */
4087 smp_mb__before_clear_bit();
4088 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4089 __release_stripe(conf, sh);
4090 }
4091 spin_unlock_irq(&conf->device_lock);
4092 }
4093 kfree(cb);
4094}
4095
4096static void release_stripe_plug(struct mddev *mddev,
4097 struct stripe_head *sh)
4098{
4099 struct blk_plug_cb *blk_cb = blk_check_plugged(
4100 raid5_unplug, mddev,
4101 sizeof(struct raid5_plug_cb));
4102 struct raid5_plug_cb *cb;
4103
4104 if (!blk_cb) {
4105 release_stripe(sh);
4106 return;
4107 }
4108
4109 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4110
4111 if (cb->list.next == NULL)
4112 INIT_LIST_HEAD(&cb->list);
4113
4114 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4115 list_add_tail(&sh->lru, &cb->list);
4116 else
4117 release_stripe(sh);
4118}
4119
Shaohua Li620125f2012-10-11 13:49:05 +11004120static void make_discard_request(struct mddev *mddev, struct bio *bi)
4121{
4122 struct r5conf *conf = mddev->private;
4123 sector_t logical_sector, last_sector;
4124 struct stripe_head *sh;
4125 int remaining;
4126 int stripe_sectors;
4127
4128 if (mddev->reshape_position != MaxSector)
4129 /* Skip discard while reshape is happening */
4130 return;
4131
4132 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4133 last_sector = bi->bi_sector + (bi->bi_size>>9);
4134
4135 bi->bi_next = NULL;
4136 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4137
4138 stripe_sectors = conf->chunk_sectors *
4139 (conf->raid_disks - conf->max_degraded);
4140 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4141 stripe_sectors);
4142 sector_div(last_sector, stripe_sectors);
4143
4144 logical_sector *= conf->chunk_sectors;
4145 last_sector *= conf->chunk_sectors;
4146
4147 for (; logical_sector < last_sector;
4148 logical_sector += STRIPE_SECTORS) {
4149 DEFINE_WAIT(w);
4150 int d;
4151 again:
4152 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4153 prepare_to_wait(&conf->wait_for_overlap, &w,
4154 TASK_UNINTERRUPTIBLE);
4155 spin_lock_irq(&sh->stripe_lock);
4156 for (d = 0; d < conf->raid_disks; d++) {
4157 if (d == sh->pd_idx || d == sh->qd_idx)
4158 continue;
4159 if (sh->dev[d].towrite || sh->dev[d].toread) {
4160 set_bit(R5_Overlap, &sh->dev[d].flags);
4161 spin_unlock_irq(&sh->stripe_lock);
4162 release_stripe(sh);
4163 schedule();
4164 goto again;
4165 }
4166 }
4167 finish_wait(&conf->wait_for_overlap, &w);
4168 for (d = 0; d < conf->raid_disks; d++) {
4169 if (d == sh->pd_idx || d == sh->qd_idx)
4170 continue;
4171 sh->dev[d].towrite = bi;
4172 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4173 raid5_inc_bi_active_stripes(bi);
4174 }
4175 spin_unlock_irq(&sh->stripe_lock);
4176 if (conf->mddev->bitmap) {
4177 for (d = 0;
4178 d < conf->raid_disks - conf->max_degraded;
4179 d++)
4180 bitmap_startwrite(mddev->bitmap,
4181 sh->sector,
4182 STRIPE_SECTORS,
4183 0);
4184 sh->bm_seq = conf->seq_flush + 1;
4185 set_bit(STRIPE_BIT_DELAY, &sh->state);
4186 }
4187
4188 set_bit(STRIPE_HANDLE, &sh->state);
4189 clear_bit(STRIPE_DELAYED, &sh->state);
4190 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4191 atomic_inc(&conf->preread_active_stripes);
4192 release_stripe_plug(mddev, sh);
4193 }
4194
4195 remaining = raid5_dec_bi_active_stripes(bi);
4196 if (remaining == 0) {
4197 md_write_end(mddev);
4198 bio_endio(bi, 0);
4199 }
4200}
4201
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004202static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203{
NeilBrownd1688a62011-10-11 16:49:52 +11004204 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004205 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 sector_t new_sector;
4207 sector_t logical_sector, last_sector;
4208 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004209 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004210 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211
Tejun Heoe9c74692010-09-03 11:56:18 +02004212 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4213 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004214 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004215 }
4216
NeilBrown3d310eb2005-06-21 17:17:26 -07004217 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004218
NeilBrown802ba062006-12-13 00:34:13 -08004219 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004220 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004221 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004222 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004223
Shaohua Li620125f2012-10-11 13:49:05 +11004224 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4225 make_discard_request(mddev, bi);
4226 return;
4227 }
4228
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4230 last_sector = bi->bi_sector + (bi->bi_size>>9);
4231 bi->bi_next = NULL;
4232 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004233
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4235 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004236 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004237
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004238 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004239 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004240 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004241 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004242 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004243 * 64bit on a 32bit platform, and so it might be
4244 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004245 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004246 * the lock is dropped, so once we get a reference
4247 * to the stripe that we think it is, we will have
4248 * to check again.
4249 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004250 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004251 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004252 ? logical_sector < conf->reshape_progress
4253 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004254 previous = 1;
4255 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004256 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004257 ? logical_sector < conf->reshape_safe
4258 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004259 spin_unlock_irq(&conf->device_lock);
4260 schedule();
4261 goto retry;
4262 }
4263 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004264 spin_unlock_irq(&conf->device_lock);
4265 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004266
NeilBrown112bf892009-03-31 14:39:38 +11004267 new_sector = raid5_compute_sector(conf, logical_sector,
4268 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004269 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004270 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 (unsigned long long)new_sector,
4272 (unsigned long long)logical_sector);
4273
NeilBrownb5663ba2009-03-31 14:39:38 +11004274 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004275 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004277 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004278 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004279 * stripe, so we must do the range check again.
4280 * Expansion could still move past after this
4281 * test, but as we are holding a reference to
4282 * 'sh', we know that if that happens,
4283 * STRIPE_EXPANDING will get set and the expansion
4284 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004285 */
4286 int must_retry = 0;
4287 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004288 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004289 ? logical_sector >= conf->reshape_progress
4290 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004291 /* mismatch, need to try again */
4292 must_retry = 1;
4293 spin_unlock_irq(&conf->device_lock);
4294 if (must_retry) {
4295 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004296 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004297 goto retry;
4298 }
4299 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004300
Namhyung Kimffd96e32011-07-18 17:38:51 +10004301 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004302 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004303 logical_sector < mddev->suspend_hi) {
4304 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004305 /* As the suspend_* range is controlled by
4306 * userspace, we want an interruptible
4307 * wait.
4308 */
4309 flush_signals(current);
4310 prepare_to_wait(&conf->wait_for_overlap,
4311 &w, TASK_INTERRUPTIBLE);
4312 if (logical_sector >= mddev->suspend_lo &&
4313 logical_sector < mddev->suspend_hi)
4314 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004315 goto retry;
4316 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004317
4318 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004319 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004320 /* Stripe is busy expanding or
4321 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 * and wait a while
4323 */
NeilBrown482c0832011-04-18 18:25:42 +10004324 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 release_stripe(sh);
4326 schedule();
4327 goto retry;
4328 }
4329 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004330 set_bit(STRIPE_HANDLE, &sh->state);
4331 clear_bit(STRIPE_DELAYED, &sh->state);
majianpeng895e3c52012-07-31 10:05:44 +10004332 if ((bi->bi_rw & REQ_NOIDLE) &&
NeilBrown729a1862009-12-14 12:49:50 +11004333 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4334 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004335 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 } else {
4337 /* cannot get stripe for read-ahead, just give-up */
4338 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4339 finish_wait(&conf->wait_for_overlap, &w);
4340 break;
4341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004343
Shaohua Lie7836bd62012-07-19 16:01:31 +10004344 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004345 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
NeilBrown16a53ec2006-06-26 00:27:38 -07004347 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004349
Neil Brown0e13fe232008-06-28 08:31:20 +10004350 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352}
4353
NeilBrownfd01b882011-10-11 16:47:53 +11004354static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004355
NeilBrownfd01b882011-10-11 16:47:53 +11004356static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357{
NeilBrown52c03292006-06-26 00:27:43 -07004358 /* reshaping is quite different to recovery/resync so it is
4359 * handled quite separately ... here.
4360 *
4361 * On each call to sync_request, we gather one chunk worth of
4362 * destination stripes and flag them as expanding.
4363 * Then we find all the source stripes and request reads.
4364 * As the reads complete, handle_stripe will copy the data
4365 * into the destination stripe and release that stripe.
4366 */
NeilBrownd1688a62011-10-11 16:49:52 +11004367 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004369 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004370 int raid_disks = conf->previous_raid_disks;
4371 int data_disks = raid_disks - conf->max_degraded;
4372 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004373 int i;
4374 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004375 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004376 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004377 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004378 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004379
NeilBrownfef9c612009-03-31 15:16:46 +11004380 if (sector_nr == 0) {
4381 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004382 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004383 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4384 sector_nr = raid5_size(mddev, 0, 0)
4385 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004386 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004387 conf->reshape_progress > 0)
4388 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004389 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004390 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004391 mddev->curr_resync_completed = sector_nr;
4392 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004393 *skipped = 1;
4394 return sector_nr;
4395 }
NeilBrown52c03292006-06-26 00:27:43 -07004396 }
4397
NeilBrown7a661382009-03-31 15:21:40 +11004398 /* We need to process a full chunk at a time.
4399 * If old and new chunk sizes differ, we need to process the
4400 * largest of these
4401 */
Andre Noll664e7c42009-06-18 08:45:27 +10004402 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4403 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004404 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004405 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004406
NeilBrownb5254dd2012-05-21 09:27:01 +10004407 /* We update the metadata at least every 10 seconds, or when
4408 * the data about to be copied would over-write the source of
4409 * the data at the front of the range. i.e. one new_stripe
4410 * along from reshape_progress new_maps to after where
4411 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004412 */
NeilBrownfef9c612009-03-31 15:16:46 +11004413 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004414 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004415 readpos = conf->reshape_progress;
4416 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004417 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004418 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004419 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004420 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004421 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004422 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004423 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004424 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004425 readpos -= min_t(sector_t, reshape_sectors, readpos);
4426 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004427 }
NeilBrown52c03292006-06-26 00:27:43 -07004428
NeilBrownb5254dd2012-05-21 09:27:01 +10004429 /* Having calculated the 'writepos' possibly use it
4430 * to set 'stripe_addr' which is where we will write to.
4431 */
4432 if (mddev->reshape_backwards) {
4433 BUG_ON(conf->reshape_progress == 0);
4434 stripe_addr = writepos;
4435 BUG_ON((mddev->dev_sectors &
4436 ~((sector_t)reshape_sectors - 1))
4437 - reshape_sectors - stripe_addr
4438 != sector_nr);
4439 } else {
4440 BUG_ON(writepos != sector_nr + reshape_sectors);
4441 stripe_addr = sector_nr;
4442 }
4443
NeilBrownc8f517c2009-03-31 15:28:40 +11004444 /* 'writepos' is the most advanced device address we might write.
4445 * 'readpos' is the least advanced device address we might read.
4446 * 'safepos' is the least address recorded in the metadata as having
4447 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004448 * If there is a min_offset_diff, these are adjusted either by
4449 * increasing the safepos/readpos if diff is negative, or
4450 * increasing writepos if diff is positive.
4451 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004452 * ensure safety in the face of a crash - that must be done by userspace
4453 * making a backup of the data. So in that case there is no particular
4454 * rush to update metadata.
4455 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4456 * update the metadata to advance 'safepos' to match 'readpos' so that
4457 * we can be safe in the event of a crash.
4458 * So we insist on updating metadata if safepos is behind writepos and
4459 * readpos is beyond writepos.
4460 * In any case, update the metadata every 10 seconds.
4461 * Maybe that number should be configurable, but I'm not sure it is
4462 * worth it.... maybe it could be a multiple of safemode_delay???
4463 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004464 if (conf->min_offset_diff < 0) {
4465 safepos += -conf->min_offset_diff;
4466 readpos += -conf->min_offset_diff;
4467 } else
4468 writepos += conf->min_offset_diff;
4469
NeilBrown2c810cd2012-05-21 09:27:00 +10004470 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004471 ? (safepos > writepos && readpos < writepos)
4472 : (safepos < writepos && readpos > writepos)) ||
4473 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004474 /* Cannot proceed until we've updated the superblock... */
4475 wait_event(conf->wait_for_overlap,
4476 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004477 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004478 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004479 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004480 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004481 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004482 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004483 kthread_should_stop());
4484 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004485 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004486 spin_unlock_irq(&conf->device_lock);
4487 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004488 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004489 }
4490
NeilBrownab69ae12009-03-31 15:26:47 +11004491 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004492 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004493 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004494 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004495 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004496 set_bit(STRIPE_EXPANDING, &sh->state);
4497 atomic_inc(&conf->reshape_stripes);
4498 /* If any of this stripe is beyond the end of the old
4499 * array, then we need to zero those blocks
4500 */
4501 for (j=sh->disks; j--;) {
4502 sector_t s;
4503 if (j == sh->pd_idx)
4504 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004505 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004506 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004507 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004508 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004509 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004510 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004511 continue;
4512 }
4513 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4514 set_bit(R5_Expanded, &sh->dev[j].flags);
4515 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4516 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004517 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004518 set_bit(STRIPE_EXPAND_READY, &sh->state);
4519 set_bit(STRIPE_HANDLE, &sh->state);
4520 }
NeilBrownab69ae12009-03-31 15:26:47 +11004521 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004522 }
4523 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004524 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004525 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004526 else
NeilBrown7a661382009-03-31 15:21:40 +11004527 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004528 spin_unlock_irq(&conf->device_lock);
4529 /* Ok, those stripe are ready. We can start scheduling
4530 * reads on the source stripes.
4531 * The source stripes are determined by mapping the first and last
4532 * block on the destination stripes.
4533 */
NeilBrown52c03292006-06-26 00:27:43 -07004534 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004535 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004536 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004537 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004538 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004539 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004540 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004541 if (last_sector >= mddev->dev_sectors)
4542 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004543 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004544 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004545 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4546 set_bit(STRIPE_HANDLE, &sh->state);
4547 release_stripe(sh);
4548 first_sector += STRIPE_SECTORS;
4549 }
NeilBrownab69ae12009-03-31 15:26:47 +11004550 /* Now that the sources are clearly marked, we can release
4551 * the destination stripes
4552 */
4553 while (!list_empty(&stripes)) {
4554 sh = list_entry(stripes.next, struct stripe_head, lru);
4555 list_del_init(&sh->lru);
4556 release_stripe(sh);
4557 }
NeilBrownc6207272008-02-06 01:39:52 -08004558 /* If this takes us to the resync_max point where we have to pause,
4559 * then we need to write out the superblock.
4560 */
NeilBrown7a661382009-03-31 15:21:40 +11004561 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004562 if ((sector_nr - mddev->curr_resync_completed) * 2
4563 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004564 /* Cannot proceed until we've updated the superblock... */
4565 wait_event(conf->wait_for_overlap,
4566 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004567 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004568 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004569 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004570 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4571 md_wakeup_thread(mddev->thread);
4572 wait_event(mddev->sb_wait,
4573 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4574 || kthread_should_stop());
4575 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004576 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004577 spin_unlock_irq(&conf->device_lock);
4578 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004579 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004580 }
NeilBrown7a661382009-03-31 15:21:40 +11004581 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004582}
4583
4584/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004585static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004586{
NeilBrownd1688a62011-10-11 16:49:52 +11004587 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004588 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004589 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004590 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004591 int still_degraded = 0;
4592 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
NeilBrown72626682005-09-09 16:23:54 -07004594 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004596
NeilBrown29269552006-03-27 01:18:10 -08004597 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4598 end_reshape(conf);
4599 return 0;
4600 }
NeilBrown72626682005-09-09 16:23:54 -07004601
4602 if (mddev->curr_resync < max_sector) /* aborted */
4603 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4604 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004605 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004606 conf->fullsync = 0;
4607 bitmap_close_sync(mddev->bitmap);
4608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 return 0;
4610 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004611
NeilBrown64bd6602009-08-03 10:59:58 +10004612 /* Allow raid5_quiesce to complete */
4613 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4614
NeilBrown52c03292006-06-26 00:27:43 -07004615 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4616 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004617
NeilBrownc6207272008-02-06 01:39:52 -08004618 /* No need to check resync_max as we never do more than one
4619 * stripe, and as resync_max will always be on a chunk boundary,
4620 * if the check in md_do_sync didn't fire, there is no chance
4621 * of overstepping resync_max here
4622 */
4623
NeilBrown16a53ec2006-06-26 00:27:38 -07004624 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 * to resync, then assert that we are finished, because there is
4626 * nothing we can do.
4627 */
NeilBrown3285edf2006-06-26 00:27:55 -07004628 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004629 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004630 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004631 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 return rv;
4633 }
NeilBrown72626682005-09-09 16:23:54 -07004634 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004635 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004636 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4637 /* we can skip this block, and probably more */
4638 sync_blocks /= STRIPE_SECTORS;
4639 *skipped = 1;
4640 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
NeilBrownb47490c2008-02-06 01:39:50 -08004643 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4644
NeilBrowna8c906c2009-06-09 14:39:59 +10004645 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004647 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004649 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004651 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004653 /* Need to check if array will still be degraded after recovery/resync
4654 * We don't need to check the 'failed' flag as when that gets set,
4655 * recovery aborts.
4656 */
NeilBrownf001a702009-06-09 14:30:31 +10004657 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004658 if (conf->disks[i].rdev == NULL)
4659 still_degraded = 1;
4660
4661 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4662
NeilBrown83206d62011-07-26 11:19:49 +10004663 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
NeilBrown14425772009-10-16 15:55:25 +11004665 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 release_stripe(sh);
4667
4668 return STRIPE_SECTORS;
4669}
4670
NeilBrownd1688a62011-10-11 16:49:52 +11004671static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004672{
4673 /* We may not be able to submit a whole bio at once as there
4674 * may not be enough stripe_heads available.
4675 * We cannot pre-allocate enough stripe_heads as we may need
4676 * more than exist in the cache (if we allow ever large chunks).
4677 * So we do one stripe head at a time and record in
4678 * ->bi_hw_segments how many have been done.
4679 *
4680 * We *know* that this entire raid_bio is in one chunk, so
4681 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4682 */
4683 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004684 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004685 sector_t sector, logical_sector, last_sector;
4686 int scnt = 0;
4687 int remaining;
4688 int handled = 0;
4689
4690 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004691 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004692 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004693 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4694
4695 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004696 logical_sector += STRIPE_SECTORS,
4697 sector += STRIPE_SECTORS,
4698 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004699
Shaohua Lie7836bd62012-07-19 16:01:31 +10004700 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004701 /* already done this stripe */
4702 continue;
4703
NeilBrowna8c906c2009-06-09 14:39:59 +10004704 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004705
4706 if (!sh) {
4707 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004708 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004709 conf->retry_read_aligned = raid_bio;
4710 return handled;
4711 }
4712
Neil Brown387bb172007-02-08 14:20:29 -08004713 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4714 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004715 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004716 conf->retry_read_aligned = raid_bio;
4717 return handled;
4718 }
4719
majianpeng3f9e7c12012-07-31 10:04:21 +10004720 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004721 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004722 release_stripe(sh);
4723 handled++;
4724 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004725 remaining = raid5_dec_bi_active_stripes(raid_bio);
Neil Brown0e13fe232008-06-28 08:31:20 +10004726 if (remaining == 0)
4727 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004728 if (atomic_dec_and_test(&conf->active_aligned_reads))
4729 wake_up(&conf->wait_for_stripe);
4730 return handled;
4731}
4732
Shaohua Li46a06402012-08-02 08:33:15 +10004733#define MAX_STRIPE_BATCH 8
4734static int handle_active_stripes(struct r5conf *conf)
4735{
4736 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4737 int i, batch_size = 0;
4738
4739 while (batch_size < MAX_STRIPE_BATCH &&
4740 (sh = __get_priority_stripe(conf)) != NULL)
4741 batch[batch_size++] = sh;
4742
4743 if (batch_size == 0)
4744 return batch_size;
4745 spin_unlock_irq(&conf->device_lock);
4746
4747 for (i = 0; i < batch_size; i++)
4748 handle_stripe(batch[i]);
4749
4750 cond_resched();
4751
4752 spin_lock_irq(&conf->device_lock);
4753 for (i = 0; i < batch_size; i++)
4754 __release_stripe(conf, batch[i]);
4755 return batch_size;
4756}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004757
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758/*
4759 * This is our raid5 kernel thread.
4760 *
4761 * We scan the hash table for stripes which can be handled now.
4762 * During the scan, completed stripes are saved for us by the interrupt
4763 * handler, so that they will not have to wait for our next wakeup.
4764 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004765static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766{
Shaohua Li4ed87312012-10-11 13:34:00 +11004767 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004768 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004770 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
Dan Williams45b42332007-07-09 11:56:43 -07004772 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
4774 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004776 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 handled = 0;
4778 spin_lock_irq(&conf->device_lock);
4779 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004780 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004781 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
NeilBrown0021b7b2012-07-31 09:08:14 +02004783 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004784 !list_empty(&conf->bitmap_list)) {
4785 /* Now is a good time to flush some bitmap updates */
4786 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004787 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004788 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004789 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004790 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004791 activate_bit_delay(conf);
4792 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004793 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004794
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004795 while ((bio = remove_bio_from_retry(conf))) {
4796 int ok;
4797 spin_unlock_irq(&conf->device_lock);
4798 ok = retry_aligned_read(conf, bio);
4799 spin_lock_irq(&conf->device_lock);
4800 if (!ok)
4801 break;
4802 handled++;
4803 }
4804
Shaohua Li46a06402012-08-02 08:33:15 +10004805 batch_size = handle_active_stripes(conf);
4806 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004808 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Shaohua Li46a06402012-08-02 08:33:15 +10004810 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4811 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004812 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004813 spin_lock_irq(&conf->device_lock);
4814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 }
Dan Williams45b42332007-07-09 11:56:43 -07004816 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
4818 spin_unlock_irq(&conf->device_lock);
4819
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004820 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004821 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822
Dan Williams45b42332007-07-09 11:56:43 -07004823 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824}
4825
NeilBrown3f294f42005-11-08 21:39:25 -08004826static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004827raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004828{
NeilBrownd1688a62011-10-11 16:49:52 +11004829 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004830 if (conf)
4831 return sprintf(page, "%d\n", conf->max_nr_stripes);
4832 else
4833 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004834}
4835
NeilBrownc41d4ac2010-06-01 19:37:24 +10004836int
NeilBrownfd01b882011-10-11 16:47:53 +11004837raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004838{
NeilBrownd1688a62011-10-11 16:49:52 +11004839 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004840 int err;
4841
4842 if (size <= 16 || size > 32768)
4843 return -EINVAL;
4844 while (size < conf->max_nr_stripes) {
4845 if (drop_one_stripe(conf))
4846 conf->max_nr_stripes--;
4847 else
4848 break;
4849 }
4850 err = md_allow_write(mddev);
4851 if (err)
4852 return err;
4853 while (size > conf->max_nr_stripes) {
4854 if (grow_one_stripe(conf))
4855 conf->max_nr_stripes++;
4856 else break;
4857 }
4858 return 0;
4859}
4860EXPORT_SYMBOL(raid5_set_cache_size);
4861
NeilBrown3f294f42005-11-08 21:39:25 -08004862static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004863raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004864{
NeilBrownd1688a62011-10-11 16:49:52 +11004865 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004866 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004867 int err;
4868
NeilBrown3f294f42005-11-08 21:39:25 -08004869 if (len >= PAGE_SIZE)
4870 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004871 if (!conf)
4872 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004873
Dan Williams4ef197d82008-04-28 02:15:54 -07004874 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004875 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004876 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004877 if (err)
4878 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004879 return len;
4880}
NeilBrown007583c2005-11-08 21:39:30 -08004881
NeilBrown96de1e62005-11-08 21:39:39 -08004882static struct md_sysfs_entry
4883raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4884 raid5_show_stripe_cache_size,
4885 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004886
4887static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004888raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004889{
NeilBrownd1688a62011-10-11 16:49:52 +11004890 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004891 if (conf)
4892 return sprintf(page, "%d\n", conf->bypass_threshold);
4893 else
4894 return 0;
4895}
4896
4897static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004898raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004899{
NeilBrownd1688a62011-10-11 16:49:52 +11004900 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004901 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004902 if (len >= PAGE_SIZE)
4903 return -EINVAL;
4904 if (!conf)
4905 return -ENODEV;
4906
Dan Williams4ef197d82008-04-28 02:15:54 -07004907 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004908 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004909 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004910 return -EINVAL;
4911 conf->bypass_threshold = new;
4912 return len;
4913}
4914
4915static struct md_sysfs_entry
4916raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4917 S_IRUGO | S_IWUSR,
4918 raid5_show_preread_threshold,
4919 raid5_store_preread_threshold);
4920
4921static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004922stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004923{
NeilBrownd1688a62011-10-11 16:49:52 +11004924 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004925 if (conf)
4926 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4927 else
4928 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004929}
4930
NeilBrown96de1e62005-11-08 21:39:39 -08004931static struct md_sysfs_entry
4932raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004933
NeilBrown007583c2005-11-08 21:39:30 -08004934static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004935 &raid5_stripecache_size.attr,
4936 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004937 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004938 NULL,
4939};
NeilBrown007583c2005-11-08 21:39:30 -08004940static struct attribute_group raid5_attrs_group = {
4941 .name = NULL,
4942 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004943};
4944
Dan Williams80c3a6c2009-03-17 18:10:40 -07004945static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004946raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004947{
NeilBrownd1688a62011-10-11 16:49:52 +11004948 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004949
4950 if (!sectors)
4951 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004952 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004953 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004954 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004955
Andre Noll9d8f0362009-06-18 08:45:01 +10004956 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004957 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004958 return sectors * (raid_disks - conf->max_degraded);
4959}
4960
NeilBrownd1688a62011-10-11 16:49:52 +11004961static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004962{
4963 struct raid5_percpu *percpu;
4964 unsigned long cpu;
4965
4966 if (!conf->percpu)
4967 return;
4968
4969 get_online_cpus();
4970 for_each_possible_cpu(cpu) {
4971 percpu = per_cpu_ptr(conf->percpu, cpu);
4972 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004973 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004974 }
4975#ifdef CONFIG_HOTPLUG_CPU
4976 unregister_cpu_notifier(&conf->cpu_notify);
4977#endif
4978 put_online_cpus();
4979
4980 free_percpu(conf->percpu);
4981}
4982
NeilBrownd1688a62011-10-11 16:49:52 +11004983static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004984{
4985 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004986 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004987 kfree(conf->disks);
4988 kfree(conf->stripe_hashtbl);
4989 kfree(conf);
4990}
4991
Dan Williams36d1c642009-07-14 11:48:22 -07004992#ifdef CONFIG_HOTPLUG_CPU
4993static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4994 void *hcpu)
4995{
NeilBrownd1688a62011-10-11 16:49:52 +11004996 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07004997 long cpu = (long)hcpu;
4998 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4999
5000 switch (action) {
5001 case CPU_UP_PREPARE:
5002 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07005003 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07005004 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005005 if (!percpu->scribble)
5006 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5007
5008 if (!percpu->scribble ||
5009 (conf->level == 6 && !percpu->spare_page)) {
5010 safe_put_page(percpu->spare_page);
5011 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005012 pr_err("%s: failed memory allocation for cpu%ld\n",
5013 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005014 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005015 }
5016 break;
5017 case CPU_DEAD:
5018 case CPU_DEAD_FROZEN:
5019 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005020 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005021 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005022 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005023 break;
5024 default:
5025 break;
5026 }
5027 return NOTIFY_OK;
5028}
5029#endif
5030
NeilBrownd1688a62011-10-11 16:49:52 +11005031static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005032{
5033 unsigned long cpu;
5034 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005035 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005036 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005037 int err;
5038
Dan Williams36d1c642009-07-14 11:48:22 -07005039 allcpus = alloc_percpu(struct raid5_percpu);
5040 if (!allcpus)
5041 return -ENOMEM;
5042 conf->percpu = allcpus;
5043
5044 get_online_cpus();
5045 err = 0;
5046 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005047 if (conf->level == 6) {
5048 spare_page = alloc_page(GFP_KERNEL);
5049 if (!spare_page) {
5050 err = -ENOMEM;
5051 break;
5052 }
5053 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5054 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005055 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005056 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005057 err = -ENOMEM;
5058 break;
5059 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005060 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005061 }
5062#ifdef CONFIG_HOTPLUG_CPU
5063 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5064 conf->cpu_notify.priority = 0;
5065 if (err == 0)
5066 err = register_cpu_notifier(&conf->cpu_notify);
5067#endif
5068 put_online_cpus();
5069
5070 return err;
5071}
5072
NeilBrownd1688a62011-10-11 16:49:52 +11005073static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074{
NeilBrownd1688a62011-10-11 16:49:52 +11005075 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005076 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005077 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005079 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080
NeilBrown91adb562009-03-31 14:39:39 +11005081 if (mddev->new_level != 5
5082 && mddev->new_level != 4
5083 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005084 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005085 mdname(mddev), mddev->new_level);
5086 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 }
NeilBrown91adb562009-03-31 14:39:39 +11005088 if ((mddev->new_level == 5
5089 && !algorithm_valid_raid5(mddev->new_layout)) ||
5090 (mddev->new_level == 6
5091 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005092 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005093 mdname(mddev), mddev->new_layout);
5094 return ERR_PTR(-EIO);
5095 }
5096 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005097 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005098 mdname(mddev), mddev->raid_disks);
5099 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
Andre Noll664e7c42009-06-18 08:45:27 +10005102 if (!mddev->new_chunk_sectors ||
5103 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5104 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005105 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5106 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005107 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005108 }
5109
NeilBrownd1688a62011-10-11 16:49:52 +11005110 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005111 if (conf == NULL)
5112 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005113 spin_lock_init(&conf->device_lock);
5114 init_waitqueue_head(&conf->wait_for_stripe);
5115 init_waitqueue_head(&conf->wait_for_overlap);
5116 INIT_LIST_HEAD(&conf->handle_list);
5117 INIT_LIST_HEAD(&conf->hold_list);
5118 INIT_LIST_HEAD(&conf->delayed_list);
5119 INIT_LIST_HEAD(&conf->bitmap_list);
5120 INIT_LIST_HEAD(&conf->inactive_list);
5121 atomic_set(&conf->active_stripes, 0);
5122 atomic_set(&conf->preread_active_stripes, 0);
5123 atomic_set(&conf->active_aligned_reads, 0);
5124 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005125 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005126
5127 conf->raid_disks = mddev->raid_disks;
5128 if (mddev->reshape_position == MaxSector)
5129 conf->previous_raid_disks = mddev->raid_disks;
5130 else
5131 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005132 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5133 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005134
NeilBrown5e5e3e72009-10-16 16:35:30 +11005135 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005136 GFP_KERNEL);
5137 if (!conf->disks)
5138 goto abort;
5139
5140 conf->mddev = mddev;
5141
5142 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5143 goto abort;
5144
Dan Williams36d1c642009-07-14 11:48:22 -07005145 conf->level = mddev->new_level;
5146 if (raid5_alloc_percpu(conf) != 0)
5147 goto abort;
5148
NeilBrown0c55e022010-05-03 14:09:02 +10005149 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005150
NeilBrowndafb20f2012-03-19 12:46:39 +11005151 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005152 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005153 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005154 || raid_disk < 0)
5155 continue;
5156 disk = conf->disks + raid_disk;
5157
NeilBrown17045f52011-12-23 10:17:53 +11005158 if (test_bit(Replacement, &rdev->flags)) {
5159 if (disk->replacement)
5160 goto abort;
5161 disk->replacement = rdev;
5162 } else {
5163 if (disk->rdev)
5164 goto abort;
5165 disk->rdev = rdev;
5166 }
NeilBrown91adb562009-03-31 14:39:39 +11005167
5168 if (test_bit(In_sync, &rdev->flags)) {
5169 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005170 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5171 " disk %d\n",
5172 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005173 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005174 /* Cannot rely on bitmap to complete recovery */
5175 conf->fullsync = 1;
5176 }
5177
Andre Noll09c9e5f2009-06-18 08:45:55 +10005178 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005179 conf->level = mddev->new_level;
5180 if (conf->level == 6)
5181 conf->max_degraded = 2;
5182 else
5183 conf->max_degraded = 1;
5184 conf->algorithm = mddev->new_layout;
5185 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005186 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005187 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005188 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005189 conf->prev_algo = mddev->layout;
5190 }
NeilBrown91adb562009-03-31 14:39:39 +11005191
5192 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005193 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005194 if (grow_stripes(conf, conf->max_nr_stripes)) {
5195 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005196 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5197 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005198 goto abort;
5199 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005200 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5201 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005202
NeilBrown02326052012-07-03 15:56:52 +10005203 sprintf(pers_name, "raid%d", mddev->new_level);
5204 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005205 if (!conf->thread) {
5206 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005207 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005208 mdname(mddev));
5209 goto abort;
5210 }
5211
5212 return conf;
5213
5214 abort:
5215 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005216 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005217 return ERR_PTR(-EIO);
5218 } else
5219 return ERR_PTR(-ENOMEM);
5220}
5221
NeilBrownc148ffd2009-11-13 17:47:00 +11005222
5223static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5224{
5225 switch (algo) {
5226 case ALGORITHM_PARITY_0:
5227 if (raid_disk < max_degraded)
5228 return 1;
5229 break;
5230 case ALGORITHM_PARITY_N:
5231 if (raid_disk >= raid_disks - max_degraded)
5232 return 1;
5233 break;
5234 case ALGORITHM_PARITY_0_6:
5235 if (raid_disk == 0 ||
5236 raid_disk == raid_disks - 1)
5237 return 1;
5238 break;
5239 case ALGORITHM_LEFT_ASYMMETRIC_6:
5240 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5241 case ALGORITHM_LEFT_SYMMETRIC_6:
5242 case ALGORITHM_RIGHT_SYMMETRIC_6:
5243 if (raid_disk == raid_disks - 1)
5244 return 1;
5245 }
5246 return 0;
5247}
5248
NeilBrownfd01b882011-10-11 16:47:53 +11005249static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005250{
NeilBrownd1688a62011-10-11 16:49:52 +11005251 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005252 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005253 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005254 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005255 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005256 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005257 long long min_offset_diff = 0;
5258 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005259
Andre Noll8c6ac862009-06-18 08:48:06 +10005260 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005261 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005262 " -- starting background reconstruction\n",
5263 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005264
5265 rdev_for_each(rdev, mddev) {
5266 long long diff;
5267 if (rdev->raid_disk < 0)
5268 continue;
5269 diff = (rdev->new_data_offset - rdev->data_offset);
5270 if (first) {
5271 min_offset_diff = diff;
5272 first = 0;
5273 } else if (mddev->reshape_backwards &&
5274 diff < min_offset_diff)
5275 min_offset_diff = diff;
5276 else if (!mddev->reshape_backwards &&
5277 diff > min_offset_diff)
5278 min_offset_diff = diff;
5279 }
5280
NeilBrownf6705572006-03-27 01:18:11 -08005281 if (mddev->reshape_position != MaxSector) {
5282 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005283 * Difficulties arise if the stripe we would write to
5284 * next is at or after the stripe we would read from next.
5285 * For a reshape that changes the number of devices, this
5286 * is only possible for a very short time, and mdadm makes
5287 * sure that time appears to have past before assembling
5288 * the array. So we fail if that time hasn't passed.
5289 * For a reshape that keeps the number of devices the same
5290 * mdadm must be monitoring the reshape can keeping the
5291 * critical areas read-only and backed up. It will start
5292 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005293 */
5294 sector_t here_new, here_old;
5295 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005296 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005297
NeilBrown88ce4932009-03-31 15:24:23 +11005298 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005299 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005300 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005301 mdname(mddev));
5302 return -EINVAL;
5303 }
NeilBrownf6705572006-03-27 01:18:11 -08005304 old_disks = mddev->raid_disks - mddev->delta_disks;
5305 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005306 * further up in new geometry must map after here in old
5307 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005308 */
5309 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005310 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005311 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005312 printk(KERN_ERR "md/raid:%s: reshape_position not "
5313 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005314 return -EINVAL;
5315 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005316 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005317 /* here_new is the stripe we will write to */
5318 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005319 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005320 (old_disks-max_degraded));
5321 /* here_old is the first stripe that we might need to read
5322 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005323 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005324 if ((here_new * mddev->new_chunk_sectors !=
5325 here_old * mddev->chunk_sectors)) {
5326 printk(KERN_ERR "md/raid:%s: reshape position is"
5327 " confused - aborting\n", mdname(mddev));
5328 return -EINVAL;
5329 }
NeilBrown67ac6012009-08-13 10:06:24 +10005330 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005331 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005332 * and taking constant backups.
5333 * mdadm always starts a situation like this in
5334 * readonly mode so it can take control before
5335 * allowing any writes. So just check for that.
5336 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005337 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5338 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5339 /* not really in-place - so OK */;
5340 else if (mddev->ro == 0) {
5341 printk(KERN_ERR "md/raid:%s: in-place reshape "
5342 "must be started in read-only mode "
5343 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005344 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005345 return -EINVAL;
5346 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005347 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005348 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005349 here_old * mddev->chunk_sectors)
5350 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005351 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005352 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005353 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5354 "auto-recovery - aborting.\n",
5355 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005356 return -EINVAL;
5357 }
NeilBrown0c55e022010-05-03 14:09:02 +10005358 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5359 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005360 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005361 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005362 BUG_ON(mddev->level != mddev->new_level);
5363 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005364 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005365 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005366 }
5367
NeilBrown245f46c2009-03-31 14:39:39 +11005368 if (mddev->private == NULL)
5369 conf = setup_conf(mddev);
5370 else
5371 conf = mddev->private;
5372
NeilBrown91adb562009-03-31 14:39:39 +11005373 if (IS_ERR(conf))
5374 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005375
NeilBrownb5254dd2012-05-21 09:27:01 +10005376 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005377 mddev->thread = conf->thread;
5378 conf->thread = NULL;
5379 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
NeilBrown17045f52011-12-23 10:17:53 +11005381 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5382 i++) {
5383 rdev = conf->disks[i].rdev;
5384 if (!rdev && conf->disks[i].replacement) {
5385 /* The replacement is all we have yet */
5386 rdev = conf->disks[i].replacement;
5387 conf->disks[i].replacement = NULL;
5388 clear_bit(Replacement, &rdev->flags);
5389 conf->disks[i].rdev = rdev;
5390 }
5391 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005392 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005393 if (conf->disks[i].replacement &&
5394 conf->reshape_progress != MaxSector) {
5395 /* replacements and reshape simply do not mix. */
5396 printk(KERN_ERR "md: cannot handle concurrent "
5397 "replacement and reshape.\n");
5398 goto abort;
5399 }
NeilBrown2f115882010-06-17 17:41:03 +10005400 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005401 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005402 continue;
5403 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005404 /* This disc is not fully in-sync. However if it
5405 * just stored parity (beyond the recovery_offset),
5406 * when we don't need to be concerned about the
5407 * array being dirty.
5408 * When reshape goes 'backwards', we never have
5409 * partially completed devices, so we only need
5410 * to worry about reshape going forwards.
5411 */
5412 /* Hack because v0.91 doesn't store recovery_offset properly. */
5413 if (mddev->major_version == 0 &&
5414 mddev->minor_version > 90)
5415 rdev->recovery_offset = reshape_offset;
5416
NeilBrownc148ffd2009-11-13 17:47:00 +11005417 if (rdev->recovery_offset < reshape_offset) {
5418 /* We need to check old and new layout */
5419 if (!only_parity(rdev->raid_disk,
5420 conf->algorithm,
5421 conf->raid_disks,
5422 conf->max_degraded))
5423 continue;
5424 }
5425 if (!only_parity(rdev->raid_disk,
5426 conf->prev_algo,
5427 conf->previous_raid_disks,
5428 conf->max_degraded))
5429 continue;
5430 dirty_parity_disks++;
5431 }
NeilBrown91adb562009-03-31 14:39:39 +11005432
NeilBrown17045f52011-12-23 10:17:53 +11005433 /*
5434 * 0 for a fully functional array, 1 or 2 for a degraded array.
5435 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005436 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437
NeilBrown674806d2010-06-16 17:17:53 +10005438 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005439 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005441 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 goto abort;
5443 }
5444
NeilBrown91adb562009-03-31 14:39:39 +11005445 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005446 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005447 mddev->resync_max_sectors = mddev->dev_sectors;
5448
NeilBrownc148ffd2009-11-13 17:47:00 +11005449 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005451 if (mddev->ok_start_degraded)
5452 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005453 "md/raid:%s: starting dirty degraded array"
5454 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005455 mdname(mddev));
5456 else {
5457 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005458 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005459 mdname(mddev));
5460 goto abort;
5461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 }
5463
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005465 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5466 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005467 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5468 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 else
NeilBrown0c55e022010-05-03 14:09:02 +10005470 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5471 " out of %d devices, algorithm %d\n",
5472 mdname(mddev), conf->level,
5473 mddev->raid_disks - mddev->degraded,
5474 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475
5476 print_raid5_conf(conf);
5477
NeilBrownfef9c612009-03-31 15:16:46 +11005478 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005479 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005480 atomic_set(&conf->reshape_stripes, 0);
5481 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5482 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5483 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5484 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5485 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005486 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005487 }
5488
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489
5490 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005491 if (mddev->to_remove == &raid5_attrs_group)
5492 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005493 else if (mddev->kobj.sd &&
5494 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005495 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005496 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005497 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005498 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5499
5500 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005501 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005502 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005503 /* read-ahead size must cover two whole stripes, which
5504 * is 2 * (datadisks) * chunksize where 'n' is the
5505 * number of raid devices
5506 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5508 int stripe = data_disks *
5509 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5510 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5511 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005512
5513 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005514
5515 mddev->queue->backing_dev_info.congested_data = mddev;
5516 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005517
5518 chunk_size = mddev->chunk_sectors << 9;
5519 blk_queue_io_min(mddev->queue, chunk_size);
5520 blk_queue_io_opt(mddev->queue, chunk_size *
5521 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005522 /*
5523 * We can only discard a whole stripe. It doesn't make sense to
5524 * discard data disk but write parity disk
5525 */
5526 stripe = stripe * PAGE_SIZE;
5527 mddev->queue->limits.discard_alignment = stripe;
5528 mddev->queue->limits.discard_granularity = stripe;
5529 /*
5530 * unaligned part of discard request will be ignored, so can't
5531 * guarantee discard_zerors_data
5532 */
5533 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005534
NeilBrown05616be2012-05-21 09:27:00 +10005535 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005536 disk_stack_limits(mddev->gendisk, rdev->bdev,
5537 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005538 disk_stack_limits(mddev->gendisk, rdev->bdev,
5539 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005540 /*
5541 * discard_zeroes_data is required, otherwise data
5542 * could be lost. Consider a scenario: discard a stripe
5543 * (the stripe could be inconsistent if
5544 * discard_zeroes_data is 0); write one disk of the
5545 * stripe (the stripe could be inconsistent again
5546 * depending on which disks are used to calculate
5547 * parity); the disk is broken; The stripe data of this
5548 * disk is lost.
5549 */
5550 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5551 !bdev_get_queue(rdev->bdev)->
5552 limits.discard_zeroes_data)
5553 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10005554 }
Shaohua Li620125f2012-10-11 13:49:05 +11005555
5556 if (discard_supported &&
5557 mddev->queue->limits.max_discard_sectors >= stripe &&
5558 mddev->queue->limits.discard_granularity >= stripe)
5559 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5560 mddev->queue);
5561 else
5562 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5563 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 }
5565
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 return 0;
5567abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005568 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005569 print_raid5_conf(conf);
5570 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005572 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 return -EIO;
5574}
5575
NeilBrownfd01b882011-10-11 16:47:53 +11005576static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577{
NeilBrownd1688a62011-10-11 16:49:52 +11005578 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579
NeilBrown01f96c02011-09-21 15:30:20 +10005580 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005581 if (mddev->queue)
5582 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005583 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005584 mddev->private = NULL;
5585 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 return 0;
5587}
5588
NeilBrownfd01b882011-10-11 16:47:53 +11005589static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590{
NeilBrownd1688a62011-10-11 16:49:52 +11005591 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 int i;
5593
Andre Noll9d8f0362009-06-18 08:45:01 +10005594 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5595 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005596 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 for (i = 0; i < conf->raid_disks; i++)
5598 seq_printf (seq, "%s",
5599 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005600 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602}
5603
NeilBrownd1688a62011-10-11 16:49:52 +11005604static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605{
5606 int i;
5607 struct disk_info *tmp;
5608
NeilBrown0c55e022010-05-03 14:09:02 +10005609 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 if (!conf) {
5611 printk("(conf==NULL)\n");
5612 return;
5613 }
NeilBrown0c55e022010-05-03 14:09:02 +10005614 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5615 conf->raid_disks,
5616 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
5618 for (i = 0; i < conf->raid_disks; i++) {
5619 char b[BDEVNAME_SIZE];
5620 tmp = conf->disks + i;
5621 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005622 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5623 i, !test_bit(Faulty, &tmp->rdev->flags),
5624 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 }
5626}
5627
NeilBrownfd01b882011-10-11 16:47:53 +11005628static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629{
5630 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005631 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005633 int count = 0;
5634 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635
5636 for (i = 0; i < conf->raid_disks; i++) {
5637 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005638 if (tmp->replacement
5639 && tmp->replacement->recovery_offset == MaxSector
5640 && !test_bit(Faulty, &tmp->replacement->flags)
5641 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5642 /* Replacement has just become active. */
5643 if (!tmp->rdev
5644 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5645 count++;
5646 if (tmp->rdev) {
5647 /* Replaced device not technically faulty,
5648 * but we need to be sure it gets removed
5649 * and never re-added.
5650 */
5651 set_bit(Faulty, &tmp->rdev->flags);
5652 sysfs_notify_dirent_safe(
5653 tmp->rdev->sysfs_state);
5654 }
5655 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5656 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005657 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005658 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005659 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005660 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005661 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 }
5663 }
NeilBrown6b965622010-08-18 11:56:59 +10005664 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005665 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005666 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005668 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669}
5670
NeilBrownb8321b62011-12-23 10:17:51 +11005671static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672{
NeilBrownd1688a62011-10-11 16:49:52 +11005673 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005675 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005676 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 struct disk_info *p = conf->disks + number;
5678
5679 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005680 if (rdev == p->rdev)
5681 rdevp = &p->rdev;
5682 else if (rdev == p->replacement)
5683 rdevp = &p->replacement;
5684 else
5685 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005686
NeilBrown657e3e42011-12-23 10:17:52 +11005687 if (number >= conf->raid_disks &&
5688 conf->reshape_progress == MaxSector)
5689 clear_bit(In_sync, &rdev->flags);
5690
5691 if (test_bit(In_sync, &rdev->flags) ||
5692 atomic_read(&rdev->nr_pending)) {
5693 err = -EBUSY;
5694 goto abort;
5695 }
5696 /* Only remove non-faulty devices if recovery
5697 * isn't possible.
5698 */
5699 if (!test_bit(Faulty, &rdev->flags) &&
5700 mddev->recovery_disabled != conf->recovery_disabled &&
5701 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005702 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005703 number < conf->raid_disks) {
5704 err = -EBUSY;
5705 goto abort;
5706 }
5707 *rdevp = NULL;
5708 synchronize_rcu();
5709 if (atomic_read(&rdev->nr_pending)) {
5710 /* lost the race, try later */
5711 err = -EBUSY;
5712 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005713 } else if (p->replacement) {
5714 /* We must have just cleared 'rdev' */
5715 p->rdev = p->replacement;
5716 clear_bit(Replacement, &p->replacement->flags);
5717 smp_mb(); /* Make sure other CPUs may see both as identical
5718 * but will never see neither - if they are careful
5719 */
5720 p->replacement = NULL;
5721 clear_bit(WantReplacement, &rdev->flags);
5722 } else
5723 /* We might have just removed the Replacement as faulty-
5724 * clear the bit just in case
5725 */
5726 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727abort:
5728
5729 print_raid5_conf(conf);
5730 return err;
5731}
5732
NeilBrownfd01b882011-10-11 16:47:53 +11005733static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734{
NeilBrownd1688a62011-10-11 16:49:52 +11005735 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005736 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 int disk;
5738 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005739 int first = 0;
5740 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741
NeilBrown7f0da592011-07-28 11:39:22 +10005742 if (mddev->recovery_disabled == conf->recovery_disabled)
5743 return -EBUSY;
5744
NeilBrowndc10c642012-03-19 12:46:37 +11005745 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005747 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748
Neil Brown6c2fce22008-06-28 08:31:31 +10005749 if (rdev->raid_disk >= 0)
5750 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751
5752 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005753 * find the disk ... but prefer rdev->saved_raid_disk
5754 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005756 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005757 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005758 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005759 first = rdev->saved_raid_disk;
5760
5761 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005762 p = conf->disks + disk;
5763 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005764 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005766 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005767 if (rdev->saved_raid_disk != disk)
5768 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005769 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005770 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005772 }
5773 for (disk = first; disk <= last; disk++) {
5774 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005775 if (test_bit(WantReplacement, &p->rdev->flags) &&
5776 p->replacement == NULL) {
5777 clear_bit(In_sync, &rdev->flags);
5778 set_bit(Replacement, &rdev->flags);
5779 rdev->raid_disk = disk;
5780 err = 0;
5781 conf->fullsync = 1;
5782 rcu_assign_pointer(p->replacement, rdev);
5783 break;
5784 }
5785 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005786out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005788 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789}
5790
NeilBrownfd01b882011-10-11 16:47:53 +11005791static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792{
5793 /* no resync is happening, and there is enough space
5794 * on all devices, so we can resize.
5795 * We need to make sure resync covers any new space.
5796 * If the array is shrinking we should possibly wait until
5797 * any io in the removed space completes, but it hardly seems
5798 * worth it.
5799 */
NeilBrowna4a61252012-05-22 13:55:27 +10005800 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005801 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005802 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5803 if (mddev->external_size &&
5804 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005805 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005806 if (mddev->bitmap) {
5807 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5808 if (ret)
5809 return ret;
5810 }
5811 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005812 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005813 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005814 if (sectors > mddev->dev_sectors &&
5815 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005816 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5818 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005819 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005820 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 return 0;
5822}
5823
NeilBrownfd01b882011-10-11 16:47:53 +11005824static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005825{
5826 /* Can only proceed if there are plenty of stripe_heads.
5827 * We need a minimum of one full stripe,, and for sensible progress
5828 * it is best to have about 4 times that.
5829 * If we require 4 times, then the default 256 4K stripe_heads will
5830 * allow for chunk sizes up to 256K, which is probably OK.
5831 * If the chunk size is greater, user-space should request more
5832 * stripe_heads first.
5833 */
NeilBrownd1688a62011-10-11 16:49:52 +11005834 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005835 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5836 > conf->max_nr_stripes ||
5837 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5838 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005839 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5840 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005841 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5842 / STRIPE_SIZE)*4);
5843 return 0;
5844 }
5845 return 1;
5846}
5847
NeilBrownfd01b882011-10-11 16:47:53 +11005848static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005849{
NeilBrownd1688a62011-10-11 16:49:52 +11005850 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005851
NeilBrown88ce4932009-03-31 15:24:23 +11005852 if (mddev->delta_disks == 0 &&
5853 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005854 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005855 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005856 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005857 return -EINVAL;
5858 if (mddev->delta_disks < 0) {
5859 /* We might be able to shrink, but the devices must
5860 * be made bigger first.
5861 * For raid6, 4 is the minimum size.
5862 * Otherwise 2 is the minimum
5863 */
5864 int min = 2;
5865 if (mddev->level == 6)
5866 min = 4;
5867 if (mddev->raid_disks + mddev->delta_disks < min)
5868 return -EINVAL;
5869 }
NeilBrown29269552006-03-27 01:18:10 -08005870
NeilBrown01ee22b2009-06-18 08:47:20 +10005871 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005872 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005873
NeilBrownec32a2b2009-03-31 15:17:38 +11005874 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
NeilBrown63c70c42006-03-27 01:18:13 -08005875}
5876
NeilBrownfd01b882011-10-11 16:47:53 +11005877static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005878{
NeilBrownd1688a62011-10-11 16:49:52 +11005879 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005880 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005881 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005882 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005883
NeilBrownf4168852007-02-28 20:11:53 -08005884 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005885 return -EBUSY;
5886
NeilBrown01ee22b2009-06-18 08:47:20 +10005887 if (!check_stripe_cache(mddev))
5888 return -ENOSPC;
5889
NeilBrown30b67642012-05-22 13:55:28 +10005890 if (has_failed(conf))
5891 return -EINVAL;
5892
NeilBrownc6563a82012-05-21 09:27:00 +10005893 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005894 if (!test_bit(In_sync, &rdev->flags)
5895 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005896 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005897 }
NeilBrown63c70c42006-03-27 01:18:13 -08005898
NeilBrownf4168852007-02-28 20:11:53 -08005899 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005900 /* Not enough devices even to make a degraded array
5901 * of that size
5902 */
5903 return -EINVAL;
5904
NeilBrownec32a2b2009-03-31 15:17:38 +11005905 /* Refuse to reduce size of the array. Any reductions in
5906 * array size must be through explicit setting of array_size
5907 * attribute.
5908 */
5909 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5910 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005911 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005912 "before number of disks\n", mdname(mddev));
5913 return -EINVAL;
5914 }
5915
NeilBrownf6705572006-03-27 01:18:11 -08005916 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005917 spin_lock_irq(&conf->device_lock);
5918 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005919 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005920 conf->prev_chunk_sectors = conf->chunk_sectors;
5921 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005922 conf->prev_algo = conf->algorithm;
5923 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005924 conf->generation++;
5925 /* Code that selects data_offset needs to see the generation update
5926 * if reshape_progress has been set - so a memory barrier needed.
5927 */
5928 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005929 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005930 conf->reshape_progress = raid5_size(mddev, 0, 0);
5931 else
5932 conf->reshape_progress = 0;
5933 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005934 spin_unlock_irq(&conf->device_lock);
5935
5936 /* Add some new drives, as many as will fit.
5937 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005938 * Don't add devices if we are reducing the number of
5939 * devices in the array. This is because it is not possible
5940 * to correctly record the "partially reconstructed" state of
5941 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005942 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005943 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005944 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005945 if (rdev->raid_disk < 0 &&
5946 !test_bit(Faulty, &rdev->flags)) {
5947 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005948 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005949 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005950 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005951 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005952 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005953
5954 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005955 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005956 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005957 } else if (rdev->raid_disk >= conf->previous_raid_disks
5958 && !test_bit(Faulty, &rdev->flags)) {
5959 /* This is a spare that was manually added */
5960 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005961 }
NeilBrown29269552006-03-27 01:18:10 -08005962
NeilBrown87a8dec2011-01-31 11:57:43 +11005963 /* When a reshape changes the number of devices,
5964 * ->degraded is measured against the larger of the
5965 * pre and post number of devices.
5966 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005967 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005968 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005969 spin_unlock_irqrestore(&conf->device_lock, flags);
5970 }
NeilBrown63c70c42006-03-27 01:18:13 -08005971 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005972 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005973 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005974
NeilBrown29269552006-03-27 01:18:10 -08005975 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5976 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5977 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5978 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5979 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005980 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005981 if (!mddev->sync_thread) {
5982 mddev->recovery = 0;
5983 spin_lock_irq(&conf->device_lock);
5984 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005985 rdev_for_each(rdev, mddev)
5986 rdev->new_data_offset = rdev->data_offset;
5987 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005988 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005989 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005990 spin_unlock_irq(&conf->device_lock);
5991 return -EAGAIN;
5992 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005993 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005994 md_wakeup_thread(mddev->sync_thread);
5995 md_new_event(mddev);
5996 return 0;
5997}
NeilBrown29269552006-03-27 01:18:10 -08005998
NeilBrownec32a2b2009-03-31 15:17:38 +11005999/* This is called from the reshape thread and should make any
6000 * changes needed in 'conf'
6001 */
NeilBrownd1688a62011-10-11 16:49:52 +11006002static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006003{
NeilBrown29269552006-03-27 01:18:10 -08006004
NeilBrownf6705572006-03-27 01:18:11 -08006005 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006006 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006007
NeilBrownf6705572006-03-27 01:18:11 -08006008 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006009 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006010 rdev_for_each(rdev, conf->mddev)
6011 rdev->data_offset = rdev->new_data_offset;
6012 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006013 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006014 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006015 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006016
6017 /* read-ahead size must cover two whole stripes, which is
6018 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6019 */
NeilBrown4a5add42010-06-01 19:37:28 +10006020 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006021 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006022 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006023 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006024 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6025 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6026 }
NeilBrown29269552006-03-27 01:18:10 -08006027 }
NeilBrown29269552006-03-27 01:18:10 -08006028}
6029
NeilBrownec32a2b2009-03-31 15:17:38 +11006030/* This is called from the raid5d thread with mddev_lock held.
6031 * It makes config changes to the device.
6032 */
NeilBrownfd01b882011-10-11 16:47:53 +11006033static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006034{
NeilBrownd1688a62011-10-11 16:49:52 +11006035 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006036
6037 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6038
NeilBrownec32a2b2009-03-31 15:17:38 +11006039 if (mddev->delta_disks > 0) {
6040 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6041 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006042 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006043 } else {
6044 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006045 spin_lock_irq(&conf->device_lock);
6046 mddev->degraded = calc_degraded(conf);
6047 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006048 for (d = conf->raid_disks ;
6049 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006050 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006051 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006052 if (rdev)
6053 clear_bit(In_sync, &rdev->flags);
6054 rdev = conf->disks[d].replacement;
6055 if (rdev)
6056 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006057 }
NeilBrowncea9c222009-03-31 15:15:05 +11006058 }
NeilBrown88ce4932009-03-31 15:24:23 +11006059 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006060 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006061 mddev->reshape_position = MaxSector;
6062 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006063 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006064 }
6065}
6066
NeilBrownfd01b882011-10-11 16:47:53 +11006067static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006068{
NeilBrownd1688a62011-10-11 16:49:52 +11006069 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006070
6071 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006072 case 2: /* resume for a suspend */
6073 wake_up(&conf->wait_for_overlap);
6074 break;
6075
NeilBrown72626682005-09-09 16:23:54 -07006076 case 1: /* stop all writes */
6077 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006078 /* '2' tells resync/reshape to pause so that all
6079 * active stripes can drain
6080 */
6081 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006082 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006083 atomic_read(&conf->active_stripes) == 0 &&
6084 atomic_read(&conf->active_aligned_reads) == 0,
NeilBrown72626682005-09-09 16:23:54 -07006085 conf->device_lock, /* nothing */);
NeilBrown64bd6602009-08-03 10:59:58 +10006086 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006087 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006088 /* allow reshape to continue */
6089 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006090 break;
6091
6092 case 0: /* re-enable writes */
6093 spin_lock_irq(&conf->device_lock);
6094 conf->quiesce = 0;
6095 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006096 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006097 spin_unlock_irq(&conf->device_lock);
6098 break;
6099 }
NeilBrown72626682005-09-09 16:23:54 -07006100}
NeilBrownb15c2e52006-01-06 00:20:16 -08006101
NeilBrownd562b0c2009-03-31 14:39:39 +11006102
NeilBrownfd01b882011-10-11 16:47:53 +11006103static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006104{
NeilBrowne373ab12011-10-11 16:48:59 +11006105 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006106 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006107
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006108 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006109 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006110 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6111 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006112 return ERR_PTR(-EINVAL);
6113 }
6114
NeilBrowne373ab12011-10-11 16:48:59 +11006115 sectors = raid0_conf->strip_zone[0].zone_end;
6116 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006117 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006118 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006119 mddev->new_layout = ALGORITHM_PARITY_N;
6120 mddev->new_chunk_sectors = mddev->chunk_sectors;
6121 mddev->raid_disks += 1;
6122 mddev->delta_disks = 1;
6123 /* make sure it will be not marked as dirty */
6124 mddev->recovery_cp = MaxSector;
6125
6126 return setup_conf(mddev);
6127}
6128
6129
NeilBrownfd01b882011-10-11 16:47:53 +11006130static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006131{
6132 int chunksect;
6133
6134 if (mddev->raid_disks != 2 ||
6135 mddev->degraded > 1)
6136 return ERR_PTR(-EINVAL);
6137
6138 /* Should check if there are write-behind devices? */
6139
6140 chunksect = 64*2; /* 64K by default */
6141
6142 /* The array must be an exact multiple of chunksize */
6143 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6144 chunksect >>= 1;
6145
6146 if ((chunksect<<9) < STRIPE_SIZE)
6147 /* array size does not allow a suitable chunk size */
6148 return ERR_PTR(-EINVAL);
6149
6150 mddev->new_level = 5;
6151 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006152 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006153
6154 return setup_conf(mddev);
6155}
6156
NeilBrownfd01b882011-10-11 16:47:53 +11006157static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006158{
6159 int new_layout;
6160
6161 switch (mddev->layout) {
6162 case ALGORITHM_LEFT_ASYMMETRIC_6:
6163 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6164 break;
6165 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6166 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6167 break;
6168 case ALGORITHM_LEFT_SYMMETRIC_6:
6169 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6170 break;
6171 case ALGORITHM_RIGHT_SYMMETRIC_6:
6172 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6173 break;
6174 case ALGORITHM_PARITY_0_6:
6175 new_layout = ALGORITHM_PARITY_0;
6176 break;
6177 case ALGORITHM_PARITY_N:
6178 new_layout = ALGORITHM_PARITY_N;
6179 break;
6180 default:
6181 return ERR_PTR(-EINVAL);
6182 }
6183 mddev->new_level = 5;
6184 mddev->new_layout = new_layout;
6185 mddev->delta_disks = -1;
6186 mddev->raid_disks -= 1;
6187 return setup_conf(mddev);
6188}
6189
NeilBrownd562b0c2009-03-31 14:39:39 +11006190
NeilBrownfd01b882011-10-11 16:47:53 +11006191static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006192{
NeilBrown88ce4932009-03-31 15:24:23 +11006193 /* For a 2-drive array, the layout and chunk size can be changed
6194 * immediately as not restriping is needed.
6195 * For larger arrays we record the new value - after validation
6196 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006197 */
NeilBrownd1688a62011-10-11 16:49:52 +11006198 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006199 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006200
NeilBrown597a7112009-06-18 08:47:42 +10006201 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006202 return -EINVAL;
6203 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006204 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006205 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006206 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006207 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006208 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006209 /* not factor of array size */
6210 return -EINVAL;
6211 }
6212
6213 /* They look valid */
6214
NeilBrown88ce4932009-03-31 15:24:23 +11006215 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006216 /* can make the change immediately */
6217 if (mddev->new_layout >= 0) {
6218 conf->algorithm = mddev->new_layout;
6219 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006220 }
6221 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006222 conf->chunk_sectors = new_chunk ;
6223 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006224 }
6225 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6226 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006227 }
NeilBrown50ac1682009-06-18 08:47:55 +10006228 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006229}
6230
NeilBrownfd01b882011-10-11 16:47:53 +11006231static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006232{
NeilBrown597a7112009-06-18 08:47:42 +10006233 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006234
NeilBrown597a7112009-06-18 08:47:42 +10006235 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006236 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006237 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006238 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006239 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006240 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006241 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006242 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006243 /* not factor of array size */
6244 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006245 }
NeilBrown88ce4932009-03-31 15:24:23 +11006246
6247 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006248 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006249}
6250
NeilBrownfd01b882011-10-11 16:47:53 +11006251static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006252{
6253 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006254 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006255 * raid1 - if there are two drives. We need to know the chunk size
6256 * raid4 - trivial - just use a raid4 layout.
6257 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006258 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006259 if (mddev->level == 0)
6260 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006261 if (mddev->level == 1)
6262 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006263 if (mddev->level == 4) {
6264 mddev->new_layout = ALGORITHM_PARITY_N;
6265 mddev->new_level = 5;
6266 return setup_conf(mddev);
6267 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006268 if (mddev->level == 6)
6269 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006270
6271 return ERR_PTR(-EINVAL);
6272}
6273
NeilBrownfd01b882011-10-11 16:47:53 +11006274static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006275{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006276 /* raid4 can take over:
6277 * raid0 - if there is only one strip zone
6278 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006279 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006280 if (mddev->level == 0)
6281 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006282 if (mddev->level == 5 &&
6283 mddev->layout == ALGORITHM_PARITY_N) {
6284 mddev->new_layout = 0;
6285 mddev->new_level = 4;
6286 return setup_conf(mddev);
6287 }
6288 return ERR_PTR(-EINVAL);
6289}
NeilBrownd562b0c2009-03-31 14:39:39 +11006290
NeilBrown84fc4b52011-10-11 16:49:58 +11006291static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006292
NeilBrownfd01b882011-10-11 16:47:53 +11006293static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006294{
6295 /* Currently can only take over a raid5. We map the
6296 * personality to an equivalent raid6 personality
6297 * with the Q block at the end.
6298 */
6299 int new_layout;
6300
6301 if (mddev->pers != &raid5_personality)
6302 return ERR_PTR(-EINVAL);
6303 if (mddev->degraded > 1)
6304 return ERR_PTR(-EINVAL);
6305 if (mddev->raid_disks > 253)
6306 return ERR_PTR(-EINVAL);
6307 if (mddev->raid_disks < 3)
6308 return ERR_PTR(-EINVAL);
6309
6310 switch (mddev->layout) {
6311 case ALGORITHM_LEFT_ASYMMETRIC:
6312 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6313 break;
6314 case ALGORITHM_RIGHT_ASYMMETRIC:
6315 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6316 break;
6317 case ALGORITHM_LEFT_SYMMETRIC:
6318 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6319 break;
6320 case ALGORITHM_RIGHT_SYMMETRIC:
6321 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6322 break;
6323 case ALGORITHM_PARITY_0:
6324 new_layout = ALGORITHM_PARITY_0_6;
6325 break;
6326 case ALGORITHM_PARITY_N:
6327 new_layout = ALGORITHM_PARITY_N;
6328 break;
6329 default:
6330 return ERR_PTR(-EINVAL);
6331 }
6332 mddev->new_level = 6;
6333 mddev->new_layout = new_layout;
6334 mddev->delta_disks = 1;
6335 mddev->raid_disks += 1;
6336 return setup_conf(mddev);
6337}
6338
6339
NeilBrown84fc4b52011-10-11 16:49:58 +11006340static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006341{
6342 .name = "raid6",
6343 .level = 6,
6344 .owner = THIS_MODULE,
6345 .make_request = make_request,
6346 .run = run,
6347 .stop = stop,
6348 .status = status,
6349 .error_handler = error,
6350 .hot_add_disk = raid5_add_disk,
6351 .hot_remove_disk= raid5_remove_disk,
6352 .spare_active = raid5_spare_active,
6353 .sync_request = sync_request,
6354 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006355 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006356 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006357 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006358 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006359 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006360 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006361};
NeilBrown84fc4b52011-10-11 16:49:58 +11006362static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363{
6364 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006365 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 .owner = THIS_MODULE,
6367 .make_request = make_request,
6368 .run = run,
6369 .stop = stop,
6370 .status = status,
6371 .error_handler = error,
6372 .hot_add_disk = raid5_add_disk,
6373 .hot_remove_disk= raid5_remove_disk,
6374 .spare_active = raid5_spare_active,
6375 .sync_request = sync_request,
6376 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006377 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006378 .check_reshape = raid5_check_reshape,
6379 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006380 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006381 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006382 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383};
6384
NeilBrown84fc4b52011-10-11 16:49:58 +11006385static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386{
NeilBrown2604b702006-01-06 00:20:36 -08006387 .name = "raid4",
6388 .level = 4,
6389 .owner = THIS_MODULE,
6390 .make_request = make_request,
6391 .run = run,
6392 .stop = stop,
6393 .status = status,
6394 .error_handler = error,
6395 .hot_add_disk = raid5_add_disk,
6396 .hot_remove_disk= raid5_remove_disk,
6397 .spare_active = raid5_spare_active,
6398 .sync_request = sync_request,
6399 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006400 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006401 .check_reshape = raid5_check_reshape,
6402 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006403 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006404 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006405 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006406};
6407
6408static int __init raid5_init(void)
6409{
NeilBrown16a53ec2006-06-26 00:27:38 -07006410 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006411 register_md_personality(&raid5_personality);
6412 register_md_personality(&raid4_personality);
6413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414}
6415
NeilBrown2604b702006-01-06 00:20:36 -08006416static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417{
NeilBrown16a53ec2006-06-26 00:27:38 -07006418 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006419 unregister_md_personality(&raid5_personality);
6420 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421}
6422
6423module_init(raid5_init);
6424module_exit(raid5_exit);
6425MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006426MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006428MODULE_ALIAS("md-raid5");
6429MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006430MODULE_ALIAS("md-level-5");
6431MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006432MODULE_ALIAS("md-personality-8"); /* RAID6 */
6433MODULE_ALIAS("md-raid6");
6434MODULE_ALIAS("md-level-6");
6435
6436/* This used to be two separate modules, they were: */
6437MODULE_ALIAS("raid5");
6438MODULE_ALIAS("raid6");