blob: 2bf617d6f4fd43dbe1a35384334564a62f339ed3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07005 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
NeilBrown16a53ec2006-06-26 00:27:38 -07007 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
15 *
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
NeilBrownae3c20c2006-07-10 04:44:17 -070021/*
22 * BITMAP UNPLUGGING:
23 *
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
27 *
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100030 * conf->seq_write is the number of the last batch successfully written.
31 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070032 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100035 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070036 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
44 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
NeilBrownbff61972009-03-31 14:33:13 +110046#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080047#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110048#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070049#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040050#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070051#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110052#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070053#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100055#include <linux/ratelimit.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110056#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110057#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110058#include "raid0.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110059#include "bitmap.h"
NeilBrown72626682005-09-09 16:23:54 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * Stripe cache
63 */
64
65#define NR_STRIPES 256
66#define STRIPE_SIZE PAGE_SIZE
67#define STRIPE_SHIFT (PAGE_SHIFT - 9)
68#define STRIPE_SECTORS (STRIPE_SIZE>>9)
69#define IO_THRESHOLD 1
Dan Williams8b3e6cd2008-04-28 02:15:53 -070070#define BYPASS_THRESHOLD 1
NeilBrownfccddba2006-01-06 00:20:33 -080071#define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define HASH_MASK (NR_HASH - 1)
73
NeilBrownd1688a62011-10-11 16:49:52 +110074static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110075{
76 int hash = (sect >> STRIPE_SHIFT) & HASH_MASK;
77 return &conf->stripe_hashtbl[hash];
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* bio's attached to a stripe+device for I/O are linked together in bi_sector
81 * order without overlap. There may be several bio's per stripe+device, and
82 * a bio could span several devices.
83 * When walking this list for a particular stripe+device, we must never proceed
84 * beyond a bio that extends past this device, as the next bio might no longer
85 * be valid.
NeilBrowndb298e12011-10-07 14:23:00 +110086 * This function is used to determine the 'next' bio in the list, given the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * of the current stripe+device
88 */
NeilBrowndb298e12011-10-07 14:23:00 +110089static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector)
90{
91 int sectors = bio->bi_size >> 9;
92 if (bio->bi_sector + sectors < sector + STRIPE_SECTORS)
93 return bio->bi_next;
94 else
95 return NULL;
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jens Axboe960e7392008-08-15 10:41:18 +020098/*
Jens Axboe5b99c2f2008-08-15 10:56:11 +020099 * We maintain a biased count of active stripes in the bottom 16 bits of
100 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
Jens Axboe960e7392008-08-15 10:41:18 +0200101 */
Shaohua Lie7836bd62012-07-19 16:01:31 +1000102static inline int raid5_bi_processed_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200103{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000104 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
105 return (atomic_read(segments) >> 16) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200106}
107
Shaohua Lie7836bd62012-07-19 16:01:31 +1000108static inline int raid5_dec_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200109{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000110 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
111 return atomic_sub_return(1, segments) & 0xffff;
Jens Axboe960e7392008-08-15 10:41:18 +0200112}
113
Shaohua Lie7836bd62012-07-19 16:01:31 +1000114static inline void raid5_inc_bi_active_stripes(struct bio *bio)
Jens Axboe960e7392008-08-15 10:41:18 +0200115{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000116 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
117 atomic_inc(segments);
Jens Axboe960e7392008-08-15 10:41:18 +0200118}
119
Shaohua Lie7836bd62012-07-19 16:01:31 +1000120static inline void raid5_set_bi_processed_stripes(struct bio *bio,
121 unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200122{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000123 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
124 int old, new;
Jens Axboe960e7392008-08-15 10:41:18 +0200125
Shaohua Lie7836bd62012-07-19 16:01:31 +1000126 do {
127 old = atomic_read(segments);
128 new = (old & 0xffff) | (cnt << 16);
129 } while (atomic_cmpxchg(segments, old, new) != old);
Jens Axboe960e7392008-08-15 10:41:18 +0200130}
131
Shaohua Lie7836bd62012-07-19 16:01:31 +1000132static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt)
Jens Axboe960e7392008-08-15 10:41:18 +0200133{
Shaohua Lie7836bd62012-07-19 16:01:31 +1000134 atomic_t *segments = (atomic_t *)&bio->bi_phys_segments;
135 atomic_set(segments, cnt);
Jens Axboe960e7392008-08-15 10:41:18 +0200136}
137
NeilBrownd0dabf72009-03-31 14:39:38 +1100138/* Find first data disk in a raid6 stripe */
139static inline int raid6_d0(struct stripe_head *sh)
140{
NeilBrown67cc2b82009-03-31 14:39:38 +1100141 if (sh->ddf_layout)
142 /* ddf always start from first device */
143 return 0;
144 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100145 if (sh->qd_idx == sh->disks - 1)
146 return 0;
147 else
148 return sh->qd_idx + 1;
149}
NeilBrown16a53ec2006-06-26 00:27:38 -0700150static inline int raid6_next_disk(int disk, int raid_disks)
151{
152 disk++;
153 return (disk < raid_disks) ? disk : 0;
154}
Dan Williamsa4456852007-07-09 11:56:43 -0700155
NeilBrownd0dabf72009-03-31 14:39:38 +1100156/* When walking through the disks in a raid5, starting at raid6_d0,
157 * We need to map each disk to a 'slot', where the data disks are slot
158 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
159 * is raid_disks-1. This help does that mapping.
160 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100161static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
162 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100163{
Dan Williams66295422009-10-19 18:09:32 -0700164 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100165
NeilBrowne4424fe2009-10-16 16:27:34 +1100166 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700167 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100168 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100169 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100170 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100171 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100172 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700173 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100174 return slot;
175}
176
Dan Williamsa4456852007-07-09 11:56:43 -0700177static void return_io(struct bio *return_bi)
178{
179 struct bio *bi = return_bi;
180 while (bi) {
Dan Williamsa4456852007-07-09 11:56:43 -0700181
182 return_bi = bi->bi_next;
183 bi->bi_next = NULL;
184 bi->bi_size = 0;
Neil Brown0e13fe232008-06-28 08:31:20 +1000185 bio_endio(bi, 0);
Dan Williamsa4456852007-07-09 11:56:43 -0700186 bi = return_bi;
187 }
188}
189
NeilBrownd1688a62011-10-11 16:49:52 +1100190static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Dan Williams600aa102008-06-28 08:32:05 +1000192static int stripe_operations_active(struct stripe_head *sh)
193{
194 return sh->check_state || sh->reconstruct_state ||
195 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
196 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
197}
198
Shaohua Li4eb788d2012-07-19 16:01:31 +1000199static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Shaohua Li4eb788d2012-07-19 16:01:31 +1000201 BUG_ON(!list_empty(&sh->lru));
202 BUG_ON(atomic_read(&conf->active_stripes)==0);
203 if (test_bit(STRIPE_HANDLE, &sh->state)) {
204 if (test_bit(STRIPE_DELAYED, &sh->state) &&
205 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
206 list_add_tail(&sh->lru, &conf->delayed_list);
207 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
208 sh->bm_seq - conf->seq_write > 0)
209 list_add_tail(&sh->lru, &conf->bitmap_list);
210 else {
211 clear_bit(STRIPE_DELAYED, &sh->state);
212 clear_bit(STRIPE_BIT_DELAY, &sh->state);
213 list_add_tail(&sh->lru, &conf->handle_list);
214 }
215 md_wakeup_thread(conf->mddev->thread);
216 } else {
217 BUG_ON(stripe_operations_active(sh));
218 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
219 if (atomic_dec_return(&conf->preread_active_stripes)
220 < IO_THRESHOLD)
221 md_wakeup_thread(conf->mddev->thread);
222 atomic_dec(&conf->active_stripes);
223 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
224 list_add_tail(&sh->lru, &conf->inactive_list);
225 wake_up(&conf->wait_for_stripe);
226 if (conf->retry_read_aligned)
227 md_wakeup_thread(conf->mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 }
229 }
230}
NeilBrownd0dabf72009-03-31 14:39:38 +1100231
Shaohua Li4eb788d2012-07-19 16:01:31 +1000232static void __release_stripe(struct r5conf *conf, struct stripe_head *sh)
233{
234 if (atomic_dec_and_test(&sh->count))
235 do_release_stripe(conf, sh);
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static void release_stripe(struct stripe_head *sh)
239{
NeilBrownd1688a62011-10-11 16:49:52 +1100240 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned long flags;
NeilBrown16a53ec2006-06-26 00:27:38 -0700242
Shaohua Li4eb788d2012-07-19 16:01:31 +1000243 local_irq_save(flags);
244 if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) {
245 do_release_stripe(conf, sh);
246 spin_unlock(&conf->device_lock);
247 }
248 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
NeilBrownfccddba2006-01-06 00:20:33 -0800251static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Dan Williams45b42332007-07-09 11:56:43 -0700253 pr_debug("remove_hash(), stripe %llu\n",
254 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
NeilBrownfccddba2006-01-06 00:20:33 -0800256 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
NeilBrownd1688a62011-10-11 16:49:52 +1100259static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
NeilBrownfccddba2006-01-06 00:20:33 -0800261 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Dan Williams45b42332007-07-09 11:56:43 -0700263 pr_debug("insert_hash(), stripe %llu\n",
264 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
NeilBrownfccddba2006-01-06 00:20:33 -0800266 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269
270/* find an idle stripe, make sure it is unhashed, and return it. */
NeilBrownd1688a62011-10-11 16:49:52 +1100271static struct stripe_head *get_free_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct stripe_head *sh = NULL;
274 struct list_head *first;
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (list_empty(&conf->inactive_list))
277 goto out;
278 first = conf->inactive_list.next;
279 sh = list_entry(first, struct stripe_head, lru);
280 list_del_init(first);
281 remove_hash(sh);
282 atomic_inc(&conf->active_stripes);
283out:
284 return sh;
285}
286
NeilBrowne4e11e32010-06-16 16:45:16 +1000287static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct page *p;
290 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000291 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
NeilBrowne4e11e32010-06-16 16:45:16 +1000293 for (i = 0; i < num ; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 p = sh->dev[i].page;
295 if (!p)
296 continue;
297 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800298 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300}
301
NeilBrowne4e11e32010-06-16 16:45:16 +1000302static int grow_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000305 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
NeilBrowne4e11e32010-06-16 16:45:16 +1000307 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct page *page;
309
310 if (!(page = alloc_page(GFP_KERNEL))) {
311 return 1;
312 }
313 sh->dev[i].page = page;
314 }
315 return 0;
316}
317
NeilBrown784052e2009-03-31 15:19:07 +1100318static void raid5_build_block(struct stripe_head *sh, int i, int previous);
NeilBrownd1688a62011-10-11 16:49:52 +1100319static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100320 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
NeilBrownb5663ba2009-03-31 14:39:38 +1100322static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
NeilBrownd1688a62011-10-11 16:49:52 +1100324 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800325 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200327 BUG_ON(atomic_read(&sh->count) != 0);
328 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000329 BUG_ON(stripe_operations_active(sh));
Dan Williamsd84e0f12007-01-02 13:52:30 -0700330
Dan Williams45b42332007-07-09 11:56:43 -0700331 pr_debug("init_stripe called, stripe %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 (unsigned long long)sh->sector);
333
334 remove_hash(sh);
NeilBrown16a53ec2006-06-26 00:27:38 -0700335
NeilBrown86b42c72009-03-31 15:19:03 +1100336 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100337 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100339 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 sh->state = 0;
341
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800342
343 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct r5dev *dev = &sh->dev[i];
345
Dan Williamsd84e0f12007-01-02 13:52:30 -0700346 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 test_bit(R5_LOCKED, &dev->flags)) {
Dan Williamsd84e0f12007-01-02 13:52:30 -0700348 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700350 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000352 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +1100355 raid5_build_block(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357 insert_hash(conf, sh);
358}
359
NeilBrownd1688a62011-10-11 16:49:52 +1100360static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100361 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 struct stripe_head *sh;
NeilBrownfccddba2006-01-06 00:20:33 -0800364 struct hlist_node *hn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Dan Williams45b42332007-07-09 11:56:43 -0700366 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
NeilBrownfccddba2006-01-06 00:20:33 -0800367 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100368 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700370 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return NULL;
372}
373
NeilBrown674806d2010-06-16 17:17:53 +1000374/*
375 * Need to check if array has failed when deciding whether to:
376 * - start an array
377 * - remove non-faulty devices
378 * - add a spare
379 * - allow a reshape
380 * This determination is simple when no reshape is happening.
381 * However if there is a reshape, we need to carefully check
382 * both the before and after sections.
383 * This is because some failed devices may only affect one
384 * of the two sections, and some non-in_sync devices may
385 * be insync in the section most affected by failed devices.
386 */
NeilBrown908f4fb2011-12-23 10:17:50 +1100387static int calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000388{
NeilBrown908f4fb2011-12-23 10:17:50 +1100389 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000390 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000391
392 rcu_read_lock();
393 degraded = 0;
394 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100395 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000396 if (rdev && test_bit(Faulty, &rdev->flags))
397 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000398 if (!rdev || test_bit(Faulty, &rdev->flags))
399 degraded++;
400 else if (test_bit(In_sync, &rdev->flags))
401 ;
402 else
403 /* not in-sync or faulty.
404 * If the reshape increases the number of devices,
405 * this is being recovered by the reshape, so
406 * this 'previous' section is not in_sync.
407 * If the number of devices is being reduced however,
408 * the device can only be part of the array if
409 * we are reverting a reshape, so this section will
410 * be in-sync.
411 */
412 if (conf->raid_disks >= conf->previous_raid_disks)
413 degraded++;
414 }
415 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100416 if (conf->raid_disks == conf->previous_raid_disks)
417 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000418 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100419 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000420 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100421 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000422 if (rdev && test_bit(Faulty, &rdev->flags))
423 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000424 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100425 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000426 else if (test_bit(In_sync, &rdev->flags))
427 ;
428 else
429 /* not in-sync or faulty.
430 * If reshape increases the number of devices, this
431 * section has already been recovered, else it
432 * almost certainly hasn't.
433 */
434 if (conf->raid_disks <= conf->previous_raid_disks)
NeilBrown908f4fb2011-12-23 10:17:50 +1100435 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000436 }
437 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100438 if (degraded2 > degraded)
439 return degraded2;
440 return degraded;
441}
442
443static int has_failed(struct r5conf *conf)
444{
445 int degraded;
446
447 if (conf->mddev->reshape_position == MaxSector)
448 return conf->mddev->degraded > conf->max_degraded;
449
450 degraded = calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000451 if (degraded > conf->max_degraded)
452 return 1;
453 return 0;
454}
455
NeilBrownb5663ba2009-03-31 14:39:38 +1100456static struct stripe_head *
NeilBrownd1688a62011-10-11 16:49:52 +1100457get_active_stripe(struct r5conf *conf, sector_t sector,
NeilBrowna8c906c2009-06-09 14:39:59 +1000458 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 struct stripe_head *sh;
461
Dan Williams45b42332007-07-09 11:56:43 -0700462 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 spin_lock_irq(&conf->device_lock);
465
466 do {
NeilBrown72626682005-09-09 16:23:54 -0700467 wait_event_lock_irq(conf->wait_for_stripe,
NeilBrowna8c906c2009-06-09 14:39:59 +1000468 conf->quiesce == 0 || noquiesce,
Lukas Czernereed8c022012-11-30 11:42:40 +0100469 conf->device_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100470 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (!sh) {
472 if (!conf->inactive_blocked)
473 sh = get_free_stripe(conf);
474 if (noblock && sh == NULL)
475 break;
476 if (!sh) {
477 conf->inactive_blocked = 1;
478 wait_event_lock_irq(conf->wait_for_stripe,
479 !list_empty(&conf->inactive_list) &&
NeilBrown50368052005-12-12 02:39:17 -0800480 (atomic_read(&conf->active_stripes)
481 < (conf->max_nr_stripes *3/4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 || !conf->inactive_blocked),
Lukas Czernereed8c022012-11-30 11:42:40 +0100483 conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 conf->inactive_blocked = 0;
485 } else
NeilBrownb5663ba2009-03-31 14:39:38 +1100486 init_stripe(sh, sector, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 } else {
488 if (atomic_read(&sh->count)) {
NeilBrownab69ae12009-03-31 15:26:47 +1100489 BUG_ON(!list_empty(&sh->lru)
Shaohua Li8811b592012-08-02 08:33:00 +1000490 && !test_bit(STRIPE_EXPANDING, &sh->state)
491 && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 } else {
493 if (!test_bit(STRIPE_HANDLE, &sh->state))
494 atomic_inc(&conf->active_stripes);
NeilBrownff4e8d92006-07-10 04:44:16 -0700495 if (list_empty(&sh->lru) &&
496 !test_bit(STRIPE_EXPANDING, &sh->state))
NeilBrown16a53ec2006-06-26 00:27:38 -0700497 BUG();
498 list_del_init(&sh->lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500 }
501 } while (sh == NULL);
502
503 if (sh)
504 atomic_inc(&sh->count);
505
506 spin_unlock_irq(&conf->device_lock);
507 return sh;
508}
509
NeilBrown05616be2012-05-21 09:27:00 +1000510/* Determine if 'data_offset' or 'new_data_offset' should be used
511 * in this stripe_head.
512 */
513static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
514{
515 sector_t progress = conf->reshape_progress;
516 /* Need a memory barrier to make sure we see the value
517 * of conf->generation, or ->data_offset that was set before
518 * reshape_progress was updated.
519 */
520 smp_rmb();
521 if (progress == MaxSector)
522 return 0;
523 if (sh->generation == conf->generation - 1)
524 return 0;
525 /* We are in a reshape, and this is a new-generation stripe,
526 * so use new_data_offset.
527 */
528 return 1;
529}
530
NeilBrown6712ecf2007-09-27 12:47:43 +0200531static void
532raid5_end_read_request(struct bio *bi, int error);
533static void
534raid5_end_write_request(struct bio *bi, int error);
Dan Williams91c00922007-01-02 13:52:30 -0700535
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000536static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700537{
NeilBrownd1688a62011-10-11 16:49:52 +1100538 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700539 int i, disks = sh->disks;
540
541 might_sleep();
542
543 for (i = disks; i--; ) {
544 int rw;
NeilBrown9a3e1102011-12-23 10:17:53 +1100545 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100546 struct bio *bi, *rbi;
547 struct md_rdev *rdev, *rrdev = NULL;
Tejun Heoe9c74692010-09-03 11:56:18 +0200548 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
549 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
550 rw = WRITE_FUA;
551 else
552 rw = WRITE;
Shaohua Li9e444762012-10-11 13:49:49 +1100553 if (test_bit(R5_Discard, &sh->dev[i].flags))
Shaohua Li620125f2012-10-11 13:49:05 +1100554 rw |= REQ_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +0200555 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Dan Williams91c00922007-01-02 13:52:30 -0700556 rw = READ;
NeilBrown9a3e1102011-12-23 10:17:53 +1100557 else if (test_and_clear_bit(R5_WantReplace,
558 &sh->dev[i].flags)) {
559 rw = WRITE;
560 replace_only = 1;
561 } else
Dan Williams91c00922007-01-02 13:52:30 -0700562 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +1000563 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
564 rw |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -0700565
566 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +1100567 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -0700568
569 bi->bi_rw = rw;
NeilBrown977df362011-12-23 10:17:53 +1100570 rbi->bi_rw = rw;
571 if (rw & WRITE) {
Dan Williams91c00922007-01-02 13:52:30 -0700572 bi->bi_end_io = raid5_end_write_request;
NeilBrown977df362011-12-23 10:17:53 +1100573 rbi->bi_end_io = raid5_end_write_request;
574 } else
Dan Williams91c00922007-01-02 13:52:30 -0700575 bi->bi_end_io = raid5_end_read_request;
576
577 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +1100578 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +1100579 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
580 rdev = rcu_dereference(conf->disks[i].rdev);
581 if (!rdev) {
582 rdev = rrdev;
583 rrdev = NULL;
584 }
NeilBrown9a3e1102011-12-23 10:17:53 +1100585 if (rw & WRITE) {
586 if (replace_only)
587 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +1100588 if (rdev == rrdev)
589 /* We raced and saw duplicates */
590 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +1100591 } else {
NeilBrowndd054fc2011-12-23 10:17:53 +1100592 if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +1100593 rdev = rrdev;
594 rrdev = NULL;
595 }
NeilBrown977df362011-12-23 10:17:53 +1100596
Dan Williams91c00922007-01-02 13:52:30 -0700597 if (rdev && test_bit(Faulty, &rdev->flags))
598 rdev = NULL;
599 if (rdev)
600 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +1100601 if (rrdev && test_bit(Faulty, &rrdev->flags))
602 rrdev = NULL;
603 if (rrdev)
604 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -0700605 rcu_read_unlock();
606
NeilBrown73e92e52011-07-28 11:39:22 +1000607 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +1100608 * need to check for writes. We never accept write errors
609 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +1000610 */
611 while ((rw & WRITE) && rdev &&
612 test_bit(WriteErrorSeen, &rdev->flags)) {
613 sector_t first_bad;
614 int bad_sectors;
615 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
616 &first_bad, &bad_sectors);
617 if (!bad)
618 break;
619
620 if (bad < 0) {
621 set_bit(BlockedBadBlocks, &rdev->flags);
622 if (!conf->mddev->external &&
623 conf->mddev->flags) {
624 /* It is very unlikely, but we might
625 * still need to write out the
626 * bad block log - better give it
627 * a chance*/
628 md_check_recovery(conf->mddev);
629 }
majianpeng18507532012-07-03 12:11:54 +1000630 /*
631 * Because md_wait_for_blocked_rdev
632 * will dec nr_pending, we must
633 * increment it first.
634 */
635 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +1000636 md_wait_for_blocked_rdev(rdev, conf->mddev);
637 } else {
638 /* Acknowledged bad block - skip the write */
639 rdev_dec_pending(rdev, conf->mddev);
640 rdev = NULL;
641 }
642 }
643
Dan Williams91c00922007-01-02 13:52:30 -0700644 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100645 if (s->syncing || s->expanding || s->expanded
646 || s->replacing)
Dan Williams91c00922007-01-02 13:52:30 -0700647 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
648
Dan Williams2b7497f2008-06-28 08:31:52 +1000649 set_bit(STRIPE_IO_STARTED, &sh->state);
650
Dan Williams91c00922007-01-02 13:52:30 -0700651 bi->bi_bdev = rdev->bdev;
652 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700653 __func__, (unsigned long long)sh->sector,
Dan Williams91c00922007-01-02 13:52:30 -0700654 bi->bi_rw, i);
655 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000656 if (use_new_offset(conf, sh))
657 bi->bi_sector = (sh->sector
658 + rdev->new_data_offset);
659 else
660 bi->bi_sector = (sh->sector
661 + rdev->data_offset);
majianpeng3f9e7c12012-07-31 10:04:21 +1000662 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
663 bi->bi_rw |= REQ_FLUSH;
664
Dan Williams91c00922007-01-02 13:52:30 -0700665 bi->bi_flags = 1 << BIO_UPTODATE;
Dan Williams91c00922007-01-02 13:52:30 -0700666 bi->bi_idx = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700667 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
668 bi->bi_io_vec[0].bv_offset = 0;
669 bi->bi_size = STRIPE_SIZE;
670 bi->bi_next = NULL;
NeilBrown977df362011-12-23 10:17:53 +1100671 if (rrdev)
672 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Dan Williams91c00922007-01-02 13:52:30 -0700673 generic_make_request(bi);
NeilBrown977df362011-12-23 10:17:53 +1100674 }
675 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +1100676 if (s->syncing || s->expanding || s->expanded
677 || s->replacing)
NeilBrown977df362011-12-23 10:17:53 +1100678 md_sync_acct(rrdev->bdev, STRIPE_SECTORS);
679
680 set_bit(STRIPE_IO_STARTED, &sh->state);
681
682 rbi->bi_bdev = rrdev->bdev;
683 pr_debug("%s: for %llu schedule op %ld on "
684 "replacement disc %d\n",
685 __func__, (unsigned long long)sh->sector,
686 rbi->bi_rw, i);
687 atomic_inc(&sh->count);
NeilBrown05616be2012-05-21 09:27:00 +1000688 if (use_new_offset(conf, sh))
689 rbi->bi_sector = (sh->sector
690 + rrdev->new_data_offset);
691 else
692 rbi->bi_sector = (sh->sector
693 + rrdev->data_offset);
NeilBrown977df362011-12-23 10:17:53 +1100694 rbi->bi_flags = 1 << BIO_UPTODATE;
695 rbi->bi_idx = 0;
696 rbi->bi_io_vec[0].bv_len = STRIPE_SIZE;
697 rbi->bi_io_vec[0].bv_offset = 0;
698 rbi->bi_size = STRIPE_SIZE;
699 rbi->bi_next = NULL;
700 generic_make_request(rbi);
701 }
702 if (!rdev && !rrdev) {
Namhyung Kimb0629622011-06-14 14:20:19 +1000703 if (rw & WRITE)
Dan Williams91c00922007-01-02 13:52:30 -0700704 set_bit(STRIPE_DEGRADED, &sh->state);
705 pr_debug("skip op %ld on disc %d for sector %llu\n",
706 bi->bi_rw, i, (unsigned long long)sh->sector);
707 clear_bit(R5_LOCKED, &sh->dev[i].flags);
708 set_bit(STRIPE_HANDLE, &sh->state);
709 }
710 }
711}
712
713static struct dma_async_tx_descriptor *
714async_copy_data(int frombio, struct bio *bio, struct page *page,
715 sector_t sector, struct dma_async_tx_descriptor *tx)
716{
717 struct bio_vec *bvl;
718 struct page *bio_page;
719 int i;
720 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -0700721 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -0700722 enum async_tx_flags flags = 0;
Dan Williams91c00922007-01-02 13:52:30 -0700723
724 if (bio->bi_sector >= sector)
725 page_offset = (signed)(bio->bi_sector - sector) * 512;
726 else
727 page_offset = (signed)(sector - bio->bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -0700728
Dan Williams0403e382009-09-08 17:42:50 -0700729 if (frombio)
730 flags |= ASYNC_TX_FENCE;
731 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
732
Dan Williams91c00922007-01-02 13:52:30 -0700733 bio_for_each_segment(bvl, bio, i) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000734 int len = bvl->bv_len;
Dan Williams91c00922007-01-02 13:52:30 -0700735 int clen;
736 int b_offset = 0;
737
738 if (page_offset < 0) {
739 b_offset = -page_offset;
740 page_offset += b_offset;
741 len -= b_offset;
742 }
743
744 if (len > 0 && page_offset + len > STRIPE_SIZE)
745 clen = STRIPE_SIZE - page_offset;
746 else
747 clen = len;
748
749 if (clen > 0) {
Namhyung Kimfcde9072011-06-14 14:23:57 +1000750 b_offset += bvl->bv_offset;
751 bio_page = bvl->bv_page;
Dan Williams91c00922007-01-02 13:52:30 -0700752 if (frombio)
753 tx = async_memcpy(page, bio_page, page_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700754 b_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700755 else
756 tx = async_memcpy(bio_page, page, b_offset,
Dan Williamsa08abd82009-06-03 11:43:59 -0700757 page_offset, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700758 }
Dan Williamsa08abd82009-06-03 11:43:59 -0700759 /* chain the operations */
760 submit.depend_tx = tx;
761
Dan Williams91c00922007-01-02 13:52:30 -0700762 if (clen < len) /* hit end of page */
763 break;
764 page_offset += len;
765 }
766
767 return tx;
768}
769
770static void ops_complete_biofill(void *stripe_head_ref)
771{
772 struct stripe_head *sh = stripe_head_ref;
773 struct bio *return_bi = NULL;
Dan Williamse4d84902007-09-24 10:06:13 -0700774 int i;
Dan Williams91c00922007-01-02 13:52:30 -0700775
Harvey Harrisone46b2722008-04-28 02:15:50 -0700776 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700777 (unsigned long long)sh->sector);
778
779 /* clear completed biofills */
780 for (i = sh->disks; i--; ) {
781 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -0700782
783 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -0700784 /* and check if we need to reply to a read request,
785 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +1000786 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -0700787 */
788 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -0700789 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -0700790
Dan Williams91c00922007-01-02 13:52:30 -0700791 BUG_ON(!dev->read);
792 rbi = dev->read;
793 dev->read = NULL;
794 while (rbi && rbi->bi_sector <
795 dev->sector + STRIPE_SECTORS) {
796 rbi2 = r5_next_bio(rbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +1000797 if (!raid5_dec_bi_active_stripes(rbi)) {
Dan Williams91c00922007-01-02 13:52:30 -0700798 rbi->bi_next = return_bi;
799 return_bi = rbi;
800 }
Dan Williams91c00922007-01-02 13:52:30 -0700801 rbi = rbi2;
802 }
803 }
804 }
Dan Williams83de75c2008-06-28 08:31:58 +1000805 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700806
807 return_io(return_bi);
808
Dan Williamse4d84902007-09-24 10:06:13 -0700809 set_bit(STRIPE_HANDLE, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -0700810 release_stripe(sh);
811}
812
813static void ops_run_biofill(struct stripe_head *sh)
814{
815 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -0700816 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700817 int i;
818
Harvey Harrisone46b2722008-04-28 02:15:50 -0700819 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700820 (unsigned long long)sh->sector);
821
822 for (i = sh->disks; i--; ) {
823 struct r5dev *dev = &sh->dev[i];
824 if (test_bit(R5_Wantfill, &dev->flags)) {
825 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +1000826 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700827 dev->read = rbi = dev->toread;
828 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +1000829 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -0700830 while (rbi && rbi->bi_sector <
831 dev->sector + STRIPE_SECTORS) {
832 tx = async_copy_data(0, rbi, dev->page,
833 dev->sector, tx);
834 rbi = r5_next_bio(rbi, dev->sector);
835 }
836 }
837 }
838
839 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -0700840 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
841 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -0700842}
843
Dan Williams4e7d2c02009-08-29 19:13:11 -0700844static void mark_target_uptodate(struct stripe_head *sh, int target)
845{
846 struct r5dev *tgt;
847
848 if (target < 0)
849 return;
850
851 tgt = &sh->dev[target];
852 set_bit(R5_UPTODATE, &tgt->flags);
853 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
854 clear_bit(R5_Wantcompute, &tgt->flags);
855}
856
Dan Williamsac6b53b2009-07-14 13:40:19 -0700857static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -0700858{
859 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -0700860
Harvey Harrisone46b2722008-04-28 02:15:50 -0700861 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -0700862 (unsigned long long)sh->sector);
863
Dan Williamsac6b53b2009-07-14 13:40:19 -0700864 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -0700865 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700866 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -0700867
Dan Williamsecc65c92008-06-28 08:31:57 +1000868 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
869 if (sh->check_state == check_state_compute_run)
870 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -0700871 set_bit(STRIPE_HANDLE, &sh->state);
872 release_stripe(sh);
873}
874
Dan Williamsd6f38f32009-07-14 11:50:52 -0700875/* return a pointer to the address conversion region of the scribble buffer */
876static addr_conv_t *to_addr_conv(struct stripe_head *sh,
877 struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -0700878{
Dan Williamsd6f38f32009-07-14 11:50:52 -0700879 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
880}
881
882static struct dma_async_tx_descriptor *
883ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
884{
Dan Williams91c00922007-01-02 13:52:30 -0700885 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -0700886 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -0700887 int target = sh->ops.target;
888 struct r5dev *tgt = &sh->dev[target];
889 struct page *xor_dest = tgt->page;
890 int count = 0;
891 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -0700892 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -0700893 int i;
894
895 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -0700896 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -0700897 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
898
899 for (i = disks; i--; )
900 if (i != target)
901 xor_srcs[count++] = sh->dev[i].page;
902
903 atomic_inc(&sh->count);
904
Dan Williams0403e382009-09-08 17:42:50 -0700905 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700906 ops_complete_compute, sh, to_addr_conv(sh, percpu));
Dan Williams91c00922007-01-02 13:52:30 -0700907 if (unlikely(count == 1))
Dan Williamsa08abd82009-06-03 11:43:59 -0700908 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700909 else
Dan Williamsa08abd82009-06-03 11:43:59 -0700910 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -0700911
Dan Williams91c00922007-01-02 13:52:30 -0700912 return tx;
913}
914
Dan Williamsac6b53b2009-07-14 13:40:19 -0700915/* set_syndrome_sources - populate source buffers for gen_syndrome
916 * @srcs - (struct page *) array of size sh->disks
917 * @sh - stripe_head to parse
918 *
919 * Populates srcs in proper layout order for the stripe and returns the
920 * 'count' of sources to be used in a call to async_gen_syndrome. The P
921 * destination buffer is recorded in srcs[count] and the Q destination
922 * is recorded in srcs[count+1]].
923 */
924static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
925{
926 int disks = sh->disks;
927 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
928 int d0_idx = raid6_d0(sh);
929 int count;
930 int i;
931
932 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +1100933 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700934
935 count = 0;
936 i = d0_idx;
937 do {
938 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
939
940 srcs[slot] = sh->dev[i].page;
941 i = raid6_next_disk(i, disks);
942 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -0700943
NeilBrowne4424fe2009-10-16 16:27:34 +1100944 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -0700945}
946
947static struct dma_async_tx_descriptor *
948ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
949{
950 int disks = sh->disks;
951 struct page **blocks = percpu->scribble;
952 int target;
953 int qd_idx = sh->qd_idx;
954 struct dma_async_tx_descriptor *tx;
955 struct async_submit_ctl submit;
956 struct r5dev *tgt;
957 struct page *dest;
958 int i;
959 int count;
960
961 if (sh->ops.target < 0)
962 target = sh->ops.target2;
963 else if (sh->ops.target2 < 0)
964 target = sh->ops.target;
965 else
966 /* we should only have one valid target */
967 BUG();
968 BUG_ON(target < 0);
969 pr_debug("%s: stripe %llu block: %d\n",
970 __func__, (unsigned long long)sh->sector, target);
971
972 tgt = &sh->dev[target];
973 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
974 dest = tgt->page;
975
976 atomic_inc(&sh->count);
977
978 if (target == qd_idx) {
979 count = set_syndrome_sources(blocks, sh);
980 blocks[count] = NULL; /* regenerating p is not necessary */
981 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -0700982 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
983 ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700984 to_addr_conv(sh, percpu));
985 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
986 } else {
987 /* Compute any data- or p-drive using XOR */
988 count = 0;
989 for (i = disks; i-- ; ) {
990 if (i == target || i == qd_idx)
991 continue;
992 blocks[count++] = sh->dev[i].page;
993 }
994
Dan Williams0403e382009-09-08 17:42:50 -0700995 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
996 NULL, ops_complete_compute, sh,
Dan Williamsac6b53b2009-07-14 13:40:19 -0700997 to_addr_conv(sh, percpu));
998 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
999 }
1000
1001 return tx;
1002}
1003
1004static struct dma_async_tx_descriptor *
1005ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1006{
1007 int i, count, disks = sh->disks;
1008 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1009 int d0_idx = raid6_d0(sh);
1010 int faila = -1, failb = -1;
1011 int target = sh->ops.target;
1012 int target2 = sh->ops.target2;
1013 struct r5dev *tgt = &sh->dev[target];
1014 struct r5dev *tgt2 = &sh->dev[target2];
1015 struct dma_async_tx_descriptor *tx;
1016 struct page **blocks = percpu->scribble;
1017 struct async_submit_ctl submit;
1018
1019 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1020 __func__, (unsigned long long)sh->sector, target, target2);
1021 BUG_ON(target < 0 || target2 < 0);
1022 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1023 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1024
Dan Williams6c910a72009-09-16 12:24:54 -07001025 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001026 * slot number conversion for 'faila' and 'failb'
1027 */
1028 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001029 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001030 count = 0;
1031 i = d0_idx;
1032 do {
1033 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1034
1035 blocks[slot] = sh->dev[i].page;
1036
1037 if (i == target)
1038 faila = slot;
1039 if (i == target2)
1040 failb = slot;
1041 i = raid6_next_disk(i, disks);
1042 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001043
1044 BUG_ON(faila == failb);
1045 if (failb < faila)
1046 swap(faila, failb);
1047 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1048 __func__, (unsigned long long)sh->sector, faila, failb);
1049
1050 atomic_inc(&sh->count);
1051
1052 if (failb == syndrome_disks+1) {
1053 /* Q disk is one of the missing disks */
1054 if (faila == syndrome_disks) {
1055 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001056 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1057 ops_complete_compute, sh,
1058 to_addr_conv(sh, percpu));
NeilBrowne4424fe2009-10-16 16:27:34 +11001059 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Dan Williamsac6b53b2009-07-14 13:40:19 -07001060 STRIPE_SIZE, &submit);
1061 } else {
1062 struct page *dest;
1063 int data_target;
1064 int qd_idx = sh->qd_idx;
1065
1066 /* Missing D+Q: recompute D from P, then recompute Q */
1067 if (target == qd_idx)
1068 data_target = target2;
1069 else
1070 data_target = target;
1071
1072 count = 0;
1073 for (i = disks; i-- ; ) {
1074 if (i == data_target || i == qd_idx)
1075 continue;
1076 blocks[count++] = sh->dev[i].page;
1077 }
1078 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001079 init_async_submit(&submit,
1080 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1081 NULL, NULL, NULL,
1082 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001083 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
1084 &submit);
1085
1086 count = set_syndrome_sources(blocks, sh);
Dan Williams0403e382009-09-08 17:42:50 -07001087 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1088 ops_complete_compute, sh,
1089 to_addr_conv(sh, percpu));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001090 return async_gen_syndrome(blocks, 0, count+2,
1091 STRIPE_SIZE, &submit);
1092 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001093 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001094 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1095 ops_complete_compute, sh,
1096 to_addr_conv(sh, percpu));
1097 if (failb == syndrome_disks) {
1098 /* We're missing D+P. */
1099 return async_raid6_datap_recov(syndrome_disks+2,
1100 STRIPE_SIZE, faila,
1101 blocks, &submit);
1102 } else {
1103 /* We're missing D+D. */
1104 return async_raid6_2data_recov(syndrome_disks+2,
1105 STRIPE_SIZE, faila, failb,
1106 blocks, &submit);
1107 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001108 }
1109}
1110
1111
Dan Williams91c00922007-01-02 13:52:30 -07001112static void ops_complete_prexor(void *stripe_head_ref)
1113{
1114 struct stripe_head *sh = stripe_head_ref;
1115
Harvey Harrisone46b2722008-04-28 02:15:50 -07001116 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001117 (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001118}
1119
1120static struct dma_async_tx_descriptor *
Dan Williamsd6f38f32009-07-14 11:50:52 -07001121ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1122 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001123{
Dan Williams91c00922007-01-02 13:52:30 -07001124 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001125 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001126 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001127 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001128
1129 /* existing parity data subtracted */
1130 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1131
Harvey Harrisone46b2722008-04-28 02:15:50 -07001132 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001133 (unsigned long long)sh->sector);
1134
1135 for (i = disks; i--; ) {
1136 struct r5dev *dev = &sh->dev[i];
1137 /* Only process blocks that are known to be uptodate */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001138 if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001139 xor_srcs[count++] = dev->page;
1140 }
1141
Dan Williams0403e382009-09-08 17:42:50 -07001142 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001143 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001144 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001145
1146 return tx;
1147}
1148
1149static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001150ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001151{
1152 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001153 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001154
Harvey Harrisone46b2722008-04-28 02:15:50 -07001155 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001156 (unsigned long long)sh->sector);
1157
1158 for (i = disks; i--; ) {
1159 struct r5dev *dev = &sh->dev[i];
1160 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001161
Dan Williamsd8ee0722008-06-28 08:32:06 +10001162 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001163 struct bio *wbi;
1164
Shaohua Lib17459c2012-07-19 16:01:31 +10001165 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001166 chosen = dev->towrite;
1167 dev->towrite = NULL;
1168 BUG_ON(dev->written);
1169 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001170 spin_unlock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001171
1172 while (wbi && wbi->bi_sector <
1173 dev->sector + STRIPE_SECTORS) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001174 if (wbi->bi_rw & REQ_FUA)
1175 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001176 if (wbi->bi_rw & REQ_SYNC)
1177 set_bit(R5_SyncIO, &dev->flags);
Shaohua Li9e444762012-10-11 13:49:49 +11001178 if (wbi->bi_rw & REQ_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001179 set_bit(R5_Discard, &dev->flags);
Shaohua Li9e444762012-10-11 13:49:49 +11001180 else
Shaohua Li620125f2012-10-11 13:49:05 +11001181 tx = async_copy_data(1, wbi, dev->page,
1182 dev->sector, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001183 wbi = r5_next_bio(wbi, dev->sector);
1184 }
1185 }
1186 }
1187
1188 return tx;
1189}
1190
Dan Williamsac6b53b2009-07-14 13:40:19 -07001191static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001192{
1193 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001194 int disks = sh->disks;
1195 int pd_idx = sh->pd_idx;
1196 int qd_idx = sh->qd_idx;
1197 int i;
Shaohua Li9e444762012-10-11 13:49:49 +11001198 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001199
Harvey Harrisone46b2722008-04-28 02:15:50 -07001200 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001201 (unsigned long long)sh->sector);
1202
Shaohua Libc0934f2012-05-22 13:55:05 +10001203 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001204 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001205 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e444762012-10-11 13:49:49 +11001206 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001207 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001208
Dan Williams91c00922007-01-02 13:52:30 -07001209 for (i = disks; i--; ) {
1210 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001211
Tejun Heoe9c74692010-09-03 11:56:18 +02001212 if (dev->written || i == pd_idx || i == qd_idx) {
Shaohua Li9e444762012-10-11 13:49:49 +11001213 if (!discard)
1214 set_bit(R5_UPTODATE, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001215 if (fua)
1216 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001217 if (sync)
1218 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001219 }
Dan Williams91c00922007-01-02 13:52:30 -07001220 }
1221
Dan Williamsd8ee0722008-06-28 08:32:06 +10001222 if (sh->reconstruct_state == reconstruct_state_drain_run)
1223 sh->reconstruct_state = reconstruct_state_drain_result;
1224 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1225 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1226 else {
1227 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1228 sh->reconstruct_state = reconstruct_state_result;
1229 }
Dan Williams91c00922007-01-02 13:52:30 -07001230
1231 set_bit(STRIPE_HANDLE, &sh->state);
1232 release_stripe(sh);
1233}
1234
1235static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001236ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1237 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001238{
Dan Williams91c00922007-01-02 13:52:30 -07001239 int disks = sh->disks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001240 struct page **xor_srcs = percpu->scribble;
Dan Williamsa08abd82009-06-03 11:43:59 -07001241 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001242 int count = 0, pd_idx = sh->pd_idx, i;
1243 struct page *xor_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001244 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001245 unsigned long flags;
Dan Williams91c00922007-01-02 13:52:30 -07001246
Harvey Harrisone46b2722008-04-28 02:15:50 -07001247 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001248 (unsigned long long)sh->sector);
1249
Shaohua Li620125f2012-10-11 13:49:05 +11001250 for (i = 0; i < sh->disks; i++) {
1251 if (pd_idx == i)
1252 continue;
1253 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1254 break;
1255 }
1256 if (i >= sh->disks) {
1257 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001258 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1259 ops_complete_reconstruct(sh);
1260 return;
1261 }
Dan Williams91c00922007-01-02 13:52:30 -07001262 /* check if prexor is active which means only process blocks
1263 * that are part of a read-modify-write (written)
1264 */
Dan Williamsd8ee0722008-06-28 08:32:06 +10001265 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1266 prexor = 1;
Dan Williams91c00922007-01-02 13:52:30 -07001267 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1268 for (i = disks; i--; ) {
1269 struct r5dev *dev = &sh->dev[i];
1270 if (dev->written)
1271 xor_srcs[count++] = dev->page;
1272 }
1273 } else {
1274 xor_dest = sh->dev[pd_idx].page;
1275 for (i = disks; i--; ) {
1276 struct r5dev *dev = &sh->dev[i];
1277 if (i != pd_idx)
1278 xor_srcs[count++] = dev->page;
1279 }
1280 }
1281
Dan Williams91c00922007-01-02 13:52:30 -07001282 /* 1/ if we prexor'd then the dest is reused as a source
1283 * 2/ if we did not prexor then we are redoing the parity
1284 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1285 * for the synchronous xor case
1286 */
Dan Williams88ba2aa2009-04-09 16:16:18 -07001287 flags = ASYNC_TX_ACK |
Dan Williams91c00922007-01-02 13:52:30 -07001288 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1289
1290 atomic_inc(&sh->count);
1291
Dan Williamsac6b53b2009-07-14 13:40:19 -07001292 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
Dan Williamsd6f38f32009-07-14 11:50:52 -07001293 to_addr_conv(sh, percpu));
Dan Williamsa08abd82009-06-03 11:43:59 -07001294 if (unlikely(count == 1))
1295 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1296 else
1297 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001298}
1299
Dan Williamsac6b53b2009-07-14 13:40:19 -07001300static void
1301ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1302 struct dma_async_tx_descriptor *tx)
1303{
1304 struct async_submit_ctl submit;
1305 struct page **blocks = percpu->scribble;
Shaohua Li620125f2012-10-11 13:49:05 +11001306 int count, i;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001307
1308 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1309
Shaohua Li620125f2012-10-11 13:49:05 +11001310 for (i = 0; i < sh->disks; i++) {
1311 if (sh->pd_idx == i || sh->qd_idx == i)
1312 continue;
1313 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1314 break;
1315 }
1316 if (i >= sh->disks) {
1317 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001318 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1319 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1320 ops_complete_reconstruct(sh);
1321 return;
1322 }
1323
Dan Williamsac6b53b2009-07-14 13:40:19 -07001324 count = set_syndrome_sources(blocks, sh);
1325
1326 atomic_inc(&sh->count);
1327
1328 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1329 sh, to_addr_conv(sh, percpu));
1330 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001331}
1332
1333static void ops_complete_check(void *stripe_head_ref)
1334{
1335 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001336
Harvey Harrisone46b2722008-04-28 02:15:50 -07001337 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001338 (unsigned long long)sh->sector);
1339
Dan Williamsecc65c92008-06-28 08:31:57 +10001340 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07001341 set_bit(STRIPE_HANDLE, &sh->state);
1342 release_stripe(sh);
1343}
1344
Dan Williamsac6b53b2009-07-14 13:40:19 -07001345static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07001346{
Dan Williams91c00922007-01-02 13:52:30 -07001347 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001348 int pd_idx = sh->pd_idx;
1349 int qd_idx = sh->qd_idx;
1350 struct page *xor_dest;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001351 struct page **xor_srcs = percpu->scribble;
Dan Williams91c00922007-01-02 13:52:30 -07001352 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001353 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001354 int count;
1355 int i;
Dan Williams91c00922007-01-02 13:52:30 -07001356
Harvey Harrisone46b2722008-04-28 02:15:50 -07001357 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001358 (unsigned long long)sh->sector);
1359
Dan Williamsac6b53b2009-07-14 13:40:19 -07001360 count = 0;
1361 xor_dest = sh->dev[pd_idx].page;
1362 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07001363 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001364 if (i == pd_idx || i == qd_idx)
1365 continue;
1366 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07001367 }
1368
Dan Williamsd6f38f32009-07-14 11:50:52 -07001369 init_async_submit(&submit, 0, NULL, NULL, NULL,
1370 to_addr_conv(sh, percpu));
Dan Williams099f53c2009-04-08 14:28:37 -07001371 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07001372 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001373
Dan Williams91c00922007-01-02 13:52:30 -07001374 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001375 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1376 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001377}
1378
Dan Williamsac6b53b2009-07-14 13:40:19 -07001379static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1380{
1381 struct page **srcs = percpu->scribble;
1382 struct async_submit_ctl submit;
1383 int count;
1384
1385 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1386 (unsigned long long)sh->sector, checkp);
1387
1388 count = set_syndrome_sources(srcs, sh);
1389 if (!checkp)
1390 srcs[count] = NULL;
1391
1392 atomic_inc(&sh->count);
1393 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1394 sh, to_addr_conv(sh, percpu));
1395 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1396 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1397}
1398
Dan Williams417b8d42009-10-16 16:25:22 +11001399static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07001400{
1401 int overlap_clear = 0, i, disks = sh->disks;
1402 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11001403 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001404 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001405 struct raid5_percpu *percpu;
1406 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07001407
Dan Williamsd6f38f32009-07-14 11:50:52 -07001408 cpu = get_cpu();
1409 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10001410 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07001411 ops_run_biofill(sh);
1412 overlap_clear++;
1413 }
1414
Dan Williams7b3a8712008-06-28 08:32:09 +10001415 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07001416 if (level < 6)
1417 tx = ops_run_compute5(sh, percpu);
1418 else {
1419 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1420 tx = ops_run_compute6_1(sh, percpu);
1421 else
1422 tx = ops_run_compute6_2(sh, percpu);
1423 }
1424 /* terminate the chain if reconstruct is not set to be run */
1425 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10001426 async_tx_ack(tx);
1427 }
Dan Williams91c00922007-01-02 13:52:30 -07001428
Dan Williams600aa102008-06-28 08:32:05 +10001429 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
Dan Williamsd6f38f32009-07-14 11:50:52 -07001430 tx = ops_run_prexor(sh, percpu, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001431
Dan Williams600aa102008-06-28 08:32:05 +10001432 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001433 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07001434 overlap_clear++;
1435 }
1436
Dan Williamsac6b53b2009-07-14 13:40:19 -07001437 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1438 if (level < 6)
1439 ops_run_reconstruct5(sh, percpu, tx);
1440 else
1441 ops_run_reconstruct6(sh, percpu, tx);
1442 }
Dan Williams91c00922007-01-02 13:52:30 -07001443
Dan Williamsac6b53b2009-07-14 13:40:19 -07001444 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1445 if (sh->check_state == check_state_run)
1446 ops_run_check_p(sh, percpu);
1447 else if (sh->check_state == check_state_run_q)
1448 ops_run_check_pq(sh, percpu, 0);
1449 else if (sh->check_state == check_state_run_pq)
1450 ops_run_check_pq(sh, percpu, 1);
1451 else
1452 BUG();
1453 }
Dan Williams91c00922007-01-02 13:52:30 -07001454
Dan Williams91c00922007-01-02 13:52:30 -07001455 if (overlap_clear)
1456 for (i = disks; i--; ) {
1457 struct r5dev *dev = &sh->dev[i];
1458 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1459 wake_up(&sh->raid_conf->wait_for_overlap);
1460 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07001461 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07001462}
1463
Dan Williams417b8d42009-10-16 16:25:22 +11001464#ifdef CONFIG_MULTICORE_RAID456
1465static void async_run_ops(void *param, async_cookie_t cookie)
1466{
1467 struct stripe_head *sh = param;
1468 unsigned long ops_request = sh->ops.request;
1469
1470 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1471 wake_up(&sh->ops.wait_for_ops);
1472
1473 __raid_run_ops(sh, ops_request);
1474 release_stripe(sh);
1475}
1476
1477static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1478{
1479 /* since handle_stripe can be called outside of raid5d context
1480 * we need to ensure sh->ops.request is de-staged before another
1481 * request arrives
1482 */
1483 wait_event(sh->ops.wait_for_ops,
1484 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1485 sh->ops.request = ops_request;
1486
1487 atomic_inc(&sh->count);
1488 async_schedule(async_run_ops, sh);
1489}
1490#else
1491#define raid_run_ops __raid_run_ops
1492#endif
1493
NeilBrownd1688a62011-10-11 16:49:52 +11001494static int grow_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 struct stripe_head *sh;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001497 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
NeilBrown3f294f42005-11-08 21:39:25 -08001498 if (!sh)
1499 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10001500
NeilBrown3f294f42005-11-08 21:39:25 -08001501 sh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001502 #ifdef CONFIG_MULTICORE_RAID456
1503 init_waitqueue_head(&sh->ops.wait_for_ops);
1504 #endif
NeilBrown3f294f42005-11-08 21:39:25 -08001505
Shaohua Lib17459c2012-07-19 16:01:31 +10001506 spin_lock_init(&sh->stripe_lock);
1507
NeilBrowne4e11e32010-06-16 16:45:16 +10001508 if (grow_buffers(sh)) {
1509 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001510 kmem_cache_free(conf->slab_cache, sh);
1511 return 0;
1512 }
1513 /* we just created an active stripe so... */
1514 atomic_set(&sh->count, 1);
1515 atomic_inc(&conf->active_stripes);
1516 INIT_LIST_HEAD(&sh->lru);
1517 release_stripe(sh);
1518 return 1;
1519}
1520
NeilBrownd1688a62011-10-11 16:49:52 +11001521static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08001522{
Christoph Lametere18b8902006-12-06 20:33:20 -08001523 struct kmem_cache *sc;
NeilBrown5e5e3e72009-10-16 16:35:30 +11001524 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
NeilBrownf4be6b42010-06-01 19:37:25 +10001526 if (conf->mddev->gendisk)
1527 sprintf(conf->cache_name[0],
1528 "raid%d-%s", conf->level, mdname(conf->mddev));
1529 else
1530 sprintf(conf->cache_name[0],
1531 "raid%d-%p", conf->level, conf->mddev);
1532 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1533
NeilBrownad01c9e2006-03-27 01:18:07 -08001534 conf->active_name = 0;
1535 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001537 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (!sc)
1539 return 1;
1540 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001541 conf->pool_size = devs;
NeilBrown16a53ec2006-06-26 00:27:38 -07001542 while (num--)
NeilBrown3f294f42005-11-08 21:39:25 -08001543 if (!grow_one_stripe(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return 0;
1546}
NeilBrown29269552006-03-27 01:18:10 -08001547
Dan Williamsd6f38f32009-07-14 11:50:52 -07001548/**
1549 * scribble_len - return the required size of the scribble region
1550 * @num - total number of disks in the array
1551 *
1552 * The size must be enough to contain:
1553 * 1/ a struct page pointer for each device in the array +2
1554 * 2/ room to convert each entry in (1) to its corresponding dma
1555 * (dma_map_page()) or page (page_address()) address.
1556 *
1557 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1558 * calculate over all devices (not just the data blocks), using zeros in place
1559 * of the P and Q blocks.
1560 */
1561static size_t scribble_len(int num)
1562{
1563 size_t len;
1564
1565 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1566
1567 return len;
1568}
1569
NeilBrownd1688a62011-10-11 16:49:52 +11001570static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08001571{
1572 /* Make all the stripes able to hold 'newsize' devices.
1573 * New slots in each stripe get 'page' set to a new page.
1574 *
1575 * This happens in stages:
1576 * 1/ create a new kmem_cache and allocate the required number of
1577 * stripe_heads.
1578 * 2/ gather all the old stripe_heads and tranfer the pages across
1579 * to the new stripe_heads. This will have the side effect of
1580 * freezing the array as once all stripe_heads have been collected,
1581 * no IO will be possible. Old stripe heads are freed once their
1582 * pages have been transferred over, and the old kmem_cache is
1583 * freed when all stripes are done.
1584 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1585 * we simple return a failre status - no need to clean anything up.
1586 * 4/ allocate new pages for the new slots in the new stripe_heads.
1587 * If this fails, we don't bother trying the shrink the
1588 * stripe_heads down again, we just leave them as they are.
1589 * As each stripe_head is processed the new one is released into
1590 * active service.
1591 *
1592 * Once step2 is started, we cannot afford to wait for a write,
1593 * so we use GFP_NOIO allocations.
1594 */
1595 struct stripe_head *osh, *nsh;
1596 LIST_HEAD(newstripes);
1597 struct disk_info *ndisks;
Dan Williamsd6f38f32009-07-14 11:50:52 -07001598 unsigned long cpu;
Dan Williamsb5470dc2008-06-27 21:44:04 -07001599 int err;
Christoph Lametere18b8902006-12-06 20:33:20 -08001600 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08001601 int i;
1602
1603 if (newsize <= conf->pool_size)
1604 return 0; /* never bother to shrink */
1605
Dan Williamsb5470dc2008-06-27 21:44:04 -07001606 err = md_allow_write(conf->mddev);
1607 if (err)
1608 return err;
NeilBrown2a2275d2007-01-26 00:57:11 -08001609
NeilBrownad01c9e2006-03-27 01:18:07 -08001610 /* Step 1 */
1611 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1612 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09001613 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001614 if (!sc)
1615 return -ENOMEM;
1616
1617 for (i = conf->max_nr_stripes; i; i--) {
Namhyung Kim6ce32842011-07-18 17:38:50 +10001618 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
NeilBrownad01c9e2006-03-27 01:18:07 -08001619 if (!nsh)
1620 break;
1621
NeilBrownad01c9e2006-03-27 01:18:07 -08001622 nsh->raid_conf = conf;
Dan Williams417b8d42009-10-16 16:25:22 +11001623 #ifdef CONFIG_MULTICORE_RAID456
1624 init_waitqueue_head(&nsh->ops.wait_for_ops);
1625 #endif
NeilBrowncb13ff62012-09-24 16:27:20 +10001626 spin_lock_init(&nsh->stripe_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001627
1628 list_add(&nsh->lru, &newstripes);
1629 }
1630 if (i) {
1631 /* didn't get enough, give up */
1632 while (!list_empty(&newstripes)) {
1633 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1634 list_del(&nsh->lru);
1635 kmem_cache_free(sc, nsh);
1636 }
1637 kmem_cache_destroy(sc);
1638 return -ENOMEM;
1639 }
1640 /* Step 2 - Must use GFP_NOIO now.
1641 * OK, we have enough stripes, start collecting inactive
1642 * stripes and copying them over
1643 */
1644 list_for_each_entry(nsh, &newstripes, lru) {
1645 spin_lock_irq(&conf->device_lock);
1646 wait_event_lock_irq(conf->wait_for_stripe,
1647 !list_empty(&conf->inactive_list),
Lukas Czernereed8c022012-11-30 11:42:40 +01001648 conf->device_lock);
NeilBrownad01c9e2006-03-27 01:18:07 -08001649 osh = get_free_stripe(conf);
1650 spin_unlock_irq(&conf->device_lock);
1651 atomic_set(&nsh->count, 1);
1652 for(i=0; i<conf->pool_size; i++)
1653 nsh->dev[i].page = osh->dev[i].page;
1654 for( ; i<newsize; i++)
1655 nsh->dev[i].page = NULL;
1656 kmem_cache_free(conf->slab_cache, osh);
1657 }
1658 kmem_cache_destroy(conf->slab_cache);
1659
1660 /* Step 3.
1661 * At this point, we are holding all the stripes so the array
1662 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07001663 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08001664 */
1665 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1666 if (ndisks) {
1667 for (i=0; i<conf->raid_disks; i++)
1668 ndisks[i] = conf->disks[i];
1669 kfree(conf->disks);
1670 conf->disks = ndisks;
1671 } else
1672 err = -ENOMEM;
1673
Dan Williamsd6f38f32009-07-14 11:50:52 -07001674 get_online_cpus();
1675 conf->scribble_len = scribble_len(newsize);
1676 for_each_present_cpu(cpu) {
1677 struct raid5_percpu *percpu;
1678 void *scribble;
1679
1680 percpu = per_cpu_ptr(conf->percpu, cpu);
1681 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1682
1683 if (scribble) {
1684 kfree(percpu->scribble);
1685 percpu->scribble = scribble;
1686 } else {
1687 err = -ENOMEM;
1688 break;
1689 }
1690 }
1691 put_online_cpus();
1692
NeilBrownad01c9e2006-03-27 01:18:07 -08001693 /* Step 4, return new stripes to service */
1694 while(!list_empty(&newstripes)) {
1695 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1696 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001697
NeilBrownad01c9e2006-03-27 01:18:07 -08001698 for (i=conf->raid_disks; i < newsize; i++)
1699 if (nsh->dev[i].page == NULL) {
1700 struct page *p = alloc_page(GFP_NOIO);
1701 nsh->dev[i].page = p;
1702 if (!p)
1703 err = -ENOMEM;
1704 }
1705 release_stripe(nsh);
1706 }
1707 /* critical section pass, GFP_NOIO no longer needed */
1708
1709 conf->slab_cache = sc;
1710 conf->active_name = 1-conf->active_name;
1711 conf->pool_size = newsize;
1712 return err;
1713}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
NeilBrownd1688a62011-10-11 16:49:52 +11001715static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716{
1717 struct stripe_head *sh;
1718
NeilBrown3f294f42005-11-08 21:39:25 -08001719 spin_lock_irq(&conf->device_lock);
1720 sh = get_free_stripe(conf);
1721 spin_unlock_irq(&conf->device_lock);
1722 if (!sh)
1723 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02001724 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10001725 shrink_buffers(sh);
NeilBrown3f294f42005-11-08 21:39:25 -08001726 kmem_cache_free(conf->slab_cache, sh);
1727 atomic_dec(&conf->active_stripes);
1728 return 1;
1729}
1730
NeilBrownd1688a62011-10-11 16:49:52 +11001731static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08001732{
1733 while (drop_one_stripe(conf))
1734 ;
1735
NeilBrown29fc7e32006-02-03 03:03:41 -08001736 if (conf->slab_cache)
1737 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 conf->slab_cache = NULL;
1739}
1740
NeilBrown6712ecf2007-09-27 12:47:43 +02001741static void raid5_end_read_request(struct bio * bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
NeilBrown99c0fb52009-03-31 14:39:38 +11001743 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001744 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001745 int disks = sh->disks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownd6950432006-07-10 04:44:20 -07001747 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11001748 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10001749 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 for (i=0 ; i<disks; i++)
1752 if (bi == &sh->dev[i].req)
1753 break;
1754
Dan Williams45b42332007-07-09 11:56:43 -07001755 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1756 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 uptodate);
1758 if (i == disks) {
1759 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001760 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
NeilBrown14a75d32011-12-23 10:17:52 +11001762 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11001763 /* If replacement finished while this request was outstanding,
1764 * 'replacement' might be NULL already.
1765 * In that case it moved down to 'rdev'.
1766 * rdev is not removed until all requests are finished.
1767 */
NeilBrown14a75d32011-12-23 10:17:52 +11001768 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001769 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11001770 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
NeilBrown05616be2012-05-21 09:27:00 +10001772 if (use_new_offset(conf, sh))
1773 s = sh->sector + rdev->new_data_offset;
1774 else
1775 s = sh->sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (uptodate) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08001778 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11001779 /* Note that this cannot happen on a
1780 * replacement device. We just fail those on
1781 * any error
1782 */
Christian Dietrich8bda4702011-07-27 11:00:36 +10001783 printk_ratelimited(
1784 KERN_INFO
1785 "md/raid:%s: read error corrected"
1786 " (%lu sectors at %llu on %s)\n",
1787 mdname(conf->mddev), STRIPE_SECTORS,
NeilBrown05616be2012-05-21 09:27:00 +10001788 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001789 bdevname(rdev->bdev, b));
Namhyung Kimddd51152011-07-27 11:00:36 +10001790 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08001791 clear_bit(R5_ReadError, &sh->dev[i].flags);
1792 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10001793 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
1794 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1795
NeilBrown14a75d32011-12-23 10:17:52 +11001796 if (atomic_read(&rdev->read_errors))
1797 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11001799 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08001800 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10001801 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07001802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrownd6950432006-07-10 04:44:20 -07001804 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11001805 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
1806 printk_ratelimited(
1807 KERN_WARNING
1808 "md/raid:%s: read error on replacement device "
1809 "(sector %llu on %s).\n",
1810 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001811 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11001812 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001813 else if (conf->mddev->degraded >= conf->max_degraded) {
1814 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001815 printk_ratelimited(
1816 KERN_WARNING
1817 "md/raid:%s: read error not correctable "
1818 "(sector %llu on %s).\n",
1819 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001820 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001821 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001822 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08001823 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10001824 set_bad = 1;
Christian Dietrich8bda4702011-07-27 11:00:36 +10001825 printk_ratelimited(
1826 KERN_WARNING
1827 "md/raid:%s: read error NOT corrected!! "
1828 "(sector %llu on %s).\n",
1829 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10001830 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10001831 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10001832 } else if (atomic_read(&rdev->read_errors)
NeilBrownba22dcb2005-11-08 21:39:31 -08001833 > conf->max_nr_stripes)
NeilBrown14f8d262006-01-06 00:20:14 -08001834 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10001835 "md/raid:%s: Too many read errors, failing device %s.\n",
NeilBrownd6950432006-07-10 04:44:20 -07001836 mdname(conf->mddev), bdn);
NeilBrownba22dcb2005-11-08 21:39:31 -08001837 else
1838 retry = 1;
1839 if (retry)
majianpeng3f9e7c12012-07-31 10:04:21 +10001840 if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
1841 set_bit(R5_ReadError, &sh->dev[i].flags);
1842 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
1843 } else
1844 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08001845 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08001846 clear_bit(R5_ReadError, &sh->dev[i].flags);
1847 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10001848 if (!(set_bad
1849 && test_bit(In_sync, &rdev->flags)
1850 && rdev_set_badblocks(
1851 rdev, sh->sector, STRIPE_SECTORS, 0)))
1852 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08001853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
NeilBrown14a75d32011-12-23 10:17:52 +11001855 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1857 set_bit(STRIPE_HANDLE, &sh->state);
1858 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
NeilBrownd710e132008-10-13 11:55:12 +11001861static void raid5_end_write_request(struct bio *bi, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
NeilBrown99c0fb52009-03-31 14:39:38 +11001863 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11001864 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08001865 int disks = sh->disks, i;
NeilBrown977df362011-12-23 10:17:53 +11001866 struct md_rdev *uninitialized_var(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrownb84db562011-07-28 11:39:23 +10001868 sector_t first_bad;
1869 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11001870 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
NeilBrown977df362011-12-23 10:17:53 +11001872 for (i = 0 ; i < disks; i++) {
1873 if (bi == &sh->dev[i].req) {
1874 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 break;
NeilBrown977df362011-12-23 10:17:53 +11001876 }
1877 if (bi == &sh->dev[i].rreq) {
1878 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11001879 if (rdev)
1880 replacement = 1;
1881 else
1882 /* rdev was removed and 'replacement'
1883 * replaced it. rdev is not removed
1884 * until all requests are finished.
1885 */
1886 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11001887 break;
1888 }
1889 }
Dan Williams45b42332007-07-09 11:56:43 -07001890 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1892 uptodate);
1893 if (i == disks) {
1894 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02001895 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
1897
NeilBrown977df362011-12-23 10:17:53 +11001898 if (replacement) {
1899 if (!uptodate)
1900 md_error(conf->mddev, rdev);
1901 else if (is_badblock(rdev, sh->sector,
1902 STRIPE_SECTORS,
1903 &first_bad, &bad_sectors))
1904 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
1905 } else {
1906 if (!uptodate) {
1907 set_bit(WriteErrorSeen, &rdev->flags);
1908 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11001909 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1910 set_bit(MD_RECOVERY_NEEDED,
1911 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11001912 } else if (is_badblock(rdev, sh->sector,
1913 STRIPE_SECTORS,
1914 &first_bad, &bad_sectors))
1915 set_bit(R5_MadeGood, &sh->dev[i].flags);
1916 }
1917 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
NeilBrown977df362011-12-23 10:17:53 +11001919 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
1920 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrownc04be0a2006-10-03 01:15:53 -07001922 release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
NeilBrown784052e2009-03-31 15:19:07 +11001925static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
NeilBrown784052e2009-03-31 15:19:07 +11001927static void raid5_build_block(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 struct r5dev *dev = &sh->dev[i];
1930
1931 bio_init(&dev->req);
1932 dev->req.bi_io_vec = &dev->vec;
1933 dev->req.bi_vcnt++;
1934 dev->req.bi_max_vecs++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 dev->req.bi_private = sh;
NeilBrown995c4272011-12-23 10:17:52 +11001936 dev->vec.bv_page = dev->page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
NeilBrown977df362011-12-23 10:17:53 +11001938 bio_init(&dev->rreq);
1939 dev->rreq.bi_io_vec = &dev->rvec;
1940 dev->rreq.bi_vcnt++;
1941 dev->rreq.bi_max_vecs++;
1942 dev->rreq.bi_private = sh;
1943 dev->rvec.bv_page = dev->page;
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 dev->flags = 0;
NeilBrown784052e2009-03-31 15:19:07 +11001946 dev->sector = compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
1948
NeilBrownfd01b882011-10-11 16:47:53 +11001949static void error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
1951 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11001952 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11001953 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10001954 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
NeilBrown908f4fb2011-12-23 10:17:50 +11001956 spin_lock_irqsave(&conf->device_lock, flags);
1957 clear_bit(In_sync, &rdev->flags);
1958 mddev->degraded = calc_degraded(conf);
1959 spin_unlock_irqrestore(&conf->device_lock, flags);
1960 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1961
NeilBrownde393cd2011-07-28 11:31:48 +10001962 set_bit(Blocked, &rdev->flags);
NeilBrown6f8d0c72011-05-11 14:38:44 +10001963 set_bit(Faulty, &rdev->flags);
1964 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1965 printk(KERN_ALERT
1966 "md/raid:%s: Disk failure on %s, disabling device.\n"
1967 "md/raid:%s: Operation continuing on %d devices.\n",
1968 mdname(mddev),
1969 bdevname(rdev->bdev, b),
1970 mdname(mddev),
1971 conf->raid_disks - mddev->degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07001972}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974/*
1975 * Input: a 'big' sector number,
1976 * Output: index of the data and parity disk, and the sector # in them.
1977 */
NeilBrownd1688a62011-10-11 16:49:52 +11001978static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11001979 int previous, int *dd_idx,
1980 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
NeilBrown6e3b96e2010-04-23 07:08:28 +10001982 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10001983 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11001985 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11001986 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11001988 int algorithm = previous ? conf->prev_algo
1989 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10001990 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1991 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11001992 int raid_disks = previous ? conf->previous_raid_disks
1993 : conf->raid_disks;
1994 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 /* First compute the information on this sector */
1997
1998 /*
1999 * Compute the chunk number and the sector offset inside the chunk
2000 */
2001 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2002 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 /*
2005 * Compute the stripe number
2006 */
NeilBrown35f2a592010-04-20 14:13:34 +10002007 stripe = chunk_number;
2008 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002009 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 /*
2011 * Select the parity disk based on the user selected algorithm.
2012 */
NeilBrown84789552011-07-27 11:00:36 +10002013 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002014 switch(conf->level) {
2015 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002016 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002017 break;
2018 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002019 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002021 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002022 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 (*dd_idx)++;
2024 break;
2025 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002026 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002027 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 (*dd_idx)++;
2029 break;
2030 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002031 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002032 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 break;
2034 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002035 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002036 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002038 case ALGORITHM_PARITY_0:
2039 pd_idx = 0;
2040 (*dd_idx)++;
2041 break;
2042 case ALGORITHM_PARITY_N:
2043 pd_idx = data_disks;
2044 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002046 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002047 }
2048 break;
2049 case 6:
2050
NeilBrowne183eae2009-03-31 15:20:22 +11002051 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002052 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002053 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002054 qd_idx = pd_idx + 1;
2055 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002056 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002057 qd_idx = 0;
2058 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002059 (*dd_idx) += 2; /* D D P Q D */
2060 break;
2061 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002062 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002063 qd_idx = pd_idx + 1;
2064 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002065 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002066 qd_idx = 0;
2067 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002068 (*dd_idx) += 2; /* D D P Q D */
2069 break;
2070 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002071 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002072 qd_idx = (pd_idx + 1) % raid_disks;
2073 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002074 break;
2075 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002076 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002077 qd_idx = (pd_idx + 1) % raid_disks;
2078 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002079 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002080
2081 case ALGORITHM_PARITY_0:
2082 pd_idx = 0;
2083 qd_idx = 1;
2084 (*dd_idx) += 2;
2085 break;
2086 case ALGORITHM_PARITY_N:
2087 pd_idx = data_disks;
2088 qd_idx = data_disks + 1;
2089 break;
2090
2091 case ALGORITHM_ROTATING_ZERO_RESTART:
2092 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2093 * of blocks for computing Q is different.
2094 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002095 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002096 qd_idx = pd_idx + 1;
2097 if (pd_idx == raid_disks-1) {
2098 (*dd_idx)++; /* Q D D D P */
2099 qd_idx = 0;
2100 } else if (*dd_idx >= pd_idx)
2101 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002102 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002103 break;
2104
2105 case ALGORITHM_ROTATING_N_RESTART:
2106 /* Same a left_asymmetric, by first stripe is
2107 * D D D P Q rather than
2108 * Q D D D P
2109 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002110 stripe2 += 1;
2111 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002112 qd_idx = pd_idx + 1;
2113 if (pd_idx == raid_disks-1) {
2114 (*dd_idx)++; /* Q D D D P */
2115 qd_idx = 0;
2116 } else if (*dd_idx >= pd_idx)
2117 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002118 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002119 break;
2120
2121 case ALGORITHM_ROTATING_N_CONTINUE:
2122 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002123 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002124 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2125 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002126 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002127 break;
2128
2129 case ALGORITHM_LEFT_ASYMMETRIC_6:
2130 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002131 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002132 if (*dd_idx >= pd_idx)
2133 (*dd_idx)++;
2134 qd_idx = raid_disks - 1;
2135 break;
2136
2137 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002138 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002139 if (*dd_idx >= pd_idx)
2140 (*dd_idx)++;
2141 qd_idx = raid_disks - 1;
2142 break;
2143
2144 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002145 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002146 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2147 qd_idx = raid_disks - 1;
2148 break;
2149
2150 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002151 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002152 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2153 qd_idx = raid_disks - 1;
2154 break;
2155
2156 case ALGORITHM_PARITY_0_6:
2157 pd_idx = 0;
2158 (*dd_idx)++;
2159 qd_idx = raid_disks - 1;
2160 break;
2161
NeilBrown16a53ec2006-06-26 00:27:38 -07002162 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002163 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002164 }
2165 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
2167
NeilBrown911d4ee2009-03-31 14:39:38 +11002168 if (sh) {
2169 sh->pd_idx = pd_idx;
2170 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002171 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 /*
2174 * Finally, compute the new sector number
2175 */
2176 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2177 return new_sector;
2178}
2179
2180
NeilBrown784052e2009-03-31 15:19:07 +11002181static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
NeilBrownd1688a62011-10-11 16:49:52 +11002183 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002184 int raid_disks = sh->disks;
2185 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002187 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2188 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002189 int algorithm = previous ? conf->prev_algo
2190 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 sector_t stripe;
2192 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002193 sector_t chunk_number;
2194 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002196 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
NeilBrown16a53ec2006-06-26 00:27:38 -07002198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2200 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
NeilBrown16a53ec2006-06-26 00:27:38 -07002202 if (i == sh->pd_idx)
2203 return 0;
2204 switch(conf->level) {
2205 case 4: break;
2206 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002207 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 case ALGORITHM_LEFT_ASYMMETRIC:
2209 case ALGORITHM_RIGHT_ASYMMETRIC:
2210 if (i > sh->pd_idx)
2211 i--;
2212 break;
2213 case ALGORITHM_LEFT_SYMMETRIC:
2214 case ALGORITHM_RIGHT_SYMMETRIC:
2215 if (i < sh->pd_idx)
2216 i += raid_disks;
2217 i -= (sh->pd_idx + 1);
2218 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002219 case ALGORITHM_PARITY_0:
2220 i -= 1;
2221 break;
2222 case ALGORITHM_PARITY_N:
2223 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002225 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002226 }
2227 break;
2228 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11002229 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002230 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11002231 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002232 case ALGORITHM_LEFT_ASYMMETRIC:
2233 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11002234 case ALGORITHM_ROTATING_ZERO_RESTART:
2235 case ALGORITHM_ROTATING_N_RESTART:
2236 if (sh->pd_idx == raid_disks-1)
2237 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07002238 else if (i > sh->pd_idx)
2239 i -= 2; /* D D P Q D */
2240 break;
2241 case ALGORITHM_LEFT_SYMMETRIC:
2242 case ALGORITHM_RIGHT_SYMMETRIC:
2243 if (sh->pd_idx == raid_disks-1)
2244 i--; /* Q D D D P */
2245 else {
2246 /* D D P Q D */
2247 if (i < sh->pd_idx)
2248 i += raid_disks;
2249 i -= (sh->pd_idx + 2);
2250 }
2251 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002252 case ALGORITHM_PARITY_0:
2253 i -= 2;
2254 break;
2255 case ALGORITHM_PARITY_N:
2256 break;
2257 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11002258 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11002259 if (sh->pd_idx == 0)
2260 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11002261 else {
2262 /* D D Q P D */
2263 if (i < sh->pd_idx)
2264 i += raid_disks;
2265 i -= (sh->pd_idx + 1);
2266 }
NeilBrown99c0fb52009-03-31 14:39:38 +11002267 break;
2268 case ALGORITHM_LEFT_ASYMMETRIC_6:
2269 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2270 if (i > sh->pd_idx)
2271 i--;
2272 break;
2273 case ALGORITHM_LEFT_SYMMETRIC_6:
2274 case ALGORITHM_RIGHT_SYMMETRIC_6:
2275 if (i < sh->pd_idx)
2276 i += data_disks + 1;
2277 i -= (sh->pd_idx + 1);
2278 break;
2279 case ALGORITHM_PARITY_0_6:
2280 i -= 1;
2281 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07002282 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002283 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002284 }
2285 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
2287
2288 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10002289 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
NeilBrown112bf892009-03-31 14:39:38 +11002291 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11002292 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11002293 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2294 || sh2.qd_idx != sh->qd_idx) {
NeilBrown0c55e022010-05-03 14:09:02 +10002295 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2296 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return 0;
2298 }
2299 return r_sector;
2300}
2301
2302
Dan Williams600aa102008-06-28 08:32:05 +10002303static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002304schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10002305 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07002306{
2307 int i, pd_idx = sh->pd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11002308 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002309 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07002310
Dan Williamse33129d2007-01-02 13:52:30 -07002311 if (rcw) {
2312 /* if we are not expanding this is a proper write request, and
2313 * there will be bios with new data to be drained into the
2314 * stripe cache
2315 */
2316 if (!expand) {
Dan Williams600aa102008-06-28 08:32:05 +10002317 sh->reconstruct_state = reconstruct_state_drain_run;
2318 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2319 } else
2320 sh->reconstruct_state = reconstruct_state_run;
Dan Williamse33129d2007-01-02 13:52:30 -07002321
Dan Williamsac6b53b2009-07-14 13:40:19 -07002322 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002323
2324 for (i = disks; i--; ) {
2325 struct r5dev *dev = &sh->dev[i];
2326
2327 if (dev->towrite) {
2328 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10002329 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002330 if (!expand)
2331 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002332 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002333 }
2334 }
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002335 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002336 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002337 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07002338 } else {
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002339 BUG_ON(level == 6);
Dan Williamse33129d2007-01-02 13:52:30 -07002340 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2341 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2342
Dan Williamsd8ee0722008-06-28 08:32:06 +10002343 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
Dan Williams600aa102008-06-28 08:32:05 +10002344 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2345 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002346 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002347
2348 for (i = disks; i--; ) {
2349 struct r5dev *dev = &sh->dev[i];
2350 if (i == pd_idx)
2351 continue;
2352
Dan Williamse33129d2007-01-02 13:52:30 -07002353 if (dev->towrite &&
2354 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10002355 test_bit(R5_Wantcompute, &dev->flags))) {
2356 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07002357 set_bit(R5_LOCKED, &dev->flags);
2358 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10002359 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002360 }
2361 }
2362 }
2363
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002364 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07002365 * are in flight
2366 */
2367 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2368 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10002369 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07002370
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002371 if (level == 6) {
2372 int qd_idx = sh->qd_idx;
2373 struct r5dev *dev = &sh->dev[qd_idx];
2374
2375 set_bit(R5_LOCKED, &dev->flags);
2376 clear_bit(R5_UPTODATE, &dev->flags);
2377 s->locked++;
2378 }
2379
Dan Williams600aa102008-06-28 08:32:05 +10002380 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b2722008-04-28 02:15:50 -07002381 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10002382 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07002383}
NeilBrown16a53ec2006-06-26 00:27:38 -07002384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385/*
2386 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07002387 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 * The bi_next chain must be in order.
2389 */
2390static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2391{
2392 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11002393 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07002394 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
NeilBrowncbe47ec2011-07-26 11:20:35 +10002396 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 (unsigned long long)bi->bi_sector,
2398 (unsigned long long)sh->sector);
2399
Shaohua Lib17459c2012-07-19 16:01:31 +10002400 /*
2401 * If several bio share a stripe. The bio bi_phys_segments acts as a
2402 * reference count to avoid race. The reference count should already be
2403 * increased before this function is called (for example, in
2404 * make_request()), so other bio sharing this stripe will not free the
2405 * stripe. If a stripe is owned by one stripe, the stripe lock will
2406 * protect it.
2407 */
2408 spin_lock_irq(&sh->stripe_lock);
NeilBrown72626682005-09-09 16:23:54 -07002409 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002411 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07002412 firstwrite = 1;
2413 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 bip = &sh->dev[dd_idx].toread;
2415 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2416 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2417 goto overlap;
2418 bip = & (*bip)->bi_next;
2419 }
2420 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2421 goto overlap;
2422
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002423 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 if (*bip)
2425 bi->bi_next = *bip;
2426 *bip = bi;
Shaohua Lie7836bd62012-07-19 16:01:31 +10002427 raid5_inc_bi_active_stripes(bi);
NeilBrown72626682005-09-09 16:23:54 -07002428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 if (forwrite) {
2430 /* check if page is covered */
2431 sector_t sector = sh->dev[dd_idx].sector;
2432 for (bi=sh->dev[dd_idx].towrite;
2433 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2434 bi && bi->bi_sector <= sector;
2435 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2436 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2437 sector = bi->bi_sector + (bi->bi_size>>9);
2438 }
2439 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2440 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2441 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10002442
2443 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2444 (unsigned long long)(*bip)->bi_sector,
2445 (unsigned long long)sh->sector, dd_idx);
NeilBrownb97390a2012-10-11 13:50:12 +11002446 spin_unlock_irq(&sh->stripe_lock);
NeilBrowncbe47ec2011-07-26 11:20:35 +10002447
2448 if (conf->mddev->bitmap && firstwrite) {
2449 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2450 STRIPE_SECTORS, 0);
2451 sh->bm_seq = conf->seq_flush+1;
2452 set_bit(STRIPE_BIT_DELAY, &sh->state);
2453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return 1;
2455
2456 overlap:
2457 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10002458 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 return 0;
2460}
2461
NeilBrownd1688a62011-10-11 16:49:52 +11002462static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08002463
NeilBrownd1688a62011-10-11 16:49:52 +11002464static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002465 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08002466{
NeilBrown784052e2009-03-31 15:19:07 +11002467 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10002468 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11002469 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002470 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11002471 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07002472
NeilBrown112bf892009-03-31 14:39:38 +11002473 raid5_compute_sector(conf,
2474 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08002475 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11002476 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11002477 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08002478}
2479
Dan Williamsa4456852007-07-09 11:56:43 -07002480static void
NeilBrownd1688a62011-10-11 16:49:52 +11002481handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002482 struct stripe_head_state *s, int disks,
2483 struct bio **return_bi)
2484{
2485 int i;
2486 for (i = disks; i--; ) {
2487 struct bio *bi;
2488 int bitmap_end = 0;
2489
2490 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11002491 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07002492 rcu_read_lock();
2493 rdev = rcu_dereference(conf->disks[i].rdev);
2494 if (rdev && test_bit(In_sync, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10002495 atomic_inc(&rdev->nr_pending);
2496 else
2497 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07002498 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10002499 if (rdev) {
2500 if (!rdev_set_badblocks(
2501 rdev,
2502 sh->sector,
2503 STRIPE_SECTORS, 0))
2504 md_error(conf->mddev, rdev);
2505 rdev_dec_pending(rdev, conf->mddev);
2506 }
Dan Williamsa4456852007-07-09 11:56:43 -07002507 }
Shaohua Lib17459c2012-07-19 16:01:31 +10002508 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002509 /* fail all writes first */
2510 bi = sh->dev[i].towrite;
2511 sh->dev[i].towrite = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10002512 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11002513 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07002514 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07002515
2516 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2517 wake_up(&conf->wait_for_overlap);
2518
2519 while (bi && bi->bi_sector <
2520 sh->dev[i].sector + STRIPE_SECTORS) {
2521 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2522 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002523 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002524 md_write_end(conf->mddev);
2525 bi->bi_next = *return_bi;
2526 *return_bi = bi;
2527 }
2528 bi = nextbi;
2529 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002530 if (bitmap_end)
2531 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2532 STRIPE_SECTORS, 0, 0);
2533 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002534 /* and fail all 'written' */
2535 bi = sh->dev[i].written;
2536 sh->dev[i].written = NULL;
2537 if (bi) bitmap_end = 1;
2538 while (bi && bi->bi_sector <
2539 sh->dev[i].sector + STRIPE_SECTORS) {
2540 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2541 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002542 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002543 md_write_end(conf->mddev);
2544 bi->bi_next = *return_bi;
2545 *return_bi = bi;
2546 }
2547 bi = bi2;
2548 }
2549
Dan Williamsb5e98d62007-01-02 13:52:31 -07002550 /* fail any reads if this device is non-operational and
2551 * the data has not reached the cache yet.
2552 */
2553 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2554 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2555 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11002556 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002557 bi = sh->dev[i].toread;
2558 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11002559 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07002560 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2561 wake_up(&conf->wait_for_overlap);
Dan Williamsa4456852007-07-09 11:56:43 -07002562 while (bi && bi->bi_sector <
2563 sh->dev[i].sector + STRIPE_SECTORS) {
2564 struct bio *nextbi =
2565 r5_next_bio(bi, sh->dev[i].sector);
2566 clear_bit(BIO_UPTODATE, &bi->bi_flags);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002567 if (!raid5_dec_bi_active_stripes(bi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002568 bi->bi_next = *return_bi;
2569 *return_bi = bi;
2570 }
2571 bi = nextbi;
2572 }
2573 }
Dan Williamsa4456852007-07-09 11:56:43 -07002574 if (bitmap_end)
2575 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2576 STRIPE_SECTORS, 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10002577 /* If we were in the middle of a write the parity block might
2578 * still be locked - so just clear all R5_LOCKED flags
2579 */
2580 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07002581 }
2582
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002583 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2584 if (atomic_dec_and_test(&conf->pending_full_writes))
2585 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002586}
2587
NeilBrown7f0da592011-07-28 11:39:22 +10002588static void
NeilBrownd1688a62011-10-11 16:49:52 +11002589handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10002590 struct stripe_head_state *s)
2591{
2592 int abort = 0;
2593 int i;
2594
NeilBrown7f0da592011-07-28 11:39:22 +10002595 clear_bit(STRIPE_SYNCING, &sh->state);
2596 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11002597 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10002598 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10002599 * Don't even need to abort as that is handled elsewhere
2600 * if needed, and not always wanted e.g. if there is a known
2601 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11002602 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10002603 * non-sync devices, or abort the recovery
2604 */
NeilBrown18b98372012-04-01 23:48:38 +10002605 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
2606 /* During recovery devices cannot be removed, so
2607 * locking and refcounting of rdevs is not needed
2608 */
2609 for (i = 0; i < conf->raid_disks; i++) {
2610 struct md_rdev *rdev = conf->disks[i].rdev;
2611 if (rdev
2612 && !test_bit(Faulty, &rdev->flags)
2613 && !test_bit(In_sync, &rdev->flags)
2614 && !rdev_set_badblocks(rdev, sh->sector,
2615 STRIPE_SECTORS, 0))
2616 abort = 1;
2617 rdev = conf->disks[i].replacement;
2618 if (rdev
2619 && !test_bit(Faulty, &rdev->flags)
2620 && !test_bit(In_sync, &rdev->flags)
2621 && !rdev_set_badblocks(rdev, sh->sector,
2622 STRIPE_SECTORS, 0))
2623 abort = 1;
2624 }
2625 if (abort)
2626 conf->recovery_disabled =
2627 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10002628 }
NeilBrown18b98372012-04-01 23:48:38 +10002629 md_done_sync(conf->mddev, STRIPE_SECTORS, !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10002630}
2631
NeilBrown9a3e1102011-12-23 10:17:53 +11002632static int want_replace(struct stripe_head *sh, int disk_idx)
2633{
2634 struct md_rdev *rdev;
2635 int rv = 0;
2636 /* Doing recovery so rcu locking not required */
2637 rdev = sh->raid_conf->disks[disk_idx].replacement;
2638 if (rdev
2639 && !test_bit(Faulty, &rdev->flags)
2640 && !test_bit(In_sync, &rdev->flags)
2641 && (rdev->recovery_offset <= sh->sector
2642 || rdev->mddev->recovery_cp <= sh->sector))
2643 rv = 1;
2644
2645 return rv;
2646}
2647
NeilBrown93b3dbc2011-07-27 11:00:36 +10002648/* fetch_block - checks the given member device to see if its data needs
Dan Williams1fe797e2008-06-28 09:16:30 +10002649 * to be read or computed to satisfy a request.
2650 *
2651 * Returns 1 when no more member devices need to be checked, otherwise returns
NeilBrown93b3dbc2011-07-27 11:00:36 +10002652 * 0 to tell the loop in handle_stripe_fill to continue
Dan Williamsf38e1212007-01-02 13:52:30 -07002653 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002654static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2655 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07002656{
2657 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10002658 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2659 &sh->dev[s->failed_num[1]] };
Dan Williamsf38e1212007-01-02 13:52:30 -07002660
Dan Williamsf38e1212007-01-02 13:52:30 -07002661 /* is the data in this block needed, and can we get it? */
2662 if (!test_bit(R5_LOCKED, &dev->flags) &&
Dan Williams1fe797e2008-06-28 09:16:30 +10002663 !test_bit(R5_UPTODATE, &dev->flags) &&
2664 (dev->toread ||
2665 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2666 s->syncing || s->expanding ||
NeilBrown9a3e1102011-12-23 10:17:53 +11002667 (s->replacing && want_replace(sh, disk_idx)) ||
NeilBrown5d35e092011-07-27 11:00:36 +10002668 (s->failed >= 1 && fdev[0]->toread) ||
2669 (s->failed >= 2 && fdev[1]->toread) ||
NeilBrown93b3dbc2011-07-27 11:00:36 +10002670 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2671 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2672 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002673 /* we would like to get this block, possibly by computing it,
2674 * otherwise read it if the backing disk is insync
2675 */
2676 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2677 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2678 if ((s->uptodate == disks - 1) &&
NeilBrownf2b3b442011-07-26 11:35:19 +10002679 (s->failed && (disk_idx == s->failed_num[0] ||
2680 disk_idx == s->failed_num[1]))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002681 /* have disk failed, and we're requested to fetch it;
2682 * do compute it
2683 */
2684 pr_debug("Computing stripe %llu block %d\n",
2685 (unsigned long long)sh->sector, disk_idx);
2686 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2687 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2688 set_bit(R5_Wantcompute, &dev->flags);
2689 sh->ops.target = disk_idx;
2690 sh->ops.target2 = -1; /* no 2nd target */
2691 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10002692 /* Careful: from this point on 'uptodate' is in the eye
2693 * of raid_run_ops which services 'compute' operations
2694 * before writes. R5_Wantcompute flags a block that will
2695 * be R5_UPTODATE by the time it is needed for a
2696 * subsequent operation.
2697 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002698 s->uptodate++;
2699 return 1;
2700 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2701 /* Computing 2-failure is *very* expensive; only
2702 * do it if failed >= 2
2703 */
2704 int other;
2705 for (other = disks; other--; ) {
2706 if (other == disk_idx)
2707 continue;
2708 if (!test_bit(R5_UPTODATE,
2709 &sh->dev[other].flags))
2710 break;
2711 }
2712 BUG_ON(other < 0);
2713 pr_debug("Computing stripe %llu blocks %d,%d\n",
2714 (unsigned long long)sh->sector,
2715 disk_idx, other);
2716 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2717 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2718 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2719 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2720 sh->ops.target = disk_idx;
2721 sh->ops.target2 = other;
2722 s->uptodate += 2;
2723 s->req_compute = 1;
2724 return 1;
2725 } else if (test_bit(R5_Insync, &dev->flags)) {
2726 set_bit(R5_LOCKED, &dev->flags);
2727 set_bit(R5_Wantread, &dev->flags);
2728 s->locked++;
2729 pr_debug("Reading block %d (sync=%d)\n",
2730 disk_idx, s->syncing);
2731 }
2732 }
2733
2734 return 0;
2735}
2736
2737/**
NeilBrown93b3dbc2011-07-27 11:00:36 +10002738 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002739 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10002740static void handle_stripe_fill(struct stripe_head *sh,
2741 struct stripe_head_state *s,
2742 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002743{
2744 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002745
2746 /* look for blocks to read/compute, skip this if a compute
2747 * is already in flight, or if the stripe contents are in the
2748 * midst of changing due to a write
2749 */
2750 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2751 !sh->reconstruct_state)
2752 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10002753 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07002754 break;
Dan Williamsa4456852007-07-09 11:56:43 -07002755 set_bit(STRIPE_HANDLE, &sh->state);
2756}
2757
2758
Dan Williams1fe797e2008-06-28 09:16:30 +10002759/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07002760 * any written block on an uptodate or failed drive can be returned.
2761 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2762 * never LOCKED, so we don't need to test 'failed' directly.
2763 */
NeilBrownd1688a62011-10-11 16:49:52 +11002764static void handle_stripe_clean_event(struct r5conf *conf,
Dan Williamsa4456852007-07-09 11:56:43 -07002765 struct stripe_head *sh, int disks, struct bio **return_bi)
2766{
2767 int i;
2768 struct r5dev *dev;
2769
2770 for (i = disks; i--; )
2771 if (sh->dev[i].written) {
2772 dev = &sh->dev[i];
2773 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e444762012-10-11 13:49:49 +11002774 (test_bit(R5_UPTODATE, &dev->flags) ||
2775 test_and_clear_bit(R5_Discard, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002776 /* We can return any write requests */
2777 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07002778 pr_debug("Return write for disc %d\n", i);
Dan Williamsa4456852007-07-09 11:56:43 -07002779 wbi = dev->written;
2780 dev->written = NULL;
2781 while (wbi && wbi->bi_sector <
2782 dev->sector + STRIPE_SECTORS) {
2783 wbi2 = r5_next_bio(wbi, dev->sector);
Shaohua Lie7836bd62012-07-19 16:01:31 +10002784 if (!raid5_dec_bi_active_stripes(wbi)) {
Dan Williamsa4456852007-07-09 11:56:43 -07002785 md_write_end(conf->mddev);
2786 wbi->bi_next = *return_bi;
2787 *return_bi = wbi;
2788 }
2789 wbi = wbi2;
2790 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002791 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2792 STRIPE_SECTORS,
Dan Williamsa4456852007-07-09 11:56:43 -07002793 !test_bit(STRIPE_DEGRADED, &sh->state),
Shaohua Li7eaf7e82012-07-19 16:01:31 +10002794 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002795 }
2796 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07002797
2798 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2799 if (atomic_dec_and_test(&conf->pending_full_writes))
2800 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07002801}
2802
NeilBrownd1688a62011-10-11 16:49:52 +11002803static void handle_stripe_dirtying(struct r5conf *conf,
NeilBrownc8ac1802011-07-27 11:00:36 +10002804 struct stripe_head *sh,
2805 struct stripe_head_state *s,
2806 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07002807{
2808 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002809 sector_t recovery_cp = conf->mddev->recovery_cp;
2810
2811 /* RAID6 requires 'rcw' in current implementation.
2812 * Otherwise, check whether resync is now happening or should start.
2813 * If yes, then the array is dirty (after unclean shutdown or
2814 * initial creation), so parity in some stripes might be inconsistent.
2815 * In this case, we need to always do reconstruct-write, to ensure
2816 * that in case of drive failure or read-error correction, we
2817 * generate correct data from the parity.
2818 */
2819 if (conf->max_degraded == 2 ||
2820 (recovery_cp < MaxSector && sh->sector >= recovery_cp)) {
2821 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10002822 * look like rcw is cheaper
2823 */
2824 rcw = 1; rmw = 2;
Alexander Lyakasa7854482012-10-11 13:50:12 +11002825 pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n",
2826 conf->max_degraded, (unsigned long long)recovery_cp,
2827 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10002828 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07002829 /* would I have to read this buffer for read_modify_write */
2830 struct r5dev *dev = &sh->dev[i];
2831 if ((dev->towrite || i == sh->pd_idx) &&
2832 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002833 !(test_bit(R5_UPTODATE, &dev->flags) ||
2834 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07002835 if (test_bit(R5_Insync, &dev->flags))
2836 rmw++;
2837 else
2838 rmw += 2*disks; /* cannot read it */
2839 }
2840 /* Would I have to read this buffer for reconstruct_write */
2841 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2842 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002843 !(test_bit(R5_UPTODATE, &dev->flags) ||
2844 test_bit(R5_Wantcompute, &dev->flags))) {
2845 if (test_bit(R5_Insync, &dev->flags)) rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07002846 else
2847 rcw += 2*disks;
2848 }
2849 }
Dan Williams45b42332007-07-09 11:56:43 -07002850 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
Dan Williamsa4456852007-07-09 11:56:43 -07002851 (unsigned long long)sh->sector, rmw, rcw);
2852 set_bit(STRIPE_HANDLE, &sh->state);
2853 if (rmw < rcw && rmw > 0)
2854 /* prefer read-modify-write, but need to get some data */
2855 for (i = disks; i--; ) {
2856 struct r5dev *dev = &sh->dev[i];
2857 if ((dev->towrite || i == sh->pd_idx) &&
2858 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002859 !(test_bit(R5_UPTODATE, &dev->flags) ||
2860 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07002861 test_bit(R5_Insync, &dev->flags)) {
2862 if (
2863 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002864 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002865 "%d for r-m-w\n", i);
2866 set_bit(R5_LOCKED, &dev->flags);
2867 set_bit(R5_Wantread, &dev->flags);
2868 s->locked++;
2869 } else {
2870 set_bit(STRIPE_DELAYED, &sh->state);
2871 set_bit(STRIPE_HANDLE, &sh->state);
2872 }
2873 }
2874 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002875 if (rcw <= rmw && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07002876 /* want reconstruct write, but need to get some data */
NeilBrownc8ac1802011-07-27 11:00:36 +10002877 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07002878 for (i = disks; i--; ) {
2879 struct r5dev *dev = &sh->dev[i];
2880 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10002881 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07002882 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07002883 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10002884 test_bit(R5_Wantcompute, &dev->flags))) {
2885 rcw++;
2886 if (!test_bit(R5_Insync, &dev->flags))
2887 continue; /* it's a failed drive */
Dan Williamsa4456852007-07-09 11:56:43 -07002888 if (
2889 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07002890 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07002891 "%d for Reconstruct\n", i);
2892 set_bit(R5_LOCKED, &dev->flags);
2893 set_bit(R5_Wantread, &dev->flags);
2894 s->locked++;
2895 } else {
2896 set_bit(STRIPE_DELAYED, &sh->state);
2897 set_bit(STRIPE_HANDLE, &sh->state);
2898 }
2899 }
2900 }
NeilBrownc8ac1802011-07-27 11:00:36 +10002901 }
Dan Williamsa4456852007-07-09 11:56:43 -07002902 /* now if nothing is locked, and if we have enough data,
2903 * we can start a write request
2904 */
Dan Williamsf38e1212007-01-02 13:52:30 -07002905 /* since handle_stripe can be called at any time we need to handle the
2906 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07002907 * subsequent call wants to start a write request. raid_run_ops only
2908 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07002909 * simultaneously. If this is not the case then new writes need to be
2910 * held off until the compute completes.
2911 */
Dan Williams976ea8d2008-06-28 08:32:03 +10002912 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2913 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2914 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07002915 schedule_reconstruction(sh, s, rcw == 0, 0);
Dan Williamsa4456852007-07-09 11:56:43 -07002916}
2917
NeilBrownd1688a62011-10-11 16:49:52 +11002918static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07002919 struct stripe_head_state *s, int disks)
2920{
Dan Williamsecc65c92008-06-28 08:31:57 +10002921 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07002922
Dan Williamsbd2ab672008-04-10 21:29:27 -07002923 set_bit(STRIPE_HANDLE, &sh->state);
2924
Dan Williamsecc65c92008-06-28 08:31:57 +10002925 switch (sh->check_state) {
2926 case check_state_idle:
2927 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07002928 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07002929 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10002930 sh->check_state = check_state_run;
2931 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002932 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07002933 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10002934 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07002935 }
NeilBrownf2b3b442011-07-26 11:35:19 +10002936 dev = &sh->dev[s->failed_num[0]];
Dan Williamsecc65c92008-06-28 08:31:57 +10002937 /* fall through */
2938 case check_state_compute_result:
2939 sh->check_state = check_state_idle;
2940 if (!dev)
2941 dev = &sh->dev[sh->pd_idx];
2942
2943 /* check that a write has not made the stripe insync */
2944 if (test_bit(STRIPE_INSYNC, &sh->state))
2945 break;
2946
2947 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07002948 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2949 BUG_ON(s->uptodate != disks);
2950
2951 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10002952 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07002953 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07002954
Dan Williamsa4456852007-07-09 11:56:43 -07002955 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07002956 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002957 break;
2958 case check_state_run:
2959 break; /* we will be called again upon completion */
2960 case check_state_check_result:
2961 sh->check_state = check_state_idle;
2962
2963 /* if a failure occurred during the check operation, leave
2964 * STRIPE_INSYNC not set and let the stripe be handled again
2965 */
2966 if (s->failed)
2967 break;
2968
2969 /* handle a successful check operation, if parity is correct
2970 * we are done. Otherwise update the mismatch count and repair
2971 * parity if !MD_RECOVERY_CHECK
2972 */
Dan Williamsad283ea2009-08-29 19:09:26 -07002973 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10002974 /* parity is correct (on disc,
2975 * not in buffer any more)
2976 */
2977 set_bit(STRIPE_INSYNC, &sh->state);
2978 else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11002979 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsecc65c92008-06-28 08:31:57 +10002980 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2981 /* don't try to repair!! */
2982 set_bit(STRIPE_INSYNC, &sh->state);
2983 else {
2984 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10002985 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10002986 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2987 set_bit(R5_Wantcompute,
2988 &sh->dev[sh->pd_idx].flags);
2989 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002990 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10002991 s->uptodate++;
2992 }
2993 }
2994 break;
2995 case check_state_compute_run:
2996 break;
2997 default:
2998 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2999 __func__, sh->check_state,
3000 (unsigned long long) sh->sector);
3001 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003002 }
3003}
3004
3005
NeilBrownd1688a62011-10-11 16:49:52 +11003006static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07003007 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10003008 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003009{
Dan Williamsa4456852007-07-09 11:56:43 -07003010 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11003011 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07003012 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07003013
3014 set_bit(STRIPE_HANDLE, &sh->state);
3015
3016 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003017
Dan Williamsa4456852007-07-09 11:56:43 -07003018 /* Want to check and possibly repair P and Q.
3019 * However there could be one 'failed' device, in which
3020 * case we can only check one of them, possibly using the
3021 * other to generate missing data
3022 */
3023
Dan Williamsd82dfee2009-07-14 13:40:57 -07003024 switch (sh->check_state) {
3025 case check_state_idle:
3026 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10003027 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003028 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07003029 * makes sense to check P (If anything else were failed,
3030 * we would have used P to recreate it).
3031 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003032 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07003033 }
NeilBrownf2b3b442011-07-26 11:35:19 +10003034 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07003035 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07003036 * anything, so it makes sense to check it
3037 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003038 if (sh->check_state == check_state_run)
3039 sh->check_state = check_state_run_pq;
3040 else
3041 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07003042 }
Dan Williams36d1c642009-07-14 11:48:22 -07003043
Dan Williamsd82dfee2009-07-14 13:40:57 -07003044 /* discard potentially stale zero_sum_result */
3045 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07003046
Dan Williamsd82dfee2009-07-14 13:40:57 -07003047 if (sh->check_state == check_state_run) {
3048 /* async_xor_zero_sum destroys the contents of P */
3049 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
3050 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07003051 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003052 if (sh->check_state >= check_state_run &&
3053 sh->check_state <= check_state_run_pq) {
3054 /* async_syndrome_zero_sum preserves P and Q, so
3055 * no need to mark them !uptodate here
3056 */
3057 set_bit(STRIPE_OP_CHECK, &s->ops_request);
3058 break;
3059 }
Dan Williams36d1c642009-07-14 11:48:22 -07003060
Dan Williamsd82dfee2009-07-14 13:40:57 -07003061 /* we have 2-disk failure */
3062 BUG_ON(s->failed != 2);
3063 /* fall through */
3064 case check_state_compute_result:
3065 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07003066
Dan Williamsd82dfee2009-07-14 13:40:57 -07003067 /* check that a write has not made the stripe insync */
3068 if (test_bit(STRIPE_INSYNC, &sh->state))
3069 break;
Dan Williamsa4456852007-07-09 11:56:43 -07003070
3071 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07003072 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07003073 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07003074 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
Dan Williamsa4456852007-07-09 11:56:43 -07003075 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003076 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07003077 s->locked++;
3078 set_bit(R5_LOCKED, &dev->flags);
3079 set_bit(R5_Wantwrite, &dev->flags);
3080 }
3081 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10003082 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07003083 s->locked++;
3084 set_bit(R5_LOCKED, &dev->flags);
3085 set_bit(R5_Wantwrite, &dev->flags);
3086 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003087 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003088 dev = &sh->dev[pd_idx];
3089 s->locked++;
3090 set_bit(R5_LOCKED, &dev->flags);
3091 set_bit(R5_Wantwrite, &dev->flags);
3092 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07003093 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07003094 dev = &sh->dev[qd_idx];
3095 s->locked++;
3096 set_bit(R5_LOCKED, &dev->flags);
3097 set_bit(R5_Wantwrite, &dev->flags);
3098 }
3099 clear_bit(STRIPE_DEGRADED, &sh->state);
3100
3101 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003102 break;
3103 case check_state_run:
3104 case check_state_run_q:
3105 case check_state_run_pq:
3106 break; /* we will be called again upon completion */
3107 case check_state_check_result:
3108 sh->check_state = check_state_idle;
3109
3110 /* handle a successful check operation, if parity is correct
3111 * we are done. Otherwise update the mismatch count and repair
3112 * parity if !MD_RECOVERY_CHECK
3113 */
3114 if (sh->ops.zero_sum_result == 0) {
3115 /* both parities are correct */
3116 if (!s->failed)
3117 set_bit(STRIPE_INSYNC, &sh->state);
3118 else {
3119 /* in contrast to the raid5 case we can validate
3120 * parity, but still have a failure to write
3121 * back
3122 */
3123 sh->check_state = check_state_compute_result;
3124 /* Returning at this point means that we may go
3125 * off and bring p and/or q uptodate again so
3126 * we make sure to check zero_sum_result again
3127 * to verify if p or q need writeback
3128 */
3129 }
3130 } else {
Jianpeng Ma7f7583d2012-10-11 14:17:59 +11003131 atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches);
Dan Williamsd82dfee2009-07-14 13:40:57 -07003132 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
3133 /* don't try to repair!! */
3134 set_bit(STRIPE_INSYNC, &sh->state);
3135 else {
3136 int *target = &sh->ops.target;
3137
3138 sh->ops.target = -1;
3139 sh->ops.target2 = -1;
3140 sh->check_state = check_state_compute_run;
3141 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3142 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3143 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
3144 set_bit(R5_Wantcompute,
3145 &sh->dev[pd_idx].flags);
3146 *target = pd_idx;
3147 target = &sh->ops.target2;
3148 s->uptodate++;
3149 }
3150 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
3151 set_bit(R5_Wantcompute,
3152 &sh->dev[qd_idx].flags);
3153 *target = qd_idx;
3154 s->uptodate++;
3155 }
3156 }
3157 }
3158 break;
3159 case check_state_compute_run:
3160 break;
3161 default:
3162 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
3163 __func__, sh->check_state,
3164 (unsigned long long) sh->sector);
3165 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07003166 }
3167}
3168
NeilBrownd1688a62011-10-11 16:49:52 +11003169static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07003170{
3171 int i;
3172
3173 /* We have read all the blocks in this stripe and now we need to
3174 * copy some of them into a target stripe for expand.
3175 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07003176 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003177 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3178 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11003179 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11003180 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07003181 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07003182 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07003183
NeilBrown784052e2009-03-31 15:19:07 +11003184 sector_t bn = compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11003185 sector_t s = raid5_compute_sector(conf, bn, 0,
3186 &dd_idx, NULL);
NeilBrowna8c906c2009-06-09 14:39:59 +10003187 sh2 = get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07003188 if (sh2 == NULL)
3189 /* so far only the early blocks of this stripe
3190 * have been requested. When later blocks
3191 * get requested, we will try again
3192 */
3193 continue;
3194 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
3195 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
3196 /* must have already done this block */
3197 release_stripe(sh2);
3198 continue;
3199 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07003200
3201 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07003202 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003203 tx = async_memcpy(sh2->dev[dd_idx].page,
Dan Williams88ba2aa2009-04-09 16:16:18 -07003204 sh->dev[i].page, 0, 0, STRIPE_SIZE,
Dan Williamsa08abd82009-06-03 11:43:59 -07003205 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003206
Dan Williamsa4456852007-07-09 11:56:43 -07003207 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
3208 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
3209 for (j = 0; j < conf->raid_disks; j++)
3210 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10003211 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003212 !test_bit(R5_Expanded, &sh2->dev[j].flags))
3213 break;
3214 if (j == conf->raid_disks) {
3215 set_bit(STRIPE_EXPAND_READY, &sh2->state);
3216 set_bit(STRIPE_HANDLE, &sh2->state);
3217 }
3218 release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07003219
Dan Williamsa4456852007-07-09 11:56:43 -07003220 }
NeilBrowna2e08552007-09-11 15:23:36 -07003221 /* done submitting copies, wait for them to complete */
3222 if (tx) {
3223 async_tx_ack(tx);
3224 dma_wait_for_async_tx(tx);
3225 }
Dan Williamsa4456852007-07-09 11:56:43 -07003226}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
3228/*
3229 * handle_stripe - do things to a stripe.
3230 *
NeilBrown9a3e1102011-12-23 10:17:53 +11003231 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
3232 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11003234 * return some read requests which now have data
3235 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 * schedule a read on some buffers
3237 * schedule a write of some buffers
3238 * return confirmation of parity correctness
3239 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 */
Dan Williamsa4456852007-07-09 11:56:43 -07003241
NeilBrownacfe7262011-07-27 11:00:36 +10003242static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07003243{
NeilBrownd1688a62011-10-11 16:49:52 +11003244 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08003245 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003246 struct r5dev *dev;
3247 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11003248 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07003249
NeilBrownacfe7262011-07-27 11:00:36 +10003250 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07003251
NeilBrownacfe7262011-07-27 11:00:36 +10003252 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
3253 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
3254 s->failed_num[0] = -1;
3255 s->failed_num[1] = -1;
3256
3257 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07003258 rcu_read_lock();
3259 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003260 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10003261 sector_t first_bad;
3262 int bad_sectors;
3263 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10003264
NeilBrown16a53ec2006-06-26 00:27:38 -07003265 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07003266
Dan Williams45b42332007-07-09 11:56:43 -07003267 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11003268 i, dev->flags,
3269 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003270 /* maybe we can reply to a read
3271 *
3272 * new wantfill requests are only permitted while
3273 * ops_complete_biofill is guaranteed to be inactive
3274 */
3275 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3276 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3277 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07003278
3279 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10003280 if (test_bit(R5_LOCKED, &dev->flags))
3281 s->locked++;
3282 if (test_bit(R5_UPTODATE, &dev->flags))
3283 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003284 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003285 s->compute++;
3286 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07003287 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003288
NeilBrownacfe7262011-07-27 11:00:36 +10003289 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003290 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10003291 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10003292 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003293 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10003294 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003295 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10003296 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07003297 }
Dan Williamsa4456852007-07-09 11:56:43 -07003298 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10003299 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11003300 /* Prefer to use the replacement for reads, but only
3301 * if it is recovered enough and has no bad blocks.
3302 */
3303 rdev = rcu_dereference(conf->disks[i].replacement);
3304 if (rdev && !test_bit(Faulty, &rdev->flags) &&
3305 rdev->recovery_offset >= sh->sector + STRIPE_SECTORS &&
3306 !is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3307 &first_bad, &bad_sectors))
3308 set_bit(R5_ReadRepl, &dev->flags);
3309 else {
NeilBrown9a3e1102011-12-23 10:17:53 +11003310 if (rdev)
3311 set_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11003312 rdev = rcu_dereference(conf->disks[i].rdev);
3313 clear_bit(R5_ReadRepl, &dev->flags);
3314 }
NeilBrown9283d8c2011-12-08 16:27:57 +11003315 if (rdev && test_bit(Faulty, &rdev->flags))
3316 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10003317 if (rdev) {
3318 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3319 &first_bad, &bad_sectors);
3320 if (s->blocked_rdev == NULL
3321 && (test_bit(Blocked, &rdev->flags)
3322 || is_bad < 0)) {
3323 if (is_bad < 0)
3324 set_bit(BlockedBadBlocks,
3325 &rdev->flags);
3326 s->blocked_rdev = rdev;
3327 atomic_inc(&rdev->nr_pending);
3328 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003329 }
NeilBrown415e72d2010-06-17 17:25:21 +10003330 clear_bit(R5_Insync, &dev->flags);
3331 if (!rdev)
3332 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10003333 else if (is_bad) {
3334 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10003335 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
3336 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10003337 /* treat as in-sync, but with a read error
3338 * which we can now try to correct
3339 */
3340 set_bit(R5_Insync, &dev->flags);
3341 set_bit(R5_ReadError, &dev->flags);
3342 }
3343 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10003344 set_bit(R5_Insync, &dev->flags);
NeilBrown30d7a482011-12-23 09:57:00 +11003345 else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10003346 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11003347 set_bit(R5_Insync, &dev->flags);
3348 else if (test_bit(R5_UPTODATE, &dev->flags) &&
3349 test_bit(R5_Expanded, &dev->flags))
3350 /* If we've reshaped into here, we assume it is Insync.
3351 * We will shortly update recovery_offset to make
3352 * it official.
3353 */
3354 set_bit(R5_Insync, &dev->flags);
3355
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003356 if (rdev && test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003357 /* This flag does not apply to '.replacement'
3358 * only to .rdev, so make sure to check that*/
3359 struct md_rdev *rdev2 = rcu_dereference(
3360 conf->disks[i].rdev);
3361 if (rdev2 == rdev)
3362 clear_bit(R5_Insync, &dev->flags);
3363 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10003364 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003365 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10003366 } else
3367 clear_bit(R5_WriteError, &dev->flags);
3368 }
Adam Kwolek5d8c71f2011-12-09 14:26:11 +11003369 if (rdev && test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11003370 /* This flag does not apply to '.replacement'
3371 * only to .rdev, so make sure to check that*/
3372 struct md_rdev *rdev2 = rcu_dereference(
3373 conf->disks[i].rdev);
3374 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10003375 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11003376 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10003377 } else
3378 clear_bit(R5_MadeGood, &dev->flags);
3379 }
NeilBrown977df362011-12-23 10:17:53 +11003380 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
3381 struct md_rdev *rdev2 = rcu_dereference(
3382 conf->disks[i].replacement);
3383 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
3384 s->handle_bad_blocks = 1;
3385 atomic_inc(&rdev2->nr_pending);
3386 } else
3387 clear_bit(R5_MadeGoodRepl, &dev->flags);
3388 }
NeilBrown415e72d2010-06-17 17:25:21 +10003389 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003390 /* The ReadError flag will just be confusing now */
3391 clear_bit(R5_ReadError, &dev->flags);
3392 clear_bit(R5_ReWrite, &dev->flags);
3393 }
NeilBrown415e72d2010-06-17 17:25:21 +10003394 if (test_bit(R5_ReadError, &dev->flags))
3395 clear_bit(R5_Insync, &dev->flags);
3396 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10003397 if (s->failed < 2)
3398 s->failed_num[s->failed] = i;
3399 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11003400 if (rdev && !test_bit(Faulty, &rdev->flags))
3401 do_recovery = 1;
NeilBrown415e72d2010-06-17 17:25:21 +10003402 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003403 }
NeilBrown9a3e1102011-12-23 10:17:53 +11003404 if (test_bit(STRIPE_SYNCING, &sh->state)) {
3405 /* If there is a failed device being replaced,
3406 * we must be recovering.
3407 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10003408 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11003409 * else we can only be replacing
3410 * sync and recovery both need to read all devices, and so
3411 * use the same flag.
3412 */
3413 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10003414 sh->sector >= conf->mddev->recovery_cp ||
3415 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11003416 s->syncing = 1;
3417 else
3418 s->replacing = 1;
3419 }
NeilBrown16a53ec2006-06-26 00:27:38 -07003420 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10003421}
NeilBrownf4168852007-02-28 20:11:53 -08003422
NeilBrowncc940152011-07-26 11:35:35 +10003423static void handle_stripe(struct stripe_head *sh)
3424{
3425 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11003426 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10003427 int i;
NeilBrown84789552011-07-27 11:00:36 +10003428 int prexor;
3429 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10003430 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10003431
3432 clear_bit(STRIPE_HANDLE, &sh->state);
Dan Williams257a4b42011-11-08 16:22:06 +11003433 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10003434 /* already being handled, ensure it gets handled
3435 * again when current action finishes */
3436 set_bit(STRIPE_HANDLE, &sh->state);
3437 return;
3438 }
3439
3440 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3441 set_bit(STRIPE_SYNCING, &sh->state);
3442 clear_bit(STRIPE_INSYNC, &sh->state);
3443 }
3444 clear_bit(STRIPE_DELAYED, &sh->state);
3445
3446 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3447 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3448 (unsigned long long)sh->sector, sh->state,
3449 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3450 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10003451
NeilBrownacfe7262011-07-27 11:00:36 +10003452 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10003453
NeilBrownbc2607f2011-07-28 11:39:22 +10003454 if (s.handle_bad_blocks) {
3455 set_bit(STRIPE_HANDLE, &sh->state);
3456 goto finish;
3457 }
3458
NeilBrown474af965fe2011-07-27 11:00:36 +10003459 if (unlikely(s.blocked_rdev)) {
3460 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11003461 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10003462 set_bit(STRIPE_HANDLE, &sh->state);
3463 goto finish;
3464 }
3465 /* There is nothing for the blocked_rdev to block */
3466 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3467 s.blocked_rdev = NULL;
3468 }
3469
3470 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3471 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3472 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3473 }
3474
3475 pr_debug("locked=%d uptodate=%d to_read=%d"
3476 " to_write=%d failed=%d failed_num=%d,%d\n",
3477 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3478 s.failed_num[0], s.failed_num[1]);
3479 /* check if the array has lost more than max_degraded devices and,
3480 * if so, some requests might need to be failed.
3481 */
NeilBrown9a3f5302011-11-08 16:22:01 +11003482 if (s.failed > conf->max_degraded) {
3483 sh->check_state = 0;
3484 sh->reconstruct_state = 0;
3485 if (s.to_read+s.to_write+s.written)
3486 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
NeilBrown9a3e1102011-12-23 10:17:53 +11003487 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11003488 handle_failed_sync(conf, sh, &s);
3489 }
NeilBrown474af965fe2011-07-27 11:00:36 +10003490
3491 /*
3492 * might be able to return some write requests if the parity blocks
3493 * are safe, or on a failed drive
3494 */
3495 pdev = &sh->dev[sh->pd_idx];
3496 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3497 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3498 qdev = &sh->dev[sh->qd_idx];
3499 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3500 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3501 || conf->level < 6;
3502
3503 if (s.written &&
3504 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3505 && !test_bit(R5_LOCKED, &pdev->flags)
Shaohua Li9e444762012-10-11 13:49:49 +11003506 && (test_bit(R5_UPTODATE, &pdev->flags) ||
3507 test_bit(R5_Discard, &pdev->flags))))) &&
NeilBrown474af965fe2011-07-27 11:00:36 +10003508 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3509 && !test_bit(R5_LOCKED, &qdev->flags)
Shaohua Li9e444762012-10-11 13:49:49 +11003510 && (test_bit(R5_UPTODATE, &qdev->flags) ||
3511 test_bit(R5_Discard, &qdev->flags))))))
NeilBrown474af965fe2011-07-27 11:00:36 +10003512 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3513
3514 /* Now we might consider reading some blocks, either to check/generate
3515 * parity, or to satisfy requests
3516 * or to load a block that is being partially written.
3517 */
3518 if (s.to_read || s.non_overwrite
3519 || (conf->level == 6 && s.to_write && s.failed)
NeilBrown9a3e1102011-12-23 10:17:53 +11003520 || (s.syncing && (s.uptodate + s.compute < disks))
3521 || s.replacing
3522 || s.expanding)
NeilBrown474af965fe2011-07-27 11:00:36 +10003523 handle_stripe_fill(sh, &s, disks);
3524
NeilBrown84789552011-07-27 11:00:36 +10003525 /* Now we check to see if any write operations have recently
3526 * completed
3527 */
3528 prexor = 0;
3529 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3530 prexor = 1;
3531 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3532 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3533 sh->reconstruct_state = reconstruct_state_idle;
3534
3535 /* All the 'written' buffers and the parity block are ready to
3536 * be written back to disk
3537 */
Shaohua Li9e444762012-10-11 13:49:49 +11003538 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
3539 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003540 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e444762012-10-11 13:49:49 +11003541 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
3542 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10003543 for (i = disks; i--; ) {
3544 struct r5dev *dev = &sh->dev[i];
3545 if (test_bit(R5_LOCKED, &dev->flags) &&
3546 (i == sh->pd_idx || i == sh->qd_idx ||
3547 dev->written)) {
3548 pr_debug("Writing block %d\n", i);
3549 set_bit(R5_Wantwrite, &dev->flags);
3550 if (prexor)
3551 continue;
3552 if (!test_bit(R5_Insync, &dev->flags) ||
3553 ((i == sh->pd_idx || i == sh->qd_idx) &&
3554 s.failed == 0))
3555 set_bit(STRIPE_INSYNC, &sh->state);
3556 }
3557 }
3558 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3559 s.dec_preread_active = 1;
3560 }
3561
3562 /* Now to consider new write requests and what else, if anything
3563 * should be read. We do not handle new writes when:
3564 * 1/ A 'write' operation (copy+xor) is already in flight.
3565 * 2/ A 'check' operation is in flight, as it may clobber the parity
3566 * block.
3567 */
3568 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3569 handle_stripe_dirtying(conf, sh, &s, disks);
3570
3571 /* maybe we need to check and possibly fix the parity for this stripe
3572 * Any reads will already have been scheduled, so we just see if enough
3573 * data is available. The parity check is held off while parity
3574 * dependent operations are in flight.
3575 */
3576 if (sh->check_state ||
3577 (s.syncing && s.locked == 0 &&
3578 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3579 !test_bit(STRIPE_INSYNC, &sh->state))) {
3580 if (conf->level == 6)
3581 handle_parity_checks6(conf, sh, &s, disks);
3582 else
3583 handle_parity_checks5(conf, sh, &s, disks);
3584 }
NeilBrownc5a31002011-07-27 11:00:36 +10003585
NeilBrown9a3e1102011-12-23 10:17:53 +11003586 if (s.replacing && s.locked == 0
3587 && !test_bit(STRIPE_INSYNC, &sh->state)) {
3588 /* Write out to replacement devices where possible */
3589 for (i = 0; i < conf->raid_disks; i++)
3590 if (test_bit(R5_UPTODATE, &sh->dev[i].flags) &&
3591 test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
3592 set_bit(R5_WantReplace, &sh->dev[i].flags);
3593 set_bit(R5_LOCKED, &sh->dev[i].flags);
3594 s.locked++;
3595 }
3596 set_bit(STRIPE_INSYNC, &sh->state);
3597 }
3598 if ((s.syncing || s.replacing) && s.locked == 0 &&
3599 test_bit(STRIPE_INSYNC, &sh->state)) {
NeilBrownc5a31002011-07-27 11:00:36 +10003600 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3601 clear_bit(STRIPE_SYNCING, &sh->state);
3602 }
3603
3604 /* If the failed drives are just a ReadError, then we might need
3605 * to progress the repair/check process
3606 */
3607 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3608 for (i = 0; i < s.failed; i++) {
3609 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3610 if (test_bit(R5_ReadError, &dev->flags)
3611 && !test_bit(R5_LOCKED, &dev->flags)
3612 && test_bit(R5_UPTODATE, &dev->flags)
3613 ) {
3614 if (!test_bit(R5_ReWrite, &dev->flags)) {
3615 set_bit(R5_Wantwrite, &dev->flags);
3616 set_bit(R5_ReWrite, &dev->flags);
3617 set_bit(R5_LOCKED, &dev->flags);
3618 s.locked++;
3619 } else {
3620 /* let's read it back */
3621 set_bit(R5_Wantread, &dev->flags);
3622 set_bit(R5_LOCKED, &dev->flags);
3623 s.locked++;
3624 }
3625 }
3626 }
3627
3628
NeilBrown3687c062011-07-27 11:00:36 +10003629 /* Finish reconstruct operations initiated by the expansion process */
3630 if (sh->reconstruct_state == reconstruct_state_result) {
3631 struct stripe_head *sh_src
3632 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3633 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3634 /* sh cannot be written until sh_src has been read.
3635 * so arrange for sh to be delayed a little
3636 */
3637 set_bit(STRIPE_DELAYED, &sh->state);
3638 set_bit(STRIPE_HANDLE, &sh->state);
3639 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3640 &sh_src->state))
3641 atomic_inc(&conf->preread_active_stripes);
3642 release_stripe(sh_src);
3643 goto finish;
3644 }
3645 if (sh_src)
3646 release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07003647
NeilBrown3687c062011-07-27 11:00:36 +10003648 sh->reconstruct_state = reconstruct_state_idle;
3649 clear_bit(STRIPE_EXPANDING, &sh->state);
3650 for (i = conf->raid_disks; i--; ) {
3651 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3652 set_bit(R5_LOCKED, &sh->dev[i].flags);
3653 s.locked++;
3654 }
3655 }
3656
3657 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3658 !sh->reconstruct_state) {
3659 /* Need to write out all blocks after computing parity */
3660 sh->disks = conf->raid_disks;
3661 stripe_set_idx(sh->sector, conf, 0, sh);
3662 schedule_reconstruction(sh, &s, 1, 1);
3663 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3664 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3665 atomic_dec(&conf->reshape_stripes);
3666 wake_up(&conf->wait_for_overlap);
3667 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3668 }
3669
3670 if (s.expanding && s.locked == 0 &&
3671 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3672 handle_stripe_expansion(conf, sh);
3673
3674finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07003675 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10003676 if (unlikely(s.blocked_rdev)) {
3677 if (conf->mddev->external)
3678 md_wait_for_blocked_rdev(s.blocked_rdev,
3679 conf->mddev);
3680 else
3681 /* Internal metadata will immediately
3682 * be written by raid5d, so we don't
3683 * need to wait here.
3684 */
3685 rdev_dec_pending(s.blocked_rdev,
3686 conf->mddev);
3687 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07003688
NeilBrownbc2607f2011-07-28 11:39:22 +10003689 if (s.handle_bad_blocks)
3690 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11003691 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10003692 struct r5dev *dev = &sh->dev[i];
3693 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3694 /* We own a safe reference to the rdev */
3695 rdev = conf->disks[i].rdev;
3696 if (!rdev_set_badblocks(rdev, sh->sector,
3697 STRIPE_SECTORS, 0))
3698 md_error(conf->mddev, rdev);
3699 rdev_dec_pending(rdev, conf->mddev);
3700 }
NeilBrownb84db562011-07-28 11:39:23 +10003701 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3702 rdev = conf->disks[i].rdev;
3703 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003704 STRIPE_SECTORS, 0);
NeilBrownb84db562011-07-28 11:39:23 +10003705 rdev_dec_pending(rdev, conf->mddev);
3706 }
NeilBrown977df362011-12-23 10:17:53 +11003707 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
3708 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11003709 if (!rdev)
3710 /* rdev have been moved down */
3711 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11003712 rdev_clear_badblocks(rdev, sh->sector,
NeilBrownc6563a82012-05-21 09:27:00 +10003713 STRIPE_SECTORS, 0);
NeilBrown977df362011-12-23 10:17:53 +11003714 rdev_dec_pending(rdev, conf->mddev);
3715 }
NeilBrownbc2607f2011-07-28 11:39:22 +10003716 }
3717
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07003718 if (s.ops_request)
3719 raid_run_ops(sh, s.ops_request);
3720
Dan Williamsf0e43bc2008-06-28 08:31:55 +10003721 ops_run_io(sh, &s);
3722
NeilBrownc5709ef2011-07-26 11:35:20 +10003723 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11003724 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02003725 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11003726 * have actually been submitted.
3727 */
3728 atomic_dec(&conf->preread_active_stripes);
3729 if (atomic_read(&conf->preread_active_stripes) <
3730 IO_THRESHOLD)
3731 md_wakeup_thread(conf->mddev->thread);
3732 }
3733
NeilBrownc5709ef2011-07-26 11:35:20 +10003734 return_io(s.return_bi);
NeilBrown16a53ec2006-06-26 00:27:38 -07003735
Dan Williams257a4b42011-11-08 16:22:06 +11003736 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07003737}
3738
NeilBrownd1688a62011-10-11 16:49:52 +11003739static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740{
3741 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3742 while (!list_empty(&conf->delayed_list)) {
3743 struct list_head *l = conf->delayed_list.next;
3744 struct stripe_head *sh;
3745 sh = list_entry(l, struct stripe_head, lru);
3746 list_del_init(l);
3747 clear_bit(STRIPE_DELAYED, &sh->state);
3748 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3749 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003750 list_add_tail(&sh->lru, &conf->hold_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 }
NeilBrown482c0832011-04-18 18:25:42 +10003752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753}
3754
NeilBrownd1688a62011-10-11 16:49:52 +11003755static void activate_bit_delay(struct r5conf *conf)
NeilBrown72626682005-09-09 16:23:54 -07003756{
3757 /* device_lock is held */
3758 struct list_head head;
3759 list_add(&head, &conf->bitmap_list);
3760 list_del_init(&conf->bitmap_list);
3761 while (!list_empty(&head)) {
3762 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3763 list_del_init(&sh->lru);
3764 atomic_inc(&sh->count);
3765 __release_stripe(conf, sh);
3766 }
3767}
3768
NeilBrownfd01b882011-10-11 16:47:53 +11003769int md_raid5_congested(struct mddev *mddev, int bits)
NeilBrownf022b2f2006-10-03 01:15:56 -07003770{
NeilBrownd1688a62011-10-11 16:49:52 +11003771 struct r5conf *conf = mddev->private;
NeilBrownf022b2f2006-10-03 01:15:56 -07003772
3773 /* No difference between reads and writes. Just check
3774 * how busy the stripe_cache is
3775 */
NeilBrown3fa841d2009-09-23 18:10:29 +10003776
NeilBrownf022b2f2006-10-03 01:15:56 -07003777 if (conf->inactive_blocked)
3778 return 1;
3779 if (conf->quiesce)
3780 return 1;
3781 if (list_empty_careful(&conf->inactive_list))
3782 return 1;
3783
3784 return 0;
3785}
NeilBrown11d8a6e2010-07-26 11:57:07 +10003786EXPORT_SYMBOL_GPL(md_raid5_congested);
3787
3788static int raid5_congested(void *data, int bits)
3789{
NeilBrownfd01b882011-10-11 16:47:53 +11003790 struct mddev *mddev = data;
NeilBrown11d8a6e2010-07-26 11:57:07 +10003791
3792 return mddev_congested(mddev, bits) ||
3793 md_raid5_congested(mddev, bits);
3794}
NeilBrownf022b2f2006-10-03 01:15:56 -07003795
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003796/* We want read requests to align with chunks where possible,
3797 * but write requests don't need to.
3798 */
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003799static int raid5_mergeable_bvec(struct request_queue *q,
3800 struct bvec_merge_data *bvm,
3801 struct bio_vec *biovec)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003802{
NeilBrownfd01b882011-10-11 16:47:53 +11003803 struct mddev *mddev = q->queuedata;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003804 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003805 int max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003806 unsigned int chunk_sectors = mddev->chunk_sectors;
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003807 unsigned int bio_sectors = bvm->bi_size >> 9;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003808
Alasdair G Kergoncc371e62008-07-03 09:53:43 +02003809 if ((bvm->bi_rw & 1) == WRITE)
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003810 return biovec->bv_len; /* always allow writes to be mergeable */
3811
Andre Noll664e7c42009-06-18 08:45:27 +10003812 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3813 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)23032a02006-12-10 02:20:45 -08003814 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3815 if (max < 0) max = 0;
3816 if (max <= biovec->bv_len && bio_sectors == 0)
3817 return biovec->bv_len;
3818 else
3819 return max;
3820}
3821
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003822
NeilBrownfd01b882011-10-11 16:47:53 +11003823static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003824{
3825 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
Andre Noll9d8f0362009-06-18 08:45:01 +10003826 unsigned int chunk_sectors = mddev->chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003827 unsigned int bio_sectors = bio->bi_size >> 9;
3828
Andre Noll664e7c42009-06-18 08:45:27 +10003829 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3830 chunk_sectors = mddev->new_chunk_sectors;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003831 return chunk_sectors >=
3832 ((sector & (chunk_sectors - 1)) + bio_sectors);
3833}
3834
3835/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003836 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3837 * later sampled by raid5d.
3838 */
NeilBrownd1688a62011-10-11 16:49:52 +11003839static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003840{
3841 unsigned long flags;
3842
3843 spin_lock_irqsave(&conf->device_lock, flags);
3844
3845 bi->bi_next = conf->retry_read_aligned_list;
3846 conf->retry_read_aligned_list = bi;
3847
3848 spin_unlock_irqrestore(&conf->device_lock, flags);
3849 md_wakeup_thread(conf->mddev->thread);
3850}
3851
3852
NeilBrownd1688a62011-10-11 16:49:52 +11003853static struct bio *remove_bio_from_retry(struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003854{
3855 struct bio *bi;
3856
3857 bi = conf->retry_read_aligned;
3858 if (bi) {
3859 conf->retry_read_aligned = NULL;
3860 return bi;
3861 }
3862 bi = conf->retry_read_aligned_list;
3863 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08003864 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003865 bi->bi_next = NULL;
Jens Axboe960e7392008-08-15 10:41:18 +02003866 /*
3867 * this sets the active strip count to 1 and the processed
3868 * strip count to zero (upper 8 bits)
3869 */
Shaohua Lie7836bd62012-07-19 16:01:31 +10003870 raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003871 }
3872
3873 return bi;
3874}
3875
3876
3877/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003878 * The "raid5_align_endio" should check if the read succeeded and if it
3879 * did, call bio_endio on the original bio (having bio_put the new bio
3880 * first).
3881 * If the read failed..
3882 */
NeilBrown6712ecf2007-09-27 12:47:43 +02003883static void raid5_align_endio(struct bio *bi, int error)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003884{
3885 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11003886 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11003887 struct r5conf *conf;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003888 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
NeilBrown3cb03002011-10-11 16:45:26 +11003889 struct md_rdev *rdev;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003890
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003891 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003892
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003893 rdev = (void*)raid_bi->bi_next;
3894 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11003895 mddev = rdev->mddev;
3896 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003897
3898 rdev_dec_pending(rdev, conf->mddev);
3899
3900 if (!error && uptodate) {
NeilBrown6712ecf2007-09-27 12:47:43 +02003901 bio_endio(raid_bi, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003902 if (atomic_dec_and_test(&conf->active_aligned_reads))
3903 wake_up(&conf->wait_for_stripe);
NeilBrown6712ecf2007-09-27 12:47:43 +02003904 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003905 }
3906
3907
Dan Williams45b42332007-07-09 11:56:43 -07003908 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003909
3910 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003911}
3912
Neil Brown387bb172007-02-08 14:20:29 -08003913static int bio_fits_rdev(struct bio *bi)
3914{
Jens Axboe165125e2007-07-24 09:28:11 +02003915 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
Neil Brown387bb172007-02-08 14:20:29 -08003916
Martin K. Petersenae03bf62009-05-22 17:17:50 -04003917 if ((bi->bi_size>>9) > queue_max_sectors(q))
Neil Brown387bb172007-02-08 14:20:29 -08003918 return 0;
3919 blk_recount_segments(q, bi);
Martin K. Petersen8a783622010-02-26 00:20:39 -05003920 if (bi->bi_phys_segments > queue_max_segments(q))
Neil Brown387bb172007-02-08 14:20:29 -08003921 return 0;
3922
3923 if (q->merge_bvec_fn)
3924 /* it's too hard to apply the merge_bvec_fn at this stage,
3925 * just just give up
3926 */
3927 return 0;
3928
3929 return 1;
3930}
3931
3932
NeilBrownfd01b882011-10-11 16:47:53 +11003933static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003934{
NeilBrownd1688a62011-10-11 16:49:52 +11003935 struct r5conf *conf = mddev->private;
NeilBrown8553fe72009-12-14 12:49:47 +11003936 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003937 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11003938 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11003939 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003940
3941 if (!in_chunk_boundary(mddev, raid_bio)) {
Dan Williams45b42332007-07-09 11:56:43 -07003942 pr_debug("chunk_aligned_read : non aligned\n");
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003943 return 0;
3944 }
3945 /*
NeilBrowna167f662010-10-26 18:31:13 +11003946 * use bio_clone_mddev to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003947 */
NeilBrowna167f662010-10-26 18:31:13 +11003948 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003949 if (!align_bi)
3950 return 0;
3951 /*
3952 * set bi_end_io to a new function, and set bi_private to the
3953 * original bio.
3954 */
3955 align_bi->bi_end_io = raid5_align_endio;
3956 align_bi->bi_private = raid_bio;
3957 /*
3958 * compute position
3959 */
NeilBrown112bf892009-03-31 14:39:38 +11003960 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3961 0,
NeilBrown911d4ee2009-03-31 14:39:38 +11003962 &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003963
NeilBrown671488c2011-12-23 10:17:52 +11003964 end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003965 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11003966 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
3967 if (!rdev || test_bit(Faulty, &rdev->flags) ||
3968 rdev->recovery_offset < end_sector) {
3969 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3970 if (rdev &&
3971 (test_bit(Faulty, &rdev->flags) ||
3972 !(test_bit(In_sync, &rdev->flags) ||
3973 rdev->recovery_offset >= end_sector)))
3974 rdev = NULL;
3975 }
3976 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10003977 sector_t first_bad;
3978 int bad_sectors;
3979
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08003980 atomic_inc(&rdev->nr_pending);
3981 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003982 raid_bio->bi_next = (void*)rdev;
3983 align_bi->bi_bdev = rdev->bdev;
3984 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003985
NeilBrown31c176e2011-07-28 11:39:22 +10003986 if (!bio_fits_rdev(align_bi) ||
3987 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3988 &first_bad, &bad_sectors)) {
3989 /* too big in some way, or has a known bad block */
Neil Brown387bb172007-02-08 14:20:29 -08003990 bio_put(align_bi);
3991 rdev_dec_pending(rdev, mddev);
3992 return 0;
3993 }
3994
majianpeng6c0544e2012-06-12 08:31:10 +08003995 /* No reshape active, so we can trust rdev->data_offset */
3996 align_bi->bi_sector += rdev->data_offset;
3997
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08003998 spin_lock_irq(&conf->device_lock);
3999 wait_event_lock_irq(conf->wait_for_stripe,
4000 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01004001 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004002 atomic_inc(&conf->active_aligned_reads);
4003 spin_unlock_irq(&conf->device_lock);
4004
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004005 generic_make_request(align_bi);
4006 return 1;
4007 } else {
4008 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004009 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004010 return 0;
4011 }
4012}
4013
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004014/* __get_priority_stripe - get the next stripe to process
4015 *
4016 * Full stripe writes are allowed to pass preread active stripes up until
4017 * the bypass_threshold is exceeded. In general the bypass_count
4018 * increments when the handle_list is handled before the hold_list; however, it
4019 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
4020 * stripe with in flight i/o. The bypass_count will be reset when the
4021 * head of the hold_list has changed, i.e. the head was promoted to the
4022 * handle_list.
4023 */
NeilBrownd1688a62011-10-11 16:49:52 +11004024static struct stripe_head *__get_priority_stripe(struct r5conf *conf)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004025{
4026 struct stripe_head *sh;
4027
4028 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
4029 __func__,
4030 list_empty(&conf->handle_list) ? "empty" : "busy",
4031 list_empty(&conf->hold_list) ? "empty" : "busy",
4032 atomic_read(&conf->pending_full_writes), conf->bypass_count);
4033
4034 if (!list_empty(&conf->handle_list)) {
4035 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
4036
4037 if (list_empty(&conf->hold_list))
4038 conf->bypass_count = 0;
4039 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
4040 if (conf->hold_list.next == conf->last_hold)
4041 conf->bypass_count++;
4042 else {
4043 conf->last_hold = conf->hold_list.next;
4044 conf->bypass_count -= conf->bypass_threshold;
4045 if (conf->bypass_count < 0)
4046 conf->bypass_count = 0;
4047 }
4048 }
4049 } else if (!list_empty(&conf->hold_list) &&
4050 ((conf->bypass_threshold &&
4051 conf->bypass_count > conf->bypass_threshold) ||
4052 atomic_read(&conf->pending_full_writes) == 0)) {
4053 sh = list_entry(conf->hold_list.next,
4054 typeof(*sh), lru);
4055 conf->bypass_count -= conf->bypass_threshold;
4056 if (conf->bypass_count < 0)
4057 conf->bypass_count = 0;
4058 } else
4059 return NULL;
4060
4061 list_del_init(&sh->lru);
4062 atomic_inc(&sh->count);
4063 BUG_ON(atomic_read(&sh->count) != 1);
4064 return sh;
4065}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08004066
Shaohua Li8811b592012-08-02 08:33:00 +10004067struct raid5_plug_cb {
4068 struct blk_plug_cb cb;
4069 struct list_head list;
4070};
4071
4072static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
4073{
4074 struct raid5_plug_cb *cb = container_of(
4075 blk_cb, struct raid5_plug_cb, cb);
4076 struct stripe_head *sh;
4077 struct mddev *mddev = cb->cb.data;
4078 struct r5conf *conf = mddev->private;
4079
4080 if (cb->list.next && !list_empty(&cb->list)) {
4081 spin_lock_irq(&conf->device_lock);
4082 while (!list_empty(&cb->list)) {
4083 sh = list_first_entry(&cb->list, struct stripe_head, lru);
4084 list_del_init(&sh->lru);
4085 /*
4086 * avoid race release_stripe_plug() sees
4087 * STRIPE_ON_UNPLUG_LIST clear but the stripe
4088 * is still in our list
4089 */
4090 smp_mb__before_clear_bit();
4091 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
4092 __release_stripe(conf, sh);
4093 }
4094 spin_unlock_irq(&conf->device_lock);
4095 }
4096 kfree(cb);
4097}
4098
4099static void release_stripe_plug(struct mddev *mddev,
4100 struct stripe_head *sh)
4101{
4102 struct blk_plug_cb *blk_cb = blk_check_plugged(
4103 raid5_unplug, mddev,
4104 sizeof(struct raid5_plug_cb));
4105 struct raid5_plug_cb *cb;
4106
4107 if (!blk_cb) {
4108 release_stripe(sh);
4109 return;
4110 }
4111
4112 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
4113
4114 if (cb->list.next == NULL)
4115 INIT_LIST_HEAD(&cb->list);
4116
4117 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
4118 list_add_tail(&sh->lru, &cb->list);
4119 else
4120 release_stripe(sh);
4121}
4122
Shaohua Li620125f2012-10-11 13:49:05 +11004123static void make_discard_request(struct mddev *mddev, struct bio *bi)
4124{
4125 struct r5conf *conf = mddev->private;
4126 sector_t logical_sector, last_sector;
4127 struct stripe_head *sh;
4128 int remaining;
4129 int stripe_sectors;
4130
4131 if (mddev->reshape_position != MaxSector)
4132 /* Skip discard while reshape is happening */
4133 return;
4134
4135 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4136 last_sector = bi->bi_sector + (bi->bi_size>>9);
4137
4138 bi->bi_next = NULL;
4139 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
4140
4141 stripe_sectors = conf->chunk_sectors *
4142 (conf->raid_disks - conf->max_degraded);
4143 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
4144 stripe_sectors);
4145 sector_div(last_sector, stripe_sectors);
4146
4147 logical_sector *= conf->chunk_sectors;
4148 last_sector *= conf->chunk_sectors;
4149
4150 for (; logical_sector < last_sector;
4151 logical_sector += STRIPE_SECTORS) {
4152 DEFINE_WAIT(w);
4153 int d;
4154 again:
4155 sh = get_active_stripe(conf, logical_sector, 0, 0, 0);
4156 prepare_to_wait(&conf->wait_for_overlap, &w,
4157 TASK_UNINTERRUPTIBLE);
4158 spin_lock_irq(&sh->stripe_lock);
4159 for (d = 0; d < conf->raid_disks; d++) {
4160 if (d == sh->pd_idx || d == sh->qd_idx)
4161 continue;
4162 if (sh->dev[d].towrite || sh->dev[d].toread) {
4163 set_bit(R5_Overlap, &sh->dev[d].flags);
4164 spin_unlock_irq(&sh->stripe_lock);
4165 release_stripe(sh);
4166 schedule();
4167 goto again;
4168 }
4169 }
4170 finish_wait(&conf->wait_for_overlap, &w);
4171 for (d = 0; d < conf->raid_disks; d++) {
4172 if (d == sh->pd_idx || d == sh->qd_idx)
4173 continue;
4174 sh->dev[d].towrite = bi;
4175 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
4176 raid5_inc_bi_active_stripes(bi);
4177 }
4178 spin_unlock_irq(&sh->stripe_lock);
4179 if (conf->mddev->bitmap) {
4180 for (d = 0;
4181 d < conf->raid_disks - conf->max_degraded;
4182 d++)
4183 bitmap_startwrite(mddev->bitmap,
4184 sh->sector,
4185 STRIPE_SECTORS,
4186 0);
4187 sh->bm_seq = conf->seq_flush + 1;
4188 set_bit(STRIPE_BIT_DELAY, &sh->state);
4189 }
4190
4191 set_bit(STRIPE_HANDLE, &sh->state);
4192 clear_bit(STRIPE_DELAYED, &sh->state);
4193 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4194 atomic_inc(&conf->preread_active_stripes);
4195 release_stripe_plug(mddev, sh);
4196 }
4197
4198 remaining = raid5_dec_bi_active_stripes(bi);
4199 if (remaining == 0) {
4200 md_write_end(mddev);
4201 bio_endio(bi, 0);
4202 }
4203}
4204
Linus Torvaldsb4fdcb02011-11-04 17:06:58 -07004205static void make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206{
NeilBrownd1688a62011-10-11 16:49:52 +11004207 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11004208 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 sector_t new_sector;
4210 sector_t logical_sector, last_sector;
4211 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01004212 const int rw = bio_data_dir(bi);
NeilBrown49077322010-03-25 16:20:56 +11004213 int remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Tejun Heoe9c74692010-09-03 11:56:18 +02004215 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
4216 md_flush_request(mddev, bi);
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004217 return;
NeilBrowne5dcdd82005-09-09 16:23:41 -07004218 }
4219
NeilBrown3d310eb2005-06-21 17:17:26 -07004220 md_write_start(mddev, bi);
NeilBrown06d91a52005-06-21 17:17:12 -07004221
NeilBrown802ba062006-12-13 00:34:13 -08004222 if (rw == READ &&
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004223 mddev->reshape_position == MaxSector &&
NeilBrown21a52c62010-04-01 15:02:13 +11004224 chunk_aligned_read(mddev,bi))
Christoph Hellwig5a7bbad2011-09-12 12:12:01 +02004225 return;
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08004226
Shaohua Li620125f2012-10-11 13:49:05 +11004227 if (unlikely(bi->bi_rw & REQ_DISCARD)) {
4228 make_discard_request(mddev, bi);
4229 return;
4230 }
4231
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4233 last_sector = bi->bi_sector + (bi->bi_size>>9);
4234 bi->bi_next = NULL;
4235 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
NeilBrown06d91a52005-06-21 17:17:12 -07004236
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
4238 DEFINE_WAIT(w);
NeilBrownb5663ba2009-03-31 14:39:38 +11004239 int previous;
NeilBrownb578d552006-03-27 01:18:12 -08004240
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004241 retry:
NeilBrownb5663ba2009-03-31 14:39:38 +11004242 previous = 0;
NeilBrownb578d552006-03-27 01:18:12 -08004243 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11004244 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11004245 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f72006-03-27 01:18:15 -08004246 * 64bit on a 32bit platform, and so it might be
4247 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02004248 * Of course reshape_progress could change after
NeilBrowndf8e7f72006-03-27 01:18:15 -08004249 * the lock is dropped, so once we get a reference
4250 * to the stripe that we think it is, we will have
4251 * to check again.
4252 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004253 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004254 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004255 ? logical_sector < conf->reshape_progress
4256 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11004257 previous = 1;
4258 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10004259 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11004260 ? logical_sector < conf->reshape_safe
4261 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08004262 spin_unlock_irq(&conf->device_lock);
4263 schedule();
4264 goto retry;
4265 }
4266 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004267 spin_unlock_irq(&conf->device_lock);
4268 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004269
NeilBrown112bf892009-03-31 14:39:38 +11004270 new_sector = raid5_compute_sector(conf, logical_sector,
4271 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11004272 &dd_idx, NULL);
NeilBrown0c55e022010-05-03 14:09:02 +10004273 pr_debug("raid456: make_request, sector %llu logical %llu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 (unsigned long long)new_sector,
4275 (unsigned long long)logical_sector);
4276
NeilBrownb5663ba2009-03-31 14:39:38 +11004277 sh = get_active_stripe(conf, new_sector, previous,
NeilBrowna8c906c2009-06-09 14:39:59 +10004278 (bi->bi_rw&RWA_MASK), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11004280 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004281 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f72006-03-27 01:18:15 -08004282 * stripe, so we must do the range check again.
4283 * Expansion could still move past after this
4284 * test, but as we are holding a reference to
4285 * 'sh', we know that if that happens,
4286 * STRIPE_EXPANDING will get set and the expansion
4287 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004288 */
4289 int must_retry = 0;
4290 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004291 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11004292 ? logical_sector >= conf->reshape_progress
4293 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004294 /* mismatch, need to try again */
4295 must_retry = 1;
4296 spin_unlock_irq(&conf->device_lock);
4297 if (must_retry) {
4298 release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07004299 schedule();
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004300 goto retry;
4301 }
4302 }
NeilBrowne62e58a2009-07-01 13:15:35 +10004303
Namhyung Kimffd96e32011-07-18 17:38:51 +10004304 if (rw == WRITE &&
NeilBrowna5c308d2009-07-01 13:15:35 +10004305 logical_sector >= mddev->suspend_lo &&
NeilBrowne464eaf2006-03-27 01:18:14 -08004306 logical_sector < mddev->suspend_hi) {
4307 release_stripe(sh);
NeilBrowne62e58a2009-07-01 13:15:35 +10004308 /* As the suspend_* range is controlled by
4309 * userspace, we want an interruptible
4310 * wait.
4311 */
4312 flush_signals(current);
4313 prepare_to_wait(&conf->wait_for_overlap,
4314 &w, TASK_INTERRUPTIBLE);
4315 if (logical_sector >= mddev->suspend_lo &&
4316 logical_sector < mddev->suspend_hi)
4317 schedule();
NeilBrowne464eaf2006-03-27 01:18:14 -08004318 goto retry;
4319 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004320
4321 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
Namhyung Kimffd96e32011-07-18 17:38:51 +10004322 !add_stripe_bio(sh, bi, dd_idx, rw)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08004323 /* Stripe is busy expanding or
4324 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 * and wait a while
4326 */
NeilBrown482c0832011-04-18 18:25:42 +10004327 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 release_stripe(sh);
4329 schedule();
4330 goto retry;
4331 }
4332 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown6ed30032008-02-06 01:40:00 -08004333 set_bit(STRIPE_HANDLE, &sh->state);
4334 clear_bit(STRIPE_DELAYED, &sh->state);
NeilBrowna852d7b2012-09-19 12:48:30 +10004335 if ((bi->bi_rw & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11004336 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4337 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10004338 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 } else {
4340 /* cannot get stripe for read-ahead, just give-up */
4341 clear_bit(BIO_UPTODATE, &bi->bi_flags);
4342 finish_wait(&conf->wait_for_overlap, &w);
4343 break;
4344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 }
NeilBrown7c13edc2011-04-18 18:25:43 +10004346
Shaohua Lie7836bd62012-07-19 16:01:31 +10004347 remaining = raid5_dec_bi_active_stripes(bi);
NeilBrownf6344752006-03-27 01:18:17 -08004348 if (remaining == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
NeilBrown16a53ec2006-06-26 00:27:38 -07004350 if ( rw == WRITE )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 md_write_end(mddev);
NeilBrown6712ecf2007-09-27 12:47:43 +02004352
Neil Brown0e13fe232008-06-28 08:31:20 +10004353 bio_endio(bi, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355}
4356
NeilBrownfd01b882011-10-11 16:47:53 +11004357static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11004358
NeilBrownfd01b882011-10-11 16:47:53 +11004359static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360{
NeilBrown52c03292006-06-26 00:27:43 -07004361 /* reshaping is quite different to recovery/resync so it is
4362 * handled quite separately ... here.
4363 *
4364 * On each call to sync_request, we gather one chunk worth of
4365 * destination stripes and flag them as expanding.
4366 * Then we find all the source stripes and request reads.
4367 * As the reads complete, handle_stripe will copy the data
4368 * into the destination stripe and release that stripe.
4369 */
NeilBrownd1688a62011-10-11 16:49:52 +11004370 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 struct stripe_head *sh;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004372 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08004373 int raid_disks = conf->previous_raid_disks;
4374 int data_disks = raid_disks - conf->max_degraded;
4375 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07004376 int i;
4377 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11004378 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11004379 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11004380 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11004381 struct list_head stripes;
NeilBrown52c03292006-06-26 00:27:43 -07004382
NeilBrownfef9c612009-03-31 15:16:46 +11004383 if (sector_nr == 0) {
4384 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10004385 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004386 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
4387 sector_nr = raid5_size(mddev, 0, 0)
4388 - conf->reshape_progress;
NeilBrown2c810cd2012-05-21 09:27:00 +10004389 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11004390 conf->reshape_progress > 0)
4391 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004392 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004393 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11004394 mddev->curr_resync_completed = sector_nr;
4395 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownfef9c612009-03-31 15:16:46 +11004396 *skipped = 1;
4397 return sector_nr;
4398 }
NeilBrown52c03292006-06-26 00:27:43 -07004399 }
4400
NeilBrown7a661382009-03-31 15:21:40 +11004401 /* We need to process a full chunk at a time.
4402 * If old and new chunk sizes differ, we need to process the
4403 * largest of these
4404 */
Andre Noll664e7c42009-06-18 08:45:27 +10004405 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
4406 reshape_sectors = mddev->new_chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004407 else
Andre Noll9d8f0362009-06-18 08:45:01 +10004408 reshape_sectors = mddev->chunk_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004409
NeilBrownb5254dd2012-05-21 09:27:01 +10004410 /* We update the metadata at least every 10 seconds, or when
4411 * the data about to be copied would over-write the source of
4412 * the data at the front of the range. i.e. one new_stripe
4413 * along from reshape_progress new_maps to after where
4414 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07004415 */
NeilBrownfef9c612009-03-31 15:16:46 +11004416 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08004417 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11004418 readpos = conf->reshape_progress;
4419 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11004420 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08004421 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10004422 if (mddev->reshape_backwards) {
NeilBrowned37d832009-05-27 21:39:05 +10004423 writepos -= min_t(sector_t, reshape_sectors, writepos);
NeilBrownc8f517c2009-03-31 15:28:40 +11004424 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11004425 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11004426 } else {
NeilBrown7a661382009-03-31 15:21:40 +11004427 writepos += reshape_sectors;
NeilBrowned37d832009-05-27 21:39:05 +10004428 readpos -= min_t(sector_t, reshape_sectors, readpos);
4429 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11004430 }
NeilBrown52c03292006-06-26 00:27:43 -07004431
NeilBrownb5254dd2012-05-21 09:27:01 +10004432 /* Having calculated the 'writepos' possibly use it
4433 * to set 'stripe_addr' which is where we will write to.
4434 */
4435 if (mddev->reshape_backwards) {
4436 BUG_ON(conf->reshape_progress == 0);
4437 stripe_addr = writepos;
4438 BUG_ON((mddev->dev_sectors &
4439 ~((sector_t)reshape_sectors - 1))
4440 - reshape_sectors - stripe_addr
4441 != sector_nr);
4442 } else {
4443 BUG_ON(writepos != sector_nr + reshape_sectors);
4444 stripe_addr = sector_nr;
4445 }
4446
NeilBrownc8f517c2009-03-31 15:28:40 +11004447 /* 'writepos' is the most advanced device address we might write.
4448 * 'readpos' is the least advanced device address we might read.
4449 * 'safepos' is the least address recorded in the metadata as having
4450 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10004451 * If there is a min_offset_diff, these are adjusted either by
4452 * increasing the safepos/readpos if diff is negative, or
4453 * increasing writepos if diff is positive.
4454 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11004455 * ensure safety in the face of a crash - that must be done by userspace
4456 * making a backup of the data. So in that case there is no particular
4457 * rush to update metadata.
4458 * Otherwise if 'safepos' is behind 'writepos', then we really need to
4459 * update the metadata to advance 'safepos' to match 'readpos' so that
4460 * we can be safe in the event of a crash.
4461 * So we insist on updating metadata if safepos is behind writepos and
4462 * readpos is beyond writepos.
4463 * In any case, update the metadata every 10 seconds.
4464 * Maybe that number should be configurable, but I'm not sure it is
4465 * worth it.... maybe it could be a multiple of safemode_delay???
4466 */
NeilBrownb5254dd2012-05-21 09:27:01 +10004467 if (conf->min_offset_diff < 0) {
4468 safepos += -conf->min_offset_diff;
4469 readpos += -conf->min_offset_diff;
4470 } else
4471 writepos += conf->min_offset_diff;
4472
NeilBrown2c810cd2012-05-21 09:27:00 +10004473 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11004474 ? (safepos > writepos && readpos < writepos)
4475 : (safepos < writepos && readpos > writepos)) ||
4476 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07004477 /* Cannot proceed until we've updated the superblock... */
4478 wait_event(conf->wait_for_overlap,
4479 atomic_read(&conf->reshape_stripes)==0);
NeilBrownfef9c612009-03-31 15:16:46 +11004480 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004481 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004482 conf->reshape_checkpoint = jiffies;
NeilBrown850b2b42006-10-03 01:15:46 -07004483 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown52c03292006-06-26 00:27:43 -07004484 md_wakeup_thread(mddev->thread);
NeilBrown850b2b42006-10-03 01:15:46 -07004485 wait_event(mddev->sb_wait, mddev->flags == 0 ||
NeilBrown52c03292006-06-26 00:27:43 -07004486 kthread_should_stop());
4487 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004488 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07004489 spin_unlock_irq(&conf->device_lock);
4490 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004491 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown52c03292006-06-26 00:27:43 -07004492 }
4493
NeilBrownab69ae12009-03-31 15:26:47 +11004494 INIT_LIST_HEAD(&stripes);
NeilBrown7a661382009-03-31 15:21:40 +11004495 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
NeilBrown52c03292006-06-26 00:27:43 -07004496 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10004497 int skipped_disk = 0;
NeilBrowna8c906c2009-06-09 14:39:59 +10004498 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004499 set_bit(STRIPE_EXPANDING, &sh->state);
4500 atomic_inc(&conf->reshape_stripes);
4501 /* If any of this stripe is beyond the end of the old
4502 * array, then we need to zero those blocks
4503 */
4504 for (j=sh->disks; j--;) {
4505 sector_t s;
4506 if (j == sh->pd_idx)
4507 continue;
NeilBrownf4168852007-02-28 20:11:53 -08004508 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11004509 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08004510 continue;
NeilBrown784052e2009-03-31 15:19:07 +11004511 s = compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11004512 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10004513 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07004514 continue;
4515 }
4516 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4517 set_bit(R5_Expanded, &sh->dev[j].flags);
4518 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4519 }
NeilBrowna9f326e2009-09-23 18:06:41 +10004520 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07004521 set_bit(STRIPE_EXPAND_READY, &sh->state);
4522 set_bit(STRIPE_HANDLE, &sh->state);
4523 }
NeilBrownab69ae12009-03-31 15:26:47 +11004524 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07004525 }
4526 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10004527 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11004528 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11004529 else
NeilBrown7a661382009-03-31 15:21:40 +11004530 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07004531 spin_unlock_irq(&conf->device_lock);
4532 /* Ok, those stripe are ready. We can start scheduling
4533 * reads on the source stripes.
4534 * The source stripes are determined by mapping the first and last
4535 * block on the destination stripes.
4536 */
NeilBrown52c03292006-06-26 00:27:43 -07004537 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11004538 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11004539 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07004540 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10004541 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10004542 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11004543 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11004544 if (last_sector >= mddev->dev_sectors)
4545 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07004546 while (first_sector <= last_sector) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004547 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07004548 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4549 set_bit(STRIPE_HANDLE, &sh->state);
4550 release_stripe(sh);
4551 first_sector += STRIPE_SECTORS;
4552 }
NeilBrownab69ae12009-03-31 15:26:47 +11004553 /* Now that the sources are clearly marked, we can release
4554 * the destination stripes
4555 */
4556 while (!list_empty(&stripes)) {
4557 sh = list_entry(stripes.next, struct stripe_head, lru);
4558 list_del_init(&sh->lru);
4559 release_stripe(sh);
4560 }
NeilBrownc6207272008-02-06 01:39:52 -08004561 /* If this takes us to the resync_max point where we have to pause,
4562 * then we need to write out the superblock.
4563 */
NeilBrown7a661382009-03-31 15:21:40 +11004564 sector_nr += reshape_sectors;
NeilBrownc03f6a12009-04-17 11:06:30 +10004565 if ((sector_nr - mddev->curr_resync_completed) * 2
4566 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08004567 /* Cannot proceed until we've updated the superblock... */
4568 wait_event(conf->wait_for_overlap,
4569 atomic_read(&conf->reshape_stripes) == 0);
NeilBrownfef9c612009-03-31 15:16:46 +11004570 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11004571 mddev->curr_resync_completed = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11004572 conf->reshape_checkpoint = jiffies;
NeilBrownc6207272008-02-06 01:39:52 -08004573 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4574 md_wakeup_thread(mddev->thread);
4575 wait_event(mddev->sb_wait,
4576 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4577 || kthread_should_stop());
4578 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11004579 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08004580 spin_unlock_irq(&conf->device_lock);
4581 wake_up(&conf->wait_for_overlap);
NeilBrownacb180b2009-04-14 16:28:34 +10004582 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrownc6207272008-02-06 01:39:52 -08004583 }
NeilBrown7a661382009-03-31 15:21:40 +11004584 return reshape_sectors;
NeilBrown52c03292006-06-26 00:27:43 -07004585}
4586
4587/* FIXME go_faster isn't used */
NeilBrownfd01b882011-10-11 16:47:53 +11004588static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
NeilBrown52c03292006-06-26 00:27:43 -07004589{
NeilBrownd1688a62011-10-11 16:49:52 +11004590 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07004591 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11004592 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11004593 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07004594 int still_degraded = 0;
4595 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
NeilBrown72626682005-09-09 16:23:54 -07004597 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11004599
NeilBrown29269552006-03-27 01:18:10 -08004600 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4601 end_reshape(conf);
4602 return 0;
4603 }
NeilBrown72626682005-09-09 16:23:54 -07004604
4605 if (mddev->curr_resync < max_sector) /* aborted */
4606 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4607 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07004608 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07004609 conf->fullsync = 0;
4610 bitmap_close_sync(mddev->bitmap);
4611
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 return 0;
4613 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08004614
NeilBrown64bd6602009-08-03 10:59:58 +10004615 /* Allow raid5_quiesce to complete */
4616 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4617
NeilBrown52c03292006-06-26 00:27:43 -07004618 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4619 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08004620
NeilBrownc6207272008-02-06 01:39:52 -08004621 /* No need to check resync_max as we never do more than one
4622 * stripe, and as resync_max will always be on a chunk boundary,
4623 * if the check in md_do_sync didn't fire, there is no chance
4624 * of overstepping resync_max here
4625 */
4626
NeilBrown16a53ec2006-06-26 00:27:38 -07004627 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 * to resync, then assert that we are finished, because there is
4629 * nothing we can do.
4630 */
NeilBrown3285edf2006-06-26 00:27:55 -07004631 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07004632 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004633 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07004634 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 return rv;
4636 }
NeilBrown72626682005-09-09 16:23:54 -07004637 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
NeilBrown3855ad92005-11-08 21:39:38 -08004638 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
NeilBrown72626682005-09-09 16:23:54 -07004639 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4640 /* we can skip this block, and probably more */
4641 sync_blocks /= STRIPE_SECTORS;
4642 *skipped = 1;
4643 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
NeilBrownb47490c2008-02-06 01:39:50 -08004646 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4647
NeilBrowna8c906c2009-06-09 14:39:59 +10004648 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 if (sh == NULL) {
NeilBrowna8c906c2009-06-09 14:39:59 +10004650 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07004652 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08004654 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004656 /* Need to check if array will still be degraded after recovery/resync
4657 * We don't need to check the 'failed' flag as when that gets set,
4658 * recovery aborts.
4659 */
NeilBrownf001a702009-06-09 14:30:31 +10004660 for (i = 0; i < conf->raid_disks; i++)
NeilBrown16a53ec2006-06-26 00:27:38 -07004661 if (conf->disks[i].rdev == NULL)
4662 still_degraded = 1;
4663
4664 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4665
NeilBrown83206d62011-07-26 11:19:49 +10004666 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
NeilBrown14425772009-10-16 15:55:25 +11004668 handle_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 release_stripe(sh);
4670
4671 return STRIPE_SECTORS;
4672}
4673
NeilBrownd1688a62011-10-11 16:49:52 +11004674static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004675{
4676 /* We may not be able to submit a whole bio at once as there
4677 * may not be enough stripe_heads available.
4678 * We cannot pre-allocate enough stripe_heads as we may need
4679 * more than exist in the cache (if we allow ever large chunks).
4680 * So we do one stripe head at a time and record in
4681 * ->bi_hw_segments how many have been done.
4682 *
4683 * We *know* that this entire raid_bio is in one chunk, so
4684 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4685 */
4686 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11004687 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004688 sector_t sector, logical_sector, last_sector;
4689 int scnt = 0;
4690 int remaining;
4691 int handled = 0;
4692
4693 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
NeilBrown112bf892009-03-31 14:39:38 +11004694 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11004695 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004696 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4697
4698 for (; logical_sector < last_sector;
Neil Brown387bb172007-02-08 14:20:29 -08004699 logical_sector += STRIPE_SECTORS,
4700 sector += STRIPE_SECTORS,
4701 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004702
Shaohua Lie7836bd62012-07-19 16:01:31 +10004703 if (scnt < raid5_bi_processed_stripes(raid_bio))
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004704 /* already done this stripe */
4705 continue;
4706
NeilBrowna8c906c2009-06-09 14:39:59 +10004707 sh = get_active_stripe(conf, sector, 0, 1, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004708
4709 if (!sh) {
4710 /* failed to get a stripe - must wait */
Shaohua Lie7836bd62012-07-19 16:01:31 +10004711 raid5_set_bi_processed_stripes(raid_bio, scnt);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004712 conf->retry_read_aligned = raid_bio;
4713 return handled;
4714 }
4715
Neil Brown387bb172007-02-08 14:20:29 -08004716 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4717 release_stripe(sh);
Shaohua Lie7836bd62012-07-19 16:01:31 +10004718 raid5_set_bi_processed_stripes(raid_bio, scnt);
Neil Brown387bb172007-02-08 14:20:29 -08004719 conf->retry_read_aligned = raid_bio;
4720 return handled;
4721 }
4722
majianpeng3f9e7c12012-07-31 10:04:21 +10004723 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07004724 handle_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004725 release_stripe(sh);
4726 handled++;
4727 }
Shaohua Lie7836bd62012-07-19 16:01:31 +10004728 remaining = raid5_dec_bi_active_stripes(raid_bio);
Neil Brown0e13fe232008-06-28 08:31:20 +10004729 if (remaining == 0)
4730 bio_endio(raid_bio, 0);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004731 if (atomic_dec_and_test(&conf->active_aligned_reads))
4732 wake_up(&conf->wait_for_stripe);
4733 return handled;
4734}
4735
Shaohua Li46a06402012-08-02 08:33:15 +10004736#define MAX_STRIPE_BATCH 8
4737static int handle_active_stripes(struct r5conf *conf)
4738{
4739 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
4740 int i, batch_size = 0;
4741
4742 while (batch_size < MAX_STRIPE_BATCH &&
4743 (sh = __get_priority_stripe(conf)) != NULL)
4744 batch[batch_size++] = sh;
4745
4746 if (batch_size == 0)
4747 return batch_size;
4748 spin_unlock_irq(&conf->device_lock);
4749
4750 for (i = 0; i < batch_size; i++)
4751 handle_stripe(batch[i]);
4752
4753 cond_resched();
4754
4755 spin_lock_irq(&conf->device_lock);
4756 for (i = 0; i < batch_size; i++)
4757 __release_stripe(conf, batch[i]);
4758 return batch_size;
4759}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004760
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761/*
4762 * This is our raid5 kernel thread.
4763 *
4764 * We scan the hash table for stripes which can be handled now.
4765 * During the scan, completed stripes are saved for us by the interrupt
4766 * handler, so that they will not have to wait for our next wakeup.
4767 */
Shaohua Li4ed87312012-10-11 13:34:00 +11004768static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769{
Shaohua Li4ed87312012-10-11 13:34:00 +11004770 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11004771 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004773 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Dan Williams45b42332007-07-09 11:56:43 -07004775 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
4777 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004779 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 handled = 0;
4781 spin_lock_irq(&conf->device_lock);
4782 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004783 struct bio *bio;
Shaohua Li46a06402012-08-02 08:33:15 +10004784 int batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
NeilBrown0021b7b2012-07-31 09:08:14 +02004786 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10004787 !list_empty(&conf->bitmap_list)) {
4788 /* Now is a good time to flush some bitmap updates */
4789 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08004790 spin_unlock_irq(&conf->device_lock);
NeilBrown72626682005-09-09 16:23:54 -07004791 bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08004792 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10004793 conf->seq_write = conf->seq_flush;
NeilBrown72626682005-09-09 16:23:54 -07004794 activate_bit_delay(conf);
4795 }
NeilBrown0021b7b2012-07-31 09:08:14 +02004796 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07004797
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08004798 while ((bio = remove_bio_from_retry(conf))) {
4799 int ok;
4800 spin_unlock_irq(&conf->device_lock);
4801 ok = retry_aligned_read(conf, bio);
4802 spin_lock_irq(&conf->device_lock);
4803 if (!ok)
4804 break;
4805 handled++;
4806 }
4807
Shaohua Li46a06402012-08-02 08:33:15 +10004808 batch_size = handle_active_stripes(conf);
4809 if (!batch_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 break;
Shaohua Li46a06402012-08-02 08:33:15 +10004811 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
Shaohua Li46a06402012-08-02 08:33:15 +10004813 if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) {
4814 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10004815 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10004816 spin_lock_irq(&conf->device_lock);
4817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 }
Dan Williams45b42332007-07-09 11:56:43 -07004819 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
4821 spin_unlock_irq(&conf->device_lock);
4822
Dan Williamsc9f21aa2008-07-23 12:05:51 -07004823 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10004824 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
Dan Williams45b42332007-07-09 11:56:43 -07004826 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827}
4828
NeilBrown3f294f42005-11-08 21:39:25 -08004829static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004830raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004831{
NeilBrownd1688a62011-10-11 16:49:52 +11004832 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004833 if (conf)
4834 return sprintf(page, "%d\n", conf->max_nr_stripes);
4835 else
4836 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004837}
4838
NeilBrownc41d4ac2010-06-01 19:37:24 +10004839int
NeilBrownfd01b882011-10-11 16:47:53 +11004840raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10004841{
NeilBrownd1688a62011-10-11 16:49:52 +11004842 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004843 int err;
4844
4845 if (size <= 16 || size > 32768)
4846 return -EINVAL;
4847 while (size < conf->max_nr_stripes) {
4848 if (drop_one_stripe(conf))
4849 conf->max_nr_stripes--;
4850 else
4851 break;
4852 }
4853 err = md_allow_write(mddev);
4854 if (err)
4855 return err;
4856 while (size > conf->max_nr_stripes) {
4857 if (grow_one_stripe(conf))
4858 conf->max_nr_stripes++;
4859 else break;
4860 }
4861 return 0;
4862}
4863EXPORT_SYMBOL(raid5_set_cache_size);
4864
NeilBrown3f294f42005-11-08 21:39:25 -08004865static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004866raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08004867{
NeilBrownd1688a62011-10-11 16:49:52 +11004868 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004869 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07004870 int err;
4871
NeilBrown3f294f42005-11-08 21:39:25 -08004872 if (len >= PAGE_SIZE)
4873 return -EINVAL;
NeilBrown96de1e62005-11-08 21:39:39 -08004874 if (!conf)
4875 return -ENODEV;
NeilBrown3f294f42005-11-08 21:39:25 -08004876
Dan Williams4ef197d82008-04-28 02:15:54 -07004877 if (strict_strtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08004878 return -EINVAL;
NeilBrownc41d4ac2010-06-01 19:37:24 +10004879 err = raid5_set_cache_size(mddev, new);
Dan Williamsb5470dc2008-06-27 21:44:04 -07004880 if (err)
4881 return err;
NeilBrown3f294f42005-11-08 21:39:25 -08004882 return len;
4883}
NeilBrown007583c2005-11-08 21:39:30 -08004884
NeilBrown96de1e62005-11-08 21:39:39 -08004885static struct md_sysfs_entry
4886raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4887 raid5_show_stripe_cache_size,
4888 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08004889
4890static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004891raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004892{
NeilBrownd1688a62011-10-11 16:49:52 +11004893 struct r5conf *conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004894 if (conf)
4895 return sprintf(page, "%d\n", conf->bypass_threshold);
4896 else
4897 return 0;
4898}
4899
4900static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004901raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004902{
NeilBrownd1688a62011-10-11 16:49:52 +11004903 struct r5conf *conf = mddev->private;
Dan Williams4ef197d82008-04-28 02:15:54 -07004904 unsigned long new;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004905 if (len >= PAGE_SIZE)
4906 return -EINVAL;
4907 if (!conf)
4908 return -ENODEV;
4909
Dan Williams4ef197d82008-04-28 02:15:54 -07004910 if (strict_strtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004911 return -EINVAL;
Dan Williams4ef197d82008-04-28 02:15:54 -07004912 if (new > conf->max_nr_stripes)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004913 return -EINVAL;
4914 conf->bypass_threshold = new;
4915 return len;
4916}
4917
4918static struct md_sysfs_entry
4919raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4920 S_IRUGO | S_IWUSR,
4921 raid5_show_preread_threshold,
4922 raid5_store_preread_threshold);
4923
4924static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004925stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08004926{
NeilBrownd1688a62011-10-11 16:49:52 +11004927 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08004928 if (conf)
4929 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4930 else
4931 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08004932}
4933
NeilBrown96de1e62005-11-08 21:39:39 -08004934static struct md_sysfs_entry
4935raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08004936
NeilBrown007583c2005-11-08 21:39:30 -08004937static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08004938 &raid5_stripecache_size.attr,
4939 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07004940 &raid5_preread_bypass_threshold.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08004941 NULL,
4942};
NeilBrown007583c2005-11-08 21:39:30 -08004943static struct attribute_group raid5_attrs_group = {
4944 .name = NULL,
4945 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08004946};
4947
Dan Williams80c3a6c2009-03-17 18:10:40 -07004948static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11004949raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07004950{
NeilBrownd1688a62011-10-11 16:49:52 +11004951 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07004952
4953 if (!sectors)
4954 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11004955 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11004956 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11004957 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004958
Andre Noll9d8f0362009-06-18 08:45:01 +10004959 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
Andre Noll664e7c42009-06-18 08:45:27 +10004960 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07004961 return sectors * (raid_disks - conf->max_degraded);
4962}
4963
NeilBrownd1688a62011-10-11 16:49:52 +11004964static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07004965{
4966 struct raid5_percpu *percpu;
4967 unsigned long cpu;
4968
4969 if (!conf->percpu)
4970 return;
4971
4972 get_online_cpus();
4973 for_each_possible_cpu(cpu) {
4974 percpu = per_cpu_ptr(conf->percpu, cpu);
4975 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07004976 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07004977 }
4978#ifdef CONFIG_HOTPLUG_CPU
4979 unregister_cpu_notifier(&conf->cpu_notify);
4980#endif
4981 put_online_cpus();
4982
4983 free_percpu(conf->percpu);
4984}
4985
NeilBrownd1688a62011-10-11 16:49:52 +11004986static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10004987{
4988 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07004989 raid5_free_percpu(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10004990 kfree(conf->disks);
4991 kfree(conf->stripe_hashtbl);
4992 kfree(conf);
4993}
4994
Dan Williams36d1c642009-07-14 11:48:22 -07004995#ifdef CONFIG_HOTPLUG_CPU
4996static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4997 void *hcpu)
4998{
NeilBrownd1688a62011-10-11 16:49:52 +11004999 struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify);
Dan Williams36d1c642009-07-14 11:48:22 -07005000 long cpu = (long)hcpu;
5001 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
5002
5003 switch (action) {
5004 case CPU_UP_PREPARE:
5005 case CPU_UP_PREPARE_FROZEN:
Dan Williamsd6f38f32009-07-14 11:50:52 -07005006 if (conf->level == 6 && !percpu->spare_page)
Dan Williams36d1c642009-07-14 11:48:22 -07005007 percpu->spare_page = alloc_page(GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005008 if (!percpu->scribble)
5009 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
5010
5011 if (!percpu->scribble ||
5012 (conf->level == 6 && !percpu->spare_page)) {
5013 safe_put_page(percpu->spare_page);
5014 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005015 pr_err("%s: failed memory allocation for cpu%ld\n",
5016 __func__, cpu);
Akinobu Mita55af6bb2010-05-26 14:43:35 -07005017 return notifier_from_errno(-ENOMEM);
Dan Williams36d1c642009-07-14 11:48:22 -07005018 }
5019 break;
5020 case CPU_DEAD:
5021 case CPU_DEAD_FROZEN:
5022 safe_put_page(percpu->spare_page);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005023 kfree(percpu->scribble);
Dan Williams36d1c642009-07-14 11:48:22 -07005024 percpu->spare_page = NULL;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005025 percpu->scribble = NULL;
Dan Williams36d1c642009-07-14 11:48:22 -07005026 break;
5027 default:
5028 break;
5029 }
5030 return NOTIFY_OK;
5031}
5032#endif
5033
NeilBrownd1688a62011-10-11 16:49:52 +11005034static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07005035{
5036 unsigned long cpu;
5037 struct page *spare_page;
Tejun Heoa29d8b82010-02-02 14:39:15 +09005038 struct raid5_percpu __percpu *allcpus;
Dan Williamsd6f38f32009-07-14 11:50:52 -07005039 void *scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005040 int err;
5041
Dan Williams36d1c642009-07-14 11:48:22 -07005042 allcpus = alloc_percpu(struct raid5_percpu);
5043 if (!allcpus)
5044 return -ENOMEM;
5045 conf->percpu = allcpus;
5046
5047 get_online_cpus();
5048 err = 0;
5049 for_each_present_cpu(cpu) {
Dan Williamsd6f38f32009-07-14 11:50:52 -07005050 if (conf->level == 6) {
5051 spare_page = alloc_page(GFP_KERNEL);
5052 if (!spare_page) {
5053 err = -ENOMEM;
5054 break;
5055 }
5056 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
5057 }
NeilBrown5e5e3e72009-10-16 16:35:30 +11005058 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
Dan Williamsd6f38f32009-07-14 11:50:52 -07005059 if (!scribble) {
Dan Williams36d1c642009-07-14 11:48:22 -07005060 err = -ENOMEM;
5061 break;
5062 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07005063 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
Dan Williams36d1c642009-07-14 11:48:22 -07005064 }
5065#ifdef CONFIG_HOTPLUG_CPU
5066 conf->cpu_notify.notifier_call = raid456_cpu_notify;
5067 conf->cpu_notify.priority = 0;
5068 if (err == 0)
5069 err = register_cpu_notifier(&conf->cpu_notify);
5070#endif
5071 put_online_cpus();
5072
5073 return err;
5074}
5075
NeilBrownd1688a62011-10-11 16:49:52 +11005076static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077{
NeilBrownd1688a62011-10-11 16:49:52 +11005078 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005079 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11005080 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10005082 char pers_name[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
NeilBrown91adb562009-03-31 14:39:39 +11005084 if (mddev->new_level != 5
5085 && mddev->new_level != 4
5086 && mddev->new_level != 6) {
NeilBrown0c55e022010-05-03 14:09:02 +10005087 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005088 mdname(mddev), mddev->new_level);
5089 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 }
NeilBrown91adb562009-03-31 14:39:39 +11005091 if ((mddev->new_level == 5
5092 && !algorithm_valid_raid5(mddev->new_layout)) ||
5093 (mddev->new_level == 6
5094 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005095 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
NeilBrown91adb562009-03-31 14:39:39 +11005096 mdname(mddev), mddev->new_layout);
5097 return ERR_PTR(-EIO);
5098 }
5099 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrown0c55e022010-05-03 14:09:02 +10005100 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
NeilBrown91adb562009-03-31 14:39:39 +11005101 mdname(mddev), mddev->raid_disks);
5102 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11005103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104
Andre Noll664e7c42009-06-18 08:45:27 +10005105 if (!mddev->new_chunk_sectors ||
5106 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
5107 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005108 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
5109 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11005110 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11005111 }
5112
NeilBrownd1688a62011-10-11 16:49:52 +11005113 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11005114 if (conf == NULL)
5115 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11005116 spin_lock_init(&conf->device_lock);
5117 init_waitqueue_head(&conf->wait_for_stripe);
5118 init_waitqueue_head(&conf->wait_for_overlap);
5119 INIT_LIST_HEAD(&conf->handle_list);
5120 INIT_LIST_HEAD(&conf->hold_list);
5121 INIT_LIST_HEAD(&conf->delayed_list);
5122 INIT_LIST_HEAD(&conf->bitmap_list);
5123 INIT_LIST_HEAD(&conf->inactive_list);
5124 atomic_set(&conf->active_stripes, 0);
5125 atomic_set(&conf->preread_active_stripes, 0);
5126 atomic_set(&conf->active_aligned_reads, 0);
5127 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11005128 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11005129
5130 conf->raid_disks = mddev->raid_disks;
5131 if (mddev->reshape_position == MaxSector)
5132 conf->previous_raid_disks = mddev->raid_disks;
5133 else
5134 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005135 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
5136 conf->scribble_len = scribble_len(max_disks);
NeilBrown91adb562009-03-31 14:39:39 +11005137
NeilBrown5e5e3e72009-10-16 16:35:30 +11005138 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11005139 GFP_KERNEL);
5140 if (!conf->disks)
5141 goto abort;
5142
5143 conf->mddev = mddev;
5144
5145 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
5146 goto abort;
5147
Dan Williams36d1c642009-07-14 11:48:22 -07005148 conf->level = mddev->new_level;
5149 if (raid5_alloc_percpu(conf) != 0)
5150 goto abort;
5151
NeilBrown0c55e022010-05-03 14:09:02 +10005152 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11005153
NeilBrowndafb20f2012-03-19 12:46:39 +11005154 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11005155 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11005156 if (raid_disk >= max_disks
NeilBrown91adb562009-03-31 14:39:39 +11005157 || raid_disk < 0)
5158 continue;
5159 disk = conf->disks + raid_disk;
5160
NeilBrown17045f52011-12-23 10:17:53 +11005161 if (test_bit(Replacement, &rdev->flags)) {
5162 if (disk->replacement)
5163 goto abort;
5164 disk->replacement = rdev;
5165 } else {
5166 if (disk->rdev)
5167 goto abort;
5168 disk->rdev = rdev;
5169 }
NeilBrown91adb562009-03-31 14:39:39 +11005170
5171 if (test_bit(In_sync, &rdev->flags)) {
5172 char b[BDEVNAME_SIZE];
NeilBrown0c55e022010-05-03 14:09:02 +10005173 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
5174 " disk %d\n",
5175 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05005176 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11005177 /* Cannot rely on bitmap to complete recovery */
5178 conf->fullsync = 1;
5179 }
5180
Andre Noll09c9e5f2009-06-18 08:45:55 +10005181 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown91adb562009-03-31 14:39:39 +11005182 conf->level = mddev->new_level;
5183 if (conf->level == 6)
5184 conf->max_degraded = 2;
5185 else
5186 conf->max_degraded = 1;
5187 conf->algorithm = mddev->new_layout;
5188 conf->max_nr_stripes = NR_STRIPES;
NeilBrownfef9c612009-03-31 15:16:46 +11005189 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11005190 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10005191 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11005192 conf->prev_algo = mddev->layout;
5193 }
NeilBrown91adb562009-03-31 14:39:39 +11005194
5195 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11005196 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
NeilBrown91adb562009-03-31 14:39:39 +11005197 if (grow_stripes(conf, conf->max_nr_stripes)) {
5198 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005199 "md/raid:%s: couldn't allocate %dkB for buffers\n",
5200 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005201 goto abort;
5202 } else
NeilBrown0c55e022010-05-03 14:09:02 +10005203 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
5204 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11005205
NeilBrown02326052012-07-03 15:56:52 +10005206 sprintf(pers_name, "raid%d", mddev->new_level);
5207 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11005208 if (!conf->thread) {
5209 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005210 "md/raid:%s: couldn't allocate thread.\n",
NeilBrown91adb562009-03-31 14:39:39 +11005211 mdname(mddev));
5212 goto abort;
5213 }
5214
5215 return conf;
5216
5217 abort:
5218 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10005219 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11005220 return ERR_PTR(-EIO);
5221 } else
5222 return ERR_PTR(-ENOMEM);
5223}
5224
NeilBrownc148ffd2009-11-13 17:47:00 +11005225
5226static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
5227{
5228 switch (algo) {
5229 case ALGORITHM_PARITY_0:
5230 if (raid_disk < max_degraded)
5231 return 1;
5232 break;
5233 case ALGORITHM_PARITY_N:
5234 if (raid_disk >= raid_disks - max_degraded)
5235 return 1;
5236 break;
5237 case ALGORITHM_PARITY_0_6:
5238 if (raid_disk == 0 ||
5239 raid_disk == raid_disks - 1)
5240 return 1;
5241 break;
5242 case ALGORITHM_LEFT_ASYMMETRIC_6:
5243 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5244 case ALGORITHM_LEFT_SYMMETRIC_6:
5245 case ALGORITHM_RIGHT_SYMMETRIC_6:
5246 if (raid_disk == raid_disks - 1)
5247 return 1;
5248 }
5249 return 0;
5250}
5251
NeilBrownfd01b882011-10-11 16:47:53 +11005252static int run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11005253{
NeilBrownd1688a62011-10-11 16:49:52 +11005254 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10005255 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11005256 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11005257 struct md_rdev *rdev;
NeilBrownc148ffd2009-11-13 17:47:00 +11005258 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11005259 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10005260 long long min_offset_diff = 0;
5261 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11005262
Andre Noll8c6ac862009-06-18 08:48:06 +10005263 if (mddev->recovery_cp != MaxSector)
NeilBrown0c55e022010-05-03 14:09:02 +10005264 printk(KERN_NOTICE "md/raid:%s: not clean"
Andre Noll8c6ac862009-06-18 08:48:06 +10005265 " -- starting background reconstruction\n",
5266 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10005267
5268 rdev_for_each(rdev, mddev) {
5269 long long diff;
5270 if (rdev->raid_disk < 0)
5271 continue;
5272 diff = (rdev->new_data_offset - rdev->data_offset);
5273 if (first) {
5274 min_offset_diff = diff;
5275 first = 0;
5276 } else if (mddev->reshape_backwards &&
5277 diff < min_offset_diff)
5278 min_offset_diff = diff;
5279 else if (!mddev->reshape_backwards &&
5280 diff > min_offset_diff)
5281 min_offset_diff = diff;
5282 }
5283
NeilBrownf6705572006-03-27 01:18:11 -08005284 if (mddev->reshape_position != MaxSector) {
5285 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10005286 * Difficulties arise if the stripe we would write to
5287 * next is at or after the stripe we would read from next.
5288 * For a reshape that changes the number of devices, this
5289 * is only possible for a very short time, and mdadm makes
5290 * sure that time appears to have past before assembling
5291 * the array. So we fail if that time hasn't passed.
5292 * For a reshape that keeps the number of devices the same
5293 * mdadm must be monitoring the reshape can keeping the
5294 * critical areas read-only and backed up. It will start
5295 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08005296 */
5297 sector_t here_new, here_old;
5298 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11005299 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrownf6705572006-03-27 01:18:11 -08005300
NeilBrown88ce4932009-03-31 15:24:23 +11005301 if (mddev->new_level != mddev->level) {
NeilBrown0c55e022010-05-03 14:09:02 +10005302 printk(KERN_ERR "md/raid:%s: unsupported reshape "
NeilBrownf4168852007-02-28 20:11:53 -08005303 "required - aborting.\n",
NeilBrownf6705572006-03-27 01:18:11 -08005304 mdname(mddev));
5305 return -EINVAL;
5306 }
NeilBrownf6705572006-03-27 01:18:11 -08005307 old_disks = mddev->raid_disks - mddev->delta_disks;
5308 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08005309 * further up in new geometry must map after here in old
5310 * geometry.
NeilBrownf6705572006-03-27 01:18:11 -08005311 */
5312 here_new = mddev->reshape_position;
Andre Noll664e7c42009-06-18 08:45:27 +10005313 if (sector_div(here_new, mddev->new_chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005314 (mddev->raid_disks - max_degraded))) {
NeilBrown0c55e022010-05-03 14:09:02 +10005315 printk(KERN_ERR "md/raid:%s: reshape_position not "
5316 "on a stripe boundary\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005317 return -EINVAL;
5318 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005319 reshape_offset = here_new * mddev->new_chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005320 /* here_new is the stripe we will write to */
5321 here_old = mddev->reshape_position;
Andre Noll9d8f0362009-06-18 08:45:01 +10005322 sector_div(here_old, mddev->chunk_sectors *
NeilBrownf4168852007-02-28 20:11:53 -08005323 (old_disks-max_degraded));
5324 /* here_old is the first stripe that we might need to read
5325 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10005326 if (mddev->delta_disks == 0) {
NeilBrownb5254dd2012-05-21 09:27:01 +10005327 if ((here_new * mddev->new_chunk_sectors !=
5328 here_old * mddev->chunk_sectors)) {
5329 printk(KERN_ERR "md/raid:%s: reshape position is"
5330 " confused - aborting\n", mdname(mddev));
5331 return -EINVAL;
5332 }
NeilBrown67ac6012009-08-13 10:06:24 +10005333 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10005334 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10005335 * and taking constant backups.
5336 * mdadm always starts a situation like this in
5337 * readonly mode so it can take control before
5338 * allowing any writes. So just check for that.
5339 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005340 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
5341 abs(min_offset_diff) >= mddev->new_chunk_sectors)
5342 /* not really in-place - so OK */;
5343 else if (mddev->ro == 0) {
5344 printk(KERN_ERR "md/raid:%s: in-place reshape "
5345 "must be started in read-only mode "
5346 "- aborting\n",
NeilBrown0c55e022010-05-03 14:09:02 +10005347 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10005348 return -EINVAL;
5349 }
NeilBrown2c810cd2012-05-21 09:27:00 +10005350 } else if (mddev->reshape_backwards
NeilBrownb5254dd2012-05-21 09:27:01 +10005351 ? (here_new * mddev->new_chunk_sectors + min_offset_diff <=
NeilBrown67ac6012009-08-13 10:06:24 +10005352 here_old * mddev->chunk_sectors)
5353 : (here_new * mddev->new_chunk_sectors >=
NeilBrownb5254dd2012-05-21 09:27:01 +10005354 here_old * mddev->chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08005355 /* Reading from the same stripe as writing to - bad */
NeilBrown0c55e022010-05-03 14:09:02 +10005356 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
5357 "auto-recovery - aborting.\n",
5358 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005359 return -EINVAL;
5360 }
NeilBrown0c55e022010-05-03 14:09:02 +10005361 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
5362 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08005363 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08005364 } else {
NeilBrown91adb562009-03-31 14:39:39 +11005365 BUG_ON(mddev->level != mddev->new_level);
5366 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10005367 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11005368 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08005369 }
5370
NeilBrown245f46c2009-03-31 14:39:39 +11005371 if (mddev->private == NULL)
5372 conf = setup_conf(mddev);
5373 else
5374 conf = mddev->private;
5375
NeilBrown91adb562009-03-31 14:39:39 +11005376 if (IS_ERR(conf))
5377 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08005378
NeilBrownb5254dd2012-05-21 09:27:01 +10005379 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11005380 mddev->thread = conf->thread;
5381 conf->thread = NULL;
5382 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383
NeilBrown17045f52011-12-23 10:17:53 +11005384 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
5385 i++) {
5386 rdev = conf->disks[i].rdev;
5387 if (!rdev && conf->disks[i].replacement) {
5388 /* The replacement is all we have yet */
5389 rdev = conf->disks[i].replacement;
5390 conf->disks[i].replacement = NULL;
5391 clear_bit(Replacement, &rdev->flags);
5392 conf->disks[i].rdev = rdev;
5393 }
5394 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11005395 continue;
NeilBrown17045f52011-12-23 10:17:53 +11005396 if (conf->disks[i].replacement &&
5397 conf->reshape_progress != MaxSector) {
5398 /* replacements and reshape simply do not mix. */
5399 printk(KERN_ERR "md: cannot handle concurrent "
5400 "replacement and reshape.\n");
5401 goto abort;
5402 }
NeilBrown2f115882010-06-17 17:41:03 +10005403 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11005404 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10005405 continue;
5406 }
NeilBrownc148ffd2009-11-13 17:47:00 +11005407 /* This disc is not fully in-sync. However if it
5408 * just stored parity (beyond the recovery_offset),
5409 * when we don't need to be concerned about the
5410 * array being dirty.
5411 * When reshape goes 'backwards', we never have
5412 * partially completed devices, so we only need
5413 * to worry about reshape going forwards.
5414 */
5415 /* Hack because v0.91 doesn't store recovery_offset properly. */
5416 if (mddev->major_version == 0 &&
5417 mddev->minor_version > 90)
5418 rdev->recovery_offset = reshape_offset;
5419
NeilBrownc148ffd2009-11-13 17:47:00 +11005420 if (rdev->recovery_offset < reshape_offset) {
5421 /* We need to check old and new layout */
5422 if (!only_parity(rdev->raid_disk,
5423 conf->algorithm,
5424 conf->raid_disks,
5425 conf->max_degraded))
5426 continue;
5427 }
5428 if (!only_parity(rdev->raid_disk,
5429 conf->prev_algo,
5430 conf->previous_raid_disks,
5431 conf->max_degraded))
5432 continue;
5433 dirty_parity_disks++;
5434 }
NeilBrown91adb562009-03-31 14:39:39 +11005435
NeilBrown17045f52011-12-23 10:17:53 +11005436 /*
5437 * 0 for a fully functional array, 1 or 2 for a degraded array.
5438 */
NeilBrown908f4fb2011-12-23 10:17:50 +11005439 mddev->degraded = calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440
NeilBrown674806d2010-06-16 17:17:53 +10005441 if (has_failed(conf)) {
NeilBrown0c55e022010-05-03 14:09:02 +10005442 printk(KERN_ERR "md/raid:%s: not enough operational devices"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 " (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07005444 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 goto abort;
5446 }
5447
NeilBrown91adb562009-03-31 14:39:39 +11005448 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10005449 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11005450 mddev->resync_max_sectors = mddev->dev_sectors;
5451
NeilBrownc148ffd2009-11-13 17:47:00 +11005452 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 mddev->recovery_cp != MaxSector) {
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005454 if (mddev->ok_start_degraded)
5455 printk(KERN_WARNING
NeilBrown0c55e022010-05-03 14:09:02 +10005456 "md/raid:%s: starting dirty degraded array"
5457 " - data corruption possible.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005458 mdname(mddev));
5459 else {
5460 printk(KERN_ERR
NeilBrown0c55e022010-05-03 14:09:02 +10005461 "md/raid:%s: cannot start dirty degraded array.\n",
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005462 mdname(mddev));
5463 goto abort;
5464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 }
5466
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 if (mddev->degraded == 0)
NeilBrown0c55e022010-05-03 14:09:02 +10005468 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
5469 " devices, algorithm %d\n", mdname(mddev), conf->level,
NeilBrowne183eae2009-03-31 15:20:22 +11005470 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
5471 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 else
NeilBrown0c55e022010-05-03 14:09:02 +10005473 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
5474 " out of %d devices, algorithm %d\n",
5475 mdname(mddev), conf->level,
5476 mddev->raid_disks - mddev->degraded,
5477 mddev->raid_disks, mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478
5479 print_raid5_conf(conf);
5480
NeilBrownfef9c612009-03-31 15:16:46 +11005481 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11005482 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08005483 atomic_set(&conf->reshape_stripes, 0);
5484 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5485 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5486 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5487 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5488 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005489 "reshape");
NeilBrownf6705572006-03-27 01:18:11 -08005490 }
5491
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492
5493 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10005494 if (mddev->to_remove == &raid5_attrs_group)
5495 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005496 else if (mddev->kobj.sd &&
5497 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08005498 printk(KERN_WARNING
NeilBrown4a5add42010-06-01 19:37:28 +10005499 "raid5: failed to create sysfs attributes for %s\n",
NeilBrown5e55e2f2007-03-26 21:32:14 -08005500 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10005501 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5502
5503 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005504 int chunk_size;
Shaohua Li620125f2012-10-11 13:49:05 +11005505 bool discard_supported = true;
NeilBrown4a5add42010-06-01 19:37:28 +10005506 /* read-ahead size must cover two whole stripes, which
5507 * is 2 * (datadisks) * chunksize where 'n' is the
5508 * number of raid devices
5509 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 int data_disks = conf->previous_raid_disks - conf->max_degraded;
5511 int stripe = data_disks *
5512 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
5513 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5514 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
NeilBrown4a5add42010-06-01 19:37:28 +10005515
5516 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005517
5518 mddev->queue->backing_dev_info.congested_data = mddev;
5519 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
NeilBrown9f7c2222010-07-26 12:04:13 +10005520
5521 chunk_size = mddev->chunk_sectors << 9;
5522 blk_queue_io_min(mddev->queue, chunk_size);
5523 blk_queue_io_opt(mddev->queue, chunk_size *
5524 (conf->raid_disks - conf->max_degraded));
Shaohua Li620125f2012-10-11 13:49:05 +11005525 /*
5526 * We can only discard a whole stripe. It doesn't make sense to
5527 * discard data disk but write parity disk
5528 */
5529 stripe = stripe * PAGE_SIZE;
5530 mddev->queue->limits.discard_alignment = stripe;
5531 mddev->queue->limits.discard_granularity = stripe;
5532 /*
5533 * unaligned part of discard request will be ignored, so can't
5534 * guarantee discard_zerors_data
5535 */
5536 mddev->queue->limits.discard_zeroes_data = 0;
NeilBrown9f7c2222010-07-26 12:04:13 +10005537
NeilBrown05616be2012-05-21 09:27:00 +10005538 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10005539 disk_stack_limits(mddev->gendisk, rdev->bdev,
5540 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10005541 disk_stack_limits(mddev->gendisk, rdev->bdev,
5542 rdev->new_data_offset << 9);
Shaohua Li620125f2012-10-11 13:49:05 +11005543 /*
5544 * discard_zeroes_data is required, otherwise data
5545 * could be lost. Consider a scenario: discard a stripe
5546 * (the stripe could be inconsistent if
5547 * discard_zeroes_data is 0); write one disk of the
5548 * stripe (the stripe could be inconsistent again
5549 * depending on which disks are used to calculate
5550 * parity); the disk is broken; The stripe data of this
5551 * disk is lost.
5552 */
5553 if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) ||
5554 !bdev_get_queue(rdev->bdev)->
5555 limits.discard_zeroes_data)
5556 discard_supported = false;
NeilBrown05616be2012-05-21 09:27:00 +10005557 }
Shaohua Li620125f2012-10-11 13:49:05 +11005558
5559 if (discard_supported &&
5560 mddev->queue->limits.max_discard_sectors >= stripe &&
5561 mddev->queue->limits.discard_granularity >= stripe)
5562 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
5563 mddev->queue);
5564 else
5565 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
5566 mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 }
5568
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 return 0;
5570abort:
NeilBrown01f96c02011-09-21 15:30:20 +10005571 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11005572 print_raid5_conf(conf);
5573 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 mddev->private = NULL;
NeilBrown0c55e022010-05-03 14:09:02 +10005575 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 return -EIO;
5577}
5578
NeilBrownfd01b882011-10-11 16:47:53 +11005579static int stop(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580{
NeilBrownd1688a62011-10-11 16:49:52 +11005581 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
NeilBrown01f96c02011-09-21 15:30:20 +10005583 md_unregister_thread(&mddev->thread);
NeilBrown11d8a6e2010-07-26 11:57:07 +10005584 if (mddev->queue)
5585 mddev->queue->backing_dev_info.congested_fn = NULL;
Dan Williams95fc17a2009-07-31 12:39:15 +10005586 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10005587 mddev->private = NULL;
5588 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 return 0;
5590}
5591
NeilBrownfd01b882011-10-11 16:47:53 +11005592static void status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593{
NeilBrownd1688a62011-10-11 16:49:52 +11005594 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 int i;
5596
Andre Noll9d8f0362009-06-18 08:45:01 +10005597 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5598 mddev->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07005599 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 for (i = 0; i < conf->raid_disks; i++)
5601 seq_printf (seq, "%s",
5602 conf->disks[i].rdev &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005603 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605}
5606
NeilBrownd1688a62011-10-11 16:49:52 +11005607static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608{
5609 int i;
5610 struct disk_info *tmp;
5611
NeilBrown0c55e022010-05-03 14:09:02 +10005612 printk(KERN_DEBUG "RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 if (!conf) {
5614 printk("(conf==NULL)\n");
5615 return;
5616 }
NeilBrown0c55e022010-05-03 14:09:02 +10005617 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5618 conf->raid_disks,
5619 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620
5621 for (i = 0; i < conf->raid_disks; i++) {
5622 char b[BDEVNAME_SIZE];
5623 tmp = conf->disks + i;
5624 if (tmp->rdev)
NeilBrown0c55e022010-05-03 14:09:02 +10005625 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5626 i, !test_bit(Faulty, &tmp->rdev->flags),
5627 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 }
5629}
5630
NeilBrownfd01b882011-10-11 16:47:53 +11005631static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632{
5633 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11005634 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10005636 int count = 0;
5637 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638
5639 for (i = 0; i < conf->raid_disks; i++) {
5640 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11005641 if (tmp->replacement
5642 && tmp->replacement->recovery_offset == MaxSector
5643 && !test_bit(Faulty, &tmp->replacement->flags)
5644 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
5645 /* Replacement has just become active. */
5646 if (!tmp->rdev
5647 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
5648 count++;
5649 if (tmp->rdev) {
5650 /* Replaced device not technically faulty,
5651 * but we need to be sure it gets removed
5652 * and never re-added.
5653 */
5654 set_bit(Faulty, &tmp->rdev->flags);
5655 sysfs_notify_dirent_safe(
5656 tmp->rdev->sysfs_state);
5657 }
5658 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
5659 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10005660 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08005661 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07005662 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10005663 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11005664 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 }
5666 }
NeilBrown6b965622010-08-18 11:56:59 +10005667 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005668 mddev->degraded = calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005669 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10005671 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672}
5673
NeilBrownb8321b62011-12-23 10:17:51 +11005674static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675{
NeilBrownd1688a62011-10-11 16:49:52 +11005676 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11005678 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11005679 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 struct disk_info *p = conf->disks + number;
5681
5682 print_raid5_conf(conf);
NeilBrown657e3e42011-12-23 10:17:52 +11005683 if (rdev == p->rdev)
5684 rdevp = &p->rdev;
5685 else if (rdev == p->replacement)
5686 rdevp = &p->replacement;
5687 else
5688 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11005689
NeilBrown657e3e42011-12-23 10:17:52 +11005690 if (number >= conf->raid_disks &&
5691 conf->reshape_progress == MaxSector)
5692 clear_bit(In_sync, &rdev->flags);
5693
5694 if (test_bit(In_sync, &rdev->flags) ||
5695 atomic_read(&rdev->nr_pending)) {
5696 err = -EBUSY;
5697 goto abort;
5698 }
5699 /* Only remove non-faulty devices if recovery
5700 * isn't possible.
5701 */
5702 if (!test_bit(Faulty, &rdev->flags) &&
5703 mddev->recovery_disabled != conf->recovery_disabled &&
5704 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11005705 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11005706 number < conf->raid_disks) {
5707 err = -EBUSY;
5708 goto abort;
5709 }
5710 *rdevp = NULL;
5711 synchronize_rcu();
5712 if (atomic_read(&rdev->nr_pending)) {
5713 /* lost the race, try later */
5714 err = -EBUSY;
5715 *rdevp = rdev;
NeilBrowndd054fc2011-12-23 10:17:53 +11005716 } else if (p->replacement) {
5717 /* We must have just cleared 'rdev' */
5718 p->rdev = p->replacement;
5719 clear_bit(Replacement, &p->replacement->flags);
5720 smp_mb(); /* Make sure other CPUs may see both as identical
5721 * but will never see neither - if they are careful
5722 */
5723 p->replacement = NULL;
5724 clear_bit(WantReplacement, &rdev->flags);
5725 } else
5726 /* We might have just removed the Replacement as faulty-
5727 * clear the bit just in case
5728 */
5729 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730abort:
5731
5732 print_raid5_conf(conf);
5733 return err;
5734}
5735
NeilBrownfd01b882011-10-11 16:47:53 +11005736static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737{
NeilBrownd1688a62011-10-11 16:49:52 +11005738 struct r5conf *conf = mddev->private;
Neil Brown199050e2008-06-28 08:31:33 +10005739 int err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 int disk;
5741 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10005742 int first = 0;
5743 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744
NeilBrown7f0da592011-07-28 11:39:22 +10005745 if (mddev->recovery_disabled == conf->recovery_disabled)
5746 return -EBUSY;
5747
NeilBrowndc10c642012-03-19 12:46:37 +11005748 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10005750 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751
Neil Brown6c2fce22008-06-28 08:31:31 +10005752 if (rdev->raid_disk >= 0)
5753 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754
5755 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07005756 * find the disk ... but prefer rdev->saved_raid_disk
5757 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 */
NeilBrown16a53ec2006-06-26 00:27:38 -07005759 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10005760 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07005761 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10005762 first = rdev->saved_raid_disk;
5763
5764 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11005765 p = conf->disks + disk;
5766 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08005767 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 rdev->raid_disk = disk;
Neil Brown199050e2008-06-28 08:31:33 +10005769 err = 0;
NeilBrown72626682005-09-09 16:23:54 -07005770 if (rdev->saved_raid_disk != disk)
5771 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08005772 rcu_assign_pointer(p->rdev, rdev);
NeilBrown5cfb22a2012-07-03 11:46:53 +10005773 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005775 }
5776 for (disk = first; disk <= last; disk++) {
5777 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11005778 if (test_bit(WantReplacement, &p->rdev->flags) &&
5779 p->replacement == NULL) {
5780 clear_bit(In_sync, &rdev->flags);
5781 set_bit(Replacement, &rdev->flags);
5782 rdev->raid_disk = disk;
5783 err = 0;
5784 conf->fullsync = 1;
5785 rcu_assign_pointer(p->replacement, rdev);
5786 break;
5787 }
5788 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10005789out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10005791 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792}
5793
NeilBrownfd01b882011-10-11 16:47:53 +11005794static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795{
5796 /* no resync is happening, and there is enough space
5797 * on all devices, so we can resize.
5798 * We need to make sure resync covers any new space.
5799 * If the array is shrinking we should possibly wait until
5800 * any io in the removed space completes, but it hardly seems
5801 * worth it.
5802 */
NeilBrowna4a61252012-05-22 13:55:27 +10005803 sector_t newsize;
Andre Noll9d8f0362009-06-18 08:45:01 +10005804 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10005805 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
5806 if (mddev->external_size &&
5807 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11005808 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10005809 if (mddev->bitmap) {
5810 int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0);
5811 if (ret)
5812 return ret;
5813 }
5814 md_set_array_sectors(mddev, newsize);
Andre Nollf233ea52008-07-21 17:05:22 +10005815 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005816 revalidate_disk(mddev->gendisk);
NeilBrownb0986362011-05-11 15:52:21 +10005817 if (sectors > mddev->dev_sectors &&
5818 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11005819 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5821 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005822 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07005823 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 return 0;
5825}
5826
NeilBrownfd01b882011-10-11 16:47:53 +11005827static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10005828{
5829 /* Can only proceed if there are plenty of stripe_heads.
5830 * We need a minimum of one full stripe,, and for sensible progress
5831 * it is best to have about 4 times that.
5832 * If we require 4 times, then the default 256 4K stripe_heads will
5833 * allow for chunk sizes up to 256K, which is probably OK.
5834 * If the chunk size is greater, user-space should request more
5835 * stripe_heads first.
5836 */
NeilBrownd1688a62011-10-11 16:49:52 +11005837 struct r5conf *conf = mddev->private;
NeilBrown01ee22b2009-06-18 08:47:20 +10005838 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5839 > conf->max_nr_stripes ||
5840 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5841 > conf->max_nr_stripes) {
NeilBrown0c55e022010-05-03 14:09:02 +10005842 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5843 mdname(mddev),
NeilBrown01ee22b2009-06-18 08:47:20 +10005844 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5845 / STRIPE_SIZE)*4);
5846 return 0;
5847 }
5848 return 1;
5849}
5850
NeilBrownfd01b882011-10-11 16:47:53 +11005851static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08005852{
NeilBrownd1688a62011-10-11 16:49:52 +11005853 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08005854
NeilBrown88ce4932009-03-31 15:24:23 +11005855 if (mddev->delta_disks == 0 &&
5856 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10005857 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10005858 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10005859 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11005860 return -EINVAL;
5861 if (mddev->delta_disks < 0) {
5862 /* We might be able to shrink, but the devices must
5863 * be made bigger first.
5864 * For raid6, 4 is the minimum size.
5865 * Otherwise 2 is the minimum
5866 */
5867 int min = 2;
5868 if (mddev->level == 6)
5869 min = 4;
5870 if (mddev->raid_disks + mddev->delta_disks < min)
5871 return -EINVAL;
5872 }
NeilBrown29269552006-03-27 01:18:10 -08005873
NeilBrown01ee22b2009-06-18 08:47:20 +10005874 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08005875 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08005876
NeilBrowne56108d62012-10-11 14:24:13 +11005877 return resize_stripes(conf, (conf->previous_raid_disks
5878 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08005879}
5880
NeilBrownfd01b882011-10-11 16:47:53 +11005881static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08005882{
NeilBrownd1688a62011-10-11 16:49:52 +11005883 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11005884 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08005885 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07005886 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08005887
NeilBrownf4168852007-02-28 20:11:53 -08005888 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08005889 return -EBUSY;
5890
NeilBrown01ee22b2009-06-18 08:47:20 +10005891 if (!check_stripe_cache(mddev))
5892 return -ENOSPC;
5893
NeilBrown30b67642012-05-22 13:55:28 +10005894 if (has_failed(conf))
5895 return -EINVAL;
5896
NeilBrownc6563a82012-05-21 09:27:00 +10005897 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11005898 if (!test_bit(In_sync, &rdev->flags)
5899 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08005900 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10005901 }
NeilBrown63c70c42006-03-27 01:18:13 -08005902
NeilBrownf4168852007-02-28 20:11:53 -08005903 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08005904 /* Not enough devices even to make a degraded array
5905 * of that size
5906 */
5907 return -EINVAL;
5908
NeilBrownec32a2b2009-03-31 15:17:38 +11005909 /* Refuse to reduce size of the array. Any reductions in
5910 * array size must be through explicit setting of array_size
5911 * attribute.
5912 */
5913 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5914 < mddev->array_sectors) {
NeilBrown0c55e022010-05-03 14:09:02 +10005915 printk(KERN_ERR "md/raid:%s: array size must be reduced "
NeilBrownec32a2b2009-03-31 15:17:38 +11005916 "before number of disks\n", mdname(mddev));
5917 return -EINVAL;
5918 }
5919
NeilBrownf6705572006-03-27 01:18:11 -08005920 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08005921 spin_lock_irq(&conf->device_lock);
5922 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08005923 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10005924 conf->prev_chunk_sectors = conf->chunk_sectors;
5925 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11005926 conf->prev_algo = conf->algorithm;
5927 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10005928 conf->generation++;
5929 /* Code that selects data_offset needs to see the generation update
5930 * if reshape_progress has been set - so a memory barrier needed.
5931 */
5932 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10005933 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11005934 conf->reshape_progress = raid5_size(mddev, 0, 0);
5935 else
5936 conf->reshape_progress = 0;
5937 conf->reshape_safe = conf->reshape_progress;
NeilBrown29269552006-03-27 01:18:10 -08005938 spin_unlock_irq(&conf->device_lock);
5939
5940 /* Add some new drives, as many as will fit.
5941 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10005942 * Don't add devices if we are reducing the number of
5943 * devices in the array. This is because it is not possible
5944 * to correctly record the "partially reconstructed" state of
5945 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08005946 */
NeilBrown87a8dec2011-01-31 11:57:43 +11005947 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11005948 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11005949 if (rdev->raid_disk < 0 &&
5950 !test_bit(Faulty, &rdev->flags)) {
5951 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11005952 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11005953 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11005954 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11005955 else
NeilBrown87a8dec2011-01-31 11:57:43 +11005956 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10005957
5958 if (sysfs_link_rdev(mddev, rdev))
NeilBrown87a8dec2011-01-31 11:57:43 +11005959 /* Failure here is OK */;
NeilBrown50da0842011-01-31 11:57:43 +11005960 }
NeilBrown87a8dec2011-01-31 11:57:43 +11005961 } else if (rdev->raid_disk >= conf->previous_raid_disks
5962 && !test_bit(Faulty, &rdev->flags)) {
5963 /* This is a spare that was manually added */
5964 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11005965 }
NeilBrown29269552006-03-27 01:18:10 -08005966
NeilBrown87a8dec2011-01-31 11:57:43 +11005967 /* When a reshape changes the number of devices,
5968 * ->degraded is measured against the larger of the
5969 * pre and post number of devices.
5970 */
NeilBrownec32a2b2009-03-31 15:17:38 +11005971 spin_lock_irqsave(&conf->device_lock, flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11005972 mddev->degraded = calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11005973 spin_unlock_irqrestore(&conf->device_lock, flags);
5974 }
NeilBrown63c70c42006-03-27 01:18:13 -08005975 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10005976 mddev->reshape_position = conf->reshape_progress;
NeilBrown850b2b42006-10-03 01:15:46 -07005977 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownf6705572006-03-27 01:18:11 -08005978
NeilBrown29269552006-03-27 01:18:10 -08005979 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5980 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5981 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5982 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5983 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10005984 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08005985 if (!mddev->sync_thread) {
5986 mddev->recovery = 0;
5987 spin_lock_irq(&conf->device_lock);
5988 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10005989 rdev_for_each(rdev, mddev)
5990 rdev->new_data_offset = rdev->data_offset;
5991 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11005992 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11005993 mddev->reshape_position = MaxSector;
NeilBrown29269552006-03-27 01:18:10 -08005994 spin_unlock_irq(&conf->device_lock);
5995 return -EAGAIN;
5996 }
NeilBrownc8f517c2009-03-31 15:28:40 +11005997 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08005998 md_wakeup_thread(mddev->sync_thread);
5999 md_new_event(mddev);
6000 return 0;
6001}
NeilBrown29269552006-03-27 01:18:10 -08006002
NeilBrownec32a2b2009-03-31 15:17:38 +11006003/* This is called from the reshape thread and should make any
6004 * changes needed in 'conf'
6005 */
NeilBrownd1688a62011-10-11 16:49:52 +11006006static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08006007{
NeilBrown29269552006-03-27 01:18:10 -08006008
NeilBrownf6705572006-03-27 01:18:11 -08006009 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrown05616be2012-05-21 09:27:00 +10006010 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006011
NeilBrownf6705572006-03-27 01:18:11 -08006012 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11006013 conf->previous_raid_disks = conf->raid_disks;
NeilBrown05616be2012-05-21 09:27:00 +10006014 rdev_for_each(rdev, conf->mddev)
6015 rdev->data_offset = rdev->new_data_offset;
6016 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11006017 conf->reshape_progress = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08006018 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11006019 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07006020
6021 /* read-ahead size must cover two whole stripes, which is
6022 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
6023 */
NeilBrown4a5add42010-06-01 19:37:28 +10006024 if (conf->mddev->queue) {
NeilBrowncea9c222009-03-31 15:15:05 +11006025 int data_disks = conf->raid_disks - conf->max_degraded;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006026 int stripe = data_disks * ((conf->chunk_sectors << 9)
NeilBrowncea9c222009-03-31 15:15:05 +11006027 / PAGE_SIZE);
NeilBrown16a53ec2006-06-26 00:27:38 -07006028 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
6029 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
6030 }
NeilBrown29269552006-03-27 01:18:10 -08006031 }
NeilBrown29269552006-03-27 01:18:10 -08006032}
6033
NeilBrownec32a2b2009-03-31 15:17:38 +11006034/* This is called from the raid5d thread with mddev_lock held.
6035 * It makes config changes to the device.
6036 */
NeilBrownfd01b882011-10-11 16:47:53 +11006037static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11006038{
NeilBrownd1688a62011-10-11 16:49:52 +11006039 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11006040
6041 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6042
NeilBrownec32a2b2009-03-31 15:17:38 +11006043 if (mddev->delta_disks > 0) {
6044 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
6045 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10006046 revalidate_disk(mddev->gendisk);
NeilBrownec32a2b2009-03-31 15:17:38 +11006047 } else {
6048 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11006049 spin_lock_irq(&conf->device_lock);
6050 mddev->degraded = calc_degraded(conf);
6051 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11006052 for (d = conf->raid_disks ;
6053 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10006054 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11006055 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10006056 if (rdev)
6057 clear_bit(In_sync, &rdev->flags);
6058 rdev = conf->disks[d].replacement;
6059 if (rdev)
6060 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10006061 }
NeilBrowncea9c222009-03-31 15:15:05 +11006062 }
NeilBrown88ce4932009-03-31 15:24:23 +11006063 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10006064 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11006065 mddev->reshape_position = MaxSector;
6066 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10006067 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11006068 }
6069}
6070
NeilBrownfd01b882011-10-11 16:47:53 +11006071static void raid5_quiesce(struct mddev *mddev, int state)
NeilBrown72626682005-09-09 16:23:54 -07006072{
NeilBrownd1688a62011-10-11 16:49:52 +11006073 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07006074
6075 switch(state) {
NeilBrowne464eaf2006-03-27 01:18:14 -08006076 case 2: /* resume for a suspend */
6077 wake_up(&conf->wait_for_overlap);
6078 break;
6079
NeilBrown72626682005-09-09 16:23:54 -07006080 case 1: /* stop all writes */
6081 spin_lock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006082 /* '2' tells resync/reshape to pause so that all
6083 * active stripes can drain
6084 */
6085 conf->quiesce = 2;
NeilBrown72626682005-09-09 16:23:54 -07006086 wait_event_lock_irq(conf->wait_for_stripe,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006087 atomic_read(&conf->active_stripes) == 0 &&
6088 atomic_read(&conf->active_aligned_reads) == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01006089 conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006090 conf->quiesce = 1;
NeilBrown72626682005-09-09 16:23:54 -07006091 spin_unlock_irq(&conf->device_lock);
NeilBrown64bd6602009-08-03 10:59:58 +10006092 /* allow reshape to continue */
6093 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006094 break;
6095
6096 case 0: /* re-enable writes */
6097 spin_lock_irq(&conf->device_lock);
6098 conf->quiesce = 0;
6099 wake_up(&conf->wait_for_stripe);
NeilBrowne464eaf2006-03-27 01:18:14 -08006100 wake_up(&conf->wait_for_overlap);
NeilBrown72626682005-09-09 16:23:54 -07006101 spin_unlock_irq(&conf->device_lock);
6102 break;
6103 }
NeilBrown72626682005-09-09 16:23:54 -07006104}
NeilBrownb15c2e52006-01-06 00:20:16 -08006105
NeilBrownd562b0c2009-03-31 14:39:39 +11006106
NeilBrownfd01b882011-10-11 16:47:53 +11006107static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11006108{
NeilBrowne373ab12011-10-11 16:48:59 +11006109 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07006110 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11006111
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006112 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11006113 if (raid0_conf->nr_strip_zones > 1) {
NeilBrown0c55e022010-05-03 14:09:02 +10006114 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
6115 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006116 return ERR_PTR(-EINVAL);
6117 }
6118
NeilBrowne373ab12011-10-11 16:48:59 +11006119 sectors = raid0_conf->strip_zone[0].zone_end;
6120 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10006121 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006122 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11006123 mddev->new_layout = ALGORITHM_PARITY_N;
6124 mddev->new_chunk_sectors = mddev->chunk_sectors;
6125 mddev->raid_disks += 1;
6126 mddev->delta_disks = 1;
6127 /* make sure it will be not marked as dirty */
6128 mddev->recovery_cp = MaxSector;
6129
6130 return setup_conf(mddev);
6131}
6132
6133
NeilBrownfd01b882011-10-11 16:47:53 +11006134static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006135{
6136 int chunksect;
6137
6138 if (mddev->raid_disks != 2 ||
6139 mddev->degraded > 1)
6140 return ERR_PTR(-EINVAL);
6141
6142 /* Should check if there are write-behind devices? */
6143
6144 chunksect = 64*2; /* 64K by default */
6145
6146 /* The array must be an exact multiple of chunksize */
6147 while (chunksect && (mddev->array_sectors & (chunksect-1)))
6148 chunksect >>= 1;
6149
6150 if ((chunksect<<9) < STRIPE_SIZE)
6151 /* array size does not allow a suitable chunk size */
6152 return ERR_PTR(-EINVAL);
6153
6154 mddev->new_level = 5;
6155 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10006156 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11006157
6158 return setup_conf(mddev);
6159}
6160
NeilBrownfd01b882011-10-11 16:47:53 +11006161static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11006162{
6163 int new_layout;
6164
6165 switch (mddev->layout) {
6166 case ALGORITHM_LEFT_ASYMMETRIC_6:
6167 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
6168 break;
6169 case ALGORITHM_RIGHT_ASYMMETRIC_6:
6170 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
6171 break;
6172 case ALGORITHM_LEFT_SYMMETRIC_6:
6173 new_layout = ALGORITHM_LEFT_SYMMETRIC;
6174 break;
6175 case ALGORITHM_RIGHT_SYMMETRIC_6:
6176 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
6177 break;
6178 case ALGORITHM_PARITY_0_6:
6179 new_layout = ALGORITHM_PARITY_0;
6180 break;
6181 case ALGORITHM_PARITY_N:
6182 new_layout = ALGORITHM_PARITY_N;
6183 break;
6184 default:
6185 return ERR_PTR(-EINVAL);
6186 }
6187 mddev->new_level = 5;
6188 mddev->new_layout = new_layout;
6189 mddev->delta_disks = -1;
6190 mddev->raid_disks -= 1;
6191 return setup_conf(mddev);
6192}
6193
NeilBrownd562b0c2009-03-31 14:39:39 +11006194
NeilBrownfd01b882011-10-11 16:47:53 +11006195static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11006196{
NeilBrown88ce4932009-03-31 15:24:23 +11006197 /* For a 2-drive array, the layout and chunk size can be changed
6198 * immediately as not restriping is needed.
6199 * For larger arrays we record the new value - after validation
6200 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11006201 */
NeilBrownd1688a62011-10-11 16:49:52 +11006202 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10006203 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11006204
NeilBrown597a7112009-06-18 08:47:42 +10006205 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11006206 return -EINVAL;
6207 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006208 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11006209 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006210 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11006211 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006212 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11006213 /* not factor of array size */
6214 return -EINVAL;
6215 }
6216
6217 /* They look valid */
6218
NeilBrown88ce4932009-03-31 15:24:23 +11006219 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10006220 /* can make the change immediately */
6221 if (mddev->new_layout >= 0) {
6222 conf->algorithm = mddev->new_layout;
6223 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11006224 }
6225 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10006226 conf->chunk_sectors = new_chunk ;
6227 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11006228 }
6229 set_bit(MD_CHANGE_DEVS, &mddev->flags);
6230 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11006231 }
NeilBrown50ac1682009-06-18 08:47:55 +10006232 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11006233}
6234
NeilBrownfd01b882011-10-11 16:47:53 +11006235static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11006236{
NeilBrown597a7112009-06-18 08:47:42 +10006237 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10006238
NeilBrown597a7112009-06-18 08:47:42 +10006239 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11006240 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006241 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10006242 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11006243 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006244 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11006245 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10006246 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11006247 /* not factor of array size */
6248 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11006249 }
NeilBrown88ce4932009-03-31 15:24:23 +11006250
6251 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10006252 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11006253}
6254
NeilBrownfd01b882011-10-11 16:47:53 +11006255static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11006256{
6257 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006258 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006259 * raid1 - if there are two drives. We need to know the chunk size
6260 * raid4 - trivial - just use a raid4 layout.
6261 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11006262 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006263 if (mddev->level == 0)
6264 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11006265 if (mddev->level == 1)
6266 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11006267 if (mddev->level == 4) {
6268 mddev->new_layout = ALGORITHM_PARITY_N;
6269 mddev->new_level = 5;
6270 return setup_conf(mddev);
6271 }
NeilBrownfc9739c2009-03-31 14:57:20 +11006272 if (mddev->level == 6)
6273 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11006274
6275 return ERR_PTR(-EINVAL);
6276}
6277
NeilBrownfd01b882011-10-11 16:47:53 +11006278static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11006279{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006280 /* raid4 can take over:
6281 * raid0 - if there is only one strip zone
6282 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11006283 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07006284 if (mddev->level == 0)
6285 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11006286 if (mddev->level == 5 &&
6287 mddev->layout == ALGORITHM_PARITY_N) {
6288 mddev->new_layout = 0;
6289 mddev->new_level = 4;
6290 return setup_conf(mddev);
6291 }
6292 return ERR_PTR(-EINVAL);
6293}
NeilBrownd562b0c2009-03-31 14:39:39 +11006294
NeilBrown84fc4b52011-10-11 16:49:58 +11006295static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11006296
NeilBrownfd01b882011-10-11 16:47:53 +11006297static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11006298{
6299 /* Currently can only take over a raid5. We map the
6300 * personality to an equivalent raid6 personality
6301 * with the Q block at the end.
6302 */
6303 int new_layout;
6304
6305 if (mddev->pers != &raid5_personality)
6306 return ERR_PTR(-EINVAL);
6307 if (mddev->degraded > 1)
6308 return ERR_PTR(-EINVAL);
6309 if (mddev->raid_disks > 253)
6310 return ERR_PTR(-EINVAL);
6311 if (mddev->raid_disks < 3)
6312 return ERR_PTR(-EINVAL);
6313
6314 switch (mddev->layout) {
6315 case ALGORITHM_LEFT_ASYMMETRIC:
6316 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
6317 break;
6318 case ALGORITHM_RIGHT_ASYMMETRIC:
6319 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
6320 break;
6321 case ALGORITHM_LEFT_SYMMETRIC:
6322 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
6323 break;
6324 case ALGORITHM_RIGHT_SYMMETRIC:
6325 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
6326 break;
6327 case ALGORITHM_PARITY_0:
6328 new_layout = ALGORITHM_PARITY_0_6;
6329 break;
6330 case ALGORITHM_PARITY_N:
6331 new_layout = ALGORITHM_PARITY_N;
6332 break;
6333 default:
6334 return ERR_PTR(-EINVAL);
6335 }
6336 mddev->new_level = 6;
6337 mddev->new_layout = new_layout;
6338 mddev->delta_disks = 1;
6339 mddev->raid_disks += 1;
6340 return setup_conf(mddev);
6341}
6342
6343
NeilBrown84fc4b52011-10-11 16:49:58 +11006344static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07006345{
6346 .name = "raid6",
6347 .level = 6,
6348 .owner = THIS_MODULE,
6349 .make_request = make_request,
6350 .run = run,
6351 .stop = stop,
6352 .status = status,
6353 .error_handler = error,
6354 .hot_add_disk = raid5_add_disk,
6355 .hot_remove_disk= raid5_remove_disk,
6356 .spare_active = raid5_spare_active,
6357 .sync_request = sync_request,
6358 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006359 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10006360 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08006361 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006362 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07006363 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11006364 .takeover = raid6_takeover,
NeilBrown16a53ec2006-06-26 00:27:38 -07006365};
NeilBrown84fc4b52011-10-11 16:49:58 +11006366static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367{
6368 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08006369 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 .owner = THIS_MODULE,
6371 .make_request = make_request,
6372 .run = run,
6373 .stop = stop,
6374 .status = status,
6375 .error_handler = error,
6376 .hot_add_disk = raid5_add_disk,
6377 .hot_remove_disk= raid5_remove_disk,
6378 .spare_active = raid5_spare_active,
6379 .sync_request = sync_request,
6380 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006381 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08006382 .check_reshape = raid5_check_reshape,
6383 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006384 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07006385 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11006386 .takeover = raid5_takeover,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387};
6388
NeilBrown84fc4b52011-10-11 16:49:58 +11006389static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390{
NeilBrown2604b702006-01-06 00:20:36 -08006391 .name = "raid4",
6392 .level = 4,
6393 .owner = THIS_MODULE,
6394 .make_request = make_request,
6395 .run = run,
6396 .stop = stop,
6397 .status = status,
6398 .error_handler = error,
6399 .hot_add_disk = raid5_add_disk,
6400 .hot_remove_disk= raid5_remove_disk,
6401 .spare_active = raid5_spare_active,
6402 .sync_request = sync_request,
6403 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07006404 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08006405 .check_reshape = raid5_check_reshape,
6406 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11006407 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08006408 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11006409 .takeover = raid4_takeover,
NeilBrown2604b702006-01-06 00:20:36 -08006410};
6411
6412static int __init raid5_init(void)
6413{
NeilBrown16a53ec2006-06-26 00:27:38 -07006414 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006415 register_md_personality(&raid5_personality);
6416 register_md_personality(&raid4_personality);
6417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418}
6419
NeilBrown2604b702006-01-06 00:20:36 -08006420static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421{
NeilBrown16a53ec2006-06-26 00:27:38 -07006422 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08006423 unregister_md_personality(&raid5_personality);
6424 unregister_md_personality(&raid4_personality);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425}
6426
6427module_init(raid5_init);
6428module_exit(raid5_exit);
6429MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11006430MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08006432MODULE_ALIAS("md-raid5");
6433MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08006434MODULE_ALIAS("md-level-5");
6435MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07006436MODULE_ALIAS("md-personality-8"); /* RAID6 */
6437MODULE_ALIAS("md-raid6");
6438MODULE_ALIAS("md-level-6");
6439
6440/* This used to be two separate modules, they were: */
6441MODULE_ALIAS("raid5");
6442MODULE_ALIAS("raid6");